[183] | 1 |
|
---|
| 2 | #include "config_xor.h"
|
---|
| 3 |
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include <string.h>
|
---|
| 6 | #include <stdlib.h>
|
---|
| 7 | #include <sys/types.h>
|
---|
| 8 | #include <sys/stat.h>
|
---|
[271] | 9 | #include <fcntl.h>
|
---|
[183] | 10 |
|
---|
[276] | 11 | #ifdef HAVE_DIRENT_H
|
---|
| 12 | #include <dirent.h>
|
---|
| 13 | #define NAMLEN(dirent) sl_strlen((dirent)->d_name)
|
---|
| 14 | #else
|
---|
| 15 | #define dirent direct
|
---|
| 16 | #define NAMLEN(dirent) (dirent)->d_namlen
|
---|
| 17 | #ifdef HAVE_SYS_NDIR_H
|
---|
| 18 | #include <sys/ndir.h>
|
---|
| 19 | #endif
|
---|
| 20 | #ifdef HAVE_SYS_DIR_H
|
---|
| 21 | #include <sys/dir.h>
|
---|
| 22 | #endif
|
---|
| 23 | #ifdef HAVE_NDIR_H
|
---|
| 24 | #include <ndir.h>
|
---|
| 25 | #endif
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
[271] | 28 | #if !defined(O_NONBLOCK)
|
---|
| 29 | #if defined(O_NDELAY)
|
---|
| 30 | #define O_NONBLOCK O_NDELAY
|
---|
| 31 | #else
|
---|
| 32 | #define O_NONBLOCK 0
|
---|
| 33 | #endif
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
[183] | 36 | #ifdef USE_LOGFILE_MONITOR
|
---|
| 37 |
|
---|
| 38 | #undef FIL__
|
---|
| 39 | #define FIL__ _("sh_log_check.c")
|
---|
| 40 |
|
---|
| 41 | /* Debian/Ubuntu: libpcre3-dev */
|
---|
[203] | 42 | #ifdef HAVE_PCRE_PCRE_H
|
---|
| 43 | #include <pcre/pcre.h>
|
---|
| 44 | #else
|
---|
[183] | 45 | #include <pcre.h>
|
---|
[203] | 46 | #endif
|
---|
[183] | 47 |
|
---|
| 48 | #include "samhain.h"
|
---|
| 49 | #include "sh_pthread.h"
|
---|
| 50 | #include "sh_utils.h"
|
---|
[265] | 51 | #include "sh_unix.h"
|
---|
[183] | 52 | #include "sh_string.h"
|
---|
| 53 | #include "sh_log_check.h"
|
---|
| 54 | #include "sh_log_evalrule.h"
|
---|
[265] | 55 | #include "sh_log_correlate.h"
|
---|
| 56 | #include "sh_log_mark.h"
|
---|
| 57 | #include "sh_log_repeat.h"
|
---|
[275] | 58 | #include "sh_extern.h"
|
---|
[183] | 59 |
|
---|
| 60 | /* List of supported logfile types, format is
|
---|
[260] | 61 | * {
|
---|
| 62 | * "TYPE_CODE",
|
---|
| 63 | * Reader_Callback_Function,
|
---|
| 64 | * Parser_Callback_function,
|
---|
| 65 | * Evaluate_Callback_Function
|
---|
| 66 | * }
|
---|
[183] | 67 | * If Reader_Callback_Function is NULL, the default (line-oriented
|
---|
| 68 | * text file) reader is used.
|
---|
| 69 | */
|
---|
| 70 | struct sh_logfile_type sh_logtypes_def[] = {
|
---|
[185] | 71 | { "SYSLOG", NULL, sh_parse_syslog, NULL },
|
---|
| 72 | { "SAMBA", sh_read_samba, sh_parse_samba, NULL },
|
---|
| 73 | { "APACHE", NULL, sh_parse_apache, sh_eval_fileinfo_apache },
|
---|
[183] | 74 | #if defined(HAVE_SYS_ACCT_H)
|
---|
[185] | 75 | { "PACCT", sh_read_pacct, sh_parse_pacct, NULL },
|
---|
[183] | 76 | #endif
|
---|
[276] | 77 | { "SHELL", sh_read_shell, sh_parse_shell, NULL },
|
---|
[183] | 78 | };
|
---|
| 79 |
|
---|
| 80 | /* -------------------------- Internal Stuff -------------------------- */
|
---|
| 81 |
|
---|
| 82 | struct logfile_record {
|
---|
| 83 | dev_t device_id;
|
---|
| 84 | ino_t inode;
|
---|
| 85 | fpos_t offset;
|
---|
| 86 | };
|
---|
| 87 |
|
---|
[276] | 88 | static int do_checkpoint_cleanup = S_FALSE;
|
---|
| 89 |
|
---|
[265] | 90 | static char * save_dir = NULL;
|
---|
[183] | 91 |
|
---|
[276] | 92 | static const char * get_save_dir(void)
|
---|
[183] | 93 | {
|
---|
[276] | 94 | int retval;
|
---|
[183] | 95 |
|
---|
| 96 | if (!save_dir)
|
---|
| 97 | {
|
---|
[276] | 98 | save_dir = sh_util_strdup(DEFAULT_DATAROOT);
|
---|
[183] | 99 |
|
---|
| 100 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 101 | retval = tf_trust_check (save_dir, SL_YESPRIV);
|
---|
| 102 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 103 |
|
---|
| 104 | if (retval != 0)
|
---|
| 105 | {
|
---|
| 106 | return(NULL);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
[276] | 109 | return save_dir;
|
---|
| 110 | }
|
---|
[183] | 111 |
|
---|
[276] | 112 | static void clean_dir()
|
---|
| 113 | {
|
---|
| 114 | DIR * dir;
|
---|
| 115 | struct dirent * entry;
|
---|
[183] | 116 |
|
---|
[276] | 117 | const char * dirpath;
|
---|
| 118 |
|
---|
| 119 | if (S_FALSE == do_checkpoint_cleanup)
|
---|
| 120 | return;
|
---|
| 121 |
|
---|
| 122 | dirpath = get_save_dir();
|
---|
| 123 |
|
---|
| 124 | if (dirpath)
|
---|
[183] | 125 | {
|
---|
[276] | 126 | dir = opendir(dirpath);
|
---|
| 127 | if (dir)
|
---|
| 128 | {
|
---|
| 129 | unsigned long a, b;
|
---|
| 130 | int retval;
|
---|
| 131 | char c;
|
---|
| 132 | size_t dlen = strlen(dirpath) + 1;
|
---|
| 133 | time_t now = time(NULL);
|
---|
| 134 |
|
---|
| 135 | while (NULL != (entry = readdir(dir)))
|
---|
| 136 | {
|
---|
| 137 | retval = sscanf(entry->d_name, "%lu_%lu%c", &a, &b, &c);
|
---|
| 138 |
|
---|
| 139 | if (2 == retval)
|
---|
| 140 | {
|
---|
| 141 | struct stat buf;
|
---|
| 142 | char * path;
|
---|
| 143 | size_t plen = strlen(entry->d_name) + 1;
|
---|
| 144 |
|
---|
| 145 | if (SL_TRUE == sl_ok_adds(plen, dlen))
|
---|
| 146 | {
|
---|
| 147 | plen += dlen;
|
---|
| 148 | path = SH_ALLOC(plen);
|
---|
| 149 | (void) sl_snprintf(path, plen, "%s/%s",
|
---|
| 150 | dirpath,entry->d_name);
|
---|
| 151 |
|
---|
| 152 | if (0 == retry_lstat(FIL__, __LINE__, path, &buf) &&
|
---|
| 153 | S_ISREG(buf.st_mode))
|
---|
| 154 | {
|
---|
| 155 | if (buf.st_mtime < now &&
|
---|
| 156 | (now - buf.st_mtime) > 2592000) /* 30 days */
|
---|
| 157 | {
|
---|
| 158 | if (0 == tf_trust_check (path, SL_YESPRIV))
|
---|
| 159 | {
|
---|
| 160 | unlink(path);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | closedir(dir);
|
---|
| 168 | }
|
---|
[183] | 169 | }
|
---|
[276] | 170 | }
|
---|
[183] | 171 |
|
---|
[276] | 172 | static char * build_path (struct sh_logfile * record)
|
---|
| 173 | {
|
---|
| 174 | size_t plen;
|
---|
| 175 | char * path = NULL;
|
---|
| 176 | const char * dir = get_save_dir();
|
---|
| 177 |
|
---|
| 178 | if (dir)
|
---|
| 179 | {
|
---|
| 180 | plen = strlen(dir);
|
---|
| 181 |
|
---|
| 182 | if (SL_TRUE == sl_ok_adds(plen, 130))
|
---|
| 183 | {
|
---|
| 184 | plen += 130; /* 64 + 64 + 2 */
|
---|
| 185 | path = SH_ALLOC(plen);
|
---|
| 186 | (void) sl_snprintf(path, plen, "%s/%lu_%lu", dir,
|
---|
| 187 | (unsigned long) record->device_id,
|
---|
| 188 | (unsigned long) record->inode);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[183] | 192 | return path;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | static void save_pos (struct sh_logfile * record)
|
---|
| 196 | {
|
---|
| 197 | char * path;
|
---|
| 198 | FILE * fd;
|
---|
[276] | 199 | mode_t mask;
|
---|
[183] | 200 | struct logfile_record save_rec;
|
---|
| 201 |
|
---|
| 202 | path = build_path(record);
|
---|
| 203 |
|
---|
| 204 | if (path)
|
---|
| 205 | {
|
---|
[265] | 206 | if (0 != sh_unix_check_piddir (path))
|
---|
| 207 | {
|
---|
| 208 | SH_FREE(path);
|
---|
| 209 | return;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[276] | 212 | mask = umask(S_IWGRP | S_IWOTH);
|
---|
[183] | 213 | fd = fopen(path, "wb");
|
---|
[276] | 214 | (void) umask(mask);
|
---|
| 215 |
|
---|
[183] | 216 | if (fd)
|
---|
| 217 | {
|
---|
| 218 | save_rec.device_id = record->device_id;
|
---|
| 219 | save_rec.inode = record->inode;
|
---|
| 220 | memcpy(&(save_rec.offset), &(record->offset), sizeof(fpos_t));
|
---|
| 221 | if (1 != fwrite(&save_rec, sizeof(struct logfile_record), 1, fd))
|
---|
| 222 | {
|
---|
[252] | 223 | (void) sl_fclose(FIL__, __LINE__, fd);
|
---|
[183] | 224 | (void) remove(path);
|
---|
| 225 | }
|
---|
| 226 | else
|
---|
| 227 | {
|
---|
[252] | 228 | (void) sl_fclose(FIL__, __LINE__, fd);
|
---|
[183] | 229 | }
|
---|
| 230 | }
|
---|
| 231 | SH_FREE(path);
|
---|
| 232 | }
|
---|
| 233 | return;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | static int read_pos (struct sh_logfile * record)
|
---|
| 237 | {
|
---|
| 238 | int retval = 0;
|
---|
| 239 | char * path;
|
---|
| 240 | FILE * fd;
|
---|
| 241 | struct logfile_record save_rec;
|
---|
| 242 |
|
---|
| 243 | path = build_path(record);
|
---|
| 244 |
|
---|
| 245 | if (path)
|
---|
| 246 | {
|
---|
| 247 | fd = fopen(path, "rb");
|
---|
| 248 | if (fd)
|
---|
| 249 | {
|
---|
| 250 | if (1 == fread(&save_rec, sizeof(struct logfile_record), 1, fd))
|
---|
| 251 | {
|
---|
| 252 | if (save_rec.device_id == record->device_id &&
|
---|
| 253 | save_rec.inode == record->inode)
|
---|
| 254 | {
|
---|
| 255 | memcpy(&(record->offset),&(save_rec.offset),sizeof(fpos_t));
|
---|
| 256 | retval = 1;
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
[252] | 259 | (void) sl_fclose(FIL__, __LINE__, fd);
|
---|
[183] | 260 | }
|
---|
| 261 | SH_FREE(path);
|
---|
| 262 | }
|
---|
| 263 | return retval;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /*@null@*/ static struct sh_logfile * sh_watched_logs = NULL;
|
---|
| 267 |
|
---|
| 268 | int sh_add_watch (const char * str)
|
---|
| 269 | {
|
---|
| 270 | char * filename;
|
---|
| 271 |
|
---|
| 272 | unsigned int i;
|
---|
| 273 | unsigned int defsize;
|
---|
| 274 | struct sh_logfile_type * log_type = NULL;
|
---|
| 275 | struct sh_logfile * thisfile;
|
---|
| 276 | struct stat buf;
|
---|
| 277 |
|
---|
| 278 | unsigned int nfields = 3; /* logtype:path[:regex] */
|
---|
| 279 | size_t lengths[3];
|
---|
| 280 | char * new = sh_util_strdup(str);
|
---|
| 281 | char ** splits = split_array(new, &nfields, ':', lengths);
|
---|
| 282 |
|
---|
| 283 | if (nfields < 2 || (lengths[0] == 0 || lengths[0] >= SH_MAX_LCODE_SIZE || lengths[1] == 0))
|
---|
| 284 | {
|
---|
| 285 | sh_string * msg = sh_string_new(0);
|
---|
| 286 | sh_string_add_from_char(msg, _("Format error: "));
|
---|
| 287 | sh_string_add_from_char(msg, str);
|
---|
| 288 |
|
---|
| 289 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 290 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 291 | sh_string_str(msg),
|
---|
| 292 | _("sh_add_watch"));
|
---|
| 293 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 294 | sh_string_destroy(&msg);
|
---|
| 295 |
|
---|
| 296 | SH_FREE(new);
|
---|
| 297 | return -2;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | defsize =
|
---|
| 301 | (unsigned int) (sizeof(sh_logtypes_def)/sizeof(struct sh_logfile_type));
|
---|
| 302 |
|
---|
| 303 | for (i = 0; i < defsize; ++i)
|
---|
| 304 | {
|
---|
| 305 | if (0 == strcmp(splits[0], sh_logtypes_def[i].code))
|
---|
| 306 | {
|
---|
| 307 | log_type = &(sh_logtypes_def[i]);
|
---|
| 308 | break;
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | if (log_type == NULL)
|
---|
| 313 | {
|
---|
| 314 | sh_string * msg = sh_string_new(0);
|
---|
| 315 | sh_string_add_from_char(msg, _("Unsupported log type: "));
|
---|
| 316 | sh_string_add_from_char(msg, splits[0]);
|
---|
| 317 |
|
---|
| 318 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 319 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 320 | sh_string_str(msg),
|
---|
| 321 | _("sh_add_watch"));
|
---|
| 322 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 323 | sh_string_destroy(&msg);
|
---|
| 324 |
|
---|
| 325 | SH_FREE(new);
|
---|
| 326 | return -3;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[275] | 329 | if (splits[1][0] != '/' && 0 != strcmp(splits[0], _("SHELL")))
|
---|
[183] | 330 | {
|
---|
| 331 | sh_string * msg = sh_string_new(0);
|
---|
| 332 | sh_string_add_from_char(msg, _("Logfile path not absolute: "));
|
---|
| 333 | sh_string_add_from_char(msg, splits[1]);
|
---|
| 334 |
|
---|
| 335 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 336 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 337 | sh_string_str(msg),
|
---|
| 338 | _("sh_add_watch"));
|
---|
| 339 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 340 | sh_string_destroy(&msg);
|
---|
| 341 |
|
---|
| 342 | SH_FREE(new);
|
---|
| 343 | return -4;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | filename = /*@i@*/sh_util_strdup(splits[1]);
|
---|
| 347 | thisfile = SH_ALLOC(sizeof(struct sh_logfile));
|
---|
| 348 |
|
---|
| 349 | thisfile->filename = filename;
|
---|
[276] | 350 | if (0 == strcmp(splits[0], _("SHELL")))
|
---|
[275] | 351 | thisfile->flags = SH_LOGFILE_NOFILE;
|
---|
| 352 | else
|
---|
| 353 | thisfile->flags = SH_LOGFILE_REWIND;
|
---|
[183] | 354 | thisfile->inode = 0;
|
---|
| 355 | thisfile->device_id = 0;
|
---|
| 356 | thisfile->fp = NULL;
|
---|
| 357 | if (log_type->get_record)
|
---|
| 358 | thisfile->get_record = log_type->get_record;
|
---|
| 359 | else
|
---|
| 360 | thisfile->get_record = sh_default_reader;
|
---|
| 361 | thisfile->parse_record = log_type->parse_record;
|
---|
| 362 |
|
---|
| 363 | /* An optional regex for parsing the file. The result
|
---|
| 364 | * 'fileinfo' should contain info about host/time position.
|
---|
| 365 | */
|
---|
| 366 | if (log_type->eval_fileinfo)
|
---|
| 367 | {
|
---|
| 368 | if (nfields == 3 && lengths[2] > 0)
|
---|
| 369 | {
|
---|
| 370 | thisfile->fileinfo = log_type->eval_fileinfo(splits[2]);
|
---|
| 371 |
|
---|
| 372 | if (thisfile->fileinfo == NULL)
|
---|
| 373 | {
|
---|
| 374 | sh_string * msg = sh_string_new(0);
|
---|
| 375 | sh_string_add_from_char(msg, _("Logfile format description not recognized: "));
|
---|
| 376 | sh_string_add_from_char(msg, splits[2]);
|
---|
| 377 |
|
---|
| 378 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 379 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 380 | sh_string_str(msg),
|
---|
| 381 | _("sh_add_watch"));
|
---|
| 382 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 383 | sh_string_destroy(&msg);
|
---|
| 384 |
|
---|
| 385 | SH_FREE(filename);
|
---|
| 386 | SH_FREE(thisfile);
|
---|
| 387 | SH_FREE(new);
|
---|
| 388 | return -1;
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
| 391 | else
|
---|
| 392 | {
|
---|
| 393 | sh_string * msg = sh_string_new(0);
|
---|
| 394 | sh_string_add_from_char(msg, _("Logfile format description missing: "));
|
---|
| 395 | sh_string_add_from_char(msg, splits[1]);
|
---|
| 396 |
|
---|
| 397 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 398 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 399 | sh_string_str(msg),
|
---|
| 400 | _("sh_add_watch"));
|
---|
| 401 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 402 | sh_string_destroy(&msg);
|
---|
| 403 |
|
---|
| 404 | SH_FREE(filename);
|
---|
| 405 | SH_FREE(thisfile);
|
---|
| 406 | SH_FREE(new);
|
---|
| 407 | return -1;
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
| 410 | else
|
---|
| 411 | {
|
---|
| 412 | thisfile->fileinfo = NULL;
|
---|
| 413 | }
|
---|
| 414 | thisfile->next = sh_watched_logs;
|
---|
| 415 |
|
---|
| 416 | /* Try reading saved offset. On success clear rewind flag.
|
---|
| 417 | */
|
---|
[275] | 418 | if ((thisfile->flags & SH_LOGFILE_NOFILE) == 0)
|
---|
[183] | 419 | {
|
---|
[275] | 420 | if (0 == stat(thisfile->filename, &buf))
|
---|
| 421 | {
|
---|
| 422 | if (S_ISREG(buf.st_mode)
|
---|
[271] | 423 | #ifdef S_ISLNK
|
---|
[275] | 424 | || S_ISLNK(buf.st_mode)
|
---|
[271] | 425 | #endif
|
---|
[275] | 426 | )
|
---|
[271] | 427 | {
|
---|
[275] | 428 | thisfile->inode = buf.st_ino;
|
---|
| 429 | thisfile->device_id = buf.st_dev;
|
---|
| 430 |
|
---|
| 431 | if (0 != read_pos(thisfile))
|
---|
| 432 | {
|
---|
| 433 | thisfile->flags &= ~SH_LOGFILE_REWIND;
|
---|
| 434 | }
|
---|
[271] | 435 | }
|
---|
[275] | 436 | else if (S_ISFIFO(buf.st_mode))
|
---|
| 437 | {
|
---|
| 438 | thisfile->inode = buf.st_ino;
|
---|
| 439 | thisfile->device_id = buf.st_dev;
|
---|
| 440 | thisfile->flags |= SH_LOGFILE_PIPE;
|
---|
| 441 | }
|
---|
[183] | 442 | }
|
---|
[275] | 443 | else
|
---|
[271] | 444 | {
|
---|
[275] | 445 | sh_string * msg = sh_string_new(0);
|
---|
| 446 | sh_string_add_from_char(msg, _("Logfile is not a regular file, link, or named pipe: "));
|
---|
| 447 | sh_string_add_from_char(msg, splits[1]);
|
---|
| 448 |
|
---|
| 449 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 450 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
| 451 | sh_string_str(msg),
|
---|
| 452 | _("sh_add_watch"));
|
---|
| 453 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 454 | sh_string_destroy(&msg);
|
---|
| 455 |
|
---|
| 456 | SH_FREE(filename);
|
---|
| 457 | SH_FREE(thisfile);
|
---|
| 458 | SH_FREE(new);
|
---|
| 459 | return -1;
|
---|
[271] | 460 | }
|
---|
[183] | 461 | }
|
---|
| 462 |
|
---|
| 463 | sh_watched_logs = thisfile;
|
---|
| 464 |
|
---|
| 465 | SH_FREE(new);
|
---|
| 466 | return 0;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | void sh_dump_watches()
|
---|
| 470 | {
|
---|
| 471 | struct sh_logfile * thisfile;
|
---|
| 472 |
|
---|
| 473 | while (sh_watched_logs)
|
---|
| 474 | {
|
---|
| 475 | thisfile = sh_watched_logs;
|
---|
| 476 | sh_watched_logs = thisfile->next;
|
---|
| 477 |
|
---|
[275] | 478 | if ((thisfile->flags & SH_LOGFILE_NOFILE) == 0 &&
|
---|
| 479 | (thisfile->flags & SH_LOGFILE_PIPE) == 0)
|
---|
[271] | 480 | {
|
---|
| 481 | save_pos(thisfile);
|
---|
| 482 | }
|
---|
[183] | 483 |
|
---|
[275] | 484 | if ((thisfile->flags & SH_LOGFILE_NOFILE) == 0)
|
---|
| 485 | {
|
---|
| 486 | if (thisfile->fp)
|
---|
| 487 | sl_fclose(FIL__, __LINE__, thisfile->fp);
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[183] | 490 | if (thisfile->filename)
|
---|
| 491 | SH_FREE(thisfile->filename);
|
---|
| 492 | SH_FREE(thisfile);
|
---|
| 493 | }
|
---|
| 494 | return;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | /* This variable is not used anywhere. It only exist
|
---|
| 498 | * to assign &new to them, which keeps gcc from
|
---|
| 499 | * putting it into a register, and avoids the 'clobbered
|
---|
| 500 | * by longjmp' warning. And no, 'volatile' proved insufficient.
|
---|
| 501 | */
|
---|
| 502 | static void * sh_dummy_thisfile = NULL;
|
---|
| 503 |
|
---|
| 504 | void sh_check_watches()
|
---|
| 505 | {
|
---|
| 506 | struct sh_logrecord * logrecord;
|
---|
| 507 | struct sh_logfile * thisfile = sh_watched_logs;
|
---|
| 508 | sh_string * record = sh_string_new(0);
|
---|
| 509 | char * tmp;
|
---|
| 510 |
|
---|
| 511 | /* Take the address to keep gcc from putting them into registers.
|
---|
| 512 | * Avoids the 'clobbered by longjmp' warning.
|
---|
| 513 | */
|
---|
| 514 | sh_dummy_thisfile = (void*) &thisfile;
|
---|
| 515 |
|
---|
| 516 | while (thisfile)
|
---|
| 517 | {
|
---|
[271] | 518 | volatile size_t count = 0;
|
---|
| 519 |
|
---|
[183] | 520 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 521 | tmp = sh_util_safe_name (thisfile->filename);
|
---|
| 522 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_CHKS,
|
---|
| 523 | tmp);
|
---|
| 524 | SH_FREE(tmp);
|
---|
| 525 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 526 |
|
---|
| 527 | for (;;) {
|
---|
| 528 |
|
---|
| 529 | record = thisfile->get_record(record, thisfile);
|
---|
| 530 |
|
---|
| 531 | if (record)
|
---|
| 532 | {
|
---|
| 533 | logrecord = thisfile->parse_record(record, thisfile->fileinfo);
|
---|
| 534 | ++count;
|
---|
| 535 |
|
---|
| 536 | if (logrecord)
|
---|
| 537 | {
|
---|
| 538 | logrecord->filename = thisfile->filename;
|
---|
[185] | 539 |
|
---|
| 540 | /* Don't report if 'init', just set file pointer
|
---|
| 541 | */
|
---|
| 542 | if (sh.flag.checkSum != SH_CHECK_INIT)
|
---|
| 543 | {
|
---|
| 544 | sh_eval_process_msg(logrecord);
|
---|
| 545 | }
|
---|
[183] | 546 |
|
---|
| 547 | if (logrecord->message)
|
---|
| 548 | sh_string_destroy(&(logrecord->message));
|
---|
| 549 | if (logrecord->host)
|
---|
| 550 | sh_string_destroy(&(logrecord->host));
|
---|
[185] | 551 | if (logrecord->timestr)
|
---|
| 552 | sh_string_destroy(&(logrecord->timestr));
|
---|
[183] | 553 | SH_FREE(logrecord);
|
---|
| 554 | }
|
---|
| 555 | }
|
---|
| 556 | else
|
---|
| 557 | {
|
---|
[185] | 558 | record = sh_string_new(0);
|
---|
[183] | 559 | break;
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 564 | tmp = sh_util_safe_name (thisfile->filename);
|
---|
| 565 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_CHKE,
|
---|
| 566 | tmp, (unsigned long)count);
|
---|
| 567 | SH_FREE(tmp);
|
---|
| 568 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 569 |
|
---|
| 570 | thisfile = thisfile->next;
|
---|
| 571 | }
|
---|
| 572 | sh_string_destroy(&record);
|
---|
| 573 | return;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | /********************************************************
|
---|
| 577 | * Search rotated logfile
|
---|
| 578 | */
|
---|
| 579 | #include <unistd.h>
|
---|
| 580 | #include <libgen.h>
|
---|
| 581 | #include <dirent.h>
|
---|
| 582 |
|
---|
| 583 | char * sh_rotated_log_search(const char * path, struct stat * buf)
|
---|
| 584 | {
|
---|
| 585 |
|
---|
| 586 | size_t size;
|
---|
| 587 | int i;
|
---|
| 588 | char * searchpath;
|
---|
| 589 | struct stat sbuf;
|
---|
| 590 | DIR * dp;
|
---|
| 591 | char * dname;
|
---|
| 592 | char * bname;
|
---|
| 593 |
|
---|
| 594 | dname = sh_util_dirname(path);
|
---|
| 595 | bname = sh_util_basename(path);
|
---|
| 596 |
|
---|
| 597 | size = strlen(dname) + strlen(bname) + 4;
|
---|
| 598 | searchpath = SH_ALLOC(size);
|
---|
| 599 |
|
---|
| 600 | for (i = 0; i < 2; ++i)
|
---|
| 601 | {
|
---|
| 602 | snprintf(searchpath, size, "%s/%s.%1d", dname, bname, i);
|
---|
| 603 | if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
|
---|
| 604 | {
|
---|
| 605 | SH_FREE(dname);
|
---|
| 606 | SH_FREE(bname);
|
---|
| 607 | return searchpath;
|
---|
| 608 | }
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | SH_FREE(searchpath);
|
---|
| 612 |
|
---|
| 613 | if (NULL != (dp = opendir(dname)))
|
---|
| 614 | {
|
---|
| 615 | struct dirent * de;
|
---|
| 616 |
|
---|
| 617 | while (NULL != (de = readdir(dp)))
|
---|
| 618 | {
|
---|
| 619 | if (0 == strcmp(de->d_name, ".") || 0 == strcmp(de->d_name, ".."))
|
---|
| 620 | continue;
|
---|
| 621 |
|
---|
| 622 | size = strlen(dname) + strlen(de->d_name) + 2;
|
---|
| 623 | searchpath = SH_ALLOC(size);
|
---|
| 624 | snprintf(searchpath, size, "%s/%s", dname, de->d_name);
|
---|
| 625 |
|
---|
| 626 | if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
|
---|
| 627 | {
|
---|
| 628 | SH_FREE(dname);
|
---|
| 629 | SH_FREE(bname);
|
---|
| 630 | closedir(dp);
|
---|
| 631 | return searchpath;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | SH_FREE(searchpath);
|
---|
| 635 | }
|
---|
| 636 | closedir(dp);
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | SH_FREE(dname);
|
---|
| 640 | SH_FREE(bname);
|
---|
| 641 |
|
---|
| 642 | return NULL;
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | /* Open file, position at stored offset
|
---|
| 646 | */
|
---|
| 647 | int sh_open_for_reader (struct sh_logfile * logfile)
|
---|
| 648 | {
|
---|
| 649 | struct stat buf;
|
---|
| 650 | sh_string * filename;
|
---|
| 651 |
|
---|
| 652 | /* check whether file exists, get inode to check for
|
---|
| 653 | * logfile rotation
|
---|
| 654 | */
|
---|
| 655 | if (0 != retry_stat(FIL__, __LINE__, logfile->filename, &buf))
|
---|
| 656 | {
|
---|
| 657 | char * tmp;
|
---|
| 658 |
|
---|
| 659 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 660 | tmp = sh_util_safe_name (logfile->filename);
|
---|
| 661 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_MISS,
|
---|
| 662 | tmp);
|
---|
| 663 | SH_FREE(tmp);
|
---|
| 664 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 665 |
|
---|
| 666 | memset (&(logfile->offset), '\0', sizeof(fpos_t));
|
---|
| 667 | logfile->flags |= SH_LOGFILE_REWIND;
|
---|
| 668 | return 0;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | filename = sh_string_new(0);
|
---|
| 672 | (void) sh_string_set_from_char (filename, logfile->filename);
|
---|
| 673 |
|
---|
| 674 | /* detect and handle logfile rotation
|
---|
| 675 | */
|
---|
[271] | 676 | if (logfile->inode != buf.st_ino &&
|
---|
| 677 | logfile->inode != 0 &&
|
---|
| 678 | !S_ISFIFO(buf.st_mode))
|
---|
[183] | 679 | {
|
---|
| 680 | /* Case 1) We have dealt with the moved file already.
|
---|
| 681 | * Clear the moved flag, set the rewind flag,
|
---|
| 682 | * fix logfile->inode.
|
---|
| 683 | */
|
---|
| 684 | if ((logfile->flags & SH_LOGFILE_MOVED) != 0)
|
---|
| 685 | {
|
---|
| 686 | /* done with rotated file, start with current file
|
---|
| 687 | */
|
---|
| 688 | memset (&(logfile->offset), '\0', sizeof(fpos_t));
|
---|
| 689 | logfile->flags |= SH_LOGFILE_REWIND;
|
---|
| 690 | logfile->flags &= ~SH_LOGFILE_MOVED;
|
---|
| 691 | logfile->inode = buf.st_ino;
|
---|
| 692 | logfile->device_id = buf.st_dev;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | /* Case 2) Searching for rotated file.
|
---|
| 696 | * If found: set the moved flag, fix path for fopen.
|
---|
| 697 | * If not found: set the rewind flag, fix logfile->inode.
|
---|
| 698 | */
|
---|
| 699 | else
|
---|
| 700 | {
|
---|
| 701 | char *oldfile = sh_rotated_log_search(logfile->filename, &buf);
|
---|
| 702 |
|
---|
| 703 | if (NULL != oldfile)
|
---|
| 704 | {
|
---|
| 705 | (void) sh_string_set_from_char (filename, oldfile);
|
---|
| 706 | SH_FREE(oldfile);
|
---|
| 707 | logfile->flags |= SH_LOGFILE_MOVED;
|
---|
| 708 | }
|
---|
| 709 | else
|
---|
| 710 | {
|
---|
| 711 | memset (&(logfile->offset), '\0', sizeof(fpos_t));
|
---|
| 712 | logfile->flags |= SH_LOGFILE_REWIND;
|
---|
| 713 | logfile->inode = buf.st_ino;
|
---|
| 714 | logfile->device_id = buf.st_dev;
|
---|
| 715 | }
|
---|
| 716 | }
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | /* open file
|
---|
| 720 | */
|
---|
[271] | 721 | if (!S_ISFIFO(buf.st_mode))
|
---|
| 722 | {
|
---|
| 723 | logfile->fp = fopen(filename->str, "r");
|
---|
| 724 | }
|
---|
| 725 | else
|
---|
| 726 | {
|
---|
| 727 | int fd_temp = open (filename->str, O_RDONLY|O_NONBLOCK);
|
---|
| 728 |
|
---|
| 729 | if (fd_temp >= 0)
|
---|
| 730 | {
|
---|
| 731 | logfile->fp = fdopen(fd_temp, "r");
|
---|
| 732 | }
|
---|
| 733 | }
|
---|
| 734 |
|
---|
[183] | 735 | if (!logfile->fp)
|
---|
| 736 | {
|
---|
| 737 | char * tmp;
|
---|
| 738 |
|
---|
| 739 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 740 | tmp = sh_util_safe_name (logfile->filename);
|
---|
| 741 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EOPEN,
|
---|
| 742 | tmp);
|
---|
| 743 | SH_FREE(tmp);
|
---|
| 744 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 745 |
|
---|
| 746 | sh_string_destroy(&filename);
|
---|
| 747 | return 0;
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | sh_string_destroy(&filename);
|
---|
| 751 |
|
---|
[271] | 752 | if ((logfile->flags & SH_LOGFILE_PIPE) == 0)
|
---|
[183] | 753 | {
|
---|
[271] | 754 | if ((logfile->flags & SH_LOGFILE_REWIND) != 0)
|
---|
[183] | 755 | {
|
---|
| 756 | rewind(logfile->fp);
|
---|
| 757 | fgetpos(logfile->fp, &(logfile->offset));
|
---|
[271] | 758 | logfile->flags &= ~SH_LOGFILE_REWIND;
|
---|
[183] | 759 | }
|
---|
[271] | 760 | else
|
---|
| 761 | {
|
---|
| 762 | /* file too short
|
---|
| 763 | */
|
---|
| 764 | if (0 != fsetpos(logfile->fp, &(logfile->offset)))
|
---|
| 765 | {
|
---|
| 766 | rewind(logfile->fp);
|
---|
| 767 | fgetpos(logfile->fp, &(logfile->offset));
|
---|
| 768 | }
|
---|
| 769 | }
|
---|
[183] | 770 | }
|
---|
| 771 |
|
---|
| 772 | return 1;
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 | /******************************************************
|
---|
| 776 | * Default reader for ascii text files
|
---|
| 777 | */
|
---|
| 778 | sh_string * sh_default_reader (sh_string * s, struct sh_logfile * logfile)
|
---|
| 779 | {
|
---|
[275] | 780 | volatile int status;
|
---|
[183] | 781 | char * tmp;
|
---|
| 782 |
|
---|
| 783 | start_read:
|
---|
| 784 |
|
---|
| 785 | if (logfile->fp)
|
---|
| 786 | {
|
---|
| 787 | /* Result cannot be larger than 8192, thus cast is ok
|
---|
| 788 | */
|
---|
| 789 | status = (int) sh_string_read(s, logfile->fp, 8192);
|
---|
| 790 | if (status <= 0)
|
---|
| 791 | {
|
---|
| 792 | fgetpos(logfile->fp, &(logfile->offset));
|
---|
[252] | 793 | sl_fclose(FIL__, __LINE__, logfile->fp);
|
---|
[183] | 794 | logfile->fp = NULL;
|
---|
| 795 | sh_string_destroy(&s);
|
---|
[271] | 796 | if (status == 0 || (logfile->flags & SH_LOGFILE_PIPE) != 0)
|
---|
[183] | 797 | {
|
---|
| 798 | return NULL;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 802 | tmp = sh_util_safe_name (logfile->filename);
|
---|
| 803 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EREAD,
|
---|
| 804 | tmp);
|
---|
| 805 | SH_FREE(tmp);
|
---|
| 806 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 807 |
|
---|
| 808 | return NULL;
|
---|
| 809 | }
|
---|
| 810 | return s;
|
---|
| 811 | }
|
---|
| 812 |
|
---|
| 813 | if (0 != sh_open_for_reader(logfile))
|
---|
| 814 | goto start_read;
|
---|
| 815 |
|
---|
| 816 | return NULL;
|
---|
| 817 | }
|
---|
| 818 |
|
---|
[276] | 819 | struct task_entry
|
---|
[275] | 820 | {
|
---|
| 821 | sh_tas_t task;
|
---|
[276] | 822 | struct task_entry * next;
|
---|
| 823 | };
|
---|
[275] | 824 |
|
---|
[276] | 825 | static struct task_entry * tasklist = NULL;
|
---|
| 826 |
|
---|
| 827 | static struct task_entry * task_find(FILE * fp)
|
---|
| 828 | {
|
---|
| 829 | struct task_entry * entry = tasklist;
|
---|
| 830 | while (entry)
|
---|
| 831 | {
|
---|
| 832 | if (entry->task.pipe == fp)
|
---|
| 833 | return (entry);
|
---|
| 834 | entry = entry->next;
|
---|
| 835 | }
|
---|
| 836 | return NULL;
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 | static void task_remove(struct task_entry * task)
|
---|
| 840 | {
|
---|
| 841 | struct task_entry * entry = tasklist;
|
---|
| 842 | struct task_entry * prev = tasklist;
|
---|
| 843 |
|
---|
| 844 | while (entry)
|
---|
| 845 | {
|
---|
| 846 | if (entry == task)
|
---|
| 847 | {
|
---|
| 848 | if (entry == tasklist)
|
---|
| 849 | {
|
---|
| 850 | tasklist = entry->next;
|
---|
| 851 | SH_FREE(entry);
|
---|
| 852 | return;
|
---|
| 853 | }
|
---|
| 854 | else
|
---|
| 855 | {
|
---|
| 856 | prev->next = entry->next;
|
---|
| 857 | SH_FREE(entry);
|
---|
| 858 | return;
|
---|
| 859 | }
|
---|
| 860 | }
|
---|
| 861 | prev = entry;
|
---|
| 862 | entry = entry->next;
|
---|
| 863 | }
|
---|
| 864 | return;
|
---|
| 865 | }
|
---|
| 866 |
|
---|
| 867 | static void task_add(struct task_entry * entry)
|
---|
| 868 | {
|
---|
| 869 | entry->next = tasklist;
|
---|
| 870 | tasklist = entry;
|
---|
| 871 | return;
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 | sh_string * sh_command_reader (sh_string * s, struct sh_logfile * logfile)
|
---|
| 875 | {
|
---|
| 876 | struct task_entry * entry;
|
---|
| 877 |
|
---|
| 878 | volatile int status;
|
---|
[275] | 879 | char * tmp;
|
---|
| 880 |
|
---|
| 881 | start_read:
|
---|
| 882 |
|
---|
| 883 | if (logfile->fp)
|
---|
| 884 | {
|
---|
| 885 | /* Result cannot be larger than 8192, thus cast is ok
|
---|
| 886 | */
|
---|
| 887 | status = (int) sh_string_read(s, logfile->fp, 8192);
|
---|
| 888 |
|
---|
| 889 | if (status <= 0)
|
---|
| 890 | {
|
---|
[276] | 891 | entry = task_find(logfile->fp);
|
---|
| 892 | sh_ext_pclose (&(entry->task));
|
---|
| 893 | task_remove(entry);
|
---|
| 894 |
|
---|
[275] | 895 | logfile->fp = NULL;
|
---|
| 896 | sh_string_destroy(&s);
|
---|
| 897 |
|
---|
| 898 | if (status == 0)
|
---|
| 899 | {
|
---|
| 900 | return NULL;
|
---|
| 901 | }
|
---|
| 902 |
|
---|
| 903 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 904 | tmp = sh_util_safe_name (logfile->filename);
|
---|
| 905 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EREAD,
|
---|
| 906 | tmp);
|
---|
| 907 | SH_FREE(tmp);
|
---|
| 908 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 909 |
|
---|
| 910 | return NULL;
|
---|
| 911 | }
|
---|
| 912 | return s;
|
---|
| 913 | }
|
---|
| 914 |
|
---|
[276] | 915 | entry = SH_ALLOC(sizeof(struct task_entry));
|
---|
| 916 |
|
---|
[415] | 917 | status = sh_ext_popen_init (&(entry->task), logfile->filename, logfile->filename, NULL);
|
---|
[275] | 918 | if (0 == status)
|
---|
| 919 | {
|
---|
[276] | 920 | task_add(entry);
|
---|
| 921 | logfile->fp = entry->task.pipe;
|
---|
[275] | 922 | goto start_read;
|
---|
| 923 | }
|
---|
| 924 | else
|
---|
| 925 | {
|
---|
[276] | 926 | SH_FREE(entry);
|
---|
[275] | 927 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 928 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
| 929 | _("Could not open pipe"), _("sh_command reader"));
|
---|
| 930 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 931 | }
|
---|
| 932 |
|
---|
| 933 | return NULL;
|
---|
| 934 | }
|
---|
| 935 |
|
---|
[183] | 936 | /******************************************************
|
---|
[185] | 937 | * Reader for continued text files
|
---|
| 938 | */
|
---|
| 939 | sh_string * sh_cont_reader (sh_string * s, struct sh_logfile * logfile, char*cont)
|
---|
| 940 | {
|
---|
| 941 | int status;
|
---|
| 942 | char * tmp;
|
---|
| 943 | sh_string * str;
|
---|
| 944 | int remain = 8192;
|
---|
| 945 | int count = 0;
|
---|
| 946 |
|
---|
| 947 | if (!sh_string_truncate(s, 0))
|
---|
| 948 | return NULL;
|
---|
| 949 |
|
---|
| 950 | start_read:
|
---|
| 951 |
|
---|
| 952 | if (logfile->fp)
|
---|
| 953 | {
|
---|
| 954 | str = sh_string_new(0);
|
---|
| 955 |
|
---|
| 956 | /* Result cannot be larger than 8192, thus cast is ok
|
---|
| 957 | */
|
---|
| 958 | status = (int) sh_string_read(str, logfile->fp, 8192);
|
---|
| 959 |
|
---|
| 960 | if (status > 0)
|
---|
| 961 | {
|
---|
| 962 |
|
---|
| 963 | do {
|
---|
| 964 | s = sh_string_add (s, str);
|
---|
| 965 | count += status;
|
---|
| 966 | remain -= status;
|
---|
| 967 |
|
---|
| 968 | if (remain <= 0)
|
---|
| 969 | {
|
---|
| 970 | return s;
|
---|
| 971 | }
|
---|
| 972 |
|
---|
| 973 | status = (int) sh_string_read_cont(str, logfile->fp, count, cont);
|
---|
| 974 |
|
---|
| 975 | if (status == 0)
|
---|
| 976 | {
|
---|
| 977 | return s;
|
---|
| 978 | }
|
---|
| 979 | }
|
---|
| 980 | while (status > 0);
|
---|
| 981 | }
|
---|
| 982 |
|
---|
| 983 | if (status <= 0)
|
---|
| 984 | {
|
---|
| 985 | fgetpos(logfile->fp, &(logfile->offset));
|
---|
[252] | 986 | sl_fclose(FIL__, __LINE__, logfile->fp);
|
---|
[185] | 987 | logfile->fp = NULL;
|
---|
| 988 | sh_string_destroy(&s);
|
---|
[271] | 989 | if (status == 0 || (logfile->flags & SH_LOGFILE_PIPE) != 0)
|
---|
[185] | 990 | {
|
---|
| 991 | return NULL;
|
---|
| 992 | }
|
---|
| 993 |
|
---|
| 994 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 995 | tmp = sh_util_safe_name (logfile->filename);
|
---|
| 996 | sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EREAD,
|
---|
| 997 | tmp);
|
---|
| 998 | SH_FREE(tmp);
|
---|
| 999 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 1000 |
|
---|
| 1001 | return NULL;
|
---|
| 1002 | }
|
---|
| 1003 |
|
---|
| 1004 | return s;
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 | if (0 != sh_open_for_reader(logfile))
|
---|
| 1008 | goto start_read;
|
---|
| 1009 |
|
---|
| 1010 | return NULL;
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 | /******************************************************
|
---|
[183] | 1014 | * Reader for binary files
|
---|
| 1015 | */
|
---|
[362] | 1016 | sh_string * sh_binary_reader (void * s, size_t size,
|
---|
| 1017 | struct sh_logfile * logfile)
|
---|
[183] | 1018 | {
|
---|
| 1019 | size_t status;
|
---|
| 1020 |
|
---|
| 1021 | start_read:
|
---|
| 1022 |
|
---|
| 1023 | if (logfile->fp)
|
---|
| 1024 | {
|
---|
| 1025 |
|
---|
| 1026 | status = fread(s, size, 1, logfile->fp);
|
---|
| 1027 |
|
---|
| 1028 | if (status != 1)
|
---|
| 1029 | {
|
---|
[362] | 1030 | memset(s, '\0', size);
|
---|
[271] | 1031 | if (ferror(logfile->fp) && (logfile->flags & SH_LOGFILE_PIPE) == 0)
|
---|
[183] | 1032 | {
|
---|
| 1033 | char * tmp;
|
---|
| 1034 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 1035 | tmp = sh_util_safe_name (logfile->filename);
|
---|
| 1036 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_LOGMON_EREAD,
|
---|
| 1037 | tmp);
|
---|
| 1038 | SH_FREE(tmp);
|
---|
| 1039 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 1040 | }
|
---|
| 1041 | fgetpos(logfile->fp, &(logfile->offset));
|
---|
[252] | 1042 | sl_fclose(FIL__, __LINE__, logfile->fp);
|
---|
[183] | 1043 | logfile->fp = NULL;
|
---|
| 1044 | return NULL;
|
---|
| 1045 | }
|
---|
| 1046 | return s;
|
---|
| 1047 | }
|
---|
| 1048 |
|
---|
| 1049 | if (0 != sh_open_for_reader(logfile))
|
---|
| 1050 | goto start_read;
|
---|
| 1051 |
|
---|
| 1052 | return NULL;
|
---|
| 1053 | }
|
---|
| 1054 |
|
---|
| 1055 |
|
---|
| 1056 |
|
---|
| 1057 | /**********************************************************
|
---|
| 1058 | *
|
---|
| 1059 | * Utilities
|
---|
| 1060 | *
|
---|
| 1061 | **********************************************************/
|
---|
| 1062 |
|
---|
[265] | 1063 | /* Return current year, unless that would result
|
---|
| 1064 | * in a date far in the future. If that happens,
|
---|
| 1065 | * return last year.
|
---|
| 1066 | */
|
---|
| 1067 | static int year_guess (struct tm * btime)
|
---|
| 1068 | {
|
---|
| 1069 | int year;
|
---|
| 1070 | struct tm ts;
|
---|
| 1071 | time_t now = time(NULL);
|
---|
| 1072 | time_t check;
|
---|
| 1073 |
|
---|
| 1074 | memcpy(&ts, localtime(&now), sizeof(struct tm));
|
---|
| 1075 | year = ts.tm_year;
|
---|
| 1076 |
|
---|
| 1077 | /* Check result to detect year wrap
|
---|
| 1078 | * (logfile entry from last year).
|
---|
| 1079 | */
|
---|
| 1080 | btime->tm_year = year;
|
---|
| 1081 | check = mktime(btime);
|
---|
| 1082 | if (check > (now + (86400*30)))
|
---|
| 1083 | --year;
|
---|
| 1084 |
|
---|
| 1085 | return year;
|
---|
| 1086 | }
|
---|
| 1087 |
|
---|
[183] | 1088 | time_t conv_timestamp (struct tm * btime,
|
---|
| 1089 | struct tm * old_tm, time_t * old_time)
|
---|
| 1090 | {
|
---|
| 1091 | time_t timestamp;
|
---|
| 1092 | long offtime;
|
---|
| 1093 |
|
---|
| 1094 | /* timestamp - mktime is slooow, thus cache result
|
---|
| 1095 | */
|
---|
| 1096 | if (btime->tm_isdst == old_tm->tm_isdst &&
|
---|
| 1097 | btime->tm_year == old_tm->tm_year &&
|
---|
| 1098 | btime->tm_mon == old_tm->tm_mon &&
|
---|
| 1099 | btime->tm_mday == old_tm->tm_mday)
|
---|
| 1100 | {
|
---|
| 1101 | offtime =
|
---|
| 1102 | (btime->tm_hour - old_tm->tm_hour) * 3600 +
|
---|
| 1103 | (btime->tm_min - old_tm->tm_min) * 60 +
|
---|
| 1104 | (btime->tm_sec - old_tm->tm_sec);
|
---|
[265] | 1105 |
|
---|
[183] | 1106 | *old_time += offtime;
|
---|
| 1107 | memcpy(old_tm, btime, sizeof(struct tm));
|
---|
| 1108 | timestamp = *old_time;
|
---|
| 1109 | }
|
---|
| 1110 | else
|
---|
| 1111 | {
|
---|
[265] | 1112 | int year_btime = btime->tm_year;
|
---|
| 1113 |
|
---|
| 1114 | if (btime->tm_year == 0)
|
---|
| 1115 | btime->tm_year = year_guess(btime);
|
---|
[183] | 1116 | timestamp = mktime(btime);
|
---|
[265] | 1117 | btime->tm_year = year_btime;
|
---|
[183] | 1118 | *old_time = timestamp;
|
---|
| 1119 | memcpy(old_tm, btime, sizeof(struct tm));
|
---|
| 1120 | }
|
---|
| 1121 | return timestamp;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | /*********************************************************
|
---|
| 1125 | *
|
---|
| 1126 | * MODULE STUFF
|
---|
| 1127 | *
|
---|
| 1128 | *********************************************************/
|
---|
| 1129 | #include "sh_modules.h"
|
---|
| 1130 |
|
---|
| 1131 | SH_MUTEX_STATIC(mutex_logmon_check, PTHREAD_MUTEX_INITIALIZER);
|
---|
| 1132 |
|
---|
| 1133 | static int ShLogmonActive = S_FALSE;
|
---|
| 1134 | #define SH_LOGMON_INTERVAL 10
|
---|
| 1135 | static time_t sh_logmon_interval = SH_LOGMON_INTERVAL;
|
---|
| 1136 |
|
---|
| 1137 | int sh_log_check_init (struct mod_type * arg)
|
---|
| 1138 | {
|
---|
[257] | 1139 | #if !defined(HAVE_PTHREAD)
|
---|
| 1140 | (void) arg;
|
---|
| 1141 | #endif
|
---|
| 1142 |
|
---|
[183] | 1143 | if (ShLogmonActive == S_FALSE)
|
---|
| 1144 | return SH_MOD_FAILED;
|
---|
| 1145 | #ifdef HAVE_PTHREAD
|
---|
| 1146 | if (arg != NULL && arg->initval < 0 &&
|
---|
| 1147 | (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
|
---|
| 1148 | {
|
---|
| 1149 | if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
|
---|
| 1150 | return SH_MOD_THREAD;
|
---|
| 1151 | else
|
---|
| 1152 | return SH_MOD_FAILED;
|
---|
| 1153 | }
|
---|
[335] | 1154 | else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
|
---|
| 1155 | (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
|
---|
| 1156 | {
|
---|
| 1157 | return SH_MOD_THREAD;
|
---|
| 1158 | }
|
---|
[183] | 1159 | #endif
|
---|
| 1160 | if (sh_watched_logs != NULL)
|
---|
| 1161 | return 0;
|
---|
| 1162 |
|
---|
| 1163 | return -1;
|
---|
| 1164 | }
|
---|
| 1165 |
|
---|
| 1166 | int sh_log_check_timer(time_t tcurrent)
|
---|
| 1167 | {
|
---|
| 1168 | static time_t lastcheck = 0;
|
---|
| 1169 |
|
---|
| 1170 | SL_ENTER(_("sh_log_check_timer"));
|
---|
| 1171 | if ((time_t) (tcurrent - lastcheck) >= sh_logmon_interval)
|
---|
| 1172 | {
|
---|
| 1173 | lastcheck = tcurrent;
|
---|
| 1174 | SL_RETURN((-1), _("sh_log_check_timer"));
|
---|
| 1175 | }
|
---|
| 1176 | SL_RETURN(0, _("sh_log_check_timer"));
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
| 1179 |
|
---|
| 1180 | int sh_log_check_check(void)
|
---|
| 1181 | {
|
---|
| 1182 | int status = 0;
|
---|
| 1183 |
|
---|
| 1184 | SL_ENTER(_("sh_log_check_check"));
|
---|
| 1185 |
|
---|
| 1186 | SH_MUTEX_LOCK(mutex_logmon_check);
|
---|
| 1187 |
|
---|
| 1188 | status = 0;
|
---|
| 1189 |
|
---|
| 1190 | if( ShLogmonActive != S_FALSE )
|
---|
| 1191 | {
|
---|
| 1192 | sh_check_watches();
|
---|
[260] | 1193 | sh_keep_match();
|
---|
[265] | 1194 | sh_log_mark_check();
|
---|
[276] | 1195 | clean_dir();
|
---|
[183] | 1196 | }
|
---|
| 1197 | SH_MUTEX_UNLOCK(mutex_logmon_check);
|
---|
| 1198 |
|
---|
| 1199 | SL_RETURN(status, _("sh_log_check_check"));
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
| 1202 | int sh_log_check_reconf(void)
|
---|
| 1203 | {
|
---|
| 1204 | int status = 0;
|
---|
| 1205 |
|
---|
| 1206 | SL_ENTER(_("sh_log_check_check"));
|
---|
| 1207 |
|
---|
| 1208 | SH_MUTEX_LOCK(mutex_logmon_check);
|
---|
| 1209 |
|
---|
| 1210 | ShLogmonActive = S_FALSE;
|
---|
| 1211 | sh_logmon_interval = SH_LOGMON_INTERVAL;
|
---|
| 1212 | sh_dump_watches();
|
---|
| 1213 | sh_eval_cleanup();
|
---|
| 1214 |
|
---|
| 1215 | SH_MUTEX_UNLOCK(mutex_logmon_check);
|
---|
| 1216 |
|
---|
| 1217 | SL_RETURN(status, _("sh_log_check_check"));
|
---|
| 1218 | }
|
---|
| 1219 |
|
---|
| 1220 | int sh_log_check_cleanup(void)
|
---|
| 1221 | {
|
---|
[265] | 1222 | sh_log_mark_destroy();
|
---|
[183] | 1223 | return sh_log_check_reconf();
|
---|
| 1224 | }
|
---|
| 1225 |
|
---|
| 1226 | /********************* OPTIONS **********************/
|
---|
| 1227 |
|
---|
| 1228 | static int sh_logmon_set_active (const char *str);
|
---|
[276] | 1229 | static int sh_logmon_set_clean (const char *str);
|
---|
[183] | 1230 | static int sh_logmon_set_interval(const char *str);
|
---|
| 1231 | static int sh_logmon_add_watch (const char * str);
|
---|
| 1232 | static int sh_logmon_add_group (const char * str);
|
---|
| 1233 | static int sh_logmon_end_group (const char * str);
|
---|
| 1234 | static int sh_logmon_add_host (const char * str);
|
---|
| 1235 | static int sh_logmon_end_host (const char * str);
|
---|
| 1236 | static int sh_logmon_add_queue (const char * str);
|
---|
| 1237 | static int sh_logmon_add_rule (const char * str);
|
---|
| 1238 | extern int sh_set_hidepid(const char *s);
|
---|
[265] | 1239 | static int sh_logmon_set_save_dir(const char *s);
|
---|
[183] | 1240 |
|
---|
| 1241 | sh_rconf sh_log_check_table[] = {
|
---|
| 1242 | {
|
---|
| 1243 | N_("logmonactive"),
|
---|
| 1244 | sh_logmon_set_active,
|
---|
| 1245 | },
|
---|
| 1246 | {
|
---|
| 1247 | N_("logmoninterval"),
|
---|
| 1248 | sh_logmon_set_interval,
|
---|
| 1249 | },
|
---|
| 1250 | {
|
---|
[276] | 1251 | N_("logmonclean"),
|
---|
| 1252 | sh_logmon_set_clean,
|
---|
| 1253 | },
|
---|
| 1254 | {
|
---|
[183] | 1255 | N_("logmonwatch"),
|
---|
| 1256 | sh_logmon_add_watch,
|
---|
| 1257 | },
|
---|
| 1258 | {
|
---|
| 1259 | N_("logmonqueue"),
|
---|
| 1260 | sh_logmon_add_queue,
|
---|
| 1261 | },
|
---|
| 1262 | {
|
---|
| 1263 | N_("logmongroup"),
|
---|
| 1264 | sh_logmon_add_group,
|
---|
| 1265 | },
|
---|
| 1266 | {
|
---|
| 1267 | N_("logmonendgroup"),
|
---|
| 1268 | sh_logmon_end_group,
|
---|
| 1269 | },
|
---|
| 1270 | {
|
---|
| 1271 | N_("logmonhost"),
|
---|
| 1272 | sh_logmon_add_host,
|
---|
| 1273 | },
|
---|
| 1274 | {
|
---|
| 1275 | N_("logmonendhost"),
|
---|
| 1276 | sh_logmon_end_host,
|
---|
| 1277 | },
|
---|
| 1278 | {
|
---|
| 1279 | N_("logmonrule"),
|
---|
| 1280 | sh_logmon_add_rule,
|
---|
| 1281 | },
|
---|
| 1282 | {
|
---|
| 1283 | N_("logmonhidepid"),
|
---|
| 1284 | sh_set_hidepid,
|
---|
| 1285 | },
|
---|
| 1286 | {
|
---|
[265] | 1287 | N_("logmonsavedir"),
|
---|
| 1288 | sh_logmon_set_save_dir,
|
---|
| 1289 | },
|
---|
| 1290 | {
|
---|
| 1291 | N_("logmonmarkseverity"),
|
---|
[349] | 1292 | sh_log_set_mark_severity,
|
---|
[265] | 1293 | },
|
---|
| 1294 | {
|
---|
| 1295 | N_("logmonburstthreshold"),
|
---|
| 1296 | sh_repeat_set_trigger,
|
---|
| 1297 | },
|
---|
| 1298 | {
|
---|
| 1299 | N_("logmonburstqueue"),
|
---|
| 1300 | sh_repeat_set_queue,
|
---|
| 1301 | },
|
---|
| 1302 | {
|
---|
| 1303 | N_("logmonburstcron"),
|
---|
| 1304 | sh_repeat_set_cron,
|
---|
| 1305 | },
|
---|
| 1306 | {
|
---|
[358] | 1307 | N_("logmondeadtime"),
|
---|
| 1308 | sh_keep_deadtime,
|
---|
| 1309 | },
|
---|
| 1310 | {
|
---|
[183] | 1311 | NULL,
|
---|
| 1312 | NULL
|
---|
| 1313 | }
|
---|
| 1314 | };
|
---|
| 1315 |
|
---|
| 1316 | /* Decide if we're active.
|
---|
| 1317 | */
|
---|
| 1318 | static int sh_logmon_set_active(const char *str)
|
---|
| 1319 | {
|
---|
| 1320 | int value;
|
---|
| 1321 |
|
---|
| 1322 | SL_ENTER(_("sh_logmon_set_active"));
|
---|
| 1323 |
|
---|
| 1324 | value = sh_util_flagval(str, &ShLogmonActive);
|
---|
| 1325 |
|
---|
| 1326 | SL_RETURN((value), _("sh_logmon_set_active"));
|
---|
| 1327 | }
|
---|
| 1328 |
|
---|
[276] | 1329 | /* Decide if we're active.
|
---|
| 1330 | */
|
---|
| 1331 | static int sh_logmon_set_clean(const char *str)
|
---|
| 1332 | {
|
---|
| 1333 | int value;
|
---|
| 1334 |
|
---|
| 1335 | SL_ENTER(_("sh_logmon_set_active"));
|
---|
| 1336 |
|
---|
| 1337 | value = sh_util_flagval(str, &do_checkpoint_cleanup);
|
---|
| 1338 |
|
---|
| 1339 | SL_RETURN((value), _("sh_logmon_set_active"));
|
---|
| 1340 | }
|
---|
| 1341 |
|
---|
[265] | 1342 | static int sh_logmon_set_save_dir(const char *str)
|
---|
| 1343 | {
|
---|
| 1344 | int retval = -1;
|
---|
| 1345 |
|
---|
| 1346 | SL_ENTER(_("sh_logmon_set_save_dir"));
|
---|
| 1347 |
|
---|
| 1348 | if (str && str[0] == '/')
|
---|
| 1349 | {
|
---|
| 1350 | if (save_dir)
|
---|
| 1351 | {
|
---|
| 1352 | SH_FREE(save_dir);
|
---|
| 1353 | save_dir = NULL;
|
---|
| 1354 | }
|
---|
| 1355 | save_dir = sh_util_strdup(str);
|
---|
| 1356 | retval = 0;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 | SL_RETURN((retval), _("sh_logmon_set_save_dir"));
|
---|
| 1360 | }
|
---|
| 1361 |
|
---|
[183] | 1362 | static int sh_logmon_set_interval (const char * c)
|
---|
| 1363 | {
|
---|
| 1364 | int retval = 0;
|
---|
| 1365 | long val;
|
---|
| 1366 |
|
---|
| 1367 | SL_ENTER(_("sh_logmon_set_interval"));
|
---|
| 1368 | val = strtol (c, (char **)NULL, 10);
|
---|
| 1369 | if (val <= 0)
|
---|
| 1370 | {
|
---|
| 1371 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
| 1372 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 1373 | _("log monitoring interval"), c);
|
---|
| 1374 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
| 1375 | retval = -1;
|
---|
| 1376 | }
|
---|
[362] | 1377 | else
|
---|
| 1378 | {
|
---|
| 1379 | sh_logmon_interval = (time_t) val;
|
---|
| 1380 | }
|
---|
| 1381 | SL_RETURN(retval, _("sh_logmon_set_interval"));
|
---|
[183] | 1382 | }
|
---|
| 1383 |
|
---|
| 1384 | /* Add a watch on a logfile.
|
---|
| 1385 | * Format: TYPE : Filename [: File_Format]
|
---|
| 1386 | */
|
---|
| 1387 | static int sh_logmon_add_watch (const char * str)
|
---|
| 1388 | {
|
---|
| 1389 | return sh_add_watch(str);
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
| 1392 | /* Add a host.
|
---|
| 1393 | * Format: Name_Regex
|
---|
| 1394 | */
|
---|
| 1395 | static int sh_logmon_add_host (const char * str)
|
---|
| 1396 | {
|
---|
| 1397 | return sh_eval_hadd(str);
|
---|
| 1398 | }
|
---|
| 1399 |
|
---|
| 1400 | /* End a host.
|
---|
| 1401 | * Format: Name
|
---|
| 1402 | */
|
---|
| 1403 | static int sh_logmon_end_host (const char * str)
|
---|
| 1404 | {
|
---|
| 1405 | (void) str;
|
---|
| 1406 | return sh_eval_hend(NULL);
|
---|
| 1407 | }
|
---|
| 1408 |
|
---|
| 1409 | /* Add a group of rules.
|
---|
| 1410 | * Groups can be under hosts, but not vice versa.
|
---|
| 1411 | * Format: Name : Prefix_Regex
|
---|
| 1412 | */
|
---|
| 1413 | static int sh_logmon_add_group (const char * str)
|
---|
| 1414 | {
|
---|
| 1415 | return sh_eval_gadd(str);
|
---|
| 1416 | }
|
---|
| 1417 |
|
---|
| 1418 | /* End a group of rules.
|
---|
| 1419 | * Format: Name
|
---|
| 1420 | */
|
---|
| 1421 | static int sh_logmon_end_group (const char * str)
|
---|
| 1422 | {
|
---|
| 1423 | (void) str;
|
---|
| 1424 | return sh_eval_gend(NULL);
|
---|
| 1425 | }
|
---|
| 1426 |
|
---|
| 1427 | /* Define a reporting queue.
|
---|
[272] | 1428 | * Format: Label : [Interval] : TYPE : Severity[:alias]
|
---|
[183] | 1429 | * TYPE must be 'report' or 'sum'
|
---|
| 1430 | * Interval is ignored for TYPE='report'
|
---|
| 1431 | */
|
---|
| 1432 | static int sh_logmon_add_queue (const char * str)
|
---|
| 1433 | {
|
---|
| 1434 | return sh_eval_qadd(str);
|
---|
| 1435 | }
|
---|
| 1436 |
|
---|
| 1437 | /* Define a check rule.
|
---|
[260] | 1438 | * Format: [KEEP(seconds,label):]Queue_Label : Regex
|
---|
| 1439 | * KEEP indicates that we keep the label, to perform
|
---|
| 1440 | * correlation matching
|
---|
[183] | 1441 | */
|
---|
| 1442 | static int sh_logmon_add_rule (const char * str)
|
---|
| 1443 | {
|
---|
| 1444 | return sh_eval_radd(str);
|
---|
| 1445 | }
|
---|
| 1446 |
|
---|
| 1447 |
|
---|
| 1448 | #if 0
|
---|
| 1449 |
|
---|
| 1450 | /* >>>>>>>>>>> MAIN <<<<<<<<<<<<<<<<<<< */
|
---|
| 1451 |
|
---|
| 1452 | int main (int argc, char * argv[])
|
---|
| 1453 | {
|
---|
| 1454 | int status, i;
|
---|
| 1455 | FILE * fp;
|
---|
| 1456 | sh_string * s = NULL;
|
---|
| 1457 | static char template[] = "/tmp/xtest.XXXXXX";
|
---|
| 1458 |
|
---|
| 1459 | /* pacct */
|
---|
| 1460 | status = sh_add_watch("PACCT:/var/log/account/pacct");
|
---|
| 1461 | sh_check_watches();
|
---|
| 1462 | sh_dump_watches();
|
---|
| 1463 | exit(0);
|
---|
| 1464 |
|
---|
| 1465 | /* apache log */
|
---|
| 1466 | sh_eval_gadd("four_o_four:404");
|
---|
| 1467 | sh_eval_qadd("test:1:sum:7");
|
---|
| 1468 | sh_eval_radd("test:^(\\d+.\\d+.\\d+.\\d+).*");
|
---|
| 1469 | sh_eval_gend(NULL);
|
---|
| 1470 | sh_eval_radd("trash:.*");
|
---|
| 1471 | status = sh_add_watch("APACHE:/var/log/apache2/access.log:combined");
|
---|
| 1472 | sh_check_watches();
|
---|
| 1473 | sh_dump_watches();
|
---|
| 1474 | exit(0);
|
---|
| 1475 |
|
---|
| 1476 | /* logfile */
|
---|
| 1477 | sh_set_hidepid(1);
|
---|
| 1478 | sh_eval_hadd("hslxmsrv1");
|
---|
| 1479 | sh_eval_gadd("postfix:postfix");
|
---|
| 1480 | sh_eval_qadd("test::report:7");
|
---|
| 1481 | sh_eval_radd("test:postfix/smtpd: disconnect from localhost.*");
|
---|
| 1482 | sh_eval_radd("trash:postfix/smtpd: disconnect.*");
|
---|
| 1483 | sh_eval_hadd("hspc05");
|
---|
| 1484 | sh_eval_gadd("cron:CRON");
|
---|
| 1485 | sh_eval_qadd("test:1:sum:7");
|
---|
| 1486 | sh_eval_radd("test:CRON: PAM adding faulty module: (/lib/security/.*.so)");
|
---|
| 1487 | sh_eval_radd("trash:.*");
|
---|
| 1488 | status = sh_add_watch("SYSLOG:/var/log/messages");
|
---|
| 1489 | sh_check_watches();
|
---|
| 1490 |
|
---|
| 1491 | sh_dump_watches();
|
---|
| 1492 | exit(0);
|
---|
| 1493 |
|
---|
| 1494 | printf("%d types\n",
|
---|
| 1495 | (int) (sizeof(sh_logtypes_def)/sizeof(struct sh_logfile_type)));
|
---|
| 1496 |
|
---|
| 1497 | /* test sh_add_watch
|
---|
| 1498 | */
|
---|
| 1499 | status = sh_add_watch("");
|
---|
| 1500 | printf("%2d: zero length, expect -1\n", status);
|
---|
| 1501 | status = sh_add_watch(NULL);
|
---|
| 1502 | printf("%2d: NULL, expect -2\n", status);
|
---|
| 1503 | status = sh_add_watch("0123456789012345:/var/log/messages");
|
---|
| 1504 | printf("%2d: long, expect -2\n", status);
|
---|
| 1505 | status = sh_add_watch("012345678901234:/var/log/messages");
|
---|
| 1506 | printf("%2d: exact length, expect -3\n", status);
|
---|
| 1507 | status = sh_add_watch("01234567890123:56789");
|
---|
| 1508 | printf("%2d: short length, expect -3\n", status);
|
---|
| 1509 | status = sh_add_watch("SYSLOG:var/log/messages");
|
---|
| 1510 | printf("%2d: short badpath, expect -4\n", status);
|
---|
| 1511 | status = sh_add_watch("SYSLOG:/var/log/messages");
|
---|
[237] | 1512 | /* status = sh_add_watch("SYSLOG:/var/log/dpkg.log.1"); */
|
---|
[183] | 1513 | printf("%2d: short path ok, expect 0\n", status);
|
---|
| 1514 |
|
---|
| 1515 | /* test sh_string_read
|
---|
| 1516 | */
|
---|
| 1517 | s = sh_string_new();
|
---|
| 1518 |
|
---|
| 1519 | status = /*@i@*/mkstemp(template);
|
---|
| 1520 |
|
---|
| 1521 | if (status < 0) {
|
---|
| 1522 | fprintf(stderr, "error in mkstemp!\n"); exit(EXIT_FAILURE); }
|
---|
| 1523 |
|
---|
| 1524 | fp = fdopen(status, "r+");
|
---|
| 1525 | if (!fp) {
|
---|
| 1526 | fprintf(stderr, "error in fdopen!\n"); exit(EXIT_FAILURE); }
|
---|
| 1527 |
|
---|
[237] | 1528 | for (i = 0; i < 80; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 0 */
|
---|
| 1529 | for (i = 0; i < 118; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 1 */
|
---|
| 1530 | for (i = 0; i < 119; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 2 */
|
---|
| 1531 | for (i = 0; i < 120; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 3 */
|
---|
| 1532 | for (i = 0; i < 121; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 4 */
|
---|
[183] | 1533 | for (i = 0; i < 238; ++i) { fputc ('a', fp); } fputc ('\n', fp);
|
---|
| 1534 | for (i = 0; i < 239; ++i) { fputc ('a', fp); } fputc ('\n', fp);
|
---|
| 1535 | for (i = 0; i < 240; ++i) { fputc ('a', fp); } fputc ('\n', fp);
|
---|
| 1536 | for (i = 0; i < 241; ++i) { fputc ('a', fp); } fputc ('\n', fp);
|
---|
| 1537 |
|
---|
| 1538 | rewind(fp);
|
---|
| 1539 |
|
---|
| 1540 | for (i = 0; i < 9; ++i)
|
---|
| 1541 | {
|
---|
| 1542 | status = (int) sh_string_read(s, fp, 120);
|
---|
| 1543 | printf("%d: status = %d, len = %d, size = %d\n",
|
---|
| 1544 | i, status, (int)s->len, (int)s->siz);
|
---|
| 1545 | if (status == -2)
|
---|
| 1546 | (void) sh_string_read(s, fp, 240);
|
---|
| 1547 | else
|
---|
| 1548 | printf("%s\n", s->str);
|
---|
| 1549 | }
|
---|
| 1550 |
|
---|
| 1551 | rewind(fp);
|
---|
| 1552 |
|
---|
| 1553 | (void) sh_string_truncate(s, 0);
|
---|
| 1554 |
|
---|
| 1555 | for (i = 0; i < 9; ++i)
|
---|
| 1556 | {
|
---|
| 1557 | status = (int) sh_string_read(s, fp, 240);
|
---|
| 1558 | printf("%d: status = %d, len = %d, size = %d\n",
|
---|
| 1559 | i, status, (int)s->len, (int)s->siz);
|
---|
| 1560 | if (status == -2)
|
---|
| 1561 | (void) sh_string_read(s, fp, 240);
|
---|
| 1562 | else
|
---|
| 1563 | {
|
---|
| 1564 | for (status = 0; status < (int)s->len; ++status)
|
---|
| 1565 | {
|
---|
| 1566 | if (s->str[status] != 'a')
|
---|
| 1567 | {
|
---|
| 1568 | break;
|
---|
| 1569 | }
|
---|
| 1570 | }
|
---|
| 1571 | printf("%d %s\n", status, s->str);
|
---|
| 1572 | }
|
---|
| 1573 | }
|
---|
| 1574 |
|
---|
[252] | 1575 | sl_fclose(FIL__, __LINE__, fp); remove(template);
|
---|
[183] | 1576 |
|
---|
| 1577 |
|
---|
| 1578 |
|
---|
| 1579 | return 0;
|
---|
| 1580 | }
|
---|
| 1581 | #endif
|
---|
| 1582 |
|
---|
| 1583 | /* #ifdef USE_LOGFILE_MONITOR */
|
---|
| 1584 | #endif
|
---|
| 1585 |
|
---|