about summary refs log tree commit diff
path: root/posix/tst-posix_spawn-setsid.c
blob: b47eb16cc58a73bf2798caec238dc031d61fc52e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Test posix_spawn setsid attribute.
   Copyright (C) 2017-2023 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
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <intprops.h>
#include <paths.h>
#include <spawn.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>

#include <support/check.h>
#include <support/xunistd.h>
#include <support/temp_file.h>
#include <tst-spawn.h>

/* Nonzero if the program gets called via `exec'.  */
static int restart;

/* Hold the four initial argument used to respawn the process, plus
   the extra '--direct' and '--restart', and a final NULL.  */
static char *initial_argv[7];
static int initial_argv_count;

#define CMDLINE_OPTIONS \
  { "restart", no_argument, &restart, 1 },

static char *pidfile;

static pid_t
read_child_sid (void)
{
  int pidfd = xopen (pidfile, O_RDONLY, 0);

  char buf[INT_STRLEN_BOUND (pid_t)];
  ssize_t n = read (pidfd, buf, sizeof (buf));
  TEST_VERIFY (n < sizeof buf && n >= 0);
  buf[n] = '\0';

  /* We only expect to read the PID.  */
  char *endp;
  long int rpid = strtol (buf, &endp, 10);
  TEST_VERIFY (endp != buf);

  xclose (pidfd);

  return rpid;
}

/* Called on process re-execution, write down the session id on PIDFILE.  */
static void
handle_restart (const char *pidfile)
{
  int pidfd = xopen (pidfile, O_WRONLY, 0);

  char buf[INT_STRLEN_BOUND (pid_t)];
  int s = snprintf (buf, sizeof buf, "%d", getsid (0));
  size_t n = write (pidfd, buf, s);
  TEST_VERIFY (n == s);

  xclose (pidfd);
}

static void
do_test_setsid (bool test_setsid)
{
  /* Current session ID.  */
  pid_t sid = getsid (0);
  TEST_VERIFY (sid != (pid_t) -1);

  posix_spawnattr_t attrp;
  TEST_COMPARE (posix_spawnattr_init (&attrp), 0);
  if (test_setsid)
    TEST_COMPARE (posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID), 0);

  /* 1 or 4 elements from initial_argv:
       + path to ld.so          optional
       + --library-path         optional
       + the library path       optional
       + application name
       + --direct
       + --restart
       + pidfile  */
  int argv_size = initial_argv_count + 2;
  char *args[argv_size];
  int argc = 0;

  for (char **arg = initial_argv; *arg != NULL; arg++)
    args[argc++] = *arg;
  args[argc++] = pidfile;
  args[argc] = NULL;
  TEST_VERIFY (argc < argv_size);

  PID_T_TYPE pid;
  TEST_COMPARE (POSIX_SPAWN (&pid, args[0], NULL, &attrp, args, environ), 0);
  TEST_COMPARE (posix_spawnattr_destroy (&attrp), 0);

  siginfo_t sinfo;
  TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0);
  TEST_COMPARE (sinfo.si_code, CLD_EXITED);
  TEST_COMPARE (sinfo.si_status, 0);

  pid_t child_sid = read_child_sid ();

  /* Child should have a different session ID than parent.  */
  TEST_VERIFY (child_sid != (pid_t) -1);

  if (test_setsid)
    TEST_VERIFY (child_sid != sid);
  else
    TEST_VERIFY (child_sid == sid);
}

static int
do_test (int argc, char *argv[])
{
  /* We must have either:

     - one or four parameters if called initially:
       + argv[1]: path for ld.so        optional
       + argv[2]: "--library-path"      optional
       + argv[3]: the library path      optional
       + argv[4]: the application name

     - six parameters left if called through re-execution:
       + argv[5/1]: the application name
       + argv[6/2]: the pidfile

     * When built with --enable-hardcoded-path-in-tests or issued without
       using the loader directly.  */

  if (restart)
    {
      handle_restart (argv[1]);
      return 0;
    }

  TEST_VERIFY_EXIT (argc == 2 || argc == 5);

  int i;
  for (i = 0; i < argc - 1; i++)
    initial_argv[i] = argv[i + 1];
  initial_argv[i++] = (char *) "--direct";
  initial_argv[i++] = (char *) "--restart";
  initial_argv_count = i;

  create_temp_file ("tst-posix_spawn-setsid-", &pidfile);

  do_test_setsid (false);
  do_test_setsid (true);

  return 0;
}

#define TEST_FUNCTION_ARGV do_test
#include <support/test-driver.c>