Fix check for sysklogd logger tool at runtime, replacing logit
The sysklogd logger is capable of logging the PID of the actual service,
rather than just the logger tool itself (which would be a differet PID
every time).
@@ -242,6 +242,7 @@ static void fallback_logger(char *ident, char *prio)
*/
static int lredirect(svc_t *svc)
{
+ static int have_sysklogd = -1;
pid_t pid;
int fd;
@@ -260,6 +261,31 @@ static int lredirect(svc_t *svc)
return -1;
}
+ /*
+ * First time, check if we have sysklogd logger tool.
+ * It supports logging the actual PID of the service.
+ */
+ if (have_sysklogd == -1) {
+ FILE *pp;
+
+ pp = popen("logger -h 2>/dev/null", "r");
+ if (pp) {
+ char buf[128];
+
+ have_sysklogd = 0;
+ while (fgets(buf, sizeof(buf), pp)) {
+ if (strstr(buf, "-I PID")) {
+ have_sysklogd = 1;
+ break;
+ }
+ }
+
+ pclose(pp);
+ } else {
+ have_sysklogd = 0;
+ }
+ }
+
pid = fork();
if (pid == 0) {
char *prio = "daemon.info";
@@ -281,19 +307,27 @@ static int lredirect(svc_t *svc)
/* Default syslog identity name[:id] */
tag = svc_ident(svc, buf, sizeof(buf));
- if (!whichp(_PATH_LOGIT)) {
+ if (!have_sysklogd && !whichp(_PATH_LOGIT)) {
logit(LOG_INFO, _PATH_LOGIT " missing, using syslog for %s instead", svc->name);
fallback_logger(tag, prio);
_exit(0);
}
if (svc->log.file[0] == '/') {
+ if (have_sysklogd) {
+ char rot[25], pid[16];
+
+ snprintf(rot, sizeof(rot), "%d:%d", logfile_size_max, logfile_count_max);
+ snprintf(pid, sizeof(pid), "%d", getppid());
+ execlp("logger", "logger", "-f", svc->log.file, "-b", "-t", tag, "-p", prio, "-I", pid, "-r", rot, NULL);
+ } else {
char sz[20], num[3];
snprintf(sz, sizeof(sz), "%d", logfile_size_max);
snprintf(num, sizeof(num), "%d", logfile_count_max);
execlp(_PATH_LOGIT, "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL);
+ }
_exit(1);
}
@@ -302,7 +336,14 @@ static int lredirect(svc_t *svc)
if (svc->log.prio[0])
prio = svc->log.prio;
+ if (have_sysklogd) {
+ char pid[16];
+
+ snprintf(pid, sizeof(pid), "%d", getppid());
+ execlp("logger", "logger", "-t", tag, "-p", prio, "-I", pid, NULL);
+ } else {
execlp(_PATH_LOGIT, "logit", "-t", tag, "-p", prio, NULL);
+ }
_exit(1);
}
