VirtualBox

Changeset 3433 in kBuild


Ignore:
Timestamp:
Sep 2, 2020 5:25:31 PM (5 years ago)
Author:
bird
Message:

kash: refactoring forkshell(); simple stuff.

Location:
trunk/src/kash
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/Makefile.kmk

    r3409 r3433  
    4343kash_DEFS.win = \
    4444        BSD YY_NO_UNISTD_H \
    45         SH_DEAL_WITH_CRLF PC_PATH_SEP PC_DRIVE_LETTERS PC_EXE_EXTS EXEC_HASH_BANG_SCRIPT
     45        SH_DEAL_WITH_CRLF PC_PATH_SEP PC_DRIVE_LETTERS PC_EXE_EXTS EXEC_HASH_BANG_SCRIPT \
     46        KASH_USE_FORKSHELL2
    4647kash_DEFS.os2 = \
    4748        HAVE_SYS_SIGNAME HAVE_SYSCTL_H HAVE_SETPROGNAME PC_OS2_LIBPATHS \
  • trunk/src/kash/eval.c

    r3065 r3433  
    413413
    414414
     415#ifdef KASH_USE_FORKSHELL2
     416/*
     417 * Child of evalsubshell.
     418 */
     419struct evalsubshellchild
     420{
     421    int flags;
     422    int backgnd;
     423};
     424
     425static int evalsubshell_child(shinstance *psh, union node *n, void *argp)
     426{
     427    struct evalsubshellchild args = *(struct evalsubshellchild *)argp;
     428
     429    INTON;
     430    if (args.backgnd)
     431            args.flags &=~ EV_TESTED;
     432    redirect(psh, n->nredir.redirect, 0);
     433    /* never returns */
     434    evaltree(psh, n->nredir.n, args.flags | EV_EXIT);
     435    /** @todo make us return here. */
     436    return 0;
     437}
     438#endif /* KASH_USE_FORKSHELL2 */
     439
    415440
    416441/*
     
    427452        INTOFF;
    428453        jp = makejob(psh, n, 1);
     454#ifdef KASH_USE_FORKSHELL2
     455        {
     456                struct evalsubshellchild args;
     457                args.flags = flags;
     458                args.backgnd = backgnd;
     459                forkshell2(psh, jp, n, backgnd ? FORK_BG : FORK_FG,
     460                           evalsubshell_child, n, &args, sizeof(args));
     461        }
     462#else
    429463        if (forkshell(psh, jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
    430464                INTON;
     
    435469                evaltree(psh, n->nredir.n, flags | EV_EXIT);
    436470        }
     471#endif
    437472        if (! backgnd)
    438473                psh->exitstatus = waitforjob(psh, jp);
     
    475510
    476511
     512#ifdef KASH_USE_FORKSHELL2
     513/*
     514 * Child of evalpipe.
     515 */
     516struct evalpipechild
     517{
     518        int prevfd;
     519        int pip[2];
     520};
     521
     522static int evalpipe_child(shinstance *psh, union node *n, void *argp)
     523{
     524        struct evalpipechild args = *(struct evalpipechild *)argp;
     525
     526        if (args.prevfd > 0) {
     527                movefd(psh, args.prevfd, 0);
     528        }
     529        if (args.pip[1] >= 0) {
     530                shfile_close(&psh->fdtab, args.pip[0]);
     531                if (args.pip[1] != 1) {
     532                        movefd(psh, args.pip[1], 1);
     533                }
     534        }
     535        evaltree(psh, n, EV_EXIT);
     536        /** @todo make it return thru here. */
     537        return 0;
     538}
     539#endif /* KASH_USE_FORKSHELL2 */
    477540
    478541/*
     
    508571                        }
    509572                }
     573#ifdef KASH_USE_FORKSHELL2
     574                {
     575                        struct evalpipechild args;
     576                        args.prevfd = prevfd;
     577                        args.pip[0] = pip[0];
     578                        args.pip[1] = pip[1];
     579                        forkshell2(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG,
     580                                   evalpipe_child, lp->n, &args, sizeof(args));
     581                }
     582#else
    510583                if (forkshell(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
    511584                        INTON;
     
    521594                        evaltree(psh, lp->n, EV_EXIT);
    522595                }
     596#endif
    523597                if (prevfd >= 0)
    524598                        shfile_close(&psh->fdtab, prevfd);
     
    533607}
    534608
    535 
     609#ifdef KASH_USE_FORKSHELL2
     610/*
     611 * evalbackcmd child.
     612 */
     613struct evalbackcmdchild
     614{
     615        int pip[2];
     616};
     617
     618static int evalbackcmd_child(shinstance *psh, union node *n, void *argp)
     619{
     620        struct evalbackcmdchild args = *(struct evalbackcmdchild *)argp;
     621
     622        FORCEINTON;
     623        shfile_close(&psh->fdtab, args.pip[0]);
     624        if (args.pip[1] != 1) {
     625                movefd(psh, args.pip[1], 1);
     626        }
     627        eflag(psh) = 0;
     628        evaltree(psh, n, EV_EXIT);
     629        /* NOTREACHED */ /** @todo make it return here to simplify thread handling (no need for setjmp). */
     630        return 0;
     631}
     632#endif /* KASH_USE_FORKSHELL2 */
    536633
    537634/*
     
    574671                        error(psh, "Pipe call failed");
    575672                jp = makejob(psh, n, 1);
     673#ifdef KASH_USE_FORKSHELL2
     674                {
     675                        struct evalbackcmdchild args;
     676                        args.pip[0] = pip[0];
     677                        args.pip[1] = pip[1];
     678                        forkshell2(psh, jp, n, FORK_NOJOB,
     679                                   evalbackcmd_child, n, &args, sizeof(args));
     680                }
     681#else
    576682                if (forkshell(psh, jp, n, FORK_NOJOB) == 0) {
    577683                        FORCEINTON;
     
    584690                        /* NOTREACHED */
    585691                }
     692#endif
    586693                shfile_close(&psh->fdtab, pip[1]);
    587694                result->fd = pip[0];
  • trunk/src/kash/jobs.c

    r3408 r3433  
    800800}
    801801
     802int forkshell2(struct shinstance *psh, struct job *jp, union node *n, int mode,
     803               int (*child)(struct shinstance *, void *, union node *),
     804               union node *nchild, void *argp, size_t arglen)
     805{
     806        pid_t pid;
     807
     808        TRACE((psh, "forkshell2(%%%d, %p, %d, %p, %p, %p, %d) called\n", jp - psh->jobtab, n, mode, child, nchild, argp, (int)arglen));
     809        pid = sh_fork(psh);
     810        if (pid == 0)
     811        {
     812                /* child */
     813                (void)arglen;
     814                forkchild(psh, jp, n, mode, 0);
     815                sh_exit(psh, child(psh, nchild, argp));
     816                return 0;
     817        }
     818
     819        /* parent */
     820        if (pid != -1)
     821                return forkparent(psh, jp, n, mode, pid);
     822        TRACE((psh, "Fork failed, errno=%d\n", errno));
     823        INTON;
     824        error(psh, "Cannot fork");
     825        return -1; /* won't get here */
     826}
     827
    802828int
    803829forkparent(shinstance *psh, struct job *jp, union node *n, int mode, pid_t pid)
  • trunk/src/kash/jobs.h

    r1233 r3433  
    9696struct job *makejob(struct shinstance *, union node *, int);
    9797int forkshell(struct shinstance *, struct job *, union node *, int);
     98#ifdef KASH_USE_FORKSHELL2
     99int forkshell2(struct shinstance *, struct job *, union node *, int,
     100               int (*child)(struct shinstance *, void *, union node *),
     101               union node *, void *, size_t);
     102#endif
    98103void forkchild(struct shinstance *, struct job *, union node *, int, int);
    99104int forkparent(struct shinstance *, struct job *, union node *, int, pid_t);
  • trunk/src/kash/redir.c

    r2648 r3433  
    243243}
    244244
     245#ifdef KASH_USE_FORKSHELL2
     246struct openherechild
     247{
     248        int pip[2];
     249        size_t len;
     250};
     251static int openhere_child(shinstance *psh, union node *n, void *argp)
     252{
     253        struct openherechild args = *(struct openherechild *)argp;
     254
     255        shfile_close(&psh->fdtab, args.pip[0]);
     256        sh_signal(psh, SIGINT, SH_SIG_IGN);
     257        sh_signal(psh, SIGQUIT, SH_SIG_IGN);
     258        sh_signal(psh, SIGHUP, SH_SIG_IGN);
     259# ifdef SIGTSTP
     260        sh_signal(psh, SIGTSTP, SH_SIG_IGN);
     261# endif
     262        sh_signal(psh, SIGPIPE, SH_SIG_DFL);
     263        if (n->type == NHERE)
     264                xwrite(psh, args.pip[1], n->nhere.doc->narg.text, args.len);
     265        else
     266                expandhere(psh, n->nhere.doc, args.pip[1]);
     267        return 0;
     268}
     269
     270#endif /* KASH_USE_FORKSHELL2*/
    245271
    246272/*
     
    265291                }
    266292        }
     293#ifdef KASH_USE_FORKSHELL2
     294        {
     295                struct openherechild args;
     296                args.pip[0] = pip[0];
     297                args.pip[1] = pip[1];
     298                args.len = len;
     299                forkshell2(psh, (struct job *)NULL, (union node *)NULL, FORK_NOJOB,
     300                           openhere_child, redir, &args, sizeof(args));
     301        }
     302#else
    267303        if (forkshell(psh, (struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
    268304                shfile_close(&psh->fdtab, pip[0]);
     
    280316                sh__exit(psh, 0);
    281317        }
     318#endif
    282319out:
    283320        shfile_close(&psh->fdtab, pip[1]);
  • trunk/src/kash/tests/Makefile.kmk

    r2423 r3433  
    4545        pipe-1 \
    4646        pipe-2 \
    47         exec-1 \
    4847        )
     48
     49#       exec-1  - lost
    4950
    5051
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette