diff options
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/port-linux.c | 63 | ||||
-rw-r--r-- | openbsd-compat/port-linux.h | 7 |
2 files changed, 67 insertions, 3 deletions
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c index 88c601e20..cda751dea 100644 --- a/openbsd-compat/port-linux.c +++ b/openbsd-compat/port-linux.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $Id: port-linux.c,v 1.6 2009/10/24 04:04:13 dtucker Exp $ */ | 1 | /* $Id: port-linux.c,v 1.7 2009/12/08 02:39:48 dtucker Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com> | 4 | * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com> |
@@ -23,15 +23,17 @@ | |||
23 | 23 | ||
24 | #include "includes.h" | 24 | #include "includes.h" |
25 | 25 | ||
26 | #if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST) | ||
26 | #include <errno.h> | 27 | #include <errno.h> |
27 | #include <stdarg.h> | 28 | #include <stdarg.h> |
28 | #include <string.h> | 29 | #include <string.h> |
30 | #include <stdio.h> | ||
29 | 31 | ||
30 | #ifdef WITH_SELINUX | ||
31 | #include "log.h" | 32 | #include "log.h" |
32 | #include "xmalloc.h" | 33 | #include "xmalloc.h" |
33 | #include "port-linux.h" | 34 | #include "port-linux.h" |
34 | 35 | ||
36 | #ifdef WITH_SELINUX | ||
35 | #include <selinux/selinux.h> | 37 | #include <selinux/selinux.h> |
36 | #include <selinux/flask.h> | 38 | #include <selinux/flask.h> |
37 | #include <selinux/get_context_list.h> | 39 | #include <selinux/get_context_list.h> |
@@ -204,3 +206,60 @@ ssh_selinux_change_context(const char *newname) | |||
204 | xfree(newctx); | 206 | xfree(newctx); |
205 | } | 207 | } |
206 | #endif /* WITH_SELINUX */ | 208 | #endif /* WITH_SELINUX */ |
209 | |||
210 | #ifdef LINUX_OOM_ADJUST | ||
211 | #define OOM_ADJ_PATH "/proc/self/oom_adj" | ||
212 | /* | ||
213 | * The magic "don't kill me", as documented in eg: | ||
214 | * http://lxr.linux.no/#linux+v2.6.32/Documentation/filesystems/proc.txt | ||
215 | */ | ||
216 | #define OOM_ADJ_NOKILL -17 | ||
217 | |||
218 | static int oom_adj_save = INT_MIN; | ||
219 | |||
220 | /* | ||
221 | * Tell the kernel's out-of-memory killer to avoid sshd. | ||
222 | * Returns the previous oom_adj value or zero. | ||
223 | */ | ||
224 | void | ||
225 | oom_adjust_setup(void) | ||
226 | { | ||
227 | FILE *fp; | ||
228 | |||
229 | debug3("%s", __func__); | ||
230 | if ((fp = fopen(OOM_ADJ_PATH, "r+")) != NULL) { | ||
231 | if (fscanf(fp, "%d", &oom_adj_save) != 1) | ||
232 | logit("error reading %s: %s", OOM_ADJ_PATH, strerror(errno)); | ||
233 | else { | ||
234 | rewind(fp); | ||
235 | if (fprintf(fp, "%d\n", OOM_ADJ_NOKILL) <= 0) | ||
236 | logit("error writing %s: %s", | ||
237 | OOM_ADJ_PATH, strerror(errno)); | ||
238 | else | ||
239 | verbose("Set %s from %d to %d", | ||
240 | OOM_ADJ_PATH, oom_adj_save, OOM_ADJ_NOKILL); | ||
241 | } | ||
242 | fclose(fp); | ||
243 | } | ||
244 | } | ||
245 | |||
246 | /* Restore the saved OOM adjustment */ | ||
247 | void | ||
248 | oom_adjust_restore(void) | ||
249 | { | ||
250 | FILE *fp; | ||
251 | |||
252 | debug3("%s", __func__); | ||
253 | if (oom_adj_save == INT_MIN || (fp = fopen(OOM_ADJ_PATH, "w")) == NULL) | ||
254 | return; | ||
255 | |||
256 | if (fprintf(fp, "%d\n", oom_adj_save) <= 0) | ||
257 | logit("error writing %s: %s", OOM_ADJ_PATH, strerror(errno)); | ||
258 | else | ||
259 | verbose("Set %s to %d", OOM_ADJ_PATH, oom_adj_save); | ||
260 | |||
261 | fclose(fp); | ||
262 | return; | ||
263 | } | ||
264 | #endif /* LINUX_OOM_ADJUST */ | ||
265 | #endif /* WITH_SELINUX || LINUX_OOM_ADJUST */ | ||
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h index 6ad4a49f6..209d9a7a2 100644 --- a/openbsd-compat/port-linux.h +++ b/openbsd-compat/port-linux.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $Id: port-linux.h,v 1.3 2009/10/24 04:04:13 dtucker Exp $ */ | 1 | /* $Id: port-linux.h,v 1.4 2009/12/08 02:39:48 dtucker Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2006 Damien Miller <djm@openbsd.org> | 4 | * Copyright (c) 2006 Damien Miller <djm@openbsd.org> |
@@ -26,4 +26,9 @@ void ssh_selinux_setup_exec_context(char *); | |||
26 | void ssh_selinux_change_context(const char *); | 26 | void ssh_selinux_change_context(const char *); |
27 | #endif | 27 | #endif |
28 | 28 | ||
29 | #ifdef LINUX_OOM_ADJUST | ||
30 | void oom_adjust_restore(void); | ||
31 | void oom_adjust_setup(void); | ||
32 | #endif | ||
33 | |||
29 | #endif /* ! _PORT_LINUX_H */ | 34 | #endif /* ! _PORT_LINUX_H */ |