Back to index

glibc  2.9
tst-spawn.c
Go to the documentation of this file.
00001 /* Tests for spawn.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <error.h>
00023 #include <fcntl.h>
00024 #include <spawn.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 #include <wait.h>
00029 #include <sys/param.h>
00030 
00031 
00032 /* Nonzero if the program gets called via `exec'.  */
00033 static int restart;
00034 
00035 
00036 #define CMDLINE_OPTIONS \
00037   { "restart", no_argument, &restart, 1 },
00038 
00039 /* Prototype for our test function.  */
00040 extern void do_prepare (int argc, char *argv[]);
00041 extern int do_test (int argc, char *argv[]);
00042 
00043 /* We have a preparation function.  */
00044 #define PREPARE do_prepare
00045 
00046 #include "../test-skeleton.c"
00047 
00048 
00049 /* Name of the temporary files.  */
00050 static char *name1;
00051 static char *name2;
00052 static char *name3;
00053 
00054 /* The contents of our files.  */
00055 static const char fd1string[] = "This file should get closed";
00056 static const char fd2string[] = "This file should stay opened";
00057 static const char fd3string[] = "This file will be opened";
00058 
00059 
00060 /* We have a preparation function.  */
00061 void
00062 do_prepare (int argc, char *argv[])
00063 {
00064    size_t name_len;
00065 
00066    name_len = strlen (test_dir);
00067    name1 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
00068    mempcpy (mempcpy (name1, test_dir, name_len),
00069            "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
00070    add_temp_file (name1);
00071 
00072    name2 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
00073    mempcpy (mempcpy (name2, test_dir, name_len),
00074            "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
00075    add_temp_file (name2);
00076 
00077    name3 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
00078    mempcpy (mempcpy (name3, test_dir, name_len),
00079            "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
00080    add_temp_file (name3);
00081 }
00082 
00083 
00084 static int
00085 handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
00086               const char *fd4s, const char *name)
00087 {
00088   char buf[100];
00089   int fd1;
00090   int fd2;
00091   int fd3;
00092   int fd4;
00093 
00094   /* First get the descriptors.  */
00095   fd1 = atol (fd1s);
00096   fd2 = atol (fd2s);
00097   fd3 = atol (fd3s);
00098   fd4 = atol (fd4s);
00099 
00100   /* Sanity check.  */
00101   if (fd1 == fd2)
00102     error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
00103   if (fd1 == fd3)
00104     error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same");
00105   if (fd1 == fd4)
00106     error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same");
00107   if (fd2 == fd3)
00108     error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same");
00109   if (fd2 == fd4)
00110     error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same");
00111   if (fd3 == fd4)
00112     error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same");
00113 
00114   /* First the easy part: read from the file descriptor which is
00115      supposed to be open.  */
00116   if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
00117     error (EXIT_FAILURE, errno, "file 2 not in right position");
00118   /* The duped descriptor must have the same position.  */
00119   if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string))
00120     error (EXIT_FAILURE, errno, "file 4 not in right position");
00121   if (lseek (fd2, 0, SEEK_SET) != 0)
00122     error (EXIT_FAILURE, 0, "cannot reset position in file 2");
00123   if (lseek (fd4, 0, SEEK_CUR) != 0)
00124     error (EXIT_FAILURE, errno, "file 4 not set back, too");
00125   if (read (fd2, buf, sizeof buf) != strlen (fd2string))
00126     error (EXIT_FAILURE, 0, "cannot read file 2");
00127   if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
00128     error (EXIT_FAILURE, 0, "file 2 does not match");
00129 
00130   /* Now read from the third file.  */
00131   if (read (fd3, buf, sizeof buf) != strlen (fd3string))
00132     error (EXIT_FAILURE, 0, "cannot read file 3");
00133   if (memcmp (fd3string, buf, strlen (fd3string)) != 0)
00134     error (EXIT_FAILURE, 0, "file 3 does not match");
00135   /* Try to write to the file.  This should not be allowed.  */
00136   if (write (fd3, "boo!", 4) != -1 || errno != EBADF)
00137     error (EXIT_FAILURE, 0, "file 3 is writable");
00138 
00139   /* Now try to read the first file.  First make sure it is not opened.  */
00140   if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
00141     error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
00142 
00143   /* Now open the file and read it.  */
00144   fd1 = open (name, O_RDONLY);
00145   if (fd1 == -1)
00146     error (EXIT_FAILURE, errno,
00147           "cannot open first file \"%s\" for verification", name);
00148 
00149   if (read (fd1, buf, sizeof buf) != strlen (fd1string))
00150     error (EXIT_FAILURE, errno, "cannot read file 1");
00151   if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
00152     error (EXIT_FAILURE, 0, "file 1 does not match");
00153 
00154   return 0;
00155 }
00156 
00157 
00158 int
00159 do_test (int argc, char *argv[])
00160 {
00161   pid_t pid;
00162   int fd1;
00163   int fd2;
00164   int fd3;
00165   int fd4;
00166   int status;
00167   posix_spawn_file_actions_t actions;
00168   char fd1name[18];
00169   char fd2name[18];
00170   char fd3name[18];
00171   char fd4name[18];
00172   char *spargv[12];
00173 
00174   /* We must have
00175      - four parameters left of called initially
00176        + path for ld.so
00177        + "--library-path"
00178        + the library path
00179        + the application name
00180      - five parameters left if called through re-execution
00181        + file descriptor number which is supposed to be closed
00182        + the open file descriptor
00183        + the newly opened file descriptor
00184        + thhe duped second descriptor
00185        + the name of the closed descriptor
00186   */
00187   if (argc != (restart ? 6 : 5))
00188     error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
00189 
00190   if (restart)
00191     return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]);
00192 
00193   /* Prepare the test.  We are creating two files: one which file descriptor
00194      will be marked with FD_CLOEXEC, another which is not.  */
00195 
00196    /* Open our test files.   */
00197    fd1 = mkstemp (name1);
00198    if (fd1 == -1)
00199      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name1);
00200    fd2 = mkstemp (name2);
00201    if (fd2 == -1)
00202      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name2);
00203    fd3 = mkstemp (name3);
00204    if (fd3 == -1)
00205      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name3);
00206 
00207    /* Write something in the files.  */
00208    if (write (fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
00209      error (EXIT_FAILURE, errno, "cannot write to first file");
00210    if (write (fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
00211      error (EXIT_FAILURE, errno, "cannot write to second file");
00212    if (write (fd3, fd3string, strlen (fd3string)) != strlen (fd3string))
00213      error (EXIT_FAILURE, errno, "cannot write to third file");
00214 
00215    /* Close the third file.  It'll be opened by `spawn'.  */
00216    close (fd3);
00217 
00218    /* Tell `spawn' what to do.  */
00219    if (posix_spawn_file_actions_init (&actions) != 0)
00220      error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init");
00221    /* Close `fd1'.  */
00222    if (posix_spawn_file_actions_addclose (&actions, fd1) != 0)
00223      error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose");
00224    /* We want to open the third file.  */
00225    if (posix_spawn_file_actions_addopen (&actions, fd3, name3,
00226                                     O_RDONLY, 0666) != 0)
00227      error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen");
00228    /* We dup the second descriptor.  */
00229    fd4 = MAX (2, MAX (fd1, MAX (fd2, fd3))) + 1;
00230    if (posix_spawn_file_actions_adddup2 (&actions, fd2, fd4) != 0)
00231      error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2");
00232 
00233    /* Now spawn the process.  */
00234    snprintf (fd1name, sizeof fd1name, "%d", fd1);
00235    snprintf (fd2name, sizeof fd2name, "%d", fd2);
00236    snprintf (fd3name, sizeof fd3name, "%d", fd3);
00237    snprintf (fd4name, sizeof fd4name, "%d", fd4);
00238 
00239    spargv[0] = argv[1];
00240    spargv[1] = argv[2];
00241    spargv[2] = argv[3];
00242    spargv[3] = argv[4];
00243    spargv[4] = (char *) "--direct";
00244    spargv[5] = (char *) "--restart";
00245    spargv[6] = fd1name;
00246    spargv[7] = fd2name;
00247    spargv[8] = fd3name;
00248    spargv[9] = fd4name;
00249    spargv[10] = name1;
00250    spargv[11] = NULL;
00251 
00252    if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0)
00253      error (EXIT_FAILURE, errno, "posix_spawn");
00254 
00255    /* Cleanup.  */
00256    if (posix_spawn_file_actions_destroy (&actions) != 0)
00257      error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy");
00258 
00259   /* Wait for the child.  */
00260   if (waitpid (pid, &status, 0) != pid)
00261     error (EXIT_FAILURE, errno, "wrong child");
00262 
00263   if (WTERMSIG (status) != 0)
00264     error (EXIT_FAILURE, 0, "Child terminated incorrectly");
00265   status = WEXITSTATUS (status);
00266 
00267   /* Remove the test files.  */
00268   unlink (name1);
00269   unlink (name2);
00270   unlink (name3);
00271 
00272   return status;
00273 }