1 | /* $Id: shinstance.c 2291 2009-02-28 01:06:16Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * The shell instance methods.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
|
---|
8 | *
|
---|
9 | *
|
---|
10 | * This file is part of kBuild.
|
---|
11 | *
|
---|
12 | * kBuild is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * kBuild is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with kBuild; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <string.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <assert.h>
|
---|
34 | #ifdef _MSC_VER
|
---|
35 | # include <process.h>
|
---|
36 | #else
|
---|
37 | # include <unistd.h>
|
---|
38 | # include <pwd.h>
|
---|
39 | #endif
|
---|
40 | #include "shinstance.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*******************************************************************************
|
---|
44 | * Global Variables *
|
---|
45 | *******************************************************************************/
|
---|
46 | /** The mutex protecting the the globals and some shell instance members (sigs). */
|
---|
47 | static shmtx g_sh_mtx;
|
---|
48 | /** The root shell instance. */
|
---|
49 | static shinstance *g_sh_root;
|
---|
50 | /** The first shell instance. */
|
---|
51 | static shinstance *g_sh_head;
|
---|
52 | /** The last shell instance. */
|
---|
53 | static shinstance *g_sh_tail;
|
---|
54 | /** The number of shells. */
|
---|
55 | static int volatile g_num_shells;
|
---|
56 | /** Per signal state for determining a common denominator.
|
---|
57 | * @remarks defaults and unmasked actions aren't counted. */
|
---|
58 | struct shsigstate
|
---|
59 | {
|
---|
60 | /** The current signal action. */
|
---|
61 | #ifndef _MSC_VER
|
---|
62 | struct sigaction sa;
|
---|
63 | #else
|
---|
64 | struct
|
---|
65 | {
|
---|
66 | void (*sa_handler)(int);
|
---|
67 | int sa_flags;
|
---|
68 | shsigset_t sa_mask;
|
---|
69 | } sa;
|
---|
70 | #endif
|
---|
71 | /** The number of restarts (siginterrupt / SA_RESTART). */
|
---|
72 | int num_restart;
|
---|
73 | /** The number of ignore handlers. */
|
---|
74 | int num_ignore;
|
---|
75 | /** The number of specific handlers. */
|
---|
76 | int num_specific;
|
---|
77 | /** The number of threads masking it. */
|
---|
78 | int num_masked;
|
---|
79 | } g_sig_state[NSIG];
|
---|
80 |
|
---|
81 |
|
---|
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 | * Links the shell instance.
|
---|
108 | *
|
---|
109 | * @param psh The shell.
|
---|
110 | */
|
---|
111 | static void sh_int_link(shinstance *psh)
|
---|
112 | {
|
---|
113 | shmtxtmp tmp;
|
---|
114 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
115 |
|
---|
116 | if (psh->rootshell)
|
---|
117 | g_sh_root = psh;
|
---|
118 |
|
---|
119 | psh->next = NULL;
|
---|
120 | psh->prev = g_sh_tail;
|
---|
121 | if (g_sh_tail)
|
---|
122 | g_sh_tail->next = psh;
|
---|
123 | else
|
---|
124 | g_sh_tail = g_sh_head = psh;
|
---|
125 | g_sh_tail = psh;
|
---|
126 |
|
---|
127 | g_num_shells++;
|
---|
128 |
|
---|
129 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
130 | }
|
---|
131 |
|
---|
132 | #if 0
|
---|
133 | /**
|
---|
134 | * Unlink the shell instance.
|
---|
135 | *
|
---|
136 | * @param psh The shell.
|
---|
137 | */
|
---|
138 | static void sh_int_unlink(shinstance *psh)
|
---|
139 | {
|
---|
140 | shmtxtmp tmp;
|
---|
141 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
142 |
|
---|
143 | g_num_shells--;
|
---|
144 |
|
---|
145 | if (g_sh_tail == psh)
|
---|
146 | g_sh_tail = psh->prev;
|
---|
147 | else
|
---|
148 | psh->next->prev = psh->prev;
|
---|
149 |
|
---|
150 | if (g_sh_head == psh)
|
---|
151 | g_sh_head = psh->next;
|
---|
152 | else
|
---|
153 | psh->prev->next = psh->next;
|
---|
154 |
|
---|
155 | if (g_sh_root == psh)
|
---|
156 | g_sh_root = 0;
|
---|
157 |
|
---|
158 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
159 | }
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Destroys the shell instance.
|
---|
164 | *
|
---|
165 | * This will work on partially initialized instances (because I'm lazy).
|
---|
166 | *
|
---|
167 | * @param psh The shell instance to be destroyed.
|
---|
168 | */
|
---|
169 | static void sh_destroy(shinstance *psh)
|
---|
170 | {
|
---|
171 | memset(psh, 0, sizeof(*psh));
|
---|
172 | sh_free(NULL, psh);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Clones an environment vector.
|
---|
177 | *
|
---|
178 | * @returns 0 on success, -1 and errno on failure.
|
---|
179 | * @param dstp Where to store the clone.
|
---|
180 | * @param src The vector to be cloned.
|
---|
181 | */
|
---|
182 | static int sh_env_clone(char ***dstp, char **src)
|
---|
183 | {
|
---|
184 | char **dst;
|
---|
185 | size_t items;
|
---|
186 |
|
---|
187 | /* count first */
|
---|
188 | items = 0;
|
---|
189 | while (src[items])
|
---|
190 | items++;
|
---|
191 |
|
---|
192 | /* alloc clone array. */
|
---|
193 | *dstp = dst = sh_malloc(NULL, sizeof(*dst) * items + 1);
|
---|
194 | if (!dst)
|
---|
195 | return -1;
|
---|
196 |
|
---|
197 | /* copy the items */
|
---|
198 | dst[items] = NULL;
|
---|
199 | while (items-- > 0)
|
---|
200 | {
|
---|
201 | dst[items] = sh_strdup(NULL, src[items]);
|
---|
202 | if (!dst[items])
|
---|
203 | {
|
---|
204 | /* allocation error, clean up. */
|
---|
205 | while (dst[++items])
|
---|
206 | sh_free(NULL, dst[items]);
|
---|
207 | sh_free(NULL, dst);
|
---|
208 | errno = ENOMEM;
|
---|
209 | return -1;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | return 0;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Creates a root shell instance.
|
---|
218 | *
|
---|
219 | * @param inherit The shell to inherit from. If NULL inherit from environment and such.
|
---|
220 | * @param argc The argument count.
|
---|
221 | * @param argv The argument vector.
|
---|
222 | * @param envp The environment vector.
|
---|
223 | *
|
---|
224 | * @returns pointer to root shell on success, NULL on failure.
|
---|
225 | */
|
---|
226 | shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv, char **envp)
|
---|
227 | {
|
---|
228 | shinstance *psh;
|
---|
229 | int i;
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * The allocations.
|
---|
233 | */
|
---|
234 | psh = sh_calloc(NULL, sizeof(*psh), 1);
|
---|
235 | if (psh)
|
---|
236 | {
|
---|
237 | /* Init it enought for sh_destroy() to not get upset */
|
---|
238 | /* ... */
|
---|
239 |
|
---|
240 | /* Call the basic initializers. */
|
---|
241 | if ( !sh_env_clone(&psh->shenviron, envp)
|
---|
242 | && !shfile_init(&psh->fdtab, inherit ? &inherit->fdtab : NULL))
|
---|
243 | {
|
---|
244 | /* the special stuff. */
|
---|
245 | #ifdef _MSC_VER
|
---|
246 | psh->pid = _getpid();
|
---|
247 | #else
|
---|
248 | psh->pid = getpid();
|
---|
249 | #endif
|
---|
250 | /*sh_sigemptyset(&psh->sigrestartset);*/
|
---|
251 | for (i = 0; i < NSIG; i++)
|
---|
252 | psh->sigactions[i].sh_handler = SH_SIG_UNK;
|
---|
253 | if (inherit)
|
---|
254 | psh->sigmask = psh->sigmask;
|
---|
255 | else
|
---|
256 | {
|
---|
257 | #if defined(SH_PURE_STUB_MODE) || defined(_MSC_VER)
|
---|
258 | sh_sigemptyset(&psh->sigmask);
|
---|
259 | #else
|
---|
260 | sigprocmask(SIG_SETMASK, NULL, &psh->sigmask);
|
---|
261 | #endif
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* memalloc.c */
|
---|
265 | psh->stacknleft = MINSIZE;
|
---|
266 | psh->herefd = -1;
|
---|
267 | psh->stackp = &psh->stackbase;
|
---|
268 | psh->stacknxt = psh->stackbase.space;
|
---|
269 |
|
---|
270 | /* input.c */
|
---|
271 | psh->plinno = 1;
|
---|
272 | psh->init_editline = 0;
|
---|
273 | psh->parsefile = &psh->basepf;
|
---|
274 |
|
---|
275 | /* output.c */
|
---|
276 | psh->output.bufsize = OUTBUFSIZ;
|
---|
277 | psh->output.fd = 1;
|
---|
278 | psh->output.psh = psh;
|
---|
279 | psh->errout.bufsize = 100;
|
---|
280 | psh->errout.fd = 2;
|
---|
281 | psh->errout.psh = psh;
|
---|
282 | psh->memout.fd = MEM_OUT;
|
---|
283 | psh->memout.psh = psh;
|
---|
284 | psh->out1 = &psh->output;
|
---|
285 | psh->out2 = &psh->errout;
|
---|
286 |
|
---|
287 | /* jobs.c */
|
---|
288 | psh->backgndpid = -1;
|
---|
289 | #if JOBS
|
---|
290 | psh->curjob = -1;
|
---|
291 | #else
|
---|
292 | # error asdf
|
---|
293 | #endif
|
---|
294 | psh->ttyfd = -1;
|
---|
295 |
|
---|
296 | /* link it. */
|
---|
297 | sh_int_link(psh);
|
---|
298 | return psh;
|
---|
299 | }
|
---|
300 |
|
---|
301 | sh_destroy(psh);
|
---|
302 | }
|
---|
303 | return NULL;
|
---|
304 | }
|
---|
305 |
|
---|
306 | char *sh_getenv(shinstance *psh, const char *var)
|
---|
307 | {
|
---|
308 | size_t len;
|
---|
309 | int i = 0;
|
---|
310 |
|
---|
311 | if (!var)
|
---|
312 | return NULL;
|
---|
313 |
|
---|
314 | len = strlen(var);
|
---|
315 | i = 0;
|
---|
316 | while (psh->shenviron[i])
|
---|
317 | {
|
---|
318 | const char *item = psh->shenviron[i];
|
---|
319 | if ( !strncmp(item, var, len)
|
---|
320 | && item[len] == '=')
|
---|
321 | return (char *)item + len + 1;
|
---|
322 | }
|
---|
323 |
|
---|
324 | return NULL;
|
---|
325 | }
|
---|
326 |
|
---|
327 | char **sh_environ(shinstance *psh)
|
---|
328 | {
|
---|
329 | return psh->shenviron;
|
---|
330 | }
|
---|
331 |
|
---|
332 | const char *sh_gethomedir(shinstance *psh, const char *user)
|
---|
333 | {
|
---|
334 | const char *ret = NULL;
|
---|
335 |
|
---|
336 | #ifdef _MSC_VER
|
---|
337 | ret = sh_getenv(psh, "HOME");
|
---|
338 | if (!ret)
|
---|
339 | ret = sh_getenv(psh, "USERPROFILE");
|
---|
340 | #else
|
---|
341 | struct passwd *pwd = getpwnam(user); /** @todo use getpwdnam_r */
|
---|
342 | (void)psh;
|
---|
343 | ret = pwd ? pwd->pw_dir : NULL;
|
---|
344 | #endif
|
---|
345 |
|
---|
346 | return ret;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Lazy initialization of a signal state, globally.
|
---|
351 | *
|
---|
352 | * @param psh The shell doing the lazy work.
|
---|
353 | * @param signo The signal (valid).
|
---|
354 | */
|
---|
355 | static void sh_int_lazy_init_sigaction(shinstance *psh, int signo)
|
---|
356 | {
|
---|
357 | if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
|
---|
358 | {
|
---|
359 | shmtxtmp tmp;
|
---|
360 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
361 |
|
---|
362 | if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
|
---|
363 | {
|
---|
364 | shsigaction_t shold;
|
---|
365 | shinstance *cur;
|
---|
366 | #ifndef _MSC_VER
|
---|
367 | struct sigaction old;
|
---|
368 | if (!sigaction(signo, NULL, &old))
|
---|
369 | {
|
---|
370 | /* convert */
|
---|
371 | shold.sh_flags = old.sa_flags;
|
---|
372 | shold.sh_mask = old.sa_mask;
|
---|
373 | if (old.sa_handler == SIG_DFL)
|
---|
374 | shold.sh_handler = SH_SIG_DFL;
|
---|
375 | else
|
---|
376 | {
|
---|
377 | assert(old.sa_handler == SIG_IGN);
|
---|
378 | shold.sh_handler = SH_SIG_IGN;
|
---|
379 | }
|
---|
380 | }
|
---|
381 | else
|
---|
382 | #endif
|
---|
383 | {
|
---|
384 | /* fake */
|
---|
385 | #ifndef _MSC_VER
|
---|
386 | assert(0);
|
---|
387 | old.sa_handler = SIG_DFL;
|
---|
388 | old.sa_flags = 0;
|
---|
389 | sigemptyset(&shold.sh_mask);
|
---|
390 | sigaddset(&shold.sh_mask, signo);
|
---|
391 | #endif
|
---|
392 | shold.sh_flags = 0;
|
---|
393 | sh_sigemptyset(&shold.sh_mask);
|
---|
394 | sh_sigaddset(&shold.sh_mask, signo);
|
---|
395 | shold.sh_handler = SH_SIG_DFL;
|
---|
396 | }
|
---|
397 |
|
---|
398 | /* update globals */
|
---|
399 | #ifndef _MSC_VER
|
---|
400 | g_sig_state[signo].sa = old;
|
---|
401 | #else
|
---|
402 | g_sig_state[signo].sa.sa_handler = SIG_DFL;
|
---|
403 | g_sig_state[signo].sa.sa_flags = 0;
|
---|
404 | g_sig_state[signo].sa.sa_mask = shold.sh_mask;
|
---|
405 | #endif
|
---|
406 | TRACE2((psh, "sh_int_lazy_init_sigaction: signo=%d:%s sa_handler=%p sa_flags=%#x\n",
|
---|
407 | signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
|
---|
408 |
|
---|
409 | /* update all shells */
|
---|
410 | for (cur = g_sh_head; cur; cur = cur->next)
|
---|
411 | {
|
---|
412 | assert(cur->sigactions[signo].sh_handler == SH_SIG_UNK);
|
---|
413 | cur->sigactions[signo] = shold;
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Perform the default signal action on the shell.
|
---|
423 | *
|
---|
424 | * @param psh The shell instance.
|
---|
425 | * @param signo The signal.
|
---|
426 | */
|
---|
427 | static void sh_sig_do_default(shinstance *psh, int signo)
|
---|
428 | {
|
---|
429 | /** @todo */
|
---|
430 | }
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Deliver a signal to a shell.
|
---|
434 | *
|
---|
435 | * @param psh The shell instance.
|
---|
436 | * @param pshDst The shell instance to signal.
|
---|
437 | * @param signo The signal.
|
---|
438 | * @param locked Whether we're owning the lock or not.
|
---|
439 | */
|
---|
440 | static void sh_sig_do_signal(shinstance *psh, shinstance *pshDst, int signo, int locked)
|
---|
441 | {
|
---|
442 | shsig_t pfn = pshDst->sigactions[signo].sh_handler;
|
---|
443 | if (pfn == SH_SIG_UNK)
|
---|
444 | {
|
---|
445 | sh_int_lazy_init_sigaction(pshDst, signo);
|
---|
446 | pfn = pshDst->sigactions[signo].sh_handler;
|
---|
447 | }
|
---|
448 |
|
---|
449 | if (pfn == SH_SIG_DFL)
|
---|
450 | sh_sig_do_default(pshDst, signo);
|
---|
451 | else if (pfn == SH_SIG_IGN)
|
---|
452 | /* ignore it */;
|
---|
453 | else
|
---|
454 | {
|
---|
455 | assert(pfn != SH_SIG_ERR);
|
---|
456 | pfn(pshDst, signo);
|
---|
457 | }
|
---|
458 | (void)locked;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Handler for external signals.
|
---|
463 | *
|
---|
464 | * @param signo The signal.
|
---|
465 | */
|
---|
466 | static void sh_sig_common_handler(int signo)
|
---|
467 | {
|
---|
468 | shinstance *psh;
|
---|
469 |
|
---|
470 | fprintf(stderr, "sh_sig_common_handler: signo=%d:%s\n", signo, sys_signame[signo]);
|
---|
471 |
|
---|
472 | /*
|
---|
473 | * No need to take locks if there is only one shell.
|
---|
474 | * Since this will be the initial case, just avoid the deadlock
|
---|
475 | * hell for a litte while...
|
---|
476 | */
|
---|
477 | if (g_num_shells <= 1)
|
---|
478 | {
|
---|
479 | psh = g_sh_head;
|
---|
480 | if (psh)
|
---|
481 | sh_sig_do_signal(NULL, psh, signo, 0 /* no lock */);
|
---|
482 | }
|
---|
483 | else
|
---|
484 | {
|
---|
485 | shmtxtmp tmp;
|
---|
486 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
487 |
|
---|
488 | /** @todo signal focus chain or something? Atm there will only be one shell,
|
---|
489 | * so it's not really important until we go threaded for real... */
|
---|
490 | psh = g_sh_tail;
|
---|
491 | while (psh != NULL)
|
---|
492 | {
|
---|
493 | sh_sig_do_signal(NULL, psh, signo, 1 /* locked */);
|
---|
494 | psh = psh->prev;
|
---|
495 | }
|
---|
496 |
|
---|
497 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | int sh_sigaction(shinstance *psh, int signo, const struct shsigaction *newp, struct shsigaction *oldp)
|
---|
502 | {
|
---|
503 | if (newp)
|
---|
504 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=%p:{.sh_handler=%p, .sh_flags=%#x} oldp=%p\n",
|
---|
505 | signo, sys_signame[signo], newp, newp->sh_handler, newp->sh_flags, oldp));
|
---|
506 | else
|
---|
507 | TRACE2((psh, "sh_sigaction: signo=%d:%s newp=NULL oldp=%p\n", signo, sys_signame[signo], oldp));
|
---|
508 |
|
---|
509 | /*
|
---|
510 | * Input validation.
|
---|
511 | */
|
---|
512 | if (signo >= NSIG || signo <= 0)
|
---|
513 | {
|
---|
514 | errno = EINVAL;
|
---|
515 | return -1;
|
---|
516 | }
|
---|
517 |
|
---|
518 | #ifdef SH_PURE_STUB_MODE
|
---|
519 | return -1;
|
---|
520 | #else
|
---|
521 |
|
---|
522 | /*
|
---|
523 | * Make sure our data is correct.
|
---|
524 | */
|
---|
525 | sh_int_lazy_init_sigaction(psh, signo);
|
---|
526 |
|
---|
527 | /*
|
---|
528 | * Get the old one if requested.
|
---|
529 | */
|
---|
530 | if (oldp)
|
---|
531 | *oldp = psh->sigactions[signo];
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * Set the new one if it has changed.
|
---|
535 | *
|
---|
536 | * This will be attempted coordinated with the other signal handlers so
|
---|
537 | * that we can arrive at a common denominator.
|
---|
538 | */
|
---|
539 | if ( newp
|
---|
540 | && memcmp(&psh->sigactions[signo], newp, sizeof(*newp)))
|
---|
541 | {
|
---|
542 | shmtxtmp tmp;
|
---|
543 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
544 |
|
---|
545 | /* Undo the accounting for the current entry. */
|
---|
546 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
---|
547 | g_sig_state[signo].num_ignore--;
|
---|
548 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
---|
549 | g_sig_state[signo].num_specific--;
|
---|
550 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
---|
551 | g_sig_state[signo].num_restart--;
|
---|
552 |
|
---|
553 | /* Set the new entry. */
|
---|
554 | psh->sigactions[signo] = *newp;
|
---|
555 |
|
---|
556 | /* Add the bits for the new action entry. */
|
---|
557 | if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
|
---|
558 | g_sig_state[signo].num_ignore++;
|
---|
559 | else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
|
---|
560 | g_sig_state[signo].num_specific++;
|
---|
561 | if (psh->sigactions[signo].sh_flags & SA_RESTART)
|
---|
562 | g_sig_state[signo].num_restart++;
|
---|
563 |
|
---|
564 | /*
|
---|
565 | * Calc new common action.
|
---|
566 | *
|
---|
567 | * This is quit a bit ASSUMPTIVE about the limited use. We will not
|
---|
568 | * bother synching the mask, and we pretend to care about SA_RESTART.
|
---|
569 | * The only thing we really actually care about is the sh_handler.
|
---|
570 | *
|
---|
571 | * On second though, it's possible we should just tie this to the root
|
---|
572 | * shell since it only really applies to external signal ...
|
---|
573 | */
|
---|
574 | if ( g_sig_state[signo].num_specific
|
---|
575 | || g_sig_state[signo].num_ignore != g_num_shells)
|
---|
576 | g_sig_state[signo].sa.sa_handler = sh_sig_common_handler;
|
---|
577 | else if (g_sig_state[signo].num_ignore)
|
---|
578 | g_sig_state[signo].sa.sa_handler = SIG_IGN;
|
---|
579 | else
|
---|
580 | g_sig_state[signo].sa.sa_handler = SIG_DFL;
|
---|
581 | g_sig_state[signo].sa.sa_flags = psh->sigactions[signo].sh_flags & SA_RESTART;
|
---|
582 |
|
---|
583 | TRACE2((psh, "sh_sigaction: setting signo=%d:%s to {.sa_handler=%p, .sa_flags=%#x}\n",
|
---|
584 | signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
|
---|
585 | # ifdef _MSC_VER
|
---|
586 | if (signal(signo, g_sig_state[signo].sa.sa_handler) == SIG_ERR)
|
---|
587 | # else
|
---|
588 | if (sigaction(signo, &g_sig_state[signo].sa, NULL))
|
---|
589 | # endif
|
---|
590 | assert(0);
|
---|
591 |
|
---|
592 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
593 | }
|
---|
594 |
|
---|
595 | return 0;
|
---|
596 | #endif
|
---|
597 | }
|
---|
598 |
|
---|
599 | shsig_t sh_signal(shinstance *psh, int signo, shsig_t handler)
|
---|
600 | {
|
---|
601 | shsigaction_t sa;
|
---|
602 | shsig_t ret;
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * Implementation using sh_sigaction.
|
---|
606 | */
|
---|
607 | if (sh_sigaction(psh, signo, NULL, &sa))
|
---|
608 | return SH_SIG_ERR;
|
---|
609 |
|
---|
610 | ret = sa.sh_handler;
|
---|
611 | sa.sh_flags &= SA_RESTART;
|
---|
612 | sa.sh_handler = handler;
|
---|
613 | sh_sigemptyset(&sa.sh_mask);
|
---|
614 | sh_sigaddset(&sa.sh_mask, signo); /* ?? */
|
---|
615 | if (sh_sigaction(psh, signo, &sa, NULL))
|
---|
616 | return SH_SIG_ERR;
|
---|
617 |
|
---|
618 | return ret;
|
---|
619 | }
|
---|
620 |
|
---|
621 | int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
|
---|
622 | {
|
---|
623 | shsigaction_t sa;
|
---|
624 | int oldflags = 0;
|
---|
625 |
|
---|
626 | /*
|
---|
627 | * Implementation using sh_sigaction.
|
---|
628 | */
|
---|
629 | if (sh_sigaction(psh, signo, NULL, &sa))
|
---|
630 | return -1;
|
---|
631 | oldflags = sa.sh_flags;
|
---|
632 | if (interrupt)
|
---|
633 | sa.sh_flags &= ~SA_RESTART;
|
---|
634 | else
|
---|
635 | sa.sh_flags |= ~SA_RESTART;
|
---|
636 | if (!((oldflags ^ sa.sh_flags) & SA_RESTART))
|
---|
637 | return 0; /* unchanged. */
|
---|
638 |
|
---|
639 | return sh_sigaction(psh, signo, &sa, NULL);
|
---|
640 | }
|
---|
641 |
|
---|
642 | void sh_sigemptyset(shsigset_t *setp)
|
---|
643 | {
|
---|
644 | memset(setp, 0, sizeof(*setp));
|
---|
645 | }
|
---|
646 |
|
---|
647 | void sh_sigfillset(shsigset_t *setp)
|
---|
648 | {
|
---|
649 | memset(setp, 0xff, sizeof(*setp));
|
---|
650 | }
|
---|
651 |
|
---|
652 | void sh_sigaddset(shsigset_t *setp, int signo)
|
---|
653 | {
|
---|
654 | #ifdef _MSC_VER
|
---|
655 | *setp |= 1U << signo;
|
---|
656 | #else
|
---|
657 | sigaddset(setp, signo);
|
---|
658 | #endif
|
---|
659 | }
|
---|
660 |
|
---|
661 | void sh_sigdelset(shsigset_t *setp, int signo)
|
---|
662 | {
|
---|
663 | #ifdef _MSC_VER
|
---|
664 | *setp &= ~(1U << signo);
|
---|
665 | #else
|
---|
666 | sigdelset(setp, signo);
|
---|
667 | #endif
|
---|
668 | }
|
---|
669 |
|
---|
670 | int sh_sigismember(shsigset_t const *setp, int signo)
|
---|
671 | {
|
---|
672 | #ifdef _MSC_VER
|
---|
673 | return !!(*setp & (1U << signo));
|
---|
674 | #else
|
---|
675 | return !!sigismember(setp, signo);
|
---|
676 | #endif
|
---|
677 | }
|
---|
678 |
|
---|
679 | int sh_sigprocmask(shinstance *psh, int operation, shsigset_t const *newp, shsigset_t *oldp)
|
---|
680 | {
|
---|
681 | int rc;
|
---|
682 |
|
---|
683 | if ( operation != SIG_BLOCK
|
---|
684 | && operation != SIG_UNBLOCK
|
---|
685 | && operation != SIG_SETMASK)
|
---|
686 | {
|
---|
687 | errno = EINVAL;
|
---|
688 | return -1;
|
---|
689 | }
|
---|
690 |
|
---|
691 | #if (defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)) && !defined(_MSC_VER)
|
---|
692 | rc = sigprocmask(operation, newp, oldp);
|
---|
693 | if (!rc && newp)
|
---|
694 | psh->sigmask = *newp;
|
---|
695 |
|
---|
696 | #else
|
---|
697 | if (oldp)
|
---|
698 | *oldp = psh->sigmask;
|
---|
699 | if (newp)
|
---|
700 | {
|
---|
701 | /* calc the new mask */
|
---|
702 | shsigset_t mask = psh->sigmask;
|
---|
703 | switch (operation)
|
---|
704 | {
|
---|
705 | case SIG_BLOCK:
|
---|
706 | for (rc = 0; rc < NSIG; rc++)
|
---|
707 | if (sh_sigismember(newp, rc))
|
---|
708 | sh_sigaddset(&mask, rc);
|
---|
709 | break;
|
---|
710 | case SIG_UNBLOCK:
|
---|
711 | for (rc = 0; rc < NSIG; rc++)
|
---|
712 | if (sh_sigismember(newp, rc))
|
---|
713 | sh_sigdelset(&mask, rc);
|
---|
714 | break;
|
---|
715 | case SIG_SETMASK:
|
---|
716 | mask = *newp;
|
---|
717 | break;
|
---|
718 | }
|
---|
719 |
|
---|
720 | # if defined(SH_STUB_MODE) || defined(_MSC_VER)
|
---|
721 | rc = 0;
|
---|
722 | # else
|
---|
723 | rc = sigprocmask(operation, &mask, NULL);
|
---|
724 | if (!rc)
|
---|
725 | # endif
|
---|
726 | psh->sigmask = mask;
|
---|
727 | }
|
---|
728 |
|
---|
729 | #endif
|
---|
730 | return rc;
|
---|
731 | }
|
---|
732 |
|
---|
733 | void sh_abort(shinstance *psh)
|
---|
734 | {
|
---|
735 | shsigset_t set;
|
---|
736 | TRACE2((psh, "sh_abort\n"));
|
---|
737 |
|
---|
738 | /* block other async signals */
|
---|
739 | sh_sigfillset(&set);
|
---|
740 | sh_sigdelset(&set, SIGABRT);
|
---|
741 | sh_sigprocmask(psh, SIG_SETMASK, &set, NULL);
|
---|
742 |
|
---|
743 | sh_sig_do_signal(psh, psh, SIGABRT, 0 /* no lock */);
|
---|
744 |
|
---|
745 | /** @todo die in a nicer manner. */
|
---|
746 | *(char *)1 = 3;
|
---|
747 |
|
---|
748 | TRACE2((psh, "sh_abort returns!\n"));
|
---|
749 | (void)psh;
|
---|
750 | abort();
|
---|
751 | }
|
---|
752 |
|
---|
753 | void sh_raise_sigint(shinstance *psh)
|
---|
754 | {
|
---|
755 | TRACE2((psh, "sh_raise(SIGINT)\n"));
|
---|
756 |
|
---|
757 | sh_sig_do_signal(psh, psh, SIGINT, 0 /* no lock */);
|
---|
758 |
|
---|
759 | TRACE2((psh, "sh_raise(SIGINT) returns\n"));
|
---|
760 | }
|
---|
761 |
|
---|
762 | int sh_kill(shinstance *psh, pid_t pid, int signo)
|
---|
763 | {
|
---|
764 | shinstance *pshDst;
|
---|
765 | shmtxtmp tmp;
|
---|
766 | int rc;
|
---|
767 |
|
---|
768 | /*
|
---|
769 | * Self or any of the subshells?
|
---|
770 | */
|
---|
771 | shmtx_enter(&g_sh_mtx, &tmp);
|
---|
772 |
|
---|
773 | pshDst = g_sh_tail;
|
---|
774 | while (pshDst != NULL)
|
---|
775 | {
|
---|
776 | if (pshDst->pid == pid)
|
---|
777 | {
|
---|
778 | TRACE2((psh, "sh_kill(%d, %d): pshDst=%p\n", pid, signo, pshDst));
|
---|
779 | sh_sig_do_signal(psh, pshDst, signo, 1 /* locked */);
|
---|
780 |
|
---|
781 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
782 | return 0;
|
---|
783 | }
|
---|
784 | pshDst = pshDst->prev;
|
---|
785 | }
|
---|
786 |
|
---|
787 | shmtx_leave(&g_sh_mtx, &tmp);
|
---|
788 |
|
---|
789 | /*
|
---|
790 | * Some other process, call kill where possible
|
---|
791 | */
|
---|
792 | #ifdef SH_PURE_STUB_MODE
|
---|
793 | rc = -1;
|
---|
794 |
|
---|
795 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
796 | # ifdef _MSC_VER
|
---|
797 | errno = ENOSYS;
|
---|
798 | rc = -1;
|
---|
799 | # else
|
---|
800 | fprintf(stderr, "kill(%d, %d)\n", pid, signo);
|
---|
801 | rc = kill(pid, signo);
|
---|
802 | # endif
|
---|
803 |
|
---|
804 | #else
|
---|
805 | #endif
|
---|
806 |
|
---|
807 | TRACE2((psh, "sh_kill(%d, %d) -> %d [%d]\n", pid, signo, rc, errno));
|
---|
808 | return rc;
|
---|
809 | }
|
---|
810 |
|
---|
811 | int sh_killpg(shinstance *psh, pid_t pgid, int signo)
|
---|
812 | {
|
---|
813 | int rc;
|
---|
814 |
|
---|
815 | #ifdef SH_PURE_STUB_MODE
|
---|
816 | rc = -1;
|
---|
817 |
|
---|
818 | #elif defined(SH_STUB_MODE)
|
---|
819 | # ifdef _MSC_VER
|
---|
820 | errno = ENOSYS;
|
---|
821 | rc = -1;
|
---|
822 | # else
|
---|
823 | //fprintf(stderr, "killpg(%d, %d)\n", pgid, signo);
|
---|
824 | rc = killpg(pgid, signo);
|
---|
825 | # endif
|
---|
826 |
|
---|
827 | #else
|
---|
828 | #endif
|
---|
829 |
|
---|
830 | TRACE2((psh, "sh_killpg(%d, %d) -> %d [%d]\n", pgid, signo, rc, errno));
|
---|
831 | (void)psh;
|
---|
832 | return rc;
|
---|
833 | }
|
---|
834 |
|
---|
835 | clock_t sh_times(shinstance *psh, shtms *tmsp)
|
---|
836 | {
|
---|
837 | #ifdef SH_PURE_STUB_MODE
|
---|
838 | return 0;
|
---|
839 |
|
---|
840 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
841 | (void)psh;
|
---|
842 | # ifdef _MSC_VER
|
---|
843 | errno = ENOSYS;
|
---|
844 | return (clock_t)-1;
|
---|
845 | # else
|
---|
846 | return times(tmsp);
|
---|
847 | # endif
|
---|
848 |
|
---|
849 | #else
|
---|
850 | #endif
|
---|
851 | }
|
---|
852 |
|
---|
853 | int sh_sysconf_clk_tck(void)
|
---|
854 | {
|
---|
855 | #ifdef SH_PURE_STUB_MODE
|
---|
856 | return 1;
|
---|
857 | #else
|
---|
858 | # ifdef _MSC_VER
|
---|
859 | return CLK_TCK;
|
---|
860 | # else
|
---|
861 | return sysconf(_SC_CLK_TCK);
|
---|
862 | # endif
|
---|
863 | #endif
|
---|
864 | }
|
---|
865 |
|
---|
866 | pid_t sh_fork(shinstance *psh)
|
---|
867 | {
|
---|
868 | pid_t pid;
|
---|
869 | TRACE2((psh, "sh_fork\n"));
|
---|
870 |
|
---|
871 | #ifdef SH_PURE_STUB_MODE
|
---|
872 | pid = -1;
|
---|
873 |
|
---|
874 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
875 | # ifdef _MSC_VER
|
---|
876 | # ifdef SH_FORKED_MODE
|
---|
877 | /** @todo */
|
---|
878 | *(char *)1 = 0x1;
|
---|
879 | # else
|
---|
880 | pid = -1;
|
---|
881 | errno = ENOSYS;
|
---|
882 | # endif
|
---|
883 | # else
|
---|
884 | pid = fork();
|
---|
885 | # endif
|
---|
886 |
|
---|
887 | #else
|
---|
888 |
|
---|
889 | #endif
|
---|
890 |
|
---|
891 | TRACE2((psh, "sh_fork -> %d [%d]\n", pid, errno));
|
---|
892 | (void)psh;
|
---|
893 | return pid;
|
---|
894 | }
|
---|
895 |
|
---|
896 | pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
|
---|
897 | {
|
---|
898 | pid_t pidret;
|
---|
899 |
|
---|
900 | *statusp = 0;
|
---|
901 | #ifdef SH_PURE_STUB_MODE
|
---|
902 | pidret = -1;
|
---|
903 |
|
---|
904 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
905 | # ifdef _MSC_VER
|
---|
906 | pidret = -1;
|
---|
907 | errno = ENOSYS;
|
---|
908 | # else
|
---|
909 | pidret = waitpid(pid, statusp, flags);
|
---|
910 | # endif
|
---|
911 |
|
---|
912 | #else
|
---|
913 | #endif
|
---|
914 |
|
---|
915 | TRACE2((psh, "waitpid(%d, %p, %#x) -> %d [%d] *statusp=%#x (rc=%d)\n", pid, statusp, flags,
|
---|
916 | pidret, errno, *statusp, WEXITSTATUS(*statusp)));
|
---|
917 | (void)psh;
|
---|
918 | return pidret;
|
---|
919 | }
|
---|
920 |
|
---|
921 | void sh__exit(shinstance *psh, int rc)
|
---|
922 | {
|
---|
923 | TRACE2((psh, "sh__exit(%d)\n", rc));
|
---|
924 | (void)psh;
|
---|
925 |
|
---|
926 | #ifdef SH_PURE_STUB_MODE
|
---|
927 | return -1;
|
---|
928 |
|
---|
929 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
930 | _exit(rc);
|
---|
931 |
|
---|
932 | #else
|
---|
933 | #endif
|
---|
934 | }
|
---|
935 |
|
---|
936 | int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
|
---|
937 | {
|
---|
938 | #ifdef _MSC_VER
|
---|
939 | intptr_t rc;
|
---|
940 | #else
|
---|
941 | int rc;
|
---|
942 | #endif
|
---|
943 |
|
---|
944 | #ifdef DEBUG
|
---|
945 | /* log it all */
|
---|
946 | TRACE2((psh, "sh_execve(%p:{%s}, %p, %p}\n", exe, exe, argv, envp));
|
---|
947 | for (rc = 0; argv[rc]; rc++)
|
---|
948 | TRACE2((psh, " argv[%d]=%p:{%s}\n", rc, argv[rc], argv[rc]));
|
---|
949 | #endif
|
---|
950 |
|
---|
951 | if (!envp)
|
---|
952 | envp = sh_environ(psh);
|
---|
953 |
|
---|
954 | #ifdef SH_PURE_STUB_MODE
|
---|
955 | rc = -1;
|
---|
956 |
|
---|
957 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
958 | # ifdef _MSC_VER
|
---|
959 | rc = _spawnve(_P_WAIT, exe, (char **)argv, (char **)envp);
|
---|
960 | # else
|
---|
961 | rc = execve(exe, (char **)argv, (char **)envp);
|
---|
962 | # endif
|
---|
963 |
|
---|
964 | #else
|
---|
965 | #endif
|
---|
966 |
|
---|
967 | TRACE2((psh, "sh_execve -> %d [%d]\n", rc, errno));
|
---|
968 | (void)psh;
|
---|
969 | return (int)rc;
|
---|
970 | }
|
---|
971 |
|
---|
972 | uid_t sh_getuid(shinstance *psh)
|
---|
973 | {
|
---|
974 | #ifdef SH_PURE_STUB_MODE
|
---|
975 | uid_t uid = 0;
|
---|
976 |
|
---|
977 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
978 | # ifdef _MSC_VER
|
---|
979 | uid_t uid = 0;
|
---|
980 | # else
|
---|
981 | uid_t uid = getuid();
|
---|
982 | # endif
|
---|
983 |
|
---|
984 | #else
|
---|
985 | #endif
|
---|
986 |
|
---|
987 | TRACE2((psh, "sh_getuid() -> %d [%d]\n", uid, errno));
|
---|
988 | (void)psh;
|
---|
989 | return uid;
|
---|
990 | }
|
---|
991 |
|
---|
992 | uid_t sh_geteuid(shinstance *psh)
|
---|
993 | {
|
---|
994 | #ifdef SH_PURE_STUB_MODE
|
---|
995 | uid_t euid = 0;
|
---|
996 |
|
---|
997 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
998 | # ifdef _MSC_VER
|
---|
999 | uid_t euid = 0;
|
---|
1000 | # else
|
---|
1001 | uid_t euid = geteuid();
|
---|
1002 | # endif
|
---|
1003 |
|
---|
1004 | #else
|
---|
1005 | #endif
|
---|
1006 |
|
---|
1007 | TRACE2((psh, "sh_geteuid() -> %d [%d]\n", euid, errno));
|
---|
1008 | (void)psh;
|
---|
1009 | return euid;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | gid_t sh_getgid(shinstance *psh)
|
---|
1013 | {
|
---|
1014 | #ifdef SH_PURE_STUB_MODE
|
---|
1015 | gid_t gid = 0;
|
---|
1016 |
|
---|
1017 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1018 | # ifdef _MSC_VER
|
---|
1019 | gid_t gid = 0;
|
---|
1020 | # else
|
---|
1021 | gid_t gid = getgid();
|
---|
1022 | # endif
|
---|
1023 |
|
---|
1024 | #else
|
---|
1025 | #endif
|
---|
1026 |
|
---|
1027 | TRACE2((psh, "sh_getgid() -> %d [%d]\n", gid, errno));
|
---|
1028 | (void)psh;
|
---|
1029 | return gid;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | gid_t sh_getegid(shinstance *psh)
|
---|
1033 | {
|
---|
1034 | #ifdef SH_PURE_STUB_MODE
|
---|
1035 | gid_t egid = 0;
|
---|
1036 |
|
---|
1037 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1038 | # ifdef _MSC_VER
|
---|
1039 | gid_t egid = 0;
|
---|
1040 | # else
|
---|
1041 | gid_t egid = getegid();
|
---|
1042 | # endif
|
---|
1043 |
|
---|
1044 | #else
|
---|
1045 | #endif
|
---|
1046 |
|
---|
1047 | TRACE2((psh, "sh_getegid() -> %d [%d]\n", egid, errno));
|
---|
1048 | (void)psh;
|
---|
1049 | return egid;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | pid_t sh_getpid(shinstance *psh)
|
---|
1053 | {
|
---|
1054 | pid_t pid;
|
---|
1055 |
|
---|
1056 | #ifdef SH_PURE_STUB_MODE
|
---|
1057 | pid = 0;
|
---|
1058 |
|
---|
1059 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1060 | # ifdef _MSC_VER
|
---|
1061 | pid = _getpid();
|
---|
1062 | # else
|
---|
1063 | pid = getpid();
|
---|
1064 | # endif
|
---|
1065 | #else
|
---|
1066 | #endif
|
---|
1067 |
|
---|
1068 | (void)psh;
|
---|
1069 | return pid;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | pid_t sh_getpgrp(shinstance *psh)
|
---|
1073 | {
|
---|
1074 | #ifdef SH_PURE_STUB_MODE
|
---|
1075 | pid_t pgrp = 0;
|
---|
1076 |
|
---|
1077 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1078 | # ifdef _MSC_VER
|
---|
1079 | pid_t pgrp = _getpid();
|
---|
1080 | # else
|
---|
1081 | pid_t pgrp = getpgrp();
|
---|
1082 | # endif
|
---|
1083 |
|
---|
1084 | #else
|
---|
1085 | #endif
|
---|
1086 |
|
---|
1087 | TRACE2((psh, "sh_getpgrp() -> %d [%d]\n", pgrp, errno));
|
---|
1088 | (void)psh;
|
---|
1089 | return pgrp;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | pid_t sh_getpgid(shinstance *psh, pid_t pid)
|
---|
1093 | {
|
---|
1094 | #ifdef SH_PURE_STUB_MODE
|
---|
1095 | pid_t pgid = pid;
|
---|
1096 |
|
---|
1097 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1098 | # ifdef _MSC_VER
|
---|
1099 | pid_t pgid = pid;
|
---|
1100 | # else
|
---|
1101 | pid_t pgid = getpgid(pid);
|
---|
1102 | # endif
|
---|
1103 |
|
---|
1104 | #else
|
---|
1105 | #endif
|
---|
1106 |
|
---|
1107 | TRACE2((psh, "sh_getpgid(%d) -> %d [%d]\n", pid, pgid, errno));
|
---|
1108 | (void)psh;
|
---|
1109 | return pgid;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
|
---|
1113 | {
|
---|
1114 | #ifdef SH_PURE_STUB_MODE
|
---|
1115 | int rc = -1;
|
---|
1116 |
|
---|
1117 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1118 | # ifdef _MSC_VER
|
---|
1119 | int rc = -1;
|
---|
1120 | errno = ENOSYS;
|
---|
1121 | # else
|
---|
1122 | int rc = setpgid(pid, pgid);
|
---|
1123 | # endif
|
---|
1124 |
|
---|
1125 | #else
|
---|
1126 | #endif
|
---|
1127 |
|
---|
1128 | TRACE2((psh, "sh_setpgid(%d, %d) -> %d [%d]\n", pid, pgid, rc, errno));
|
---|
1129 | (void)psh;
|
---|
1130 | return rc;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | pid_t sh_tcgetpgrp(shinstance *psh, int fd)
|
---|
1134 | {
|
---|
1135 | pid_t pgrp;
|
---|
1136 |
|
---|
1137 | #ifdef SH_PURE_STUB_MODE
|
---|
1138 | pgrp = -1;
|
---|
1139 |
|
---|
1140 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1141 | # ifdef _MSC_VER
|
---|
1142 | pgrp = -1;
|
---|
1143 | errno = ENOSYS;
|
---|
1144 | # else
|
---|
1145 | pgrp = tcgetpgrp(fd);
|
---|
1146 | # endif
|
---|
1147 |
|
---|
1148 | #else
|
---|
1149 | #endif
|
---|
1150 |
|
---|
1151 | TRACE2((psh, "sh_tcgetpgrp(%d) -> %d [%d]\n", fd, pgrp, errno));
|
---|
1152 | (void)psh;
|
---|
1153 | return pgrp;
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
|
---|
1157 | {
|
---|
1158 | int rc;
|
---|
1159 | TRACE2((psh, "sh_tcsetpgrp(%d, %d)\n", fd, pgrp));
|
---|
1160 |
|
---|
1161 | #ifdef SH_PURE_STUB_MODE
|
---|
1162 | rc = -1;
|
---|
1163 |
|
---|
1164 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1165 | # ifdef _MSC_VER
|
---|
1166 | rc = -1;
|
---|
1167 | errno = ENOSYS;
|
---|
1168 | # else
|
---|
1169 | rc = tcsetpgrp(fd, pgrp);
|
---|
1170 | # endif
|
---|
1171 |
|
---|
1172 | #else
|
---|
1173 | #endif
|
---|
1174 |
|
---|
1175 | TRACE2((psh, "sh_tcsetpgrp(%d, %d) -> %d [%d]\n", fd, pgrp, rc, errno));
|
---|
1176 | (void)psh;
|
---|
1177 | return rc;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
|
---|
1181 | {
|
---|
1182 | #ifdef SH_PURE_STUB_MODE
|
---|
1183 | int rc = -1;
|
---|
1184 |
|
---|
1185 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1186 | # ifdef _MSC_VER
|
---|
1187 | int rc = -1;
|
---|
1188 | errno = ENOSYS;
|
---|
1189 | # else
|
---|
1190 | int rc = getrlimit(resid, limp);
|
---|
1191 | # endif
|
---|
1192 |
|
---|
1193 | #else
|
---|
1194 | /* returned the stored limit */
|
---|
1195 | #endif
|
---|
1196 |
|
---|
1197 | TRACE2((psh, "sh_getrlimit(%d, %p) -> %d [%d] {%ld,%ld}\n",
|
---|
1198 | resid, limp, rc, errno, (long)limp->rlim_cur, (long)limp->rlim_max));
|
---|
1199 | (void)psh;
|
---|
1200 | return rc;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
|
---|
1204 | {
|
---|
1205 | #ifdef SH_PURE_STUB_MODE
|
---|
1206 | int rc = -1;
|
---|
1207 |
|
---|
1208 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
1209 | # ifdef _MSC_VER
|
---|
1210 | int rc = -1;
|
---|
1211 | errno = ENOSYS;
|
---|
1212 | # else
|
---|
1213 | int rc = setrlimit(resid, limp);
|
---|
1214 | # endif
|
---|
1215 |
|
---|
1216 | #else
|
---|
1217 | /* if max(shell) < limp; then setrlimit; fi
|
---|
1218 | if success; then store limit for later retrival and maxing. */
|
---|
1219 |
|
---|
1220 | #endif
|
---|
1221 |
|
---|
1222 | TRACE2((psh, "sh_setrlimit(%d, %p:{%ld,%ld}) -> %d [%d]\n",
|
---|
1223 | resid, limp, (long)limp->rlim_cur, (long)limp->rlim_max, rc, errno));
|
---|
1224 | (void)psh;
|
---|
1225 | return rc;
|
---|
1226 | }
|
---|
1227 |
|
---|