diff options
author | Darren Tucker <dtucker@zip.com.au> | 2011-01-17 11:55:59 +1100 |
---|---|---|
committer | Darren Tucker <dtucker@zip.com.au> | 2011-01-17 11:55:59 +1100 |
commit | 0c93adc7c1814b113d25c5e214973a3aa630b0af (patch) | |
tree | d55c4497e9b77183ca6c98b55137c55f8b55500f /openbsd-compat/port-linux.c | |
parent | 1ccbfa88b1defffa6cd4b533bcc97f737162afee (diff) |
- (dtucker) [openbsd-compat/port-linux.c] Bug #1838: Add support for the new
Linux OOM-killer magic values that changed in 2.6.36 kernels, with fallback
to the old values. Feedback from vapier at gentoo org and djm, ok djm.
Diffstat (limited to 'openbsd-compat/port-linux.c')
-rw-r--r-- | openbsd-compat/port-linux.c | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c index 86d16dc6e..d89101d18 100644 --- a/openbsd-compat/port-linux.c +++ b/openbsd-compat/port-linux.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $Id: port-linux.c,v 1.9 2010/09/10 00:30:25 dtucker Exp $ */ | 1 | /* $Id: port-linux.c,v 1.10 2011/01/17 00:56:00 dtucker Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com> | 4 | * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com> |
@@ -208,14 +208,22 @@ ssh_selinux_change_context(const char *newname) | |||
208 | #endif /* WITH_SELINUX */ | 208 | #endif /* WITH_SELINUX */ |
209 | 209 | ||
210 | #ifdef LINUX_OOM_ADJUST | 210 | #ifdef LINUX_OOM_ADJUST |
211 | #define OOM_ADJ_PATH "/proc/self/oom_adj" | ||
212 | /* | 211 | /* |
213 | * The magic "don't kill me", as documented in eg: | 212 | * The magic "don't kill me" values, old and new, as documented in eg: |
214 | * http://lxr.linux.no/#linux+v2.6.32/Documentation/filesystems/proc.txt | 213 | * http://lxr.linux.no/#linux+v2.6.32/Documentation/filesystems/proc.txt |
214 | * http://lxr.linux.no/#linux+v2.6.36/Documentation/filesystems/proc.txt | ||
215 | */ | 215 | */ |
216 | #define OOM_ADJ_NOKILL -17 | ||
217 | 216 | ||
218 | static int oom_adj_save = INT_MIN; | 217 | static int oom_adj_save = INT_MIN; |
218 | static char *oom_adj_path = NULL; | ||
219 | struct { | ||
220 | char *path; | ||
221 | int value; | ||
222 | } oom_adjust[] = { | ||
223 | {"/proc/self/oom_score_adj", -1000}, /* kernels >= 2.6.36 */ | ||
224 | {"/proc/self/oom_adj", -17}, /* kernels <= 2.6.35 */ | ||
225 | {NULL, 0}, | ||
226 | }; | ||
219 | 227 | ||
220 | /* | 228 | /* |
221 | * Tell the kernel's out-of-memory killer to avoid sshd. | 229 | * Tell the kernel's out-of-memory killer to avoid sshd. |
@@ -224,23 +232,31 @@ static int oom_adj_save = INT_MIN; | |||
224 | void | 232 | void |
225 | oom_adjust_setup(void) | 233 | oom_adjust_setup(void) |
226 | { | 234 | { |
235 | int i, value; | ||
227 | FILE *fp; | 236 | FILE *fp; |
228 | 237 | ||
229 | debug3("%s", __func__); | 238 | debug3("%s", __func__); |
230 | if ((fp = fopen(OOM_ADJ_PATH, "r+")) != NULL) { | 239 | for (i = 0; oom_adjust[i].path != NULL; i++) { |
231 | if (fscanf(fp, "%d", &oom_adj_save) != 1) | 240 | oom_adj_path = oom_adjust[i].path; |
232 | verbose("error reading %s: %s", OOM_ADJ_PATH, strerror(errno)); | 241 | value = oom_adjust[i].value; |
233 | else { | 242 | if ((fp = fopen(oom_adj_path, "r+")) != NULL) { |
234 | rewind(fp); | 243 | if (fscanf(fp, "%d", &oom_adj_save) != 1) |
235 | if (fprintf(fp, "%d\n", OOM_ADJ_NOKILL) <= 0) | 244 | verbose("error reading %s: %s", oom_adj_path, |
236 | verbose("error writing %s: %s", | 245 | strerror(errno)); |
237 | OOM_ADJ_PATH, strerror(errno)); | 246 | else { |
238 | else | 247 | rewind(fp); |
239 | verbose("Set %s from %d to %d", | 248 | if (fprintf(fp, "%d\n", value) <= 0) |
240 | OOM_ADJ_PATH, oom_adj_save, OOM_ADJ_NOKILL); | 249 | verbose("error writing %s: %s", |
250 | oom_adj_path, strerror(errno)); | ||
251 | else | ||
252 | verbose("Set %s from %d to %d", | ||
253 | oom_adj_path, oom_adj_save, value); | ||
254 | } | ||
255 | fclose(fp); | ||
256 | return; | ||
241 | } | 257 | } |
242 | fclose(fp); | ||
243 | } | 258 | } |
259 | oom_adj_path = NULL; | ||
244 | } | 260 | } |
245 | 261 | ||
246 | /* Restore the saved OOM adjustment */ | 262 | /* Restore the saved OOM adjustment */ |
@@ -250,13 +266,14 @@ oom_adjust_restore(void) | |||
250 | FILE *fp; | 266 | FILE *fp; |
251 | 267 | ||
252 | debug3("%s", __func__); | 268 | debug3("%s", __func__); |
253 | if (oom_adj_save == INT_MIN || (fp = fopen(OOM_ADJ_PATH, "w")) == NULL) | 269 | if (oom_adj_save == INT_MIN || oom_adj_save == NULL || |
270 | (fp = fopen(oom_adj_path, "w")) == NULL) | ||
254 | return; | 271 | return; |
255 | 272 | ||
256 | if (fprintf(fp, "%d\n", oom_adj_save) <= 0) | 273 | if (fprintf(fp, "%d\n", oom_adj_save) <= 0) |
257 | verbose("error writing %s: %s", OOM_ADJ_PATH, strerror(errno)); | 274 | verbose("error writing %s: %s", oom_adj_path, strerror(errno)); |
258 | else | 275 | else |
259 | verbose("Set %s to %d", OOM_ADJ_PATH, oom_adj_save); | 276 | verbose("Set %s to %d", oom_adj_path, oom_adj_save); |
260 | 277 | ||
261 | fclose(fp); | 278 | fclose(fp); |
262 | return; | 279 | return; |