start-stop-daemon.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  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 aswell.
  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. #define VERSION "1.1.1.1"
  23. //#include <config.h>
  24. #include <macros.h>
  25. #if defined(linux) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
  26. # define OSLinux
  27. #elif defined(__GNU__)
  28. # define OSHurd
  29. #elif defined(__sun)
  30. # define OSsunos
  31. #elif defined(OPENBSD) || defined(__OpenBSD__)
  32. # define OSOpenBSD
  33. #elif defined(hpux)
  34. # define OShpux
  35. #elif defined(__FreeBSD__)
  36. # define OSFreeBSD
  37. #elif defined(__NetBSD__)
  38. # define OSNetBSD
  39. #elif defined(__APPLE__)
  40. # define OSLinux
  41. #else
  42. # error Unknown architecture - cannot build start-stop-daemon
  43. #endif
  44. #define MIN_POLL_INTERVAL 20000 /* µs */
  45. #ifdef HAVE_SYS_CDEFS_H
  46. #include <sys/cdefs.h>
  47. #endif
  48. #ifdef HAVE_SYS_SYSCALL_H
  49. #include <sys/syscall.h>
  50. #endif
  51. #if defined(OSHurd)
  52. #include <hurd.h>
  53. #include <ps.h>
  54. #endif
  55. #if defined(OSOpenBSD) || defined(OSFreeBSD) || defined(OSNetBSD)
  56. #include <sys/param.h>
  57. #include <sys/proc.h>
  58. #include <err.h>
  59. #endif
  60. #ifdef HAVE_KVM_H
  61. #include <sys/sysctl.h>
  62. #include <sys/user.h>
  63. #include <kvm.h>
  64. #endif
  65. #if defined(OShpux)
  66. #include <sys/param.h>
  67. #include <sys/pstat.h>
  68. #endif
  69. #include <sys/types.h>
  70. #include <sys/time.h>
  71. #include <sys/stat.h>
  72. #include <sys/ioctl.h>
  73. #include <sys/termios.h>
  74. #include <assert.h>
  75. #include <errno.h>
  76. #include <limits.h>
  77. #include <fcntl.h>
  78. #include <dirent.h>
  79. #include <ctype.h>
  80. #include <string.h>
  81. #include <pwd.h>
  82. #include <grp.h>
  83. #include <signal.h>
  84. #include <unistd.h>
  85. #ifdef HAVE_STDDEF_H
  86. #include <stddef.h>
  87. #endif
  88. #include <stdbool.h>
  89. #include <stdarg.h>
  90. #include <stdlib.h>
  91. #include <stdio.h>
  92. #include <getopt.h>
  93. #ifdef HAVE_ERROR_H
  94. #include <error.h>
  95. #endif
  96. #ifdef _POSIX_PRIORITY_SCHEDULING
  97. #include <sched.h>
  98. #else
  99. #define SCHED_OTHER -1
  100. #define SCHED_FIFO -1
  101. #define SCHED_RR -1
  102. #endif
  103. #if defined(OSLinux)
  104. /* This comes from TASK_COMM_LEN defined in linux's include/linux/sched.h. */
  105. #define PROCESS_NAME_SIZE 15
  106. #endif
  107. #if defined(SYS_ioprio_set) && defined(linux)
  108. #define HAVE_IOPRIO_SET
  109. #endif
  110. #if defined(__APPLE__)
  111. #undef _POSIX_PRIORITY_SCHEDULING
  112. #endif
  113. enum {
  114. IOPRIO_WHO_PROCESS = 1,
  115. IOPRIO_WHO_PGRP,
  116. IOPRIO_WHO_USER,
  117. };
  118. enum {
  119. IOPRIO_CLASS_NONE,
  120. IOPRIO_CLASS_RT,
  121. IOPRIO_CLASS_BE,
  122. IOPRIO_CLASS_IDLE,
  123. };
  124. static int testmode = 0;
  125. static int quietmode = 0;
  126. static int exitnodo = 1;
  127. static int start = 0;
  128. static int stop = 0;
  129. static int background = 0;
  130. static int mpidfile = 0;
  131. static int signal_nr = SIGTERM;
  132. static int user_id = -1;
  133. static int runas_uid = -1;
  134. static int runas_gid = -1;
  135. static const char *userspec = NULL;
  136. static char *changeuser = NULL;
  137. static const char *changegroup = NULL;
  138. static char *changeroot = NULL;
  139. static const char *changedir = "/";
  140. static const char *cmdname = NULL;
  141. static char *execname = NULL;
  142. static char *startas = NULL;
  143. static const char *pidfile = NULL;
  144. static char what_stop[1024];
  145. static const char *progname = "";
  146. static int nicelevel = 0;
  147. static int umask_value = -1;
  148. #define IOPRIO_CLASS_SHIFT 13
  149. #define IOPRIO_PRIO_VALUE(class, prio) (((class) << IOPRIO_CLASS_SHIFT) | (prio))
  150. #define IO_SCHED_PRIO_MIN 0
  151. #define IO_SCHED_PRIO_MAX 7
  152. static struct stat exec_stat;
  153. #if defined(OSHurd)
  154. static struct proc_stat_list *procset = NULL;
  155. #endif
  156. struct pid_list {
  157. struct pid_list *next;
  158. pid_t pid;
  159. };
  160. static struct pid_list *found = NULL;
  161. static struct pid_list *killed = NULL;
  162. /* Resource scheduling policy. */
  163. struct res_schedule {
  164. const char *policy_name;
  165. int policy;
  166. int priority;
  167. };
  168. struct schedule_item {
  169. enum {
  170. sched_timeout,
  171. sched_signal,
  172. sched_goto,
  173. sched_forever /* Only seen within parse_schedule and callees */
  174. } type;
  175. int value; /* Seconds, signal no., or index into array. */
  176. };
  177. static struct res_schedule *proc_sched = NULL;
  178. static struct res_schedule *io_sched = NULL;
  179. static int schedule_length;
  180. static struct schedule_item *schedule = NULL;
  181. static void *xmalloc(int size);
  182. static void push(struct pid_list **list, pid_t pid);
  183. static void do_help(void);
  184. static void parse_options(int argc, char * const *argv);
  185. static bool pid_is_user(pid_t pid, uid_t uid);
  186. static bool pid_is_cmd(pid_t pid, const char *name);
  187. static void check(pid_t pid);
  188. static void do_pidfile(const char *name);
  189. static void do_stop(int signal_nr, int quietmode,
  190. int *n_killed, int *n_notkilled, int retry_nr);
  191. #if defined(OSLinux) || defined(OShpux)
  192. static bool pid_is_exec(pid_t pid, const struct stat *esb);
  193. #endif
  194. static void fatal(const char *format, ...)
  195. DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1);
  196. static void warning(const char *format, ...)
  197. DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1);
  198. static void badusage(const char *msg)
  199. DPKG_ATTR_NORET;
  200. static void
  201. warning(const char *format, ...)
  202. {
  203. va_list arglist;
  204. fprintf(stderr, "%s: warning: ", progname);
  205. va_start(arglist, format);
  206. vfprintf(stderr, format, arglist);
  207. va_end(arglist);
  208. }
  209. static void
  210. fatal(const char *format, ...)
  211. {
  212. va_list arglist;
  213. int errno_fatal = errno;
  214. fprintf(stderr, "%s: ", progname);
  215. va_start(arglist, format);
  216. vfprintf(stderr, format, arglist);
  217. va_end(arglist);
  218. if (errno_fatal)
  219. fprintf(stderr, " (%s)\n", strerror(errno_fatal));
  220. else
  221. fprintf(stderr, "\n");
  222. exit(2);
  223. }
  224. static void *
  225. xmalloc(int size)
  226. {
  227. void *ptr;
  228. ptr = malloc(size);
  229. if (ptr)
  230. return ptr;
  231. fatal("malloc(%d) failed", size);
  232. }
  233. static char *
  234. xstrdup(const char *str)
  235. {
  236. char *new_str;
  237. new_str = strdup(str);
  238. if (new_str)
  239. return new_str;
  240. fatal("strdup(%s) failed", str);
  241. }
  242. static void
  243. xgettimeofday(struct timeval *tv)
  244. {
  245. if (gettimeofday(tv, NULL) != 0)
  246. fatal("gettimeofday failed: %s", strerror(errno));
  247. }
  248. static void
  249. tmul(struct timeval *a, int b)
  250. {
  251. a->tv_sec *= b;
  252. a->tv_usec *= b;
  253. a->tv_sec = a->tv_sec + a->tv_usec / 1000000;
  254. a->tv_usec %= 1000000;
  255. }
  256. static long
  257. get_open_fd_max(void)
  258. {
  259. #ifdef HAVE_GETDTABLESIZE
  260. return getdtablesize();
  261. #else
  262. return sysconf(_SC_OPEN_MAX);
  263. #endif
  264. }
  265. static void
  266. daemonize(void)
  267. {
  268. pid_t pid;
  269. if (quietmode < 0)
  270. printf("Detaching to start %s...", startas);
  271. pid = fork();
  272. if (pid < 0)
  273. fatal("Unable to do first fork.\n");
  274. else if (pid) /* Parent. */
  275. _exit(0);
  276. /* Create a new session. */
  277. #ifdef HAVE_SETSID
  278. setsid();
  279. #else
  280. setpgid(0, 0);
  281. #endif
  282. pid = fork();
  283. if (pid < 0)
  284. fatal("Unable to do second fork.\n");
  285. else if (pid) /* Parent. */
  286. _exit(0);
  287. if (quietmode < 0)
  288. printf("done.\n");
  289. }
  290. static void
  291. push(struct pid_list **list, pid_t pid)
  292. {
  293. struct pid_list *p;
  294. p = xmalloc(sizeof(*p));
  295. p->next = *list;
  296. p->pid = pid;
  297. *list = p;
  298. }
  299. static void
  300. clear(struct pid_list **list)
  301. {
  302. struct pid_list *here, *next;
  303. for (here = *list; here != NULL; here = next) {
  304. next = here->next;
  305. free(here);
  306. }
  307. *list = NULL;
  308. }
  309. static void
  310. do_help(void)
  311. {
  312. printf(
  313. "Usage: start-stop-daemon [<option> ...] <command>\n"
  314. "\n"
  315. "Commands:\n"
  316. " -S|--start -- <argument> ... start a program and pass <arguments> to it\n"
  317. " -K|--stop stop a program\n"
  318. " -H|--help print help information\n"
  319. " -V|--version print version\n"
  320. "\n"
  321. "Matching options (at least one is required):\n"
  322. " -p|--pidfile <pid-file> pid file to check\n"
  323. " -x|--exec <executable> program to start/check if it is running\n"
  324. " -n|--name <process-name> process name to check\n"
  325. " -u|--user <username|uid> process owner to check\n"
  326. "\n"
  327. "Options:\n"
  328. " -g|--group <group|gid> run process as this group\n"
  329. " -c|--chuid <name|uid[:group|gid]>\n"
  330. " change to this user/group before starting\n"
  331. " process\n"
  332. " -s|--signal <signal> signal to send (default TERM)\n"
  333. " -a|--startas <pathname> program to start (default is <executable>)\n"
  334. " -r|--chroot <directory> chroot to <directory> before starting\n"
  335. " -d|--chdir <directory> change to <directory> (default is /)\n"
  336. " -N|--nicelevel <incr> add incr to the process's nice level\n"
  337. " -P|--procsched <policy[:prio]>\n"
  338. " use <policy> with <prio> for the kernel\n"
  339. " process scheduler (default prio is 0)\n"
  340. " -I|--iosched <class[:prio]> use <class> with <prio> to set the IO\n"
  341. " scheduler (default prio is 4)\n"
  342. " -k|--umask <mask> change the umask to <mask> before starting\n"
  343. " -b|--background force the process to detach\n"
  344. " -m|--make-pidfile create the pidfile before starting\n"
  345. " -R|--retry <schedule> check whether processes die, and retry\n"
  346. " -t|--test test mode, don't do anything\n"
  347. " -o|--oknodo exit status 0 (not 1) if nothing done\n"
  348. " -q|--quiet be more quiet\n"
  349. " -v|--verbose be more verbose\n"
  350. "\n"
  351. "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
  352. " -<signal-num>|[-]<signal-name> send that signal\n"
  353. " <timeout> wait that many seconds\n"
  354. " forever repeat remainder forever\n"
  355. "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
  356. "\n"
  357. "The process scheduler <policy> can be one of:\n"
  358. " other, fifo or rr\n"
  359. "\n"
  360. "The IO scheduler <class> can be one of:\n"
  361. " real-time, best-effort or idle\n"
  362. "\n"
  363. "Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo)\n"
  364. " 3 = trouble 2 = with --retry, processes wouldn't die\n");
  365. }
  366. static void
  367. do_version(void)
  368. {
  369. printf("start-stop-daemon %s for Debian\n\n", VERSION);
  370. printf("Written by Marek Michalkiewicz, public domain.\n");
  371. }
  372. static void
  373. badusage(const char *msg)
  374. {
  375. if (msg)
  376. fprintf(stderr, "%s: %s\n", progname, msg);
  377. fprintf(stderr, "Try '%s --help' for more information.\n", progname);
  378. exit(3);
  379. }
  380. struct sigpair {
  381. const char *name;
  382. int signal;
  383. };
  384. static const struct sigpair siglist[] = {
  385. { "ABRT", SIGABRT },
  386. { "ALRM", SIGALRM },
  387. { "FPE", SIGFPE },
  388. { "HUP", SIGHUP },
  389. { "ILL", SIGILL },
  390. { "INT", SIGINT },
  391. { "KILL", SIGKILL },
  392. { "PIPE", SIGPIPE },
  393. { "QUIT", SIGQUIT },
  394. { "SEGV", SIGSEGV },
  395. { "TERM", SIGTERM },
  396. { "USR1", SIGUSR1 },
  397. { "USR2", SIGUSR2 },
  398. { "CHLD", SIGCHLD },
  399. { "CONT", SIGCONT },
  400. { "STOP", SIGSTOP },
  401. { "TSTP", SIGTSTP },
  402. { "TTIN", SIGTTIN },
  403. { "TTOU", SIGTTOU }
  404. };
  405. static int
  406. parse_integer(const char *string, int *value_r)
  407. {
  408. unsigned long ul;
  409. char *ep;
  410. if (!string[0])
  411. return -1;
  412. ul = strtoul(string, &ep, 10);
  413. if (string == ep || ul > INT_MAX || *ep != '\0')
  414. return -1;
  415. *value_r = ul;
  416. return 0;
  417. }
  418. static int
  419. parse_signal(const char *signal_str, int *signal_nr)
  420. {
  421. unsigned int i;
  422. if (parse_integer(signal_str, signal_nr) == 0)
  423. return 0;
  424. for (i = 0; i < sizeof_array(siglist); i++) {
  425. if (strcmp(signal_str, siglist[i].name) == 0) {
  426. *signal_nr = siglist[i].signal;
  427. return 0;
  428. }
  429. }
  430. return -1;
  431. }
  432. static int
  433. parse_umask(const char *string, int *value_r)
  434. {
  435. if (!string[0])
  436. return -1;
  437. errno = 0;
  438. *value_r = strtoul(string, NULL, 0);
  439. if (errno)
  440. return -1;
  441. else
  442. return 0;
  443. }
  444. static void
  445. validate_proc_schedule(void)
  446. {
  447. #ifdef _POSIX_PRIORITY_SCHEDULING
  448. int prio_min, prio_max;
  449. prio_min = sched_get_priority_min(proc_sched->policy);
  450. prio_max = sched_get_priority_max(proc_sched->policy);
  451. if (proc_sched->priority < prio_min)
  452. badusage("process scheduler priority less than min");
  453. if (proc_sched->priority > prio_max)
  454. badusage("process scheduler priority greater than max");
  455. #endif
  456. }
  457. static void
  458. parse_proc_schedule(const char *string)
  459. {
  460. char *policy_str, *prio_str;
  461. int prio = 0;
  462. policy_str = xstrdup(string);
  463. policy_str = strtok(policy_str, ":");
  464. prio_str = strtok(NULL, ":");
  465. if (prio_str && parse_integer(prio_str, &prio) != 0)
  466. fatal("invalid process scheduler priority");
  467. proc_sched = xmalloc(sizeof(*proc_sched));
  468. proc_sched->policy_name = policy_str;
  469. if (strcmp(policy_str, "other") == 0) {
  470. proc_sched->policy = SCHED_OTHER;
  471. proc_sched->priority = 0;
  472. } else if (strcmp(policy_str, "fifo") == 0) {
  473. proc_sched->policy = SCHED_FIFO;
  474. proc_sched->priority = prio;
  475. } else if (strcmp(policy_str, "rr") == 0) {
  476. proc_sched->policy = SCHED_RR;
  477. proc_sched->priority = prio;
  478. } else
  479. badusage("invalid process scheduler policy");
  480. validate_proc_schedule();
  481. }
  482. static void
  483. parse_io_schedule(const char *string)
  484. {
  485. char *class_str, *prio_str;
  486. int prio = 4;
  487. class_str = xstrdup(string);
  488. class_str = strtok(class_str, ":");
  489. prio_str = strtok(NULL, ":");
  490. if (prio_str && parse_integer(prio_str, &prio) != 0)
  491. fatal("invalid IO scheduler priority");
  492. io_sched = xmalloc(sizeof(*io_sched));
  493. io_sched->policy_name = class_str;
  494. if (strcmp(class_str, "real-time") == 0) {
  495. io_sched->policy = IOPRIO_CLASS_RT;
  496. io_sched->priority = prio;
  497. } else if (strcmp(class_str, "best-effort") == 0) {
  498. io_sched->policy = IOPRIO_CLASS_BE;
  499. io_sched->priority = prio;
  500. } else if (strcmp(class_str, "idle") == 0) {
  501. io_sched->policy = IOPRIO_CLASS_IDLE;
  502. io_sched->priority = 7;
  503. } else
  504. badusage("invalid IO scheduler policy");
  505. if (io_sched->priority < IO_SCHED_PRIO_MIN)
  506. badusage("IO scheduler priority less than min");
  507. if (io_sched->priority > IO_SCHED_PRIO_MAX)
  508. badusage("IO scheduler priority greater than max");
  509. }
  510. static void
  511. set_proc_schedule(struct res_schedule *sched)
  512. {
  513. #ifdef _POSIX_PRIORITY_SCHEDULING
  514. struct sched_param param;
  515. param.sched_priority = sched->priority;
  516. if (sched_setscheduler(getpid(), sched->policy, &param) == -1)
  517. fatal("Unable to set process scheduler");
  518. #endif
  519. }
  520. #ifdef HAVE_IOPRIO_SET
  521. static inline int
  522. ioprio_set(int which, int who, int ioprio)
  523. {
  524. return syscall(SYS_ioprio_set, which, who, ioprio);
  525. }
  526. #endif
  527. static void
  528. set_io_schedule(struct res_schedule *sched)
  529. {
  530. #ifdef HAVE_IOPRIO_SET
  531. int io_sched_mask;
  532. io_sched_mask = IOPRIO_PRIO_VALUE(sched->policy, sched->priority);
  533. if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), io_sched_mask) == -1)
  534. warning("Unable to alter IO priority to mask %i (%s)\n",
  535. io_sched_mask, strerror(errno));
  536. #endif
  537. }
  538. static void
  539. parse_schedule_item(const char *string, struct schedule_item *item)
  540. {
  541. const char *after_hyph;
  542. if (!strcmp(string, "forever")) {
  543. item->type = sched_forever;
  544. } else if (isdigit(string[0])) {
  545. item->type = sched_timeout;
  546. if (parse_integer(string, &item->value) != 0)
  547. badusage("invalid timeout value in schedule");
  548. } else if ((after_hyph = string + (string[0] == '-')) &&
  549. parse_signal(after_hyph, &item->value) == 0) {
  550. item->type = sched_signal;
  551. } else {
  552. badusage("invalid schedule item (must be [-]<signal-name>, "
  553. "-<signal-number>, <timeout> or 'forever'");
  554. }
  555. }
  556. static void
  557. parse_schedule(const char *schedule_str)
  558. {
  559. char item_buf[20];
  560. const char *slash;
  561. int count, repeatat;
  562. size_t str_len;
  563. count = 0;
  564. for (slash = schedule_str; *slash; slash++)
  565. if (*slash == '/')
  566. count++;
  567. schedule_length = (count == 0) ? 4 : count + 1;
  568. schedule = xmalloc(sizeof(*schedule) * schedule_length);
  569. if (count == 0) {
  570. schedule[0].type = sched_signal;
  571. schedule[0].value = signal_nr;
  572. parse_schedule_item(schedule_str, &schedule[1]);
  573. if (schedule[1].type != sched_timeout) {
  574. badusage ("--retry takes timeout, or schedule list"
  575. " of at least two items");
  576. }
  577. schedule[2].type = sched_signal;
  578. schedule[2].value = SIGKILL;
  579. schedule[3] = schedule[1];
  580. } else {
  581. count = 0;
  582. repeatat = -1;
  583. while (schedule_str != NULL) {
  584. slash = strchr(schedule_str, '/');
  585. str_len = slash ? (size_t)(slash - schedule_str) : strlen(schedule_str);
  586. if (str_len >= sizeof(item_buf))
  587. badusage("invalid schedule item: far too long"
  588. " (you must delimit items with slashes)");
  589. memcpy(item_buf, schedule_str, str_len);
  590. item_buf[str_len] = '\0';
  591. schedule_str = slash ? slash + 1 : NULL;
  592. parse_schedule_item(item_buf, &schedule[count]);
  593. if (schedule[count].type == sched_forever) {
  594. if (repeatat >= 0)
  595. badusage("invalid schedule: 'forever'"
  596. " appears more than once");
  597. repeatat = count;
  598. continue;
  599. }
  600. count++;
  601. }
  602. if (repeatat >= 0) {
  603. schedule[count].type = sched_goto;
  604. schedule[count].value = repeatat;
  605. count++;
  606. }
  607. assert(count == schedule_length);
  608. }
  609. }
  610. static void
  611. parse_options(int argc, char * const *argv)
  612. {
  613. static struct option longopts[] = {
  614. { "help", 0, NULL, 'H'},
  615. { "stop", 0, NULL, 'K'},
  616. { "start", 0, NULL, 'S'},
  617. { "version", 0, NULL, 'V'},
  618. { "startas", 1, NULL, 'a'},
  619. { "name", 1, NULL, 'n'},
  620. { "oknodo", 0, NULL, 'o'},
  621. { "pidfile", 1, NULL, 'p'},
  622. { "quiet", 0, NULL, 'q'},
  623. { "signal", 1, NULL, 's'},
  624. { "test", 0, NULL, 't'},
  625. { "user", 1, NULL, 'u'},
  626. { "group", 1, NULL, 'g'},
  627. { "chroot", 1, NULL, 'r'},
  628. { "verbose", 0, NULL, 'v'},
  629. { "exec", 1, NULL, 'x'},
  630. { "chuid", 1, NULL, 'c'},
  631. { "nicelevel", 1, NULL, 'N'},
  632. { "procsched", 1, NULL, 'P'},
  633. { "iosched", 1, NULL, 'I'},
  634. { "umask", 1, NULL, 'k'},
  635. { "background", 0, NULL, 'b'},
  636. { "make-pidfile", 0, NULL, 'm'},
  637. { "retry", 1, NULL, 'R'},
  638. { "chdir", 1, NULL, 'd'},
  639. { NULL, 0, NULL, 0 }
  640. };
  641. const char *umask_str = NULL;
  642. const char *signal_str = NULL;
  643. const char *schedule_str = NULL;
  644. const char *proc_schedule_str = NULL;
  645. const char *io_schedule_str = NULL;
  646. int c;
  647. for (;;) {
  648. c = getopt_long(argc, argv,
  649. "HKSVa:n:op:qr:s:tu:vx:c:N:P:I:k:bmR:g:d:",
  650. longopts, NULL);
  651. if (c == -1)
  652. break;
  653. switch (c) {
  654. case 'H': /* --help */
  655. do_help();
  656. exit(0);
  657. case 'K': /* --stop */
  658. stop = 1;
  659. break;
  660. case 'S': /* --start */
  661. start = 1;
  662. break;
  663. case 'V': /* --version */
  664. do_version();
  665. exit(0);
  666. case 'a': /* --startas <pathname> */
  667. startas = optarg;
  668. break;
  669. case 'n': /* --name <process-name> */
  670. cmdname = optarg;
  671. break;
  672. case 'o': /* --oknodo */
  673. exitnodo = 0;
  674. break;
  675. case 'p': /* --pidfile <pid-file> */
  676. pidfile = optarg;
  677. break;
  678. case 'q': /* --quiet */
  679. quietmode = 1;
  680. break;
  681. case 's': /* --signal <signal> */
  682. signal_str = optarg;
  683. break;
  684. case 't': /* --test */
  685. testmode = 1;
  686. break;
  687. case 'u': /* --user <username>|<uid> */
  688. userspec = optarg;
  689. break;
  690. case 'v': /* --verbose */
  691. quietmode = -1;
  692. break;
  693. case 'x': /* --exec <executable> */
  694. execname = optarg;
  695. break;
  696. case 'c': /* --chuid <username>|<uid> */
  697. /* we copy the string just in case we need the
  698. * argument later. */
  699. changeuser = xstrdup(optarg);
  700. changeuser = strtok(changeuser, ":");
  701. changegroup = strtok(NULL, ":");
  702. break;
  703. case 'g': /* --group <group>|<gid> */
  704. changegroup = optarg;
  705. break;
  706. case 'r': /* --chroot /new/root */
  707. changeroot = optarg;
  708. break;
  709. case 'N': /* --nice */
  710. nicelevel = atoi(optarg);
  711. break;
  712. case 'P': /* --procsched */
  713. proc_schedule_str = optarg;
  714. break;
  715. case 'I': /* --iosched */
  716. io_schedule_str = optarg;
  717. break;
  718. case 'k': /* --umask <mask> */
  719. umask_str = optarg;
  720. break;
  721. case 'b': /* --background */
  722. background = 1;
  723. break;
  724. case 'm': /* --make-pidfile */
  725. mpidfile = 1;
  726. break;
  727. case 'R': /* --retry <schedule>|<timeout> */
  728. schedule_str = optarg;
  729. break;
  730. case 'd': /* --chdir /new/dir */
  731. changedir = optarg;
  732. break;
  733. default:
  734. /* Message printed by getopt. */
  735. badusage(NULL);
  736. }
  737. }
  738. if (signal_str != NULL) {
  739. if (parse_signal(signal_str, &signal_nr) != 0)
  740. badusage("signal value must be numeric or name"
  741. " of signal (KILL, INT, ...)");
  742. }
  743. if (schedule_str != NULL) {
  744. parse_schedule(schedule_str);
  745. }
  746. if (proc_schedule_str != NULL)
  747. parse_proc_schedule(proc_schedule_str);
  748. if (io_schedule_str != NULL)
  749. parse_io_schedule(io_schedule_str);
  750. if (umask_str != NULL) {
  751. if (parse_umask(umask_str, &umask_value) != 0)
  752. badusage("umask value must be a positive number");
  753. }
  754. if (start == stop)
  755. badusage("need one of --start or --stop");
  756. if (!execname && !pidfile && !userspec && !cmdname)
  757. badusage("need at least one of --exec, --pidfile, --user or --name");
  758. #ifdef PROCESS_NAME_SIZE
  759. if (cmdname && strlen(cmdname) > PROCESS_NAME_SIZE)
  760. warning("this system is not able to track process names\n"
  761. "longer than %d characters, please use --exec "
  762. "instead of --name.\n", PROCESS_NAME_SIZE);
  763. #endif
  764. if (!startas)
  765. startas = execname;
  766. if (start && !startas)
  767. badusage("--start needs --exec or --startas");
  768. if (mpidfile && pidfile == NULL)
  769. badusage("--make-pidfile requires --pidfile");
  770. if (background && !start)
  771. badusage("--background is only relevant with --start");
  772. }
  773. #if defined(OSHurd)
  774. static void
  775. init_procset(void)
  776. {
  777. struct ps_context *context;
  778. error_t err;
  779. err = ps_context_create(getproc(), &context);
  780. if (err)
  781. error(1, err, "ps_context_create");
  782. err = proc_stat_list_create(context, &procset);
  783. if (err)
  784. error(1, err, "proc_stat_list_create");
  785. err = proc_stat_list_add_all(procset, 0, 0);
  786. if (err)
  787. error(1, err, "proc_stat_list_add_all");
  788. }
  789. static struct proc_stat *
  790. get_proc_stat (pid_t pid, ps_flags_t flags)
  791. {
  792. struct proc_stat *ps;
  793. ps_flags_t wanted_flags = PSTAT_PID | flags;
  794. if (!procset)
  795. init_procset();
  796. ps = proc_stat_list_pid_proc_stat(procset, pid);
  797. if (!ps)
  798. return NULL;
  799. if (proc_stat_set_flags(ps, wanted_flags))
  800. return NULL;
  801. if ((proc_stat_flags(ps) & wanted_flags) != wanted_flags)
  802. return NULL;
  803. return ps;
  804. }
  805. #endif
  806. #if defined(OSLinux)
  807. static bool
  808. pid_is_exec(pid_t pid, const struct stat *esb)
  809. {
  810. char lname[32];
  811. char lcontents[_POSIX_PATH_MAX];
  812. const char deleted[] = " (deleted)";
  813. int nread;
  814. struct stat sb;
  815. sprintf(lname, "/proc/%d/exe", pid);
  816. nread = readlink(lname, lcontents, sizeof(lcontents)-1);
  817. if (nread == -1)
  818. return false;
  819. lcontents[nread] = '\0';
  820. if (strcmp(lcontents + nread - strlen(deleted), deleted) == 0)
  821. lcontents[nread - strlen(deleted)] = '\0';
  822. if (stat(lcontents, &sb) != 0)
  823. return false;
  824. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  825. }
  826. #elif defined(OShpux)
  827. static bool
  828. pid_is_exec(pid_t pid, const struct stat *esb)
  829. {
  830. struct pst_status pst;
  831. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  832. return false;
  833. return ((dev_t)pst.pst_text.psf_fsid.psfs_id == esb->st_dev &&
  834. (ino_t)pst.pst_text.psf_fileid == esb->st_ino);
  835. }
  836. #elif defined(HAVE_KVM_H)
  837. static bool
  838. pid_is_exec(pid_t pid, const char *name)
  839. {
  840. kvm_t *kd;
  841. int nentries;
  842. struct kinfo_proc *kp;
  843. char errbuf[_POSIX2_LINE_MAX], *pidexec;
  844. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  845. if (kd == NULL)
  846. errx(1, "%s", errbuf);
  847. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  848. if (kp == NULL)
  849. errx(1, "%s", kvm_geterr(kd));
  850. pidexec = (&kp->kp_proc)->p_comm;
  851. if (strlen(name) != strlen(pidexec))
  852. return false;
  853. return (strcmp(name, pidexec) == 0) ? 1 : 0;
  854. }
  855. #endif
  856. #if defined(OSLinux)
  857. static bool
  858. pid_is_user(pid_t pid, uid_t uid)
  859. {
  860. struct stat sb;
  861. char buf[32];
  862. sprintf(buf, "/proc/%d", pid);
  863. if (stat(buf, &sb) != 0)
  864. return false;
  865. return (sb.st_uid == uid);
  866. }
  867. #elif defined(OSHurd)
  868. static bool
  869. pid_is_user(pid_t pid, uid_t uid)
  870. {
  871. struct proc_stat *ps;
  872. ps = get_proc_stat(pid, PSTAT_OWNER_UID);
  873. return ps && proc_stat_owner_uid(ps) == uid;
  874. }
  875. #elif defined(OShpux)
  876. static bool
  877. pid_is_user(pid_t pid, uid_t uid)
  878. {
  879. struct pst_status pst;
  880. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  881. return false;
  882. return ((uid_t)pst.pst_uid == uid);
  883. }
  884. #elif defined(HAVE_KVM_H)
  885. static bool
  886. pid_is_user(pid_t pid, uid_t uid)
  887. {
  888. kvm_t *kd;
  889. int nentries; /* Value not used. */
  890. uid_t proc_uid;
  891. struct kinfo_proc *kp;
  892. char errbuf[_POSIX2_LINE_MAX];
  893. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  894. if (kd == NULL)
  895. errx(1, "%s", errbuf);
  896. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  897. if (kp == NULL)
  898. errx(1, "%s", kvm_geterr(kd));
  899. if (kp->kp_proc.p_cred)
  900. kvm_read(kd, (u_long)&(kp->kp_proc.p_cred->p_ruid),
  901. &proc_uid, sizeof(uid_t));
  902. else
  903. return false;
  904. return (proc_uid == (uid_t)uid);
  905. }
  906. #endif
  907. #if defined(OSLinux)
  908. static bool
  909. pid_is_cmd(pid_t pid, const char *name)
  910. {
  911. char buf[32];
  912. FILE *f;
  913. int c;
  914. sprintf(buf, "/proc/%d/stat", pid);
  915. f = fopen(buf, "r");
  916. if (!f)
  917. return false;
  918. while ((c = getc(f)) != EOF && c != '(')
  919. ;
  920. if (c != '(') {
  921. fclose(f);
  922. return false;
  923. }
  924. /* This hopefully handles command names containing ')'. */
  925. while ((c = getc(f)) != EOF && c == *name)
  926. name++;
  927. fclose(f);
  928. return (c == ')' && *name == '\0');
  929. }
  930. #elif defined(OSHurd)
  931. static bool
  932. pid_is_cmd(pid_t pid, const char *name)
  933. {
  934. struct proc_stat *ps;
  935. ps = get_proc_stat(pid, PSTAT_ARGS);
  936. return ps && !strcmp(proc_stat_args(ps), name);
  937. }
  938. #elif defined(OShpux)
  939. static bool
  940. pid_is_cmd(pid_t pid, const char *name)
  941. {
  942. struct pst_status pst;
  943. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  944. return false;
  945. return (strcmp(pst.pst_ucomm, name) == 0);
  946. }
  947. #elif defined(HAVE_KVM_H)
  948. static bool
  949. pid_is_cmd(pid_t pid, const char *name)
  950. {
  951. kvm_t *kd;
  952. int nentries, argv_len = 0;
  953. struct kinfo_proc *kp;
  954. char errbuf[_POSIX2_LINE_MAX], buf[_POSIX2_LINE_MAX];
  955. char **pid_argv_p;
  956. char *start_argv_0_p, *end_argv_0_p;
  957. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  958. if (kd == NULL)
  959. errx(1, "%s", errbuf);
  960. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  961. if (kp == NULL)
  962. errx(1, "%s", kvm_geterr(kd));
  963. pid_argv_p = kvm_getargv(kd, kp, argv_len);
  964. if (pid_argv_p == NULL)
  965. errx(1, "%s", kvm_geterr(kd));
  966. start_argv_0_p = *pid_argv_p;
  967. /* Find and compare string. */
  968. /* Find end of argv[0] then copy and cut of str there. */
  969. end_argv_0_p = strchr(*pid_argv_p, ' ');
  970. if (end_argv_0_p == NULL)
  971. /* There seems to be no space, so we have the command
  972. * allready in its desired form. */
  973. start_argv_0_p = *pid_argv_p;
  974. else {
  975. /* Tests indicate that this never happens, since
  976. * kvm_getargv itself cuts of tailing stuff. This is
  977. * not what the manpage says, however. */
  978. strncpy(buf, *pid_argv_p, (end_argv_0_p - start_argv_0_p));
  979. buf[(end_argv_0_p - start_argv_0_p) + 1] = '\0';
  980. start_argv_0_p = buf;
  981. }
  982. if (strlen(name) != strlen(start_argv_0_p))
  983. return false;
  984. return (strcmp(name, start_argv_0_p) == 0) ? 1 : 0;
  985. }
  986. #endif
  987. #if defined(OSHurd)
  988. static bool
  989. pid_is_running(pid_t pid)
  990. {
  991. return get_proc_stat(pid, 0) != NULL;
  992. }
  993. #else /* !OSHurd */
  994. static bool
  995. pid_is_running(pid_t pid)
  996. {
  997. if (kill(pid, 0) == 0 || errno == EPERM)
  998. return true;
  999. else if (errno == ESRCH)
  1000. return false;
  1001. else
  1002. fatal("Error checking pid %u status: %s", pid, strerror(errno));
  1003. }
  1004. #endif
  1005. static void
  1006. check(pid_t pid)
  1007. {
  1008. #if defined(OSLinux) || defined(OShpux)
  1009. if (execname && !pid_is_exec(pid, &exec_stat))
  1010. return;
  1011. #elif defined(HAVE_KVM_H)
  1012. if (execname && !pid_is_exec(pid, execname))
  1013. return;
  1014. #elif defined(OSHurd) || defined(OSFreeBSD) || defined(OSNetBSD)
  1015. /* Let's try this to see if it works. */
  1016. if (execname && !pid_is_cmd(pid, execname))
  1017. return;
  1018. #endif
  1019. if (userspec && !pid_is_user(pid, user_id))
  1020. return;
  1021. if (cmdname && !pid_is_cmd(pid, cmdname))
  1022. return;
  1023. if (start && !pid_is_running(pid))
  1024. return;
  1025. push(&found, pid);
  1026. }
  1027. static void
  1028. do_pidfile(const char *name)
  1029. {
  1030. FILE *f;
  1031. static pid_t pid = 0;
  1032. if (pid) {
  1033. check(pid);
  1034. return;
  1035. }
  1036. f = fopen(name, "r");
  1037. if (f) {
  1038. if (fscanf(f, "%d", &pid) == 1)
  1039. check(pid);
  1040. fclose(f);
  1041. } else if (errno != ENOENT)
  1042. fatal("open pidfile %s: %s", name, strerror(errno));
  1043. }
  1044. #if defined(OSLinux) || defined (OSsunos)
  1045. static void
  1046. do_procinit(void)
  1047. {
  1048. DIR *procdir;
  1049. struct dirent *entry;
  1050. int foundany;
  1051. pid_t pid;
  1052. procdir = opendir("/proc");
  1053. if (!procdir)
  1054. fatal("opendir /proc: %s", strerror(errno));
  1055. foundany = 0;
  1056. while ((entry = readdir(procdir)) != NULL) {
  1057. if (sscanf(entry->d_name, "%d", &pid) != 1)
  1058. continue;
  1059. foundany++;
  1060. check(pid);
  1061. }
  1062. closedir(procdir);
  1063. if (!foundany)
  1064. fatal("nothing in /proc - not mounted?");
  1065. }
  1066. #elif defined(OSHurd)
  1067. static int
  1068. check_proc_stat(struct proc_stat *ps)
  1069. {
  1070. check(ps->pid);
  1071. return 0;
  1072. }
  1073. static void
  1074. do_procinit(void)
  1075. {
  1076. if (!procset)
  1077. init_procset();
  1078. proc_stat_list_for_each(procset, check_proc_stat);
  1079. }
  1080. #elif defined(OShpux)
  1081. static void
  1082. do_procinit(void)
  1083. {
  1084. struct pst_status pst[10];
  1085. int i, count;
  1086. int idx = 0;
  1087. while ((count = pstat_getproc(pst, sizeof(pst[0]), 10, idx)) > 0) {
  1088. for (i = 0; i < count; i++)
  1089. check(pst[i].pst_pid);
  1090. idx = pst[count - 1].pst_idx + 1;
  1091. }
  1092. }
  1093. #elif defined(HAVE_KVM_H)
  1094. static void
  1095. do_procinit(void)
  1096. {
  1097. /* Nothing to do. */
  1098. }
  1099. #endif
  1100. static void
  1101. do_findprocs(void)
  1102. {
  1103. clear(&found);
  1104. if (pidfile)
  1105. do_pidfile(pidfile);
  1106. else
  1107. do_procinit();
  1108. }
  1109. static void
  1110. do_stop(int signal_nr, int quietmode, int *n_killed, int *n_notkilled,
  1111. int retry_nr)
  1112. {
  1113. struct pid_list *p;
  1114. do_findprocs();
  1115. *n_killed = 0;
  1116. *n_notkilled = 0;
  1117. if (!found)
  1118. return;
  1119. clear(&killed);
  1120. for (p = found; p; p = p->next) {
  1121. if (testmode) {
  1122. if (quietmode <= 0)
  1123. printf("Would send signal %d to %d.\n",
  1124. signal_nr, p->pid);
  1125. (*n_killed)++;
  1126. } else if (kill(p->pid, signal_nr) == 0) {
  1127. push(&killed, p->pid);
  1128. (*n_killed)++;
  1129. } else {
  1130. if (signal_nr)
  1131. warning("failed to kill %d: %s\n",
  1132. p->pid, strerror(errno));
  1133. (*n_notkilled)++;
  1134. }
  1135. }
  1136. if (quietmode < 0 && killed) {
  1137. printf("Stopped %s (pid", what_stop);
  1138. for (p = killed; p; p = p->next)
  1139. printf(" %d", p->pid);
  1140. putchar(')');
  1141. if (retry_nr > 0)
  1142. printf(", retry #%d", retry_nr);
  1143. printf(".\n");
  1144. }
  1145. }
  1146. static void
  1147. set_what_stop(const char *str)
  1148. {
  1149. strncpy(what_stop, str, sizeof(what_stop));
  1150. what_stop[sizeof(what_stop) - 1] = '\0';
  1151. }
  1152. static int
  1153. run_stop_schedule(void)
  1154. {
  1155. int r, position, n_killed, n_notkilled, value, ratio, anykilled, retry_nr;
  1156. struct timeval stopat, before, after, interval, maxinterval;
  1157. if (testmode) {
  1158. if (schedule != NULL) {
  1159. if (quietmode <= 0)
  1160. printf("Ignoring --retry in test mode\n");
  1161. schedule = NULL;
  1162. }
  1163. }
  1164. if (cmdname)
  1165. set_what_stop(cmdname);
  1166. else if (execname)
  1167. set_what_stop(execname);
  1168. else if (pidfile)
  1169. sprintf(what_stop, "process in pidfile '%.200s'", pidfile);
  1170. else if (userspec)
  1171. sprintf(what_stop, "process(es) owned by '%.200s'", userspec);
  1172. else
  1173. fatal("internal error, please report");
  1174. n_killed = 0;
  1175. anykilled = 0;
  1176. retry_nr = 0;
  1177. if (schedule == NULL) {
  1178. do_stop(signal_nr, quietmode, &n_killed, &n_notkilled, 0);
  1179. if (n_notkilled > 0 && quietmode <= 0)
  1180. printf("%d pids were not killed\n", n_notkilled);
  1181. if (n_killed)
  1182. anykilled = 1;
  1183. goto x_finished;
  1184. }
  1185. for (position = 0; position < schedule_length; ) {
  1186. value = schedule[position].value;
  1187. n_notkilled = 0;
  1188. switch (schedule[position].type) {
  1189. case sched_goto:
  1190. position = value;
  1191. continue;
  1192. case sched_signal:
  1193. do_stop(value, quietmode, &n_killed, &n_notkilled, retry_nr++);
  1194. if (!n_killed)
  1195. goto x_finished;
  1196. else
  1197. anykilled = 1;
  1198. goto next_item;
  1199. case sched_timeout:
  1200. /* We want to keep polling for the processes, to see if they've exited,
  1201. * or until the timeout expires.
  1202. *
  1203. * This is a somewhat complicated algorithm to try to ensure that we
  1204. * notice reasonably quickly when all the processes have exited, but
  1205. * don't spend too much CPU time polling. In particular, on a fast
  1206. * machine with quick-exiting daemons we don't want to delay system
  1207. * shutdown too much, whereas on a slow one, or where processes are
  1208. * taking some time to exit, we want to increase the polling
  1209. * interval.
  1210. *
  1211. * The algorithm is as follows: we measure the elapsed time it takes
  1212. * to do one poll(), and wait a multiple of this time for the next
  1213. * poll. However, if that would put us past the end of the timeout
  1214. * period we wait only as long as the timeout period, but in any case
  1215. * we always wait at least MIN_POLL_INTERVAL (20ms). The multiple
  1216. * (‘ratio’) starts out as 2, and increases by 1 for each poll to a
  1217. * maximum of 10; so we use up to between 30% and 10% of the
  1218. * machine's resources (assuming a few reasonable things about system
  1219. * performance).
  1220. */
  1221. xgettimeofday(&stopat);
  1222. stopat.tv_sec += value;
  1223. ratio = 1;
  1224. for (;;) {
  1225. xgettimeofday(&before);
  1226. if (timercmp(&before, &stopat, >))
  1227. goto next_item;
  1228. do_stop(0, 1, &n_killed, &n_notkilled, 0);
  1229. if (!n_killed)
  1230. goto x_finished;
  1231. xgettimeofday(&after);
  1232. if (!timercmp(&after, &stopat, <))
  1233. goto next_item;
  1234. if (ratio < 10)
  1235. ratio++;
  1236. timersub(&stopat, &after, &maxinterval);
  1237. timersub(&after, &before, &interval);
  1238. tmul(&interval, ratio);
  1239. if (interval.tv_sec < 0 || interval.tv_usec < 0)
  1240. interval.tv_sec = interval.tv_usec = 0;
  1241. if (timercmp(&interval, &maxinterval, >))
  1242. interval = maxinterval;
  1243. if (interval.tv_sec == 0 &&
  1244. interval.tv_usec <= MIN_POLL_INTERVAL)
  1245. interval.tv_usec = MIN_POLL_INTERVAL;
  1246. r = select(0, NULL, NULL, NULL, &interval);
  1247. if (r < 0 && errno != EINTR)
  1248. fatal("select() failed for pause: %s",
  1249. strerror(errno));
  1250. }
  1251. default:
  1252. assert(!"schedule[].type value must be valid");
  1253. }
  1254. next_item:
  1255. position++;
  1256. }
  1257. if (quietmode <= 0)
  1258. printf("Program %s, %d process(es), refused to die.\n",
  1259. what_stop, n_killed);
  1260. return 2;
  1261. x_finished:
  1262. if (!anykilled) {
  1263. if (quietmode <= 0)
  1264. printf("No %s found running; none killed.\n", what_stop);
  1265. return exitnodo;
  1266. } else {
  1267. return 0;
  1268. }
  1269. }
  1270. int
  1271. main(int argc, char **argv)
  1272. {
  1273. int devnull_fd = -1;
  1274. gid_t rgid;
  1275. uid_t ruid;
  1276. #ifdef HAVE_TIOCNOTTY
  1277. int tty_fd = -1;
  1278. #endif
  1279. progname = argv[0];
  1280. parse_options(argc, argv);
  1281. argc -= optind;
  1282. argv += optind;
  1283. if (execname) {
  1284. char *fullexecname;
  1285. if (changeroot) {
  1286. int fullexecname_len = strlen(changeroot) + 1 +
  1287. strlen(execname) + 1;
  1288. fullexecname = xmalloc(fullexecname_len);
  1289. snprintf(fullexecname, fullexecname_len, "%s/%s",
  1290. changeroot, execname);
  1291. } else
  1292. fullexecname = execname;
  1293. if (stat(fullexecname, &exec_stat))
  1294. fatal("stat %s: %s", fullexecname, strerror(errno));
  1295. if (fullexecname != execname)
  1296. free(fullexecname);
  1297. }
  1298. if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
  1299. struct passwd *pw;
  1300. pw = getpwnam(userspec);
  1301. if (!pw)
  1302. fatal("user '%s' not found\n", userspec);
  1303. user_id = pw->pw_uid;
  1304. }
  1305. if (changegroup && sscanf(changegroup, "%d", &runas_gid) != 1) {
  1306. struct group *gr = getgrnam(changegroup);
  1307. if (!gr)
  1308. fatal("group '%s' not found\n", changegroup);
  1309. runas_gid = gr->gr_gid;
  1310. }
  1311. if (changeuser) {
  1312. struct passwd *pw;
  1313. if (sscanf(changeuser, "%d", &runas_uid) == 1)
  1314. pw = getpwuid(runas_uid);
  1315. else
  1316. pw = getpwnam(changeuser);
  1317. if (!pw)
  1318. fatal("user '%s' not found\n", changeuser);
  1319. runas_uid = pw->pw_uid;
  1320. if (changegroup == NULL) {
  1321. /* Pass the default group of this user. */
  1322. changegroup = ""; /* Just empty. */
  1323. runas_gid = pw->pw_gid;
  1324. }
  1325. if (access(pw->pw_dir, F_OK) == 0)
  1326. setenv("HOME", pw->pw_dir, 1);
  1327. }
  1328. if (stop) {
  1329. int i = run_stop_schedule();
  1330. exit(i);
  1331. }
  1332. do_findprocs();
  1333. if (found) {
  1334. if (quietmode <= 0)
  1335. printf("%s already running.\n", execname ? execname : "process");
  1336. exit(exitnodo);
  1337. }
  1338. if (testmode && quietmode <= 0) {
  1339. printf("Would start %s ", startas);
  1340. while (argc-- > 0)
  1341. printf("%s ", *argv++);
  1342. if (changeuser != NULL) {
  1343. printf(" (as user %s[%d]", changeuser, runas_uid);
  1344. if (changegroup != NULL)
  1345. printf(", and group %s[%d])", changegroup, runas_gid);
  1346. else
  1347. printf(")");
  1348. }
  1349. if (changeroot != NULL)
  1350. printf(" in directory %s", changeroot);
  1351. if (nicelevel)
  1352. printf(", and add %i to the priority", nicelevel);
  1353. if (proc_sched)
  1354. printf(", with scheduling policy %s with priority %i",
  1355. proc_sched->policy_name, proc_sched->priority);
  1356. if (io_sched)
  1357. printf(", with IO scheduling class %s with priority %i",
  1358. io_sched->policy_name, io_sched->priority);
  1359. printf(".\n");
  1360. }
  1361. if (testmode)
  1362. exit(0);
  1363. if (quietmode < 0)
  1364. printf("Starting %s...\n", startas);
  1365. *--argv = startas;
  1366. if (background) {
  1367. /* Ok, we need to detach this process. */
  1368. daemonize();
  1369. #ifdef HAVE_TIOCNOTTY
  1370. tty_fd = open("/dev/tty", O_RDWR);
  1371. #endif
  1372. devnull_fd = open("/dev/null", O_RDWR);
  1373. }
  1374. if (nicelevel) {
  1375. errno = 0;
  1376. if ((nice(nicelevel) == -1) && (errno != 0))
  1377. fatal("Unable to alter nice level by %i: %s",
  1378. nicelevel, strerror(errno));
  1379. }
  1380. if (proc_sched)
  1381. set_proc_schedule(proc_sched);
  1382. if (io_sched)
  1383. set_io_schedule(io_sched);
  1384. if (umask_value >= 0)
  1385. umask(umask_value);
  1386. if (mpidfile && pidfile != NULL) {
  1387. /* User wants _us_ to make the pidfile. :) */
  1388. FILE *pidf = fopen(pidfile, "w");
  1389. pid_t pidt = getpid();
  1390. if (pidf == NULL)
  1391. fatal("Unable to open pidfile '%s' for writing: %s",
  1392. pidfile, strerror(errno));
  1393. fprintf(pidf, "%d\n", pidt);
  1394. if (fflush(pidf))
  1395. fatal("unable to flush pidfile '%s'", pidfile);
  1396. if (fsync(fileno(pidf)))
  1397. fatal("unable to sync pidfile '%s'", pidfile);
  1398. fclose(pidf);
  1399. }
  1400. if (changeroot != NULL) {
  1401. if (chdir(changeroot) < 0)
  1402. fatal("Unable to chdir() to %s", changeroot);
  1403. if (chroot(changeroot) < 0)
  1404. fatal("Unable to chroot() to %s", changeroot);
  1405. }
  1406. if (chdir(changedir) < 0)
  1407. fatal("Unable to chdir() to %s", changedir);
  1408. rgid = getgid();
  1409. ruid = getuid();
  1410. if (changegroup != NULL) {
  1411. if (rgid != (gid_t)runas_gid)
  1412. if (setgid(runas_gid))
  1413. fatal("Unable to set gid to %d", runas_gid);
  1414. }
  1415. if (changeuser != NULL) {
  1416. /* We assume that if our real user and group are the same as
  1417. * the ones we should switch to, the supplementary groups
  1418. * will be already in place. */
  1419. if (rgid != (gid_t)runas_gid || ruid != (uid_t)runas_uid)
  1420. if (initgroups(changeuser, runas_gid))
  1421. fatal("Unable to set initgroups() with gid %d",
  1422. runas_gid);
  1423. if (ruid != (uid_t)runas_uid)
  1424. if (setuid(runas_uid))
  1425. fatal("Unable to set uid to %s", changeuser);
  1426. }
  1427. if (background) {
  1428. /* Continue background setup. */
  1429. int i;
  1430. #ifdef HAVE_TIOCNOTTY
  1431. /* Change tty. */
  1432. ioctl(tty_fd, TIOCNOTTY, 0);
  1433. close(tty_fd);
  1434. #endif
  1435. if (umask_value < 0)
  1436. umask(022); /* Set a default for dumb programs. */
  1437. dup2(devnull_fd, 0); /* stdin */
  1438. dup2(devnull_fd, 1); /* stdout */
  1439. dup2(devnull_fd, 2); /* stderr */
  1440. /* Now close all extra fds. */
  1441. for (i = get_open_fd_max() - 1; i >= 3; --i)
  1442. close(i);
  1443. }
  1444. execv(startas, argv);
  1445. fatal("Unable to start %s: %s", startas, strerror(errno));
  1446. }