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"
71 ,const char * const * arguments
72 ,const char * terminal
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 "Please 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 the installation system.
143 write(1, press_enter, sizeof(press_enter) - 1);
147 message(log, "Executing ");
150 message(log, "%s ", *arg++);
153 execve(program, (char * *)arguments, (char * *)environment);
154 message(log, "%s: could not execute: %s.\r\n", program, strerror(errno));
166 const char pattern[]="MemTotal:";
168 f=fopen("/proc/meminfo","r");
169 while (NULL != fgets(s,79,f)) {
170 p=strstr(s, pattern);
173 return(atoi(p+strlen(pattern)));
185 f=fopen("/proc/sys/vm/freepages","r");
189 f=fopen("/proc/sys/vm/freepages","w");
190 fprintf(f,"30\t40\t50\n");
191 printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
197 shutdown_system(int do_reboot)
199 static const char * const umount_args[] = {"/bin/umount", "-a", "-n", 0};
202 /* Allow Ctrl-Alt-Del to reboot system. */
203 reboot(RB_ENABLE_CAD);
205 /* Send signals to every process _except_ pid 1 */
206 message(console, "Sending SIGHUP to all processes.\r\n");
210 message(console, "Sending SIGKILL to all processes.\r\n");
213 waitfor(run("/bin/umount", umount_args, console, 0));
217 reboot(do_reboot ?RB_AUTOBOOT : RB_HALT_SYSTEM);
228 reboot_signal(int sig)
237 /* initrd doesn't work anyway */
241 /* This is used on the initial ramdisk */
243 /* message(log, "Init exiting.");
249 configure_terminals( int serial_cons );
252 init_main(struct FileInfo * i, int argc, char * * argv)
254 static const char * const rc = "etc/rc";
255 const char * arguments[100];
262 #ifndef INCLUDE_DINSTALL
263 const char * tty_commands[2] = { "bin/sh", "bin/sh"};
265 const char * tty_commands[2] = { "sbin/dinstall", "bin/sh"};
268 int serial_console = 0;
271 * If I am started as /linuxrc instead of /sbin/init, I don't have the
272 * environment that init expects. I can't fix the signal behavior. Try
273 * to divorce from the controlling terminal with setsid(). This won't work
274 * if I am the process group leader.
278 signal(SIGUSR1, halt_signal);
279 signal(SIGUSR2, reboot_signal);
280 signal(SIGINT, reboot_signal);
281 signal(SIGTERM, exit_signal);
283 reboot(RB_DISABLE_CAD);
285 message(log, "%s: started. ", argv[0]);
287 for ( j = 1; j < argc; j++ ) {
288 if ( strcmp(argv[j], "single") == 0 ) {
290 tty_commands[0] = "bin/sh";
294 for ( j = 0; __environ[j] != 0; j++ ) {
295 if ( strncmp(__environ[j], "tty", 3) == 0
296 && __environ[j][3] >= '1'
297 && __environ[j][3] <= '2'
298 && __environ[j][4] == '=' ) {
299 const char * s = &__environ[j][5];
301 if ( *s == 0 || strcmp(s, "off") == 0 )
304 tty_commands[__environ[j][3] - '1'] = s;
306 /* Should catch the syntax of Sparc kernel console setting. */
307 /* The kernel does not recognize a serial console when getting*/
308 /* console=/dev/ttySX !! */
309 else if ( strcmp(__environ[j], "console=ttya") == 0 ) {
312 else if ( strcmp(__environ[j], "console=ttyb") == 0 ) {
315 /* standard console settings */
316 else if ( strncmp(__environ[j], "console=", 8) == 0 ) {
317 first_terminal=&(__environ[j][8]);
319 else if ( strncmp(__environ[j], "TERM=", 5) == 0) {
320 term_ptr=__environ[j];
323 configure_terminals( serial_console );
325 printf("mounting /proc ...\n");
326 if (mount("/proc","/proc","proc",0,0)) {
327 perror("mounting /proc failed\n");
333 if (mem_total() < 3500) { /* not enough memory for standard install */
335 retval= stat("/etc/swappartition",&statbuf);
338 You do not have enough RAM, hence you must boot using the Boot Disk
339 for Low Memory systems.
341 Read the instructions in the install.html file.
344 } else { /* everything OK */
347 f=fopen("/etc/swappartition","r");
350 *(strstr(swap,"\n"))='\0';
352 if (swapon(swap,0)) {
353 perror("swapon failed\n");
355 f=fopen("/etc/swaps","w");
356 fprintf(f,"%s none swap rw 0 0",swap);
364 * Don't modify **argv directly, it would show up in the "ps" display.
365 * I don't want "init" to look like "rc".
368 for ( j = 1; j < argc; j++ ) {
369 arguments[j] = argv[j];
374 waitfor(run(rc, arguments, console, 0));
376 if ( 0 == create_swap) {
377 if (unlink("/etc/swappartition")) {
378 perror("unlinking /etc/swappartition");
382 arguments[0] = "-sh";
388 if ( pid1 == 0 && tty_commands[0] ) {
389 /* Ask before starting a shell */
391 arguments[0] = tty_commands[0];
393 pid1 = run(tty_commands[0], arguments, first_terminal, 0);
395 if ( pid2 == 0 && tty_commands[1] )
396 pid2 = run(tty_commands[1], arguments, second_terminal, 1);
397 wpid = wait(&status);
400 message(log, "pid %d exited, status=%x.\n", wpid, status);
402 if ( wpid == pid1 ) {
411 configure_terminals( int serial_cons )
413 //struct stat statbuf;
416 switch (serial_cons) {
418 strcpy( console, "/dev/ttyS0" );
421 strcpy( console, "/dev/ttyS1" );
426 strcpy( console, tty );
427 if (!strncmp( tty, "/dev/ttyS", 9 ))
431 /* falls back to /dev/tty1 if an error occurs */
432 strcpy( console, default_console );
435 first_terminal = console;
436 if (serial_cons && !strncmp(term_ptr,"TERM=linux",10))
437 term_ptr = "TERM=vt100";