simpleinit.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /* simpleinit.c - poe@daimi.aau.dk */
  2. /* Version 1.21 */
  3. /* gerg@snapgear.com -- modified for direct console support DEC/1999 */
  4. /* davidm@snapgear.com -- modified for init.conf SEP/2004 */
  5. /* toby@snapgear.com -- allow the array of commands to grow as needed OCT/2004 */
  6. /* davidm@snapgear.com -- use dynamically allocated tables APR/2005 */
  7. #define _GNU_SOURCE /* For crypt() and termios defines */
  8. #include <sys/types.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <stdio.h>
  13. #include <fcntl.h>
  14. #include <string.h>
  15. #include <signal.h>
  16. #include <pwd.h>
  17. #include <sys/file.h>
  18. #include <sys/wait.h>
  19. #include <sys/stat.h>
  20. #include <sys/termios.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/uio.h>
  23. #include <linux/version.h>
  24. #include <utmp.h>
  25. #include <errno.h>
  26. #include <time.h>
  27. #include <termios.h>
  28. #ifdef SHADOW_PWD
  29. #include <shadow.h>
  30. #endif
  31. #if __GNU_LIBRARY__ > 5
  32. #include <sys/reboot.h>
  33. #endif
  34. #include "pathnames.h"
  35. #define BUF_SIZ 100 /* max size of a line in inittab */
  36. #define NUMCMD 30 /* step size when realloc more space for the commands */
  37. #define NUMTOK 20 /* max number of tokens in inittab command */
  38. /* Threshold time for detecting "fast" spawning processes */
  39. static int testtime = 90;
  40. /* Number of rapid respawns that counts as too fast */
  41. static int maxspawn = 5;
  42. /* Maximum delay between runs */
  43. static int maxdelay = 595;
  44. /* Time between successive runs of a process */
  45. static int delaytime = 5;
  46. #define MAXTRIES 3 /* number of tries allowed when giving the password */
  47. #define RUN_RC /* Use a console if possible */
  48. #ifndef CTRL
  49. #define CTRL(X) ((X)&037)
  50. #endif
  51. #ifdef INCLUDE_TIMEZONE
  52. char tzone[BUF_SIZ];
  53. #endif
  54. /* #define DEBUGGING */
  55. /* Define this if you want init to ignore the termcap field in inittab for
  56. console ttys. */
  57. /* #define SPECIAL_CONSOLE_TERM */
  58. struct initline {
  59. pid_t pid;
  60. time_t lastrun;
  61. time_t nextrun;
  62. char **toks;
  63. short delay;
  64. char tty[10];
  65. char termcap[30];
  66. char *line;
  67. char *fullline;
  68. unsigned char xcnt;
  69. };
  70. struct initline *inittab;
  71. /* How many struct initline's will fit in the memory pointed to by inittab */
  72. int inittab_size = 0;
  73. int numcmd;
  74. int stopped = 0; /* are we stopped */
  75. int reload = 0; /* are we stopped */
  76. int run_sigint_processing = 0;
  77. extern void spawn(int);
  78. extern void hup_handler();
  79. extern void reload_inittab();
  80. extern void read_inittab(void);
  81. static int read_initfile(const char *);
  82. extern void tstp_handler();
  83. extern void int_handler();
  84. extern void sigint_processing();
  85. extern void cont_handler();
  86. extern void set_tz(void);
  87. extern void write_wtmp(void);
  88. extern void make_ascii_tty(void);
  89. extern void make_console(const char *);
  90. extern int boot_single(int singlearg, int argc, char *argv[]);
  91. #ifdef CONFIG_USER_INIT_CONF
  92. extern void load_init_conf(void);
  93. #endif
  94. /* Keep track of console device, if any... */
  95. #if LINUX_VERSION_CODE < 0x020100
  96. char *console_device = NULL;
  97. int console_baud = -1;
  98. #else
  99. int have_console = 0;
  100. #endif
  101. static void err(const char *s)
  102. {
  103. struct iovec output[2];
  104. #if LINUX_VERSION_CODE < 0x020100
  105. int fd;
  106. #endif
  107. output[0].iov_base = "init: ";
  108. output[0].iov_len = 6;
  109. output[1].iov_base = (void *)s;
  110. output[1].iov_len = strlen(s);
  111. #if LINUX_VERSION_CODE < 0x020100
  112. if (console_device == NULL) return;
  113. if((fd = open(console_device, O_WRONLY)) < 0) return;
  114. writev(fd, output, 2);
  115. close(fd);
  116. #else
  117. if (have_console)
  118. writev(1, output, 2);
  119. #endif
  120. }
  121. void
  122. add_tok(struct initline *p, char *tok)
  123. {
  124. int i;
  125. for (i = 0; p->toks && p->toks[i]; i++)
  126. ;
  127. /* allocate space for new entry and terminating NULL */
  128. p->toks = (char **) realloc(p->toks, (i + 2) * sizeof(char *));
  129. if (!p->toks) {
  130. err("malloc failed\n");
  131. _exit(1);
  132. }
  133. p->toks[i++] = tok;
  134. p->toks[i] = NULL;
  135. }
  136. static void enter_single(void)
  137. {
  138. pid_t pid;
  139. char *av[2];
  140. err("Booting to single user mode\n");
  141. av[0] = _PATH_BSHELL;
  142. av[1] = NULL;
  143. if((pid = vfork()) == 0) {
  144. extern char **environ;
  145. /* the child */
  146. execve(_PATH_BSHELL, av, environ);
  147. err("exec of single user shell failed\n");
  148. _exit(0);
  149. } else if(pid > 0) {
  150. int i;
  151. while(wait(&i) != pid) /* nothing */;
  152. } else if(pid < 0) {
  153. err("fork of single user shell failed\n");
  154. }
  155. unlink(_PATH_SINGLE);
  156. }
  157. #if LINUX_VERSION_CODE < 0x020100
  158. static void
  159. set_console_baud(int baud)
  160. {
  161. switch (baud) {
  162. case 50: console_baud = B50; break;
  163. case 75: console_baud = B75; break;
  164. case 110: console_baud = B110; break;
  165. case 134: console_baud = B134; break;
  166. case 150: console_baud = B150; break;
  167. case 200: console_baud = B200; break;
  168. case 300: console_baud = B300; break;
  169. case 600: console_baud = B600; break;
  170. case 1200: console_baud = B1200; break;
  171. case 1800: console_baud = B1800; break;
  172. case 2400: console_baud = B2400; break;
  173. case 4800: console_baud = B4800; break;
  174. default:
  175. case 9600: console_baud = B9600; break;
  176. case 19200: console_baud = B19200; break;
  177. case 38400: console_baud = B38400; break;
  178. case 57600: console_baud = B57600; break;
  179. case 115200: console_baud = B115200; break;
  180. case 230400: console_baud = B230400; break;
  181. case 460800: console_baud = B460800; break;
  182. }
  183. }
  184. #endif
  185. static int do_command(const char *path, const char *filename, int dowait)
  186. {
  187. pid_t pid, wpid;
  188. int stat, st;
  189. if((pid = vfork()) == 0) {
  190. /* the child */
  191. char *argv[3];
  192. #ifdef INCLUDE_TIMEZONE
  193. char tz[BUF_SIZ];
  194. #endif
  195. char *env[3];
  196. /* Use /dev/null for stdin */
  197. close(0);
  198. open("/dev/null", O_RDONLY);
  199. argv[0] = (char *)path;
  200. argv[1] = (char *)filename;
  201. argv[2] = NULL;
  202. env[0] = "PATH=/bin:/usr/bin:/etc:/sbin:/usr/sbin";
  203. #ifdef INCLUDE_TIMEZONE
  204. strcpy(tz, "TZ=");
  205. strcat(tz, tzone);
  206. env[1] = tz;
  207. env[2] = NULL;
  208. #else
  209. env[1] = NULL;
  210. #endif
  211. execve(path, argv, env);
  212. err("exec rc failed\n");
  213. _exit(2);
  214. } else if(pid > 0) {
  215. if (!dowait)
  216. stat = 0;
  217. else {
  218. /* parent, wait till rc process dies before spawning */
  219. while ((wpid = wait(&stat)) != pid)
  220. if (wpid == -1 && errno == ECHILD) { /* see wait(2) manpage */
  221. stat = 0;
  222. break;
  223. }
  224. }
  225. } else if(pid < 0) {
  226. err("fork of rc shell failed\n");
  227. stat = -1;
  228. }
  229. st = WEXITSTATUS(stat);
  230. return st;
  231. }
  232. /*
  233. * run /etc/rc. The environment is passed to the script, so the RC environment
  234. * variable can be used to decide what to do. RC may be set from LILO.
  235. */
  236. static int do_rc(void)
  237. {
  238. int rc;
  239. rc = do_command(_PATH_BSHELL, _PATH_RC, 1);
  240. if (rc)
  241. return(rc);
  242. #ifdef CONFIG_USER_INIT_RUN_FIREWALL
  243. rc = do_command(_PATH_FIREWALL, "-i", 1);
  244. if (rc)
  245. err(_PATH_FIREWALL " failed!");
  246. #endif
  247. #ifdef CONFIG_USER_FLATFSD_FLATFSD
  248. rc = do_command(_PATH_BSHELL, _PATH_CONFIGRC, 1);
  249. if (rc)
  250. err(_PATH_CONFIGRC " failed!");
  251. #endif
  252. #ifdef CONFIG_USER_INIT_RUN_FIREWALL
  253. rc = do_command(_PATH_FIREWALL, NULL, 0);
  254. if (rc)
  255. err(_PATH_FIREWALL " failed!");
  256. #endif
  257. #ifdef INCLUDE_TIMEZONE
  258. /* We read the timezone file here, because the flat file system
  259. * has probably been created by now.
  260. */
  261. set_tz();
  262. #endif
  263. return(0);
  264. }
  265. void respawn_children(int signo) {
  266. int i, delta = -1;
  267. time_t now;
  268. alarm(0);
  269. if ((now = time(NULL)) == 0) now = 1;
  270. for(i = 0; i < numcmd; i++) {
  271. if(inittab[i].pid < 0) { /* Start jobs */
  272. if(stopped)
  273. inittab[i].pid = -1;
  274. /*
  275. ** Do not spawn child from signal handler !
  276. ** SIGALRM would be blocked for the child
  277. */
  278. else if (signo == 0)
  279. spawn(i);
  280. }
  281. /* Check for naughty jobs */
  282. if (inittab[i].nextrun > now) {
  283. int d;
  284. d = inittab[i].nextrun - now;
  285. if (delta < 0 || d < delta)
  286. delta = d;
  287. }
  288. }
  289. if (delta > 0) {
  290. alarm(delta);
  291. }
  292. }
  293. int main(int argc, char *argv[])
  294. {
  295. int i;
  296. struct sigaction sa;
  297. /*
  298. * setup all the signal handlers here
  299. */
  300. memset(&sa, 0, sizeof(sa));
  301. /* sa.sa_flags = SA_RESETHAND we want to keep the handlers armed */
  302. sa.sa_handler = tstp_handler;
  303. sigaction(SIGTSTP, &sa, NULL);
  304. sa.sa_handler = cont_handler;
  305. sigaction(SIGCONT, &sa, NULL);
  306. sa.sa_handler = int_handler;
  307. sigaction(SIGINT, &sa, NULL);
  308. sa.sa_handler = respawn_children;
  309. sigaction(SIGALRM, &sa, NULL);
  310. sa.sa_handler = hup_handler;
  311. sigaction(SIGHUP, &sa, NULL);
  312. #if defined(CONSOLE_BAUD_RATE) && LINUX_VERSION_CODE < 0x020100
  313. set_console_baud(CONSOLE_BAUD_RATE);
  314. #endif
  315. /*
  316. * start up in single user mode if /etc/singleboot exists or if
  317. * argv[1] is "single".
  318. */
  319. if(boot_single(0, argc, argv)) enter_single();
  320. #ifdef RUN_RC
  321. /* Register console if defined by boot */
  322. #if LINUX_VERSION_CODE < 0x020100
  323. if ((console_device = getenv("CONSOLE"))) {
  324. char *sp;
  325. unsetenv("CONSOLE");
  326. if ((sp = strchr(console_device, ','))) {
  327. *sp++ = 0;
  328. set_console_baud(atoi(sp));
  329. }
  330. }
  331. make_ascii_tty();
  332. #else
  333. {
  334. struct stat st;
  335. if (isatty(1)) {
  336. have_console = 1;
  337. make_ascii_tty();
  338. } else if (fstat(1, &st) == -1 && errno == EBADF) {
  339. /* No stdout, so send everything to /dev/null */
  340. close(0); close(1); close(2);
  341. open("/dev/null", O_RDWR);
  342. dup(0);
  343. dup(0);
  344. }
  345. }
  346. #endif
  347. /*If we get a SIGTSTP before multi-user mode, do nothing*/
  348. while(stopped)
  349. pause();
  350. if(do_rc() != 0 && boot_single(1, argc, argv) && !stopped)
  351. enter_single();
  352. while(stopped) /*Also if /etc/rc fails & we get SIGTSTP*/
  353. pause();
  354. #endif
  355. /* initialize the array of commands */
  356. inittab = (struct initline *)malloc(NUMCMD * sizeof(struct initline));
  357. inittab_size = NUMCMD;
  358. if (!inittab) {
  359. /* failure case - what do you do if init fails? */
  360. err("malloc failed");
  361. _exit(1);
  362. }
  363. write_wtmp(); /* write boottime record */
  364. read_inittab();
  365. #ifdef DEBUGGING
  366. for(i = 0; i < numcmd; i++) {
  367. char **p = inittab[i].toks;
  368. printf("toks= %s %s %s %s\n",p[0], p[1], p[2], p[3]);
  369. printf("tty= %s\n", inittab[i].tty);
  370. printf("termcap= %s\n", inittab[i].termcap);
  371. }
  372. /*exit(0);*/
  373. #endif
  374. #if LINUX_VERSION_CODE < 0x020100
  375. for(i = 0; i < getdtablesize(); i++) close(i);
  376. #else
  377. /* Always leave 0, 1, and 2 connected (to /dev/null) for the child process */
  378. for(i = 3; i < getdtablesize(); i++) close(i);
  379. #endif
  380. for (;;) {
  381. pid_t pid;
  382. int vec;
  383. if (run_sigint_processing) {
  384. run_sigint_processing = 0;
  385. sigint_processing();
  386. }
  387. respawn_children(0);
  388. if (reload) {
  389. reload = 0;
  390. reload_inittab();
  391. continue; /* process all reloads before waiting */
  392. }
  393. pid = wait(&vec);
  394. alarm(0);
  395. /* clear utmp entry, and append to wtmp if possible */
  396. #if 0 /* DAVIDM */
  397. {
  398. struct utmp *ut;
  399. int ut_fd;
  400. utmpname(_PATH_UTMP);
  401. setutent();
  402. while((ut = getutent())) {
  403. if(ut->ut_pid == pid) {
  404. time(&ut->ut_time);
  405. bzero(&ut->ut_user, UT_NAMESIZE);
  406. bzero(&ut->ut_host, sizeof(ut->ut_host));
  407. ut->ut_type = DEAD_PROCESS;
  408. ut->ut_pid = 0;
  409. ut->ut_addr = 0;
  410. endutent();
  411. pututline(ut);
  412. if((ut_fd = open(_PATH_WTMP, O_APPEND|O_WRONLY)) >= 0) {
  413. flock(ut_fd, LOCK_EX|LOCK_NB);
  414. write(ut_fd, (const void *)ut, sizeof(struct utmp));
  415. flock(ut_fd, LOCK_UN|LOCK_NB);
  416. close(ut_fd);
  417. }
  418. break;
  419. }
  420. }
  421. endutent();
  422. }
  423. #endif
  424. for(i = 0; i < numcmd; i++) {
  425. if(pid == inittab[i].pid) {
  426. inittab[i].pid = -1;
  427. }
  428. }
  429. }
  430. }
  431. /*
  432. * return true if we should boot up in singleuser mode. If argv[i] is
  433. * "single" or the file /etc/singleboot exists, then singleuser mode should
  434. * be entered. If /etc/securesingle exists ask for root password first.
  435. */
  436. int boot_single(int singlearg, int argc, char *argv[])
  437. {
  438. char *pass, *rootpass = NULL;
  439. struct passwd *pwd;
  440. int i;
  441. for(i = 1; i < argc; i++) {
  442. if(argv[i] && !strcmp(argv[i], "single")) singlearg = 1;
  443. }
  444. return 0;
  445. }
  446. void spawn(int i)
  447. {
  448. pid_t pid;
  449. int j;
  450. time_t t;
  451. struct initline *it;
  452. char buf[150];
  453. it = inittab + i;
  454. t = time(NULL);
  455. /* Check for held process */
  456. if ((unsigned long)(it->nextrun - t - 1) < maxdelay)
  457. return;
  458. if (it->lastrun + testtime > t) { /* Respawning quickly */
  459. if (it->xcnt < 0xff)
  460. it->xcnt++;
  461. } else { /* Normal respawning */
  462. it->xcnt = 0;
  463. it->lastrun = t;
  464. it->delay = delaytime;
  465. }
  466. if (it->xcnt >= maxspawn) { /* Too many too quickly */
  467. strcpy(buf, it->toks[0]);
  468. strcat(buf, " respawning too fast\n");
  469. err(buf);
  470. it->pid = -1;
  471. if (it->delay >= maxdelay)
  472. it->delay = maxdelay;
  473. else if (it->delay < delaytime)
  474. it->delay = delaytime;
  475. else if((it->delay *= 2) > maxdelay)
  476. it->delay = maxdelay;
  477. it->nextrun = t + it->delay;
  478. /* Fiddle with the tracking vars to ensure that only
  479. * one attempt is made to run this next time around.
  480. */
  481. it->lastrun = it->nextrun;
  482. it->xcnt -= 2;
  483. return;
  484. }
  485. it->nextrun = t + delaytime;
  486. if((pid = vfork()) < 0) {
  487. it->pid = -1;
  488. err("fork failed\n");
  489. return;
  490. }
  491. if(pid) {
  492. /* this is the parent */
  493. it->pid = pid;
  494. return;
  495. } else {
  496. /* this is the child */
  497. char term[40];
  498. char *prog;
  499. #ifdef INCLUDE_TIMEZONE
  500. char tz[BUF_SIZ];
  501. #endif
  502. char *env[4];
  503. setsid();
  504. /* Close everything other than 0, 1 and 2 */
  505. for(j = 3; j < getdtablesize(); j++) {
  506. close(j);
  507. }
  508. /* Now set up 0, 1 and 2 */
  509. make_console(it->tty);
  510. strcpy(term, "TERM=");
  511. strcat(term, it->termcap);
  512. env[0] = term;
  513. env[1] = "PATH=/bin:/usr/bin:/etc:/sbin:/usr/sbin";
  514. #ifdef INCLUDE_TIMEZONE
  515. strcpy(tz, "TZ=");
  516. strcat(tz, tzone);
  517. env[2] = tz;
  518. env[3] = NULL;
  519. #else
  520. env[2] = NULL;
  521. #endif
  522. prog = it->toks[0];
  523. if (*prog == '-' && *(prog+1))
  524. prog++;
  525. execve(prog, it->toks, env);
  526. strcpy(buf, it->toks[0]);
  527. strcat(buf, " exec failed\n");
  528. err(buf);
  529. _exit(1);
  530. }
  531. }
  532. static void init_itab(struct initline *p) {
  533. bzero(p, sizeof(struct initline));
  534. p->pid = -1;
  535. p->nextrun = time(NULL);
  536. }
  537. static void clear_itab(struct initline *p) {
  538. if (p->line)
  539. free(p->line);
  540. if (p->fullline)
  541. free(p->fullline);
  542. if (p->toks)
  543. free(p->toks);
  544. init_itab(p);
  545. }
  546. void read_inittab(void)
  547. {
  548. int i;
  549. /*
  550. * free any old data and start again
  551. */
  552. for (i = 0; i < numcmd; i++)
  553. clear_itab(&inittab[i]);
  554. numcmd = 0;
  555. /* Fake an inittab entry if boot console defined */
  556. #ifdef CONFIG_USER_INIT_CONSOLE_SH
  557. #if LINUX_VERSION_CODE < 0x020100
  558. if (console_device && strcmp(console_device, "/dev/null"))
  559. #else
  560. if (have_console)
  561. #endif
  562. {
  563. struct initline *p;
  564. p = inittab + numcmd++;
  565. init_itab(p);
  566. p->fullline = strdup("console");
  567. strcpy(p->tty, "console");
  568. strcpy(p->termcap, "linux");
  569. add_tok(p, "-/bin/sh");
  570. }
  571. #endif
  572. i = 0;
  573. if (read_initfile(_PATH_INITTAB) == 0)
  574. i++;
  575. #ifdef CONFIG_USER_FLATFSD_FLATFSD
  576. if (read_initfile(_PATH_CONFIGTAB) == 0)
  577. i++;
  578. #endif
  579. if (i == 0) {
  580. err("Failed to open " _PATH_INITTAB
  581. #ifdef CONFIG_USER_FLATFSD_FLATFSD
  582. " or " _PATH_CONFIGTAB
  583. #endif
  584. "."
  585. );
  586. }
  587. #ifdef CONFIG_USER_INIT_CONF
  588. load_init_conf();
  589. #endif
  590. /* if needed, shrink the array using realloc -
  591. * must be done here so that we include the results of all init files
  592. * when calculating number of commands */
  593. if ((numcmd + 2) < (inittab_size - NUMCMD)) {
  594. /* round up from numcmd to the nearest multiple of NUMCMD */
  595. inittab_size = ((numcmd + 2) / NUMCMD + 1) * NUMCMD;
  596. inittab = realloc(inittab, inittab_size * sizeof(struct initline));
  597. if (!inittab) {
  598. /* failure case - what do you do if init fails? */
  599. err("malloc failed");
  600. _exit(1);
  601. }
  602. }
  603. if (numcmd == 0)
  604. _exit(1);
  605. }
  606. static int
  607. read_initfile(const char *initfile)
  608. {
  609. struct initline *p;
  610. FILE *f;
  611. char *buf = NULL;
  612. size_t buf_len = 0;
  613. int i,j,k;
  614. char *ptr, *getty;
  615. #ifdef SPECIAL_CONSOLE_TERM
  616. char tty[50];
  617. struct stat stb;
  618. char *termenv;
  619. termenv = getenv("TERM"); /* set by kernel */
  620. #endif
  621. i = numcmd;
  622. if (!(f = fopen(initfile, "r")))
  623. return 1;
  624. while(!feof(f)) {
  625. if (i+2 == inittab_size) {
  626. /* need to realloc inittab */
  627. inittab_size += NUMCMD;
  628. inittab = realloc(inittab, inittab_size * sizeof(struct initline));
  629. if (!inittab) {
  630. /* failure case - what do you do if init fails? */
  631. err("malloc failed");
  632. _exit(1);
  633. }
  634. }
  635. if (getline(&buf, &buf_len, f) == -1) break;
  636. for(k = 0; k < buf_len && buf[k]; k++) {
  637. if(buf[k] == '#') {
  638. buf[k] = '\0'; break;
  639. }
  640. }
  641. if(buf[0] == '\0' || buf[0] == '\n') continue;
  642. p = inittab + i;
  643. init_itab(p);
  644. p->line = strdup(buf);
  645. p->fullline = strdup(buf);
  646. if (!p->line || !p->fullline) {
  647. err("Not memory to allocate inittab entry");
  648. clear_itab(p);
  649. continue;
  650. }
  651. ptr = strtok(p->line, ":");
  652. if (!ptr) {
  653. err("Missing TTY/ID field in inittab");
  654. clear_itab(p);
  655. continue;
  656. }
  657. strncpy(p->tty, ptr, 9);
  658. //p->tty[9] = '\0';
  659. ptr = strtok(NULL, ":");
  660. if (!ptr) {
  661. err("Missing TERMTYPE field in inittab");
  662. clear_itab(p);
  663. continue;
  664. }
  665. strncpy(p->termcap, ptr, 29);
  666. //p->termcap[29] = '\0';
  667. getty = strtok(NULL, " \t\n");
  668. if (!getty) {
  669. err("Missing PROCESS field in inittab");
  670. clear_itab(p);
  671. continue;
  672. }
  673. add_tok(p, getty);
  674. j = 1;
  675. while((ptr = strtok(NULL, " \t\n")))
  676. add_tok(p, ptr);
  677. #ifdef SPECIAL_CONSOLE_TERM
  678. /* special-case termcap for the console ttys */
  679. strcpy(tty, "/dev/");
  680. strcat(tty, p->tty);
  681. if(!termenv || stat(tty, &stb) < 0) {
  682. err("no TERM or cannot stat tty\n");
  683. } else {
  684. /* is it a console tty? */
  685. if(major(stb.st_rdev) == 4 && minor(stb.st_rdev) < 64) {
  686. strncpy(p->termcap, termenv, 30);
  687. p->termcap[29] = 0;
  688. }
  689. }
  690. #endif
  691. i++;
  692. }
  693. if (buf)
  694. free(buf);
  695. fclose(f);
  696. numcmd = i;
  697. return 0;
  698. }
  699. void hup_handler()
  700. {
  701. reload = 1;
  702. }
  703. void reload_inittab()
  704. {
  705. int i;
  706. int oldnum;
  707. char ** saveline = (char **) malloc(inittab_size * sizeof(char *));
  708. pid_t * savepid = (pid_t*) malloc(inittab_size * sizeof(pid_t));
  709. if (!saveline || !savepid) {
  710. /* another failure case - what DO you do if init fails */
  711. err("malloc failed");
  712. _exit(1);
  713. }
  714. for (i=0; i<numcmd; i++) {
  715. savepid[i] = inittab[i].pid;
  716. saveline[i] = strdup(inittab[i].fullline);
  717. if (!saveline[i]) {
  718. err("malloc failed");
  719. _exit(1);
  720. }
  721. }
  722. oldnum = numcmd;
  723. read_inittab();
  724. /* See which ones still exist */
  725. for(i = 0; i < numcmd; i++) {
  726. int j;
  727. for(j = 0; j < oldnum; j++) {
  728. if(strcmp(saveline[j], inittab[i].fullline) == 0) {
  729. inittab[i].pid = savepid[j];
  730. savepid[j] = -1;
  731. break;
  732. }
  733. }
  734. }
  735. /* Kill off processes no longer needed and free memory */
  736. for(i = 0; i < oldnum; i++) {
  737. if (savepid[i] > 1)
  738. kill(savepid[i], SIGTERM);
  739. free(saveline[i]);
  740. }
  741. free(saveline);
  742. free(savepid);
  743. }
  744. void tstp_handler()
  745. {
  746. stopped++;
  747. }
  748. void cont_handler()
  749. {
  750. stopped = 0;
  751. }
  752. void int_handler()
  753. {
  754. run_sigint_processing = 1;
  755. }
  756. void sigint_processing()
  757. {
  758. /*
  759. * After Linux 0.96b PL1, we get a SIGINT when
  760. * the user presses Ctrl-Alt-Del...
  761. */
  762. int pid;
  763. sync();
  764. sync();
  765. if((pid = vfork()) == 0) {
  766. char *av[2];
  767. extern char **environ;
  768. /* reboot properly... */
  769. av[0] = _PATH_REBOOT;
  770. av[1] = NULL;
  771. execve(_PATH_REBOOT, av, environ);
  772. #if __GNU_LIBRARY__ > 5
  773. reboot(0x1234567);
  774. #else
  775. reboot(0xfee1dead, 672274793, 0x1234567);
  776. #endif
  777. _exit(2);
  778. } else if(pid < 0) {
  779. /* fork failed, try the hard way... */
  780. #if __GNU_LIBRARY__ > 5
  781. reboot(0x1234567);
  782. #else
  783. reboot(0xfee1dead, 672274793, 0x1234567);
  784. #endif
  785. }
  786. }
  787. #ifdef INCLUDE_TIMEZONE
  788. void set_tz(void)
  789. {
  790. FILE *f;
  791. int len;
  792. if((f = fopen("/etc/config/TZ", "r")) == NULL &&
  793. (f = fopen("/etc/TZ", "r")) == NULL)
  794. return;
  795. fgets(tzone, BUF_SIZ-2, f);
  796. fclose(f);
  797. if((len=strlen(tzone)) < 2)
  798. return;
  799. tzone[len-1] = 0; /* get rid of the '\n' */
  800. setenv("TZ", tzone, 0);
  801. }
  802. #endif
  803. #ifdef CONFIG_USER_INIT_CONF
  804. void load_init_conf(void)
  805. {
  806. char line[BUF_SIZ];
  807. FILE *f;
  808. if ((f = fopen("/etc/config/init.conf", "r")) == NULL &&
  809. (f = fopen("/etc/init.conf", "r")) == NULL)
  810. return;
  811. while (fgets(line, sizeof(line) - 2, f)) {
  812. if (strncasecmp(line, "delaytime=", 10) == 0)
  813. delaytime = atoi(line + 10);
  814. if (strncasecmp(line, "maxdelay=", 9) == 0)
  815. maxdelay = atoi(line + 9);
  816. if (strncasecmp(line, "maxspawn=", 9) == 0)
  817. maxspawn = atoi(line + 9);
  818. if (strncasecmp(line, "testtime=", 9) == 0)
  819. testtime = atoi(line + 9);
  820. }
  821. fclose(f);
  822. }
  823. #endif
  824. void write_wtmp(void)
  825. {
  826. #if 0
  827. int fd;
  828. struct utmp ut;
  829. bzero((char *)&ut, sizeof(ut));
  830. strcpy(ut.ut_line, "~");
  831. bzero(ut.ut_name, sizeof(ut.ut_name));
  832. time(&ut.ut_time);
  833. ut.ut_type = BOOT_TIME;
  834. if((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND)) >= 0) {
  835. flock(fd, LOCK_EX|LOCK_NB); /* make sure init won't hang */
  836. write(fd, (char *)&ut, sizeof(ut));
  837. flock(fd, LOCK_UN|LOCK_NB);
  838. close(fd);
  839. }
  840. #endif
  841. }
  842. void make_ascii_tty(void)
  843. {
  844. struct termios tty;
  845. const char *pt;
  846. if (tcgetattr(0, &tty) < 0)
  847. return;
  848. tty.c_iflag &= ~(INLCR|IGNCR|IUCLC);
  849. tty.c_iflag |= ICRNL;
  850. tty.c_oflag &= ~(OCRNL|OLCUC|ONOCR|ONLRET|OFILL);
  851. tty.c_oflag |= OPOST|ONLCR;
  852. tty.c_cflag |= CLOCAL;
  853. tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE;
  854. #ifdef IEXTEN
  855. tty.c_lflag |= IEXTEN;
  856. #endif
  857. #if LINUX_VERSION_CODE < 0x020100
  858. if (console_baud != -1)
  859. cfsetospeed(&tty, console_baud);
  860. #endif
  861. tty.c_cc[VINTR] = CTRL('C');
  862. tty.c_cc[VQUIT] = CTRL('\\');
  863. tty.c_cc[VERASE] = CTRL('H'); /*127*/
  864. tty.c_cc[VKILL] = CTRL('U'); /*Changed from non-standard ^X*/
  865. tty.c_cc[VEOF] = CTRL('D');
  866. tty.c_cc[VTIME] = 0;
  867. tty.c_cc[VMIN] = 1;
  868. tty.c_cc[VSTART] = CTRL('Q');
  869. tty.c_cc[VSTOP] = CTRL('S');
  870. tty.c_cc[VSUSP] = CTRL('Z');
  871. #ifdef VWERASE
  872. tty.c_cc[VWERASE] = CTRL('W');
  873. #endif
  874. /* Pick up simple environment setting of VERASE.
  875. * Useful for setting on kernel command line.
  876. * e.g. TTYERASE=^?
  877. */
  878. pt = getenv("TTYERASE");
  879. if (pt && pt[0] == '^' && pt[1]) {
  880. tty.c_cc[VERASE] = (pt[1] == '?') ? 127 : CTRL(pt[1]);
  881. }
  882. tcsetattr(0, TCSANOW, &tty);
  883. }
  884. void make_console(const char *tty)
  885. {
  886. char devname[32];
  887. close(0); close(1); close(2);
  888. if (tty && *tty) {
  889. #if LINUX_VERSION_CODE < 0x020100
  890. /*
  891. * until we get proper console support under 2.0
  892. */
  893. if (strcmp(tty, "console") == 0) {
  894. strcpy(devname, console_device);
  895. }
  896. else
  897. #endif
  898. {
  899. strcpy(devname, "/dev/");
  900. strcat(devname, tty);
  901. }
  902. /* Try to open the specified console */
  903. if (open(devname, O_RDWR|O_NONBLOCK) >= 0) {
  904. fcntl(0, F_SETFL, 0);
  905. dup(0);
  906. dup(0);
  907. make_ascii_tty();
  908. ioctl(0, TIOCSCTTY, (char*)0);
  909. return;
  910. }
  911. }
  912. /* No go, so send to /dev/null */
  913. open("/dev/null", O_RDWR|O_NONBLOCK);
  914. dup(0);
  915. dup(0);
  916. }