summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/build-fhsenv-bubblewrap/container-init.cc
blob: 82c1cb57ed5a5a7d43d6421e085bd1970ef93eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <fstream>

#include <fcntl.h>
#include <spawn.h>
#include <string.h>
#include <unistd.h>

#include <sys/wait.h>

const char LD_SO_CONF[] = R"(/lib
/lib/x86_64-linux-gnu
/lib64
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib64
/lib/i386-linux-gnu
/lib32
/usr/lib/i386-linux-gnu
/usr/lib32
/run/opengl-driver/lib
/run/opengl-driver-32/lib
)";

int main(int, const char *argv[]) {
  std::ofstream ld_so_conf;
  ld_so_conf.open("/etc/ld.so.conf");
  ld_so_conf << LD_SO_CONF;
  ld_so_conf.close();
  if (!ld_so_conf) {
    perror("Failed to generate ld.so.conf");
    return 1;
  }

  int e;
  pid_t pid;
  const char *ldconfig_argv[] = {"/bin/ldconfig", NULL};
  char *ldconfig_envp[] = {NULL};
  posix_spawn_file_actions_t action;
  posix_spawn_file_actions_init(&action);
  posix_spawn_file_actions_addopen (&action, STDERR_FILENO, "/dev/null", O_WRONLY|O_APPEND, 0);
  if ((e = posix_spawn(&pid, ldconfig_argv[0], &action, NULL,
                       (char *const *)ldconfig_argv, ldconfig_envp))) {
    fprintf(stderr, "Failed to run ldconfig: %s\n", strerror(e));
    return 1;
  }
  posix_spawn_file_actions_destroy(&action);

  int status;
  if (waitpid(pid, &status, 0) == -1) {
    perror("Failed to wait for ldconfig");
    return 1;
  }
  if (WIFEXITED(status)) {
    if (WEXITSTATUS(status)) {
      fprintf(stderr, "ldconfig exited %d\n", WEXITSTATUS(status));
      return 1;
    }
  } else {
    fprintf(stderr, "ldconfig killed by signal %d\n", WTERMSIG(status));
    return 1;
  }

  argv[0] = "/init";
  execv(argv[0], (char *const *)argv);

  perror("Failed to exec stage 2 init");
  return 1;
}