From 92a217d0be3dd3f19eeb8dfd883d9ce4bfed7366 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 19 May 2014 19:48:55 +0200 Subject: m + add --alarm= options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-server/mds-server.c | 59 ++++++++++++++++++++++++++++----------------- src/mds-server/mds-server.h | 8 ++++++ 2 files changed, 45 insertions(+), 22 deletions(-) (limited to 'src/mds-server') diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c index 0ad7736..55db770 100644 --- a/src/mds-server/mds-server.c +++ b/src/mds-server/mds-server.c @@ -63,7 +63,6 @@ int main(int argc_, char** argv_) int unparsed_args_ptr = 1; char* unparsed_args[ARGC_LIMIT + LIBEXEC_ARGC_EXTRA_LIMIT + 1]; int i; - pthread_t slave_thread; #if (LIBEXEC_ARGC_EXTRA_LIMIT < 3) # error LIBEXEC_ARGC_EXTRA_LIMIT is too small, need at least 3. @@ -104,6 +103,8 @@ int main(int argc_, char** argv_) } else if (strequals(arg, "--re-exec")) /* Re-exec state-marshal. */ reexec = 1; + else if (startswith(arg, "--alarm=")) /* Schedule an alarm signal for forced abort. */ + alarm((unsigned)min(atoi(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */ else /* Not recognised, it is probably for another server. */ unparsed_args[unparsed_args_ptr++] = arg; @@ -183,28 +184,9 @@ int main(int argc_, char** argv_) /* Accepting incoming connections. */ while (running && (terminating == 0)) - { - /* Accept connection. */ - int client_fd = accept(socket_fd, NULL, NULL); - if (client_fd >= 0) - { - /* Increase number of running slaves. */ - with_mutex (slave_mutex, running_slaves++;); - - /* Start slave thread. */ - create_slave(&slave_thread, client_fd); - } - else - /* Handle errors and shutdown. */ - if ((errno == EINTR) && terminating) /* Interrupted for termination. */ - goto terminate; - else if ((errno == ECONNABORTED) || (errno == EINVAL)) /* Closing. */ - running = 0; - else if (errno != EINTR) /* Error. */ - perror(*argv); - } + if (accept_connection(socket_fd) == 1) + break; - terminate: if (reexecing) goto reexec; @@ -232,6 +214,39 @@ int main(int argc_, char** argv_) } +/** + * Accept an incoming and start a slave thread for it + * + * @param socket_fd The file descriptor of the server socket + * @return Zero normally, 1 if terminating + */ +int accept_connection(int socket_fd) +{ + pthread_t slave_thread; + int client_fd; + + /* Accept connection. */ + client_fd = accept(socket_fd, NULL, NULL); + if (client_fd >= 0) + { + /* Increase number of running slaves. */ + with_mutex (slave_mutex, running_slaves++;); + + /* Start slave thread. */ + create_slave(&slave_thread, client_fd); + } + else + /* Handle errors and shutdown. */ + if ((errno == EINTR) && terminating) /* Interrupted for termination. */ + return 1; + else if ((errno == ECONNABORTED) || (errno == EINVAL)) /* Closing. */ + running = 0; + else if (errno != EINTR) /* Error. */ + perror(*argv); + return 0; +} + + /** * Master function for slave threads * diff --git a/src/mds-server/mds-server.h b/src/mds-server/mds-server.h index 8154807..19f3044 100644 --- a/src/mds-server/mds-server.h +++ b/src/mds-server/mds-server.h @@ -24,6 +24,14 @@ #include +/** + * Accept an incoming and start a slave thread for it + * + * @param socket_fd The file descriptor of the server socket + * @return Zero normally, 1 if terminating + */ +int accept_connection(int socket_fd); + /** * Master function for slave threads * -- cgit v1.2.3-70-g09d2