server: Cleanly shut down senders on exit.
[paraslash.git] / server.c
index 4f1d5708f76e997ff8fc5ee477dd97d65a6b3785..dd0acdadec94384b5c62ea878b617be52ab02b0d 100644 (file)
--- a/server.c
+++ b/server.c
@@ -106,6 +106,24 @@ static struct signal_task *signal_task;
 /** The process id of the audio file selector process. */
 pid_t afs_pid = 0;
 
+/* The the main server process (parent of afs and the command handlers). */
+static pid_t server_pid;
+
+/**
+ * Tell whether the executing process is a command handler.
+ *
+ * Cleanup on exit must be performed differently for command handlers.
+ *
+ * \return True if the pid of the executing process is neither the server pid
+ * nor the afs pid.
+ */
+bool process_is_command_handler(void)
+{
+       pid_t pid = getpid();
+
+       return pid != afs_pid && pid != server_pid;
+}
+
 /** The task responsible for server command handling. */
 struct server_command_task {
        /** TCP port on which para_server listens for connections. */
@@ -532,6 +550,7 @@ static void server_init(int argc, char **argv, struct server_command_task *sct)
        /* become daemon */
        if (OPT_GIVEN(DAEMON))
                daemon_pipe = daemonize(true /* parent waits for SIGTERM */);
+       server_pid = getpid();
        init_random_seed_or_die();
        daemon_log_welcome("server");
        init_ipc_or_die(); /* init mmd struct and mmd->lock */
@@ -632,12 +651,12 @@ int main(int argc, char *argv[])
        ret = schedule(&sched);
        sched_shutdown(&sched);
        signal_shutdown(signal_task);
-       if (sct->child_fd < 0) { /* parent (server) */
+       if (!process_is_command_handler()) { /* parent (server) */
                if (ret < 0)
                        PARA_EMERG_LOG("%s\n", para_strerror(-ret));
-       } else { /* child (command handler) */
+       } else {
                /*
-                * We hold the lock: it was re-acquired in server_select()
+                * We hold the mmd lock: it was re-acquired in server_select()
                 * after the select call.
                 */
                mutex_unlock(mmd_mutex);
@@ -645,6 +664,7 @@ int main(int argc, char *argv[])
                close_listed_fds();
                ret = handle_connect(sct->child_fd);
        }
+       vss_shutdown();
        lls_free_parse_result(server_lpr, CMD_PTR);
        if (server_lpr != cmdline_lpr)
                lls_free_parse_result(cmdline_lpr, CMD_PTR);