]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Don't subclass Session - we can't know the type until it has authenticated
[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 gboolean
305 get_start_local_sessions (Seat *seat)
306 {
307     return SEAT_GET_CLASS (seat)->get_start_local_sessions (seat);
308 }
309
310 static void
311 display_server_stopped_cb (DisplayServer *display_server, Seat *seat)
312 {
313     GList *list, *link;
314     Session *active_session;
315
316     g_debug ("Display server stopped");
317
318     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
319     seat->priv->display_servers = g_list_remove (seat->priv->display_servers, display_server);
320
321     if (seat->priv->stopping)
322     {
323         check_stopped (seat);
324         g_object_unref (display_server);
325         return;
326     }
327
328     /* Stop all sessions on this display server */
329     list = g_list_copy (seat->priv->sessions);
330     for (link = list; link; link = link->next)
331         g_object_ref (link->data);
332     for (link = list; link; link = link->next)
333     {
334         Session *session = link->data;
335
336         if (session_get_display_server (session) != display_server || session_get_is_stopping (session))
337             continue;
338
339         /* Stop seat if this is the only display server and it failed to start a greeter */
340         if (IS_GREETER (session) &&
341             !session_get_is_started (session) &&
342             g_list_length (seat->priv->display_servers) == 0)
343         {
344             g_debug ("Stopping seat, greeter display server failed to start");
345             seat_stop (seat);
346         }
347
348         g_debug ("Stopping session");
349         session_stop (session);
350     }
351     g_list_free_full (list, g_object_unref);
352
353     if (!seat->priv->stopping && get_start_local_sessions (seat))
354     {
355         /* If we were the active session, switch to a greeter */
356         active_session = seat_get_active_session (seat);
357         if (!active_session || session_get_display_server (active_session) == display_server)
358         {
359             g_debug ("Active display server stopped, starting greeter");
360             seat_switch_to_greeter (seat);
361         }
362     }
363
364     g_object_unref (display_server);
365 }
366
367 static void
368 switch_to_greeter_from_failed_session (Seat *seat, Session *session)
369 {
370     Greeter *greeter_session;
371
372     greeter_session = create_greeter_session (seat);
373     if (session_get_is_guest (session))
374         greeter_set_hint (greeter_session, "select-guest", "true");
375     else
376         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
377     if (seat->priv->session_to_activate)
378         g_object_unref (seat->priv->session_to_activate);
379     seat->priv->session_to_activate = g_object_ref (greeter_session);
380
381     if (seat->priv->share_display_server)
382         session_set_display_server (SESSION (greeter_session), session_get_display_server (session));
383     else
384     {
385         DisplayServer *display_server;
386
387         display_server = create_display_server (seat);
388         if (!display_server_start (display_server))
389         {
390             g_debug ("Failed to start display server for greeter");
391             seat_stop (seat);
392         }
393
394         session_set_display_server (session, display_server);
395     }
396
397     start_session (seat, SESSION (greeter_session));
398
399     /* Stop failed session */
400     session_stop (session);
401 }
402
403 static void
404 start_session (Seat *seat, Session *session)
405 {
406     if (session_start (session))
407         return;
408
409     if (IS_GREETER (session))
410     {
411         g_debug ("Failed to start greeter");
412         display_server_stop (session_get_display_server (session));
413         return;
414     }
415
416     g_debug ("Failed to start session, starting greeter");
417     switch_to_greeter_from_failed_session (seat, session);
418 }
419
420 static void
421 run_session (Seat *seat, Session *session)
422 {
423     const gchar *script;
424
425     if (IS_GREETER (session))
426         script = seat_get_string_property (seat, "greeter-setup-script");
427     else
428         script = seat_get_string_property (seat, "session-setup-script");
429     if (script && !run_script (seat, session_get_display_server (session), script, NULL))
430     {
431         g_debug ("Switching to greeter due to failed setup script");
432         switch_to_greeter_from_failed_session (seat, session);
433         return;
434     }
435
436     if (!IS_GREETER (session))
437         g_signal_emit (seat, signals[RUNNING_USER_SESSION], 0, session);
438
439     session_run (session);
440
441     // FIXME: Wait until the session is ready
442
443     if (session == seat->priv->session_to_activate)
444     {
445         seat_set_active_session (seat, session);
446         g_object_unref (seat->priv->session_to_activate);
447         seat->priv->session_to_activate = NULL;
448     }
449 }
450
451 static void
452 session_authentication_complete_cb (Session *session, Seat *seat)
453 {
454     if (session_get_is_authenticated (session))
455     {
456         g_debug ("Session authenticated, running command");
457         run_session (seat, session);
458     }
459     else if (!IS_GREETER (session))
460     {
461         g_debug ("Switching to greeter due to failed authentication");
462         switch_to_greeter_from_failed_session (seat, session);
463     }
464     else
465     {
466         g_debug ("Stopping session that failed authentication");
467         session_stop (session);
468     }
469 }
470
471 static void
472 session_stopped_cb (Session *session, Seat *seat)
473 {
474     DisplayServer *display_server;
475
476     g_debug ("Session stopped");
477
478     display_server = session_get_display_server (session);
479
480     /* Cleanup */
481     if (!IS_GREETER (session))
482     {
483         const gchar *script;
484         script = seat_get_string_property (seat, "session-cleanup-script");
485         if (script)
486             run_script (seat, display_server, script, session_get_user (session));
487     }
488
489     g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
490     seat->priv->sessions = g_list_remove (seat->priv->sessions, session);
491
492     /* We were waiting for this session, but it didn't start :( */
493     // FIXME: Start a greeter on this?
494     if (session == seat->priv->session_to_activate)
495     {
496         g_object_unref (seat->priv->session_to_activate);
497         seat->priv->session_to_activate = NULL;
498     }
499
500     if (seat->priv->stopping)
501     {
502         check_stopped (seat);
503         g_object_unref (session);
504         return;
505     }
506
507     /* If this is the greeter session then re-use this display server */
508     if (IS_GREETER (session) && seat->priv->share_display_server &&
509         greeter_get_start_session (GREETER (session)))
510     {
511         GList *link;
512
513         for (link = seat->priv->sessions; link; link = link->next)
514         {
515             Session *s = link->data;
516
517             /* Skip this session and sessions on other display servers */
518             if (s == session || session_get_display_server (s) != display_server || session_get_is_stopping (s))
519                 continue;
520
521             if (session_get_is_authenticated (s))
522             {
523                 g_debug ("Greeter stopped, running session");
524                 run_session (seat, s);
525             }
526             else
527             {
528                 g_debug ("Greeter stopped, starting session authentication");
529                 start_session (seat, s);
530             }
531             break;
532         }
533     }
534     /* If this is the greeter and nothing else is running then stop the seat */
535     else if (IS_GREETER (session) &&
536         !greeter_get_start_session (GREETER (session)) &&
537         g_list_length (seat->priv->display_servers) == 1 &&
538         g_list_nth_data (seat->priv->display_servers, 0) == display_server)
539     {
540         g_debug ("Stopping seat, failed to start a greeter");
541         seat_stop (seat);
542     }
543     /* If we were the active session, switch to a greeter */
544     else if (!IS_GREETER (session) && session == seat_get_active_session (seat))
545     {
546         g_debug ("Active session stopped, starting greeter");
547         seat_switch_to_greeter (seat);
548     }
549
550     /* Stop the display server if no-longer required */
551     if (display_server && !display_server_get_is_stopping (display_server))
552     {
553         GList *link;
554         int n_sessions = 0;
555
556         for (link = seat->priv->sessions; link; link = link->next)
557         {
558             Session *s = link->data;
559             if (s == session)
560                 continue;
561             if (session_get_display_server (s) == display_server)
562                 n_sessions++;
563         }
564         if (n_sessions == 0)
565         {
566             g_debug ("Stopping display server, no sessions require it");
567             display_server_stop (display_server);
568         }
569     }
570
571     g_signal_emit (seat, signals[SESSION_REMOVED], 0, session);
572     g_object_unref (session);
573 }
574
575 static void
576 set_session_env (Session *session)
577 {
578     /* Connect using the session bus */
579     if (getuid () != 0)
580     {
581         if (g_getenv ("DBUS_SESSION_BUS_ADDRESS"))
582             session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
583         session_set_env (session, "LDM_BUS", "SESSION");
584         if (g_getenv ("LD_PRELOAD"))
585             session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
586         if (g_getenv ("LD_LIBRARY_PATH"))
587             session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
588         if (g_getenv ("PATH"))
589             session_set_env (session, "PATH", g_getenv ("PATH"));
590     }
591
592     /* Variables required for regression tests */
593     if (g_getenv ("LIGHTDM_TEST_ROOT"))
594     {
595         session_set_env (session, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
596         session_set_env (session, "DBUS_SYSTEM_BUS_ADDRESS", g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
597         session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
598         session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
599         session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
600         session_set_env (session, "GI_TYPELIB_PATH", g_getenv ("GI_TYPELIB_PATH"));
601     }
602 }
603
604 static Session *
605 create_session (Seat *seat, gboolean autostart)
606 {
607     Session *session;
608
609     session = SEAT_GET_CLASS (seat)->create_session (seat);
610     seat->priv->sessions = g_list_append (seat->priv->sessions, session);
611     if (autostart)
612         g_signal_connect (session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
613     g_signal_connect (session, "stopped", G_CALLBACK (session_stopped_cb), seat);
614
615     set_session_env (session);
616
617     g_signal_emit (seat, signals[SESSION_ADDED], 0, session);
618
619     return session;
620 }
621
622 static gchar **
623 get_session_argv (SessionConfig *session_config, const gchar *session_wrapper)
624 {
625     gboolean result;
626     int argc;
627     gchar **argv, *path;
628     GError *error = NULL;
629
630     /* If configured, run sessions through a wrapper */
631     if (session_wrapper)
632     {
633         argv = g_malloc (sizeof (gchar *) * 3);
634         path = g_find_program_in_path (session_wrapper);
635         argv[0] = path ? path : g_strdup (session_wrapper);
636         argv[1] = g_strdup (session_config_get_command (session_config));
637         argv[2] = NULL;
638         return argv;
639     }
640
641     /* Split command into an array listing and make command absolute */
642     result = g_shell_parse_argv (session_config_get_command (session_config), &argc, &argv, &error);
643     if (error)
644         g_debug ("Invalid session command '%s': %s", session_config_get_command (session_config), error->message);
645     g_clear_error (&error);
646     if (!result)
647         return NULL;
648     path = g_find_program_in_path (argv[0]);
649     if (path)
650     {
651         g_free (argv[0]);
652         argv[0] = path;
653     }
654   
655     return argv;
656 }
657
658 static SessionConfig *
659 find_session_config (const gchar *sessions_dir, const gchar *session_name)
660 {
661     gchar **dirs;
662     SessionConfig *session_config = NULL;
663     int i;
664     GError *error = NULL;
665
666     g_return_val_if_fail (sessions_dir != NULL, NULL);
667     g_return_val_if_fail (session_name != NULL, NULL);
668
669     dirs = g_strsplit (sessions_dir, ":", -1);
670     for (i = 0; dirs[i]; i++)
671     {
672         gchar *filename, *path;
673
674         filename = g_strdup_printf ("%s.desktop", session_name);
675         path = g_build_filename (dirs[i], filename, NULL);
676         g_free (filename);
677         session_config = session_config_new_from_file (path, &error);
678         g_free (path);
679         if (session_config)
680             break;
681
682         if (dirs[i+1] == NULL)
683             g_debug ("Failed to find session configuration %s", session_name);
684         g_clear_error (&error);
685     }
686     g_strfreev (dirs);
687
688     return session_config;
689 }
690
691 static Session *
692 create_user_session (Seat *seat, const gchar *username)
693 {
694     User *user;
695     gchar *sessions_dir;
696     const gchar *session_name, *language;
697     SessionConfig *session_config;
698     Session *session = NULL;
699
700     /* Load user preferences */
701     user = accounts_get_user_by_name (username);
702     if (!user)
703     {
704         g_debug ("Can't login unknown user '%s'", username);
705         return NULL;
706     }
707     session_name = user_get_xsession (user);
708     language = user_get_language (user);
709
710     if (!session_name)
711         session_name = seat_get_string_property (seat, "user-session");
712     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
713     session_config = find_session_config (sessions_dir, session_name);
714     g_free (sessions_dir);
715     if (session_config)
716     {
717         gchar **argv;
718
719         session = create_session (seat, TRUE);
720         session_set_env (session, "DESKTOP_SESSION", session_name);
721         session_set_env (session, "GDMSESSION", session_name);
722         if (language && language[0] != '\0')
723         {
724             session_set_env (session, "LANG", language);
725             session_set_env (session, "GDM_LANG", language);
726         }
727         session_set_pam_service (session, AUTOLOGIN_SERVICE);
728         session_set_username (session, username);
729         session_set_do_authenticate (session, TRUE);
730         argv = get_session_argv (session_config, seat_get_string_property (seat, "session-wrapper"));
731         session_set_argv (session, argv);
732         g_strfreev (argv);
733
734         g_object_unref (session_config);
735     }
736     else
737         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
738
739
740     g_object_unref (user);
741
742     return session;
743 }
744
745 static Session *
746 create_guest_session (Seat *seat)
747 {
748     gchar *sessions_dir, **argv;
749     SessionConfig *session_config;
750     Session *session;
751
752     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
753     session_config = find_session_config (sessions_dir, seat_get_string_property (seat, "user-session"));
754     g_free (sessions_dir);
755     if (!session_config)
756     {
757         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
758         return NULL;
759     }
760
761     session = create_session (seat, TRUE);
762     session_set_do_authenticate (session, TRUE);
763     session_set_is_guest (session, TRUE);
764     argv = get_session_argv (session_config, seat_get_string_property (seat, "session-wrapper"));
765     g_object_unref (session_config);
766     session_set_argv (session, argv);
767     g_strfreev (argv);
768   
769     return session;
770 }
771
772 static Session *
773 greeter_create_session_cb (Greeter *greeter, Seat *seat)
774 {
775     Session *session;
776
777     session = create_session (seat, FALSE);
778     session_set_display_server (session, session_get_display_server (SESSION (greeter)));
779
780     return g_object_ref (session);
781 }
782
783 static void
784 prepend_argv (gchar ***argv, const gchar *value)
785 {
786     gchar **old_argv, **new_argv;
787     gint i;
788
789     old_argv = *argv;
790     new_argv = g_malloc (sizeof (gchar *) * (g_strv_length (*argv) + 2));
791     new_argv[0] = g_strdup (value);
792     for (i = 0; old_argv[i]; i++)
793         new_argv[i + 1] = old_argv[i];
794     new_argv[i + 1] = NULL;
795
796     g_free (*argv);
797     *argv = new_argv;
798 }
799
800 static Session *
801 find_user_session (Seat *seat, const gchar *username)
802 {
803     GList *link;
804
805     if (!username)
806         return NULL;
807
808     for (link = seat->priv->sessions; link; link = link->next)
809     {
810         Session *session = link->data;
811
812         if (!session_get_is_stopping (session) && strcmp (session_get_username (session), username) == 0)
813             return session;
814     }
815
816     return NULL;
817 }
818
819 static gboolean
820 greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *session_name, Seat *seat)
821 {
822     Session *session, *existing_session;
823     const gchar *username, *language = NULL;
824     SessionConfig *session_config;
825     User *user;
826     gchar *sessions_dir = NULL;
827     gchar **argv;
828
829     /* Get the session to use */
830     if (greeter_get_guest_authenticated (greeter))
831     {
832         session = create_guest_session (seat);
833         if (!session)
834             return FALSE;
835         session_set_pam_service (session, AUTOLOGIN_SERVICE);
836     }
837     else
838         session = greeter_get_authentication_session (greeter);
839
840     /* Switch to this session when it is ready */
841     if (seat->priv->session_to_activate)
842         g_object_unref (seat->priv->session_to_activate);
843     seat->priv->session_to_activate = g_object_ref (session);
844
845     /* Return to existing session if it is open */
846     username = session_get_username (session);
847     existing_session = find_user_session (seat, username);
848     if (existing_session && session != existing_session)
849     {
850         g_debug ("Returning to existing user session %s", username);
851         session_stop (session);
852         seat_set_active_session (seat, existing_session);
853         return TRUE;
854     }
855
856     /* Get session command to run */
857     switch (type)
858     {
859     case SESSION_TYPE_LOCAL:
860         sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
861         break;
862     case SESSION_TYPE_REMOTE:
863         sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
864         break;
865     }
866
867     /* Load user preferences */
868     user = session_get_user (session);
869     if (user)
870     {
871         if (!session_name)
872             session_name = user_get_xsession (user);
873         language = user_get_language (user);
874     }
875
876     if (!session_name)
877         session_name = seat_get_string_property (seat, "user-session");
878     if (user)
879         user_set_xsession (session_get_user (session), session_name);
880
881     session_config = find_session_config (sessions_dir, session_name);
882     g_free (sessions_dir);
883     if (!session_config)
884     {
885         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
886         return FALSE;
887     }
888
889     argv = get_session_argv (session_config, seat_get_string_property (seat, "session-wrapper"));
890     g_object_unref (session_config);
891     session_set_argv (session, argv);
892     g_strfreev (argv);
893     session_set_env (session, "DESKTOP_SESSION", session_name);
894     session_set_env (session, "GDMSESSION", session_name);
895     if (language && language[0] != '\0')
896     {
897         session_set_env (session, "LANG", language);
898         session_set_env (session, "GDM_LANG", language);
899     }
900
901     /* If can re-use the display server, stop the greeter first */
902     if (seat->priv->share_display_server)
903     {
904         /* Run on the same display server after the greeter has stopped */
905         session_set_display_server (session, session_get_display_server (SESSION (greeter)));
906
907         g_debug ("Stopping greeter");
908         session_stop (SESSION (greeter));
909
910         return TRUE;
911     }
912     /* Otherwise start a new display server for this session */
913     else
914     {
915         DisplayServer *display_server;
916
917         display_server = create_display_server (seat);
918         if (!display_server_start (display_server))
919             return FALSE;
920
921         session_set_display_server (session, display_server);
922
923         return TRUE;
924     }
925 }
926
927 static Greeter *
928 create_greeter_session (Seat *seat)
929 {
930     gchar *sessions_dir, **argv;
931     SessionConfig *session_config;
932     Greeter *greeter_session;
933     gchar *greeter_user;
934     const gchar *greeter_wrapper;
935
936     sessions_dir = config_get_string (config_get_instance (), "LightDM", "greeters-directory");
937     session_config = find_session_config (sessions_dir, seat_get_string_property (seat, "greeter-session"));
938     g_free (sessions_dir);
939     if (!session_config)
940         return NULL;
941
942     argv = get_session_argv (session_config, NULL);
943     greeter_wrapper = seat_get_string_property (seat, "greeter-wrapper");
944     if (greeter_wrapper)
945     {
946         gchar *path;
947         path = g_find_program_in_path (greeter_wrapper);
948         prepend_argv (&argv, path ? path : greeter_wrapper);
949         g_free (path);
950     }
951
952     greeter_session = SEAT_GET_CLASS (seat)->create_greeter_session (seat);
953     seat->priv->sessions = g_list_append (seat->priv->sessions, SESSION (greeter_session));
954     g_signal_connect (greeter_session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
955     g_signal_connect (greeter_session, "stopped", G_CALLBACK (session_stopped_cb), seat);
956   
957     set_session_env (SESSION (greeter_session));
958
959     session_set_pam_service (SESSION (greeter_session), GREETER_SERVICE);
960     greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
961     session_set_username (SESSION (greeter_session), greeter_user);
962     g_free (greeter_user);
963     session_set_argv (SESSION (greeter_session), argv);
964     g_strfreev (argv);
965
966     greeter_set_pam_services (greeter_session, USER_SERVICE, AUTOLOGIN_SERVICE);
967     g_signal_connect (greeter_session, "create-session", G_CALLBACK (greeter_create_session_cb), seat);
968     g_signal_connect (greeter_session, "start-session", G_CALLBACK (greeter_start_session_cb), seat);
969
970     /* Set hints to greeter */
971     greeter_set_hint (greeter_session, "default-session", seat_get_string_property (seat, "user-session"));
972     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
973     greeter_set_hint (greeter_session, "hide-users", seat_get_boolean_property (seat, "greeter-hide-users") ? "true" : "false");
974     greeter_set_hint (greeter_session, "show-manual-login", seat_get_boolean_property (seat, "greeter-show-manual-login") ? "true" : "false");
975     greeter_set_hint (greeter_session, "show-remote-login", seat_get_boolean_property (seat, "greeter-show-remote-login") ? "true" : "false");
976     greeter_set_hint (greeter_session, "has-guest-account", seat_get_allow_guest (seat) && seat_get_boolean_property (seat, "greeter-allow-guest") ? "true" : "false");
977
978     g_object_unref (session_config);
979
980     return greeter_session;
981 }
982
983 static Session *
984 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
985 {
986     GList *link;
987
988     for (link = seat->priv->sessions; link; link = link->next)
989     {
990         Session *session = link->data;
991         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
992             return session;
993     }
994
995     return NULL;
996 }
997
998 static void
999 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
1000 {
1001     const gchar *script;
1002     Session *session;
1003
1004     /* Run setup script */
1005     script = seat_get_string_property (seat, "display-setup-script");
1006     if (script && !run_script (seat, display_server, script, NULL))
1007     {
1008         g_debug ("Stopping display server due to failed setup script");
1009         display_server_stop (display_server);
1010         return;
1011     }
1012
1013     /* Stop if don't need to run a session */
1014     if (!get_start_local_sessions (seat))
1015         return;
1016
1017     emit_upstart_signal ("login-session-start");
1018
1019     /* Start the session waiting for this display server */
1020     session = find_session_for_display_server (seat, display_server);
1021     if (session)
1022     {
1023         if (session_get_is_authenticated (session))
1024         {
1025             g_debug ("Display server ready, running session");
1026             run_session (seat, session);
1027         }
1028         else
1029         {
1030             g_debug ("Display server ready, starting session authentication");
1031             start_session (seat, session);
1032         }
1033     }
1034     else
1035     {
1036         g_debug ("Stopping not required display server");
1037         display_server_stop (display_server);
1038     }
1039 }
1040
1041 static DisplayServer *
1042 create_display_server (Seat *seat)
1043 {
1044     DisplayServer *display_server;
1045
1046     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat);
1047     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1048     g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
1049     g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_stopped_cb), seat);
1050
1051     return display_server;
1052 }
1053
1054 static Greeter *
1055 find_greeter_session (Seat *seat)
1056 {
1057     GList *link;
1058
1059     for (link = seat->priv->sessions; link; link = link->next)
1060     {
1061         Session *session = link->data;
1062         if (!session_get_is_stopping (session) && IS_GREETER (session))
1063             return GREETER (session);
1064     }
1065
1066     return NULL;
1067 }
1068
1069 gboolean
1070 seat_switch_to_greeter (Seat *seat)
1071 {
1072     Greeter *greeter_session;
1073     DisplayServer *display_server;
1074
1075     g_return_val_if_fail (seat != NULL, FALSE);
1076
1077     if (!seat->priv->can_switch)
1078         return FALSE;
1079
1080     /* Switch to greeter if one open (shouldn't be though) */
1081     greeter_session = find_greeter_session (seat);
1082     if (greeter_session)
1083     {
1084         g_debug ("Switching to existing greeter");
1085         seat_set_active_session (seat, SESSION (greeter_session));
1086         return TRUE;
1087     }
1088
1089     greeter_session = create_greeter_session (seat);
1090     if (seat->priv->session_to_activate)
1091         g_object_unref (seat->priv->session_to_activate);
1092     seat->priv->session_to_activate = g_object_ref (greeter_session);
1093
1094     display_server = create_display_server (seat);
1095     session_set_display_server (SESSION (greeter_session), display_server);
1096     if (!display_server_start (display_server))
1097         return FALSE;
1098
1099     return TRUE;
1100 }
1101
1102 gboolean
1103 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1104 {
1105     Session *session;
1106     DisplayServer *display_server;
1107
1108     g_return_val_if_fail (seat != NULL, FALSE);
1109     g_return_val_if_fail (username != NULL, FALSE);
1110
1111     if (!seat->priv->can_switch)
1112         return FALSE;
1113
1114     g_debug ("Switching to user %s", username);
1115
1116     session = find_user_session (seat, username);
1117     if (session)
1118     {
1119         g_debug ("Switching to existing user session %s", username);
1120         seat_set_active_session (seat, session);
1121         return TRUE;
1122     }
1123
1124     session = create_user_session (seat, username);
1125     if (!session)
1126         return FALSE;
1127     if (seat->priv->session_to_activate)
1128         g_object_unref (seat->priv->session_to_activate);
1129     seat->priv->session_to_activate = g_object_ref (session);
1130     session_set_pam_service (session, USER_SERVICE);
1131
1132     display_server = create_display_server (seat);
1133     session_set_display_server (session, display_server);
1134     if (!display_server_start (display_server))
1135         return FALSE;
1136
1137     return FALSE;
1138 }
1139
1140 static Session *
1141 find_guest_session (Seat *seat)
1142 {
1143     GList *link;
1144
1145     for (link = seat->priv->sessions; link; link = link->next)
1146     {
1147         Session *session = link->data;
1148         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1149             return session;
1150     }
1151
1152     return NULL;
1153 }
1154
1155 gboolean
1156 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1157 {
1158     Session *session;
1159     DisplayServer *display_server;
1160
1161     g_return_val_if_fail (seat != NULL, FALSE);
1162
1163     if (!seat->priv->can_switch || !seat_get_allow_guest (seat))
1164         return FALSE;
1165
1166     /* Switch to session if one open */
1167     session = find_guest_session (seat);
1168     if (session)
1169     {
1170         g_debug ("Switching to existing guest account %s", session_get_username (session));
1171         seat_set_active_session (seat, session);
1172         return TRUE;
1173     }
1174
1175     display_server = create_display_server (seat);
1176     if (!display_server_start (display_server))
1177         return FALSE;
1178
1179     session = create_guest_session (seat);
1180     if (!session)
1181         return FALSE;
1182     if (seat->priv->session_to_activate)
1183         g_object_unref (seat->priv->session_to_activate);
1184     seat->priv->session_to_activate = g_object_ref (session);
1185     session_set_pam_service (session, AUTOLOGIN_SERVICE);
1186     session_set_display_server (session, display_server);
1187
1188     return TRUE;
1189 }
1190
1191 gboolean
1192 seat_lock (Seat *seat, const gchar *username)
1193 {
1194     Greeter *greeter_session;
1195     DisplayServer *display_server;
1196
1197     g_return_val_if_fail (seat != NULL, FALSE);
1198
1199     if (!seat->priv->can_switch)
1200         return FALSE;
1201
1202     g_debug ("Locking seat");
1203
1204     /* Switch to greeter if one open (shouldn't be though) */
1205     greeter_session = find_greeter_session (seat);
1206     if (greeter_session)
1207     {
1208         g_debug ("Switching to existing greeter");
1209         seat_set_active_session (seat, SESSION (greeter_session));
1210         return TRUE;
1211     }
1212
1213     display_server = create_display_server (seat);
1214     if (!display_server_start (display_server))
1215         return FALSE;
1216
1217     greeter_session = create_greeter_session (seat);
1218     if (seat->priv->session_to_activate)
1219         g_object_unref (seat->priv->session_to_activate);
1220     seat->priv->session_to_activate = g_object_ref (greeter_session);
1221     greeter_set_hint (greeter_session, "lock-screen", "true");
1222     if (username)
1223         greeter_set_hint (greeter_session, "select-user", username);
1224     session_set_display_server (SESSION (greeter_session), display_server);
1225
1226     return TRUE;
1227 }
1228
1229 void
1230 seat_stop (Seat *seat)
1231 {
1232     g_return_if_fail (seat != NULL);
1233
1234     if (seat->priv->stopping)
1235         return;
1236
1237     g_debug ("Stopping seat");
1238     seat->priv->stopping = TRUE;
1239     SEAT_GET_CLASS (seat)->stop (seat);
1240 }
1241
1242 gboolean
1243 seat_get_is_stopping (Seat *seat)
1244 {
1245     g_return_val_if_fail (seat != NULL, FALSE);
1246     return seat->priv->stopping;
1247 }
1248
1249 static gboolean
1250 seat_real_get_start_local_sessions (Seat *seat)
1251 {
1252     return TRUE;
1253 }
1254
1255 static void
1256 seat_real_setup (Seat *seat)
1257 {
1258 }
1259
1260 static gboolean
1261 seat_real_start (Seat *seat)
1262 {
1263     const gchar *autologin_username;
1264     int autologin_timeout;
1265     gboolean autologin_guest;
1266     gboolean autologin_in_background;
1267     Session *session = NULL;
1268     DisplayServer *display_server;
1269
1270     g_debug ("Starting seat");
1271
1272     display_server = create_display_server (seat);
1273
1274     /* If this display server doesn't have a session running on it, just start it */
1275     if (!get_start_local_sessions (seat))
1276         return display_server_start (display_server);
1277
1278     /* Get autologin settings */
1279     autologin_username = seat_get_string_property (seat, "autologin-user");
1280     if (g_strcmp0 (autologin_username, "") == 0)
1281         autologin_username = NULL;
1282     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1283     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1284     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1285
1286     /* Autologin if configured */
1287     if (autologin_timeout == 0 || autologin_in_background)
1288     {
1289         if (autologin_guest)
1290             session = create_guest_session (seat);
1291         else if (autologin_username != NULL)
1292             session = create_user_session (seat, autologin_username);
1293       
1294         if (session)
1295         {
1296             if (seat->priv->session_to_activate)
1297                 g_object_unref (seat->priv->session_to_activate);
1298             seat->priv->session_to_activate = g_object_ref (session);
1299             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1300         }
1301
1302         /* Load in background if required */
1303         if (autologin_in_background && session)
1304         {
1305             DisplayServer *background_display_server;
1306
1307             background_display_server = create_display_server (seat);
1308             session_set_display_server (session, background_display_server);
1309             if (!display_server_start (background_display_server))
1310                 return FALSE;
1311
1312             /* Start a greeter as well */
1313             session = NULL;
1314         }
1315     }
1316
1317     /* Fallback to a greeter */
1318     if (!session)
1319     {
1320         Greeter *greeter_session;
1321
1322         greeter_session = create_greeter_session (seat);
1323         if (seat->priv->session_to_activate)
1324             g_object_unref (seat->priv->session_to_activate);
1325         seat->priv->session_to_activate = g_object_ref (greeter_session);
1326         session = SESSION (greeter_session);
1327
1328         if (autologin_timeout)
1329         {
1330             gchar *value;
1331
1332             value = g_strdup_printf ("%d", autologin_timeout);
1333             greeter_set_hint (greeter_session, "autologin-timeout", value);
1334             g_free (value);
1335             if (autologin_username)
1336                 greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1337             if (autologin_guest)
1338                 greeter_set_hint (greeter_session, "autologin-guest", "true");
1339         }
1340     }
1341
1342     /* Fail if can't start a session */
1343     if (!session)
1344     {
1345         seat_stop (seat);
1346         return FALSE;
1347     }
1348
1349     /* Start display server to show session on */
1350     session_set_display_server (session, display_server);
1351     if (!display_server_start (display_server))
1352         return FALSE;
1353
1354     return TRUE;
1355 }
1356
1357 static Greeter *
1358 seat_real_create_greeter_session (Seat *seat)
1359 {
1360     return greeter_new ();
1361 }
1362
1363 static Session *
1364 seat_real_create_session (Seat *seat)
1365 {
1366     return session_new ();
1367 }
1368
1369 static void
1370 seat_real_set_active_session (Seat *seat, Session *session)
1371 {
1372 }
1373
1374 static Session *
1375 seat_real_get_active_session (Seat *seat)
1376 {
1377     return NULL;
1378 }
1379
1380 static void
1381 seat_real_stop (Seat *seat)
1382 {
1383     GList *list, *link;
1384
1385     check_stopped (seat);
1386     if (seat->priv->stopped)
1387         return;
1388
1389     /* Stop all the display servers and sessions on the seat. Copy the list as
1390      * it might be modified if a display server / session stops during this loop */
1391     list = g_list_copy (seat->priv->display_servers);
1392     for (link = list; link; link = link->next)
1393         g_object_ref (link->data);
1394     for (link = list; link; link = link->next)
1395     {
1396         DisplayServer *display_server = link->data;
1397         if (!display_server_get_is_stopping (display_server))
1398         {
1399             g_debug ("Stopping display server");
1400             display_server_stop (display_server);
1401         }
1402     }
1403     g_list_free_full (list, g_object_unref);
1404     list = g_list_copy (seat->priv->sessions);
1405     for (link = list; link; link = link->next)
1406         g_object_ref (link->data);
1407     for (link = list; link; link = link->next)
1408     {
1409         Session *session = link->data;
1410         if (!session_get_is_stopping (session))
1411         {
1412             g_debug ("Stopping session");
1413             session_stop (session);
1414         }
1415     }
1416     g_list_free_full (list, g_object_unref);
1417 }
1418
1419 static void
1420 seat_init (Seat *seat)
1421 {
1422     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1423     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1424     seat->priv->share_display_server = TRUE;
1425 }
1426
1427 static void
1428 seat_finalize (GObject *object)
1429 {
1430     Seat *self;
1431     GList *link;
1432
1433     self = SEAT (object);
1434
1435     g_hash_table_unref (self->priv->properties);
1436     for (link = self->priv->display_servers; link; link = link->next)
1437     {
1438         DisplayServer *display_server = link->data;
1439         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1440     }  
1441     g_list_free_full (self->priv->display_servers, g_object_unref);
1442     for (link = self->priv->sessions; link; link = link->next)
1443     {
1444         Session *session = link->data;
1445         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1446     }
1447     g_list_free_full (self->priv->sessions, g_object_unref);
1448     if (self->priv->session_to_activate)
1449         g_object_unref (self->priv->session_to_activate);
1450
1451     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1452 }
1453
1454 static void
1455 seat_class_init (SeatClass *klass)
1456 {
1457     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1458
1459     klass->get_start_local_sessions = seat_real_get_start_local_sessions;
1460     klass->setup = seat_real_setup;
1461     klass->start = seat_real_start;
1462     klass->create_greeter_session = seat_real_create_greeter_session;
1463     klass->create_session = seat_real_create_session;
1464     klass->set_active_session = seat_real_set_active_session;
1465     klass->get_active_session = seat_real_get_active_session;
1466     klass->run_script = seat_real_run_script;
1467     klass->stop = seat_real_stop;
1468
1469     object_class->finalize = seat_finalize;
1470
1471     g_type_class_add_private (klass, sizeof (SeatPrivate));
1472
1473     signals[SESSION_ADDED] =
1474         g_signal_new ("session-added",
1475                       G_TYPE_FROM_CLASS (klass),
1476                       G_SIGNAL_RUN_LAST,
1477                       G_STRUCT_OFFSET (SeatClass, session_added),
1478                       NULL, NULL,
1479                       NULL,
1480                       G_TYPE_NONE, 1, SESSION_TYPE);
1481     signals[RUNNING_USER_SESSION] =
1482         g_signal_new ("running-user-session",
1483                       G_TYPE_FROM_CLASS (klass),
1484                       G_SIGNAL_RUN_LAST,
1485                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1486                       NULL, NULL,
1487                       NULL,
1488                       G_TYPE_NONE, 1, SESSION_TYPE);
1489     signals[SESSION_REMOVED] =
1490         g_signal_new ("session-removed",
1491                       G_TYPE_FROM_CLASS (klass),
1492                       G_SIGNAL_RUN_LAST,
1493                       G_STRUCT_OFFSET (SeatClass, session_removed),
1494                       NULL, NULL,
1495                       NULL,
1496                       G_TYPE_NONE, 1, SESSION_TYPE);
1497     signals[STOPPED] =
1498         g_signal_new ("stopped",
1499                       G_TYPE_FROM_CLASS (klass),
1500                       G_SIGNAL_RUN_LAST,
1501                       G_STRUCT_OFFSET (SeatClass, stopped),
1502                       NULL, NULL,
1503                       NULL,
1504                       G_TYPE_NONE, 0);
1505 }