debian/copyright: GNU make is not GPL3, the rest remains as before. Updated copyright...
[kbuild-mirror.git] / src / kash / shinstance.c
blob2b26ab47bfd0f98673b3aff1cefd948fa0b25907
1 /* $Id$ */
2 /** @file
4 * The shell instance methods.
6 * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
9 * This file is part of kBuild.
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.
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.
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
27 /*******************************************************************************
28 * Header Files *
29 *******************************************************************************/
30 #include <string.h>
31 #include <stdlib.h>
32 #include <assert.h>
33 #ifndef _MSC_VER
34 # include <unistd.h>
35 # include <pwd.h>
36 extern char **environ;
37 #endif
38 #include "shinstance.h"
41 /*******************************************************************************
42 * Global Variables *
43 *******************************************************************************/
44 /** The mutex protecting the the globals and some shell instance members (sigs). */
45 static shmtx g_sh_mtx;
46 /** The root shell instance. */
47 static shinstance *g_sh_root;
48 /** The first shell instance. */
49 static shinstance *g_sh_head;
50 /** The last shell instance. */
51 static shinstance *g_sh_tail;
52 /** The number of shells. */
53 static int g_num_shells;
54 /** Per signal state for determining a common denominator.
55 * @remarks defaults and unmasked actions aren't counted. */
56 struct shsigstate
58 /** The current signal action. */
59 #ifndef _MSC_VER
60 struct sigaction sa;
61 #else
62 struct
64 void (*sa_handler)(int);
65 int sa_flags;
66 shsigset_t sa_mask;
67 } sa;
68 #endif
69 /** The number of restarts (siginterrupt / SA_RESTART). */
70 int num_restart;
71 /** The number of ignore handlers. */
72 int num_ignore;
73 /** The number of specific handlers. */
74 int num_specific;
75 /** The number of threads masking it. */
76 int num_masked;
77 } g_sig_state[NSIG];
81 typedef struct shmtxtmp { int i; } shmtxtmp;
83 int shmtx_init(shmtx *pmtx)
85 pmtx->b[0] = 0;
86 return 0;
89 void shmtx_delete(shmtx *pmtx)
91 pmtx->b[0] = 0;
94 void shmtx_enter(shmtx *pmtx, shmtxtmp *ptmp)
96 pmtx->b[0] = 0;
97 ptmp->i = 0;
100 void shmtx_leave(shmtx *pmtx, shmtxtmp *ptmp)
102 pmtx->b[0] = 0;
103 ptmp->i = 432;
108 * Links the shell instance.
110 * @param psh The shell.
112 static void sh_int_link(shinstance *psh)
114 shmtxtmp tmp;
115 shmtx_enter(&g_sh_mtx, &tmp);
117 if (psh->rootshell)
118 g_sh_root = psh;
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;
128 g_num_shells++;
130 shmtx_leave(&g_sh_mtx, &tmp);
135 * Unlink the shell instance.
137 * @param psh The shell.
139 static void sh_int_unlink(shinstance *psh)
141 shmtxtmp tmp;
142 shmtx_enter(&g_sh_mtx, &tmp);
144 g_num_shells--;
146 if (g_sh_tail == psh)
147 g_sh_tail = psh->prev;
148 else
149 psh->next->prev = psh->prev;
151 if (g_sh_head == psh)
152 g_sh_head = psh->next;
153 else
154 psh->prev->next = psh->next;
156 if (g_sh_root == psh)
157 g_sh_root = 0;
159 shmtx_leave(&g_sh_mtx, &tmp);
164 * Creates a root shell instance.
166 * @param inherit The shell to inherit from. If NULL inherit from environment and such.
167 * @param argc The argument count.
168 * @param argv The argument vector.
170 * @returns pointer to root shell on success, NULL on failure.
172 shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv)
174 shinstance *psh;
175 int i;
177 psh = calloc(sizeof(*psh), 1);
178 if (psh)
180 /* the special stuff. */
181 #ifdef _MSC_VER
182 psh->pid = _getpid();
183 #else
184 psh->pid = getpid();
185 #endif
186 /*sh_sigemptyset(&psh->sigrestartset);*/
187 for (i = 0; i < NSIG; i++)
188 psh->sigactions[i].sh_handler = SH_SIG_UNK;
190 /* memalloc.c */
191 psh->stacknleft = MINSIZE;
192 psh->herefd = -1;
193 psh->stackp = &psh->stackbase;
194 psh->stacknxt = psh->stackbase.space;
196 /* input.c */
197 psh->plinno = 1;
198 psh->init_editline = 0;
199 psh->parsefile = &psh->basepf;
201 /* output.c */
202 psh->output.bufsize = OUTBUFSIZ;
203 psh->output.fd = 1;
204 psh->output.psh = psh;
205 psh->errout.bufsize = 100;
206 psh->errout.fd = 2;
207 psh->errout.psh = psh;
208 psh->memout.fd = MEM_OUT;
209 psh->memout.psh = psh;
210 psh->out1 = &psh->output;
211 psh->out2 = &psh->errout;
213 /* jobs.c */
214 psh->backgndpid = -1;
215 #if JOBS
216 psh->curjob = -1;
217 #else
218 # error asdf
219 #endif
220 psh->ttyfd = -1;
222 /* link it. */
223 sh_int_link(psh);
226 return psh;
230 char *sh_getenv(shinstance *psh, const char *var)
232 #ifdef SH_PURE_STUB_MODE
233 return NULL;
234 #elif defined(SH_STUB_MODE)
235 (void)psh;
236 return getenv(var);
237 #else
238 #endif
241 char **sh_environ(shinstance *psh)
243 #ifdef SH_PURE_STUB_MODE
244 static char *s_null[2] = {0,0};
245 return &s_null[0];
246 #elif defined(SH_STUB_MODE)
247 (void)psh;
248 return environ;
249 #else
250 #endif
253 const char *sh_gethomedir(shinstance *psh, const char *user)
255 #ifdef SH_PURE_STUB_MODE
256 return NULL;
257 #elif defined(SH_STUB_MODE)
258 (void)psh;
259 # ifdef _MSC_VER
260 return NULL;
261 # else
262 struct passwd *pwd = getpwnam(user);
263 return pwd ? pwd->pw_dir : NULL;
264 # endif
265 #else
266 #endif
270 * Lazy initialization of a signal state, globally.
272 * @param psh The shell doing the lazy work.
273 * @param signo The signal (valid).
275 static void sh_int_lazy_init_sigaction(shinstance *psh, int signo)
277 if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
279 shmtxtmp tmp;
280 shmtx_enter(&g_sh_mtx, &tmp);
282 if (psh->sigactions[signo].sh_handler == SH_SIG_UNK)
284 shsigaction_t shold;
285 shinstance *cur;
286 #ifndef _MSC_VER
287 struct sigaction old;
288 if (!sigaction(signo, NULL, &old))
290 /* convert */
291 shold.sh_flags = old.sa_flags;
292 shold.sh_mask = old.sa_mask;
293 if (old.sa_handler == SIG_DFL)
294 shold.sh_handler = SH_SIG_DFL;
295 else
297 assert(old.sa_handler == SIG_IGN);
298 shold.sh_handler = SH_SIG_IGN;
301 else
302 #endif
304 /* fake */
305 #ifndef _MSC_VER
306 assert(0);
307 old.sa_handler = SIG_DFL;
308 old.sa_flags = 0;
309 sigemptyset(&shold.sh_mask);
310 sigaddset(&shold.sh_mask, signo);
311 #endif
312 shold.sh_flags = 0;
313 sh_sigemptyset(&shold.sh_mask);
314 sh_sigaddset(&shold.sh_mask, signo);
315 shold.sh_handler = SH_SIG_DFL;
318 /* update globals */
319 #ifndef _MSC_VER
320 g_sig_state[signo].sa = old;
321 #else
322 g_sig_state[signo].sa.sa_handler = SIG_DFL;
323 g_sig_state[signo].sa.sa_flags = 0;
324 g_sig_state[signo].sa.sa_mask = shold.sh_mask;
325 #endif
326 TRACE2((psh, "sh_int_lazy_init_sigaction: signo=%d:%s sa_handler=%p sa_flags=%#x\n",
327 signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
329 /* update all shells */
330 for (cur = g_sh_head; cur; cur = cur->next)
332 assert(cur->sigactions[signo].sh_handler == SH_SIG_UNK);
333 cur->sigactions[signo] = shold;
337 shmtx_leave(&g_sh_mtx, &tmp);
343 * Handler for external signals.
345 * @param signo The signal.
347 static void sh_sig_common_handler(int signo)
349 fprintf(stderr, "sh_sig_common_handler: signo=%d:%s\n", signo, sys_signame[signo]);
353 int sh_sigaction(shinstance *psh, int signo, const struct shsigaction *newp, struct shsigaction *oldp)
355 if (newp)
356 TRACE2((psh, "sh_sigaction: signo=%d:%s newp=%p:{.sh_handler=%p, .sh_flags=%#x} oldp=%p\n",
357 signo, sys_signame[signo], newp, newp->sh_handler, newp->sh_flags, oldp));
358 else
359 TRACE2((psh, "sh_sigaction: signo=%d:%s newp=NULL oldp=%p\n", signo, sys_signame[signo], oldp));
362 * Input validation.
364 if (signo >= NSIG || signo <= 0)
366 errno = EINVAL;
367 return -1;
370 #ifdef SH_PURE_STUB_MODE
371 return -1;
372 #else
375 * Make sure our data is correct.
377 sh_int_lazy_init_sigaction(psh, signo);
380 * Get the old one if requested.
382 if (oldp)
383 *oldp = psh->sigactions[signo];
386 * Set the new one if it has changed.
388 * This will be attempted coordinated with the other signal handlers so
389 * that we can arrive at a common denominator.
391 if ( newp
392 && memcmp(&psh->sigactions[signo], newp, sizeof(*newp)))
394 shmtxtmp tmp;
395 shmtx_enter(&g_sh_mtx, &tmp);
397 /* Undo the accounting for the current entry. */
398 if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
399 g_sig_state[signo].num_ignore--;
400 else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
401 g_sig_state[signo].num_specific--;
402 if (psh->sigactions[signo].sh_flags & SA_RESTART)
403 g_sig_state[signo].num_restart--;
405 /* Set the new entry. */
406 psh->sigactions[signo] = *newp;
408 /* Add the bits for the new action entry. */
409 if (psh->sigactions[signo].sh_handler == SH_SIG_IGN)
410 g_sig_state[signo].num_ignore++;
411 else if (psh->sigactions[signo].sh_handler != SH_SIG_DFL)
412 g_sig_state[signo].num_specific++;
413 if (psh->sigactions[signo].sh_flags & SA_RESTART)
414 g_sig_state[signo].num_restart++;
417 * Calc new common action.
419 * This is quit a bit ASSUMPTIVE about the limited use. We will not
420 * bother synching the mask, and we pretend to care about SA_RESTART.
421 * The only thing we really actually care about is the sh_handler.
423 * On second though, it's possible we should just tie this to the root
424 * shell since it only really applies to external signal ...
426 if ( g_sig_state[signo].num_specific
427 || g_sig_state[signo].num_ignore != g_num_shells)
428 g_sig_state[signo].sa.sa_handler = sh_sig_common_handler;
429 else if (g_sig_state[signo].num_ignore)
430 g_sig_state[signo].sa.sa_handler = SIG_IGN;
431 else
432 g_sig_state[signo].sa.sa_handler = SIG_DFL;
433 g_sig_state[signo].sa.sa_flags = psh->sigactions[signo].sh_flags & SA_RESTART;
435 TRACE2((psh, "sh_sigaction: setting signo=%d:%s to {.sa_handler=%p, .sa_flags=%#x}\n",
436 signo, sys_signame[signo], g_sig_state[signo].sa.sa_handler, g_sig_state[signo].sa.sa_flags));
437 # ifdef _MSC_VER
438 if (signal(signo, g_sig_state[signo].sa.sa_handler) == SIG_ERR)
439 # else
440 if (sigaction(signo, &g_sig_state[signo].sa, NULL))
441 # endif
442 assert(0);
444 shmtx_leave(&g_sh_mtx, &tmp);
447 return 0;
448 #endif
451 shsig_t sh_signal(shinstance *psh, int signo, shsig_t handler)
453 shsigaction_t sa;
454 shsig_t ret;
457 * Implementation using sh_sigaction.
459 if (sh_sigaction(psh, signo, NULL, &sa))
460 return SH_SIG_ERR;
462 ret = sa.sh_handler;
463 sa.sh_flags &= SA_RESTART;
464 sa.sh_handler = handler;
465 sh_sigemptyset(&sa.sh_mask);
466 sh_sigaddset(&sa.sh_mask, signo); /* ?? */
467 if (sh_sigaction(psh, signo, &sa, NULL))
468 return SH_SIG_ERR;
470 return ret;
473 int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
475 shsigaction_t sa;
476 int oldflags = 0;
479 * Implementation using sh_sigaction.
481 if (sh_sigaction(psh, signo, NULL, &sa))
482 return -1;
483 oldflags = sa.sh_flags;
484 if (interrupt)
485 sa.sh_flags &= ~SA_RESTART;
486 else
487 sa.sh_flags |= ~SA_RESTART;
488 if (!((oldflags ^ sa.sh_flags) & SA_RESTART))
489 return 0; /* unchanged. */
491 return sh_sigaction(psh, signo, &sa, NULL);
494 void sh_sigemptyset(shsigset_t *setp)
496 memset(setp, 0, sizeof(*setp));
499 void sh_sigaddset(shsigset_t *setp, int signo)
501 #ifdef _MSC_VER
502 *setp |= 1U << signo;
503 #else
504 sigaddset(setp, signo);
505 #endif
508 void sh_sigdelset(shsigset_t *setp, int signo)
510 #ifdef _MSC_VER
511 *setp &= ~(1U << signo);
512 #else
513 sigdelset(setp, signo);
514 #endif
517 int sh_sigismember(shsigset_t *setp, int signo)
519 #ifdef _MSC_VER
520 return !!(*setp & (1U << signo));
521 #else
522 return !!sigismember(setp, signo);
523 #endif
526 int sh_sigprocmask(shinstance *psh, int operation, shsigset_t const *newp, shsigset_t *oldp)
528 #ifdef SH_PURE_STUB_MODE
529 return -1;
530 #elif defined(SH_STUB_MODE)
531 (void)psh;
532 # ifdef _MSC_VER
533 return -1;
534 # else
535 return sigprocmask(operation, newp, oldp);
536 # endif
537 #else
538 #endif
541 void sh_abort(shinstance *psh)
543 TRACE2((psh, "sh_abort\n"));
545 #ifdef SH_PURE_STUB_MODE
546 #elif defined(SH_STUB_MODE)
547 abort();
548 #else
549 #endif
551 TRACE2((psh, "sh_abort returns!\n"));
552 (void)psh;
555 void sh_raise_sigint(shinstance *psh)
557 TRACE2((psh, "sh_raise(SIGINT)\n"));
559 #ifdef SH_PURE_STUB_MODE
560 #elif defined(SH_STUB_MODE)
561 (void)psh;
562 raise(SIGINT);
563 #else
564 #endif
566 TRACE2((psh, "sh_raise(SIGINT) returns\n"));
567 (void)psh;
570 int sh_kill(shinstance *psh, pid_t pid, int signo)
572 int rc;
574 #ifdef SH_PURE_STUB_MODE
575 rc = -1;
576 #elif defined(SH_STUB_MODE)
577 # ifdef _MSC_VER
578 rc = -1;
579 # else
580 //fprintf(stderr, "kill(%d, %d)\n", pid, signo);
581 rc = kill(pid, signo);
582 # endif
583 #else
584 #endif
586 TRACE2((psh, "sh_kill(%d, %d) -> %d [%d]\n", pid, signo, rc, errno));
587 (void)psh;
588 return rc;
591 int sh_killpg(shinstance *psh, pid_t pgid, int signo)
593 int rc;
595 #ifdef SH_PURE_STUB_MODE
596 rc = -1;
597 #elif defined(SH_STUB_MODE)
598 # ifdef _MSC_VER
599 rc = -1;
600 # else
601 //fprintf(stderr, "killpg(%d, %d)\n", pgid, signo);
602 rc = killpg(pgid, signo);
603 # endif
604 #else
605 #endif
607 TRACE2((psh, "sh_killpg(%d, %d) -> %d [%d]\n", pgid, signo, rc, errno));
608 (void)psh;
609 return rc;
612 clock_t sh_times(shinstance *psh, shtms *tmsp)
614 #ifdef SH_PURE_STUB_MODE
615 return 0;
616 #elif defined(SH_STUB_MODE)
617 (void)psh;
618 # ifdef _MSC_VER
619 return 0;
620 # else
621 return times(tmsp);
622 # endif
623 #else
624 #endif
627 int sh_sysconf_clk_tck(void)
629 #ifdef SH_PURE_STUB_MODE
630 return 1;
631 #else
632 # ifdef _MSC_VER
633 return CLK_TCK;
634 # else
635 return sysconf(_SC_CLK_TCK);
636 # endif
637 #endif
640 pid_t sh_fork(shinstance *psh)
642 pid_t pid;
643 TRACE2((psh, "sh_fork\n"));
645 #ifdef SH_PURE_STUB_MODE
646 pid = -1;
647 #elif defined(SH_STUB_MODE)
648 # ifdef _MSC_VER
649 pid = -1;
650 # else
651 pid = fork();
652 # endif
653 #else
654 #endif
656 TRACE2((psh, "sh_fork -> %d [%d]\n", pid, errno));
657 (void)psh;
658 return pid;
661 pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
663 pid_t pidret;
665 *statusp = 0;
666 #ifdef SH_PURE_STUB_MODE
667 pidret = -1;
668 #elif defined(SH_STUB_MODE)
669 # ifdef _MSC_VER
670 pidret = -1;
671 # else
672 pidret = waitpid(pid, statusp, flags);
673 # endif
674 #else
675 #endif
677 TRACE2((psh, "waitpid(%d, %p, %#x) -> %d [%d] *statusp=%#x (rc=%d)\n", pid, statusp, flags,
678 pidret, errno, *statusp, WEXITSTATUS(*statusp)));
679 (void)psh;
680 return pidret;
683 void sh__exit(shinstance *psh, int rc)
685 TRACE2((psh, "sh__exit(%d)\n", rc));
686 (void)psh;
688 #ifdef SH_PURE_STUB_MODE
689 return -1;
690 #elif defined(SH_STUB_MODE)
691 _exit(rc);
692 #else
693 #endif
696 int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
698 int rc;
700 #ifdef DEBUG
701 /* log it all */
702 TRACE2((psh, "sh_execve(%p:{%s}, %p, %p}\n", exe, exe, argv, envp));
703 for (rc = 0; argv[rc]; rc++)
704 TRACE2((psh, " argv[%d]=%p:{%s}\n", rc, argv[rc], argv[rc]));
705 #endif
707 #ifdef SH_PURE_STUB_MODE
708 rc = -1;
709 #elif defined(SH_STUB_MODE)
710 # ifdef _MSC_VER
711 rc = -1;
712 # else
713 rc = execve(exe, (char **)argv, (char **)envp);
714 # endif
715 #else
716 #endif
718 TRACE2((psh, "sh_execve -> %d [%d]\n", rc, errno));
719 (void)psh;
720 return rc;
723 uid_t sh_getuid(shinstance *psh)
725 #ifdef SH_PURE_STUB_MODE
726 uid_t uid = 0;
727 #elif defined(SH_STUB_MODE)
728 # ifdef _MSC_VER
729 uid_t uid = 0;
730 # else
731 uid_t uid = getuid();
732 # endif
733 #else
734 #endif
736 TRACE2((psh, "sh_getuid() -> %d [%d]\n", uid, errno));
737 (void)psh;
738 return uid;
741 uid_t sh_geteuid(shinstance *psh)
743 #ifdef SH_PURE_STUB_MODE
744 uid_t euid = 0;
745 #elif defined(SH_STUB_MODE)
746 # ifdef _MSC_VER
747 uid_t euid = 0;
748 # else
749 uid_t euid = geteuid();
750 # endif
751 #else
752 #endif
754 TRACE2((psh, "sh_geteuid() -> %d [%d]\n", euid, errno));
755 (void)psh;
756 return euid;
759 gid_t sh_getgid(shinstance *psh)
761 #ifdef SH_PURE_STUB_MODE
762 gid_t gid = 0;
763 #elif defined(SH_STUB_MODE)
764 # ifdef _MSC_VER
765 gid_t gid = 0;
766 # else
767 gid_t gid = getgid();
768 # endif
769 #else
770 #endif
772 TRACE2((psh, "sh_getgid() -> %d [%d]\n", gid, errno));
773 (void)psh;
774 return gid;
777 gid_t sh_getegid(shinstance *psh)
779 #ifdef SH_PURE_STUB_MODE
780 gid_t egid = 0;
781 #elif defined(SH_STUB_MODE)
782 # ifdef _MSC_VER
783 gid_t egid = 0;
784 # else
785 gid_t egid = getegid();
786 # endif
787 #else
788 #endif
790 TRACE2((psh, "sh_getegid() -> %d [%d]\n", egid, errno));
791 (void)psh;
792 return egid;
795 pid_t sh_getpid(shinstance *psh)
797 pid_t pid;
799 #ifdef SH_PURE_STUB_MODE
800 pid = 0;
801 #elif defined(SH_STUB_MODE)
802 # ifdef _MSC_VER
803 pid = _getpid();
804 # else
805 pid = getpid();
806 # endif
807 #else
808 #endif
810 (void)psh;
811 return pid;
814 pid_t sh_getpgrp(shinstance *psh)
816 #ifdef SH_PURE_STUB_MODE
817 pid_t pgrp = 0;
818 #elif defined(SH_STUB_MODE)
819 # ifdef _MSC_VER
820 pid_t pgrp _getpid();
821 # else
822 pid_t pgrp = getpgrp();
823 # endif
824 #else
825 #endif
827 TRACE2((psh, "sh_getpgrp() -> %d [%d]\n", pgrp, errno));
828 (void)psh;
829 return pgrp;
832 pid_t sh_getpgid(shinstance *psh, pid_t pid)
834 #ifdef SH_PURE_STUB_MODE
835 pid_t pgid = pid;
836 #elif defined(SH_STUB_MODE)
837 # ifdef _MSC_VER
838 pid_t pgid = pid;
839 # else
840 pid_t pgid = getpgid(pid);
841 # endif
842 #else
843 #endif
845 TRACE2((psh, "sh_getpgid(%d) -> %d [%d]\n", pid, pgid, errno));
846 (void)psh;
847 return pgid;
850 int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
852 #ifdef SH_PURE_STUB_MODE
853 int rc = -1;
854 #elif defined(SH_STUB_MODE)
855 # ifdef _MSC_VER
856 int rc = -1;
857 # else
858 int rc = setpgid(pid, pgid);
859 # endif
860 #else
861 #endif
863 TRACE2((psh, "sh_setpgid(%d, %d) -> %d [%d]\n", pid, pgid, rc, errno));
864 (void)psh;
865 return rc;
868 pid_t sh_tcgetpgrp(shinstance *psh, int fd)
870 pid_t pgrp;
872 #ifdef SH_PURE_STUB_MODE
873 pgrp = -1;
874 #elif defined(SH_STUB_MODE)
875 # ifdef _MSC_VER
876 pgrp = -1;
877 # else
878 pgrp = tcgetpgrp(fd);
879 # endif
880 #else
881 #endif
883 TRACE2((psh, "sh_tcgetpgrp(%d) -> %d [%d]\n", fd, pgrp, errno));
884 (void)psh;
885 return pgrp;
888 int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
890 int rc;
891 TRACE2((psh, "sh_tcsetpgrp(%d, %d)\n", fd, pgrp));
893 #ifdef SH_PURE_STUB_MODE
894 rc = -1;
895 #elif defined(SH_STUB_MODE)
896 # ifdef _MSC_VER
897 rc = -1;
898 # else
899 rc = tcsetpgrp(fd, pgrp);
900 # endif
901 #else
902 #endif
904 TRACE2((psh, "sh_tcsetpgrp(%d, %d) -> %d [%d]\n", fd, pgrp, rc, errno));
905 (void)psh;
906 return rc;
909 int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
911 #ifdef SH_PURE_STUB_MODE
912 int rc = -1;
913 #elif defined(SH_STUB_MODE)
914 # ifdef _MSC_VER
915 int rc = -1;
916 # else
917 int rc = getrlimit(resid, limp);
918 # endif
919 #else
920 #endif
922 TRACE2((psh, "sh_getrlimit(%d, %p) -> %d [%d] {%ld,%ld}\n",
923 resid, limp, rc, errno, (long)limp->rlim_cur, (long)limp->rlim_max));
924 (void)psh;
925 return rc;
928 int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
930 #ifdef SH_PURE_STUB_MODE
931 int rc = -1;
932 #elif defined(SH_STUB_MODE)
933 # ifdef _MSC_VER
934 int rc = -1;
935 # else
936 int rc = setrlimit(resid, limp);
937 # endif
938 #else
939 #endif
941 TRACE2((psh, "sh_setrlimit(%d, %p:{%ld,%ld}) -> %d [%d]\n",
942 resid, limp, (long)limp->rlim_cur, (long)limp->rlim_max, rc, errno));
943 (void)psh;
944 return rc;