diff options
author | Mattias Andrée <maandree@kth.se> | 2016-07-11 16:29:27 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2016-07-11 16:29:27 +0200 |
commit | 93c22493e847fa074d62fd7d8f7d618a19690db0 (patch) | |
tree | b661ca450e19d3e38e04ef3802e8952ff997197b /src | |
parent | Place in background unless -f (diff) | |
download | coopgammad-93c22493e847fa074d62fd7d8f7d618a19690db0.tar.gz coopgammad-93c22493e847fa074d62fd7d8f7d618a19690db0.tar.bz2 coopgammad-93c22493e847fa074d62fd7d8f7d618a19690db0.tar.xz |
Create and listen to socket + do not use deprecated usleep
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src')
-rw-r--r-- | src/gammad.c | 40 | ||||
-rw-r--r-- | src/util.c | 19 | ||||
-rw-r--r-- | src/util.h | 11 |
3 files changed, 66 insertions, 4 deletions
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 <libgamma.h> +#include <sys/socket.h> #include <sys/stat.h> +#include <sys/un.h> #include <errno.h> #include <fcntl.h> #include <pwd.h> -#include <stdlib.h> +#include <signal.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <unistd.h> -#include <signal.h> #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) \ @@ -21,6 +21,7 @@ #include <errno.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include <unistd.h> @@ -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); +} + @@ -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); + |