inittab.c 6.1 KB

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