about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2007-01-03 23:02:17 +0000
committerUlrich Drepper <drepper@redhat.com>2007-01-03 23:02:17 +0000
commitdf859a0c1599efaa8d654b9c9a5994e666bc2b23 (patch)
tree79c7863cfaf402baed8974252c2a8244d8d63753
parent64a0aa0fac2e55e03feb8c848876a65b98018b5b (diff)
downloadglibc-df859a0c1599efaa8d654b9c9a5994e666bc2b23.tar.gz
glibc-df859a0c1599efaa8d654b9c9a5994e666bc2b23.tar.xz
glibc-df859a0c1599efaa8d654b9c9a5994e666bc2b23.zip
* posix/execvp.c: Include alloca.h.
	(allocate_scripts_argv): Renamed to...
	(scripts_argv): ... this.  Don't allocate buffer here nor count
	arguments.
	(execvp): Use alloca if possible.
	* posix/Makefile: Add rules to build and run tst-vfork3 test.
	* posix/tst-vfork3.c: New test.

2007-01-03  Ulrich Drepper  <drepper@redhat.com>

	* string/Makefile (tst-strxfrm2-ENV): Define.
	* stdlib/Makefile (tst-strtod3-ENV): Define.
-rw-r--r--ChangeLog15
-rw-r--r--posix/Makefile15
-rw-r--r--posix/execvp.c104
-rw-r--r--posix/tst-vfork3.c224
-rw-r--r--stdlib/Makefile3
-rw-r--r--string/Makefile3
6 files changed, 317 insertions, 47 deletions
diff --git a/ChangeLog b/ChangeLog
index cd8d547d72..bc8becc3e9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2007-01-03  Jakub Jelinek  <jakub@redhat.com>
+
+	* posix/execvp.c: Include alloca.h.
+	(allocate_scripts_argv): Renamed to...
+	(scripts_argv): ... this.  Don't allocate buffer here nor count
+	arguments.
+	(execvp): Use alloca if possible.
+	* posix/Makefile: Add rules to build and run tst-vfork3 test.
+	* posix/tst-vfork3.c: New test.
+
+2007-01-03  Ulrich Drepper  <drepper@redhat.com>
+
+	* string/Makefile (tst-strxfrm2-ENV): Define.
+	* stdlib/Makefile (tst-strtod3-ENV): Define.
+
 2007-01-02  Ulrich Drepper  <drepper@redhat.com>
 
 	* posix/getconf.c: Update copyright year.
diff --git a/posix/Makefile b/posix/Makefile
index b68435b0df..712e7c2069 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1991-1999, 2000-2005, 2006 Free Software Foundation, Inc.
+# Copyright (C) 1991-1999, 2000-2006, 2007 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
@@ -83,7 +83,7 @@ tests		:= tstgetopt testfnm runtests runptests	     \
 		   bug-regex21 bug-regex22 bug-regex23 bug-regex24 \
 		   bug-regex25 bug-regex26 tst-nice tst-nanosleep tst-regex2 \
 		   transbug tst-rxspencer tst-pcre tst-boost \
-		   bug-ga1 tst-vfork1 tst-vfork2 tst-waitid \
+		   bug-ga1 tst-vfork1 tst-vfork2 tst-vfork3 tst-waitid \
 		   tst-getaddrinfo2 bug-glob1 bug-glob2 tst-sysconf \
 		   tst-execvp1 tst-execvp2 tst-execlp1 tst-execlp2 \
 		   tst-execv1 tst-execv2 tst-execl1 tst-execl2 \
@@ -108,7 +108,8 @@ generated := $(addprefix wordexp-test-result, 1 2 3 4 5 6 7 8 9 10) \
 	     bug-regex21-mem bug-regex21.mtrace \
 	     tst-rxspencer-mem tst-rxspencer.mtrace tst-getconf.out \
 	     tst-pcre-mem tst-pcre.mtrace tst-boost-mem tst-boost.mtrace \
-	     bug-ga2.mtrace bug-ga2-mem bug-glob2.mtrace bug-glob2-mem
+	     bug-ga2.mtrace bug-ga2-mem bug-glob2.mtrace bug-glob2-mem \
+	     tst-vfork3-mem tst-vfork3.mtrace
 
 include ../Rules
 
@@ -174,6 +175,7 @@ tst-exec-ARGS = -- $(built-program-cmd)
 tst-spawn-ARGS = -- $(built-program-cmd)
 tst-dir-ARGS = `pwd` `cd $(common-objdir)/$(subdir); pwd` `cd $(common-objdir); pwd` $(objpfx)tst-dir
 tst-chmod-ARGS = `pwd`
+tst-vfork3-ARGS = --test-dir=$(objpfx)
 
 tst-fnmatch-ENV = LOCPATH=$(common-objpfx)localedata
 tst-regexloc-ENV = LOCPATH=$(common-objpfx)localedata
@@ -218,7 +220,7 @@ ifeq (no,$(cross-compiling))
 tests: $(objpfx)bug-regex2-mem $(objpfx)bug-regex14-mem \
   $(objpfx)bug-regex21-mem $(objpfx)tst-rxspencer-mem \
   $(objpfx)tst-pcre-mem $(objpfx)tst-boost-mem $(objpfx)tst-getconf.out \
-  $(objpfx)bug-glob2-mem
+  $(objpfx)bug-glob2-mem $(objpfx)tst-vfork3-mem
 xtests: $(objpfx)bug-ga2-mem
 endif
 
@@ -245,6 +247,11 @@ bug-regex21-ENV = MALLOC_TRACE=$(objpfx)bug-regex21.mtrace
 $(objpfx)bug-regex21-mem: $(objpfx)bug-regex21.out
 	$(common-objpfx)malloc/mtrace $(objpfx)bug-regex21.mtrace > $@
 
+tst-vfork3-ENV = MALLOC_TRACE=$(objpfx)tst-vfork3.mtrace
+
+$(objpfx)tst-vfork3-mem: $(objpfx)tst-vfork3.out
+	$(common-objpfx)malloc/mtrace $(objpfx)tst-vfork3.mtrace > $@
+
 # tst-rxspencer.mtrace is generated only when run without --utf8
 # option, since otherwise the file has almost 100M and takes very long
 # time to process.
diff --git a/posix/execvp.c b/posix/execvp.c
index 8421bd048f..887379e3a2 100644
--- a/posix/execvp.c
+++ b/posix/execvp.c
@@ -1,4 +1,5 @@
-/* Copyright (C) 1991,92,1995-99,2002,2004,2005 Free Software Foundation, Inc.
+/* Copyright (C) 1991,92, 1995-99, 2002, 2004, 2005, 2007
+   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
@@ -16,6 +17,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <alloca.h>
 #include <unistd.h>
 #include <stdarg.h>
 #include <stdbool.h>
@@ -27,29 +29,18 @@
 
 /* The file is accessible but it is not an executable file.  Invoke
    the shell to interpret it as a script.  */
-static char **
+static void
 internal_function
-allocate_scripts_argv (const char *file, char *const argv[])
+scripts_argv (const char *file, char *const argv[], int argc, char **new_argv)
 {
-  /* Count the arguments.  */
-  int argc = 0;
-  while (argv[argc++])
-    ;
-
   /* Construct an argument list for the shell.  */
-  char **new_argv = (char **) malloc ((argc + 1) * sizeof (char *));
-  if (new_argv != NULL)
+  new_argv[0] = (char *) _PATH_BSHELL;
+  new_argv[1] = (char *) file;
+  while (argc > 1)
     {
-      new_argv[0] = (char *) _PATH_BSHELL;
-      new_argv[1] = (char *) file;
-      while (argc > 1)
-	{
-	  new_argv[argc] = argv[argc - 1];
-	  --argc;
-	}
+      new_argv[argc] = argv[argc - 1];
+      --argc;
     }
-
-  return new_argv;
 }
 
 
@@ -67,8 +58,6 @@ execvp (file, argv)
       return -1;
     }
 
-  char **script_argv = NULL;
-
   if (strchr (file, '/') != NULL)
     {
       /* Don't search when it contains a slash.  */
@@ -76,46 +65,71 @@ execvp (file, argv)
 
       if (errno == ENOEXEC)
 	{
-	  script_argv = allocate_scripts_argv (file, argv);
+	  /* Count the arguments.  */
+	  int argc = 0;
+	  while (argv[argc++])
+	    ;
+	  size_t len = (argc + 1) * sizeof (char *);
+	  char **script_argv;
+	  void *ptr = NULL;
+	  if (__libc_use_alloca (len))
+	    script_argv = alloca (len);
+	  else
+	    script_argv = ptr = malloc (len);
+
 	  if (script_argv != NULL)
 	    {
+	      scripts_argv (file, argv, argc, script_argv);
 	      __execve (script_argv[0], script_argv, __environ);
 
-	      free (script_argv);
+	      free (ptr);
 	    }
 	}
     }
   else
     {
+      size_t pathlen;
+      size_t alloclen = 0;
       char *path = getenv ("PATH");
+      if (path == NULL)
+	{
+	  pathlen = confstr (_CS_PATH, (char *) NULL, 0);
+	  alloclen = pathlen + 1;
+	}
+      else
+	pathlen = strlen (path);
+
+      size_t len = strlen (file) + 1;
+      alloclen += pathlen + len + 1;
+
+      char *name;
       char *path_malloc = NULL;
+      if (__libc_use_alloca (alloclen))
+	name = alloca (alloclen);
+      else
+	{
+	  path_malloc = name = malloc (alloclen);
+	  if (name == NULL)
+	    return -1;
+	}
+
       if (path == NULL)
 	{
 	  /* There is no `PATH' in the environment.
 	     The default search path is the current directory
 	     followed by the path `confstr' returns for `_CS_PATH'.  */
-	  size_t len = confstr (_CS_PATH, (char *) NULL, 0);
-	  path = (char *) malloc (1 + len);
-	  if (path == NULL)
-	    return -1;
+	  path = name + pathlen + len + 1;
 	  path[0] = ':';
-	  (void) confstr (_CS_PATH, path + 1, len);
-	  path_malloc = path;
+	  (void) confstr (_CS_PATH, path + 1, pathlen);
 	}
 
-      size_t len = strlen (file) + 1;
-      size_t pathlen = strlen (path);
-      char *name = malloc (pathlen + len + 1);
-      if (name == NULL)
-	{
-	  free (path_malloc);
-	  return -1;
-	}
       /* Copy the file name at the top.  */
       name = (char *) memcpy (name + pathlen + 1, file, len);
       /* And add the slash.  */
       *--name = '/';
 
+      char **script_argv = NULL;
+      void *script_argv_malloc = NULL;
       bool got_eacces = false;
       char *p = path;
       do
@@ -139,7 +153,15 @@ execvp (file, argv)
 	    {
 	      if (script_argv == NULL)
 		{
-		  script_argv = allocate_scripts_argv (startp, argv);
+		  /* Count the arguments.  */
+		  int argc = 0;
+		  while (argv[argc++])
+		    ;
+		  size_t arglen = (argc + 1) * sizeof (char *);
+		  if (__libc_use_alloca (alloclen + arglen))
+		    script_argv = alloca (arglen);
+		  else
+		    script_argv = script_argv_malloc = malloc (arglen);
 		  if (script_argv == NULL)
 		    {
 		      /* A possible EACCES error is not as important as
@@ -147,6 +169,7 @@ execvp (file, argv)
 		      got_eacces = false;
 		      break;
 		    }
+		  scripts_argv (startp, argv, argc, script_argv);
 		}
 
 	      __execve (script_argv[0], script_argv, __environ);
@@ -184,11 +207,10 @@ execvp (file, argv)
       /* We tried every element and none of them worked.  */
       if (got_eacces)
 	/* At least one failure was due to permissions, so report that
-           error.  */
+	   error.  */
 	__set_errno (EACCES);
 
-      free (script_argv);
-      free (name - pathlen);
+      free (script_argv_malloc);
       free (path_malloc);
     }
 
diff --git a/posix/tst-vfork3.c b/posix/tst-vfork3.c
new file mode 100644
index 0000000000..954d395fd2
--- /dev/null
+++ b/posix/tst-vfork3.c
@@ -0,0 +1,224 @@
+/* Test for vfork functions.
+   Copyright (C) 2007 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
+
+   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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <mcheck.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+static int do_test (void);
+static void do_prepare (void);
+char *tmpdirname;
+
+#define TEST_FUNCTION do_test ()
+#define PREPARE(argc, argv) do_prepare ()
+#include "../test-skeleton.c"
+
+static int
+do_test (void)
+{
+  mtrace ();
+
+  const char *path = getenv ("PATH");
+  if (path == NULL)
+    path = "/bin";
+  char pathbuf[strlen (tmpdirname) + 1 + strlen (path) + 1];
+  strcpy (stpcpy (stpcpy (pathbuf, tmpdirname), ":"), path);
+  if (setenv ("PATH", pathbuf, 1) < 0)
+    {
+      puts ("setenv failed");
+      return 1;
+    }
+
+  size_t i;
+  char *argv[3] = { (char *) "script1.sh", (char *) "1", NULL };
+  for (i = 0; i < 5; i++)
+    {
+      pid_t pid = vfork ();
+      if (pid < 0)
+	{
+	  printf ("vfork failed: %m\n");
+	  return 1;
+	}
+      else if (pid == 0)
+	{
+	  execvp ("script1.sh", argv);
+	  _exit (errno);
+	}
+      int status;
+      if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
+	{
+	  puts ("waitpid failed");
+	  return 1;
+	}
+      else if (status != 0)
+	{
+	  if (WIFEXITED (status))
+	    printf ("script1.sh failed with status %d\n",
+		    WEXITSTATUS (status));
+	  else
+	    printf ("script1.sh kill by signal %d\n",
+		    WTERMSIG (status));
+	  return 1;
+	}
+    }
+
+  argv[0] = (char *) "script2.sh";
+  argv[1] = (char *) "2";
+  for (i = 0; i < 5; i++)
+    {
+      pid_t pid = vfork ();
+      if (pid < 0)
+	{
+	  printf ("vfork failed: %m\n");
+	  return 1;
+	}
+      else if (pid == 0)
+	{
+	  execvp ("script2.sh", argv);
+	  _exit (errno);
+	}
+      int status;
+      if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
+	{
+	  puts ("waitpid failed");
+	  return 1;
+	}
+      else if (status != 0)
+	{
+	  printf ("script2.sh failed with status %d\n", status);
+	  return 1;
+	}
+    }
+
+  for (i = 0; i < 5; i++)
+    {
+      pid_t pid = vfork ();
+      if (pid < 0)
+	{
+	  printf ("vfork failed: %m\n");
+	  return 1;
+	}
+      else if (pid == 0)
+	{
+	  execlp ("script2.sh", "script2.sh", "3", NULL);
+	  _exit (errno);
+	}
+      int status;
+      if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
+	{
+	  puts ("waitpid failed");
+	  return 1;
+	}
+      else if (status != 0)
+	{
+	  printf ("script2.sh failed with status %d\n", status);
+	  return 1;
+	}
+    }
+
+  unsetenv ("PATH");
+  argv[0] = (char *) "echo";
+  argv[1] = (char *) "script 4";
+  for (i = 0; i < 5; i++)
+    {
+      pid_t pid = vfork ();
+      if (pid < 0)
+	{
+	  printf ("vfork failed: %m\n");
+	  return 1;
+	}
+      else if (pid == 0)
+	{
+	  execvp ("echo", argv);
+	  _exit (errno);
+	}
+      int status;
+      if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
+	{
+	  puts ("waitpid failed");
+	  return 1;
+	}
+      else if (status != 0)
+	{
+	  printf ("echo failed with status %d\n", status);
+	  return 1;
+	}
+    }
+
+  return 0;
+}
+
+static void
+do_prepare (void)
+{
+  size_t len = strlen (test_dir) + sizeof ("/tst-vfork3.XXXXXX");
+  tmpdirname = malloc (len);
+  char *script1 = malloc (len + sizeof "/script1.sh");
+  char *script2 = malloc (len + sizeof "/script2.sh");
+  if (tmpdirname == NULL || script1 == NULL || script2 == NULL)
+    {
+      puts ("out of memory");
+      exit (1);
+    }
+  strcpy (stpcpy (tmpdirname, test_dir), "/tst-vfork3.XXXXXX");
+
+  tmpdirname = mkdtemp (tmpdirname);
+  if (tmpdirname == NULL)
+    {
+      puts ("could not create temporary directory");
+      exit (1);
+    }
+
+  strcpy (stpcpy (script1, tmpdirname), "/script1.sh");
+  strcpy (stpcpy (script2, tmpdirname), "/script2.sh");
+
+  /* Need to make sure tmpdirname is at the end of the linked list.  */
+  add_temp_file (script1);
+  add_temp_file (tmpdirname);
+  add_temp_file (script2);
+
+  const char content1[] = "#!/bin/sh\necho script $1\n";
+  int fd = open (script1, O_WRONLY | O_CREAT, 0700);
+  if (fd < 0
+      || TEMP_FAILURE_RETRY (write (fd, content1, sizeof content1))
+	 != sizeof content1
+      || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
+    {
+      printf ("Could not write %s\n", script1);
+      exit (1);
+    }
+  close (fd);
+
+  const char content2[] = "echo script $1\n";
+  fd = open (script2, O_WRONLY | O_CREAT, 0700);
+  if (fd < 0
+      || TEMP_FAILURE_RETRY (write (fd, content2, sizeof content2))
+	 != sizeof content2
+      || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
+    {
+      printf ("Could not write %s\n", script2);
+      exit (1);
+    }
+  close (fd);
+}
diff --git a/stdlib/Makefile b/stdlib/Makefile
index 2699ca61f6..f6648355cb 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1991-2002,2003,2004,2005,2006 Free Software Foundation, Inc.
+# Copyright (C) 1991-2006, 2007 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
@@ -113,6 +113,7 @@ include ../Rules
 test-canon-ARGS = --test-dir=${common-objpfx}stdlib
 
 tst-strtod-ENV = LOCPATH=$(common-objpfx)localedata
+tst-strtod3-ENV = LOCPATH=$(common-objpfx)localedata
 testmb2-ENV = LOCPATH=$(common-objpfx)localedata
 
 # Run a test on the header files we use.
diff --git a/string/Makefile b/string/Makefile
index 3e79f4d909..f087022b3c 100644
--- a/string/Makefile
+++ b/string/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1991-2002, 2005, 2006 Free Software Foundation, Inc.
+# Copyright (C) 1991-2002, 2005, 2006, 2007 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
@@ -64,6 +64,7 @@ tester-ENV = LANGUAGE=C
 inl-tester-ENV = LANGUAGE=C
 noinl-tester-ENV = LANGUAGE=C
 tst-strxfrm-ENV = LOCPATH=$(common-objpfx)localedata
+tst-strxfrm2-ENV = LOCPATH=$(common-objpfx)localedata
 bug-strcoll1-ENV = LOCPATH=$(common-objpfx)localedata
 CFLAGS-inl-tester.c = -fno-builtin
 CFLAGS-noinl-tester.c = -fno-builtin