10.12. Creating a Multiarch Wrapper

The Multiarch Wrapper is used to wrap certain binaries that have hardcoded paths to libraries or are architecture specific.

10.12.1. Installation of The Multiarch Wrapper

Create the source file:

cat > multiarch_wrapper.c << "EOF"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifndef DEFAULT_ARCH
# define DEFAULT_ARCH "64"
#endif

int main (int argc, char *argv[])
{
  char *use_arch;
  if ((use_arch = getenv("USE_ARCH")) == NULL)
    use_arch = DEFAULT_ARCH;

  char *filename = malloc(strlen(argv[0]) + strlen(use_arch) + 2);
  strcpy(filename, argv[0]);
  strcat(filename, "-");
  strcat(filename, use_arch);

  int ret = execvp(filename, argv);
  if ((ret != 0)&&(errno != 0))
  {
    char *errmsg = malloc(strlen(filename) + 19);
    strcpy(errmsg, "Unable to execute ");
    strcat(errmsg, filename);
    perror(errmsg);
    free(errmsg);
  }

  free(filename);

  return ret;
}
EOF

Compile and Install the Multiarch Wrapper:

gcc ${BUILD64} multiarch_wrapper.c -o /usr/bin/multiarch_wrapper

This multiarch wrapper is going to be used later on in the book with perl. It will also be very useful outside of the base CLFS system.

Creating the testcase:

echo 'echo "32bit Version"' > test-32
echo 'echo "64bit Version"' > test-64
chmod 755 test-32 test-64
ln -sv /usr/bin/multiarch_wrapper test

Testing the wrapper:

USE_ARCH=32 ./test
USE_ARCH=64 ./test

The output of the above command should be:

32bit Version
64bit Version

10.12.2. Contents of The Multiarch Wrapper

Installed programs: multiarch_wrapper

Short Descriptions

multiarch_wrapper

Will execute a different program based on the USE_ARCH variable. The USE_ARCH variable will be the suffix of the executed program.