1 | /* $Id: shinstance.c 2282 2009-02-24 04:08:51Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * The shell instance methods.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
|
---|
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 | shmtxtmp tmp;
|
---|
350 | shinstance *psh;
|
---|
351 |
|
---|
352 | fprintf(stderr, "sh_sig_common_handler: signo=%d:%s\n", signo, sys_signame[signo]);
|
---|
353 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
354 |
|
---|
355 | /** @todo signal focus chain or something? Atm there will only be one shell,
|
---|
356 | * so it's not really important until we go threaded for real... */
|
---|
357 | psh = g_sh_tail;
|
---|
358 | while (psh != NULL)
|
---|
359 | {
|
---|
360 | if (psh->sigactions[signo].sh_handler == SH_SIG_DFL)
|
---|
361 | /* implement... */;
|
---|
362 | else if (psh->sigactions[signo].sh_handler != SIG_IGN)
|
---|
363 | psh->sigactions[signo].sh_handler(psh, signo);
|
---|
364 |
|
---|
365 | psh = psh->prev;
|
---|
366 | }
|
---|
367 |
|
---|
368 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | int sh_sigaction(shinstance *psh, int signo, const struct shsigaction *newp, struct shsigaction *oldp)
|
---|
373 | {
|
---|
374 | if (newp)
|
---|
375 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=%p:{.sh_handler=%p, .sh_flags=%#x} oldp=%p\n",
|
---|
376 | signo, sys_signame[signo], newp, newp->sh_handler, newp->sh_flags, oldp));
|
---|
377 | else
|
---|
378 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=NULL oldp=%p\n", signo, sys_signame[signo], oldp));
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * Input validation.
|
---|
382 | */
|
---|
383 | if (signo >= NSIG || signo <= 0)
|
---|
384 | {
|
---|
385 | errno = EINVAL;
|
---|
386 | return -1;
|
---|
387 | }
|
---|
388 |
|
---|
389 | #ifdef SH_PURE_STUB_MODE
|
---|
390 | return -1;
|
---|
391 | #else
|
---|
392 |
|
---|
393 | /*
|
---|
394 | * Make sure our data is correct.
|
---|
395 | */
|
---|
396 | sh_int_lazy_init_sigaction(psh, signo);
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Get the old one if requested.
|
---|
400 | */
|
---|
401 | if (oldp)
|
---|
402 | *oldp = psh->sigactions[signo];
|
---|
403 |
|
---|
404 | /*
|
---|
405 | * Set the new one if it has changed.
|
---|
406 | *
|
---|
407 | * This will be attempted coordinated with the other signal handlers so
|
---|
408 | * that we can arrive at a common denominator.
|
---|
409 | */
|
---|
410 | if ( newp
|
---|
411 | && memcmp(&psh->sigactions[signo], newp, sizeof(*newp)))
|
---|
412 | {
|
---|
413 | shmtxtmp tmp;
|
---|
414 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
415 |
|
---|
416 | /* Undo the accounting for the current entry. */
|
---|
417 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
---|
418 | g_sig_state[signo].num_ignore--;
|
---|
419 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
---|
420 | g_sig_state[signo].num_specific--;
|
---|
421 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
---|
422 | g_sig_state[signo].num_restart--;
|
---|
423 |
|
---|
424 | /* Set the new entry. */
|
---|
425 | psh->sigactions[signo] = *newp;
|
---|
426 |
|
---|
427 | /* Add the bits for the new action entry. */
|
---|
428 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
---|
429 | g_sig_state[signo].num_ignore++;
|
---|
430 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
---|
431 | g_sig_state[signo].num_specific++;
|
---|
432 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
---|
433 | g_sig_state[signo].num_restart++;
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * Calc new common action.
|
---|
437 | *
|
---|
438 | * This is quit a bit ASSUMPTIVE about the limited use. We will not
|
---|
439 | * bother synching the mask, and we pretend to care about SA_RESTART.
|
---|
440 | * The only thing we really actually care about is the sh_handler.
|
---|
441 | *
|
---|
442 | * On second though, it's possible we should just tie this to the root
|
---|
443 | * shell since it only really applies to external signal ...
|
---|
444 | */
|
---|
445 | if ( g_sig_state[signo].num_specific
|
---|
446 | || g_sig_state[signo].num_ignore != g_num_shells)
|
---|
447 | g_sig_state[signo].sa.sa_handler = sh_sig_common_handler;
|
---|
448 | else if (g_sig_state[signo].num_ignore)
|
---|
449 | g_sig_state[signo].sa.sa_handler = SIG_IGN;
|
---|
450 | else
|
---|
451 | g_sig_state[signo].sa.sa_handler = SIG_DFL;
|
---|
452 | g_sig_state[signo].sa.sa_flags = psh->sigactions[signo].sh_flags & SA_RESTART;
|
---|
453 |
|
---|
454 | TRACE2((psh, "sh_sigaction: setting signo=%d:%s to {.sa_handler=%p, .sa_flags=%#x}\n",
|
---|
455 | signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
|
---|
456 | # ifdef _MSC_VER
|
---|
457 | if (signal(signo, g_sig_state[signo].sa.sa_handler) == SIG_ERR)
|
---|
458 | # else
|
---|
459 | if (sigaction(signo, &g_sig_state[signo].sa, NULL))
|
---|
460 | # endif
|
---|
461 | assert(0);
|
---|
462 |
|
---|
463 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
464 | }
|
---|
465 |
|
---|
466 | return 0;
|
---|
467 | #endif
|
---|
468 | }
|
---|
469 |
|
---|
470 | shsig_t sh_signal(shinstance *psh, int signo, shsig_t handler)
|
---|
471 | {
|
---|
472 | shsigaction_t sa;
|
---|
473 | shsig_t ret;
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Implementation using sh_sigaction.
|
---|
477 | */
|
---|
478 | if (sh_sigaction(psh, signo, NULL, &sa))
|
---|
479 | return SH_SIG_ERR;
|
---|
480 |
|
---|
481 | ret = sa.sh_handler;
|
---|
482 | sa.sh_flags &= SA_RESTART;
|
---|
483 | sa.sh_handler = handler;
|
---|
484 | sh_sigemptyset(&sa.sh_mask);
|
---|
485 | sh_sigaddset(&sa.sh_mask, signo); /* ?? */
|
---|
486 | if (sh_sigaction(psh, signo, &sa, NULL))
|
---|
487 | return SH_SIG_ERR;
|
---|
488 |
|
---|
489 | return ret;
|
---|
490 | }
|
---|
491 |
|
---|
492 | int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
|
---|
493 | {
|
---|
494 | shsigaction_t sa;
|
---|
495 | int oldflags = 0;
|
---|
496 |
|
---|
497 | /*
|
---|
498 | * Implementation using sh_sigaction.
|
---|
499 | */
|
---|
500 | if (sh_sigaction(psh, signo, NULL, &sa))
|
---|
501 | return -1;
|
---|
502 | oldflags = sa.sh_flags;
|
---|
503 | if (interrupt)
|
---|
504 | sa.sh_flags &= ~SA_RESTART;
|
---|
505 | else
|
---|
506 | sa.sh_flags |= ~SA_RESTART;
|
---|
507 | if (!((oldflags ^ sa.sh_flags) & SA_RESTART))
|
---|
508 | return 0; /* unchanged. */
|
---|
509 |
|
---|
510 | return sh_sigaction(psh, signo, &sa, NULL);
|
---|
511 | }
|
---|
512 |
|
---|
513 | void sh_sigemptyset(shsigset_t *setp)
|
---|
514 | {
|
---|
515 | memset(setp, 0, sizeof(*setp));
|
---|
516 | }
|
---|
517 |
|
---|
518 | void sh_sigaddset(shsigset_t *setp, int signo)
|
---|
519 | {
|
---|
520 | #ifdef _MSC_VER
|
---|
521 | *setp |= 1U << signo;
|
---|
522 | #else
|
---|
523 | sigaddset(setp, signo);
|
---|
524 | #endif
|
---|
525 | }
|
---|
526 |
|
---|
527 | void sh_sigdelset(shsigset_t *setp, int signo)
|
---|
528 | {
|
---|
529 | #ifdef _MSC_VER
|
---|
530 | *setp &= ~(1U << signo);
|
---|
531 | #else
|
---|
532 | sigdelset(setp, signo);
|
---|
533 | #endif
|
---|
534 | }
|
---|
535 |
|
---|
536 | int sh_sigismember(shsigset_t *setp, int signo)
|
---|
537 | {
|
---|
538 | #ifdef _MSC_VER
|
---|
539 | return !!(*setp & (1U << signo));
|
---|
540 | #else
|
---|
541 | return !!sigismember(setp, signo);
|
---|
542 | #endif
|
---|
543 | }
|
---|
544 |
|
---|
545 | int sh_sigprocmask(shinstance *psh, int operation, shsigset_t const *newp, shsigset_t *oldp)
|
---|
546 | {
|
---|
547 | #ifdef SH_PURE_STUB_MODE
|
---|
548 | return -1;
|
---|
549 | #elif defined(SH_STUB_MODE)
|
---|
550 | (void)psh;
|
---|
551 | # ifdef _MSC_VER
|
---|
552 | return -1;
|
---|
553 | # else
|
---|
554 | return sigprocmask(operation, newp, oldp);
|
---|
555 | # endif
|
---|
556 | #else
|
---|
557 | #endif
|
---|
558 | }
|
---|
559 |
|
---|
560 | void sh_abort(shinstance *psh)
|
---|
561 | {
|
---|
562 | TRACE2((psh, "sh_abort\n"));
|
---|
563 |
|
---|
564 | #ifdef SH_PURE_STUB_MODE
|
---|
565 | #elif defined(SH_STUB_MODE)
|
---|
566 | abort();
|
---|
567 | #else
|
---|
568 | #endif
|
---|
569 |
|
---|
570 | TRACE2((psh, "sh_abort returns!\n"));
|
---|
571 | (void)psh;
|
---|
572 | }
|
---|
573 |
|
---|
574 | void sh_raise_sigint(shinstance *psh)
|
---|
575 | {
|
---|
576 | TRACE2((psh, "sh_raise(SIGINT)\n"));
|
---|
577 |
|
---|
578 | #ifdef SH_PURE_STUB_MODE
|
---|
579 | #elif defined(SH_STUB_MODE)
|
---|
580 | (void)psh;
|
---|
581 | raise(SIGINT);
|
---|
582 | #else
|
---|
583 | #endif
|
---|
584 |
|
---|
585 | TRACE2((psh, "sh_raise(SIGINT) returns\n"));
|
---|
586 | (void)psh;
|
---|
587 | }
|
---|
588 |
|
---|
589 | int sh_kill(shinstance *psh, pid_t pid, int signo)
|
---|
590 | {
|
---|
591 | int rc;
|
---|
592 |
|
---|
593 | #ifdef SH_PURE_STUB_MODE
|
---|
594 | rc = -1;
|
---|
595 | #elif defined(SH_STUB_MODE)
|
---|
596 | # ifdef _MSC_VER
|
---|
597 | rc = -1;
|
---|
598 | # else
|
---|
599 | fprintf(stderr, "kill(%d, %d)\n", pid, signo);
|
---|
600 | rc = kill(pid, signo);
|
---|
601 | # endif
|
---|
602 | #else
|
---|
603 | #endif
|
---|
604 |
|
---|
605 | TRACE2((psh, "sh_kill(%d, %d) -> %d [%d]\n", pid, signo, rc, errno));
|
---|
606 | (void)psh;
|
---|
607 | return rc;
|
---|
608 | }
|
---|
609 |
|
---|
610 | int sh_killpg(shinstance *psh, pid_t pgid, int signo)
|
---|
611 | {
|
---|
612 | int rc;
|
---|
613 |
|
---|
614 | #ifdef SH_PURE_STUB_MODE
|
---|
615 | rc = -1;
|
---|
616 | #elif defined(SH_STUB_MODE)
|
---|
617 | # ifdef _MSC_VER
|
---|
618 | rc = -1;
|
---|
619 | # else
|
---|
620 | //fprintf(stderr, "killpg(%d, %d)\n", pgid, signo);
|
---|
621 | rc = killpg(pgid, signo);
|
---|
622 | # endif
|
---|
623 | #else
|
---|
624 | #endif
|
---|
625 |
|
---|
626 | TRACE2((psh, "sh_killpg(%d, %d) -> %d [%d]\n", pgid, signo, rc, errno));
|
---|
627 | (void)psh;
|
---|
628 | return rc;
|
---|
629 | }
|
---|
630 |
|
---|
631 | clock_t sh_times(shinstance *psh, shtms *tmsp)
|
---|
632 | {
|
---|
633 | #ifdef SH_PURE_STUB_MODE
|
---|
634 | return 0;
|
---|
635 | #elif defined(SH_STUB_MODE)
|
---|
636 | (void)psh;
|
---|
637 | # ifdef _MSC_VER
|
---|
638 | return 0;
|
---|
639 | # else
|
---|
640 | return times(tmsp);
|
---|
641 | # endif
|
---|
642 | #else
|
---|
643 | #endif
|
---|
644 | }
|
---|
645 |
|
---|
646 | int sh_sysconf_clk_tck(void)
|
---|
647 | {
|
---|
648 | #ifdef SH_PURE_STUB_MODE
|
---|
649 | return 1;
|
---|
650 | #else
|
---|
651 | # ifdef _MSC_VER
|
---|
652 | return CLK_TCK;
|
---|
653 | # else
|
---|
654 | return sysconf(_SC_CLK_TCK);
|
---|
655 | # endif
|
---|
656 | #endif
|
---|
657 | }
|
---|
658 |
|
---|
659 | pid_t sh_fork(shinstance *psh)
|
---|
660 | {
|
---|
661 | pid_t pid;
|
---|
662 | TRACE2((psh, "sh_fork\n"));
|
---|
663 |
|
---|
664 | #ifdef SH_PURE_STUB_MODE
|
---|
665 | pid = -1;
|
---|
666 | #elif defined(SH_STUB_MODE)
|
---|
667 | # ifdef _MSC_VER
|
---|
668 | pid = -1;
|
---|
669 | # else
|
---|
670 | pid = fork();
|
---|
671 | # endif
|
---|
672 | #else
|
---|
673 | #endif
|
---|
674 |
|
---|
675 | TRACE2((psh, "sh_fork -> %d [%d]\n", pid, errno));
|
---|
676 | (void)psh;
|
---|
677 | return pid;
|
---|
678 | }
|
---|
679 |
|
---|
680 | pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
|
---|
681 | {
|
---|
682 | pid_t pidret;
|
---|
683 |
|
---|
684 | *statusp = 0;
|
---|
685 | #ifdef SH_PURE_STUB_MODE
|
---|
686 | pidret = -1;
|
---|
687 | #elif defined(SH_STUB_MODE)
|
---|
688 | # ifdef _MSC_VER
|
---|
689 | pidret = -1;
|
---|
690 | # else
|
---|
691 | pidret = waitpid(pid, statusp, flags);
|
---|
692 | # endif
|
---|
693 | #else
|
---|
694 | #endif
|
---|
695 |
|
---|
696 | TRACE2((psh, "waitpid(%d, %p, %#x) -> %d [%d] *statusp=%#x (rc=%d)\n", pid, statusp, flags,
|
---|
697 | pidret, errno, *statusp, WEXITSTATUS(*statusp)));
|
---|
698 | (void)psh;
|
---|
699 | return pidret;
|
---|
700 | }
|
---|
701 |
|
---|
702 | void sh__exit(shinstance *psh, int rc)
|
---|
703 | {
|
---|
704 | TRACE2((psh, "sh__exit(%d)\n", rc));
|
---|
705 | (void)psh;
|
---|
706 |
|
---|
707 | #ifdef SH_PURE_STUB_MODE
|
---|
708 | return -1;
|
---|
709 | #elif defined(SH_STUB_MODE)
|
---|
710 | _exit(rc);
|
---|
711 | #else
|
---|
712 | #endif
|
---|
713 | }
|
---|
714 |
|
---|
715 | int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
|
---|
716 | {
|
---|
717 | int rc;
|
---|
718 |
|
---|
719 | #ifdef DEBUG
|
---|
720 | /* log it all */
|
---|
721 | TRACE2((psh, "sh_execve(%p:{%s}, %p, %p}\n", exe, exe, argv, envp));
|
---|
722 | for (rc = 0; argv[rc]; rc++)
|
---|
723 | TRACE2((psh, " argv[%d]=%p:{%s}\n", rc, argv[rc], argv[rc]));
|
---|
724 | #endif
|
---|
725 |
|
---|
726 | #ifdef SH_PURE_STUB_MODE
|
---|
727 | rc = -1;
|
---|
728 | #elif defined(SH_STUB_MODE)
|
---|
729 | # ifdef _MSC_VER
|
---|
730 | rc = -1;
|
---|
731 | # else
|
---|
732 | rc = execve(exe, (char **)argv, (char **)envp);
|
---|
733 | # endif
|
---|
734 | #else
|
---|
735 | #endif
|
---|
736 |
|
---|
737 | TRACE2((psh, "sh_execve -> %d [%d]\n", rc, errno));
|
---|
738 | (void)psh;
|
---|
739 | return rc;
|
---|
740 | }
|
---|
741 |
|
---|
742 | uid_t sh_getuid(shinstance *psh)
|
---|
743 | {
|
---|
744 | #ifdef SH_PURE_STUB_MODE
|
---|
745 | uid_t uid = 0;
|
---|
746 | #elif defined(SH_STUB_MODE)
|
---|
747 | # ifdef _MSC_VER
|
---|
748 | uid_t uid = 0;
|
---|
749 | # else
|
---|
750 | uid_t uid = getuid();
|
---|
751 | # endif
|
---|
752 | #else
|
---|
753 | #endif
|
---|
754 |
|
---|
755 | TRACE2((psh, "sh_getuid() -> %d [%d]\n", uid, errno));
|
---|
756 | (void)psh;
|
---|
757 | return uid;
|
---|
758 | }
|
---|
759 |
|
---|
760 | uid_t sh_geteuid(shinstance *psh)
|
---|
761 | {
|
---|
762 | #ifdef SH_PURE_STUB_MODE
|
---|
763 | uid_t euid = 0;
|
---|
764 | #elif defined(SH_STUB_MODE)
|
---|
765 | # ifdef _MSC_VER
|
---|
766 | uid_t euid = 0;
|
---|
767 | # else
|
---|
768 | uid_t euid = geteuid();
|
---|
769 | # endif
|
---|
770 | #else
|
---|
771 | #endif
|
---|
772 |
|
---|
773 | TRACE2((psh, "sh_geteuid() -> %d [%d]\n", euid, errno));
|
---|
774 | (void)psh;
|
---|
775 | return euid;
|
---|
776 | }
|
---|
777 |
|
---|
778 | gid_t sh_getgid(shinstance *psh)
|
---|
779 | {
|
---|
780 | #ifdef SH_PURE_STUB_MODE
|
---|
781 | gid_t gid = 0;
|
---|
782 | #elif defined(SH_STUB_MODE)
|
---|
783 | # ifdef _MSC_VER
|
---|
784 | gid_t gid = 0;
|
---|
785 | # else
|
---|
786 | gid_t gid = getgid();
|
---|
787 | # endif
|
---|
788 | #else
|
---|
789 | #endif
|
---|
790 |
|
---|
791 | TRACE2((psh, "sh_getgid() -> %d [%d]\n", gid, errno));
|
---|
792 | (void)psh;
|
---|
793 | return gid;
|
---|
794 | }
|
---|
795 |
|
---|
796 | gid_t sh_getegid(shinstance *psh)
|
---|
797 | {
|
---|
798 | #ifdef SH_PURE_STUB_MODE
|
---|
799 | gid_t egid = 0;
|
---|
800 | #elif defined(SH_STUB_MODE)
|
---|
801 | # ifdef _MSC_VER
|
---|
802 | gid_t egid = 0;
|
---|
803 | # else
|
---|
804 | gid_t egid = getegid();
|
---|
805 | # endif
|
---|
806 | #else
|
---|
807 | #endif
|
---|
808 |
|
---|
809 | TRACE2((psh, "sh_getegid() -> %d [%d]\n", egid, errno));
|
---|
810 | (void)psh;
|
---|
811 | return egid;
|
---|
812 | }
|
---|
813 |
|
---|
814 | pid_t sh_getpid(shinstance *psh)
|
---|
815 | {
|
---|
816 | pid_t pid;
|
---|
817 |
|
---|
818 | #ifdef SH_PURE_STUB_MODE
|
---|
819 | pid = 0;
|
---|
820 | #elif defined(SH_STUB_MODE)
|
---|
821 | # ifdef _MSC_VER
|
---|
822 | pid = _getpid();
|
---|
823 | # else
|
---|
824 | pid = getpid();
|
---|
825 | # endif
|
---|
826 | #else
|
---|
827 | #endif
|
---|
828 |
|
---|
829 | (void)psh;
|
---|
830 | return pid;
|
---|
831 | }
|
---|
832 |
|
---|
833 | pid_t sh_getpgrp(shinstance *psh)
|
---|
834 | {
|
---|
835 | #ifdef SH_PURE_STUB_MODE
|
---|
836 | pid_t pgrp = 0;
|
---|
837 | #elif defined(SH_STUB_MODE)
|
---|
838 | # ifdef _MSC_VER
|
---|
839 | pid_t pgrp _getpid();
|
---|
840 | # else
|
---|
841 | pid_t pgrp = getpgrp();
|
---|
842 | # endif
|
---|
843 | #else
|
---|
844 | #endif
|
---|
845 |
|
---|
846 | TRACE2((psh, "sh_getpgrp() -> %d [%d]\n", pgrp, errno));
|
---|
847 | (void)psh;
|
---|
848 | return pgrp;
|
---|
849 | }
|
---|
850 |
|
---|
851 | pid_t sh_getpgid(shinstance *psh, pid_t pid)
|
---|
852 | {
|
---|
853 | #ifdef SH_PURE_STUB_MODE
|
---|
854 | pid_t pgid = pid;
|
---|
855 | #elif defined(SH_STUB_MODE)
|
---|
856 | # ifdef _MSC_VER
|
---|
857 | pid_t pgid = pid;
|
---|
858 | # else
|
---|
859 | pid_t pgid = getpgid(pid);
|
---|
860 | # endif
|
---|
861 | #else
|
---|
862 | #endif
|
---|
863 |
|
---|
864 | TRACE2((psh, "sh_getpgid(%d) -> %d [%d]\n", pid, pgid, errno));
|
---|
865 | (void)psh;
|
---|
866 | return pgid;
|
---|
867 | }
|
---|
868 |
|
---|
869 | int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
|
---|
870 | {
|
---|
871 | #ifdef SH_PURE_STUB_MODE
|
---|
872 | int rc = -1;
|
---|
873 | #elif defined(SH_STUB_MODE)
|
---|
874 | # ifdef _MSC_VER
|
---|
875 | int rc = -1;
|
---|
876 | # else
|
---|
877 | int rc = setpgid(pid, pgid);
|
---|
878 | # endif
|
---|
879 | #else
|
---|
880 | #endif
|
---|
881 |
|
---|
882 | TRACE2((psh, "sh_setpgid(%d, %d) -> %d [%d]\n", pid, pgid, rc, errno));
|
---|
883 | (void)psh;
|
---|
884 | return rc;
|
---|
885 | }
|
---|
886 |
|
---|
887 | pid_t sh_tcgetpgrp(shinstance *psh, int fd)
|
---|
888 | {
|
---|
889 | pid_t pgrp;
|
---|
890 |
|
---|
891 | #ifdef SH_PURE_STUB_MODE
|
---|
892 | pgrp = -1;
|
---|
893 | #elif defined(SH_STUB_MODE)
|
---|
894 | # ifdef _MSC_VER
|
---|
895 | pgrp = -1;
|
---|
896 | # else
|
---|
897 | pgrp = tcgetpgrp(fd);
|
---|
898 | # endif
|
---|
899 | #else
|
---|
900 | #endif
|
---|
901 |
|
---|
902 | TRACE2((psh, "sh_tcgetpgrp(%d) -> %d [%d]\n", fd, pgrp, errno));
|
---|
903 | (void)psh;
|
---|
904 | return pgrp;
|
---|
905 | }
|
---|
906 |
|
---|
907 | int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
|
---|
908 | {
|
---|
909 | int rc;
|
---|
910 | TRACE2((psh, "sh_tcsetpgrp(%d, %d)\n", fd, pgrp));
|
---|
911 |
|
---|
912 | #ifdef SH_PURE_STUB_MODE
|
---|
913 | rc = -1;
|
---|
914 | #elif defined(SH_STUB_MODE)
|
---|
915 | # ifdef _MSC_VER
|
---|
916 | rc = -1;
|
---|
917 | # else
|
---|
918 | rc = tcsetpgrp(fd, pgrp);
|
---|
919 | # endif
|
---|
920 | #else
|
---|
921 | #endif
|
---|
922 |
|
---|
923 | TRACE2((psh, "sh_tcsetpgrp(%d, %d) -> %d [%d]\n", fd, pgrp, rc, errno));
|
---|
924 | (void)psh;
|
---|
925 | return rc;
|
---|
926 | }
|
---|
927 |
|
---|
928 | int sh_getrlimit(shinstance *psh, int resid, 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 = getrlimit(resid, limp);
|
---|
937 | # endif
|
---|
938 | #else
|
---|
939 | #endif
|
---|
940 |
|
---|
941 | TRACE2((psh, "sh_getrlimit(%d, %p) -> %d [%d] {%ld,%ld}\n",
|
---|
942 | resid, limp, rc, errno, (long)limp->rlim_cur, (long)limp->rlim_max));
|
---|
943 | (void)psh;
|
---|
944 | return rc;
|
---|
945 | }
|
---|
946 |
|
---|
947 | int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
|
---|
948 | {
|
---|
949 | #ifdef SH_PURE_STUB_MODE
|
---|
950 | int rc = -1;
|
---|
951 | #elif defined(SH_STUB_MODE)
|
---|
952 | # ifdef _MSC_VER
|
---|
953 | int rc = -1;
|
---|
954 | # else
|
---|
955 | int rc = setrlimit(resid, limp);
|
---|
956 | # endif
|
---|
957 | #else
|
---|
958 | #endif
|
---|
959 |
|
---|
960 | TRACE2((psh, "sh_setrlimit(%d, %p:{%ld,%ld}) -> %d [%d]\n",
|
---|
961 | resid, limp, (long)limp->rlim_cur, (long)limp->rlim_max, rc, errno));
|
---|
962 | (void)psh;
|
---|
963 | return rc;
|
---|
964 | }
|
---|
965 |
|
---|