1 /* $NetBSD: main.c,v 1.82 2019/02/09 09:33:20 kre 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.82 2019/02/09 09:33:20 kre 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 112 uid = getuid(); 113 gid = getgid(); 114 115 max_user_fd = fcntl(0, F_MAXFD); 116 if (max_user_fd < 2) 117 max_user_fd = 2; 118 119 setlocale(LC_ALL, ""); 120 121 posix = getenv("POSIXLY_CORRECT") != NULL; 122 #if PROFILE 123 monitor(4, etext, profile_buf, sizeof profile_buf, 50); 124 #endif 125 state = 0; 126 if (setjmp(main_handler.loc)) { 127 /* 128 * When a shell procedure is executed, we raise the 129 * exception EXSHELLPROC to clean up before executing 130 * the shell procedure. 131 */ 132 switch (exception) { 133 case EXSHELLPROC: 134 rootpid = getpid(); 135 rootshell = 1; 136 minusc = NULL; 137 state = 3; 138 break; 139 140 case EXEXEC: 141 exitstatus = exerrno; 142 break; 143 144 case EXERROR: 145 exitstatus = 2; 146 break; 147 148 default: 149 break; 150 } 151 152 if (exception != EXSHELLPROC) { 153 if (state == 0 || iflag == 0 || ! rootshell || 154 exception == EXEXIT) 155 exitshell(exitstatus); 156 } 157 reset(); 158 if (exception == EXINT) { 159 out2c('\n'); 160 flushout(&errout); 161 } 162 popstackmark(&smark); 163 FORCEINTON; /* enable interrupts */ 164 if (state == 1) 165 goto state1; 166 else if (state == 2) 167 goto state2; 168 else if (state == 3) 169 goto state3; 170 else 171 goto state4; 172 } 173 handler = &main_handler; 174 #ifdef DEBUG 175 #if DEBUG >= 2 176 debug = 1; /* this may be reset by procargs() later */ 177 #endif 178 opentrace(); 179 trputs("Shell args: "); trargs(argv); 180 #if DEBUG >= 3 181 set_debug(((DEBUG)==3 ? "_@" : "++"), 1); 182 #endif 183 #endif 184 rootpid = getpid(); 185 rootshell = 1; 186 init(); 187 initpwd(); 188 setstackmark(&smark); 189 procargs(argc, argv); 190 191 /* 192 * Limit bogus system(3) or popen(3) calls in setuid binaries, 193 * by requiring the -p flag 194 */ 195 if (!pflag && (uid != geteuid() || gid != getegid())) { 196 setuid(uid); 197 setgid(gid); 198 /* PS1 might need to be changed accordingly. */ 199 choose_ps1(); 200 } 201 202 if (argv[0] && argv[0][0] == '-') { 203 state = 1; 204 read_profile("/etc/profile"); 205 state1: 206 state = 2; 207 read_profile(".profile"); 208 } 209 state2: 210 state = 3; 211 if ((iflag || !posix) && 212 getuid() == geteuid() && getgid() == getegid()) { 213 struct stackmark env_smark; 214 215 setstackmark(&env_smark); 216 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 217 state = 3; 218 read_profile(expandenv(shinit)); 219 } 220 popstackmark(&env_smark); 221 } 222 state3: 223 state = 4; 224 line_number = 1; /* undo anything from profile files */ 225 226 if (sflag == 0 || minusc) { 227 static int sigs[] = { 228 SIGINT, SIGQUIT, SIGHUP, 229 #ifdef SIGTSTP 230 SIGTSTP, 231 #endif 232 SIGPIPE 233 }; 234 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0])) 235 size_t i; 236 237 for (i = 0; i < SIGSSIZE; i++) 238 setsignal(sigs[i], 0); 239 } 240 241 if (minusc) 242 evalstring(minusc, sflag ? 0 : EV_EXIT); 243 244 if (sflag || minusc == NULL) { 245 state4: /* XXX ??? - why isn't this before the "if" statement */ 246 cmdloop(1); 247 if (iflag) { 248 out2str("\n"); 249 flushout(&errout); 250 } 251 } 252 #if PROFILE 253 monitor(0); 254 #endif 255 line_number = plinno; 256 exitshell(exitstatus); 257 /* NOTREACHED */ 258 } 259 260 261 /* 262 * Read and execute commands. "Top" is nonzero for the top level command 263 * loop; it turns on prompting if the shell is interactive. 264 */ 265 266 void 267 cmdloop(int top) 268 { 269 union node *n; 270 struct stackmark smark; 271 int inter; 272 int numeof = 0; 273 enum skipstate skip; 274 275 CTRACE(DBG_ALWAYS, ("cmdloop(%d) called\n", top)); 276 setstackmark(&smark); 277 for (;;) { 278 if (pendingsigs) 279 dotrap(); 280 inter = 0; 281 if (iflag == 1 && top) { 282 inter = 1; 283 showjobs(out2, SHOW_CHANGED); 284 chkmail(0); 285 flushout(&errout); 286 nflag = 0; 287 } 288 n = parsecmd(inter); 289 VXTRACE(DBG_PARSE|DBG_EVAL|DBG_CMDS,("cmdloop: "),showtree(n)); 290 if (n == NEOF) { 291 if (!top || numeof >= 50) 292 break; 293 if (nflag) 294 break; 295 if (!stoppedjobs()) { 296 if (!iflag || !Iflag) 297 break; 298 out2str("\nUse \"exit\" to leave shell.\n"); 299 } 300 numeof++; 301 } else if (n != NULL && nflag == 0) { 302 job_warning = (job_warning == 2) ? 1 : 0; 303 numeof = 0; 304 evaltree(n, 0); 305 } 306 rststackmark(&smark); 307 308 /* 309 * Any SKIP* can occur here! SKIP(FUNC|BREAK|CONT) occur when 310 * a dotcmd is in a loop or a function body and appropriate 311 * built-ins occurs in file scope in the sourced file. Values 312 * other than SKIPFILE are reset by the appropriate eval*() 313 * that contained the dotcmd() call. 314 */ 315 skip = current_skipstate(); 316 if (skip != SKIPNONE) { 317 if (skip == SKIPFILE) 318 stop_skipping(); 319 break; 320 } 321 } 322 popstackmark(&smark); 323 } 324 325 326 327 /* 328 * Read /etc/profile or .profile. Return on error. 329 */ 330 331 STATIC void 332 read_profile(const char *name) 333 { 334 int fd; 335 int xflag_set = 0; 336 int vflag_set = 0; 337 338 if (*name == '\0') 339 return; 340 341 INTOFF; 342 if ((fd = open(name, O_RDONLY)) >= 0) 343 setinputfd(fd, 1); 344 INTON; 345 if (fd < 0) 346 return; 347 /* -q turns off -x and -v just when executing init files */ 348 if (qflag) { 349 if (xflag) 350 xflag = 0, xflag_set = 1; 351 if (vflag) 352 vflag = 0, vflag_set = 1; 353 } 354 (void)set_dot_funcnest(1); /* allow profile to "return" */ 355 cmdloop(0); 356 (void)set_dot_funcnest(0); 357 if (qflag) { 358 if (xflag_set) 359 xflag = 1; 360 if (vflag_set) 361 vflag = 1; 362 } 363 popfile(); 364 } 365 366 367 368 /* 369 * Read a file containing shell functions. 370 */ 371 372 void 373 readcmdfile(char *name) 374 { 375 int fd; 376 377 INTOFF; 378 if ((fd = open(name, O_RDONLY)) >= 0) 379 setinputfd(fd, 1); 380 else 381 error("Can't open %s", name); 382 INTON; 383 cmdloop(0); 384 popfile(); 385 } 386 387 388 389 int 390 exitcmd(int argc, char **argv) 391 { 392 if (stoppedjobs()) 393 return 0; 394 if (argc > 1) 395 exitshell(number(argv[1])); 396 else 397 exitshell_savedstatus(); 398 /* NOTREACHED */ 399 } 400