supervise-daemon: remove references to PATH_MAX

This commit is contained in:
William Hubbs 2018-02-21 18:14:56 -06:00
parent a6cc7f06cf
commit e275da84de

View File

@ -224,6 +224,22 @@ static char * expand_home(const char *home, const char *path)
return nh; return nh;
} }
static char *make_cmdline(char **argv)
{
char **c;
char *cmdline = NULL;
size_t len = 0;
for (c = argv; c && *c; c++)
len += (strlen(*c) + 1);
cmdline = xmalloc(len);
for (c = argv; c && *c; c++) {
strcat(cmdline, *c);
strcat(cmdline, " ");
}
return cmdline;
}
static void child_process(char *exec, char **argv) static void child_process(char *exec, char **argv)
{ {
RC_STRINGLIST *env_list; RC_STRINGLIST *env_list;
@ -234,8 +250,7 @@ static void child_process(char *exec, char **argv)
size_t len; size_t len;
char *newpath; char *newpath;
char *np; char *np;
char **c; char *cmdline = NULL;
char cmdline[PATH_MAX];
time_t start_time; time_t start_time;
char start_count_string[20]; char start_count_string[20];
char start_time_string[20]; char start_time_string[20];
@ -395,15 +410,9 @@ static void child_process(char *exec, char **argv)
for (i = getdtablesize() - 1; i >= 3; --i) for (i = getdtablesize() - 1; i >= 3; --i)
fcntl(i, F_SETFD, FD_CLOEXEC); fcntl(i, F_SETFD, FD_CLOEXEC);
cmdline = make_cmdline(argv);
*cmdline = '\0';
c = argv;
while (c && *c) {
strcat(cmdline, *c);
strcat(cmdline, " ");
c++;
}
syslog(LOG_INFO, "Child command line: %s", cmdline); syslog(LOG_INFO, "Child command line: %s", cmdline);
free(cmdline);
execvp(exec, argv); execvp(exec, argv);
#ifdef HAVE_PAM #ifdef HAVE_PAM
@ -551,8 +560,8 @@ int main(int argc, char **argv)
char *token; char *token;
int i; int i;
int n; int n;
char exec_file[PATH_MAX]; char *exec_file = NULL;
char name[PATH_MAX]; char *varbuf = NULL;
struct timespec ts; struct timespec ts;
struct passwd *pw; struct passwd *pw;
struct group *gr; struct group *gr;
@ -561,7 +570,7 @@ int main(int argc, char **argv)
int child_argc = 0; int child_argc = 0;
char **child_argv = NULL; char **child_argv = NULL;
char *str = NULL; char *str = NULL;
char cmdline[PATH_MAX]; char *cmdline = NULL;
applet = basename_c(argv[0]); applet = basename_c(argv[0]);
atexit(cleanup); atexit(cleanup);
@ -593,13 +602,7 @@ int main(int argc, char **argv)
} }
} }
*cmdline = '\0'; cmdline = make_cmdline(argv);
c = argv;
while (c && *c) {
strcat(cmdline, *c);
strcat(cmdline, " ");
c++;
}
if (svcname) { if (svcname) {
argc--; argc--;
argv++; argv++;
@ -761,9 +764,11 @@ int main(int argc, char **argv)
child_argv = xmalloc((child_argc + 1) * sizeof(char *)); child_argv = xmalloc((child_argc + 1) * sizeof(char *));
memset(child_argv, 0, (child_argc + 1) * sizeof(char *)); memset(child_argv, 0, (child_argc + 1) * sizeof(char *));
for (x = 0; x < child_argc; x++) { for (x = 0; x < child_argc; x++) {
sprintf(name, "argv_%d", x); xasprintf(&varbuf, "argv_%d", x);
str = rc_service_value_get(svcname, name); str = rc_service_value_get(svcname, varbuf);
child_argv[x] = str; child_argv[x] = str;
free(varbuf);
varbuf = NULL;
} }
free(str); free(str);
str = rc_service_value_get(svcname, "child_pid"); str = rc_service_value_get(svcname, "child_pid");
@ -792,32 +797,31 @@ int main(int argc, char **argv)
if (*exec == '/' || *exec == '.') { if (*exec == '/' || *exec == '.') {
/* Full or relative path */ /* Full or relative path */
if (ch_root) if (ch_root)
snprintf(exec_file, sizeof(exec_file), xasprintf(&exec_file, "%s/%s", ch_root, exec);
"%s/%s", ch_root, exec);
else else
snprintf(exec_file, sizeof(exec_file), xasprintf(&exec_file, "%s", exec);
"%s", exec);
} else { } else {
/* Something in $PATH */ /* Something in $PATH */
p = tmp = xstrdup(getenv("PATH")); p = tmp = xstrdup(getenv("PATH"));
*exec_file = '\0'; exec_file = NULL;
while ((token = strsep(&p, ":"))) { while ((token = strsep(&p, ":"))) {
if (ch_root) if (ch_root)
snprintf(exec_file, sizeof(exec_file), xasprintf(&exec_file, "%s/%s/%s", ch_root, token, exec);
"%s/%s/%s",
ch_root, token, exec);
else else
snprintf(exec_file, sizeof(exec_file), xasprintf(&exec_file, "%s/%s", token, exec);
"%s/%s", token, exec); if (exec_file && exists(exec_file))
if (exists(exec_file))
break; break;
*exec_file = '\0'; free(exec_file);
exec_file = NULL;
} }
free(tmp); free(tmp);
} }
if ( !exists(exec_file)) if (!exists(exec_file)) {
eerrorx("%s: %s does not exist", applet, eerror("%s: %s does not exist", applet,
*exec_file ? exec_file : exec); *exec_file ? exec_file : exec);
free(exec_file);
exit(EXIT_FAILURE);
}
} else } else
eerrorx("%s: nothing to start", applet); eerrorx("%s: nothing to start", applet);
@ -840,6 +844,8 @@ int main(int argc, char **argv)
einfov("Detaching to start `%s'", exec); einfov("Detaching to start `%s'", exec);
syslog(LOG_INFO, "Supervisor command line: %s", cmdline); syslog(LOG_INFO, "Supervisor command line: %s", cmdline);
free(cmdline);
cmdline = NULL;
/* Remove existing pidfile */ /* Remove existing pidfile */
if (pidfile) if (pidfile)
@ -852,12 +858,13 @@ int main(int argc, char **argv)
fclose(fp); fclose(fp);
rc_service_value_set(svcname, "pidfile", pidfile); rc_service_value_set(svcname, "pidfile", pidfile);
sprintf(name, "%i", respawn_delay); varbuf = NULL;
rc_service_value_set(svcname, "respawn_delay", name); xasprintf(&varbuf, "%i", respawn_delay);
sprintf(name, "%i", respawn_max); rc_service_value_set(svcname, "respawn_delay", varbuf);
rc_service_value_set(svcname, "respawn_max", name); xasprintf(&varbuf, "%i", respawn_max);
sprintf(name, "%i", respawn_period); rc_service_value_set(svcname, "respawn_max", varbuf);
rc_service_value_set(svcname, "respawn_period", name); xasprintf(&varbuf, "%i", respawn_period);
rc_service_value_set(svcname, "respawn_period", varbuf);
child_pid = fork(); child_pid = fork();
if (child_pid == -1) if (child_pid == -1)
eerrorx("%s: fork: %s", applet, strerror(errno)); eerrorx("%s: fork: %s", applet, strerror(errno));
@ -875,13 +882,16 @@ int main(int argc, char **argv)
c = argv; c = argv;
x = 0; x = 0;
while (c && *c) { while (c && *c) {
snprintf(name, sizeof(name), "argv_%-d",x); varbuf = NULL;
rc_service_value_set(svcname, name, *c); xasprintf(&varbuf, "argv_%-d",x);
rc_service_value_set(svcname, varbuf, *c);
free(varbuf);
varbuf = NULL;
x++; x++;
c++; c++;
} }
sprintf(name, "%d", x); xasprintf(&varbuf, "%d", x);
rc_service_value_set(svcname, "argc", name); rc_service_value_set(svcname, "argc", varbuf);
rc_service_value_set(svcname, "exec", exec); rc_service_value_set(svcname, "exec", exec);
supervisor(exec, argv); supervisor(exec, argv);
} else } else