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