patch from vodz:

I reduce 148 bytes from start_stop_daemon.c code. Also reduced
    memory allocated.
This commit is contained in:
Eric Andersen 2001-10-31 09:55:39 +00:00
parent 2c669dd108
commit 950d8b496f
2 changed files with 52 additions and 72 deletions

View File

@ -28,25 +28,23 @@ static const char *userspec = NULL;
static const char *cmdname = NULL; static const char *cmdname = NULL;
static char *execname = NULL; static char *execname = NULL;
static char *startas = NULL; static char *startas = NULL;
static const char *progname = "";
struct pid_list { typedef struct pid_list {
struct pid_list *next; struct pid_list *next;
int pid; int pid;
}; } pid_list;
static struct pid_list *found = NULL; static pid_list *found = NULL;
static struct pid_list *killed = NULL;
static void static inline void
push(struct pid_list **list, int pid) push(int pid)
{ {
struct pid_list *p; pid_list *p;
p = xmalloc(sizeof(*p)); p = xmalloc(sizeof(*p));
p->next = *list; p->next = found;
p->pid = pid; p->pid = pid;
*list = p; found = p;
} }
@ -85,7 +83,6 @@ parse_options(int argc, char * const *argv)
break; break;
default: default:
show_usage(); show_usage();
exit(1);
} }
} }
@ -169,7 +166,7 @@ check(int pid)
if (cmdname && !pid_is_cmd(pid, cmdname)) { if (cmdname && !pid_is_cmd(pid, cmdname)) {
return; return;
} }
push(&found, pid); push(pid);
} }
@ -202,7 +199,8 @@ static void
do_stop(void) do_stop(void)
{ {
char what[1024]; char what[1024];
struct pid_list *p; pid_list *p;
int killed = 0;
if (cmdname) if (cmdname)
strcpy(what, cmdname); strcpy(what, cmdname);
@ -215,19 +213,21 @@ do_stop(void)
if (!found) { if (!found) {
printf("no %s found; none killed.\n", what); printf("no %s found; none killed.\n", what);
exit(0); return;
} }
for (p = found; p; p = p->next) { for (p = found; p; p = p->next) {
if (kill(p->pid, signal_nr) == 0) if (kill(p->pid, signal_nr) == 0) {
push(&killed, p->pid); p->pid = -p->pid;
else killed++;
printf("%s: warning: failed to kill %d: %s\n", } else {
progname, p->pid, strerror(errno)); perror_msg("warning: failed to kill %d:", p->pid);
}
} }
if (killed) { if (killed) {
printf("stopped %s (pid", what); printf("stopped %s (pid", what);
for (p = killed; p; p = p->next) for (p = found; p; p = p->next)
printf(" %d", p->pid); if(p->pid < 0)
printf(" %d", -p->pid);
printf(").\n"); printf(").\n");
} }
} }
@ -236,33 +236,23 @@ do_stop(void)
int int
start_stop_daemon_main(int argc, char **argv) start_stop_daemon_main(int argc, char **argv)
{ {
progname = argv[0];
parse_options(argc, argv); parse_options(argc, argv);
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (userspec && sscanf(userspec, "%d", &user_id) != 1) { if (userspec && sscanf(userspec, "%d", &user_id) != 1)
struct passwd *pw; user_id = my_getpwnam(userspec);
pw = getpwnam(userspec);
if (!pw)
error_msg_and_die ("user `%s' not found\n", userspec);
user_id = pw->pw_uid;
}
do_procfs(); do_procfs();
if (stop) { if (stop) {
do_stop(); do_stop();
exit(0); return EXIT_SUCCESS;
} }
if (found) { if (found) {
printf("%s already running.\n", execname); printf("%s already running.\n%d\n", execname ,found->pid);
printf("%d\n",found->pid); return EXIT_SUCCESS;
exit(0);
} }
*--argv = startas; *--argv = startas;
execv(startas, argv); execv(startas, argv);

View File

@ -28,25 +28,23 @@ static const char *userspec = NULL;
static const char *cmdname = NULL; static const char *cmdname = NULL;
static char *execname = NULL; static char *execname = NULL;
static char *startas = NULL; static char *startas = NULL;
static const char *progname = "";
struct pid_list { typedef struct pid_list {
struct pid_list *next; struct pid_list *next;
int pid; int pid;
}; } pid_list;
static struct pid_list *found = NULL; static pid_list *found = NULL;
static struct pid_list *killed = NULL;
static void static inline void
push(struct pid_list **list, int pid) push(int pid)
{ {
struct pid_list *p; pid_list *p;
p = xmalloc(sizeof(*p)); p = xmalloc(sizeof(*p));
p->next = *list; p->next = found;
p->pid = pid; p->pid = pid;
*list = p; found = p;
} }
@ -85,7 +83,6 @@ parse_options(int argc, char * const *argv)
break; break;
default: default:
show_usage(); show_usage();
exit(1);
} }
} }
@ -169,7 +166,7 @@ check(int pid)
if (cmdname && !pid_is_cmd(pid, cmdname)) { if (cmdname && !pid_is_cmd(pid, cmdname)) {
return; return;
} }
push(&found, pid); push(pid);
} }
@ -202,7 +199,8 @@ static void
do_stop(void) do_stop(void)
{ {
char what[1024]; char what[1024];
struct pid_list *p; pid_list *p;
int killed = 0;
if (cmdname) if (cmdname)
strcpy(what, cmdname); strcpy(what, cmdname);
@ -215,19 +213,21 @@ do_stop(void)
if (!found) { if (!found) {
printf("no %s found; none killed.\n", what); printf("no %s found; none killed.\n", what);
exit(0); return;
} }
for (p = found; p; p = p->next) { for (p = found; p; p = p->next) {
if (kill(p->pid, signal_nr) == 0) if (kill(p->pid, signal_nr) == 0) {
push(&killed, p->pid); p->pid = -p->pid;
else killed++;
printf("%s: warning: failed to kill %d: %s\n", } else {
progname, p->pid, strerror(errno)); perror_msg("warning: failed to kill %d:", p->pid);
}
} }
if (killed) { if (killed) {
printf("stopped %s (pid", what); printf("stopped %s (pid", what);
for (p = killed; p; p = p->next) for (p = found; p; p = p->next)
printf(" %d", p->pid); if(p->pid < 0)
printf(" %d", -p->pid);
printf(").\n"); printf(").\n");
} }
} }
@ -236,33 +236,23 @@ do_stop(void)
int int
start_stop_daemon_main(int argc, char **argv) start_stop_daemon_main(int argc, char **argv)
{ {
progname = argv[0];
parse_options(argc, argv); parse_options(argc, argv);
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (userspec && sscanf(userspec, "%d", &user_id) != 1) { if (userspec && sscanf(userspec, "%d", &user_id) != 1)
struct passwd *pw; user_id = my_getpwnam(userspec);
pw = getpwnam(userspec);
if (!pw)
error_msg_and_die ("user `%s' not found\n", userspec);
user_id = pw->pw_uid;
}
do_procfs(); do_procfs();
if (stop) { if (stop) {
do_stop(); do_stop();
exit(0); return EXIT_SUCCESS;
} }
if (found) { if (found) {
printf("%s already running.\n", execname); printf("%s already running.\n%d\n", execname ,found->pid);
printf("%d\n",found->pid); return EXIT_SUCCESS;
exit(0);
} }
*--argv = startas; *--argv = startas;
execv(startas, argv); execv(startas, argv);