start-stop-daemon.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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)
  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__) || defined(__FreeBSD_kernel__)
  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. return (strcmp(name, process_name) == 0);
  1317. }
  1318. #endif
  1319. #if defined(OSHurd)
  1320. static bool
  1321. pid_is_running(pid_t pid)
  1322. {
  1323. return get_proc_stat(pid, 0) != NULL;
  1324. }
  1325. #else /* !OSHurd */
  1326. static bool
  1327. pid_is_running(pid_t pid)
  1328. {
  1329. if (kill(pid, 0) == 0 || errno == EPERM)
  1330. return true;
  1331. else if (errno == ESRCH)
  1332. return false;
  1333. else
  1334. fatal("error checking pid %u status", pid);
  1335. }
  1336. #endif
  1337. static enum status_code
  1338. pid_check(pid_t pid)
  1339. {
  1340. if (execname && !pid_is_exec(pid, &exec_stat))
  1341. return STATUS_DEAD;
  1342. if (match_ppid > 0 && !pid_is_child(pid, match_ppid))
  1343. return STATUS_DEAD;
  1344. if (userspec && !pid_is_user(pid, user_id))
  1345. return STATUS_DEAD;
  1346. if (cmdname && !pid_is_cmd(pid, cmdname))
  1347. return STATUS_DEAD;
  1348. if (action != ACTION_STOP && !pid_is_running(pid))
  1349. return STATUS_DEAD;
  1350. pid_list_push(&found, pid);
  1351. return STATUS_OK;
  1352. }
  1353. static enum status_code
  1354. do_pidfile(const char *name)
  1355. {
  1356. FILE *f;
  1357. static pid_t pid = 0;
  1358. if (pid)
  1359. return pid_check(pid);
  1360. f = fopen(name, "r");
  1361. if (f) {
  1362. enum status_code pid_status;
  1363. if (fscanf(f, "%d", &pid) == 1)
  1364. pid_status = pid_check(pid);
  1365. else
  1366. pid_status = STATUS_UNKNOWN;
  1367. fclose(f);
  1368. if (pid_status == STATUS_DEAD)
  1369. return STATUS_DEAD_PIDFILE;
  1370. else
  1371. return pid_status;
  1372. } else if (errno == ENOENT)
  1373. return STATUS_DEAD;
  1374. else
  1375. fatal("unable to open pidfile %s", name);
  1376. }
  1377. #if defined(OSLinux) || defined (OSsunos)
  1378. static enum status_code
  1379. do_procinit(void)
  1380. {
  1381. DIR *procdir;
  1382. struct dirent *entry;
  1383. int foundany;
  1384. pid_t pid;
  1385. enum status_code prog_status = STATUS_DEAD;
  1386. procdir = opendir("/proc");
  1387. if (!procdir)
  1388. fatal("unable to opendir /proc");
  1389. foundany = 0;
  1390. while ((entry = readdir(procdir)) != NULL) {
  1391. enum status_code pid_status;
  1392. if (sscanf(entry->d_name, "%d", &pid) != 1)
  1393. continue;
  1394. foundany++;
  1395. pid_status = pid_check(pid);
  1396. if (pid_status < prog_status)
  1397. prog_status = pid_status;
  1398. }
  1399. closedir(procdir);
  1400. if (!foundany)
  1401. fatal("nothing in /proc - not mounted?");
  1402. return prog_status;
  1403. }
  1404. #elif defined(OSHurd)
  1405. static int
  1406. check_proc_stat(struct proc_stat *ps)
  1407. {
  1408. pid_check(ps->pid);
  1409. return 0;
  1410. }
  1411. static enum status_code
  1412. do_procinit(void)
  1413. {
  1414. if (!procset)
  1415. init_procset();
  1416. proc_stat_list_for_each(procset, check_proc_stat);
  1417. if (found)
  1418. return STATUS_OK;
  1419. else
  1420. return STATUS_DEAD;
  1421. }
  1422. #elif defined(OShpux)
  1423. static enum status_code
  1424. do_procinit(void)
  1425. {
  1426. struct pst_status pst[10];
  1427. int i, count;
  1428. int idx = 0;
  1429. enum status_code prog_status = STATUS_DEAD;
  1430. while ((count = pstat_getproc(pst, sizeof(pst[0]), 10, idx)) > 0) {
  1431. enum status_code pid_status;
  1432. for (i = 0; i < count; i++) {
  1433. pid_status = pid_check(pst[i].pst_pid);
  1434. if (pid_status < prog_status)
  1435. prog_status = pid_status;
  1436. }
  1437. idx = pst[count - 1].pst_idx + 1;
  1438. }
  1439. return prog_status;
  1440. }
  1441. #elif defined(HAVE_KVM_H)
  1442. static enum status_code
  1443. do_procinit(void)
  1444. {
  1445. kvm_t *kd;
  1446. int nentries, i;
  1447. struct kinfo_proc *kp;
  1448. char errbuf[_POSIX2_LINE_MAX];
  1449. enum status_code prog_status = STATUS_DEAD;
  1450. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  1451. if (kd == NULL)
  1452. errx(1, "%s", errbuf);
  1453. kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries);
  1454. if (kp == NULL)
  1455. errx(1, "%s", kvm_geterr(kd));
  1456. for (i = 0; i < nentries; i++) {
  1457. enum status_code pid_status;
  1458. pid_t pid;
  1459. #if defined(OSFreeBSD)
  1460. pid = kp[i].ki_pid;
  1461. #elif defined(OSOpenBSD)
  1462. pid = kp[i].p_pid;
  1463. #elif defined(OSDragonFlyBSD)
  1464. pid = kp[i].kp_pid;
  1465. #else
  1466. pid = kp[i].kp_proc.p_pid;
  1467. #endif
  1468. pid_status = pid_check(pid);
  1469. if (pid_status < prog_status)
  1470. prog_status = pid_status;
  1471. }
  1472. kvm_close(kd);
  1473. return prog_status;
  1474. }
  1475. #endif
  1476. static enum status_code
  1477. do_findprocs(void)
  1478. {
  1479. pid_list_free(&found);
  1480. if (match_pid > 0)
  1481. return pid_check(match_pid);
  1482. else if (pidfile)
  1483. return do_pidfile(pidfile);
  1484. else
  1485. return do_procinit();
  1486. }
  1487. static void
  1488. do_start(int argc, char **argv)
  1489. {
  1490. int devnull_fd = -1;
  1491. gid_t rgid;
  1492. uid_t ruid;
  1493. do_findprocs();
  1494. if (found) {
  1495. if (quietmode <= 0)
  1496. printf("%s already running.\n", execname ? execname : "process");
  1497. exit(exitnodo);
  1498. }
  1499. if (testmode && quietmode <= 0) {
  1500. printf("Would start %s ", startas);
  1501. while (argc-- > 0)
  1502. printf("%s ", *argv++);
  1503. if (changeuser != NULL) {
  1504. printf(" (as user %s[%d]", changeuser, runas_uid);
  1505. if (changegroup != NULL)
  1506. printf(", and group %s[%d])", changegroup, runas_gid);
  1507. else
  1508. printf(")");
  1509. }
  1510. if (changeroot != NULL)
  1511. printf(" in directory %s", changeroot);
  1512. if (nicelevel)
  1513. printf(", and add %i to the priority", nicelevel);
  1514. if (proc_sched)
  1515. printf(", with scheduling policy %s with priority %i",
  1516. proc_sched->policy_name, proc_sched->priority);
  1517. if (io_sched)
  1518. printf(", with IO scheduling class %s with priority %i",
  1519. io_sched->policy_name, io_sched->priority);
  1520. printf(".\n");
  1521. }
  1522. if (testmode)
  1523. exit(0);
  1524. if (quietmode < 0)
  1525. printf("Starting %s...\n", startas);
  1526. *--argv = startas;
  1527. if (background)
  1528. /* Ok, we need to detach this process. */
  1529. daemonize();
  1530. if (background && close_io) {
  1531. devnull_fd = open("/dev/null", O_RDWR);
  1532. if (devnull_fd < 0)
  1533. fatal("unable to open '%s'", "/dev/null");
  1534. }
  1535. if (nicelevel) {
  1536. errno = 0;
  1537. if ((nice(nicelevel) == -1) && (errno != 0))
  1538. fatal("unable to alter nice level by %i", nicelevel);
  1539. }
  1540. if (proc_sched)
  1541. set_proc_schedule(proc_sched);
  1542. if (io_sched)
  1543. set_io_schedule(io_sched);
  1544. if (umask_value >= 0)
  1545. umask(umask_value);
  1546. if (mpidfile && pidfile != NULL)
  1547. /* User wants _us_ to make the pidfile. */
  1548. write_pidfile(pidfile, getpid());
  1549. if (changeroot != NULL) {
  1550. if (chdir(changeroot) < 0)
  1551. fatal("unable to chdir() to %s", changeroot);
  1552. if (chroot(changeroot) < 0)
  1553. fatal("unable to chroot() to %s", changeroot);
  1554. }
  1555. if (chdir(changedir) < 0)
  1556. fatal("unable to chdir() to %s", changedir);
  1557. rgid = getgid();
  1558. ruid = getuid();
  1559. if (changegroup != NULL) {
  1560. if (rgid != (gid_t)runas_gid)
  1561. if (setgid(runas_gid))
  1562. fatal("unable to set gid to %d", runas_gid);
  1563. }
  1564. if (changeuser != NULL) {
  1565. /* We assume that if our real user and group are the same as
  1566. * the ones we should switch to, the supplementary groups
  1567. * will be already in place. */
  1568. if (rgid != (gid_t)runas_gid || ruid != (uid_t)runas_uid)
  1569. if (initgroups(changeuser, runas_gid))
  1570. fatal("unable to set initgroups() with gid %d",
  1571. runas_gid);
  1572. if (ruid != (uid_t)runas_uid)
  1573. if (setuid(runas_uid))
  1574. fatal("unable to set uid to %s", changeuser);
  1575. }
  1576. /* Set a default umask for dumb programs. */
  1577. if (background && umask_value < 0)
  1578. umask(022);
  1579. if (background && close_io) {
  1580. int i;
  1581. dup2(devnull_fd, 0); /* stdin */
  1582. dup2(devnull_fd, 1); /* stdout */
  1583. dup2(devnull_fd, 2); /* stderr */
  1584. /* Now close all extra fds. */
  1585. for (i = get_open_fd_max() - 1; i >= 3; --i)
  1586. close(i);
  1587. }
  1588. execv(startas, argv);
  1589. fatal("unable to start %s", startas);
  1590. }
  1591. static void
  1592. do_stop(int sig_num, int *n_killed, int *n_notkilled)
  1593. {
  1594. struct pid_list *p;
  1595. do_findprocs();
  1596. *n_killed = 0;
  1597. *n_notkilled = 0;
  1598. if (!found)
  1599. return;
  1600. pid_list_free(&killed);
  1601. for (p = found; p; p = p->next) {
  1602. if (testmode) {
  1603. if (quietmode <= 0)
  1604. printf("Would send signal %d to %d.\n",
  1605. sig_num, p->pid);
  1606. (*n_killed)++;
  1607. } else if (kill(p->pid, sig_num) == 0) {
  1608. pid_list_push(&killed, p->pid);
  1609. (*n_killed)++;
  1610. } else {
  1611. if (sig_num)
  1612. warning("failed to kill %d: %s\n",
  1613. p->pid, strerror(errno));
  1614. (*n_notkilled)++;
  1615. }
  1616. }
  1617. }
  1618. static void
  1619. do_stop_summary(int retry_nr)
  1620. {
  1621. struct pid_list *p;
  1622. if (quietmode >= 0 || !killed)
  1623. return;
  1624. printf("Stopped %s (pid", what_stop);
  1625. for (p = killed; p; p = p->next)
  1626. printf(" %d", p->pid);
  1627. putchar(')');
  1628. if (retry_nr > 0)
  1629. printf(", retry #%d", retry_nr);
  1630. printf(".\n");
  1631. }
  1632. static void
  1633. set_what_stop(const char *str)
  1634. {
  1635. strncpy(what_stop, str, sizeof(what_stop));
  1636. what_stop[sizeof(what_stop) - 1] = '\0';
  1637. }
  1638. /*
  1639. * We want to keep polling for the processes, to see if they've exited, or
  1640. * until the timeout expires.
  1641. *
  1642. * This is a somewhat complicated algorithm to try to ensure that we notice
  1643. * reasonably quickly when all the processes have exited, but don't spend
  1644. * too much CPU time polling. In particular, on a fast machine with
  1645. * quick-exiting daemons we don't want to delay system shutdown too much,
  1646. * whereas on a slow one, or where processes are taking some time to exit,
  1647. * we want to increase the polling interval.
  1648. *
  1649. * The algorithm is as follows: we measure the elapsed time it takes to do
  1650. * one poll(), and wait a multiple of this time for the next poll. However,
  1651. * if that would put us past the end of the timeout period we wait only as
  1652. * long as the timeout period, but in any case we always wait at least
  1653. * MIN_POLL_INTERVAL (20ms). The multiple (‘ratio’) starts out as 2, and
  1654. * increases by 1 for each poll to a maximum of 10; so we use up to between
  1655. * 30% and 10% of the machine's resources (assuming a few reasonable things
  1656. * about system performance).
  1657. */
  1658. static bool
  1659. do_stop_timeout(int timeout, int *n_killed, int *n_notkilled)
  1660. {
  1661. struct timeval stopat, before, after, interval, maxinterval;
  1662. int rc, ratio;
  1663. xgettimeofday(&stopat);
  1664. stopat.tv_sec += timeout;
  1665. ratio = 1;
  1666. for (;;) {
  1667. xgettimeofday(&before);
  1668. if (timercmp(&before, &stopat, >))
  1669. return false;
  1670. do_stop(0, n_killed, n_notkilled);
  1671. if (!*n_killed)
  1672. return true;
  1673. xgettimeofday(&after);
  1674. if (!timercmp(&after, &stopat, <))
  1675. return false;
  1676. if (ratio < 10)
  1677. ratio++;
  1678. timersub(&stopat, &after, &maxinterval);
  1679. timersub(&after, &before, &interval);
  1680. tmul(&interval, ratio);
  1681. if (interval.tv_sec < 0 || interval.tv_usec < 0)
  1682. interval.tv_sec = interval.tv_usec = 0;
  1683. if (timercmp(&interval, &maxinterval, >))
  1684. interval = maxinterval;
  1685. if (interval.tv_sec == 0 &&
  1686. interval.tv_usec <= MIN_POLL_INTERVAL)
  1687. interval.tv_usec = MIN_POLL_INTERVAL;
  1688. rc = select(0, NULL, NULL, NULL, &interval);
  1689. if (rc < 0 && errno != EINTR)
  1690. fatal("select() failed for pause");
  1691. }
  1692. }
  1693. static int
  1694. finish_stop_schedule(bool anykilled)
  1695. {
  1696. if (anykilled)
  1697. return 0;
  1698. if (quietmode <= 0)
  1699. printf("No %s found running; none killed.\n", what_stop);
  1700. return exitnodo;
  1701. }
  1702. static int
  1703. run_stop_schedule(void)
  1704. {
  1705. int position, n_killed, n_notkilled, value, retry_nr;
  1706. bool anykilled;
  1707. if (testmode) {
  1708. if (schedule != NULL) {
  1709. if (quietmode <= 0)
  1710. printf("Ignoring --retry in test mode\n");
  1711. schedule = NULL;
  1712. }
  1713. }
  1714. if (cmdname)
  1715. set_what_stop(cmdname);
  1716. else if (execname)
  1717. set_what_stop(execname);
  1718. else if (pidfile)
  1719. sprintf(what_stop, "process in pidfile '%.200s'", pidfile);
  1720. else if (userspec)
  1721. sprintf(what_stop, "process(es) owned by '%.200s'", userspec);
  1722. else
  1723. fatal("internal error, no match option, please report");
  1724. anykilled = false;
  1725. retry_nr = 0;
  1726. if (schedule == NULL) {
  1727. do_stop(signal_nr, &n_killed, &n_notkilled);
  1728. do_stop_summary(0);
  1729. if (n_notkilled > 0 && quietmode <= 0)
  1730. printf("%d pids were not killed\n", n_notkilled);
  1731. if (n_killed)
  1732. anykilled = true;
  1733. return finish_stop_schedule(anykilled);
  1734. }
  1735. for (position = 0; position < schedule_length; position++) {
  1736. reposition:
  1737. value = schedule[position].value;
  1738. n_notkilled = 0;
  1739. switch (schedule[position].type) {
  1740. case sched_goto:
  1741. position = value;
  1742. goto reposition;
  1743. case sched_signal:
  1744. do_stop(value, &n_killed, &n_notkilled);
  1745. do_stop_summary(retry_nr++);
  1746. if (!n_killed)
  1747. return finish_stop_schedule(anykilled);
  1748. else
  1749. anykilled = true;
  1750. continue;
  1751. case sched_timeout:
  1752. if (do_stop_timeout(value, &n_killed, &n_notkilled))
  1753. return finish_stop_schedule(anykilled);
  1754. else
  1755. continue;
  1756. default:
  1757. assert(!"schedule[].type value must be valid");
  1758. }
  1759. }
  1760. if (quietmode <= 0)
  1761. printf("Program %s, %d process(es), refused to die.\n",
  1762. what_stop, n_killed);
  1763. return 2;
  1764. }
  1765. int
  1766. main(int argc, char **argv)
  1767. {
  1768. progname = argv[0];
  1769. parse_options(argc, argv);
  1770. setup_options();
  1771. argc -= optind;
  1772. argv += optind;
  1773. if (action == ACTION_START)
  1774. do_start(argc, argv);
  1775. if (action == ACTION_STOP) {
  1776. int i = run_stop_schedule();
  1777. exit(i);
  1778. }
  1779. if (action == ACTION_STATUS) {
  1780. enum status_code prog_status;
  1781. prog_status = do_findprocs();
  1782. exit(prog_status);
  1783. }
  1784. return 0;
  1785. }