Changeset View
Changeset View
Standalone View
Standalone View
daemon_syslog.c
Context not available. | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <string.h> | |||||
static void dummy_sighandler(int); | static void dummy_sighandler(int); | ||||
static void restrict_process(const char *); | static void restrict_process(const char *); | ||||
Context not available. | |||||
{ | { | ||||
struct pidfh *ppfh, *pfh; | struct pidfh *ppfh, *pfh; | ||||
sigset_t mask, oldmask; | sigset_t mask, oldmask; | ||||
int ch, nochdir, noclose, restart, serrno; | int ch, nochdir, noclose, restart, serrno, syslog; | ||||
const char *pidfile, *ppidfile, *user; | const char *pidfile, *ppidfile, *user, *tag, *priority; | ||||
pid_t otherpid, pid; | pid_t otherpid, pid; | ||||
nochdir = noclose = 1; | nochdir = noclose = 1; | ||||
restart = 0; | restart = 0; | ||||
ppidfile = pidfile = user = NULL; | syslog = 0; | ||||
while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) { | ppidfile = pidfile = user = tag = priority = NULL; | ||||
while ((ch = getopt(argc, argv, "cfp:P:ru:t:sh:")) != -1) { | |||||
switch (ch) { | switch (ch) { | ||||
case 'c': | case 'c': | ||||
nochdir = 0; | nochdir = 0; | ||||
Context not available. | |||||
case 'f': | case 'f': | ||||
noclose = 0; | noclose = 0; | ||||
break; | break; | ||||
case 'h': | |||||
priority = optarg; | |||||
break; | |||||
case 'p': | case 'p': | ||||
pidfile = optarg; | pidfile = optarg; | ||||
break; | break; | ||||
Context not available. | |||||
case 'r': | case 'r': | ||||
restart = 1; | restart = 1; | ||||
break; | break; | ||||
case 's': | |||||
syslog = 1; | |||||
break; | |||||
case 't': | |||||
tag = optarg; | |||||
break; | |||||
case 'u': | case 'u': | ||||
user = optarg; | user = optarg; | ||||
break; | break; | ||||
Context not available. | |||||
if (argc == 0) | if (argc == 0) | ||||
usage(); | usage(); | ||||
ppfh = pfh = NULL; | ppfh = pfh = NULL; | ||||
/* | /* | ||||
Context not available. | |||||
if (user != NULL) | if (user != NULL) | ||||
restrict_process(user); | restrict_process(user); | ||||
/* Grell's code */ | |||||
garga: I don't think this comment is relevant | |||||
if(syslog) | |||||
{ | |||||
FILE *fl; | |||||
char logger_cmd[200] = "logger"; | |||||
char tagstring[200]; | |||||
char prioritystring[200]; | |||||
jhbUnsubmitted Not Done Inline ActionsRather than using fixed-size strings, consider using open_memstream(). It lets you use stdio to build a dynamically-sized string. (sbuf is also an option but is in a separate library.) You would do something like this: if (syslog) { FILE *fp; char *logger_cmd; size_t len; int fd; fp = open_memstream(&logger_cmd, &len); fprintf(fp, "logger"); if (tag != NULL) fprintf(fp, " -t %s", tag); if (priority != NULL) fprintf(fp, "-p %s", priority); fclose(fp); fp = popen(logger_cmd, "w"); if (fp == NULL) goto exit; fd = fileno(fp); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); close(fd); } jhb: Rather than using fixed-size strings, consider using open_memstream(). It lets you use stdio… | |||||
if(tag != NULL){ | |||||
snprintf(tagstring, sizeof tagstring, " -t %s", tag); | |||||
strncat(logger_cmd, tagstring, 199); | |||||
} | |||||
if(priority != NULL){ /* user specifies the facility and priority */ | |||||
snprintf(prioritystring, sizeof prioritystring, " -p %s", priority); | |||||
strncat(logger_cmd, prioritystring, 199); | |||||
} | |||||
fl = popen(logger_cmd,"w"); | |||||
if(fl == NULL){ | |||||
goto exit; | |||||
} | |||||
int nf; | |||||
nf = fileno(fl); | |||||
dup2(nf,STDOUT_FILENO); | |||||
dup2(nf,STDERR_FILENO); | |||||
execvp(argv[0], argv); | |||||
} | |||||
/* end Grell's code */ | |||||
gargaUnsubmitted Not Done Inline ActionsThis comment is also not relevant garga: This comment is also not relevant | |||||
execvp(argv[0], argv); | execvp(argv[0], argv); | ||||
/* | /* | ||||
* execvp() failed -- report the error. The child is | * execvp() failed -- report the error. The child is | ||||
* now running, so the exit status doesn't matter. | * now running, so the exit status doesn't matter. | ||||
Context not available. | |||||
usage(void) | usage(void) | ||||
{ | { | ||||
(void)fprintf(stderr, | (void)fprintf(stderr, | ||||
"usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] " | "usage: daemon [-cfrs] [-h facility.priority] [-p child_pidfile] [-P supervisor_pidfile] [-t custom_tag] " | ||||
"[-u user]\n command arguments ...\n"); | "[-u user]\n command arguments ...\n"); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
Context not available. |
I don't think this comment is relevant