1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
|
/*
* Init A System-V Init Clone.
*
* Usage: /sbin/init
* init [0123456SsQqAaBbCc]
* telinit [0123456SsQqAaBbCc]
*
* Version: @(#)init.c 2.75-2 02-Jun-1998 miquels@cistron.nl
*
* This file is part of the sysvinit suite,
* Copyright 1991-1998 Miquel van Smoorenburg.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Modified: 21 Feb 1998, Al Viro:
* 'U' flag added to telinit. It forces init to re-exec itself
* (passing its state through exec, certainly).
* May be useful for smoother (heh) upgrades.
* 24 Feb 1998, AV:
* did_boot made global and added to state - thanks, Miquel.
* Yet another file descriptors leak - close state pipe if
* re_exec fails.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/kd.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <termios.h>
#include <utmp.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/syslog.h>
#include <sys/time.h>
#include "init.h"
#include "initreq.h"
#include "paths.h"
#include "reboot.h"
#include "set.h"
#ifndef SIGPWR
# define SIGPWR SIGUSR2
#endif
/* Set a signal handler. */
#define SETSIG(sa, sig, fun) sa.sa_handler = fun; \
sa.sa_flags = SA_RESTART; \
sigaction(sig, &sa, NULL);
/* Version information */
char *Version = "@(#) init 2.75-2 02-Jun-1998 miquels@cistron.nl";
char *bootmsg = "version 2.75 booting";
#define E_VERSION "INIT_VERSION=sysvinit-2.75"
CHILD *family = NULL; /* The linked list of all entries */
CHILD *newFamily = NULL; /* The list after inittab re-read */
CHILD ch_emerg = { /* Emergency shell */
0, 0, 0, 0, 0,
"~~",
"S",
3,
"/sbin/sulogin",
NULL,
NULL
};
char runlevel = 'S'; /* The current run level */
char thislevel = 'S'; /* The current runlevel */
char prevlevel = 'N'; /* Previous runlevel */
int dfl_level = 0; /* Default runlevel */
int got_cont = 0; /* Set if we received the SIGCONT signal */
int emerg_shell = 0; /* Start emergency shell? */
unsigned int got_signals; /* Set if we received a signal. */
int wrote_reboot = 1; /* Set when we wrote the reboot record */
int sltime = 5; /* Sleep time between TERM and KILL */
char *argv0; /* First arguments; show up in ps listing */
int maxproclen; /* Maximal length of argv[0] */
struct utmp utproto; /* Only used for sizeof(utproto.ut_id) */
char *console_dev = CONSOLE; /* Console device. */
int wrote_iosave = 0; /* Did we write ioctl.save yet? */
int pipe_fd = -1; /* /dev/initctl */
int did_boot = 0; /* Did we already do BOOT* stuff? */
/* Used by re-exec part */
int reload = 0; /* Should we do initialization stuff? */
char *myname="/sbin/init"; /* What should we exec */
int oops_error; /* Used by some of the re-exec code. */
char *Signature = "12567362"; /* Signature for re-exec fd */
/* Macro to see if this is a special action */
#define ISPOWER(i) ((i) == POWERWAIT || (i) == POWERFAIL || \
(i) == POWEROKWAIT || (i) == POWERFAILNOW || \
(i) == CTRLALTDEL)
/* ascii values for the `action' field. */
struct actions {
char *name;
int act;
} actions[] = {
{ "respawn", RESPAWN },
{ "wait", WAIT },
{ "once", ONCE },
{ "boot", BOOT },
{ "bootwait", BOOTWAIT },
{ "powerfail", POWERFAIL },
{ "powerfailnow",POWERFAILNOW },
{ "powerwait", POWERWAIT },
{ "powerokwait", POWEROKWAIT },
{ "ctrlaltdel", CTRLALTDEL },
{ "off", OFF },
{ "ondemand", ONDEMAND },
{ "initdefault", INITDEFAULT },
{ "sysinit", SYSINIT },
{ "kbrequest", KBREQUEST },
{ NULL, 0 },
};
/*
* State parser token table (see receive_state)
*/
struct {
char name[4];
int cmd;
} cmds[] = {
{ "VER", C_VER },
{ "END", C_END },
{ "REC", C_REC },
{ "EOR", C_EOR },
{ "LEV", C_LEV },
{ "FL ", C_FLAG },
{ "AC ", C_ACTION },
{ "CMD", C_PROCESS },
{ "PID", C_PID },
{ "EXS", C_EXS },
{ "-RL", D_RUNLEVEL },
{ "-TL", D_THISLEVEL },
{ "-PL", D_PREVLEVEL },
{ "-SI", D_GOTSIGN },
{ "-WR", D_WROTE_REBOOT},
{ "-ST", D_SLTIME },
{ "-DB", D_DIDBOOT },
{ "", 0 }
};
struct {
char *name;
int mask;
} flags[]={
{"RU",RUNNING},
{"DE",DEMAND},
{"XD",XECUTED},
{NULL,0}
};
/*
* Sleep a number of seconds
*/
void do_sleep(int sec)
{
int old_alrm;
int f;
int ga;
ga = ISMEMBER(got_signals, SIGALRM);
/*
* Save old alarm time and wait 'sec' seconds.
*/
old_alrm = alarm(0);
for(f = 0; f < sec; f++) {
DELSET(got_signals, SIGALRM);
alarm(1);
while(ISMEMBER(got_signals, SIGALRM) == 0) pause();
}
/*
* Reset old values of got_alrm flag and the timer
*/
if (ga)
ADDSET(got_signals, SIGALRM);
else
DELSET(got_signals, SIGALRM);
if (old_alrm)
alarm(old_alrm - sec > 0 ? old_alrm - sec : 1);
}
/*
* Send the state info of the previous running init to
* the new one, in a version-independant way.
*/
void send_state(int fd)
{
FILE *fp;
CHILD *p;
int i,val;
fp = fdopen(fd,"w");
fprintf(fp, "VER%s\n", Version);
fprintf(fp, "-RL%c\n", runlevel);
fprintf(fp, "-TL%c\n", thislevel);
fprintf(fp, "-PL%c\n", prevlevel);
fprintf(fp, "-SI%u\n", got_signals);
fprintf(fp, "-WR%d\n", wrote_reboot);
fprintf(fp, "-ST%d\n", sltime);
fprintf(fp, "-DB%d\n", did_boot);
for (p = family; p; p = p->next) {
fprintf(fp, "REC%s\n", p->id);
fprintf(fp, "LEV%s\n", p->rlevel);
for (i = 0, val = p->flags; flags[i].mask; i++)
if (val & flags[i].mask) {
val &= ~flags[i].mask;
fprintf(fp, "FL %s\n",flags[i].name);
}
fprintf(fp, "PID%d\n",p->pid);
fprintf(fp, "EXS%u\n",p->exstat);
for(i = 0; actions[i].act; i++)
if (actions[i].act == p->action) {
fprintf(fp, "AC %s\n", actions[i].name);
break;
}
fprintf(fp, "CMD%s\n", p->process);
fprintf(fp, "EOR\n");
}
fprintf(fp, "END\n");
fclose(fp);
}
/*
* Read a string from a file descriptor.
* FIXME: why not use fgets() ?
*/
static int get_string(char *p, int size, FILE *f)
{
int c;
while ((c = getc(f)) != EOF && c != '\n') {
if (--size > 0)
*p++ = c;
}
*p = '\0';
return (c != EOF) && (size > 0);
}
/*
* Read trailing data from the state pipe until we see a newline.
*/
static int get_void(FILE *f)
{
int c;
while ((c = getc(f)) != EOF && c != '\n')
;
return (c != EOF);
}
/*
* Read the next "command" from the state pipe.
*/
static int get_cmd(FILE *f)
{
char cmd[4] = " ";
int i;
if (fread(cmd, 1, 3, f) != 3)
return C_EOF;
for(i = 0; cmds[i].cmd && strcmp(cmds[i].name, cmd) != 0; i++)
;
return cmds[i].cmd;
}
/*
* Read a CHILD * from the state pipe.
*/
static CHILD *get_record(FILE *f)
{
int cmd;
char s[32];
int i;
CHILD *p;
do {
switch (cmd = get_cmd(f)) {
case C_END:
get_void(f);
return NULL;
case 0:
get_void(f);
break;
case C_REC:
break;
case D_RUNLEVEL:
fscanf(f, "%c\n", &runlevel);
break;
case D_THISLEVEL:
fscanf(f, "%c\n", &thislevel);
break;
case D_PREVLEVEL:
fscanf(f, "%c\n", &prevlevel);
break;
case D_GOTSIGN:
fscanf(f, "%u\n", &got_signals);
break;
case D_WROTE_REBOOT:
fscanf(f, "%d\n", &wrote_reboot);
break;
case D_SLTIME:
fscanf(f, "%d\n", &sltime);
break;
case D_DIDBOOT:
fscanf(f, "%d\n", &did_boot);
break;
default:
if (cmd > 0 || cmd == C_EOF) {
oops_error = -1;
return NULL;
}
}
} while (cmd != C_REC);
while ((p = (CHILD *)malloc(sizeof(CHILD))) == NULL ) {
log(L_VB, "out of memory");
do_sleep(5);
}
memset(p, 0, sizeof(CHILD));
get_string(p->id, sizeof(p->id), f);
do switch(cmd = get_cmd(f)) {
case 0:
case C_EOR:
get_void(f);
break;
case C_PID:
fscanf(f, "%d\n", &(p->pid));
break;
case C_EXS:
fscanf(f, "%u\n", &(p->exstat));
break;
case C_LEV:
get_string(p->rlevel, sizeof(p->rlevel), f);
break;
case C_PROCESS:
get_string(p->process, sizeof(p->process), f);
break;
case C_FLAG:
get_string(s, 32, f);
for(i = 0; flags[i].name; i++) {
if (strcmp(flags[i].name,s) == 0)
break;
}
p->flags |= flags[i].mask;
break;
case C_ACTION:
get_string(s, 32, f);
for(i = 0; actions[i].name; i++) {
if (strcmp(actions[i].name, s) == 0)
break;
}
p->action = actions[i].act ? : OFF;
break;
default:
free(p);
oops_error = -1;
return NULL;
} while( cmd != C_EOR);
return p;
}
/*
* Read the complete state info from the state pipe.
* Returns 0 on success
*/
int receive_state(int fd)
{
FILE *f;
char old_version[256];
CHILD **pp;
f = fdopen(fd, "r");
if (get_cmd(f) != C_VER)
return -1;
get_string(old_version, 256, f);
oops_error = 0;
for (pp = &family; (*pp = get_record(f)) != NULL; pp = &((*pp)->next))
;
fclose(f);
return oops_error;
}
/*
* Set the process title. We do not check for overflow of
* the stack space since we know there is plenty for
* our needs and we'll never use more than 10 bytes anyway.
*/
int setproctitle(char *fmt, ...)
{
va_list ap;
int len;
char buf[256];
buf[0] = 0;
va_start(ap, fmt);
len = vsprintf(buf, fmt, ap);
va_end(ap);
memset(argv0, 0, maxproclen + 1);
strncpy(argv0, buf, maxproclen);
return len;
}
/*
* Open a device with retries.
*/
int serial_open(char *dev, int mode)
{
int f, fd;
int m;
/*
* Open device in nonblocking mode.
*/
m = mode | O_NONBLOCK;
/*
* Retry the open five times.
*/
for(f = 0;f < 5; f++)
if ((fd = open(dev, m)) >= 0) break;
if (fd < 0) return(fd);
/*
* Set original flags.
*/
if (m != mode)
fcntl(fd, F_SETFL, mode);
return(fd);
}
/*
* We got a signal (HUP PWR WINCH ALRM INT)
*/
void signal_handler(int sig)
{
ADDSET(got_signals, sig);
}
/*
* SIGCHLD: one of our children has died.
*/
void chld_handler()
{
CHILD *ch;
int pid, st;
/*
* Find out which process(es) this was (were)
*/
while((pid = waitpid(-1, &st, WNOHANG)) != 0) {
if (errno == ECHILD) break;
for( ch = family; ch; ch = ch->next )
if ( ch->pid == pid && (ch->flags & RUNNING) ) {
#if DEBUG
log(L_VB, "chld_handler: marked %d as zombie",
ch->pid);
#endif
ADDSET(got_signals, SIGCHLD);
ch->exstat = st;
ch->flags |= ZOMBIE;
if (ch->new) {
ch->new->exstat = st;
ch->new->flags |= ZOMBIE;
}
break;
}
#if DEBUG
if (ch == NULL)
log(L_VB, "chld_handler: unknown child %d exited.",
pid);
#endif
}
}
/*
* Linux ignores all signals sent to init when the
* SIG_DFL handler is installed. Therefore we must catch SIGTSTP
* and SIGCONT, or else they won't work....
*
* The SIGCONT handler
*/
void cont_handler()
{
got_cont = 1;
}
/*
* OOPS: segmentation violation!
*/
void segv_handler()
{
log(L_VB, "PANIC: segmentation violation! giving up..");
while(1) pause();
}
/*
* The SIGSTOP & SIGTSTP handler
*/
void stop_handler()
{
got_cont = 0;
while(!got_cont) pause();
got_cont = 0;
}
/*
* Set terminal settings to reasonable defaults
*/
void set_term(int how)
{
struct termios tty;
int fd, fd2;
int restore_ok = 0;
if ((fd = serial_open(console_dev, O_RDWR|O_NOCTTY)) < 0) {
log(L_VB, "can't open %s", console_dev);
return;
}
/*
* Do we want to save or restore modes.
*/
if (how > 0) {
/*
* Save the terminal settings.
*/
(void) ioctl(fd, TCGETS, &tty);
if ((fd2 = open(IOSAVE, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
if (errno != EROFS)
log(L_VB, "can't open(%s, O_WRONLY): %s",
IOSAVE, strerror(errno));
(void) close(fd);
(void) close(fd2);
return;
}
(void) write(fd2, &tty, sizeof(struct termios));
wrote_iosave = 1;
(void) close(fd);
(void) close(fd2);
return;
}
/*
* Restore the terminal settings.
*/
if (wrote_iosave && (fd2 = open(IOSAVE, O_RDONLY)) >= 0) {
if (read(fd2, &tty, sizeof(tty)) == sizeof(tty))
restore_ok = 1;
close(fd2);
}
if (restore_ok == 0) {
/*
* No old settings - create some resonable defaults.
*/
(void) ioctl(fd, TCGETS, &tty);
tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
tty.c_cflag |= HUPCL|CLOCAL;
tty.c_cc[VINTR] = 3;
tty.c_cc[VQUIT] = 28;
tty.c_cc[VERASE] = 127;
tty.c_cc[VKILL] = 24;
tty.c_cc[VEOF] = 4;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_cc[VSTART] = 17;
tty.c_cc[VSTOP] = 19;
tty.c_cc[VSUSP] = 26;
}
/*
* Set pre and post processing
*/
tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
tty.c_oflag = OPOST|ONLCR;
tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
/*
* Now set the terminal line.
*/
(void) ioctl(fd, TCSETS, &tty);
(void) close(fd);
}
/*
* Print to the system console
*/
void print(char *s)
{
int fd;
if ((fd = serial_open(console_dev, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
write(fd, s, strlen(s));
close(fd);
}
}
/*
* Log something to a logfile and the console.
*/
void log(int loglevel, char *s, ...)
{
va_list va_alist;
char buf[256];
va_start(va_alist, s);
vsprintf(buf, s, va_alist);
va_end(va_alist);
if (loglevel & L_SY) {
/*
* Re-etablish connection with syslogd every time.
*/
openlog("init", 0, LOG_DAEMON);
syslog(LOG_INFO, buf);
/* closelog(); NOT needed with recent libc's. */
}
/*
* And log to the console.
*/
if (loglevel & L_CO) {
print("\rINIT: ");
print(buf);
print("\r\n");
}
}
/*
* See if one character of s2 is in s1
*/
int any(char *s1, char *s2)
{
while(*s2)
if (strchr(s1, *s2++) != NULL)
return(1);
return(0);
}
/*
* Fork and execute.
*
* This function is too long and indents too deep.
*
*/
int spawn(CHILD *ch, int *res)
{
char *args[16]; /* Argv array */
char buf[136]; /* Line buffer */
int f, st; /* Scratch variables */
char *ptr; /* Ditto */
time_t t; /* System time */
int oldAlarm; /* Previous alarm value */
char *proc = ch->process; /* Command line */
char i_lvl[] = "RUNLEVEL=x"; /* Runlevel in environment. */
char i_prev[] = "PREVLEVEL=x";/* Previous runlevel. */
char i_cons[32]; /* console device. */
pid_t pid, pgrp; /* child, console process group. */
sigset_t nmask, omask; /* For blocking SIGCHLD */
*res = -1;
/* Skip '+' if it's there */
if (proc[0] == '+') proc++;
ch->flags |= XECUTED;
if (ch->action == RESPAWN || ch->action == ONDEMAND) {
/* Is the date stamp from less than 2 minutes ago? */
time(&t);
if (ch->tm + TESTTIME > t) {
ch->count++;
} else {
ch->count = 0;
ch->tm = t;
}
/* Do we try to respawn too fast? */
if (ch->count >= MAXSPAWN) {
log(L_VB, "Id \"%s\" respawning too fast: disabled for %d minutes",
ch->id, SLEEPTIME / 60);
ch->flags &= ~RUNNING;
ch->flags |= FAILING;
/* Remember the time we stopped */
ch->tm = t;
/* Try again in 5 minutes */
oldAlarm = alarm(0);
if (oldAlarm > SLEEPTIME || oldAlarm <= 0) oldAlarm = SLEEPTIME;
alarm(oldAlarm);
return(-1);
}
}
/* See if there is an "initscript" (except in single user mode). */
if (access(INITSCRIPT, R_OK) == 0 && runlevel != 'S') {
/* Build command line using "initscript" */
args[1] = SHELL;
args[2] = INITSCRIPT;
args[3] = ch->id;
args[4] = ch->rlevel;
args[5] = "unknown";
for(f = 0; actions[f].name; f++) {
if (ch->action == actions[f].act) {
args[5] = actions[f].name;
break;
}
}
args[6] = proc;
args[7] = NULL;
} else if (any(proc, "~`!$^&*()=|\\{}[];\"'<>?")) {
/* See if we need to fire off a shell for this command */
/* Give command line to shell */
args[1] = SHELL;
args[2] = "-c";
strcpy(buf, "exec ");
strcat(buf, proc);
args[3] = buf;
args[4] = NULL;
} else {
/* Split up command line arguments */
strcpy(buf, proc);
ptr = buf;
for(f = 1; f < 15; f++) {
/* Skip white space */
while(*ptr == ' ' || *ptr == '\t') ptr++;
args[f] = ptr;
/* May be trailing space.. */
if (*ptr == 0) break;
/* Skip this `word' */
while(*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#')
ptr++;
/* If end-of-line, break */
if (*ptr == '#' || *ptr == 0) {
f++;
*ptr = 0;
break;
}
/* End word with \0 and continue */
*ptr++ = 0;
}
args[f] = NULL;
}
args[0] = args[1];
while(1) {
/*
* Block sigchild while forking.
*/
sigemptyset(&nmask);
sigaddset(&nmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &nmask, &omask);
if ((pid = fork()) == 0) {
close(0);
close(1);
close(2);
if (pipe_fd >= 0) close(pipe_fd);
sigprocmask(SIG_SETMASK, &omask, NULL);
/* Now set RUNLEVEL and PREVLEVEL */
sprintf(i_cons, "CONSOLE=%s", console_dev);
i_lvl[9] = thislevel;
i_prev[10] = prevlevel;
putenv(i_lvl);
putenv(i_prev);
putenv(i_cons);
putenv(E_VERSION);
/*
* In sysinit, boot, bootwait or single user mode:
* any wait-type subprocess gets the console
* as its controlling tty (if not in use yet).
*/
if (strchr("*#sS", runlevel) && ch->flags & WAITING) {
/*
* We fork once extra. This is so that we can
* wait and change the process group and session
* of the console after exit of the leader.
*/
setsid();
f = serial_open(console_dev, O_RDWR|O_NOCTTY);
(void)ioctl(f, TIOCSCTTY, 0);
dup(f);
dup(f);
if ((pid = fork()) < 0) {
log(L_VB, "cannot fork");
exit(1);
}
if (pid > 0) {
/*
* Ignore keyboard signals etc.
*/
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGCHLD, SIG_DFL);
while(waitpid(pid, &st, 0) != pid)
;
pgrp = 0;
(void)ioctl(f, TIOCGPGRP, &pgrp);
if (pgrp != getpid())
exit(0);
/*
* De-attach session from controlling tty.
* Or actually, the other way around.
* This _without_ SIGHUP etc.
*/
if ((pid = fork()) < 0) {
log(L_VB, "cannot fork");
exit(1);
}
if (pid == 0) {
setsid();
(void)ioctl(f, TIOCSCTTY, 1);
exit(0);
}
while(waitpid(pid, &st, 0) != pid)
;
exit(0);
}
/* Set ioctl settings to default ones */
set_term(0);
} else {
if ((f = serial_open(console_dev, O_RDWR|O_NOCTTY)) < 0) {
log(L_VB, "open(%s): %s", console_dev,
strerror(errno));
f = open("/dev/null", O_RDWR);
}
dup(f);
dup(f);
setsid();
}
/* Reset all the signals */
for(f = 1; f < NSIG; f++) signal(f, SIG_DFL);
execvp(args[1], args + 1);
/*
* Is this a bug in execvp? It does _not_ execute shell
* scripts (/etc/rc !), so we try again with
* 'sh -c exec ...'
*/
if (errno == ENOEXEC) {
args[1] = SHELL;
args[2] = "-c";
strcpy(buf, "exec ");
strcat(buf, proc);
args[3] = buf;
args[4] = NULL;
execvp(args[1], args + 1);
}
log(L_VB, "cannot execute \"%s\"", args[1]);
exit(1);
}
*res = pid;
sigprocmask(SIG_SETMASK, &omask, NULL);
#if DEBUG
log(L_VB, "Started id %s (pid %d)", ch->id, pid);
#endif
if (pid == -1) {
log(L_VB, "cannot fork, retry..", NULL, NULL);
do_sleep(5);
continue;
}
return(pid);
}
}
/*
* Start a child running!
*/
void startup(CHILD *ch)
{
/*
* See if it's disabled
*/
if (ch->flags & FAILING) return;
switch(ch->action) {
case SYSINIT:
case BOOTWAIT:
case WAIT:
case POWERWAIT:
case POWERFAILNOW:
case POWEROKWAIT:
case CTRLALTDEL:
if (!(ch->flags & XECUTED)) ch->flags |= WAITING;
case KBREQUEST:
case BOOT:
case POWERFAIL:
case ONCE:
if (ch->flags & XECUTED) break;
case ONDEMAND:
case RESPAWN:
ch->flags |= RUNNING;
if (spawn(ch, &(ch->pid)) < 0) break;
/*
* Do NOT log if process field starts with '+'
* FIXME: that's for compatibility with *very*
* old getties - probably it can be taken out.
*/
if (ch->process[0] != '+')
write_utmp_wtmp("", ch->id, ch->pid,
INIT_PROCESS, "");
break;
}
}
/*
* My version of strtok(3).
*/
char *get_part(char *str, int tok)
{
static char *s;
char *p, *q;
if (str != NULL)
s = str;
if (s == NULL || *s == 0)
return(NULL);
q = p = s;
while(*p != tok && *p)
p++;
if (*p == tok)
*p++ = 0;
s = p;
return q;
}
/*
* Read the inittab file.
*/
void read_inittab(void)
{
FILE *fp; /* The INITTAB file */
char buf[256]; /* Line buffer */
char err[64]; /* Error message. */
char *id, *rlevel,
*action, *process; /* Fields of a line */
char *p;
CHILD *ch, *old, *i; /* Pointers to CHILD structure */
CHILD *head = NULL; /* Head of linked list */
int lineNo = 0; /* Line number in INITTAB file */
int actionNo; /* Decoded action field */
int f; /* Counter */
int round; /* round 0 for SIGTERM, round 1 for SIGKILL */
int foundOne = 0; /* No killing no sleep */
int talk; /* Talk to the user */
int done = 0; /* Ready yet? */
sigset_t nmask, omask; /* For blocking SIGCHLD. */
#ifdef INITLVL
struct stat st; /* To stat INITLVL */
#endif
#if DEBUG
if (newFamily != NULL) {
log(L_VB, "PANIC newFamily != NULL");
exit(1);
}
log(L_VB, "Reading inittab");
#endif
/*
* Open INITTAB and real line by line.
*/
if ((fp = fopen(INITTAB, "r")) == NULL)
log(L_VB, "No inittab file found");
while(!done) {
/*
* Add single user shell entry at the end.
*/
if (fp == NULL || fgets(buf, 255, fp) == NULL) {
done = 1;
/*
* See if we have a single user entry.
*/
for(old = newFamily; old; old = old->next)
if (strcmp(old->rlevel, "S") == 0) break;
if (old == NULL)
sprintf(buf, "~~:S:wait:%s\n", SHELL);
else
continue;
}
lineNo++;
/*
* Skip comments and empty lines
*/
for(p = buf; *p == ' ' || *p == '\t'; p++)
;
if (*p == '#' || *p == '\n') continue;
/*
* Decode the fields
*/
id = get_part(p, ':');
rlevel = get_part(NULL, ':');
action = get_part(NULL, ':');
process = get_part(NULL, '\n');
/*
* Check if syntax is OK. Be very verbose here, to
* avoid newbie postings on comp.os.linux.setup :)
*/
err[0] = 0;
if (!id || !*id) strcpy(err, "missing id field");
if (!rlevel) strcpy(err, "missing runlevel field");
if (!process) strcpy(err, "missing process field");
if (!action || !*action)
strcpy(err, "missing action field");
if (id && strlen(id) > sizeof(utproto.ut_id))
sprintf(err, "id field too long (max %d characters)",
(int)sizeof(utproto.ut_id));
if (rlevel && strlen(rlevel) > 11)
strcpy(err, "rlevel field too long (max 11 characters)");
if (process && strlen(process) > 127)
strcpy(err, "process field too long");
if (action && strlen(action) > 32)
strcpy(err, "action field too long");
if (err[0] != 0) {
log(L_VB, "%s[%d]: %s", INITTAB, lineNo, err);
#if DEBUG
log(L_VB, "%s:%s:%s:%s", id, rlevel, action, process);
#endif
continue;
}
/*
* Decode the "action" field
*/
actionNo = -1;
for(f = 0; actions[f].name; f++)
if (strcasecmp(action, actions[f].name) == 0) {
actionNo = actions[f].act;
break;
}
if (actionNo == -1) {
log(L_VB, "%s[%d]: %s: unknown action field",
INITTAB, lineNo, action);
continue;
}
/*
* See if the id field is unique
*/
for(old = newFamily; old; old = old->next) {
if(strcmp(old->id, id) == 0 && strcmp(id, "~~")) {
log(L_VB, "%s[%d]: duplicate ID field \"%s\"",
INITTAB, lineNo, id);
break;
}
}
if (old) continue;
/*
* Allocate a CHILD structure
*/
while ((ch = malloc(sizeof(CHILD))) == NULL) {
log(L_VB, "out of memory");
do_sleep(5);
}
memset(ch, 0, sizeof(CHILD));
/*
* And fill it in.
*/
ch->action = actionNo;
strncpy(ch->id, id, sizeof(utproto.ut_id) + 1); /* Hack for different libs. */
strncpy(ch->process, process, 127);
if (rlevel[0]) {
for(f = 0; f < 11 && rlevel[f]; f++) {
ch->rlevel[f] = rlevel[f];
if (ch->rlevel[f] == 's') ch->rlevel[f] = 'S';
}
strncpy(ch->rlevel, rlevel, 11);
} else {
strcpy(ch->rlevel, "0123456789");
if (ISPOWER(ch->action))
strcpy(ch->rlevel, "S0123456789");
}
/*
* We have the fake runlevel '#' for SYSINIT and
* '*' for BOOT and BOOTWAIT.
*/
if (ch->action == SYSINIT) strcpy(ch->rlevel, "#");
if (ch->action == BOOT || ch->action == BOOTWAIT)
strcpy(ch->rlevel, "*");
/*
* Now add it to the linked list. Special for powerfail.
*/
if (ISPOWER(ch->action)) {
/*
* Disable by default
*/
ch->flags |= XECUTED;
/*
* Tricky: insert at the front of the list..
*/
old = NULL;
for(i = newFamily; i; i = i->next) {
if (!ISPOWER(i->action)) break;
old = i;
}
/*
* Now add after entry "old"
*/
if (old) {
ch->next = i;
old->next = ch;
if (i == NULL) head = ch;
} else {
ch->next = newFamily;
newFamily = ch;
if (ch->next == NULL) head = ch;
}
} else {
/*
* Just add at end of the list
*/
if (ch->action == KBREQUEST) ch->flags |= XECUTED;
ch->next = NULL;
if (head)
head->next = ch;
else
newFamily = ch;
head = ch;
}
/*
* Walk through the old list comparing id fields
*/
for(old = family; old; old = old->next)
if (strcmp(old->id, ch->id) == 0) {
old->new = ch;
break;
}
}
/*
* We're done.
*/
if (fp) fclose(fp);
/*
* Loop through the list of children, and see if they need to
* be killed.
*/
#if DEBUG
log(L_VB, "Checking for children to kill");
#endif
for(round = 0; round < 2; round++) {
talk = 1;
for(ch = family; ch; ch = ch->next) {
ch->flags &= ~KILLME;
/*
* Is this line deleted?
*/
if (ch->new == NULL) ch->flags |= KILLME;
/*
* If the entry has changed, kill it anyway. Note that
* we do not check ch->process, only the "action" field.
* This way, you can turn an entry "off" immediately, but
* changes in the command line will only become effective
* after the running version has exited.
*/
if (ch->new && ch->action != ch->new->action) ch->flags |= KILLME;
/*
* Only BOOT processes may live in all levels
*/
if (ch->action != BOOT &&
strchr(ch->rlevel, runlevel) == NULL) {
/*
* Ondemand procedures live always,
* except in single user
*/
if (runlevel == 'S' || !(ch->flags & DEMAND))
ch->flags |= KILLME;
}
/*
* Now, if this process may live note so in the new list
*/
if ((ch->flags & KILLME) == 0) {
ch->new->flags = ch->flags;
ch->new->pid = ch->pid;
ch->new->exstat = ch->exstat;
continue;
}
/*
* Is this process still around?
*/
if ((ch->flags & RUNNING) == 0) {
ch->flags &= ~KILLME;
continue;
}
#if DEBUG
log(L_VB, "Killing \"%s\"", ch->process);
#endif
switch(round) {
case 0: /* Send TERM signal */
if (talk)
log(L_CO, "Sending processes the TERM signal");
kill(-(ch->pid), SIGTERM);
foundOne = 1;
break;
case 1: /* Send KILL signal and collect status */
if (talk)
log(L_CO, "Sending processes the KILL signal");
kill(-(ch->pid), SIGKILL);
break;
}
talk = 0;
}
/*
* See if we have to wait 5 seconds
*/
if (foundOne && round == 0) {
/*
* Yup, but check every second if we still have children.
*/
for(f = 0; f < sltime; f++) {
for(ch = family; ch; ch = ch->next) {
if (!(ch->flags & KILLME)) continue;
if ((ch->flags & RUNNING) && !(ch->flags & ZOMBIE))
break;
}
if (ch == NULL) {
/*
* No running children, skip SIGKILL
*/
round = 1;
foundOne = 0; /* Skip the sleep below. */
break;
}
do_sleep(1);
}
}
}
/*
* Now give all processes the chance to die and collect exit statuses.
*/
if (foundOne) do_sleep(1);
for(ch = family; ch; ch = ch->next)
if (ch->flags & KILLME) {
if (!(ch->flags & ZOMBIE))
log(L_CO, "Pid %d [id %s] seems to hang", ch->pid,
ch->id);
else {
#if DEBUG
log(L_VB, "Updating utmp for pid %d [id %s]", ch->pid, ch->id);
#endif
ch->flags &= ~RUNNING;
if (ch->process[0] != '+')
write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
}
}
/*
* Both rounds done; clean up the list.
*/
sigemptyset(&nmask);
sigaddset(&nmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &nmask, &omask);
for(ch = family; ch; ch = old) {
old = ch->next;
free(ch);
}
family = newFamily;
for(ch = family; ch; ch = ch->next) ch->new = NULL;
newFamily = NULL;
sigprocmask(SIG_SETMASK, &omask, NULL);
#ifdef INITLVL
/*
* Dispose of INITLVL file.
*/
if (lstat(INITLVL, &st) >= 0 && S_ISLNK(st.st_mode)) {
/*
* INITLVL is a symbolic link, so just truncate the file.
*/
close(open(INITLVL, O_WRONLY|O_TRUNC));
} else {
/*
* Delete INITLVL file.
*/
unlink(INITLVL);
}
#endif
#ifdef INITLVL2
/*
* Dispose of INITLVL2 file.
*/
if (lstat(INITLVL2, &st) >= 0 && S_ISLNK(st.st_mode)) {
/*
* INITLVL2 is a symbolic link, so just truncate the file.
*/
close(open(INITLVL2, O_WRONLY|O_TRUNC));
} else {
/*
* Delete INITLVL2 file.
*/
unlink(INITLVL2);
}
#endif
}
/*
* Walk through the family list and start up children.
* The entries that do not belong here at all are removed
* from the list.
*/
void start_if_needed(void)
{
CHILD *ch; /* Pointer to child */
int delete; /* Delete this entry from list? */
#if DEBUG
log(L_VB, "Checking for children to start");
#endif
for(ch = family; ch; ch = ch->next) {
#if DEBUG
if (ch->rlevel[0] == 'C') {
log(L_VB, "%s: flags %d", ch->process, ch->flags);
}
#endif
/* Are we waiting for this process? Then quit here. */
if (ch->flags & WAITING) break;
/* Already running? OK, don't touch it */
if (ch->flags & RUNNING) continue;
/* See if we have to start it up */
delete = 1;
if (strchr(ch->rlevel, runlevel) ||
((ch->flags & DEMAND) && !strchr("#*Ss", runlevel))) {
startup(ch);
delete = 0;
}
if (delete) {
/* FIXME: is this OK? */
ch->flags &= ~(RUNNING|WAITING);
if (!ISPOWER(ch->action) && ch->action != KBREQUEST)
ch->flags &= ~XECUTED;
ch->pid = 0;
} else
/* Do we have to wait for this process? */
if (ch->flags & WAITING) break;
}
/* Done. */
}
/*
* Ask the user on the console for a runlevel
*/
int ask_runlevel()
{
int lvl = -1;
char buf[8];
int fd;
set_term(0);
fd = serial_open(console_dev, O_RDWR|O_NOCTTY);
if (fd < 0) return('S');
while(!strchr("0123456789S", lvl)) {
write(fd, "\nEnter runlevel: ", 17);
buf[0] = 0;
read(fd, buf, 8);
if (buf[0] != 0 && (buf[1] == '\r' || buf[1] == '\n'))
lvl = buf[0];
if (islower(lvl)) lvl = toupper(lvl);
}
close(fd);
return lvl;
}
/*
* Search the INITTAB file for the 'initdefault' field, with the default
* runlevel. If this fails, ask the user to supply a runlevel.
*/
int get_init_default(void)
{
CHILD *ch;
int lvl = -1;
char *p;
/*
* Look for initdefault.
*/
for(ch = family; ch; ch = ch->next)
if (ch->action == INITDEFAULT) {
p = ch->rlevel;
while(*p) {
if (*p > lvl) lvl = *p;
p++;
}
break;
}
/*
* See if level is valid
*/
if (lvl > 0) {
if (islower(lvl)) lvl = toupper(lvl);
if (strchr("0123456789S", lvl) == NULL) {
log(L_VB, "Initdefault level '%c' is invalid", lvl);
lvl = 0;
}
}
/*
* Ask for runlevel on console if needed.
*/
if (lvl <= 0) lvl = ask_runlevel();
/*
* Log the fact that we have a runlevel now.
*/
return lvl;
}
/*
* We got signaled.
*
* Do actions for the new level. If we are compatible with
* the "old" INITLVL and arg == 0, try to read the new
* runlevel from that file first.
*/
int read_level(int arg)
{
unsigned char foo = 'X'; /* Contents of INITLVL */
int st; /* Sleep time */
CHILD *ch; /* Walk through list */
FILE *fp; /* File pointer for INITLVL */
int ok = 1;
struct stat stt;
if (arg) foo = arg;
#ifdef INITLVL
ok = 0;
if (arg == 0) {
fp = NULL;
if (stat(INITLVL, &stt) != 0 || stt.st_size != 0L)
fp = fopen(INITLVL, "r");
#ifdef INITLVL2
if (fp == NULL && (stat(INITLVL2, &stt) != 0 || stt.st_size != 0L))
fp = fopen(INITLVL2, "r");
#endif
if (fp == NULL) {
/* INITLVL file is empty or not there - act as 'init q' */
log(L_SY, "Re-reading inittab");
return(runlevel);
}
ok = fscanf(fp, "%c %d", &foo, &st);
fclose(fp);
} else {
/* We go to the new runlevel passed as an argument. */
foo = arg;
ok = 1;
}
if (ok == 2) sltime = st;
#endif /* INITLVL */
if (islower(foo)) foo = toupper(foo);
if (ok < 1 || ok > 2 || strchr("QS0123456789ABCU", foo) == NULL) {
log(L_VB, "bad runlevel: %c", foo);
return(runlevel);
}
/* Log this action */
switch(foo) {
case 'S':
log(L_VB, "Going single user");
break;
case 'Q':
log(L_SY, "Re-reading inittab");
break;
case 'A':
case 'B':
case 'C':
log(L_SY, "Activating demand-procedures for '%c'", foo);
break;
case 'U':
log(L_SY, "Trying to re-exec init");
return 'U';
default:
log(L_VB, "Switching to runlevel: %c", foo);
}
if (foo == 'Q') return(runlevel);
/* Check if this is a runlevel a, b or c */
if (strchr("ABC", foo)) {
if (runlevel == 'S') return(runlevel);
/* Read inittab again first! */
read_inittab();
/* Mark those special tasks */
for(ch = family; ch; ch = ch->next)
if (strchr(ch->rlevel, foo) != NULL ||
strchr(ch->rlevel, tolower(foo)) != NULL) {
ch->flags |= DEMAND;
ch->flags &= ~XECUTED;
#if DEBUG
log(L_VB, "Marking (%s) as ondemand, flags %d",
ch->id, ch->flags);
#endif
}
return(runlevel);
}
/* Store both the old and the new runlevel. */
write_utmp_wtmp("runlevel", "~~", foo + 256*runlevel, RUN_LVL, "~");
thislevel = foo;
prevlevel = runlevel;
return(foo);
}
/*
* This procedure is called after every signal (SIGHUP, SIGALRM..)
*
* Only clear the 'failing' flag if the process is sleeping
* longer than 5 minutes, or inittab was read again due
* to user interaction.
*/
void fail_check(void)
{
time_t t; /* System time */
CHILD *ch; /* Pointer to child structure */
time_t next_alarm = 0; /* When to set next alarm */
time(&t);
for(ch = family; ch; ch = ch->next) {
if (ch->flags & FAILING) {
/* Can we free this sucker? */
if (ch->tm + SLEEPTIME < t) {
ch->flags &= ~FAILING;
ch->count = 0;
ch->tm = 0;
} else {
/* No, we'll look again later */
if (next_alarm == 0 || ch->tm + SLEEPTIME > next_alarm)
next_alarm = ch->tm + SLEEPTIME;
}
}
}
if (next_alarm) {
next_alarm -= t;
if (next_alarm < 1) next_alarm = 1;
alarm(next_alarm);
}
}
/* Set all 'Fail' timers to 0 */
void fail_cancel(void)
{
CHILD *ch;
for(ch = family; ch; ch = ch->next) {
ch->count = 0;
ch->tm = 0;
ch->flags &= ~FAILING;
}
}
/*
* Start up powerfail entries.
*/
void do_power_fail(int pwrstat)
{
CHILD *ch;
/*
* Tell powerwait & powerfail entries to start up
*/
for (ch = family; ch; ch = ch->next) {
if (pwrstat == 'O') {
/*
* The power is OK again.
*/
if (ch->action == POWEROKWAIT)
ch->flags &= ~XECUTED;
} else if (pwrstat == 'L') {
/*
* Low battery, shut down now.
*/
if (ch->action == POWERFAILNOW)
ch->flags &= ~XECUTED;
} else {
/*
* Power is failing, shutdown imminent
*/
if (ch->action == POWERFAIL || ch->action == POWERWAIT)
ch->flags &= ~XECUTED;
}
}
}
/*
* Check for state-pipe presence
*/
int check_pipe(int fd)
{
struct timeval t;
fd_set s;
char signature[8];
FD_ZERO(&s);
FD_SET(fd, &s);
t.tv_sec = t.tv_usec = 0;
if (select(fd+1, &s, NULL, NULL, &t) != 1)
return 0;
if (read(fd, signature, 8) != 8)
return 0;
return strcmp(Signature, signature) == 0;
}
/*
* Make a state-pipe.
*/
int make_pipe(int fd)
{
int fds[2];
pipe(fds);
dup2(fds[0], fd);
close(fds[0]);
fcntl(fds[1], F_SETFD, 1);
fcntl(fd, F_SETFD, 0);
write(fds[1], Signature, 8);
return fds[1];
}
/*
* Attempt to re-exec.
*/
void re_exec(void)
{
sigset_t mask, oldset;
pid_t pid;
int fd;
CHILD *ch;
if (strchr("S12345",runlevel) == NULL)
return;
closelog(); /* AAAARRRGH! It _is_ needed */
/*
* Reset the alarm, and block all signals.
*/
alarm(0);
sigfillset(&mask);
sigprocmask(SIG_BLOCK, &mask, &oldset);
/*
* construct a pipe fd --> STATE_PIPE and write a signature
*/
fd = make_pipe(STATE_PIPE);
/*
* It's a backup day today, so I'm pissed off. Being a BOFH, however,
* does have it's advantages...
*/
fail_cancel();
close(pipe_fd);
pipe_fd = -1;
DELSET(got_signals, SIGCHLD);
DELSET(got_signals, SIGHUP);
DELSET(got_signals, SIGUSR1);
/*
* That should be cleaned.
*/
for(ch = family; ch; ch = ch->next)
if (ch->flags & ZOMBIE) {
#if DEBUG
log(L_VB, "Child died, PID= %d", ch->pid);
#endif
ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
if (ch->process[0] != '+')
write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
}
if ((pid = fork()) > 0) {
/*
* Yup. _Parent_ exec's ...
*/
execl(myname, myname, NULL);
} else if (pid == 0) {
/*
* ... while child sends her the
* state information and dies
*/
send_state(fd);
exit(0);
}
/*
* We shouldn't be here, something failed.
* Bitch, close the state pipe, unblock signals and return.
*/
close(fd);
close(STATE_PIPE);
sigprocmask(SIG_SETMASK, &oldset, NULL);
log(L_CO, "Attempt to re-exec failed");
}
/*
* We got a change runlevel request through the
* init.fifo. Process it.
*/
void fifo_new_level(int level)
{
int oldlevel;
#if CHANGE_WAIT
CHILD *ch;
#endif
if (level == runlevel) return;
#if CHANGE_WAIT
/* Are we waiting for a child? */
for(ch = family; ch; ch = ch->next)
if (ch->flags & WAITING) break;
if (ch == NULL)
#endif
{
/* We need to go into a new runlevel */
oldlevel = runlevel;
runlevel = read_level(level);
if (runlevel == 'U') {
runlevel = oldlevel;
re_exec();
} else {
if (oldlevel != 'S' && runlevel == 'S') set_term(0);
if (runlevel == '6' || runlevel == '0' || runlevel == '1') set_term(0);
read_inittab();
fail_cancel();
setproctitle("init [%c]", runlevel);
}
}
}
/*
* Read from the init FIFO. Processes like telnetd and rlogind can
* ask us to create login processes on their behalf.
*
* FIXME: this needs to be finished. NOT that it is buggy, but we need
* to add the telnetd/rlogind stuff so people can start using it.
* Maybe move to using an AF_UNIX socket so we can use
* the 2.2 kernel credential stuff to see who we're talking to.
*
*/
void check_init_fifo(void)
{
struct init_request request;
int n;
fd_set fds;
int quit = 0;
struct stat st;
/* Try to open /dev/initctl */
if (pipe_fd < 0) {
if ((pipe_fd = open(INIT_FIFO, O_RDWR|O_NONBLOCK)) >= 0) {
fstat(pipe_fd, &st);
if (!S_ISFIFO(st.st_mode)) {
log(L_VB, "%s is not a fifo", INIT_FIFO);
close(pipe_fd);
pipe_fd = -1;
}
}
if (pipe_fd >= 0) {
/* Don't use fd's 0, 1 or 2. */
(void) dup2(pipe_fd, PIPE_FD);
close(pipe_fd);
pipe_fd = PIPE_FD;
}
}
/* Wait for data to appear, _if_ the pipe was opened. */
if (pipe_fd >= 0) while(!quit) {
/* Do select, return on EINTR. */
FD_ZERO(&fds);
FD_SET(pipe_fd, &fds);
n = select(pipe_fd + 1, &fds, NULL, NULL, NULL);
if (n <= 0) {
if (errno == EINTR) return;
continue;
}
/* Read the data, return on EINTR. */
n = read(pipe_fd, &request, sizeof(request));
if (n == 0) {
/*
* End of file. This can't happen under Linux (because
* the pipe is opened O_RDWR - see select() in the
* kernel) but you never know...
*/
close(pipe_fd);
pipe_fd = -1;
return;
}
if (n <= 0) {
if (errno == EINTR) return;
log(L_VB, "error reading initrequest");
continue;
}
/* Process request. */
if (request.magic != INIT_MAGIC || n != sizeof(request)) {
log(L_VB, "got bogus initrequest");
continue;
}
switch(request.cmd) {
case INIT_CMD_RUNLVL:
sltime = request.sleeptime;
fifo_new_level(request.runlevel);
quit = 1;
break;
case INIT_CMD_POWERFAIL:
sltime = request.sleeptime;
do_power_fail('F');
quit = 1;
break;
case INIT_CMD_POWERFAILNOW:
sltime = request.sleeptime;
do_power_fail('L');
quit = 1;
break;
case INIT_CMD_POWEROK:
sltime = request.sleeptime;
do_power_fail('O');
quit = 1;
break;
default:
log(L_VB, "got unimplemented initrequest.");
break;
}
}
/* We come here if the pipe couldn't be opened. */
if (pipe_fd < 0) pause();
}
/*
* This function is used in the transition
* sysinit (-> single user) boot -> multi-user.
*/
void boot_transitions()
{
CHILD *ch;
static int newlevel = 0;
int loglevel;
int oldlevel;
static int warn = 1;
/* Check if there is something to wait for! */
for( ch = family; ch; ch = ch->next )
if ((ch->flags & RUNNING) && ch->action != BOOT) break;
if (ch == NULL) {
/* No processes left in this level, proceed to next level. */
loglevel = -1;
oldlevel = 'N';
switch(runlevel) {
case '#': /* SYSINIT -> BOOT */
#if DEBUG
log(L_VB, "SYSINIT -> BOOT");
#endif
/* Save tty modes. */
set_term(1);
/* Write a boot record. */
wrote_reboot = 0;
write_wtmp("reboot", "~~", 0, BOOT_TIME, "~");
/* Get our run level */
newlevel = dfl_level ? dfl_level : get_init_default();
if (newlevel == 'S') {
runlevel = newlevel;
/* Not really 'S' but show anyway. */
setproctitle("init [S]");
} else
runlevel = '*';
break;
case '*': /* BOOT -> NORMAL */
#if DEBUG
log(L_VB, "BOOT -> NORMAL");
#endif
/* Save tty modes. */
set_term(1);
if (runlevel != newlevel)
loglevel = newlevel;
runlevel = newlevel;
did_boot = 1;
warn = 1;
break;
case 'S': /* Ended SU mode */
case 's':
#if DEBUG
log(L_VB, "END SU MODE");
#endif
/* Save tty modes. */
set_term(1);
newlevel = get_init_default();
if (!did_boot && newlevel != 'S')
runlevel = '*';
else {
if (runlevel != newlevel)
loglevel = newlevel;
runlevel = newlevel;
oldlevel = 'S';
}
warn = 1;
for(ch = family; ch; ch = ch->next)
if (strcmp(ch->rlevel, "S") == 0)
ch->flags &= ~(FAILING|WAITING|XECUTED);
break;
default:
if (warn)
log(L_VB, "no more processes left in this runlevel");
warn = 0;
loglevel = -1;
if (got_signals == 0)
check_init_fifo();
break;
}
if (loglevel > 0) {
log(L_VB, "Entering runlevel: %c", runlevel);
write_utmp_wtmp("runlevel", "~~", runlevel + 256 * oldlevel, RUN_LVL, "~");
thislevel = runlevel;
prevlevel = oldlevel;
setproctitle("init [%c]", runlevel);
}
}
}
/*
* Init got hit by a signal. See which signal it is,
* and act accordingly.
*/
void process_signals()
{
int pwrstat;
int oldlevel;
int fd;
CHILD *ch;
char c;
if (ISMEMBER(got_signals, SIGPWR)) {
#if DEBUG
log(L_VB, "got SIGPWR");
#endif
/* See _what_ kind of SIGPWR this is. */
pwrstat = 0;
if ((fd = open(PWRSTAT, O_RDONLY)) >= 0) {
c = 0;
read(fd, &c, 1);
pwrstat = c;
close(fd);
unlink(PWRSTAT);
}
do_power_fail(pwrstat);
DELSET(got_signals, SIGPWR);
}
if (ISMEMBER(got_signals, SIGINT)) {
#if DEBUG
log(L_VB, "got SIGINT");
#endif
/* Tell ctrlaltdel entry to start up */
for(ch = family; ch; ch = ch->next)
if (ch->action == CTRLALTDEL)
ch->flags &= ~XECUTED;
DELSET(got_signals, SIGINT);
}
if (ISMEMBER(got_signals, SIGWINCH)) {
#if DEBUG
log(L_VB, "got SIGWINCH");
#endif
/* Tell kbrequest entry to start up */
for(ch = family; ch; ch = ch->next)
if (ch->action == KBREQUEST)
ch->flags &= ~XECUTED;
DELSET(got_signals, SIGWINCH);
}
if (ISMEMBER(got_signals, SIGALRM)) {
#if DEBUG
log(L_VB, "got SIGALRM");
#endif
/* The timer went off: check it out */
DELSET(got_signals, SIGALRM);
}
if (ISMEMBER(got_signals, SIGCHLD)) {
#if DEBUG
log(L_VB, "got SIGCHLD");
#endif
/* First set flag to 0 */
DELSET(got_signals, SIGCHLD);
/* See which child this was */
for(ch = family; ch; ch = ch->next)
if (ch->flags & ZOMBIE) {
#if DEBUG
log(L_VB, "Child died, PID= %d", ch->pid);
#endif
ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
if (ch->process[0] != '+')
write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
}
}
if (ISMEMBER(got_signals, SIGHUP)) {
#if DEBUG
log(L_VB, "got SIGHUP");
#endif
#if CHANGE_WAIT
/* Are we waiting for a child? */
for(ch = family; ch; ch = ch->next)
if (ch->flags & WAITING) break;
if (ch == NULL)
#endif
{
/* We need to go into a new runlevel */
oldlevel = runlevel;
#ifdef INITLVL
runlevel = read_level(0);
#endif
if (runlevel == 'U') {
runlevel = oldlevel;
re_exec();
} else {
if (oldlevel != 'S' && runlevel == 'S') set_term(0);
if (runlevel == '6' || runlevel == '0' || runlevel == '1') set_term(0);
read_inittab();
fail_cancel();
setproctitle("init [%c]", runlevel);
DELSET(got_signals, SIGHUP);
}
}
}
if (ISMEMBER(got_signals, SIGUSR1)) {
/*
* SIGUSR1 means close and reopen /dev/initctl
*/
#if DEBUG
log(L_VB, "got SIGHUP");
#endif
close(pipe_fd);
pipe_fd = -1;
DELSET(got_signals, SIGUSR1);
}
}
/*
* The main loop
*/
int init_main()
{
int f, st;
CHILD *ch;
sigset_t sgt;
struct sigaction sa;
char *s;
if (!reload) {
#if INITDEBUG
/*
* Fork so we can debug the init process.
*/
if ((f = fork()) > 0) {
while(wait(&st) != f)
;
write(1, "PRNT: init killed.\r\n", 20);
while(1) pause();
}
#endif
/*
* Tell the kernel to send us SIGINT when CTRL-ALT-DEL
* is pressed, and that we want to handle keyboard signals.
*/
init_reboot(BMAGIC_SOFT);
if ((f = open(VT_MASTER, O_RDWR | O_NOCTTY)) >= 0) {
(void) ioctl(f, KDSIGACCEPT, SIGWINCH);
close(f);
} else
(void) ioctl(0, KDSIGACCEPT, SIGWINCH);
/*
* Ignore all signals.
*/
for(f = 1; f <= NSIG; f++)
signal(f, SIG_IGN);
}
SETSIG(sa, SIGALRM, signal_handler);
SETSIG(sa, SIGHUP, signal_handler);
SETSIG(sa, SIGINT, signal_handler);
SETSIG(sa, SIGCHLD, chld_handler);
SETSIG(sa, SIGPWR, signal_handler);
SETSIG(sa, SIGWINCH, signal_handler);
SETSIG(sa, SIGUSR1, signal_handler);
SETSIG(sa, SIGSTOP, stop_handler);
SETSIG(sa, SIGTSTP, stop_handler);
SETSIG(sa, SIGCONT, cont_handler);
SETSIG(sa, SIGSEGV, segv_handler);
/*
* NOTE: instead of calling siginterrupt, we could just as
* well install the signal handler without SA_RESTART in the
* first place.
*/
siginterrupt (SIGALRM, 1);
siginterrupt (SIGHUP, 1);
siginterrupt (SIGINT, 1);
siginterrupt (SIGPWR, 1);
siginterrupt (SIGUSR1, 1);
siginterrupt (SIGWINCH, 1);
if (!reload) {
/* Close whatever files are open, and reset the console. */
close(0);
close(1);
close(2);
if ((s = getenv("CONSOLE")) != NULL)
console_dev = s;
set_term(0);
setsid();
/*
* Set default PATH variable (for ksh)
*/
if (getenv("PATH") == NULL) putenv(PATH_DFL);
/*
* Initialize /var/run/utmp (only works if /var is on
* root and mounted rw)
*/
(void) close(open(UTMP_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0644));
/*
* Say hello to the world
*/
log(L_CO, bootmsg);
/*
* See if we have to start an emergency shell.
*/
if (emerg_shell) {
SETSIG(sa, SIGCHLD, SIG_DFL);
if (spawn(&ch_emerg, &f) > 0) {
while(wait(&st) != f)
;
}
SETSIG(sa, SIGCHLD, chld_handler);
}
/*
* Start normal boot procedure.
*/
runlevel = '#';
read_inittab();
} else {
/*
* Restart: unblock signals and let the show go on
*/
log(L_CO, bootmsg);
sigfillset(&sgt);
sigprocmask(SIG_UNBLOCK, &sgt, NULL);
}
start_if_needed();
while(1) {
/* See if we need to make the boot transitions. */
boot_transitions();
#if DEBUG
log(L_VB, "init_main: waiting..");
#endif
/* Check if there are processes to be waited on. */
for(ch = family; ch; ch = ch->next)
if ((ch->flags & RUNNING) && ch->action != BOOT) break;
#if CHANGE_WAIT
/* Wait until we get hit by some signal. */
while (ch != NULL && got_signals == 0) {
if (ISMEMBER(got_signals, SIGHUP)) {
/* See if there are processes to be waited on. */
for(ch = family; ch; ch = ch->next)
if (ch->flags & WAITING) break;
}
if (ch != NULL) check_init_fifo();
}
#else /* CHANGE_WAIT */
if (ch != NULL && got_signals == 0) check_init_fifo();
#endif /* CHANGE_WAIT */
/* Check the 'failing' flags */
fail_check();
/* Process any signals. */
process_signals();
/* See what we need to start up (again) */
start_if_needed();
}
/*NOTREACHED*/
}
/*
* Tell the user about the syntax we expect.
*/
void Usage(char *s)
{
fprintf(stderr, "Usage: %s 0123456SsQqAaBbCcUu\n", s);
exit(1);
}
/*
* Main entry for init and telinit.
*/
int main(int argc, char *argv[])
{
struct init_request request;
char *p;
FILE *fp;
int f, fd;
/* Get my own name */
if ((p = strrchr(argv[0], '/')) != NULL)
p++;
else
p = argv[0];
/*
* Is this telinit or init ?
*/
#ifndef TEST
if (getpid() == INITPID || !strcmp(p, "init.new") || !strcmp(p, "sh"))
#endif
{
/*
* Check for re-exec
*/
if (check_pipe(STATE_PIPE)) {
receive_state(STATE_PIPE);
myname = strdup(argv[0]);
argv0 = argv[0];
maxproclen = strlen(argv[0]);
reload = 1;
wrote_iosave = 1;
init_main();
}
/* Check command line arguments */
maxproclen = strlen(argv[0]) + 1;
for(f = 1; f < argc; f++) {
if (!strcmp(argv[f], "single") || !strcmp(argv[f], "-s"))
dfl_level = 'S';
else if (!strcmp(argv[f], "-a") || !strcmp(argv[f], "auto"))
putenv("AUTOBOOT=YES");
else if (!strcmp(argv[f], "-b") || !strcmp(argv[f], "emergency"))
emerg_shell = 1;
else if (strchr("0123456789sS", argv[f][0])
&& strlen(argv[f]) == 1)
dfl_level = argv[f][0];
/* "init u" in the very beginning makes no sence */
if (dfl_level == 's') dfl_level = 'S';
maxproclen += strlen(argv[f]) + 1;
}
maxproclen--;
/* Start booting. */
argv0 = argv[0];
argv[1] = NULL;
setproctitle("init boot");
init_main(dfl_level);
}
/* Nope, this is a change-run-level init */
while((f = getopt(argc, argv, "t:")) != EOF) {
switch(f) {
case 't':
sltime = atoi(optarg);
break;
default:
Usage(p);
break;
}
}
if (geteuid() != 0) {
fprintf(stderr, "init: must be superuser.\n");
exit(1);
}
/* Check syntax. */
if (argc - optind != 1 || strlen(argv[optind]) != 1) Usage(p);
if (!strchr("0123456789SsQqAaBbCcUu", argv[optind][0])) Usage(p);
umask(022);
/* Open the fifo and write a command. */
memset(&request, 0, sizeof(request));
request.magic = INIT_MAGIC;
request.cmd = INIT_CMD_RUNLVL;
request.runlevel = argv[optind][0];
request.sleeptime = sltime;
/* Make sure we don't hang on opening /etc/init.fifo */
signal(SIGALRM, signal_handler);
siginterrupt (SIGALRM, 1);
alarm(3);
if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0 &&
write(fd, &request, sizeof(request)) == sizeof(request)) {
close(fd);
alarm(0);
return 0;
}
#ifndef INITLVL
perror(INIT_FIFO);
exit(1);
#endif
/* Fallthrough to the old method. */
#ifdef INITLVL
/* Now write the new runlevel. */
if ((fp = fopen(INITLVL, "w")) == NULL) {
fprintf(stderr, "%s: cannot create %s\n", p, INITLVL);
exit(1);
}
fprintf(fp, "%s %d", argv[optind], sltime);
fclose(fp);
/* And tell init about the pending runlevel change. */
if (kill(INITPID, SIGHUP) < 0) perror(p);
exit(0);
#endif
}
|