[Clfs-commits] [CLFS Trac] #210: The way to use execvp in the multiarch_wrapper, in order to keep control over the processes.

CLFS Trac trac at cross-lfs.org
Sun Jun 7 06:42:05 PDT 2009


#210: The way to use execvp in the multiarch_wrapper, in order to keep control
over the processes.
--------------------------+-------------------------------------------------
  Reporter:  manurootcpp  |       Owner:  clfs-commits at lists.cross-lfs.org.
      Type:  task         |      Status:  new                              
  Priority:  minor        |   Milestone:  CLFS Standard 1.2.0              
 Component:  BOOK         |     Version:  CLFS Standard 1.2.0              
Resolution:               |    Keywords:  multiarch_wrapper                
--------------------------+-------------------------------------------------
Comment (by jciccone):

 How about this:

 {{{
 #define _GNU_SOURCE

 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>

 #ifndef DEF_SUFFIX
 #  define DEF_SUFFIX "64"
 #endif

 int main(int argc, char **argv)
 {
   char *filename;
   char *suffix;

   if(!(suffix = getenv("USE_ARCH")))
     if(!(suffix = getenv("BUILDENV")))
       suffix = DEF_SUFFIX;

   if (asprintf(&filename, "%s-%s", argv[0], suffix) < 0) {
     perror(argv[0]);
     return -1;
   }

   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;
     }
   }
   goto end;

 end_error:
   perror(argv[0]);
 end:
   free(filename);

   return status;
 }
 }}}

 I noticed the version that you provided was missing a few headers and
 _GNU_SOURCE but I borrowed the way you used execvp in a fork. I also
 noticed that the memory still wasn't getting freed when the execvp failed.
 So I changed some of the error handling. In valgrind it now shows all
 memory is freed at any place the program can exit.
 [[BR]]
 I also noticed that when the execvp failed to run because the file was
 missing it would say [blah]-config not found instead of
 [blah]-config-{32,n32,64}. I fixed that by changing the perror.
 [[BR]]
 I also added the ability to use the BUILDENV enviornment variable to do
 the same thing, with USE_ARCH superseding it. In stead of a function I
 used some labels. It made the code a lot smaller and it'd probably be
 easier to type in for those people not using c&p or ssh.
 [[BR]]
 I'd like to get your opinion on the program before I make the change to
 the book. I dont think I missed anything, but its nice to have a second
 set of eyes on it.

-- 
Ticket URL: <http://trac.cross-lfs.org/ticket/210#comment:4>
CLFS Trac <http://trac.cross-lfs.org>
The Cross Linux From Scratch Project.


More information about the Clfs-commits mailing list