start-stop-daemon.c 48 KB

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