about summary refs log tree commit diff
path: root/posix/tst-spawn.c
blob: a9fd6008faf6daecf2da500a8ddf58a9ab6af4d2 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Tests for spawn.
   Copyright (C) 2000-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.

   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 <stdio.h>
#include <getopt.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <spawn.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>

#include <support/check.h>
#include <support/xunistd.h>
#include <support/temp_file.h>
#include <support/support.h>


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


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

/* Name of the temporary files.  */
static char *name1;
static char *name2;
static char *name3;

/* Descriptors for the temporary files.  */
static int temp_fd1 = -1;
static int temp_fd2 = -1;
static int temp_fd3 = -1;

/* The contents of our files.  */
static const char fd1string[] = "This file should get closed";
static const char fd2string[] = "This file should stay opened";
static const char fd3string[] = "This file will be opened";


/* We have a preparation function.  */
static void
do_prepare (int argc, char *argv[])
{
  /* We must not open any files in the restart case.  */
  if (restart)
    return;

  TEST_VERIFY_EXIT ((temp_fd1 = create_temp_file ("spawn", &name1)) != -1);
  TEST_VERIFY_EXIT ((temp_fd2 = create_temp_file ("spawn", &name2)) != -1);
  TEST_VERIFY_EXIT ((temp_fd3 = create_temp_file ("spawn", &name3)) != -1);
}
#define PREPARE do_prepare


static int
handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
		const char *fd4s, const char *name)
{
  char buf[100];
  int fd1;
  int fd2;
  int fd3;
  int fd4;

  /* First get the descriptors.  */
  fd1 = atol (fd1s);
  fd2 = atol (fd2s);
  fd3 = atol (fd3s);
  fd4 = atol (fd4s);

  /* Sanity check.  */
  TEST_VERIFY_EXIT (fd1 != fd2);
  TEST_VERIFY_EXIT (fd1 != fd3);
  TEST_VERIFY_EXIT (fd1 != fd4);
  TEST_VERIFY_EXIT (fd2 != fd3);
  TEST_VERIFY_EXIT (fd2 != fd4);
  TEST_VERIFY_EXIT (fd3 != fd4);

  /* First the easy part: read from the file descriptor which is
     supposed to be open.  */
  TEST_COMPARE (xlseek (fd2, 0, SEEK_CUR), strlen (fd2string));
  /* The duped descriptor must have the same position.  */
  TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), strlen (fd2string));
  TEST_COMPARE (xlseek (fd2, 0, SEEK_SET), 0);
  TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), 0);
  TEST_COMPARE (read (fd2, buf, sizeof buf), strlen (fd2string));
  TEST_COMPARE_BLOB (fd2string, strlen (fd2string), buf, strlen (fd2string));

  /* Now read from the third file.  */
  TEST_COMPARE (read (fd3, buf, sizeof buf), strlen (fd3string));
  TEST_COMPARE_BLOB (fd3string, strlen (fd3string), buf, strlen (fd3string));
  /* Try to write to the file.  This should not be allowed.  */
  TEST_COMPARE (write (fd3, "boo!", 4), -1);
  TEST_COMPARE (errno, EBADF);

  /* Now try to read the first file.  First make sure it is not opened.  */
  TEST_COMPARE (lseek (fd1, 0, SEEK_CUR), (off_t) -1);
  TEST_COMPARE (errno, EBADF);

  /* Now open the file and read it.  */
  fd1 = xopen (name, O_RDONLY, 0600);

  TEST_COMPARE (read (fd1, buf, sizeof buf), strlen (fd1string));
  TEST_COMPARE_BLOB (fd1string, strlen (fd1string), buf, strlen (fd1string));

  return 0;
}


static int
do_test (int argc, char *argv[])
{
  pid_t pid;
  int fd4;
  int status;
  posix_spawn_file_actions_t actions;
  char fd1name[18];
  char fd2name[18];
  char fd3name[18];
  char fd4name[18];
  char *name3_copy;
  char *spargv[12];
  int i;

  /* We must have
     - one or four parameters left if called initially
       + path for ld.so		optional
       + "--library-path"	optional
       + the library path	optional
       + the application name
     - five parameters left if called through re-execution
       + file descriptor number which is supposed to be closed
       + the open file descriptor
       + the newly opened file descriptor
       + thhe duped second descriptor
       + the name of the closed descriptor
  */
  if (argc != (restart ? 6 : 2) && argc != (restart ? 6 : 5))
    FAIL_EXIT1 ("wrong number of arguments (%d)", argc);

  if (restart)
    return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]);

  /* Prepare the test.  We are creating two files: one which file descriptor
     will be marked with FD_CLOEXEC, another which is not.  */

  /* Write something in the files.  */
  xwrite (temp_fd1, fd1string, strlen (fd1string));
  xwrite (temp_fd2, fd2string, strlen (fd2string));
  xwrite (temp_fd3, fd3string, strlen (fd3string));

  /* Close the third file.  It'll be opened by `spawn'.  */
  xclose (temp_fd3);

  /* Tell `spawn' what to do.  */
  TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
  /* Close `temp_fd1'.  */
  TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, temp_fd1), 0);
  /* We want to open the third file.  */
  name3_copy = xstrdup (name3);
  TEST_COMPARE (posix_spawn_file_actions_addopen (&actions, temp_fd3,
						  name3_copy,
						  O_RDONLY, 0666),
		0);
  /* Overwrite the name to check that a copy has been made.  */
  memset (name3_copy, 'X', strlen (name3_copy));

  /* We dup the second descriptor.  */
  fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1;
  TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4),
	        0);

  /* Now spawn the process.  */
  snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
  snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
  snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
  snprintf (fd4name, sizeof fd4name, "%d", fd4);

  for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++)
    spargv[i] = argv[i + 1];
  spargv[i++] = (char *) "--direct";
  spargv[i++] = (char *) "--restart";
  spargv[i++] = fd1name;
  spargv[i++] = fd2name;
  spargv[i++] = fd3name;
  spargv[i++] = fd4name;
  spargv[i++] = name1;
  spargv[i] = NULL;

  TEST_COMPARE (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ),
		0);

  /* Same test but with a NULL pid argument.  */
  TEST_COMPARE (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ),
		0);

  /* Cleanup.  */
  TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0);
  free (name3_copy);

  /* Wait for the children.  */
  TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
  TEST_VERIFY (WIFEXITED (status));
  TEST_VERIFY (!WIFSIGNALED (status));
  TEST_COMPARE (WEXITSTATUS (status), 0);

  xwaitpid (-1, &status, 0);
  TEST_VERIFY (WIFEXITED (status));
  TEST_VERIFY (!WIFSIGNALED (status));
  TEST_COMPARE (WEXITSTATUS (status), 0);

  return 0;
}

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