1 /* $NetBSD: main.c,v 1.85 2020/02/07 01:25:08 fox Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\ 38 The Regents of the University of California. All rights reserved."); 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95"; 44 #else 45 __RCSID("$NetBSD: main.c,v 1.85 2020/02/07 01:25:08 fox Exp $"); 46 #endif 47 #endif /* not lint */ 48 49 #include <errno.h> 50 #include <stdio.h> 51 #include <signal.h> 52 #include <sys/stat.h> 53 #include <unistd.h> 54 #include <stdlib.h> 55 #include <locale.h> 56 #include <fcntl.h> 57 58 59 #include "shell.h" 60 #include "main.h" 61 #include "mail.h" 62 #include "options.h" 63 #include "builtins.h" 64 #include "output.h" 65 #include "parser.h" 66 #include "nodes.h" 67 #include "expand.h" 68 #include "eval.h" 69 #include "jobs.h" 70 #include "input.h" 71 #include "trap.h" 72 #include "var.h" 73 #include "show.h" 74 #include "memalloc.h" 75 #include "error.h" 76 #include "init.h" 77 #include "mystring.h" 78 #include "exec.h" 79 #include "cd.h" 80 #include "redir.h" 81 82 #define PROFILE 0 83 84 int rootpid; 85 int rootshell; 86 struct jmploc main_handler; 87 int max_user_fd; 88 #if PROFILE 89 short profile_buf[16384]; 90 extern int etext(); 91 #endif 92 93 STATIC void read_profile(const char *); 94 95 /* 96 * Main routine. We initialize things, parse the arguments, execute 97 * profiles if we're a login shell, and then call cmdloop to execute 98 * commands. The setjmp call sets up the location to jump to when an 99 * exception occurs. When an exception occurs the variable "state" 100 * is used to figure out how far we had gotten. 101 */ 102 103 int 104 main(int argc, char **argv) 105 { 106 struct stackmark smark; 107 volatile int state; 108 char *shinit; 109 uid_t uid; 110 gid_t gid; 111 sigset_t mask; 112 113 /* 114 * If we happen to be invoked with SIGCHLD ignored, we cannot 115 * successfully do almost anything. Perhaps we should remember 116 * its state and pass it on ignored to children if it was ignored 117 * on entry, but that seems like just leaving the shit on the 118 * footpath for someone else to fall into... 119 */ 120 (void)signal(SIGCHLD, SIG_DFL); 121 /* 122 * Similarly, SIGCHLD must not be blocked 123 */ 124 sigemptyset(&mask); 125 sigaddset(&mask, SIGCHLD); 126 sigprocmask(SIG_UNBLOCK, &mask, NULL); 127 128 uid = getuid(); 129 gid = getgid(); 130 131 max_user_fd = fcntl(0, F_MAXFD); 132 if (max_user_fd < 2) 133 max_user_fd = 2; 134 135 setlocale(LC_ALL, ""); 136 137 posix = getenv("POSIXLY_CORRECT") != NULL; 138 #if PROFILE 139 monitor(4, etext, profile_buf, sizeof profile_buf, 50); 140 #endif 141 state = 0; 142 if (setjmp(main_handler.loc)) { 143 /* 144 * When a shell procedure is executed, we raise the 145 * exception EXSHELLPROC to clean up before executing 146 * the shell procedure. 147 */ 148 switch (exception) { 149 case EXSHELLPROC: 150 rootpid = getpid(); 151 rootshell = 1; 152 minusc = NULL; 153 state = 3; 154 break; 155 156 case EXEXEC: 157 exitstatus = exerrno; 158 break; 159 160 case EXERROR: 161 exitstatus = 2; 162 break; 163 164 default: 165 break; 166 } 167 168 if (exception != EXSHELLPROC) { 169 if (state == 0 || iflag == 0 || ! rootshell || 170 exception == EXEXIT) 171 exitshell(exitstatus); 172 } 173 reset(); 174 if (exception == EXINT) { 175 out2c('\n'); 176 flushout(&errout); 177 } 178 popstackmark(&smark); 179 FORCEINTON; /* enable interrupts */ 180 if (state == 1) 181 goto state1; 182 else if (state == 2) 183 goto state2; 184 else if (state == 3) 185 goto state3; 186 else 187 goto state4; 188 } 189 handler = &main_handler; 190 #ifdef DEBUG 191 #if DEBUG >= 2 192 debug = 1; /* this may be reset by procargs() later */ 193 #endif 194 opentrace(); 195 trputs("Shell args: "); trargs(argv); 196 #if DEBUG >= 3 197 set_debug(((DEBUG)==3 ? "_@" : "++"), 1); 198 #endif 199 #endif 200 rootpid = getpid(); 201 rootshell = 1; 202 init(); 203 initpwd(); 204 setstackmark(&smark); 205 procargs(argc, argv); 206 207 /* 208 * Limit bogus system(3) or popen(3) calls in setuid binaries, 209 * by requiring the -p flag 210 */ 211 if (!pflag && (uid != geteuid() || gid != getegid())) { 212 setuid(uid); 213 setgid(gid); 214 /* PS1 might need to be changed accordingly. */ 215 choose_ps1(); 216 } 217 218 if (argv[0] && argv[0][0] == '-') { 219 state = 1; 220 read_profile("/etc/profile"); 221 state1: 222 state = 2; 223 read_profile(".profile"); 224 } 225 state2: 226 state = 3; 227 if ((iflag || !posix) && 228 getuid() == geteuid() && getgid() == getegid()) { 229 struct stackmark env_smark; 230 231 setstackmark(&env_smark); 232 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 233 state = 3; 234 read_profile(expandenv(shinit)); 235 } 236 popstackmark(&env_smark); 237 } 238 state3: 239 state = 4; 240 line_number = 1; /* undo anything from profile files */ 241 242 if (sflag == 0 || minusc) { 243 static int sigs[] = { 244 SIGINT, SIGQUIT, SIGHUP, 245 #ifdef SIGTSTP 246 SIGTSTP, 247 #endif 248 SIGPIPE 249 }; 250 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0])) 251 size_t i; 252 253 for (i = 0; i < SIGSSIZE; i++) 254 setsignal(sigs[i], 0); 255 } 256 257 if (minusc) 258 evalstring(minusc, sflag ? 0 : EV_EXIT); 259 260 if (sflag || minusc == NULL) { 261 state4: /* XXX ??? - why isn't this before the "if" statement */ 262 cmdloop(1); 263 if (iflag) { 264 out2str("\n"); 265 flushout(&errout); 266 } 267 } 268 #if PROFILE 269 monitor(0); 270 #endif 271 line_number = plinno; 272 exitshell(exitstatus); 273 /* NOTREACHED */ 274 } 275 276 277 /* 278 * Read and execute commands. "Top" is nonzero for the top level command 279 * loop; it turns on prompting if the shell is interactive. 280 */ 281 282 void 283 cmdloop(int top) 284 { 285 union node *n; 286 struct stackmark smark; 287 int inter; 288 int numeof = 0; 289 enum skipstate skip; 290 291 CTRACE(DBG_ALWAYS, ("cmdloop(%d) called\n", top)); 292 setstackmark(&smark); 293 for (;;) { 294 if (pendingsigs) 295 dotrap(); 296 inter = 0; 297 if (iflag == 1 && top) { 298 inter = 1; 299 showjobs(out2, SHOW_CHANGED); 300 chkmail(0); 301 flushout(&errout); 302 nflag = 0; 303 } 304 n = parsecmd(inter); 305 VXTRACE(DBG_PARSE|DBG_EVAL|DBG_CMDS,("cmdloop: "),showtree(n)); 306 if (n == NEOF) { 307 if (!top || numeof >= 50) 308 break; 309 if (nflag) 310 break; 311 if (!stoppedjobs()) { 312 if (!iflag || !Iflag) 313 break; 314 out2str("\nUse \"exit\" to leave shell.\n"); 315 } 316 numeof++; 317 } else if (n != NULL && nflag == 0) { 318 job_warning = (job_warning == 2) ? 1 : 0; 319 numeof = 0; 320 evaltree(n, 0); 321 } 322 rststackmark(&smark); 323 324 /* 325 * Any SKIP* can occur here! SKIP(FUNC|BREAK|CONT) occur when 326 * a dotcmd is in a loop or a function body and appropriate 327 * built-ins occurs in file scope in the sourced file. Values 328 * other than SKIPFILE are reset by the appropriate eval*() 329 * that contained the dotcmd() call. 330 */ 331 skip = current_skipstate(); 332 if (skip != SKIPNONE) { 333 if (skip == SKIPFILE) 334 stop_skipping(); 335 break; 336 } 337 } 338 popstackmark(&smark); 339 } 340 341 342 343 /* 344 * Read /etc/profile or .profile. Return on error. 345 */ 346 347 STATIC void 348 read_profile(const char *name) 349 { 350 int fd; 351 int xflag_set = 0; 352 int vflag_set = 0; 353 354 if (*name == '\0') 355 return; 356 357 INTOFF; 358 if ((fd = open(name, O_RDONLY)) >= 0) 359 setinputfd(fd, 1); 360 INTON; 361 if (fd < 0) 362 return; 363 /* -q turns off -x and -v just when executing init files */ 364 if (qflag) { 365 if (xflag) 366 xflag = 0, xflag_set = 1; 367 if (vflag) 368 vflag = 0, vflag_set = 1; 369 } 370 (void)set_dot_funcnest(1); /* allow profile to "return" */ 371 cmdloop(0); 372 (void)set_dot_funcnest(0); 373 if (qflag) { 374 if (xflag_set) 375 xflag = 1; 376 if (vflag_set) 377 vflag = 1; 378 } 379 popfile(); 380 } 381 382 383 384 /* 385 * Read a file containing shell functions. 386 */ 387 388 void 389 readcmdfile(char *name) 390 { 391 int fd; 392 393 INTOFF; 394 if ((fd = open(name, O_RDONLY)) >= 0) 395 setinputfd(fd, 1); 396 else 397 error("Can't open %s", name); 398 INTON; 399 cmdloop(0); 400 popfile(); 401 } 402 403 404 405 int 406 exitcmd(int argc, char **argv) 407 { 408 if (stoppedjobs()) 409 return 0; 410 if (argc > 1) 411 exitshell(number(argv[1])); 412 else 413 exitshell_savedstatus(); 414 /* NOTREACHED */ 415 } 416