1 | /* $Id: shinstance.c 1233 2007-10-09 23:24:41Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * The shell instance methods.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include <string.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #ifndef _MSC_VER
|
---|
30 | # include <unistd.h>
|
---|
31 | # include <pwd.h>
|
---|
32 | extern char **environ;
|
---|
33 | #endif
|
---|
34 | #include "shinstance.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Creates a root shell instance.
|
---|
39 | *
|
---|
40 | * @param inherit The shell to inherit from. If NULL inherit from environment and such.
|
---|
41 | * @param argc The argument count.
|
---|
42 | * @param argv The argument vector.
|
---|
43 | *
|
---|
44 | * @returns pointer to root shell on success, NULL on failure.
|
---|
45 | */
|
---|
46 | shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv)
|
---|
47 | {
|
---|
48 | shinstance *psh;
|
---|
49 |
|
---|
50 | psh = calloc(sizeof(*psh), 1);
|
---|
51 | if (psh)
|
---|
52 | {
|
---|
53 | /* the special stuff. */
|
---|
54 | #ifdef _MSC_VER
|
---|
55 | psh->pid = _getpid();
|
---|
56 | #else
|
---|
57 | psh->pid = getpid();
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | /* memalloc.c */
|
---|
61 | psh->stacknleft = MINSIZE;
|
---|
62 | psh->herefd = -1;
|
---|
63 | psh->stackp = &psh->stackbase;
|
---|
64 | psh->stacknxt = psh->stackbase.space;
|
---|
65 |
|
---|
66 | /* input.c */
|
---|
67 | psh->plinno = 1;
|
---|
68 | psh->init_editline = 0;
|
---|
69 | psh->parsefile = &psh->basepf;
|
---|
70 |
|
---|
71 | /* output.c */
|
---|
72 | psh->output.bufsize = OUTBUFSIZ;
|
---|
73 | psh->output.fd = 1;
|
---|
74 | psh->output.psh = psh;
|
---|
75 | psh->errout.bufsize = 100;
|
---|
76 | psh->errout.fd = 2;
|
---|
77 | psh->errout.psh = psh;
|
---|
78 | psh->memout.fd = MEM_OUT;
|
---|
79 | psh->memout.psh = psh;
|
---|
80 | psh->out1 = &psh->output;
|
---|
81 | psh->out2 = &psh->errout;
|
---|
82 |
|
---|
83 | /* jobs.c */
|
---|
84 | psh->backgndpid = -1;
|
---|
85 | #if JOBS
|
---|
86 | psh->curjob = -1;
|
---|
87 | #endif
|
---|
88 | psh->ttyfd = -1;
|
---|
89 |
|
---|
90 | }
|
---|
91 | return psh;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | char *sh_getenv(shinstance *psh, const char *var)
|
---|
96 | {
|
---|
97 | #ifdef SH_PURE_STUB_MODE
|
---|
98 | return NULL;
|
---|
99 | #elif defined(SH_STUB_MODE)
|
---|
100 | return getenv(var);
|
---|
101 | #else
|
---|
102 | #endif
|
---|
103 | }
|
---|
104 |
|
---|
105 | char **sh_environ(shinstance *psh)
|
---|
106 | {
|
---|
107 | #ifdef SH_PURE_STUB_MODE
|
---|
108 | static char *s_null[2] = {0,0};
|
---|
109 | return &s_null[0];
|
---|
110 | #elif defined(SH_STUB_MODE)
|
---|
111 | return environ;
|
---|
112 | #else
|
---|
113 | #endif
|
---|
114 | }
|
---|
115 |
|
---|
116 | const char *sh_gethomedir(shinstance *psh, const char *user)
|
---|
117 | {
|
---|
118 | #ifdef SH_PURE_STUB_MODE
|
---|
119 | return NULL;
|
---|
120 | #elif defined(SH_STUB_MODE)
|
---|
121 | # ifdef _MSC_VER
|
---|
122 | return NULL;
|
---|
123 | # else
|
---|
124 | struct passwd *pwd = getpwnam(user);
|
---|
125 | return pwd ? pwd->pw_dir : NULL;
|
---|
126 | # endif
|
---|
127 | #else
|
---|
128 | #endif
|
---|
129 | }
|
---|
130 |
|
---|
131 | int sh_sigaction(int signo, const struct sh_sigaction *newp, struct sh_sigaction *oldp)
|
---|
132 | {
|
---|
133 | #ifdef SH_PURE_STUB_MODE
|
---|
134 | return -1;
|
---|
135 | #elif defined(SH_STUB_MODE)
|
---|
136 | # ifdef _MSC_VER
|
---|
137 | return -1;
|
---|
138 | # else
|
---|
139 | struct sigaction old;
|
---|
140 | if (newp)
|
---|
141 | return -1;
|
---|
142 | if (sigaction(signo, NULL, &old))
|
---|
143 | return -1;
|
---|
144 | oldp->sh_flags = old.sa_flags;
|
---|
145 | oldp->sh_handler = old.sa_handler;
|
---|
146 | oldp->sh_mask = old.sa_mask;
|
---|
147 | return 0;
|
---|
148 | # endif
|
---|
149 | #else
|
---|
150 | #endif
|
---|
151 | }
|
---|
152 |
|
---|
153 | sh_sig_t sh_signal(shinstance *psh, int signo, sh_sig_t handler)
|
---|
154 | {
|
---|
155 | return (sh_sig_t)-1;
|
---|
156 | }
|
---|
157 |
|
---|
158 | int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
|
---|
159 | {
|
---|
160 | return -1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | void sh_sigemptyset(sh_sigset_t *setp)
|
---|
164 | {
|
---|
165 | memset(setp, 0, sizeof(*setp));
|
---|
166 | }
|
---|
167 |
|
---|
168 | int sh_sigprocmask(shinstance *psh, int operation, sh_sigset_t const *newp, sh_sigset_t *oldp)
|
---|
169 | {
|
---|
170 | return -1;
|
---|
171 | }
|
---|
172 |
|
---|
173 | void sh_abort(shinstance *psh)
|
---|
174 | {
|
---|
175 | #ifdef SH_PURE_STUB_MODE
|
---|
176 | #elif defined(SH_STUB_MODE)
|
---|
177 | abort();
|
---|
178 | #else
|
---|
179 | #endif
|
---|
180 | }
|
---|
181 |
|
---|
182 | void sh_raise_sigint(shinstance *psh)
|
---|
183 | {
|
---|
184 | #ifdef SH_PURE_STUB_MODE
|
---|
185 | #elif defined(SH_STUB_MODE)
|
---|
186 | raise(SIGINT);
|
---|
187 | #else
|
---|
188 | #endif
|
---|
189 | }
|
---|
190 |
|
---|
191 | int sh_kill(shinstance *psh, pid_t pid, int signo)
|
---|
192 | {
|
---|
193 | #ifdef SH_PURE_STUB_MODE
|
---|
194 | return -1;
|
---|
195 | #elif defined(SH_STUB_MODE)
|
---|
196 | # ifdef _MSC_VER
|
---|
197 | return -1;
|
---|
198 | # else
|
---|
199 | return kill(pid, signo);
|
---|
200 | # endif
|
---|
201 | #else
|
---|
202 | #endif
|
---|
203 | }
|
---|
204 |
|
---|
205 | int sh_killpg(shinstance *psh, pid_t pgid, int signo)
|
---|
206 | {
|
---|
207 | #ifdef SH_PURE_STUB_MODE
|
---|
208 | return -1;
|
---|
209 | #elif defined(SH_STUB_MODE)
|
---|
210 | # ifdef _MSC_VER
|
---|
211 | return -1;
|
---|
212 | # else
|
---|
213 | return killpg(pgid, signo);
|
---|
214 | # endif
|
---|
215 | #else
|
---|
216 | #endif
|
---|
217 | }
|
---|
218 |
|
---|
219 | clock_t sh_times(shinstance *psh, shtms *tmsp)
|
---|
220 | {
|
---|
221 | #ifdef SH_PURE_STUB_MODE
|
---|
222 | return 0;
|
---|
223 | #elif defined(SH_STUB_MODE)
|
---|
224 | # ifdef _MSC_VER
|
---|
225 | return 0;
|
---|
226 | # else
|
---|
227 | return times(tmsp);
|
---|
228 | # endif
|
---|
229 | #else
|
---|
230 | #endif
|
---|
231 | }
|
---|
232 |
|
---|
233 | int sh_sysconf_clk_tck(void)
|
---|
234 | {
|
---|
235 | #ifdef SH_PURE_STUB_MODE
|
---|
236 | return 1;
|
---|
237 | #else
|
---|
238 | # ifdef _MSC_VER
|
---|
239 | return CLK_TCK;
|
---|
240 | # else
|
---|
241 | return sysconf(_SC_CLK_TCK);
|
---|
242 | # endif
|
---|
243 | #endif
|
---|
244 | }
|
---|
245 |
|
---|
246 | pid_t sh_fork(shinstance *psh)
|
---|
247 | {
|
---|
248 | #ifdef SH_PURE_STUB_MODE
|
---|
249 | return -1;
|
---|
250 | #elif defined(SH_STUB_MODE)
|
---|
251 | # ifdef _MSC_VER
|
---|
252 | return -1;
|
---|
253 | # else
|
---|
254 | return fork();
|
---|
255 | # endif
|
---|
256 | #else
|
---|
257 | #endif
|
---|
258 | }
|
---|
259 |
|
---|
260 | pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
|
---|
261 | {
|
---|
262 | #ifdef SH_PURE_STUB_MODE
|
---|
263 | return -1;
|
---|
264 | #elif defined(SH_STUB_MODE)
|
---|
265 | # ifdef _MSC_VER
|
---|
266 | return -1;
|
---|
267 | # else
|
---|
268 | return waitpid(pid, statusp, flags);
|
---|
269 | # endif
|
---|
270 | #else
|
---|
271 | #endif
|
---|
272 | }
|
---|
273 |
|
---|
274 | void sh__exit(shinstance *psh, int rc)
|
---|
275 | {
|
---|
276 | #ifdef SH_PURE_STUB_MODE
|
---|
277 | return -1;
|
---|
278 | #elif defined(SH_STUB_MODE)
|
---|
279 | _exit(rc);
|
---|
280 | #else
|
---|
281 | #endif
|
---|
282 | }
|
---|
283 |
|
---|
284 | int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
|
---|
285 | {
|
---|
286 | #ifdef SH_PURE_STUB_MODE
|
---|
287 | return -1;
|
---|
288 | #elif defined(SH_STUB_MODE)
|
---|
289 | # ifdef _MSC_VER
|
---|
290 | return -1;
|
---|
291 | # else
|
---|
292 | return execve(exe, (char **)argv, (char **)envp);
|
---|
293 | # endif
|
---|
294 | #else
|
---|
295 | #endif
|
---|
296 | }
|
---|
297 |
|
---|
298 | uid_t sh_getuid(shinstance *psh)
|
---|
299 | {
|
---|
300 | #ifdef SH_PURE_STUB_MODE
|
---|
301 | return 0;
|
---|
302 | #elif defined(SH_STUB_MODE)
|
---|
303 | # ifdef _MSC_VER
|
---|
304 | return 0;
|
---|
305 | # else
|
---|
306 | return getuid();
|
---|
307 | # endif
|
---|
308 | #else
|
---|
309 | #endif
|
---|
310 | }
|
---|
311 |
|
---|
312 | uid_t sh_geteuid(shinstance *psh)
|
---|
313 | {
|
---|
314 | #ifdef SH_PURE_STUB_MODE
|
---|
315 | return 0;
|
---|
316 | #elif defined(SH_STUB_MODE)
|
---|
317 | # ifdef _MSC_VER
|
---|
318 | return 0;
|
---|
319 | # else
|
---|
320 | return geteuid();
|
---|
321 | # endif
|
---|
322 | #else
|
---|
323 | #endif
|
---|
324 | }
|
---|
325 |
|
---|
326 | gid_t sh_getgid(shinstance *psh)
|
---|
327 | {
|
---|
328 | #ifdef SH_PURE_STUB_MODE
|
---|
329 | return 0;
|
---|
330 | #elif defined(SH_STUB_MODE)
|
---|
331 | # ifdef _MSC_VER
|
---|
332 | return 0;
|
---|
333 | # else
|
---|
334 | return getgid();
|
---|
335 | # endif
|
---|
336 | #else
|
---|
337 | #endif
|
---|
338 | }
|
---|
339 |
|
---|
340 | gid_t sh_getegid(shinstance *psh)
|
---|
341 | {
|
---|
342 | #ifdef SH_PURE_STUB_MODE
|
---|
343 | return 0;
|
---|
344 | #elif defined(SH_STUB_MODE)
|
---|
345 | # ifdef _MSC_VER
|
---|
346 | return 0;
|
---|
347 | # else
|
---|
348 | return getegid();
|
---|
349 | # endif
|
---|
350 | #else
|
---|
351 | #endif
|
---|
352 | }
|
---|
353 |
|
---|
354 | pid_t sh_getpid(shinstance *psh)
|
---|
355 | {
|
---|
356 | #ifdef SH_PURE_STUB_MODE
|
---|
357 | return 0;
|
---|
358 | #elif defined(SH_STUB_MODE)
|
---|
359 | # ifdef _MSC_VER
|
---|
360 | return _getpid();
|
---|
361 | # else
|
---|
362 | return getpid();
|
---|
363 | # endif
|
---|
364 | #else
|
---|
365 | #endif
|
---|
366 | }
|
---|
367 |
|
---|
368 | pid_t sh_getpgrp(shinstance *psh)
|
---|
369 | {
|
---|
370 | #ifdef SH_PURE_STUB_MODE
|
---|
371 | return 0;
|
---|
372 | #elif defined(SH_STUB_MODE)
|
---|
373 | # ifdef _MSC_VER
|
---|
374 | return _getpid();
|
---|
375 | # else
|
---|
376 | return getpgrp();
|
---|
377 | # endif
|
---|
378 | #else
|
---|
379 | #endif
|
---|
380 | }
|
---|
381 |
|
---|
382 | pid_t sh_getpgid(shinstance *psh, pid_t pid)
|
---|
383 | {
|
---|
384 | #ifdef SH_PURE_STUB_MODE
|
---|
385 | return pid;
|
---|
386 | #elif defined(SH_STUB_MODE)
|
---|
387 | # ifdef _MSC_VER
|
---|
388 | return pid;
|
---|
389 | # else
|
---|
390 | return getpgid(pid);
|
---|
391 | # endif
|
---|
392 | #else
|
---|
393 | #endif
|
---|
394 | }
|
---|
395 |
|
---|
396 | int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
|
---|
397 | {
|
---|
398 | #ifdef SH_PURE_STUB_MODE
|
---|
399 | return -1;
|
---|
400 | #elif defined(SH_STUB_MODE)
|
---|
401 | # ifdef _MSC_VER
|
---|
402 | return -1;
|
---|
403 | # else
|
---|
404 | return setpgid(pid, pgid);
|
---|
405 | # endif
|
---|
406 | #else
|
---|
407 | #endif
|
---|
408 | }
|
---|
409 |
|
---|
410 | pid_t sh_tcgetpgrp(shinstance *psh, int fd)
|
---|
411 | {
|
---|
412 | #ifdef SH_PURE_STUB_MODE
|
---|
413 | return -1;
|
---|
414 | #elif defined(SH_STUB_MODE)
|
---|
415 | # ifdef _MSC_VER
|
---|
416 | return -1;
|
---|
417 | # else
|
---|
418 | return tcgetpgrp(fd);
|
---|
419 | # endif
|
---|
420 | #else
|
---|
421 | #endif
|
---|
422 | }
|
---|
423 |
|
---|
424 | int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
|
---|
425 | {
|
---|
426 | #ifdef SH_PURE_STUB_MODE
|
---|
427 | return -1;
|
---|
428 | #elif defined(SH_STUB_MODE)
|
---|
429 | # ifdef _MSC_VER
|
---|
430 | return -1;
|
---|
431 | # else
|
---|
432 | return tcsetpgrp(fd, pgrp);
|
---|
433 | # endif
|
---|
434 | #else
|
---|
435 | #endif
|
---|
436 | }
|
---|
437 |
|
---|
438 | int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
|
---|
439 | {
|
---|
440 | #ifdef SH_PURE_STUB_MODE
|
---|
441 | return -1;
|
---|
442 | #elif defined(SH_STUB_MODE)
|
---|
443 | # ifdef _MSC_VER
|
---|
444 | return -1;
|
---|
445 | # else
|
---|
446 | return getrlimit(resid, limp);
|
---|
447 | # endif
|
---|
448 | #else
|
---|
449 | #endif
|
---|
450 | }
|
---|
451 |
|
---|
452 | int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
|
---|
453 | {
|
---|
454 | #ifdef SH_PURE_STUB_MODE
|
---|
455 | return -1;
|
---|
456 | #elif defined(SH_STUB_MODE)
|
---|
457 | # ifdef _MSC_VER
|
---|
458 | return -1;
|
---|
459 | # else
|
---|
460 | return setrlimit(resid, limp);
|
---|
461 | # endif
|
---|
462 | #else
|
---|
463 | #endif
|
---|
464 | }
|
---|
465 |
|
---|