public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
@ 2017-04-05  5:41 daurnimator
  2017-04-05  8:34 ` Florian Weimer
  2017-04-05 12:31 ` Joseph Myers
  0 siblings, 2 replies; 26+ messages in thread
From: daurnimator @ 2017-04-05  5:41 UTC (permalink / raw)
  To: libc-alpha; +Cc: daurnimator

I forgot to actually run my new test when I sent in v2.
Turns out I made a terrible mistake and didn't actually have the right
result check in the linux code. (setsid returns the new sid on success; not 0)

--- >8 ---
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

2017-04-05  Daurnimator  <quae@daurnimator.com>

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

diff --git a/conform/data/spawn.h-data b/conform/data/spawn.h-data
index fb206f7ecf..bcba36c492 100644
--- a/conform/data/spawn.h-data
+++ b/conform/data/spawn.h-data
@@ -14,6 +14,7 @@ constant POSIX_SPAWN_SETSCHEDPARAM
 constant POSIX_SPAWN_SETSCHEDULER
 constant POSIX_SPAWN_SETSIGDEF
 constant POSIX_SPAWN_SETSIGMASK
+constant POSIX_SPAWN_SETSID
 
 function int posix_spawnattr_destroy (posix_spawnattr_t*)
 function int posix_spawnattr_getsigdefault (const posix_spawnattr_t*, sigset_t*)
diff --git a/posix/Makefile b/posix/Makefile
index ae17646323..a9f53a973d 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -90,7 +90,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 36e3867e17..8d2ace1b87 100644
--- a/posix/spawn.h
+++ b/posix/spawn.h
@@ -60,6 +60,7 @@ typedef struct
 #ifdef __USE_GNU
 # define POSIX_SPAWN_USEVFORK		0x40
 #endif
+#define POSIX_SPAWN_SETSID		0x80
 
 
 __BEGIN_DECLS
diff --git a/posix/spawnattr_setflags.c b/posix/spawnattr_setflags.c
index 9b3d1e022a..62d2f00c20 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 0000000000..b3cd5e54b1
--- /dev/null
+++ b/posix/tst-posix_spawn-setsid.c
@@ -0,0 +1,70 @@
+/* Test that spawn attribute setsid works.
+   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>
+
+static int
+do_test (void)
+{
+  posix_spawnattr_t attrp;
+  int res;
+  int child_pid;
+  int sid, child_sid;
+  char *args[2];
+
+  /* record sid for current process */
+  sid = getsid(0);
+
+  posix_spawnattr_init(&attrp);
+  if (posix_spawnattr_setflags(&attrp, POSIX_SPAWN_SETSID))
+    {
+      printf("error: posix_spawnattr_setflags: %m\n");
+      return 1;
+    }
+
+  /* run the program 'true' */
+  args[0] = (char *)"true";
+  args[1] = NULL;
+
+  res = posix_spawnp(&child_pid, "true", NULL, &attrp, args, environ);
+  posix_spawnattr_destroy(&attrp);
+  if (res)
+    {
+      printf("error: posix_spawnp: %m\n");
+      return 1;
+    }
+
+  /* child should have a different sid */
+  child_sid = getsid(child_pid);
+  if (child_sid == sid)
+    {
+      printf("error: child sid matches\n");
+      return 1;
+    }
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/sysdeps/mach/hurd/spawni.c b/sysdeps/mach/hurd/spawni.c
index 284875ac30..74303839e4 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 5cc2ad1363..9cad25ca4e 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 d7f9e83575..3cf77d50c3 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
+      && (ret = __setsid ()) < 0)
+    goto fail;
+
   /* Set the process group ID.  */
   if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
       && (ret = __setpgid (0, attr->__pgrp)) != 0)
-- 
2.11.1

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-05  5:41 [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID daurnimator
@ 2017-04-05  8:34 ` Florian Weimer
  2017-04-05 11:01   ` Daurnimator
  2017-04-05 12:31 ` Joseph Myers
  1 sibling, 1 reply; 26+ messages in thread
From: Florian Weimer @ 2017-04-05  8:34 UTC (permalink / raw)
  To: daurnimator; +Cc: libc-alpha

On 04/05/2017 07:41 AM, daurnimator wrote:

> +static int
> +do_test (void)
> +{
> +  posix_spawnattr_t attrp;
> +  int res;
> +  int child_pid;
> +  int sid, child_sid;
> +  char *args[2];

Thanks for writing the test.  There are some style issues.  We prefer 
in-line declarations these days, and there should be a space before “(” 
in a function call.

Please use the new test skeleton in <support/test-driver.c>.  (Sorry, 
still need to update the wiki.)

> +  posix_spawnattr_init(&attrp);
> +  if (posix_spawnattr_setflags(&attrp, POSIX_SPAWN_SETSID))
> +    {
> +      printf("error: posix_spawnattr_setflags: %m\n");

You need to capture the return value of posix_spawnattr_setflags and 
assign it to errno prior to using %m.  You can include <support/check.h> 
and use FAIL_EXIT1.

> +  /* run the program 'true' */
> +  args[0] = (char *)"true";
> +  args[1] = NULL;
> +
> +  res = posix_spawnp(&child_pid, "true", NULL, &attrp, args, environ);
> +  posix_spawnattr_destroy(&attrp);
> +  if (res)
> +    {
> +      printf("error: posix_spawnp: %m\n");

See above, needs to set errno.

> +      return 1;
> +    }
> +
> +  /* child should have a different sid */
> +  child_sid = getsid(child_pid);

Please add error checking for the getsid result.

> +  if (child_sid == sid)
> +    {
> +      printf("error: child sid matches\n");
> +      return 1;
> +    }

I think you should run the test case twice, and whether 
POSIX_SPAWN_SETSID is applied should be controlled by a flag.  Then you 
can check if the getsid result remains unchanged or is changed, as 
appropriate.

You aren't listed in MAINTAINERS on the wiki.  Do you have a GNU 
copyright assignment on file?

Thanks,
Florian

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-05  8:34 ` Florian Weimer
@ 2017-04-05 11:01   ` Daurnimator
  2017-04-06 12:37     ` Florian Weimer
  0 siblings, 1 reply; 26+ messages in thread
From: Daurnimator @ 2017-04-05 11:01 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha

On 5 April 2017 at 18:34, Florian Weimer <fweimer@redhat.com> wrote:
> On 04/05/2017 07:41 AM, daurnimator wrote:
>
>> +static int
>> +do_test (void)
>> +{
>> +  posix_spawnattr_t attrp;
>> +  int res;
>> +  int child_pid;
>> +  int sid, child_sid;
>> +  char *args[2];
>
>
> Thanks for writing the test.  There are some style issues.  We prefer
> in-line declarations these days

Do you mean putting 'static int do_test (void) {' on one line?

> and there should be a space before “(” in a
> function call.

There already was?

> Please use the new test skeleton in <support/test-driver.c>.  (Sorry, still
> need to update the wiki.)

I just worked from the existing test 'tst-posix_spawn-fd.c'.

> You aren't listed in MAINTAINERS on the wiki.  Do you have a GNU copyright
> assignment on file?

I do not.

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-05  5:41 [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID daurnimator
  2017-04-05  8:34 ` Florian Weimer
@ 2017-04-05 12:31 ` Joseph Myers
  2017-04-05 12:44   ` Joseph Myers
  2017-04-05 12:58   ` Szabolcs Nagy
  1 sibling, 2 replies; 26+ messages in thread
From: Joseph Myers @ 2017-04-05 12:31 UTC (permalink / raw)
  To: daurnimator; +Cc: libc-alpha

On Wed, 5 Apr 2017, daurnimator wrote:

> 2017-04-05  Daurnimator  <quae@daurnimator.com>
> 
>     [BZ #21340]
>     * conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.

You should never add a symbol to the conform/ expectations unless it is 
actually defined by a released standard.  In a case like this, of a symbol 
intended to be added in a future version, it should not be in the conform/ 
data, and should be conditioned on __USE_GNU in the headers.

Note that http://austingroupbugs.net/view.php?id=1044 has the issue8 tag, 
meaning it's for the next major revision of POSIX, not a bug fix for a TC 
for the current revision.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-05 12:31 ` Joseph Myers
@ 2017-04-05 12:44   ` Joseph Myers
  2017-04-05 12:58   ` Szabolcs Nagy
  1 sibling, 0 replies; 26+ messages in thread
From: Joseph Myers @ 2017-04-05 12:44 UTC (permalink / raw)
  To: daurnimator; +Cc: libc-alpha

Also, new user-visible features should have a NEWS entry explaining the 
feature (which might in this case say it's accepted for the next major 
revision of POSIX, as well as something about what it does).

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-05 12:31 ` Joseph Myers
  2017-04-05 12:44   ` Joseph Myers
@ 2017-04-05 12:58   ` Szabolcs Nagy
  2017-04-05 15:02     ` Joseph Myers
  1 sibling, 1 reply; 26+ messages in thread
From: Szabolcs Nagy @ 2017-04-05 12:58 UTC (permalink / raw)
  To: Joseph Myers, daurnimator; +Cc: nd, libc-alpha

On 05/04/17 13:31, Joseph Myers wrote:
> On Wed, 5 Apr 2017, daurnimator wrote:
>> 2017-04-05  Daurnimator  <quae@daurnimator.com>
>>
>>     [BZ #21340]
>>     * conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.
> 
> You should never add a symbol to the conform/ expectations unless it is 
> actually defined by a released standard.  In a case like this, of a symbol 
> intended to be added in a future version, it should not be in the conform/ 
> data, and should be conditioned on __USE_GNU in the headers.

i dont see why the __USE_GNU is necessary.

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-05 12:58   ` Szabolcs Nagy
@ 2017-04-05 15:02     ` Joseph Myers
  0 siblings, 0 replies; 26+ messages in thread
From: Joseph Myers @ 2017-04-05 15:02 UTC (permalink / raw)
  To: Szabolcs Nagy; +Cc: daurnimator, nd, libc-alpha

On Wed, 5 Apr 2017, Szabolcs Nagy wrote:

> On 05/04/17 13:31, Joseph Myers wrote:
> > On Wed, 5 Apr 2017, daurnimator wrote:
> >> 2017-04-05  Daurnimator  <quae@daurnimator.com>
> >>
> >>     [BZ #21340]
> >>     * conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.
> > 
> > You should never add a symbol to the conform/ expectations unless it is 
> > actually defined by a released standard.  In a case like this, of a symbol 
> > intended to be added in a future version, it should not be in the conform/ 
> > data, and should be conditioned on __USE_GNU in the headers.
> 
> i dont see why the __USE_GNU is necessary.

Although there are a few exceptions (e.g. errno numbers), common practice 
in glibc is not to rely on reserved namespaces other than _[_A-Z]* and to 
declare symbols only for the relevant standards (and likewise for 
link-time namespace).

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-05 11:01   ` Daurnimator
@ 2017-04-06 12:37     ` Florian Weimer
  2017-04-06 13:22       ` Zack Weinberg
  2017-04-09 13:22       ` Daurnimator
  0 siblings, 2 replies; 26+ messages in thread
From: Florian Weimer @ 2017-04-06 12:37 UTC (permalink / raw)
  To: Daurnimator; +Cc: libc-alpha

On 04/05/2017 01:01 PM, Daurnimator wrote:

>> You aren't listed in MAINTAINERS on the wiki.  Do you have a GNU copyright
>> assignment on file?
>
> I do not.

Would you be willing to assign copyright for this and future changes?

If yes, please pick request-assign.future from here:

<https://sourceware.org/glibc/wiki/Contribution%20checklist#FSF_copyright_Assignment>

and send the filled-out form to <assign@gnu.org>.  As far as I 
understand it, citizens from most countries can complete the process 
electronically these days.

Thanks,
Florian

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-06 12:37     ` Florian Weimer
@ 2017-04-06 13:22       ` Zack Weinberg
  2017-04-06 13:23         ` Florian Weimer
  2017-04-09 13:22       ` Daurnimator
  1 sibling, 1 reply; 26+ messages in thread
From: Zack Weinberg @ 2017-04-06 13:22 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Daurnimator, GNU C Library

On Thu, Apr 6, 2017 at 8:37 AM, Florian Weimer <fweimer@redhat.com> wrote:
> On 04/05/2017 01:01 PM, Daurnimator wrote:
>
>>> You aren't listed in MAINTAINERS on the wiki.  Do you have a GNU
>>> copyright
>>> assignment on file?
>>
>> I do not.
>
> Would you be willing to assign copyright for this and future changes?

It would obviously be better to have a copyright assignment, but this
particular patch may be uncopyrightable on the grounds that there is
no other way to implement the specification.  I think if I had been
given the assignment I would have written token-for-token identical
code.

zw

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-06 13:22       ` Zack Weinberg
@ 2017-04-06 13:23         ` Florian Weimer
  2017-04-06 13:24           ` Zack Weinberg
  0 siblings, 1 reply; 26+ messages in thread
From: Florian Weimer @ 2017-04-06 13:23 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Daurnimator, GNU C Library

On 04/06/2017 03:22 PM, Zack Weinberg wrote:
> On Thu, Apr 6, 2017 at 8:37 AM, Florian Weimer <fweimer@redhat.com> wrote:
>> On 04/05/2017 01:01 PM, Daurnimator wrote:
>>
>>>> You aren't listed in MAINTAINERS on the wiki.  Do you have a GNU
>>>> copyright
>>>> assignment on file?
>>>
>>> I do not.
>>
>> Would you be willing to assign copyright for this and future changes?
>
> It would obviously be better to have a copyright assignment, but this
> particular patch may be uncopyrightable on the grounds that there is
> no other way to implement the specification.  I think if I had been
> given the assignment I would have written token-for-token identical
> code.

This argument does not apply to the test case.

Thanks,
Florian

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-06 13:23         ` Florian Weimer
@ 2017-04-06 13:24           ` Zack Weinberg
  2017-04-07 12:27             ` Daurnimator
  0 siblings, 1 reply; 26+ messages in thread
From: Zack Weinberg @ 2017-04-06 13:24 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Daurnimator, GNU C Library

On Thu, Apr 6, 2017 at 9:22 AM, Florian Weimer <fweimer@redhat.com> wrote:
> On 04/06/2017 03:22 PM, Zack Weinberg wrote:
>>
>> It would obviously be better to have a copyright assignment, but this
>> particular patch may be uncopyrightable on the grounds that there is
>> no other way to implement the specification.  I think if I had been
>> given the assignment I would have written token-for-token identical
>> code.
>
> This argument does not apply to the test case.

Oh, good point, I didn't notice that there was a test case in the revised patch.

zw

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-06 13:24           ` Zack Weinberg
@ 2017-04-07 12:27             ` Daurnimator
  2017-04-07 13:46               ` Zack Weinberg
  0 siblings, 1 reply; 26+ messages in thread
From: Daurnimator @ 2017-04-07 12:27 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Florian Weimer, GNU C Library

On 6 April 2017 at 23:24, Zack Weinberg <zackw@panix.com> wrote:
> On Thu, Apr 6, 2017 at 9:22 AM, Florian Weimer <fweimer@redhat.com> wrote:
>> On 04/06/2017 03:22 PM, Zack Weinberg wrote:
>>>
>>> It would obviously be better to have a copyright assignment, but this
>>> particular patch may be uncopyrightable on the grounds that there is
>>> no other way to implement the specification.  I think if I had been
>>> given the assignment I would have written token-for-token identical
>>> code.
>>
>> This argument does not apply to the test case.
>
> Oh, good point, I didn't notice that there was a test case in the revised patch.
>
> zw

FWIW I'd find it easier to resubmit the patch without the test case
than to fill out the copyright assignment...

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-07 12:27             ` Daurnimator
@ 2017-04-07 13:46               ` Zack Weinberg
  0 siblings, 0 replies; 26+ messages in thread
From: Zack Weinberg @ 2017-04-07 13:46 UTC (permalink / raw)
  To: Daurnimator; +Cc: Florian Weimer, GNU C Library

On Fri, Apr 7, 2017 at 8:27 AM, Daurnimator <quae@daurnimator.com> wrote:
> On 6 April 2017 at 23:24, Zack Weinberg <zackw@panix.com> wrote:
>> Oh, good point, I didn't notice that there was a test case in the revised patch.
>
> FWIW I'd find it easier to resubmit the patch without the test case
> than to fill out the copyright assignment...

I understand that the paperwork is a pain, but we really do want the test case.

zw

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-06 12:37     ` Florian Weimer
  2017-04-06 13:22       ` Zack Weinberg
@ 2017-04-09 13:22       ` Daurnimator
  2017-04-21  5:04         ` Daurnimator
  1 sibling, 1 reply; 26+ messages in thread
From: Daurnimator @ 2017-04-09 13:22 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On 6 April 2017 at 22:37, Florian Weimer <fweimer@redhat.com> wrote:
> Would you be willing to assign copyright for this and future changes?
>
> If yes, please pick request-assign.future from here:
>
> <https://sourceware.org/glibc/wiki/Contribution%20checklist#FSF_copyright_Assignment>
>
> and send the filled-out form to <assign@gnu.org>.  As far as I understand
> it, citizens from most countries can complete the process electronically
> these days.

Sorry, but I'm not willing to link my legal name to this pseudonym.

Please still consider merging my changes (as they should be uncopyrightable).
However as I understand it, you will have to re-write the test case.

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-09 13:22       ` Daurnimator
@ 2017-04-21  5:04         ` Daurnimator
  2017-04-21 12:39           ` Adhemerval Zanella
  2017-04-21 14:13           ` Adhemerval Zanella
  0 siblings, 2 replies; 26+ messages in thread
From: Daurnimator @ 2017-04-21  5:04 UTC (permalink / raw)
  To: GNU C Library; +Cc: Zack Weinberg, Florian Weimer

On 9 April 2017 at 23:21, Daurnimator <quae@daurnimator.com> wrote:
> On 6 April 2017 at 22:37, Florian Weimer <fweimer@redhat.com> wrote:
>> Would you be willing to assign copyright for this and future changes?
>>
>> If yes, please pick request-assign.future from here:
>>
>> <https://sourceware.org/glibc/wiki/Contribution%20checklist#FSF_copyright_Assignment>
>>
>> and send the filled-out form to <assign@gnu.org>.  As far as I understand
>> it, citizens from most countries can complete the process electronically
>> these days.
>
> Sorry, but I'm not willing to link my legal name to this pseudonym.
>
> Please still consider merging my changes (as they should be uncopyrightable).
> However as I understand it, you will have to re-write the test case.

I'd still love to get this patch merged; is someone willing to rewrite
the test case?

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21  5:04         ` Daurnimator
@ 2017-04-21 12:39           ` Adhemerval Zanella
  2017-04-21 14:13           ` Adhemerval Zanella
  1 sibling, 0 replies; 26+ messages in thread
From: Adhemerval Zanella @ 2017-04-21 12:39 UTC (permalink / raw)
  To: libc-alpha



On 21/04/2017 02:03, Daurnimator wrote:
> On 9 April 2017 at 23:21, Daurnimator <quae@daurnimator.com> wrote:
>> On 6 April 2017 at 22:37, Florian Weimer <fweimer@redhat.com> wrote:
>>> Would you be willing to assign copyright for this and future changes?
>>>
>>> If yes, please pick request-assign.future from here:
>>>
>>> <https://sourceware.org/glibc/wiki/Contribution%20checklist#FSF_copyright_Assignment>
>>>
>>> and send the filled-out form to <assign@gnu.org>.  As far as I understand
>>> it, citizens from most countries can complete the process electronically
>>> these days.
>>
>> Sorry, but I'm not willing to link my legal name to this pseudonym.
>>
>> Please still consider merging my changes (as they should be uncopyrightable).
>> However as I understand it, you will have to re-write the test case.
> 
> I'd still love to get this patch merged; is someone willing to rewrite
> the test case?
> 

I will work on this.

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21  5:04         ` Daurnimator
  2017-04-21 12:39           ` Adhemerval Zanella
@ 2017-04-21 14:13           ` Adhemerval Zanella
  2017-04-21 14:24             ` Florian Weimer
  2017-04-21 18:24             ` Joseph Myers
  1 sibling, 2 replies; 26+ messages in thread
From: Adhemerval Zanella @ 2017-04-21 14:13 UTC (permalink / raw)
  To: libc-alpha; +Cc: Zack Weinberg, Florian Weimer

On 21/04/2017 02:03, Daurnimator wrote:
> On 9 April 2017 at 23:21, Daurnimator <quae@daurnimator.com> wrote:
>> On 6 April 2017 at 22:37, Florian Weimer <fweimer@redhat.com> wrote:
>>> Would you be willing to assign copyright for this and future changes?
>>>
>>> If yes, please pick request-assign.future from here:
>>>
>>> <https://sourceware.org/glibc/wiki/Contribution%20checklist#FSF_copyright_Assignment>
>>>
>>> and send the filled-out form to <assign@gnu.org>.  As far as I understand
>>> it, citizens from most countries can complete the process electronically
>>> these days.
>>
>> Sorry, but I'm not willing to link my legal name to this pseudonym.
>>
>> Please still consider merging my changes (as they should be uncopyrightable).
>> However as I understand it, you will have to re-write the test case.
> 
> I'd still love to get this patch merged; is someone willing to rewrite
> the test case?
> 

What about now? I have adjusted the testcase using support library and
fixed some nits in ChangeLog.

--

	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                        | 15 +++++++++
 conform/data/spawn.h-data        |  1 +
 posix/Makefile                   |  2 +-
 posix/spawn.h                    |  1 +
 posix/spawnattr_setflags.c       |  1 +
 posix/tst-posix_spawn-setsid.c   | 68 ++++++++++++++++++++++++++++++++++++++++
 sysdeps/mach/hurd/spawni.c       |  3 ++
 sysdeps/posix/spawni.c           |  7 ++++-
 sysdeps/unix/sysv/linux/spawni.c |  4 +++
 9 files changed, 100 insertions(+), 2 deletions(-)
 create mode 100644 posix/tst-posix_spawn-setsid.c

diff --git a/conform/data/spawn.h-data b/conform/data/spawn.h-data
index fb206f7..bcba36c 100644
--- a/conform/data/spawn.h-data
+++ b/conform/data/spawn.h-data
@@ -14,6 +14,7 @@ constant POSIX_SPAWN_SETSCHEDPARAM
 constant POSIX_SPAWN_SETSCHEDULER
 constant POSIX_SPAWN_SETSIGDEF
 constant POSIX_SPAWN_SETSIGMASK
+constant POSIX_SPAWN_SETSID
 
 function int posix_spawnattr_destroy (posix_spawnattr_t*)
 function int posix_spawnattr_getsigdefault (const posix_spawnattr_t*, sigset_t*)
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..8d2ace1 100644
--- a/posix/spawn.h
+++ b/posix/spawn.h
@@ -60,6 +60,7 @@ typedef struct
 #ifdef __USE_GNU
 # define POSIX_SPAWN_USEVFORK		0x40
 #endif
+#define POSIX_SPAWN_SETSID		0x80
 
 
 __BEGIN_DECLS
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..246b39f
--- /dev/null
+++ b/posix/tst-posix_spawn-setsid.c
@@ -0,0 +1,68 @@
+/* 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 int
+do_test (void)
+{
+  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 (&attrp);
+  if (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID))
+    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)
+    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 (child_sid == sid)
+    FAIL_EXIT1 ("child sid matches parent one");
+
+  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..3cf77d5 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
+      && (ret = __setsid ()) < 0)
+    goto fail;
+
   /* Set the process group ID.  */
   if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
       && (ret = __setpgid (0, attr->__pgrp)) != 0)

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 14:13           ` Adhemerval Zanella
@ 2017-04-21 14:24             ` Florian Weimer
  2017-04-21 18:29               ` Adhemerval Zanella
  2017-04-21 18:24             ` Joseph Myers
  1 sibling, 1 reply; 26+ messages in thread
From: Florian Weimer @ 2017-04-21 14:24 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha; +Cc: Zack Weinberg

On 04/21/2017 04:13 PM, Adhemerval Zanella wrote:

> 	* conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.

I think this has to go away ...


> diff --git a/posix/spawn.h b/posix/spawn.h
> index 36e3867..8d2ace1 100644
> --- a/posix/spawn.h
> +++ b/posix/spawn.h
> @@ -60,6 +60,7 @@ typedef struct
>   #ifdef __USE_GNU
>   # define POSIX_SPAWN_USEVFORK		0x40
>   #endif
> +#define POSIX_SPAWN_SETSID		0x80


... because this has to be governed by __GNU_SOURCE.

> +static int
> +do_test (void)
> +{
> +  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 (&attrp);
> +  if (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID))
> +    FAIL_EXIT1 ("posix_spawnattr_setflags: %m");

Please run the test twice, once with setting POSIX_SPAWN_SETSID, and 
once without.

Thanks,
Florian

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 14:13           ` Adhemerval Zanella
  2017-04-21 14:24             ` Florian Weimer
@ 2017-04-21 18:24             ` Joseph Myers
  2017-04-21 18:26               ` Adhemerval Zanella
  1 sibling, 1 reply; 26+ messages in thread
From: Joseph Myers @ 2017-04-21 18:24 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Zack Weinberg, Florian Weimer

On Fri, 21 Apr 2017, Adhemerval Zanella wrote:

> 	* conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.

See my previous comments.  http://austingroupbugs.net/view.php?id=1044 has 
the issue8 tag - this is *not* a bug fix to the standard for a future TC 
(which would be applied unconditionally to the headers and expectations), 
it's a new feature for the next major POSIX revision, so does not go in 
conform/ expectations until such a revision exists (at which point it 
would be appropriately conditional in those expectations) and is 
conditioned on __USE_GNU until such a revision exists (at which point it 
would be conditional on the feature test macros for the new revision).

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 18:24             ` Joseph Myers
@ 2017-04-21 18:26               ` Adhemerval Zanella
  0 siblings, 0 replies; 26+ messages in thread
From: Adhemerval Zanella @ 2017-04-21 18:26 UTC (permalink / raw)
  To: Joseph Myers; +Cc: libc-alpha, Zack Weinberg, Florian Weimer



On 21/04/2017 12:45, Joseph Myers wrote:
> On Fri, 21 Apr 2017, Adhemerval Zanella wrote:
> 
>> 	* conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.
> 
> See my previous comments.  http://austingroupbugs.net/view.php?id=1044 has 
> the issue8 tag - this is *not* a bug fix to the standard for a future TC 
> (which would be applied unconditionally to the headers and expectations), 
> it's a new feature for the next major POSIX revision, so does not go in 
> conform/ expectations until such a revision exists (at which point it 
> would be appropriately conditional in those expectations) and is 
> conditioned on __USE_GNU until such a revision exists (at which point it 
> would be conditional on the feature test macros for the new revision).
> 

Thanks for the point, Florian has also brought it to my attention and
I have sent an updated patch (which I can't get a link due sourceware.org
outage).

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 14:24             ` Florian Weimer
@ 2017-04-21 18:29               ` Adhemerval Zanella
  2017-04-21 19:53                 ` Joseph Myers
  2017-04-21 20:04                 ` Florian Weimer
  0 siblings, 2 replies; 26+ messages in thread
From: Adhemerval Zanella @ 2017-04-21 18:29 UTC (permalink / raw)
  To: Florian Weimer, libc-alpha; +Cc: Zack Weinberg



On 21/04/2017 11:24, Florian Weimer wrote:
> On 04/21/2017 04:13 PM, Adhemerval Zanella wrote:
> 
>>     * conform/data/spawn.h-data: Add POSIX_SPAWN_SETSID flag.
> 
> I think this has to go away ...
> 
> 
>> diff --git a/posix/spawn.h b/posix/spawn.h
>> index 36e3867..8d2ace1 100644
>> --- a/posix/spawn.h
>> +++ b/posix/spawn.h
>> @@ -60,6 +60,7 @@ typedef struct
>>   #ifdef __USE_GNU
>>   # define POSIX_SPAWN_USEVFORK        0x40
>>   #endif
>> +#define POSIX_SPAWN_SETSID        0x80
> 
> 
> ... because this has to be governed by __GNU_SOURCE.
> 
>> +static int
>> +do_test (void)
>> +{
>> +  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 (&attrp);
>> +  if (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID))
>> +    FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
> 
> Please run the test twice, once with setting POSIX_SPAWN_SETSID, and once without.
> 
> Thanks,
> Florian

I think this iteration should fix the issues you bring.

--

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

        [BZ #21340]
        * 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.


---

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..c2d9534
--- /dev/null
+++ b/posix/tst-posix_spawn-setsid.c
@@ -0,0 +1,92 @@
+/* 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 int
+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)
+    {
+      if (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID))
+	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)
+    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");
+    }
+
+  return 0;
+}
+
+static int
+do_test (void)
+{
+  int ret = 0;
+
+  ret += do_test_setsid (false);
+  ret += 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..3cf77d5 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
+      && (ret = __setsid ()) < 0)
+    goto fail;
+
   /* Set the process group ID.  */
   if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
       && (ret = __setpgid (0, attr->__pgrp)) != 0)

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 18:29               ` Adhemerval Zanella
@ 2017-04-21 19:53                 ` Joseph Myers
  2017-04-21 20:43                   ` Adhemerval Zanella
  2017-04-21 20:04                 ` Florian Weimer
  1 sibling, 1 reply; 26+ messages in thread
From: Joseph Myers @ 2017-04-21 19:53 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: Florian Weimer, libc-alpha, Zack Weinberg

On Fri, 21 Apr 2017, Adhemerval Zanella wrote:

> I think this iteration should fix the issues you bring.
> 
> --
> 
> 	Daurnimator  <quae@daurnimator.com>
>         Adhemerval Zanella  <adhemerval.zanella@linaro.org>
> 
>         [BZ #21340]
>         * 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.

I think you should also add a NEWS entry for the new feature.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 18:29               ` Adhemerval Zanella
  2017-04-21 19:53                 ` Joseph Myers
@ 2017-04-21 20:04                 ` Florian Weimer
  2017-04-21 20:43                   ` Adhemerval Zanella
  1 sibling, 1 reply; 26+ messages in thread
From: Florian Weimer @ 2017-04-21 20:04 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: Florian Weimer, libc-alpha, Zack Weinberg

* Adhemerval Zanella:

> +static int
> +do_test_setsid (bool test_setsid)

This should return void.

> +  if (test_setsid)
> +    {
> +      if (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID))
> +	FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
> +    }

You need to set errno before you can use %m.

> +  res = posix_spawnp (&child, "true", NULL, &attrp, args, environ);
> +  /* posix_spawnattr_destroy is noop.  */
> +  posix_spawnattr_destroy (&attrp);
> +
> +  if (res != 0)
> +    FAIL_EXIT1 ("posix_spawnp: %m");

Likewise.

> +static int
> +do_test (void)
> +{
> +  int ret = 0;
> +
> +  ret += do_test_setsid (false);
> +  ret += do_test_setsid (true);
> +
> +  return 0;
> +}

ret is effectively unused.

> diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c
> index d7f9e83..3cf77d5 100644
> --- a/sysdeps/unix/sysv/linux/spawni.c
> +++ b/sysdeps/unix/sysv/linux/spawni.c

> +  if ((attr->__flags & POSIX_SPAWN_SETSID) != 0
> +      && (ret = __setsid ()) < 0)
> +    goto fail;

I believe the assignment to ret is dead.

> +
>    /* Set the process group ID.  */
>    if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
>        && (ret = __setpgid (0, attr->__pgrp)) != 0)

Apparently, this is an existing problem with the code ...

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 19:53                 ` Joseph Myers
@ 2017-04-21 20:43                   ` Adhemerval Zanella
  0 siblings, 0 replies; 26+ messages in thread
From: Adhemerval Zanella @ 2017-04-21 20:43 UTC (permalink / raw)
  To: Joseph Myers; +Cc: Florian Weimer, libc-alpha, Zack Weinberg



On 21/04/2017 16:53, Joseph Myers wrote:
> On Fri, 21 Apr 2017, Adhemerval Zanella wrote:
> 
>> I think this iteration should fix the issues you bring.
>>
>> --
>>
>> 	Daurnimator  <quae@daurnimator.com>
>>         Adhemerval Zanella  <adhemerval.zanella@linaro.org>
>>
>>         [BZ #21340]
>>         * 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.
> 
> I think you should also add a NEWS entry for the new feature.
> 

I will add one.

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 20:04                 ` Florian Weimer
@ 2017-04-21 20:43                   ` Adhemerval Zanella
  2017-04-21 20:47                     ` Florian Weimer
  0 siblings, 1 reply; 26+ messages in thread
From: Adhemerval Zanella @ 2017-04-21 20:43 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Florian Weimer, libc-alpha, Zack Weinberg



On 21/04/2017 17:04, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> +static int
>> +do_test_setsid (bool test_setsid)
> 
> This should return void.

Ack.

> 
>> +  if (test_setsid)
>> +    {
>> +      if (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID))
>> +	FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
>> +    }
> 
> You need to set errno before you can use %m.

Ack.

> 
>> +  res = posix_spawnp (&child, "true", NULL, &attrp, args, environ);
>> +  /* posix_spawnattr_destroy is noop.  */
>> +  posix_spawnattr_destroy (&attrp);
>> +
>> +  if (res != 0)
>> +    FAIL_EXIT1 ("posix_spawnp: %m");
> 
> Likewise.

Ack.

> 
>> +static int
>> +do_test (void)
>> +{
>> +  int ret = 0;
>> +
>> +  ret += do_test_setsid (false);
>> +  ret += do_test_setsid (true);
>> +
>> +  return 0;
>> +}
> 
> ret is effectively unused.

Yeah, I though about doing that since we bail out with an exit for
failure. I will change it.

> 
>> diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c
>> index d7f9e83..3cf77d5 100644
>> --- a/sysdeps/unix/sysv/linux/spawni.c
>> +++ b/sysdeps/unix/sysv/linux/spawni.c
> 
>> +  if ((attr->__flags & POSIX_SPAWN_SETSID) != 0
>> +      && (ret = __setsid ()) < 0)
>> +    goto fail;
> 
> I believe the assignment to ret is dead.
> 
>> +
>>    /* Set the process group ID.  */
>>    if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
>>        && (ret = __setpgid (0, attr->__pgrp)) != 0)
> 
> Apparently, this is an existing problem with the code ...
> 

If I recall correctly, on initial iterations for posix_spawn rewrite I used the ret
return to signal the error, but it seems later I generalize to use errno instead.
I will fix this usage, but I think extra fixes should go to on a different patch.

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

* Re: [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID
  2017-04-21 20:43                   ` Adhemerval Zanella
@ 2017-04-21 20:47                     ` Florian Weimer
  0 siblings, 0 replies; 26+ messages in thread
From: Florian Weimer @ 2017-04-21 20:47 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: Florian Weimer, libc-alpha, Zack Weinberg

* Adhemerval Zanella:

> I will fix this usage, but I think extra fixes should go to on a
> different patch.

Thanks, and agreed.

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

end of thread, other threads:[~2017-04-21 20:47 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-05  5:41 [PATCH v3][BZ 21340] add support for POSIX_SPAWN_SETSID daurnimator
2017-04-05  8:34 ` Florian Weimer
2017-04-05 11:01   ` Daurnimator
2017-04-06 12:37     ` Florian Weimer
2017-04-06 13:22       ` Zack Weinberg
2017-04-06 13:23         ` Florian Weimer
2017-04-06 13:24           ` Zack Weinberg
2017-04-07 12:27             ` Daurnimator
2017-04-07 13:46               ` Zack Weinberg
2017-04-09 13:22       ` Daurnimator
2017-04-21  5:04         ` Daurnimator
2017-04-21 12:39           ` Adhemerval Zanella
2017-04-21 14:13           ` Adhemerval Zanella
2017-04-21 14:24             ` Florian Weimer
2017-04-21 18:29               ` Adhemerval Zanella
2017-04-21 19:53                 ` Joseph Myers
2017-04-21 20:43                   ` Adhemerval Zanella
2017-04-21 20:04                 ` Florian Weimer
2017-04-21 20:43                   ` Adhemerval Zanella
2017-04-21 20:47                     ` Florian Weimer
2017-04-21 18:24             ` Joseph Myers
2017-04-21 18:26               ` Adhemerval Zanella
2017-04-05 12:31 ` Joseph Myers
2017-04-05 12:44   ` Joseph Myers
2017-04-05 12:58   ` Szabolcs Nagy
2017-04-05 15:02     ` Joseph Myers

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).