/
gsm-manager.c
4136 lines (3438 loc) · 138 KB
/
gsm-manager.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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2007 Novell, Inc.
* Copyright (C) 2008 Red Hat, Inc.
* Copyright (C) 2008 William Jon McCann <jmccann@redhat.com>
*
* 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 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <gtk/gtk.h> /* for logout dialog */
#include <gio/gio.h> /* for gsettings */
#include <gdk/gdkx.h>
#include "gsm-manager.h"
#include "gsm-manager-glue.h"
#include "gsm-store.h"
#include "gsm-inhibitor.h"
#include "gsm-presence.h"
#include "gsm-xsmp-client.h"
#include "gsm-dbus-client.h"
#include "gsm-autostart-app.h"
#include "gsm-util.h"
#include "mdm.h"
#include "gsm-logout-dialog.h"
#include "gsm-inhibit-dialog.h"
#include "gsm-consolekit.h"
#ifdef HAVE_SYSTEMD
#include "gsm-systemd.h"
#endif
#include "gsm-session-save.h"
#define GSM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSM_TYPE_MANAGER, GsmManagerPrivate))
#define GSM_MANAGER_DBUS_PATH "/org/gnome/SessionManager"
#define GSM_MANAGER_DBUS_NAME "org.gnome.SessionManager"
#define GSM_MANAGER_PHASE_TIMEOUT 30 /* seconds */
/* In the exit phase, all apps were already given the chance to inhibit the session end
* At that stage we don't want to wait much for apps to respond, we want to exit, and fast.
*/
#define GSM_MANAGER_EXIT_PHASE_TIMEOUT 1 /* seconds */
#define MDM_FLEXISERVER_COMMAND "mdmflexiserver"
#define MDM_FLEXISERVER_ARGS "--startnew Standard"
#define GDM_FLEXISERVER_COMMAND "gdmflexiserver"
#define GDM_FLEXISERVER_ARGS "--startnew Standard"
#define LOCKDOWN_SCHEMA "org.mate.lockdown"
#define KEY_LOCK_DISABLE "disable-lock-screen"
#define KEY_LOG_OUT_DISABLE "disable-log-out"
#define KEY_USER_SWITCH_DISABLE "disable-user-switching"
#define SESSION_SCHEMA "org.mate.session"
#define KEY_IDLE_DELAY "idle-delay"
#define KEY_AUTOSAVE "auto-save-session"
#define SCREENSAVER_SCHEMA "org.mate.screensaver"
#define KEY_SLEEP_LOCK "lock-enabled"
#ifdef __GNUC__
#define UNUSED_VARIABLE __attribute__ ((unused))
#else
#define UNUSED_VARIABLE
#endif
typedef enum
{
GSM_MANAGER_LOGOUT_NONE,
GSM_MANAGER_LOGOUT_LOGOUT,
GSM_MANAGER_LOGOUT_REBOOT,
GSM_MANAGER_LOGOUT_REBOOT_INTERACT,
GSM_MANAGER_LOGOUT_REBOOT_MDM,
GSM_MANAGER_LOGOUT_SHUTDOWN,
GSM_MANAGER_LOGOUT_SHUTDOWN_INTERACT,
GSM_MANAGER_LOGOUT_SHUTDOWN_MDM
} GsmManagerLogoutType;
typedef struct {
gboolean failsafe;
GsmStore *clients;
GsmStore *inhibitors;
GsmStore *apps;
GsmPresence *presence;
/* Current status */
GsmManagerPhase phase;
guint phase_timeout_id;
GSList *pending_apps;
GsmManagerLogoutMode logout_mode;
GSList *query_clients;
guint query_timeout_id;
/* This is used for GSM_MANAGER_PHASE_END_SESSION only at the moment,
* since it uses a sublist of all running client that replied in a
* specific way */
GSList *next_query_clients;
/* This is the action that will be done just before we exit */
GsmManagerLogoutType logout_type;
GtkWidget *inhibit_dialog;
/* List of clients which were disconnected due to disabled condition
* and shouldn't be automatically restarted */
GSList *condition_clients;
GSettings *settings_session;
GSettings *settings_lockdown;
GSettings *settings_screensaver;
DBusGProxy *bus_proxy;
DBusGConnection *connection;
} GsmManagerPrivate;
enum {
PROP_0,
PROP_CLIENT_STORE,
PROP_FAILSAFE
};
enum {
PHASE_CHANGED,
CLIENT_ADDED,
CLIENT_REMOVED,
INHIBITOR_ADDED,
INHIBITOR_REMOVED,
SESSION_RUNNING,
SESSION_OVER,
LAST_SIGNAL
};
static guint signals [LAST_SIGNAL] = { 0 };
static void gsm_manager_class_init (GsmManagerClass *klass);
static void gsm_manager_init (GsmManager *manager);
static void gsm_manager_finalize (GObject *object);
static gboolean _log_out_is_locked_down (GsmManager *manager);
static gboolean _switch_user_is_locked_down (GsmManager *manager);
static void _handle_client_end_session_response (GsmManager *manager,
GsmClient *client,
gboolean is_ok,
gboolean do_last,
gboolean cancel,
const char *reason);
static gboolean auto_save_is_enabled (GsmManager *manager);
static void maybe_save_session (GsmManager *manager);
static gpointer manager_object = NULL;
G_DEFINE_TYPE_WITH_PRIVATE (GsmManager, gsm_manager, G_TYPE_OBJECT)
GQuark
gsm_manager_error_quark (void)
{
static GQuark ret = 0;
if (ret == 0) {
ret = g_quark_from_static_string ("gsm_manager_error");
}
return ret;
}
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
GType
gsm_manager_error_get_type (void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
ENUM_ENTRY (GSM_MANAGER_ERROR_GENERAL, "GeneralError"),
ENUM_ENTRY (GSM_MANAGER_ERROR_NOT_IN_INITIALIZATION, "NotInInitialization"),
ENUM_ENTRY (GSM_MANAGER_ERROR_NOT_IN_RUNNING, "NotInRunning"),
ENUM_ENTRY (GSM_MANAGER_ERROR_ALREADY_REGISTERED, "AlreadyRegistered"),
ENUM_ENTRY (GSM_MANAGER_ERROR_NOT_REGISTERED, "NotRegistered"),
ENUM_ENTRY (GSM_MANAGER_ERROR_INVALID_OPTION, "InvalidOption"),
ENUM_ENTRY (GSM_MANAGER_ERROR_LOCKED_DOWN, "LockedDown"),
{ 0, 0, 0 }
};
g_assert (GSM_MANAGER_NUM_ERRORS == G_N_ELEMENTS (values) - 1);
etype = g_enum_register_static ("GsmManagerError", values);
}
return etype;
}
static gboolean
_debug_client (const char *id,
GsmClient *client,
GsmManager *manager)
{
g_debug ("GsmManager: Client %s", gsm_client_peek_id (client));
return FALSE;
}
static void
debug_clients (GsmManager *manager)
{
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
gsm_store_foreach (priv->clients,
(GsmStoreFunc)_debug_client,
manager);
}
static gboolean
_debug_inhibitor (const char *id,
GsmInhibitor *inhibitor,
GsmManager *manager)
{
g_debug ("GsmManager: Inhibitor app:%s client:%s bus-name:%s reason:%s",
gsm_inhibitor_peek_app_id (inhibitor),
gsm_inhibitor_peek_client_id (inhibitor),
gsm_inhibitor_peek_bus_name (inhibitor),
gsm_inhibitor_peek_reason (inhibitor));
return FALSE;
}
static void
debug_inhibitors (GsmManager *manager)
{
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
gsm_store_foreach (priv->inhibitors,
(GsmStoreFunc)_debug_inhibitor,
manager);
}
static gboolean
_find_by_cookie (const char *id,
GsmInhibitor *inhibitor,
guint *cookie_ap)
{
guint cookie_b;
cookie_b = gsm_inhibitor_peek_cookie (inhibitor);
return (*cookie_ap == cookie_b);
}
static gboolean
_find_by_startup_id (const char *id,
GsmClient *client,
const char *startup_id_a)
{
const char *startup_id_b;
startup_id_b = gsm_client_peek_startup_id (client);
if (IS_STRING_EMPTY (startup_id_b)) {
return FALSE;
}
return (strcmp (startup_id_a, startup_id_b) == 0);
}
static void
app_condition_changed (GsmApp *app,
gboolean condition,
GsmManager *manager)
{
GsmClient *client;
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
g_debug ("GsmManager: app:%s condition changed condition:%d",
gsm_app_peek_id (app),
condition);
client = (GsmClient *)gsm_store_find (priv->clients,
(GsmStoreFunc)_find_by_startup_id,
(char *)gsm_app_peek_startup_id (app));
if (condition) {
if (!gsm_app_is_running (app) && client == NULL) {
GError *error = NULL;
gboolean UNUSED_VARIABLE res;
g_debug ("GsmManager: starting app '%s'", gsm_app_peek_id (app));
res = gsm_app_start (app, &error);
if (error != NULL) {
g_warning ("Not able to start app from its condition: %s",
error->message);
g_error_free (error);
}
} else {
g_debug ("GsmManager: not starting - app still running '%s'", gsm_app_peek_id (app));
}
} else {
GError *error;
gboolean UNUSED_VARIABLE res;
if (client != NULL) {
/* Kill client in case condition if false and make sure it won't
* be automatically restarted by adding the client to
* condition_clients */
priv->condition_clients =
g_slist_prepend (priv->condition_clients, client);
g_debug ("GsmManager: stopping client %s for app", gsm_client_peek_id (client));
error = NULL;
res = gsm_client_stop (client, &error);
if (error != NULL) {
g_warning ("Not able to stop app client from its condition: %s",
error->message);
g_error_free (error);
}
} else {
g_debug ("GsmManager: stopping app %s", gsm_app_peek_id (app));
/* If we don't have a client then we should try to kill the app,
* if it is running */
error = NULL;
if (gsm_app_is_running (app)) {
res = gsm_app_stop (app, &error);
if (error != NULL) {
g_warning ("Not able to stop app from its condition: %s",
error->message);
g_error_free (error);
}
}
}
}
}
static const char *
phase_num_to_name (guint phase)
{
const char *name;
switch (phase) {
case GSM_MANAGER_PHASE_STARTUP:
name = "STARTUP";
break;
case GSM_MANAGER_PHASE_INITIALIZATION:
name = "INITIALIZATION";
break;
case GSM_MANAGER_PHASE_WINDOW_MANAGER:
name = "WINDOW_MANAGER";
break;
case GSM_MANAGER_PHASE_PANEL:
name = "PANEL";
break;
case GSM_MANAGER_PHASE_DESKTOP:
name = "DESKTOP";
break;
case GSM_MANAGER_PHASE_APPLICATION:
name = "APPLICATION";
break;
case GSM_MANAGER_PHASE_RUNNING:
name = "RUNNING";
break;
case GSM_MANAGER_PHASE_QUERY_END_SESSION:
name = "QUERY_END_SESSION";
break;
case GSM_MANAGER_PHASE_END_SESSION:
name = "END_SESSION";
break;
case GSM_MANAGER_PHASE_EXIT:
name = "EXIT";
break;
default:
g_assert_not_reached ();
break;
}
return name;
}
static void start_phase (GsmManager *manager);
static void
quit_request_completed_consolekit (GsmConsolekit *consolekit,
GError *error,
gpointer user_data)
{
MdmLogoutAction fallback_action = GPOINTER_TO_INT (user_data);
if (error != NULL) {
mdm_set_logout_action (fallback_action);
}
g_object_unref (consolekit);
gtk_main_quit ();
}
#ifdef HAVE_SYSTEMD
static void
quit_request_completed_systemd (GsmSystemd *systemd,
GError *error,
gpointer user_data)
{
MdmLogoutAction fallback_action = GPOINTER_TO_INT (user_data);
if (error != NULL) {
mdm_set_logout_action (fallback_action);
}
g_object_unref (systemd);
gtk_main_quit ();
}
#endif
static void
gsm_manager_quit (GsmManager *manager)
{
GsmConsolekit *consolekit;
GsmManagerPrivate *priv;
#ifdef HAVE_SYSTEMD
GsmSystemd *systemd;
#endif
priv = gsm_manager_get_instance_private (manager);
/* See the comment in request_reboot() for some more details about how
* this works. */
switch (priv->logout_type) {
case GSM_MANAGER_LOGOUT_LOGOUT:
gtk_main_quit ();
break;
case GSM_MANAGER_LOGOUT_REBOOT:
case GSM_MANAGER_LOGOUT_REBOOT_INTERACT:
mdm_set_logout_action (MDM_LOGOUT_ACTION_NONE);
#ifdef HAVE_SYSTEMD
if (LOGIND_RUNNING()) {
systemd = gsm_get_systemd ();
g_signal_connect (systemd,
"request-completed",
G_CALLBACK (quit_request_completed_systemd),
GINT_TO_POINTER (MDM_LOGOUT_ACTION_REBOOT));
gsm_systemd_attempt_restart (systemd);
}
else {
#endif
consolekit = gsm_get_consolekit ();
g_signal_connect (consolekit,
"request-completed",
G_CALLBACK (quit_request_completed_consolekit),
GINT_TO_POINTER (MDM_LOGOUT_ACTION_REBOOT));
gsm_consolekit_attempt_restart (consolekit);
#ifdef HAVE_SYSTEMD
}
#endif
break;
case GSM_MANAGER_LOGOUT_REBOOT_MDM:
mdm_set_logout_action (MDM_LOGOUT_ACTION_REBOOT);
gtk_main_quit ();
break;
case GSM_MANAGER_LOGOUT_SHUTDOWN:
case GSM_MANAGER_LOGOUT_SHUTDOWN_INTERACT:
mdm_set_logout_action (MDM_LOGOUT_ACTION_NONE);
#ifdef HAVE_SYSTEMD
if (LOGIND_RUNNING()) {
systemd = gsm_get_systemd ();
g_signal_connect (systemd,
"request-completed",
G_CALLBACK (quit_request_completed_systemd),
GINT_TO_POINTER (MDM_LOGOUT_ACTION_SHUTDOWN));
gsm_systemd_attempt_stop (systemd);
}
else {
#endif
consolekit = gsm_get_consolekit ();
g_signal_connect (consolekit,
"request-completed",
G_CALLBACK (quit_request_completed_consolekit),
GINT_TO_POINTER (MDM_LOGOUT_ACTION_SHUTDOWN));
gsm_consolekit_attempt_stop (consolekit);
#ifdef HAVE_SYSTEMD
}
#endif
break;
case GSM_MANAGER_LOGOUT_SHUTDOWN_MDM:
mdm_set_logout_action (MDM_LOGOUT_ACTION_SHUTDOWN);
gtk_main_quit ();
break;
default:
g_assert_not_reached ();
break;
}
}
static void
end_phase (GsmManager *manager)
{
GsmManagerPrivate *priv;
gboolean start_next_phase = TRUE;
priv = gsm_manager_get_instance_private (manager);
g_debug ("GsmManager: ending phase %s\n",
phase_num_to_name (priv->phase));
g_slist_free (priv->pending_apps);
priv->pending_apps = NULL;
g_slist_free (priv->query_clients);
priv->query_clients = NULL;
g_slist_free (priv->next_query_clients);
priv->next_query_clients = NULL;
if (priv->phase_timeout_id > 0) {
g_source_remove (priv->phase_timeout_id);
priv->phase_timeout_id = 0;
}
switch (priv->phase) {
case GSM_MANAGER_PHASE_STARTUP:
case GSM_MANAGER_PHASE_INITIALIZATION:
case GSM_MANAGER_PHASE_WINDOW_MANAGER:
case GSM_MANAGER_PHASE_PANEL:
case GSM_MANAGER_PHASE_DESKTOP:
case GSM_MANAGER_PHASE_APPLICATION:
break;
case GSM_MANAGER_PHASE_RUNNING:
if (_log_out_is_locked_down (manager)) {
g_warning ("Unable to logout: Logout has been locked down");
start_next_phase = FALSE;
}
break;
case GSM_MANAGER_PHASE_QUERY_END_SESSION:
break;
case GSM_MANAGER_PHASE_END_SESSION:
if (auto_save_is_enabled (manager))
maybe_save_session (manager);
break;
case GSM_MANAGER_PHASE_EXIT:
start_next_phase = FALSE;
gsm_manager_quit (manager);
break;
default:
g_assert_not_reached ();
break;
}
if (start_next_phase) {
priv->phase++;
start_phase (manager);
}
}
static void
app_registered (GsmApp *app,
GsmManager *manager)
{
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
priv->pending_apps = g_slist_remove (priv->pending_apps, app);
g_signal_handlers_disconnect_by_func (app, app_registered, manager);
if (priv->pending_apps == NULL) {
if (priv->phase_timeout_id > 0) {
g_source_remove (priv->phase_timeout_id);
priv->phase_timeout_id = 0;
}
end_phase (manager);
}
}
static gboolean
_client_failed_to_stop (const char *id,
GsmClient *client,
gpointer user_data)
{
g_debug ("GsmManager: client failed to stop: %s, %s", gsm_client_peek_id (client), gsm_client_peek_app_id (client));
return FALSE;
}
static gboolean
on_phase_timeout (GsmManager *manager)
{
GSList *a;
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
priv->phase_timeout_id = 0;
switch (priv->phase) {
case GSM_MANAGER_PHASE_STARTUP:
case GSM_MANAGER_PHASE_INITIALIZATION:
case GSM_MANAGER_PHASE_WINDOW_MANAGER:
case GSM_MANAGER_PHASE_PANEL:
case GSM_MANAGER_PHASE_DESKTOP:
case GSM_MANAGER_PHASE_APPLICATION:
for (a = priv->pending_apps; a; a = a->next) {
g_warning ("Application '%s' failed to register before timeout",
gsm_app_peek_app_id (a->data));
g_signal_handlers_disconnect_by_func (a->data, app_registered, manager);
/* FIXME: what if the app was filling in a required slot? */
}
break;
case GSM_MANAGER_PHASE_RUNNING:
break;
case GSM_MANAGER_PHASE_QUERY_END_SESSION:
case GSM_MANAGER_PHASE_END_SESSION:
break;
case GSM_MANAGER_PHASE_EXIT:
gsm_store_foreach (priv->clients,
(GsmStoreFunc)_client_failed_to_stop,
NULL);
break;
default:
g_assert_not_reached ();
break;
}
end_phase (manager);
return FALSE;
}
static gboolean
_autostart_delay_timeout (GsmApp *app)
{
GError *error = NULL;
gboolean res;
if (!gsm_app_peek_is_disabled (app)
&& !gsm_app_peek_is_conditionally_disabled (app)) {
res = gsm_app_start (app, &error);
if (!res) {
if (error != NULL) {
g_warning ("Could not launch application '%s': %s",
gsm_app_peek_app_id (app),
error->message);
g_error_free (error);
}
}
}
g_object_unref (app);
return FALSE;
}
static gboolean
_start_app (const char *id,
GsmApp *app,
GsmManager *manager)
{
GError *error;
gboolean res;
int delay;
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
if (gsm_app_peek_phase (app) != priv->phase) {
goto out;
}
/* Keep track of app autostart condition in order to react
* accordingly in the future. */
g_signal_connect (app,
"condition-changed",
G_CALLBACK (app_condition_changed),
manager);
if (gsm_app_peek_is_disabled (app)
|| gsm_app_peek_is_conditionally_disabled (app)) {
g_debug ("GsmManager: Skipping disabled app: %s", id);
goto out;
}
delay = gsm_app_peek_autostart_delay (app);
if (delay > 0) {
g_timeout_add_seconds (delay,
(GSourceFunc)_autostart_delay_timeout,
g_object_ref (app));
g_debug ("GsmManager: %s is scheduled to start in %d seconds", id, delay);
goto out;
}
error = NULL;
res = gsm_app_start (app, &error);
if (!res) {
if (error != NULL) {
g_warning ("Could not launch application '%s': %s",
gsm_app_peek_app_id (app),
error->message);
g_error_free (error);
error = NULL;
}
goto out;
}
if (priv->phase < GSM_MANAGER_PHASE_APPLICATION) {
g_signal_connect (app,
"exited",
G_CALLBACK (app_registered),
manager);
g_signal_connect (app,
"registered",
G_CALLBACK (app_registered),
manager);
priv->pending_apps = g_slist_prepend (priv->pending_apps, app);
}
out:
return FALSE;
}
static void
do_phase_startup (GsmManager *manager)
{
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
gsm_store_foreach (priv->apps,
(GsmStoreFunc)_start_app,
manager);
if (priv->pending_apps != NULL) {
if (priv->phase < GSM_MANAGER_PHASE_APPLICATION) {
priv->phase_timeout_id = g_timeout_add_seconds (GSM_MANAGER_PHASE_TIMEOUT,
(GSourceFunc)on_phase_timeout,
manager);
}
} else {
end_phase (manager);
}
}
typedef struct {
GsmManager *manager;
guint flags;
} ClientEndSessionData;
static gboolean
_client_end_session (GsmClient *client,
ClientEndSessionData *data)
{
gboolean ret;
GError *error;
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (data->manager);
error = NULL;
ret = gsm_client_end_session (client, data->flags, &error);
if (! ret) {
g_warning ("Unable to query client: %s", error->message);
g_error_free (error);
/* FIXME: what should we do if we can't communicate with client? */
} else {
g_debug ("GsmManager: adding client to end-session clients: %s", gsm_client_peek_id (client));
priv->query_clients = g_slist_prepend (priv->query_clients,
client);
}
return FALSE;
}
static gboolean
_client_end_session_helper (const char *id,
GsmClient *client,
ClientEndSessionData *data)
{
return _client_end_session (client, data);
}
static void
do_phase_end_session (GsmManager *manager)
{
ClientEndSessionData data;
GsmManagerPrivate *priv;
data.manager = manager;
data.flags = 0;
priv = gsm_manager_get_instance_private (manager);
if (priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_FORCE) {
data.flags |= GSM_CLIENT_END_SESSION_FLAG_FORCEFUL;
}
if (auto_save_is_enabled (manager)) {
data.flags |= GSM_CLIENT_END_SESSION_FLAG_SAVE;
}
if (priv->phase_timeout_id > 0) {
g_source_remove (priv->phase_timeout_id);
priv->phase_timeout_id = 0;
}
if (gsm_store_size (priv->clients) > 0) {
priv->phase_timeout_id = g_timeout_add_seconds (GSM_MANAGER_PHASE_TIMEOUT,
(GSourceFunc)on_phase_timeout,
manager);
gsm_store_foreach (priv->clients,
(GsmStoreFunc)_client_end_session_helper,
&data);
} else {
end_phase (manager);
}
}
static void
do_phase_end_session_part_2 (GsmManager *manager)
{
ClientEndSessionData data;
GsmManagerPrivate *priv;
data.manager = manager;
data.flags = 0;
priv = gsm_manager_get_instance_private (manager);
if (priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_FORCE) {
data.flags |= GSM_CLIENT_END_SESSION_FLAG_FORCEFUL;
}
if (auto_save_is_enabled (manager)) {
data.flags |= GSM_CLIENT_END_SESSION_FLAG_SAVE;
}
data.flags |= GSM_CLIENT_END_SESSION_FLAG_LAST;
/* keep the timeout that was started at the beginning of the
* GSM_MANAGER_PHASE_END_SESSION phase */
if (g_slist_length (priv->next_query_clients) > 0) {
g_slist_foreach (priv->next_query_clients,
(GFunc)_client_end_session,
&data);
g_slist_free (priv->next_query_clients);
priv->next_query_clients = NULL;
} else {
end_phase (manager);
}
}
static gboolean
_client_stop (const char *id,
GsmClient *client,
gpointer user_data)
{
gboolean ret;
GError *error;
error = NULL;
ret = gsm_client_stop (client, &error);
if (! ret) {
g_warning ("Unable to stop client: %s", error->message);
g_error_free (error);
/* FIXME: what should we do if we can't communicate with client? */
} else {
g_debug ("GsmManager: stopped client: %s", gsm_client_peek_id (client));
}
return FALSE;
}
static void
do_phase_exit (GsmManager *manager)
{
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
if (gsm_store_size (priv->clients) > 0) {
priv->phase_timeout_id = g_timeout_add_seconds (GSM_MANAGER_EXIT_PHASE_TIMEOUT,
(GSourceFunc)on_phase_timeout,
manager);
gsm_store_foreach (priv->clients,
(GsmStoreFunc)_client_stop,
NULL);
} else {
end_phase (manager);
}
}
static gboolean
_client_query_end_session (const char *id,
GsmClient *client,
ClientEndSessionData *data)
{
gboolean ret;
GError *error;
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (data->manager);
error = NULL;
ret = gsm_client_query_end_session (client, data->flags, &error);
if (! ret) {
g_warning ("Unable to query client: %s", error->message);
g_error_free (error);
/* FIXME: what should we do if we can't communicate with client? */
} else {
g_debug ("GsmManager: adding client to query clients: %s", gsm_client_peek_id (client));
priv->query_clients = g_slist_prepend (priv->query_clients, client);
}
return FALSE;
}
static gboolean
inhibitor_has_flag (gpointer key,
GsmInhibitor *inhibitor,
gpointer data)
{
guint flag;
guint flags;
flag = GPOINTER_TO_UINT (data);
flags = gsm_inhibitor_peek_flags (inhibitor);
return (flags & flag);
}
static gboolean
gsm_manager_is_logout_inhibited (GsmManager *manager)
{
GsmInhibitor *inhibitor;
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
if (priv->inhibitors == NULL) {
return FALSE;
}
inhibitor = (GsmInhibitor *)gsm_store_find (priv->inhibitors,
(GsmStoreFunc)inhibitor_has_flag,
GUINT_TO_POINTER (GSM_INHIBITOR_FLAG_LOGOUT));
if (inhibitor == NULL) {
return FALSE;
}
return TRUE;
}
static gboolean
gsm_manager_is_idle_inhibited (GsmManager *manager)
{
GsmInhibitor *inhibitor;
GsmManagerPrivate *priv;
priv = gsm_manager_get_instance_private (manager);
if (priv->inhibitors == NULL) {
return FALSE;
}
inhibitor = (GsmInhibitor *)gsm_store_find (priv->inhibitors,
(GsmStoreFunc)inhibitor_has_flag,
GUINT_TO_POINTER (GSM_INHIBITOR_FLAG_IDLE));
if (inhibitor == NULL) {
return FALSE;
}