Remove gratuitous and unnecessary "BusyBox" refernece in login prompt
[oweals/busybox.git] / init / start_stop_daemon.c
index f8b2d936c3aaaed53ac28f0352f5960c91784b68..5765261834369ad38311a19833cb5c7c993b75fe 100644 (file)
 #include <sys/stat.h>
 #include <dirent.h>
 #include <unistd.h>
-#include "pwd.h"
 
 #include "busybox.h"
+#include "pwd_.h"
 
 static int start = 0;
 static int stop = 0;
+static int fork_before_exec = 0;
 static int signal_nr = 15;
 static int user_id = -1;
 static const char *userspec = NULL;
 static const char *cmdname = NULL;
 static char *execname = NULL;
 static char *startas = NULL;
-static const char *progname = "";
 
-struct pid_list {
+typedef struct pid_list {
        struct pid_list *next;
        int pid;
-};
+} pid_list;
 
-static struct pid_list *found = NULL;
-static struct pid_list *killed = NULL;
+static pid_list *found = NULL;
 
-static void
-push(struct pid_list **list, int pid)
+static inline void
+push(int pid)
 {
-       struct pid_list *p;
+       pid_list *p;
 
        p = xmalloc(sizeof(*p));
-       p->next = *list;
+       p->next = found;
        p->pid = pid;
-       *list = p;
+       found = p;
 }
 
 
@@ -57,7 +56,7 @@ parse_options(int argc, char * const *argv)
        int c;
 
        for (;;) {
-           c = getopt (argc, argv, "a:n:s:u:x:KS");
+           c = getopt (argc, argv, "a:n:s:u:x:KSb");
                if (c == EOF)
                        break;
                switch (c) {
@@ -83,9 +82,11 @@ parse_options(int argc, char * const *argv)
                case 'x':
                        execname = optarg;
                        break;
+               case 'b':
+                       fork_before_exec = 1;
+                       break;
                default:
                        show_usage();
-                       exit(1);
                }
        }
 
@@ -112,6 +113,7 @@ pid_is_exec(int pid, const char *exec)
        sprintf(buf, "/proc/%d/cmdline", pid);
        fp = fopen(buf, "r");
        if (fp && fgets (buf, sizeof (buf), fp) ) {
+               fclose(fp);
            if (strncmp (buf, exec, strlen(exec)) == 0)
                return 1;
        }
@@ -169,7 +171,7 @@ check(int pid)
        if (cmdname && !pid_is_cmd(pid, cmdname)) {
                return;
        }
-       push(&found, pid);
+       push(pid);
 }
 
 
@@ -202,7 +204,8 @@ static void
 do_stop(void)
 {
        char what[1024];
-       struct pid_list *p;
+       pid_list *p;
+       int killed = 0;
 
        if (cmdname)
                strcpy(what, cmdname);
@@ -215,19 +218,21 @@ do_stop(void)
 
        if (!found) {
                printf("no %s found; none killed.\n", what);
-               exit(0);
+               return;
        }
        for (p = found; p; p = p->next) {
-               if (kill(p->pid, signal_nr) == 0)
-                       push(&killed, p->pid);
-               else
-                       printf("%s: warning: failed to kill %d: %s\n",
-                              progname, p->pid, strerror(errno));
+               if (kill(p->pid, signal_nr) == 0) {
+                       p->pid = -p->pid;
+                       killed++;
+               } else {
+                       perror_msg("warning: failed to kill %d:", p->pid);
+               }
        }
        if (killed) {
                printf("stopped %s (pid", what);
-               for (p = killed; p; p = p->next)
-                       printf(" %d", p->pid);
+               for (p = found; p; p = p->next)
+                       if(p->pid < 0)
+                               printf(" %d", -p->pid);
                printf(").\n");
        }
 }
@@ -236,35 +241,30 @@ do_stop(void)
 int
 start_stop_daemon_main(int argc, char **argv)
 {
-       progname = argv[0];
-
        parse_options(argc, argv);
        argc -= optind;
        argv += optind;
 
-       if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
-               struct passwd *pw;
-
-               pw = getpwnam(userspec);
-               if (!pw)
-                       error_msg_and_die ("user `%s' not found\n", userspec);
-
-               user_id = pw->pw_uid;
-       }
+       if (userspec && sscanf(userspec, "%d", &user_id) != 1)
+               user_id = my_getpwnam(userspec);
 
        do_procfs();
 
        if (stop) {
                do_stop();
-               exit(0);
+               return EXIT_SUCCESS;
        }
 
        if (found) {
-               printf("%s already running.\n", execname);
-               printf("%d\n",found->pid);
-               exit(0);
+               printf("%s already running.\n%d\n", execname ,found->pid);
+               return EXIT_SUCCESS;
        }
        *--argv = startas;
+       if (fork_before_exec) {
+               if (daemon(0, 0) == -1)
+                       perror_msg_and_die ("unable to fork");
+       }
+       setsid();
        execv(startas, argv);
        perror_msg_and_die ("unable to start %s", startas);
 }