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 ++++++++++++++++++++++++++++++++++++---- src/util.c | 19 +++++++++++++++++++ src/util.h | 11 +++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) (limited to 'src') 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) \ diff --git a/src/util.c b/src/util.c index 715aa6b..81926c6 100644 --- a/src/util.c +++ b/src/util.c @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -133,3 +134,21 @@ int dup2atleast(int fd, int atleast) return new; } + +/** + * Perform a timed suspention of the process. + * The process resumes when the timer expires, + * or when it is interrupted. + * + * @param ms The number of milliseconds to sleep, + * must be less than 1000 + */ +void msleep(int ms) +{ + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = (long)ms * 1000000L; + if (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL) == ENOTSUP) + nanosleep(&ts, NULL); +} + diff --git a/src/util.h b/src/util.h index 25c1a71..5941e50 100644 --- a/src/util.h +++ b/src/util.h @@ -52,3 +52,14 @@ void* nread(int fd, size_t* n); */ int dup2atleast(int fd, int atleast); + +/** + * Perform a timed suspention of the process. + * The process resumes when the timer expires, + * or when it is interrupted. + * + * @param ms The number of milliseconds to sleep, + * must be less than 1000 + */ +void msleep(int ms); + -- cgit v1.2.3-70-g09d2