]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Use a SessionConfig class for the session .desktop file
[sojka/lightdm.git] / src / seat.c
1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  * 
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/wait.h>
15
16 #include "seat.h"
17 #include "configuration.h"
18 #include "guest-account.h"
19 #include "greeter.h"
20 #include "session-config.h"
21
22 enum {
23     SESSION_ADDED,
24     RUNNING_USER_SESSION,
25     SESSION_REMOVED,
26     STOPPED,
27     LAST_SIGNAL
28 };
29 static guint signals[LAST_SIGNAL] = { 0 };
30
31 struct SeatPrivate
32 {
33     /* Configuration for this seat */
34     GHashTable *properties;
35
36     /* TRUE if able to switch users */
37     gboolean can_switch;
38
39     /* TRUE if display server can be shared for sessions */
40     gboolean share_display_server;
41
42     /* The display servers on this seat */
43     GList *display_servers;
44
45     /* The sessions on this seat */
46     GList *sessions;
47
48     /* The session to set active when it starts */
49     Session *session_to_activate;
50
51     /* TRUE if stopping this seat (waiting for displays to stop) */
52     gboolean stopping;
53
54     /* TRUE if stopped */
55     gboolean stopped;
56 };
57
58 /* PAM services to use */
59 #define GREETER_SERVICE   "lightdm-greeter"
60 #define USER_SERVICE      "lightdm"
61 #define AUTOLOGIN_SERVICE "lightdm-autologin"
62
63 G_DEFINE_TYPE (Seat, seat, G_TYPE_OBJECT);
64
65 typedef struct
66 {
67     const gchar *name;
68     GType type;
69 } SeatModule;
70 static GHashTable *seat_modules = NULL;
71
72 // FIXME: Make a get_display_server() that re-uses display servers if supported
73 static DisplayServer *create_display_server (Seat *seat);
74 static Greeter *create_greeter_session (Seat *seat);
75 static void start_session (Seat *seat, Session *session);
76
77 void
78 seat_register_module (const gchar *name, GType type)
79 {
80     SeatModule *module;
81
82     if (!seat_modules)
83         seat_modules = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
84
85     g_debug ("Registered seat module %s", name);
86
87     module = g_malloc0 (sizeof (SeatModule));
88     module->name = g_strdup (name);
89     module->type = type;
90     g_hash_table_insert (seat_modules, g_strdup (name), module);
91 }
92
93 Seat *
94 seat_new (const gchar *module_name)
95 {
96     Seat *seat;
97     SeatModule *m = NULL;
98   
99     g_return_val_if_fail (module_name != NULL, NULL);
100
101     if (seat_modules)
102         m = g_hash_table_lookup (seat_modules, module_name);
103     if (!m)
104         return NULL;
105
106     seat = g_object_new (m->type, NULL);
107
108     return seat;
109 }
110
111 void
112 seat_set_property (Seat *seat, const gchar *name, const gchar *value)
113 {
114     g_return_if_fail (seat != NULL);
115     g_hash_table_insert (seat->priv->properties, g_strdup (name), g_strdup (value));
116 }
117
118 const gchar *
119 seat_get_string_property (Seat *seat, const gchar *name)
120 {
121     g_return_val_if_fail (seat != NULL, NULL);
122     return g_hash_table_lookup (seat->priv->properties, name);
123 }
124
125 gboolean
126 seat_get_boolean_property (Seat *seat, const gchar *name)
127 {
128     return g_strcmp0 (seat_get_string_property (seat, name), "true") == 0;
129 }
130
131 gint
132 seat_get_integer_property (Seat *seat, const gchar *name)
133 {
134     const gchar *value;
135
136     value = seat_get_string_property (seat, name);
137     return value ? atoi (value) : 0;
138 }
139
140 void
141 seat_set_can_switch (Seat *seat, gboolean can_switch)
142 {
143     g_return_if_fail (seat != NULL);
144
145     seat->priv->can_switch = can_switch;
146 }
147
148 void
149 seat_set_share_display_server (Seat *seat, gboolean share_display_server)
150 {
151     g_return_if_fail (seat != NULL);
152
153     seat->priv->share_display_server = share_display_server;
154 }
155
156 gboolean
157 seat_start (Seat *seat)
158 {
159     g_return_val_if_fail (seat != NULL, FALSE);
160   
161     SEAT_GET_CLASS (seat)->setup (seat);
162     return SEAT_GET_CLASS (seat)->start (seat);
163 }
164
165 GList *
166 seat_get_sessions (Seat *seat)
167 {
168     g_return_val_if_fail (seat != NULL, NULL);
169     return seat->priv->sessions;
170 }
171
172 void
173 seat_set_active_session (Seat *seat, Session *session)
174 {
175     GList *link;
176
177     g_return_if_fail (seat != NULL);
178
179     SEAT_GET_CLASS (seat)->set_active_session (seat, session);
180
181     /* Stop any greeters */
182     for (link = seat->priv->sessions; link; link = link->next)
183     {
184         Session *s = link->data;
185
186         if (s == session || session_get_is_stopping (s))
187             continue;
188
189         if (IS_GREETER (s))
190         {
191             g_debug ("Stopping greeter");
192             session_stop (s);
193         }
194     }
195 }
196
197 Session *
198 seat_get_active_session (Seat *seat)
199 {
200     g_return_val_if_fail (seat != NULL, NULL);
201     return SEAT_GET_CLASS (seat)->get_active_session (seat);
202 }
203
204 gboolean
205 seat_get_can_switch (Seat *seat)
206 {
207     g_return_val_if_fail (seat != NULL, FALSE);
208     return seat->priv->can_switch;
209 }
210
211 gboolean
212 seat_get_allow_guest (Seat *seat)
213 {
214     g_return_val_if_fail (seat != NULL, FALSE);
215     return seat_get_boolean_property (seat, "allow-guest") && guest_account_is_installed ();
216 }
217
218 static gboolean
219 run_script (Seat *seat, DisplayServer *display_server, const gchar *script_name, User *user)
220 {
221     Process *script;
222     gboolean result = FALSE;
223   
224     script = process_new ();
225
226     process_set_command (script, script_name);
227
228     /* Set POSIX variables */
229     process_set_clear_environment (script, TRUE);
230     process_set_env (script, "SHELL", "/bin/sh");
231
232     /* Variables required for regression tests */
233     if (g_getenv ("LIGHTDM_TEST_ROOT"))
234     {
235         process_set_env (script, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
236         process_set_env (script, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
237         process_set_env (script, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
238         process_set_env (script, "PATH", g_getenv ("PATH"));
239     }
240     else
241         process_set_env (script, "PATH", "/usr/local/bin:/usr/bin:/bin");
242
243     if (user)
244     {
245         process_set_env (script, "USER", user_get_name (user));
246         process_set_env (script, "LOGNAME", user_get_name (user));
247         process_set_env (script, "HOME", user_get_home_directory (user));
248     }
249     else
250         process_set_env (script, "HOME", "/");
251
252     SEAT_GET_CLASS (seat)->run_script (seat, display_server, script);
253
254     if (process_start (script, TRUE))
255     {
256         int exit_status;
257
258         exit_status = process_get_exit_status (script);
259         if (WIFEXITED (exit_status))
260         {
261             g_debug ("Exit status of %s: %d", script_name, WEXITSTATUS (exit_status));
262             result = WEXITSTATUS (exit_status) == EXIT_SUCCESS;
263         }
264     }
265
266     g_object_unref (script);
267
268     return result;
269 }
270
271 static void
272 seat_real_run_script (Seat *seat, DisplayServer *display_server, Process *process)
273 {  
274 }
275
276 static void
277 emit_upstart_signal (const gchar *signal)
278 {
279     g_return_if_fail (signal != NULL);
280     g_return_if_fail (signal[0] != 0);
281
282     if (getuid () != 0)
283         return;
284
285     gchar *cmd = g_strdup_printf ("initctl -q emit %s DISPLAY_MANAGER=lightdm", signal);
286     g_spawn_command_line_async (cmd, NULL); /* OK if it fails, probably not installed */
287     g_free (cmd);
288 }
289
290 static void
291 check_stopped (Seat *seat)
292 {
293     if (seat->priv->stopping &&
294         !seat->priv->stopped &&
295         g_list_length (seat->priv->display_servers) == 0 &&
296         g_list_length (seat->priv->sessions) == 0)
297     {
298         seat->priv->stopped = TRUE;
299         g_debug ("Seat stopped");
300         g_signal_emit (seat, signals[STOPPED], 0);
301     }
302 }
303
304 static void
305 display_server_stopped_cb (DisplayServer *display_server, Seat *seat)
306 {
307     GList *list, *link;
308     Session *active_session;
309
310     g_debug ("Display server stopped");
311
312     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
313     seat->priv->display_servers = g_list_remove (seat->priv->display_servers, display_server);
314
315     if (seat->priv->stopping)
316     {
317         check_stopped (seat);
318         g_object_unref (display_server);
319         return;
320     }
321
322     /* Stop all sessions on this display server */
323     list = g_list_copy (seat->priv->sessions);
324     for (link = list; link; link = link->next)
325         g_object_ref (link->data);
326     for (link = list; link; link = link->next)
327     {
328         Session *session = link->data;
329
330         if (session_get_display_server (session) != display_server || session_get_is_stopping (session))
331             continue;
332
333         /* Stop seat if this is the only display server and it failed to start a greeter */
334         if (IS_GREETER (session) &&
335             !session_get_is_started (session) &&
336             g_list_length (seat->priv->display_servers) == 0)
337         {
338             g_debug ("Stopping seat, greeter display server failed to start");
339             seat_stop (seat);
340         }
341
342         g_debug ("Stopping session");
343         session_stop (session);
344     }
345     g_list_free_full (list, g_object_unref);
346
347     if (!seat->priv->stopping && display_server_get_start_local_sessions (display_server))
348     {
349         /* If we were the active session, switch to a greeter */
350         active_session = seat_get_active_session (seat);
351         if (!active_session || session_get_display_server (active_session) == display_server)
352         {
353             g_debug ("Active display server stopped, starting greeter");
354             seat_switch_to_greeter (seat);
355         }
356     }
357
358     g_object_unref (display_server);
359 }
360
361 static void
362 switch_to_greeter_from_failed_session (Seat *seat, Session *session)
363 {
364     Greeter *greeter_session;
365
366     greeter_session = create_greeter_session (seat);
367     if (session_get_is_guest (session))
368         greeter_set_hint (greeter_session, "select-guest", "true");
369     else
370         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
371     if (seat->priv->session_to_activate)
372         g_object_unref (seat->priv->session_to_activate);
373     seat->priv->session_to_activate = g_object_ref (greeter_session);
374
375     if (seat->priv->share_display_server)
376         session_set_display_server (SESSION (greeter_session), session_get_display_server (session));
377     else
378     {
379         DisplayServer *display_server;
380
381         display_server = create_display_server (seat);
382         if (!display_server_start (display_server))
383         {
384             g_debug ("Failed to start display server for greeter");
385             seat_stop (seat);
386         }
387
388         session_set_display_server (session, display_server);
389     }
390
391     start_session (seat, SESSION (greeter_session));
392
393     /* Stop failed session */
394     session_stop (session);
395 }
396
397 static void
398 start_session (Seat *seat, Session *session)
399 {
400     if (session_start (session))
401         return;
402
403     if (IS_GREETER (session))
404     {
405         g_debug ("Failed to start greeter");
406         display_server_stop (session_get_display_server (session));
407         return;
408     }
409
410     g_debug ("Failed to start session, starting greeter");
411     switch_to_greeter_from_failed_session (seat, session);
412 }
413
414 static void
415 run_session (Seat *seat, Session *session)
416 {
417     const gchar *script;
418
419     if (IS_GREETER (session))
420         script = seat_get_string_property (seat, "greeter-setup-script");
421     else
422         script = seat_get_string_property (seat, "session-setup-script");
423     if (script && !run_script (seat, session_get_display_server (session), script, NULL))
424     {
425         g_debug ("Switching to greeter due to failed setup script");
426         switch_to_greeter_from_failed_session (seat, session);
427         return;
428     }
429
430     if (!IS_GREETER (session))
431         g_signal_emit (seat, signals[RUNNING_USER_SESSION], 0, session);
432
433     session_run (session);
434
435     // FIXME: Wait until the session is ready
436
437     if (session == seat->priv->session_to_activate)
438     {
439         seat_set_active_session (seat, session);
440         g_object_unref (seat->priv->session_to_activate);
441         seat->priv->session_to_activate = NULL;
442     }
443 }
444
445 static void
446 session_authentication_complete_cb (Session *session, Seat *seat)
447 {
448     if (session_get_is_authenticated (session))
449     {
450         g_debug ("Session authenticated, running command");
451         run_session (seat, session);
452     }
453     else if (!IS_GREETER (session))
454     {
455         g_debug ("Switching to greeter due to failed authentication");
456         switch_to_greeter_from_failed_session (seat, session);
457     }
458     else
459     {
460         g_debug ("Stopping session that failed authentication");
461         session_stop (session);
462     }
463 }
464
465 static void
466 session_stopped_cb (Session *session, Seat *seat)
467 {
468     DisplayServer *display_server;
469
470     g_debug ("Session stopped");
471
472     display_server = session_get_display_server (session);
473
474     /* Cleanup */
475     if (!IS_GREETER (session))
476     {
477         const gchar *script;
478         script = seat_get_string_property (seat, "session-cleanup-script");
479         if (script)
480             run_script (seat, display_server, script, session_get_user (session));
481     }
482
483     g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
484     seat->priv->sessions = g_list_remove (seat->priv->sessions, session);
485
486     /* We were waiting for this session, but it didn't start :( */
487     // FIXME: Start a greeter on this?
488     if (session == seat->priv->session_to_activate)
489     {
490         g_object_unref (seat->priv->session_to_activate);
491         seat->priv->session_to_activate = NULL;
492     }
493
494     if (seat->priv->stopping)
495     {
496         check_stopped (seat);
497         g_object_unref (session);
498         return;
499     }
500
501     /* If this is the greeter session then re-use this display server */
502     if (IS_GREETER (session) && seat->priv->share_display_server &&
503         greeter_get_start_session (GREETER (session)))
504     {
505         GList *link;
506
507         for (link = seat->priv->sessions; link; link = link->next)
508         {
509             Session *s = link->data;
510
511             /* Skip this session and sessions on other display servers */
512             if (s == session || session_get_display_server (s) != display_server || session_get_is_stopping (s))
513                 continue;
514
515             if (session_get_is_authenticated (s))
516             {
517                 g_debug ("Greeter stopped, running session");
518                 run_session (seat, s);
519             }
520             else
521             {
522                 g_debug ("Greeter stopped, starting session authentication");
523                 start_session (seat, s);
524             }
525             break;
526         }
527     }
528     /* If this is the greeter and nothing else is running then stop the seat */
529     else if (IS_GREETER (session) &&
530         !greeter_get_start_session (GREETER (session)) &&
531         g_list_length (seat->priv->display_servers) == 1 &&
532         g_list_nth_data (seat->priv->display_servers, 0) == display_server)
533     {
534         g_debug ("Stopping seat, failed to start a greeter");
535         seat_stop (seat);
536     }
537     /* If we were the active session, switch to a greeter */
538     else if (!IS_GREETER (session) && session == seat_get_active_session (seat))
539     {
540         g_debug ("Active session stopped, starting greeter");
541         seat_switch_to_greeter (seat);
542     }
543
544     /* Stop the display server if no-longer required */
545     if (display_server && !display_server_get_is_stopping (display_server))
546     {
547         GList *link;
548         int n_sessions = 0;
549
550         for (link = seat->priv->sessions; link; link = link->next)
551         {
552             Session *s = link->data;
553             if (s == session)
554                 continue;
555             if (session_get_display_server (s) == display_server)
556                 n_sessions++;
557         }
558         if (n_sessions == 0)
559         {
560             g_debug ("Stopping display server, no sessions require it");
561             display_server_stop (display_server);
562         }
563     }
564
565     g_signal_emit (seat, signals[SESSION_REMOVED], 0, session);
566     g_object_unref (session);
567 }
568
569 static void
570 set_session_env (Session *session)
571 {
572     /* Connect using the session bus */
573     if (getuid () != 0)
574     {
575         if (g_getenv ("DBUS_SESSION_BUS_ADDRESS"))
576             session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
577         session_set_env (session, "LDM_BUS", "SESSION");
578         if (g_getenv ("LD_PRELOAD"))
579             session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
580         if (g_getenv ("LD_LIBRARY_PATH"))
581             session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
582         if (g_getenv ("PATH"))
583             session_set_env (session, "PATH", g_getenv ("PATH"));
584     }
585
586     /* Variables required for regression tests */
587     if (g_getenv ("LIGHTDM_TEST_ROOT"))
588     {
589         session_set_env (session, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
590         session_set_env (session, "DBUS_SYSTEM_BUS_ADDRESS", g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
591         session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
592         session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
593         session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
594         session_set_env (session, "GI_TYPELIB_PATH", g_getenv ("GI_TYPELIB_PATH"));
595     }
596 }
597
598 static Session *
599 create_session (Seat *seat, gboolean autostart)
600 {
601     Session *session;
602
603     session = SEAT_GET_CLASS (seat)->create_session (seat);
604     seat->priv->sessions = g_list_append (seat->priv->sessions, session);
605     if (autostart)
606         g_signal_connect (session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
607     g_signal_connect (session, "stopped", G_CALLBACK (session_stopped_cb), seat);
608
609     set_session_env (session);
610
611     g_signal_emit (seat, signals[SESSION_ADDED], 0, session);
612
613     return session;
614 }
615
616 static gchar **
617 get_session_argv (SessionConfig *session_config, const gchar *session_wrapper)
618 {
619     gboolean result;
620     int argc;
621     gchar **argv, *path;
622     GError *error = NULL;
623
624     /* If configured, run sessions through a wrapper */
625     if (session_wrapper)
626     {
627         argv = g_malloc (sizeof (gchar *) * 3);
628         path = g_find_program_in_path (session_wrapper);
629         argv[0] = path ? path : g_strdup (session_wrapper);
630         argv[1] = g_strdup (session_config_get_command (session_config));
631         argv[2] = NULL;
632         return argv;
633     }
634
635     /* Split command into an array listing and make command absolute */
636     result = g_shell_parse_argv (session_config_get_command (session_config), &argc, &argv, &error);
637     if (error)
638         g_debug ("Invalid session command '%s': %s", session_config_get_command (session_config), error->message);
639     g_clear_error (&error);
640     if (!result)
641         return NULL;
642     path = g_find_program_in_path (argv[0]);
643     if (path)
644     {
645         g_free (argv[0]);
646         argv[0] = path;
647     }
648   
649     return argv;
650 }
651
652 static SessionConfig *
653 find_session_config (const gchar *sessions_dir, const gchar *session_name)
654 {
655     gchar **dirs;
656     SessionConfig *session_config = NULL;
657     int i;
658     GError *error = NULL;
659
660     g_return_val_if_fail (sessions_dir != NULL, NULL);
661     g_return_val_if_fail (session_name != NULL, NULL);
662
663     dirs = g_strsplit (sessions_dir, ":", -1);
664     for (i = 0; dirs[i]; i++)
665     {
666         gchar *filename, *path;
667
668         filename = g_strdup_printf ("%s.desktop", session_name);
669         path = g_build_filename (dirs[i], filename, NULL);
670         g_free (filename);
671         session_config = session_config_new_from_file (path, &error);
672         g_free (path);
673         if (session_config)
674             break;
675
676         if (dirs[i+1] == NULL)
677             g_debug ("Failed to find session configuration %s", session_name);
678         g_clear_error (&error);
679     }
680     g_strfreev (dirs);
681
682     return session_config;
683 }
684
685 static Session *
686 create_user_session (Seat *seat, const gchar *username)
687 {
688     User *user;
689     gchar *sessions_dir;
690     const gchar *session_name, *language;
691     SessionConfig *session_config;
692     Session *session = NULL;
693
694     /* Load user preferences */
695     user = accounts_get_user_by_name (username);
696     if (!user)
697     {
698         g_debug ("Can't login unknown user '%s'", username);
699         return NULL;
700     }
701     session_name = user_get_xsession (user);
702     language = user_get_language (user);
703
704     if (!session_name)
705         session_name = seat_get_string_property (seat, "user-session");
706     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
707     session_config = find_session_config (sessions_dir, session_name);
708     g_free (sessions_dir);
709     if (session_config)
710     {
711         gchar **argv;
712
713         session = create_session (seat, TRUE);
714         session_set_env (session, "DESKTOP_SESSION", session_name);
715         session_set_env (session, "GDMSESSION", session_name);
716         if (language && language[0] != '\0')
717         {
718             session_set_env (session, "LANG", language);
719             session_set_env (session, "GDM_LANG", language);
720         }
721         session_set_pam_service (session, AUTOLOGIN_SERVICE);
722         session_set_username (session, username);
723         session_set_do_authenticate (session, TRUE);
724         argv = get_session_argv (session_config, seat_get_string_property (seat, "session-wrapper"));
725         g_object_unref (session_config);
726         session_set_argv (session, argv);
727         g_strfreev (argv);
728     }
729     else
730         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
731
732     g_object_unref (user);
733
734     return session;
735 }
736
737 static Session *
738 create_guest_session (Seat *seat)
739 {
740     gchar *sessions_dir, **argv;
741     SessionConfig *session_config;
742     Session *session;
743
744     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
745     session_config = find_session_config (sessions_dir, seat_get_string_property (seat, "user-session"));
746     g_free (sessions_dir);
747     if (!session_config)
748     {
749         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
750         return NULL;
751     }
752
753     session = create_session (seat, TRUE);
754     session_set_do_authenticate (session, TRUE);
755     session_set_is_guest (session, TRUE);
756     argv = get_session_argv (session_config, seat_get_string_property (seat, "session-wrapper"));
757     g_object_unref (session_config);
758     session_set_argv (session, argv);
759     g_strfreev (argv);
760   
761     return session;
762 }
763
764 static Session *
765 greeter_create_session_cb (Greeter *greeter, Seat *seat)
766 {
767     Session *session;
768
769     session = create_session (seat, FALSE);
770     session_set_display_server (session, session_get_display_server (SESSION (greeter)));
771
772     return g_object_ref (session);
773 }
774
775 static void
776 prepend_argv (gchar ***argv, const gchar *value)
777 {
778     gchar **old_argv, **new_argv;
779     gint i;
780
781     old_argv = *argv;
782     new_argv = g_malloc (sizeof (gchar *) * (g_strv_length (*argv) + 2));
783     new_argv[0] = g_strdup (value);
784     for (i = 0; old_argv[i]; i++)
785         new_argv[i + 1] = old_argv[i];
786     new_argv[i + 1] = NULL;
787
788     g_free (*argv);
789     *argv = new_argv;
790 }
791
792 static Session *
793 find_user_session (Seat *seat, const gchar *username)
794 {
795     GList *link;
796
797     if (!username)
798         return NULL;
799
800     for (link = seat->priv->sessions; link; link = link->next)
801     {
802         Session *session = link->data;
803
804         if (!session_get_is_stopping (session) && strcmp (session_get_username (session), username) == 0)
805             return session;
806     }
807
808     return NULL;
809 }
810
811 static gboolean
812 greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *session_name, Seat *seat)
813 {
814     Session *session, *existing_session;
815     const gchar *username, *language = NULL;
816     SessionConfig *session_config;
817     User *user;
818     gchar *sessions_dir = NULL;
819     gchar **argv;
820
821     /* Get the session to use */
822     if (greeter_get_guest_authenticated (greeter))
823     {
824         session = create_guest_session (seat);
825         if (!session)
826             return FALSE;
827         session_set_pam_service (session, AUTOLOGIN_SERVICE);
828     }
829     else
830         session = greeter_get_authentication_session (greeter);
831
832     /* Switch to this session when it is ready */
833     if (seat->priv->session_to_activate)
834         g_object_unref (seat->priv->session_to_activate);
835     seat->priv->session_to_activate = g_object_ref (session);
836
837     /* Return to existing session if it is open */
838     username = session_get_username (session);
839     existing_session = find_user_session (seat, username);
840     if (existing_session && session != existing_session)
841     {
842         g_debug ("Returning to existing user session %s", username);
843         session_stop (session);
844         seat_set_active_session (seat, existing_session);
845         return TRUE;
846     }
847
848     /* Get session command to run */
849     switch (type)
850     {
851     case SESSION_TYPE_LOCAL:
852         sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
853         break;
854     case SESSION_TYPE_REMOTE:
855         sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
856         break;
857     }
858
859     /* Load user preferences */
860     user = session_get_user (session);
861     if (user)
862     {
863         if (!session_name)
864             session_name = user_get_xsession (user);
865         language = user_get_language (user);
866     }
867
868     if (!session_name)
869         session_name = seat_get_string_property (seat, "user-session");
870     if (user)
871         user_set_xsession (session_get_user (session), session_name);
872
873     session_config = find_session_config (sessions_dir, session_name);
874     g_free (sessions_dir);
875     if (!session_config)
876     {
877         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
878         return FALSE;
879     }
880
881     argv = get_session_argv (session_config, seat_get_string_property (seat, "session-wrapper"));
882     g_object_unref (session_config);
883     session_set_argv (session, argv);
884     g_strfreev (argv);
885     session_set_env (session, "DESKTOP_SESSION", session_name);
886     session_set_env (session, "GDMSESSION", session_name);
887     if (language && language[0] != '\0')
888     {
889         session_set_env (session, "LANG", language);
890         session_set_env (session, "GDM_LANG", language);
891     }
892
893     /* If can re-use the display server, stop the greeter first */
894     if (seat->priv->share_display_server)
895     {
896         /* Run on the same display server after the greeter has stopped */
897         session_set_display_server (session, session_get_display_server (SESSION (greeter)));
898
899         g_debug ("Stopping greeter");
900         session_stop (SESSION (greeter));
901
902         return TRUE;
903     }
904     /* Otherwise start a new display server for this session */
905     else
906     {
907         DisplayServer *display_server;
908
909         display_server = create_display_server (seat);
910         if (!display_server_start (display_server))
911             return FALSE;
912
913         session_set_display_server (session, display_server);
914
915         return TRUE;
916     }
917 }
918
919 static Greeter *
920 create_greeter_session (Seat *seat)
921 {
922     gchar *sessions_dir, **argv;
923     SessionConfig *session_config;
924     Greeter *greeter_session;
925     gchar *greeter_user;
926     const gchar *greeter_wrapper;
927
928     sessions_dir = config_get_string (config_get_instance (), "LightDM", "greeters-directory");
929     session_config = find_session_config (sessions_dir, seat_get_string_property (seat, "greeter-session"));
930     g_free (sessions_dir);
931     if (!session_config)
932         return NULL;
933
934     argv = get_session_argv (session_config, NULL);
935     g_object_unref (session_config);
936     greeter_wrapper = seat_get_string_property (seat, "greeter-wrapper");
937     if (greeter_wrapper)
938     {
939         gchar *path;
940         path = g_find_program_in_path (greeter_wrapper);
941         prepend_argv (&argv, path ? path : greeter_wrapper);
942         g_free (path);
943     }
944
945     greeter_session = SEAT_GET_CLASS (seat)->create_greeter_session (seat);
946     seat->priv->sessions = g_list_append (seat->priv->sessions, SESSION (greeter_session));
947     g_signal_connect (greeter_session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
948     g_signal_connect (greeter_session, "stopped", G_CALLBACK (session_stopped_cb), seat);
949   
950     set_session_env (SESSION (greeter_session));
951
952     session_set_pam_service (SESSION (greeter_session), GREETER_SERVICE);
953     greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
954     session_set_username (SESSION (greeter_session), greeter_user);
955     g_free (greeter_user);
956     session_set_argv (SESSION (greeter_session), argv);
957     g_strfreev (argv);
958
959     greeter_set_pam_services (greeter_session, USER_SERVICE, AUTOLOGIN_SERVICE);
960     g_signal_connect (greeter_session, "create-session", G_CALLBACK (greeter_create_session_cb), seat);
961     g_signal_connect (greeter_session, "start-session", G_CALLBACK (greeter_start_session_cb), seat);
962
963     /* Set hints to greeter */
964     greeter_set_hint (greeter_session, "default-session", seat_get_string_property (seat, "user-session"));
965     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
966     greeter_set_hint (greeter_session, "hide-users", seat_get_boolean_property (seat, "greeter-hide-users") ? "true" : "false");
967     greeter_set_hint (greeter_session, "show-manual-login", seat_get_boolean_property (seat, "greeter-show-manual-login") ? "true" : "false");
968     greeter_set_hint (greeter_session, "show-remote-login", seat_get_boolean_property (seat, "greeter-show-remote-login") ? "true" : "false");
969     greeter_set_hint (greeter_session, "has-guest-account", seat_get_allow_guest (seat) && seat_get_boolean_property (seat, "greeter-allow-guest") ? "true" : "false");
970
971     return greeter_session;
972 }
973
974 static Session *
975 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
976 {
977     GList *link;
978
979     for (link = seat->priv->sessions; link; link = link->next)
980     {
981         Session *session = link->data;
982         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
983             return session;
984     }
985
986     return NULL;
987 }
988
989 static void
990 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
991 {
992     const gchar *script;
993     Session *session;
994
995     /* Run setup script */
996     script = seat_get_string_property (seat, "display-setup-script");
997     if (script && !run_script (seat, display_server, script, NULL))
998     {
999         g_debug ("Stopping display server due to failed setup script");
1000         display_server_stop (display_server);
1001         return;
1002     }
1003
1004     /* Stop if don't need to run a session */
1005     if (!display_server_get_start_local_sessions (display_server))
1006         return;
1007
1008     emit_upstart_signal ("login-session-start");
1009
1010     /* Start the session waiting for this display server */
1011     session = find_session_for_display_server (seat, display_server);
1012     if (session)
1013     {
1014         if (session_get_is_authenticated (session))
1015         {
1016             g_debug ("Display server ready, running session");
1017             run_session (seat, session);
1018         }
1019         else
1020         {
1021             g_debug ("Display server ready, starting session authentication");
1022             start_session (seat, session);
1023         }
1024     }
1025     else
1026     {
1027         g_debug ("Stopping not required display server");
1028         display_server_stop (display_server);
1029     }
1030 }
1031
1032 static DisplayServer *
1033 create_display_server (Seat *seat)
1034 {
1035     DisplayServer *display_server;
1036
1037     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat, "x");
1038     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1039     g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
1040     g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_stopped_cb), seat);
1041
1042     return display_server;
1043 }
1044
1045 static Greeter *
1046 find_greeter_session (Seat *seat)
1047 {
1048     GList *link;
1049
1050     for (link = seat->priv->sessions; link; link = link->next)
1051     {
1052         Session *session = link->data;
1053         if (!session_get_is_stopping (session) && IS_GREETER (session))
1054             return GREETER (session);
1055     }
1056
1057     return NULL;
1058 }
1059
1060 gboolean
1061 seat_switch_to_greeter (Seat *seat)
1062 {
1063     Greeter *greeter_session;
1064     DisplayServer *display_server;
1065
1066     g_return_val_if_fail (seat != NULL, FALSE);
1067
1068     if (!seat->priv->can_switch)
1069         return FALSE;
1070
1071     /* Switch to greeter if one open (shouldn't be though) */
1072     greeter_session = find_greeter_session (seat);
1073     if (greeter_session)
1074     {
1075         g_debug ("Switching to existing greeter");
1076         seat_set_active_session (seat, SESSION (greeter_session));
1077         return TRUE;
1078     }
1079
1080     greeter_session = create_greeter_session (seat);
1081     if (seat->priv->session_to_activate)
1082         g_object_unref (seat->priv->session_to_activate);
1083     seat->priv->session_to_activate = g_object_ref (greeter_session);
1084
1085     display_server = create_display_server (seat);
1086     session_set_display_server (SESSION (greeter_session), display_server);
1087     if (!display_server_start (display_server))
1088         return FALSE;
1089
1090     return TRUE;
1091 }
1092
1093 gboolean
1094 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1095 {
1096     Session *session;
1097     DisplayServer *display_server;
1098
1099     g_return_val_if_fail (seat != NULL, FALSE);
1100     g_return_val_if_fail (username != NULL, FALSE);
1101
1102     if (!seat->priv->can_switch)
1103         return FALSE;
1104
1105     g_debug ("Switching to user %s", username);
1106
1107     session = find_user_session (seat, username);
1108     if (session)
1109     {
1110         g_debug ("Switching to existing user session %s", username);
1111         seat_set_active_session (seat, session);
1112         return TRUE;
1113     }
1114
1115     session = create_user_session (seat, username);
1116     if (!session)
1117         return FALSE;
1118     if (seat->priv->session_to_activate)
1119         g_object_unref (seat->priv->session_to_activate);
1120     seat->priv->session_to_activate = g_object_ref (session);
1121     session_set_pam_service (session, USER_SERVICE);
1122
1123     display_server = create_display_server (seat);
1124     session_set_display_server (session, display_server);
1125     if (!display_server_start (display_server))
1126         return FALSE;
1127
1128     return FALSE;
1129 }
1130
1131 static Session *
1132 find_guest_session (Seat *seat)
1133 {
1134     GList *link;
1135
1136     for (link = seat->priv->sessions; link; link = link->next)
1137     {
1138         Session *session = link->data;
1139         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1140             return session;
1141     }
1142
1143     return NULL;
1144 }
1145
1146 gboolean
1147 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1148 {
1149     Session *session;
1150     DisplayServer *display_server;
1151
1152     g_return_val_if_fail (seat != NULL, FALSE);
1153
1154     if (!seat->priv->can_switch || !seat_get_allow_guest (seat))
1155         return FALSE;
1156
1157     /* Switch to session if one open */
1158     session = find_guest_session (seat);
1159     if (session)
1160     {
1161         g_debug ("Switching to existing guest account %s", session_get_username (session));
1162         seat_set_active_session (seat, session);
1163         return TRUE;
1164     }
1165
1166     display_server = create_display_server (seat);
1167     if (!display_server_start (display_server))
1168         return FALSE;
1169
1170     session = create_guest_session (seat);
1171     if (!session)
1172         return FALSE;
1173     if (seat->priv->session_to_activate)
1174         g_object_unref (seat->priv->session_to_activate);
1175     seat->priv->session_to_activate = g_object_ref (session);
1176     session_set_pam_service (session, AUTOLOGIN_SERVICE);
1177     session_set_display_server (session, display_server);
1178
1179     return TRUE;
1180 }
1181
1182 gboolean
1183 seat_lock (Seat *seat, const gchar *username)
1184 {
1185     Greeter *greeter_session;
1186     DisplayServer *display_server;
1187
1188     g_return_val_if_fail (seat != NULL, FALSE);
1189
1190     if (!seat->priv->can_switch)
1191         return FALSE;
1192
1193     g_debug ("Locking seat");
1194
1195     /* Switch to greeter if one open (shouldn't be though) */
1196     greeter_session = find_greeter_session (seat);
1197     if (greeter_session)
1198     {
1199         g_debug ("Switching to existing greeter");
1200         seat_set_active_session (seat, SESSION (greeter_session));
1201         return TRUE;
1202     }
1203
1204     display_server = create_display_server (seat);
1205     if (!display_server_start (display_server))
1206         return FALSE;
1207
1208     greeter_session = create_greeter_session (seat);
1209     if (seat->priv->session_to_activate)
1210         g_object_unref (seat->priv->session_to_activate);
1211     seat->priv->session_to_activate = g_object_ref (greeter_session);
1212     greeter_set_hint (greeter_session, "lock-screen", "true");
1213     if (username)
1214         greeter_set_hint (greeter_session, "select-user", username);
1215     session_set_display_server (SESSION (greeter_session), display_server);
1216
1217     return TRUE;
1218 }
1219
1220 void
1221 seat_stop (Seat *seat)
1222 {
1223     g_return_if_fail (seat != NULL);
1224
1225     if (seat->priv->stopping)
1226         return;
1227
1228     g_debug ("Stopping seat");
1229     seat->priv->stopping = TRUE;
1230     SEAT_GET_CLASS (seat)->stop (seat);
1231 }
1232
1233 gboolean
1234 seat_get_is_stopping (Seat *seat)
1235 {
1236     g_return_val_if_fail (seat != NULL, FALSE);
1237     return seat->priv->stopping;
1238 }
1239
1240 static void
1241 seat_real_setup (Seat *seat)
1242 {
1243 }
1244
1245 static gboolean
1246 seat_real_start (Seat *seat)
1247 {
1248     const gchar *autologin_username;
1249     int autologin_timeout;
1250     gboolean autologin_guest;
1251     gboolean autologin_in_background;
1252     Session *session = NULL;
1253     DisplayServer *display_server;
1254
1255     g_debug ("Starting seat");
1256
1257     display_server = create_display_server (seat);
1258
1259     /* If this display server doesn't have a session running on it, just start it */
1260     if (!display_server_get_start_local_sessions (display_server))
1261         return display_server_start (display_server);
1262
1263     /* Get autologin settings */
1264     autologin_username = seat_get_string_property (seat, "autologin-user");
1265     if (g_strcmp0 (autologin_username, "") == 0)
1266         autologin_username = NULL;
1267     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1268     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1269     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1270
1271     /* Autologin if configured */
1272     if (autologin_timeout == 0 || autologin_in_background)
1273     {
1274         if (autologin_guest)
1275             session = create_guest_session (seat);
1276         else if (autologin_username != NULL)
1277             session = create_user_session (seat, autologin_username);
1278       
1279         if (session)
1280         {
1281             if (seat->priv->session_to_activate)
1282                 g_object_unref (seat->priv->session_to_activate);
1283             seat->priv->session_to_activate = g_object_ref (session);
1284             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1285         }
1286
1287         /* Load in background if required */
1288         if (autologin_in_background && session)
1289         {
1290             DisplayServer *background_display_server;
1291
1292             background_display_server = create_display_server (seat);
1293             session_set_display_server (session, background_display_server);
1294             if (!display_server_start (background_display_server))
1295                 return FALSE;
1296
1297             /* Start a greeter as well */
1298             session = NULL;
1299         }
1300     }
1301
1302     /* Fallback to a greeter */
1303     if (!session)
1304     {
1305         Greeter *greeter_session;
1306
1307         greeter_session = create_greeter_session (seat);
1308         if (seat->priv->session_to_activate)
1309             g_object_unref (seat->priv->session_to_activate);
1310         seat->priv->session_to_activate = g_object_ref (greeter_session);
1311         session = SESSION (greeter_session);
1312
1313         if (autologin_timeout)
1314         {
1315             gchar *value;
1316
1317             value = g_strdup_printf ("%d", autologin_timeout);
1318             greeter_set_hint (greeter_session, "autologin-timeout", value);
1319             g_free (value);
1320             if (autologin_username)
1321                 greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1322             if (autologin_guest)
1323                 greeter_set_hint (greeter_session, "autologin-guest", "true");
1324         }
1325     }
1326
1327     /* Fail if can't start a session */
1328     if (!session)
1329     {
1330         seat_stop (seat);
1331         return FALSE;
1332     }
1333
1334     /* Start display server to show session on */
1335     session_set_display_server (session, display_server);
1336     if (!display_server_start (display_server))
1337         return FALSE;
1338
1339     return TRUE;
1340 }
1341
1342 static void
1343 seat_real_set_active_session (Seat *seat, Session *session)
1344 {
1345 }
1346
1347 static Session *
1348 seat_real_get_active_session (Seat *seat)
1349 {
1350     return NULL;
1351 }
1352
1353 static void
1354 seat_real_stop (Seat *seat)
1355 {
1356     GList *list, *link;
1357
1358     check_stopped (seat);
1359     if (seat->priv->stopped)
1360         return;
1361
1362     /* Stop all the display servers and sessions on the seat. Copy the list as
1363      * it might be modified if a display server / session stops during this loop */
1364     list = g_list_copy (seat->priv->display_servers);
1365     for (link = list; link; link = link->next)
1366         g_object_ref (link->data);
1367     for (link = list; link; link = link->next)
1368     {
1369         DisplayServer *display_server = link->data;
1370         if (!display_server_get_is_stopping (display_server))
1371         {
1372             g_debug ("Stopping display server");
1373             display_server_stop (display_server);
1374         }
1375     }
1376     g_list_free_full (list, g_object_unref);
1377     list = g_list_copy (seat->priv->sessions);
1378     for (link = list; link; link = link->next)
1379         g_object_ref (link->data);
1380     for (link = list; link; link = link->next)
1381     {
1382         Session *session = link->data;
1383         if (!session_get_is_stopping (session))
1384         {
1385             g_debug ("Stopping session");
1386             session_stop (session);
1387         }
1388     }
1389     g_list_free_full (list, g_object_unref);
1390 }
1391
1392 static void
1393 seat_init (Seat *seat)
1394 {
1395     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1396     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1397     seat->priv->share_display_server = TRUE;
1398 }
1399
1400 static void
1401 seat_finalize (GObject *object)
1402 {
1403     Seat *self;
1404     GList *link;
1405
1406     self = SEAT (object);
1407
1408     g_hash_table_unref (self->priv->properties);
1409     for (link = self->priv->display_servers; link; link = link->next)
1410     {
1411         DisplayServer *display_server = link->data;
1412         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1413     }  
1414     g_list_free_full (self->priv->display_servers, g_object_unref);
1415     for (link = self->priv->sessions; link; link = link->next)
1416     {
1417         Session *session = link->data;
1418         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1419     }
1420     g_list_free_full (self->priv->sessions, g_object_unref);
1421     if (self->priv->session_to_activate)
1422         g_object_unref (self->priv->session_to_activate);
1423
1424     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1425 }
1426
1427 static void
1428 seat_class_init (SeatClass *klass)
1429 {
1430     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1431
1432     klass->setup = seat_real_setup;
1433     klass->start = seat_real_start;
1434     klass->set_active_session = seat_real_set_active_session;
1435     klass->get_active_session = seat_real_get_active_session;
1436     klass->run_script = seat_real_run_script;
1437     klass->stop = seat_real_stop;
1438
1439     object_class->finalize = seat_finalize;
1440
1441     g_type_class_add_private (klass, sizeof (SeatPrivate));
1442
1443     signals[SESSION_ADDED] =
1444         g_signal_new ("session-added",
1445                       G_TYPE_FROM_CLASS (klass),
1446                       G_SIGNAL_RUN_LAST,
1447                       G_STRUCT_OFFSET (SeatClass, session_added),
1448                       NULL, NULL,
1449                       NULL,
1450                       G_TYPE_NONE, 1, SESSION_TYPE);
1451     signals[RUNNING_USER_SESSION] =
1452         g_signal_new ("running-user-session",
1453                       G_TYPE_FROM_CLASS (klass),
1454                       G_SIGNAL_RUN_LAST,
1455                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1456                       NULL, NULL,
1457                       NULL,
1458                       G_TYPE_NONE, 1, SESSION_TYPE);
1459     signals[SESSION_REMOVED] =
1460         g_signal_new ("session-removed",
1461                       G_TYPE_FROM_CLASS (klass),
1462                       G_SIGNAL_RUN_LAST,
1463                       G_STRUCT_OFFSET (SeatClass, session_removed),
1464                       NULL, NULL,
1465                       NULL,
1466                       G_TYPE_NONE, 1, SESSION_TYPE);
1467     signals[STOPPED] =
1468         g_signal_new ("stopped",
1469                       G_TYPE_FROM_CLASS (klass),
1470                       G_SIGNAL_RUN_LAST,
1471                       G_STRUCT_OFFSET (SeatClass, stopped),
1472                       NULL, NULL,
1473                       NULL,
1474                       G_TYPE_NONE, 0);
1475 }