"notify": address Joachim Wiberg's comments (pull/45)
This commit is contained in:
parent
bc103013cb
commit
7038e51a0f
@ -190,6 +190,7 @@ int decode(char *name, struct _code *codetab);
|
|||||||
static void logit(char *, ...);
|
static void logit(char *, ...);
|
||||||
static void notifier_add(struct notifiers *newn, const char *program);
|
static void notifier_add(struct notifiers *newn, const char *program);
|
||||||
static void notifier_invoke(const char *logfile);
|
static void notifier_invoke(const char *logfile);
|
||||||
|
static void notifier_free_all(void);
|
||||||
void reload(int);
|
void reload(int);
|
||||||
static int validate(struct sockaddr *sa, const char *hname);
|
static int validate(struct sockaddr *sa, const char *hname);
|
||||||
static int waitdaemon(int);
|
static int waitdaemon(int);
|
||||||
@ -2260,7 +2261,6 @@ void debug_switch(int signo)
|
|||||||
*/
|
*/
|
||||||
static void close_open_log_files(void)
|
static void close_open_log_files(void)
|
||||||
{
|
{
|
||||||
struct notifier *np = NULL, *npnext = NULL;
|
|
||||||
struct filed *f = NULL, *next = NULL;
|
struct filed *f = NULL, *next = NULL;
|
||||||
|
|
||||||
SIMPLEQ_FOREACH_SAFE(f, &fhead, f_link, next) {
|
SIMPLEQ_FOREACH_SAFE(f, &fhead, f_link, next) {
|
||||||
@ -2287,9 +2287,6 @@ static void close_open_log_files(void)
|
|||||||
|
|
||||||
free(f);
|
free(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
SIMPLEQ_FOREACH_SAFE(np, ¬head, n_link, npnext)
|
|
||||||
free(np);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void die(int signo)
|
void die(int signo)
|
||||||
@ -2565,6 +2562,12 @@ static void init(void)
|
|||||||
close_open_log_files();
|
close_open_log_files();
|
||||||
|
|
||||||
fhead = newf;
|
fhead = newf;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Free all notifiers
|
||||||
|
*/
|
||||||
|
notifier_free_all();
|
||||||
|
|
||||||
nothead = newn;
|
nothead = newn;
|
||||||
|
|
||||||
Initialized = 1;
|
Initialized = 1;
|
||||||
@ -3370,23 +3373,24 @@ static void logit(char *fmt, ...)
|
|||||||
static void notifier_add(struct notifiers *newn, const char *program)
|
static void notifier_add(struct notifiers *newn, const char *program)
|
||||||
{
|
{
|
||||||
while (*program && isspace(*program))
|
while (*program && isspace(*program))
|
||||||
program++;
|
++program;
|
||||||
|
|
||||||
/* Check whether it is accessible, regardless of TOCTOU */
|
/* Check whether it is accessible, regardless of TOCTOU */
|
||||||
if (!access(program, X_OK)) {
|
if (!access(program, X_OK)) {
|
||||||
struct notifier *np;
|
struct notifier *np;
|
||||||
size_t len;
|
|
||||||
|
|
||||||
len = strlen(program);
|
np = calloc(1, sizeof(*np));
|
||||||
|
if (!np) {
|
||||||
np = calloc(1, sizeof(*np) + len +1);
|
|
||||||
if (np) {
|
|
||||||
/* xxx Actually wastes space -- vararray? */
|
|
||||||
np->n_program = (char*)&np[1];
|
|
||||||
memcpy(np->n_program, program, len);
|
|
||||||
SIMPLEQ_INSERT_TAIL(newn, np, n_link);
|
|
||||||
} else
|
|
||||||
ERR("Cannot allocate memory for a notify program");
|
ERR("Cannot allocate memory for a notify program");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
np->n_program = strdup(program);
|
||||||
|
if (!np->n_program) {
|
||||||
|
free (np);
|
||||||
|
ERR("Cannot allocate memory for a notify program");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SIMPLEQ_INSERT_TAIL(newn, np, n_link);
|
||||||
} else
|
} else
|
||||||
logit("notify: non-existing, or not executable program\n");
|
logit("notify: non-existing, or not executable program\n");
|
||||||
}
|
}
|
||||||
@ -3394,31 +3398,42 @@ static void notifier_add(struct notifiers *newn, const char *program)
|
|||||||
static void notifier_invoke(const char *logfile)
|
static void notifier_invoke(const char *logfile)
|
||||||
{
|
{
|
||||||
char *argv[3];
|
char *argv[3];
|
||||||
|
int childpid;
|
||||||
struct notifier *np;
|
struct notifier *np;
|
||||||
|
|
||||||
logit("notify: rotated %s, invoking hooks\n", logfile);
|
logit("notify: rotated %s, invoking hooks\n", logfile);
|
||||||
|
|
||||||
SIMPLEQ_FOREACH(np, ¬head, n_link) {
|
SIMPLEQ_FOREACH(np, ¬head, n_link) {
|
||||||
switch (fork()) {
|
childpid = fork();
|
||||||
|
|
||||||
|
switch (childpid) {
|
||||||
case -1:
|
case -1:
|
||||||
ERR("Cannot start notifier %s", np->n_program);
|
ERR("Cannot start notifier %s", np->n_program);
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
/* No specific I/O setup, just use what we had */
|
|
||||||
argv[0] = np->n_program;
|
argv[0] = np->n_program;
|
||||||
argv[1] = (char*)logfile; /* logical unconst */
|
argv[1] = (char*)logfile;
|
||||||
argv[2] = NULL;
|
argv[2] = NULL;
|
||||||
execv(argv[0], argv);
|
execv(argv[0], argv);
|
||||||
_exit(1);
|
_exit(1);
|
||||||
default:
|
default:
|
||||||
/* Do not care beside that, no special process group
|
logit("notify: forked child pid %d for %s\n",
|
||||||
* etc.; it will eventually be reaped via reapchild() */
|
childpid, np->n_program);
|
||||||
logit("notify: forked for %s\n", np->n_program);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void notifier_free_all(void)
|
||||||
|
{
|
||||||
|
struct notifier *np, *npnext;
|
||||||
|
|
||||||
|
SIMPLEQ_FOREACH_SAFE(np, ¬head, n_link, npnext) {
|
||||||
|
free(np->n_program);
|
||||||
|
free(np);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 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…
Reference in New Issue
Block a user