]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Set XDG_SESSION_DESKTOP, this is used by pam_systemd
[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 last session set to active */
49     Session *active_session;
50
51     /* The session belonging to the active greeter user */
52     Session *next_session;
53
54     /* The session to set active when it starts */
55     Session *session_to_activate;
56   
57     /* TRUE once we have started */
58     gboolean started;
59
60     /* TRUE if stopping this seat (waiting for displays to stop) */
61     gboolean stopping;
62
63     /* TRUE if stopped */
64     gboolean stopped;
65 };
66
67 /* PAM services to use */
68 #define GREETER_SERVICE   "lightdm-greeter"
69 #define USER_SERVICE      "lightdm"
70 #define AUTOLOGIN_SERVICE "lightdm-autologin"
71
72 static void seat_logger_iface_init (LoggerInterface *iface);
73
74 G_DEFINE_TYPE_WITH_CODE (Seat, seat, G_TYPE_OBJECT,
75                          G_IMPLEMENT_INTERFACE (
76                              LOGGER_TYPE, seat_logger_iface_init));
77
78 typedef struct
79 {
80     const gchar *name;
81     GType type;
82 } SeatModule;
83 static GHashTable *seat_modules = NULL;
84
85 // FIXME: Make a get_display_server() that re-uses display servers if supported
86 static DisplayServer *create_display_server (Seat *seat, const gchar *session_type);
87 static Greeter *create_greeter_session (Seat *seat);
88 static void start_session (Seat *seat, Session *session);
89
90 void
91 seat_register_module (const gchar *name, GType type)
92 {
93     SeatModule *module;
94
95     if (!seat_modules)
96         seat_modules = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
97
98     g_debug ("Registered seat module %s", name);
99
100     module = g_malloc0 (sizeof (SeatModule));
101     module->name = g_strdup (name);
102     module->type = type;
103     g_hash_table_insert (seat_modules, g_strdup (name), module);
104 }
105
106 Seat *
107 seat_new (const gchar *module_name)
108 {
109     Seat *seat;
110     SeatModule *m = NULL;
111   
112     g_return_val_if_fail (module_name != NULL, NULL);
113
114     if (seat_modules)
115         m = g_hash_table_lookup (seat_modules, module_name);
116     if (!m)
117         return NULL;
118
119     seat = g_object_new (m->type, NULL);
120
121     return seat;
122 }
123
124 void
125 seat_set_property (Seat *seat, const gchar *name, const gchar *value)
126 {
127     g_return_if_fail (seat != NULL);
128     g_hash_table_insert (seat->priv->properties, g_strdup (name), g_strdup (value));
129 }
130
131 const gchar *
132 seat_get_string_property (Seat *seat, const gchar *name)
133 {
134     g_return_val_if_fail (seat != NULL, NULL);
135     return g_hash_table_lookup (seat->priv->properties, name);
136 }
137
138 gchar **
139 seat_get_string_list_property (Seat *seat, const gchar *name)
140 {
141     g_return_val_if_fail (seat != NULL, NULL);
142     return g_strsplit (g_hash_table_lookup (seat->priv->properties, name), ";", 0);
143 }
144
145 gboolean
146 seat_get_boolean_property (Seat *seat, const gchar *name)
147 {
148     return g_strcmp0 (seat_get_string_property (seat, name), "true") == 0;
149 }
150
151 gint
152 seat_get_integer_property (Seat *seat, const gchar *name)
153 {
154     const gchar *value;
155
156     value = seat_get_string_property (seat, name);
157     return value ? atoi (value) : 0;
158 }
159
160 const gchar *
161 seat_get_name (Seat *seat)
162 {
163     const gchar *name;
164
165     name = seat_get_string_property (seat, "xdg-seat");
166     if (name)
167         return name;
168
169     return "seat0";
170 }
171
172 void
173 seat_set_can_switch (Seat *seat, gboolean can_switch)
174 {
175     g_return_if_fail (seat != NULL);
176
177     seat->priv->can_switch = can_switch;
178 }
179
180 void
181 seat_set_share_display_server (Seat *seat, gboolean share_display_server)
182 {
183     g_return_if_fail (seat != NULL);
184
185     seat->priv->share_display_server = share_display_server;
186 }
187
188 gboolean
189 seat_start (Seat *seat)
190 {
191     g_return_val_if_fail (seat != NULL, FALSE);
192   
193     SEAT_GET_CLASS (seat)->setup (seat);
194     return SEAT_GET_CLASS (seat)->start (seat);
195 }
196
197 GList *
198 seat_get_sessions (Seat *seat)
199 {
200     g_return_val_if_fail (seat != NULL, NULL);
201     return seat->priv->sessions;
202 }
203
204 void
205 seat_set_active_session (Seat *seat, Session *session)
206 {
207     GList *link;
208
209     g_return_if_fail (seat != NULL);
210
211     SEAT_GET_CLASS (seat)->set_active_session (seat, session);
212
213     /* Stop any greeters */
214     for (link = seat->priv->sessions; link; link = link->next)
215     {
216         Session *s = link->data;
217
218         if (s == session || session_get_is_stopping (s))
219             continue;
220
221         if (IS_GREETER (s))
222         {
223             l_debug (seat, "Stopping greeter");
224             session_stop (s);
225         }
226     }
227
228     /* Lock previous sessions */
229     if (seat->priv->active_session)
230     {
231         if (session != seat->priv->active_session && !IS_GREETER (seat->priv->active_session))
232             session_lock (seat->priv->active_session);
233         g_object_unref (seat->priv->active_session);
234     }
235
236     session_activate (session);
237     seat->priv->active_session = g_object_ref (session);
238 }
239
240 Session *
241 seat_get_active_session (Seat *seat)
242 {
243     g_return_val_if_fail (seat != NULL, NULL);
244     return SEAT_GET_CLASS (seat)->get_active_session (seat);
245 }
246
247 Session *
248 seat_get_next_session (Seat *seat)
249 {
250     g_return_val_if_fail (seat != NULL, NULL);
251     return seat->priv->next_session;
252 }
253
254 gboolean
255 seat_get_can_switch (Seat *seat)
256 {
257     g_return_val_if_fail (seat != NULL, FALSE);
258     return seat->priv->can_switch;
259 }
260
261 gboolean
262 seat_get_allow_guest (Seat *seat)
263 {
264     g_return_val_if_fail (seat != NULL, FALSE);
265     return seat_get_boolean_property (seat, "allow-guest") && guest_account_is_installed ();
266 }
267
268 static gboolean
269 run_script (Seat *seat, DisplayServer *display_server, const gchar *script_name, User *user)
270 {
271     Process *script;
272     gboolean result = FALSE;
273   
274     script = process_new (NULL, NULL);
275
276     process_set_command (script, script_name);
277
278     /* Set POSIX variables */
279     process_set_clear_environment (script, TRUE);
280     process_set_env (script, "SHELL", "/bin/sh");
281
282     /* Variables required for regression tests */
283     if (g_getenv ("LIGHTDM_TEST_ROOT"))
284     {
285         process_set_env (script, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
286         process_set_env (script, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
287         process_set_env (script, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
288         process_set_env (script, "PATH", g_getenv ("PATH"));
289     }
290     else
291         process_set_env (script, "PATH", "/usr/local/bin:/usr/bin:/bin");
292
293     if (user)
294     {
295         process_set_env (script, "USER", user_get_name (user));
296         process_set_env (script, "LOGNAME", user_get_name (user));
297         process_set_env (script, "HOME", user_get_home_directory (user));
298     }
299     else
300         process_set_env (script, "HOME", "/");
301
302     SEAT_GET_CLASS (seat)->run_script (seat, display_server, script);
303
304     if (process_start (script, TRUE))
305     {
306         int exit_status;
307
308         exit_status = process_get_exit_status (script);
309         if (WIFEXITED (exit_status))
310         {
311             l_debug (seat, "Exit status of %s: %d", script_name, WEXITSTATUS (exit_status));
312             result = WEXITSTATUS (exit_status) == EXIT_SUCCESS;
313         }
314     }
315
316     g_object_unref (script);
317
318     return result;
319 }
320
321 static void
322 seat_real_run_script (Seat *seat, DisplayServer *display_server, Process *process)
323 {  
324 }
325
326 static void
327 emit_upstart_signal (const gchar *signal)
328 {
329     g_return_if_fail (signal != NULL);
330     g_return_if_fail (signal[0] != 0);
331
332     if (getuid () != 0)
333         return;
334
335     gchar *cmd = g_strdup_printf ("initctl -q emit %s DISPLAY_MANAGER=lightdm", signal);
336     g_spawn_command_line_async (cmd, NULL); /* OK if it fails, probably not installed */
337     g_free (cmd);
338 }
339
340 static void
341 check_stopped (Seat *seat)
342 {
343     if (seat->priv->stopping &&
344         !seat->priv->stopped &&
345         g_list_length (seat->priv->display_servers) == 0 &&
346         g_list_length (seat->priv->sessions) == 0)
347     {
348         seat->priv->stopped = TRUE;
349         l_debug (seat, "Stopped");
350         g_signal_emit (seat, signals[STOPPED], 0);
351     }
352 }
353
354 static gboolean
355 get_start_local_sessions (Seat *seat)
356 {
357     return SEAT_GET_CLASS (seat)->get_start_local_sessions (seat);
358 }
359
360 static void
361 display_server_stopped_cb (DisplayServer *display_server, Seat *seat)
362 {
363     const gchar *script;
364     GList *list, *link;
365     Session *active_session;
366
367     l_debug (seat, "Display server stopped");
368
369     /* Run a script right after stopping the display server */
370     script = seat_get_string_property (seat, "display-stopped-script");
371     if (script)
372         run_script (seat, NULL, script, NULL);
373
374     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
375     seat->priv->display_servers = g_list_remove (seat->priv->display_servers, display_server);
376
377     if (seat->priv->stopping || !seat->priv->started)
378     {
379         check_stopped (seat);
380         g_object_unref (display_server);
381         return;
382     }
383
384     /* Stop all sessions on this display server */
385     list = g_list_copy (seat->priv->sessions);
386     for (link = list; link; link = link->next)
387         g_object_ref (link->data);
388     for (link = list; link; link = link->next)
389     {
390         Session *session = link->data;
391
392         if (session_get_display_server (session) != display_server || session_get_is_stopping (session))
393             continue;
394
395         /* Stop seat if this is the only display server and it failed to start a greeter */
396         if (IS_GREETER (session) &&
397             !session_get_is_started (session) &&
398             g_list_length (seat->priv->display_servers) == 0)
399         {
400             l_debug (seat, "Stopping; greeter display server failed to start");
401             seat_stop (seat);
402         }
403
404         l_debug (seat, "Stopping session");
405         session_stop (session);
406     }
407     g_list_free_full (list, g_object_unref);
408
409     if (!seat->priv->stopping && get_start_local_sessions (seat))
410     {
411         /* If we were the active session, switch to a greeter */
412         active_session = seat_get_active_session (seat);
413         if (!active_session || session_get_display_server (active_session) == display_server)
414         {
415             l_debug (seat, "Active display server stopped, starting greeter");
416             if (!seat_switch_to_greeter (seat))
417             {
418                 l_debug (seat, "Stopping; failed to start a greeter");
419                 seat_stop (seat);
420             }
421         }
422     }
423
424     g_object_unref (display_server);
425 }
426
427 static gboolean
428 can_share_display_server (Seat *seat, DisplayServer *display_server)
429 {
430     return seat->priv->share_display_server && display_server_get_can_share (display_server);
431 }
432
433 static void
434 switch_to_greeter_from_failed_session (Seat *seat, Session *session)
435 {
436     Greeter *greeter_session;
437
438     greeter_session = create_greeter_session (seat);
439     if (session_get_is_guest (session))
440         greeter_set_hint (greeter_session, "select-guest", "true");
441     else
442         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
443     if (seat->priv->session_to_activate)
444         g_object_unref (seat->priv->session_to_activate);
445     seat->priv->session_to_activate = g_object_ref (greeter_session);
446
447     if (can_share_display_server (seat, session_get_display_server (session)))
448         session_set_display_server (SESSION (greeter_session), session_get_display_server (session));
449     else
450     {
451         DisplayServer *display_server;
452
453         display_server = create_display_server (seat, session_get_session_type (session));
454         session_set_display_server (session, display_server);
455         if (!display_server_start (display_server))
456         {
457             l_debug (seat, "Failed to start display server for greeter");
458             seat_stop (seat);
459         }
460     }
461
462     start_session (seat, SESSION (greeter_session));
463
464     /* Stop failed session */
465     session_stop (session);
466 }
467
468 static void
469 start_session (Seat *seat, Session *session)
470 {
471     /* Use system location for greeter log file */
472     if (IS_GREETER (session))
473     {
474         gchar *log_dir, *filename, *log_filename;
475
476         log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
477         filename = g_strdup_printf ("%s-greeter.log", display_server_get_name (session_get_display_server (session)));
478         log_filename = g_build_filename (log_dir, filename, NULL);
479         g_free (log_dir);
480         g_free (filename);
481         session_set_log_file (session, log_filename);
482         g_free (log_filename);
483     }
484
485     if (session_start (session))
486         return;
487
488     if (IS_GREETER (session))
489     {
490         l_debug (seat, "Failed to start greeter");
491         display_server_stop (session_get_display_server (session));
492         return;
493     }
494
495     l_debug (seat, "Failed to start session, starting greeter");
496     switch_to_greeter_from_failed_session (seat, session);
497 }
498
499 static void
500 run_session (Seat *seat, Session *session)
501 {
502     const gchar *script;
503
504     if (IS_GREETER (session))
505         script = seat_get_string_property (seat, "greeter-setup-script");
506     else
507         script = seat_get_string_property (seat, "session-setup-script");
508     if (script && !run_script (seat, session_get_display_server (session), script, session_get_user (session)))
509     {
510         l_debug (seat, "Switching to greeter due to failed setup script");
511         switch_to_greeter_from_failed_session (seat, session);
512         return;
513     }
514
515     if (!IS_GREETER (session))
516     {
517         g_signal_emit (seat, signals[RUNNING_USER_SESSION], 0, session);
518         emit_upstart_signal ("desktop-session-start");
519     }
520
521     session_run (session);
522
523     // FIXME: Wait until the session is ready
524
525     if (session == seat->priv->session_to_activate)
526     {
527         seat_set_active_session (seat, session);
528         g_object_unref (seat->priv->session_to_activate);
529         seat->priv->session_to_activate = NULL;
530     }
531     else if (seat->priv->active_session)
532     {
533         /* Multiple sessions can theoretically be on the same VT (especially
534            if using Mir).  If a new session appears on an existing active VT,
535            logind will mark it as active, while ConsoleKit will re-mark the
536            oldest session as active.  In either case, that may not be the
537            session that we want to be active.  So let's be explicit and
538            re-activate the correct session whenever a new session starts.
539            There's no harm to do this in seats that enforce separate VTs. */
540         session_activate (seat->priv->active_session);
541     }
542 }
543
544 static Session *
545 find_user_session (Seat *seat, const gchar *username, Session *ignore_session)
546 {
547     GList *link;
548
549     if (!username)
550         return NULL;
551
552     for (link = seat->priv->sessions; link; link = link->next)
553     {
554         Session *session = link->data;
555
556         if (session == ignore_session)
557             continue;
558
559         if (!session_get_is_stopping (session) && g_strcmp0 (session_get_username (session), username) == 0)
560             return session;
561     }
562
563     return NULL;
564 }
565
566 static void
567 greeter_active_username_changed_cb (Greeter *greeter, GParamSpec *pspec, Seat *seat)
568 {
569     Session *session;
570
571     session = find_user_session (seat, greeter_get_active_username (greeter), seat->priv->active_session);
572
573     if (seat->priv->next_session)
574         g_object_unref (seat->priv->next_session);
575     seat->priv->next_session = session ? g_object_ref (session) : NULL;
576
577     SEAT_GET_CLASS (seat)->set_next_session (seat, session);
578 }
579
580 static void
581 session_authentication_complete_cb (Session *session, Seat *seat)
582 {
583     if (session_get_is_authenticated (session))
584     {
585         Session *s;
586
587         s = find_user_session (seat, session_get_username (session), session);
588         if (s)
589         {
590             l_debug (seat, "Session authenticated, switching to existing user session");
591             seat_set_active_session (seat, s);
592             session_stop (session);
593         }
594         else
595         {
596             l_debug (seat, "Session authenticated, running command");
597             run_session (seat, session);
598         }
599     }
600     else if (!IS_GREETER (session))
601     {
602         l_debug (seat, "Switching to greeter due to failed authentication");
603         switch_to_greeter_from_failed_session (seat, session);
604     }
605     else
606     {
607         l_debug (seat, "Stopping session that failed authentication");
608         session_stop (session);
609     }
610 }
611
612 static void
613 session_stopped_cb (Session *session, Seat *seat)
614 {
615     DisplayServer *display_server;
616
617     l_debug (seat, "Session stopped");
618
619     g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
620     seat->priv->sessions = g_list_remove (seat->priv->sessions, session);
621     if (session == seat->priv->active_session)
622     {
623         g_object_unref (seat->priv->active_session);
624         seat->priv->active_session = NULL;
625     }
626     if (session == seat->priv->next_session)
627     {
628         g_object_unref (seat->priv->next_session);
629         seat->priv->next_session = NULL;
630     }
631     if (session == seat->priv->session_to_activate)
632     {
633         g_object_unref (seat->priv->session_to_activate);
634         seat->priv->session_to_activate = NULL;
635     }
636
637     display_server = session_get_display_server (session);
638     if (!display_server)
639     {
640         g_object_unref (session);
641         return;
642     }
643
644     /* Cleanup */
645     if (!IS_GREETER (session))
646     {
647         const gchar *script;
648         script = seat_get_string_property (seat, "session-cleanup-script");
649         if (script)
650             run_script (seat, display_server, script, session_get_user (session));
651     }
652
653     /* We were waiting for this session, but it didn't start :( */
654     // FIXME: Start a greeter on this?
655     if (session == seat->priv->session_to_activate)
656     {
657         g_object_unref (seat->priv->session_to_activate);
658         seat->priv->session_to_activate = NULL;
659     }
660
661     if (seat->priv->stopping)
662     {
663         check_stopped (seat);
664         g_object_unref (session);
665         return;
666     }
667
668     /* If this is the greeter session then re-use this display server */
669     if (IS_GREETER (session) &&
670         can_share_display_server (seat, display_server) &&
671         greeter_get_start_session (GREETER (session)))
672     {
673         GList *link;
674
675         for (link = seat->priv->sessions; link; link = link->next)
676         {
677             Session *s = link->data;
678
679             /* Skip this session and sessions on other display servers */
680             if (s == session || session_get_display_server (s) != display_server || session_get_is_stopping (s))
681                 continue;
682
683             if (session_get_is_authenticated (s))
684             {
685                 l_debug (seat, "Greeter stopped, running session");
686                 run_session (seat, s);
687             }
688             else
689             {
690                 l_debug (seat, "Greeter stopped, starting session authentication");
691                 start_session (seat, s);
692             }
693             break;
694         }
695     }
696     /* If this is the greeter and nothing else is running then stop the seat */
697     else if (IS_GREETER (session) &&
698         !greeter_get_start_session (GREETER (session)) &&
699         g_list_length (seat->priv->display_servers) == 1 &&
700         g_list_nth_data (seat->priv->display_servers, 0) == display_server)
701     {
702         l_debug (seat, "Stopping; failed to start a greeter");
703         seat_stop (seat);
704     }
705     /* If we were the active session, switch to a greeter */
706     else if (!IS_GREETER (session) && session == seat_get_active_session (seat))
707     {
708         l_debug (seat, "Active session stopped, starting greeter");
709         if (!seat_switch_to_greeter (seat))
710         {
711             l_debug (seat, "Stopping; failed to start a greeter");
712             seat_stop (seat);
713         }
714     }
715
716     /* Stop the display server if no-longer required */
717     if (display_server && !display_server_get_is_stopping (display_server))
718     {
719         GList *link;
720         int n_sessions = 0;
721
722         for (link = seat->priv->sessions; link; link = link->next)
723         {
724             Session *s = link->data;
725             if (s == session)
726                 continue;
727             if (session_get_display_server (s) == display_server)
728                 n_sessions++;
729         }
730         if (n_sessions == 0)
731         {
732             l_debug (seat, "Stopping display server, no sessions require it");
733             display_server_stop (display_server);
734         }
735     }
736
737     g_signal_emit (seat, signals[SESSION_REMOVED], 0, session);
738     g_object_unref (session);
739 }
740
741 static void
742 set_session_env (Session *session)
743 {
744     /* Connect using the session bus */
745     if (getuid () != 0)
746     {
747         if (g_getenv ("DBUS_SESSION_BUS_ADDRESS"))
748             session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
749         session_set_env (session, "LDM_BUS", "SESSION");
750         if (g_getenv ("LD_PRELOAD"))
751             session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
752         if (g_getenv ("LD_LIBRARY_PATH"))
753             session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
754         if (g_getenv ("PATH"))
755             session_set_env (session, "PATH", g_getenv ("PATH"));
756     }
757
758     /* Variables required for regression tests */
759     if (g_getenv ("LIGHTDM_TEST_ROOT"))
760     {
761         session_set_env (session, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
762         session_set_env (session, "DBUS_SYSTEM_BUS_ADDRESS", g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
763         session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
764         session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
765         session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
766         session_set_env (session, "GI_TYPELIB_PATH", g_getenv ("GI_TYPELIB_PATH"));
767     }
768 }
769
770 static Session *
771 create_session (Seat *seat, gboolean autostart)
772 {
773     Session *session;
774
775     session = SEAT_GET_CLASS (seat)->create_session (seat);
776     seat->priv->sessions = g_list_append (seat->priv->sessions, session);
777     if (autostart)
778         g_signal_connect (session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
779     g_signal_connect (session, "stopped", G_CALLBACK (session_stopped_cb), seat);
780
781     set_session_env (session);
782
783     g_signal_emit (seat, signals[SESSION_ADDED], 0, session);
784
785     return session;
786 }
787
788 static gchar **
789 get_session_argv (Seat *seat, SessionConfig *session_config, const gchar *session_wrapper)
790 {
791     gboolean result;
792     int argc;
793     gchar **argv, *path;
794     GError *error = NULL;
795
796     /* If configured, run sessions through a wrapper */
797     if (session_wrapper)
798     {
799         argv = g_malloc (sizeof (gchar *) * 3);
800         path = g_find_program_in_path (session_wrapper);
801         argv[0] = path ? path : g_strdup (session_wrapper);
802         argv[1] = g_strdup (session_config_get_command (session_config));
803         argv[2] = NULL;
804         return argv;
805     }
806
807     /* Split command into an array listing and make command absolute */
808     result = g_shell_parse_argv (session_config_get_command (session_config), &argc, &argv, &error);
809     if (error)
810         l_debug (seat, "Invalid session command '%s': %s", session_config_get_command (session_config), error->message);
811     g_clear_error (&error);
812     if (!result)
813         return NULL;
814     path = g_find_program_in_path (argv[0]);
815     if (path)
816     {
817         g_free (argv[0]);
818         argv[0] = path;
819     }
820   
821     return argv;
822 }
823
824 static SessionConfig *
825 find_session_config (Seat *seat, const gchar *sessions_dir, const gchar *session_name)
826 {
827     gchar **dirs;
828     SessionConfig *session_config = NULL;
829     int i;
830     GError *error = NULL;
831
832     g_return_val_if_fail (sessions_dir != NULL, NULL);
833     g_return_val_if_fail (session_name != NULL, NULL);
834
835     dirs = g_strsplit (sessions_dir, ":", -1);
836     for (i = 0; dirs[i]; i++)
837     {
838         gchar *filename, *path;
839
840         filename = g_strdup_printf ("%s.desktop", session_name);
841         path = g_build_filename (dirs[i], filename, NULL);
842         g_free (filename);
843         session_config = session_config_new_from_file (path, &error);
844         g_free (path);
845         if (session_config)
846             break;
847
848         if (dirs[i+1] == NULL)
849             l_debug (seat, "Failed to find session configuration %s", session_name);
850         g_clear_error (&error);
851     }
852     g_strfreev (dirs);
853
854     return session_config;
855 }
856
857 static void
858 configure_session (Session *session, SessionConfig *config, const gchar *session_name, const gchar *language)
859 {
860     const gchar *desktop_name;
861
862     session_set_session_type (session, session_config_get_session_type (config));
863     session_set_env (session, "XDG_SESSION_DESKTOP", session_name);
864     session_set_env (session, "DESKTOP_SESSION", session_name);
865     session_set_env (session, "GDMSESSION", session_name);
866     desktop_name = session_config_get_desktop_name (config);
867     if (desktop_name)
868         session_set_env (session, "XDG_CURRENT_DESKTOP", desktop_name);
869     if (language && language[0] != '\0')
870     {
871         session_set_env (session, "LANG", language);
872         session_set_env (session, "GDM_LANG", language);
873     }
874 }
875
876 static Session *
877 create_user_session (Seat *seat, const gchar *username, gboolean autostart)
878 {
879     User *user;
880     gchar *sessions_dir;
881     const gchar *session_name, *language;
882     SessionConfig *session_config;
883     Session *session = NULL;
884
885     l_debug (seat, "Creating user session");
886
887     /* Load user preferences */
888     user = accounts_get_user_by_name (username);
889     if (!user)
890     {
891         l_debug (seat, "Can't login unknown user '%s'", username);
892         return NULL;
893     }
894     session_name = user_get_xsession (user);
895     language = user_get_language (user);
896
897     if (!session_name)
898         session_name = seat_get_string_property (seat, "user-session");
899     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
900     session_config = find_session_config (seat, sessions_dir, session_name);
901     g_free (sessions_dir);
902     if (session_config)
903     {
904         gchar **argv;
905
906         session = create_session (seat, autostart);
907         configure_session (session, session_config, session_name, language);
908         session_set_username (session, username);
909         session_set_do_authenticate (session, TRUE);
910         argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
911         session_set_argv (session, argv);
912         g_strfreev (argv);
913         g_object_unref (session_config);
914     }
915     else
916         l_debug (seat, "Can't find session '%s'", seat_get_string_property (seat, "user-session"));
917
918     g_object_unref (user);
919
920     return session;
921 }
922
923 static void
924 prepend_argv (gchar ***argv, const gchar *value)
925 {
926     gchar **old_argv, **new_argv;
927     gint i;
928
929     old_argv = *argv;
930     new_argv = g_malloc (sizeof (gchar *) * (g_strv_length (*argv) + 2));
931     new_argv[0] = g_strdup (value);
932     for (i = 0; old_argv[i]; i++)
933         new_argv[i + 1] = old_argv[i];
934     new_argv[i + 1] = NULL;
935
936     g_free (*argv);
937     *argv = new_argv;
938 }
939
940 static Session *
941 create_guest_session (Seat *seat, const gchar *session_name)
942 {
943     const gchar *guest_wrapper;
944     gchar *sessions_dir, **argv;
945     SessionConfig *session_config;
946     Session *session;
947
948     if (!session_name)
949         session_name = seat_get_string_property (seat, "guest-session");
950     if (!session_name)
951         session_name = seat_get_string_property (seat, "user-session");
952     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
953     session_config = find_session_config (seat, sessions_dir, session_name);
954     g_free (sessions_dir);
955     if (!session_config)
956     {
957         l_debug (seat, "Can't find session '%s'", seat_get_string_property (seat, "user-session"));
958         return NULL;
959     }
960
961     session = create_session (seat, TRUE);
962     configure_session (session, session_config, session_name, NULL);
963     session_set_do_authenticate (session, TRUE);
964     session_set_is_guest (session, TRUE);
965     argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
966     guest_wrapper = seat_get_string_property (seat, "guest-wrapper");
967     if (guest_wrapper)
968     {
969         gchar *path;
970         path = g_find_program_in_path (guest_wrapper);
971         prepend_argv (&argv, path ? path : guest_wrapper);
972         g_free (path);
973     }
974
975     session_set_argv (session, argv);
976     g_strfreev (argv);
977     g_object_unref (session_config);
978   
979     return session;
980 }
981
982 static Session *
983 greeter_create_session_cb (Greeter *greeter, Seat *seat)
984 {
985     Session *session;
986
987     session = create_session (seat, FALSE);
988     session_set_session_type (session, session_get_session_type (SESSION (greeter)));
989     session_set_display_server (session, session_get_display_server (SESSION (greeter)));
990
991     return g_object_ref (session);
992 }
993
994 static gboolean
995 greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *session_name, Seat *seat)
996 {
997     Session *session, *existing_session;
998     const gchar *username;
999     DisplayServer *display_server;
1000
1001     /* Get the session to use */
1002     if (greeter_get_guest_authenticated (greeter))
1003     {
1004         session = create_guest_session (seat, session_name);
1005         if (!session)
1006             return FALSE;
1007         session_set_pam_service (session, AUTOLOGIN_SERVICE);
1008     }
1009     else
1010     {
1011         const gchar *language = NULL;
1012         SessionConfig *session_config;
1013         User *user;
1014         gchar *sessions_dir = NULL;
1015         gchar **argv;
1016
1017         session = greeter_get_authentication_session (greeter);
1018   
1019         /* Get session command to run */
1020         switch (type)
1021         {
1022         case SESSION_TYPE_LOCAL:
1023             sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
1024             break;
1025         case SESSION_TYPE_REMOTE:
1026             sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
1027             break;
1028         }
1029
1030         /* Load user preferences */
1031         user = session_get_user (session);
1032         if (user)
1033         {
1034             if (!session_name)
1035                 session_name = user_get_xsession (user);
1036             language = user_get_language (user);
1037         }
1038
1039         if (!session_name)
1040             session_name = seat_get_string_property (seat, "user-session");
1041         if (user)
1042             user_set_xsession (session_get_user (session), session_name);
1043
1044         session_config = find_session_config (seat, sessions_dir, session_name);
1045         g_free (sessions_dir);
1046         if (!session_config)
1047         {
1048             l_debug (seat, "Can't find session '%s'", seat_get_string_property (seat, "user-session"));
1049             return FALSE;
1050         }
1051
1052         configure_session (session, session_config, session_name, language);
1053         argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
1054         session_set_argv (session, argv);
1055         g_strfreev (argv);
1056         g_object_unref (session_config);
1057     }
1058
1059     /* Switch to this session when it is ready */
1060     if (seat->priv->session_to_activate)
1061         g_object_unref (seat->priv->session_to_activate);
1062     seat->priv->session_to_activate = g_object_ref (session);
1063
1064     /* Return to existing session if it is open */
1065     username = session_get_username (session);
1066     existing_session = find_user_session (seat, username, NULL);
1067     if (existing_session && session != existing_session)
1068     {
1069         l_debug (seat, "Returning to existing user session %s", username);
1070         session_stop (session);
1071         session_unlock (existing_session);
1072         seat_set_active_session (seat, existing_session);
1073         return TRUE;
1074     }
1075
1076     /* If can re-use the display server, stop the greeter first */
1077     display_server = session_get_display_server (SESSION (greeter));
1078     if (can_share_display_server (seat, display_server) &&
1079         strcmp (display_server_get_session_type (display_server), session_get_session_type (session)) == 0)
1080     {
1081         l_debug (seat, "Stopping greeter; display server will be re-used for user session");
1082
1083         /* Run on the same display server after the greeter has stopped */
1084         session_set_display_server (session, display_server);
1085
1086         /* Stop the greeter */
1087         session_stop (SESSION (greeter));
1088
1089         return TRUE;
1090     }
1091     /* Otherwise start a new display server for this session */
1092     else
1093     {
1094         display_server = create_display_server (seat, session_get_session_type (session));
1095         session_set_display_server (session, display_server);
1096         if (!display_server_start (display_server))
1097         {
1098             l_debug (seat, "Failed to start display server for new session");
1099             return FALSE;
1100         }
1101
1102         return TRUE;
1103     }
1104 }
1105
1106 static Greeter *
1107 create_greeter_session (Seat *seat)
1108 {
1109     gchar *sessions_dir, **argv;
1110     SessionConfig *session_config;
1111     Greeter *greeter_session;
1112     const gchar *greeter_wrapper;
1113
1114     l_debug (seat, "Creating greeter session");
1115
1116     sessions_dir = config_get_string (config_get_instance (), "LightDM", "greeters-directory");
1117     session_config = find_session_config (seat, sessions_dir, seat_get_string_property (seat, "greeter-session"));
1118     g_free (sessions_dir);
1119     if (!session_config)
1120         return NULL;
1121
1122     argv = get_session_argv (seat, session_config, NULL);
1123     greeter_wrapper = seat_get_string_property (seat, "greeter-wrapper");
1124     if (greeter_wrapper)
1125     {
1126         gchar *path;
1127         path = g_find_program_in_path (greeter_wrapper);
1128         prepend_argv (&argv, path ? path : greeter_wrapper);
1129         g_free (path);
1130     }
1131
1132     greeter_session = SEAT_GET_CLASS (seat)->create_greeter_session (seat);
1133     session_set_session_type (SESSION (greeter_session), session_config_get_session_type (session_config));
1134     seat->priv->sessions = g_list_append (seat->priv->sessions, SESSION (greeter_session));
1135     g_signal_connect (greeter_session, "notify::active-username", G_CALLBACK (greeter_active_username_changed_cb), seat);
1136     g_signal_connect (greeter_session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
1137     g_signal_connect (greeter_session, "stopped", G_CALLBACK (session_stopped_cb), seat);
1138   
1139     set_session_env (SESSION (greeter_session));
1140     session_set_env (SESSION (greeter_session), "XDG_SESSION_CLASS", "greeter");
1141
1142     session_set_pam_service (SESSION (greeter_session), GREETER_SERVICE);
1143     if (getuid () == 0)
1144     {
1145         gchar *greeter_user;      
1146         greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
1147         session_set_username (SESSION (greeter_session), greeter_user);
1148         g_free (greeter_user);
1149     }
1150     else
1151     {
1152         /* In test mode run the greeter as ourself */
1153         session_set_username (SESSION (greeter_session), user_get_name (accounts_get_current_user ()));
1154     }
1155     session_set_argv (SESSION (greeter_session), argv);
1156     g_strfreev (argv);
1157
1158     greeter_set_pam_services (greeter_session, USER_SERVICE, AUTOLOGIN_SERVICE);
1159     g_signal_connect (greeter_session, "create-session", G_CALLBACK (greeter_create_session_cb), seat);
1160     g_signal_connect (greeter_session, "start-session", G_CALLBACK (greeter_start_session_cb), seat);
1161
1162     /* Set hints to greeter */
1163     greeter_set_hint (greeter_session, "default-session", seat_get_string_property (seat, "user-session"));
1164     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
1165     greeter_set_hint (greeter_session, "hide-users", seat_get_boolean_property (seat, "greeter-hide-users") ? "true" : "false");
1166     greeter_set_hint (greeter_session, "show-manual-login", seat_get_boolean_property (seat, "greeter-show-manual-login") ? "true" : "false");
1167     greeter_set_hint (greeter_session, "show-remote-login", seat_get_boolean_property (seat, "greeter-show-remote-login") ? "true" : "false");
1168     greeter_set_hint (greeter_session, "has-guest-account", seat_get_allow_guest (seat) && seat_get_boolean_property (seat, "greeter-allow-guest") ? "true" : "false");
1169
1170     g_object_unref (session_config);
1171
1172     return greeter_session;
1173 }
1174
1175 static Session *
1176 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
1177 {
1178     GList *link;
1179
1180     for (link = seat->priv->sessions; link; link = link->next)
1181     {
1182         Session *session = link->data;
1183         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
1184             return session;
1185     }
1186
1187     return NULL;
1188 }
1189
1190 static void
1191 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
1192 {
1193     const gchar *script;
1194     Session *session;
1195
1196     /* Run setup script */
1197     script = seat_get_string_property (seat, "display-setup-script");
1198     if (script && !run_script (seat, display_server, script, NULL))
1199     {
1200         l_debug (seat, "Stopping display server due to failed setup script");
1201         display_server_stop (display_server);
1202         return;
1203     }
1204
1205     /* Stop if don't need to run a session */
1206     if (!get_start_local_sessions (seat))
1207         return;
1208
1209     emit_upstart_signal ("login-session-start");
1210
1211     /* Start the session waiting for this display server */
1212     session = find_session_for_display_server (seat, display_server);
1213     if (session)
1214     {
1215         if (session_get_is_authenticated (session))
1216         {
1217             l_debug (seat, "Display server ready, running session");
1218             run_session (seat, session);
1219         }
1220         else
1221         {
1222             l_debug (seat, "Display server ready, starting session authentication");
1223             start_session (seat, session);
1224         }
1225     }
1226     else
1227     {
1228         l_debug (seat, "Stopping not required display server");
1229         display_server_stop (display_server);
1230     }
1231 }
1232
1233 static DisplayServer *
1234 create_display_server (Seat *seat, const gchar *session_type)
1235 {
1236     DisplayServer *display_server;
1237
1238     l_debug (seat, "Creating display server of type %s", session_type);
1239
1240     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat, session_type);
1241     if (!display_server)
1242         return NULL;
1243
1244     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1245     g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
1246     g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_stopped_cb), seat);
1247
1248     return display_server;
1249 }
1250
1251 static Greeter *
1252 find_greeter_session (Seat *seat)
1253 {
1254     GList *link;
1255
1256     for (link = seat->priv->sessions; link; link = link->next)
1257     {
1258         Session *session = link->data;
1259         if (!session_get_is_stopping (session) && IS_GREETER (session))
1260             return GREETER (session);
1261     }
1262
1263     return NULL;
1264 }
1265
1266 gboolean
1267 seat_switch_to_greeter (Seat *seat)
1268 {
1269     Greeter *greeter_session;
1270     DisplayServer *display_server;
1271
1272     g_return_val_if_fail (seat != NULL, FALSE);
1273
1274     if (!seat->priv->can_switch)
1275         return FALSE;
1276
1277     /* Switch to greeter if one open (shouldn't be though) */
1278     greeter_session = find_greeter_session (seat);
1279     if (greeter_session)
1280     {
1281         l_debug (seat, "Switching to existing greeter");
1282         seat_set_active_session (seat, SESSION (greeter_session));
1283         return TRUE;
1284     }
1285
1286     greeter_session = create_greeter_session (seat);
1287     if (!greeter_session)
1288         return FALSE;
1289
1290     if (seat->priv->session_to_activate)
1291         g_object_unref (seat->priv->session_to_activate);
1292     seat->priv->session_to_activate = g_object_ref (greeter_session);
1293
1294     display_server = create_display_server (seat, session_get_session_type (SESSION (greeter_session)));
1295     session_set_display_server (SESSION (greeter_session), display_server);
1296     if (!display_server_start (display_server))
1297         return FALSE;
1298
1299     return TRUE;
1300 }
1301
1302 static void
1303 switch_authentication_complete_cb (Session *session, Seat *seat)
1304 {
1305     Greeter *greeter_session;
1306     DisplayServer *display_server;
1307
1308     /* If authenticated, then unlock existing session or start new one */
1309     if (session_get_is_authenticated (session))
1310     {
1311         Session *s;
1312
1313         s = find_user_session (seat, session_get_username (session), session);
1314         if (s)
1315         {
1316             l_debug (seat, "Session authenticated, switching to existing user session");
1317             session_unlock (s);
1318             seat_set_active_session (seat, s);
1319             session_stop (session);
1320         }
1321         else
1322         {
1323             l_debug (seat, "Session authenticated, starting display server");
1324             if (seat->priv->session_to_activate)
1325                 g_object_unref (seat->priv->session_to_activate);
1326             seat->priv->session_to_activate = g_object_ref (session);
1327             display_server = create_display_server (seat, session_get_session_type (session));
1328             session_set_display_server (session, display_server);
1329             display_server_start (display_server);
1330         }
1331
1332         return;
1333     }
1334
1335     l_debug (seat, "Switching to greeter to authenticate session");
1336
1337     session_stop (session);
1338
1339     greeter_session = create_greeter_session (seat);
1340     if (session_get_is_guest (session))
1341         greeter_set_hint (greeter_session, "select-guest", "true");
1342     else
1343         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
1344     if (seat->priv->session_to_activate)
1345         g_object_unref (seat->priv->session_to_activate);
1346     seat->priv->session_to_activate = g_object_ref (greeter_session);
1347
1348     display_server = create_display_server (seat, session_get_session_type (SESSION (greeter_session)));
1349     session_set_display_server (SESSION (greeter_session), display_server);
1350     display_server_start (display_server);
1351 }
1352
1353 gboolean
1354 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1355 {
1356     Session *session;
1357
1358     g_return_val_if_fail (seat != NULL, FALSE);
1359     g_return_val_if_fail (username != NULL, FALSE);
1360
1361     if (!seat->priv->can_switch)
1362         return FALSE;
1363
1364     /* If we're already on this session, then ignore */
1365     session = find_user_session (seat, username, NULL);
1366     if (session && session == seat->priv->active_session)
1367         return TRUE;
1368
1369     l_debug (seat, "Switching to user %s", username);
1370
1371     /* Attempt to authenticate them */
1372     session = create_user_session (seat, username, FALSE);
1373     g_signal_connect (session, "authentication-complete", G_CALLBACK (switch_authentication_complete_cb), seat);
1374     session_set_pam_service (session, USER_SERVICE);
1375     session_start (session);
1376
1377     return FALSE;
1378 }
1379
1380 static Session *
1381 find_guest_session (Seat *seat)
1382 {
1383     GList *link;
1384
1385     for (link = seat->priv->sessions; link; link = link->next)
1386     {
1387         Session *session = link->data;
1388         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1389             return session;
1390     }
1391
1392     return NULL;
1393 }
1394
1395 gboolean
1396 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1397 {
1398     Session *session;
1399     DisplayServer *display_server;
1400
1401     g_return_val_if_fail (seat != NULL, FALSE);
1402
1403     if (!seat->priv->can_switch || !seat_get_allow_guest (seat))
1404         return FALSE;
1405
1406     /* Switch to session if one open */
1407     session = find_guest_session (seat);
1408     if (session)
1409     {
1410         l_debug (seat, "Switching to existing guest account %s", session_get_username (session));
1411         seat_set_active_session (seat, session);
1412         return TRUE;
1413     }
1414
1415     session = create_guest_session (seat, session_name);
1416     if (!session)
1417         return FALSE;
1418
1419     display_server = create_display_server (seat, session_get_session_type (session));
1420
1421     if (seat->priv->session_to_activate)
1422         g_object_unref (seat->priv->session_to_activate);
1423     seat->priv->session_to_activate = g_object_ref (session);
1424     session_set_pam_service (session, AUTOLOGIN_SERVICE);
1425     session_set_display_server (session, display_server);
1426
1427     return display_server_start (display_server);
1428 }
1429
1430 gboolean
1431 seat_lock (Seat *seat, const gchar *username)
1432 {
1433     Greeter *greeter_session;
1434     DisplayServer *display_server;
1435
1436     g_return_val_if_fail (seat != NULL, FALSE);
1437
1438     if (!seat->priv->can_switch)
1439         return FALSE;
1440
1441     l_debug (seat, "Locking");
1442
1443     /* Switch to greeter if one open (shouldn't be though) */
1444     greeter_session = find_greeter_session (seat);
1445     if (greeter_session)
1446     {
1447         l_debug (seat, "Switching to existing greeter");
1448         seat_set_active_session (seat, SESSION (greeter_session));
1449         return TRUE;
1450     }
1451
1452     greeter_session = create_greeter_session (seat);
1453     if (!greeter_session)
1454         return FALSE;
1455
1456     display_server = create_display_server (seat, session_get_session_type (SESSION (greeter_session)));
1457
1458     if (seat->priv->session_to_activate)
1459         g_object_unref (seat->priv->session_to_activate);
1460     seat->priv->session_to_activate = g_object_ref (greeter_session);
1461     greeter_set_hint (greeter_session, "lock-screen", "true");
1462     if (username)
1463         greeter_set_hint (greeter_session, "select-user", username);
1464     session_set_display_server (SESSION (greeter_session), display_server);
1465
1466     return display_server_start (display_server);
1467 }
1468
1469 void
1470 seat_stop (Seat *seat)
1471 {
1472     g_return_if_fail (seat != NULL);
1473
1474     if (seat->priv->stopping)
1475         return;
1476
1477     l_debug (seat, "Stopping");
1478     seat->priv->stopping = TRUE;
1479     SEAT_GET_CLASS (seat)->stop (seat);
1480 }
1481
1482 gboolean
1483 seat_get_is_stopping (Seat *seat)
1484 {
1485     g_return_val_if_fail (seat != NULL, FALSE);
1486     return seat->priv->stopping;
1487 }
1488
1489 static gboolean
1490 seat_real_get_start_local_sessions (Seat *seat)
1491 {
1492     return TRUE;
1493 }
1494
1495 static void
1496 seat_real_setup (Seat *seat)
1497 {
1498 }
1499
1500 static gboolean
1501 seat_real_start (Seat *seat)
1502 {
1503     const gchar *autologin_username;
1504     int autologin_timeout;
1505     gboolean autologin_guest;
1506     gboolean autologin_in_background;
1507     Session *session = NULL, *background_session = NULL;
1508
1509     l_debug (seat, "Starting");
1510
1511     /* If this display server doesn't have a session running on it, just start it */
1512     if (!get_start_local_sessions (seat))
1513     {
1514         DisplayServer *display_server;
1515         display_server = create_display_server (seat, "x"); // FIXME: Not necessarily an X seat, but not sure what to put here
1516         return display_server_start (display_server);
1517     }
1518
1519     /* Get autologin settings */
1520     autologin_username = seat_get_string_property (seat, "autologin-user");
1521     if (g_strcmp0 (autologin_username, "") == 0)
1522         autologin_username = NULL;
1523     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1524     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1525     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1526
1527     /* Autologin if configured */
1528     if (autologin_timeout == 0 || autologin_in_background)
1529     {
1530         if (autologin_guest)
1531             session = create_guest_session (seat, NULL);
1532         else if (autologin_username != NULL)
1533             session = create_user_session (seat, autologin_username, TRUE);
1534
1535         if (session)
1536             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1537
1538         /* Load in background if required */
1539         if (autologin_in_background && session)
1540         {
1541             background_session = session;
1542             session = NULL;
1543         }
1544       
1545         if (session)
1546         {
1547             DisplayServer *display_server;
1548
1549             if (seat->priv->session_to_activate)
1550                 g_object_unref (seat->priv->session_to_activate);
1551             seat->priv->session_to_activate = g_object_ref (session);
1552
1553             display_server = create_display_server (seat, session_get_session_type (session));
1554             session_set_display_server (session, display_server);
1555             if (!display_server || !display_server_start (display_server))
1556             {
1557                 l_debug (seat, "Can't create display server for automatic login");
1558                 session_stop (session);
1559                 if (display_server)
1560                     display_server_stop (display_server);
1561                 session = NULL;
1562             }
1563         }
1564     }
1565
1566     /* Fallback to a greeter */
1567     if (!session)
1568     {
1569         Greeter *greeter_session;
1570         DisplayServer *display_server;
1571
1572         greeter_session = create_greeter_session (seat);
1573         if (!greeter_session)
1574         {
1575             l_debug (seat, "Failed to create greeter session");
1576             return FALSE;
1577         }
1578
1579         if (seat->priv->session_to_activate)
1580             g_object_unref (seat->priv->session_to_activate);
1581         seat->priv->session_to_activate = g_object_ref (greeter_session);
1582         session = SESSION (greeter_session);
1583
1584         if (autologin_timeout)
1585         {
1586             gchar *value;
1587
1588             value = g_strdup_printf ("%d", autologin_timeout);
1589             greeter_set_hint (greeter_session, "autologin-timeout", value);
1590             g_free (value);
1591             if (autologin_username)
1592                 greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1593             if (autologin_guest)
1594                 greeter_set_hint (greeter_session, "autologin-guest", "true");
1595         }
1596
1597         display_server = create_display_server (seat, session_get_session_type (session));
1598         session_set_display_server (session, display_server);
1599         if (!display_server || !display_server_start (display_server))
1600         {
1601             l_debug (seat, "Can't create display server for greeter");
1602             session_stop (session);
1603             if (display_server)
1604                 display_server_stop (display_server);
1605             session = NULL;
1606         }
1607     }
1608
1609     /* Fail if can't start a session */
1610     if (!session)
1611     {
1612         seat_stop (seat);
1613         return FALSE;
1614     }
1615
1616     /* Start background session */
1617     if (background_session)
1618     {
1619         DisplayServer *background_display_server;
1620
1621         background_display_server = create_display_server (seat, session_get_session_type (background_session));
1622         session_set_display_server (background_session, background_display_server);
1623         if (!display_server_start (background_display_server))
1624             l_warning (seat, "Failed to start display server for background session");
1625     }
1626
1627     seat->priv->started = TRUE;
1628     return TRUE;
1629 }
1630
1631 static Greeter *
1632 seat_real_create_greeter_session (Seat *seat)
1633 {
1634     return greeter_new ();
1635 }
1636
1637 static Session *
1638 seat_real_create_session (Seat *seat)
1639 {
1640     return session_new ();
1641 }
1642
1643 static void
1644 seat_real_set_active_session (Seat *seat, Session *session)
1645 {
1646 }
1647
1648 static void
1649 seat_real_set_next_session (Seat *seat, Session *session)
1650 {
1651 }
1652
1653 static Session *
1654 seat_real_get_active_session (Seat *seat)
1655 {
1656     return NULL;
1657 }
1658
1659 static void
1660 seat_real_stop (Seat *seat)
1661 {
1662     GList *list, *link;
1663
1664     check_stopped (seat);
1665     if (seat->priv->stopped)
1666         return;
1667
1668     /* Stop all the display servers and sessions on the seat. Copy the list as
1669      * it might be modified if a display server / session stops during this loop */
1670     list = g_list_copy (seat->priv->display_servers);
1671     for (link = list; link; link = link->next)
1672         g_object_ref (link->data);
1673     for (link = list; link; link = link->next)
1674     {
1675         DisplayServer *display_server = link->data;
1676         if (!display_server_get_is_stopping (display_server))
1677         {
1678             l_debug (seat, "Stopping display server");
1679             display_server_stop (display_server);
1680         }
1681     }
1682     g_list_free_full (list, g_object_unref);
1683     list = g_list_copy (seat->priv->sessions);
1684     for (link = list; link; link = link->next)
1685         g_object_ref (link->data);
1686     for (link = list; link; link = link->next)
1687     {
1688         Session *session = link->data;
1689         if (!session_get_is_stopping (session))
1690         {
1691             l_debug (seat, "Stopping session");
1692             session_stop (session);
1693         }
1694     }
1695     g_list_free_full (list, g_object_unref);
1696 }
1697
1698 static void
1699 seat_init (Seat *seat)
1700 {
1701     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1702     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1703     seat->priv->share_display_server = TRUE;
1704 }
1705
1706 static void
1707 seat_finalize (GObject *object)
1708 {
1709     Seat *self;
1710     GList *link;
1711
1712     self = SEAT (object);
1713
1714     g_hash_table_unref (self->priv->properties);
1715     for (link = self->priv->display_servers; link; link = link->next)
1716     {
1717         DisplayServer *display_server = link->data;
1718         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1719     }  
1720     g_list_free_full (self->priv->display_servers, g_object_unref);
1721     for (link = self->priv->sessions; link; link = link->next)
1722     {
1723         Session *session = link->data;
1724         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1725     }
1726     g_list_free_full (self->priv->sessions, g_object_unref);
1727     if (self->priv->active_session)
1728         g_object_unref (self->priv->active_session);
1729     if (self->priv->next_session)
1730         g_object_unref (self->priv->next_session);
1731     if (self->priv->session_to_activate)
1732         g_object_unref (self->priv->session_to_activate);
1733
1734     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1735 }
1736
1737 static void
1738 seat_class_init (SeatClass *klass)
1739 {
1740     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1741
1742     klass->get_start_local_sessions = seat_real_get_start_local_sessions;
1743     klass->setup = seat_real_setup;
1744     klass->start = seat_real_start;
1745     klass->create_greeter_session = seat_real_create_greeter_session;
1746     klass->create_session = seat_real_create_session;
1747     klass->set_active_session = seat_real_set_active_session;
1748     klass->get_active_session = seat_real_get_active_session;
1749     klass->set_next_session = seat_real_set_next_session;
1750     klass->run_script = seat_real_run_script;
1751     klass->stop = seat_real_stop;
1752
1753     object_class->finalize = seat_finalize;
1754
1755     g_type_class_add_private (klass, sizeof (SeatPrivate));
1756
1757     signals[SESSION_ADDED] =
1758         g_signal_new ("session-added",
1759                       G_TYPE_FROM_CLASS (klass),
1760                       G_SIGNAL_RUN_LAST,
1761                       G_STRUCT_OFFSET (SeatClass, session_added),
1762                       NULL, NULL,
1763                       NULL,
1764                       G_TYPE_NONE, 1, SESSION_TYPE);
1765     signals[RUNNING_USER_SESSION] =
1766         g_signal_new ("running-user-session",
1767                       G_TYPE_FROM_CLASS (klass),
1768                       G_SIGNAL_RUN_LAST,
1769                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1770                       NULL, NULL,
1771                       NULL,
1772                       G_TYPE_NONE, 1, SESSION_TYPE);
1773     signals[SESSION_REMOVED] =
1774         g_signal_new ("session-removed",
1775                       G_TYPE_FROM_CLASS (klass),
1776                       G_SIGNAL_RUN_LAST,
1777                       G_STRUCT_OFFSET (SeatClass, session_removed),
1778                       NULL, NULL,
1779                       NULL,
1780                       G_TYPE_NONE, 1, SESSION_TYPE);
1781     signals[STOPPED] =
1782         g_signal_new ("stopped",
1783                       G_TYPE_FROM_CLASS (klass),
1784                       G_SIGNAL_RUN_LAST,
1785                       G_STRUCT_OFFSET (SeatClass, stopped),
1786                       NULL, NULL,
1787                       NULL,
1788                       G_TYPE_NONE, 0);
1789 }
1790
1791 static gint
1792 seat_real_logprefix (Logger *self, gchar *buf, gulong buflen)
1793 {
1794     Seat *seat = SEAT (self);
1795     const gchar *name = seat_get_string_property (seat, "seat-name");
1796     if (name)
1797         return g_snprintf (buf, buflen, "Seat %s: ", name);
1798     else
1799         return g_snprintf (buf, buflen, "Seat: ");
1800 }
1801
1802 static void
1803 seat_logger_iface_init (LoggerInterface *iface)
1804 {
1805     iface->logprefix = &seat_real_logprefix;
1806 }