1fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes/* $OpenBSD: jobs.c,v 1.43 2015/09/10 22:48:58 nicm Exp $ */ 25155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 35155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*- 4811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011, 5fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes * 2012, 2013, 2014, 2015, 2016 6fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes * mirabilos <m@mirbsd.org> 75155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 85155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Provided that these terms and disclaimer and all copyright notices 95155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * are retained or reproduced in an accompanying document, permission 105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * is granted to deal in this work without restriction, including un- 115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * limited rights to use, publicly perform, distribute, sell, modify, 125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * merge, give away, or sublicence. 135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to 155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * the utmost extent permitted by applicable law, neither express nor 165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * implied; without malicious intent or gross negligence. In no event 175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * may a licensor, author or contributor be held liable for indirect, 185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * direct, other damage, loss, or other issues arising in any way out 195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * of dealing in the work, even if advised of the possibility of such 205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * damage or existence of a defect, except proven that it results out 215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * of said person's immediate fault when using the work as intended. 225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#include "sh.h" 255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 2677740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.121 2016/07/25 00:04:44 tg Exp $"); 275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#if HAVE_KILLPG 295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define mksh_killpg killpg 305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#else 315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* cross fingers and hope kill is killpg-endowed */ 325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define mksh_killpg(p,s) kill(-(p), (s)) 335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Order important! */ 365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PRUNNING 0 375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PEXITED 1 385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PSIGNALLED 2 395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PSTOPPED 3 405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 41c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glasertypedef struct proc Proc; 425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustruct proc { 435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Proc *next; /* next process in pipeline (if any) */ 445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru pid_t pid; /* process id */ 455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int state; 465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int status; /* wait status */ 47c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser /* process command string from vistree */ 4877740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes char command[256 - (ALLOC_OVERHEAD + sizeof(Proc *) + 4977740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes sizeof(pid_t) + 2 * sizeof(int))]; 505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}; 515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Notify/print flag - j_print() argument */ 535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_SHORT 1 /* print signals processes were killed by */ 545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_MEDIUM 2 /* print [job-num] -/+ command */ 555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_LONG 3 /* print [job-num] -/+ pid command */ 565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_PGRP 4 /* print pgrp */ 575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* put_job() flags */ 595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PJ_ON_FRONT 0 /* at very front */ 605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PJ_PAST_STOPPED 1 /* just past any stopped jobs */ 615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Job.flags values */ 635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_STARTED 0x001 /* set when all processes in job are started */ 645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_WAITING 0x002 /* set if j_waitj() is waiting on job */ 655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_W_ASYNCNOTIFY 0x004 /* set if waiting and async notification ok */ 665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_XXCOM 0x008 /* set for $(command) jobs */ 675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_FG 0x010 /* running in foreground (also has tty pgrp) */ 6803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#define JF_SAVEDTTY 0x020 /* j->ttystat is valid */ 695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_CHANGED 0x040 /* process has changed state */ 705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_KNOWN 0x080 /* $! referenced */ 715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_ZOMBIE 0x100 /* known, unwaited process */ 725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_REMOVE 0x200 /* flagged for removal (j_jobs()/j_noityf()) */ 735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_USETTYMODE 0x400 /* tty mode saved if process exits normally */ 745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_SAVEDTTYPGRP 0x800 /* j->saved_ttypgrp is valid */ 755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querutypedef struct job Job; 775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustruct job { 785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Job *next; /* next job in list */ 795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Proc *proc_list; /* process list */ 805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Proc *last_proc; /* last process in list */ 815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru struct timeval systime; /* system time used by job */ 825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru struct timeval usrtime; /* user time used by job */ 835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru pid_t pgrp; /* process group of job */ 845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru pid_t ppid; /* pid of process that forked job */ 855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int job; /* job number: %n */ 865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int flags; /* see JF_* */ 875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru volatile int state; /* job state */ 885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int status; /* exit status of last process */ 89fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes int age; /* number of jobs started */ 905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Coproc_id coproc_id; /* 0 or id of coprocess output pipe */ 915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 92c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_ttyst ttystat; /* saved tty state for stopped jobs */ 935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru pid_t saved_ttypgrp; /* saved tty process group for stopped jobs */ 945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}; 965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Flags for j_waitj() */ 985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_NONE 0x00 995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_INTERRUPT 0x01 /* ^C will stop the wait */ 1005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_ASYNCNOTIFY 0x02 /* asynchronous notification during wait ok */ 1015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_STOPPEDWAIT 0x04 /* wait even if job stopped */ 10203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#define JW_PIPEST 0x08 /* want PIPESTATUS */ 1035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Error codes for j_lookup() */ 105811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#define JL_NOSUCH 0 /* no such job */ 106811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#define JL_AMBIG 1 /* %foo or %?foo is ambiguous */ 107811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#define JL_INVALID 2 /* non-pid, non-% job id */ 1085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 109c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic const char * const lookup_msgs[] = { 1105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru "no such job", 1115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru "ambiguous", 112811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser "argument must be %job or process id" 1135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}; 1145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *job_list; /* job list */ 1165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *last_job; 1175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *async_job; 1185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic pid_t async_pid; 1195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int nzombie; /* # of zombies owned by this process */ 121fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughesstatic int njobs; /* # of jobs started */ 1225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef CHILD_MAX 1245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define CHILD_MAX 25 1255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 1265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 12703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 1285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* held_sigchld is set if sigchld occurs before a job is completely started */ 1295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic volatile sig_atomic_t held_sigchld; 13003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 1315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 1335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic struct shf *shl_j; 1345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic bool ttypgrp_ok; /* set if can use tty pgrps */ 1355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic pid_t restore_ttypgrp = -1; 1365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int const tt_sigs[] = { SIGTSTP, SIGTTIN, SIGTTOU }; 1375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 1385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void j_set_async(Job *); 1405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void j_startjob(Job *); 1415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int j_waitj(Job *, int, const char *); 1425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void j_sigchld(int); 1435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void j_print(Job *, int, struct shf *); 1445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *j_lookup(const char *, int *); 1455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *new_job(void); 1465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Proc *new_proc(void); 1475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void check_job(Job *); 1485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void put_job(Job *, int); 1495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void remove_job(Job *, const char *); 1505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int kill_job(Job *, int); 1515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 152c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void tty_init_talking(void); 153c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void tty_init_state(void); 154c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser 1555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* initialise job control */ 1565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid 1575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_init(void) 1585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 1595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 1605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru bool mflagset = Flag(FMONITOR) != 127; 1615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Flag(FMONITOR) = 0; 1635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 1645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 1665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (void)sigemptyset(&sm_default); 1675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &sm_default, NULL); 1685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (void)sigemptyset(&sm_sigchld); 1705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (void)sigaddset(&sm_sigchld, SIGCHLD); 1715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[SIGCHLD], j_sigchld, 1735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP); 17403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else 17503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* Make sure SIGCHLD isn't ignored - can do odd things under SYSV */ 17603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra setsig(&sigtraps[SIGCHLD], SIG_DFL, SS_RESTORE_ORIG|SS_FORCE); 17703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 1785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 1805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!mflagset && Flag(FTALKING)) 1815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Flag(FMONITOR) = 1; 1825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 1845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * shl_j is used to do asynchronous notification (used in 1855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * an interrupt handler, so need a distinct shf) 1865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 1875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shl_j = shf_fdopen(2, SHF_WR, NULL); 1885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR) || Flag(FTALKING)) { 1905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int i; 1915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 1935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * the TF_SHELL_USES test is a kludge that lets us know if 1945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * if the signals have been changed by the shell. 1955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 1965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (i = NELEM(tt_sigs); --i >= 0; ) { 1975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigtraps[tt_sigs[i]].flags |= TF_SHELL_USES; 1985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* j_change() sets this to SS_RESTORE_DFL if FMONITOR */ 1995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[tt_sigs[i]], SIG_IGN, 2005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_IGN|SS_FORCE); 2015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 2025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 2035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 204c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser /* j_change() calls tty_init_talking() and tty_init_state() */ 2055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR)) 2065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_change(); 2075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 2085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 209c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser if (Flag(FTALKING)) { 210c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser tty_init_talking(); 211c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser tty_init_state(); 212c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser } 2135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 2145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 21503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrastatic int 21603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraproc_errorlevel(Proc *p) 21703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra{ 21803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra switch (p->state) { 21903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra case PEXITED: 220fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes return ((WEXITSTATUS(p->status)) & 255); 22103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra case PSIGNALLED: 222fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes return (ksh_sigmask(WTERMSIG(p->status))); 22303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra default: 22403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra return (0); 22503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra } 22603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra} 22703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 228737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#if !defined(MKSH_UNEMPLOYED) && HAVE_GETSID 229737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes/* suspend the shell */ 230737fdce098f804459a925438e48dd711c31bbc9eElliott Hughesvoid 231737fdce098f804459a925438e48dd711c31bbc9eElliott Hughesj_suspend(void) 232737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes{ 233737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes struct sigaction sa, osa; 234737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes 235737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes /* Restore tty and pgrp. */ 236737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (ttypgrp_ok) { 237737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (tty_hasstate) 238737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes mksh_tcset(tty_fd, &tty_state); 239737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (restore_ttypgrp >= 0) { 240737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (tcsetpgrp(tty_fd, restore_ttypgrp) < 0) { 24177740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, 24277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes Tj_suspend, "tcsetpgrp", cstrerror(errno)); 243737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } else if (setpgid(0, restore_ttypgrp) < 0) { 24477740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, 24577740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes Tj_suspend, "setpgid", cstrerror(errno)); 246737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } 247737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } 248737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } 249737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes 250737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes /* Suspend the shell. */ 251737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes memset(&sa, 0, sizeof(sa)); 252737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes sigemptyset(&sa.sa_mask); 253737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes sa.sa_handler = SIG_DFL; 254737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes sigaction(SIGTSTP, &sa, &osa); 255737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes kill(0, SIGTSTP); 256737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes 257737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes /* Back from suspend, reset signals, pgrp and tty. */ 258737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes sigaction(SIGTSTP, &osa, NULL); 259737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (ttypgrp_ok) { 260737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (restore_ttypgrp >= 0) { 261737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (setpgid(0, kshpid) < 0) { 26277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, 26377740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes Tj_suspend, "setpgid", cstrerror(errno)); 264737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes ttypgrp_ok = false; 265737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } else if (tcsetpgrp(tty_fd, kshpid) < 0) { 26677740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, 26777740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes Tj_suspend, "tcsetpgrp", cstrerror(errno)); 268737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes ttypgrp_ok = false; 269737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } 270737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } 271737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes tty_init_state(); 272737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } 273737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes} 274737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#endif 275737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes 2765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* job cleanup before shell exit */ 2775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid 2785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_exit(void) 2795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 2805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* kill stopped, and possibly running, jobs */ 28103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j; 28203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra bool killed = false; 2835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 2845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) { 2855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->ppid == procpid && 2865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (j->state == PSTOPPED || 2875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (j->state == PRUNNING && 2885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ((j->flags & JF_FG) || 2895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid))))) { 29003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra killed = true; 2915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->pgrp == 0) 2925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru kill_job(j, SIGHUP); 2935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 2945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru mksh_killpg(j->pgrp, SIGHUP); 2955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 2965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PSTOPPED) { 2975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->pgrp == 0) 2985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru kill_job(j, SIGCONT); 2995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 3005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru mksh_killpg(j->pgrp, SIGCONT); 3015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 3035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (killed) 3065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sleep(1); 3075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_notify(); 3085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 3095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 3105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (kshpid == procpid && restore_ttypgrp >= 0) { 3115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 3125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Need to restore the tty pgrp to what it was when the 3135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * shell started up, so that the process that started us 3145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * will be able to access the tty when we are done. 3155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Also need to restore our process group in case we are 3165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * about to do an exec so that both our parent and the 3175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * process we are to become will be able to access the tty. 3185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 3195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru tcsetpgrp(tty_fd, restore_ttypgrp); 3205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setpgid(0, restore_ttypgrp); 3215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR)) { 3235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Flag(FMONITOR) = 0; 3245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_change(); 3255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 3275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 3285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 3295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 3305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* turn job control on or off according to Flag(FMONITOR) */ 3315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid 3325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_change(void) 3335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 3345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int i; 3355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 3365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR)) { 3375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru bool use_tty = Flag(FTALKING); 3385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 339c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser /* don't call mksh_tcget until we own the tty process group */ 3405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (use_tty) 341c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser tty_init_talking(); 3425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 3435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* no controlling tty, no SIGT* */ 344c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser if ((ttypgrp_ok = (use_tty && tty_fd >= 0 && tty_devtty))) { 3455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[SIGTTIN], SIG_DFL, 3465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_ORIG|SS_FORCE); 3475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* wait to be given tty (POSIX.1, B.2, job control) */ 34803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra while (/* CONSTCOND */ 1) { 3495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru pid_t ttypgrp; 3505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 3515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((ttypgrp = tcgetpgrp(tty_fd)) < 0) { 35277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, 35377740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes "j_init", "tcgetpgrp", 354c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser cstrerror(errno)); 3555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ttypgrp_ok = false; 3565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 3575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp == kshpgrp) 3595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 3605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru kill(0, SIGTTIN); 3615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (i = NELEM(tt_sigs); --i >= 0; ) 3645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[tt_sigs[i]], SIG_IGN, 3655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_DFL|SS_FORCE); 3665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp_ok && kshpgrp != kshpid) { 3675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (setpgid(0, kshpid) < 0) { 36877740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, 36977740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes "j_init", "setpgid", cstrerror(errno)); 3705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ttypgrp_ok = false; 3715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else { 3725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (tcsetpgrp(tty_fd, kshpid) < 0) { 37377740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, 37477740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes "j_init", "tcsetpgrp", 375c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser cstrerror(errno)); 3765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ttypgrp_ok = false; 3775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else 3785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru restore_ttypgrp = kshpgrp; 3795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru kshpgrp = kshpid; 3805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 3815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 382c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifndef MKSH_DISABLE_TTY_WARNING 3835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (use_tty && !ttypgrp_ok) 38477740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_sD_s, "warning", 38503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra "won't have full job control"); 386c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif 3875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else { 3885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ttypgrp_ok = false; 3895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FTALKING)) 3905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (i = NELEM(tt_sigs); --i >= 0; ) 3915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[tt_sigs[i]], SIG_IGN, 3925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_IGN|SS_FORCE); 3935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 3945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (i = NELEM(tt_sigs); --i >= 0; ) { 3955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (sigtraps[tt_sigs[i]].flags & 3965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (TF_ORIG_IGN | TF_ORIG_DFL)) 3975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[tt_sigs[i]], 3985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (sigtraps[tt_sigs[i]].flags & TF_ORIG_IGN) ? 3995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SIG_IGN : SIG_DFL, 4005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_ORIG|SS_FORCE); 4015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 4025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 403c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser tty_init_state(); 4045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 4055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 4065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 40703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#if HAVE_NICE 40803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra/* run nice(3) and ignore the result */ 40903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrastatic void 41003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraksh_nice(int ness) 41103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra{ 41203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#if defined(__USE_FORTIFY_LEVEL) && (__USE_FORTIFY_LEVEL > 0) 413c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser int eno; 41403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 41503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra errno = 0; 41603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* this is gonna annoy users; complain to your distro, people! */ 417c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser if (nice(ness) == -1 && (eno = errno) != 0) 41877740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_sD_s, "bgnice", cstrerror(eno)); 41903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else 42003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra (void)nice(ness); 42103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 42203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra} 42303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 42403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 4255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* execute tree in child subprocess */ 4265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint 4275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruexchild(struct op *t, int flags, 4285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru volatile int *xerrok, 42903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* used if XPCLOSE or XCCLOSE */ 43003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int close_fd) 4315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 43203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* for pipelines */ 43303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra static Proc *last_proc; 4345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 43503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int rv = 0, forksleep, jwflags = JW_NONE; 43603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 4375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 43803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 43903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Proc *p; 44003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j; 44103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra pid_t cldpid; 44203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 44303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (flags & XPIPEST) { 44403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra flags &= ~XPIPEST; 44503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra jwflags |= JW_PIPEST; 44603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra } 4475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 4485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (flags & XEXEC) 4495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 4505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND 4515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * (also done in another execute() below) 4525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 4535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (execute(t, flags & (XEXEC | XERROK), xerrok)); 4545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 45503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 4565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* no SIGCHLDs while messing with job and process lists */ 4575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 45803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 4595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 46003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra p = new_proc(); 46103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra p->next = NULL; 46203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra p->state = PRUNNING; 46303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra p->status = 0; 46403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra p->pid = 0; 4655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 4665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* link process into jobs list */ 4675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (flags & XPIPEI) { 4685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* continuing with a pipe */ 4695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!last_job) 47077740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes internal_errorf("exchild: XPIPEI and no last_job - pid %d", 4715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (int)procpid); 47203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j = last_job; 4735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (last_proc) 47403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra last_proc->next = p; 47503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra last_proc = p; 4765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else { 47703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* fills in j->job */ 47803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j = new_job(); 4795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 4805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * we don't consider XXCOMs foreground since they don't get 4815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * tty process group and we don't save or restore tty modes. 4825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 48303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->flags = (flags & XXCOM) ? JF_XXCOM : 4845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE)); 48503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra timerclear(&j->usrtime); 48603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra timerclear(&j->systime); 48703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->state = PRUNNING; 48803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->pgrp = 0; 48903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->ppid = procpid; 49003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->age = ++njobs; 49103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->proc_list = p; 49203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->coproc_id = 0; 49303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra last_job = j; 49403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra last_proc = p; 49503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra put_job(j, PJ_PAST_STOPPED); 4965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 4975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 49803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vistree(p->command, sizeof(p->command), t); 4995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 5005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* create child process */ 5015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru forksleep = 1; 50203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra while ((cldpid = fork()) < 0 && errno == EAGAIN && forksleep < 32) { 50303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (intrsig) 50403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* allow user to ^C out... */ 5055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 5065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sleep(forksleep); 5075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru forksleep <<= 1; 5085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 50903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* ensure $RANDOM changes between parent and child */ 510811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser rndset((unsigned long)cldpid); 51103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* fork failed? */ 51203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (cldpid < 0) { 51303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra kill_job(j, SIGKILL); 51403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra remove_job(j, "fork failed"); 51503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 5165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 51703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 51803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra errorf("can't fork - try again"); 5195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 52003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra p->pid = cldpid ? cldpid : (procpid = getpid()); 5215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 5225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 5235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* job control set up */ 5245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR) && !(flags&XXCOM)) { 52503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra bool dotty = false; 526c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser 52703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (j->pgrp == 0) { 52803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* First process */ 52903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->pgrp = p->pid; 53003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra dotty = true; 5315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 5325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 53303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* 53403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * set pgrp in both parent and child to deal with race 5355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * condition 5365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 53703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra setpgid(p->pid, j->pgrp); 5385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp_ok && dotty && !(flags & XBGND)) 53903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra tcsetpgrp(tty_fd, j->pgrp); 5405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 5415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 5425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 5435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* used to close pipe input fd */ 54403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (close_fd >= 0 && (((flags & XPCLOSE) && cldpid) || 54503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra ((flags & XCCLOSE) && !cldpid))) 5465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru close(close_fd); 54703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (!cldpid) { 5485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* child */ 5495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 5505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Do this before restoring signal */ 5515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (flags & XCOPROC) 5525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru coproc_cleanup(false); 5535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru cleanup_parents_env(); 5545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 55503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* 55603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * If FMONITOR or FTALKING is set, these signals are ignored, 5575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * if neither FMONITOR nor FTALKING are set, the signals have 5585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * their inherited values. 5595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 5605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR) && !(flags & XXCOM)) { 5615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (forksleep = NELEM(tt_sigs); --forksleep >= 0; ) 5625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[tt_sigs[forksleep]], SIG_DFL, 5635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_DFL|SS_FORCE); 5645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 5655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 5665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#if HAVE_NICE 5675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FBGNICE) && (flags & XBGND)) 56803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra ksh_nice(4); 5695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 5705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((flags & XBGND) 5715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 5725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru && !Flag(FMONITOR) 5735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 5745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ) { 5755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[SIGINT], SIG_IGN, 5765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_IGN|SS_FORCE); 5775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru setsig(&sigtraps[SIGQUIT], SIG_IGN, 5785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru SS_RESTORE_IGN|SS_FORCE); 5795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((!(flags & (XPIPEI | XCOPROC))) && 5805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ((forksleep = open("/dev/null", 0)) > 0)) { 5815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (void)ksh_dup2(forksleep, 0, true); 5825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru close(forksleep); 5835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 5845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 58503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* in case of $(jobs) command */ 58603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra remove_job(j, "child"); 587811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#ifndef MKSH_NOPROSPECTOFWORK 588811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser /* remove_job needs SIGCHLD blocked still */ 589811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser sigprocmask(SIG_SETMASK, &omask, NULL); 590811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#endif 5915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru nzombie = 0; 5925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 5935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ttypgrp_ok = false; 5945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Flag(FMONITOR) = 0; 5955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 5965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Flag(FTALKING) = 0; 5975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru cleartraps(); 5985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* no return */ 5995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru execute(t, (flags & XERROK) | XEXEC, NULL); 6005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_SMALL 6015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (t->type == TPIPE) 6025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru unwind(LLEAVE); 60377740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes internal_warningf("%s: execute() returned", "exchild"); 60403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra fptreef(shl_out, 8, "%s: tried to execute {\n\t%T\n}\n", 60503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra "exchild", t); 6065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_flush(shl_out); 6075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 6085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru unwind(LLEAVE); 6095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* NOTREACHED */ 6105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 6115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* shell (parent) stuff */ 61303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (!(flags & XPIPEO)) { 61403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* last process in a job */ 61503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j_startjob(j); 6165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (flags & XCOPROC) { 61703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->coproc_id = coproc.id; 6185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* n jobs using co-process output */ 6195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru coproc.njobs++; 6205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* j using co-process input */ 62103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra coproc.job = (void *)j; 6225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 6235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (flags & XBGND) { 62403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j_set_async(j); 6255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FTALKING)) { 62603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra shf_fprintf(shl_out, "[%d]", j->job); 62703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra for (p = j->proc_list; p; p = p->next) 62877740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes shf_fprintf(shl_out, Tf__d, 62903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra (int)p->pid); 6305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_putchar('\n', shl_out); 6315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_flush(shl_out); 6325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 6335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else 63403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra rv = j_waitj(j, jwflags, "jw:last proc"); 6355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 6365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 63703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 6385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 63903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 6405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (rv); 6425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 6435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* start the last job: only used for $(command) jobs */ 6455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid 6465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustartlast(void) 6475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 64803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 6495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 6505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 65203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 6535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 65403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* no need to report error - waitlast() will do it */ 65503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (last_job) { 6565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* ensure it isn't removed by check_job() */ 6575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru last_job->flags |= JF_WAITING; 6585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_startjob(last_job); 6595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 66003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 6615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 66203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 6635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 6645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* wait for last job: only used for $(command) jobs */ 6665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint 6675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruwaitlast(void) 6685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 66903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int rv; 67003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j; 67103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 6725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 6735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 67503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 6765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j = last_job; 6785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!j || !(j->flags & JF_STARTED)) { 6795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!j) 68077740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(true, Tf_sD_s, "waitlast", "no last job"); 6815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 68277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes internal_warningf(Tf_sD_s, "waitlast", Tnot_started); 68303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 6845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 68503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 68603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* not so arbitrary, non-zero value */ 68703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra return (125); 6885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 6895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 69003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra rv = j_waitj(j, JW_NONE, "waitlast"); 6915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 69203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 6935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 69403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 6955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (rv); 6975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 6985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 6995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* wait for child, interruptable. */ 7005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint 7015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruwaitfor(const char *cp, int *sigp) 7025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 70303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int rv, ecode, flags = JW_INTERRUPT|JW_ASYNCNOTIFY; 70403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j; 70503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 7075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 70903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 7105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *sigp = 0; 7125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (cp == NULL) { 7145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 7155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * wait for an unspecified job - always returns 0, so 7165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * don't have to worry about exited/signaled jobs 7175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 7185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j; j = j->next) 7195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* AT&T ksh will wait for stopped jobs - we don't */ 7205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->ppid == procpid && j->state == PRUNNING) 7215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 7225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!j) { 72303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 72503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 7265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (-1); 7275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 7285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else if ((j = j_lookup(cp, &ecode))) { 7295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* don't report normal job completion */ 7305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru flags &= ~JW_ASYNCNOTIFY; 7315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->ppid != procpid) { 73203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 73403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 7355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (-1); 7365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 7375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else { 73803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 74003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 7415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ecode != JL_NOSUCH) 74277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_sD_s, cp, lookup_msgs[ecode]); 7435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (-1); 7445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 7455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* AT&T ksh will wait for stopped jobs - we don't */ 7475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru rv = j_waitj(j, flags, "jw:waitfor"); 7485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 74903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 75103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 7525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 75303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (rv < 0) 75403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* we were interrupted */ 755fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes *sigp = ksh_sigmask(-rv); 7565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (rv); 7585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 7595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* kill (built-in) a job */ 7615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint 7625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_kill(const char *cp, int sig) 7635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 76403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j; 76503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int rv = 0, ecode; 76603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 7685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 77003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 7715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 7725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((j = j_lookup(cp, &ecode)) == NULL) { 77303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 77503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 77677740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_sD_s, cp, lookup_msgs[ecode]); 7775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (1); 7785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 7795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 78003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (j->pgrp == 0) { 78103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* started when !Flag(FMONITOR) */ 7825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (kill_job(j, sig) < 0) { 78377740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_sD_s, cp, cstrerror(errno)); 7845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru rv = 1; 7855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 7865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else { 7875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 7885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PSTOPPED && (sig == SIGTERM || sig == SIGHUP)) 7895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru mksh_killpg(j->pgrp, SIGCONT); 7905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 7915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (mksh_killpg(j->pgrp, sig) < 0) { 79277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_sD_s, cp, cstrerror(errno)); 7935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru rv = 1; 7945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 7955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 7965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 79703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 7985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 79903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 8005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (rv); 8025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 8035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 8055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* fg and bg built-ins: called only if Flag(FMONITOR) set */ 8065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint 8075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_resume(const char *cp, int bg) 8085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 80903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j; 81003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Proc *p; 81103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int ecode, rv = 0; 81203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra bool running; 8135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 8145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 8165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((j = j_lookup(cp, &ecode)) == NULL) { 8185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 81977740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_sD_s, cp, lookup_msgs[ecode]); 8205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (1); 8215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->pgrp == 0) { 8245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 8255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru bi_errorf("job not job-controlled"); 8265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (1); 8275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (bg) 8305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shprintf("[%d] ", j->job); 8315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 83203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra running = false; 8335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p = j->proc_list; p != NULL; p = p->next) { 8345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (p->state == PSTOPPED) { 8355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->state = PRUNNING; 8365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->status = 0; 83703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra running = true; 8385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_puts(p->command, shl_stdout); 8405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (p->next) 8415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_puts("| ", shl_stdout); 8425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_putc('\n', shl_stdout); 8445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_flush(shl_stdout); 8455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (running) 8465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->state = PRUNNING; 8475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru put_job(j, PJ_PAST_STOPPED); 8495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (bg) 8505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_set_async(j); 8515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else { 8525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* attach tty to job */ 8535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PRUNNING) { 8545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp_ok && (j->flags & JF_SAVEDTTY)) 855c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_tcset(tty_fd, &j->ttystat); 8565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* See comment in j_waitj regarding saved_ttypgrp. */ 8575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp_ok && 8585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru tcsetpgrp(tty_fd, (j->flags & JF_SAVEDTTYPGRP) ? 8595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->saved_ttypgrp : j->pgrp) < 0) { 8605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru rv = errno; 8615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->flags & JF_SAVEDTTY) 862c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_tcset(tty_fd, &tty_state); 86303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra sigprocmask(SIG_SETMASK, &omask, NULL); 86477740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_ldfailed, 86577740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes "fg: 1st", "tcsetpgrp", tty_fd, 86603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra (long)((j->flags & JF_SAVEDTTYPGRP) ? 86777740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes j->saved_ttypgrp : j->pgrp), 868c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser cstrerror(rv)); 8695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (1); 8705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_FG; 8735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~JF_KNOWN; 8745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j == async_job) 8755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru async_job = NULL; 8765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PRUNNING && mksh_killpg(j->pgrp, SIGCONT) < 0) { 879c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser int eno = errno; 8805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 8815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!bg) { 8825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~JF_FG; 8835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp_ok && (j->flags & JF_SAVEDTTY)) 884c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_tcset(tty_fd, &tty_state); 8855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp_ok && tcsetpgrp(tty_fd, kshpgrp) < 0) 88677740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(true, Tf_ldfailed, 88703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra "fg: 2nd", "tcsetpgrp", tty_fd, 88877740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes (long)kshpgrp, cstrerror(errno)); 8895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 89177740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_s_sD_s, "can't continue job", 892c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser cp, cstrerror(eno)); 8935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (1); 8945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!bg) { 8965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ttypgrp_ok) { 8975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~(JF_SAVEDTTY | JF_SAVEDTTYPGRP); 8985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 8995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru rv = j_waitj(j, JW_NONE, "jw:resume"); 9005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 9015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 9025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (rv); 9035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 9045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 9055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* are there any running or stopped jobs ? */ 9075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint 9085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_stopped_running(void) 9095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 91003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j; 91103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int which = 0; 9125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) { 9145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 9155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->ppid == procpid && j->state == PSTOPPED) 9165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru which |= 1; 9175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 9185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid && 9195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->ppid == procpid && j->state == PRUNNING) 9205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru which |= 2; 9215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 9225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (which) { 9235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shellf("You have %s%s%s jobs\n", 9245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru which & 1 ? "stopped" : "", 9255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru which == 3 ? " and " : "", 9265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru which & 2 ? "running" : ""); 9275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (1); 9285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 9295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (0); 9315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 9325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* list jobs for jobs built-in */ 9355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint 9365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_jobs(const char *cp, int slp, 93703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* 0: short, 1: long, 2: pgrp */ 93803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int nflag) 9395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 94003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j, *tmp; 94103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int how, zflag = 0; 94203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 9435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 9445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 94603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 9475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 94803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra if (nflag < 0) { 94903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* kludge: print zombies */ 9505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru nflag = 0; 9515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru zflag = 1; 9525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 9535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (cp) { 954811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser int ecode; 9555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((j = j_lookup(cp, &ecode)) == NULL) { 95703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 9585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 95903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 96077740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes bi_errorf(Tf_sD_s, cp, lookup_msgs[ecode]); 9615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (1); 9625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 9635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else 9645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j = job_list; 9655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru how = slp == 0 ? JP_MEDIUM : (slp == 1 ? JP_LONG : JP_PGRP); 9665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (; j; j = j->next) { 9675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((!(j->flags & JF_ZOMBIE) || zflag) && 9685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (!nflag || (j->flags & JF_CHANGED))) { 9695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_print(j, how, shl_stdout); 9705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PEXITED || j->state == PSIGNALLED) 9715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_REMOVE; 9725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 9735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (cp) 9745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 9755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 9765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Remove jobs after printing so there won't be multiple + or - jobs */ 9775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j; j = tmp) { 9785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru tmp = j->next; 9795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->flags & JF_REMOVE) 98077740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes remove_job(j, Tjobs); 9815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 98203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 9835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 98403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 9855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (0); 9865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 9875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* list jobs for top-level notification */ 9895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid 9905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_notify(void) 9915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 99203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j, *tmp; 99303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 9945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 9955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 9965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 99703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 9985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j; j = j->next) { 9995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 10005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR) && (j->flags & JF_CHANGED)) 10015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_print(j, JP_MEDIUM, shl_out); 10025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 100303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* 100403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * Remove job after doing reports so there aren't 10055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * multiple +/- jobs. 10065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 10075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PEXITED || j->state == PSIGNALLED) 10085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_REMOVE; 10095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 10105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j; j = tmp) { 10115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru tmp = j->next; 10125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->flags & JF_REMOVE) 10135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru remove_job(j, "notify"); 10145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 10155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_flush(shl_out); 101603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 10175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 101803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 10195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 10205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Return pid of last process in last asynchronous job */ 10225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querupid_t 10235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_async(void) 10245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 102503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 10265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigset_t omask; 10275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 102903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 10305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (async_job) 10325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru async_job->flags |= JF_KNOWN; 10335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 103403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 10355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigprocmask(SIG_SETMASK, &omask, NULL); 103603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 10375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (async_pid); 10395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 10405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 10425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Make j the last async process 10435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 10445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 10455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 10465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void 10475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_set_async(Job *j) 10485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 104950012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Job *jl, *oldest; 10505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (async_job && (async_job->flags & (JF_KNOWN|JF_ZOMBIE)) == JF_ZOMBIE) 10525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru remove_job(async_job, "async"); 10535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!(j->flags & JF_STARTED)) { 105477740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes internal_warningf(Tf_sD_s, "j_async", Tjob_not_started); 10555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return; 10565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 10575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru async_job = j; 10585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru async_pid = j->last_proc->pid; 10595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru while (nzombie > CHILD_MAX) { 10605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru oldest = NULL; 10615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (jl = job_list; jl; jl = jl->next) 10625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (jl != async_job && (jl->flags & JF_ZOMBIE) && 10635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (!oldest || jl->age < oldest->age)) 10645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru oldest = jl; 10655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!oldest) { 10665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* XXX debugging */ 10675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!(async_job->flags & JF_ZOMBIE) || nzombie != 1) { 106803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra internal_warningf("%s: bad nzombie (%d)", 106903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra "j_async", nzombie); 10705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru nzombie = 0; 10715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 10725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 10735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 10745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru remove_job(oldest, "zombie"); 10755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 10765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 10775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 10795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Start a job: set STARTED, check for held signals and set j->last_proc 10805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 10815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 10825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 10835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void 10845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_startjob(Job *j) 10855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 108650012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Proc *p; 10875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 10885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_STARTED; 10895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p = j->proc_list; p->next; p = p->next) 10905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ; 10915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->last_proc = p; 10925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 109303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 10945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (held_sigchld) { 10955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru held_sigchld = 0; 10965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Don't call j_sigchld() as it may remove job... */ 10975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru kill(procpid, SIGCHLD); 10985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 109903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 11005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 11015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 11025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 11035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * wait for job to complete or change state 11045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 11055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 11065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 11075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int 11085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_waitj(Job *j, 110903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* see JW_* */ 111003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int flags, 11115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru const char *where) 11125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 1113b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes Proc *p; 111403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int rv; 1115c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND 1116c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser sigset_t omask; 1117c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif 11185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 11195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 11205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * No auto-notify on the job we are waiting on. 11215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 11225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_WAITING; 11235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (flags & JW_ASYNCNOTIFY) 11245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_W_ASYNCNOTIFY; 11255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 11265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 11275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!Flag(FMONITOR)) 11285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 11295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru flags |= JW_STOPPEDWAIT; 11305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 11315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru while (j->state == PRUNNING || 11325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ((flags & JW_STOPPEDWAIT) && j->state == PSTOPPED)) { 113303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 1134c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND 1135c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser sigprocmask(SIG_SETMASK, &sm_default, &omask); 1136c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser pause(); 1137c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser /* note that handlers may run here so they need to know */ 1138c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser sigprocmask(SIG_SETMASK, &omask, NULL); 1139c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#else 11405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sigsuspend(&sm_default); 1141c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif 114203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else 114303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j_sigchld(SIGCHLD); 114403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 11455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (fatal_trap) { 11465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int oldf = j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY); 11475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY); 11485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru runtraps(TF_FATAL); 114903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* not reached... */ 115003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->flags |= oldf; 11515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 11525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((flags & JW_INTERRUPT) && (rv = trap_pending())) { 11535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY); 11545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (-rv); 11555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 11565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 11575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY); 11585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 11595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->flags & JF_FG) { 11605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~JF_FG; 11615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 11625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR) && ttypgrp_ok && j->pgrp) { 11635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 11645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Save the tty's current pgrp so it can be restored 11655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * when the job is foregrounded. This is to 11665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * deal with things like the GNU su which does 11675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * a fork/exec instead of an exec (the fork means 11685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * the execed shell gets a different pid from its 11695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * pgrp, so naturally it sets its pgrp and gets hosed 11705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * when it gets foregrounded by the parent shell which 11715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * has restored the tty's pgrp to that of the su 11725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * process). 11735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 11745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PSTOPPED && 11755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (j->saved_ttypgrp = tcgetpgrp(tty_fd)) >= 0) 11765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_SAVEDTTYPGRP; 11775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (tcsetpgrp(tty_fd, kshpgrp) < 0) 117877740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(true, Tf_ldfailed, 117903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra "j_waitj:", "tcsetpgrp", tty_fd, 118077740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes (long)kshpgrp, cstrerror(errno)); 11815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PSTOPPED) { 11825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_SAVEDTTY; 1183c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_tcget(tty_fd, &j->ttystat); 11845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 11855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 11865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 1187c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser if (tty_hasstate) { 11885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 11895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Only restore tty settings if job was originally 11905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * started in the foreground. Problems can be 11915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * caused by things like 'more foobar &' which will 11925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * typically get and save the shell's vi/emacs tty 11935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * settings before setting up the tty for itself; 11945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * when more exits, it restores the 'original' 11955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * settings, and things go down hill from there... 11965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 11975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PEXITED && j->status == 0 && 11985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (j->flags & JF_USETTYMODE)) { 1199c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_tcget(tty_fd, &tty_state); 12005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else { 1201c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_tcset(tty_fd, &tty_state); 12025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /*- 12035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Don't use tty mode if job is stopped and 12045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * later restarted and exits. Consider 12055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * the sequence: 12065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * vi foo (stopped) 12075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * ... 12085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * stty something 12095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * ... 12105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * fg (vi; ZZ) 12115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * mode should be that of the stty, not what 12125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * was before the vi started. 12135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 12145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PSTOPPED) 12155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~JF_USETTYMODE; 12165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 12175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 12185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 12195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 12205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If it looks like user hit ^C to kill a job, pretend we got 12215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * one too to break out of for loops, etc. (AT&T ksh does this 12225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * even when not monitoring, but this doesn't make sense since 12235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * a tty generated ^C goes to the whole process group) 12245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 1225fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes if (Flag(FMONITOR) && j->state == PSIGNALLED && 1226fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes WIFSIGNALED(j->last_proc->status)) { 1227fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes int termsig; 1228fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes 1229fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes if ((termsig = WTERMSIG(j->last_proc->status)) > 0 && 1230fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes termsig < ksh_NSIG && 1231fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes (sigtraps[termsig].flags & TF_TTY_INTR)) 1232fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes trapsig(termsig); 12335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 12345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 12355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 12365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 12375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_usrtime = j->usrtime; 12385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_systime = j->systime; 12395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru rv = j->status; 12405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1241b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes if (!(p = j->proc_list)) { 1242b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes ; /* nothing */ 1243b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes } else if (flags & JW_PIPEST) { 124403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra uint32_t num = 0; 124503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra struct tbl *vp; 124603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 124703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra unset(vp_pipest, 1); 124803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp = vp_pipest; 124903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp->flag = DEFINED | ISSET | INTEGER | RDONLY | ARRAY | INT_U; 125003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra goto got_array; 125103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 125203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra while (p != NULL) { 125303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra { 125403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra struct tbl *vq; 125503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 125603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* strlen(vp_pipest->name) == 10 */ 125703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vq = alloc(offsetof(struct tbl, name[0]) + 11, 125803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp_pipest->areap); 125903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra memset(vq, 0, offsetof(struct tbl, name[0])); 126003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra memcpy(vq->name, vp_pipest->name, 11); 126103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp->u.array = vq; 126203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp = vq; 126303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra } 126403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp->areap = vp_pipest->areap; 126503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp->ua.index = ++num; 126603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp->flag = DEFINED | ISSET | INTEGER | RDONLY | 126703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra ARRAY | INT_U | AINDEX; 126803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra got_array: 126903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra vp->val.i = proc_errorlevel(p); 1270811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser if (Flag(FPIPEFAIL) && vp->val.i) 1271811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser rv = vp->val.i; 127203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra p = p->next; 127303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra } 1274b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes } else if (Flag(FPIPEFAIL)) { 1275b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes do { 127696b43632c2aa206ac1ec0eb70b34847d58d52633Elliott Hughes const int i = proc_errorlevel(p); 1277737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes 1278b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes if (i) 1279737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes rv = i; 1280b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes } while ((p = p->next)); 128103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra } 128203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra 12835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!(flags & JW_ASYNCNOTIFY) 12845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 12855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru && (!Flag(FMONITOR) || j->state != PSTOPPED) 12865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 12875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ) { 12885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_print(j, JP_SHORT, shl_out); 12895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_flush(shl_out); 12905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 12915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state != PSTOPPED 12925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 12935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru && (!Flag(FMONITOR) || !(flags & JW_ASYNCNOTIFY)) 12945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 12955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ) 12965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru remove_job(j, where); 12975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 12985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (rv); 12995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 13005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 13015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 13025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * SIGCHLD handler to reap children and update job states 13035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 13045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 13055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 13065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* ARGSUSED */ 13075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void 13085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_sigchld(int sig MKSH_A_UNUSED) 13095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 1310811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser int saved_errno = errno; 13115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Job *j; 13125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru Proc *p = NULL; 13135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru pid_t pid; 13145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int status; 13155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru struct rusage ru0, ru1; 1316c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND 1317c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser sigset_t omask; 1318c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser 1319c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser /* this handler can run while SIGCHLD is not blocked, so block it now */ 1320c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); 1321c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif 13225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 132303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 13245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 13255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Don't wait for any processes if a job is partially started. 13265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * This is so we don't do away with the process group leader 13275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * before all the processes in a pipe line are started (so the 13285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * setpgid() won't fail) 13295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 13305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j; j = j->next) 13315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->ppid == procpid && !(j->flags & JF_STARTED)) { 13325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru held_sigchld = 1; 1333c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser goto j_sigchld_out; 13345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 133503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 13365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 13375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru getrusage(RUSAGE_CHILDREN, &ru0); 13385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru do { 133903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 1340737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes pid = waitpid(-1, &status, (WNOHANG | 134156b517d46cdf4f6ccd6b62b207110e2afc3db30bElliott Hughes#if defined(WCONTINUED) && defined(WIFCONTINUED) 1342737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes WCONTINUED | 1343737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#endif 1344737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes WUNTRACED)); 134503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else 134603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra pid = wait(&status); 134703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 13485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 13495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 13505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * return if this would block (0) or no children 13515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * or interrupted (-1) 13525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 13535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (pid <= 0) 1354c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser goto j_sigchld_out; 13555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 13565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru getrusage(RUSAGE_CHILDREN, &ru1); 13575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 13585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* find job and process structures for this pid */ 13595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) 13605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p = j->proc_list; p != NULL; p = p->next) 13615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (p->pid == pid) 13625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru goto found; 13635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru found: 13645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j == NULL) { 13655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Can occur if process has kids, then execs shell 13665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru warningf(true, "bad process waited for (pid = %d)", 13675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru pid); 13685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 13695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ru0 = ru1; 13705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru continue; 13715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 13725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 13735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru timeradd(&j->usrtime, &ru1.ru_utime, &j->usrtime); 13745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru timersub(&j->usrtime, &ru0.ru_utime, &j->usrtime); 13755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru timeradd(&j->systime, &ru1.ru_stime, &j->systime); 13765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru timersub(&j->systime, &ru0.ru_stime, &j->systime); 13775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ru0 = ru1; 13785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->status = status; 13795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 13805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (WIFSTOPPED(status)) 13815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->state = PSTOPPED; 13825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 138356b517d46cdf4f6ccd6b62b207110e2afc3db30bElliott Hughes#if defined(WCONTINUED) && defined(WIFCONTINUED) 1384737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes if (WIFCONTINUED(status)) { 1385737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes p->state = j->state = PRUNNING; 1386737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes /* skip check_job(), no-op in this case */ 1387737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes continue; 1388737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes } else 1389737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#endif 13905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 13915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (WIFSIGNALED(status)) 13925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->state = PSIGNALLED; 13935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 13945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->state = PEXITED; 13955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 139603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* check to see if entire job is done */ 139703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra check_job(j); 139803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra } 139903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK 140003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra while (/* CONSTCOND */ 1); 140103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else 140203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra while (/* CONSTCOND */ 0); 140303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif 1404c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser 1405c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser j_sigchld_out: 1406c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND 1407c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser sigprocmask(SIG_SETMASK, &omask, NULL); 1408c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif 1409811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser errno = saved_errno; 14105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 14115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 14125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 14135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Called only when a process in j has exited/stopped (ie, called only 14145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * from j_sigchld()). If no processes are running, the job status 14155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * and state are updated, asynchronous job notification is done and, 14165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * if unneeded, the job is removed. 14175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 14185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 14195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 14205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void 14215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querucheck_job(Job *j) 14225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 142350012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes int jstate; 142450012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Proc *p; 14255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 14265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* XXX debugging (nasty - interrupt routine using shl_out) */ 14275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!(j->flags & JF_STARTED)) { 142877740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes internal_warningf("check_job: job started (flags 0x%X)", 142977740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes (unsigned int)j->flags); 14305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return; 14315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 14325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 14335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru jstate = PRUNNING; 14345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p=j->proc_list; p != NULL; p = p->next) { 14355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (p->state == PRUNNING) 143603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* some processes still running */ 143703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra return; 14385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (p->state > jstate) 14395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru jstate = p->state; 14405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 14415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->state = jstate; 144203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra j->status = proc_errorlevel(j->last_proc); 14435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 14445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 14455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Note when co-process dies: can't be done in j_wait() nor 14465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * remove_job() since neither may be called for non-interactive 14475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * shells. 14485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 14495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PEXITED || j->state == PSIGNALLED) { 14505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 14515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * No need to keep co-process input any more 14525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * (at least, this is what ksh93d thinks) 14535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 14545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (coproc.job == j) { 14555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru coproc.job = NULL; 14565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 14575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * XXX would be nice to get the closes out of here 14585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * so they aren't done in the signal handler. 14595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Would mean a check in coproc_getfd() to 14605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * do "if job == 0 && write >= 0, close write". 14615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 14625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru coproc_write_close(coproc.write); 14635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 14645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Do we need to keep the output? */ 14655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->coproc_id && j->coproc_id == coproc.id && 14665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru --coproc.njobs == 0) 14675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru coproc_readw_close(coproc.read); 14685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 14695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 14705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_CHANGED; 14715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 14725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FMONITOR) && !(j->flags & JF_XXCOM)) { 14735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 14745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Only put stopped jobs at the front to avoid confusing 14755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * the user (don't want finished jobs effecting %+ or %-) 14765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 14775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->state == PSTOPPED) 14785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru put_job(j, PJ_ON_FRONT); 14795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (Flag(FNOTIFY) && 14805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY)) != JF_WAITING) { 14815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Look for the real file descriptor 2 */ 14825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru { 14835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru struct env *ep; 14845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru int fd = 2; 14855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 14865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (ep = e; ep; ep = ep->oenv) 14875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ep->savefd && ep->savefd[2]) 14885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru fd = ep->savefd[2]; 14895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_reopen(fd, SHF_WR, shl_j); 14905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 14915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 14925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Can't call j_notify() as it removes jobs. The job 14935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * must stay in the job list as j_waitj() may be 14945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * running with this job. 14955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 14965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j_print(j, JP_MEDIUM, shl_j); 14975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_flush(shl_j); 14985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (!(j->flags & JF_WAITING) && j->state != PSTOPPED) 14995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru remove_job(j, "notify"); 15005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 15015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 15025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 15035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ( 15045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED 15055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru !Flag(FMONITOR) && 15065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 15075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru !(j->flags & (JF_WAITING|JF_FG)) && 15085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->state != PSTOPPED) { 15095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j == async_job || (j->flags & JF_KNOWN)) { 15105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags |= JF_ZOMBIE; 15115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->job = -1; 15125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru nzombie++; 15135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else 15145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru remove_job(j, "checkjob"); 15155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 15165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 15175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 15185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 15195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Print job status in either short, medium or long format. 15205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 15215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 15225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 15235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void 15245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_print(Job *j, int how, struct shf *shf) 15255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 152650012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Proc *p; 152750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes int state; 152850012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes int status; 1529fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes bool coredumped; 153050012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes char jobchar = ' '; 153150012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes char buf[64]; 15325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru const char *filler; 153350012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes int output = 0; 15345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 15355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (how == JP_PGRP) { 15365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 15375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * POSIX doesn't say what to do it there is no process 15385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * group leader (ie, !FMONITOR). We arbitrarily return 15395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * last pid (which is what $! returns). 15405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 154177740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes shf_fprintf(shf, Tf_dN, (int)(j->pgrp ? j->pgrp : 15425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru (j->last_proc ? j->last_proc->pid : 0))); 15435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return; 15445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 15455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->flags &= ~JF_CHANGED; 15465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru filler = j->job > 10 ? "\n " : "\n "; 15475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j == job_list) 15485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru jobchar = '+'; 15495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else if (j == job_list->next) 15505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru jobchar = '-'; 15515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 15525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p = j->proc_list; p != NULL;) { 1553fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes coredumped = false; 15545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru switch (p->state) { 15555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case PRUNNING: 15565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru memcpy(buf, "Running", 8); 15575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 1558fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes case PSTOPPED: { 1559fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes int stopsig = WSTOPSIG(p->status); 1560fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes 1561fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes strlcpy(buf, stopsig > 0 && stopsig < ksh_NSIG ? 1562fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes sigtraps[stopsig].mess : "Stopped", sizeof(buf)); 15635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 1564fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes } 1565fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes case PEXITED: { 1566fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes int exitstatus = (WEXITSTATUS(p->status)) & 255; 1567fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes 15685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (how == JP_SHORT) 15695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru buf[0] = '\0'; 1570fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes else if (exitstatus == 0) 15715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru memcpy(buf, "Done", 5); 15725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 15735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_snprintf(buf, sizeof(buf), "Done (%d)", 1574fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes exitstatus); 15755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 1576fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes } 1577fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes case PSIGNALLED: { 1578fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes int termsig = WTERMSIG(p->status); 15795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifdef WCOREDUMP 15805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (WCOREDUMP(p->status)) 1581fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes coredumped = true; 15825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif 15835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 15845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * kludge for not reporting 'normal termination 15855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * signals' (i.e. SIGINT, SIGPIPE) 15865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 15875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (how == JP_SHORT && !coredumped && 1588fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes (termsig == SIGINT || termsig == SIGPIPE)) { 15895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru buf[0] = '\0'; 15905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else 1591fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes strlcpy(buf, termsig > 0 && termsig < ksh_NSIG ? 1592fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes sigtraps[termsig].mess : "Signalled", 15935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru sizeof(buf)); 15945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 1595fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes } 1596c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser default: 1597c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser buf[0] = '\0'; 15985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 15995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (how != JP_SHORT) { 16015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (p == j->proc_list) 16025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_fprintf(shf, "[%d] %c ", j->job, jobchar); 16035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else 160403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra shf_puts(filler, shf); 16055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 16065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (how == JP_LONG) 16085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_fprintf(shf, "%5d ", (int)p->pid); 16095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (how == JP_SHORT) { 16115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (buf[0]) { 16125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru output = 1; 16135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_fprintf(shf, "%s%s ", 16145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru buf, coredumped ? " (core dumped)" : null); 16155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 16165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else { 16175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru output = 1; 16185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_fprintf(shf, "%-20s %s%s%s", buf, p->command, 16195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->next ? "|" : null, 16205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru coredumped ? " (core dumped)" : null); 16215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 16225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru state = p->state; 16245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru status = p->status; 16255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p = p->next; 16265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru while (p && p->state == state && p->status == status) { 16275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (how == JP_LONG) 16285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_fprintf(shf, "%s%5d %-20s %s%s", filler, 162977740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes (int)p->pid, T1space, p->command, 16305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->next ? "|" : null); 16315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru else if (how == JP_MEDIUM) 163277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes shf_fprintf(shf, Tf__ss, p->command, 16335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p->next ? "|" : null); 16345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p = p->next; 16355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 16365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 16375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (output) 16385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru shf_putc('\n', shf); 16395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 16405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 16425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Convert % sequence to job 16435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 16445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 16455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 16465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job * 16475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_lookup(const char *cp, int *ecodep) 16485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 164903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Job *j, *last_match; 165003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra Proc *p; 165103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra size_t len; 165203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra int job = 0; 16535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 1654b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes if (ksh_isdigit(*cp) && getn(cp, &job)) { 16555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Look for last_proc->pid (what $! returns) first... */ 16565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) 16575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->last_proc && j->last_proc->pid == job) 16585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (j); 16595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* 16605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * ...then look for process group (this is non-POSIX, 16615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * but should not break anything 16625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 16635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) 16645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->pgrp && j->pgrp == job) 16655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (j); 1666b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes goto j_lookup_nosuch; 16675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 16685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (*cp != '%') { 1669b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes j_lookup_invalid: 16705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ecodep) 16715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *ecodep = JL_INVALID; 16725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (NULL); 16735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 16745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru switch (*++cp) { 16755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case '\0': /* non-standard */ 16765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case '+': 16775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case '%': 16785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (job_list != NULL) 16795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (job_list); 16805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 16815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case '-': 16835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (job_list != NULL && job_list->next) 16845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (job_list->next); 16855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 16865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 16875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case '0': case '1': case '2': case '3': case '4': 16885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case '5': case '6': case '7': case '8': case '9': 1689b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes if (!getn(cp, &job)) 1690b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes goto j_lookup_invalid; 16915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) 16925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j->job == job) 16935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (j); 16945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 16955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 169603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* %?string */ 169703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra case '?': 16985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru last_match = NULL; 16995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) 17005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p = j->proc_list; p != NULL; p = p->next) 17015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (strstr(p->command, cp+1) != NULL) { 17025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (last_match) { 17035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ecodep) 17045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *ecodep = JL_AMBIG; 17055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (NULL); 17065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 17075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru last_match = j; 17085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 17095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (last_match) 17105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (last_match); 17115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 17125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 171303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra /* %string */ 171403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra default: 17155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru len = strlen(cp); 17165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru last_match = NULL; 17175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j != NULL; j = j->next) 17185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (strncmp(cp, j->proc_list->command, len) == 0) { 17195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (last_match) { 17205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ecodep) 17215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *ecodep = JL_AMBIG; 17225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (NULL); 17235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 17245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru last_match = j; 17255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 17265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (last_match) 17275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (last_match); 17285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 17295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 1730b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes j_lookup_nosuch: 17315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (ecodep) 17325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *ecodep = JL_NOSUCH; 17335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (NULL); 17345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 17355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *free_jobs; 17375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Proc *free_procs; 17385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 17405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * allocate a new job and fill in the job number. 17415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 17425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 17435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 17445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job * 17455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querunew_job(void) 17465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 174750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes int i; 174850012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Job *newj, *j; 17495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (free_jobs != NULL) { 17515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru newj = free_jobs; 17525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru free_jobs = free_jobs->next; 17535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else 17545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru newj = alloc(sizeof(Job), APERM); 17555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* brute force method */ 17575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (i = 1; ; i++) { 17585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (j = job_list; j && j->job != i; j = j->next) 17595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ; 17605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j == NULL) 17615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 17625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 17635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru newj->job = i; 17645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (newj); 17665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 17675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 17695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Allocate new process struct 17705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 17715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 17725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 17735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Proc * 17745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querunew_proc(void) 17755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 177650012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Proc *p; 17775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (free_procs != NULL) { 17795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p = free_procs; 17805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru free_procs = free_procs->next; 17815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } else 17825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p = alloc(sizeof(Proc), APERM); 17835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (p); 17855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 17865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 17885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Take job out of job_list and put old structures into free list. 17895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Keeps nzombies, last_job and async_job up to date. 17905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 17915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 17925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 17935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void 17945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruremove_job(Job *j, const char *where) 17955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 179650012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Proc *p, *tmp; 179750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Job **prev, *curr; 17985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 17995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru prev = &job_list; 1800c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser curr = job_list; 1801c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser while (curr && curr != j) { 1802c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser prev = &curr->next; 1803c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser curr = *prev; 1804c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser } 18055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (curr != j) { 180677740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes internal_warningf("remove_job: job %s (%s)", Tnot_found, where); 18075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return; 18085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 18095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *prev = curr->next; 18105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* free up proc structures */ 18125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p = j->proc_list; p != NULL; ) { 18135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru tmp = p; 18145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru p = p->next; 18155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru tmp->next = free_procs; 18165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru free_procs = tmp; 18175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 18185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if ((j->flags & JF_ZOMBIE) && j->ppid == procpid) 18205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru --nzombie; 18215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->next = free_jobs; 18225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru free_jobs = j; 18235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j == last_job) 18255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru last_job = NULL; 18265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (j == async_job) 18275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru async_job = NULL; 18285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 18295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 18315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * put j in a particular location (taking it out job_list if it is there 18325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * already) 18335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 18345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 18355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 18365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void 18375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruput_job(Job *j, int where) 18385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 183950012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Job **prev, *curr; 18405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru /* Remove job from list (if there) */ 18425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru prev = &job_list; 18435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru curr = job_list; 1844c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser while (curr && curr != j) { 1845c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser prev = &curr->next; 1846c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser curr = *prev; 1847c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser } 18485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (curr == j) 18495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *prev = curr->next; 18505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru switch (where) { 18525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case PJ_ON_FRONT: 18535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->next = job_list; 18545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru job_list = j; 18555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 18565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru case PJ_PAST_STOPPED: 18585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru prev = &job_list; 18595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru curr = job_list; 18605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (; curr && curr->state == PSTOPPED; prev = &curr->next, 18615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru curr = *prev) 18625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru ; 18635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru j->next = curr; 18645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *prev = j; 18655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru break; 18665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru } 18675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 18685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* 18705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * nuke a job (called when unable to start full job). 18715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * 18725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked. 18735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */ 18745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int 18755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querukill_job(Job *j, int sig) 18765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{ 187750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes Proc *p; 187850012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes int rval = 0; 18795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru 18805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru for (p = j->proc_list; p != NULL; p = p->next) 18815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (p->pid != 0) 18825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru if (kill(p->pid, sig) < 0) 18835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru rval = -1; 18845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru return (rval); 18855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru} 1886c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser 1887c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void 1888c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glasertty_init_talking(void) 1889c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser{ 1890c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser switch (tty_init_fd()) { 1891c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser case 0: 1892c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser break; 1893c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser case 1: 1894c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifndef MKSH_DISABLE_TTY_WARNING 189577740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_sD_s_sD_s, 189677740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes "No controlling tty", Topen, T_devtty, cstrerror(errno)); 1897c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif 1898c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser break; 1899c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser case 2: 1900c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifndef MKSH_DISABLE_TTY_WARNING 190177740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_sD_s_s, Tcant_find, Ttty_fd, 190277740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes cstrerror(errno)); 1903c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif 1904c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser break; 1905c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser case 3: 190677740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_ssfaileds, "j_ttyinit", 190777740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes Ttty_fd_dupof, cstrerror(errno)); 1908c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser break; 1909c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser case 4: 191077740fcd3dfcd2a78a8ad0ea0f0314dd6b23ecb6Elliott Hughes warningf(false, Tf_sD_sD_s, "j_ttyinit", 1911c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser "can't set close-on-exec flag", cstrerror(errno)); 1912c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser break; 1913c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser } 1914c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser} 1915c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser 1916c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void 1917c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glasertty_init_state(void) 1918c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser{ 1919c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser if (tty_fd >= 0) { 1920c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser mksh_tcget(tty_fd, &tty_state); 1921c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser tty_hasstate = true; 1922c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser } 1923c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser} 1924