[Clfs-commits] commit: r5044 - /trunk/BOOK/final-system/multilib/multiarch_wrapper.xml

svn at cross-lfs.org svn at cross-lfs.org
Mon Jun 8 19:36:43 PDT 2009


Author: jciccone
Date: Tue Jun  9 02:36:42 2009
New Revision: 5044

Log:
Update the Multiarch Wrapper after the discussion in ticket #210. Thanks manurootcpp for pointing out an obvious memory leak in the wrapper.

Modified:
    trunk/BOOK/final-system/multilib/multiarch_wrapper.xml

Modified: trunk/BOOK/final-system/multilib/multiarch_wrapper.xml
==============================================================================
--- trunk/BOOK/final-system/multilib/multiarch_wrapper.xml (original)
+++ trunk/BOOK/final-system/multilib/multiarch_wrapper.xml Tue Jun  9 02:36:42 2009
@@ -28,32 +28,59 @@
     <para os="a">Create the source file:</para>
 
 <screen os="b"><userinput>cat > multiarch_wrapper.c << "EOF"
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
 
-#ifndef USE_ARCH
-#define USE_ARCH "64"
+#ifndef DEF_SUFFIX
+#  define DEF_SUFFIX "64"
 #endif
 
 int main(int argc, char **argv)
 {
   char *filename;
-  char *use_arch;
+  char *suffix;
 
-  if(!(use_arch = getenv("USE_ARCH")))
-    use_arch = USE_ARCH;
+  if(!(suffix = getenv("USE_ARCH")))
+    if(!(suffix = getenv("BUILDENV")))
+      suffix = DEF_SUFFIX;
 
-  filename = (char *) malloc(strlen(argv[0]) + strlen(use_arch) + 2);
-  strcpy(filename, argv[0]);
-  strcat(filename, "-");
-  strcat(filename, use_arch);
+  if (asprintf(&filename, "%s-%s", argv[0], suffix) < 0) {
+    perror(argv[0]);
+    return -1;
+  }
 
-  execvp(filename, argv);
+  int status = EXIT_FAILURE;
+  pid_t pid = fork();
+
+  if (pid == 0) {
+    execvp(filename, argv);
+    perror(filename);
+    goto end;
+  } else if (pid < 0) {
+    goto end_error;
+  } else {
+    if (waitpid(pid, &status, 0) != pid) {
+      status = EXIT_FAILURE;
+      goto end_error;
+    }
+    status = WEXITSTATUS(status);
+  }
+  goto end;
+
+end_error:
   perror(argv[0]);
+end:
   free(filename);
+
+  return status;
 }
+
 EOF</userinput></screen>
 
     <para os="c">Compile and Install the Multiarch Wrapper:</para>




More information about the Clfs-commits mailing list