start_stop_daemon.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini start-stop-daemon implementation(s) for busybox
  4. *
  5. * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
  6. * Adapted for busybox David Kimdon <dwhedon@gordian.com>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. */
  10. #include "busybox.h"
  11. #include <getopt.h>
  12. #include <sys/resource.h>
  13. static int signal_nr = 15;
  14. static int user_id = -1;
  15. static int quiet;
  16. static char *userspec;
  17. static char *chuid;
  18. static char *cmdname;
  19. static char *execname;
  20. static char *pidfile;
  21. struct pid_list {
  22. struct pid_list *next;
  23. pid_t pid;
  24. };
  25. static struct pid_list *found;
  26. static inline void push(pid_t pid)
  27. {
  28. struct pid_list *p;
  29. p = xmalloc(sizeof(*p));
  30. p->next = found;
  31. p->pid = pid;
  32. found = p;
  33. }
  34. static int pid_is_exec(pid_t pid, const char *name)
  35. {
  36. char buf[sizeof("/proc//exe") + sizeof(int)*3];
  37. char *execbuf;
  38. int sz;
  39. int equal;
  40. sprintf(buf, "/proc/%d/exe", pid);
  41. sz = strlen(name) + 1;
  42. execbuf = xzalloc(sz);
  43. readlink(buf, execbuf, sz);
  44. /* if readlink fails, execbuf still contains "" */
  45. equal = !strcmp(execbuf, name);
  46. if (ENABLE_FEATURE_CLEAN_UP)
  47. free(execbuf);
  48. return equal;
  49. }
  50. static int pid_is_user(int pid, int uid)
  51. {
  52. struct stat sb;
  53. char buf[sizeof("/proc/") + sizeof(int)*3];
  54. sprintf(buf, "/proc/%u", pid);
  55. if (stat(buf, &sb) != 0)
  56. return 0;
  57. return (sb.st_uid == uid);
  58. }
  59. static int pid_is_cmd(pid_t pid, const char *name)
  60. {
  61. char fname[sizeof("/proc//stat") + sizeof(int)*3];
  62. char *buf;
  63. int r = 0;
  64. sprintf(fname, "/proc/%u/stat", pid);
  65. buf = xmalloc_open_read_close(fname, NULL);
  66. if (buf) {
  67. char *p = strchr(buf, '(');
  68. if (p) {
  69. char *pe = strrchr(++p, ')');
  70. if (pe) {
  71. *pe = '\0';
  72. r = !strcmp(p, name);
  73. }
  74. }
  75. free(buf);
  76. }
  77. return r;
  78. }
  79. static void check(int pid)
  80. {
  81. if (execname && !pid_is_exec(pid, execname)) {
  82. return;
  83. }
  84. if (userspec && !pid_is_user(pid, user_id)) {
  85. return;
  86. }
  87. if (cmdname && !pid_is_cmd(pid, cmdname)) {
  88. return;
  89. }
  90. push(pid);
  91. }
  92. static void do_pidfile(void)
  93. {
  94. FILE *f;
  95. pid_t pid;
  96. f = fopen(pidfile, "r");
  97. if (f) {
  98. if (fscanf(f, "%u", &pid) == 1)
  99. check(pid);
  100. fclose(f);
  101. } else if (errno != ENOENT)
  102. bb_perror_msg_and_die("open pidfile %s", pidfile);
  103. }
  104. static void do_procinit(void)
  105. {
  106. DIR *procdir;
  107. struct dirent *entry;
  108. int foundany, pid;
  109. if (pidfile) {
  110. do_pidfile();
  111. return;
  112. }
  113. procdir = xopendir("/proc");
  114. foundany = 0;
  115. while ((entry = readdir(procdir)) != NULL) {
  116. pid = bb_strtou(entry->d_name, NULL, 10);
  117. if (errno)
  118. continue;
  119. foundany++;
  120. check(pid);
  121. }
  122. closedir(procdir);
  123. if (!foundany)
  124. bb_error_msg_and_die ("nothing in /proc - not mounted?");
  125. }
  126. static int do_stop(void)
  127. {
  128. char *what;
  129. struct pid_list *p;
  130. int killed = 0;
  131. do_procinit();
  132. if (cmdname)
  133. what = xstrdup(cmdname);
  134. else if (execname)
  135. what = xstrdup(execname);
  136. else if (pidfile)
  137. what = xasprintf("process in pidfile '%s'", pidfile);
  138. else if (userspec)
  139. what = xasprintf("process(es) owned by '%s'", userspec);
  140. else
  141. bb_error_msg_and_die("internal error, please report");
  142. if (!found) {
  143. if (!quiet)
  144. printf("no %s found; none killed\n", what);
  145. if (ENABLE_FEATURE_CLEAN_UP)
  146. free(what);
  147. return -1;
  148. }
  149. for (p = found; p; p = p->next) {
  150. if (kill(p->pid, signal_nr) == 0) {
  151. p->pid = -p->pid;
  152. killed++;
  153. } else {
  154. bb_perror_msg("warning: failed to kill %d", p->pid);
  155. }
  156. }
  157. if (!quiet && killed) {
  158. printf("stopped %s (pid", what);
  159. for (p = found; p; p = p->next)
  160. if(p->pid < 0)
  161. printf(" %d", -p->pid);
  162. puts(")");
  163. }
  164. if (ENABLE_FEATURE_CLEAN_UP)
  165. free(what);
  166. return killed;
  167. }
  168. #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
  169. static const struct option long_options[] = {
  170. { "stop", 0, NULL, 'K' },
  171. { "start", 0, NULL, 'S' },
  172. { "background", 0, NULL, 'b' },
  173. { "quiet", 0, NULL, 'q' },
  174. { "make-pidfile", 0, NULL, 'm' },
  175. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  176. { "oknodo", 0, NULL, 'o' },
  177. { "verbose", 0, NULL, 'v' },
  178. { "nicelevel", 1, NULL, 'N' },
  179. #endif
  180. { "startas", 1, NULL, 'a' },
  181. { "name", 1, NULL, 'n' },
  182. { "signal", 1, NULL, 's' },
  183. { "user", 1, NULL, 'u' },
  184. { "chuid", 1, NULL, 'c' },
  185. { "exec", 1, NULL, 'x' },
  186. { "pidfile", 1, NULL, 'p' },
  187. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  188. { "retry", 1, NULL, 'R' },
  189. #endif
  190. { 0, 0, 0, 0 }
  191. };
  192. #endif
  193. enum {
  194. CTX_STOP = 0x1,
  195. CTX_START = 0x2,
  196. OPT_BACKGROUND = 0x4,
  197. OPT_QUIET = 0x8,
  198. OPT_MAKEPID = 0x10,
  199. OPT_OKNODO = 0x20 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
  200. OPT_VERBOSE = 0x40 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
  201. OPT_NICELEVEL = 0x80 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
  202. };
  203. int start_stop_daemon_main(int argc, char **argv)
  204. {
  205. unsigned opt;
  206. char *signame = NULL;
  207. char *startas = NULL;
  208. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  209. // char *retry_arg = NULL;
  210. // int retries = -1;
  211. char *opt_N;
  212. #endif
  213. #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
  214. applet_long_options = long_options;
  215. #endif
  216. /* Check required one context option was given */
  217. opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
  218. opt = getopt32(argc, argv, "KSbqm"
  219. // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
  220. USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:")
  221. "a:n:s:u:c:x:p:"
  222. USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
  223. // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
  224. ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
  225. quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
  226. if (signame) {
  227. signal_nr = get_signum(signame);
  228. if (signal_nr < 0) bb_show_usage();
  229. }
  230. if (!startas)
  231. startas = execname;
  232. // USE_FEATURE_START_STOP_DAEMON_FANCY(
  233. // if (retry_arg)
  234. // retries = xatoi_u(retry_arg);
  235. // )
  236. argc -= optind;
  237. argv += optind;
  238. if (userspec) {
  239. user_id = bb_strtou(userspec, NULL, 10);
  240. if (errno)
  241. user_id = xuname2uid(userspec);
  242. }
  243. if (opt & CTX_STOP) {
  244. int i = do_stop();
  245. return (opt & OPT_OKNODO) ? 0 : (i<=0);
  246. }
  247. do_procinit();
  248. if (found) {
  249. if (!quiet)
  250. printf("%s already running\n%d\n", execname, found->pid);
  251. return !(opt & OPT_OKNODO);
  252. }
  253. *--argv = startas;
  254. if (opt & OPT_BACKGROUND) {
  255. setsid();
  256. bb_daemonize();
  257. }
  258. if (opt & OPT_MAKEPID) {
  259. /* user wants _us_ to make the pidfile */
  260. FILE *pidf = xfopen(pidfile, "w");
  261. pid_t pidt = getpid();
  262. fprintf(pidf, "%d\n", pidt);
  263. fclose(pidf);
  264. }
  265. if (chuid) {
  266. user_id = bb_strtou(chuid, NULL, 10);
  267. if (errno)
  268. user_id = xuname2uid(chuid);
  269. xsetuid(user_id);
  270. }
  271. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  272. if (opt & OPT_NICELEVEL) {
  273. /* Set process priority */
  274. int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
  275. if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
  276. bb_perror_msg_and_die("setpriority(%d)", prio);
  277. }
  278. }
  279. #endif
  280. execv(startas, argv);
  281. bb_perror_msg_and_die("cannot start %s", startas);
  282. }