Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 5 additions & 58 deletions server/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,29 @@
*/

#include <glib.h>
#include <sys/signalfd.h>
#include <glib-unix.h>

#include <libdleyna/core/main-loop.h>
#include <libdleyna/server/control-point-server.h>

#define DLS_SERVER_SERVICE_NAME "dleyna-server-service"

static guint g_sig_id;

static gboolean prv_quit_handler(GIOChannel *source, GIOCondition condition,
gpointer user_data)
static gboolean prv_quit_handler(gpointer user_data)
{
dleyna_main_loop_quit();
g_sig_id = 0;

return FALSE;
}

static gboolean prv_init_signal_handler(sigset_t mask)
{
gboolean retval = FALSE;
int fd = -1;
GIOChannel *channel = NULL;

fd = signalfd(-1, &mask, SFD_NONBLOCK);
if (fd == -1)
goto on_error;

channel = g_io_channel_unix_new(fd);
g_io_channel_set_close_on_unref(channel, TRUE);

if (g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL) !=
G_IO_STATUS_NORMAL)
goto on_error;

if (g_io_channel_set_encoding(channel, NULL, NULL) !=
G_IO_STATUS_NORMAL)
goto on_error;

g_sig_id = g_io_add_watch(channel, G_IO_IN | G_IO_PRI,
prv_quit_handler,
NULL);

retval = TRUE;

on_error:

if (channel)
g_io_channel_unref(channel);

return retval;
}

int main(int argc, char *argv[])
{
sigset_t mask;
g_unix_signal_add (SIGTERM, prv_quit_handler, NULL);
g_unix_signal_add (SIGINT, prv_quit_handler, NULL);
int retval = 1;

sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT);

if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
goto out;

if (!prv_init_signal_handler(mask))
goto out;

retval = dleyna_main_loop_start(DLS_SERVER_SERVICE_NAME,
return dleyna_main_loop_start(DLS_SERVER_SERVICE_NAME,
dleyna_control_point_get_server(),
NULL);

out:

if (g_sig_id)
(void) g_source_remove(g_sig_id);

return retval;
}