Back to index

glibc  2.9
tst-exec.c
Go to the documentation of this file.
00001 /* Tests for exec.
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 <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <wait.h>
00028 
00029 
00030 /* Nonzero if the program gets called via `exec'.  */
00031 static int restart;
00032 
00033 
00034 #define CMDLINE_OPTIONS \
00035   { "restart", no_argument, &restart, 1 },
00036 
00037 /* Prototype for our test function.  */
00038 extern void do_prepare (int argc, char *argv[]);
00039 extern int do_test (int argc, char *argv[]);
00040 
00041 /* We have a preparation function.  */
00042 #define PREPARE do_prepare
00043 
00044 #include "../test-skeleton.c"
00045 
00046 
00047 /* Name of the temporary files.  */
00048 static char *name1;
00049 static char *name2;
00050 
00051 /* The contents of our files.  */
00052 static const char fd1string[] = "This file should get closed";
00053 static const char fd2string[] = "This file should stay opened";
00054 
00055 
00056 /* We have a preparation function.  */
00057 void
00058 do_prepare (int argc, char *argv[])
00059 {
00060    size_t name_len;
00061 
00062    name_len = strlen (test_dir);
00063    name1 = malloc (name_len + sizeof ("/execXXXXXX"));
00064    mempcpy (mempcpy (name1, test_dir, name_len),
00065            "/execXXXXXX", sizeof ("/execXXXXXX"));
00066    add_temp_file (name1);
00067 
00068    name2 = malloc (name_len + sizeof ("/execXXXXXX"));
00069    mempcpy (mempcpy (name2, test_dir, name_len),
00070            "/execXXXXXX", sizeof ("/execXXXXXX"));
00071    add_temp_file (name2);
00072 }
00073 
00074 
00075 static int
00076 handle_restart (const char *fd1s, const char *fd2s, const char *name)
00077 {
00078   char buf[100];
00079   int fd1;
00080   int fd2;
00081 
00082   /* First get the descriptors.  */
00083   fd1 = atol (fd1s);
00084   fd2 = atol (fd2s);
00085 
00086   /* Sanity check.  */
00087   if (fd1 == fd2)
00088     error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
00089 
00090   /* First the easy part: read from the file descriptor which is
00091      supposed to be open.  */
00092   if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
00093     error (EXIT_FAILURE, errno, "file 2 not in right position");
00094   if (lseek (fd2, 0, SEEK_SET) != 0)
00095     error (EXIT_FAILURE, 0, "cannot reset position in file 2");
00096   if (read (fd2, buf, sizeof buf) != strlen (fd2string))
00097     error (EXIT_FAILURE, 0, "cannot read file 2");
00098   if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
00099     error (EXIT_FAILURE, 0, "file 2 does not match");
00100 
00101   /* No try to read the first file.  First make sure it is not opened.  */
00102   if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
00103     error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
00104 
00105   /* Now open the file and read it.  */
00106   fd1 = open (name, O_RDONLY);
00107   if (fd1 == -1)
00108     error (EXIT_FAILURE, errno,
00109           "cannot open first file \"%s\" for verification", name);
00110 
00111   if (read (fd1, buf, sizeof buf) != strlen (fd1string))
00112     error (EXIT_FAILURE, errno, "cannot read file 1");
00113   if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
00114     error (EXIT_FAILURE, 0, "file 1 does not match");
00115 
00116   return 0;
00117 }
00118 
00119 
00120 int
00121 do_test (int argc, char *argv[])
00122 {
00123   pid_t pid;
00124   int fd1;
00125   int fd2;
00126   int flags;
00127   int status;
00128 
00129   /* We must have
00130      - four parameters left of called initially
00131        + path for ld.so
00132        + "--library-path"
00133        + the library path
00134        + the application name
00135      - three parameters left if called through re-execution
00136        + file descriptor number which is supposed to be closed
00137        + the open file descriptor
00138        + the name of the closed desriptor
00139   */
00140 
00141   if (restart)
00142     {
00143       if (argc != 4)
00144        error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
00145 
00146       return handle_restart (argv[1], argv[2], argv[3]);
00147     }
00148 
00149   if (argc != 5)
00150     error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
00151 
00152   /* Prepare the test.  We are creating two files: one which file descriptor
00153      will be marked with FD_CLOEXEC, another which is not.  */
00154 
00155    /* Open our test files.   */
00156    fd1 = mkstemp (name1);
00157    if (fd1 == -1)
00158      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name1);
00159    fd2 = mkstemp (name2);
00160    if (fd2 == -1)
00161      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name2);
00162 
00163    /* Set the bit.  */
00164    flags = fcntl (fd1, F_GETFD, 0);
00165    if (flags < 0)
00166      error (EXIT_FAILURE, errno, "cannot get flags");
00167    flags |= FD_CLOEXEC;
00168    if (fcntl (fd1, F_SETFD, flags) < 0)
00169      error (EXIT_FAILURE, errno, "cannot set flags");
00170 
00171    /* Write something in the files.  */
00172    if (write (fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
00173      error (EXIT_FAILURE, errno, "cannot write to first file");
00174    if (write (fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
00175      error (EXIT_FAILURE, errno, "cannot write to second file");
00176 
00177   /* We want to test the `exec' function.  To do this we restart the program
00178      with an additional parameter.  But first create another process.  */
00179   pid = fork ();
00180   if (pid == 0)
00181     {
00182       char fd1name[18];
00183       char fd2name[18];
00184 
00185       snprintf (fd1name, sizeof fd1name, "%d", fd1);
00186       snprintf (fd2name, sizeof fd2name, "%d", fd2);
00187 
00188       /* This is the child.  Construct the command line.  */
00189       execl (argv[1], argv[1], argv[2], argv[3], argv[4], "--direct",
00190             "--restart", fd1name, fd2name, name1, NULL);
00191 
00192       error (EXIT_FAILURE, errno, "cannot exec");
00193     }
00194   else if (pid == (pid_t) -1)
00195     error (EXIT_FAILURE, errno, "cannot fork");
00196 
00197   /* Wait for the child.  */
00198   if (waitpid (pid, &status, 0) != pid)
00199     error (EXIT_FAILURE, errno, "wrong child");
00200 
00201   if (WTERMSIG (status) != 0)
00202     error (EXIT_FAILURE, 0, "Child terminated incorrectly");
00203   status = WEXITSTATUS (status);
00204 
00205   /* Remove the test files.  */
00206   unlink (name1);
00207   unlink (name2);
00208 
00209   return status;
00210 }