source: trunk/src/kash/shinstance.c@ 2287

Last change on this file since 2287 was 2287, checked in by bird, 16 years ago

kash: windows build fixes.

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