| 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 |
#include "rsconf.h" |
| 38 |
|
| 39 |
#ifndef O_LARGEFILE |
| 40 |
#define O_LARGEFILE 0 |
| 41 |
#endif |
| 42 |
#ifndef HAVE_LSEEK64 |
| 43 |
#define lseek64(fd, offset, whence) lseek(fd, offset, whence) |
| 44 |
#endif |
| 45 |
|
| 46 |
/* some important standard states */ |
| 47 |
#define STATE_INITIALIZING "INITIALIZING" |
| 48 |
#define STATE_CLEAN_CLOSE "CLEAN CLOSE" |
| 49 |
|
| 50 |
static int fd_osf = -1; |
| 51 |
|
| 52 |
/* check if old osf points to a problem and, if so, report */ |
| 53 |
static void osf_checkOnStartup(void) { |
| 54 |
int do_rename = 1; |
| 55 |
const char *fn_osf = (const char *)glblGetOperatingStateFile(loadConf); |
| 56 |
char iobuf[sizeof(STATE_CLEAN_CLOSE)]; |
| 57 |
const int len_clean_close = sizeof(STATE_CLEAN_CLOSE) - 1; |
| 58 |
assert(fn_osf != NULL); |
| 59 |
|
| 60 |
const int fd = open(fn_osf, O_RDONLY | O_LARGEFILE | O_CLOEXEC, 0); |
| 61 |
if (fd == -1) { |
| 62 |
if (errno != ENOENT) { |
| 63 |
LogError(errno, RS_RET_ERR, |
| 64 |
"error opening existing operatingStateFile '%s' - " |
| 65 |
"this may be an indication of a problem; ignoring", |
| 66 |
fn_osf); |
| 67 |
} |
| 68 |
do_rename = 0; |
| 69 |
goto done; |
| 70 |
} |
| 71 |
assert(fd != -1); |
| 72 |
int offs = lseek64(fd, -(len_clean_close + 1), SEEK_END); |
| 73 |
if (offs == -1) { |
| 74 |
LogError(errno, RS_RET_IO_ERROR, |
| 75 |
"error seeking to end of existing operatingStateFile " |
| 76 |
"'%s' - this may be an indication of a problem, e.g. empty file", |
| 77 |
fn_osf); |
| 78 |
goto done; |
| 79 |
} |
| 80 |
int rd = read(fd, iobuf, len_clean_close); |
| 81 |
if (rd == -1) { |
| 82 |
LogError(errno, RS_RET_IO_ERROR, |
| 83 |
"error reading existing operatingStateFile " |
| 84 |
"'%s' - this probably indicates an improper shutdown", |
| 85 |
fn_osf); |
| 86 |
goto done; |
| 87 |
} else { |
| 88 |
assert(rd <= len_clean_close); |
| 89 |
iobuf[rd] = '\0'; |
| 90 |
if (rd != len_clean_close || strcmp(iobuf, STATE_CLEAN_CLOSE) != 0) { |
| 91 |
LogError(errno, RS_RET_IO_ERROR, |
| 92 |
"existing operatingStateFile '%s' does not end " |
| 93 |
"with '%s, instead it has '%s' - this probably indicates an " |
| 94 |
"improper shutdown", |
| 95 |
fn_osf, STATE_CLEAN_CLOSE, iobuf); |
| 96 |
goto done; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/* all ok! */ |
| 101 |
do_rename = 0; |
| 102 |
done: |
| 103 |
if (fd != -1) { |
| 104 |
close(fd); |
| 105 |
} |
| 106 |
if (do_rename) { |
| 107 |
char newname[MAXFNAME]; |
| 108 |
snprintf(newname, sizeof(newname) - 1, "%s.previous", fn_osf); |
| 109 |
newname[MAXFNAME - 1] = '\0'; |
| 110 |
if (rename(fn_osf, newname) != 0) { |
| 111 |
LogError(errno, RS_RET_IO_ERROR, |
| 112 |
"could not rename existing operatingStateFile " |
| 113 |
"'%s' to '%s' - ignored, but will probably be overwritten now", |
| 114 |
fn_osf, newname); |
| 115 |
} else { |
| 116 |
LogMsg(errno, RS_RET_OK, LOG_INFO, |
| 117 |
"existing state file '%s' renamed " |
| 118 |
"to '%s' - you may want to review it", |
| 119 |
fn_osf, newname); |
| 120 |
} |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
void osf_open(void) { |
| 126 |
assert(fd_osf == -1); |
| 127 |
const char *fn_osf = (const char *)glblGetOperatingStateFile(loadConf); |
| 128 |
assert(fn_osf != NULL); |
| 129 |
|
| 130 |
osf_checkOnStartup(); |
| 131 |
|
| 132 |
fd_osf = |
| 133 |
open(fn_osf, O_WRONLY | O_CREAT | O_APPEND | O_LARGEFILE | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 134 |
if (fd_osf == -1) { |
| 135 |
LogError(errno, RS_RET_ERR, |
| 136 |
"error opening operatingStateFile '%s' for write - " |
| 137 |
"ignoring it", |
| 138 |
fn_osf); |
| 139 |
goto done; |
| 140 |
} |
| 141 |
assert(fd_osf != -1); |
| 142 |
|
| 143 |
osf_write(OSF_TAG_STATE, STATE_INITIALIZING " " VERSION); |
| 144 |
done: |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
void ATTR_NONNULL() osf_write(const char *const tag, const char *const line) { |
| 150 |
char buf[1024]; /* intentionally small */ |
| 151 |
time_t tt; |
| 152 |
ssize_t wr; |
| 153 |
size_t len; |
| 154 |
struct tm tm; |
| 155 |
|
| 156 |
DBGPRINTF("osf: %s %s: ", tag, line); /* ensure everything is inside the debug log */ |
| 157 |
|
| 158 |
if (fd_osf == -1) return; |
| 159 |
|
| 160 |
time(&tt); |
| 161 |
localtime_r(&tt, &tm); |
| 162 |
len = snprintf(buf, sizeof(buf) - 1, "%d%2.2d%2.2d-%2.2d%2.2d%2.2d: %-5.5s %s\n", tm.tm_year + 1900, tm.tm_mon + 1, |
| 163 |
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tag, line); |
| 164 |
if (len > sizeof(buf) - 1) { |
| 165 |
len = sizeof(buf) - 1; /* overflow, truncate */ |
| 166 |
} |
| 167 |
wr = write(fd_osf, buf, len); |
| 168 |
// TODO: handle EINTR |
| 169 |
if (wr != (ssize_t)len) { |
| 170 |
LogError(errno, RS_RET_IO_ERROR, "error writing operating state file - line lost"); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
void osf_close(void) { |
| 176 |
if (fd_osf == -1) return; |
| 177 |
osf_write(OSF_TAG_STATE, STATE_CLEAN_CLOSE); |
| 178 |
close(fd_osf); |
| 179 |
} |