1 /* OperatingStateFile Handler.
2 *
3 * Copyright 2018 Adiscon GmbH.
4 *
5 * This file is part of the rsyslog runtime library.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * -or-
13 * see COPYING.ASL20 in the source distribution
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include "rsyslog.h"
35 #include "errmsg.h"
36 #include "operatingstate.h"
37
38 #ifndef O_LARGEFILE
39 #define O_LARGEFILE 0
40 #endif
41 #ifndef HAVE_LSEEK64
42 # define lseek64(fd, offset, whence) lseek(fd, offset, whence)
43 #endif
44
45 /* some important standard states */
46 #define STATE_INITIALIZING "INITIALIZING"
47 #define STATE_CLEAN_CLOSE "CLEAN CLOSE"
48
49 static int fd_osf = -1;
50
51 /* check if old osf points to a problem and, if so, report */
52 static void
osf_checkOnStartup(void)53 osf_checkOnStartup(void)
54 {
55 int do_rename = 1;
56 const char *fn_osf = (const char*) glblGetOperatingStateFile();
57 char iobuf[sizeof(STATE_CLEAN_CLOSE)];
58 const int len_clean_close = sizeof(STATE_CLEAN_CLOSE) - 1;
59 assert(fn_osf != NULL);
60
61 const int fd = open(fn_osf, O_RDONLY|O_LARGEFILE|O_CLOEXEC, 0);
62 if(fd == -1) {
63 if(errno != ENOENT) {
64 LogError(errno, RS_RET_ERR, "error opening existing operatingStateFile '%s' - "
65 "this may be an indication of a problem; ignoring", fn_osf);
66 }
67 do_rename = 0;
68 goto done;
69 }
70 assert(fd != -1);
71 int offs = lseek64(fd, -(len_clean_close + 1), SEEK_END);
72 if(offs == -1){
73 LogError(errno, RS_RET_IO_ERROR, "error seeking to end of existing operatingStateFile "
74 "'%s' - this may be an indication of a problem, e.g. empty file", fn_osf);
75 goto done;
76 }
77 int rd = read(fd, iobuf, len_clean_close);
78 if(rd == -1) {
79 LogError(errno, RS_RET_IO_ERROR, "error reading existing operatingStateFile "
80 "'%s' - this probably indicates an improper shutdown", fn_osf);
81 goto done;
82 } else {
83 assert(rd <= len_clean_close);
84 iobuf[rd] = '\0';
85 if(rd != len_clean_close || strcmp(iobuf, STATE_CLEAN_CLOSE) != 0) {
86 LogError(errno, RS_RET_IO_ERROR, "existing operatingStateFile '%s' does not end "
87 "with '%s, instead it has '%s' - this probably indicates an "
88 "improper shutdown", fn_osf, STATE_CLEAN_CLOSE, iobuf);
89 goto done;
90 }
91 }
92
93 /* all ok! */
94 do_rename = 0;
95 done:
96 if(fd != -1) {
97 close(fd);
98 }
99 if(do_rename) {
100 char newname[MAXFNAME];
101 snprintf(newname, sizeof(newname)-1, "%s.previous", fn_osf);
102 newname[MAXFNAME-1] = '\0';
103 if(rename(fn_osf, newname) != 0) {
104 LogError(errno, RS_RET_IO_ERROR, "could not rename existing operatingStateFile "
105 "'%s' to '%s' - ignored, but will probably be overwritten now",
106 fn_osf, newname);
107 } else {
108 LogMsg(errno, RS_RET_OK, LOG_INFO, "existing state file '%s' renamed "
109 "to '%s' - you may want to review it", fn_osf, newname);
110 }
111 }
112 return;
113 }
114
115 void
osf_open(void)116 osf_open(void)
117 {
118 assert(fd_osf == -1);
119 const char *fn_osf = (const char*) glblGetOperatingStateFile();
120 assert(fn_osf != NULL);
121
122 osf_checkOnStartup();
123
124 fd_osf = open(fn_osf, O_WRONLY|O_CREAT|O_APPEND|O_LARGEFILE|O_CLOEXEC,
125 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
126 if(fd_osf == -1) {
127 LogError(errno, RS_RET_ERR, "error opening operatingStateFile '%s' for write - "
128 "ignoring it", fn_osf);
129 goto done;
130 }
131 assert(fd_osf != -1);
132
133 osf_write(OSF_TAG_STATE, STATE_INITIALIZING " " VERSION);
134 done:
135 return;
136 }
137
138
ATTR_NONNULL()139 void ATTR_NONNULL()
140 osf_write(const char *const tag, const char *const line)
141 {
142 char buf[1024]; /* intentionally small */
143 time_t tt;
144 ssize_t wr;
145 size_t len;
146 struct tm tm;
147
148 DBGPRINTF("osf: %s %s: ", tag, line); /* ensure everything is inside the debug log */
149
150 if(fd_osf == -1)
151 return;
152
153 time(&tt);
154 localtime_r(&tt, &tm);
155 len = snprintf(buf, sizeof(buf)-1, "%d%2.2d%2.2d-%2.2d%2.2d%2.2d: %-5.5s %s\n",
156 tm.tm_year+1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
157 tag, line);
158 if(len > sizeof(buf)-1) {
159 len = sizeof(buf)-1; /* overflow, truncate */
160 }
161 wr = write(fd_osf, buf, len);
162 // TODO: handle EINTR
163 if(wr != (ssize_t) len) {
164 LogError(errno, RS_RET_IO_ERROR, "error writing operating state file - line lost");
165 }
166 }
167
168
169 void
osf_close(void)170 osf_close(void)
171 {
172 if(fd_osf == -1)
173 return;
174 osf_write(OSF_TAG_STATE, STATE_CLEAN_CLOSE);
175 close(fd_osf);
176 }
177