10 #include <sys/fcntl.h>
13 #include <sys/mount.h>
14 #include <sys/reboot.h>
15 #include <sys/kdaemon.h>
16 #include <sys/sysmacros.h>
17 #include <linux/serial.h> /* for serial_struct */
18 #include <sys/vt.h> /* for vt_stat */
19 #include <sys/ioctl.h>
21 static const char init_usage[] = "Used internally by the system.";
22 static char console[16] = "";
23 static const char* default_console = "/dev/tty2";
24 static char* first_terminal = NULL;
25 static const char* second_terminal = "/dev/tty2";
26 static const char* log = "/dev/tty3";
27 static char* term_ptr = NULL;
30 message(const char* terminal, const char * pattern, ...)
37 * Open the console device each time a message is printed. If the user
38 * has switched consoles, the open will get the new console. If we kept
39 * the console open, we'd always print to the same one.
42 || ((fd = open(terminal, O_WRONLY|O_NOCTTY)) < 0)
43 || ((con = fdopen(fd, "w")) == NULL) )
46 va_start(arguments, pattern);
47 vfprintf(con, pattern, arguments);
58 message(log, "Waiting for process %d.\n", pid);
59 while ( (wpid = wait(&status)) != pid ) {
63 ,"pid %d exited, status=%x.\n"
72 run(const char* program, const char* const* arguments,
73 const char* terminal, int get_enter)
75 static const char control_characters[] = {
95 static char * environment[] = {
97 "PATH=/bin:/sbin:/usr/bin:/usr/sbin",
104 static const char press_enter[] =
105 "\nPlease press Enter to activate this console. ";
109 environment[3]=term_ptr;
114 const char * const * arg;
121 open(terminal, O_RDWR);
124 tcsetpgrp(0, getpgrp());
127 memcpy(t.c_cc, control_characters, sizeof(control_characters));
129 t.c_iflag = ICRNL|IXON|IXOFF;
130 t.c_oflag = OPOST|ONLCR;
131 t.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
132 tcsetattr(0, TCSANOW, &t);
136 * Save memory by not exec-ing anything large (like a shell)
137 * before the user wants it. This is critical if swap is not
138 * enabled and the system has low memory. Generally this will
139 * be run on the second virtual console, and the first will
140 * be allowed to start a shell or whatever an init script
144 write(1, press_enter, sizeof(press_enter) - 1);
148 message(log, "Executing ");
151 message(log, "%s ", *arg++);
154 execve(program, (char * *)arguments, (char * *)environment);
155 message(log, "%s: could not execute: %s.\r\n", program, strerror(errno));
167 const char pattern[]="MemTotal:";
169 f=fopen("/proc/meminfo","r");
170 while (NULL != fgets(s,79,f)) {
171 p=strstr(s, pattern);
174 return(atoi(p+strlen(pattern)));
186 f=fopen("/proc/sys/vm/freepages","r");
190 f=fopen("/proc/sys/vm/freepages","w");
191 fprintf(f,"30\t40\t50\n");
192 printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
198 shutdown_system(void)
200 static const char * const umount_args[] = {"/bin/umount", "-a", "-n", 0};
201 static const char * const swapoff_args[] = {"/bin/swapoff", "-a", 0};
203 message(console, "The system is going down NOW !!");
205 /* Allow Ctrl-Alt-Del to reboot system. */
206 reboot(RB_ENABLE_CAD);
208 /* Send signals to every process _except_ pid 1 */
209 message(console, "Sending SIGHUP to all processes.\r\n");
213 message(console, "Sending SIGKILL to all processes.\r\n");
216 waitfor(run("/bin/swapoff", swapoff_args, console, 0));
217 waitfor(run("/bin/umount", umount_args, console, 0));
219 if (get_kernel_revision() <= 2*65536+2*256+11) {
220 /* Removed bdflush call, kupdate in kernels >2.2.11 */
230 message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
231 reboot( RB_HALT_SYSTEM);
236 reboot_signal(int sig)
239 message(console, "Please stand by while rebooting the system.\r\n");
240 reboot( RB_AUTOBOOT);
245 configure_terminals( int serial_cons, int single_user_mode )
248 struct serial_struct sr;
252 switch (serial_cons) {
255 * identify the real console backend and try to make use of it */
256 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
257 sprintf( console, "/dev/ttyS%d", sr.line );
258 serial_cons = sr.line+1;
260 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
261 sprintf( console, "/dev/tty%d", vt.v_active );
265 /* unknown backend: fallback to /dev/console */
266 strcpy( console, "/dev/console" );
272 strcpy( console, "/dev/cua0" );
275 strcpy( console, "/dev/cua1" );
280 strcpy( console, tty );
281 if (!strncmp( tty, "/dev/cua", 8 ))
285 /* falls back to /dev/tty1 if an error occurs */
286 strcpy( console, default_console );
289 first_terminal = console;
290 #if defined (__sparc__)
291 if (serial_cons > 0 && !strncmp(term_ptr,"TERM=linux",10))
292 term_ptr = "TERM=vt100";
295 /* disable other but the first terminal:
296 * VT is not initialized anymore on 2.2 kernel when booting from
297 * serial console, therefore modprobe is flooding the display with
298 * "can't locate module char-major-4" messages. */
305 init_main(int argc, char * * argv)
307 const char * rc_arguments[100];
308 const char * arguments[100];
314 const char * tty_commands[3] = { "etc/init.d/rcS", "bin/sh"};
315 int serial_console = 0;
319 * If I am started as /linuxrc instead of /sbin/init, I don't have the
320 * environment that init expects. I can't fix the signal behavior. Try
321 * to divorce from the controlling terminal with setsid(). This won't work
322 * if I am the process group leader.
326 signal(SIGUSR1, halt_signal);
327 signal(SIGUSR2, reboot_signal);
328 signal(SIGINT, reboot_signal);
329 signal(SIGTERM, reboot_signal);
331 reboot(RB_DISABLE_CAD);
333 message(log, "%s: started. ", argv[0]);
335 for ( j = 1; j < argc; j++ ) {
336 if ( strcmp(argv[j], "single") == 0 ) {
338 tty_commands[0] = "bin/sh";
342 for ( j = 0; __environ[j] != 0; j++ ) {
343 if ( strncmp(__environ[j], "tty", 3) == 0
344 && __environ[j][3] >= '1'
345 && __environ[j][3] <= '2'
346 && __environ[j][4] == '=' ) {
347 const char * s = &__environ[j][5];
349 if ( *s == 0 || strcmp(s, "off") == 0 )
352 tty_commands[__environ[j][3] - '1'] = s;
354 /* Should catch the syntax of Sparc kernel console setting. */
355 /* The kernel does not recognize a serial console when getting*/
356 /* console=/dev/ttySX !! */
357 else if ( strcmp(__environ[j], "console=ttya") == 0 ) {
360 else if ( strcmp(__environ[j], "console=ttyb") == 0 ) {
363 /* standard console settings */
364 else if ( strncmp(__environ[j], "console=", 8) == 0 ) {
365 first_terminal=&(__environ[j][8]);
367 else if ( strncmp(__environ[j], "TERM=", 5) == 0) {
368 term_ptr=__environ[j];
372 printf("mounting /proc ...\n");
373 if (mount("/proc","/proc","proc",0,0)) {
374 perror("mounting /proc failed\n");
378 if (get_kernel_revision() >= 2*65536+1*256+71) {
379 /* if >= 2.1.71 kernel, /dev/console is not a symlink anymore:
380 * use it as primary console */
384 /* Make sure /etc/init.d/rc exists */
385 retval= stat(tty_commands[0],&statbuf);
389 configure_terminals( serial_console, run_rc);
393 /* not enough memory to do anything useful*/
394 if (mem_total() < 2000) {
395 retval= stat("/etc/fstab",&statbuf);
397 printf("You do not have enough RAM, sorry.\n");
398 while (1) { sleep(1);}
400 /* Try to turn on swap */
401 static const char * const swapon_args[] = {"/bin/swapon", "-a", 0};
402 waitfor(run("/bin/swapon", swapon_args, console, 0));
403 if (mem_total() < 2000) {
404 printf("You do not have enough RAM, sorry.\n");
405 while (1) { sleep(1);}
411 * Don't modify **argv directly, it would show up in the "ps" display.
412 * I don't want "init" to look like "rc".
414 rc_arguments[0] = tty_commands[0];
415 for ( j = 1; j < argc; j++ ) {
416 rc_arguments[j] = argv[j];
420 arguments[0] = "-sh";
423 /* Ok, now launch the rc script /etc/init.d/rcS and prepare to start up
424 * some VTs on tty1 and tty2 if somebody hits enter
430 if ( pid1 == 0 && tty_commands[0] ) {
431 if ( run_rc == TRUE ) {
432 pid1 = run(tty_commands[0], rc_arguments, first_terminal, 0);
434 pid2 = run(tty_commands[1], arguments, first_terminal, 1);
437 if ( pid2 == 0 && second_terminal && tty_commands[1] ) {
438 pid2 = run(tty_commands[1], arguments, second_terminal, 1);
440 wpid = wait(&status);
441 if ( wpid > 0 && wpid != pid1) {
442 message(log, "pid %d exited, status=%x.\n", wpid, status);
444 if ( wpid == pid2 ) {