26
26
#include <unistd.h>
30
#include <glib-unix.h>
32
#include <sys/types.h>
33
#include <sys/socket.h>
29
39
#include <telepathy-glib/telepathy-glib.h>
30
40
#include <telepathy-glib/debug-sender.h>
32
42
#include "mcd-service.h"
34
44
static TpDebugSender *debug_sender;
45
static McdService *mcd = NULL;
48
static int quit_pipe[2];
49
#define QUIT_READ_END 0
50
#define QUIT_WRITE_END 1
36
53
#ifdef BUILD_AS_ANDROID_SERVICE
37
54
int telepathy_mission_control_main (int argc, char **argv);
41
on_abort (McdService * mcd)
58
on_abort (McdService * _mcd)
43
60
g_debug ("Exiting now ...");
45
mcd_debug_print_tree (mcd);
62
mcd_debug_print_tree (_mcd);
48
64
g_debug ("MC now exits .. bye bye");
65
mcd_service_stop (_mcd);
70
signal_handler (int sig)
75
if ((quit_pipe[QUIT_WRITE_END] > 0) &&
76
write (quit_pipe[QUIT_WRITE_END], "\0", 1) != 1)
78
/* If we can't write to the socket, dying seems a good
79
* response to SIGINT. We'd use exit(), but that's not
80
* async-signal-safe, so we'll have to resort to _exit().
81
* We use write() because it is async-signal-safe. */
82
static const char message[] =
83
"Unable to write to quit pipe - buffer full?\n"
84
"Will exit instead.\n";
86
write (STDERR_FILENO, message, strlen (message));
94
quit_idle_cb (gpointer user_data)
96
mcd_mission_abort (MCD_MISSION (mcd));
101
quit_event_cb (GIOChannel *source, GIOCondition condition, gpointer data)
103
g_idle_add_full (G_PRIORITY_LOW, quit_idle_cb, NULL, NULL);
108
init_quit_pipe (void)
112
GError *error = NULL;
114
if (!g_unix_open_pipe (quit_pipe, FD_CLOEXEC, &error))
116
g_warning ("Failed to get a pipe: %s", error->message);
117
g_clear_error (&error);
120
for (i = 0 ; i < 2 ; i++)
123
val = fcntl (quit_pipe[i], F_GETFL, 0);
126
g_warning ("Failed to get flags from file descriptor %d: %s",
127
quit_pipe[i], strerror (errno));
130
val = fcntl (quit_pipe[i], F_SETFL, val | O_NONBLOCK);
133
g_warning ("Failed to set flags from file descriptor %d: %s",
134
quit_pipe[i], strerror (errno));
138
channel = g_io_channel_unix_new (quit_pipe[QUIT_READ_END]);
139
g_io_add_watch (channel, G_IO_IN, quit_event_cb, NULL);
53
144
#ifdef BUILD_AS_ANDROID_SERVICE
80
174
/* Listen for suicide notification */
81
175
g_signal_connect_after (mcd, "abort", G_CALLBACK (on_abort), mcd);
180
sigemptyset (&empty_mask);
181
act.sa_handler = signal_handler;
182
act.sa_mask = empty_mask;
184
sigaction (SIGINT, &act, NULL);
84
188
mcd_mission_connect (MCD_MISSION (mcd));
86
190
mcd_service_run (MCD_OBJECT (mcd));
192
g_clear_object (&mcd);
88
193
tp_clear_object (&debug_sender);