From 93c22493e847fa074d62fd7d8f7d618a19690db0 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 11 Jul 2016 16:29:27 +0200 Subject: Create and listen to socket + do not use deprecated usleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/gammad.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'src/gammad.c') diff --git a/src/gammad.c b/src/gammad.c index 6fb2076..9534f9f 100644 --- a/src/gammad.c +++ b/src/gammad.c @@ -17,15 +17,17 @@ */ #include +#include #include +#include #include #include #include -#include +#include #include +#include #include #include -#include #include "arg.h" #include "output.h" @@ -48,6 +50,11 @@ struct output* outputs = NULL; */ size_t outputs_n = 0; +/** + * The server socket's file descriptor + */ +int socketfd = -1; + /** @@ -239,7 +246,7 @@ static int is_pidfile_reusable(const char* pidfile, const char* token) { if (++tries > 1) goto bad; - usleep(100000); /* 1 tenth of a second */ /* TODO replace with nanosleep */ + msleep(100); /* 1 tenth of a second */ goto retry; } @@ -588,7 +595,26 @@ int main(int argc, char** argv) goto fail; } - /* TODO socket */ + /* Create socket and start listening */ + { + struct sockaddr_un address; + address.sun_family = AF_UNIX; + if (strlen(socketpath) >= sizeof(address.sun_path)) + { + errno = ENAMETOOLONG; + goto fail; + } + strcpy(address.sun_path, socketpath); + unlink(socketpath); + if ((socketfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) + goto fail; + if (fchmod(socketfd, S_IRWXU) < 0) + goto fail; + if (bind(socketfd, (struct sockaddr*)(&address), sizeof(address)) < 0) + goto fail; + if (listen(socketfd, SOMAXCONN) < 0) + goto fail; + } /* Change directory to / to avoid blocking umounting. */ if (chdir("/") < 0) @@ -689,6 +715,12 @@ int main(int argc, char** argv) /* Done */ rc = 0; done: + if (socketfd >= 0) + { + shutdown(socketfd, SHUT_RDWR); + close(socketfd); + unlink(socketpath); + } #define RESTORE_RAMPS(SUFFIX, MEMBER) \ do \ if (outputs[i].saved_ramps.MEMBER.red != NULL) \ -- cgit v1.2.3-70-g09d2