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.
master
Doug Coleman 2020-06-30 21:10:13 -05:00
parent 3a091577ae
commit 464bd705f4
2 changed files with 123 additions and 22 deletions

View File

@ -79,7 +79,7 @@ IN: io.launcher.unix
: reset-ignored-signals ( process -- process ) : reset-ignored-signals ( process -- process )
SIGPIPE SIG_DFL signal drop ; SIGPIPE SIG_DFL signal drop ;
: spawn-process ( process -- * ) : fork-process ( process -- pid )
[ reset-ignored-signals ] [ 2drop 248 _exit ] recover [ reset-ignored-signals ] [ 2drop 248 _exit ] recover
[ setup-process-group ] [ 2drop 249 _exit ] recover [ setup-process-group ] [ 2drop 249 _exit ] recover
[ setup-priority ] [ 2drop 250 _exit ] recover [ setup-priority ] [ 2drop 250 _exit ] recover
@ -90,10 +90,19 @@ IN: io.launcher.unix
255 _exit 255 _exit
f throw ; 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 (current-process) ( -- handle ) getpid ;
M: unix (run-process) ( process -- pid ) M: unix (run-process) ( process -- pid )
'[ _ spawn-process ] [ ] with-fork ; '[ _ fork-process ] [ ] with-fork ;
M: unix (kill-process) ( process -- ) M: unix (kill-process) ( process -- )
[ handle>> SIGTERM ] [ group>> ] bi { [ handle>> SIGTERM ] [ group>> ] bi {

View File

@ -1,6 +1,8 @@
USING: alien.c-types alien.data alien.syntax classes.struct USING: alien alien.c-types alien.data alien.syntax
generalizations io.encodings.utf8 kernel libc math sequences environment.unix generalizations io.encodings.utf8 kernel libc
unix unix.types unix.utilities ; math sequences simple-tokenizer strings unix unix.types
unix.utilities ;
QUALIFIED-WITH: alien.c-types ac
IN: unix.process IN: unix.process
! Low-level Unix process launching utilities. These are used ! Low-level Unix process launching utilities. These are used
@ -9,35 +11,125 @@ IN: unix.process
FUNCTION: pid_t fork ( ) 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 execv ( c-string path, c-string* argv )
FUNCTION: int execvp ( 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 ) 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: uint sigset_t
TYPEDEF: void posix_spawnattr_t
FUNCTION: int posix_spawn ( pid_t *pid, c-string path, FUNCTION: int posix_spawn_file_actions_init ( posix_spawn_file_actions_t* file_actions )
posix_spawn_file_actions_t *file_actions, FUNCTION: int posix_spawn_file_actions_destroy ( posix_spawn_file_actions_t* file_actions )
posix_spawnattr_t *attrp,
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 <ref>
[ 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 <ref>
[ 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 ) c-string* argv, c-string* envp )
FUNCTION: int posix_spawnp ( pid_t *pid, c-string file, FUNCTION: int posix_spawnp ( pid_t* pid, c-string file,
posix_spawn_file_actions_t *file_actions, posix_spawn_file_actions_t* file_actions,
posix_spawnattr_t *attrp, posix_spawnattr_t* attrp,
c-string* argv, c-string* envp ) 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 <ref> ] dip utf8 malloc-string ] 4dip [ [ 0 pid_t <ref> ] dip utf8 malloc-string ] 4dip
[ utf8 strings>alien ] bi@ [ utf8 strings>alien ]
[ [ dup sequence? [ utf8 strings>alien ] when ] bi*
posix_spawnp dup 0 = [ drop ] [ throw-errno ] if [ posix_spawnp check-posix ] 6 nkeep 5drop pid_t deref ;
] 6 nkeep 5drop pid_t deref ;
: posix-spawn-args-with-path ( seq -- int ) : posix-spawn-custom-env ( cmd env -- int )
[ first f f ] keep f posix-spawn ; [ 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 ) : exec ( pathname argv -- int )
[ utf8 malloc-string ] [ utf8 strings>alien ] bi* execv ; [ 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 ; [ [ first ] keep ] dip exec-with-env ;
: with-fork ( child parent -- ) : 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 kill ( pid_t pid, int sig )
FUNCTION: int raise ( int sig ) FUNCTION: int raise ( int sig )