inittab.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. if (dev_exist("console"))
  155. tty = "console";
  156. else
  157. tty = get_active_console(line, sizeof(line));
  158. }
  159. if (tty != NULL) {
  160. split = strchr(tty, ',');
  161. if (split != NULL)
  162. *split = '\0';
  163. if (!dev_exist(tty)) {
  164. DEBUG(4, "skipping %s\n", tty);
  165. return;
  166. }
  167. console = strdup(tty);
  168. a->id = strdup(tty);
  169. }
  170. else {
  171. console = NULL;
  172. a->id = NULL;
  173. }
  174. a->tout.cb = respawn;
  175. /* shift arguments only if not yet done */
  176. if (a->argv[0] != ask) {
  177. for (i = MAX_ARGS - 1; i >= 1; i--)
  178. a->argv[i] = a->argv[i - 1];
  179. a->argv[0] = ask;
  180. }
  181. a->respawn = 500;
  182. a->proc.cb = child_exit;
  183. if (!a->proc.pid)
  184. fork_worker(a);
  185. }
  186. static void rcrespawn(struct init_action *a)
  187. {
  188. a->tout.cb = respawn;
  189. a->respawn = 500;
  190. a->proc.cb = child_exit;
  191. if (!a->proc.pid)
  192. fork_worker(a);
  193. }
  194. static struct init_handler handlers[] = {
  195. {
  196. .name = "sysinit",
  197. .cb = runrc,
  198. }, {
  199. .name = "shutdown",
  200. .cb = runrc,
  201. }, {
  202. .name = "askfirst",
  203. .cb = askfirst,
  204. .multi = 1,
  205. }, {
  206. .name = "askconsole",
  207. .cb = askconsole,
  208. .multi = 1,
  209. }, {
  210. .name = "respawn",
  211. .cb = rcrespawn,
  212. .multi = 1,
  213. }, {
  214. .name = "askconsolelate",
  215. .cb = askconsole,
  216. .multi = 1,
  217. }, {
  218. .name = "respawnlate",
  219. .cb = rcrespawn,
  220. .multi = 1,
  221. }
  222. };
  223. static int add_action(struct init_action *a, const char *name)
  224. {
  225. int i;
  226. for (i = 0; i < ARRAY_SIZE(handlers); i++)
  227. if (!strcmp(handlers[i].name, name)) {
  228. a->handler = &handlers[i];
  229. list_add_tail(&a->list, &actions);
  230. return 0;
  231. }
  232. ERROR("Unknown init handler %s\n", name);
  233. return -1;
  234. }
  235. void procd_inittab_run(const char *handler)
  236. {
  237. struct init_action *a;
  238. list_for_each_entry(a, &actions, list)
  239. if (!strcmp(a->handler->name, handler)) {
  240. a->handler->cb(a);
  241. if (!a->handler->multi)
  242. break;
  243. }
  244. }
  245. void procd_inittab_kill(void)
  246. {
  247. struct init_action *a;
  248. list_for_each_entry(a, &actions, list) {
  249. a->respawn = -1;
  250. if (a->proc.pid)
  251. kill(a->proc.pid, SIGKILL);
  252. }
  253. }
  254. void procd_inittab(void)
  255. {
  256. #define LINE_LEN 128
  257. FILE *fp = fopen(tab, "r");
  258. struct init_action *a;
  259. regex_t pat_inittab;
  260. regmatch_t matches[5];
  261. char *line;
  262. if (!fp) {
  263. ERROR("Failed to open %s: %m\n", tab);
  264. return;
  265. }
  266. regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
  267. line = malloc(LINE_LEN);
  268. a = calloc(1, sizeof(struct init_action));
  269. while (fgets(line, LINE_LEN, fp)) {
  270. char *tags[TAG_PROCESS + 1];
  271. char *tok;
  272. int i;
  273. int len = strlen(line);
  274. while (isspace(line[len - 1]))
  275. len--;
  276. line[len] = 0;
  277. if (*line == '#')
  278. continue;
  279. if (regexec(&pat_inittab, line, 5, matches, 0))
  280. continue;
  281. DEBUG(4, "Parsing inittab - %s\n", line);
  282. for (i = TAG_ID; i <= TAG_PROCESS; i++) {
  283. line[matches[i].rm_eo] = '\0';
  284. tags[i] = &line[matches[i + 1].rm_so];
  285. };
  286. tok = strtok(tags[TAG_PROCESS], " ");
  287. for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
  288. a->argv[i] = tok;
  289. tok = strtok(NULL, " ");
  290. }
  291. a->argv[i] = NULL;
  292. a->id = tags[TAG_ID];
  293. a->line = line;
  294. if (add_action(a, tags[TAG_ACTION]))
  295. continue;
  296. line = malloc(LINE_LEN);
  297. a = calloc(1, sizeof(struct init_action));
  298. }
  299. fclose(fp);
  300. free(line);
  301. free(a);
  302. regfree(&pat_inittab);
  303. }