public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID
@ 2017-04-24 12:47 Adhemerval Zanella
  2017-04-24 12:47 ` [PATCH 2/2] posix: Remove ununsed posix_spawn internal assignment Adhemerval Zanella
  2017-04-24 14:09 ` [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Joseph Myers
  0 siblings, 2 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2017-04-24 12:47 UTC (permalink / raw)
  To: libc-alpha

This patch adds support for the POSIX_SPAWN_SETSID flag.

It was recently accepted by the Austin Group:
http://austingroupbugs.net/view.php?id=1044

Checked on x86_64

	Daurnimator  <quae@daurnimator.com>
	Adhemerval Zanella  <adhemerval.zanella@linaro.org>

	[BZ #21340]
	* conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.
	* posix/Makefile (tests): Add tst-posix_spawn-setsid to list of tests.
	* posix/spawn.h: define POSIX_SPAWN_SETSID flag.
	* posix/spawnattr_setflags.c (ALL_FLAGS): Add POSIX_SPAWN_SETSID to
	valid flags.
	* posix/tst-posix_spawn-setsid.c: Add test for POSIX_SPAWN_SETSID.
	* sysdeps/mach/hurd/spawni.c (__spawni): Implementation of
	POSIX_SPAWN_SETSID.
	* sysdeps/posix/spawni.c (__spawni): Likewise.
	* sysdeps/unix/sysv/linux/spawni.c (__spawni_child): Likewise.
---
 ChangeLog                        | 14 ++++++
 posix/Makefile                   |  2 +-
 posix/spawn.h                    |  1 +
 posix/spawnattr_setflags.c       |  1 +
 posix/tst-posix_spawn-setsid.c   | 95 ++++++++++++++++++++++++++++++++++++++++
 sysdeps/mach/hurd/spawni.c       |  3 ++
 sysdeps/posix/spawni.c           |  7 ++-
 sysdeps/unix/sysv/linux/spawni.c |  4 ++
 8 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100644 posix/tst-posix_spawn-setsid.c

diff --git a/posix/Makefile b/posix/Makefile
index a6586ea..0fc509c 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -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))
diff --git a/posix/spawn.h b/posix/spawn.h
index 36e3867..a1154a3 100644
--- a/posix/spawn.h
+++ b/posix/spawn.h
@@ -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
 
 
diff --git a/posix/spawnattr_setflags.c b/posix/spawnattr_setflags.c
index 9b3d1e0..62d2f00 100644
--- a/posix/spawnattr_setflags.c
+++ b/posix/spawnattr_setflags.c
@@ -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.  */
diff --git a/posix/tst-posix_spawn-setsid.c b/posix/tst-posix_spawn-setsid.c
new file mode 100644
index 0000000..256bd72
--- /dev/null
+++ b/posix/tst-posix_spawn-setsid.c
@@ -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>
diff --git a/sysdeps/mach/hurd/spawni.c b/sysdeps/mach/hurd/spawni.c
index 284875a..7430383 100644
--- a/sysdeps/mach/hurd/spawni.c
+++ b/sysdeps/mach/hurd/spawni.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);
diff --git a/sysdeps/posix/spawni.c b/sysdeps/posix/spawni.c
index 5cc2ad1..9cad25c 100644
--- a/sysdeps/posix/spawni.c
+++ b/sysdeps/posix/spawni.c
@@ -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)
diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c
index d7f9e83..fc5d170 100644
--- a/sysdeps/unix/sysv/linux/spawni.c
+++ b/sysdeps/unix/sysv/linux/spawni.c
@@ -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)
-- 
2.7.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/2] posix: Remove ununsed posix_spawn internal assignment
  2017-04-24 12:47 [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Adhemerval Zanella
@ 2017-04-24 12:47 ` Adhemerval Zanella
  2017-04-24 13:01   ` Andreas Schwab
  2017-04-24 14:09 ` [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Joseph Myers
  1 sibling, 1 reply; 6+ messages in thread
From: Adhemerval Zanella @ 2017-04-24 12:47 UTC (permalink / raw)
  To: libc-alpha

The internal 'ret' variable in '__spawni_child' function is not
used after assignment in most cases.

Checked on x86_64-linux-gnu.

	* sysdeps/unix/sysv/linux/spawni.c (__spawni_child): Remove ununsed
	assignment.
---
 ChangeLog                        |  5 +++++
 sysdeps/unix/sysv/linux/spawni.c | 30 ++++++++++++++----------------
 2 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c
index fc5d170..f93fbe1 100644
--- a/sysdeps/unix/sysv/linux/spawni.c
+++ b/sysdeps/unix/sysv/linux/spawni.c
@@ -124,7 +124,6 @@ __spawni_child (void *arguments)
   struct posix_spawn_args *args = arguments;
   const posix_spawnattr_t *restrict attr = args->attr;
   const posix_spawn_file_actions_t *file_actions = args->fa;
-  int ret;
 
   /* The child must ensure that no signal handler are enabled because it shared
      memory with parent, so the signal disposition must be either SIG_DFL or
@@ -167,12 +166,12 @@ __spawni_child (void *arguments)
   if ((attr->__flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
       == POSIX_SPAWN_SETSCHEDPARAM)
     {
-      if ((ret = __sched_setparam (0, &attr->__sp)) == -1)
+      if (__sched_setparam (0, &attr->__sp) == -1)
 	goto fail;
     }
   else if ((attr->__flags & POSIX_SPAWN_SETSCHEDULER) != 0)
     {
-      if ((ret = __sched_setscheduler (0, attr->__policy, &attr->__sp)) == -1)
+      if (__sched_setscheduler (0, attr->__policy, &attr->__sp) == -1)
 	goto fail;
     }
 #endif
@@ -183,13 +182,13 @@ __spawni_child (void *arguments)
 
   /* Set the process group ID.  */
   if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
-      && (ret = __setpgid (0, attr->__pgrp)) != 0)
+      && (__setpgid (0, attr->__pgrp) != 0))
     goto fail;
 
   /* Set the effective user and group IDs.  */
   if ((attr->__flags & POSIX_SPAWN_RESETIDS) != 0
-      && ((ret = local_seteuid (__getuid ())) != 0
-	  || (ret = local_setegid (__getgid ())) != 0))
+      && ((local_seteuid (__getuid ()) != 0)
+	  || (local_setegid (__getgid ()) != 0)))
     goto fail;
 
   /* Execute the file actions.  */
@@ -206,8 +205,7 @@ __spawni_child (void *arguments)
 	  switch (action->tag)
 	    {
 	    case spawn_do_close:
-	      if ((ret =
-		   close_not_cancel (action->action.close_action.fd)) != 0)
+	      if (close_not_cancel (action->action.close_action.fd) != 0)
 		{
 		  if (!have_fdlimit)
 		    {
@@ -232,10 +230,10 @@ __spawni_child (void *arguments)
 		   paths (like /dev/watchdog).  */
 		close_not_cancel (action->action.open_action.fd);
 
-		ret = open_not_cancel (action->action.open_action.path,
-				       action->action.
-				       open_action.oflag | O_LARGEFILE,
-				       action->action.open_action.mode);
+		int ret = open_not_cancel (action->action.open_action.path,
+					   action->action.
+					   open_action.oflag | O_LARGEFILE,
+					   action->action.open_action.mode);
 
 		if (ret == -1)
 		  goto fail;
@@ -245,19 +243,19 @@ __spawni_child (void *arguments)
 		/* Make sure the desired file descriptor is used.  */
 		if (ret != action->action.open_action.fd)
 		  {
-		    if ((ret = __dup2 (new_fd, action->action.open_action.fd))
+		    if (__dup2 (new_fd, action->action.open_action.fd)
 			!= action->action.open_action.fd)
 		      goto fail;
 
-		    if ((ret = close_not_cancel (new_fd)) != 0)
+		    if (close_not_cancel (new_fd) != 0)
 		      goto fail;
 		  }
 	      }
 	      break;
 
 	    case spawn_do_dup2:
-	      if ((ret = __dup2 (action->action.dup2_action.fd,
-				 action->action.dup2_action.newfd))
+	      if (__dup2 (action->action.dup2_action.fd,
+			  action->action.dup2_action.newfd)
 		  != action->action.dup2_action.newfd)
 		goto fail;
 	      break;
-- 
2.7.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] posix: Remove ununsed posix_spawn internal assignment
  2017-04-24 12:47 ` [PATCH 2/2] posix: Remove ununsed posix_spawn internal assignment Adhemerval Zanella
@ 2017-04-24 13:01   ` Andreas Schwab
  2017-04-24 13:29     ` Adhemerval Zanella
  0 siblings, 1 reply; 6+ messages in thread
From: Andreas Schwab @ 2017-04-24 13:01 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha

On Apr 24 2017, Adhemerval Zanella <adhemerval.zanella@linaro.org> wrote:

> 	* sysdeps/unix/sysv/linux/spawni.c (__spawni_child): Remove ununsed
> 	assignment.

Ok.

> @@ -183,13 +182,13 @@ __spawni_child (void *arguments)
>  
>    /* Set the process group ID.  */
>    if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
> -      && (ret = __setpgid (0, attr->__pgrp)) != 0)
> +      && (__setpgid (0, attr->__pgrp) != 0))
>      goto fail;
>  
>    /* Set the effective user and group IDs.  */
>    if ((attr->__flags & POSIX_SPAWN_RESETIDS) != 0
> -      && ((ret = local_seteuid (__getuid ())) != 0
> -	  || (ret = local_setegid (__getgid ())) != 0))
> +      && ((local_seteuid (__getuid ()) != 0)
> +	  || (local_setegid (__getgid ()) != 0)))
>      goto fail;
>  
>    /* Execute the file actions.  */

The extra pair of parens around the expressions are no longer needed.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] posix: Remove ununsed posix_spawn internal assignment
  2017-04-24 13:01   ` Andreas Schwab
@ 2017-04-24 13:29     ` Adhemerval Zanella
  0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2017-04-24 13:29 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: libc-alpha



On 24/04/2017 10:01, Andreas Schwab wrote:
> On Apr 24 2017, Adhemerval Zanella <adhemerval.zanella@linaro.org> wrote:
> 
>> 	* sysdeps/unix/sysv/linux/spawni.c (__spawni_child): Remove ununsed
>> 	assignment.
> 
> Ok.
> 
>> @@ -183,13 +182,13 @@ __spawni_child (void *arguments)
>>  
>>    /* Set the process group ID.  */
>>    if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
>> -      && (ret = __setpgid (0, attr->__pgrp)) != 0)
>> +      && (__setpgid (0, attr->__pgrp) != 0))
>>      goto fail;
>>  
>>    /* Set the effective user and group IDs.  */
>>    if ((attr->__flags & POSIX_SPAWN_RESETIDS) != 0
>> -      && ((ret = local_seteuid (__getuid ())) != 0
>> -	  || (ret = local_setegid (__getgid ())) != 0))
>> +      && ((local_seteuid (__getuid ()) != 0)
>> +	  || (local_setegid (__getgid ()) != 0)))
>>      goto fail;
>>  
>>    /* Execute the file actions.  */
> 
> The extra pair of parens around the expressions are no longer needed.
> 
> Andreas.
> 

Right, I will remove then on commit.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-24 12:47 [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Adhemerval Zanella
  2017-04-24 12:47 ` [PATCH 2/2] posix: Remove ununsed posix_spawn internal assignment Adhemerval Zanella
@ 2017-04-24 14:09 ` Joseph Myers
  2017-04-24 18:05   ` Adhemerval Zanella
  1 sibling, 1 reply; 6+ messages in thread
From: Joseph Myers @ 2017-04-24 14:09 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha

On Mon, 24 Apr 2017, Adhemerval Zanella wrote:

> This patch adds support for the POSIX_SPAWN_SETSID flag.
> 
> It was recently accepted by the Austin Group:
> http://austingroupbugs.net/view.php?id=1044
> 
> Checked on x86_64
> 
> 	Daurnimator  <quae@daurnimator.com>
> 	Adhemerval Zanella  <adhemerval.zanella@linaro.org>
> 
> 	[BZ #21340]
> 	* conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.

The ChangeLog entry needs updating to reflect the current patch contents.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-24 14:09 ` [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Joseph Myers
@ 2017-04-24 18:05   ` Adhemerval Zanella
  0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2017-04-24 18:05 UTC (permalink / raw)
  To: Joseph Myers; +Cc: libc-alpha



On 24/04/2017 11:09, Joseph Myers wrote:
> On Mon, 24 Apr 2017, Adhemerval Zanella wrote:
> 
>> This patch adds support for the POSIX_SPAWN_SETSID flag.
>>
>> It was recently accepted by the Austin Group:
>> http://austingroupbugs.net/view.php?id=1044
>>
>> Checked on x86_64
>>
>> 	Daurnimator  <quae@daurnimator.com>
>> 	Adhemerval Zanella  <adhemerval.zanella@linaro.org>
>>
>> 	[BZ #21340]
>> 	* conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.
> 
> The ChangeLog entry needs updating to reflect the current patch contents.
> 

In fact I did removed it my local commit, I just forgot to update
commit message.  I will also add this news entry:

* posix_spawnattr_setflags now supports POSIX_SPAWN_SETSID flag
  to create a new session ID for the posix_spawn and posix_spawnp.
  It is scheduled to be added on next major revision of POSIX,
  so current support is enabled with _GNU_SOURCE.  

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-04-24 18:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-24 12:47 [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Adhemerval Zanella
2017-04-24 12:47 ` [PATCH 2/2] posix: Remove ununsed posix_spawn internal assignment Adhemerval Zanella
2017-04-24 13:01   ` Andreas Schwab
2017-04-24 13:29     ` Adhemerval Zanella
2017-04-24 14:09 ` [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Joseph Myers
2017-04-24 18:05   ` Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).