inittab.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/ioctl.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <regex.h>
  23. #include <ctype.h>
  24. #include <libubox/utils.h>
  25. #include <libubox/list.h>
  26. #include "utils/utils.h"
  27. #include "procd.h"
  28. #include "rcS.h"
  29. #ifndef O_PATH
  30. #define O_PATH 010000000
  31. #endif
  32. #define TAG_ID 0
  33. #define TAG_RUNLVL 1
  34. #define TAG_ACTION 2
  35. #define TAG_PROCESS 3
  36. #define MAX_ARGS 8
  37. struct init_action;
  38. char *console = NULL;
  39. struct init_handler {
  40. const char *name;
  41. void (*cb) (struct init_action *a);
  42. int multi;
  43. };
  44. struct init_action {
  45. struct list_head list;
  46. char *id;
  47. char *argv[MAX_ARGS];
  48. char *line;
  49. struct init_handler *handler;
  50. struct uloop_process proc;
  51. int respawn;
  52. struct uloop_timeout tout;
  53. };
  54. static const char *tab = "/etc/inittab";
  55. static char *ask = "/sbin/askfirst";
  56. static LIST_HEAD(actions);
  57. static int dev_exist(const char *dev)
  58. {
  59. int dfd, fd;
  60. dfd = open("/dev", O_PATH|O_DIRECTORY);
  61. if (dfd < 0)
  62. return 0;
  63. fd = openat(dfd, dev, O_RDONLY);
  64. close(dfd);
  65. if (fd < 0)
  66. return 0;
  67. close(fd);
  68. return 1;
  69. }
  70. static void fork_worker(struct init_action *a)
  71. {
  72. pid_t p;
  73. a->proc.pid = fork();
  74. if (!a->proc.pid) {
  75. p = setsid();
  76. if (patch_stdio(a->id))
  77. ERROR("Failed to setup i/o redirection\n");
  78. ioctl(STDIN_FILENO, TIOCSCTTY, 1);
  79. tcsetpgrp(STDIN_FILENO, p);
  80. execvp(a->argv[0], a->argv);
  81. ERROR("Failed to execute %s: %m\n", a->argv[0]);
  82. exit(-1);
  83. }
  84. if (a->proc.pid > 0) {
  85. DEBUG(4, "Launched new %s action, pid=%d\n",
  86. a->handler->name,
  87. (int) a->proc.pid);
  88. uloop_process_add(&a->proc);
  89. }
  90. }
  91. static void child_exit(struct uloop_process *proc, int ret)
  92. {
  93. struct init_action *a = container_of(proc, struct init_action, proc);
  94. DEBUG(4, "pid:%d, exitcode:%d\n", proc->pid, ret);
  95. proc->pid = 0;
  96. if (a->respawn < 0)
  97. return;
  98. if (!dev_exist(a->id)) {
  99. DEBUG(4, "Skipping respawn: device '%s' does not exist anymore\n", a->id);
  100. return;
  101. }
  102. uloop_timeout_set(&a->tout, a->respawn);
  103. }
  104. static void respawn(struct uloop_timeout *tout)
  105. {
  106. struct init_action *a = container_of(tout, struct init_action, tout);
  107. if (!a->proc.pid)
  108. fork_worker(a);
  109. }
  110. static void rcdone(struct runqueue *q)
  111. {
  112. procd_state_next();
  113. }
  114. static void runrc(struct init_action *a)
  115. {
  116. if (!a->argv[1] || !a->argv[2]) {
  117. ERROR("valid format is rcS <S|K> <param>\n");
  118. return;
  119. }
  120. /* proceed even if no init or shutdown scripts run */
  121. if (rcS(a->argv[1], a->argv[2], rcdone))
  122. rcdone(NULL);
  123. }
  124. static void askfirst(struct init_action *a)
  125. {
  126. int i;
  127. if (!dev_exist(a->id) || (console && !strcmp(console, a->id))) {
  128. DEBUG(4, "Skipping %s\n", a->id);
  129. return;
  130. }
  131. a->tout.cb = respawn;
  132. /* shift arguments only if not yet done */
  133. if (a->argv[0] != ask) {
  134. for (i = MAX_ARGS - 1; i >= 1; i--)
  135. a->argv[i] = a->argv[i - 1];
  136. a->argv[0] = ask;
  137. }
  138. a->respawn = 500;
  139. a->proc.cb = child_exit;
  140. if (!a->proc.pid)
  141. fork_worker(a);
  142. }
  143. static void askconsole(struct init_action *a)
  144. {
  145. char line[256], *tty, *split;
  146. int i;
  147. /* First, try console= on the kernel command line,
  148. * then fallback to /sys/class/tty/console/active,
  149. * which should work when linux,stdout-path (or equivalent)
  150. * is in the device tree
  151. */
  152. tty = get_cmdline_val("console", line, sizeof(line));
  153. if (tty == NULL ||
  154. get_cmdline_val_offset("console", line, sizeof(line), 1)) {
  155. if (dev_exist("console"))
  156. tty = "console";
  157. else
  158. tty = get_active_console(line, sizeof(line));
  159. }
  160. if (tty != NULL) {
  161. split = strchr(tty, ',');
  162. if (split != NULL)
  163. *split = '\0';
  164. if (!dev_exist(tty)) {
  165. DEBUG(4, "skipping %s\n", tty);
  166. return;
  167. }
  168. console = strdup(tty);
  169. a->id = strdup(tty);
  170. }
  171. else {
  172. console = NULL;
  173. a->id = NULL;
  174. }
  175. a->tout.cb = respawn;
  176. /* shift arguments only if not yet done */
  177. if (a->argv[0] != ask) {
  178. for (i = MAX_ARGS - 1; i >= 1; i--)
  179. a->argv[i] = a->argv[i - 1];
  180. a->argv[0] = ask;
  181. }
  182. a->respawn = 500;
  183. a->proc.cb = child_exit;
  184. if (!a->proc.pid)
  185. fork_worker(a);
  186. }
  187. static void rcrespawn(struct init_action *a)
  188. {
  189. a->tout.cb = respawn;
  190. a->respawn = 500;
  191. a->proc.cb = child_exit;
  192. if (!a->proc.pid)
  193. fork_worker(a);
  194. }
  195. static struct init_handler handlers[] = {
  196. {
  197. .name = "sysinit",
  198. .cb = runrc,
  199. }, {
  200. .name = "shutdown",
  201. .cb = runrc,
  202. }, {
  203. .name = "askfirst",
  204. .cb = askfirst,
  205. .multi = 1,
  206. }, {
  207. .name = "askconsole",
  208. .cb = askconsole,
  209. .multi = 1,
  210. }, {
  211. .name = "respawn",
  212. .cb = rcrespawn,
  213. .multi = 1,
  214. }, {
  215. .name = "askconsolelate",
  216. .cb = askconsole,
  217. .multi = 1,
  218. }, {
  219. .name = "respawnlate",
  220. .cb = rcrespawn,
  221. .multi = 1,
  222. }
  223. };
  224. static int add_action(struct init_action *a, const char *name)
  225. {
  226. int i;
  227. for (i = 0; i < ARRAY_SIZE(handlers); i++)
  228. if (!strcmp(handlers[i].name, name)) {
  229. a->handler = &handlers[i];
  230. list_add_tail(&a->list, &actions);
  231. return 0;
  232. }
  233. ERROR("Unknown init handler %s\n", name);
  234. return -1;
  235. }
  236. void procd_inittab_run(const char *handler)
  237. {
  238. struct init_action *a;
  239. list_for_each_entry(a, &actions, list)
  240. if (!strcmp(a->handler->name, handler)) {
  241. a->handler->cb(a);
  242. if (!a->handler->multi)
  243. break;
  244. }
  245. }
  246. void procd_inittab_kill(void)
  247. {
  248. struct init_action *a;
  249. list_for_each_entry(a, &actions, list) {
  250. a->respawn = -1;
  251. if (a->proc.pid)
  252. kill(a->proc.pid, SIGKILL);
  253. }
  254. }
  255. void procd_inittab(void)
  256. {
  257. #define LINE_LEN 128
  258. FILE *fp = fopen(tab, "r");
  259. struct init_action *a;
  260. regex_t pat_inittab;
  261. regmatch_t matches[5];
  262. char *line;
  263. if (!fp) {
  264. ERROR("Failed to open %s: %m\n", tab);
  265. return;
  266. }
  267. regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
  268. line = malloc(LINE_LEN);
  269. a = calloc(1, sizeof(struct init_action));
  270. while (fgets(line, LINE_LEN, fp)) {
  271. char *tags[TAG_PROCESS + 1];
  272. char *tok;
  273. int i;
  274. int len = strlen(line);
  275. while (isspace(line[len - 1]))
  276. len--;
  277. line[len] = 0;
  278. if (*line == '#')
  279. continue;
  280. if (regexec(&pat_inittab, line, 5, matches, 0))
  281. continue;
  282. DEBUG(4, "Parsing inittab - %s\n", line);
  283. for (i = TAG_ID; i <= TAG_PROCESS; i++) {
  284. line[matches[i].rm_eo] = '\0';
  285. tags[i] = &line[matches[i + 1].rm_so];
  286. };
  287. tok = strtok(tags[TAG_PROCESS], " ");
  288. for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
  289. a->argv[i] = tok;
  290. tok = strtok(NULL, " ");
  291. }
  292. a->argv[i] = NULL;
  293. a->id = tags[TAG_ID];
  294. a->line = line;
  295. if (add_action(a, tags[TAG_ACTION]))
  296. continue;
  297. line = malloc(LINE_LEN);
  298. a = calloc(1, sizeof(struct init_action));
  299. }
  300. fclose(fp);
  301. free(line);
  302. free(a);
  303. regfree(&pat_inittab);
  304. }