~ubuntu-branches/ubuntu/precise/telepathy-mission-control-5/precise-proposed

« back to all changes in this revision

Viewing changes to server/mc-server.c

  • Committer: Ken VanDine
  • Date: 2012-04-03 14:58:11 UTC
  • mfrom: (0.13.1)
  • Revision ID: ken.vandine@canonical.com-20120403145811-tdeuigczvnxr97qh
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <unistd.h>
27
27
#include <glib.h>
28
28
 
 
29
#ifdef G_OS_UNIX
 
30
#include <glib-unix.h>
 
31
#include <fcntl.h>
 
32
#include <sys/types.h>
 
33
#include <sys/socket.h>
 
34
#include <errno.h>
 
35
#include <string.h>
 
36
#include <signal.h>
 
37
#endif
 
38
 
29
39
#include <telepathy-glib/telepathy-glib.h>
30
40
#include <telepathy-glib/debug-sender.h>
31
41
 
32
42
#include "mcd-service.h"
33
43
 
34
44
static TpDebugSender *debug_sender;
 
45
static McdService *mcd = NULL;
 
46
 
 
47
#ifdef G_OS_UNIX
 
48
static int quit_pipe[2];
 
49
#define QUIT_READ_END 0
 
50
#define QUIT_WRITE_END 1
 
51
#endif
35
52
 
36
53
#ifdef BUILD_AS_ANDROID_SERVICE
37
54
int telepathy_mission_control_main (int argc, char **argv);
38
55
#endif
39
56
 
40
57
static void
41
 
on_abort (McdService * mcd)
 
58
on_abort (McdService * _mcd)
42
59
{
43
60
    g_debug ("Exiting now ...");
44
61
 
45
 
    mcd_debug_print_tree (mcd);
 
62
    mcd_debug_print_tree (_mcd);
46
63
 
47
 
    g_object_unref (mcd);
48
64
    g_debug ("MC now exits .. bye bye");
49
 
    exit (0);
50
 
}
 
65
    mcd_service_stop (_mcd);
 
66
}
 
67
 
 
68
#ifdef G_OS_UNIX
 
69
static void
 
70
signal_handler (int sig)
 
71
{
 
72
    switch (sig)
 
73
      {
 
74
        case SIGINT:
 
75
            if ((quit_pipe[QUIT_WRITE_END] > 0) &&
 
76
                write (quit_pipe[QUIT_WRITE_END], "\0", 1) != 1)
 
77
              {
 
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";
 
85
 
 
86
                write (STDERR_FILENO, message, strlen (message));
 
87
                _exit (1);
 
88
              }
 
89
            break;
 
90
      }
 
91
}
 
92
 
 
93
static gboolean
 
94
quit_idle_cb (gpointer user_data)
 
95
{
 
96
    mcd_mission_abort (MCD_MISSION (mcd));
 
97
    return FALSE;
 
98
}
 
99
 
 
100
static gboolean
 
101
quit_event_cb (GIOChannel *source, GIOCondition condition, gpointer data)
 
102
{
 
103
    g_idle_add_full (G_PRIORITY_LOW, quit_idle_cb, NULL, NULL);
 
104
    return FALSE;
 
105
}
 
106
 
 
107
static void
 
108
init_quit_pipe (void)
 
109
{
 
110
    int i;
 
111
    GIOChannel *channel;
 
112
    GError *error = NULL;
 
113
 
 
114
    if (!g_unix_open_pipe (quit_pipe, FD_CLOEXEC, &error))
 
115
      {
 
116
        g_warning ("Failed to get a pipe: %s", error->message);
 
117
        g_clear_error (&error);
 
118
        return;
 
119
      }
 
120
    for (i = 0 ; i < 2 ; i++)
 
121
      {
 
122
        int val;
 
123
        val = fcntl (quit_pipe[i], F_GETFL, 0);
 
124
        if (val < 0)
 
125
          {
 
126
            g_warning ("Failed to get flags from file descriptor %d: %s",
 
127
                       quit_pipe[i], strerror (errno));
 
128
            continue;
 
129
          }
 
130
        val = fcntl (quit_pipe[i], F_SETFL, val | O_NONBLOCK);
 
131
        if (val < 0)
 
132
          {
 
133
            g_warning ("Failed to set flags from file descriptor %d: %s",
 
134
                       quit_pipe[i], strerror (errno));
 
135
            continue;
 
136
          }
 
137
      }
 
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);
 
140
}
 
141
#endif
51
142
 
52
143
int
53
144
#ifdef BUILD_AS_ANDROID_SERVICE
56
147
main (int argc, char **argv)
57
148
#endif
58
149
{
59
 
    McdService *mcd;
 
150
#ifdef G_OS_UNIX
 
151
    struct sigaction act;
 
152
    sigset_t empty_mask;
 
153
#endif
60
154
 
61
155
    g_type_init ();
62
156
    g_set_application_name ("Account manager");
80
174
    /* Listen for suicide notification */
81
175
    g_signal_connect_after (mcd, "abort", G_CALLBACK (on_abort), mcd);
82
176
 
 
177
    /* Set up signals */
 
178
#ifdef G_OS_UNIX
 
179
    init_quit_pipe ();
 
180
    sigemptyset (&empty_mask);
 
181
    act.sa_handler = signal_handler;
 
182
    act.sa_mask    = empty_mask;
 
183
    act.sa_flags   = 0;
 
184
    sigaction (SIGINT, &act, NULL);
 
185
#endif
 
186
 
83
187
    /* connect */
84
188
    mcd_mission_connect (MCD_MISSION (mcd));
85
189
 
86
190
    mcd_service_run (MCD_OBJECT (mcd));
87
191
 
 
192
    g_clear_object (&mcd);
88
193
    tp_clear_object (&debug_sender);
89
194
 
90
195
    return 0;