inittab.c 6.0 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. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <sys/ioctl.h>
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <regex.h>
  22. #include <ctype.h>
  23. #include <libubox/utils.h>
  24. #include <libubox/list.h>
  25. #include "utils/utils.h"
  26. #include "procd.h"
  27. #include "rcS.h"
  28. #define TAG_ID 0
  29. #define TAG_RUNLVL 1
  30. #define TAG_ACTION 2
  31. #define TAG_PROCESS 3
  32. #define MAX_ARGS 8
  33. struct init_action;
  34. char *console = NULL;
  35. struct init_handler {
  36. const char *name;
  37. void (*cb) (struct init_action *a);
  38. int multi;
  39. };
  40. struct init_action {
  41. struct list_head list;
  42. char *id;
  43. char *argv[MAX_ARGS];
  44. char *line;
  45. struct init_handler *handler;
  46. struct uloop_process proc;
  47. int respawn;
  48. struct uloop_timeout tout;
  49. };
  50. static const char *tab = "/etc/inittab";
  51. static char *ask = "/sbin/askfirst";
  52. static LIST_HEAD(actions);
  53. static int dev_open(const char *dev)
  54. {
  55. int fd = -1;
  56. if (dev) {
  57. chdir("/dev");
  58. fd = open( dev, O_RDWR);
  59. chdir("/");
  60. }
  61. return fd;
  62. }
  63. static int dev_exist(const char *dev)
  64. {
  65. int res;
  66. res = dev_open(dev);
  67. if (res != -1) {
  68. close(res);
  69. }
  70. return (res != -1);
  71. }
  72. static void fork_worker(struct init_action *a)
  73. {
  74. int fd;
  75. pid_t p;
  76. a->proc.pid = fork();
  77. if (!a->proc.pid) {
  78. p = setsid();
  79. fd = dev_open(a->id);
  80. if (fd != -1)
  81. {
  82. dup2(fd, STDIN_FILENO);
  83. dup2(fd, STDOUT_FILENO);
  84. dup2(fd, STDERR_FILENO);
  85. if (fd > STDERR_FILENO)
  86. close(fd);
  87. }
  88. ioctl(STDIN_FILENO, TIOCSCTTY, 1);
  89. tcsetpgrp(STDIN_FILENO, p);
  90. execvp(a->argv[0], a->argv);
  91. ERROR("Failed to execute %s\n", a->argv[0]);
  92. exit(-1);
  93. }
  94. if (a->proc.pid > 0) {
  95. DEBUG(4, "Launched new %s action, pid=%d\n",
  96. a->handler->name,
  97. (int) a->proc.pid);
  98. uloop_process_add(&a->proc);
  99. }
  100. }
  101. static void child_exit(struct uloop_process *proc, int ret)
  102. {
  103. struct init_action *a = container_of(proc, struct init_action, proc);
  104. DEBUG(4, "pid:%d\n", proc->pid);
  105. uloop_timeout_set(&a->tout, a->respawn);
  106. }
  107. static void respawn(struct uloop_timeout *tout)
  108. {
  109. struct init_action *a = container_of(tout, struct init_action, tout);
  110. fork_worker(a);
  111. }
  112. static void rcdone(struct runqueue *q)
  113. {
  114. procd_state_next();
  115. }
  116. static void runrc(struct init_action *a)
  117. {
  118. if (!a->argv[1] || !a->argv[2]) {
  119. ERROR("valid format is rcS <S|K> <param>\n");
  120. return;
  121. }
  122. rcS(a->argv[1], a->argv[2], rcdone);
  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. for (i = MAX_ARGS - 1; i >= 1; i--)
  133. a->argv[i] = a->argv[i - 1];
  134. a->argv[0] = ask;
  135. a->respawn = 500;
  136. a->proc.cb = child_exit;
  137. fork_worker(a);
  138. }
  139. static void askconsole(struct init_action *a)
  140. {
  141. char line[256], *tty, *split;
  142. int i;
  143. tty = get_cmdline_val("console", line, sizeof(line));
  144. if (tty != NULL) {
  145. split = strchr(tty, ',');
  146. if (split != NULL)
  147. *split = '\0';
  148. if (!dev_exist(tty)) {
  149. DEBUG(4, "skipping %s\n", tty);
  150. return;
  151. }
  152. console = strdup(tty);
  153. a->id = strdup(tty);
  154. }
  155. else {
  156. console = NULL;
  157. a->id = NULL;
  158. }
  159. a->tout.cb = respawn;
  160. for (i = MAX_ARGS - 1; i >= 1; i--)
  161. a->argv[i] = a->argv[i - 1];
  162. a->argv[0] = ask;
  163. a->respawn = 500;
  164. a->proc.cb = child_exit;
  165. fork_worker(a);
  166. }
  167. static void rcrespawn(struct init_action *a)
  168. {
  169. a->tout.cb = respawn;
  170. a->respawn = 500;
  171. a->proc.cb = child_exit;
  172. fork_worker(a);
  173. }
  174. static struct init_handler handlers[] = {
  175. {
  176. .name = "sysinit",
  177. .cb = runrc,
  178. }, {
  179. .name = "shutdown",
  180. .cb = runrc,
  181. }, {
  182. .name = "askfirst",
  183. .cb = askfirst,
  184. .multi = 1,
  185. }, {
  186. .name = "askconsole",
  187. .cb = askconsole,
  188. .multi = 1,
  189. }, {
  190. .name = "respawn",
  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\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 = malloc(sizeof(struct init_action));
  235. memset(a, 0, sizeof(struct init_action));
  236. while (fgets(line, LINE_LEN, fp)) {
  237. char *tags[TAG_PROCESS + 1];
  238. char *tok;
  239. int i;
  240. int len = strlen(line);
  241. while (isspace(line[len - 1]))
  242. len--;
  243. line[len] = 0;
  244. if (*line == '#')
  245. continue;
  246. if (regexec(&pat_inittab, line, 5, matches, 0))
  247. continue;
  248. DEBUG(4, "Parsing inittab - %s", line);
  249. for (i = TAG_ID; i <= TAG_PROCESS; i++) {
  250. line[matches[i].rm_eo] = '\0';
  251. tags[i] = &line[matches[i + 1].rm_so];
  252. };
  253. tok = strtok(tags[TAG_PROCESS], " ");
  254. for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
  255. a->argv[i] = tok;
  256. tok = strtok(NULL, " ");
  257. }
  258. a->argv[i] = NULL;
  259. a->id = tags[TAG_ID];
  260. a->line = line;
  261. if (add_action(a, tags[TAG_ACTION]))
  262. continue;
  263. line = malloc(LINE_LEN);
  264. a = malloc(sizeof(struct init_action));
  265. memset(a, 0, sizeof(struct init_action));
  266. }
  267. fclose(fp);
  268. free(line);
  269. free(a);
  270. regfree(&pat_inittab);
  271. }