aboutsummaryrefslogtreecommitdiffstats
path: root/s2u.c
blob: 03ee852684c7ef6a005630f79ddaf50c7117294b (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*===========================================================================
 * Project         : Mandriva Linux
 * Module          : s2u
 * File            : s2u.c
 * Version         : $Id$
 * Authors         : Stew Benedict and Frederic Lepied
 * Created On      : Mon Aug  2 08:28:19 2004
 * Purpose         : read dbus messages addressed to the user and take
 *                 appropriate actions.
 *===========================================================================*/

/***************************************************************************
 *
 * Copyright (C) 2004,2005,2009 Mandrakesoft
 * Copyright (C) 2005 Mandriva
 *
 * Stew Benedict, <sbenedict@mandriva.com>
 * Frederic Lepied, <flepied@mandriva.com>
 * Eugeni Dodonov <eugeni@mandriva.com>
 *
 * code borrowed/adapted from the hal project -
 *                  http://www.freedesktop.org/Software/hal
 *
 * Licensed under the GNU General Public License version 2.0
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

static const char rcs_id[] = "$Id$";
static const char compile_id[] = "$Compile: " __FILE__ " " __DATE__ " " __TIME__ " $";

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <gdk/gdk.h>
#include <libnotify/notify.h>

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

static DBusConnection *dbus_connection;
static gchar *cookie = NULL;
static NotifyNotification *n;

/** Print out program usage.
 *
 */
static void
usage ()
{
    g_printerr ("\n" "usage : s2u [--daemon=yes|no] [--help] [--debug]\n");
    g_printerr (
         "\n"
         "        --daemon=yes|no    Become a daemon\n"
         "        --debug            Print debug informations\n"
         "        --help             Show this information and exit\n"
         "\n"
         "s2u monitors messages through D-BUS,\n"
         "taking appropraite actions.\n");
}


/** If #TRUE, we will daemonize */
static dbus_bool_t opt_become_daemon = FALSE;

static void
die (const char *message)
{
  g_printerr ("*** %s", message);
  exit (1);
}

/** Message handler for method invocations. All invocations on any object
 *  or interface is routed through this function.
 *
 *  @param  connection          D-BUS connection
 *  @param  message             Message
 *  @param  user_data           User data
 *  @return                     What to do with the message
 */

static DBusHandlerResult
filter_function (DBusConnection * connection,
         DBusMessage * message, void *user_data)
{

/*
    INFO (("obj_path=%s interface=%s method=%s",
    dbus_message_get_path(message),
    dbus_message_get_interface(message),
    dbus_message_get_member(message)));
*/

    /* message, interface, method */
    if (dbus_message_is_signal (message,
                     "com.mandriva.user",
                     "message") &&
        strcmp (dbus_message_get_path (message),
            "/com/mandriva/user") == 0) {
      gchar* args[] =
        {
          "/usr/bin/xauth",
          "add",
          NULL,
          ".",
          NULL,
          NULL
        };

      args[2] = getenv("DISPLAY");
      args[4] = cookie;

      g_print("s2u: detected hostname change; setting cookie to '%s' for %s.\n", cookie, args[2]);

      g_spawn_async("/", args, NULL, 0, NULL, NULL, NULL, NULL);

      return DBUS_HANDLER_RESULT_HANDLED;
    } else if (dbus_message_is_signal (message,
                     "com.mandriva.user",
                     "updatemenu")) {

          g_print("s2u: updatemenu signal received.\n");

      g_spawn_command_line_async("/etc/X11/xinit.d/menu", NULL);

      return DBUS_HANDLER_RESULT_HANDLED;
    } else if (dbus_message_is_signal (message,
                     "com.mandriva.user",
                     "security_notification")) {
        /* msec */
        char *string;
        DBusError error;
        dbus_error_init(&error);
        if (dbus_message_get_args (message,
                    &error,
                    DBUS_TYPE_STRING, &string,
                    DBUS_TYPE_INVALID)) {
            n = notify_notification_new("MSEC", string, GTK_STOCK_INFO, NULL);
            if (!notify_notification_show (n, NULL)) {
                g_printerr("notify_notification_show: failed to show notification\n");
            }
            g_object_unref(G_OBJECT(n));
        }
        else {
            fprintf (stderr, "an error occurred: %s\n", error.message);
        }
        dbus_error_free(&error);
        return DBUS_HANDLER_RESULT_HANDLED;
    } else {
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }
}


static void
service_dbus_init (void)
{
    GError *error = NULL;

    dbus_connection = dbus_g_connection_get_connection (dbus_g_bus_get (DBUS_BUS_SYSTEM, &error));
    if (dbus_connection == NULL) {
        g_printerr ("dbus_bus_get(): %s", error->message);
        exit (1);
    }

         dbus_bus_add_match (dbus_connection,
                             "type='signal',"
                             "interface='com.mandriva.user'",
                NULL);


    dbus_connection_add_filter (dbus_connection, filter_function, NULL,
                    NULL);

}

int
main (int argc, char *argv[])
{
    GMainLoop *loop;
    gchar* args[] = {
        "/usr/bin/xauth",
        "list",
        NULL,
        NULL
    };
    gint in;
    gchar result[255];
    gchar* idx;
    dbus_bool_t opt_debug = FALSE;

    while (1) {
        int c;
        int option_index = 0;
        const char *opt;
        static struct option long_options[] = {
            {"daemon", 1, NULL, 0},
            {"help", 0, NULL, 0},
            {"debug", 0, NULL, 0},
            {NULL, 0, NULL, 0}
        };

        c = getopt_long (argc, argv, "",
                 long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
        case 0:
            opt = long_options[option_index].name;

            if (strcmp (opt, "help") == 0) {
                usage ();
                return 0;
            } else if (strcmp (opt, "daemon") == 0) {
                if (strcmp ("yes", optarg) == 0) {
                    opt_become_daemon = TRUE;
                } else if (strcmp ("no", optarg) == 0) {
                    opt_become_daemon = FALSE;
                } else {
                    usage ();
                    return 1;
                }
            } else if (strcmp (opt, "debug") == 0) {
                opt_debug = TRUE;
            }
            break;

        default:
            usage ();
            return 1;
            break;
        }
    }

    memset(result, 0, sizeof(result));

    args[2] = getenv("DISPLAY");

    g_spawn_async_with_pipes("/", args, NULL, 0, NULL, NULL, NULL,
                 NULL, &in, NULL, NULL);
    if (read(in, result, sizeof(result)) <= 0) {
        die("unable to read X11 cookie");
    } else {
        close(in);

        idx = rindex(result, ' ');
        if (idx == NULL) {
            die ("unable to read X11 cookie");
        }
        cookie = g_strdup(idx+1);
        cookie[strlen(cookie) - 1] = '\0';
        if (opt_debug) {
            g_print("%s: cookie for %s = '%s'\n", argv[0], args[2], cookie);
        }
    }

    if (opt_become_daemon) {
        int child_pid;
        int dev_null_fd;

        if (chdir ("/") < 0) {
            g_printerr ("Could not chdir to /, errno=%d", errno);
            return 1;
        }

        child_pid = fork ();
        switch (child_pid) {
        case -1:
            g_printerr ("Cannot fork(), errno=%d", errno);
            break;

        case 0:
            /* child */

            dev_null_fd = open ("/dev/null", O_RDWR);
            /* ignore if we can't open /dev/null */
            if (dev_null_fd > 0) {
                /* attach /dev/null to stdout, stdin, stderr */
                dup2 (dev_null_fd, 0);
                if (opt_debug == FALSE) {
                        dup2 (dev_null_fd, 1);
                    dup2 (dev_null_fd, 2);
                }
            }

            umask (022);
            break;

        default:
            /* parent */
            exit (0);
            break;
        }

        /* Create session */
        setsid ();
    }

    gdk_init(&argc, &argv);

    /* init libnotify */
    notify_init("s2u");

    loop = g_main_loop_new (NULL, FALSE);

    /* set up the dbus services */
    service_dbus_init ();

    /* run the main loop and serve clients */

    g_main_loop_run (loop);

    return 0;
}

/*
 * Local variables:
 * mode: c
 * End:
 *
 * s2u.c ends here
 */