forked from community-ssu/dsme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoom.c
73 lines (56 loc) · 1.67 KB
/
oom.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
/**
@file oom.c
This file implements functions to protect/unprotect from the OOM killer
by setting the appropriate value to oom_adj.
<p>
Copyright (C) 2006-2009 Nokia Corporation.
@author Semi Malinen <[email protected]>
This file is part of Dsme.
Dsme is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Dsme is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Dsme. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dsme/oom.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define OOM_ADJ_PATH "/proc/self/oom_adj"
#define OOM_ADJ_PROTECT_VALUE (-17)
#define OOM_ADJ_UNPROTECT_VALUE 0
static bool set_oom_adj_value(int i)
{
FILE* file = 0;
file = fopen(OOM_ADJ_PATH, "w");
if (!file) {
perror(OOM_ADJ_PATH);
return false;
}
if (fprintf(file, "%i", i) < 0) {
fprintf(stderr, "%s: Write failed\n", OOM_ADJ_PATH);
fclose(file);
return false;
}
if (fclose(file) < 0) {
perror(OOM_ADJ_PATH);
return false;
}
return true;
}
bool protect_from_oom(void)
{
return set_oom_adj_value(OOM_ADJ_PROTECT_VALUE);
}
bool unprotect_from_oom(void)
{
return set_oom_adj_value(OOM_ADJ_UNPROTECT_VALUE);
}
bool adjust_oom(int oom_adj)
{
return set_oom_adj_value(oom_adj);
}