inittab.c 6.4 KB

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