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 get_kernel_revision()
201 int major=0, minor=0, patch=0;
203 f = fopen("/proc/sys/kernel/osrelease","r");
204 fscanf(f,"%d.%d.%d",&major,&minor,&patch);
206 return major*65536 + minor*256 + patch;
211 shutdown_system(void)
213 static const char * const umount_args[] = {"/bin/umount", "-a", "-n", 0};
214 static const char * const swapoff_args[] = {"/bin/swapoff", "-a", 0};
216 message(console, "The system is going down NOW !!");
218 /* Allow Ctrl-Alt-Del to reboot system. */
219 reboot(RB_ENABLE_CAD);
221 /* Send signals to every process _except_ pid 1 */
222 message(console, "Sending SIGHUP to all processes.\r\n");
226 message(console, "Sending SIGKILL to all processes.\r\n");
229 waitfor(run("/bin/swapoff", swapoff_args, console, 0));
230 waitfor(run("/bin/umount", umount_args, console, 0));
232 if (get_kernel_revision() <= 2*65536+2*256+11) {
233 /* Removed bdflush call, kupdate in kernels >2.2.11 */
243 message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
244 reboot( RB_HALT_SYSTEM);
249 reboot_signal(int sig)
252 message(console, "Please stand by while rebooting the system.\r\n");
253 reboot( RB_AUTOBOOT);
258 configure_terminals( int serial_cons, int single_user_mode )
261 struct serial_struct sr;
265 switch (serial_cons) {
268 * identify the real console backend and try to make use of it */
269 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
270 sprintf( console, "/dev/ttyS%d", sr.line );
271 serial_cons = sr.line+1;
273 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
274 sprintf( console, "/dev/tty%d", vt.v_active );
278 /* unknown backend: fallback to /dev/console */
279 strcpy( console, "/dev/console" );
285 strcpy( console, "/dev/cua0" );
288 strcpy( console, "/dev/cua1" );
293 strcpy( console, tty );
294 if (!strncmp( tty, "/dev/cua", 8 ))
298 /* falls back to /dev/tty1 if an error occurs */
299 strcpy( console, default_console );
302 first_terminal = console;
304 if (serial_cons > 0 && !strncmp(term_ptr,"TERM=linux",10))
305 term_ptr = "TERM=vt100";
308 /* disable other but the first terminal:
309 * VT is not initialized anymore on 2.2 kernel when booting from
310 * serial console, therefore modprobe is flooding the display with
311 * "can't locate module char-major-4" messages. */
318 init_main(int argc, char * * argv)
320 const char * rc_arguments[100];
321 const char * arguments[100];
327 const char * tty_commands[3] = { "etc/init.d/rcS", "bin/sh"};
328 int serial_console = 0;
332 * If I am started as /linuxrc instead of /sbin/init, I don't have the
333 * environment that init expects. I can't fix the signal behavior. Try
334 * to divorce from the controlling terminal with setsid(). This won't work
335 * if I am the process group leader.
339 signal(SIGUSR1, halt_signal);
340 signal(SIGUSR2, reboot_signal);
341 signal(SIGINT, reboot_signal);
342 signal(SIGTERM, reboot_signal);
344 reboot(RB_DISABLE_CAD);
346 message(log, "%s: started. ", argv[0]);
348 for ( j = 1; j < argc; j++ ) {
349 if ( strcmp(argv[j], "single") == 0 ) {
351 tty_commands[0] = "bin/sh";
355 for ( j = 0; __environ[j] != 0; j++ ) {
356 if ( strncmp(__environ[j], "tty", 3) == 0
357 && __environ[j][3] >= '1'
358 && __environ[j][3] <= '2'
359 && __environ[j][4] == '=' ) {
360 const char * s = &__environ[j][5];
362 if ( *s == 0 || strcmp(s, "off") == 0 )
365 tty_commands[__environ[j][3] - '1'] = s;
367 /* Should catch the syntax of Sparc kernel console setting. */
368 /* The kernel does not recognize a serial console when getting*/
369 /* console=/dev/ttySX !! */
370 else if ( strcmp(__environ[j], "console=ttya") == 0 ) {
373 else if ( strcmp(__environ[j], "console=ttyb") == 0 ) {
376 /* standard console settings */
377 else if ( strncmp(__environ[j], "console=", 8) == 0 ) {
378 first_terminal=&(__environ[j][8]);
380 else if ( strncmp(__environ[j], "TERM=", 5) == 0) {
381 term_ptr=__environ[j];
385 printf("mounting /proc ...\n");
386 if (mount("/proc","/proc","proc",0,0)) {
387 perror("mounting /proc failed\n");
391 if (get_kernel_revision() >= 2*65536+1*256+71) {
392 /* if >= 2.1.71 kernel, /dev/console is not a symlink anymore:
393 * use it as primary console */
397 /* Make sure /etc/init.d/rc exists */
398 retval= stat(tty_commands[0],&statbuf);
402 configure_terminals( serial_console, run_rc);
406 /* not enough memory to do anything useful*/
407 if (mem_total() < 2000) {
408 retval= stat("/etc/fstab",&statbuf);
410 printf("You do not have enough RAM, sorry.\n");
411 while (1) { sleep(1);}
413 /* Try to turn on swap */
414 static const char * const swapon_args[] = {"/bin/swapon", "-a", 0};
415 waitfor(run("/bin/swapon", swapon_args, console, 0));
416 if (mem_total() < 2000) {
417 printf("You do not have enough RAM, sorry.\n");
418 while (1) { sleep(1);}
424 * Don't modify **argv directly, it would show up in the "ps" display.
425 * I don't want "init" to look like "rc".
427 rc_arguments[0] = tty_commands[0];
428 for ( j = 1; j < argc; j++ ) {
429 rc_arguments[j] = argv[j];
433 arguments[0] = "-sh";
436 /* Ok, now launch the rc script /etc/init.d/rcS and prepare to start up
437 * some VTs on tty1 and tty2 if somebody hits enter
443 if ( pid1 == 0 && tty_commands[0] ) {
444 if ( run_rc == TRUE ) {
445 pid1 = run(tty_commands[0], rc_arguments, first_terminal, 0);
447 pid2 = run(tty_commands[1], arguments, first_terminal, 1);
450 if ( pid2 == 0 && second_terminal && tty_commands[1] ) {
451 pid2 = run(tty_commands[1], arguments, second_terminal, 1);
453 wpid = wait(&status);
454 if ( wpid > 0 && wpid != pid1) {
455 message(log, "pid %d exited, status=%x.\n", wpid, status);
457 if ( wpid == pid2 ) {