]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/dbus-env.c
c1c0075ccb1b12850a7fb4ff46ab04c4de04fd97
[sojka/lightdm.git] / tests / src / dbus-env.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <glib.h>
8
9 static gchar *
10 create_bus (const gchar *config_file, GPid *pid)
11 {
12     int name_pipe[2];
13     gchar *command, address[1024];
14     gchar **argv;
15     ssize_t n_read;
16     GError *error = NULL;
17
18     if (pipe (name_pipe) < 0)
19     {
20         g_warning ("Error creating pipe: %s", strerror (errno));
21         exit (EXIT_FAILURE);
22     }
23     command = g_strdup_printf ("dbus-daemon --config-file=%s --print-address=%d", config_file, name_pipe[1]);
24     if (!g_shell_parse_argv (command, NULL, &argv, &error))
25     {
26         g_warning ("Error parsing command line: %s", error->message);
27         exit (EXIT_FAILURE);
28     }
29     g_clear_error (&error);
30     if (!g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, pid, &error))
31     {
32         g_warning ("Error launching D-Bus: %s", error->message);
33         exit (EXIT_FAILURE);
34     }
35
36     n_read = read (name_pipe[0], address, 1023);
37     if (n_read < 0)
38     {
39         g_warning ("Error reading D-Bus address: %s", strerror (errno));
40         exit (EXIT_FAILURE);
41     }
42     address[n_read] = '\0';
43
44     if (n_read > 0 && address[n_read - 1] == '\n')
45         address[n_read - 1] = '\0';
46   
47     return g_strdup (address);
48 }
49
50 int
51 main (int argc, char **argv)
52 {
53     gchar *conf_file, *system_bus_address, *session_bus_address;
54     GPid system_bus_pid, session_bus_pid, child_pid;
55     int status;
56
57     conf_file = g_build_filename (DATADIR, "system.conf", NULL);
58     system_bus_address = create_bus (conf_file, &system_bus_pid);
59     g_free (conf_file);
60     g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", system_bus_address, TRUE);
61
62     conf_file = g_build_filename (DATADIR, "session.conf", NULL);
63     session_bus_address = create_bus (conf_file, &session_bus_pid);
64     g_free (conf_file);
65     g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_address, TRUE);
66
67     child_pid = fork ();
68     if (child_pid == 0)
69     {
70         execvp (argv[1], argv + 1);
71         _exit (EXIT_FAILURE);
72     }
73     waitpid (child_pid, &status, 0);
74
75     kill (session_bus_pid, SIGTERM);
76     kill (system_bus_pid, SIGTERM);
77
78     if (WIFEXITED (status))
79         return WEXITSTATUS (status);
80     else
81         return EXIT_FAILURE;
82 }
83