bug fix for SIGTRAP issue caused by uxlaunch(part of TZIVI-216)
[profile/ivi/uxlaunch.git] / src / uxlaunch.c
1 /*
2  * uxlaunch.c: desktop session starter
3  *
4  * (C) Copyright 2009 Intel Corporation
5  * Authors:
6  *     Auke Kok <auke@linux.intel.com>
7  *     Arjan van de Ven <arjan@linux.intel.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2
12  * of the License.
13  */
14
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <pwd.h>
20
21 #include "uxlaunch.h"
22
23 /*
24  * Launch apps that form the user's X session
25  */
26 static void
27 launch_user_session(void)
28 {
29         char xhost_cmd[80];
30
31         dprintf("entering launch_user_session()");
32
33         setup_user_environment();
34
35         /* this needs XDG_* set in environ */
36         get_session_type();
37
38         start_ssh_agent();
39
40         /* dbus needs the CK env var */
41         start_dbus_session_bus();
42
43         /* gconf needs dbus */
44         start_gconf();
45
46         log_environment();
47
48         maybe_start_screensaver();
49
50         start_desktop_session();
51
52         /* finally, set local username to be allowed at any time,
53          * which is not depenedent on hostname changes */
54         snprintf(xhost_cmd, 80, "/usr/bin/xhost +SI:localuser:%s",
55                  pass->pw_name);
56         if (system(xhost_cmd) != 0)
57                 lprintf("%s failed", xhost_cmd);
58
59         autostart_desktop_files();
60         do_autostart();
61         dprintf("leaving launch_user_session()");
62 }
63
64 int main(int argc, char **argv)
65 {
66         /*
67          * General objective:
68          * Do the things that need root privs first,
69          * then switch to the final user ASAP.
70          *
71          * Once we're at the target user ID, we need
72          * to start X since that's the critical element
73          * from that point on.
74          *
75          * While X is starting, we can do the things
76          * that we need to do as the user UID, but that
77          * don't need X running yet.
78          *
79          * We then wait for X to signal that it's ready
80          * to draw stuff.
81          *
82          * Once X is running, we set up the ConsoleKit session,
83          * check if the screensaver needs to lock the screen
84          * and then start the window manager.
85          * After that we go over the autostart .desktop files
86          * to launch the various autostart processes....
87          * ... and we're done.
88          */
89
90         get_options(argc, argv);
91
92         if (x_session_only) {
93                 dprintf("X session only: skipping major parts of setup");
94                 launch_user_session();
95                 wait_for_session_exit();
96                 stop_gconf();
97                 return 0;
98         }
99
100         set_tty();
101
102         setup_xauth();
103
104 #ifdef ENABLE_CHOOSER
105         if (chooser[0] != '\0')
106                 setup_chooser();
107 #endif
108
109 #ifdef ENABLE_ECRYPTFS
110         setup_efs();
111 #endif
112
113         start_oom_task();
114
115         setup_pam_session();
116
117 #ifdef WITH_CONSOLEKIT
118         setup_consolekit_session();
119 #endif
120
121         switch_to_user();
122
123         /*
124          * BUG: udev is sometimes not done when we go and start Xorg,
125          * which results in the mouse/kbd not working. To work around this
126          * we call udevadm settle and force the system to wait for
127          * device probing to complete, which may be a long time
128          */
129         dprintf("Waiting for udev to settle for 10 seconds max...");
130         if (system("/usr/bin/udevadm settle --timeout 10") != EXIT_SUCCESS)
131                 lprintf("udevadm settle bash returned an error");
132
133         start_X_server();
134
135         /*
136          * These steps don't need X running
137          * so can happen while X is talking to the
138          * hardware
139          */
140         wait_for_X_signal();
141
142         launch_user_session();
143
144         /*
145          * we do this now to make sure dbus etc are not spawning
146          * dbus-activated
147          * tasks at non-oomkillable priorities
148          */
149
150         oom_adj(xpid, -1000);
151         oom_adj(session_pid, -1000);
152         oom_adj(getpid(), -1000);
153
154         /*
155          * The desktop session runs here
156          */
157         wait_for_X_exit();
158
159         stop_gconf();
160
161         set_text_mode();
162
163         // close_consolekit_session();
164         stop_ssh_agent();
165         stop_dbus_session_bus();
166         close_pam_session();
167         stop_oom_task();
168
169         unlink(xauth_cookie_file);
170
171         /* Make sure that we clean up after ourselves */
172         sleep(2);
173
174         lprintf("Terminating uxlaunch and all children");
175         kill(0, SIGKILL);
176
177         return EXIT_SUCCESS;
178 }