main.c revision 1.79 1 /* $NetBSD: main.c,v 1.79 2018/12/11 13:31: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.79 2018/12/11 13:31: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 }
248 #if PROFILE
249 monitor(0);
250 #endif
251 exitshell(exitstatus);
252 /* NOTREACHED */
253 }
254
255
256 /*
257 * Read and execute commands. "Top" is nonzero for the top level command
258 * loop; it turns on prompting if the shell is interactive.
259 */
260
261 void
262 cmdloop(int top)
263 {
264 union node *n;
265 struct stackmark smark;
266 int inter;
267 int numeof = 0;
268 enum skipstate skip;
269
270 CTRACE(DBG_ALWAYS, ("cmdloop(%d) called\n", top));
271 setstackmark(&smark);
272 for (;;) {
273 if (pendingsigs)
274 dotrap();
275 inter = 0;
276 if (iflag == 1 && top) {
277 inter = 1;
278 showjobs(out2, SHOW_CHANGED);
279 chkmail(0);
280 flushout(&errout);
281 nflag = 0;
282 }
283 n = parsecmd(inter);
284 VXTRACE(DBG_PARSE|DBG_EVAL|DBG_CMDS,("cmdloop: "),showtree(n));
285 if (n == NEOF) {
286 if (!top || numeof >= 50)
287 break;
288 if (nflag)
289 break;
290 if (!stoppedjobs()) {
291 if (!iflag || !Iflag)
292 break;
293 out2str("\nUse \"exit\" to leave shell.\n");
294 }
295 numeof++;
296 } else if (n != NULL && nflag == 0) {
297 job_warning = (job_warning == 2) ? 1 : 0;
298 numeof = 0;
299 evaltree(n, 0);
300 }
301 rststackmark(&smark);
302
303 /*
304 * Any SKIP* can occur here! SKIP(FUNC|BREAK|CONT) occur when
305 * a dotcmd is in a loop or a function body and appropriate
306 * built-ins occurs in file scope in the sourced file. Values
307 * other than SKIPFILE are reset by the appropriate eval*()
308 * that contained the dotcmd() call.
309 */
310 skip = current_skipstate();
311 if (skip != SKIPNONE) {
312 if (skip == SKIPFILE)
313 stop_skipping();
314 break;
315 }
316 }
317 popstackmark(&smark);
318 }
319
320
321
322 /*
323 * Read /etc/profile or .profile. Return on error.
324 */
325
326 STATIC void
327 read_profile(const char *name)
328 {
329 int fd;
330 int xflag_set = 0;
331 int vflag_set = 0;
332
333 if (*name == '\0')
334 return;
335
336 INTOFF;
337 if ((fd = open(name, O_RDONLY)) >= 0)
338 setinputfd(fd, 1);
339 INTON;
340 if (fd < 0)
341 return;
342 /* -q turns off -x and -v just when executing init files */
343 if (qflag) {
344 if (xflag)
345 xflag = 0, xflag_set = 1;
346 if (vflag)
347 vflag = 0, vflag_set = 1;
348 }
349 cmdloop(0);
350 if (qflag) {
351 if (xflag_set)
352 xflag = 1;
353 if (vflag_set)
354 vflag = 1;
355 }
356 popfile();
357 }
358
359
360
361 /*
362 * Read a file containing shell functions.
363 */
364
365 void
366 readcmdfile(char *name)
367 {
368 int fd;
369
370 INTOFF;
371 if ((fd = open(name, O_RDONLY)) >= 0)
372 setinputfd(fd, 1);
373 else
374 error("Can't open %s", name);
375 INTON;
376 cmdloop(0);
377 popfile();
378 }
379
380
381
382 int
383 exitcmd(int argc, char **argv)
384 {
385 if (stoppedjobs())
386 return 0;
387 if (argc > 1)
388 exitshell(number(argv[1]));
389 else
390 exitshell_savedstatus();
391 /* NOTREACHED */
392 }
393