XMMS2

src/xmms/compat/signal_unix.c

Go to the documentation of this file.
00001 /*  XMMS2 - X Music Multiplexer System
00002  *  Copyright (C) 2003-2009 XMMS2 Team
00003  *
00004  *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  */
00016 
00017 
00018 
00019 
00020 /** @file
00021  * Takes care of unix-signals.
00022  */
00023 
00024 
00025 #include "xmmspriv/xmms_signal.h"
00026 #include "xmms/xmms_log.h"
00027 #include "xmms/xmms_object.h"
00028 
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <signal.h>
00032 #include <string.h>
00033 #include <glib.h>
00034 #include <unistd.h>
00035 
00036 static sigset_t osignals;
00037 
00038 static gpointer
00039 sigwaiter (gpointer data)
00040 {
00041     xmms_object_t *obj = (xmms_object_t *) data;
00042     xmms_object_cmd_arg_t arg;
00043     sigset_t signals;
00044     int caught;
00045 
00046     sigemptyset(&signals);
00047     sigaddset (&signals, SIGINT);
00048     sigaddset (&signals, SIGTERM);
00049 
00050     while (1337) {
00051         sigwait (&signals, &caught);
00052 
00053         switch (caught){
00054             case SIGINT:
00055             case SIGTERM:
00056                 pthread_sigmask (SIG_UNBLOCK, &signals, NULL);
00057 
00058                 xmms_log_info ("Bye!");
00059 
00060                 xmms_object_cmd_arg_init (&arg);
00061                 memset (&arg, 0, sizeof (arg));
00062                 xmms_error_reset (&arg.error);
00063                 xmms_object_cmd_call (obj, XMMS_IPC_CMD_QUIT, &arg);
00064                 break;
00065         }
00066     }
00067 }
00068 
00069 void
00070 xmms_signal_block (void)
00071 {
00072     sigset_t signals;
00073 
00074     sigemptyset(&signals);
00075 
00076     sigaddset (&signals, SIGHUP);
00077     sigaddset (&signals, SIGTERM);
00078     sigaddset (&signals, SIGINT);
00079     sigaddset (&signals, SIGPIPE);
00080 
00081     pthread_sigmask (SIG_BLOCK, &signals, &osignals);
00082 }
00083 
00084 void
00085 xmms_signal_restore (void)
00086 {
00087     pthread_sigmask (SIG_SETMASK, &osignals, NULL);
00088 }
00089 
00090 void
00091 xmms_signal_init (xmms_object_t *obj)
00092 {
00093     g_thread_create (sigwaiter, obj, FALSE, NULL);
00094 }