Bugzilla – Attachment 1507 Details for
Bug 1470
adjust Linux out-of-memory killer to stop sshd being killed
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
adjust Linux out-of-memory killer
oom-adjust.diff (text/plain), 4.81 KB, created by
Colin Watson
on 2008-05-26 08:31:50 AEST
(
hide
)
Description:
adjust Linux out-of-memory killer
Filename:
MIME Type:
Creator:
Colin Watson
Created:
2008-05-26 08:31:50 AEST
Size:
4.81 KB
patch
obsolete
>? .scp.c.swp >Index: configure.ac >=================================================================== >RCS file: /cvs/openssh/configure.ac,v >retrieving revision 1.399 >diff -p -u -r1.399 configure.ac >--- configure.ac 19 May 2008 22:57:06 -0000 1.399 >+++ configure.ac 25 May 2008 22:07:34 -0000 >@@ -579,6 +579,7 @@ main() { if (NSVersionOfRunTimeLibrary(" > AC_DEFINE(SSH_TUN_PREPEND_AF, 1, > [Prepend the address family to IP tunnel traffic]) > fi >+ AC_DEFINE(OOM_ADJUST, 1, [Adjust Linux out-of-memory killer]) > ;; > mips-sony-bsd|mips-sony-newsos4) > AC_DEFINE(NEED_SETPGRP, 1, [Need setpgrp to acquire controlling tty]) >Index: sshd.c >=================================================================== >RCS file: /cvs/openssh/sshd.c,v >retrieving revision 1.374 >diff -p -u -r1.374 sshd.c >--- sshd.c 19 May 2008 05:05:08 -0000 1.374 >+++ sshd.c 25 May 2008 22:07:34 -0000 >@@ -250,6 +250,11 @@ Buffer loginmsg; > /* Unprivileged user */ > struct passwd *privsep_pw = NULL; > >+#ifdef OOM_ADJUST >+/* Linux out-of-memory killer adjustment */ >+static char oom_adj_save[8]; >+#endif >+ > /* Prototypes for various functions defined later in this file. */ > void destroy_sensitive_data(void); > void demote_sensitive_data(void); >@@ -901,6 +906,31 @@ recv_rexec_state(int fd, Buffer *conf) > debug3("%s: done", __func__); > } > >+#ifdef OOM_ADJUST >+/* >+ * If requested in the environment, tell the Linux kernel's out-of-memory >+ * killer to avoid sshd. The old state will be restored when forking child >+ * processes. >+ */ >+static void >+oom_adjust_startup(void) >+{ >+ const char *oom_adj = getenv("SSHD_OOM_ADJUST"); >+ >+ if (!oom_adj) >+ return; >+ oom_adj_get(oom_adj_save, sizeof(oom_adj_save)); >+ oom_adj_set(oom_adj); >+} >+ >+static void >+oom_restore(void) >+{ >+ if (oom_adj_save[0]) >+ oom_adj_set(oom_adj_save); >+} >+#endif >+ > /* Accept a connection from inetd */ > static void > server_accept_inetd(int *sock_in, int *sock_out) >@@ -1606,6 +1636,11 @@ main(int ac, char **av) > /* ignore SIGPIPE */ > signal(SIGPIPE, SIG_IGN); > >+#ifdef OOM_ADJUST >+ /* Adjust out-of-memory killer */ >+ oom_adjust_startup(); >+#endif >+ > /* Get a connection, either from inetd or a listening TCP socket */ > if (inetd_flag) { > server_accept_inetd(&sock_in, &sock_out); >@@ -1643,6 +1678,10 @@ main(int ac, char **av) > > /* This is the child processing a new connection. */ > setproctitle("%s", "[accepted]"); >+ >+#ifdef OOM_ADJUST >+ oom_restore(); >+#endif > > /* > * Create a new session and process group since the 4.4BSD >Index: openbsd-compat/port-linux.c >=================================================================== >RCS file: /cvs/openssh/openbsd-compat/port-linux.c,v >retrieving revision 1.5 >diff -p -u -r1.5 port-linux.c >--- openbsd-compat/port-linux.c 26 Mar 2008 20:27:21 -0000 1.5 >+++ openbsd-compat/port-linux.c 25 May 2008 22:07:34 -0000 >@@ -18,7 +18,7 @@ > */ > > /* >- * Linux-specific portability code - just SELinux support at present >+ * Linux-specific portability code > */ > > #include "includes.h" >@@ -27,8 +27,16 @@ > #include <stdarg.h> > #include <string.h> > >-#ifdef WITH_SELINUX >+#ifdef OOM_ADJUST >+#include <sys/types.h> >+#include <sys/stat.h> >+#include <fcntl.h> >+#include <unistd.h> >+#endif >+ > #include "log.h" >+ >+#ifdef WITH_SELINUX > #include "port-linux.h" > > #include <selinux/selinux.h> >@@ -169,3 +177,47 @@ ssh_selinux_setup_pty(char *pwname, cons > debug3("%s: done", __func__); > } > #endif /* WITH_SELINUX */ >+ >+#ifdef OOM_ADJUST >+/* Get the out-of-memory adjustment file for the current process */ >+int >+oom_adj_open(void) >+{ >+ int fd = open("/proc/self/oom_adj", O_RDWR); >+ if (fd < 0) >+ logit("error opening /proc/self/oom_adj: %s", strerror(errno)); >+ return fd; >+} >+ >+/* Get the current OOM adjustment */ >+int >+oom_adj_get(char *buf, size_t maxlen) >+{ >+ ssize_t n; >+ int fd = oom_adj_open(); >+ if (fd < 0) >+ return -1; >+ n = read(fd, buf, maxlen); >+ if (n < 0) >+ logit("error reading /proc/self/oom_adj: %s", strerror(errno)); >+ else >+ buf[n] = '\0'; >+ close(fd); >+ return n < 0 ? -1 : 0; >+} >+ >+/* Set the current OOM adjustment */ >+int >+oom_adj_set(const char *buf) >+{ >+ ssize_t n; >+ int fd = oom_adj_open(); >+ if (fd < 0) >+ return -1; >+ n = write(fd, buf, strlen(buf)); >+ if (n < 0) >+ logit("error writing /proc/self/oom_adj: %s", strerror(errno)); >+ close(fd); >+ return n < 0 ? -1 : 0; >+} >+#endif >Index: openbsd-compat/port-linux.h >=================================================================== >RCS file: /cvs/openssh/openbsd-compat/port-linux.h,v >retrieving revision 1.2 >diff -p -u -r1.2 port-linux.h >--- openbsd-compat/port-linux.h 26 Mar 2008 20:27:21 -0000 1.2 >+++ openbsd-compat/port-linux.h 25 May 2008 22:07:34 -0000 >@@ -25,4 +25,10 @@ void ssh_selinux_setup_pty(char *, const > void ssh_selinux_setup_exec_context(char *); > #endif > >+#ifdef OOM_ADJUST >+int oom_adj_open(void); >+int oom_adj_get(char *buf, size_t maxlen); >+int oom_adj_set(const char *buf); >+#endif >+ > #endif /* ! _PORT_LINUX_H */
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 1470
:
1507
|
1712
|
1740
|
1741
|
1742