oinit.c revision 38589
1/*- 2 * Copyright (c) 1998 Andrzej Bialecki 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: oinit.c,v 1.8 1998/08/11 06:53:47 abial Exp $ 27 */ 28 29/* 30 * A primitive version of init(8) with simplistic user interface 31 */ 32 33#include <sys/types.h> 34#include <sys/param.h> 35#include <sys/mount.h> 36#include <sys/reboot.h> 37#include <sys/time.h> 38#include <sys/resource.h> 39#include <sys/wait.h> 40#include <ctype.h> 41#include <err.h> 42 43#ifdef USE_HISTORY 44#error "Not yet. But it's quite simple to add - patches are welcome!" 45#endif 46 47#include <errno.h> 48#include <fcntl.h> 49#include <libutil.h> 50#include <paths.h> 51#include <setjmp.h> 52#include <signal.h> 53#include <stdio.h> 54#include <string.h> 55#include <syslog.h> 56#include <unistd.h> 57#include <varargs.h> 58 59#define BUFSIZE 1024 60#define MAX_CONS 12 61 62#define NONE 0 63#define SINGLE 1 64#define MULTI 2 65#define DEATH 3 66 67#define FALSE 0 68#define TRUE 1 69 70char cwd[BUFSIZE]; 71char vty[]="0123456789abcdef"; 72char *progname; 73char *motd=NULL; 74int ncons=MAX_CONS; 75int Reboot=FALSE; 76int transition=MULTI; 77int prevtrans=SINGLE; 78jmp_buf machine; 79 80char *trans[]={ "NONE", "SINGLE", "MULTI", "DEATH" }; 81 82extern char **environ; 83 84/* Struct for holding session state */ 85struct sess { 86 char tty[11]; /* vty device path */ 87 pid_t pid; /* pid of process running on it */ 88 int (*func)(int argc, char **argv); 89 /* internal function to run on it (after forking) */ 90} ttys[MAX_CONS]; 91 92/* Struct for built-in command */ 93struct command { 94 char *cmd; /* command name */ 95 char *descr; /* command description */ 96 char *usage; /* usage */ 97 char *example; /* example of usage */ 98 int (*func)(char *); /* callback function */ 99}; 100 101/* Prototypes */ 102int cd __P((char *)); 103int pwd __P((char *)); 104int echo __P((char *)); 105int xit __P((char *)); 106int set __P((char *)); 107int unset __P((char *)); 108int env __P((char *)); 109int help __P((char *)); 110int sourcer __P((char *)); 111void do_command __P((int shell, char *cmdline)); 112void transition_handler __P((int)); 113 114/* Table of built-in functions */ 115struct command bltins[]={ 116 {"cd","Change working directory","cd [dir]","cd /etc",cd}, 117 {"pwd","Print current directory","pwd","pwd",pwd}, 118 {"exit","Exit from shell()","exit","exit",xit}, 119 {"set","Set environment variable","set [VAR=value]","set TERM=cons25",set}, 120 {"unset","Unset environment variable","unset VAR","unset EDITOR",unset}, 121 {"echo","Echo arguments on stdout","echo arg1 arg2 ...","echo Hello World!",echo}, 122 {"env","Print all environment variables","env","env",env}, 123 {".","Source-in a file with commands",". filename",". /etc/rc",sourcer}, 124 {"?","Print this help :-)","? [command]","? set",help}, 125 {NULL,NULL,NULL,NULL,NULL} 126}; 127 128/* 129 * Built-in 'cd <path>' handler 130 */ 131int 132cd(char *path) 133{ 134 if(chdir(path)) return(-1); 135 getcwd(cwd,BUFSIZE); 136 return(0); 137} 138 139/* 140 * Built-in 'pwd' handler 141 */ 142int 143pwd(char *dummy) 144{ 145 146 if(getcwd(cwd,BUFSIZE)==NULL) return(-1); 147 printf("%s\n",cwd); 148 return(0); 149} 150 151/* 152 * Built-in 'exit' handler 153 */ 154int 155xit(char *dummy) 156{ 157 _exit(0); 158} 159 160/* 161 * Built-in 'echo' handler 162 */ 163int 164echo(char *args) 165{ 166 int i=0,j; 167 int len; 168 char c; 169 int s_quote=0,d_quote=0; 170 int sep=0,no_lf=0; 171 172 if(args==NULL) { 173 printf("\n"); 174 return; 175 } 176 len=strlen(args); 177 if(len>=2) { 178 if(args[0]=='-' && args[1]=='n') { 179 no_lf++; 180 i=2; 181 while(i<len && (args[i]==' ' || args[i]=='\t')) i++; 182 } 183 } 184 while(i<len) { 185 c=args[i]; 186 switch(c) { 187 case ' ': 188 case '\t': 189 if(s_quote||d_quote) { 190 putchar(c); 191 } else if(!sep) { 192 putchar(' '); 193 sep=1; 194 } 195 break; 196 case '\\': 197 i++; 198 c=args[i]; 199 switch(c) { 200 case 'n': 201 putchar('\n'); 202 break; 203 case 'b': 204 putchar('\b'); 205 break; 206 case 't': 207 putchar('\t'); 208 break; 209 case 'r': 210 putchar('\r'); 211 break; 212 default: 213 putchar(c); 214 break; 215 } 216 break; 217 case '"': 218 if(!d_quote) { 219 d_quote=1; 220 for(j=i+1;j<len;j++) { 221 if(args[j]=='\\') { 222 j++; 223 continue; 224 } 225 if(args[j]=='"') { 226 d_quote=2; 227 break; 228 } 229 } 230 if(d_quote!=2) { 231 printf("\necho(): unmatched \"\n"); 232 return; 233 } 234 } else d_quote=0; 235 break; 236 case '\'': 237 if(!s_quote) { 238 s_quote=1; 239 for(j=i+1;j<len;j++) { 240 if(args[j]=='\\') { 241 j++; 242 continue; 243 } 244 if(args[j]=='\'') { 245 s_quote=2; 246 break; 247 } 248 } 249 if(s_quote!=2) { 250 printf("\necho(): unmatched '\n"); 251 return; 252 } 253 } else s_quote=0; 254 break; 255 case '`': 256 printf("echo(): backquote not implemented yet!\n"); 257 break; 258 default: 259 sep=0; 260 putchar(c); 261 break; 262 } 263 i++; 264 } 265 if(!no_lf) putchar('\n'); 266 fflush(stdout); 267} 268 269/* 270 * Built-in 'set VAR=value' handler 271 */ 272int 273set(char *var) 274{ 275 int res; 276 277 if(var==NULL) return(env(NULL)); 278 res=putenv(var); 279 if(res) printf("set: %s\n",strerror(errno)); 280 return(res); 281} 282 283/* 284 * Built-in 'env' handler 285 */ 286int 287env(char *dummy) 288{ 289 char **e; 290 291 e=environ; 292 while(*e!=NULL) { 293 printf("%s\n",*e++); 294 } 295 return(0); 296} 297 298/* 299 * Built-in 'unset VAR' handler 300 */ 301int 302unset(char *var) 303{ 304 if(var==NULL) { 305 printf("%s: parameter required.\n",progname); 306 return(-1); 307 } 308 return(unsetenv(var)); 309} 310 311/* 312 * Built-in '?' handler 313 */ 314int 315help(char *cmd) 316{ 317 struct command *x; 318 int found=0; 319 320 if(cmd==NULL) { 321 printf("\nBuilt-in commands:\n"); 322 printf("-------------------\n"); 323 x=bltins; 324 while(x->cmd!=NULL) { 325 printf("%s\t%s\n",x->cmd,x->descr); 326 x++; 327 } 328 printf("\nEnter '? <cmd>' for details.\n\n"); 329 return(0); 330 } else { 331 x=bltins; 332 while(x->cmd!=NULL) { 333 if(strcmp(x->cmd,cmd)==0) { 334 found++; 335 break; 336 } 337 x++; 338 } 339 if(found) { 340 printf("\n%s\t%s:\n",x->cmd,x->descr); 341 printf("\tUsage:\n\t\t%s\n",x->usage); 342 printf("\te.g:\n\t\t%s\n\n",x->example); 343 return(0); 344 } else { 345 printf("\n%s: no such command.\n\n",cmd); 346 return(-1); 347 } 348 } 349} 350 351/* 352 * Signal handler for shell() 353 */ 354void 355shell_sig(int sig) 356{ 357 switch(sig) { 358 case SIGINT: 359 case SIGQUIT: 360 case SIGTERM: 361 /* ignore ? */ 362 break; 363 default: 364 break; 365 } 366} 367 368/* 369 * Built-in '.' handler (read-in and execute commands from file) 370 */ 371int 372sourcer(char *fname) 373{ 374 FILE *fd; 375 char buf[512],*tok,*arg,**av; 376 int ac,len,f,res,i; 377 pid_t pid; 378 char *sep=" \t"; 379 380 fd=fopen(fname,"r"); 381 if(fd==NULL) { 382 printf("Couldn't open file '%s'\n",fname); 383 return(-1); 384 } 385 while(!feof(fd)) { 386 memset(buf,0,512); 387 if(fgets(buf,512,fd)==NULL) continue; 388 if((*buf=='#') || (*buf=='\n')) continue; 389 len=strlen(buf); 390 buf[len-1]='\0'; 391 if(strncmp(buf,"ncons",5)==0) { 392 tok=strtok(buf,sep); 393 tok=strtok(NULL,sep); 394 ncons=atoi(tok); 395 if((ncons<1)||(ncons>MAX_CONS)) { 396 syslog(LOG_EMERG,"%s: bad ncons value; defaulting to %d\n",fname,MAX_CONS); 397 ncons=MAX_CONS; 398 } 399 continue; 400 } else if(strncmp(buf,"motd",4)==0) { 401 tok=strtok(buf,sep); 402 motd=strdup(strtok(NULL,sep)); 403 continue; 404 } else { 405 do_command(0,buf); 406 } 407 /* Next command, please. */ 408 } 409 fclose(fd); 410 syslog(LOG_EMERG,"Done with %s",fname); 411} 412 413void 414do_command(int shell, char *cmdline) 415{ 416 char *tok,*c,*sep=" \t"; 417 char **av; 418 struct command *x; 419 int found,len; 420 int ac,i,f,res; 421 int bg=0; 422 pid_t pid; 423 424 len=strlen(cmdline); 425 if(cmdline[len-1]=='&') { 426 bg++; 427 cmdline[len-1]='\0'; 428 len--; 429 } else bg=0; 430 tok=strtok(cmdline,sep); 431 x=bltins; 432 found=0; 433 while(x->cmd!=NULL) { 434 if(strcmp(x->cmd,tok)==0) { 435 found++; 436 break; 437 } 438 x++; 439 } 440 if(found) { 441 tok=cmdline+strlen(x->cmd)+1; 442 while(*tok && isblank(*tok) && (tok<(cmdline+len))) tok++; 443 if(*tok==NULL) tok=NULL; 444 x->func(tok); 445 return; 446 } 447 ac=0; 448 av=(char **)calloc(((len+1)/2+1),sizeof(char *)); 449 av[ac++]=tok; 450 while((av[ac++]=strtok(NULL,sep))!=NULL) 451 continue; 452 switch((pid=fork())) { 453 case 0: 454 if(shell) { 455 signal(SIGINT,SIG_DFL); 456 signal(SIGQUIT,SIG_DFL); 457 signal(SIGTERM,SIG_DFL); 458 signal(SIGHUP,SIG_DFL); 459 } else { 460 close(0); 461 close(1); 462 close(2); 463 f=open(_PATH_CONSOLE,O_RDWR); 464 dup2(f,0); 465 dup2(f,1); 466 dup2(f,2); 467 if(f>2) close(f); 468 } 469 if(bg) { 470 if(daemon(0,0)) { 471 printf("do_command(%s): failed to run bg: %s\n", 472 av[0],strerror(errno)); 473 _exit(100); 474 } 475 } 476 execvp(av[0],av); 477 /* Something went wrong... */ 478 printf("do_command(%s): %s\n",av[0],strerror(errno)); 479 _exit(100); 480 break; 481 case -1: 482 printf("do_command(): %s\n",strerror(errno)); 483 break; 484 default: 485 while(waitpid(pid,&res,0)!=pid) continue; 486 if(WEXITSTATUS(res)) { 487 printf("%s exited with status %d\n", 488 av[0],WEXITSTATUS(res)); 489 } 490 break; 491 } 492 free(av); 493 return; 494} 495 496/* 497 * This is the user interface. This routine gets executed on each 498 * virtual console serviced by init. 499 * 500 * It works as normal shell does - for each external command it forks 501 * and execs, for each internal command just executes a function. 502 */ 503 504int 505shell(int argc, char **argv) 506{ 507 char buf[BUFSIZE]; 508 char *prompt=" # "; 509 int fd; 510 int res; 511 pid_t mypid; 512 513 if(motd!=NULL) { 514 if((fd=open(motd,O_RDONLY))!=-1) { 515 do { 516 res=read(fd,buf,BUFSIZE); 517 res=write(1,buf,res); 518 } while(res>0); 519 close(fd); 520 } 521 } 522 523 printf("\n\n+=========================================================+\n"); 524 printf("| Built-in shell() (enter '?' for short help on commands) |\n"); 525 printf("+=========================================================+\n\n"); 526 getcwd(cwd,BUFSIZE); 527 mypid=getpid(); 528 signal(SIGINT,shell_sig); 529 signal(SIGQUIT,shell_sig); 530 signal(SIGTERM,shell_sig); 531 while(!feof(stdin)) { 532 memset(buf,0,BUFSIZE); 533 printf("(%d)%s%s",mypid,cwd,prompt); 534 fflush(stdout); 535 if(fgets(buf,BUFSIZE-1,stdin)==NULL) continue; 536 buf[strlen(buf)-1]='\0'; 537 if(strlen(buf)==0) continue; 538 do_command(1,buf); 539 } 540 return(0); 541} 542 543/* 544 * Stub for executing some external program on a console. This is called 545 * from previously forked copy of our process, so that exec is ok. 546 */ 547int 548external_cmd(int argc, char **argv) 549{ 550 execvp(argv[0],argv); 551} 552 553/* 554 * Acquire vty and properly attach ourselves to it. 555 * Also, build basic environment for running user interface. 556 */ 557 558int 559start_session(int vty, int argc, char **argv) 560{ 561 int fd; 562 char *t; 563 564 close(0); 565 close(1); 566 close(2); 567 revoke(ttys[vty].tty); 568 fd=open(ttys[vty].tty,O_RDWR); 569 dup2(fd,0); 570 dup2(fd,1); 571 dup2(fd,2); 572 if(fd>2) close(fd); 573 login_tty(fd); 574 setpgid(0,getpid()); 575 putenv("TERM=cons25"); 576 putenv("HOME=/"); 577 putenv("PATH=/stand:/bin:/usr/bin:/sbin:."); 578 signal(SIGHUP,SIG_DFL); 579 signal(SIGINT,SIG_DFL); 580 signal(SIGQUIT,SIG_DFL); 581 signal(SIGTERM,SIG_DFL); 582 chdir("/"); 583 t=(char *)(rindex(ttys[vty].tty,'/')+1); 584 printf("\n\n\nStarting session on %s.\n",t); 585 ttys[vty].func(argc,argv); 586 _exit(0); 587} 588 589/* 590 * Execute system startup script /etc/rc 591 * 592 * (Of course if you don't like it - I don't - you can run anything you 593 * want here. Perhaps it would be useful just to read some config DB and 594 * do these things ourselves, avoiding forking lots of shells and scripts.) 595 */ 596 597/* If OINIT_RC is defined, oinit will use it's own configuration file, 598 * /etc/oinit.rc. It's format is described below. Otherwise, it will use 599 * normal /etc/rc interpreted by Bourne shell. 600 */ 601#ifndef OINIT_RC 602void 603runcom() 604{ 605 char *argv[3]; 606 pid_t pid; 607 int st; 608 int fd; 609 610 if((pid=fork())==0) { 611 /* child */ 612 close(0); 613 close(1); 614 close(2); 615 fd=open(_PATH_CONSOLE,O_RDWR); 616 dup2(fd,0); 617 dup2(fd,1); 618 dup2(fd,2); 619 if(fd>2) close(fd); 620 argv[0]="-sh"; 621 argv[1]="/etc/rc"; 622 argv[2]=0; 623 execvp("/bin/sh",argv); 624 printf("runcom(): %s\n",strerror(errno)); 625 _exit(1); 626 } 627 /* Wait for child to exit */ 628 while(pid!=waitpid(pid,(int *)0,0)) continue; 629 return; 630} 631#else 632/* Alternative /etc/rc - default is /etc/oinit.rc. Its format is as follows: 633 * - each empty line or line beginning with a '#' is discarded 634 * - any other line must contain a keyword, or a (nonblocking) command to run. 635 * 636 * Thus far, the following keywords are defined: 637 * ncons <number> number of virtual consoles to open 638 * motd <pathname> full path to motd file 639 * 640 * Examples of commands to run: 641 * 642 * ifconfig lo0 inet 127.0.0.1 netmask 255.0.0.0 643 * ifconfig ed0 inet 148.81.168.10 netmask 255.255.255.0 644 * kbdcontrol -l /usr/share/syscons/my_map.kbd 645 */ 646void 647runcom(char *fname) 648{ 649 sourcer(fname); 650} 651#endif 652 653int 654run_multi() 655{ 656 int i,j; 657 pid_t pid; 658 int found; 659 660 /* Run /etc/rc if not in single user */ 661#ifndef OINIT_RC 662 if(prevtrans==SINGLE) runcom(); 663#else 664 if(prevtrans==SINGLE) runcom(OINIT_RC); 665#endif 666 if(transition!=MULTI) return(-1); 667 668 syslog(LOG_EMERG,"*** Starting multi-user mode ***"); 669 670 /* Fork shell interface for each console */ 671 for(i=0;i<ncons;i++) { 672 if(ttys[i].pid==0) { 673 switch(pid=fork()) { 674 case 0: 675 start_session(i,0,NULL); 676 break; 677 case -1: 678 printf("%s: %s\n",progname,strerror(errno)); 679 break; 680 default: 681 ttys[i].pid=pid; 682 break; 683 } 684 } 685 } 686 /* Initialize any other services we'll use - most probably this will 687 * be a 'telnet' service (some day...). 688 */ 689 /* */ 690 691 /* Emulate multi-user */ 692 while(transition==MULTI) { 693 /* XXX Modify this to allow for checking for the input on 694 * XXX listening sockets, and forking a 'telnet' service. 695 */ 696 /* */ 697 698 /* Wait for any process to exit */ 699 pid=waitpid(-1,(int *)0,0); 700 if(pid==-1) continue; 701 found=0; 702 j=-1; 703 /* search if it's one of our sessions */ 704 for(i=0;i<ncons;i++) { 705 if(ttys[i].pid==pid) { 706 found++; 707 j=i; 708 ttys[j].pid=0; 709 break; 710 } 711 } 712 if(!found) { 713 /* Just collect the process's status */ 714 continue; 715 } else { 716 /* restart shell() on a console, if it died */ 717 if(transition!=MULTI) return(0); 718 switch(pid=fork()) { 719 case 0: 720 sleep(1); 721 start_session(j,0,NULL); 722 break; 723 case -1: 724 printf("%s: %s\n",progname,strerror(errno)); 725 break; 726 default: 727 ttys[j].pid=pid; 728 break; 729 } 730 } 731 } 732} 733 734int clang; 735 736void 737kill_timer(int sig) 738{ 739 clang=1; 740} 741 742kill_ttys() 743{ 744} 745 746/* 747 * Start a shell on ttyv0 (i.e. the console). 748 */ 749 750int 751run_single() 752{ 753 int i; 754 pid_t pid,wpid; 755 static int sigs[2]={SIGTERM,SIGKILL}; 756 757 syslog(LOG_EMERG,"*** Starting single-user mode ***"); 758 /* Kill all existing sessions */ 759 syslog(LOG_EMERG,"Killing all existing sessions..."); 760 for(i=0;i<MAX_CONS;i++) { 761 kill(ttys[i].pid,SIGHUP); 762 ttys[i].pid=0; 763 } 764 for(i=0;i<2;i++) { 765 if(kill(-1,sigs[i])==-1 && errno==ESRCH) break; 766 clang=0; 767 alarm(10); 768 do { 769 pid=waitpid(-1,(int *)0,WUNTRACED); 770 if(errno==EINTR) continue; 771 else break; 772 } while (clang==0); 773 } 774 if(errno!=ECHILD) { 775 syslog(LOG_EMERG,"Some processes would not die; ps -axl advised"); 776 } 777 /* Single-user */ 778 switch(pid=fork()) { 779 case 0: 780 start_session(0,0,NULL); 781 break; 782 case -1: 783 printf("%s: %s\n",progname,strerror(errno)); 784 printf("The system is seriously hosed. I'm dying...\n"); 785 transition=DEATH; 786 return(-1); 787 break; 788 default: 789 do { 790 wpid=waitpid(pid,(int *)0,WUNTRACED); 791 } while(wpid!=pid && transition==SINGLE); 792 if(transition!=DEATH) { 793 prevtrans=transition; 794 transition=MULTI; 795 } 796 break; 797 } 798 return(0); 799} 800 801/* 802 * Transition handler - installed as signal handler. 803 */ 804 805void 806transition_handler(int sig) 807{ 808 809 switch(sig) { 810 case SIGHUP: 811 case SIGTERM: 812 prevtrans=transition; 813 transition=SINGLE; 814 syslog(LOG_EMERG,"*** Going from %s -> %s\n",trans[prevtrans],trans[transition]); 815 if(prevtrans!=transition) longjmp(machine,sig); 816 break; 817 case SIGINT: 818 case SIGQUIT: 819 prevtrans=transition; 820 transition=DEATH; 821 syslog(LOG_EMERG,"*** Going from %s -> %s\n",trans[prevtrans],trans[transition]); 822 if(prevtrans!=transition) longjmp(machine,sig); 823 break; 824 default: 825 syslog(LOG_EMERG,"pid=%d sig=%s (ignored)\n",getpid(),sys_siglist[sig]); 826 break; 827 } 828} 829 830/* 831 * Change system state appropriately to the signals 832 */ 833 834int 835transition_machine() 836{ 837 int i; 838 839 while(transition!=DEATH) { 840 switch(transition) { 841 case MULTI: 842 run_multi(); 843 break; 844 case SINGLE: 845 run_single(); 846 break; 847 } 848 } 849 syslog(LOG_EMERG,"Killing all existing sessions..."); 850 /* Kill all sessions */ 851 kill(-1,SIGKILL); 852 /* Be nice and wait for them */ 853 while(waitpid(-1,(int *)0,WNOHANG|WUNTRACED)>0) continue; 854 unmount("/",0); 855 reboot(RB_AUTOBOOT); 856 /* NOTREACHED */ 857} 858 859int 860main(int argc, char **argv) 861{ 862 int devfs=0,c,i; 863 864 /* These are copied from real init(8) */ 865 if(getuid()!=0) 866 errx(1,"%s",strerror(EPERM)); 867 openlog("init",LOG_CONS|LOG_ODELAY,LOG_AUTH); 868 if(setsid()<0) 869 warn("initial setsid() failed"); 870 if(setlogin("root")<0) 871 warn("setlogin() failed"); 872 873 close(0); 874 close(1); 875 close(2); 876 chdir("/"); 877 878 progname=rindex(argv[0],'/'); 879 if(progname==NULL) { 880 progname=argv[0]; 881 } else progname++; 882 883 transition=MULTI; 884 885 /* We must recognize the same options as real init does */ 886 while((c=getopt(argc,argv,"dsf"))!=-1) { 887 switch(c) { 888 case 'd': 889 devfs=1; 890 break; 891 case 's': 892 transition=SINGLE; 893 break; 894 case 'f': 895 break; 896 default: 897 printf("%s: unrecognized flag '-%c'\n",progname,c); 898 break; 899 } 900 } 901 if(devfs) 902 mount("devfs","/dev",MNT_NOEXEC|MNT_RDONLY,0); 903 904 /* Fill in the sess structures. */ 905 /* XXX Really, should be filled basing on config file. */ 906 for(i=0;i<MAX_CONS;i++) { 907 sprintf(ttys[i].tty,"/dev/ttyv%c",vty[i]); 908 ttys[i].pid=0; 909 ttys[i].func=shell; 910 } 911 912 getcwd(cwd,BUFSIZE); 913 914 signal(SIGINT,transition_handler); 915 signal(SIGQUIT,transition_handler); 916 signal(SIGTERM,transition_handler); 917 signal(SIGHUP,transition_handler); 918 signal(SIGALRM,kill_timer); 919 920 setjmp(machine); 921 transition_machine(transition); 922 /* NOTREACHED */ 923 exit(100); 924} 925