inittab.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 (!dev_exist(a->id)) {
  97. DEBUG(4, "Skipping respawn: device '%s' does not exist anymore\n", a->id);
  98. return;
  99. }
  100. uloop_timeout_set(&a->tout, a->respawn);
  101. }
  102. static void respawn(struct uloop_timeout *tout)
  103. {
  104. struct init_action *a = container_of(tout, struct init_action, tout);
  105. if (!a->proc.pid)
  106. fork_worker(a);
  107. }
  108. static void rcdone(struct runqueue *q)
  109. {
  110. procd_state_next();
  111. }
  112. static void runrc(struct init_action *a)
  113. {
  114. if (!a->argv[1] || !a->argv[2]) {
  115. ERROR("valid format is rcS <S|K> <param>\n");
  116. return;
  117. }
  118. /* proceed even if no init or shutdown scripts run */
  119. if (rcS(a->argv[1], a->argv[2], rcdone))
  120. rcdone(NULL);
  121. }
  122. static void askfirst(struct init_action *a)
  123. {
  124. int i;
  125. if (!dev_exist(a->id) || (console && !strcmp(console, a->id))) {
  126. DEBUG(4, "Skipping %s\n", a->id);
  127. return;
  128. }
  129. a->tout.cb = respawn;
  130. /* shift arguments only if not yet done */
  131. if (a->argv[0] != ask) {
  132. for (i = MAX_ARGS - 1; i >= 1; i--)
  133. a->argv[i] = a->argv[i - 1];
  134. a->argv[0] = ask;
  135. }
  136. a->respawn = 500;
  137. a->proc.cb = child_exit;
  138. if (!a->proc.pid)
  139. fork_worker(a);
  140. }
  141. static void askconsole(struct init_action *a)
  142. {
  143. char line[256], *tty, *split;
  144. int i;
  145. /* First, try console= on the kernel command line,
  146. * then fallback to /sys/class/tty/console/active,
  147. * which should work when linux,stdout-path (or equivalent)
  148. * is in the device tree
  149. */
  150. tty = get_cmdline_val("console", line, sizeof(line));
  151. if (tty == NULL) {
  152. if (dev_exist("console"))
  153. tty = "console";
  154. else
  155. tty = get_active_console(line, sizeof(line));
  156. }
  157. if (tty != NULL) {
  158. split = strchr(tty, ',');
  159. if (split != NULL)
  160. *split = '\0';
  161. if (!dev_exist(tty)) {
  162. DEBUG(4, "skipping %s\n", tty);
  163. return;
  164. }
  165. console = strdup(tty);
  166. a->id = strdup(tty);
  167. }
  168. else {
  169. console = NULL;
  170. a->id = NULL;
  171. }
  172. a->tout.cb = respawn;
  173. /* shift arguments only if not yet done */
  174. if (a->argv[0] != ask) {
  175. for (i = MAX_ARGS - 1; i >= 1; i--)
  176. a->argv[i] = a->argv[i - 1];
  177. a->argv[0] = ask;
  178. }
  179. a->respawn = 500;
  180. a->proc.cb = child_exit;
  181. if (!a->proc.pid)
  182. fork_worker(a);
  183. }
  184. static void rcrespawn(struct init_action *a)
  185. {
  186. a->tout.cb = respawn;
  187. a->respawn = 500;
  188. a->proc.cb = child_exit;
  189. if (!a->proc.pid)
  190. fork_worker(a);
  191. }
  192. static struct init_handler handlers[] = {
  193. {
  194. .name = "sysinit",
  195. .cb = runrc,
  196. }, {
  197. .name = "shutdown",
  198. .cb = runrc,
  199. }, {
  200. .name = "askfirst",
  201. .cb = askfirst,
  202. .multi = 1,
  203. }, {
  204. .name = "askconsole",
  205. .cb = askconsole,
  206. .multi = 1,
  207. }, {
  208. .name = "respawn",
  209. .cb = rcrespawn,
  210. .multi = 1,
  211. }, {
  212. .name = "askconsolelate",
  213. .cb = askconsole,
  214. .multi = 1,
  215. }, {
  216. .name = "respawnlate",
  217. .cb = rcrespawn,
  218. .multi = 1,
  219. }
  220. };
  221. static int add_action(struct init_action *a, const char *name)
  222. {
  223. int i;
  224. for (i = 0; i < ARRAY_SIZE(handlers); i++)
  225. if (!strcmp(handlers[i].name, name)) {
  226. a->handler = &handlers[i];
  227. list_add_tail(&a->list, &actions);
  228. return 0;
  229. }
  230. ERROR("Unknown init handler %s\n", name);
  231. return -1;
  232. }
  233. void procd_inittab_run(const char *handler)
  234. {
  235. struct init_action *a;
  236. list_for_each_entry(a, &actions, list)
  237. if (!strcmp(a->handler->name, handler)) {
  238. a->handler->cb(a);
  239. if (!a->handler->multi)
  240. break;
  241. }
  242. }
  243. void procd_inittab(void)
  244. {
  245. #define LINE_LEN 128
  246. FILE *fp = fopen(tab, "r");
  247. struct init_action *a;
  248. regex_t pat_inittab;
  249. regmatch_t matches[5];
  250. char *line;
  251. if (!fp) {
  252. ERROR("Failed to open %s: %m\n", tab);
  253. return;
  254. }
  255. regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
  256. line = malloc(LINE_LEN);
  257. a = calloc(1, sizeof(struct init_action));
  258. while (fgets(line, LINE_LEN, fp)) {
  259. char *tags[TAG_PROCESS + 1];
  260. char *tok;
  261. int i;
  262. int len = strlen(line);
  263. while (isspace(line[len - 1]))
  264. len--;
  265. line[len] = 0;
  266. if (*line == '#')
  267. continue;
  268. if (regexec(&pat_inittab, line, 5, matches, 0))
  269. continue;
  270. DEBUG(4, "Parsing inittab - %s\n", line);
  271. for (i = TAG_ID; i <= TAG_PROCESS; i++) {
  272. line[matches[i].rm_eo] = '\0';
  273. tags[i] = &line[matches[i + 1].rm_so];
  274. };
  275. tok = strtok(tags[TAG_PROCESS], " ");
  276. for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
  277. a->argv[i] = tok;
  278. tok = strtok(NULL, " ");
  279. }
  280. a->argv[i] = NULL;
  281. a->id = tags[TAG_ID];
  282. a->line = line;
  283. if (add_action(a, tags[TAG_ACTION]))
  284. continue;
  285. line = malloc(LINE_LEN);
  286. a = calloc(1, sizeof(struct init_action));
  287. }
  288. fclose(fp);
  289. free(line);
  290. free(a);
  291. regfree(&pat_inittab);
  292. }