Use svc_ident() for more cases

- Drop basename() of svc->cmd from remaining code
  - Replace uses of svc->cmd with svc_ident(svc)
  - Use svc_ident() as default log tag

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-05-13 13:36:33 +02:00
1 parent 467f1cca81
commit fdbd25c1b5
4 files changed
+29 -26

No files matched your search

+2 -2
View File
@@ -102,8 +102,8 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask)
service_forked(svc); service_forked(svc);
if (pid != svc->pid) { if (pid != svc->pid) {
_d("Forking service %s changed PID from %d to %d", _d("Forking service %s (cmd %s) changed PID from %d to %d",
svc->cmd, svc->pid, pid); svc_ident(svc, NULL, 0), svc->cmd, svc->pid, pid);
svc->pid = pid; svc->pid = pid;
/* Complement log in service.c for non-forking services */ /* Complement log in service.c for non-forking services */
+2 -2
View File
@@ -454,12 +454,12 @@ void conf_parse_cond(svc_t *svc, char *cond)
ptr[i] = 0; ptr[i] = 0;
if (i >= sizeof(svc->cond)) { if (i >= sizeof(svc->cond)) {
logit(LOG_WARNING, "Too long event list in declaration of %s: %s", svc->cmd, ptr); logit(LOG_WARNING, "Too long event list in declaration of %s: %s", svc_ident(svc, NULL, 0), ptr);
return; return;
} }
if (!strncmp(ptr, "svc/", 4)) { if (!strncmp(ptr, "svc/", 4)) {
logit(LOG_ERR, "Unsupported cond syntax for %s: <%s", svc->cmd, ptr); logit(LOG_ERR, "Unsupported cond syntax for %s: <%s", svc_ident(svc, NULL, 0), ptr);
return; return;
} }
+23 -20
View File
@@ -198,9 +198,10 @@ static int lredirect(svc_t *svc)
pid = fork(); pid = fork();
if (pid == 0) { if (pid == 0) {
int fds;
char *tag = basename(svc->cmd);
char *prio = "daemon.info"; char *prio = "daemon.info";
char buf[MAX_IDENT_LEN];
char *tag;
int fds;
sched_yield(); sched_yield();
@@ -213,6 +214,9 @@ static int lredirect(svc_t *svc)
/* Reset signals */ /* Reset signals */
sig_unblock(); sig_unblock();
/* Default syslog identity name[:id] */
tag = svc_ident(svc, buf, sizeof(buf));
if (!whichp(_PATH_LOGIT)) { if (!whichp(_PATH_LOGIT)) {
logit(LOG_INFO, _PATH_LOGIT " missing, using syslog for %s instead", svc->name); logit(LOG_INFO, _PATH_LOGIT " missing, using syslog for %s instead", svc->name);
fallback_logger(tag, prio); fallback_logger(tag, prio);
@@ -416,20 +420,19 @@ static pid_t service_fork(svc_t *svc)
/* Set configured limits */ /* Set configured limits */
for (int i = 0; i < RLIMIT_NLIMITS; i++) { for (int i = 0; i < RLIMIT_NLIMITS; i++) {
if (setrlimit(i, &svc->rlimit[i]) == -1) if (setrlimit(i, &svc->rlimit[i]) == -1)
logit(LOG_WARNING, logit(LOG_WARNING, "%s: rlimit: failed setting %s",
"%s: rlimit: Failed setting %s", svc_ident(svc, NULL, 0), rlim2str(i));
svc->cmd, rlim2str(i));
} }
/* Set desired user+group */ /* Set desired user+group */
if (gid >= 0) { if (gid >= 0) {
if (setgid(gid)) if (setgid(gid))
_pe("%s: failed setgid(%d)", svc->cmd, gid); _pe("%s: failed setgid(%d)", svc_ident(svc, NULL, 0), gid);
} }
if (uid >= 0) { if (uid >= 0) {
if (setuid(uid)) if (setuid(uid))
_pe("%s: failed setuid(%d)", svc->cmd, uid); _pe("%s: failed setuid(%d)", svc_ident(svc, NULL, 0), uid);
/* Set default path for regular users */ /* Set default path for regular users */
if (uid > 0) if (uid > 0)
@@ -438,7 +441,7 @@ static pid_t service_fork(svc_t *svc)
setenv("HOME", home, 1); setenv("HOME", home, 1);
if (chdir(home)) { if (chdir(home)) {
if (chdir("/")) if (chdir("/"))
_pe("%s: failed chdir(%s) and chdir(/)", svc->cmd, home); _pe("%s: failed chdir(%s) and chdir(/)", svc_ident(svc, NULL, 0), home);
} }
} }
} }
@@ -475,14 +478,14 @@ static int service_start(svc_t *svc)
/* Don't try and start service if it doesn't exist. */ /* Don't try and start service if it doesn't exist. */
if (!whichp(svc->cmd)) { if (!whichp(svc->cmd)) {
logit(LOG_WARNING, "%s: missing or not in $PATH", svc->cmd); logit(LOG_WARNING, "%s: missing %s or not in $PATH", svc_ident(svc, NULL, 0), svc->cmd);
svc_missing(svc); svc_missing(svc);
return 1; return 1;
} }
/* Unlike systemd we do not allow starting service if env is missing, unless - */ /* Unlike systemd we do not allow starting service if env is missing, unless - */
if (!svc_checkenv(svc)) { if (!svc_checkenv(svc)) {
logit(LOG_WARNING, "%s: missing env file %s", svc->cmd, svc->env); logit(LOG_WARNING, "%s: missing %s env file %s", svc_ident(svc, NULL, 0), svc->cmd, svc->env);
svc_missing(svc); svc_missing(svc);
return 1; return 1;
} }
@@ -535,7 +538,7 @@ static int service_start(svc_t *svc)
int rc; int rc;
if ((rc = wordexp(svc->cmd, &we, 0))) { if ((rc = wordexp(svc->cmd, &we, 0))) {
_e("%s: failed wordexp(%s): %d", svc->cmd, svc->cmd, rc); _e("%s: failed wordexp(%s): %d", svc_ident(svc, NULL, 0), svc->cmd, rc);
nomem: nomem:
wordfree(&we); wordfree(&we);
_exit(1); _exit(1);
@@ -563,19 +566,19 @@ static int service_start(svc_t *svc)
strlcat(str, arg, sizeof(str)); strlcat(str, arg, sizeof(str));
if ((rc = wordexp(str, &we, WRDE_APPEND))) { if ((rc = wordexp(str, &we, WRDE_APPEND))) {
_e("%s: failed wordexp(%s): %d", svc->cmd, str, rc); _e("%s: failed wordexp(%s): %d", svc_ident(svc, NULL, 0), str, rc);
goto nomem; goto nomem;
} }
} }
if (we.we_wordc > MAX_NUM_SVC_ARGS) { if (we.we_wordc > MAX_NUM_SVC_ARGS) {
logit(LOG_ERR, "%s: too man args after expansion.", svc->cmd); logit(LOG_ERR, "%s: too many args to %s after expansion.", svc_ident(svc, NULL, 0), svc->cmd);
goto nomem; goto nomem;
} }
for (i = 0; i < we.we_wordc; i++) { for (i = 0; i < we.we_wordc; i++) {
if (strlen(we.we_wordv[i]) >= sizeof(svc->args[i])) { if (strlen(we.we_wordv[i]) >= sizeof(svc->args[i])) {
logit(LOG_ERR, "%s: expanded arg. '%s' too long", svc->cmd, we.we_wordv[i]); logit(LOG_ERR, "%s: expanded %s arg. '%s' too long", svc_ident(svc, NULL, 0), svc->cmd, we.we_wordv[i]);
rc = WRDE_NOSPACE; rc = WRDE_NOSPACE;
goto nomem; goto nomem;
} }
@@ -684,7 +687,7 @@ static void service_kill(svc_t *svc)
if (svc->pid <= 1) { if (svc->pid <= 1) {
/* Avoid killing ourselves or all processes ... */ /* Avoid killing ourselves or all processes ... */
_d("%s: Aborting SIGKILL, already terminated.", svc->cmd); _d("%s: Aborting SIGKILL, already terminated.", svc_ident(svc, NULL, 0));
return; return;
} }
@@ -714,7 +717,7 @@ static void service_cleanup(svc_t *svc)
fn = pid_file(svc); fn = pid_file(svc);
if (fn && remove(fn) && errno != ENOENT) if (fn && remove(fn) && errno != ENOENT)
logit(LOG_CRIT, "Failed removing service %s pidfile %s", logit(LOG_CRIT, "Failed removing service %s pidfile %s",
basename(svc->cmd), fn); svc_ident(svc, NULL, 0), fn);
/* No longer running, update books. */ /* No longer running, update books. */
if (svc_is_tty(svc) && svc->pid > 1) if (svc_is_tty(svc) && svc->pid > 1)
@@ -857,7 +860,7 @@ static int service_restart(svc_t *svc)
return 1; return 1;
if (svc->pid <= 1) { if (svc->pid <= 1) {
_d("Bad PID %d for %s, SIGHUP", svc->pid, svc->cmd); _d("%s: bad PID %d for %s, SIGHUP", svc_ident(svc, NULL, 0), svc->pid, svc->cmd);
svc->start_time = svc->pid = 0; svc->start_time = svc->pid = 0;
return 1; return 1;
} }
@@ -960,7 +963,7 @@ static void parse_env(svc_t *svc, char *env)
return; return;
if (strlen(env) >= sizeof(svc->env)) { if (strlen(env) >= sizeof(svc->env)) {
_e("%s: env file is too long (>%zu chars)", svc->cmd, sizeof(svc->env)); _e("%s: env file is too long (>%zu chars)", svc_ident(svc, NULL, 0), sizeof(svc->env));
return; return;
} }
@@ -984,7 +987,7 @@ static void parse_cgroup(svc_t *svc, char *cgroup)
} }
if (strlen(ptr) >= sizeof(svc->cgroup)) { if (strlen(ptr) >= sizeof(svc->cgroup)) {
_e("%s: cgroup settings too long (>%zu chars)", svc->cmd, sizeof(svc->cgroup)); _e("%s: cgroup settings too long (>%zu chars)", svc_ident(svc, NULL, 0), sizeof(svc->cgroup));
return; return;
} }
@@ -1009,7 +1012,7 @@ static void parse_killdelay(svc_t *svc, char *delay)
sec = strtonum(delay, 1, 60, &errstr); sec = strtonum(delay, 1, 60, &errstr);
if (errstr) { if (errstr) {
_e("%s: killdelay %s is %s (1-60)", svc->cmd, delay, errstr); _e("%s: killdelay %s is %s (1-60)", svc_ident(svc, NULL, 0), delay, errstr);
return; return;
} }
+2 -2
View File
@@ -187,7 +187,7 @@ restart:
*/ */
svc = svc_stop_completed(); svc = svc_stop_completed();
if (svc) { if (svc) {
_d("Waiting to collect %s(%d) ...", svc->cmd, svc->pid); _d("Waiting to collect %s, cmd %s(%d) ...", svc_ident(svc, NULL, 0), svc->cmd, svc->pid);
break; break;
} }
@@ -240,7 +240,7 @@ restart:
*/ */
svc = svc_stop_completed(); svc = svc_stop_completed();
if (svc) { if (svc) {
_d("Waiting to collect %s(%d) ...", svc->cmd, svc->pid); _d("Waiting to collect %s, cmd %s(%d) ...", svc_ident(svc, NULL, 0), svc->cmd, svc->pid);
break; break;
} }