IA64: Move NPTL public headers to sysdeps/ia64/nptl/.
[platform/upstream/glibc.git] / posix / tst-exec.c
1 /* Tests for exec.
2    Copyright (C) 2000-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <errno.h>
21 #include <error.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <wait.h>
27
28
29 /* Nonzero if the program gets called via `exec'.  */
30 static int restart;
31
32
33 #define CMDLINE_OPTIONS \
34   { "restart", no_argument, &restart, 1 },
35
36 /* Prototype for our test function.  */
37 extern void do_prepare (int argc, char *argv[]);
38 extern int do_test (int argc, char *argv[]);
39
40 /* We have a preparation function.  */
41 #define PREPARE do_prepare
42
43 #include "../test-skeleton.c"
44
45
46 /* Name of the temporary files.  */
47 static char *name1;
48 static char *name2;
49
50 /* The contents of our files.  */
51 static const char fd1string[] = "This file should get closed";
52 static const char fd2string[] = "This file should stay opened";
53
54
55 /* We have a preparation function.  */
56 void
57 do_prepare (int argc, char *argv[])
58 {
59    size_t name_len;
60
61    name_len = strlen (test_dir);
62    name1 = malloc (name_len + sizeof ("/execXXXXXX"));
63    mempcpy (mempcpy (name1, test_dir, name_len),
64             "/execXXXXXX", sizeof ("/execXXXXXX"));
65    add_temp_file (name1);
66
67    name2 = malloc (name_len + sizeof ("/execXXXXXX"));
68    mempcpy (mempcpy (name2, test_dir, name_len),
69             "/execXXXXXX", sizeof ("/execXXXXXX"));
70    add_temp_file (name2);
71 }
72
73
74 static int
75 handle_restart (const char *fd1s, const char *fd2s, const char *name)
76 {
77   char buf[100];
78   int fd1;
79   int fd2;
80
81   /* First get the descriptors.  */
82   fd1 = atol (fd1s);
83   fd2 = atol (fd2s);
84
85   /* Sanity check.  */
86   if (fd1 == fd2)
87     error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
88
89   /* First the easy part: read from the file descriptor which is
90      supposed to be open.  */
91   if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
92     error (EXIT_FAILURE, errno, "file 2 not in right position");
93   if (lseek (fd2, 0, SEEK_SET) != 0)
94     error (EXIT_FAILURE, 0, "cannot reset position in file 2");
95   if (read (fd2, buf, sizeof buf) != strlen (fd2string))
96     error (EXIT_FAILURE, 0, "cannot read file 2");
97   if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
98     error (EXIT_FAILURE, 0, "file 2 does not match");
99
100   /* No try to read the first file.  First make sure it is not opened.  */
101   if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
102     error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
103
104   /* Now open the file and read it.  */
105   fd1 = open (name, O_RDONLY);
106   if (fd1 == -1)
107     error (EXIT_FAILURE, errno,
108            "cannot open first file \"%s\" for verification", name);
109
110   if (read (fd1, buf, sizeof buf) != strlen (fd1string))
111     error (EXIT_FAILURE, errno, "cannot read file 1");
112   if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
113     error (EXIT_FAILURE, 0, "file 1 does not match");
114
115   return 0;
116 }
117
118
119 int
120 do_test (int argc, char *argv[])
121 {
122   pid_t pid;
123   int fd1;
124   int fd2;
125   int flags;
126   int status;
127
128   /* We must have
129      - one or four parameters left if called initially
130        + path for ld.so         optional
131        + "--library-path"       optional
132        + the library path       optional
133        + the application name
134      - three parameters left if called through re-execution
135        + file descriptor number which is supposed to be closed
136        + the open file descriptor
137        + the name of the closed desriptor
138   */
139
140   if (restart)
141     {
142       if (argc != 4)
143         error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
144
145       return handle_restart (argv[1], argv[2], argv[3]);
146     }
147
148   if (argc != 2 && argc != 5)
149     error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
150
151   /* Prepare the test.  We are creating two files: one which file descriptor
152      will be marked with FD_CLOEXEC, another which is not.  */
153
154    /* Open our test files.   */
155    fd1 = mkstemp (name1);
156    if (fd1 == -1)
157      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name1);
158    fd2 = mkstemp (name2);
159    if (fd2 == -1)
160      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name2);
161
162    /* Set the bit.  */
163    flags = fcntl (fd1, F_GETFD, 0);
164    if (flags < 0)
165      error (EXIT_FAILURE, errno, "cannot get flags");
166    flags |= FD_CLOEXEC;
167    if (fcntl (fd1, F_SETFD, flags) < 0)
168      error (EXIT_FAILURE, errno, "cannot set flags");
169
170    /* Write something in the files.  */
171    if (write (fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
172      error (EXIT_FAILURE, errno, "cannot write to first file");
173    if (write (fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
174      error (EXIT_FAILURE, errno, "cannot write to second file");
175
176   /* We want to test the `exec' function.  To do this we restart the program
177      with an additional parameter.  But first create another process.  */
178   pid = fork ();
179   if (pid == 0)
180     {
181       char fd1name[18];
182       char fd2name[18];
183
184       snprintf (fd1name, sizeof fd1name, "%d", fd1);
185       snprintf (fd2name, sizeof fd2name, "%d", fd2);
186
187       /* This is the child.  Construct the command line.  */
188       if (argc == 5)
189         execl (argv[1], argv[1], argv[2], argv[3], argv[4], "--direct",
190                "--restart", fd1name, fd2name, name1, NULL);
191       else
192         execl (argv[1], argv[1], "--direct",
193                "--restart", fd1name, fd2name, name1, NULL);
194
195       error (EXIT_FAILURE, errno, "cannot exec");
196     }
197   else if (pid == (pid_t) -1)
198     error (EXIT_FAILURE, errno, "cannot fork");
199
200   /* Wait for the child.  */
201   if (waitpid (pid, &status, 0) != pid)
202     error (EXIT_FAILURE, errno, "wrong child");
203
204   if (WTERMSIG (status) != 0)
205     error (EXIT_FAILURE, 0, "Child terminated incorrectly");
206   status = WEXITSTATUS (status);
207
208   /* Remove the test files.  */
209   unlink (name1);
210   unlink (name2);
211
212   return status;
213 }