start-stop-daemon.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. /*
  2. * A rewrite of the original Debian's start-stop-daemon Perl script
  3. * in C (faster - it is executed many times during system startup).
  4. *
  5. * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
  6. * public domain. Based conceptually on start-stop-daemon.pl, by Ian
  7. * Jackson <ijackson@gnu.ai.mit.edu>. May be used and distributed
  8. * freely for any purpose. Changes by Christian Schwarz
  9. * <schwarz@monet.m.isar.de>, to make output conform to the Debian
  10. * Console Message Standard, also placed in public domain. Minor
  11. * changes by Klee Dienes <klee@debian.org>, also placed in the Public
  12. * Domain.
  13. *
  14. * Changes by Ben Collins <bcollins@debian.org>, added --chuid, --background
  15. * and --make-pidfile options, placed in public domain as well.
  16. *
  17. * Port to OpenBSD by Sontri Tomo Huynh <huynh.29@osu.edu>
  18. * and Andreas Schuldei <andreas@schuldei.org>
  19. *
  20. * Changes by Ian Jackson: added --retry (and associated rearrangements).
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <dpkg/macros.h>
  25. #if defined(linux) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
  26. # define OSLinux
  27. #elif defined(__GNU__)
  28. # define OSHurd
  29. #elif defined(__sun)
  30. # define OSsunos
  31. #elif defined(OPENBSD) || defined(__OpenBSD__)
  32. # define OSOpenBSD
  33. #elif defined(hpux)
  34. # define OShpux
  35. #elif defined(__FreeBSD__)
  36. # define OSFreeBSD
  37. #elif defined(__NetBSD__)
  38. # define OSNetBSD
  39. #elif defined(__DragonFly__)
  40. # define OSDragonFlyBSD
  41. #else
  42. # error Unknown architecture - cannot build start-stop-daemon
  43. #endif
  44. #ifdef HAVE_SYS_PARAM_H
  45. #include <sys/param.h>
  46. #endif
  47. #ifdef HAVE_SYS_SYSCALL_H
  48. #include <sys/syscall.h>
  49. #endif
  50. #ifdef HAVE_SYS_SYSCTL_H
  51. #include <sys/sysctl.h>
  52. #endif
  53. #ifdef HAVE_SYS_PROC_H
  54. #include <sys/proc.h>
  55. #endif
  56. #ifdef HAVE_SYS_USER_H
  57. #include <sys/user.h>
  58. #endif
  59. #ifdef HAVE_SYS_PSTAT_H
  60. #include <sys/pstat.h>
  61. #endif
  62. #include <sys/types.h>
  63. #include <sys/time.h>
  64. #include <sys/stat.h>
  65. #include <sys/ioctl.h>
  66. #include <assert.h>
  67. #include <errno.h>
  68. #include <limits.h>
  69. #include <fcntl.h>
  70. #include <dirent.h>
  71. #include <ctype.h>
  72. #include <string.h>
  73. #include <pwd.h>
  74. #include <grp.h>
  75. #include <signal.h>
  76. #include <termios.h>
  77. #include <unistd.h>
  78. #ifdef HAVE_STDDEF_H
  79. #include <stddef.h>
  80. #endif
  81. #include <stdbool.h>
  82. #include <stdarg.h>
  83. #include <stdlib.h>
  84. #include <stdio.h>
  85. #include <getopt.h>
  86. #ifdef HAVE_ERROR_H
  87. #include <error.h>
  88. #endif
  89. #ifdef HAVE_ERR_H
  90. #include <err.h>
  91. #endif
  92. #if defined(OSHurd)
  93. #include <hurd.h>
  94. #include <ps.h>
  95. #endif
  96. #ifdef HAVE_KVM_H
  97. #include <kvm.h>
  98. #endif
  99. #ifdef _POSIX_PRIORITY_SCHEDULING
  100. #include <sched.h>
  101. #else
  102. #define SCHED_OTHER -1
  103. #define SCHED_FIFO -1
  104. #define SCHED_RR -1
  105. #endif
  106. #if defined(OSLinux)
  107. /* This comes from TASK_COMM_LEN defined in Linux' include/linux/sched.h. */
  108. #define PROCESS_NAME_SIZE 15
  109. #elif defined(OSsunos)
  110. #define PROCESS_NAME_SIZE 15
  111. #elif defined(OSDarwin)
  112. #define PROCESS_NAME_SIZE 16
  113. #elif defined(OSNetBSD)
  114. #define PROCESS_NAME_SIZE 16
  115. #elif defined(OSOpenBSD)
  116. #define PROCESS_NAME_SIZE 16
  117. #elif defined(OSFreeBSD)
  118. #define PROCESS_NAME_SIZE 19
  119. #elif defined(OSDragonFlyBSD)
  120. /* On DragonFlyBSD MAXCOMLEN expands to 16. */
  121. #define PROCESS_NAME_SIZE MAXCOMLEN
  122. #endif
  123. #define MIN_POLL_INTERVAL 20000 /* µs */
  124. #if defined(SYS_ioprio_set) && defined(linux)
  125. #define HAVE_IOPRIO_SET
  126. #endif
  127. #define IOPRIO_CLASS_SHIFT 13
  128. #define IOPRIO_PRIO_VALUE(class, prio) (((class) << IOPRIO_CLASS_SHIFT) | (prio))
  129. #define IO_SCHED_PRIO_MIN 0
  130. #define IO_SCHED_PRIO_MAX 7
  131. enum {
  132. IOPRIO_WHO_PROCESS = 1,
  133. IOPRIO_WHO_PGRP,
  134. IOPRIO_WHO_USER,
  135. };
  136. enum {
  137. IOPRIO_CLASS_NONE,
  138. IOPRIO_CLASS_RT,
  139. IOPRIO_CLASS_BE,
  140. IOPRIO_CLASS_IDLE,
  141. };
  142. enum action_code {
  143. ACTION_NONE,
  144. ACTION_START,
  145. ACTION_STOP,
  146. ACTION_STATUS,
  147. };
  148. static enum action_code action;
  149. static bool testmode = false;
  150. static int quietmode = 0;
  151. static int exitnodo = 1;
  152. static bool background = false;
  153. static bool close_io = true;
  154. static bool mpidfile = false;
  155. static int signal_nr = SIGTERM;
  156. static int user_id = -1;
  157. static int runas_uid = -1;
  158. static int runas_gid = -1;
  159. static const char *userspec = NULL;
  160. static char *changeuser = NULL;
  161. static const char *changegroup = NULL;
  162. static char *changeroot = NULL;
  163. static const char *changedir = "/";
  164. static const char *cmdname = NULL;
  165. static char *execname = NULL;
  166. static char *startas = NULL;
  167. static pid_t match_pid = -1;
  168. static pid_t match_ppid = -1;
  169. static const char *pidfile = NULL;
  170. static char what_stop[1024];
  171. static const char *progname = "";
  172. static int nicelevel = 0;
  173. static int umask_value = -1;
  174. static struct stat exec_stat;
  175. #if defined(OSHurd)
  176. static struct proc_stat_list *procset = NULL;
  177. #endif
  178. /* LSB Init Script process status exit codes. */
  179. enum status_code {
  180. STATUS_OK = 0,
  181. STATUS_DEAD_PIDFILE = 1,
  182. STATUS_DEAD_LOCKFILE = 2,
  183. STATUS_DEAD = 3,
  184. STATUS_UNKNOWN = 4,
  185. };
  186. struct pid_list {
  187. struct pid_list *next;
  188. pid_t pid;
  189. };
  190. static struct pid_list *found = NULL;
  191. static struct pid_list *killed = NULL;
  192. /* Resource scheduling policy. */
  193. struct res_schedule {
  194. const char *policy_name;
  195. int policy;
  196. int priority;
  197. };
  198. struct schedule_item {
  199. enum {
  200. sched_timeout,
  201. sched_signal,
  202. sched_goto,
  203. /* Only seen within parse_schedule and callees. */
  204. sched_forever,
  205. } type;
  206. /* Seconds, signal no., or index into array. */
  207. int value;
  208. };
  209. static struct res_schedule *proc_sched = NULL;
  210. static struct res_schedule *io_sched = NULL;
  211. static int schedule_length;
  212. static struct schedule_item *schedule = NULL;
  213. static void DPKG_ATTR_PRINTF(1)
  214. warning(const char *format, ...)
  215. {
  216. va_list arglist;
  217. fprintf(stderr, "%s: warning: ", progname);
  218. va_start(arglist, format);
  219. vfprintf(stderr, format, arglist);
  220. va_end(arglist);
  221. }
  222. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1)
  223. fatal(const char *format, ...)
  224. {
  225. va_list arglist;
  226. int errno_fatal = errno;
  227. fprintf(stderr, "%s: ", progname);
  228. va_start(arglist, format);
  229. vfprintf(stderr, format, arglist);
  230. va_end(arglist);
  231. if (errno_fatal)
  232. fprintf(stderr, " (%s)\n", strerror(errno_fatal));
  233. else
  234. fprintf(stderr, "\n");
  235. if (action == ACTION_STATUS)
  236. exit(STATUS_UNKNOWN);
  237. else
  238. exit(2);
  239. }
  240. static void *
  241. xmalloc(int size)
  242. {
  243. void *ptr;
  244. ptr = malloc(size);
  245. if (ptr)
  246. return ptr;
  247. fatal("malloc(%d) failed", size);
  248. }
  249. static char *
  250. xstrdup(const char *str)
  251. {
  252. char *new_str;
  253. new_str = strdup(str);
  254. if (new_str)
  255. return new_str;
  256. fatal("strdup(%s) failed", str);
  257. }
  258. static void
  259. xgettimeofday(struct timeval *tv)
  260. {
  261. if (gettimeofday(tv, NULL) != 0)
  262. fatal("gettimeofday failed");
  263. }
  264. static void
  265. tmul(struct timeval *a, int b)
  266. {
  267. a->tv_sec *= b;
  268. a->tv_usec *= b;
  269. a->tv_sec = a->tv_sec + a->tv_usec / 1000000;
  270. a->tv_usec %= 1000000;
  271. }
  272. static char *
  273. newpath(const char *dirname, const char *filename)
  274. {
  275. char *path;
  276. size_t path_len;
  277. path_len = strlen(dirname) + 1 + strlen(filename) + 1;
  278. path = xmalloc(path_len);
  279. snprintf(path, path_len, "%s/%s", dirname, filename);
  280. return path;
  281. }
  282. static long
  283. get_open_fd_max(void)
  284. {
  285. #ifdef HAVE_GETDTABLESIZE
  286. return getdtablesize();
  287. #else
  288. return sysconf(_SC_OPEN_MAX);
  289. #endif
  290. }
  291. #ifndef HAVE_SETSID
  292. static void
  293. detach_controlling_tty(void)
  294. {
  295. #ifdef HAVE_TIOCNOTTY
  296. int tty_fd;
  297. tty_fd = open("/dev/tty", O_RDWR);
  298. /* The current process does not have a controlling tty. */
  299. if (tty_fd < 0)
  300. return;
  301. if (ioctl(tty_fd, TIOCNOTTY, 0) != 0)
  302. fatal("unable to detach controlling tty");
  303. close(tty_fd);
  304. #endif
  305. }
  306. static pid_t
  307. setsid(void)
  308. {
  309. setpgid(0, 0);
  310. detach_controlling_tty();
  311. return 0;
  312. }
  313. #endif
  314. static void
  315. daemonize(void)
  316. {
  317. pid_t pid;
  318. if (quietmode < 0)
  319. printf("Detaching to start %s...", startas);
  320. pid = fork();
  321. if (pid < 0)
  322. fatal("unable to do first fork");
  323. else if (pid) /* Parent. */
  324. _exit(0);
  325. /* Create a new session. */
  326. setsid();
  327. pid = fork();
  328. if (pid < 0)
  329. fatal("unable to do second fork");
  330. else if (pid) /* Parent. */
  331. _exit(0);
  332. if (quietmode < 0)
  333. printf("done.\n");
  334. }
  335. static void
  336. write_pidfile(const char *filename, pid_t pid)
  337. {
  338. FILE *fp;
  339. int fd;
  340. fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0666);
  341. if (fd < 0)
  342. fp = NULL;
  343. else
  344. fp = fdopen(fd, "w");
  345. if (fp == NULL)
  346. fatal("unable to open pidfile '%s' for writing", filename);
  347. fprintf(fp, "%d\n", pid);
  348. if (fclose(fp))
  349. fatal("unable to close pidfile '%s'", filename);
  350. }
  351. static void
  352. pid_list_push(struct pid_list **list, pid_t pid)
  353. {
  354. struct pid_list *p;
  355. p = xmalloc(sizeof(*p));
  356. p->next = *list;
  357. p->pid = pid;
  358. *list = p;
  359. }
  360. static void
  361. pid_list_free(struct pid_list **list)
  362. {
  363. struct pid_list *here, *next;
  364. for (here = *list; here != NULL; here = next) {
  365. next = here->next;
  366. free(here);
  367. }
  368. *list = NULL;
  369. }
  370. static void
  371. usage(void)
  372. {
  373. printf(
  374. "Usage: start-stop-daemon [<option> ...] <command>\n"
  375. "\n"
  376. "Commands:\n"
  377. " -S|--start -- <argument> ... start a program and pass <arguments> to it\n"
  378. " -K|--stop stop a program\n"
  379. " -T|--status get the program status\n"
  380. " -H|--help print help information\n"
  381. " -V|--version print version\n"
  382. "\n"
  383. "Matching options (at least one is required):\n"
  384. " --pid <pid> pid to check\n"
  385. " --ppid <ppid> parent pid to check\n"
  386. " -p|--pidfile <pid-file> pid file to check\n"
  387. " -x|--exec <executable> program to start/check if it is running\n"
  388. " -n|--name <process-name> process name to check\n"
  389. " -u|--user <username|uid> process owner to check\n"
  390. "\n"
  391. "Options:\n"
  392. " -g|--group <group|gid> run process as this group\n"
  393. " -c|--chuid <name|uid[:group|gid]>\n"
  394. " change to this user/group before starting\n"
  395. " process\n"
  396. " -s|--signal <signal> signal to send (default TERM)\n"
  397. " -a|--startas <pathname> program to start (default is <executable>)\n"
  398. " -r|--chroot <directory> chroot to <directory> before starting\n"
  399. " -d|--chdir <directory> change to <directory> (default is /)\n"
  400. " -N|--nicelevel <incr> add incr to the process' nice level\n"
  401. " -P|--procsched <policy[:prio]>\n"
  402. " use <policy> with <prio> for the kernel\n"
  403. " process scheduler (default prio is 0)\n"
  404. " -I|--iosched <class[:prio]> use <class> with <prio> to set the IO\n"
  405. " scheduler (default prio is 4)\n"
  406. " -k|--umask <mask> change the umask to <mask> before starting\n"
  407. " -b|--background force the process to detach\n"
  408. " -C|--no-close do not close any file descriptor\n"
  409. " -m|--make-pidfile create the pidfile before starting\n"
  410. " -R|--retry <schedule> check whether processes die, and retry\n"
  411. " -t|--test test mode, don't do anything\n"
  412. " -o|--oknodo exit status 0 (not 1) if nothing done\n"
  413. " -q|--quiet be more quiet\n"
  414. " -v|--verbose be more verbose\n"
  415. "\n"
  416. "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
  417. " -<signal-num>|[-]<signal-name> send that signal\n"
  418. " <timeout> wait that many seconds\n"
  419. " forever repeat remainder forever\n"
  420. "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
  421. "\n"
  422. "The process scheduler <policy> can be one of:\n"
  423. " other, fifo or rr\n"
  424. "\n"
  425. "The IO scheduler <class> can be one of:\n"
  426. " real-time, best-effort or idle\n"
  427. "\n"
  428. "Exit status:\n"
  429. " 0 = done\n"
  430. " 1 = nothing done (=> 0 if --oknodo)\n"
  431. " 2 = with --retry, processes would not die\n"
  432. " 3 = trouble\n"
  433. "Exit status with --status:\n"
  434. " 0 = program is running\n"
  435. " 1 = program is not running and the pid file exists\n"
  436. " 3 = program is not running\n"
  437. " 4 = unable to determine status\n");
  438. }
  439. static void
  440. do_version(void)
  441. {
  442. printf("start-stop-daemon %s for Debian\n\n", VERSION);
  443. printf("Written by Marek Michalkiewicz, public domain.\n");
  444. }
  445. static void DPKG_ATTR_NORET
  446. badusage(const char *msg)
  447. {
  448. if (msg)
  449. fprintf(stderr, "%s: %s\n", progname, msg);
  450. fprintf(stderr, "Try '%s --help' for more information.\n", progname);
  451. if (action == ACTION_STATUS)
  452. exit(STATUS_UNKNOWN);
  453. else
  454. exit(3);
  455. }
  456. struct sigpair {
  457. const char *name;
  458. int signal;
  459. };
  460. static const struct sigpair siglist[] = {
  461. { "ABRT", SIGABRT },
  462. { "ALRM", SIGALRM },
  463. { "FPE", SIGFPE },
  464. { "HUP", SIGHUP },
  465. { "ILL", SIGILL },
  466. { "INT", SIGINT },
  467. { "KILL", SIGKILL },
  468. { "PIPE", SIGPIPE },
  469. { "QUIT", SIGQUIT },
  470. { "SEGV", SIGSEGV },
  471. { "TERM", SIGTERM },
  472. { "USR1", SIGUSR1 },
  473. { "USR2", SIGUSR2 },
  474. { "CHLD", SIGCHLD },
  475. { "CONT", SIGCONT },
  476. { "STOP", SIGSTOP },
  477. { "TSTP", SIGTSTP },
  478. { "TTIN", SIGTTIN },
  479. { "TTOU", SIGTTOU }
  480. };
  481. static int
  482. parse_unsigned(const char *string, int base, int *value_r)
  483. {
  484. long value;
  485. char *endptr;
  486. if (!string[0])
  487. return -1;
  488. errno = 0;
  489. value = strtol(string, &endptr, base);
  490. if (string == endptr || *endptr != '\0' || errno != 0)
  491. return -1;
  492. if (value < 0 || value > INT_MAX)
  493. return -1;
  494. *value_r = value;
  495. return 0;
  496. }
  497. static int
  498. parse_pid(const char *pid_str, int *pid_num)
  499. {
  500. if (parse_unsigned(pid_str, 10, pid_num) != 0)
  501. return -1;
  502. if (*pid_num == 0)
  503. return -1;
  504. return 0;
  505. }
  506. static int
  507. parse_signal(const char *sig_str, int *sig_num)
  508. {
  509. unsigned int i;
  510. if (parse_unsigned(sig_str, 10, sig_num) == 0)
  511. return 0;
  512. for (i = 0; i < array_count(siglist); i++) {
  513. if (strcmp(sig_str, siglist[i].name) == 0) {
  514. *sig_num = siglist[i].signal;
  515. return 0;
  516. }
  517. }
  518. return -1;
  519. }
  520. static int
  521. parse_umask(const char *string, int *value_r)
  522. {
  523. return parse_unsigned(string, 0, value_r);
  524. }
  525. static void
  526. validate_proc_schedule(void)
  527. {
  528. #ifdef _POSIX_PRIORITY_SCHEDULING
  529. int prio_min, prio_max;
  530. prio_min = sched_get_priority_min(proc_sched->policy);
  531. prio_max = sched_get_priority_max(proc_sched->policy);
  532. if (proc_sched->priority < prio_min)
  533. badusage("process scheduler priority less than min");
  534. if (proc_sched->priority > prio_max)
  535. badusage("process scheduler priority greater than max");
  536. #endif
  537. }
  538. static void
  539. parse_proc_schedule(const char *string)
  540. {
  541. char *policy_str, *prio_str;
  542. int prio = 0;
  543. policy_str = xstrdup(string);
  544. policy_str = strtok(policy_str, ":");
  545. prio_str = strtok(NULL, ":");
  546. if (prio_str && parse_unsigned(prio_str, 10, &prio) != 0)
  547. fatal("invalid process scheduler priority");
  548. proc_sched = xmalloc(sizeof(*proc_sched));
  549. proc_sched->policy_name = policy_str;
  550. if (strcmp(policy_str, "other") == 0) {
  551. proc_sched->policy = SCHED_OTHER;
  552. proc_sched->priority = 0;
  553. } else if (strcmp(policy_str, "fifo") == 0) {
  554. proc_sched->policy = SCHED_FIFO;
  555. proc_sched->priority = prio;
  556. } else if (strcmp(policy_str, "rr") == 0) {
  557. proc_sched->policy = SCHED_RR;
  558. proc_sched->priority = prio;
  559. } else
  560. badusage("invalid process scheduler policy");
  561. validate_proc_schedule();
  562. }
  563. static void
  564. parse_io_schedule(const char *string)
  565. {
  566. char *class_str, *prio_str;
  567. int prio = 4;
  568. class_str = xstrdup(string);
  569. class_str = strtok(class_str, ":");
  570. prio_str = strtok(NULL, ":");
  571. if (prio_str && parse_unsigned(prio_str, 10, &prio) != 0)
  572. fatal("invalid IO scheduler priority");
  573. io_sched = xmalloc(sizeof(*io_sched));
  574. io_sched->policy_name = class_str;
  575. if (strcmp(class_str, "real-time") == 0) {
  576. io_sched->policy = IOPRIO_CLASS_RT;
  577. io_sched->priority = prio;
  578. } else if (strcmp(class_str, "best-effort") == 0) {
  579. io_sched->policy = IOPRIO_CLASS_BE;
  580. io_sched->priority = prio;
  581. } else if (strcmp(class_str, "idle") == 0) {
  582. io_sched->policy = IOPRIO_CLASS_IDLE;
  583. io_sched->priority = 7;
  584. } else
  585. badusage("invalid IO scheduler policy");
  586. if (io_sched->priority < IO_SCHED_PRIO_MIN)
  587. badusage("IO scheduler priority less than min");
  588. if (io_sched->priority > IO_SCHED_PRIO_MAX)
  589. badusage("IO scheduler priority greater than max");
  590. }
  591. static void
  592. set_proc_schedule(struct res_schedule *sched)
  593. {
  594. #ifdef _POSIX_PRIORITY_SCHEDULING
  595. struct sched_param param;
  596. param.sched_priority = sched->priority;
  597. if (sched_setscheduler(getpid(), sched->policy, &param) == -1)
  598. fatal("unable to set process scheduler");
  599. #endif
  600. }
  601. #ifdef HAVE_IOPRIO_SET
  602. static inline int
  603. ioprio_set(int which, int who, int ioprio)
  604. {
  605. return syscall(SYS_ioprio_set, which, who, ioprio);
  606. }
  607. #endif
  608. static void
  609. set_io_schedule(struct res_schedule *sched)
  610. {
  611. #ifdef HAVE_IOPRIO_SET
  612. int io_sched_mask;
  613. io_sched_mask = IOPRIO_PRIO_VALUE(sched->policy, sched->priority);
  614. if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), io_sched_mask) == -1)
  615. warning("unable to alter IO priority to mask %i (%s)\n",
  616. io_sched_mask, strerror(errno));
  617. #endif
  618. }
  619. static void
  620. parse_schedule_item(const char *string, struct schedule_item *item)
  621. {
  622. const char *after_hyph;
  623. if (strcmp(string, "forever") == 0) {
  624. item->type = sched_forever;
  625. } else if (isdigit(string[0])) {
  626. item->type = sched_timeout;
  627. if (parse_unsigned(string, 10, &item->value) != 0)
  628. badusage("invalid timeout value in schedule");
  629. } else if ((after_hyph = string + (string[0] == '-')) &&
  630. parse_signal(after_hyph, &item->value) == 0) {
  631. item->type = sched_signal;
  632. } else {
  633. badusage("invalid schedule item (must be [-]<signal-name>, "
  634. "-<signal-number>, <timeout> or 'forever'");
  635. }
  636. }
  637. static void
  638. parse_schedule(const char *schedule_str)
  639. {
  640. char item_buf[20];
  641. const char *slash;
  642. int count, repeatat;
  643. size_t str_len;
  644. count = 0;
  645. for (slash = schedule_str; *slash; slash++)
  646. if (*slash == '/')
  647. count++;
  648. schedule_length = (count == 0) ? 4 : count + 1;
  649. schedule = xmalloc(sizeof(*schedule) * schedule_length);
  650. if (count == 0) {
  651. schedule[0].type = sched_signal;
  652. schedule[0].value = signal_nr;
  653. parse_schedule_item(schedule_str, &schedule[1]);
  654. if (schedule[1].type != sched_timeout) {
  655. badusage("--retry takes timeout, or schedule list"
  656. " of at least two items");
  657. }
  658. schedule[2].type = sched_signal;
  659. schedule[2].value = SIGKILL;
  660. schedule[3] = schedule[1];
  661. } else {
  662. count = 0;
  663. repeatat = -1;
  664. while (schedule_str != NULL) {
  665. slash = strchr(schedule_str, '/');
  666. str_len = slash ? (size_t)(slash - schedule_str) : strlen(schedule_str);
  667. if (str_len >= sizeof(item_buf))
  668. badusage("invalid schedule item: far too long"
  669. " (you must delimit items with slashes)");
  670. memcpy(item_buf, schedule_str, str_len);
  671. item_buf[str_len] = '\0';
  672. schedule_str = slash ? slash + 1 : NULL;
  673. parse_schedule_item(item_buf, &schedule[count]);
  674. if (schedule[count].type == sched_forever) {
  675. if (repeatat >= 0)
  676. badusage("invalid schedule: 'forever'"
  677. " appears more than once");
  678. repeatat = count;
  679. continue;
  680. }
  681. count++;
  682. }
  683. if (repeatat == count)
  684. badusage("invalid schedule: 'forever' appears last, "
  685. "nothing to repeat");
  686. if (repeatat >= 0) {
  687. schedule[count].type = sched_goto;
  688. schedule[count].value = repeatat;
  689. count++;
  690. }
  691. assert(count == schedule_length);
  692. }
  693. }
  694. static void
  695. set_action(enum action_code new_action)
  696. {
  697. if (action == new_action)
  698. return;
  699. if (action != ACTION_NONE)
  700. badusage("only one command can be specified");
  701. action = new_action;
  702. }
  703. #define OPT_PID 500
  704. #define OPT_PPID 501
  705. static void
  706. parse_options(int argc, char * const *argv)
  707. {
  708. static struct option longopts[] = {
  709. { "help", 0, NULL, 'H'},
  710. { "stop", 0, NULL, 'K'},
  711. { "start", 0, NULL, 'S'},
  712. { "status", 0, NULL, 'T'},
  713. { "version", 0, NULL, 'V'},
  714. { "startas", 1, NULL, 'a'},
  715. { "name", 1, NULL, 'n'},
  716. { "oknodo", 0, NULL, 'o'},
  717. { "pid", 1, NULL, OPT_PID},
  718. { "ppid", 1, NULL, OPT_PPID},
  719. { "pidfile", 1, NULL, 'p'},
  720. { "quiet", 0, NULL, 'q'},
  721. { "signal", 1, NULL, 's'},
  722. { "test", 0, NULL, 't'},
  723. { "user", 1, NULL, 'u'},
  724. { "group", 1, NULL, 'g'},
  725. { "chroot", 1, NULL, 'r'},
  726. { "verbose", 0, NULL, 'v'},
  727. { "exec", 1, NULL, 'x'},
  728. { "chuid", 1, NULL, 'c'},
  729. { "nicelevel", 1, NULL, 'N'},
  730. { "procsched", 1, NULL, 'P'},
  731. { "iosched", 1, NULL, 'I'},
  732. { "umask", 1, NULL, 'k'},
  733. { "background", 0, NULL, 'b'},
  734. { "no-close", 0, NULL, 'C'},
  735. { "make-pidfile", 0, NULL, 'm'},
  736. { "retry", 1, NULL, 'R'},
  737. { "chdir", 1, NULL, 'd'},
  738. { NULL, 0, NULL, 0 }
  739. };
  740. const char *pid_str = NULL;
  741. const char *ppid_str = NULL;
  742. const char *umask_str = NULL;
  743. const char *signal_str = NULL;
  744. const char *schedule_str = NULL;
  745. const char *proc_schedule_str = NULL;
  746. const char *io_schedule_str = NULL;
  747. int c;
  748. for (;;) {
  749. c = getopt_long(argc, argv,
  750. "HKSVTa:n:op:qr:s:tu:vx:c:N:P:I:k:bCmR:g:d:",
  751. longopts, NULL);
  752. if (c == -1)
  753. break;
  754. switch (c) {
  755. case 'H': /* --help */
  756. usage();
  757. exit(0);
  758. case 'K': /* --stop */
  759. set_action(ACTION_STOP);
  760. break;
  761. case 'S': /* --start */
  762. set_action(ACTION_START);
  763. break;
  764. case 'T': /* --status */
  765. set_action(ACTION_STATUS);
  766. break;
  767. case 'V': /* --version */
  768. do_version();
  769. exit(0);
  770. case 'a': /* --startas <pathname> */
  771. startas = optarg;
  772. break;
  773. case 'n': /* --name <process-name> */
  774. cmdname = optarg;
  775. break;
  776. case 'o': /* --oknodo */
  777. exitnodo = 0;
  778. break;
  779. case OPT_PID: /* --pid <pid> */
  780. pid_str = optarg;
  781. break;
  782. case OPT_PPID: /* --ppid <ppid> */
  783. ppid_str = optarg;
  784. break;
  785. case 'p': /* --pidfile <pid-file> */
  786. pidfile = optarg;
  787. break;
  788. case 'q': /* --quiet */
  789. quietmode = true;
  790. break;
  791. case 's': /* --signal <signal> */
  792. signal_str = optarg;
  793. break;
  794. case 't': /* --test */
  795. testmode = true;
  796. break;
  797. case 'u': /* --user <username>|<uid> */
  798. userspec = optarg;
  799. break;
  800. case 'v': /* --verbose */
  801. quietmode = -1;
  802. break;
  803. case 'x': /* --exec <executable> */
  804. execname = optarg;
  805. break;
  806. case 'c': /* --chuid <username>|<uid> */
  807. /* We copy the string just in case we need the
  808. * argument later. */
  809. changeuser = xstrdup(optarg);
  810. changeuser = strtok(changeuser, ":");
  811. changegroup = strtok(NULL, ":");
  812. break;
  813. case 'g': /* --group <group>|<gid> */
  814. changegroup = optarg;
  815. break;
  816. case 'r': /* --chroot /new/root */
  817. changeroot = optarg;
  818. break;
  819. case 'N': /* --nice */
  820. nicelevel = atoi(optarg);
  821. break;
  822. case 'P': /* --procsched */
  823. proc_schedule_str = optarg;
  824. break;
  825. case 'I': /* --iosched */
  826. io_schedule_str = optarg;
  827. break;
  828. case 'k': /* --umask <mask> */
  829. umask_str = optarg;
  830. break;
  831. case 'b': /* --background */
  832. background = true;
  833. break;
  834. case 'C': /* --no-close */
  835. close_io = false;
  836. break;
  837. case 'm': /* --make-pidfile */
  838. mpidfile = true;
  839. break;
  840. case 'R': /* --retry <schedule>|<timeout> */
  841. schedule_str = optarg;
  842. break;
  843. case 'd': /* --chdir /new/dir */
  844. changedir = optarg;
  845. break;
  846. default:
  847. /* Message printed by getopt. */
  848. badusage(NULL);
  849. }
  850. }
  851. if (pid_str != NULL) {
  852. if (parse_pid(pid_str, &match_pid) != 0)
  853. badusage("pid value must be a number greater than 0");
  854. }
  855. if (ppid_str != NULL) {
  856. if (parse_pid(ppid_str, &match_ppid) != 0)
  857. badusage("ppid value must be a number greater than 0");
  858. }
  859. if (signal_str != NULL) {
  860. if (parse_signal(signal_str, &signal_nr) != 0)
  861. badusage("signal value must be numeric or name"
  862. " of signal (KILL, INT, ...)");
  863. }
  864. if (schedule_str != NULL) {
  865. parse_schedule(schedule_str);
  866. }
  867. if (proc_schedule_str != NULL)
  868. parse_proc_schedule(proc_schedule_str);
  869. if (io_schedule_str != NULL)
  870. parse_io_schedule(io_schedule_str);
  871. if (umask_str != NULL) {
  872. if (parse_umask(umask_str, &umask_value) != 0)
  873. badusage("umask value must be a positive number");
  874. }
  875. if (action == ACTION_NONE)
  876. badusage("need one of --start or --stop or --status");
  877. if (!execname && !pid_str && !ppid_str && !pidfile && !userspec &&
  878. !cmdname)
  879. badusage("need at least one of --exec, --pid, --ppid, --pidfile, --user or --name");
  880. #ifdef PROCESS_NAME_SIZE
  881. if (cmdname && strlen(cmdname) > PROCESS_NAME_SIZE)
  882. warning("this system is not able to track process names\n"
  883. "longer than %d characters, please use --exec "
  884. "instead of --name.\n", PROCESS_NAME_SIZE);
  885. #endif
  886. if (!startas)
  887. startas = execname;
  888. if (action == ACTION_START && !startas)
  889. badusage("--start needs --exec or --startas");
  890. if (mpidfile && pidfile == NULL)
  891. badusage("--make-pidfile requires --pidfile");
  892. if (pid_str && pidfile)
  893. badusage("need either --pid of --pidfile, not both");
  894. if (background && action != ACTION_START)
  895. badusage("--background is only relevant with --start");
  896. if (!close_io && !background)
  897. badusage("--no-close is only relevant with --background");
  898. }
  899. static void
  900. setup_options(void)
  901. {
  902. if (execname) {
  903. char *fullexecname;
  904. /* If it's a relative path, normalize it. */
  905. if (execname[0] != '/')
  906. execname = newpath(changedir, execname);
  907. if (changeroot)
  908. fullexecname = newpath(changeroot, execname);
  909. else
  910. fullexecname = execname;
  911. if (stat(fullexecname, &exec_stat))
  912. fatal("unable to stat %s", fullexecname);
  913. if (fullexecname != execname)
  914. free(fullexecname);
  915. }
  916. if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
  917. struct passwd *pw;
  918. pw = getpwnam(userspec);
  919. if (!pw)
  920. fatal("user '%s' not found", userspec);
  921. user_id = pw->pw_uid;
  922. }
  923. if (changegroup && sscanf(changegroup, "%d", &runas_gid) != 1) {
  924. struct group *gr;
  925. gr = getgrnam(changegroup);
  926. if (!gr)
  927. fatal("group '%s' not found", changegroup);
  928. changegroup = gr->gr_name;
  929. runas_gid = gr->gr_gid;
  930. }
  931. if (changeuser) {
  932. struct passwd *pw;
  933. struct stat st;
  934. if (sscanf(changeuser, "%d", &runas_uid) == 1)
  935. pw = getpwuid(runas_uid);
  936. else
  937. pw = getpwnam(changeuser);
  938. if (!pw)
  939. fatal("user '%s' not found", changeuser);
  940. changeuser = pw->pw_name;
  941. runas_uid = pw->pw_uid;
  942. if (changegroup == NULL) {
  943. /* Pass the default group of this user. */
  944. changegroup = ""; /* Just empty. */
  945. runas_gid = pw->pw_gid;
  946. }
  947. if (stat(pw->pw_dir, &st) == 0)
  948. setenv("HOME", pw->pw_dir, 1);
  949. }
  950. }
  951. #if defined(OSLinux)
  952. static const char *
  953. proc_status_field(pid_t pid, const char *field)
  954. {
  955. static char *line = NULL;
  956. static size_t line_size = 0;
  957. FILE *fp;
  958. char filename[32];
  959. char *value = NULL;
  960. ssize_t line_len;
  961. size_t field_len = strlen(field);
  962. sprintf(filename, "/proc/%d/status", pid);
  963. fp = fopen(filename, "r");
  964. if (!fp)
  965. return NULL;
  966. while ((line_len = getline(&line, &line_size, fp)) >= 0) {
  967. if (strncasecmp(line, field, field_len) == 0) {
  968. line[line_len - 1] = '\0';
  969. value = line + field_len;
  970. while (isspace(*value))
  971. value++;
  972. break;
  973. }
  974. }
  975. fclose(fp);
  976. return value;
  977. }
  978. #endif
  979. #if defined(OSHurd)
  980. static void
  981. init_procset(void)
  982. {
  983. struct ps_context *context;
  984. error_t err;
  985. err = ps_context_create(getproc(), &context);
  986. if (err)
  987. error(1, err, "ps_context_create");
  988. err = proc_stat_list_create(context, &procset);
  989. if (err)
  990. error(1, err, "proc_stat_list_create");
  991. err = proc_stat_list_add_all(procset, 0, 0);
  992. if (err)
  993. error(1, err, "proc_stat_list_add_all");
  994. }
  995. static struct proc_stat *
  996. get_proc_stat(pid_t pid, ps_flags_t flags)
  997. {
  998. struct proc_stat *ps;
  999. ps_flags_t wanted_flags = PSTAT_PID | flags;
  1000. if (!procset)
  1001. init_procset();
  1002. ps = proc_stat_list_pid_proc_stat(procset, pid);
  1003. if (!ps)
  1004. return NULL;
  1005. if (proc_stat_set_flags(ps, wanted_flags))
  1006. return NULL;
  1007. if ((proc_stat_flags(ps) & wanted_flags) != wanted_flags)
  1008. return NULL;
  1009. return ps;
  1010. }
  1011. #endif
  1012. #if defined(OSLinux)
  1013. static bool
  1014. pid_is_exec(pid_t pid, const struct stat *esb)
  1015. {
  1016. char lname[32];
  1017. char lcontents[_POSIX_PATH_MAX + 1];
  1018. char *filename;
  1019. const char deleted[] = " (deleted)";
  1020. int nread;
  1021. struct stat sb;
  1022. sprintf(lname, "/proc/%d/exe", pid);
  1023. nread = readlink(lname, lcontents, sizeof(lcontents) - 1);
  1024. if (nread == -1)
  1025. return false;
  1026. filename = lcontents;
  1027. filename[nread] = '\0';
  1028. /* OpenVZ kernels contain a bogus patch that instead of appending,
  1029. * prepends the deleted marker. Workaround those. Otherwise handle
  1030. * the normal appended marker. */
  1031. if (strncmp(filename, deleted, strlen(deleted)) == 0)
  1032. filename += strlen(deleted);
  1033. else if (strcmp(filename + nread - strlen(deleted), deleted) == 0)
  1034. filename[nread - strlen(deleted)] = '\0';
  1035. if (stat(filename, &sb) != 0)
  1036. return false;
  1037. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  1038. }
  1039. #elif defined(OSHurd)
  1040. static bool
  1041. pid_is_exec(pid_t pid, const struct stat *esb)
  1042. {
  1043. struct proc_stat *ps;
  1044. struct stat sb;
  1045. const char *filename;
  1046. ps = get_proc_stat(pid, PSTAT_ARGS);
  1047. if (ps == NULL)
  1048. return false;
  1049. filename = proc_stat_args(ps);
  1050. if (stat(filename, &sb) != 0)
  1051. return false;
  1052. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  1053. }
  1054. #elif defined(OShpux)
  1055. static bool
  1056. pid_is_exec(pid_t pid, const struct stat *esb)
  1057. {
  1058. struct pst_status pst;
  1059. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  1060. return false;
  1061. return ((dev_t)pst.pst_text.psf_fsid.psfs_id == esb->st_dev &&
  1062. (ino_t)pst.pst_text.psf_fileid == esb->st_ino);
  1063. }
  1064. #elif defined(OSFreeBSD)
  1065. static bool
  1066. pid_is_exec(pid_t pid, const struct stat *esb)
  1067. {
  1068. struct stat sb;
  1069. int error, name[4];
  1070. size_t len;
  1071. char pathname[PATH_MAX];
  1072. name[0] = CTL_KERN;
  1073. name[1] = KERN_PROC;
  1074. name[2] = KERN_PROC_PATHNAME;
  1075. name[3] = pid;
  1076. len = sizeof(pathname);
  1077. error = sysctl(name, 4, pathname, &len, NULL, 0);
  1078. if (error != 0 && errno != ESRCH)
  1079. return false;
  1080. if (len == 0)
  1081. pathname[0] = '\0';
  1082. if (stat(pathname, &sb) != 0)
  1083. return false;
  1084. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  1085. }
  1086. #elif defined(HAVE_KVM_H)
  1087. static bool
  1088. pid_is_exec(pid_t pid, const struct stat *esb)
  1089. {
  1090. kvm_t *kd;
  1091. int nentries, argv_len = 0;
  1092. struct kinfo_proc *kp;
  1093. struct stat sb;
  1094. char errbuf[_POSIX2_LINE_MAX], buf[_POSIX2_LINE_MAX];
  1095. char **pid_argv_p;
  1096. char *start_argv_0_p, *end_argv_0_p;
  1097. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  1098. if (kd == NULL)
  1099. errx(1, "%s", errbuf);
  1100. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  1101. if (kp == NULL)
  1102. errx(1, "%s", kvm_geterr(kd));
  1103. pid_argv_p = kvm_getargv(kd, kp, argv_len);
  1104. if (pid_argv_p == NULL)
  1105. errx(1, "%s", kvm_geterr(kd));
  1106. /* Find and compare string. */
  1107. start_argv_0_p = *pid_argv_p;
  1108. /* Find end of argv[0] then copy and cut of str there. */
  1109. end_argv_0_p = strchr(*pid_argv_p, ' ');
  1110. if (end_argv_0_p == NULL)
  1111. /* There seems to be no space, so we have the command
  1112. * already in its desired form. */
  1113. start_argv_0_p = *pid_argv_p;
  1114. else {
  1115. /* Tests indicate that this never happens, since
  1116. * kvm_getargv itself cuts of tailing stuff. This is
  1117. * not what the manpage says, however. */
  1118. strncpy(buf, *pid_argv_p, (end_argv_0_p - start_argv_0_p));
  1119. buf[(end_argv_0_p - start_argv_0_p) + 1] = '\0';
  1120. start_argv_0_p = buf;
  1121. }
  1122. if (stat(start_argv_0_p, &sb) != 0)
  1123. return false;
  1124. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  1125. }
  1126. #endif
  1127. #if defined(OSLinux)
  1128. static bool
  1129. pid_is_child(pid_t pid, pid_t ppid)
  1130. {
  1131. const char *ppid_str;
  1132. pid_t proc_ppid;
  1133. int rc;
  1134. ppid_str = proc_status_field(pid, "PPid:");
  1135. if (ppid_str == NULL)
  1136. return false;
  1137. rc = parse_pid(ppid_str, &proc_ppid);
  1138. if (rc < 0)
  1139. return false;
  1140. return proc_ppid == ppid;
  1141. }
  1142. #elif defined(OSHurd)
  1143. static bool
  1144. pid_is_child(pid_t pid, pid_t ppid)
  1145. {
  1146. struct proc_stat *ps;
  1147. struct procinfo *pi;
  1148. ps = get_proc_stat(pid, PSTAT_PROC_INFO);
  1149. if (ps == NULL)
  1150. return false;
  1151. pi = proc_stat_proc_info(ps);
  1152. return pi->ppid == ppid;
  1153. }
  1154. #elif defined(OShpux)
  1155. static bool
  1156. pid_is_child(pid_t pid, pid_t ppid)
  1157. {
  1158. struct pst_status pst;
  1159. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  1160. return false;
  1161. return pst.pst_ppid == ppid;
  1162. }
  1163. #elif defined(HAVE_KVM_H)
  1164. static bool
  1165. pid_is_child(pid_t pid, pid_t ppid)
  1166. {
  1167. kvm_t *kd;
  1168. int nentries;
  1169. struct kinfo_proc *kp;
  1170. char errbuf[_POSIX2_LINE_MAX];
  1171. pid_t proc_ppid;
  1172. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  1173. if (kd == NULL)
  1174. errx(1, "%s", errbuf);
  1175. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  1176. if (kp == NULL)
  1177. errx(1, "%s", kvm_geterr(kd));
  1178. #if defined(OSFreeBSD)
  1179. proc_ppid = kp->ki_ppid;
  1180. #elif defined(OSOpenBSD)
  1181. proc_ppid = kp->p_ppid;
  1182. #elif defined(OSDragonFlyBSD)
  1183. proc_ppid = kp->kp_ppid;
  1184. #else
  1185. proc_ppid = kp->kp_proc.p_ppid;
  1186. #endif
  1187. return proc_ppid == ppid;
  1188. }
  1189. #endif
  1190. #if defined(OSLinux)
  1191. static bool
  1192. pid_is_user(pid_t pid, uid_t uid)
  1193. {
  1194. struct stat sb;
  1195. char buf[32];
  1196. sprintf(buf, "/proc/%d", pid);
  1197. if (stat(buf, &sb) != 0)
  1198. return false;
  1199. return (sb.st_uid == uid);
  1200. }
  1201. #elif defined(OSHurd)
  1202. static bool
  1203. pid_is_user(pid_t pid, uid_t uid)
  1204. {
  1205. struct proc_stat *ps;
  1206. ps = get_proc_stat(pid, PSTAT_OWNER_UID);
  1207. return ps && (uid_t)proc_stat_owner_uid(ps) == uid;
  1208. }
  1209. #elif defined(OShpux)
  1210. static bool
  1211. pid_is_user(pid_t pid, uid_t uid)
  1212. {
  1213. struct pst_status pst;
  1214. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  1215. return false;
  1216. return ((uid_t)pst.pst_uid == uid);
  1217. }
  1218. #elif defined(HAVE_KVM_H)
  1219. static bool
  1220. pid_is_user(pid_t pid, uid_t uid)
  1221. {
  1222. kvm_t *kd;
  1223. int nentries; /* Value not used. */
  1224. uid_t proc_uid;
  1225. struct kinfo_proc *kp;
  1226. char errbuf[_POSIX2_LINE_MAX];
  1227. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  1228. if (kd == NULL)
  1229. errx(1, "%s", errbuf);
  1230. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  1231. if (kp == NULL)
  1232. errx(1, "%s", kvm_geterr(kd));
  1233. #if defined(OSFreeBSD)
  1234. proc_uid = kp->ki_ruid;
  1235. #elif defined(OSOpenBSD)
  1236. proc_uid = kp->p_ruid;
  1237. #elif defined(OSDragonFlyBSD)
  1238. proc_uid = kp->kp_ruid;
  1239. #else
  1240. if (kp->kp_proc.p_cred)
  1241. kvm_read(kd, (u_long)&(kp->kp_proc.p_cred->p_ruid),
  1242. &proc_uid, sizeof(uid_t));
  1243. else
  1244. return false;
  1245. #endif
  1246. return (proc_uid == (uid_t)uid);
  1247. }
  1248. #endif
  1249. #if defined(OSLinux)
  1250. static bool
  1251. pid_is_cmd(pid_t pid, const char *name)
  1252. {
  1253. const char *comm;
  1254. comm = proc_status_field(pid, "Name:");
  1255. if (comm == NULL)
  1256. return false;
  1257. return strcmp(comm, name) == 0;
  1258. }
  1259. #elif defined(OSHurd)
  1260. static bool
  1261. pid_is_cmd(pid_t pid, const char *name)
  1262. {
  1263. struct proc_stat *ps;
  1264. size_t argv0_len;
  1265. const char *argv0;
  1266. const char *binary_name;
  1267. ps = get_proc_stat(pid, PSTAT_ARGS);
  1268. if (ps == NULL)
  1269. return false;
  1270. argv0 = proc_stat_args(ps);
  1271. argv0_len = strlen(argv0) + 1;
  1272. binary_name = basename(argv0);
  1273. if (strcmp(binary_name, name) == 0)
  1274. return true;
  1275. /* XXX: This is all kinds of ugly, but on the Hurd there's no way to
  1276. * know the command name of a process, so we have to try to match
  1277. * also on argv[1] for the case of an interpreted script. */
  1278. if (proc_stat_args_len(ps) > argv0_len) {
  1279. const char *script_name = basename(argv0 + argv0_len);
  1280. return strcmp(script_name, name) == 0;
  1281. }
  1282. return false;
  1283. }
  1284. #elif defined(OShpux)
  1285. static bool
  1286. pid_is_cmd(pid_t pid, const char *name)
  1287. {
  1288. struct pst_status pst;
  1289. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  1290. return false;
  1291. return (strcmp(pst.pst_ucomm, name) == 0);
  1292. }
  1293. #elif defined(HAVE_KVM_H)
  1294. static bool
  1295. pid_is_cmd(pid_t pid, const char *name)
  1296. {
  1297. kvm_t *kd;
  1298. int nentries;
  1299. struct kinfo_proc *kp;
  1300. char errbuf[_POSIX2_LINE_MAX], *process_name;
  1301. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  1302. if (kd == NULL)
  1303. errx(1, "%s", errbuf);
  1304. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  1305. if (kp == NULL)
  1306. errx(1, "%s", kvm_geterr(kd));
  1307. #if defined(OSFreeBSD)
  1308. process_name = kp->ki_comm;
  1309. #elif defined(OSOpenBSD)
  1310. process_name = kp->p_comm;
  1311. #elif defined(OSDragonFlyBSD)
  1312. process_name = kp->kp_comm;
  1313. #else
  1314. process_name = kp->kp_proc.p_comm;
  1315. #endif
  1316. if (strlen(name) != strlen(process_name))
  1317. return false;
  1318. return (strcmp(name, process_name) == 0);
  1319. }
  1320. #endif
  1321. #if defined(OSHurd)
  1322. static bool
  1323. pid_is_running(pid_t pid)
  1324. {
  1325. return get_proc_stat(pid, 0) != NULL;
  1326. }
  1327. #else /* !OSHurd */
  1328. static bool
  1329. pid_is_running(pid_t pid)
  1330. {
  1331. if (kill(pid, 0) == 0 || errno == EPERM)
  1332. return true;
  1333. else if (errno == ESRCH)
  1334. return false;
  1335. else
  1336. fatal("error checking pid %u status", pid);
  1337. }
  1338. #endif
  1339. static enum status_code
  1340. pid_check(pid_t pid)
  1341. {
  1342. if (execname && !pid_is_exec(pid, &exec_stat))
  1343. return STATUS_DEAD;
  1344. if (match_ppid > 0 && !pid_is_child(pid, match_ppid))
  1345. return STATUS_DEAD;
  1346. if (userspec && !pid_is_user(pid, user_id))
  1347. return STATUS_DEAD;
  1348. if (cmdname && !pid_is_cmd(pid, cmdname))
  1349. return STATUS_DEAD;
  1350. if (action != ACTION_STOP && !pid_is_running(pid))
  1351. return STATUS_DEAD;
  1352. pid_list_push(&found, pid);
  1353. return STATUS_OK;
  1354. }
  1355. static enum status_code
  1356. do_pidfile(const char *name)
  1357. {
  1358. FILE *f;
  1359. static pid_t pid = 0;
  1360. if (pid)
  1361. return pid_check(pid);
  1362. f = fopen(name, "r");
  1363. if (f) {
  1364. enum status_code pid_status;
  1365. if (fscanf(f, "%d", &pid) == 1)
  1366. pid_status = pid_check(pid);
  1367. else
  1368. pid_status = STATUS_UNKNOWN;
  1369. fclose(f);
  1370. if (pid_status == STATUS_DEAD)
  1371. return STATUS_DEAD_PIDFILE;
  1372. else
  1373. return pid_status;
  1374. } else if (errno == ENOENT)
  1375. return STATUS_DEAD;
  1376. else
  1377. fatal("unable to open pidfile %s", name);
  1378. }
  1379. #if defined(OSLinux) || defined (OSsunos)
  1380. static enum status_code
  1381. do_procinit(void)
  1382. {
  1383. DIR *procdir;
  1384. struct dirent *entry;
  1385. int foundany;
  1386. pid_t pid;
  1387. enum status_code prog_status = STATUS_DEAD;
  1388. procdir = opendir("/proc");
  1389. if (!procdir)
  1390. fatal("unable to opendir /proc");
  1391. foundany = 0;
  1392. while ((entry = readdir(procdir)) != NULL) {
  1393. enum status_code pid_status;
  1394. if (sscanf(entry->d_name, "%d", &pid) != 1)
  1395. continue;
  1396. foundany++;
  1397. pid_status = pid_check(pid);
  1398. if (pid_status < prog_status)
  1399. prog_status = pid_status;
  1400. }
  1401. closedir(procdir);
  1402. if (!foundany)
  1403. fatal("nothing in /proc - not mounted?");
  1404. return prog_status;
  1405. }
  1406. #elif defined(OSHurd)
  1407. static int
  1408. check_proc_stat(struct proc_stat *ps)
  1409. {
  1410. pid_check(ps->pid);
  1411. return 0;
  1412. }
  1413. static enum status_code
  1414. do_procinit(void)
  1415. {
  1416. if (!procset)
  1417. init_procset();
  1418. proc_stat_list_for_each(procset, check_proc_stat);
  1419. if (found)
  1420. return STATUS_OK;
  1421. else
  1422. return STATUS_DEAD;
  1423. }
  1424. #elif defined(OShpux)
  1425. static enum status_code
  1426. do_procinit(void)
  1427. {
  1428. struct pst_status pst[10];
  1429. int i, count;
  1430. int idx = 0;
  1431. enum status_code prog_status = STATUS_DEAD;
  1432. while ((count = pstat_getproc(pst, sizeof(pst[0]), 10, idx)) > 0) {
  1433. enum status_code pid_status;
  1434. for (i = 0; i < count; i++) {
  1435. pid_status = pid_check(pst[i].pst_pid);
  1436. if (pid_status < prog_status)
  1437. prog_status = pid_status;
  1438. }
  1439. idx = pst[count - 1].pst_idx + 1;
  1440. }
  1441. return prog_status;
  1442. }
  1443. #elif defined(HAVE_KVM_H)
  1444. static enum status_code
  1445. do_procinit(void)
  1446. {
  1447. kvm_t *kd;
  1448. int nentries, i;
  1449. struct kinfo_proc *kp;
  1450. char errbuf[_POSIX2_LINE_MAX];
  1451. enum status_code prog_status = STATUS_DEAD;
  1452. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  1453. if (kd == NULL)
  1454. errx(1, "%s", errbuf);
  1455. kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries);
  1456. if (kp == NULL)
  1457. errx(1, "%s", kvm_geterr(kd));
  1458. for (i = 0; i < nentries; i++) {
  1459. enum status_code pid_status;
  1460. pid_t pid;
  1461. #if defined(OSFreeBSD)
  1462. pid = kp[i].ki_pid;
  1463. #elif defined(OSOpenBSD)
  1464. pid = kp[i].p_pid;
  1465. #elif defined(OSDragonFlyBSD)
  1466. pid = kp[i].kp_pid;
  1467. #else
  1468. pid = kp[i].kp_proc.p_pid;
  1469. #endif
  1470. pid_status = pid_check(pid);
  1471. if (pid_status < prog_status)
  1472. prog_status = pid_status;
  1473. }
  1474. kvm_close(kd);
  1475. return prog_status;
  1476. }
  1477. #endif
  1478. static enum status_code
  1479. do_findprocs(void)
  1480. {
  1481. pid_list_free(&found);
  1482. if (match_pid > 0)
  1483. return pid_check(match_pid);
  1484. else if (pidfile)
  1485. return do_pidfile(pidfile);
  1486. else
  1487. return do_procinit();
  1488. }
  1489. static void
  1490. do_start(int argc, char **argv)
  1491. {
  1492. int devnull_fd = -1;
  1493. gid_t rgid;
  1494. uid_t ruid;
  1495. do_findprocs();
  1496. if (found) {
  1497. if (quietmode <= 0)
  1498. printf("%s already running.\n", execname ? execname : "process");
  1499. exit(exitnodo);
  1500. }
  1501. if (testmode && quietmode <= 0) {
  1502. printf("Would start %s ", startas);
  1503. while (argc-- > 0)
  1504. printf("%s ", *argv++);
  1505. if (changeuser != NULL) {
  1506. printf(" (as user %s[%d]", changeuser, runas_uid);
  1507. if (changegroup != NULL)
  1508. printf(", and group %s[%d])", changegroup, runas_gid);
  1509. else
  1510. printf(")");
  1511. }
  1512. if (changeroot != NULL)
  1513. printf(" in directory %s", changeroot);
  1514. if (nicelevel)
  1515. printf(", and add %i to the priority", nicelevel);
  1516. if (proc_sched)
  1517. printf(", with scheduling policy %s with priority %i",
  1518. proc_sched->policy_name, proc_sched->priority);
  1519. if (io_sched)
  1520. printf(", with IO scheduling class %s with priority %i",
  1521. io_sched->policy_name, io_sched->priority);
  1522. printf(".\n");
  1523. }
  1524. if (testmode)
  1525. exit(0);
  1526. if (quietmode < 0)
  1527. printf("Starting %s...\n", startas);
  1528. *--argv = startas;
  1529. if (background)
  1530. /* Ok, we need to detach this process. */
  1531. daemonize();
  1532. if (background && close_io) {
  1533. devnull_fd = open("/dev/null", O_RDWR);
  1534. if (devnull_fd < 0)
  1535. fatal("unable to open '%s'", "/dev/null");
  1536. }
  1537. if (nicelevel) {
  1538. errno = 0;
  1539. if ((nice(nicelevel) == -1) && (errno != 0))
  1540. fatal("unable to alter nice level by %i", nicelevel);
  1541. }
  1542. if (proc_sched)
  1543. set_proc_schedule(proc_sched);
  1544. if (io_sched)
  1545. set_io_schedule(io_sched);
  1546. if (umask_value >= 0)
  1547. umask(umask_value);
  1548. if (mpidfile && pidfile != NULL)
  1549. /* User wants _us_ to make the pidfile. */
  1550. write_pidfile(pidfile, getpid());
  1551. if (changeroot != NULL) {
  1552. if (chdir(changeroot) < 0)
  1553. fatal("unable to chdir() to %s", changeroot);
  1554. if (chroot(changeroot) < 0)
  1555. fatal("unable to chroot() to %s", changeroot);
  1556. }
  1557. if (chdir(changedir) < 0)
  1558. fatal("unable to chdir() to %s", changedir);
  1559. rgid = getgid();
  1560. ruid = getuid();
  1561. if (changegroup != NULL) {
  1562. if (rgid != (gid_t)runas_gid)
  1563. if (setgid(runas_gid))
  1564. fatal("unable to set gid to %d", runas_gid);
  1565. }
  1566. if (changeuser != NULL) {
  1567. /* We assume that if our real user and group are the same as
  1568. * the ones we should switch to, the supplementary groups
  1569. * will be already in place. */
  1570. if (rgid != (gid_t)runas_gid || ruid != (uid_t)runas_uid)
  1571. if (initgroups(changeuser, runas_gid))
  1572. fatal("unable to set initgroups() with gid %d",
  1573. runas_gid);
  1574. if (ruid != (uid_t)runas_uid)
  1575. if (setuid(runas_uid))
  1576. fatal("unable to set uid to %s", changeuser);
  1577. }
  1578. /* Set a default umask for dumb programs. */
  1579. if (background && umask_value < 0)
  1580. umask(022);
  1581. if (background && close_io) {
  1582. int i;
  1583. dup2(devnull_fd, 0); /* stdin */
  1584. dup2(devnull_fd, 1); /* stdout */
  1585. dup2(devnull_fd, 2); /* stderr */
  1586. /* Now close all extra fds. */
  1587. for (i = get_open_fd_max() - 1; i >= 3; --i)
  1588. close(i);
  1589. }
  1590. execv(startas, argv);
  1591. fatal("unable to start %s", startas);
  1592. }
  1593. static void
  1594. do_stop(int sig_num, int *n_killed, int *n_notkilled)
  1595. {
  1596. struct pid_list *p;
  1597. do_findprocs();
  1598. *n_killed = 0;
  1599. *n_notkilled = 0;
  1600. if (!found)
  1601. return;
  1602. pid_list_free(&killed);
  1603. for (p = found; p; p = p->next) {
  1604. if (testmode) {
  1605. if (quietmode <= 0)
  1606. printf("Would send signal %d to %d.\n",
  1607. sig_num, p->pid);
  1608. (*n_killed)++;
  1609. } else if (kill(p->pid, sig_num) == 0) {
  1610. pid_list_push(&killed, p->pid);
  1611. (*n_killed)++;
  1612. } else {
  1613. if (sig_num)
  1614. warning("failed to kill %d: %s\n",
  1615. p->pid, strerror(errno));
  1616. (*n_notkilled)++;
  1617. }
  1618. }
  1619. }
  1620. static void
  1621. do_stop_summary(int retry_nr)
  1622. {
  1623. struct pid_list *p;
  1624. if (quietmode >= 0 || !killed)
  1625. return;
  1626. printf("Stopped %s (pid", what_stop);
  1627. for (p = killed; p; p = p->next)
  1628. printf(" %d", p->pid);
  1629. putchar(')');
  1630. if (retry_nr > 0)
  1631. printf(", retry #%d", retry_nr);
  1632. printf(".\n");
  1633. }
  1634. static void
  1635. set_what_stop(const char *str)
  1636. {
  1637. strncpy(what_stop, str, sizeof(what_stop));
  1638. what_stop[sizeof(what_stop) - 1] = '\0';
  1639. }
  1640. /*
  1641. * We want to keep polling for the processes, to see if they've exited, or
  1642. * until the timeout expires.
  1643. *
  1644. * This is a somewhat complicated algorithm to try to ensure that we notice
  1645. * reasonably quickly when all the processes have exited, but don't spend
  1646. * too much CPU time polling. In particular, on a fast machine with
  1647. * quick-exiting daemons we don't want to delay system shutdown too much,
  1648. * whereas on a slow one, or where processes are taking some time to exit,
  1649. * we want to increase the polling interval.
  1650. *
  1651. * The algorithm is as follows: we measure the elapsed time it takes to do
  1652. * one poll(), and wait a multiple of this time for the next poll. However,
  1653. * if that would put us past the end of the timeout period we wait only as
  1654. * long as the timeout period, but in any case we always wait at least
  1655. * MIN_POLL_INTERVAL (20ms). The multiple (‘ratio’) starts out as 2, and
  1656. * increases by 1 for each poll to a maximum of 10; so we use up to between
  1657. * 30% and 10% of the machine's resources (assuming a few reasonable things
  1658. * about system performance).
  1659. */
  1660. static bool
  1661. do_stop_timeout(int timeout, int *n_killed, int *n_notkilled)
  1662. {
  1663. struct timeval stopat, before, after, interval, maxinterval;
  1664. int rc, ratio;
  1665. xgettimeofday(&stopat);
  1666. stopat.tv_sec += timeout;
  1667. ratio = 1;
  1668. for (;;) {
  1669. xgettimeofday(&before);
  1670. if (timercmp(&before, &stopat, >))
  1671. return false;
  1672. do_stop(0, n_killed, n_notkilled);
  1673. if (!*n_killed)
  1674. return true;
  1675. xgettimeofday(&after);
  1676. if (!timercmp(&after, &stopat, <))
  1677. return false;
  1678. if (ratio < 10)
  1679. ratio++;
  1680. timersub(&stopat, &after, &maxinterval);
  1681. timersub(&after, &before, &interval);
  1682. tmul(&interval, ratio);
  1683. if (interval.tv_sec < 0 || interval.tv_usec < 0)
  1684. interval.tv_sec = interval.tv_usec = 0;
  1685. if (timercmp(&interval, &maxinterval, >))
  1686. interval = maxinterval;
  1687. if (interval.tv_sec == 0 &&
  1688. interval.tv_usec <= MIN_POLL_INTERVAL)
  1689. interval.tv_usec = MIN_POLL_INTERVAL;
  1690. rc = select(0, NULL, NULL, NULL, &interval);
  1691. if (rc < 0 && errno != EINTR)
  1692. fatal("select() failed for pause");
  1693. }
  1694. }
  1695. static int
  1696. finish_stop_schedule(bool anykilled)
  1697. {
  1698. if (anykilled)
  1699. return 0;
  1700. if (quietmode <= 0)
  1701. printf("No %s found running; none killed.\n", what_stop);
  1702. return exitnodo;
  1703. }
  1704. static int
  1705. run_stop_schedule(void)
  1706. {
  1707. int position, n_killed, n_notkilled, value, retry_nr;
  1708. bool anykilled;
  1709. if (testmode) {
  1710. if (schedule != NULL) {
  1711. if (quietmode <= 0)
  1712. printf("Ignoring --retry in test mode\n");
  1713. schedule = NULL;
  1714. }
  1715. }
  1716. if (cmdname)
  1717. set_what_stop(cmdname);
  1718. else if (execname)
  1719. set_what_stop(execname);
  1720. else if (pidfile)
  1721. sprintf(what_stop, "process in pidfile '%.200s'", pidfile);
  1722. else if (userspec)
  1723. sprintf(what_stop, "process(es) owned by '%.200s'", userspec);
  1724. else
  1725. fatal("internal error, no match option, please report");
  1726. anykilled = false;
  1727. retry_nr = 0;
  1728. if (schedule == NULL) {
  1729. do_stop(signal_nr, &n_killed, &n_notkilled);
  1730. do_stop_summary(0);
  1731. if (n_notkilled > 0 && quietmode <= 0)
  1732. printf("%d pids were not killed\n", n_notkilled);
  1733. if (n_killed)
  1734. anykilled = true;
  1735. return finish_stop_schedule(anykilled);
  1736. }
  1737. for (position = 0; position < schedule_length; position++) {
  1738. reposition:
  1739. value = schedule[position].value;
  1740. n_notkilled = 0;
  1741. switch (schedule[position].type) {
  1742. case sched_goto:
  1743. position = value;
  1744. goto reposition;
  1745. case sched_signal:
  1746. do_stop(value, &n_killed, &n_notkilled);
  1747. do_stop_summary(retry_nr++);
  1748. if (!n_killed)
  1749. return finish_stop_schedule(anykilled);
  1750. else
  1751. anykilled = true;
  1752. continue;
  1753. case sched_timeout:
  1754. if (do_stop_timeout(value, &n_killed, &n_notkilled))
  1755. return finish_stop_schedule(anykilled);
  1756. else
  1757. continue;
  1758. default:
  1759. assert(!"schedule[].type value must be valid");
  1760. }
  1761. }
  1762. if (quietmode <= 0)
  1763. printf("Program %s, %d process(es), refused to die.\n",
  1764. what_stop, n_killed);
  1765. return 2;
  1766. }
  1767. int
  1768. main(int argc, char **argv)
  1769. {
  1770. progname = argv[0];
  1771. parse_options(argc, argv);
  1772. setup_options();
  1773. argc -= optind;
  1774. argv += optind;
  1775. if (action == ACTION_START)
  1776. do_start(argc, argv);
  1777. if (action == ACTION_STOP) {
  1778. int i = run_stop_schedule();
  1779. exit(i);
  1780. }
  1781. if (action == ACTION_STATUS) {
  1782. enum status_code prog_status;
  1783. prog_status = do_findprocs();
  1784. exit(prog_status);
  1785. }
  1786. return 0;
  1787. }