ap

An audio player suited to my tastes
Log | Files | Refs | README | LICENSE

commit 55c15dff701c7bb10811537a6c6beda6b9ab8e7e
parent 7b301d13108fbb92ccbbdd2202b1a01b137b8495
Author: Jacob R. Edwards <n/a>
Date:   Sat, 17 Sep 2022 16:46:10 -0700

Close server when there are no sockets or clients

Since the server can be of no further use without any clients or
names, it is of course convenient to have it shut itself in that
case. In addition, it provides a graceful way to manually close
down the server: by either using the new close command, or by using
a tool like rm(1); which would have the same affect.

Diffstat:
Maps/aps.c | 23+++++++++++++++++++++++
Maps/aps.h | 2++
Maps/command.c | 6++++++
Maps/command.h | 1+
Maps/main.c | 1+
5 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/aps/aps.c b/aps/aps.c @@ -18,6 +18,7 @@ */ #include <sys/socket.h> +#include <sys/stat.h> #include <ctype.h> #include <errno.h> @@ -26,6 +27,7 @@ #include <regex.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "aps.h" #include "arg.h" @@ -159,6 +161,15 @@ aps_handle(struct aps *aps, int fd) } int +aps_isnamed(struct aps *aps) +{ + struct stat sb; + + assert(fstat(aps->con->sock, &sb) == 0, "fstat socket"); + return sb.st_nlink; +} + +int aps_update(struct aps *aps) { int i; @@ -175,6 +186,9 @@ aps_update(struct aps *aps) } } + if (!aps_isnamed(aps) && !aps->nfds) + aps->close = 1; + return 0; } @@ -275,3 +289,12 @@ aps_play(struct aps *aps, struct item *item) queue_set(aps, item); return pplay(aps->player, item->path); } + +int +aps_unname(struct aps *aps) +{ + if (unlink(aps->con->path) == 0) + return 0; + aps_logfmt(aps, WARN, 0, "%s: unable to remove socket", aps->con->path); + return 1; +} diff --git a/aps/aps.h b/aps/aps.h @@ -35,3 +35,5 @@ int aps_command(struct aps *, int); void aps_errordrop(struct aps *, int, char *); int aps_seek(struct aps *, struct item *(*)(struct item *), char *); int aps_play(struct aps *, struct item *); +int aps_isnamed(struct aps *); +int aps_unname(struct aps *); diff --git a/aps/command.c b/aps/command.c @@ -285,3 +285,9 @@ com_commands(struct aps *aps, int fd, int argc, char **argv) return errstr; return NULL; } + +char * +com_close(struct aps *aps, int fd, int argc, char **argv) +{ + return aps_unname(aps) ? errstr : NULL; +} diff --git a/aps/command.h b/aps/command.h @@ -20,3 +20,4 @@ char *com_player(struct aps *, int, int, char **); char *com_truncate(struct aps *, int, int, char **); char *com_terminate(struct aps *, int, int, char **); char *com_commands(struct aps *, int, int, char **); +char *com_close(struct aps *, int, int, char **); diff --git a/aps/main.c b/aps/main.c @@ -41,6 +41,7 @@ static struct aps *aps; static struct command commands[] = { { "add", com_add }, + { "close", com_close }, { "commands", com_commands }, { "list", com_list }, { "log", com_log },