[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 07:02:07 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):

 I just found a mistake that had to do with the return value of the child
 process. The status returned by waitpid can not be used directly as the
 return value of the process. Other bits are used to store other flags. The
 fixed version is as follows:

 {{{
 #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;
     }
     status = WEXITSTATUS(status);
   }
   goto end;

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

   return status;
 }
 }}}

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


More information about the Clfs-commits mailing list