+/** @brief Control thread
+ *
+ * This thread is responsible for accepting control commands from Disobedience
+ * (or other controllers) over an AF_UNIX stream socket with a path specified
+ * by the @c --socket option. The protocol uses simple string commands and
+ * replies:
+ *
+ * - @c stop will shut the player down
+ * - @c query will send back the reply @c running
+ * - anything else is ignored
+ *
+ * Commands and response strings terminated by shutting down the connection or
+ * by a newline. No attempt is made to multiplex multiple clients so it is
+ * important that the command be sent as soon as the connection is made - it is
+ * assumed that both parties to the protocol are entirely cooperating with one
+ * another.
+ */
+static void *control_thread(void attribute((unused)) *arg) {
+ struct sockaddr_un sa;
+ int sfd, cfd;
+ char *line;
+ socklen_t salen;
+ FILE *fp;
+
+ assert(control_socket);
+ unlink(control_socket);
+ memset(&sa, 0, sizeof sa);
+ sa.sun_family = AF_UNIX;
+ strcpy(sa.sun_path, control_socket);
+ sfd = xsocket(PF_UNIX, SOCK_STREAM, 0);
+ if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0)
+ fatal(errno, "error binding to %s", control_socket);
+ if(listen(sfd, 128) < 0)
+ fatal(errno, "error calling listen on %s", control_socket);
+ info("listening on %s", control_socket);
+ for(;;) {
+ salen = sizeof sa;
+ cfd = accept(sfd, (struct sockaddr *)&sa, &salen);
+ if(cfd < 0) {
+ switch(errno) {
+ case EINTR:
+ case EAGAIN:
+ break;
+ default:
+ fatal(errno, "error calling accept on %s", control_socket);
+ }
+ }
+ if(!(fp = fdopen(cfd, "r+"))) {
+ error(errno, "error calling fdopen for %s connection", control_socket);
+ close(cfd);
+ continue;
+ }
+ if(!inputline(control_socket, fp, &line, '\n')) {
+ if(!strcmp(line, "stop")) {
+ info("stopped via %s", control_socket);
+ exit(0); /* terminate immediately */
+ }
+ if(!strcmp(line, "query"))
+ fprintf(fp, "running");
+ xfree(line);
+ }
+ if(fclose(fp) < 0)
+ error(errno, "error closing %s connection", control_socket);
+ }
+}
+