From d229a8b84880dddc05267798795ea29c50af28bd Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 11 Sep 2023 21:23:56 +0000 Subject: Defork execline Signed-off-by: Laurent Bercot --- package/deps.mak | 10 +++--- src/execline/background.c | 30 +++++------------ src/execline/pipeline.c | 69 +++++++++++++++++---------------------- src/libexecline/deps-lib/execline | 3 +- src/libexecline/el_gspawn0.c | 16 +++++++++ src/libexecline/el_spawn0.c | 2 +- src/libexecline/el_spawn1.c | 16 --------- 7 files changed, 62 insertions(+), 84 deletions(-) create mode 100644 src/libexecline/el_gspawn0.c delete mode 100644 src/libexecline/el_spawn1.c diff --git a/package/deps.mak b/package/deps.mak index 8fd1f8b..c76a4cc 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -54,6 +54,7 @@ src/execline/wait.o src/execline/wait.lo: src/execline/wait.c src/include/execli src/execline/withstdinas.o src/execline/withstdinas.lo: src/execline/withstdinas.c src/include/execline/execline.h src/libexecline/el_execsequence.o src/libexecline/el_execsequence.lo: src/libexecline/el_execsequence.c src/include/execline/execline.h src/libexecline/el_getstrict.o src/libexecline/el_getstrict.lo: src/libexecline/el_getstrict.c src/include/execline/execline.h +src/libexecline/el_gspawn0.o src/libexecline/el_gspawn0.lo: src/libexecline/el_gspawn0.c src/include/execline/config.h src/include/execline/execline.h src/libexecline/el_modif_and_exec.o src/libexecline/el_modif_and_exec.lo: src/libexecline/el_modif_and_exec.c src/include/execline/config.h src/include/execline/execline.h src/libexecline/el_modif_and_spawn.o src/libexecline/el_modif_and_spawn.lo: src/libexecline/el_modif_and_spawn.c src/include/execline/config.h src/include/execline/execline.h src/libexecline/el_parse.o src/libexecline/el_parse.lo: src/libexecline/el_parse.c src/include/execline/execline.h @@ -63,7 +64,6 @@ src/libexecline/el_popenv.o src/libexecline/el_popenv.lo: src/libexecline/el_pop src/libexecline/el_pushenv.o src/libexecline/el_pushenv.lo: src/libexecline/el_pushenv.c src/include/execline/execline.h src/libexecline/el_semicolon.o src/libexecline/el_semicolon.lo: src/libexecline/el_semicolon.c src/include/execline/execline.h src/libexecline/el_spawn0.o src/libexecline/el_spawn0.lo: src/libexecline/el_spawn0.c src/include/execline/config.h src/include/execline/execline.h -src/libexecline/el_spawn1.o src/libexecline/el_spawn1.lo: src/libexecline/el_spawn1.c src/include/execline/config.h src/include/execline/execline.h src/libexecline/el_substandrun.o src/libexecline/el_substandrun.lo: src/libexecline/el_substandrun.c src/include-local/exlsn.h src/libexecline/el_substandrun_str.o src/libexecline/el_substandrun_str.lo: src/libexecline/el_substandrun_str.c src/include/execline/execline.h src/include-local/exlsn.h src/libexecline/el_substitute.o src/libexecline/el_substitute.lo: src/libexecline/el_substitute.c src/include/execline/execline.h @@ -179,12 +179,12 @@ wait: src/execline/wait.o ${LIBEXECLINE} withstdinas: EXTRA_LIBS := -lskarnet withstdinas: src/execline/withstdinas.o ${LIBEXECLINE} ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),) -libexecline.a.xyzzy: src/libexecline/el_execsequence.o src/libexecline/el_getstrict.o src/libexecline/el_modif_and_exec.o src/libexecline/el_modif_and_spawn.o src/libexecline/el_parse.o src/libexecline/el_parse_from_buffer.o src/libexecline/el_parse_from_string.o src/libexecline/el_popenv.o src/libexecline/el_pushenv.o src/libexecline/el_semicolon.o src/libexecline/el_spawn0.o src/libexecline/el_spawn1.o src/libexecline/el_substandrun.o src/libexecline/el_substandrun_str.o src/libexecline/el_substitute.o src/libexecline/el_transform.o src/libexecline/el_vardupl.o src/libexecline/exlsn_define.o src/libexecline/exlsn_elglob.o src/libexecline/exlsn_importas.o src/libexecline/exlsn_multidefine.o src/libexecline/exlsn_exlp.o src/libexecline/exlsn_main.o src/libexecline/exlsn_free.o src/libexecline/exlp.o +libexecline.a.xyzzy: src/libexecline/el_execsequence.o src/libexecline/el_getstrict.o src/libexecline/el_modif_and_exec.o src/libexecline/el_modif_and_spawn.o src/libexecline/el_parse.o src/libexecline/el_parse_from_buffer.o src/libexecline/el_parse_from_string.o src/libexecline/el_popenv.o src/libexecline/el_pushenv.o src/libexecline/el_semicolon.o src/libexecline/el_spawn0.o src/libexecline/el_gspawn0.o src/libexecline/el_substandrun.o src/libexecline/el_substandrun_str.o src/libexecline/el_substitute.o src/libexecline/el_transform.o src/libexecline/el_vardupl.o src/libexecline/exlsn_define.o src/libexecline/exlsn_elglob.o src/libexecline/exlsn_importas.o src/libexecline/exlsn_multidefine.o src/libexecline/exlsn_exlp.o src/libexecline/exlsn_main.o src/libexecline/exlsn_free.o src/libexecline/exlp.o else -libexecline.a.xyzzy: src/libexecline/el_execsequence.lo src/libexecline/el_getstrict.lo src/libexecline/el_modif_and_exec.lo src/libexecline/el_modif_and_spawn.lo src/libexecline/el_parse.lo src/libexecline/el_parse_from_buffer.lo src/libexecline/el_parse_from_string.lo src/libexecline/el_popenv.lo src/libexecline/el_pushenv.lo src/libexecline/el_semicolon.lo src/libexecline/el_spawn0.lo src/libexecline/el_spawn1.lo src/libexecline/el_substandrun.lo src/libexecline/el_substandrun_str.lo src/libexecline/el_substitute.lo src/libexecline/el_transform.lo src/libexecline/el_vardupl.lo src/libexecline/exlsn_define.lo src/libexecline/exlsn_elglob.lo src/libexecline/exlsn_importas.lo src/libexecline/exlsn_multidefine.lo src/libexecline/exlsn_exlp.lo src/libexecline/exlsn_main.lo src/libexecline/exlsn_free.lo src/libexecline/exlp.lo +libexecline.a.xyzzy: src/libexecline/el_execsequence.lo src/libexecline/el_getstrict.lo src/libexecline/el_modif_and_exec.lo src/libexecline/el_modif_and_spawn.lo src/libexecline/el_parse.lo src/libexecline/el_parse_from_buffer.lo src/libexecline/el_parse_from_string.lo src/libexecline/el_popenv.lo src/libexecline/el_pushenv.lo src/libexecline/el_semicolon.lo src/libexecline/el_spawn0.lo src/libexecline/el_gspawn0.lo src/libexecline/el_substandrun.lo src/libexecline/el_substandrun_str.lo src/libexecline/el_substitute.lo src/libexecline/el_transform.lo src/libexecline/el_vardupl.lo src/libexecline/exlsn_define.lo src/libexecline/exlsn_elglob.lo src/libexecline/exlsn_importas.lo src/libexecline/exlsn_multidefine.lo src/libexecline/exlsn_exlp.lo src/libexecline/exlsn_main.lo src/libexecline/exlsn_free.lo src/libexecline/exlp.lo endif -libexecline.so.xyzzy: EXTRA_LIBS := -lskarnet -libexecline.so.xyzzy: src/libexecline/el_execsequence.lo src/libexecline/el_getstrict.lo src/libexecline/el_modif_and_exec.lo src/libexecline/el_modif_and_spawn.lo src/libexecline/el_parse.lo src/libexecline/el_parse_from_buffer.lo src/libexecline/el_parse_from_string.lo src/libexecline/el_popenv.lo src/libexecline/el_pushenv.lo src/libexecline/el_semicolon.lo src/libexecline/el_spawn0.lo src/libexecline/el_spawn1.lo src/libexecline/el_substandrun.lo src/libexecline/el_substandrun_str.lo src/libexecline/el_substitute.lo src/libexecline/el_transform.lo src/libexecline/el_vardupl.lo src/libexecline/exlsn_define.lo src/libexecline/exlsn_elglob.lo src/libexecline/exlsn_importas.lo src/libexecline/exlsn_multidefine.lo src/libexecline/exlsn_exlp.lo src/libexecline/exlsn_main.lo src/libexecline/exlsn_free.lo src/libexecline/exlp.lo +libexecline.so.xyzzy: EXTRA_LIBS := -lskarnet ${SPAWN_LIB} +libexecline.so.xyzzy: src/libexecline/el_execsequence.lo src/libexecline/el_getstrict.lo src/libexecline/el_modif_and_exec.lo src/libexecline/el_modif_and_spawn.lo src/libexecline/el_parse.lo src/libexecline/el_parse_from_buffer.lo src/libexecline/el_parse_from_string.lo src/libexecline/el_popenv.lo src/libexecline/el_pushenv.lo src/libexecline/el_semicolon.lo src/libexecline/el_spawn0.lo src/libexecline/el_gspawn0.lo src/libexecline/el_substandrun.lo src/libexecline/el_substandrun_str.lo src/libexecline/el_substitute.lo src/libexecline/el_transform.lo src/libexecline/el_vardupl.lo src/libexecline/exlsn_define.lo src/libexecline/exlsn_elglob.lo src/libexecline/exlsn_importas.lo src/libexecline/exlsn_multidefine.lo src/libexecline/exlsn_exlp.lo src/libexecline/exlsn_main.lo src/libexecline/exlsn_free.lo src/libexecline/exlp.lo execline: EXTRA_LIBS := -lskarnet ${SPAWN_LIB} ${MAYBEPTHREAD_LIB} execline: src/multicall/execline.o ${LIBEXECLINE} ${LIBNSSS} INTERNAL_LIBS := 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 #include #include +#include #include #include @@ -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 #include #include -#include +#include #include +#include #include @@ -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 + +#include +#include + +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 - -#include -#include - -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) ; -} -- cgit 1.4.1