podman

Форк
0
/
container_top_linux.c 
84 строки · 1.8 Кб
1
//go:build !remote
2

3

4
#define _GNU_SOURCE
5
#include <errno.h>
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <sys/mount.h>
9
#include <sys/wait.h>
10
#include <unistd.h>
11

12
/* keep special_exit_code in sync with container_top_linux.go */
13
int special_exit_code = 255;
14
char **argv = NULL;
15

16
void
17
create_argv (int len)
18
{
19
  /* allocate one extra element because we need a final NULL in c */
20
  argv = malloc (sizeof (char *) * (len + 1));
21
  if (argv == NULL)
22
    {
23
      fprintf (stderr, "failed to allocate ps argv");
24
      exit (special_exit_code);
25
    }
26
  /* add final NULL */
27
  argv[len] = NULL;
28
}
29

30
void
31
set_argv (int pos, char *arg)
32
{
33
  argv[pos] = arg;
34
}
35

36
/*
37
  We use cgo code here so we can fork then exec separately,
38
  this is done so we can mount proc after the fork because the pid namespace is
39
  only active after spawning children.
40
*/
41
void
42
fork_exec_ps ()
43
{
44
  int r, status = 0;
45
  pid_t pid;
46

47
  if (argv == NULL)
48
    {
49
      fprintf (stderr, "argv not initialized");
50
      exit (special_exit_code);
51
    }
52

53
  pid = fork ();
54
  if (pid < 0)
55
    {
56
      fprintf (stderr, "fork: %m");
57
      exit (special_exit_code);
58
    }
59
  if (pid == 0)
60
    {
61
      r = mount ("proc", "/proc", "proc", 0, NULL);
62
      if (r < 0)
63
        {
64
          fprintf (stderr, "mount proc: %m");
65
          exit (special_exit_code);
66
        }
67
      /* use execve to unset all env vars, we do not want to leak anything into the container */
68
      execve (argv[0], argv, NULL);
69
      fprintf (stderr, "execve: %m");
70
      exit (special_exit_code);
71
    }
72

73
  r = waitpid (pid, &status, 0);
74
  if (r < 0)
75
    {
76
      fprintf (stderr, "waitpid: %m");
77
      exit (special_exit_code);
78
    }
79
  if (WIFEXITED (status))
80
    exit (WEXITSTATUS (status));
81
  if (WIFSIGNALED (status))
82
    exit (128 + WTERMSIG (status));
83
  exit (special_exit_code);
84
}
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.