@@ -91,7 +91,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \
bug-getopt5 tst-getopt_long1 bug-regex34 bug-regex35 \
tst-pathconf tst-getaddrinfo4 tst-rxspencer-no-utf8 \
tst-fnmatch3 bug-regex36 tst-getaddrinfo5 \
- tst-posix_spawn-fd \
+ tst-posix_spawn-fd tst-posix_spawn-setsid \
tst-posix_fadvise tst-posix_fadvise64
xtests := bug-ga2
ifeq (yes,$(build-shared))
@@ -59,6 +59,7 @@ typedef struct
#define POSIX_SPAWN_SETSCHEDULER 0x20
#ifdef __USE_GNU
# define POSIX_SPAWN_USEVFORK 0x40
+# define POSIX_SPAWN_SETSID 0x80
#endif
@@ -25,6 +25,7 @@
| POSIX_SPAWN_SETSIGMASK \
| POSIX_SPAWN_SETSCHEDPARAM \
| POSIX_SPAWN_SETSCHEDULER \
+ | POSIX_SPAWN_SETSID \
| POSIX_SPAWN_USEVFORK)
/* Store flags in the attribute structure. */
new file mode 100644
@@ -0,0 +1,95 @@
+/* Test posix_spawn setsid attribute.
+ Copyright (C) 2017 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <spawn.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <support/check.h>
+
+static void
+do_test_setsid (bool test_setsid)
+{
+ pid_t sid, child_sid;
+ int res;
+
+ /* Current session ID. */
+ sid = getsid(0);
+ if (sid == (pid_t) -1)
+ FAIL_EXIT1 ("getsid (0): %m");
+
+ posix_spawnattr_t attrp;
+ /* posix_spawnattr_init should not fail (it basically memset the
+ attribute). */
+ posix_spawnattr_init (&attrp);
+ if (test_setsid)
+ {
+ res = posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID);
+ if (res != 0)
+ {
+ errno = res;
+ FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
+ }
+ }
+
+ /* Program to run. */
+ char *args[2] = { (char *) "true", NULL };
+ pid_t child;
+
+ res = posix_spawnp (&child, "true", NULL, &attrp, args, environ);
+ /* posix_spawnattr_destroy is noop. */
+ posix_spawnattr_destroy (&attrp);
+
+ if (res != 0)
+ {
+ errno = res;
+ FAIL_EXIT1 ("posix_spawnp: %m");
+ }
+
+ /* Child should have a different session ID than parent. */
+ child_sid = getsid (child);
+
+ if (child_sid == (pid_t) -1)
+ FAIL_EXIT1 ("getsid (%i): %m", child);
+
+ if (test_setsid)
+ {
+ if (child_sid == sid)
+ FAIL_EXIT1 ("child session ID matched parent one");
+ }
+ else
+ {
+ if (child_sid != sid)
+ FAIL_EXIT1 ("child session ID did not match parent one");
+ }
+}
+
+static int
+do_test (void)
+{
+ do_test_setsid (false);
+ do_test_setsid (true);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
@@ -281,6 +281,9 @@ __spawni (pid_t *pid, const char *file,
}
#endif
+ if (!err && (flags & POSIX_SPAWN_SETSID) != 0)
+ err = __proc_setsid (proc);
+
/* Set the process group ID. */
if (!err && (flags & POSIX_SPAWN_SETPGROUP) != 0)
err = __proc_setpgrp (proc, new_pid, attrp->__pgrp);
@@ -101,7 +101,8 @@ __spawni (pid_t *pid, const char *file,
to POSIX. */
|| ((flags & (POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF
| POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER
- | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_RESETIDS)) == 0
+ | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_RESETIDS
+ | POSIX_SPAWN_SETSID)) == 0
&& file_actions == NULL))
new_pid = __vfork ();
else
@@ -159,6 +160,10 @@ __spawni (pid_t *pid, const char *file,
}
#endif
+ if ((flags & POSIX_SPAWN_SETSID) != 0
+ && __setsid () < 0)
+ _exit (SPAWN_ERROR);
+
/* Set the process group ID. */
if ((flags & POSIX_SPAWN_SETPGROUP) != 0
&& __setpgid (0, attrp->__pgrp) != 0)
@@ -177,6 +177,10 @@ __spawni_child (void *arguments)
}
#endif
+ if ((attr->__flags & POSIX_SPAWN_SETSID) != 0
+ && (setsid () < 0))
+ goto fail;
+
/* Set the process group ID. */
if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
&& (ret = __setpgid (0, attr->__pgrp)) != 0)