From 7af1d56e7f6437a80dc36666bfa829b7b7b5fad9 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Wed, 15 Jun 2022 01:29:04 +0100 Subject: [PATCH] cleanup: libast: remove pre-fork(2) code (re: 7b0e0776) I had removed the legacy code for systems without fork(2) from ksh93, but not from libast. This commit deos that. --- src/lib/libast/comp/conf.tab | 9 +- src/lib/libast/comp/setsid.c | 20 +---- src/lib/libast/comp/spawnveg.c | 23 ++---- src/lib/libast/comp/vfork.c | 7 +- src/lib/libast/include/ast_std.h | 3 + src/lib/libast/man/proc.3 | 13 +-- src/lib/libast/misc/procopen.c | 137 ++++++++++++------------------- 7 files changed, 71 insertions(+), 141 deletions(-) diff --git a/src/lib/libast/comp/conf.tab b/src/lib/libast/comp/conf.tab index 8c68faaa7..4e970281a 100644 --- a/src/lib/libast/comp/conf.tab +++ b/src/lib/libast/comp/conf.tab @@ -65,18 +65,11 @@ CHAR_MAX C XX 1 L CHAR_MIN C XX 1 L CHAR_TERM POSIX SC 2 FUW CHILD_MAX POSIX SC 1 CDLMUX 6 cc{ - #if _lib_fork - #define child() fork() - #else - extern int spawnve(const char*, char* const[], char* const[]); - static char* cmd[] = { "/bin/echo", 0 }; - #define child() spawnve(cmd[0],cmd,(char**)0) - #endif int main() { int i; int n; - for (i = 3; i < 512 && (n = child()) >= 0; i++) + for (i = 3; i < 512 && (n = fork()) >= 0; i++) if (!n) _exit(0); while (wait((int*)0) > 0); for (n = 8; n < i; n <<= 1); diff --git a/src/lib/libast/comp/setsid.c b/src/lib/libast/comp/setsid.c index 23fff5150..b4a90a8f3 100644 --- a/src/lib/libast/comp/setsid.c +++ b/src/lib/libast/comp/setsid.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2021 Contributors to ksh 93u+m * +* Copyright (c) 2020-2022 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 1.0 * * by AT&T Intellectual Property * @@ -65,24 +65,6 @@ setsid(void) ioctl(fd, TIOCNOTTY, 0); close(fd); } -#else - - /* - * second child in s5 to avoid reacquiring the control tty - */ - -#if _lib_fork && HUH920711 /* some s5's botch this */ - switch (fork()) - { - case -1: - exit(1); - case 0: - break; - default: - exit(0); - } -#endif - #endif return(pg); } diff --git a/src/lib/libast/comp/spawnveg.c b/src/lib/libast/comp/spawnveg.c index 96e29f7f9..d8355aa9b 100644 --- a/src/lib/libast/comp/spawnveg.c +++ b/src/lib/libast/comp/spawnveg.c @@ -174,7 +174,6 @@ spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, i pid_t spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, int tcfd) { -#if _lib_fork || _lib_vfork int n; int m; pid_t pid; @@ -184,19 +183,15 @@ spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, i volatile int* volatile exec_errno_ptr; #else int err[2]; -#endif -#endif +#endif /* _real_vfork */ NOT_USED(tcfd); if (!envv) envv = environ; #if _lib_spawnve -#if _lib_fork || _lib_vfork if (!pgid) -#endif return spawnve(path, argv, envv); -#endif -#if _lib_fork || _lib_vfork +#endif /* _lib_spawnve */ n = errno; #if _real_vfork exec_errno = 0; @@ -209,13 +204,13 @@ spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, i fcntl(err[0], F_SETFD, FD_CLOEXEC); fcntl(err[1], F_SETFD, FD_CLOEXEC); } -#endif +#endif /* _real_vfork */ sigcritical(SIG_REG_EXEC|SIG_REG_PROC); #if _lib_vfork pid = vfork(); #else pid = fork(); -#endif +#endif /* _lib_vfork */ if (pid == -1) n = errno; else if (!pid) @@ -236,7 +231,7 @@ spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, i #elif defined(TIOCSPGRP) if (m) ioctl(2, TIOCSPGRP, &pgid); -#endif +#endif /* _lib_tcgetpgrp */ } execve(path, argv, envv); #if _real_vfork @@ -247,7 +242,7 @@ spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, i m = errno; write(err[1], &m, sizeof(m)); } -#endif +#endif /* _real_vfork */ _exit(errno == ENOENT ? EXIT_NOTFOUND : EXIT_NOEXEC); } rid = pid; @@ -280,7 +275,7 @@ spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, i } close(err[0]); } -#endif +#endif /* _real_vfork */ sigcritical(0); if (pid != -1 && pgid > 0) { @@ -295,10 +290,6 @@ spawnveg(const char* path, char* const argv[], char* const envv[], pid_t pgid, i } errno = n; return rid; -#else - errno = ENOSYS; - return -1; -#endif } #endif diff --git a/src/lib/libast/comp/vfork.c b/src/lib/libast/comp/vfork.c index b05b5336a..58f637e92 100644 --- a/src/lib/libast/comp/vfork.c +++ b/src/lib/libast/comp/vfork.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2011 AT&T Intellectual Property * -* Copyright (c) 2020-2021 Contributors to ksh 93u+m * +* Copyright (c) 2020-2022 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 1.0 * * by AT&T Intellectual Property * @@ -40,12 +40,7 @@ NoN(vfork) extern pid_t vfork(void) { -#if _lib_fork return(fork()); -#else - errno = ENOSYS; - return -1; -#endif } #endif diff --git a/src/lib/libast/include/ast_std.h b/src/lib/libast/include/ast_std.h index 73a07ec4c..168416490 100644 --- a/src/lib/libast/include/ast_std.h +++ b/src/lib/libast/include/ast_std.h @@ -64,6 +64,9 @@ struct _sfio_s; #endif #include +#if !_lib_fork +#error In 2022, libast joined the 21st century and started requiring fork(2). +#endif #include #include /* does this */ #include diff --git a/src/lib/libast/man/proc.3 b/src/lib/libast/man/proc.3 index a8e31e083..a338130b2 100644 --- a/src/lib/libast/man/proc.3 +++ b/src/lib/libast/man/proc.3 @@ -38,7 +38,7 @@ .. .TH PROC 3 .SH NAME -proc \- process control routines +proc \- process control routines (deprecated) .SH SYNOPSIS .EX #include @@ -50,14 +50,15 @@ int procrun(const char* \fIcommand\fP, char** \fIargv\fP); .EE .SH DESCRIPTION These routines provide a portable interface to process creation and execution. -They work on systems with -.IR fork (2) -and -.IR exec (2) -as well as on systems with only +They used to work on systems with only .IR spawnve (2) or .IR spawnveg (3). +Now that libast has joined the 21st century and requires +.IR fork (2) +and +.IR execve (2), +this interface is deprecated and only kept for backward compatibility. .PP .L procopen runs diff --git a/src/lib/libast/misc/procopen.c b/src/lib/libast/misc/procopen.c index 175c9d579..4e46f5458 100644 --- a/src/lib/libast/misc/procopen.c +++ b/src/lib/libast/misc/procopen.c @@ -40,19 +40,11 @@ * not quite ready for _use_spawnveg */ -#if _use_spawnveg -#if _lib_fork #undef _use_spawnveg -#else -#if _WINIX -#define _lib_fork 1 -#endif -#endif -#endif #ifndef DEBUG_PROC #define DEBUG_PROC 1 -#endif +#endif /* DEBUG_PROC */ #if _lib_socketpair #if _sys_socket @@ -60,8 +52,8 @@ #include #else #undef _lib_socketpair -#endif -#endif +#endif /* _sys_socket */ +#endif /* _lib_socketpair */ Proc_t proc_default = { -1 }; @@ -104,7 +96,7 @@ setopt(register void* a, register const void* p, register int n, const char* v) return 0; } -#endif +#endif /* DEBUG_PROC */ #if _use_spawnveg @@ -135,7 +127,7 @@ typedef struct Mod_s } Modify_t; -#endif +#endif /* _use_spawnveg */ #ifdef SIGPIPE @@ -150,7 +142,7 @@ ignoresig(int sig) signal(sig, ignoresig); } -#endif +#endif /* SIGPIPE */ /* * do modification op and save previous state for restore() @@ -159,13 +151,12 @@ ignoresig(int sig) static int modify(Proc_t* proc, int forked, int op, long arg1, long arg2) { -#if _lib_fork if (forked) { int i; #ifndef TIOCSCTTY char* s; -#endif +#endif /* !TIOCSCTTY */ switch (op) { @@ -199,7 +190,7 @@ modify(Proc_t* proc, int forked, int op, long arg1, long arg2) return -1; if ((arg2 = open(s, O_RDWR)) < 0) return -1; -#endif +#endif /* TIOCSCTTY */ for (i = 0; i <= 2; i++) if (arg1 != i && arg2 != i && fcntl(arg1, F_DUPFD, i) != i) return -1; @@ -234,11 +225,6 @@ modify(Proc_t* proc, int forked, int op, long arg1, long arg2) } #if _use_spawnveg else -#endif -#else - NoP(forked); -#endif -#if _use_spawnveg { register Modify_t* m; @@ -266,7 +252,7 @@ modify(Proc_t* proc, int forked, int op, long arg1, long arg2) } #if F_dupfd_cloexec == F_DUPFD fcntl(m->save, F_SETFD, FD_CLOEXEC); -#endif +#endif /* F_dupfd_cloexec == F_DUPFD */ close(arg2); if (fcntl(arg1, F_DUPFD, arg2) != arg2) return -1; @@ -313,7 +299,7 @@ modify(Proc_t* proc, int forked, int op, long arg1, long arg2) } #else NoP(proc); -#endif +#endif /* _use_spawnveg */ return 0; } @@ -383,7 +369,7 @@ restore(Proc_t* proc) #define restore(p) -#endif +#endif /* _use_spawnveg */ /* * fork and exec or spawn proc(argv) and return a Proc_t handle @@ -410,38 +396,30 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) char path[PATH_MAX]; char env[PATH_MAX + 2]; int pio[2]; -#if _lib_fork int pop[2]; -#endif #if !_pipe_rw && !_lib_socketpair int poi[2]; -#endif +#endif /* !_pipe_rw && !_lib_socketpair */ #if defined(SIGCHLD) && ( _lib_sigprocmask || _lib_sigsetmask ) Sig_mask_t mask; -#endif +#endif /* defined(SIGCHLD) && ( _lib_sigprocmask || _lib_sigsetmask ) */ #if _use_spawnveg int newenv = 0; -#endif +#endif /* _use_spawnveg */ #if DEBUG_PROC int debug = PROC_OPT_EXEC; -#endif +#endif /* DEBUG_PROC */ -#if _lib_fork if (!argv && (flags & (PROC_ORPHAN|PROC_OVERLAY))) -#else - if (!argv || (flags & PROC_ORPHAN)) -#endif { errno = ENOEXEC; return 0; } pio[0] = pio[1] = -1; -#if _lib_fork pop[0] = pop[1] = -1; -#endif #if !_pipe_rw && !_lib_socketpair poi[0] = poi[1] = -1; -#endif +#endif /* !_pipe_rw && !_lib_socketpair */ if (cmd && (!*cmd || !pathpath(cmd, NiL, PATH_REGULAR|PATH_EXECUTE, path, sizeof(path)))) goto bad; switch (flags & (PROC_READ|PROC_WRITE)) @@ -476,7 +454,7 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) #if _use_spawnveg if (!(flags & PROC_ORPHAN)) newenv = 1; -#endif +#endif /* _use_spawnveg */ } if (procfd >= 0) { @@ -492,11 +470,11 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) #else if (pipe(pio) || pipe(poi)) goto bad; -#endif +#endif /* _lib_socketpair */ } else if (pipe(pio)) goto bad; -#endif +#endif /* _pipe_rw */ } if (flags & PROC_OVERLAY) { @@ -506,8 +484,7 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) #if _use_spawnveg else if (argv && !(flags & PROC_ORPHAN)) proc->pid = 0; -#endif -#if _lib_fork +#endif /* _use_spawnveg */ else { if (!(flags & PROC_FOREGROUND)) @@ -528,9 +505,9 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) proc->mask = sigblock(mask); #else proc->sigchld = signal(SIGCHLD, SIG_DFL); -#endif -#endif -#endif +#endif /* _lib_sigsetmask */ +#endif /* _lib_sigprocmask */ +#endif /* defined(SIGCHLD) */ } if ((flags & PROC_ORPHAN) && pipe(pop)) goto bad; @@ -558,15 +535,14 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) #else if (proc->sigchld != SIG_IGN) signal(SIGCHLD, SIG_DFL); -#endif -#endif -#endif +#endif /* _lib_sigsetmask */ +#endif /* _lib_sigprocmask */ +#endif /* defined(SIGCHLD) */ } else if (proc->pid == -1) goto bad; forked = 1; } -#endif if (!proc->pid) { #if _use_spawnveg @@ -574,8 +550,7 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) char* oenviron0 = 0; v = 0; -#endif -#if _lib_fork +#endif /* _use_spawnveg */ if (flags & PROC_ORPHAN) { if (!(proc->pid = fork())) @@ -590,10 +565,8 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) _exit(EXIT_NOEXEC); } } -#endif #if DEBUG_PROC stropt(getenv(PROC_ENV_OPTIONS), options, sizeof(*options), setopt, &debug); -#if _lib_fork if (debug & PROC_OPT_TRACE) { if (!fork()) @@ -604,30 +577,29 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) } sleep(2); } -#endif -#endif +#endif /* DEBUG_PROC */ if (flags & PROC_DAEMON) { #ifdef SIGHUP modify(proc, forked, PROC_sig_ign, SIGHUP, 0); -#endif +#endif /* SIGHUP */ modify(proc, forked, PROC_sig_dfl, SIGTERM, 0); #ifdef SIGTSTP modify(proc, forked, PROC_sig_ign, SIGTSTP, 0); -#endif +#endif /* SIGTSTP */ #ifdef SIGTTIN modify(proc, forked, PROC_sig_ign, SIGTTIN, 0); -#endif +#endif /* SIGTTIN */ #ifdef SIGTTOU modify(proc, forked, PROC_sig_ign, SIGTTOU, 0); -#endif +#endif /* SIGTTOU */ } if (flags & (PROC_BACKGROUND|PROC_DAEMON)) { modify(proc, forked, PROC_sig_ign, SIGINT, 0); #ifdef SIGQUIT modify(proc, forked, PROC_sig_ign, SIGQUIT, 0); -#endif +#endif /* SIGQUIT */ } if (flags & (PROC_DAEMON|PROC_SESSION)) modify(proc, forked, PROC_sys_pgrp, -1, 0); @@ -657,7 +629,7 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) goto cleanup; if (poi[1] != 0 && modify(proc, forked, PROC_fd_dup|PROC_FD_CHILD, poi[1], PROC_ARG_NULL)) goto cleanup; -#endif +#endif /* _pipe_rw || _lib_socketpair */ } else if (procfd >= 0) { @@ -682,28 +654,23 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) goto cleanup; break; } -#if _lib_fork if (forked && (flags & PROC_ENVCLEAR)) environ = 0; #if _use_spawnveg - else -#endif -#endif -#if _use_spawnveg - if (newenv) + else if (newenv) { p = environ; while (*p++); if (!(oenviron = (char**)memdup(environ, (p - environ) * sizeof(char*)))) goto cleanup; } -#endif +#endif /* _use_spawnveg */ if (argv && envv != (char**)environ) { #if _use_spawnveg if (!newenv && environ[0][0] == '_' && environ[0][1] == '=') oenviron0 = environ[0]; -#endif +#endif /* _use_spawnveg */ env[0] = '_'; env[1] = '='; env[2] = 0; @@ -717,10 +684,8 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) if (!setenviron(*p++)) goto cleanup; p = argv; -#if _lib_fork if (forked && !p) return proc; -#endif #if DEBUG_PROC if (!(debug & PROC_OPT_EXEC) || (debug & PROC_OPT_VERBOSE)) { @@ -732,12 +697,12 @@ procopen(const char* cmd, char** argv, char** envv, long* modv, int flags) while (*++p) sfprintf(sfstderr, " %s", *p); sfprintf(sfstderr, "\n"); -sfsync(sfstderr); + sfsync(sfstderr); if (!(debug & PROC_OPT_EXEC)) _exit(0); p = argv; } -#endif +#endif /* DEBUG_PROC */ if (cmd) { strcpy(env + 2, path); @@ -746,7 +711,7 @@ sfsync(sfstderr); #if _use_spawnveg else if ((proc->pid = spawnveg(path, p, environ, proc->pgrp, -1)) != -1) goto cleanup; -#endif +#endif /* _use_spawnveg */ if (errno != ENOEXEC) goto cleanup; @@ -774,7 +739,7 @@ sfsync(sfstderr); #if _use_spawnveg else proc->pid = spawnveg(env + 2, p, environ, proc->pgrp, -1); -#endif +#endif /* _use_spawnveg */ cleanup: if (forked) { @@ -798,7 +763,7 @@ sfsync(sfstderr); restore(proc); if (flags & PROC_OVERLAY) exit(0); -#endif +#endif /* _use_spawnveg */ } if (proc->pid != -1) { @@ -820,9 +785,9 @@ sfsync(sfstderr); proc->mask = sigblock(mask); #else proc->sigchld = signal(SIGCHLD, SIG_DFL); -#endif -#endif -#endif +#endif /* _lib_sigsetmask */ +#endif /* _lib_sigprocmask */ +#endif /* defined(SIGCHLD) */ } } else if (modv) @@ -855,7 +820,7 @@ sfsync(sfstderr); if ((handler = signal(SIGPIPE, ignoresig)) != SIG_DFL && handler != ignoresig) signal(SIGPIPE, handler); } -#endif +#endif /* SIGPIPE */ switch (procfd) { case 0: @@ -868,7 +833,7 @@ sfsync(sfstderr); #else proc->wfd = poi[1]; close(poi[0]); -#endif +#endif /* _pipe_rw || _lib_socketpair */ /* FALLTHROUGH */ case 1: proc->rfd = pio[0]; @@ -907,9 +872,9 @@ sfsync(sfstderr); #else if (proc->sigchld != SIG_DFL) signal(SIGCHLD, proc->sigchld); -#endif -#endif -#endif +#endif /* _lib_sigsetmask */ +#endif /* _lib_sigprocmask */ +#endif /* defined(SIGCHLD) */ } if ((flags & PROC_CLEANUP) && modv) for (i = 0; n = modv[i]; i++) @@ -936,7 +901,7 @@ sfsync(sfstderr); close(poi[0]); if (poi[1] >= 0) close(poi[1]); -#endif +#endif /* !_pipe_rw && !_lib_socketpair */ procfree(proc); return 0; }