diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-09-11 21:23:56 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-09-11 21:23:56 +0000 |
commit | d229a8b84880dddc05267798795ea29c50af28bd (patch) | |
tree | 71ca36220885e52598db48f1e9b08799db53427c /src | |
parent | 421d1567d153f3dd43b618a23ca5be5d71472a9d (diff) | |
download | execline-d229a8b84880dddc05267798795ea29c50af28bd.tar.xz |
Defork execline
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/execline/background.c | 30 | ||||
-rw-r--r-- | src/execline/pipeline.c | 69 | ||||
-rw-r--r-- | src/libexecline/deps-lib/execline | 3 | ||||
-rw-r--r-- | src/libexecline/el_gspawn0.c | 16 | ||||
-rw-r--r-- | src/libexecline/el_spawn0.c | 2 | ||||
-rw-r--r-- | src/libexecline/el_spawn1.c | 16 |
6 files changed, 57 insertions, 79 deletions
diff --git a/src/execline/background.c b/src/execline/background.c index 7fefa1a..189e5c1 100644 --- a/src/execline/background.c +++ b/src/execline/background.c @@ -4,6 +4,7 @@ #include <skalibs/types.h> #include <skalibs/sgetopt.h> #include <skalibs/strerr.h> +#include <skalibs/cspawn.h> #include <skalibs/exec.h> #include <execline/execline.h> @@ -15,6 +16,9 @@ int main (int argc, char const **argv, char const *const *envp) pid_t pid ; int argc1 ; int df = 0 ; + size_t i = 2 ; + char fmt[PID_FMT + 2] = "!=" ; + PROG = "background" ; { subgetopt l = SUBGETOPT_ZERO ; @@ -36,27 +40,9 @@ int main (int argc, char const **argv, char const *const *envp) if (argc1 + 1 == argc) df = 0 ; argv[argc1] = 0 ; - if (df) - { - pid = doublefork() ; - switch (pid) - { - case -1: strerr_diefu1sys(111, "doublefork") ; - case 0: - PROG = "background (grandchild)" ; - xexec0_e(argv, envp) ; - } - } - else - { - pid = el_spawn0(argv[0], argv, envp) ; - if (!pid) strerr_diefu2sys(111, "spawn ", argv[0]) ; - } + pid = df ? gcspawn(argv[0], argv, envp, 0, 0, 0) : cspawn(argv[0], argv, envp, 0, 0, 0) ; + if (!pid) strerr_diefu2sys(111, "spawn ", argv[0]) ; if (argc1 + 1 == argc) return 0 ; - { - char fmt[PID_FMT + 2] = "!=" ; - size_t i = 2 ; - i += pid_fmt(fmt+i, pid) ; fmt[i++] = 0 ; - xmexec_en(argv + argc1 + 1, envp, fmt, i, 1) ; - } + i += pid_fmt(fmt+i, pid) ; fmt[i++] = 0 ; + xmexec_en(argv + argc1 + 1, envp, fmt, i, 1) ; } diff --git a/src/execline/pipeline.c b/src/execline/pipeline.c index 00f4538..c68b912 100644 --- a/src/execline/pipeline.c +++ b/src/execline/pipeline.c @@ -6,8 +6,9 @@ #include <skalibs/sgetopt.h> #include <skalibs/types.h> #include <skalibs/strerr.h> -#include <skalibs/exec.h> +#include <skalibs/cspawn.h> #include <skalibs/djbunix.h> +#include <skalibs/exec.h> #include <execline/execline.h> @@ -16,7 +17,18 @@ int main (int argc, char const **argv, char const *const *envp) { + cspawn_fileaction fa[2] = + { + [0] = { .type = CSPAWN_FA_CLOSE }, + [1] = { .type = CSPAWN_FA_MOVE } + } ; + pid_t pid ; + int p[2] ; + int argc1 ; int df = 0, w = 0 ; + size_t i = 2 ; + char fmt[PID_FMT + 2] = "!=" ; + PROG = "pipeline" ; { subgetopt l = SUBGETOPT_ZERO ; @@ -34,42 +46,21 @@ int main (int argc, char const **argv, char const *const *envp) } argc -= l.ind ; argv += l.ind ; } - { - pid_t pid ; - int fd ; - int argc1 = el_semicolon(argv) ; - if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ; - if (argc1 + 1 == argc) strerr_dief1x(100, "empty remainder") ; - argv[argc1] = 0 ; - if (df) - { - int p[2] ; - if (pipe(p) < 0) strerr_diefu1sys(111, "create pipe") ; - pid = doublefork() ; - switch (pid) - { - case -1: strerr_diefu1sys(111, "doublefork") ; - case 0: - PROG = "pipeline (grandchild)" ; - fd_close(p[w]) ; - if (fd_move(!w, p[!w]) < 0) strerr_diefu1sys(111, "fd_move") ; - xexec0_e(argv, envp) ; - } - fd_close(p[!w]) ; - fd = p[w] ; - } - else - { - pid = el_spawn1(argv[0], argv, envp, &fd, !w) ; - if (!pid) strerr_diefu2sys(111, "spawn ", argv[0]) ; - } - if (fd_move(w, fd) < 0) strerr_diefu1sys(111, "fd_move") ; - if (w == fd) uncoe(fd) ; - { - char fmt[PID_FMT + 2] = "!=" ; - size_t i = 2 ; - i += pid_fmt(fmt+i, pid) ; fmt[i++] = 0 ; - xmexec_en(argv + argc1 + 1, envp, fmt, i, 1) ; - } - } + + argc1 = el_semicolon(argv) ; + if (!argc1) strerr_dief1x(100, "empty block") ; + if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ; + if (argc1 + 1 == argc) strerr_dief1x(100, "empty remainder") ; + argv[argc1] = 0 ; + if (pipe(p) == -1) strerr_diefu1sys(111, "create pipe") ; + fa[0].x.fd = p[w] ; + fa[1].x.fd2[0] = !w ; + fa[1].x.fd2[1] = p[!w] ; + pid = df ? gcspawn(argv[0], argv, envp, 0, fa, 2) : cspawn(argv[0], argv, envp, 0, fa, 2) ; + if (!pid) strerr_diefu2sys(111, "spawn ", argv[0]) ; + fd_close(p[!w]) ; + if (fd_move(w, p[w]) == -1) strerr_diefu1sys(111, "fd_move") ; + if (w == p[w]) uncoe(w) ; + i += pid_fmt(fmt+i, pid) ; fmt[i++] = 0 ; + xmexec_en(argv + argc1 + 1, envp, fmt, i, 1) ; } diff --git a/src/libexecline/deps-lib/execline b/src/libexecline/deps-lib/execline index e896f2f..a1f7c74 100644 --- a/src/libexecline/deps-lib/execline +++ b/src/libexecline/deps-lib/execline @@ -9,7 +9,7 @@ el_popenv.o el_pushenv.o el_semicolon.o el_spawn0.o -el_spawn1.o +el_gspawn0.o el_substandrun.o el_substandrun_str.o el_substitute.o @@ -24,3 +24,4 @@ exlsn_main.o exlsn_free.o exlp.o -lskarnet +${SPAWN_LIB} diff --git a/src/libexecline/el_gspawn0.c b/src/libexecline/el_gspawn0.c new file mode 100644 index 0000000..d6e0d3a --- /dev/null +++ b/src/libexecline/el_gspawn0.c @@ -0,0 +1,16 @@ +/* ISC license. */ + +#include <skalibs/cspawn.h> + +#include <execline/config.h> +#include <execline/execline.h> + +pid_t el_gspawn0 (char const *prog, char const *const *argv, char const *const *envp) +{ + if (!argv[0]) + { + static char const *const newargv[3] = { EXECLINE_BINPREFIX "exit", "0", 0 } ; + return gcspawn(newargv[0], newargv, envp, 0, 0, 0) ; + } + else return gcspawn(prog, argv, envp, 0, 0, 0) ; +} diff --git a/src/libexecline/el_spawn0.c b/src/libexecline/el_spawn0.c index d4265a4..d397557 100644 --- a/src/libexecline/el_spawn0.c +++ b/src/libexecline/el_spawn0.c @@ -12,5 +12,5 @@ pid_t el_spawn0 (char const *prog, char const *const *argv, char const *const *e static char const *const newargv[3] = { EXECLINE_BINPREFIX "exit", "0", 0 } ; return cspawn(newargv[0], newargv, envp, 0, 0, 0) ; } - else return cspawn(prog, argv, envp, 0, 0, 00) ; + else return cspawn(prog, argv, envp, 0, 0, 0) ; } diff --git a/src/libexecline/el_spawn1.c b/src/libexecline/el_spawn1.c deleted file mode 100644 index d9a7f87..0000000 --- a/src/libexecline/el_spawn1.c +++ /dev/null @@ -1,16 +0,0 @@ -/* ISC license. */ - -#include <skalibs/cspawn.h> - -#include <execline/config.h> -#include <execline/execline.h> - -pid_t el_spawn1 (char const *prog, char const *const *argv, char const *const *envp, int *fd, int w) -{ - if (!argv[0]) - { - static char const *const newargv[3] = { EXECLINE_BINPREFIX "exit", "0", 0 } ; - return child_spawn1_pipe(newargv[0], newargv, 0, fd, w) ; - } - else return child_spawn1_pipe(prog, argv, envp, fd, w) ; -} |