-
Notifications
You must be signed in to change notification settings - Fork 5
/
pid_ns.c
88 lines (79 loc) · 1.81 KB
/
pid_ns.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
#include "types.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "x86.h"
#include "proc.h"
#include "spinlock.h"
#include "wstatus.h"
#include "pid_ns.h"
#include "namespace.h"
struct {
struct spinlock lock;
struct pid_ns namespaces[NNAMESPACE];
} pidnstable;
void pid_ns_init()
{
initlock(&pidnstable.lock, "pidns");
for (int i = 0; i < NNAMESPACE; i++) {
initlock(&pidnstable.namespaces[i].lock, "pidns");
pidnstable.namespaces[i].ref = 0;
}
}
void pid_ns_put(struct pid_ns* pid_ns)
{
acquire(&pidnstable.lock);
if (!pid_ns->ref) {
panic("pid_ns_put: ref == 0");
}
pid_ns->ref--;
release(&pidnstable.lock);
}
void pid_ns_get(struct pid_ns* pid_ns)
{
acquire(&pidnstable.lock);
pid_ns->ref++;
release(&pidnstable.lock);
}
struct pid_ns* pid_ns_alloc() {
acquire(&pidnstable.lock);
for (int i = 0; i < NNAMESPACE; i++) {
struct pid_ns* pid_ns = &pidnstable.namespaces[i];
if (pid_ns->ref == 0) {
pid_ns->ref = 1;
release(&pidnstable.lock);
return pid_ns;
}
}
release(&pidnstable.lock);
panic("out of pid_ns objects");
}
void pid_ns_init_ns(struct pid_ns* pid_ns, struct pid_ns* parent) {
pid_ns->parent = parent;
pid_ns->next_pid = 1;
pid_ns->pid1_ns_killed = 0;
}
struct pid_ns* pid_ns_dup(struct pid_ns* pid_ns) {
pid_ns_get(pid_ns);
return pid_ns;
}
struct pid_ns* pid_ns_new(struct pid_ns* parent) {
struct pid_ns * pid_ns = pid_ns_alloc();
pid_ns_init_ns(pid_ns, parent);
return pid_ns;
}
int pid_ns_next_pid(struct pid_ns* pid_ns) {
acquire(&pid_ns->lock);
int pid = pid_ns->next_pid++;
release(&pid_ns->lock);
return pid;
}
int pid_ns_is_max_depth(struct pid_ns* pid_ns) {
int depth = 0;
while (pid_ns) {
depth++;
pid_ns = pid_ns->parent;
}
return depth >= MAX_PID_NS_DEPTH;
}