From patchwork Mon Apr 24 12:47:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 98040 Delivered-To: patch@linaro.org Received: by 10.140.109.52 with SMTP id k49csp1465184qgf; Mon, 24 Apr 2017 05:47:23 -0700 (PDT) X-Received: by 10.99.119.74 with SMTP id s71mr23969657pgc.143.1493038043699; Mon, 24 Apr 2017 05:47:23 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id i9si18826721pgc.265.2017.04.24.05.47.23 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Apr 2017 05:47:23 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-78882-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-78882-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-78882-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=bqq4tuPd4hxXOzHaAdPvVYSNLzLiTyYLz5HmkBsi9Pf4I3lSBGMV5 W0Ks3TWnHNkGN4AWrfDd+P2MyBHV8qXfdm2CKCyA5ae3G9QFlTYKhAJ1bZoIZ0Yn Fd7aEnWDhjt7sZ0AdnrJ/sEzbSyJjG4ZHutgDeiHz9yvsZkZUrzq4s= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=VunhHV1H0gJ/tiCZQu+VNjLgoSU=; b=tybi3sjddU6PLeEQCM/etBKAy5FY lPlL2KSxBYhjD0XOlJNuh+gqraZq5hTAWbL4pCBjPpsCIMtpWdWYWKySFqnYL4GV sAfwgLGq57mvcpiPUx/ELfxsRfI/mqIaD+EGPaX0pC7z3BwL4CuG+DPTjxUkfALi LJsigSPUXkeX3H4= Received: (qmail 55996 invoked by alias); 24 Apr 2017 12:47:12 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 55978 invoked by uid 89); 24 Apr 2017 12:47:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:6615, UD:php X-HELO: mail-qt0-f171.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=PEV0Ia3nXZaevGPJuJ7tY5Dz3XdkvsNOtdceLx/nOAA=; b=GlVDMkY+UOP4vDcPya4u+7m+ozs9OyLLO1+sL3mjR367gNBgwnPkgCVwITvdUZRcTM ZVSjJYCn9ejXBJQDNoBWzO58kMhCH/wpJPETc/Lqh/xTzxYNkXz/5KIjOpar9IgRaJGF PNmYE4EN0wrCrpeYBcqHxsgVOH0HUmHgMNrOAElVhg7VSqzd/SXmjSDHQ1oHWf5c1NsL NlPVWCGMigmLnD+H3Uneiy08Nrnyke8Cr53nXm5rZCK0eX0kIPPUjKfY9XTcRr7Y1b1D z9+JtF6Mf4GWY8sizuTieijkzOueBOM6M2/Fj0HTbIp6DHpa75PyYmwf8Tiz9s4hoW4z AZ+Q== X-Gm-Message-State: AN3rC/6o9t3bKbJLIJ1juaY3nWuihi6eT0YzAvr0aP+ujEM+kO1TBlrG A9YN8KrZ5CQCjCUcgqAwPw== X-Received: by 10.200.55.235 with SMTP id e40mr24842474qtc.219.1493038029476; Mon, 24 Apr 2017 05:47:09 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v4 1/2] [BZ 21340] add support for POSIX_SPAWN_SETSID Date: Mon, 24 Apr 2017 09:47:00 -0300 Message-Id: <1493038021-16091-1-git-send-email-adhemerval.zanella@linaro.org> 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 Adhemerval Zanella [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 -- 2.7.4 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 + . */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +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 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)