1 | /* $Id: shinstance.c 1785 2008-09-14 17:31:16Z 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 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <string.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <assert.h>
|
---|
33 | #ifndef _MSC_VER
|
---|
34 | # include <unistd.h>
|
---|
35 | # include <pwd.h>
|
---|
36 | extern char **environ;
|
---|
37 | #endif
|
---|
38 | #include "shinstance.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Global Variables *
|
---|
43 | *******************************************************************************/
|
---|
44 | /** The mutex protecting the the globals and some shell instance members (sigs). */
|
---|
45 | static shmtx g_sh_mtx;
|
---|
46 | /** The root shell instance. */
|
---|
47 | static shinstance *g_sh_root;
|
---|
48 | /** The first shell instance. */
|
---|
49 | static shinstance *g_sh_head;
|
---|
50 | /** The last shell instance. */
|
---|
51 | static shinstance *g_sh_tail;
|
---|
52 | /** The number of shells. */
|
---|
53 | static int g_num_shells;
|
---|
54 | /** Per signal state for determining a common denominator.
|
---|
55 | * @remarks defaults and unmasked actions aren't counted. */
|
---|
56 | struct shsigstate
|
---|
57 | {
|
---|
58 | /** The current signal action. */
|
---|
59 | #ifndef _MSC_VER
|
---|
60 | struct sigaction sa;
|
---|
61 | #else
|
---|
62 | struct
|
---|
63 | {
|
---|
64 | void (*sa_handler)(int);
|
---|
65 | int sa_flags;
|
---|
66 | shsigset_t sa_mask;
|
---|
67 | } sa;
|
---|
68 | #endif
|
---|
69 | /** The number of restarts (siginterrupt / SA_RESTART). */
|
---|
70 | int num_restart;
|
---|
71 | /** The number of ignore handlers. */
|
---|
72 | int num_ignore;
|
---|
73 | /** The number of specific handlers. */
|
---|
74 | int num_specific;
|
---|
75 | /** The number of threads masking it. */
|
---|
76 | int num_masked;
|
---|
77 | } g_sig_state[NSIG];
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 | typedef struct shmtxtmp { int i; } shmtxtmp;
|
---|
82 |
|
---|
83 | int shmtx_init(shmtx *pmtx)
|
---|
84 | {
|
---|
85 | pmtx->b[0] = 0;
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void shmtx_delete(shmtx *pmtx)
|
---|
90 | {
|
---|
91 | pmtx->b[0] = 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void shmtx_enter(shmtx *pmtx, shmtxtmp *ptmp)
|
---|
95 | {
|
---|
96 | pmtx->b[0] = 0;
|
---|
97 | ptmp->i = 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | void shmtx_leave(shmtx *pmtx, shmtxtmp *ptmp)
|
---|
101 | {
|
---|
102 | pmtx->b[0] = 0;
|
---|
103 | ptmp->i = 432;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Links the shell instance.
|
---|
109 | *
|
---|
110 | * @param psh The shell.
|
---|
111 | */
|
---|
112 | static void sh_int_link(shinstance *psh)
|
---|
113 | {
|
---|
114 | shmtxtmp tmp;
|
---|
115 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
116 |
|
---|
117 | if (psh->rootshell)
|
---|
118 | g_sh_root = psh;
|
---|
119 |
|
---|
120 | psh->next = NULL;
|
---|
121 | psh->prev = g_sh_tail;
|
---|
122 | if (g_sh_tail)
|
---|
123 | g_sh_tail->next = psh;
|
---|
124 | else
|
---|
125 | g_sh_tail = g_sh_head = psh;
|
---|
126 | g_sh_tail = psh;
|
---|
127 |
|
---|
128 | g_num_shells++;
|
---|
129 |
|
---|
130 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Unlink the shell instance.
|
---|
136 | *
|
---|
137 | * @param psh The shell.
|
---|
138 | */
|
---|
139 | static void sh_int_unlink(shinstance *psh)
|
---|
140 | {
|
---|
141 | shmtxtmp tmp;
|
---|
142 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
143 |
|
---|
144 | g_num_shells--;
|
---|
145 |
|
---|
146 | if (g_sh_tail == psh)
|
---|
147 | g_sh_tail = psh->prev;
|
---|
148 | else
|
---|
149 | psh->next->prev = psh->prev;
|
---|
150 |
|
---|
151 | if (g_sh_head == psh)
|
---|
152 | g_sh_head = psh->next;
|
---|
153 | else
|
---|
154 | psh->prev->next = psh->next;
|
---|
155 |
|
---|
156 | if (g_sh_root == psh)
|
---|
157 | g_sh_root = 0;
|
---|
158 |
|
---|
159 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Creates a root shell instance.
|
---|
165 | *
|
---|
166 | * @param inherit The shell to inherit from. If NULL inherit from environment and such.
|
---|
167 | * @param argc The argument count.
|
---|
168 | * @param argv The argument vector.
|
---|
169 | *
|
---|
170 | * @returns pointer to root shell on success, NULL on failure.
|
---|
171 | */
|
---|
172 | shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv)
|
---|
173 | {
|
---|
174 | shinstance *psh;
|
---|
175 | int i;
|
---|
176 |
|
---|
177 | psh = calloc(sizeof(*psh), 1);
|
---|
178 | if (psh)
|
---|
179 | {
|
---|
180 | /* the special stuff. */
|
---|
181 | #ifdef _MSC_VER
|
---|
182 | psh->pid = _getpid();
|
---|
183 | #else
|
---|
184 | psh->pid = getpid();
|
---|
185 | #endif
|
---|
186 | /*sh_sigemptyset(&psh->sigrestartset);*/
|
---|
187 | for (i = 0; i < NSIG; i++)
|
---|
188 | psh->sigactions[i].sh_handler = SH_SIG_UNK;
|
---|
189 |
|
---|
190 | /* memalloc.c */
|
---|
191 | psh->stacknleft = MINSIZE;
|
---|
192 | psh->herefd = -1;
|
---|
193 | psh->stackp = &psh->stackbase;
|
---|
194 | psh->stacknxt = psh->stackbase.space;
|
---|
195 |
|
---|
196 | /* input.c */
|
---|
197 | psh->plinno = 1;
|
---|
198 | psh->init_editline = 0;
|
---|
199 | psh->parsefile = &psh->basepf;
|
---|
200 |
|
---|
201 | /* output.c */
|
---|
202 | psh->output.bufsize = OUTBUFSIZ;
|
---|
203 | psh->output.fd = 1;
|
---|
204 | psh->output.psh = psh;
|
---|
205 | psh->errout.bufsize = 100;
|
---|
206 | psh->errout.fd = 2;
|
---|
207 | psh->errout.psh = psh;
|
---|
208 | psh->memout.fd = MEM_OUT;
|
---|
209 | psh->memout.psh = psh;
|
---|
210 | psh->out1 = &psh->output;
|
---|
211 | psh->out2 = &psh->errout;
|
---|
212 |
|
---|
213 | /* jobs.c */
|
---|
214 | psh->backgndpid = -1;
|
---|
215 | #if JOBS
|
---|
216 | psh->curjob = -1;
|
---|
217 | #else
|
---|
218 | # error asdf
|
---|
219 | #endif
|
---|
220 | psh->ttyfd = -1;
|
---|
221 |
|
---|
222 | /* link it. */
|
---|
223 | sh_int_link(psh);
|
---|
224 |
|
---|
225 | }
|
---|
226 | return psh;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | char *sh_getenv(shinstance *psh, const char *var)
|
---|
231 | {
|
---|
232 | #ifdef SH_PURE_STUB_MODE
|
---|
233 | return NULL;
|
---|
234 | #elif defined(SH_STUB_MODE)
|
---|
235 | (void)psh;
|
---|
236 | return getenv(var);
|
---|
237 | #else
|
---|
238 | #endif
|
---|
239 | }
|
---|
240 |
|
---|
241 | char **sh_environ(shinstance *psh)
|
---|
242 | {
|
---|
243 | #ifdef SH_PURE_STUB_MODE
|
---|
244 | static char *s_null[2] = {0,0};
|
---|
245 | return &s_null[0];
|
---|
246 | #elif defined(SH_STUB_MODE)
|
---|
247 | (void)psh;
|
---|
248 | return environ;
|
---|
249 | #else
|
---|
250 | #endif
|
---|
251 | }
|
---|
252 |
|
---|
253 | const char *sh_gethomedir(shinstance *psh, const char *user)
|
---|
254 | {
|
---|
255 | #ifdef SH_PURE_STUB_MODE
|
---|
256 | return NULL;
|
---|
257 | #elif defined(SH_STUB_MODE)
|
---|
258 | (void)psh;
|
---|
259 | # ifdef _MSC_VER
|
---|
260 | return NULL;
|
---|
261 | # else
|
---|
262 | struct passwd *pwd = getpwnam(user);
|
---|
263 | return pwd ? pwd->pw_dir : NULL;
|
---|
264 | # endif
|
---|
265 | #else
|
---|
266 | #endif
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Lazy initialization of a signal state, globally.
|
---|
271 | *
|
---|
272 | * @param psh The shell doing the lazy work.
|
---|
273 | * @param signo The signal (valid).
|
---|
274 | */
|
---|
275 | static void sh_int_lazy_init_sigaction(shinstance *psh, int signo)
|
---|
276 | {
|
---|
277 | if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
|
---|
278 | {
|
---|
279 | shmtxtmp tmp;
|
---|
280 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
281 |
|
---|
282 | if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
|
---|
283 | {
|
---|
284 | shsigaction_t shold;
|
---|
285 | shinstance *cur;
|
---|
286 | #ifndef _MSC_VER
|
---|
287 | struct sigaction old;
|
---|
288 | if (!sigaction(signo, NULL, &old))
|
---|
289 | {
|
---|
290 | /* convert */
|
---|
291 | shold.sh_flags = old.sa_flags;
|
---|
292 | shold.sh_mask = old.sa_mask;
|
---|
293 | if (old.sa_handler == SIG_DFL)
|
---|
294 | shold.sh_handler = SH_SIG_DFL;
|
---|
295 | else
|
---|
296 | {
|
---|
297 | assert(old.sa_handler == SIG_IGN);
|
---|
298 | shold.sh_handler = SH_SIG_IGN;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | else
|
---|
302 | #endif
|
---|
303 | {
|
---|
304 | /* fake */
|
---|
305 | #ifndef _MSC_VER
|
---|
306 | assert(0);
|
---|
307 | old.sa_handler = SIG_DFL;
|
---|
308 | old.sa_flags = 0;
|
---|
309 | sigemptyset(&shold.sh_mask);
|
---|
310 | sigaddset(&shold.sh_mask, signo);
|
---|
311 | #endif
|
---|
312 | shold.sh_flags = 0;
|
---|
313 | sh_sigemptyset(&shold.sh_mask);
|
---|
314 | sh_sigaddset(&shold.sh_mask, signo);
|
---|
315 | shold.sh_handler = SH_SIG_DFL;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /* update globals */
|
---|
319 | #ifndef _MSC_VER
|
---|
320 | g_sig_state[signo].sa = old;
|
---|
321 | #else
|
---|
322 | g_sig_state[signo].sa.sa_handler = SIG_DFL;
|
---|
323 | g_sig_state[signo].sa.sa_flags = 0;
|
---|
324 | g_sig_state[signo].sa.sa_mask = shold.sh_mask;
|
---|
325 | #endif
|
---|
326 | TRACE2((psh, "sh_int_lazy_init_sigaction: signo=%d:%s sa_handler=%p sa_flags=%#x\n",
|
---|
327 | signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
|
---|
328 |
|
---|
329 | /* update all shells */
|
---|
330 | for (cur = g_sh_head; cur; cur = cur->next)
|
---|
331 | {
|
---|
332 | assert(cur->sigactions[signo].sh_handler == SH_SIG_UNK);
|
---|
333 | cur->sigactions[signo] = shold;
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Handler for external signals.
|
---|
344 | *
|
---|
345 | * @param signo The signal.
|
---|
346 | */
|
---|
347 | static void sh_sig_common_handler(int signo)
|
---|
348 | {
|
---|
349 | fprintf(stderr, "sh_sig_common_handler: signo=%d:%s\n", signo, sys_signame[signo]);
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | int sh_sigaction(shinstance *psh, int signo, const struct shsigaction *newp, struct shsigaction *oldp)
|
---|
354 | {
|
---|
355 | if (newp)
|
---|
356 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=%p:{.sh_handler=%p, .sh_flags=%#x} oldp=%p\n",
|
---|
357 | signo, sys_signame[signo], newp, newp->sh_handler, newp->sh_flags, oldp));
|
---|
358 | else
|
---|
359 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=NULL oldp=%p\n", signo, sys_signame[signo], oldp));
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * Input validation.
|
---|
363 | */
|
---|
364 | if (signo >= NSIG || signo <= 0)
|
---|
365 | {
|
---|
366 | errno = EINVAL;
|
---|
367 | return -1;
|
---|
368 | }
|
---|
369 |
|
---|
370 | #ifdef SH_PURE_STUB_MODE
|
---|
371 | return -1;
|
---|
372 | #else
|
---|
373 |
|
---|
374 | /*
|
---|
375 | * Make sure our data is correct.
|
---|
376 | */
|
---|
377 | sh_int_lazy_init_sigaction(psh, signo);
|
---|
378 |
|
---|
379 | /*
|
---|
380 | * Get the old one if requested.
|
---|
381 | */
|
---|
382 | if (oldp)
|
---|
383 | *oldp = psh->sigactions[signo];
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Set the new one if it has changed.
|
---|
387 | *
|
---|
388 | * This will be attempted coordinated with the other signal handlers so
|
---|
389 | * that we can arrive at a common denominator.
|
---|
390 | */
|
---|
391 | if ( newp
|
---|
392 | && memcmp(&psh->sigactions[signo], newp, sizeof(*newp)))
|
---|
393 | {
|
---|
394 | shmtxtmp tmp;
|
---|
395 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
396 |
|
---|
397 | /* Undo the accounting for the current entry. */
|
---|
398 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
---|
399 | g_sig_state[signo].num_ignore--;
|
---|
400 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
---|
401 | g_sig_state[signo].num_specific--;
|
---|
402 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
---|
403 | g_sig_state[signo].num_restart--;
|
---|
404 |
|
---|
405 | /* Set the new entry. */
|
---|
406 | psh->sigactions[signo] = *newp;
|
---|
407 |
|
---|
408 | /* Add the bits for the new action entry. */
|
---|
409 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
---|
410 | g_sig_state[signo].num_ignore++;
|
---|
411 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
---|
412 | g_sig_state[signo].num_specific++;
|
---|
413 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
---|
414 | g_sig_state[signo].num_restart++;
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * Calc new common action.
|
---|
418 | *
|
---|
419 | * This is quit a bit ASSUMPTIVE about the limited use. We will not
|
---|
420 | * bother synching the mask, and we pretend to care about SA_RESTART.
|
---|
421 | * The only thing we really actually care about is the sh_handler.
|
---|
422 | *
|
---|
423 | * On second though, it's possible we should just tie this to the root
|
---|
424 | * shell since it only really applies to external signal ...
|
---|
425 | */
|
---|
426 | if ( g_sig_state[signo].num_specific
|
---|
427 | || g_sig_state[signo].num_ignore != g_num_shells)
|
---|
428 | g_sig_state[signo].sa.sa_handler = sh_sig_common_handler;
|
---|
429 | else if (g_sig_state[signo].num_ignore)
|
---|
430 | g_sig_state[signo].sa.sa_handler = SIG_IGN;
|
---|
431 | else
|
---|
432 | g_sig_state[signo].sa.sa_handler = SIG_DFL;
|
---|
433 | g_sig_state[signo].sa.sa_flags = psh->sigactions[signo].sh_flags & SA_RESTART;
|
---|
434 |
|
---|
435 | TRACE2((psh, "sh_sigaction: setting signo=%d:%s to {.sa_handler=%p, .sa_flags=%#x}\n",
|
---|
436 | signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
|
---|
437 | # ifdef _MSC_VER
|
---|
438 | if (signal(signo, g_sig_state[signo].sa.sa_handler) == SIG_ERR)
|
---|
439 | # else
|
---|
440 | if (sigaction(signo, &g_sig_state[signo].sa, NULL))
|
---|
441 | # endif
|
---|
442 | assert(0);
|
---|
443 |
|
---|
444 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
445 | }
|
---|
446 |
|
---|
447 | return 0;
|
---|
448 | #endif
|
---|
449 | }
|
---|
450 |
|
---|
451 | shsig_t sh_signal(shinstance *psh, int signo, shsig_t handler)
|
---|
452 | {
|
---|
453 | shsigaction_t sa;
|
---|
454 | shsig_t ret;
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * Implementation using sh_sigaction.
|
---|
458 | */
|
---|
459 | if (sh_sigaction(psh, signo, NULL, &sa))
|
---|
460 | return SH_SIG_ERR;
|
---|
461 |
|
---|
462 | ret = sa.sh_handler;
|
---|
463 | sa.sh_flags &= SA_RESTART;
|
---|
464 | sa.sh_handler = handler;
|
---|
465 | sh_sigemptyset(&sa.sh_mask);
|
---|
466 | sh_sigaddset(&sa.sh_mask, signo); /* ?? */
|
---|
467 | if (sh_sigaction(psh, signo, &sa, NULL))
|
---|
468 | return SH_SIG_ERR;
|
---|
469 |
|
---|
470 | return ret;
|
---|
471 | }
|
---|
472 |
|
---|
473 | int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
|
---|
474 | {
|
---|
475 | shsigaction_t sa;
|
---|
476 | int oldflags = 0;
|
---|
477 |
|
---|
478 | /*
|
---|
479 | * Implementation using sh_sigaction.
|
---|
480 | */
|
---|
481 | if (sh_sigaction(psh, signo, NULL, &sa))
|
---|
482 | return -1;
|
---|
483 | oldflags = sa.sh_flags;
|
---|
484 | if (interrupt)
|
---|
485 | sa.sh_flags &= ~SA_RESTART;
|
---|
486 | else
|
---|
487 | sa.sh_flags |= ~SA_RESTART;
|
---|
488 | if (!((oldflags ^ sa.sh_flags) & SA_RESTART))
|
---|
489 | return 0; /* unchanged. */
|
---|
490 |
|
---|
491 | return sh_sigaction(psh, signo, &sa, NULL);
|
---|
492 | }
|
---|
493 |
|
---|
494 | void sh_sigemptyset(shsigset_t *setp)
|
---|
495 | {
|
---|
496 | memset(setp, 0, sizeof(*setp));
|
---|
497 | }
|
---|
498 |
|
---|
499 | void sh_sigaddset(shsigset_t *setp, int signo)
|
---|
500 | {
|
---|
501 | #ifdef _MSC_VER
|
---|
502 | *setp |= 1U << signo;
|
---|
503 | #else
|
---|
504 | sigaddset(setp, signo);
|
---|
505 | #endif
|
---|
506 | }
|
---|
507 |
|
---|
508 | void sh_sigdelset(shsigset_t *setp, int signo)
|
---|
509 | {
|
---|
510 | #ifdef _MSC_VER
|
---|
511 | *setp &= ~(1U << signo);
|
---|
512 | #else
|
---|
513 | sigdelset(setp, signo);
|
---|
514 | #endif
|
---|
515 | }
|
---|
516 |
|
---|
517 | int sh_sigismember(shsigset_t *setp, int signo)
|
---|
518 | {
|
---|
519 | #ifdef _MSC_VER
|
---|
520 | return !!(*setp & (1U << signo));
|
---|
521 | #else
|
---|
522 | return !!sigismember(setp, signo);
|
---|
523 | #endif
|
---|
524 | }
|
---|
525 |
|
---|
526 | int sh_sigprocmask(shinstance *psh, int operation, shsigset_t const *newp, shsigset_t *oldp)
|
---|
527 | {
|
---|
528 | #ifdef SH_PURE_STUB_MODE
|
---|
529 | return -1;
|
---|
530 | #elif defined(SH_STUB_MODE)
|
---|
531 | (void)psh;
|
---|
532 | # ifdef _MSC_VER
|
---|
533 | return -1;
|
---|
534 | # else
|
---|
535 | return sigprocmask(operation, newp, oldp);
|
---|
536 | # endif
|
---|
537 | #else
|
---|
538 | #endif
|
---|
539 | }
|
---|
540 |
|
---|
541 | void sh_abort(shinstance *psh)
|
---|
542 | {
|
---|
543 | TRACE2((psh, "sh_abort\n"));
|
---|
544 |
|
---|
545 | #ifdef SH_PURE_STUB_MODE
|
---|
546 | #elif defined(SH_STUB_MODE)
|
---|
547 | abort();
|
---|
548 | #else
|
---|
549 | #endif
|
---|
550 |
|
---|
551 | TRACE2((psh, "sh_abort returns!\n"));
|
---|
552 | (void)psh;
|
---|
553 | }
|
---|
554 |
|
---|
555 | void sh_raise_sigint(shinstance *psh)
|
---|
556 | {
|
---|
557 | TRACE2((psh, "sh_raise(SIGINT)\n"));
|
---|
558 |
|
---|
559 | #ifdef SH_PURE_STUB_MODE
|
---|
560 | #elif defined(SH_STUB_MODE)
|
---|
561 | (void)psh;
|
---|
562 | raise(SIGINT);
|
---|
563 | #else
|
---|
564 | #endif
|
---|
565 |
|
---|
566 | TRACE2((psh, "sh_raise(SIGINT) returns\n"));
|
---|
567 | (void)psh;
|
---|
568 | }
|
---|
569 |
|
---|
570 | int sh_kill(shinstance *psh, pid_t pid, int signo)
|
---|
571 | {
|
---|
572 | int rc;
|
---|
573 |
|
---|
574 | #ifdef SH_PURE_STUB_MODE
|
---|
575 | rc = -1;
|
---|
576 | #elif defined(SH_STUB_MODE)
|
---|
577 | # ifdef _MSC_VER
|
---|
578 | rc = -1;
|
---|
579 | # else
|
---|
580 | //fprintf(stderr, "kill(%d, %d)\n", pid, signo);
|
---|
581 | rc = kill(pid, signo);
|
---|
582 | # endif
|
---|
583 | #else
|
---|
584 | #endif
|
---|
585 |
|
---|
586 | TRACE2((psh, "sh_kill(%d, %d) -> %d [%d]\n", pid, signo, rc, errno));
|
---|
587 | (void)psh;
|
---|
588 | return rc;
|
---|
589 | }
|
---|
590 |
|
---|
591 | int sh_killpg(shinstance *psh, pid_t pgid, int signo)
|
---|
592 | {
|
---|
593 | int rc;
|
---|
594 |
|
---|
595 | #ifdef SH_PURE_STUB_MODE
|
---|
596 | rc = -1;
|
---|
597 | #elif defined(SH_STUB_MODE)
|
---|
598 | # ifdef _MSC_VER
|
---|
599 | rc = -1;
|
---|
600 | # else
|
---|
601 | //fprintf(stderr, "killpg(%d, %d)\n", pgid, signo);
|
---|
602 | rc = killpg(pgid, signo);
|
---|
603 | # endif
|
---|
604 | #else
|
---|
605 | #endif
|
---|
606 |
|
---|
607 | TRACE2((psh, "sh_killpg(%d, %d) -> %d [%d]\n", pgid, signo, rc, errno));
|
---|
608 | (void)psh;
|
---|
609 | return rc;
|
---|
610 | }
|
---|
611 |
|
---|
612 | clock_t sh_times(shinstance *psh, shtms *tmsp)
|
---|
613 | {
|
---|
614 | #ifdef SH_PURE_STUB_MODE
|
---|
615 | return 0;
|
---|
616 | #elif defined(SH_STUB_MODE)
|
---|
617 | (void)psh;
|
---|
618 | # ifdef _MSC_VER
|
---|
619 | return 0;
|
---|
620 | # else
|
---|
621 | return times(tmsp);
|
---|
622 | # endif
|
---|
623 | #else
|
---|
624 | #endif
|
---|
625 | }
|
---|
626 |
|
---|
627 | int sh_sysconf_clk_tck(void)
|
---|
628 | {
|
---|
629 | #ifdef SH_PURE_STUB_MODE
|
---|
630 | return 1;
|
---|
631 | #else
|
---|
632 | # ifdef _MSC_VER
|
---|
633 | return CLK_TCK;
|
---|
634 | # else
|
---|
635 | return sysconf(_SC_CLK_TCK);
|
---|
636 | # endif
|
---|
637 | #endif
|
---|
638 | }
|
---|
639 |
|
---|
640 | pid_t sh_fork(shinstance *psh)
|
---|
641 | {
|
---|
642 | pid_t pid;
|
---|
643 | TRACE2((psh, "sh_fork\n"));
|
---|
644 |
|
---|
645 | #ifdef SH_PURE_STUB_MODE
|
---|
646 | pid = -1;
|
---|
647 | #elif defined(SH_STUB_MODE)
|
---|
648 | # ifdef _MSC_VER
|
---|
649 | pid = -1;
|
---|
650 | # else
|
---|
651 | pid = fork();
|
---|
652 | # endif
|
---|
653 | #else
|
---|
654 | #endif
|
---|
655 |
|
---|
656 | TRACE2((psh, "sh_fork -> %d [%d]\n", pid, errno));
|
---|
657 | (void)psh;
|
---|
658 | return pid;
|
---|
659 | }
|
---|
660 |
|
---|
661 | pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
|
---|
662 | {
|
---|
663 | pid_t pidret;
|
---|
664 |
|
---|
665 | *statusp = 0;
|
---|
666 | #ifdef SH_PURE_STUB_MODE
|
---|
667 | pidret = -1;
|
---|
668 | #elif defined(SH_STUB_MODE)
|
---|
669 | # ifdef _MSC_VER
|
---|
670 | pidret = -1;
|
---|
671 | # else
|
---|
672 | pidret = waitpid(pid, statusp, flags);
|
---|
673 | # endif
|
---|
674 | #else
|
---|
675 | #endif
|
---|
676 |
|
---|
677 | TRACE2((psh, "waitpid(%d, %p, %#x) -> %d [%d] *statusp=%#x (rc=%d)\n", pid, statusp, flags,
|
---|
678 | pidret, errno, *statusp, WEXITSTATUS(*statusp)));
|
---|
679 | (void)psh;
|
---|
680 | return pidret;
|
---|
681 | }
|
---|
682 |
|
---|
683 | void sh__exit(shinstance *psh, int rc)
|
---|
684 | {
|
---|
685 | TRACE2((psh, "sh__exit(%d)\n", rc));
|
---|
686 | (void)psh;
|
---|
687 |
|
---|
688 | #ifdef SH_PURE_STUB_MODE
|
---|
689 | return -1;
|
---|
690 | #elif defined(SH_STUB_MODE)
|
---|
691 | _exit(rc);
|
---|
692 | #else
|
---|
693 | #endif
|
---|
694 | }
|
---|
695 |
|
---|
696 | int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
|
---|
697 | {
|
---|
698 | int rc;
|
---|
699 |
|
---|
700 | #ifdef DEBUG
|
---|
701 | /* log it all */
|
---|
702 | TRACE2((psh, "sh_execve(%p:{%s}, %p, %p}\n", exe, exe, argv, envp));
|
---|
703 | for (rc = 0; argv[rc]; rc++)
|
---|
704 | TRACE2((psh, " argv[%d]=%p:{%s}\n", rc, argv[rc], argv[rc]));
|
---|
705 | #endif
|
---|
706 |
|
---|
707 | #ifdef SH_PURE_STUB_MODE
|
---|
708 | rc = -1;
|
---|
709 | #elif defined(SH_STUB_MODE)
|
---|
710 | # ifdef _MSC_VER
|
---|
711 | rc = -1;
|
---|
712 | # else
|
---|
713 | rc = execve(exe, (char **)argv, (char **)envp);
|
---|
714 | # endif
|
---|
715 | #else
|
---|
716 | #endif
|
---|
717 |
|
---|
718 | TRACE2((psh, "sh_execve -> %d [%d]\n", rc, errno));
|
---|
719 | (void)psh;
|
---|
720 | return rc;
|
---|
721 | }
|
---|
722 |
|
---|
723 | uid_t sh_getuid(shinstance *psh)
|
---|
724 | {
|
---|
725 | #ifdef SH_PURE_STUB_MODE
|
---|
726 | uid_t uid = 0;
|
---|
727 | #elif defined(SH_STUB_MODE)
|
---|
728 | # ifdef _MSC_VER
|
---|
729 | uid_t uid = 0;
|
---|
730 | # else
|
---|
731 | uid_t uid = getuid();
|
---|
732 | # endif
|
---|
733 | #else
|
---|
734 | #endif
|
---|
735 |
|
---|
736 | TRACE2((psh, "sh_getuid() -> %d [%d]\n", uid, errno));
|
---|
737 | (void)psh;
|
---|
738 | return uid;
|
---|
739 | }
|
---|
740 |
|
---|
741 | uid_t sh_geteuid(shinstance *psh)
|
---|
742 | {
|
---|
743 | #ifdef SH_PURE_STUB_MODE
|
---|
744 | uid_t euid = 0;
|
---|
745 | #elif defined(SH_STUB_MODE)
|
---|
746 | # ifdef _MSC_VER
|
---|
747 | uid_t euid = 0;
|
---|
748 | # else
|
---|
749 | uid_t euid = geteuid();
|
---|
750 | # endif
|
---|
751 | #else
|
---|
752 | #endif
|
---|
753 |
|
---|
754 | TRACE2((psh, "sh_geteuid() -> %d [%d]\n", euid, errno));
|
---|
755 | (void)psh;
|
---|
756 | return euid;
|
---|
757 | }
|
---|
758 |
|
---|
759 | gid_t sh_getgid(shinstance *psh)
|
---|
760 | {
|
---|
761 | #ifdef SH_PURE_STUB_MODE
|
---|
762 | gid_t gid = 0;
|
---|
763 | #elif defined(SH_STUB_MODE)
|
---|
764 | # ifdef _MSC_VER
|
---|
765 | gid_t gid = 0;
|
---|
766 | # else
|
---|
767 | gid_t gid = getgid();
|
---|
768 | # endif
|
---|
769 | #else
|
---|
770 | #endif
|
---|
771 |
|
---|
772 | TRACE2((psh, "sh_getgid() -> %d [%d]\n", gid, errno));
|
---|
773 | (void)psh;
|
---|
774 | return gid;
|
---|
775 | }
|
---|
776 |
|
---|
777 | gid_t sh_getegid(shinstance *psh)
|
---|
778 | {
|
---|
779 | #ifdef SH_PURE_STUB_MODE
|
---|
780 | gid_t egid = 0;
|
---|
781 | #elif defined(SH_STUB_MODE)
|
---|
782 | # ifdef _MSC_VER
|
---|
783 | gid_t egid = 0;
|
---|
784 | # else
|
---|
785 | gid_t egid = getegid();
|
---|
786 | # endif
|
---|
787 | #else
|
---|
788 | #endif
|
---|
789 |
|
---|
790 | TRACE2((psh, "sh_getegid() -> %d [%d]\n", egid, errno));
|
---|
791 | (void)psh;
|
---|
792 | return egid;
|
---|
793 | }
|
---|
794 |
|
---|
795 | pid_t sh_getpid(shinstance *psh)
|
---|
796 | {
|
---|
797 | pid_t pid;
|
---|
798 |
|
---|
799 | #ifdef SH_PURE_STUB_MODE
|
---|
800 | pid = 0;
|
---|
801 | #elif defined(SH_STUB_MODE)
|
---|
802 | # ifdef _MSC_VER
|
---|
803 | pid = _getpid();
|
---|
804 | # else
|
---|
805 | pid = getpid();
|
---|
806 | # endif
|
---|
807 | #else
|
---|
808 | #endif
|
---|
809 |
|
---|
810 | (void)psh;
|
---|
811 | return pid;
|
---|
812 | }
|
---|
813 |
|
---|
814 | pid_t sh_getpgrp(shinstance *psh)
|
---|
815 | {
|
---|
816 | #ifdef SH_PURE_STUB_MODE
|
---|
817 | pid_t pgrp = 0;
|
---|
818 | #elif defined(SH_STUB_MODE)
|
---|
819 | # ifdef _MSC_VER
|
---|
820 | pid_t pgrp _getpid();
|
---|
821 | # else
|
---|
822 | pid_t pgrp = getpgrp();
|
---|
823 | # endif
|
---|
824 | #else
|
---|
825 | #endif
|
---|
826 |
|
---|
827 | TRACE2((psh, "sh_getpgrp() -> %d [%d]\n", pgrp, errno));
|
---|
828 | (void)psh;
|
---|
829 | return pgrp;
|
---|
830 | }
|
---|
831 |
|
---|
832 | pid_t sh_getpgid(shinstance *psh, pid_t pid)
|
---|
833 | {
|
---|
834 | #ifdef SH_PURE_STUB_MODE
|
---|
835 | pid_t pgid = pid;
|
---|
836 | #elif defined(SH_STUB_MODE)
|
---|
837 | # ifdef _MSC_VER
|
---|
838 | pid_t pgid = pid;
|
---|
839 | # else
|
---|
840 | pid_t pgid = getpgid(pid);
|
---|
841 | # endif
|
---|
842 | #else
|
---|
843 | #endif
|
---|
844 |
|
---|
845 | TRACE2((psh, "sh_getpgid(%d) -> %d [%d]\n", pid, pgid, errno));
|
---|
846 | (void)psh;
|
---|
847 | return pgid;
|
---|
848 | }
|
---|
849 |
|
---|
850 | int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
|
---|
851 | {
|
---|
852 | #ifdef SH_PURE_STUB_MODE
|
---|
853 | int rc = -1;
|
---|
854 | #elif defined(SH_STUB_MODE)
|
---|
855 | # ifdef _MSC_VER
|
---|
856 | int rc = -1;
|
---|
857 | # else
|
---|
858 | int rc = setpgid(pid, pgid);
|
---|
859 | # endif
|
---|
860 | #else
|
---|
861 | #endif
|
---|
862 |
|
---|
863 | TRACE2((psh, "sh_setpgid(%d, %d) -> %d [%d]\n", pid, pgid, rc, errno));
|
---|
864 | (void)psh;
|
---|
865 | return rc;
|
---|
866 | }
|
---|
867 |
|
---|
868 | pid_t sh_tcgetpgrp(shinstance *psh, int fd)
|
---|
869 | {
|
---|
870 | pid_t pgrp;
|
---|
871 |
|
---|
872 | #ifdef SH_PURE_STUB_MODE
|
---|
873 | pgrp = -1;
|
---|
874 | #elif defined(SH_STUB_MODE)
|
---|
875 | # ifdef _MSC_VER
|
---|
876 | pgrp = -1;
|
---|
877 | # else
|
---|
878 | pgrp = tcgetpgrp(fd);
|
---|
879 | # endif
|
---|
880 | #else
|
---|
881 | #endif
|
---|
882 |
|
---|
883 | TRACE2((psh, "sh_tcgetpgrp(%d) -> %d [%d]\n", fd, pgrp, errno));
|
---|
884 | (void)psh;
|
---|
885 | return pgrp;
|
---|
886 | }
|
---|
887 |
|
---|
888 | int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
|
---|
889 | {
|
---|
890 | int rc;
|
---|
891 | TRACE2((psh, "sh_tcsetpgrp(%d, %d)\n", fd, pgrp));
|
---|
892 |
|
---|
893 | #ifdef SH_PURE_STUB_MODE
|
---|
894 | rc = -1;
|
---|
895 | #elif defined(SH_STUB_MODE)
|
---|
896 | # ifdef _MSC_VER
|
---|
897 | rc = -1;
|
---|
898 | # else
|
---|
899 | rc = tcsetpgrp(fd, pgrp);
|
---|
900 | # endif
|
---|
901 | #else
|
---|
902 | #endif
|
---|
903 |
|
---|
904 | TRACE2((psh, "sh_tcsetpgrp(%d, %d) -> %d [%d]\n", fd, pgrp, rc, errno));
|
---|
905 | (void)psh;
|
---|
906 | return rc;
|
---|
907 | }
|
---|
908 |
|
---|
909 | int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
|
---|
910 | {
|
---|
911 | #ifdef SH_PURE_STUB_MODE
|
---|
912 | int rc = -1;
|
---|
913 | #elif defined(SH_STUB_MODE)
|
---|
914 | # ifdef _MSC_VER
|
---|
915 | int rc = -1;
|
---|
916 | # else
|
---|
917 | int rc = getrlimit(resid, limp);
|
---|
918 | # endif
|
---|
919 | #else
|
---|
920 | #endif
|
---|
921 |
|
---|
922 | TRACE2((psh, "sh_getrlimit(%d, %p) -> %d [%d] {%ld,%ld}\n",
|
---|
923 | resid, limp, rc, errno, (long)limp->rlim_cur, (long)limp->rlim_max));
|
---|
924 | (void)psh;
|
---|
925 | return rc;
|
---|
926 | }
|
---|
927 |
|
---|
928 | int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
|
---|
929 | {
|
---|
930 | #ifdef SH_PURE_STUB_MODE
|
---|
931 | int rc = -1;
|
---|
932 | #elif defined(SH_STUB_MODE)
|
---|
933 | # ifdef _MSC_VER
|
---|
934 | int rc = -1;
|
---|
935 | # else
|
---|
936 | int rc = setrlimit(resid, limp);
|
---|
937 | # endif
|
---|
938 | #else
|
---|
939 | #endif
|
---|
940 |
|
---|
941 | TRACE2((psh, "sh_setrlimit(%d, %p:{%ld,%ld}) -> %d [%d]\n",
|
---|
942 | resid, limp, (long)limp->rlim_cur, (long)limp->rlim_max, rc, errno));
|
---|
943 | (void)psh;
|
---|
944 | return rc;
|
---|
945 | }
|
---|
946 |
|
---|