aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/gammad.c40
-rw-r--r--src/util.c19
-rw-r--r--src/util.h11
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) \
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 <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);
+}
+
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);
+