Back to index

lightdm  1.3.2
dbus-env.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include <errno.h>
00004 #include <unistd.h>
00005 #include <sys/types.h>
00006 #include <sys/wait.h>
00007 #include <glib.h>
00008 
00009 static gchar *
00010 create_bus (const gchar *config_file, GPid *pid)
00011 {
00012     int name_pipe[2];
00013     gchar *command, address[1024];
00014     gchar **argv;
00015     ssize_t n_read;
00016     GError *error = NULL;
00017 
00018     if (pipe (name_pipe) < 0)
00019     {
00020         g_warning ("Error creating pipe: %s", strerror (errno));
00021         exit (EXIT_FAILURE);
00022     }
00023     command = g_strdup_printf ("dbus-daemon --config-file=%s --print-address=%d", config_file, name_pipe[1]);
00024     if (!g_shell_parse_argv (command, NULL, &argv, &error))
00025     {
00026         g_warning ("Error parsing command line: %s", error->message);
00027         exit (EXIT_FAILURE);
00028     }
00029     g_clear_error (&error);
00030     if (!g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, pid, &error))
00031     {
00032         g_warning ("Error launching LightDM: %s", error->message);
00033         exit (EXIT_FAILURE);
00034     }
00035 
00036     n_read = read (name_pipe[0], address, 1023);
00037     if (n_read < 0)
00038     {
00039         g_warning ("Error reading D-Bus address: %s", strerror (errno));
00040         exit (EXIT_FAILURE);
00041     }
00042     address[n_read] = '\0';
00043 
00044     if (n_read > 0 && address[n_read - 1] == '\n')
00045         address[n_read - 1] = '\0';
00046   
00047     return g_strdup (address);
00048 }
00049 
00050 int
00051 main (int argc, char **argv)
00052 {
00053     gchar *conf_file, *system_bus_address, *session_bus_address;
00054     GPid system_bus_pid, session_bus_pid, child_pid;
00055     int status;
00056 
00057     conf_file = g_build_filename (DATADIR, "system.conf", NULL);
00058     system_bus_address = create_bus (conf_file, &system_bus_pid);
00059     g_free (conf_file);
00060     g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", system_bus_address, TRUE);
00061 
00062     conf_file = g_build_filename (DATADIR, "session.conf", NULL);
00063     session_bus_address = create_bus (conf_file, &session_bus_pid);
00064     g_free (conf_file);
00065     g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_address, TRUE);
00066 
00067     child_pid = fork ();
00068     if (child_pid == 0)
00069     {
00070         execvp (argv[1], argv + 1);
00071         _exit (EXIT_FAILURE);
00072     }
00073     waitpid (child_pid, &status, 0);
00074 
00075     kill (session_bus_pid, SIGTERM);
00076     kill (system_bus_pid, SIGTERM);
00077 
00078     if (WIFEXITED (status))
00079         return WEXITSTATUS (status);
00080     else
00081         return EXIT_FAILURE;
00082 }
00083