Skip to content

Commit 2546e75

Browse files
committed
Add support for POSIX_SPAWN_SETSID
Also sync supported `<spawn.h>` features with GLibc
1 parent 472a226 commit 2546e75

File tree

8 files changed

+241
-39
lines changed

8 files changed

+241
-39
lines changed

kos/include/asm/crt/posix_spawn.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
#define __POSIX_SPAWN_SETSCHEDPARAM 0x0010 /* s.a. `posix_spawnattr_setschedparam(3)' */
8282
#define __POSIX_SPAWN_SETSCHEDULER 0x0020 /* s.a. `posix_spawnattr_setschedpolicy(3)' */
8383
#define __POSIX_SPAWN_USEVFORK 0x0040 /* Ignored */
84+
#if defined(__CRT_HAVE_setsid) || defined(__CRT_HAVE___setsid) || defined(__CRT_HAVE___libc_setsid)
85+
#define __POSIX_SPAWN_SETSID 0x0080 /* Call `setsid(2)' within the context of the new process */
86+
#endif /* ... */
87+
#define __POSIX_SPAWN_SETCGROUP 0x0100 /* Set `CLONE_INTO_CGROUP' when spawning a new process (Not implemented on KOS) */
8488
#ifdef __CRT_KOS
8589
#define __POSIX_SPAWN_NOEXECERR 0x1000 /* Don't propagate exec() error, and leave the
8690
* child as unreaped with exit status `127' */

kos/include/bits/crt/posix_spawn.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,20 @@ struct __posix_spawnattr {
5757
/* Data structure to contain information about the actions to be
5858
* performed in the new process with respect to file descriptors. */
5959
#ifdef __POSIX_SPAWN_USE_KOS
60-
#define __POSIX_SPAWN_ACTION_CLOSE 0 /* Close a file handle */
61-
#define __POSIX_SPAWN_ACTION_DUP2 1 /* Duplicate a file handle */
62-
#define __POSIX_SPAWN_ACTION_OPEN 2 /* Open a file using `open(2)' */
63-
#define __POSIX_SPAWN_ACTION_CHDIR 3 /* Change direction using `chdir(2)' */
64-
#define __POSIX_SPAWN_ACTION_FCHDIR 4 /* Change direction using `fchdir(2)' */
65-
#ifdef __CRT_KOS
66-
#define __POSIX_SPAWN_ACTION_TCSETPGRP 5 /* Call `tcsetpgrp(fd, getpid())' */
67-
#define __POSIX_SPAWN_ACTION_CLOSEFROM 6 /* Call `closefrom(fd)' */
68-
#endif /* __CRT_KOS */
60+
#define __POSIX_SPAWN_ACTION_CLOSE 0 /* Close a file handle */
61+
#define __POSIX_SPAWN_ACTION_DUP2 1 /* Duplicate a file handle */
62+
#define __POSIX_SPAWN_ACTION_OPEN 2 /* Open a file using `open(2)' */
63+
#if defined(__CRT_KOS) || (defined(__GLIBC_VERSION__) && __GLIBC_VERSION__ >= 22900)
64+
#define __POSIX_SPAWN_ACTION_CHDIR 3 /* Change direction using `chdir(2)' */
65+
#define __POSIX_SPAWN_ACTION_FCHDIR 4 /* Change direction using `fchdir(2)' */
66+
#endif /* __CRT_KOS || __GLIBC_VERSION__ >= 22900 */
67+
#if defined(__CRT_KOS) || (defined(__GLIBC_VERSION__) && __GLIBC_VERSION__ >= 23400)
68+
#define __POSIX_SPAWN_ACTION_CLOSEFROM 5 /* Call `closefrom(fd)' */
69+
#endif /* __CRT_KOS || __GLIBC_VERSION__ >= 23400 */
70+
#if defined(__CRT_KOS) || (defined(__GLIBC_VERSION__) && __GLIBC_VERSION__ >= 23500)
71+
#define __POSIX_SPAWN_ACTION_TCSETPGRP 6 /* Call `tcsetpgrp(fd, getpid())' */
72+
#endif /* __CRT_KOS || __GLIBC_VERSION__ >= 23500 */
73+
6974
struct __spawn_action {
7075
unsigned int __sa_tag; /* Action type (one of `__POSIX_SPAWN_ACTION_*') */
7176
union {
@@ -86,23 +91,29 @@ struct __spawn_action {
8691
__mode_t __sa_mode; /* Open mode. */
8792
} __sa_open_action; /* __POSIX_SPAWN_ACTION_OPEN */
8893

94+
#ifdef __POSIX_SPAWN_ACTION_CHDIR
8995
struct {
9096
char *__sa_path; /* [1..1][owned] Path to chdir(2) to. */
9197
} __sa_chdir_action; /* __POSIX_SPAWN_ACTION_CHDIR */
98+
#endif /* __POSIX_SPAWN_ACTION_CHDIR */
9299

100+
#ifdef __POSIX_SPAWN_ACTION_FCHDIR
93101
struct {
94102
__fd_t __sa_fd; /* Fd to fchdir(2) to. */
95103
} __sa_fchdir_action; /* __POSIX_SPAWN_ACTION_FCHDIR */
104+
#endif /* __POSIX_SPAWN_ACTION_FCHDIR */
96105

97-
#ifdef __CRT_KOS
98-
struct {
99-
__fd_t __sa_fd; /* `tcsetpgrp(__sa_fd, getpid())' */
100-
} __sa_tcsetpgrp_action; /* __POSIX_SPAWN_ACTION_TCSETPGRP */
101-
106+
#ifdef __POSIX_SPAWN_ACTION_CLOSEFROM
102107
struct {
103108
__fd_t __sa_fd; /* `closefrom(__sa_fd)' */
104109
} __sa_closefrom_action; /* __POSIX_SPAWN_ACTION_CLOSEFROM */
105-
#endif /* __CRT_KOS */
110+
#endif /* __POSIX_SPAWN_ACTION_CLOSEFROM */
111+
112+
#ifdef __POSIX_SPAWN_ACTION_TCSETPGRP
113+
struct {
114+
__fd_t __sa_fd; /* `tcsetpgrp(__sa_fd, getpid())' */
115+
} __sa_tcsetpgrp_action; /* __POSIX_SPAWN_ACTION_TCSETPGRP */
116+
#endif /* __POSIX_SPAWN_ACTION_TCSETPGRP */
106117

107118
} __sa_action; /* Action-specific data. */
108119
};

kos/include/libc/local/spawn/posix_fspawn_np.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* HASH CRC-32:0x889b5fd8 */
1+
/* HASH CRC-32:0x649ea05c */
22
/* Copyright (c) 2019-2025 Griefer@Work *
33
* *
44
* This software is provided 'as-is', without any express or implied *
@@ -362,6 +362,18 @@ __CREDIRECT(,int,__NOTHROW_NCX,__localdep_setpgid,(__pid_t __pid, __pid_t __pgid
362362
#undef __local___localdep_setpgid_defined
363363
#endif /* !... */
364364
#endif /* !__local___localdep_setpgid_defined */
365+
#ifndef __local___localdep_setsid_defined
366+
#define __local___localdep_setsid_defined
367+
#ifdef __CRT_HAVE_setsid
368+
__CREDIRECT(,__pid_t,__NOTHROW_NCX,__localdep_setsid,(void),setsid,())
369+
#elif defined(__CRT_HAVE___setsid)
370+
__CREDIRECT(,__pid_t,__NOTHROW_NCX,__localdep_setsid,(void),__setsid,())
371+
#elif defined(__CRT_HAVE___libc_setsid)
372+
__CREDIRECT(,__pid_t,__NOTHROW_NCX,__localdep_setsid,(void),__libc_setsid,())
373+
#else /* ... */
374+
#undef __local___localdep_setsid_defined
375+
#endif /* !... */
376+
#endif /* !__local___localdep_setsid_defined */
365377
#ifndef __local___localdep_sigaction_defined
366378
#define __local___localdep_sigaction_defined
367379
#ifdef __CRT_HAVE_sigaction
@@ -755,6 +767,7 @@ __NOTHROW_RPC(__LIBCCALL __LIBC_LOCAL_NAME(posix_fspawn_np))(__pid_t *__restrict
755767
#endif /* (__CRT_HAVE_open64 || __CRT_HAVE___open64 || __CRT_HAVE_open || __CRT_HAVE__open || __CRT_HAVE___open || __CRT_HAVE___libc_open || (__AT_FDCWD && (__CRT_HAVE_openat64 || __CRT_HAVE_openat))) && (__CRT_HAVE_dup2 || __CRT_HAVE__dup2 || __CRT_HAVE___dup2 || __CRT_HAVE___libc_dup2) && (__CRT_HAVE_close || __CRT_HAVE__close || __CRT_HAVE___close || __CRT_HAVE___libc_close) */
756768

757769

770+
#ifdef __POSIX_SPAWN_ACTION_CHDIR
758771
#if !defined(__CRT_HAVE_chdir) && !defined(__CRT_HAVE__chdir) && !defined(__CRT_HAVE___chdir) && !defined(__CRT_HAVE___libc_chdir) && (!defined(__AT_FDCWD) || !defined(__CRT_HAVE_fchdirat))
759772
#define __POSIX_SPAWN_HAVE_UNSUPPORTED_FILE_ACTION 1
760773
#else /* !__CRT_HAVE_chdir && !__CRT_HAVE__chdir && !__CRT_HAVE___chdir && !__CRT_HAVE___libc_chdir && (!__AT_FDCWD || !__CRT_HAVE_fchdirat) */
@@ -766,8 +779,10 @@ __NOTHROW_RPC(__LIBCCALL __LIBC_LOCAL_NAME(posix_fspawn_np))(__pid_t *__restrict
766779
goto __child_error;
767780
} break;
768781
#endif /* __CRT_HAVE_chdir || __CRT_HAVE__chdir || __CRT_HAVE___chdir || __CRT_HAVE___libc_chdir || (__AT_FDCWD && __CRT_HAVE_fchdirat) */
782+
#endif /* __POSIX_SPAWN_ACTION_CHDIR */
769783

770784

785+
#ifdef __POSIX_SPAWN_ACTION_FCHDIR
771786
#if !defined(__CRT_HAVE_fchdir) && !defined(__CRT_HAVE___fchdir) && !defined(__CRT_HAVE___libc_fchdir)
772787
#define __POSIX_SPAWN_HAVE_UNSUPPORTED_FILE_ACTION 1
773788
#else /* !__CRT_HAVE_fchdir && !__CRT_HAVE___fchdir && !__CRT_HAVE___libc_fchdir */
@@ -779,6 +794,7 @@ __NOTHROW_RPC(__LIBCCALL __LIBC_LOCAL_NAME(posix_fspawn_np))(__pid_t *__restrict
779794
goto __child_error;
780795
} break;
781796
#endif /* __CRT_HAVE_fchdir || __CRT_HAVE___fchdir || __CRT_HAVE___libc_fchdir */
797+
#endif /* __POSIX_SPAWN_ACTION_FCHDIR */
782798

783799

784800
#ifdef __POSIX_SPAWN_ACTION_TCSETPGRP
@@ -931,6 +947,12 @@ __NOTHROW_RPC(__LIBCCALL __LIBC_LOCAL_NAME(posix_fspawn_np))(__pid_t *__restrict
931947
goto __child_error;
932948
#endif /* (!__CRT_HAVE_sched_setscheduler && !__CRT_HAVE___sched_setscheduler && !__CRT_HAVE___libc_sched_setscheduler) || (!__CRT_HAVE_sched_setparam && !__CRT_HAVE___sched_setparam && !__CRT_HAVE___libc_sched_setparam) || (!__CRT_HAVE_sched_getparam && !__CRT_HAVE___sched_getparam && !__CRT_HAVE___libc_sched_getparam) */
933949
}
950+
#if defined(__POSIX_SPAWN_SETSID) && (defined(__CRT_HAVE_setsid) || defined(__CRT_HAVE___setsid) || defined(__CRT_HAVE___libc_setsid))
951+
if (__attrp->__flags & __POSIX_SPAWN_SETSID) {
952+
if __unlikely((__NAMESPACE_LOCAL_SYM __localdep_setsid)() < 0)
953+
goto __child_error;
954+
}
955+
#endif /* __POSIX_SPAWN_SETSID && (__CRT_HAVE_setsid || __CRT_HAVE___setsid || __CRT_HAVE___libc_setsid) */
934956
}
935957
/* When the exec succeeds, the pipe is auto-
936958
* closed because it's marked as O_CLOEXEC! */

kos/include/libc/local/spawn/posix_spawnattr_setflags.h

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* HASH CRC-32:0xf2ca152 */
1+
/* HASH CRC-32:0xfd6b6922 */
22
/* Copyright (c) 2019-2025 Griefer@Work *
33
* *
44
* This software is provided 'as-is', without any express or implied *
@@ -24,9 +24,50 @@
2424
#include <asm/crt/posix_spawn.h>
2525
#ifdef __POSIX_SPAWN_USE_KOS
2626
#include <bits/crt/posix_spawn.h>
27+
#include <asm/os/errno.h>
2728
__NAMESPACE_LOCAL_BEGIN
2829
__LOCAL_LIBC(posix_spawnattr_setflags) __ATTR_INOUT(1) __errno_t
2930
__NOTHROW_NCX(__LIBCCALL __LIBC_LOCAL_NAME(posix_spawnattr_setflags))(struct __posix_spawnattr *__restrict __attr, short int __flags) {
31+
enum {
32+
__ALL_FLAGS = 0
33+
#ifdef __POSIX_SPAWN_RESETIDS
34+
| __POSIX_SPAWN_RESETIDS
35+
#endif /* __POSIX_SPAWN_RESETIDS */
36+
#ifdef __POSIX_SPAWN_SETPGROUP
37+
| __POSIX_SPAWN_SETPGROUP
38+
#endif /* __POSIX_SPAWN_SETPGROUP */
39+
#ifdef __POSIX_SPAWN_SETSIGDEF
40+
| __POSIX_SPAWN_SETSIGDEF
41+
#endif /* __POSIX_SPAWN_SETSIGDEF */
42+
#ifdef __POSIX_SPAWN_SETSIGMASK
43+
| __POSIX_SPAWN_SETSIGMASK
44+
#endif /* __POSIX_SPAWN_SETSIGMASK */
45+
#ifdef __POSIX_SPAWN_SETSCHEDPARAM
46+
| __POSIX_SPAWN_SETSCHEDPARAM
47+
#endif /* __POSIX_SPAWN_SETSCHEDPARAM */
48+
#ifdef __POSIX_SPAWN_SETSCHEDULER
49+
| __POSIX_SPAWN_SETSCHEDULER
50+
#endif /* __POSIX_SPAWN_SETSCHEDULER */
51+
#ifdef __POSIX_SPAWN_USEVFORK
52+
| __POSIX_SPAWN_USEVFORK
53+
#endif /* __POSIX_SPAWN_USEVFORK */
54+
#ifdef __POSIX_SPAWN_SETSID
55+
| __POSIX_SPAWN_SETSID
56+
#endif /* __POSIX_SPAWN_SETSID */
57+
#ifdef __POSIX_SPAWN_SETCGROUP
58+
| __POSIX_SPAWN_SETCGROUP
59+
#endif /* __POSIX_SPAWN_SETCGROUP */
60+
#ifdef __POSIX_SPAWN_NOEXECERR
61+
| __POSIX_SPAWN_NOEXECERR
62+
#endif /* __POSIX_SPAWN_NOEXECERR */
63+
};
64+
if __unlikely((unsigned short int)__flags & ~(unsigned short int)__ALL_FLAGS) {
65+
#ifdef __EINVAL
66+
return __EINVAL;
67+
#else /* __EINVAL */
68+
return 1;
69+
#endif /* !__EINVAL */
70+
}
3071
__attr->__flags = (__UINT16_TYPE__)(unsigned short int)__flags;
3172
return 0;
3273
}

kos/include/spawn.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* HASH CRC-32:0x1116c254 */
1+
/* HASH CRC-32:0x76f915d2 */
22
/* Copyright (c) 2019-2025 Griefer@Work *
33
* *
44
* This software is provided 'as-is', without any express or implied *
@@ -80,6 +80,12 @@ __SYSDECL_BEGIN
8080
#ifdef __POSIX_SPAWN_USEVFORK
8181
#define POSIX_SPAWN_USEVFORK __POSIX_SPAWN_USEVFORK /* Ignored on KOS, which always uses vfork(2) */
8282
#endif /* __POSIX_SPAWN_USEVFORK */
83+
#ifdef __POSIX_SPAWN_SETSID
84+
#define POSIX_SPAWN_SETSID __POSIX_SPAWN_SETSID /* Call `setsid(2)' within the context of the new process */
85+
#endif /* __POSIX_SPAWN_SETSID */
86+
#ifdef __POSIX_SPAWN_SETCGROUP
87+
#define POSIX_SPAWN_SETCGROUP __POSIX_SPAWN_SETCGROUP /* Set `CLONE_INTO_CGROUP' when spawning a new process (Not implemented on KOS) */
88+
#endif /* __POSIX_SPAWN_SETCGROUP */
8389
#endif /* __USE_GNU */
8490

8591
#ifdef __POSIX_SPAWN_NOEXECERR
@@ -299,7 +305,8 @@ __NAMESPACE_LOCAL_USING_OR_IMPL(posix_spawnattr_getflags, __FORCELOCAL __ATTR_AR
299305
* - POSIX_SPAWN_SETPGROUP: s.a. posix_spawnattr_setpgroup(3)
300306
* - POSIX_SPAWN_SETSCHEDULER: s.a. posix_spawnattr_setschedpolicy(3)
301307
* - POSIX_SPAWN_SETSCHEDPARAM: s.a. posix_spawnattr_setschedparam(3)
302-
* @return: 0 : Success */
308+
* @return: 0 : Success
309+
* @return: EINVAL: The given `flags' has unknown/unsupported bits set */
303310
__CDECLARE(__ATTR_INOUT(1),__errno_t,__NOTHROW_NCX,posix_spawnattr_setflags,(posix_spawnattr_t *__restrict __attr, short int __flags),(__attr,__flags))
304311
#elif defined(__POSIX_SPAWN_USE_KOS)
305312
#include <libc/local/spawn/posix_spawnattr_setflags.h>
@@ -312,7 +319,8 @@ __CDECLARE(__ATTR_INOUT(1),__errno_t,__NOTHROW_NCX,posix_spawnattr_setflags,(pos
312319
* - POSIX_SPAWN_SETPGROUP: s.a. posix_spawnattr_setpgroup(3)
313320
* - POSIX_SPAWN_SETSCHEDULER: s.a. posix_spawnattr_setschedpolicy(3)
314321
* - POSIX_SPAWN_SETSCHEDPARAM: s.a. posix_spawnattr_setschedparam(3)
315-
* @return: 0 : Success */
322+
* @return: 0 : Success
323+
* @return: EINVAL: The given `flags' has unknown/unsupported bits set */
316324
__NAMESPACE_LOCAL_USING_OR_IMPL(posix_spawnattr_setflags, __FORCELOCAL __ATTR_ARTIFICIAL __ATTR_INOUT(1) __errno_t __NOTHROW_NCX(__LIBCCALL posix_spawnattr_setflags)(posix_spawnattr_t *__restrict __attr, short int __flags) { return (__NAMESPACE_LOCAL_SYM __LIBC_LOCAL_NAME(posix_spawnattr_setflags))(__attr, __flags); })
317325
#endif /* ... */
318326
#ifdef __CRT_HAVE_posix_spawnattr_getsigdefault
@@ -526,7 +534,7 @@ __CDECLARE(__ATTR_FDARG(2) __ATTR_INOUT(1),__errno_t,__NOTHROW_NCX,posix_spawn_f
526534
__NAMESPACE_LOCAL_USING_OR_IMPL(posix_spawn_file_actions_adddup2, __FORCELOCAL __ATTR_ARTIFICIAL __ATTR_FDARG(2) __ATTR_INOUT(1) __errno_t __NOTHROW_NCX(__LIBCCALL posix_spawn_file_actions_adddup2)(posix_spawn_file_actions_t *__restrict __file_actions, __fd_t __oldfd, __fd_t __newfd) { return (__NAMESPACE_LOCAL_SYM __LIBC_LOCAL_NAME(posix_spawn_file_actions_adddup2))(__file_actions, __oldfd, __newfd); })
527535
#endif /* ... */
528536

529-
#ifdef __USE_KOS
537+
#if defined(__USE_MISC) || defined(__USE_KOS)
530538
#ifdef __CRT_HAVE_posix_spawn_file_actions_addtcsetpgrp_np
531539
/* >> posix_spawn_file_actions_addtcsetpgrp_np(3)
532540
* Enqueue a call `tcsetpgrp(fd, getpid())' to be performed by the child process
@@ -541,9 +549,9 @@ __CDECLARE(__ATTR_FDARG(2) __ATTR_INOUT(1),__errno_t,__NOTHROW_NCX,posix_spawn_f
541549
* @return: ENOMEM: Insufficient memory to enqueue the action */
542550
__NAMESPACE_LOCAL_USING_OR_IMPL(posix_spawn_file_actions_addtcsetpgrp_np, __FORCELOCAL __ATTR_ARTIFICIAL __ATTR_FDARG(2) __ATTR_INOUT(1) __errno_t __NOTHROW_NCX(__LIBCCALL posix_spawn_file_actions_addtcsetpgrp_np)(posix_spawn_file_actions_t *__restrict __file_actions, __fd_t __fd) { return (__NAMESPACE_LOCAL_SYM __LIBC_LOCAL_NAME(posix_spawn_file_actions_addtcsetpgrp_np))(__file_actions, __fd); })
543551
#endif /* ... */
544-
#endif /* __USE_KOS */
552+
#endif /* __USE_MISC || __USE_KOS */
545553

546-
#ifdef __USE_SOLARIS
554+
#if defined(__USE_MISC) || defined(__USE_SOLARIS)
547555
#ifdef __CRT_HAVE_posix_spawn_file_actions_addclosefrom_np
548556
/* >> posix_spawn_file_actions_addclosefrom_np(3)
549557
* Enqueue a call `closefrom(lowfd)' to be performed by the child process
@@ -558,9 +566,9 @@ __CDECLARE(__ATTR_INOUT(1),__errno_t,__NOTHROW_NCX,posix_spawn_file_actions_addc
558566
* @return: ENOMEM: Insufficient memory to enqueue the action */
559567
__NAMESPACE_LOCAL_USING_OR_IMPL(posix_spawn_file_actions_addclosefrom_np, __FORCELOCAL __ATTR_ARTIFICIAL __ATTR_INOUT(1) __errno_t __NOTHROW_NCX(__LIBCCALL posix_spawn_file_actions_addclosefrom_np)(posix_spawn_file_actions_t *__restrict __file_actions, __fd_t __lowfd) { return (__NAMESPACE_LOCAL_SYM __LIBC_LOCAL_NAME(posix_spawn_file_actions_addclosefrom_np))(__file_actions, __lowfd); })
560568
#endif /* ... */
561-
#endif /* __USE_SOLARIS */
569+
#endif /* __USE_MISC || __USE_SOLARIS */
562570

563-
#ifdef __USE_GNU
571+
#if defined(__USE_MISC) || defined(__USE_GNU)
564572
#ifdef __CRT_HAVE_posix_spawn_file_actions_addchdir_np
565573
/* >> posix_spawn_file_actions_addchdir_np(3)
566574
* Enqueue a call `chdir(path)' to be performed by the child process
@@ -589,7 +597,7 @@ __CDECLARE(__ATTR_FDARG(2) __ATTR_INOUT(1),__errno_t,__NOTHROW_NCX,posix_spawn_f
589597
* @return: ENOMEM: Insufficient memory to enqueue the action */
590598
__NAMESPACE_LOCAL_USING_OR_IMPL(posix_spawn_file_actions_addfchdir_np, __FORCELOCAL __ATTR_ARTIFICIAL __ATTR_FDARG(2) __ATTR_INOUT(1) __errno_t __NOTHROW_NCX(__LIBCCALL posix_spawn_file_actions_addfchdir_np)(posix_spawn_file_actions_t *__restrict __file_actions, __fd_t __dfd) { return (__NAMESPACE_LOCAL_SYM __LIBC_LOCAL_NAME(posix_spawn_file_actions_addfchdir_np))(__file_actions, __dfd); })
591599
#endif /* ... */
592-
#endif /* __USE_GNU */
600+
#endif /* __USE_MISC || __USE_GNU */
593601

594602
#endif /* __CC__ */
595603

kos/src/libc/auto/spawn.c

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* HASH CRC-32:0xd17395ce */
1+
/* HASH CRC-32:0x2135d34d */
22
/* Copyright (c) 2019-2025 Griefer@Work *
33
* *
44
* This software is provided 'as-is', without any express or implied *
@@ -212,6 +212,7 @@ NOTHROW_RPC(LIBCCALL libc_posix_fspawn_np)(pid_t *__restrict pid,
212212

213213

214214

215+
215216
case __POSIX_SPAWN_ACTION_CHDIR: {
216217
/* Change direction using `chdir(2)' */
217218
int error;
@@ -225,6 +226,8 @@ NOTHROW_RPC(LIBCCALL libc_posix_fspawn_np)(pid_t *__restrict pid,
225226

226227

227228

229+
230+
228231
case __POSIX_SPAWN_ACTION_FCHDIR: {
229232
/* Change direction using `fchdir(2)' */
230233
int error;
@@ -239,6 +242,7 @@ NOTHROW_RPC(LIBCCALL libc_posix_fspawn_np)(pid_t *__restrict pid,
239242

240243

241244

245+
242246
case __POSIX_SPAWN_ACTION_TCSETPGRP:
243247
/* NOTE: Passing `0' as second argument to `tcsetpgrp()' is the same as `getpid()' */
244248
if unlikely(libc_tcsetpgrp(act->__sa_action.__sa_tcsetpgrp_action.__sa_fd, 0))
@@ -385,6 +389,12 @@ NOTHROW_RPC(LIBCCALL libc_posix_fspawn_np)(pid_t *__restrict pid,
385389

386390

387391
}
392+
393+
if (attrp->__flags & __POSIX_SPAWN_SETSID) {
394+
if unlikely(libc_setsid() < 0)
395+
goto child_error;
396+
}
397+
388398
}
389399
/* When the exec succeeds, the pipe is auto-
390400
* closed because it's marked as O_CLOEXEC! */
@@ -592,6 +602,8 @@ NOTHROW_NCX(LIBCCALL libc_posix_spawnattr_getflags)(posix_spawnattr_t const *__r
592602
*pflags = (short int)(unsigned short int)attr->__flags;
593603
return 0;
594604
}
605+
#include <asm/crt/posix_spawn.h>
606+
#include <asm/os/errno.h>
595607
/* >> posix_spawnattr_setflags(3)
596608
* Specify the set of additional operations to-be performed by the
597609
* child process prior to being started. The given `flags' is a set of:
@@ -601,10 +613,51 @@ NOTHROW_NCX(LIBCCALL libc_posix_spawnattr_getflags)(posix_spawnattr_t const *__r
601613
* - POSIX_SPAWN_SETPGROUP: s.a. posix_spawnattr_setpgroup(3)
602614
* - POSIX_SPAWN_SETSCHEDULER: s.a. posix_spawnattr_setschedpolicy(3)
603615
* - POSIX_SPAWN_SETSCHEDPARAM: s.a. posix_spawnattr_setschedparam(3)
604-
* @return: 0 : Success */
616+
* @return: 0 : Success
617+
* @return: EINVAL: The given `flags' has unknown/unsupported bits set */
605618
INTERN ATTR_SECTION(".text.crt.fs.exec.posix_spawn") ATTR_INOUT(1) errno_t
606619
NOTHROW_NCX(LIBCCALL libc_posix_spawnattr_setflags)(posix_spawnattr_t *__restrict attr,
607620
short int flags) {
621+
enum {
622+
ALL_FLAGS = 0
623+
#ifdef __POSIX_SPAWN_RESETIDS
624+
| __POSIX_SPAWN_RESETIDS
625+
#endif /* __POSIX_SPAWN_RESETIDS */
626+
#ifdef __POSIX_SPAWN_SETPGROUP
627+
| __POSIX_SPAWN_SETPGROUP
628+
#endif /* __POSIX_SPAWN_SETPGROUP */
629+
#ifdef __POSIX_SPAWN_SETSIGDEF
630+
| __POSIX_SPAWN_SETSIGDEF
631+
#endif /* __POSIX_SPAWN_SETSIGDEF */
632+
#ifdef __POSIX_SPAWN_SETSIGMASK
633+
| __POSIX_SPAWN_SETSIGMASK
634+
#endif /* __POSIX_SPAWN_SETSIGMASK */
635+
#ifdef __POSIX_SPAWN_SETSCHEDPARAM
636+
| __POSIX_SPAWN_SETSCHEDPARAM
637+
#endif /* __POSIX_SPAWN_SETSCHEDPARAM */
638+
#ifdef __POSIX_SPAWN_SETSCHEDULER
639+
| __POSIX_SPAWN_SETSCHEDULER
640+
#endif /* __POSIX_SPAWN_SETSCHEDULER */
641+
#ifdef __POSIX_SPAWN_USEVFORK
642+
| __POSIX_SPAWN_USEVFORK
643+
#endif /* __POSIX_SPAWN_USEVFORK */
644+
#ifdef __POSIX_SPAWN_SETSID
645+
| __POSIX_SPAWN_SETSID
646+
#endif /* __POSIX_SPAWN_SETSID */
647+
#ifdef __POSIX_SPAWN_SETCGROUP
648+
| __POSIX_SPAWN_SETCGROUP
649+
#endif /* __POSIX_SPAWN_SETCGROUP */
650+
#ifdef __POSIX_SPAWN_NOEXECERR
651+
| __POSIX_SPAWN_NOEXECERR
652+
#endif /* __POSIX_SPAWN_NOEXECERR */
653+
};
654+
if unlikely((unsigned short int)flags & ~(unsigned short int)ALL_FLAGS) {
655+
656+
return EINVAL;
657+
658+
659+
660+
}
608661
attr->__flags = (uint16_t)(unsigned short int)flags;
609662
return 0;
610663
}

0 commit comments

Comments
 (0)