summaryrefslogtreecommitdiff
path: root/openbsd-compat/port-linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/port-linux.c')
-rw-r--r--openbsd-compat/port-linux.c57
1 files changed, 37 insertions, 20 deletions
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
index 89b9a7340..5b1cf402c 100644
--- a/openbsd-compat/port-linux.c
+++ b/openbsd-compat/port-linux.c
@@ -1,4 +1,4 @@
1/* $Id: port-linux.c,v 1.8 2010/03/01 04:52:50 dtucker Exp $ */ 1/* $Id: port-linux.c,v 1.11 2011/01/17 07:50:24 dtucker Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com> 4 * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
@@ -45,7 +45,7 @@ ssh_selinux_enabled(void)
45 static int enabled = -1; 45 static int enabled = -1;
46 46
47 if (enabled == -1) { 47 if (enabled == -1) {
48 enabled = is_selinux_enabled(); 48 enabled = (is_selinux_enabled() == 1);
49 debug("SELinux support %s", enabled ? "enabled" : "disabled"); 49 debug("SELinux support %s", enabled ? "enabled" : "disabled");
50 } 50 }
51 51
@@ -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
218static int oom_adj_save = INT_MIN; 217static int oom_adj_save = INT_MIN;
218static char *oom_adj_path = NULL;
219struct {
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;
224void 232void
225oom_adjust_setup(void) 233oom_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_path == 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;