⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metalog.c

📁 Metalog is a modern replacement for syslogd and klogd. The logged messages can be dispatched accord
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <config.h>#define SYSLOG_NAMES 1#include "metalog.h"#include "metalog_p.h"#ifdef WITH_DMALLOC# include <dmalloc.h>#endifstatic int configParser(const char * const file){    pcre *re_newblock;    pcre *re_newstmt;    pcre *re_str;    pcre *re_comment;        pcre *re_null;    const char *errptr;      const char *keyword;    const char *value;    int erroffset;    int ovector[16];    int stcount;    int retcode = 0;    FILE *fp;    ConfigBlock default_block = {            DEFAULT_MINIMUM,           /* minimum */            NULL,                      /* facilities */            0,                         /* nb_facilities */            NULL,                      /* regexes */            0,                         /* nb_regexes */            (off_t) DEFAULT_MAXSIZE,   /* maxsize */            DEFAULT_MAXFILES,          /* maxfiles */            (time_t) DEFAULT_MAXTIME,  /* maxtime */            NULL,                      /* output */            NULL,                      /* command */            NULL,                      /* program */                        NULL                       /* next_block */    };    ConfigBlock *cur_block = &default_block;    pcre *new_regex;    int line_size;    char line[LINE_MAX];    if ((fp = fopen(file, "rt")) == NULL) {        perror("Can't open the configuration file");        return -2;    }    re_newblock = pcre_compile(":\\s*$", 0, &errptr, &erroffset, NULL);    re_newstmt = pcre_compile("^\\s*(.+?)\\s*=\\s*\"?(.+?)\"?\\s*$", 0,                               &errptr, &erroffset, NULL);    re_str = pcre_compile("\"(.+)\"", 0, &errptr, &erroffset, NULL);        re_comment = pcre_compile("^\\s*#", 0, &errptr, &erroffset, NULL);            re_null = pcre_compile("^\\s*$", 0, &errptr, &erroffset, NULL);    if (re_newblock == NULL || re_newstmt == NULL ||        re_comment == NULL || re_null == NULL) {        fprintf(stderr, "Internal error : can't compile parser regexes\n");        retcode = -1;        goto rtn;    }    while (fgets(line, sizeof line, fp) != NULL) {        if ((line_size = (int) strlen(line)) == 0) {            continue;        }        if (line[line_size - 1] == '\n') {            if (line_size < 2) {                continue;            }            line[--line_size] = 0;        }        if (pcre_exec(re_null, NULL, line, line_size,                      0, 0, ovector, sizeof ovector / sizeof ovector[0]) >= 0 ||            pcre_exec(re_comment, NULL, line, line_size,                      0, 0, ovector, sizeof ovector / sizeof ovector[0]) >= 0) {            continue;        }         if (pcre_exec(re_newblock, NULL, line, line_size,                      0, 0, ovector, sizeof ovector / sizeof ovector[0]) >= 0) {            ConfigBlock *previous_block = cur_block;                        if ((cur_block = malloc(sizeof *cur_block)) == NULL) {                perror("Oh no ! More memory !");                retcode = -3;                goto rtn;            }            *cur_block = default_block;            if (config_blocks == NULL) {                config_blocks = cur_block;            } else {                previous_block->next_block = cur_block;            }            continue;        }        if ((stcount =              pcre_exec(re_newstmt, NULL, line, line_size,                       0, 0, ovector,                        sizeof ovector / sizeof ovector[0])) >= 3) {            pcre_get_substring(line, ovector, stcount, 1, &keyword);            pcre_get_substring(line, ovector, stcount, 2, &value);            if (strcasecmp(keyword, "minimum") == 0) {                cur_block->minimum = atoi(value);            } else if (strcasecmp(keyword, "facility") == 0) {                int n = 0;                int *new_facilities;                                if (*value == '*' && value[1] == 0) {                    if (cur_block->facilities != NULL) {                        free(cur_block->facilities);                    }                    cur_block->facilities = NULL;                    cur_block->nb_facilities = 0;                    continue;                }                while (facilitynames[n].c_name != NULL &&                       strcasecmp(facilitynames[n].c_name, value) != 0) {                    n++;                }                if (facilitynames[n].c_name == NULL) {                    fprintf(stderr, "Unknown facility : [%s]\n", value);                    retcode = -4;                    goto rtn;                }                if (cur_block->facilities == NULL) {                    if ((cur_block->facilities =                         malloc(sizeof *(cur_block->facilities))) == NULL) {                        perror("Oh no ! More memory !");                        retcode = -3;                        goto rtn;                    }                                    } else {                    if ((new_facilities =                         realloc(cur_block->facilities,                                 (cur_block->nb_facilities + 1) *                                 sizeof *(cur_block->facilities))) == NULL) {                        perror("Oh no ! More memory !");                        retcode = -3;                        goto rtn;                    }                                    }                cur_block->facilities[cur_block->nb_facilities] =                     LOG_FAC(facilitynames[n].c_val);                cur_block->nb_facilities++;            } else if (strcasecmp(keyword, "regex") == 0) {                const char *regex;                PCREInfo *new_regexes;                                if ((regex = strdup(value)) == NULL) {                    perror("Oh no ! More memory !");                    retcode = -3;                    goto rtn;                }                if (cur_block->regexes == NULL) {                    if ((cur_block->regexes =                          malloc(sizeof *(cur_block->regexes))) == NULL) {                        perror("Oh no ! More memory !");                        retcode = -3;                        goto rtn;                    }                                    } else {                    if ((new_regexes =                          realloc(cur_block->regexes,                                  (cur_block->nb_regexes + 1) *                                 sizeof *(cur_block->regexes))) == NULL) {                        perror("Oh no ! More memory !");                        retcode = -3;                        goto rtn;                    }                    cur_block->regexes = new_regexes;                }                if ((new_regex = pcre_compile(regex, PCRE_CASELESS, &errptr, &erroffset, NULL)) == NULL) {                    fprintf(stderr, "Invalid regex : [%s]\n", regex);                    return -5;                }                {                    PCREInfo * const pcre_info = &cur_block->regexes[cur_block->nb_regexes];                                        pcre_info->pcre = new_regex;                    pcre_info->pcre_extra = pcre_study(new_regex, 0, &errptr);                }                cur_block->nb_regexes++;            } else if (strcasecmp(keyword, "maxsize") == 0) {                cur_block->maxsize = (off_t) strtoull(value, NULL, 0);                if (cur_block->output != NULL) {                    cur_block->output->maxsize = cur_block->maxsize;                }                            } else if (strcasecmp(keyword, "maxfiles") == 0) {                cur_block->maxfiles = atoi(value);                if (cur_block->output != NULL) {                    cur_block->output->maxfiles = cur_block->maxfiles;                }                            } else if (strcasecmp(keyword, "maxtime") == 0) {                cur_block->maxtime = (time_t) strtoull(value, NULL, 0);                if (cur_block->output != NULL) {                    cur_block->output->maxtime = cur_block->maxtime;                }            } else if (strcasecmp(keyword, "logdir") == 0) {                char *logdir;                Output *outputs_scan = outputs;                Output *previous_scan = NULL;                Output *new_output;                                while (outputs_scan != NULL) {                    if (outputs_scan->directory != NULL &&                         strcmp(outputs_scan->directory, value) == 0) {                        cur_block->output = outputs_scan;                        goto duplicate_output;                    }                    previous_scan = outputs_scan;                    outputs_scan = outputs_scan->next_output;                }                if ((new_output = malloc(sizeof *new_output)) == NULL ||                    (logdir = strdup(value)) == NULL) {                    if (new_output != NULL) {                        free(new_output);                    }                    perror("Oh no ! More memory !");                    retcode = -3;                    goto rtn;                }                    new_output->directory = logdir;                new_output->fp = NULL;                new_output->size = (off_t) 0;                new_output->maxsize = cur_block->maxsize;                new_output->maxfiles = cur_block->maxfiles;                new_output->maxtime = cur_block->maxtime;                new_output->next_output = NULL;                if (previous_scan != NULL) {                    previous_scan->next_output = new_output;                } else {                    outputs = new_output;                }                cur_block->output = new_output;                duplicate_output:                (void) 0;            } else if (strcasecmp(keyword, "command") == 0) {                if ((cur_block->command = strdup(value)) == NULL) {                    perror("Oh no ! More memory !");                    retcode = -3;                    goto rtn;                }            } else if (strcasecmp(keyword, "program") == 0) {                if ((cur_block->program = strdup(value)) == NULL) {                    perror("Oh no ! More memory !");                    retcode = -3;                    goto rtn;                }            }            continue;        }    }            rtn:    if (re_newblock != NULL) {        pcre_free(re_newblock);    }    if (re_newstmt != NULL) {        pcre_free(re_newstmt);    }    if (re_comment != NULL) {        pcre_free(re_comment);    }    if (re_null != NULL) {        pcre_free(re_null);    }    fclose(fp);        return retcode;}static void clearargs(char **argv) {#if defined(__linux__) && !defined(HAVE_SETPROCTITLE)	if (*argv != NULL) {		argv++;	}	while (*argv != NULL) {				memset(*argv, 0, strlen(*argv));		argv++;	}#else	(void) argv;#endif}static void setprogname(const char * const title){#ifdef HAVE_SETPROCTITLE	setproctitle("-%s", title);#elif defined(__linux__)	if (prg_name != NULL) {		strncpy(prg_name, title, 31);		prg_name[31] = 0;			}#else	(void) title;#endif}static int getDataSources(int sockets[]){    struct sockaddr_un sa;#ifdef HAVE_KLOGCTL    int fdpipe[2];    pid_t pgid;#endif        if ((sockets[0] = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {        perror("Unable to create a local socket");        return -1;    }    sa.sun_family = AF_UNIX;    if (snprintf(sa.sun_path, sizeof sa.sun_path, "%s", SOCKNAME) < 0) {        fprintf(stderr, "Socket name too long");        close(sockets[0]);        return -2;    }    unlink(sa.sun_path);    if (bind(sockets[0], (struct sockaddr *) &sa, (socklen_t) sizeof sa) < 0) {        perror("Unable to bind a local socket");        close(sockets[0]);        return -1;    }    chmod(sa.sun_path, 0666);    sockets[1] = -1;#ifdef HAVE_KLOGCTL    if (pipe(fdpipe) < 0) {        perror("Unable to create a pipe");        close(sockets[0]);        return -3;    }        pgid = getpgrp();    if ((child = fork()) < (pid_t) 0) {        perror("Unable to create the klogd child");        close(sockets[0]);        return -3;    } else if (child == (pid_t) 0) {        int s;        int t;        int u;        ssize_t written;        size_t towrite;        char line[LINE_MAX];                        signal(SIGUSR1, SIG_IGN);        signal(SIGUSR2, SIG_IGN);        setpgid((pid_t) 0, pgid);        setprogname(PROGNAME_KERNEL);        if (klogctl(1, NULL, 0) < 0 ||

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -