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

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

kash: new execve implementation for windows. more file ops.

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