-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
123 lines (91 loc) · 1.82 KB
/
util.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Utilities for the examples
Author: R. Koucha
Date: 25-Mar-2020
*/
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include "util.h"
int is_pid(const char *str)
{
const char *p = str;
while (*p) {
if (!isdigit(*p)) {
return 0;
}
p ++;
} // End while
return 1;
} // is_pid
int is_integer(const char *str)
{
const char *p = str;
if ('-' == *p) {
p ++;
}
return is_pid(p);
} // is_integer
int is_ns_name(const char *ns)
{
const char *ns_all[] = {"cgroup", "ipc", "mnt", "net", "pid", "uts", "user", NULL };
int i;
for (i = 0; ns_all[i]; i ++) {
if (!strcmp(ns, ns_all[i])) {
return 1;
}
} // End for
return 0;
} // is_valid_ns
int getanswer(void)
{
int c, c1;
c = fgetc(stdin);
// Flush stdin
c1 = c;
while (c1 != '\n') {
c1 = fgetc(stdin);
}
return c;
} // getanswer
int cmp_ns(pid_t pid1, pid_t pid2, const char *ns_name)
{
char path1[256], path2[256];
struct stat stbuf1, stbuf2;
int rc;
rc = snprintf(path1, sizeof(path1), "/proc/%d/ns/%s", pid1, ns_name);
if (rc < 0 || rc >= (int)sizeof(path1)) {
if (rc >= 0) {
errno = ENOSPC;
}
return -1;
}
rc = snprintf(path2, sizeof(path2), "/proc/%d/ns/%s", pid2, ns_name);
if (rc < 0 || rc >= (int)sizeof(path2)) {
if (rc >= 0) {
errno = ENOSPC;
}
return -1;
}
rc = stat(path1, &stbuf1);
if (rc < 0) {
ERR("stat(%s): '%m' (%d)\n", path1, errno);
return -1;
}
rc = stat(path2, &stbuf2);
if (rc < 0) {
ERR("stat(%s): '%m' (%d)\n", path2, errno);
return -1;
}
// Comparison of the identifiers
if ((stbuf1.st_dev == stbuf2.st_dev) &&
(stbuf1.st_ino == stbuf2.st_ino)) {
return 1;
} else {
return 0;
}
} // cmp_ns