aboutsummaryrefslogtreecommitdiff
path: root/debianutils
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-03-13 08:33:10 +0000
committerEric Andersen <andersen@codepoet.org>2004-03-13 08:33:10 +0000
commit63a1a7aaa520d84bcf37b7351e3923bd308ab8ea (patch)
treee64462d91b1b7edb0030cde420c4a7e2c8759264 /debianutils
parent55c704c119c871e25253d8dc3582f623d7d47708 (diff)
downloadbusybox-w32-63a1a7aaa520d84bcf37b7351e3923bd308ab8ea.tar.gz
busybox-w32-63a1a7aaa520d84bcf37b7351e3923bd308ab8ea.tar.bz2
busybox-w32-63a1a7aaa520d84bcf37b7351e3923bd308ab8ea.zip
Update docs for start_stop_daemon to match reality. Update
the reality a bit to better match debian behavior.
Diffstat (limited to 'debianutils')
-rw-r--r--debianutils/start_stop_daemon.c82
1 files changed, 53 insertions, 29 deletions
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index dfa33e1f9..6bc259fb3 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -23,21 +23,23 @@
23 23
24static int signal_nr = 15; 24static int signal_nr = 15;
25static int user_id = -1; 25static int user_id = -1;
26static int quiet = 0;
26static char *userspec = NULL; 27static char *userspec = NULL;
27static char *cmdname = NULL; 28static char *cmdname = NULL;
28static char *execname = NULL; 29static char *execname = NULL;
30static char *pidfile = NULL;
29 31
30typedef struct pid_list { 32struct pid_list {
31 struct pid_list *next; 33 struct pid_list *next;
32 int pid; 34 pid_t pid;
33} pid_list; 35};
34 36
35static pid_list *found = NULL; 37static struct pid_list *found = NULL;
36 38
37static inline void 39static inline void
38push(int pid) 40push(pid_t pid)
39{ 41{
40 pid_list *p; 42 struct pid_list *p;
41 43
42 p = xmalloc(sizeof(*p)); 44 p = xmalloc(sizeof(*p));
43 p->next = found; 45 p->next = found;
@@ -46,21 +48,19 @@ push(int pid)
46} 48}
47 49
48static int 50static int
49pid_is_exec(int pid, const char *exec) 51pid_is_exec(pid_t pid, const char *name)
50{ 52{
51 char buf[PATH_MAX]; 53 char buf[32];
52 FILE *fp; 54 struct stat sb, exec_stat;
53
54 sprintf(buf, "/proc/%d/cmdline", pid);
55 fp = fopen(buf, "r");
56 if (fp && fgets (buf, sizeof (buf), fp) ) {
57 fclose(fp);
58 if (strncmp (buf, exec, strlen(exec)) == 0)
59 return 1;
60 }
61 return 0;
62}
63 55
56 if (name && stat(name, &exec_stat))
57 bb_perror_msg_and_die("stat %s", name);
58
59 sprintf(buf, "/proc/%d/exe", pid);
60 if (stat(buf, &sb) != 0)
61 return 0;
62 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
63}
64 64
65static int 65static int
66pid_is_user(int pid, int uid) 66pid_is_user(int pid, int uid)
@@ -74,9 +74,8 @@ pid_is_user(int pid, int uid)
74 return (sb.st_uid == uid); 74 return (sb.st_uid == uid);
75} 75}
76 76
77
78static int 77static int
79pid_is_cmd(int pid, const char *name) 78pid_is_cmd(pid_t pid, const char *name)
80{ 79{
81 char buf[32]; 80 char buf[32];
82 FILE *f; 81 FILE *f;
@@ -116,9 +115,24 @@ check(int pid)
116} 115}
117 116
118 117
118static void
119do_pidfile(const char *name)
120{
121 FILE *f;
122 pid_t pid;
123
124 f = fopen(name, "r");
125 if (f) {
126 if (fscanf(f, "%d", &pid) == 1)
127 check(pid);
128 fclose(f);
129 } else if (errno != ENOENT)
130 bb_perror_msg_and_die("open pidfile %s", name);
131
132}
119 133
120static void 134static void
121do_procfs(void) 135do_procinit(void)
122{ 136{
123 DIR *procdir; 137 DIR *procdir;
124 struct dirent *entry; 138 struct dirent *entry;
@@ -145,9 +159,14 @@ static void
145do_stop(void) 159do_stop(void)
146{ 160{
147 char what[1024]; 161 char what[1024];
148 pid_list *p; 162 struct pid_list *p;
149 int killed = 0; 163 int killed = 0;
150 164
165 if (pidfile)
166 do_pidfile(pidfile);
167 else
168 do_procinit();
169
151 if (cmdname) 170 if (cmdname)
152 strcpy(what, cmdname); 171 strcpy(what, cmdname);
153 else if (execname) 172 else if (execname)
@@ -158,7 +177,8 @@ do_stop(void)
158 bb_error_msg_and_die ("internal error, please report"); 177 bb_error_msg_and_die ("internal error, please report");
159 178
160 if (!found) { 179 if (!found) {
161 printf("no %s found; none killed.\n", what); 180 if (!quiet)
181 printf("no %s found; none killed.\n", what);
162 return; 182 return;
163 } 183 }
164 for (p = found; p; p = p->next) { 184 for (p = found; p; p = p->next) {
@@ -169,7 +189,7 @@ do_stop(void)
169 bb_perror_msg("warning: failed to kill %d:", p->pid); 189 bb_perror_msg("warning: failed to kill %d:", p->pid);
170 } 190 }
171 } 191 }
172 if (killed) { 192 if (!quiet && killed) {
173 printf("stopped %s (pid", what); 193 printf("stopped %s (pid", what);
174 for (p = found; p; p = p->next) 194 for (p = found; p; p = p->next)
175 if(p->pid < 0) 195 if(p->pid < 0)
@@ -183,17 +203,20 @@ static const struct option ssd_long_options[] = {
183 { "stop", 0, NULL, 'K' }, 203 { "stop", 0, NULL, 'K' },
184 { "start", 0, NULL, 'S' }, 204 { "start", 0, NULL, 'S' },
185 { "background", 0, NULL, 'b' }, 205 { "background", 0, NULL, 'b' },
206 { "quiet", 0, NULL, 'q' },
186 { "startas", 1, NULL, 'a' }, 207 { "startas", 1, NULL, 'a' },
187 { "name", 1, NULL, 'n' }, 208 { "name", 1, NULL, 'n' },
188 { "signal", 1, NULL, 's' }, 209 { "signal", 1, NULL, 's' },
189 { "user", 1, NULL, 'u' }, 210 { "user", 1, NULL, 'u' },
190 { "exec", 1, NULL, 'x' }, 211 { "exec", 1, NULL, 'x' },
212 { "pidfile", 1, NULL, 'p' },
191 { 0, 0, 0, 0 } 213 { 0, 0, 0, 0 }
192}; 214};
193 215
194#define SSD_CTX_STOP 1 216#define SSD_CTX_STOP 1
195#define SSD_CTX_START 2 217#define SSD_CTX_START 2
196#define SSD_OPT_BACKGROUND 4 218#define SSD_OPT_BACKGROUND 4
219#define SSD_OPT_QUIET 8
197 220
198int 221int
199start_stop_daemon_main(int argc, char **argv) 222start_stop_daemon_main(int argc, char **argv)
@@ -205,8 +228,8 @@ start_stop_daemon_main(int argc, char **argv)
205 bb_applet_long_options = ssd_long_options; 228 bb_applet_long_options = ssd_long_options;
206 229
207 bb_opt_complementaly = "K~S:S~K"; 230 bb_opt_complementaly = "K~S:S~K";
208 opt = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:", 231 opt = bb_getopt_ulflags(argc, argv, "KSbqa:n:s:u:x:p:",
209 &startas, &cmdname, &signame, &userspec, &execname); 232 &startas, &cmdname, &signame, &userspec, &execname, &pidfile);
210 233
211 /* Check one and only one context option was given */ 234 /* Check one and only one context option was given */
212 if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) { 235 if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) {
@@ -232,7 +255,7 @@ start_stop_daemon_main(int argc, char **argv)
232 if (userspec && sscanf(userspec, "%d", &user_id) != 1) 255 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
233 user_id = my_getpwnam(userspec); 256 user_id = my_getpwnam(userspec);
234 257
235 do_procfs(); 258 do_procinit();
236 259
237 if (opt & SSD_CTX_STOP) { 260 if (opt & SSD_CTX_STOP) {
238 do_stop(); 261 do_stop();
@@ -240,7 +263,8 @@ start_stop_daemon_main(int argc, char **argv)
240 } 263 }
241 264
242 if (found) { 265 if (found) {
243 printf("%s already running.\n%d\n", execname ,found->pid); 266 if (!quiet)
267 printf("%s already running.\n%d\n", execname ,found->pid);
244 return EXIT_SUCCESS; 268 return EXIT_SUCCESS;
245 } 269 }
246 *--argv = startas; 270 *--argv = startas;