summaryrefslogtreecommitdiff
path: root/server/mc-server.c
blob: 6e77950de52ec73c81ce557e33d78d1ec84cf1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 8 -*- */
/*
 * This file is part of mission-control
 *
 * Copyright (C) 2007 Nokia Corporation. 
 *
 * Contact: Naba Kumar  <naba.kumar@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "config.h"

#include <stdlib.h>
#include <unistd.h>
#include <glib.h>

#ifdef G_OS_UNIX
#include <glib-unix.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#endif

#include <telepathy-glib/telepathy-glib.h>

#include "mcd-service.h"

static TpDebugSender *debug_sender;
static McdService *mcd = NULL;

#ifdef G_OS_UNIX
static int quit_pipe[2];
#define QUIT_READ_END 0
#define QUIT_WRITE_END 1
#endif

#ifdef BUILD_AS_ANDROID_SERVICE
int telepathy_mission_control_main (int argc, char **argv);
#endif

static void
on_abort (McdService * _mcd)
{
    g_debug ("Exiting now ...");

    mcd_debug_print_tree (_mcd);

    g_debug ("MC now exits .. bye bye");
    mcd_service_stop (_mcd);
}

#ifdef G_OS_UNIX
static void
signal_handler (int sig)
{
    switch (sig)
      {
        case SIGINT:
            if ((quit_pipe[QUIT_WRITE_END] > 0) &&
                write (quit_pipe[QUIT_WRITE_END], "\0", 1) != 1)
              {
                /* If we can't write to the socket, dying seems a good
                 * response to SIGINT. We'd use exit(), but that's not
                 * async-signal-safe, so we'll have to resort to _exit().
                 * We use write() because it is async-signal-safe. */
                static const char message[] =
                  "Unable to write to quit pipe - buffer full?\n"
                  "Will exit instead.\n";

                if (write (STDERR_FILENO, message, strlen (message)) == -1)
                  {
                    /* Ignore, we are returning anyway */
                  }
                _exit (1);
              }
            break;
      }
}

static gboolean
quit_idle_cb (gpointer user_data)
{
    mcd_mission_abort (MCD_MISSION (mcd));
    return FALSE;
}

static gboolean
quit_event_cb (GIOChannel *source, GIOCondition condition, gpointer data)
{
    g_idle_add_full (G_PRIORITY_LOW, quit_idle_cb, NULL, NULL);
    return FALSE;
}

static void
init_quit_pipe (void)
{
    int i;
    GIOChannel *channel;
    GError *error = NULL;

    if (!g_unix_open_pipe (quit_pipe, FD_CLOEXEC, &error))
      {
        g_warning ("Failed to get a pipe: %s", error->message);
        g_clear_error (&error);
        return;
      }
    for (i = 0 ; i < 2 ; i++)
      {
        int val;
        val = fcntl (quit_pipe[i], F_GETFL, 0);
        if (val < 0)
          {
            g_warning ("Failed to get flags from file descriptor %d: %s",
                       quit_pipe[i], strerror (errno));
            continue;
          }
        val = fcntl (quit_pipe[i], F_SETFL, val | O_NONBLOCK);
        if (val < 0)
          {
            g_warning ("Failed to set flags from file descriptor %d: %s",
                       quit_pipe[i], strerror (errno));
            continue;
          }
      }
    channel = g_io_channel_unix_new (quit_pipe[QUIT_READ_END]);
    g_io_add_watch (channel, G_IO_IN, quit_event_cb, NULL);
}
#endif

int
#ifdef BUILD_AS_ANDROID_SERVICE
telepathy_mission_control_main (int argc, char **argv)
#else
main (int argc, char **argv)
#endif
{
#ifdef G_OS_UNIX
    struct sigaction act;
    sigset_t empty_mask;
#endif

    g_type_init ();
    g_set_application_name ("Account manager");

    /* Keep a ref to the default TpDebugSender for the lifetime of the
     * McdMaster, so it will persist for the lifetime of MC, and subsequent
     * calls to tp_debug_sender_dup() will return it again */
    debug_sender = tp_debug_sender_dup ();

    /* Send all debug messages through the Telepathy infrastructure.
     *
     * Unlike CMs, we don't have "subdomains" within MC yet, so we don't want
     * to exclude any domains. */
    g_log_set_default_handler (tp_debug_sender_log_handler, NULL);

    mcd_debug_init ();
    tp_debug_set_flags (g_getenv ("MC_TP_DEBUG"));

    mcd = mcd_service_new ();

    /* Listen for suicide notification */
    g_signal_connect_after (mcd, "abort", G_CALLBACK (on_abort), mcd);

    /* Set up signals */
#ifdef G_OS_UNIX
    init_quit_pipe ();
    sigemptyset (&empty_mask);
    act.sa_handler = signal_handler;
    act.sa_mask    = empty_mask;
    act.sa_flags   = 0;
    sigaction (SIGINT, &act, NULL);
#endif

    /* connect */
    mcd_mission_connect (MCD_MISSION (mcd));

    mcd_service_run (MCD_OBJECT (mcd));

    g_clear_object (&mcd);
    tp_clear_object (&debug_sender);

    return 0;
}