Refactor, use linked list of struct filed instead of realloc'd array
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
parent
a1e3efc3c4
commit
5114907afe
115
src/syslogd.c
115
src/syslogd.c
@ -689,9 +689,8 @@ const char *sys_h_errlist[] = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
struct filed {
|
struct filed {
|
||||||
#ifndef SYSV
|
|
||||||
struct filed *f_next; /* next in linked list */
|
struct filed *f_next; /* next in linked list */
|
||||||
#endif
|
|
||||||
short f_type; /* entry type, see below */
|
short f_type; /* entry type, see below */
|
||||||
short f_file; /* file descriptor */
|
short f_file; /* file descriptor */
|
||||||
time_t f_time; /* time this was last written */
|
time_t f_time; /* time this was last written */
|
||||||
@ -860,7 +859,6 @@ static int strtobytes(char *arg);
|
|||||||
void cfline(char *line, struct filed *f);
|
void cfline(char *line, struct filed *f);
|
||||||
int decode(char *name, struct code *codetab);
|
int decode(char *name, struct code *codetab);
|
||||||
static void logit(char *, ...);
|
static void logit(char *, ...);
|
||||||
static void allocate_log(void);
|
|
||||||
void sighup_handler(int);
|
void sighup_handler(int);
|
||||||
static int create_unix_socket(const char *path);
|
static int create_unix_socket(const char *path);
|
||||||
static int *create_inet_sockets();
|
static int *create_inet_sockets();
|
||||||
@ -1678,7 +1676,7 @@ void logmsg(int pri, char *msg, const char *from, int flags)
|
|||||||
struct filed *f;
|
struct filed *f;
|
||||||
sigset_t mask;
|
sigset_t mask;
|
||||||
char *timestamp;
|
char *timestamp;
|
||||||
int fac, prilev, lognum, msglen;
|
int fac, prilev, msglen;
|
||||||
|
|
||||||
logit("logmsg: %s, flags %x, from %s, msg %s\n", textpri(pri), flags, from, msg);
|
logit("logmsg: %s, flags %x, from %s, msg %s\n", textpri(pri), flags, from, msg);
|
||||||
|
|
||||||
@ -1728,13 +1726,7 @@ void logmsg(int pri, char *msg, const char *from, int flags)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SYSV
|
|
||||||
for (lognum = 0; lognum <= nlogs; lognum++) {
|
|
||||||
f = &Files[lognum];
|
|
||||||
#else
|
|
||||||
for (f = Files; f; f = f->f_next) {
|
for (f = Files; f; f = f->f_next) {
|
||||||
#endif
|
|
||||||
|
|
||||||
/* skip messages that are incorrect priority */
|
/* skip messages that are incorrect priority */
|
||||||
if ((f->f_pmask[fac] == TABLE_NOPRI) ||
|
if ((f->f_pmask[fac] == TABLE_NOPRI) ||
|
||||||
((f->f_pmask[fac] & (1 << prilev)) == 0))
|
((f->f_pmask[fac] & (1 << prilev)) == 0))
|
||||||
@ -1813,9 +1805,6 @@ void logmsg(int pri, char *msg, const char *from, int flags)
|
|||||||
|
|
||||||
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
||||||
}
|
}
|
||||||
#if FALSE
|
|
||||||
} /* balance parentheses for emacs */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void logrotate(struct filed *f)
|
void logrotate(struct filed *f)
|
||||||
{
|
{
|
||||||
@ -2208,7 +2197,6 @@ void fprintlog(struct filed *f, char *from, int flags, char *msg)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#if FALSE
|
#if FALSE
|
||||||
}
|
|
||||||
} /* balance parentheses for emacs */
|
} /* balance parentheses for emacs */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -2416,9 +2404,6 @@ const char *cvthname(struct sockaddr_storage *f, int len)
|
|||||||
void domark(int signo)
|
void domark(int signo)
|
||||||
{
|
{
|
||||||
struct filed *f;
|
struct filed *f;
|
||||||
#ifdef SYSV
|
|
||||||
int lognum;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (MarkInterval > 0) {
|
if (MarkInterval > 0) {
|
||||||
now = time(0);
|
now = time(0);
|
||||||
@ -2429,12 +2414,7 @@ void domark(int signo)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SYSV
|
|
||||||
for (lognum = 0; lognum <= nlogs; lognum++) {
|
|
||||||
f = &Files[lognum];
|
|
||||||
#else
|
|
||||||
for (f = Files; f; f = f->f_next) {
|
for (f = Files; f; f = f->f_next) {
|
||||||
#endif
|
|
||||||
if (f->f_prevcount && now >= REPEATTIME(f)) {
|
if (f->f_prevcount && now >= REPEATTIME(f)) {
|
||||||
logit("flush %s: repeated %d times, %d sec.\n",
|
logit("flush %s: repeated %d times, %d sec.\n",
|
||||||
TypeNames[f->f_type], f->f_prevcount,
|
TypeNames[f->f_type], f->f_prevcount,
|
||||||
@ -2475,7 +2455,6 @@ void logerror(const char *type)
|
|||||||
(void)snprintf(buf, sizeof(buf), "syslogd: %s: %s", type, strerror(errno));
|
(void)snprintf(buf, sizeof(buf), "syslogd: %s: %s", type, strerror(errno));
|
||||||
errno = 0;
|
errno = 0;
|
||||||
logmsg(LOG_SYSLOG | LOG_ERR, buf, LocalHostName, ADDDATE);
|
logmsg(LOG_SYSLOG | LOG_ERR, buf, LocalHostName, ADDDATE);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void die(int signo)
|
void die(int signo)
|
||||||
@ -2540,11 +2519,7 @@ void doexit(int signo)
|
|||||||
*/
|
*/
|
||||||
void init(void)
|
void init(void)
|
||||||
{
|
{
|
||||||
#ifndef TESTING
|
|
||||||
#ifndef SYSV
|
|
||||||
struct filed **nextp = NULL;
|
struct filed **nextp = NULL;
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
struct hostent *hent;
|
struct hostent *hent;
|
||||||
struct filed *f;
|
struct filed *f;
|
||||||
unsigned int Forwarding = 0;
|
unsigned int Forwarding = 0;
|
||||||
@ -2592,11 +2567,7 @@ void init(void)
|
|||||||
Files = NULL;
|
Files = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SYSV
|
|
||||||
lognum = 0;
|
|
||||||
#else
|
|
||||||
f = NULL;
|
f = NULL;
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Get hostname */
|
/* Get hostname */
|
||||||
(void)gethostname(LocalHostName, sizeof(LocalHostName));
|
(void)gethostname(LocalHostName, sizeof(LocalHostName));
|
||||||
@ -2636,21 +2607,28 @@ void init(void)
|
|||||||
/* open the configuration file */
|
/* open the configuration file */
|
||||||
if ((cf = fopen(ConfFile, "r")) == NULL) {
|
if ((cf = fopen(ConfFile, "r")) == NULL) {
|
||||||
logit("cannot open %s.\n", ConfFile);
|
logit("cannot open %s.\n", ConfFile);
|
||||||
#ifdef SYSV
|
|
||||||
allocate_log();
|
|
||||||
f = &Files[lognum++];
|
|
||||||
#ifndef TESTING
|
#ifndef TESTING
|
||||||
cfline("*.err\t" _PATH_CONSOLE, f);
|
cfline("*.err\t" _PATH_CONSOLE, f);
|
||||||
#else
|
#else
|
||||||
snprintf(cbuf, sizeof(cbuf), "*.*\t%s", ttyname(0));
|
snprintf(cbuf, sizeof(cbuf), "*.*\t%s", ttyname(0));
|
||||||
cfline(cbuf, f);
|
cfline(cbuf, f);
|
||||||
#endif
|
#endif
|
||||||
#else
|
|
||||||
*nextp = calloc(1, sizeof(*f));
|
*nextp = calloc(1, sizeof(*f));
|
||||||
|
if (!*nextp) {
|
||||||
|
logerror("Cannot allocate memory for log target/file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
cfline("*.ERR\t" _PATH_CONSOLE, *nextp);
|
cfline("*.ERR\t" _PATH_CONSOLE, *nextp);
|
||||||
(*nextp)->f_next = calloc(1, sizeof(*f)) /* ASP */
|
|
||||||
|
(*nextp)->f_next = calloc(1, sizeof(*f)); /* ASP */
|
||||||
|
if (!*nextp) {
|
||||||
|
logerror("Cannot allocate memory for log target/file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
cfline("*.PANIC\t*", (*nextp)->f_next);
|
cfline("*.PANIC\t*", (*nextp)->f_next);
|
||||||
#endif
|
|
||||||
Initialized = 1;
|
Initialized = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -2686,13 +2664,18 @@ void init(void)
|
|||||||
cline = cbuf;
|
cline = cbuf;
|
||||||
|
|
||||||
*++p = '\0';
|
*++p = '\0';
|
||||||
#ifndef SYSV
|
|
||||||
f = (struct filed *)calloc(1, sizeof(*f));
|
f = (struct filed *)calloc(1, sizeof(*f));
|
||||||
|
if (!f) {
|
||||||
|
logerror("Cannot allocate memory for log file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!nextp)
|
||||||
|
Files = f;
|
||||||
|
else
|
||||||
*nextp = f;
|
*nextp = f;
|
||||||
nextp = &f->f_next;
|
nextp = &f->f_next;
|
||||||
#endif
|
|
||||||
allocate_log();
|
|
||||||
f = &Files[lognum++];
|
|
||||||
|
|
||||||
cfline(cbuf, f);
|
cfline(cbuf, f);
|
||||||
if (f->f_type == F_FORW || f->f_type == F_FORW_SUSP || f->f_type == F_FORW_UNKN) {
|
if (f->f_type == F_FORW || f->f_type == F_FORW_SUSP || f->f_type == F_FORW_UNKN) {
|
||||||
@ -2735,15 +2718,8 @@ void init(void)
|
|||||||
Initialized = 1;
|
Initialized = 1;
|
||||||
|
|
||||||
if (Debug) {
|
if (Debug) {
|
||||||
#ifdef SYSV
|
|
||||||
for (lognum = 0; lognum <= nlogs; lognum++) {
|
|
||||||
f = &Files[lognum];
|
|
||||||
if (f->f_type != F_UNUSED) {
|
|
||||||
printf("%2d: ", lognum);
|
|
||||||
#else
|
|
||||||
for (f = Files; f; f = f->f_next) {
|
for (f = Files; f; f = f->f_next) {
|
||||||
if (f->f_type != F_UNUSED) {
|
if (f->f_type != F_UNUSED) {
|
||||||
#endif
|
|
||||||
for (i = 0; i <= LOG_NFACILITIES; i++)
|
for (i = 0; i <= LOG_NFACILITIES; i++)
|
||||||
if (f->f_pmask[i] == TABLE_NOPRI)
|
if (f->f_pmask[i] == TABLE_NOPRI)
|
||||||
printf(" X ");
|
printf(" X ");
|
||||||
@ -2785,10 +2761,6 @@ void init(void)
|
|||||||
(void)signal(SIGHUP, sighup_handler);
|
(void)signal(SIGHUP, sighup_handler);
|
||||||
logit("syslogd: restarted.\n");
|
logit("syslogd: restarted.\n");
|
||||||
}
|
}
|
||||||
#if FALSE
|
|
||||||
}
|
|
||||||
} /* balance parentheses for emacs */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int strtobytes(char *arg)
|
static int strtobytes(char *arg)
|
||||||
{
|
{
|
||||||
@ -2837,9 +2809,7 @@ void cfline(char *line, struct filed *f)
|
|||||||
errno = 0; /* keep strerror() stuff out of logerror messages */
|
errno = 0; /* keep strerror() stuff out of logerror messages */
|
||||||
|
|
||||||
/* clear out file entry */
|
/* clear out file entry */
|
||||||
#ifndef SYSV
|
|
||||||
memset((char *)f, 0, sizeof(*f));
|
memset((char *)f, 0, sizeof(*f));
|
||||||
#endif
|
|
||||||
for (i = 0; i <= LOG_NFACILITIES; i++) {
|
for (i = 0; i <= LOG_NFACILITIES; i++) {
|
||||||
f->f_pmask[i] = TABLE_NOPRI;
|
f->f_pmask[i] = TABLE_NOPRI;
|
||||||
f->f_flags = 0;
|
f->f_flags = 0;
|
||||||
@ -3125,43 +3095,6 @@ static void logit(char *fmt, ...)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* The following function is responsible for allocating/reallocating the
|
|
||||||
* array which holds the structures which define the logging outputs.
|
|
||||||
*/
|
|
||||||
static void allocate_log(void)
|
|
||||||
{
|
|
||||||
logit("Called allocate_log, nlogs = %d.\n", nlogs);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Decide whether the array needs to be initialized or needs to
|
|
||||||
* grow.
|
|
||||||
*/
|
|
||||||
if (nlogs == -1) {
|
|
||||||
Files = malloc(sizeof(struct filed));
|
|
||||||
if (Files == NULL) {
|
|
||||||
logit("Cannot initialize log structure.");
|
|
||||||
logerror("Cannot initialize log structure.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* Re-allocate the array. */
|
|
||||||
Files = realloc(Files, (nlogs + 2) * sizeof(struct filed));
|
|
||||||
if (Files == NULL) {
|
|
||||||
logit("Cannot grow log structure.");
|
|
||||||
logerror("Cannot grow log structure.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Initialize the array element, bump the number of elements in the
|
|
||||||
* the array and return.
|
|
||||||
*/
|
|
||||||
++nlogs;
|
|
||||||
memset(&Files[nlogs], '\0', sizeof(struct filed));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The following function is resposible for handling a SIGHUP signal. Since
|
* The following function is resposible for handling a SIGHUP signal. Since
|
||||||
* we are now doing mallocs/free as part of init we had better not being
|
* we are now doing mallocs/free as part of init we had better not being
|
||||||
|
Loading…
x
Reference in New Issue
Block a user