// File: /workspace/github_c_makefile_star2_O2/arachsys[P]containers/inject.c
// Function: main
int main(int argc, char **argv, char **envp) {
  char *end, *item = NULL, *path;
  pid_t child = -1, parent, pid;
  size_t size;
  struct dirent *entry;
  DIR *dir;
  FILE *file;

  seal(argv, envp);
  if (argc < 2)
    usage(argv[0]);

  parent = strtol(argv[1], &end, 10);
  if (end == argv[1] || *end)
    usage(argv[0]);

  if (geteuid() != getuid())
    errx(EXIT_FAILURE, "setuid installation is unsafe");
  else if (getegid() != getgid())
    errx(EXIT_FAILURE, "setgid installation is unsafe");

  join(parent, "user");
  setgid(0);
  setgroups(0, NULL);
  setuid(0);

  if (!(dir = opendir("/proc")))
    errx(EXIT_FAILURE, "Failed to list processes");
  while (child < 0 && (entry = readdir(dir))) {
    pid = strtol(entry->d_name, &end, 10);
    if (end == entry->d_name || *end)
      continue;
    if (getparent(pid) == parent) {
      path = string("/proc/%u/environ", pid);
      if ((file = fopen(path, "r"))) {
        while (getdelim(&item, &size, '\0', file) >= 0)
          if (strcmp(item, "container=contain") == 0)
            child = pid;
        fclose(file);
      }
      free(path);
    }
  }
  closedir(dir);
  if (item)
    free(item);

  if (child < 0)
    errx(EXIT_FAILURE, "PID %u is not a container supervisor", parent);

  join(child, "cgroup");
  join(child, "ipc");
  join(child, "net");
  join(child, "pid");
  join(child, "time");
  join(child, "uts");
  join(child, "mnt");

  if (chdir("/") < 0)
    errx(EXIT_FAILURE, "Failed to enter container root directory");

  switch (child = fork()) {
    case -1:
      err(EXIT_FAILURE, "fork");
    case 0:
      if (argv[2])
        execvp(argv[2], argv + 2);
      else if (getenv("SHELL"))
        execl(getenv("SHELL"), getenv("SHELL"), NULL);
      else
        execl(SHELL, SHELL, NULL);
      err(EXIT_FAILURE, "exec");
  }

  waitforexit(child);
  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movl %edi, %ebx
andq $0x0, 0x8(%rsp)
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x3342
cmpl $0x1, %ebx
jle 0x277b
movq 0x8(%r14), %rdi
leaq 0x28(%rsp), %rbx
pushq $0xa
popq %rdx
movq %rbx, %rsi
callq 0x21c0
movq %rax, 0x10(%rsp)
movq (%rbx), %rax
cmpq 0x8(%r14), %rax
je 0x277b
cmpb $0x0, (%rax)
jne 0x277b
callq 0x2130
movl %eax, %ebx
callq 0x2100
cmpl %eax, %ebx
jne 0x2783
callq 0x2230
movl %eax, %ebx
callq 0x21d0
cmpl %eax, %ebx
jne 0x278c
movq %r14, 0x20(%rsp)
leaq 0x1b84(%rip), %rsi      # 0x4040
movq 0x10(%rsp), %rdi
callq 0x2837
xorl %edi, %edi
callq 0x2270
xorl %edi, %edi
xorl %esi, %esi
callq 0x2150
xorl %edi, %edi
callq 0x22e0
leaq 0x1b61(%rip), %rdi      # 0x4045
callq 0x20c0
testq %rax, %rax
je 0x2795
movq %rax, %r15
pushq $-0x1
popq %rax
leaq 0x28(%rsp), %r12
pushq $0xa
popq %r13
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x4(%rsp)
jns 0x26a1
movq %r15, %rdi
callq 0x21e0
testq %rax, %rax
je 0x2753
movq %rax, %r14
addq $0x13, %r14
movq %r14, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x21c0
movq %rax, %rbx
movq 0x28(%rsp), %rax
cmpq %r14, %rax
je 0x2510
cmpb $0x0, (%rax)
jne 0x2510
andq $0x0, 0x18(%rsp)
leaq 0x1c4e(%rip), %rdi      # 0x41a3
movl %ebx, %esi
xorl %eax, %eax
callq 0x342b
movq %rax, %rbp
movq %rax, %rdi
leaq 0x1c87(%rip), %rsi      # 0x41f2
callq 0x22a0
movq %rax, %r14
movq %rbp, %rdi
callq 0x2040
testq %r14, %r14
je 0x2613
leaq 0x18(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %r14, 0x38(%rsp)
movq %r14, %rdx
callq 0x22c0
pushq $-0x1
popq %r14
movl %r14d, %ebp
testq %rax, %rax
js 0x2602
movq 0x18(%rsp), %rdi
pushq $0x29
popq %rsi
callq 0x2120
movl %r14d, %ebp
testq %rax, %rax
je 0x2602
movq %rax, %rdi
movq %rax, 0x30(%rsp)
callq 0x20e0
movl %r14d, %ebp
cmpq $0x4, %rax
movq 0x30(%rsp), %rax
jb 0x2602
leaq 0x4(%rax), %rdi
leaq 0x40(%rsp), %rsi
movl %r13d, %edx
movq %rax, %r14
callq 0x21c0
movq %rax, %rbp
movq 0x40(%rsp), %rax
cmpq %r14, %rax
je 0x25fd
cmpb $0x20, (%rax)
je 0x2602
pushq $-0x1
popq %rax
movl %eax, %ebp
movq 0x38(%rsp), %rdi
callq 0x20b0
movq 0x18(%rsp), %rdi
jmp 0x261a
xorl %edi, %edi
pushq $-0x1
popq %rax
movl %eax, %ebp
callq 0x2040
cmpl 0x10(%rsp), %ebp
jne 0x2505
leaq 0x1a34(%rip), %rdi      # 0x4064
movl %ebx, %esi
xorl %eax, %eax
callq 0x342b
movq %rax, %r14
movq %rax, %rdi
leaq 0x1bac(%rip), %rsi      # 0x41f2
callq 0x22a0
testq %rax, %rax
je 0x2694
movq %rax, %rbp
leaq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x2310
testq %rax, %rax
js 0x268c
movq 0x8(%rsp), %rdi
leaq 0x19fd(%rip), %rsi      # 0x4075
callq 0x2170
testl %eax, %eax
movl 0x4(%rsp), %eax
cmovel %ebx, %eax
movl %eax, 0x4(%rsp)
jmp 0x2653
movq %rbp, %rdi
callq 0x20b0
movq %r14, %rdi
callq 0x2040
jmp 0x2505
movq %r15, %rdi
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x2040
leaq 0x19f2(%rip), %rsi      # 0x40ac
movl 0x4(%rsp), %ebx
movl %ebx, %edi
callq 0x2837
leaq 0x19e7(%rip), %rsi      # 0x40b3
movl %ebx, %edi
callq 0x2837
leaq 0x19dd(%rip), %rsi      # 0x40b7
movl %ebx, %edi
callq 0x2837
leaq 0x1d0c(%rip), %rsi      # 0x43f4
movl %ebx, %edi
callq 0x2837
leaq 0x19c5(%rip), %rsi      # 0x40bb
movl %ebx, %edi
callq 0x2837
leaq 0x19bc(%rip), %rsi      # 0x40c0
movl %ebx, %edi
callq 0x2837
leaq 0x19b2(%rip), %rsi      # 0x40c4
movl %ebx, %edi
callq 0x2837
leaq 0x19a8(%rip), %rdi      # 0x40c8
callq 0x20f0
testl %eax, %eax
js 0x279e
callq 0x2320
testl %eax, %eax
je 0x27af
cmpl $-0x1, %eax
je 0x27da
movl %eax, %edi
callq 0x3526
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x2040
leaq 0x191b(%rip), %rsi      # 0x4087
pushq $0x1
popq %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2110
movq (%r14), %rdi
callq 0x2813
leaq 0x187a(%rip), %rsi      # 0x4004
jmp 0x27a5
leaq 0x188f(%rip), %rsi      # 0x4022
jmp 0x27a5
leaq 0x18af(%rip), %rsi      # 0x404b
jmp 0x27a5
leaq 0x1925(%rip), %rsi      # 0x40ca
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x2110
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
testq %rdi, %rdi
jne 0x27e3
leaq 0x1934(%rip), %rdi      # 0x40f8
callq 0x2030
testq %rax, %rax
jne 0x27f3
leaq 0x1929(%rip), %rdi      # 0x40fe
movq %rdi, %rsi
jmp 0x27f9
leaq 0x1912(%rip), %rsi      # 0x40f3
jmp 0x2809
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
callq 0x22b0
jmp 0x2802
movq %rax, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2300
leaq 0x18fd(%rip), %rsi      # 0x4106
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x21f0