| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#include "cronoutils.h" |
| 86 |
#include "getopt.h" |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
int new_log_file(const char *, const char *, mode_t, const char *, |
| 92 |
PERIODICITY, int, int, char *, size_t, time_t, time_t *); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#ifndef _WIN32 |
| 98 |
#define VERSION_MSG PACKAGE " version " VERSION "\n" |
| 99 |
#else |
| 100 |
#define VERSION_MSG "cronolog version 0.1\n" |
| 101 |
#endif |
| 102 |
|
| 103 |
|
| 104 |
#define USAGE_MSG "usage: %s [OPTIONS] logfile-spec\n" \ |
| 105 |
"\n" \ |
| 106 |
" -H NAME, --hardlink=NAME maintain a hard link from NAME to current log\n" \ |
| 107 |
" -S NAME, --symlink=NAME maintain a symbolic link from NAME to current log\n" \ |
| 108 |
" -P NAME, --prev-symlink=NAME maintain a symbolic link from NAME to previous log\n" \ |
| 109 |
" -l NAME, --link=NAME same as -S/--symlink\n" \ |
| 110 |
" -h, --help print this help, then exit\n" \ |
| 111 |
" -p PERIOD, --period=PERIOD set the rotation period explicitly\n" \ |
| 112 |
" -d DELAY, --delay=DELAY set the rotation period delay\n" \ |
| 113 |
" -o, --once-only create single output log from template (not rotated)\n" \ |
| 114 |
" -x FILE, --debug=FILE write debug messages to FILE\n" \ |
| 115 |
" ( or to standard error if FILE is \"-\")\n" \ |
| 116 |
" -a, --american American date formats\n" \ |
| 117 |
" -e, --european European date formats (default)\n" \ |
| 118 |
" -s, --start-time=TIME starting time\n" \ |
| 119 |
" -z TZ, --time-zone=TZ use TZ for timezone\n" \ |
| 120 |
" -V, --version print version number, then exit\n" |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
char *short_options = "ad:eop:s:z:H:P:S:l:hVx:"; |
| 126 |
|
| 127 |
#ifndef _WIN32 |
| 128 |
struct option long_options[] = |
| 129 |
{ |
| 130 |
{ "american", no_argument, NULL, 'a' }, |
| 131 |
{ "european", no_argument, NULL, 'e' }, |
| 132 |
{ "start-time", required_argument, NULL, 's' }, |
| 133 |
{ "time-zone", required_argument, NULL, 'z' }, |
| 134 |
{ "hardlink", required_argument, NULL, 'H' }, |
| 135 |
{ "symlink", required_argument, NULL, 'S' }, |
| 136 |
{ "prev-symlink", required_argument, NULL, 'P' }, |
| 137 |
{ "link", required_argument, NULL, 'l' }, |
| 138 |
{ "period", required_argument, NULL, 'p' }, |
| 139 |
{ "delay", required_argument, NULL, 'd' }, |
| 140 |
{ "once-only", no_argument, NULL, 'o' }, |
| 141 |
{ "help", no_argument, NULL, 'h' }, |
| •142 |
{ "version", no_argument, NULL, 'V' }, |
| 143 |
{ NULL, 0, NULL, 0 } |
| 144 |
}; |
| 145 |
#endif |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
int |
| 150 |
main(int argc, char **argv) |
| 151 |
{ |
| 152 |
PERIODICITY periodicity = UNKNOWN; |
| 153 |
PERIODICITY period_delay_units = UNKNOWN; |
| 154 |
int period_multiple = 1; |
| 155 |
int period_delay = 0; |
| 156 |
int use_american_date_formats = 0; |
| 157 |
char read_buf[BUFSIZE]; |
| 158 |
char tzbuf[BUFSIZE]; |
| 159 |
char filename[MAX_PATH]; |
| 160 |
char *start_time = NULL; |
| 161 |
char *template; |
| 162 |
char *linkname = NULL; |
| 163 |
char *prevlinkname = NULL; |
| 164 |
mode_t linktype = 0; |
| 165 |
int n_bytes_read; |
| 166 |
int ch; |
| 167 |
time_t time_now; |
| 168 |
time_t time_offset = 0; |
| 169 |
time_t next_period = 0; |
| 170 |
int log_fd = -1; |
| 171 |
|
| 172 |
#ifndef _WIN32 |
| 173 |
while ((ch = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF) |
| 174 |
#else |
| 175 |
while ((ch = getopt(argc, argv, short_options)) != EOF) |
| 176 |
#endif |
| 177 |
{ |
| 178 |
switch (ch) |
| 179 |
{ |
| 180 |
case 'a': |
| 181 |
use_american_date_formats = 1; |
| 182 |
break; |
| 183 |
|
| 184 |
case 'e': |
| 185 |
use_american_date_formats = 0; |
| 186 |
break; |
| 187 |
|
| 188 |
case 's': |
| 189 |
start_time = optarg; |
| 190 |
break; |
| 191 |
|
| 192 |
case 'z': |
| 193 |
sprintf(tzbuf, "TZ=%s", optarg); |
| 194 |
putenv(tzbuf); |
| 195 |
break; |
| 196 |
|
| 197 |
case 'H': |
| 198 |
linkname = optarg; |
| 199 |
linktype = S_IFREG; |
| 200 |
break; |
| 201 |
|
| 202 |
case 'l': |
| 203 |
case 'S': |
| 204 |
linkname = optarg; |
| 205 |
#ifndef _WIN32 |
| 206 |
linktype = S_IFLNK; |
| 207 |
#endif |
| 208 |
break; |
| 209 |
|
| 210 |
case 'P': |
| 211 |
if (linkname == NULL) |
| 212 |
{ |
| 213 |
fprintf(stderr, "A current log symlink is needed to mantain a symlink to the previous log\n", argv[0]); |
| 214 |
exit(1); |
| 215 |
} |
| 216 |
prevlinkname = optarg; |
| 217 |
break; |
| 218 |
|
| 219 |
|
| 220 |
case 'd': |
| 221 |
period_delay_units = parse_timespec(optarg, &period_delay); |
| 222 |
break; |
| 223 |
|
| 224 |
case 'p': |
| 225 |
periodicity = parse_timespec(optarg, &period_multiple); |
| 226 |
if ( (periodicity == INVALID_PERIOD) |
| 227 |
|| (periodicity == PER_SECOND) && (60 % period_multiple) |
| 228 |
|| (periodicity == PER_MINUTE) && (60 % period_multiple) |
| 229 |
|| (periodicity == HOURLY) && (24 % period_multiple) |
| 230 |
|| (periodicity == DAILY) && (period_multiple > 365) |
| 231 |
|| (periodicity == WEEKLY) && (period_multiple > 52) |
| 232 |
|| (periodicity == MONTHLY) && (12 % period_multiple)) { |
| 233 |
fprintf(stderr, "%s: invalid explicit period specification (%s)\n", argv[0], start_time); |
| 234 |
exit(1); |
| 235 |
} |
| 236 |
break; |
| 237 |
|
| 238 |
case 'o': |
| 239 |
periodicity = ONCE_ONLY; |
| 240 |
break; |
| 241 |
|
| 242 |
case 'x': |
| 243 |
if (strcmp(optarg, "-") == 0) |
| 244 |
{ |
| 245 |
debug_file = stderr; |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
debug_file = fopen(optarg, "a+"); |
| 250 |
} |
| 251 |
break; |
| 252 |
|
| 253 |
case 'V': |
| 254 |
fprintf(stderr, VERSION_MSG); |
| 255 |
exit(0); |
| 256 |
|
| 257 |
case 'h': |
| 258 |
case '?': |
| 259 |
fprintf(stderr, USAGE_MSG, argv[0]); |
| 260 |
exit(1); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
if ((argc - optind) != 1) |
| 265 |
{ |
| 266 |
fprintf(stderr, USAGE_MSG, argv[0]); |
| 267 |
exit(1); |
| 268 |
} |
| 269 |
|
| 270 |
DEBUG((VERSION_MSG "\n")); |
| 271 |
|
| 272 |
if (start_time) |
| 273 |
{ |
| 274 |
time_now = parse_time(start_time, use_american_date_formats); |
| 275 |
if (time_now == -1) |
| 276 |
{ |
| 277 |
fprintf(stderr, "%s: invalid start time (%s)\n", argv[0], start_time); |
| 278 |
exit(1); |
| 279 |
} |
| 280 |
time_offset = time_now - time(NULL); |
| 281 |
DEBUG(("Using offset of %d seconds from real time\n", time_offset)); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
template = argv[optind]; |
| 289 |
if (periodicity == UNKNOWN) |
| 290 |
{ |
| 291 |
periodicity = determine_periodicity(template); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
DEBUG(("periodicity = %d %s\n", period_multiple, periods[periodicity])); |
| 296 |
|
| 297 |
if (period_delay) { |
| 298 |
if ( (period_delay_units > periodicity) |
| 299 |
|| ( period_delay_units == periodicity |
| 300 |
&& abs(period_delay) >= period_multiple)) { |
| 301 |
fprintf(stderr, "%s: period delay cannot be larger than the rollover period\n", argv[0], start_time); |
| 302 |
exit(1); |
| 303 |
} |
| 304 |
period_delay *= period_seconds[period_delay_units]; |
| 305 |
} |
| 306 |
|
| 307 |
DEBUG(("Rotation period is per %d %s\n", period_multiple, periods[periodicity])); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
for (;;) |
| 313 |
{ |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
n_bytes_read = read(0, read_buf, sizeof read_buf); |
| 318 |
if (n_bytes_read == 0) |
| 319 |
{ |
| 320 |
exit(3); |
| 321 |
} |
| 322 |
if (errno == EINTR) |
| 323 |
{ |
| 324 |
continue; |
| 325 |
} |
| 326 |
else if (n_bytes_read < 0) |
| 327 |
{ |
| 328 |
exit(4); |
| 329 |
} |
| 330 |
|
| 331 |
time_now = time(NULL) + time_offset; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
if ((time_now >= next_period) && (log_fd >= 0)) |
| 337 |
{ |
| 338 |
close(log_fd); |
| 339 |
log_fd = -1; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
if (log_fd < 0) |
| 345 |
{ |
| 346 |
log_fd = new_log_file(template, linkname, linktype, prevlinkname, |
| 347 |
periodicity, period_multiple, period_delay, |
| 348 |
filename, sizeof (filename), time_now, &next_period); |
| 349 |
} |
| 350 |
|
| 351 |
DEBUG(("%s (%d): wrote message; next period starts at %s (%d) in %d secs\n", |
| 352 |
timestamp(time_now), time_now, |
| 353 |
timestamp(next_period), next_period, |
| 354 |
next_period - time_now)); |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
if (write(log_fd, read_buf, n_bytes_read) != n_bytes_read) |
| 359 |
{ |
| 360 |
perror(filename); |
| 361 |
exit(5); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
return 1; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
int |
| 378 |
new_log_file(const char *template, const char *linkname, mode_t linktype, const char *prevlinkname, |
| 379 |
PERIODICITY periodicity, int period_multiple, int period_delay, |
| 380 |
char *pfilename, size_t pfilename_len, |
| 381 |
time_t time_now, time_t *pnext_period) |
| 382 |
{ |
| 383 |
time_t start_of_period; |
| 384 |
struct tm *tm; |
| 385 |
int log_fd; |
| 386 |
|
| 387 |
start_of_period = start_of_this_period(time_now, periodicity, period_multiple); |
| 388 |
tm = localtime(&start_of_period); |
| 389 |
strftime(pfilename, BUFSIZE, template, tm); |
| 390 |
*pnext_period = start_of_next_period(start_of_period, periodicity, period_multiple) + period_delay; |
| 391 |
|
| 392 |
DEBUG(("%s (%d): using log file \"%s\" from %s (%d) until %s (%d) (for %d secs)\n", |
| 393 |
timestamp(time_now), time_now, pfilename, |
| 394 |
timestamp(start_of_period), start_of_period, |
| 395 |
timestamp(*pnext_period), *pnext_period, |
| 396 |
*pnext_period - time_now)); |
| 397 |
|
| 398 |
log_fd = open(pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE); |
| 399 |
|
| 400 |
#ifndef DONT_CREATE_SUBDIRS |
| 401 |
if ((log_fd < 0) && (errno == ENOENT)) |
| 402 |
{ |
| 403 |
create_subdirs(pfilename); |
| 404 |
log_fd = open(pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE); |
| 405 |
} |
| 406 |
#endif |
| 407 |
|
| 408 |
if (log_fd < 0) |
| 409 |
{ |
| 410 |
perror(pfilename); |
| 411 |
exit(2); |
| 412 |
} |
| 413 |
|
| 414 |
if (linkname) |
| 415 |
{ |
| 416 |
create_link(pfilename, linkname, linktype, prevlinkname); |
| 417 |
} |
| 418 |
return log_fd; |
| 419 |
} |