start-stop-daemon: clean up string handling

This commit is contained in:
William Hubbs 2018-02-16 14:04:40 -06:00
parent 488d8989c5
commit 8dbdabcc5e

View File

@ -254,6 +254,7 @@ int main(int argc, char **argv)
#endif #endif
int opt; int opt;
size_t size = 0;
bool start = false; bool start = false;
bool stop = false; bool stop = false;
bool oknodo = false; bool oknodo = false;
@ -287,10 +288,10 @@ int main(int argc, char **argv)
char *tmp, *newpath, *np; char *tmp, *newpath, *np;
char *p; char *p;
char *token; char *token;
char exec_file[PATH_MAX]; char *exec_file = NULL;
struct passwd *pw; struct passwd *pw;
struct group *gr; struct group *gr;
char line[130]; char *line = NULL;
FILE *fp; FILE *fp;
size_t len; size_t len;
mode_t numask = 022; mode_t numask = 022;
@ -577,26 +578,22 @@ 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);
} }
@ -604,6 +601,7 @@ int main(int argc, char **argv)
if (start && !exists(exec_file)) { if (start && !exists(exec_file)) {
eerror("%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); exit(EXIT_FAILURE);
} }
@ -617,7 +615,9 @@ int main(int argc, char **argv)
if (interpreted && !pidfile) { if (interpreted && !pidfile) {
fp = fopen(exec_file, "r"); fp = fopen(exec_file, "r");
if (fp) { if (fp) {
p = fgets(line, sizeof(line), fp); line = NULL;
getline(&line, &size, fp);
p = line;
fclose(fp); fclose(fp);
if (p != NULL && line[0] == '#' && line[1] == '!') { if (p != NULL && line[0] == '#' && line[1] == '!') {
p = line + 2; p = line + 2;
@ -629,7 +629,8 @@ int main(int argc, char **argv)
if (p[len] == '\n') if (p[len] == '\n')
p[len] = '\0'; p[len] = '\0';
token = strsep(&p, " "); token = strsep(&p, " ");
strncpy(exec_file, token, sizeof(exec_file)); free(exec_file);
xasprintf(&exec_file, "%s", token);
opt = 0; opt = 0;
for (nav = argv; *nav; nav++) for (nav = argv; *nav; nav++)
opt++; opt++;