@cryptotaxi247 / netdata-1 / commits / 58863a7f5

allow user to set out of memory score via environment and disable it in configuration file; fixes #2036

Costa Tsaousis (ktsaou) committed Mar 30, 2017 at 00:32 UTC 58863a7f5aab111df0b63adb48518ddf55317dd8
1 file changed +25 -5
src/daemon.c
+25 -5
@@ -155,22 +155,42 @@ int become_user(const char *username, int pid_fd)
155 return(0);
156 }
157
158 +#ifndef OOM_SCORE_ADJ_MAX
159 +#define OOM_SCORE_ADJ_MAX 1000
160 +#endif
161 +#ifndef OOM_SCORE_ADJ_MIN
162 +#define OOM_SCORE_ADJ_MIN -1000
163 +#endif
164 +
165 static void oom_score_adj(void) {
159 - int score = (int)config_get_number(CONFIG_SECTION_GLOBAL, "OOM score", 1000);
166 + char buf[10 + 1];
167 + snprintfz(buf, 10, "%d", OOM_SCORE_ADJ_MAX);
168 +
169 + // check the environment
170 + char *s = getenv("OOMScoreAdjust");
171 + if(!s || !*s) s = buf;
172 +
173 + // check netdata.conf configuration
174 + s = config_get(CONFIG_SECTION_GLOBAL, "OOM score", s);
175 + if(!s || !*s) s = buf;
176 +
177 + if(!isdigit(*s) && *s != '-' && *s != '+') {
178 + info("Out-Of-Memory score not changed due to setting: '%s'", s);
179 + return;
180 + }
181
182 int done = 0;
183 int fd = open("/proc/self/oom_score_adj", O_WRONLY);
184 if(fd != -1) {
164 - char buf[10 + 1];
165 - ssize_t len = snprintfz(buf, 10, "%d", score);
185 + ssize_t len = strlen(s);
186 if(len > 0 && write(fd, buf, (size_t)len) == len) done = 1;
187 close(fd);
188 }
189
190 if(!done)
171 - error("Cannot adjust my Out-Of-Memory score to %d.", score);
191 + error("Cannot adjust my Out-Of-Memory score to '%s'.", s);
192 else
173 - debug(D_SYSTEM, "Adjusted my Out-Of-Memory score to %d.", score);
193 + info("Adjusted my Out-Of-Memory score to '%s'.", s);
194 }
195
196 static void process_nice_level(void) {