-
Notifications
You must be signed in to change notification settings - Fork 138
/
seat.c
1993 lines (1681 loc) · 64.7 KB
/
seat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010-2011 Robert Ancell.
* Author: Robert Ancell <[email protected]>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
* license.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "seat.h"
#include "configuration.h"
#include "guest-account.h"
#include "greeter-session.h"
#include "session-config.h"
enum {
SESSION_ADDED,
RUNNING_USER_SESSION,
SESSION_REMOVED,
STOPPED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
typedef struct
{
/* XDG name for this seat */
gchar *name;
/* Configuration for this seat */
GHashTable *properties;
/* TRUE if this seat can run multiple sessions at once */
gboolean supports_multi_session;
/* TRUE if display server can be shared for sessions */
gboolean share_display_server;
/* The display servers on this seat */
GList *display_servers;
/* The sessions on this seat */
GList *sessions;
/* The last session set to active */
Session *active_session;
/* The session belonging to the active greeter user */
Session *next_session;
/* The session to set active when it starts */
Session *session_to_activate;
/* TRUE once we have started */
gboolean started;
/* TRUE if stopping this seat (waiting for displays to stop) */
gboolean stopping;
/* TRUE if stopped */
gboolean stopped;
/* The greeter to be started to replace the current one */
GreeterSession *replacement_greeter;
} SeatPrivate;
static void seat_logger_iface_init (LoggerInterface *iface);
G_DEFINE_TYPE_WITH_CODE (Seat, seat, G_TYPE_OBJECT,
G_ADD_PRIVATE (Seat)
G_IMPLEMENT_INTERFACE (
LOGGER_TYPE, seat_logger_iface_init))
typedef struct
{
gchar *name;
GType type;
} SeatModule;
static GHashTable *seat_modules = NULL;
// FIXME: Make a get_display_server() that re-uses display servers if supported
static DisplayServer *create_display_server (Seat *seat, Session *session);
static gboolean start_display_server (Seat *seat, DisplayServer *display_server);
static GreeterSession *create_greeter_session (Seat *seat);
static void start_session (Seat *seat, Session *session);
static void
free_seat_module (gpointer data)
{
SeatModule *module = data;
g_free (module->name);
g_free (module);
}
void
seat_register_module (const gchar *name, GType type)
{
if (!seat_modules)
seat_modules = g_hash_table_new_full (g_str_hash, g_str_equal, free_seat_module, NULL);
g_debug ("Registered seat module %s", name);
SeatModule *module = g_malloc0 (sizeof (SeatModule));
module->name = g_strdup (name);
module->type = type;
g_hash_table_insert (seat_modules, g_strdup (name), module);
}
Seat *
seat_new (const gchar *module_name)
{
g_return_val_if_fail (module_name != NULL, NULL);
SeatModule *m = NULL;
if (seat_modules)
m = g_hash_table_lookup (seat_modules, module_name);
if (!m)
return NULL;
return g_object_new (m->type, NULL);
}
void
seat_set_name (Seat *seat, const gchar *name)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_if_fail (seat != NULL);
g_free (priv->name);
priv->name = g_strdup (name);
}
void
seat_set_property (Seat *seat, const gchar *name, const gchar *value)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_if_fail (seat != NULL);
g_hash_table_insert (priv->properties, g_strdup (name), g_strdup (value));
}
const gchar *
seat_get_string_property (Seat *seat, const gchar *name)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, NULL);
return g_hash_table_lookup (priv->properties, name);
}
gchar **
seat_get_string_list_property (Seat *seat, const gchar *name)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, NULL);
return g_strsplit (g_hash_table_lookup (priv->properties, name), ";", 0);
}
gboolean
seat_get_boolean_property (Seat *seat, const gchar *name)
{
const gchar *value = seat_get_string_property (seat, name);
if (!value)
return FALSE;
/* Count the number of non-whitespace characters */
gint length = 0;
for (gint i = 0; value[i]; i++)
if (!g_ascii_isspace (value[i]))
length = i + 1;
return strncmp (value, "true", MAX (length, 4)) == 0;
}
gint
seat_get_integer_property (Seat *seat, const gchar *name)
{
const gchar *value = seat_get_string_property (seat, name);
return value ? atoi (value) : 0;
}
const gchar *
seat_get_name (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, NULL);
return priv->name;
}
void
seat_set_supports_multi_session (Seat *seat, gboolean supports_multi_session)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_if_fail (seat != NULL);
priv->supports_multi_session = supports_multi_session;
}
void
seat_set_share_display_server (Seat *seat, gboolean share_display_server)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_if_fail (seat != NULL);
priv->share_display_server = share_display_server;
}
gboolean
seat_start (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, FALSE);
l_debug (seat, "Starting");
SEAT_GET_CLASS (seat)->setup (seat);
priv->started = SEAT_GET_CLASS (seat)->start (seat);
return priv->started;
}
GList *
seat_get_sessions (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, NULL);
return priv->sessions;
}
static gboolean
set_greeter_idle (gpointer greeter)
{
greeter_idle (GREETER (greeter));
return FALSE;
}
void
seat_set_active_session (Seat *seat, Session *session)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_if_fail (seat != NULL);
SEAT_GET_CLASS (seat)->set_active_session (seat, session);
/* Stop any greeters */
for (GList *link = priv->sessions; link; link = link->next)
{
Session *s = link->data;
if (s == session || session_get_is_stopping (s))
continue;
if (IS_GREETER_SESSION (s))
{
Greeter *greeter = greeter_session_get_greeter (GREETER_SESSION (s));
if (greeter_get_resettable (greeter))
{
if (priv->active_session == s)
{
l_debug (seat, "Idling greeter");
/* Do this in an idle callback, because we might very well
be in the middle of responding to a START_SESSION
request by a greeter. So they won't expect an IDLE
call during that. Plus, this isn't time-sensitive. */
g_idle_add (set_greeter_idle, greeter);
}
}
else
{
l_debug (seat, "Stopping greeter");
session_stop (s);
}
}
}
/* Lock previous sessions */
if (priv->active_session && session != priv->active_session && !IS_GREETER_SESSION (priv->active_session))
session_lock (priv->active_session);
session_activate (session);
g_clear_object (&priv->active_session);
priv->active_session = g_object_ref (session);
}
Session *
seat_get_active_session (Seat *seat)
{
g_return_val_if_fail (seat != NULL, NULL);
return SEAT_GET_CLASS (seat)->get_active_session (seat);
}
Session *
seat_get_next_session (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, NULL);
return priv->next_session;
}
/**
* Obtains the active session which lightdm expects to be active.
*
* This function is different from seat_get_active_session() in that the
* later (in the case of local seats) dynamically finds the session that is
* really active (based on the active VT), whereas this function returns the
* session that lightdm activated last by itself, which may not be the actual
* active session (i.e. VT changes).
*/
Session *
seat_get_expected_active_session (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, NULL);
return priv->active_session;
}
/**
* Sets the active session which lightdm expects to be active.
*
* This function is different from seat_set_active_session() in that the
* later performs an actual session activation, whereas this function just
* updates the active session after the session has been activated by some
* means external to lightdm (i.e. VT changes).
*/
void
seat_set_externally_activated_session (Seat *seat, Session *session)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_if_fail (seat != NULL);
g_clear_object (&priv->active_session);
priv->active_session = g_object_ref (session);
}
Session *
seat_find_session_by_login1_id (Seat *seat, const gchar *login1_session_id)
{
SeatPrivate *priv = seat_get_instance_private (seat);
for (GList *session_link = priv->sessions; session_link; session_link = session_link->next)
{
Session *session = session_link->data;
if (g_strcmp0 (login1_session_id, session_get_login1_session_id (session)) == 0)
return session;
}
return NULL;
}
gboolean
seat_get_can_switch (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
g_return_val_if_fail (seat != NULL, FALSE);
return seat_get_boolean_property (seat, "allow-user-switching") && priv->supports_multi_session;
}
gboolean
seat_get_allow_guest (Seat *seat)
{
g_return_val_if_fail (seat != NULL, FALSE);
return seat_get_boolean_property (seat, "allow-guest") && guest_account_is_installed ();
}
static gboolean
run_script (Seat *seat, DisplayServer *display_server, const gchar *script_name, User *user)
{
g_autoptr(Process) script = process_new (NULL, NULL);
process_set_command (script, script_name);
/* Set POSIX variables */
process_set_clear_environment (script, TRUE);
process_set_env (script, "SHELL", "/bin/sh");
if (g_getenv ("LD_PRELOAD"))
process_set_env (script, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
if (g_getenv ("LD_LIBRARY_PATH"))
process_set_env (script, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
if (g_getenv ("PATH"))
process_set_env (script, "PATH", g_getenv ("PATH"));
/* Variables required for regression tests */
if (g_getenv ("LIGHTDM_TEST_ROOT"))
process_set_env (script, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
process_set_env (script, "XDG_SEAT", seat_get_name (seat));
if (user)
{
process_set_env (script, "USER", user_get_name (user));
process_set_env (script, "LOGNAME", user_get_name (user));
process_set_env (script, "HOME", user_get_home_directory (user));
}
else
process_set_env (script, "HOME", "/");
SEAT_GET_CLASS (seat)->run_script (seat, display_server, script);
gboolean result = FALSE;
if (process_start (script, TRUE))
{
int exit_status = process_get_exit_status (script);
if (WIFEXITED (exit_status))
{
l_debug (seat, "Exit status of %s: %d", script_name, WEXITSTATUS (exit_status));
result = WEXITSTATUS (exit_status) == EXIT_SUCCESS;
}
}
return result;
}
static void
seat_real_run_script (Seat *seat, DisplayServer *display_server, Process *process)
{
}
static void
emit_upstart_signal (const gchar *signal)
{
g_return_if_fail (signal != NULL);
g_return_if_fail (signal[0] != 0);
if (getuid () != 0)
return;
/* OK if it fails, probably not installed or not running upstart */
g_autoptr(GSubprocess) p = g_subprocess_new (G_SUBPROCESS_FLAGS_STDERR_SILENCE, NULL, "initctl", "-q", "emit", signal, "DISPLAY_MANAGER=lightdm", NULL);
}
static void
check_stopped (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
if (priv->stopping &&
!priv->stopped &&
g_list_length (priv->display_servers) == 0 &&
g_list_length (priv->sessions) == 0)
{
priv->stopped = TRUE;
l_debug (seat, "Stopped");
g_signal_emit (seat, signals[STOPPED], 0);
}
}
static void
display_server_stopped_cb (DisplayServer *display_server, Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
l_debug (seat, "Display server stopped");
/* Run a script right after stopping the display server */
const gchar *script = seat_get_string_property (seat, "display-stopped-script");
if (script)
run_script (seat, NULL, script, NULL);
g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
priv->display_servers = g_list_remove (priv->display_servers, display_server);
if (priv->stopping || !priv->started)
{
check_stopped (seat);
g_object_unref (display_server);
return;
}
/* Stop all sessions on this display server */
GList *list = g_list_copy (priv->sessions);
for (GList *link = list; link; link = link->next)
g_object_ref (link->data);
for (GList *link = list; link; link = link->next)
{
Session *session = link->data;
if (session_get_display_server (session) != display_server || session_get_is_stopping (session))
continue;
gboolean is_failed_greeter = IS_GREETER_SESSION (session) && !session_get_is_started (session);
l_debug (seat, "Stopping session");
session_stop (session);
/* Stop seat if this is the only display server and it failed to start a greeter */
if (is_failed_greeter &&
g_list_length (priv->display_servers) == 0)
{
l_debug (seat, "Stopping; greeter display server failed to start");
seat_stop (seat);
}
}
g_list_free_full (list, g_object_unref);
if (!priv->stopping)
{
/* If we were the active session, switch to a greeter */
Session *active_session = seat_get_active_session (seat);
if (!active_session || session_get_display_server (active_session) == display_server)
{
l_debug (seat, "Active display server stopped, starting greeter");
if (!seat_switch_to_greeter (seat))
{
l_debug (seat, "Stopping; failed to start a greeter");
seat_stop (seat);
}
}
}
g_object_unref (display_server);
}
static gboolean
can_share_display_server (Seat *seat, DisplayServer *display_server)
{
SeatPrivate *priv = seat_get_instance_private (seat);
return priv->share_display_server && display_server_get_can_share (display_server);
}
static GreeterSession *
find_greeter_session (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
for (GList *link = priv->sessions; link; link = link->next)
{
Session *session = link->data;
if (!session_get_is_stopping (session) && IS_GREETER_SESSION (session))
return GREETER_SESSION (session);
}
return NULL;
}
static GreeterSession *
find_resettable_greeter (Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
for (GList *link = priv->sessions; link; link = link->next)
{
Session *session = link->data;
if (!session_get_is_stopping (session) && IS_GREETER_SESSION (session) &&
greeter_get_resettable (greeter_session_get_greeter (GREETER_SESSION (session))))
return GREETER_SESSION (session);
}
return NULL;
}
static void
set_greeter_hints (Seat *seat, Greeter *greeter)
{
greeter_clear_hints (greeter);
greeter_set_hint (greeter, "default-session", seat_get_string_property (seat, "user-session"));
greeter_set_hint (greeter, "hide-users", seat_get_boolean_property (seat, "greeter-hide-users") ? "true" : "false");
greeter_set_hint (greeter, "show-manual-login", seat_get_boolean_property (seat, "greeter-show-manual-login") ? "true" : "false");
greeter_set_hint (greeter, "show-remote-login", seat_get_boolean_property (seat, "greeter-show-remote-login") ? "true" : "false");
greeter_set_hint (greeter, "has-guest-account", seat_get_allow_guest (seat) && seat_get_boolean_property (seat, "greeter-allow-guest") ? "true" : "false");
}
static void
switch_to_greeter_from_failed_session (Seat *seat, Session *session)
{
SeatPrivate *priv = seat_get_instance_private (seat);
/* Switch to greeter if one open */
GreeterSession *greeter_session = find_resettable_greeter (seat);
gboolean existing = FALSE;
if (greeter_session)
{
l_debug (seat, "Switching to existing greeter");
set_greeter_hints (seat, greeter_session_get_greeter (greeter_session));
existing = TRUE;
}
else
{
greeter_session = create_greeter_session (seat);
}
Greeter *greeter = greeter_session_get_greeter (greeter_session);
if (session_get_is_guest (session))
greeter_set_hint (greeter, "select-guest", "true");
else
greeter_set_hint (greeter, "select-user", session_get_username (session));
if (existing)
{
greeter_reset (greeter);
seat_set_active_session (seat, SESSION (greeter_session));
}
else
{
g_clear_object (&priv->session_to_activate);
priv->session_to_activate = g_object_ref (SESSION (greeter_session));
if (can_share_display_server (seat, session_get_display_server (session)))
session_set_display_server (SESSION (greeter_session), session_get_display_server (session));
else
{
DisplayServer *display_server = create_display_server (seat, session);
session_set_display_server (session, display_server);
if (!start_display_server (seat, display_server))
{
l_debug (seat, "Failed to start display server for greeter");
seat_stop (seat);
}
}
start_session (seat, SESSION (greeter_session));
}
/* Stop failed session */
session_stop (session);
}
static void
start_session (Seat *seat, Session *session)
{
/* Use system location for greeter log file */
if (IS_GREETER_SESSION (session))
{
g_autofree gchar *log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
g_autofree gchar *filename = g_strdup_printf ("%s-greeter.log", seat_get_name (seat));
g_autofree gchar *log_filename = g_build_filename (log_dir, filename, NULL);
gboolean backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs");
session_set_log_file (session, log_filename, backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : LOG_MODE_APPEND);
}
if (session_start (session))
return;
if (IS_GREETER_SESSION (session))
{
l_debug (seat, "Failed to start greeter");
display_server_stop (session_get_display_server (session));
return;
}
l_debug (seat, "Failed to start session, starting greeter");
switch_to_greeter_from_failed_session (seat, session);
}
static void
run_session (Seat *seat, Session *session)
{
SeatPrivate *priv = seat_get_instance_private (seat);
const gchar *script;
if (IS_GREETER_SESSION (session))
script = seat_get_string_property (seat, "greeter-setup-script");
else
script = seat_get_string_property (seat, "session-setup-script");
if (script && !run_script (seat, session_get_display_server (session), script, session_get_user (session)))
{
l_debug (seat, "Switching to greeter due to failed setup script");
switch_to_greeter_from_failed_session (seat, session);
return;
}
if (!IS_GREETER_SESSION (session))
{
g_signal_emit (seat, signals[RUNNING_USER_SESSION], 0, session);
emit_upstart_signal ("desktop-session-start");
}
session_run (session);
// FIXME: Wait until the session is ready
if (session == priv->session_to_activate)
{
seat_set_active_session (seat, session);
g_clear_object (&priv->session_to_activate);
}
else if (priv->active_session)
{
/* Multiple sessions can theoretically be on the same VT (especially
if using Mir). If a new session appears on an existing active VT,
logind will mark it as active, while ConsoleKit will re-mark the
oldest session as active. In either case, that may not be the
session that we want to be active. So let's be explicit and
re-activate the correct session whenever a new session starts.
There's no harm to do this in seats that enforce separate VTs. */
session_activate (priv->active_session);
}
}
static Session *
find_user_session (Seat *seat, const gchar *username, Session *ignore_session)
{
SeatPrivate *priv = seat_get_instance_private (seat);
if (!username)
return NULL;
for (GList *link = priv->sessions; link; link = link->next)
{
Session *session = link->data;
if (session == ignore_session)
continue;
if (!session_get_is_stopping (session) && g_strcmp0 (session_get_username (session), username) == 0)
return session;
}
return NULL;
}
static void
greeter_active_username_changed_cb (Greeter *greeter, GParamSpec *pspec, Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
Session *session = find_user_session (seat, greeter_get_active_username (greeter), priv->active_session);
g_clear_object (&priv->next_session);
priv->next_session = session ? g_object_ref (session) : NULL;
SEAT_GET_CLASS (seat)->set_next_session (seat, session);
}
static void
session_authentication_complete_cb (Session *session, Seat *seat)
{
if (session_get_is_authenticated (session))
{
Session *s = find_user_session (seat, session_get_username (session), session);
if (s)
{
l_debug (seat, "Session authenticated, switching to existing user session");
seat_set_active_session (seat, s);
session_stop (session);
}
else
{
l_debug (seat, "Session authenticated, running command");
run_session (seat, session);
}
}
else if (!IS_GREETER_SESSION (session))
{
l_debug (seat, "Switching to greeter due to failed authentication");
switch_to_greeter_from_failed_session (seat, session);
}
else
{
l_debug (seat, "Stopping session that failed authentication");
session_stop (session);
}
}
static void
session_stopped_cb (Session *session, Seat *seat)
{
SeatPrivate *priv = seat_get_instance_private (seat);
l_debug (seat, "Session stopped");
g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
priv->sessions = g_list_remove (priv->sessions, session);
if (session == priv->active_session)
g_clear_object (&priv->active_session);
if (session == priv->next_session)
g_clear_object (&priv->next_session);
if (session == priv->session_to_activate)
g_clear_object (&priv->session_to_activate);
DisplayServer *display_server = session_get_display_server (session);
/* Cleanup */
if (!IS_GREETER_SESSION (session))
{
const gchar *script = seat_get_string_property (seat, "session-cleanup-script");
if (script)
run_script (seat, display_server, script, session_get_user (session));
}
/* We were waiting for this session, but it didn't start :( */
// FIXME: Start a greeter on this?
if (session == priv->session_to_activate)
g_clear_object (&priv->session_to_activate);
if (priv->stopping)
{
check_stopped (seat);
g_object_unref (session);
return;
}
/* If there is a pending replacement greeter, start it */
if (IS_GREETER_SESSION (session) && priv->replacement_greeter)
{
GreeterSession *replacement_greeter = priv->replacement_greeter;
priv->replacement_greeter = NULL;
if (session_get_is_authenticated (SESSION (replacement_greeter)))
{
l_debug (seat, "Greeter stopped, running session");
run_session (seat, SESSION (replacement_greeter));
}
else
{
l_debug (seat, "Greeter stopped, starting session authentication");
start_session (seat, SESSION (replacement_greeter));
}
g_object_unref (replacement_greeter);
}
/* If this is the greeter session then re-use this display server */
else if (IS_GREETER_SESSION (session) &&
can_share_display_server (seat, display_server) &&
greeter_get_start_session (greeter_session_get_greeter (GREETER_SESSION (session))))
{
for (GList *link = priv->sessions; link; link = link->next)
{
Session *s = link->data;
/* Skip this session and sessions on other display servers */
if (s == session || session_get_display_server (s) != display_server || session_get_is_stopping (s))
continue;
if (session_get_is_authenticated (s))
{
l_debug (seat, "Greeter stopped, running session");
run_session (seat, s);
}
else
{
l_debug (seat, "Greeter stopped, starting session authentication");
start_session (seat, s);
}
break;
}
}
/* If this is the greeter and nothing else is running then stop the seat */
else if (IS_GREETER_SESSION (session) &&
!greeter_get_start_session (greeter_session_get_greeter (GREETER_SESSION (session))) &&
g_list_length (priv->display_servers) == 1 &&
g_list_nth_data (priv->display_servers, 0) == display_server)
{
l_debug (seat, "Stopping; failed to start a greeter");
seat_stop (seat);
}
/* If we were the active session, switch to a greeter */
else if (!IS_GREETER_SESSION (session) && session == seat_get_active_session (seat))
{
l_debug (seat, "Active session stopped, starting greeter");
if (!seat_switch_to_greeter (seat))
{
l_debug (seat, "Stopping; failed to start a greeter");
seat_stop (seat);
}
}
g_signal_emit (seat, signals[SESSION_REMOVED], 0, session);
g_object_unref (session);
/* Stop the display server if no-longer required */
if (display_server && !display_server_get_is_stopping (display_server) &&
!SEAT_GET_CLASS (seat)->display_server_is_used (seat, display_server))
{
l_debug (seat, "Stopping display server, no sessions require it");
display_server_stop (display_server);
}
}
static void
set_session_env (Session *session)
{
/* Connect using the session bus */
if (getuid () != 0)
{
if (g_getenv ("DBUS_SESSION_BUS_ADDRESS"))
session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
session_set_env (session, "LDM_BUS", "SESSION");
}
/* Variables required for regression tests */
if (g_getenv ("LIGHTDM_TEST_ROOT"))
{
session_set_env (session, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
session_set_env (session, "DBUS_SYSTEM_BUS_ADDRESS", g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
session_set_env (session, "GI_TYPELIB_PATH", g_getenv ("GI_TYPELIB_PATH"));
}
if (g_getenv ("LD_PRELOAD"))
session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
if (g_getenv ("LD_LIBRARY_PATH"))
session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
}
static Session *
create_session (Seat *seat, gboolean autostart)
{
SeatPrivate *priv = seat_get_instance_private (seat);
Session *session = SEAT_GET_CLASS (seat)->create_session (seat);
priv->sessions = g_list_append (priv->sessions, session);
if (autostart)
g_signal_connect (session, SESSION_SIGNAL_AUTHENTICATION_COMPLETE, G_CALLBACK (session_authentication_complete_cb), seat);
g_signal_connect (session, SESSION_SIGNAL_STOPPED, G_CALLBACK (session_stopped_cb), seat);
set_session_env (session);
g_signal_emit (seat, signals[SESSION_ADDED], 0, session);
return session;
}
static gchar **
get_session_argv (Seat *seat, SessionConfig *session_config, const gchar *session_wrapper)
{
/* If configured, run sessions through a wrapper */
if (session_wrapper)
{
gchar **argv = g_malloc (sizeof (gchar *) * 3);
g_autofree gchar *path = g_find_program_in_path (session_wrapper);
argv[0] = path ? g_steal_pointer (&path) : g_strdup (session_wrapper);
argv[1] = g_strdup (session_config_get_command (session_config));
argv[2] = NULL;
return argv;
}
/* Split command into an array listing and make command absolute */
int argc;
g_auto(GStrv) argv = NULL;
g_autoptr(GError) error = NULL;
gboolean result = g_shell_parse_argv (session_config_get_command (session_config), &argc, &argv, &error);
if (error)
l_debug (seat, "Invalid session command '%s': %s", session_config_get_command (session_config), error->message);
if (!result)
return NULL;
g_autofree gchar *path = g_find_program_in_path (argv[0]);
if (path)
{
g_free (argv[0]);
argv[0] = g_steal_pointer (&path);
}
return g_steal_pointer (&argv);
}
static SessionConfig *
find_session_config (Seat *seat, const gchar *sessions_dir, const gchar *session_name)
{
g_return_val_if_fail (sessions_dir != NULL, NULL);
g_return_val_if_fail (session_name != NULL, NULL);
g_auto(GStrv) dirs = g_strsplit (sessions_dir, ":", -1);
for (int i = 0; dirs[i]; i++)
{
const gchar *default_session_type = "x";
if (dirs[i] != NULL && g_str_has_suffix (dirs[i], "/wayland-sessions") == TRUE)
default_session_type = "wayland";
g_autofree gchar *filename = g_strdup_printf ("%s.desktop", session_name);
g_autofree gchar *path = g_build_filename (dirs[i], filename, NULL);
g_autoptr(GError) error = NULL;
SessionConfig *session_config = session_config_new_from_file (path, default_session_type, &error);
if (session_config)
return session_config;
}
l_debug (seat, "Failed to find session configuration %s", session_name);
return NULL;
}
static void
configure_session (Session *session, SessionConfig *config, const gchar *session_name, const gchar *language)
{
session_set_config (session, config);
session_set_env (session, "XDG_SESSION_DESKTOP", session_name);
session_set_env (session, "DESKTOP_SESSION", session_name);
session_set_env (session, "GDMSESSION", session_name);
gchar **desktop_names = session_config_get_desktop_names (config);
if (desktop_names)
{
g_autofree gchar *value = g_strjoinv (":", desktop_names);
session_set_env (session, "XDG_CURRENT_DESKTOP", value);
}
if (language && language[0] != '\0')
{
session_set_env (session, "LANG", language);
session_set_env (session, "GDM_LANG", language);
}
}
static Session *
create_user_session (Seat *seat, const gchar *username, gboolean autostart)
{
l_debug (seat, "Creating user session");
/* Load user preferences */