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