aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar netblue30 <netblue30@protonmail.com>2022-05-20 10:50:21 -0400
committerLibravatar netblue30 <netblue30@protonmail.com>2022-05-20 10:50:21 -0400
commitbbdd82e2953e8aad8eaa432a93e631422da17470 (patch)
treea3b6a4f13ebcc7a23d6032233b4bf14729a7ffa3 /src
parent--oom (#5122) (diff)
downloadfirejail-bbdd82e2953e8aad8eaa432a93e631422da17470.tar.gz
firejail-bbdd82e2953e8aad8eaa432a93e631422da17470.tar.zst
firejail-bbdd82e2953e8aad8eaa432a93e631422da17470.zip
--oom
Diffstat (limited to 'src')
-rw-r--r--src/firejail/oom.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/firejail/oom.c b/src/firejail/oom.c
new file mode 100644
index 000000000..11d08fc48
--- /dev/null
+++ b/src/firejail/oom.c
@@ -0,0 +1,87 @@
1/*
2 * Copyright (C) 2014-2022 Firejail Authors
3 *
4 * This file is part of firejail project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20#include "firejail.h"
21#include <sys/wait.h>
22
23void oom_set(const char *oom_string) {
24 int oom = atoi(oom_string);
25 uid_t uid = getuid();
26 if (uid == 0) {
27 if (oom < -1000 || oom > 1000) {
28 fprintf(stderr, "Error: invalid oom value (-1000 to 1000)\n");
29 exit(1);
30 }
31 }
32 else {
33 if (oom < 0 || oom > 1000) {
34 fprintf(stderr, "Error: invalid oom value (0 to 1000)\n");
35 exit(1);
36 }
37 }
38
39 pid_t parent = getpid();
40 pid_t child = fork();
41 if (child == -1)
42 errExit("fork");
43 if (child == 0) {
44 EUID_ROOT();
45 // elevate privileges
46 if (setreuid(0, 0))
47 errExit("setreuid");
48 if (setregid(0, 0))
49 errExit("setregid");
50
51 char *fname;
52 if (asprintf(&fname, "/proc/%d/oom_score_adj", parent) == -1)
53 errExit("asprintf");
54 FILE *fp = fopen(fname, "w");
55 if (!fp) {
56 fprintf(stderr, "Error: cannot open %s\n", fname);
57 exit(1);
58 }
59 fprintf(fp, "%d", oom);
60 fclose(fp);
61 free(fname);
62
63 if (asprintf(&fname, "/proc/%d/oom_score", parent) == -1)
64 errExit("asprintf");
65 fp = fopen(fname, "r");
66 if (!fp) {
67 fprintf(stderr, "Error: cannot open %s\n", fname);
68 exit(1);
69 }
70 int newoom;
71 if (1 != fscanf(fp, "%d", &newoom)) {
72 fprintf(stderr, "Error: connot read from %s\n", fname);
73 exit(1);
74 }
75 fclose(fp);
76 free(fname);
77
78 if (!arg_quiet)
79 printf("Out-Of-Memory score adjusted, new value %d\n", newoom);
80 exit(0);
81 }
82
83 int status;
84 if (waitpid(child, &status, 0) == -1 )
85 errExit("waitpid");
86}
87