summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2014-07-02 16:00:15 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2014-07-02 16:00:15 +0200
commit8d75833aacaa6c5aebd4014c79ace5633d6dabf3 (patch)
tree1471bb4feee6c335496d10c48ab12bba8d657c63
parentb69bba9b089afc69df837ae69d55debf5a14ca1a (diff)
Shut down server process nicely on SIGINT. Reopen logfile on SIGHUP
-rw-r--r--src/miav_daemon.cc8
-rw-r--r--src/miavd.cc23
-rw-r--r--src/socket.cc28
3 files changed, 56 insertions, 3 deletions
diff --git a/src/miav_daemon.cc b/src/miav_daemon.cc
index d86d448..97c26d7 100644
--- a/src/miav_daemon.cc
+++ b/src/miav_daemon.cc
@@ -37,6 +37,9 @@
#include "server.h"
#include "socket.h"
+// Declared in miavd.cc
+extern bool server_is_running;
+
MiavDaemon::MiavDaemon(int port)
{
this->port = port;
@@ -65,6 +68,8 @@ int MiavDaemon::daemon_main()
while(1) {
Socket *csocket = new Socket(socket->slisten());
+ if(!server_is_running) break;
+
if(socket->hasError()) {
ERR(miav, "Server socket has errors, quitting.");
delete csocket;
@@ -101,6 +106,9 @@ int MiavDaemon::daemon_main()
}
delete socket;
+
+ INFO(miav, "MIaV server stopped");
+
return 0;
}
diff --git a/src/miavd.cc b/src/miavd.cc
index e9d7bc4..4ff3425 100644
--- a/src/miavd.cc
+++ b/src/miavd.cc
@@ -32,6 +32,9 @@
#include <config.h>
+// For signal
+#include <signal.h>
+
#include "miav_daemon.h"
#include "miav_config.h"
@@ -62,6 +65,23 @@ static const char usage_str[] =
" -P, --pidfile file Write pid of the running daemon to file.\n"
;
+bool server_is_running = true;
+void ctrl_c(int)
+{
+ DEBUG(server, "Ctrl+c detected");
+ server_is_running = false;
+}
+
+void reload(int)
+{
+ printf("Reopening logfile...\n");
+ if(hugin_reopen_log() != HUG_STATUS_OK) {
+ fprintf(stderr, "Could not reopen logfile!\n");
+ return;
+ }
+ DEBUG(server, "Reopened log\n");
+}
+
int main(int argc, char *argv[])
{
const char *hugin_filter = "+all";
@@ -168,5 +188,8 @@ int main(int argc, char *argv[])
if(user) cuser = user;
if(group) cgroup = group;
+ signal(SIGHUP, reload);
+ signal(SIGINT, ctrl_c);
+
return daemon.run(cuser, cgroup, !foreground, pidfile);
}
diff --git a/src/socket.cc b/src/socket.cc
index 1fffc87..349ec30 100644
--- a/src/socket.cc
+++ b/src/socket.cc
@@ -100,9 +100,31 @@ Socket Socket::slisten()
// accept new connection and get its connection descriptor
int csalen = sizeof(s.socketaddr);
- s.ssocket = accept(ssocket,
- (struct sockaddr*)&s.socketaddr,
- (socklen_t*)&csalen);
+ {
+ fd_set rfds;
+ FD_ZERO(&rfds);
+ FD_SET(ssocket, &rfds);
+ int retval = select(ssocket + 1, &rfds, NULL, NULL, NULL);
+
+ if (retval == -1) {
+ if(errno == EINTR) {
+ connected = true;
+ s.connected = true;
+ return s;
+ }
+
+ ERR(socket, "select() failed! %s", strerror(errno));
+ } else if(retval) {
+ if(FD_ISSET(ssocket, &rfds)) {
+ s.ssocket = accept(ssocket,
+ (struct sockaddr*)&s.socketaddr,
+ (socklen_t*)&csalen);
+ ERR(socket, "accept() failed! %s", strerror(errno));
+ }
+ } else {
+ // We don't do timeout
+ }
+ }
if (s.ssocket < 0) {
s.connected = false;