inittab.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <regex.h>
  21. #include <libubox/utils.h>
  22. #include <libubox/list.h>
  23. #include "procd.h"
  24. #include "rcS.h"
  25. #define TAG_ID 0
  26. #define TAG_RUNLVL 1
  27. #define TAG_ACTION 2
  28. #define TAG_PROCESS 3
  29. #define MAX_ARGS 8
  30. struct init_action;
  31. const char *console;
  32. struct init_handler {
  33. const char *name;
  34. void (*cb) (struct init_action *a);
  35. int multi;
  36. };
  37. struct init_action {
  38. struct list_head list;
  39. char *id;
  40. char *argv[MAX_ARGS];
  41. char *line;
  42. struct init_handler *handler;
  43. struct uloop_process proc;
  44. int respawn;
  45. struct uloop_timeout tout;
  46. };
  47. static const char *tab = "/etc/inittab";
  48. static char *ask = "/sbin/askfirst";
  49. static LIST_HEAD(actions);
  50. static void fork_worker(struct init_action *a)
  51. {
  52. a->proc.pid = fork();
  53. if (!a->proc.pid) {
  54. execvp(a->argv[0], a->argv);
  55. ERROR("Failed to execute %s\n", a->argv[0]);
  56. exit(-1);
  57. }
  58. if (a->proc.pid > 0) {
  59. DEBUG(2, "Launched new %s action, pid=%d\n",
  60. a->handler->name,
  61. (int) a->proc.pid);
  62. uloop_process_add(&a->proc);
  63. }
  64. }
  65. static void child_exit(struct uloop_process *proc, int ret)
  66. {
  67. struct init_action *a = container_of(proc, struct init_action, proc);
  68. DEBUG(2, "pid:%d\n", proc->pid);
  69. uloop_timeout_set(&a->tout, a->respawn);
  70. }
  71. static void respawn(struct uloop_timeout *tout)
  72. {
  73. struct init_action *a = container_of(tout, struct init_action, tout);
  74. fork_worker(a);
  75. }
  76. static void rcdone(struct runqueue *q)
  77. {
  78. procd_state_next();
  79. }
  80. static void runrc(struct init_action *a)
  81. {
  82. if (!a->argv[1] || !a->argv[2]) {
  83. ERROR("valid format is rcS <S|K> <param>\n");
  84. return;
  85. }
  86. rcS(a->argv[1], a->argv[2], rcdone);
  87. }
  88. static void askfirst(struct init_action *a)
  89. {
  90. struct stat s;
  91. int i;
  92. chdir("/dev");
  93. i = stat(a->id, &s);
  94. chdir("/");
  95. if (i || (console && !strcmp(console, a->id))) {
  96. DEBUG(2, "Skipping %s\n", a->id);
  97. return;
  98. }
  99. a->tout.cb = respawn;
  100. for (i = MAX_ARGS - 2; i >= 2; i--)
  101. a->argv[i] = a->argv[i - 2];
  102. a->argv[0] = ask;
  103. a->argv[1] = a->id;
  104. a->respawn = 500;
  105. a->proc.cb = child_exit;
  106. fork_worker(a);
  107. }
  108. static void askconsole(struct init_action *a)
  109. {
  110. struct stat s;
  111. char line[256], *tty;
  112. int i, r, fd = open("/proc/cmdline", O_RDONLY);
  113. regex_t pat_cmdline;
  114. regmatch_t matches[2];
  115. if (fd < 0)
  116. return;
  117. r = read(fd, line, sizeof(line) - 1);
  118. line[r] = '\0';
  119. close(fd);
  120. regcomp(&pat_cmdline, "console=([a-zA-Z0-9]*)", REG_EXTENDED);
  121. if (regexec(&pat_cmdline, line, 2, matches, 0))
  122. goto err_out;
  123. line[matches[1].rm_eo] = '\0';
  124. tty = &line[matches[1].rm_so];
  125. chdir("/dev");
  126. i = stat(tty, &s);
  127. chdir("/");
  128. if (i) {
  129. DEBUG(2, "skipping %s\n", tty);
  130. goto err_out;
  131. }
  132. console = strdup(tty);
  133. a->tout.cb = respawn;
  134. for (i = MAX_ARGS - 2; i >= 2; i--)
  135. a->argv[i] = a->argv[i - 2];
  136. a->argv[0] = ask;
  137. a->argv[1] = strdup(tty);
  138. a->respawn = 500;
  139. a->proc.cb = child_exit;
  140. fork_worker(a);
  141. err_out:
  142. regfree(&pat_cmdline);
  143. }
  144. static void rcrespawn(struct init_action *a)
  145. {
  146. a->tout.cb = respawn;
  147. a->respawn = 500;
  148. a->proc.cb = child_exit;
  149. fork_worker(a);
  150. }
  151. static struct init_handler handlers[] = {
  152. {
  153. .name = "sysinit",
  154. .cb = runrc,
  155. }, {
  156. .name = "shutdown",
  157. .cb = runrc,
  158. }, {
  159. .name = "askfirst",
  160. .cb = askfirst,
  161. .multi = 1,
  162. }, {
  163. .name = "askconsole",
  164. .cb = askconsole,
  165. .multi = 1,
  166. }, {
  167. .name = "respawn",
  168. .cb = rcrespawn,
  169. .multi = 1,
  170. }
  171. };
  172. static int add_action(struct init_action *a, const char *name)
  173. {
  174. int i;
  175. for (i = 0; i < ARRAY_SIZE(handlers); i++)
  176. if (!strcmp(handlers[i].name, name)) {
  177. a->handler = &handlers[i];
  178. list_add_tail(&a->list, &actions);
  179. return 0;
  180. }
  181. ERROR("Unknown init handler %s\n", name);
  182. return -1;
  183. }
  184. void procd_inittab_run(const char *handler)
  185. {
  186. struct init_action *a;
  187. list_for_each_entry(a, &actions, list)
  188. if (!strcmp(a->handler->name, handler)) {
  189. if (a->handler->multi) {
  190. a->handler->cb(a);
  191. continue;
  192. }
  193. a->handler->cb(a);
  194. break;
  195. }
  196. }
  197. void procd_inittab(void)
  198. {
  199. #define LINE_LEN 128
  200. FILE *fp = fopen(tab, "r");
  201. struct init_action *a;
  202. regex_t pat_inittab;
  203. regmatch_t matches[5];
  204. char *line;
  205. if (!fp) {
  206. ERROR("Failed to open %s\n", tab);
  207. return;
  208. }
  209. regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9/[.-.]. ]*)", REG_EXTENDED);
  210. line = malloc(LINE_LEN);
  211. a = malloc(sizeof(struct init_action));
  212. memset(a, 0, sizeof(struct init_action));
  213. while (fgets(line, LINE_LEN, fp)) {
  214. char *tags[TAG_PROCESS + 1];
  215. char *tok;
  216. int i;
  217. if (*line == '#')
  218. continue;
  219. if (regexec(&pat_inittab, line, 5, matches, 0))
  220. continue;
  221. DEBUG(2, "Parsing inittab - %s", line);
  222. for (i = TAG_ID; i <= TAG_PROCESS; i++) {
  223. line[matches[i].rm_eo] = '\0';
  224. tags[i] = &line[matches[i + 1].rm_so];
  225. };
  226. tok = strtok(tags[TAG_PROCESS], " ");
  227. for (i = 0; i < (MAX_ARGS - i - 1) && tok; i++) {
  228. a->argv[i] = tok;
  229. tok = strtok(NULL, " ");
  230. }
  231. a->argv[i] = NULL;
  232. a->id = tags[TAG_ID];
  233. a->line = line;
  234. if (add_action(a, tags[TAG_ACTION]))
  235. continue;
  236. line = malloc(LINE_LEN);
  237. a = malloc(sizeof(struct init_action));
  238. memset(a, 0, sizeof(struct init_action));
  239. }
  240. fclose(fp);
  241. free(line);
  242. free(a);
  243. regfree(&pat_inittab);
  244. }