]> gitweb.factorcode.org Git - factor.git/commitdiff
unix: Add more posix_spawnp and rename fork-process to call-fork.
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 1 Jul 2020 02:10:13 +0000 (21:10 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 1 Jul 2020 02:12:51 +0000 (21:12 -0500)
- 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
basis/unix/process/process.factor

index ce75d91cc2b7cac847e1d201759914bd037a16d5..c82ee9ff8c0dd89bd1a619b6f48102b9dbc632d1 100644 (file)
@@ -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 {
index de63b8449c3fdf85a48f876d1e07c0acdb01ac8b..86247eeb79ccb3c9eb451046edad02df60a7e742 100644 (file)
@@ -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 <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 )
 
-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_tpid, c-string file,
+                       posix_spawn_file_actions_tfile_actions,
+                       posix_spawnattr_tattrp,
                        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
-    [ utf8 strings>alien ] bi@
+    [ utf8 strings>alien ]
+    [ dup sequence? [ utf8 strings>alien ] when ] bi*
+    [ posix_spawnp check-posix ] 6 nkeep 5drop pid_t deref ;
+
+: posix-spawn-custom-env ( cmd env -- int )
+    [ dup string? [ tokenize ] when ] dip
     [
-        posix_spawnp dup 0 = [ drop ] [ throw-errno ] if
-    ] 6 nkeep 5drop pid_t deref ;
+        [
+            first
+            posix-spawn-file-actions-init
+            posix-spawnattr-init
+        ] keep
+    ] dip posix-spawn-call ;
 
-: posix-spawn-args-with-path ( seq -- int )
-    [ first f f ] keep f posix-spawn ;
+: 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 )