From 807f5f1a8b02176e03b00f4415c0cae1927b44a4 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Fri, 2 Mar 2012 18:21:09 +1100 Subject: s3-nmbd: Add stdin handler for nmbd This will help avoid runaway processes in the test env, particularly when the whole selftest.pl is killed. Andrew Bartlett --- source3/nmbd/nmbd.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/source3/nmbd/nmbd.c b/source3/nmbd/nmbd.c index 35a9a31f8a..7bd4facbb1 100644 --- a/source3/nmbd/nmbd.c +++ b/source3/nmbd/nmbd.c @@ -88,6 +88,24 @@ static void nmbd_sig_term_handler(struct tevent_context *ev, terminate(msg); } +/* + handle stdin becoming readable when we are in --foreground mode + */ +static void nmbd_stdin_handler(struct tevent_context *ev, + struct tevent_fd *fde, + uint16_t flags, + void *private_data) +{ + char c; + if (read(0, &c, 1) != 1) { + struct messaging_context *msg = talloc_get_type_abort( + private_data, struct messaging_context); + + DEBUG(0,("EOF on stdin\n")); + terminate(msg); + } +} + static bool nmbd_setup_sig_term_handler(struct messaging_context *msg) { struct tevent_signal *se; @@ -105,6 +123,19 @@ static bool nmbd_setup_sig_term_handler(struct messaging_context *msg) return true; } +static bool nmbd_setup_stdin_handler(struct messaging_context *msg, bool foreground) +{ + if (foreground) { + /* if we are running in the foreground then look for + EOF on stdin, and exit if it happens. This allows + us to die if the parent process dies + */ + tevent_add_fd(nmbd_event_context(), nmbd_event_context(), 0, TEVENT_FD_READ, nmbd_stdin_handler, msg); + } + + return true; +} + static void msg_reload_nmbd_services(struct messaging_context *msg, void *private_data, uint32_t msg_type, @@ -918,6 +949,8 @@ static bool open_sockets(bool isdaemon, int port) if (!nmbd_setup_sig_term_handler(msg)) exit(1); + if (!nmbd_setup_stdin_handler(msg, !Fork)) + exit(1); if (!nmbd_setup_sig_hup_handler(msg)) exit(1); -- cgit