Skip to content

Commit

Permalink
Extend .conf syntax for 'log' option, issue #21
Browse files Browse the repository at this point in the history
This is the first patch in a series to add support for

    log:/path/to/logfile,priority:facility.level,tag:ident

In this patch support for 'priority:' and 'tag:', log file support
coming later.

Signed-off-by: Joachim Nilsson <[email protected]>
  • Loading branch information
troglobit committed Jan 15, 2018
1 parent 44591c6 commit cbd4cc8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
57 changes: 44 additions & 13 deletions src/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static int service_start(svc_t *svc)
} else
#endif

if (svc->log) {
if (svc->log.enabled) {
int fd;

/*
Expand All @@ -222,21 +222,24 @@ static int service_start(svc_t *svc)
*/
fd = posix_openpt(O_RDWR);
if (fd == -1) {
svc->log = 0;
svc->log.enabled = 0;
goto logger_err;
}
if (grantpt(fd) == -1 || unlockpt(fd) == -1) {
close(fd);
svc->log = 0;
svc->log.enabled = 0;
goto logger_err;
}

/* SIGCHLD is still blocked for grantpt() and fork() */
sigprocmask(SIG_BLOCK, &nmask, NULL);
pid = fork();
if (pid == 0) {
int fds = open(ptsname(fd), O_RDONLY);
int fds;
char *tag = basename(svc->cmd);
char *prio = "daemon.info";

fds = open(ptsname(fd), O_RDONLY);
close(fd);
if (fds == -1)
_exit(0);
Expand All @@ -245,7 +248,12 @@ static int service_start(svc_t *svc)
/* Reset signals */
sig_unblock();

execlp("logger", "logger", "-t", svc->cmd, "-p", "daemon.info", NULL);
if (svc->log.ident[0])
tag = svc->log.ident;
if (svc->log.prio[0])
prio = svc->log.prio;

execlp("logger", "logger", "-t", tag, "-p", prio, NULL);
_exit(0);
}

Expand Down Expand Up @@ -294,7 +302,7 @@ static int service_start(svc_t *svc)
}
} else
#endif
if (svc->log)
if (svc->log.enabled)
waitpid(pid, NULL, 0);
exit(status);
} else if (log_is_debug()) {
Expand Down Expand Up @@ -495,6 +503,28 @@ void service_runlevel(int newlevel)
networking(0);
}

/*
* log:/path/to/logfile,priority:facility.level,tag:ident
*/
static void parse_log(svc_t *svc, char *arg)
{
char *tok;

tok = strtok(arg, ":, ");
while (tok) {
if (!strcmp(tok, "log"))
svc->log.enabled = 1;
else if (tok[0] == '/')
strlcpy(svc->log.file, tok, sizeof(svc->log.file));
else if (!strcmp(tok, "priority") || !strcmp(tok, "prio"))
strlcpy(svc->log.prio, strtok(NULL, ","), sizeof(svc->log.prio));
else if (!strcmp(tok, "tag") || !strcmp(tok, "identity") || !strcmp(tok, "ident"))
strlcpy(svc->log.ident, strtok(NULL, ","), sizeof(svc->log.ident));

tok = strtok(NULL, ":=, ");
}
}

/**
* service_register - Register service, task or run commands
* @type: %SVC_TYPE_SERVICE(0), %SVC_TYPE_TASK(1), %SVC_TYPE_RUN(2)
Expand Down Expand Up @@ -554,9 +584,9 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
#ifdef INETD_ENABLED
int forking = 0;
#endif
int log = 0, levels = 0;
int levels = 0;
char *line;
char *username = NULL, *pid = NULL;
char *username = NULL, *log = NULL, *pid = NULL;
char *service = NULL, *proto = NULL, *ifaces = NULL;
char *cmd, *desc, *runlevels = NULL, *cond = NULL;
svc_t *svc;
Expand Down Expand Up @@ -604,7 +634,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
forking = 0;
#endif
else if (!strncasecmp(cmd, "log", 3))
log = 1;
log = cmd;
else if (!strncasecmp(cmd, "pid", 3))
pid = cmd;
else if (cmd[0] != '/' && strchr(cmd, '/'))
Expand Down Expand Up @@ -693,10 +723,6 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
}
#endif

svc->log = log;
if (desc)
strlcpy(svc->desc, desc, sizeof(svc->desc));

/* Always clear svc PID file, for now. See TODO */
svc->pidfile[0] = 0;
/* Decode any optional pid:/optional/path/to/file.pid */
Expand Down Expand Up @@ -729,6 +755,11 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)

conf_parse_cond(svc, cond);

if (log)
parse_log(svc, log);
if (desc)
strlcpy(svc->desc, desc, sizeof(svc->desc));

#ifdef INETD_ENABLED
if (svc_is_inetd(svc)) {
char *iface, *name = service;
Expand Down
7 changes: 6 additions & 1 deletion src/svc.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ typedef struct svc {
int stdin_fd;

/* Set for services we need to redirect stdout/stderr to syslog */
int log;
struct {
int enabled;
char file[64];
char prio[20];
char ident[20];
} log;

/* Identity */
char username[MAX_USER_LEN];
Expand Down

0 comments on commit cbd4cc8

Please sign in to comment.