3
0

start_stop_daemon.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 = NULL;
  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 equal;
  39. sprintf(buf, "/proc/%d/exe", pid);
  40. execbuf = xstrdup(name);
  41. readlink(buf, execbuf, strlen(name)+1);
  42. equal = ! strcmp(execbuf, name);
  43. if (ENABLE_FEATURE_CLEAN_UP)
  44. free(execbuf);
  45. return equal;
  46. }
  47. static int pid_is_user(int pid, int uid)
  48. {
  49. struct stat sb;
  50. char buf[sizeof("/proc/") + sizeof(int)*3];
  51. sprintf(buf, "/proc/%d", pid);
  52. if (stat(buf, &sb) != 0)
  53. return 0;
  54. return (sb.st_uid == uid);
  55. }
  56. static int pid_is_cmd(pid_t pid, const char *name)
  57. {
  58. char buf[sizeof("/proc//stat") + sizeof(int)*3];
  59. FILE *f;
  60. int c;
  61. sprintf(buf, "/proc/%d/stat", pid);
  62. f = fopen(buf, "r");
  63. if (!f)
  64. return 0;
  65. while ((c = getc(f)) != EOF && c != '(')
  66. ;
  67. if (c != '(') {
  68. fclose(f);
  69. return 0;
  70. }
  71. /* this hopefully handles command names containing ')' */
  72. while ((c = getc(f)) != EOF && c == *name)
  73. name++;
  74. fclose(f);
  75. return (c == ')' && *name == '\0');
  76. }
  77. static void check(int pid)
  78. {
  79. if (execname && !pid_is_exec(pid, execname)) {
  80. return;
  81. }
  82. if (userspec && !pid_is_user(pid, user_id)) {
  83. return;
  84. }
  85. if (cmdname && !pid_is_cmd(pid, cmdname)) {
  86. return;
  87. }
  88. push(pid);
  89. }
  90. static void do_pidfile(void)
  91. {
  92. FILE *f;
  93. pid_t pid;
  94. f = fopen(pidfile, "r");
  95. if (f) {
  96. if (fscanf(f, "%d", &pid) == 1)
  97. check(pid);
  98. fclose(f);
  99. } else if (errno != ENOENT)
  100. bb_perror_msg_and_die("open pidfile %s", pidfile);
  101. }
  102. static void do_procinit(void)
  103. {
  104. DIR *procdir;
  105. struct dirent *entry;
  106. int foundany, pid;
  107. if (pidfile) {
  108. do_pidfile();
  109. return;
  110. }
  111. procdir = xopendir("/proc");
  112. foundany = 0;
  113. while ((entry = readdir(procdir)) != NULL) {
  114. if (sscanf(entry->d_name, "%d", &pid) != 1)
  115. continue;
  116. foundany++;
  117. check(pid);
  118. }
  119. closedir(procdir);
  120. if (!foundany)
  121. bb_error_msg_and_die ("nothing in /proc - not mounted?");
  122. }
  123. static int do_stop(void)
  124. {
  125. char *what;
  126. struct pid_list *p;
  127. int killed = 0;
  128. do_procinit();
  129. if (cmdname)
  130. what = xstrdup(cmdname);
  131. else if (execname)
  132. what = xstrdup(execname);
  133. else if (pidfile)
  134. what = xasprintf("process in pidfile '%s'", pidfile);
  135. else if (userspec)
  136. what = xasprintf("process(es) owned by '%s'", userspec);
  137. else
  138. bb_error_msg_and_die("internal error, please report");
  139. if (!found) {
  140. if (!quiet)
  141. printf("no %s found; none killed\n", what);
  142. if (ENABLE_FEATURE_CLEAN_UP)
  143. free(what);
  144. return -1;
  145. }
  146. for (p = found; p; p = p->next) {
  147. if (kill(p->pid, signal_nr) == 0) {
  148. p->pid = -p->pid;
  149. killed++;
  150. } else {
  151. bb_perror_msg("warning: failed to kill %d", p->pid);
  152. }
  153. }
  154. if (!quiet && killed) {
  155. printf("stopped %s (pid", what);
  156. for (p = found; p; p = p->next)
  157. if(p->pid < 0)
  158. printf(" %d", -p->pid);
  159. puts(")");
  160. }
  161. if (ENABLE_FEATURE_CLEAN_UP)
  162. free(what);
  163. return killed;
  164. }
  165. #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
  166. static const struct option ssd_long_options[] = {
  167. { "stop", 0, NULL, 'K' },
  168. { "start", 0, NULL, 'S' },
  169. { "background", 0, NULL, 'b' },
  170. { "quiet", 0, NULL, 'q' },
  171. { "make-pidfile", 0, NULL, 'm' },
  172. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  173. { "oknodo", 0, NULL, 'o' },
  174. { "verbose", 0, NULL, 'v' },
  175. { "nicelevel", 1, NULL, 'N' },
  176. #endif
  177. { "startas", 1, NULL, 'a' },
  178. { "name", 1, NULL, 'n' },
  179. { "signal", 1, NULL, 's' },
  180. { "user", 1, NULL, 'u' },
  181. { "chuid", 1, NULL, 'c' },
  182. { "exec", 1, NULL, 'x' },
  183. { "pidfile", 1, NULL, 'p' },
  184. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  185. { "retry", 1, NULL, 'R' },
  186. #endif
  187. { 0, 0, 0, 0 }
  188. };
  189. #endif
  190. #define SSD_CTX_STOP 0x1
  191. #define SSD_CTX_START 0x2
  192. #define SSD_OPT_BACKGROUND 0x4
  193. #define SSD_OPT_QUIET 0x8
  194. #define SSD_OPT_MAKEPID 0x10
  195. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  196. #define SSD_OPT_OKNODO 0x20
  197. #define SSD_OPT_VERBOSE 0x40
  198. #define SSD_OPT_NICELEVEL 0x80
  199. #endif
  200. int start_stop_daemon_main(int argc, char **argv)
  201. {
  202. unsigned opt;
  203. char *signame = NULL;
  204. char *startas = NULL;
  205. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  206. // char *retry_arg = NULL;
  207. // int retries = -1;
  208. char *opt_N;
  209. #endif
  210. #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
  211. applet_long_options = ssd_long_options;
  212. #endif
  213. /* Check required one context option was given */
  214. opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
  215. opt = getopt32(argc, argv, "KSbqm"
  216. // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
  217. USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:")
  218. "a:n:s:u:c:x:p:"
  219. USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
  220. // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
  221. ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
  222. quiet = (opt & SSD_OPT_QUIET)
  223. USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
  224. if (signame) {
  225. signal_nr = get_signum(signame);
  226. if (signal_nr < 0) bb_show_usage();
  227. }
  228. if (!startas)
  229. startas = execname;
  230. // USE_FEATURE_START_STOP_DAEMON_FANCY(
  231. // if (retry_arg)
  232. // retries = xatoi_u(retry_arg);
  233. // )
  234. argc -= optind;
  235. argv += optind;
  236. if (userspec && sscanf(userspec, "%d", &user_id) != 1)
  237. user_id = bb_xgetpwnam(userspec);
  238. if (opt & SSD_CTX_STOP) {
  239. int i = do_stop();
  240. return
  241. USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
  242. ? 0 :) !!(i<=0);
  243. }
  244. do_procinit();
  245. if (found) {
  246. if (!quiet)
  247. printf("%s already running\n%d\n", execname, found->pid);
  248. USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
  249. SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
  250. }
  251. *--argv = startas;
  252. if (opt & SSD_OPT_BACKGROUND) {
  253. xdaemon(0, 0);
  254. setsid();
  255. }
  256. if (opt & SSD_OPT_MAKEPID) {
  257. /* user wants _us_ to make the pidfile */
  258. FILE *pidf = xfopen(pidfile, "w");
  259. pid_t pidt = getpid();
  260. fprintf(pidf, "%d\n", pidt);
  261. fclose(pidf);
  262. }
  263. if (chuid) {
  264. if (sscanf(chuid, "%d", &user_id) != 1)
  265. user_id = bb_xgetpwnam(chuid);
  266. xsetuid(user_id);
  267. }
  268. #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
  269. if (opt & SSD_OPT_NICELEVEL) {
  270. /* Set process priority */
  271. int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
  272. if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
  273. bb_perror_msg_and_die("setpriority(%d)", prio);
  274. }
  275. }
  276. #endif
  277. execv(startas, argv);
  278. bb_perror_msg_and_die("cannot start %s", startas);
  279. }