10 #include <sys/fcntl.h>
13 #include <sys/mount.h>
14 #include <sys/reboot.h>
15 #include <sys/kdaemon.h>
17 #include <sys/sysmacros.h>
19 const char init_usage[] = "Used internally by the system.";
20 char console[16] = "";
21 const char * default_console = "/dev/tty1";
22 char * first_terminal = NULL;
23 const char * second_terminal = "/dev/tty2";
24 const char log[] = "/dev/tty3";
25 char * term_ptr = NULL;
28 message(const char * terminal, const char * pattern, ...)
35 * Open the console device each time a message is printed. If the user
36 * has switched consoles, the open will get the new console. If we kept
37 * the console open, we'd always print to the same one.
39 if ( ((fd = open(terminal, O_WRONLY|O_NOCTTY)) < 0)
40 || ((con = fdopen(fd, "w")) == NULL) )
43 va_start(arguments, pattern);
44 vfprintf(con, pattern, arguments);
55 message(log, "Waiting for process %d.\n", pid);
56 while ( (wpid = wait(&status)) != pid ) {
60 ,"pid %d exited, status=%x.\n"
69 run(const char* program, const char* const* arguments,
70 const char* terminal, int get_enter)
72 static const char control_characters[] = {
92 static char * environment[] = {
94 "PATH=/bin:/sbin:/usr/bin:/usr/sbin",
101 static const char press_enter[] =
102 "Please press Enter to activate this console. ";
106 environment[3]=term_ptr;
111 const char * const * arg;
118 open(terminal, O_RDWR);
121 tcsetpgrp(0, getpgrp());
124 memcpy(t.c_cc, control_characters, sizeof(control_characters));
126 t.c_iflag = ICRNL|IXON|IXOFF;
127 t.c_oflag = OPOST|ONLCR;
128 t.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
129 tcsetattr(0, TCSANOW, &t);
133 * Save memory by not exec-ing anything large (like a shell)
134 * before the user wants it. This is critical if swap is not
135 * enabled and the system has low memory. Generally this will
136 * be run on the second virtual console, and the first will
137 * be allowed to start a shell or whatever an init script
141 write(1, press_enter, sizeof(press_enter) - 1);
145 message(log, "Executing ");
148 message(log, "%s ", *arg++);
151 execve(program, (char * *)arguments, (char * *)environment);
152 message(log, "%s: could not execute: %s.\r\n", program, strerror(errno));
164 const char pattern[]="MemTotal:";
166 f=fopen("/proc/meminfo","r");
167 while (NULL != fgets(s,79,f)) {
168 p=strstr(s, pattern);
171 return(atoi(p+strlen(pattern)));
183 f=fopen("/proc/sys/vm/freepages","r");
187 f=fopen("/proc/sys/vm/freepages","w");
188 fprintf(f,"30\t40\t50\n");
189 printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
195 shutdown_system(int do_reboot)
197 static const char * const umount_args[] = {"/bin/umount", "-a", "-n", 0};
200 /* Allow Ctrl-Alt-Del to reboot system. */
201 reboot(RB_ENABLE_CAD);
203 /* Send signals to every process _except_ pid 1 */
204 message(console, "Sending SIGHUP to all processes.\r\n");
208 message(console, "Sending SIGKILL to all processes.\r\n");
211 waitfor(run("/bin/umount", umount_args, console, 0));
215 reboot(do_reboot ?RB_AUTOBOOT : RB_HALT_SYSTEM);
226 reboot_signal(int sig)
235 /* initrd doesn't work anyway */
239 /* This is used on the initial ramdisk */
241 /* message(log, "Init exiting.");
247 configure_terminals( int serial_cons )
249 //struct stat statbuf;
252 switch (serial_cons) {
254 strcpy( console, "/dev/ttyS0" );
257 strcpy( console, "/dev/ttyS1" );
262 strcpy( console, tty );
263 if (!strncmp( tty, "/dev/ttyS", 9 ))
267 /* falls back to /dev/tty1 if an error occurs */
268 strcpy( console, default_console );
271 first_terminal = console;
272 if (serial_cons && !strncmp(term_ptr,"TERM=linux",10))
273 term_ptr = "TERM=vt100";
277 init_main(int argc, char * * argv)
279 static const char* const rc = "etc/rc";
280 const char * arguments[100];
287 const char * tty_commands[2] = { "bin/sh", "bin/sh"};
289 int serial_console = 0;
292 * If I am started as /linuxrc instead of /sbin/init, I don't have the
293 * environment that init expects. I can't fix the signal behavior. Try
294 * to divorce from the controlling terminal with setsid(). This won't work
295 * if I am the process group leader.
299 signal(SIGUSR1, halt_signal);
300 signal(SIGUSR2, reboot_signal);
301 signal(SIGINT, reboot_signal);
302 signal(SIGTERM, exit_signal);
304 reboot(RB_DISABLE_CAD);
306 message(log, "%s: started. ", argv[0]);
308 for ( j = 1; j < argc; j++ ) {
309 if ( strcmp(argv[j], "single") == 0 ) {
311 tty_commands[0] = "bin/sh";
315 for ( j = 0; __environ[j] != 0; j++ ) {
316 if ( strncmp(__environ[j], "tty", 3) == 0
317 && __environ[j][3] >= '1'
318 && __environ[j][3] <= '2'
319 && __environ[j][4] == '=' ) {
320 const char * s = &__environ[j][5];
322 if ( *s == 0 || strcmp(s, "off") == 0 )
325 tty_commands[__environ[j][3] - '1'] = s;
327 /* Should catch the syntax of Sparc kernel console setting. */
328 /* The kernel does not recognize a serial console when getting*/
329 /* console=/dev/ttySX !! */
330 else if ( strcmp(__environ[j], "console=ttya") == 0 ) {
333 else if ( strcmp(__environ[j], "console=ttyb") == 0 ) {
336 /* standard console settings */
337 else if ( strncmp(__environ[j], "console=", 8) == 0 ) {
338 first_terminal=&(__environ[j][8]);
340 else if ( strncmp(__environ[j], "TERM=", 5) == 0) {
341 term_ptr=__environ[j];
344 configure_terminals( serial_console );
346 printf("mounting /proc ...\n");
347 if (mount("/proc","/proc","proc",0,0)) {
348 perror("mounting /proc failed\n");
354 /* not enough memory to do anything useful*/
355 if (mem_total() < 2000) {
357 retval= stat("/etc/swappartition",&statbuf);
359 printf("You do not have enough RAM, sorry.\n");
360 while (1) { sleep(1);}
361 } else { /* everything OK */
364 f=fopen("/etc/swappartition","r");
367 *(strstr(swap,"\n"))='\0';
369 if (swapon(swap,0)) {
370 perror("swapon failed\n");
372 f=fopen("/etc/swaps","w");
373 fprintf(f,"%s none swap rw 0 0",swap);
381 * Don't modify **argv directly, it would show up in the "ps" display.
382 * I don't want "init" to look like "rc".
385 for ( j = 1; j < argc; j++ ) {
386 arguments[j] = argv[j];
391 printf("running %s with args \"",rc);
392 for ( j = 0; j < argc; j++ ) {
393 printf("%s ", arguments[j]);
395 printf("\" on console %s\n", console);
396 waitfor(run(rc, arguments, console, 0));
400 if ( 0 == create_swap) {
401 if (unlink("/etc/swappartition")) {
402 perror("unlinking /etc/swappartition");
406 arguments[0] = "-sh";
412 if ( pid1 == 0 && tty_commands[0] ) {
413 /* Ask before starting a shell */
415 arguments[0] = tty_commands[0];
417 printf("running %s with args \"",tty_commands[0]);
418 for ( j = 0; j < argc; j++ ) {
419 printf("%s ", arguments[j]);
421 printf("\" on console %s\n", first_terminal);
422 pid1 = run(tty_commands[0], arguments, first_terminal, 1);
424 if ( pid2 == 0 && tty_commands[1] ) {
425 printf("running %s with args \"",tty_commands[0]);
426 for ( j = 0; j < argc; j++ ) {
427 printf("%s ", arguments[j]);
429 printf("\" on console %s\n", first_terminal);
430 pid2 = run(tty_commands[1], arguments, second_terminal, 1);
432 wpid = wait(&status);
435 message(log, "pid %d exited, status=%x.\n", wpid, status);
437 if ( wpid == pid1 ) {