From 464bd705f4dab037e443a4503497d5f85731ce9e Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 30 Jun 2020 21:10:13 -0500 Subject: [PATCH] unix: Add more posix_spawnp and rename fork-process to call-fork. - spawn-process will call posix_spawn() - fork-process will call fork() The environment variable should be used or else apps like VSCode won't open because the display isn't set. --- basis/io/launcher/unix/unix.factor | 13 ++- basis/unix/process/process.factor | 132 ++++++++++++++++++++++++----- 2 files changed, 123 insertions(+), 22 deletions(-) diff --git a/basis/io/launcher/unix/unix.factor b/basis/io/launcher/unix/unix.factor index ce75d91cc2..c82ee9ff8c 100644 --- a/basis/io/launcher/unix/unix.factor +++ b/basis/io/launcher/unix/unix.factor @@ -79,7 +79,7 @@ IN: io.launcher.unix : reset-ignored-signals ( process -- process ) SIGPIPE SIG_DFL signal drop ; -: spawn-process ( process -- * ) +: fork-process ( process -- pid ) [ reset-ignored-signals ] [ 2drop 248 _exit ] recover [ setup-process-group ] [ 2drop 249 _exit ] recover [ setup-priority ] [ 2drop 250 _exit ] recover @@ -90,10 +90,19 @@ IN: io.launcher.unix 255 _exit f throw ; +: spawn-process ( process -- pid ) + [ reset-ignored-signals ] [ 2drop 248 _exit ] recover + [ setup-process-group ] [ 2drop 249 _exit ] recover + [ setup-priority ] [ 2drop 250 _exit ] recover + [ setup-redirection ] [ 2drop 251 _exit ] recover + [ current-directory get cd ] [ 2drop 252 _exit ] recover + [ setup-environment ] [ 2drop 253 _exit ] recover + [ get-arguments posix-spawn ] [ drop ] recover ; + M: unix (current-process) ( -- handle ) getpid ; M: unix (run-process) ( process -- pid ) - '[ _ spawn-process ] [ ] with-fork ; + '[ _ fork-process ] [ ] with-fork ; M: unix (kill-process) ( process -- ) [ handle>> SIGTERM ] [ group>> ] bi { diff --git a/basis/unix/process/process.factor b/basis/unix/process/process.factor index de63b8449c..86247eeb79 100644 --- a/basis/unix/process/process.factor +++ b/basis/unix/process/process.factor @@ -1,6 +1,8 @@ -USING: alien.c-types alien.data alien.syntax classes.struct -generalizations io.encodings.utf8 kernel libc math sequences -unix unix.types unix.utilities ; +USING: alien alien.c-types alien.data alien.syntax +environment.unix generalizations io.encodings.utf8 kernel libc +math sequences simple-tokenizer strings unix unix.types +unix.utilities ; +QUALIFIED-WITH: alien.c-types ac IN: unix.process ! Low-level Unix process launching utilities. These are used @@ -9,35 +11,125 @@ IN: unix.process FUNCTION: pid_t fork ( ) -: fork-process ( -- pid ) [ fork ] unix-system-call ; +: call-fork ( -- pid ) [ fork ] unix-system-call ; FUNCTION: int execv ( c-string path, c-string* argv ) FUNCTION: int execvp ( c-string path, c-string* argv ) FUNCTION: int execve ( c-string path, c-string* argv, c-string* envp ) +TYPEDEF: void* posix_spawn_file_actions_t +TYPEDEF: void* posix_spawnattr_t -TYPEDEF: void posix_spawn_file_actions_t -TYPEDEF: void posix_spawnattr_t +TYPEDEF: uint sigset_t -FUNCTION: int posix_spawn ( pid_t *pid, c-string path, - posix_spawn_file_actions_t *file_actions, - posix_spawnattr_t *attrp, +FUNCTION: int posix_spawn_file_actions_init ( posix_spawn_file_actions_t* file_actions ) +FUNCTION: int posix_spawn_file_actions_destroy ( posix_spawn_file_actions_t* file_actions ) + +FUNCTION: int posix_spawnattr_init ( posix_spawnattr_t* attr ) +FUNCTION: int posix_spawnattr_destroy ( posix_spawnattr_t* attr ) + + +FUNCTION: int posix_spawn_file_actions_addclose ( + posix_spawn_file_actions_t *file_actions, int filedes ) +FUNCTION: int posix_spawn_file_actions_addopen ( + posix_spawn_file_actions_t* file_actions, int intfiledes, char* path, int oflag, mode_t mode ) +FUNCTION: int posix_spawn_file_actions_adddup2 ( + posix_spawn_file_actions_t *file_actions, int filedes, int intnewfiledes ) +FUNCTION: int posix_spawn_file_actions_addinherit_np ( + posix_spawn_file_actions_t *file_actions, int filedes ) +FUNCTION: int posix_spawn_file_actions_addchdir_np ( + posix_spawn_file_actions_t *file_actions char* path ) +FUNCTION: int posix_spawn_file_actions_addfchdir_np ( + posix_spawn_file_actions_t *file_actions, int filedes ) + +FUNCTION: int posix_spawnattr_getsigdefault ( posix_spawnattr_t* attr, sigset_t* sigdefault ) +FUNCTION: int posix_spawnattr_setsigdefault ( posix_spawnattr_t* attr, sigset_t* sigdefault ) + +FUNCTION: int posix_spawnattr_getflags ( posix_spawnattr_t* attr, ac:short* flags ) +FUNCTION: int posix_spawnattr_setflags ( posix_spawnattr_t* attr, ac:short flags ) + +FUNCTION: int posix_spawnattr_getpgroup ( posix_spawnattr_t* attr, pid_t* pgroup ) +FUNCTION: int posix_spawnattr_setpgroup ( posix_spawnattr_t* attr, pid_t pgroup ) + +FUNCTION: int posix_spawnattr_getsigmask ( posix_spawnattr_t* attr, sigset_t* sigmask ) +FUNCTION: int posix_spawnattr_setsigmask ( posix_spawnattr_t* attr, sigset_t* sigmask ) + +FUNCTION: int sigaddset ( sigset_t* set, int signo ) +FUNCTION: int sigdelset ( sigset_t* set, int signo ) +FUNCTION: int sigemptyset ( sigset_t* set ) +FUNCTION: int sigfillset ( sigset_t* set ) +FUNCTION: int sigismember ( sigset_t* set, int signo ) + +! Not on macOS +FUNCTION: int posix_spawnattr_getschedparam ( posix_spawnattr_t* attr ) +FUNCTION: int posix_spawnattr_setschedparam ( posix_spawnattr_t* attr ) +FUNCTION: int posix_spawnattr_getschedpolicy ( posix_spawnattr_t* attr ) +FUNCTION: int posix_spawnattr_setschedpolicy ( posix_spawnattr_t* attr ) + +CONSTANT: POSIX_SPAWN_RESETIDS 0x0001 +CONSTANT: POSIX_SPAWN_SETPGROUP 0x0002 +CONSTANT: POSIX_SPAWN_SETSIGDEF 0x0004 +CONSTANT: POSIX_SPAWN_SETSIGMASK 0x0008 + +CONSTANT: POSIX_SPAWN_SETSCHEDPARAM 0x0010 +CONSTANT: POSIX_SPAWN_SETSCHEDULER 0x0020 + +! Darwin-specific flags +CONSTANT: POSIX_SPAWN_SETEXEC 0x0040 +CONSTANT: POSIX_SPAWN_START_SUSPENDED 0x0080 +CONSTANT: POSIX_SPAWN_SETSID 0x0400 +CONSTANT: POSIX_SPAWN_CLOEXEC_DEFAULT 0x4000 + +CONSTANT: POSIX_SPAWN_PCONTROL_NONE 0x0000 +CONSTANT: POSIX_SPAWN_PCONTROL_THROTTLE 0x0001 +CONSTANT: POSIX_SPAWN_PCONTROL_SUSPEND 0x0002 +CONSTANT: POSIX_SPAWN_PCONTROL_KILL 0x0003 + +: check-posix ( n -- ) + dup 0 = [ drop ] [ (throw-errno) ] if ; + +: posix-spawn-file-actions-init ( -- posix_spawn_file_actions_t ) + f posix_spawn_file_actions_t + [ posix_spawn_file_actions_init check-posix ] keep ; + +: posix-spawn-file-actions-destroy ( posix_spawn_file_actions_t -- ) + posix_spawn_file_actions_destroy check-posix ; + +: posix-spawnattr-init ( -- posix_spawnattr_t ) + f posix_spawnattr_t + [ posix_spawnattr_init check-posix ] keep ; + +: posix-spawnattr-destroy ( posix_spawnattr_t -- ) + posix_spawnattr_destroy check-posix ; + +FUNCTION: int posix_spawn ( pid_t* pid, c-string path, + posix_spawn_file_actions_t* file_actions, + posix_spawnattr_t* attrp, c-string* argv, c-string* envp ) -FUNCTION: int posix_spawnp ( pid_t *pid, c-string file, - posix_spawn_file_actions_t *file_actions, - posix_spawnattr_t *attrp, +FUNCTION: int posix_spawnp ( pid_t* pid, c-string file, + posix_spawn_file_actions_t* file_actions, + posix_spawnattr_t* attrp, c-string* argv, c-string* envp ) -: posix-spawn ( path posix_spawn_file_actions_t* posix_spawnattr_t* argv envp -- pid_t ) +: posix-spawn-call ( path posix_spawn_file_actions_t* posix_spawnattr_t* argv envp -- pid_t ) [ [ 0 pid_t ] dip utf8 malloc-string ] 4dip - [ utf8 strings>alien ] bi@ - [ - posix_spawnp dup 0 = [ drop ] [ throw-errno ] if - ] 6 nkeep 5drop pid_t deref ; + [ utf8 strings>alien ] + [ dup sequence? [ utf8 strings>alien ] when ] bi* + [ posix_spawnp check-posix ] 6 nkeep 5drop pid_t deref ; -: posix-spawn-args-with-path ( seq -- int ) - [ first f f ] keep f posix-spawn ; +: posix-spawn-custom-env ( cmd env -- int ) + [ dup string? [ tokenize ] when ] dip + [ + [ + first + posix-spawn-file-actions-init + posix-spawnattr-init + ] keep + ] dip posix-spawn-call ; + +: posix-spawn ( cmd -- int ) + environ posix-spawn-custom-env ; : exec ( pathname argv -- int ) [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execv ; @@ -60,7 +152,7 @@ FUNCTION: int posix_spawnp ( pid_t *pid, c-string file, [ [ first ] keep ] dip exec-with-env ; : with-fork ( child parent -- ) - [ fork-process ] 2dip if-zero ; inline + [ call-fork ] 2dip if-zero ; inline FUNCTION: int kill ( pid_t pid, int sig ) FUNCTION: int raise ( int sig )