diff options
Diffstat (limited to 'src/firejail/cpu.c')
-rw-r--r-- | src/firejail/cpu.c | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/src/firejail/cpu.c b/src/firejail/cpu.c index 9c0214502..6b3fc063d 100644 --- a/src/firejail/cpu.c +++ b/src/firejail/cpu.c | |||
@@ -26,13 +26,13 @@ | |||
26 | static void set_cpu(const char *str) { | 26 | static void set_cpu(const char *str) { |
27 | if (strlen(str) == 0) | 27 | if (strlen(str) == 0) |
28 | return; | 28 | return; |
29 | 29 | ||
30 | int val = atoi(str); | 30 | int val = atoi(str); |
31 | if (val < 0 || val >= 32) { | 31 | if (val < 0 || val >= 32) { |
32 | fprintf(stderr, "Error: invalid cpu number. Accepted values are between 0 and 31.\n"); | 32 | fprintf(stderr, "Error: invalid cpu number. Accepted values are between 0 and 31.\n"); |
33 | exit(1); | 33 | exit(1); |
34 | } | 34 | } |
35 | 35 | ||
36 | uint32_t mask = 1; | 36 | uint32_t mask = 1; |
37 | int i; | 37 | int i; |
38 | for (i = 0; i < val; i++, mask <<= 1); | 38 | for (i = 0; i < val; i++, mask <<= 1); |
@@ -41,11 +41,11 @@ static void set_cpu(const char *str) { | |||
41 | 41 | ||
42 | void read_cpu_list(const char *str) { | 42 | void read_cpu_list(const char *str) { |
43 | EUID_ASSERT(); | 43 | EUID_ASSERT(); |
44 | 44 | ||
45 | char *tmp = strdup(str); | 45 | char *tmp = strdup(str); |
46 | if (tmp == NULL) | 46 | if (tmp == NULL) |
47 | errExit("strdup"); | 47 | errExit("strdup"); |
48 | 48 | ||
49 | char *ptr = tmp; | 49 | char *ptr = tmp; |
50 | while (*ptr != '\0') { | 50 | while (*ptr != '\0') { |
51 | if (*ptr == ',' || isdigit(*ptr)) | 51 | if (*ptr == ',' || isdigit(*ptr)) |
@@ -56,7 +56,7 @@ void read_cpu_list(const char *str) { | |||
56 | } | 56 | } |
57 | ptr++; | 57 | ptr++; |
58 | } | 58 | } |
59 | 59 | ||
60 | char *start = tmp; | 60 | char *start = tmp; |
61 | ptr = tmp; | 61 | ptr = tmp; |
62 | while (*ptr != '\0') { | 62 | while (*ptr != '\0') { |
@@ -107,17 +107,17 @@ void set_cpu_affinity(void) { | |||
107 | // set cpu affinity | 107 | // set cpu affinity |
108 | cpu_set_t mask; | 108 | cpu_set_t mask; |
109 | CPU_ZERO(&mask); | 109 | CPU_ZERO(&mask); |
110 | 110 | ||
111 | int i; | 111 | int i; |
112 | uint32_t m = 1; | 112 | uint32_t m = 1; |
113 | for (i = 0; i < 32; i++, m <<= 1) { | 113 | for (i = 0; i < 32; i++, m <<= 1) { |
114 | if (cfg.cpus & m) | 114 | if (cfg.cpus & m) |
115 | CPU_SET(i, &mask); | 115 | CPU_SET(i, &mask); |
116 | } | 116 | } |
117 | 117 | ||
118 | if (sched_setaffinity(0, sizeof(mask), &mask) == -1) | 118 | if (sched_setaffinity(0, sizeof(mask), &mask) == -1) |
119 | fwarning("cannot set cpu affinity\n"); | 119 | fwarning("cannot set cpu affinity\n"); |
120 | 120 | ||
121 | // verify cpu affinity | 121 | // verify cpu affinity |
122 | cpu_set_t mask2; | 122 | cpu_set_t mask2; |
123 | CPU_ZERO(&mask2); | 123 | CPU_ZERO(&mask2); |
@@ -147,7 +147,7 @@ static void print_cpu(int pid) { | |||
147 | return; | 147 | return; |
148 | } | 148 | } |
149 | 149 | ||
150 | #define MAXBUF 4096 | 150 | #define MAXBUF 4096 |
151 | char buf[MAXBUF]; | 151 | char buf[MAXBUF]; |
152 | while (fgets(buf, MAXBUF, fp)) { | 152 | while (fgets(buf, MAXBUF, fp)) { |
153 | if (strncmp(buf, "Cpus_allowed_list:", 18) == 0) { | 153 | if (strncmp(buf, "Cpus_allowed_list:", 18) == 0) { |
@@ -164,7 +164,7 @@ static void print_cpu(int pid) { | |||
164 | 164 | ||
165 | void cpu_print_filter(pid_t pid) { | 165 | void cpu_print_filter(pid_t pid) { |
166 | EUID_ASSERT(); | 166 | EUID_ASSERT(); |
167 | 167 | ||
168 | // if the pid is that of a firejail process, use the pid of the first child process | 168 | // if the pid is that of a firejail process, use the pid of the first child process |
169 | EUID_ROOT(); // grsecurity | 169 | EUID_ROOT(); // grsecurity |
170 | char *comm = pid_proc_comm(pid); | 170 | char *comm = pid_proc_comm(pid); |
@@ -192,4 +192,3 @@ void cpu_print_filter(pid_t pid) { | |||
192 | print_cpu(pid); | 192 | print_cpu(pid); |
193 | exit(0); | 193 | exit(0); |
194 | } | 194 | } |
195 | |||