Blame posix/tst-posix_spawn-setsid.c

Packit 6c4009
/* Test posix_spawn setsid attribute.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <spawn.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <sys/resource.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_test_setsid (bool test_setsid)
Packit 6c4009
{
Packit 6c4009
  pid_t sid, child_sid;
Packit 6c4009
  int res;
Packit 6c4009
Packit 6c4009
  /* Current session ID.  */
Packit 6c4009
  sid = getsid(0);
Packit 6c4009
  if (sid == (pid_t) -1)
Packit 6c4009
    FAIL_EXIT1 ("getsid (0): %m");
Packit 6c4009
Packit 6c4009
  posix_spawnattr_t attrp;
Packit 6c4009
  /* posix_spawnattr_init should not fail (it basically memset the
Packit 6c4009
     attribute).  */
Packit 6c4009
  posix_spawnattr_init (&attrp);
Packit 6c4009
  if (test_setsid)
Packit 6c4009
    {
Packit 6c4009
      res = posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID);
Packit 6c4009
      if (res != 0)
Packit 6c4009
	{
Packit 6c4009
	  errno = res;
Packit 6c4009
	  FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Program to run.  */
Packit 6c4009
  char *args[2] = { (char *) "true", NULL };
Packit 6c4009
  pid_t child;
Packit 6c4009
Packit 6c4009
  res = posix_spawnp (&child, "true", NULL, &attrp, args, environ);
Packit 6c4009
  /* posix_spawnattr_destroy is noop.  */
Packit 6c4009
  posix_spawnattr_destroy (&attrp);
Packit 6c4009
Packit 6c4009
  if (res != 0)
Packit 6c4009
    {
Packit 6c4009
      errno = res;
Packit 6c4009
      FAIL_EXIT1 ("posix_spawnp: %m");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Child should have a different session ID than parent.  */
Packit 6c4009
  child_sid = getsid (child);
Packit 6c4009
Packit 6c4009
  if (child_sid == (pid_t) -1)
Packit 6c4009
    FAIL_EXIT1 ("getsid (%i): %m", child);
Packit 6c4009
Packit 6c4009
  if (test_setsid)
Packit 6c4009
    {
Packit 6c4009
      if (child_sid == sid)
Packit 6c4009
	FAIL_EXIT1 ("child session ID matched parent one");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (child_sid != sid)
Packit 6c4009
	FAIL_EXIT1 ("child session ID did not match parent one");
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  do_test_setsid (false);
Packit 6c4009
  do_test_setsid (true);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>