7 #include <glib/gstdio.h>
10 #include <gio/gunixsocketaddress.h>
14 /* Timeout in ms waiting for the status we expect */
15 static int status_timeout_ms = 4000;
17 /* Timeout in ms to wait for SIGTERM to be handled by a child process */
18 #define KILL_TIMEOUT 2000
20 static gchar *test_runner_command;
21 static gchar *config_path;
22 static GKeyFile *config;
23 static GSocket *status_socket = NULL;
24 static gchar *status_socket_name = NULL;
25 static GList *statuses = NULL;
31 static GList *script = NULL;
32 static guint status_timeout = 0;
33 static gchar *temp_dir = NULL;
34 static int service_count;
40 static Process *lightdm_process = NULL;
41 static GHashTable *children = NULL;
42 static gboolean stop = FALSE;
43 static gint exit_status = 0;
44 static GDBusConnection *accounts_connection = NULL;
45 static GDBusNodeInfo *accounts_info;
46 static GDBusNodeInfo *user_info;
52 gchar *home_directory;
60 gboolean has_messages;
63 static GList *accounts_users = NULL;
64 static void handle_user_call (GDBusConnection *connection,
66 const gchar *object_path,
67 const gchar *interface_name,
68 const gchar *method_name,
70 GDBusMethodInvocation *invocation,
72 static GVariant *handle_user_get_property (GDBusConnection *connection,
74 const gchar *object_path,
75 const gchar *interface_name,
76 const gchar *property_name,
79 static const GDBusInterfaceVTable user_vtable =
82 handle_user_get_property,
84 static GDBusNodeInfo *ck_session_info;
92 static GList *ck_sessions = NULL;
93 static gint ck_session_index = 0;
94 static void handle_ck_session_call (GDBusConnection *connection,
96 const gchar *object_path,
97 const gchar *interface_name,
98 const gchar *method_name,
100 GDBusMethodInvocation *invocation,
102 static const GDBusInterfaceVTable ck_session_vtable =
104 handle_ck_session_call,
111 gboolean can_graphical;
112 gboolean can_multi_session;
115 static GList *login1_seats = NULL;
117 static Login1Seat *add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_signal);
118 static Login1Seat *find_login1_seat (const gchar *id);
119 static void remove_login1_seat (GDBusConnection *connection, const gchar *id);
129 static GList *login1_sessions = NULL;
130 static gint login1_session_index = 0;
137 static GList *status_clients = NULL;
139 static void ready (void);
140 static void quit (int status);
141 static void check_status (const gchar *status);
142 static AccountsUser *get_accounts_user_by_uid (guint uid);
143 static AccountsUser *get_accounts_user_by_name (const gchar *username);
144 static void accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal);
147 kill_timeout_cb (gpointer data)
149 Process *process = data;
151 process->kill_timeout = 0;
153 if (getenv ("DEBUG"))
154 g_print ("Sending SIGKILL to process %d\n", process->pid);
155 kill (process->pid, SIGKILL);
161 stop_process (Process *process)
163 if (process->kill_timeout != 0)
166 if (getenv ("DEBUG"))
167 g_print ("Sending SIGTERM to process %d\n", process->pid);
168 kill (process->pid, SIGTERM);
169 process->kill_timeout = g_timeout_add (KILL_TIMEOUT, kill_timeout_cb, process);
173 process_exit_cb (GPid pid, gint status, gpointer data)
178 if (getenv ("DEBUG"))
180 if (WIFEXITED (status))
181 g_print ("Process %d exited with status %d\n", pid, WEXITSTATUS (status));
183 g_print ("Process %d terminated with signal %d\n", pid, WTERMSIG (status));
186 if (lightdm_process && pid == lightdm_process->pid)
188 process = lightdm_process;
189 lightdm_process = NULL;
190 if (WIFEXITED (status))
191 status_text = g_strdup_printf ("RUNNER DAEMON-EXIT STATUS=%d", WEXITSTATUS (status));
193 status_text = g_strdup_printf ("RUNNER DAEMON-TERMINATE SIGNAL=%d", WTERMSIG (status));
194 check_status (status_text);
198 process = g_hash_table_lookup (children, GINT_TO_POINTER (pid));
201 g_hash_table_remove (children, GINT_TO_POINTER (pid));
204 if (process->kill_timeout)
205 g_source_remove (process->kill_timeout);
206 process->kill_timeout = 0;
208 /* Quit once all children have stopped */
214 watch_process (pid_t pid)
218 process = g_malloc0 (sizeof (Process));
220 process->kill_timeout = 0;
222 if (getenv ("DEBUG"))
223 g_print ("Watching process %d\n", process->pid);
224 g_child_watch_add (process->pid, process_exit_cb, NULL);
235 exit_status = status;
238 /* Stop all the children */
239 g_hash_table_iter_init (&iter, children);
244 if (!g_hash_table_iter_next (&iter, &key, &value))
247 stop_process ((Process *)value);
250 /* Don't quit until all children are stopped */
251 if (g_hash_table_size (children) > 0)
254 /* Stop the daemon */
257 stop_process (lightdm_process);
261 if (status_socket_name)
262 unlink (status_socket_name);
264 if (temp_dir && getenv ("DEBUG") == NULL)
266 gchar *command = g_strdup_printf ("rm -rf %s", temp_dir);
267 if (system (command))
268 perror ("Failed to delete temp directory");
275 fail (const gchar *event, const gchar *expected)
282 g_printerr ("Command line: %s", test_runner_command);
283 g_printerr ("Events:\n");
284 for (link = statuses; link; link = link->next)
285 g_printerr (" %s\n", (gchar *)link->data);
287 g_printerr (" %s\n", event);
289 g_printerr (" ^^^ expected \"%s\"\n", expected);
291 g_printerr ("^^^ expected nothing\n");
297 get_prefix (const gchar *text)
302 prefix = g_strdup (text);
303 for (i = 0; prefix[i] != '\0' && prefix[i] != ' '; i++);
310 get_script_line (const gchar *prefix)
314 for (link = script; link; link = link->next)
316 ScriptLine *line = link->data;
318 /* Ignore lines with other prefixes */
324 p = get_prefix (line->text);
325 matches = strcmp (prefix, p) == 0;
340 stop_loop (gpointer user_data)
342 g_main_loop_quit ((GMainLoop *)user_data);
343 return G_SOURCE_REMOVE;
347 handle_command (const gchar *command)
354 while (*c && !isspace (*c))
356 name = g_strdup_printf ("%.*s", (int) (c - command), command);
358 params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
362 gchar *param_name, *param_value;
367 while (*c && !isspace (*c) && *c != '=')
372 param_name = g_strdup_printf ("%.*s", (int) (c - start), start);
381 gboolean escaped = FALSE;
385 value = g_string_new ("");
392 g_string_append_c (value, '\\');
398 else if (!escaped && *c == '\"')
401 g_string_append_c (value, *c);
404 param_value = value->str;
405 g_string_free (value, FALSE);
412 while (*c && !isspace (*c))
414 param_value = g_strdup_printf ("%.*s", (int) (c - start), start);
418 param_value = g_strdup ("");
420 g_hash_table_insert (params, param_name, param_value);
423 if (strcmp (name, "START-DAEMON") == 0)
425 GString *command_line;
426 gchar **lightdm_argv;
428 GError *error = NULL;
430 command_line = g_string_new ("lightdm");
431 if (getenv ("DEBUG"))
432 g_string_append (command_line, " --debug");
433 g_string_append_printf (command_line, " --cache-dir %s/cache", temp_dir);
435 test_runner_command = g_strdup_printf ("PATH=%s LD_PRELOAD=%s LD_LIBRARY_PATH=%s LIGHTDM_TEST_ROOT=%s DBUS_SESSION_BUS_ADDRESS=%s %s\n",
436 g_getenv ("PATH"), g_getenv ("LD_PRELOAD"), g_getenv ("LD_LIBRARY_PATH"), g_getenv ("LIGHTDM_TEST_ROOT"), g_getenv ("DBUS_SESSION_BUS_ADDRESS"),
439 if (!g_shell_parse_argv (command_line->str, NULL, &lightdm_argv, &error))
441 g_warning ("Error parsing command line: %s", error->message);
444 g_clear_error (&error);
446 if (!g_spawn_async (NULL, lightdm_argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &lightdm_pid, &error))
448 g_warning ("Error launching LightDM: %s", error->message);
451 g_clear_error (&error);
452 lightdm_process = watch_process (lightdm_pid);
454 check_status ("RUNNER DAEMON-START");
456 else if (strcmp (name, "WAIT") == 0)
458 /* Use a main loop so that our DBus functions are still responsive */
459 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
460 g_timeout_add_seconds (1, stop_loop, loop);
461 g_main_loop_run (loop);
462 g_main_loop_unref (loop);
464 else if (strcmp (name, "ADD-SEAT") == 0)
469 id = g_hash_table_lookup (params, "ID");
470 seat = add_login1_seat (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), id, TRUE);
471 v = g_hash_table_lookup (params, "CAN-GRAPHICAL");
473 seat->can_graphical = strcmp (v, "TRUE") == 0;
474 v = g_hash_table_lookup (params, "CAN-MULTI-SESSION");
476 seat->can_multi_session = strcmp (v, "TRUE") == 0;
478 else if (strcmp (name, "UPDATE-SEAT") == 0)
483 id = g_hash_table_lookup (params, "ID");
484 seat = find_login1_seat (id);
488 GVariantBuilder invalidated_properties;
489 GError *error = NULL;
491 g_variant_builder_init (&invalidated_properties, G_VARIANT_TYPE_ARRAY);
493 v = g_hash_table_lookup (params, "CAN-GRAPHICAL");
496 seat->can_graphical = strcmp (v, "TRUE") == 0;
497 g_variant_builder_add (&invalidated_properties, "s", "CanGraphical");
499 v = g_hash_table_lookup (params, "CAN-MULTI-SESSION");
502 seat->can_multi_session = strcmp (v, "TRUE") == 0;
503 g_variant_builder_add (&invalidated_properties, "s", "CanMultiSession");
506 g_dbus_connection_emit_signal (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
509 "org.freedesktop.DBus.Properties",
511 g_variant_new ("(sa{sv}as)", "org.freedesktop.login1.Seat", NULL, &invalidated_properties),
514 g_warning ("Failed to emit PropertiesChanged: %s", error->message);
515 g_clear_error (&error);
518 else if (strcmp (name, "REMOVE-SEAT") == 0)
521 id = g_hash_table_lookup (params, "ID");
522 remove_login1_seat (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), id);
524 else if (strcmp (name, "LIST-SEATS") == 0)
526 GVariant *result, *value;
532 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
533 "org.freedesktop.DisplayManager",
534 "/org/freedesktop/DisplayManager",
535 "org.freedesktop.DBus.Properties",
537 g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Seats"),
538 G_VARIANT_TYPE ("(v)"),
539 G_DBUS_CALL_FLAGS_NONE,
544 status = g_string_new ("RUNNER LIST-SEATS SEATS=");
545 g_variant_get (result, "(v)", &value);
546 g_variant_get (value, "ao", &iter);
547 while (g_variant_iter_loop (iter, "&o", &path))
550 g_string_append (status, ",");
551 g_string_append (status, path);
554 g_variant_unref (value);
555 g_variant_unref (result);
557 check_status (status->str);
558 g_string_free (status, TRUE);
560 else if (strcmp (name, "LIST-SESSIONS") == 0)
562 GVariant *result, *value;
568 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
569 "org.freedesktop.DisplayManager",
570 "/org/freedesktop/DisplayManager",
571 "org.freedesktop.DBus.Properties",
573 g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Sessions"),
574 G_VARIANT_TYPE ("(v)"),
575 G_DBUS_CALL_FLAGS_NONE,
580 status = g_string_new ("RUNNER LIST-SESSIONS SESSIONS=");
581 g_variant_get (result, "(v)", &value);
582 g_variant_get (value, "ao", &iter);
583 while (g_variant_iter_loop (iter, "&o", &path))
586 g_string_append (status, ",");
587 g_string_append (status, path);
590 g_variant_unref (value);
591 g_variant_unref (result);
593 check_status (status->str);
594 g_string_free (status, TRUE);
596 else if (strcmp (name, "SEAT-CAN-SWITCH") == 0)
598 GVariant *result, *value;
601 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
602 "org.freedesktop.DisplayManager",
603 "/org/freedesktop/DisplayManager/Seat0",
604 "org.freedesktop.DBus.Properties",
606 g_variant_new ("(ss)", "org.freedesktop.DisplayManager.Seat", "CanSwitch"),
607 G_VARIANT_TYPE ("(v)"),
608 G_DBUS_CALL_FLAGS_NONE,
613 g_variant_get (result, "(v)", &value);
614 status = g_strdup_printf ("RUNNER SEAT-CAN-SWITCH CAN-SWITCH=%s", g_variant_get_boolean (value) ? "TRUE" : "FALSE");
615 g_variant_unref (value);
616 g_variant_unref (result);
617 check_status (status);
620 else if (strcmp (name, "SEAT-HAS-GUEST-ACCOUNT") == 0)
622 GVariant *result, *value;
625 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
626 "org.freedesktop.DisplayManager",
627 "/org/freedesktop/DisplayManager/Seat0",
628 "org.freedesktop.DBus.Properties",
630 g_variant_new ("(ss)", "org.freedesktop.DisplayManager.Seat", "HasGuestAccount"),
631 G_VARIANT_TYPE ("(v)"),
632 G_DBUS_CALL_FLAGS_NONE,
637 g_variant_get (result, "(v)", &value);
638 status = g_strdup_printf ("RUNNER SEAT-HAS-GUEST-ACCOUNT HAS-GUEST-ACCOUNT=%s", g_variant_get_boolean (value) ? "TRUE" : "FALSE");
639 g_variant_unref (value);
640 g_variant_unref (result);
641 check_status (status);
644 else if (strcmp (name, "SWITCH-TO-GREETER") == 0)
648 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
649 "org.freedesktop.DisplayManager",
650 "/org/freedesktop/DisplayManager/Seat0",
651 "org.freedesktop.DisplayManager.Seat",
653 g_variant_new ("()"),
654 G_VARIANT_TYPE ("()"),
655 G_DBUS_CALL_FLAGS_NONE,
661 check_status ("RUNNER SWITCH-TO-GREETER");
662 g_variant_unref (result);
665 check_status ("RUNNER SWITCH-TO-GREETER FAILED");
667 else if (strcmp (name, "SWITCH-TO-USER") == 0)
670 const gchar *username;
672 username = g_hash_table_lookup (params, "USERNAME");
673 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
674 "org.freedesktop.DisplayManager",
675 "/org/freedesktop/DisplayManager/Seat0",
676 "org.freedesktop.DisplayManager.Seat",
678 g_variant_new ("(ss)", username, ""),
679 G_VARIANT_TYPE ("()"),
680 G_DBUS_CALL_FLAGS_NONE,
687 status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s", username);
688 check_status (status_text);
689 g_free (status_text);
690 g_variant_unref (result);
695 status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s FAILED", username);
696 check_status (status_text);
697 g_free (status_text);
700 else if (strcmp (name, "SWITCH-TO-GUEST") == 0)
704 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
705 "org.freedesktop.DisplayManager",
706 "/org/freedesktop/DisplayManager/Seat0",
707 "org.freedesktop.DisplayManager.Seat",
709 g_variant_new ("(s)", ""),
710 G_VARIANT_TYPE ("()"),
711 G_DBUS_CALL_FLAGS_NONE,
717 check_status ("RUNNER SWITCH-TO-GUEST");
718 g_variant_unref (result);
721 check_status ("RUNNER SWITCH-TO-GUEST FAILED");
723 else if (strcmp (name, "STOP-DAEMON") == 0)
724 stop_process (lightdm_process);
725 // FIXME: Make generic RUN-COMMAND
726 else if (strcmp (name, "START-XSERVER") == 0)
728 gchar *xserver_args, *command_line;
732 GError *error = NULL;
734 xserver_args = g_hash_table_lookup (params, "ARGS");
737 command_line = g_strdup_printf ("%s/tests/src/X %s", BUILDDIR, xserver_args);
739 if (!g_shell_parse_argv (command_line, NULL, &argv, &error) ||
740 !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error))
742 g_printerr ("Error starting X server: %s", error->message);
747 process = watch_process (pid);
748 g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process);
751 else if (strcmp (name, "START-VNC-CLIENT") == 0)
753 gchar *vnc_client_args, *command_line;
757 GError *error = NULL;
759 vnc_client_args = g_hash_table_lookup (params, "ARGS");
760 if (!vnc_client_args)
761 vnc_client_args = "";
762 command_line = g_strdup_printf ("%s/tests/src/vnc-client %s", BUILDDIR, vnc_client_args);
764 if (!g_shell_parse_argv (command_line, NULL, &argv, &error) ||
765 !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error))
767 g_printerr ("Error starting VNC client: %s", error->message);
772 process = watch_process (pid);
773 g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process);
776 else if (strcmp (name, "ADD-USER") == 0)
778 gchar *status_text, *username;
781 username = g_hash_table_lookup (params, "USERNAME");
782 user = get_accounts_user_by_name (username);
784 accounts_user_set_hidden (user, FALSE, TRUE);
786 g_warning ("Unknown user %s", username);
788 status_text = g_strdup_printf ("RUNNER ADD-USER USERNAME=%s", username);
789 check_status (status_text);
790 g_free (status_text);
792 else if (strcmp (name, "UPDATE-USER") == 0)
794 GString *status_text;
797 GError *error = NULL;
799 status_text = g_string_new ("RUNNER UPDATE-USER USERNAME=");
801 username = g_hash_table_lookup (params, "USERNAME");
802 g_string_append (status_text, username);
803 user = get_accounts_user_by_name (username);
806 if (g_hash_table_lookup (params, "NAME"))
808 user->user_name = g_strdup (g_hash_table_lookup (params, "NAME"));
809 g_string_append_printf (status_text, " NAME=%s", user->user_name);
811 if (g_hash_table_lookup (params, "REAL-NAME"))
813 user->real_name = g_strdup (g_hash_table_lookup (params, "REAL-NAME"));
814 g_string_append_printf (status_text, " REAL-NAME=%s", user->real_name);
816 if (g_hash_table_lookup (params, "HOME-DIRECTORY"))
818 user->home_directory = g_strdup (g_hash_table_lookup (params, "HOME-DIRECTORY"));
819 g_string_append_printf (status_text, " HOME-DIRECTORY=%s", user->home_directory);
821 if (g_hash_table_lookup (params, "IMAGE"))
823 user->image = g_strdup (g_hash_table_lookup (params, "IMAGE"));
824 g_string_append_printf (status_text, " IMAGE=%s", user->image);
826 if (g_hash_table_lookup (params, "BACKGROUND"))
828 user->background = g_strdup (g_hash_table_lookup (params, "BACKGROUND"));
829 g_string_append_printf (status_text, " BACKGROUND=%s", user->background);
831 if (g_hash_table_lookup (params, "LANGUAGE"))
833 user->language = g_strdup (g_hash_table_lookup (params, "LANGUAGE"));
834 g_string_append_printf (status_text, " LANGUAGE=%s", user->language);
836 if (g_hash_table_lookup (params, "LAYOUTS"))
838 const gchar *value = g_hash_table_lookup (params, "LAYOUTS");
839 user->layouts = g_strsplit (value, ";", -1);
840 g_string_append_printf (status_text, " LAYOUTS=%s", value);
842 if (g_hash_table_lookup (params, "HAS-MESSAGES"))
844 user->has_messages = g_strcmp0 (g_hash_table_lookup (params, "HAS-MESSAGES"), "TRUE") == 0;
845 g_string_append_printf (status_text, " HAS-MESSAGES=%s", user->has_messages ? "TRUE" : "FALSE");
847 if (g_hash_table_lookup (params, "SESSION"))
849 user->xsession = g_strdup (g_hash_table_lookup (params, "SESSION"));
850 g_string_append_printf (status_text, " SESSION=%s", user->xsession);
854 g_warning ("Unknown user %s", username);
856 g_dbus_connection_emit_signal (accounts_connection,
859 "org.freedesktop.Accounts.User",
861 g_variant_new ("()"),
864 g_warning ("Failed to emit Changed: %s", error->message);
865 g_clear_error (&error);
867 check_status (status_text->str);
868 g_string_free (status_text, TRUE);
870 else if (strcmp (name, "DELETE-USER") == 0)
872 gchar *status_text, *username;
875 username = g_hash_table_lookup (params, "USERNAME");
876 user = get_accounts_user_by_name (username);
878 accounts_user_set_hidden (user, TRUE, TRUE);
880 g_warning ("Unknown user %s", username);
882 status_text = g_strdup_printf ("RUNNER DELETE-USER USERNAME=%s", username);
883 check_status (status_text);
884 g_free (status_text);
886 /* Forward to external processes */
887 else if (g_str_has_prefix (name, "SESSION-") ||
888 g_str_has_prefix (name, "GREETER-") ||
889 g_str_has_prefix (name, "XSERVER-") ||
890 strcmp (name, "UNITY-SYSTEM-COMPOSITOR") == 0)
893 for (link = status_clients; link; link = link->next)
895 StatusClient *client = link->data;
897 GError *error = NULL;
899 length = strlen (command);
900 if (g_socket_send (client->socket, (gchar *) &length, sizeof (length), NULL, &error) < 0 ||
901 g_socket_send (client->socket, command, strlen (command), NULL, &error) < 0)
902 g_printerr ("Failed to write to client socket: %s\n", error->message);
903 g_clear_error (&error);
908 g_printerr ("Unknown command '%s'\n", name);
913 g_hash_table_unref (params);
919 /* Stop daemon if requested */
924 /* Commands start with an asterisk */
925 line = get_script_line (NULL);
926 if (!line || line->text[0] != '*')
929 statuses = g_list_append (statuses, g_strdup (line->text));
932 handle_command (line->text + 1);
935 /* Stop at the end of the script */
936 if (get_script_line (NULL) == NULL)
941 status_timeout_cb (gpointer data)
945 line = get_script_line (NULL);
946 fail ("(timeout)", line ? line->text : NULL);
952 check_status (const gchar *status)
955 gboolean result = FALSE;
961 statuses = g_list_append (statuses, g_strdup (status));
963 if (getenv ("DEBUG"))
964 g_print ("%s\n", status);
966 /* Try and match against expected */
967 prefix = get_prefix (status);
968 line = get_script_line (prefix);
972 gchar *full_pattern = g_strdup_printf ("^%s$", line->text);
973 result = g_regex_match_simple (full_pattern, status, 0, 0);
974 g_free (full_pattern);
980 line = get_script_line (NULL);
981 fail (NULL, line ? line->text : NULL);
987 /* Restart timeout */
989 g_source_remove (status_timeout);
990 status_timeout = g_timeout_add (status_timeout_ms, status_timeout_cb, NULL);
996 status_message_cb (GSocket *socket, GIOCondition condition, StatusClient *client)
1001 GError *error = NULL;
1003 n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
1005 n_read = g_socket_receive (socket, buffer, length, NULL, &error);
1007 g_warning ("Error reading from socket: %s", error->message);
1008 g_clear_error (&error);
1011 status_clients = g_list_remove (status_clients, client);
1012 g_object_unref (client->socket);
1016 else if (n_read > 0)
1018 buffer[n_read] = '\0';
1019 check_status (buffer);
1026 status_connect_cb (gpointer data)
1029 GError *error = NULL;
1031 socket = g_socket_accept (status_socket, NULL, &error);
1033 g_warning ("Failed to accept status connection: %s", error->message);
1034 g_clear_error (&error);
1037 StatusClient *client;
1039 client = g_malloc0 (sizeof (StatusClient));
1040 client->socket = socket;
1041 client->source = g_socket_create_source (socket, G_IO_IN, NULL);
1042 status_clients = g_list_append (status_clients, client);
1044 g_source_set_callback (client->source, (GSourceFunc) status_message_cb, client, NULL);
1045 g_source_attach (client->source, NULL);
1052 load_script (const gchar *filename)
1055 gchar *data, **lines;
1057 if (!g_file_get_contents (filename, &data, NULL, NULL))
1059 g_printerr ("Unable to load script: %s\n", filename);
1060 quit (EXIT_FAILURE);
1063 lines = g_strsplit (data, "\n", -1);
1066 /* Load lines with #? prefix as expected behaviour */
1067 for (i = 0; lines[i]; i++)
1069 gchar *text = g_strstrip (lines[i]);
1070 if (g_str_has_prefix (text, "#?"))
1073 line = g_malloc0 (sizeof (ScriptLine));
1074 line->text = g_strdup (text + 2);
1076 script = g_list_append (script, line);
1083 handle_upower_call (GDBusConnection *connection,
1084 const gchar *sender,
1085 const gchar *object_path,
1086 const gchar *interface_name,
1087 const gchar *method_name,
1088 GVariant *parameters,
1089 GDBusMethodInvocation *invocation,
1092 if (strcmp (method_name, "SuspendAllowed") == 0)
1094 check_status ("UPOWER SUSPEND-ALLOWED");
1095 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1097 else if (strcmp (method_name, "Suspend") == 0)
1099 check_status ("UPOWER SUSPEND");
1100 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1102 else if (strcmp (method_name, "HibernateAllowed") == 0)
1104 check_status ("UPOWER HIBERNATE-ALLOWED");
1105 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1107 else if (strcmp (method_name, "Hibernate") == 0)
1109 check_status ("UPOWER HIBERNATE");
1110 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1113 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1117 upower_name_acquired_cb (GDBusConnection *connection,
1121 const gchar *upower_interface =
1123 " <interface name='org.freedesktop.UPower'>"
1124 " <method name='SuspendAllowed'>"
1125 " <arg name='allowed' direction='out' type='b'/>"
1127 " <method name='Suspend'/>"
1128 " <method name='HibernateAllowed'>"
1129 " <arg name='allowed' direction='out' type='b'/>"
1131 " <method name='Hibernate'/>"
1134 static const GDBusInterfaceVTable upower_vtable =
1138 GDBusNodeInfo *upower_info;
1139 GError *error = NULL;
1141 upower_info = g_dbus_node_info_new_for_xml (upower_interface, &error);
1143 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1144 g_clear_error (&error);
1147 g_dbus_connection_register_object (connection,
1148 "/org/freedesktop/UPower",
1149 upower_info->interfaces[0],
1154 g_warning ("Failed to register UPower service: %s", error->message);
1155 g_clear_error (&error);
1156 g_dbus_node_info_unref (upower_info);
1159 if (service_count == 0)
1164 start_upower_daemon (void)
1167 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1168 "org.freedesktop.UPower",
1169 G_BUS_NAME_OWNER_FLAGS_NONE,
1170 upower_name_acquired_cb,
1178 open_ck_session (GDBusConnection *connection, GVariant *params)
1185 GError *error = NULL;
1187 session = g_malloc0 (sizeof (CKSession));
1188 ck_sessions = g_list_append (ck_sessions, session);
1190 cookie = g_string_new ("ck-cookie");
1191 g_variant_get (params, "a(sv)", &iter);
1192 while (g_variant_iter_loop (iter, "(&sv)", &name, &value))
1194 if (strcmp (name, "x11-display") == 0)
1196 const gchar *display;
1197 g_variant_get (value, "&s", &display);
1198 g_string_append_printf (cookie, "-x%s", display);
1202 session->cookie = cookie->str;
1203 g_string_free (cookie, FALSE);
1204 session->path = g_strdup_printf ("/org/freedesktop/ConsoleKit/Session%d", ck_session_index++);
1205 session->id = g_dbus_connection_register_object (connection,
1207 ck_session_info->interfaces[0],
1213 g_warning ("Failed to register CK Session: %s", error->message);
1214 g_clear_error (&error);
1220 handle_ck_call (GDBusConnection *connection,
1221 const gchar *sender,
1222 const gchar *object_path,
1223 const gchar *interface_name,
1224 const gchar *method_name,
1225 GVariant *parameters,
1226 GDBusMethodInvocation *invocation,
1229 if (strcmp (method_name, "CanRestart") == 0)
1231 check_status ("CONSOLE-KIT CAN-RESTART");
1232 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1234 else if (strcmp (method_name, "CanStop") == 0)
1236 check_status ("CONSOLE-KIT CAN-STOP");
1237 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1239 else if (strcmp (method_name, "CloseSession") == 0)
1240 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1241 else if (strcmp (method_name, "OpenSession") == 0)
1243 GVariantBuilder params;
1244 g_variant_builder_init (¶ms, G_VARIANT_TYPE ("a(sv)"));
1245 CKSession *session = open_ck_session (connection, g_variant_builder_end (¶ms));
1246 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", session->cookie));
1248 else if (strcmp (method_name, "OpenSessionWithParameters") == 0)
1250 CKSession *session = open_ck_session (connection, g_variant_get_child_value (parameters, 0));
1251 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", session->cookie));
1253 else if (strcmp (method_name, "GetSessionForCookie") == 0)
1258 g_variant_get (parameters, "(&s)", &cookie);
1260 for (link = ck_sessions; link; link = link->next)
1262 CKSession *session = link->data;
1263 if (strcmp (session->cookie, cookie) == 0)
1265 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", session->path));
1270 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to find session for cookie");
1272 else if (strcmp (method_name, "Restart") == 0)
1274 check_status ("CONSOLE-KIT RESTART");
1275 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1277 else if (strcmp (method_name, "Stop") == 0)
1279 check_status ("CONSOLE-KIT STOP");
1280 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1283 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1287 handle_ck_session_call (GDBusConnection *connection,
1288 const gchar *sender,
1289 const gchar *object_path,
1290 const gchar *interface_name,
1291 const gchar *method_name,
1292 GVariant *parameters,
1293 GDBusMethodInvocation *invocation,
1296 CKSession *session = user_data;
1298 if (strcmp (method_name, "Lock") == 0)
1300 if (!session->locked)
1301 check_status ("CONSOLE-KIT LOCK-SESSION");
1302 session->locked = TRUE;
1303 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1305 else if (strcmp (method_name, "Unlock") == 0)
1307 if (session->locked)
1308 check_status ("CONSOLE-KIT UNLOCK-SESSION");
1309 session->locked = FALSE;
1310 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1312 else if (strcmp (method_name, "Activate") == 0)
1314 gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie);
1315 check_status (status);
1318 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1321 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1325 ck_name_acquired_cb (GDBusConnection *connection,
1329 const gchar *ck_interface =
1331 " <interface name='org.freedesktop.ConsoleKit.Manager'>"
1332 " <method name='CanRestart'>"
1333 " <arg name='can_restart' direction='out' type='b'/>"
1335 " <method name='CanStop'>"
1336 " <arg name='can_stop' direction='out' type='b'/>"
1338 " <method name='CloseSession'>"
1339 " <arg name='cookie' direction='in' type='s'/>"
1340 " <arg name='result' direction='out' type='b'/>"
1342 " <method name='OpenSession'>"
1343 " <arg name='cookie' direction='out' type='s'/>"
1345 " <method name='OpenSessionWithParameters'>"
1346 " <arg name='parameters' direction='in' type='a(sv)'/>"
1347 " <arg name='cookie' direction='out' type='s'/>"
1349 " <method name='GetSessionForCookie'>"
1350 " <arg name='cookie' direction='in' type='s'/>"
1351 " <arg name='ssid' direction='out' type='o'/>"
1353 " <method name='Restart'/>"
1354 " <method name='Stop'/>"
1355 " <signal name='SeatAdded'>"
1356 " <arg name='seat' type='o'/>"
1358 " <signal name='SeatRemoved'>"
1359 " <arg name='seat' type='o'/>"
1363 static const GDBusInterfaceVTable ck_vtable =
1367 const gchar *ck_session_interface =
1369 " <interface name='org.freedesktop.ConsoleKit.Session'>"
1370 " <method name='Lock'/>"
1371 " <method name='Unlock'/>"
1372 " <method name='Activate'/>"
1375 GDBusNodeInfo *ck_info;
1376 GError *error = NULL;
1378 ck_info = g_dbus_node_info_new_for_xml (ck_interface, &error);
1380 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1381 g_clear_error (&error);
1384 ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface, &error);
1386 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1387 g_clear_error (&error);
1388 if (!ck_session_info)
1390 g_dbus_connection_register_object (connection,
1391 "/org/freedesktop/ConsoleKit/Manager",
1392 ck_info->interfaces[0],
1397 g_warning ("Failed to register console kit service: %s", error->message);
1398 g_clear_error (&error);
1399 g_dbus_node_info_unref (ck_info);
1402 if (service_count == 0)
1407 start_console_kit_daemon (void)
1410 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1411 "org.freedesktop.ConsoleKit",
1412 G_BUS_NAME_OWNER_FLAGS_NONE,
1414 ck_name_acquired_cb,
1421 handle_login1_seat_call (GDBusConnection *connection,
1422 const gchar *sender,
1423 const gchar *object_path,
1424 const gchar *interface_name,
1425 const gchar *method_name,
1426 GVariant *parameters,
1427 GDBusMethodInvocation *invocation,
1430 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1434 handle_login1_seat_get_property (GDBusConnection *connection,
1435 const gchar *sender,
1436 const gchar *object_path,
1437 const gchar *interface_name,
1438 const gchar *property_name,
1442 Login1Seat *seat = user_data;
1444 if (strcmp (property_name, "CanGraphical") == 0)
1445 return g_variant_new_boolean (seat->can_graphical);
1446 else if (strcmp (property_name, "CanMultiSession") == 0)
1447 return g_variant_new_boolean (seat->can_multi_session);
1448 else if (strcmp (property_name, "Id") == 0)
1449 return g_variant_new_string (seat->id);
1455 escape_seat_id (const gchar *id)
1460 s = g_string_new ("");
1461 for (i = 0; id[i]; i++)
1463 if (isalnum (id[i]) || id[i] == '_')
1464 g_string_append_c (s, id[i]);
1466 g_string_append_printf (s, "_%02x", id[i]);
1469 return g_string_free (s, FALSE);
1473 add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_signal)
1477 GError *error = NULL;
1478 GDBusNodeInfo *login1_seat_info;
1480 const gchar *login1_seat_interface =
1482 " <interface name='org.freedesktop.login1.Seat'>"
1483 " <property name='CanGraphical' type='b' access='read'/>"
1484 " <property name='CanMultiSession' type='b' access='read'/>"
1485 " <property name='Id' type='s' access='read'/>"
1488 static const GDBusInterfaceVTable login1_seat_vtable =
1490 handle_login1_seat_call,
1491 handle_login1_seat_get_property,
1494 login1_seat_info = g_dbus_node_info_new_for_xml (login1_seat_interface, &error);
1496 g_warning ("Failed to parse login1 seat D-Bus interface: %s", error->message);
1497 g_clear_error (&error);
1498 if (!login1_seat_info)
1501 seat = g_malloc0 (sizeof (Login1Seat));
1502 login1_seats = g_list_append (login1_seats, seat);
1503 seat->id = g_strdup (id);
1504 escaped_id = escape_seat_id (seat->id);
1505 seat->path = g_strdup_printf ("/org/freedesktop/login1/seat/%s", escaped_id);
1506 g_free (escaped_id);
1507 seat->can_graphical = TRUE;
1508 seat->can_multi_session = TRUE;
1510 g_dbus_connection_register_object (connection,
1512 login1_seat_info->interfaces[0],
1513 &login1_seat_vtable,
1518 g_warning ("Failed to register login1 seat: %s", error->message);
1519 g_clear_error (&error);
1520 g_dbus_node_info_unref (login1_seat_info);
1524 g_dbus_connection_emit_signal (connection,
1526 "/org/freedesktop/login1",
1527 "org.freedesktop.login1.Manager",
1529 g_variant_new ("(so)", seat->id, seat->path),
1532 g_warning ("Failed to emit SeatNew: %s", error->message);
1533 g_clear_error (&error);
1540 find_login1_seat (const gchar *id)
1545 for (link = login1_seats; link; link = link->next)
1548 if (strcmp (seat->id, id) == 0)
1556 remove_login1_seat (GDBusConnection *connection, const gchar *id)
1559 GError *error = NULL;
1561 seat = find_login1_seat (id);
1565 g_dbus_connection_emit_signal (connection,
1567 "/org/freedesktop/login1",
1568 "org.freedesktop.login1.Manager",
1570 g_variant_new ("(so)", seat->id, seat->path),
1573 g_warning ("Failed to emit SeatNew: %s", error->message);
1574 g_clear_error (&error);
1576 login1_seats = g_list_remove (login1_seats, seat);
1578 g_free (seat->path);
1583 handle_login1_session_call (GDBusConnection *connection,
1584 const gchar *sender,
1585 const gchar *object_path,
1586 const gchar *interface_name,
1587 const gchar *method_name,
1588 GVariant *parameters,
1589 GDBusMethodInvocation *invocation,
1592 /*Login1Session *session = user_data;*/
1593 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1596 static Login1Session *
1597 create_login1_session (GDBusConnection *connection)
1599 Login1Session *session;
1600 GError *error = NULL;
1601 GDBusNodeInfo *login1_session_info;
1603 const gchar *login1_session_interface =
1605 " <interface name='org.freedesktop.login1.Session'>"
1608 static const GDBusInterfaceVTable login1_session_vtable =
1610 handle_login1_session_call,
1613 session = g_malloc0 (sizeof (Login1Session));
1614 login1_sessions = g_list_append (login1_sessions, session);
1616 session->id = g_strdup_printf ("c%d", login1_session_index++);
1617 session->path = g_strdup_printf ("/org/freedesktop/login1/Session/%s", session->id);
1619 login1_session_info = g_dbus_node_info_new_for_xml (login1_session_interface, &error);
1621 g_warning ("Failed to parse login1 session D-Bus interface: %s", error->message);
1622 g_clear_error (&error);
1623 if (!login1_session_info)
1626 g_dbus_connection_register_object (connection,
1628 login1_session_info->interfaces[0],
1629 &login1_session_vtable,
1634 g_warning ("Failed to register login1 session: %s", error->message);
1635 g_clear_error (&error);
1636 g_dbus_node_info_unref (login1_session_info);
1641 static Login1Session *
1642 find_login1_session (const gchar *id)
1646 for (link = login1_sessions; link; link = link->next)
1648 Login1Session *session = link->data;
1649 if (strcmp (session->id, id) == 0)
1657 handle_login1_call (GDBusConnection *connection,
1658 const gchar *sender,
1659 const gchar *object_path,
1660 const gchar *interface_name,
1661 const gchar *method_name,
1662 GVariant *parameters,
1663 GDBusMethodInvocation *invocation,
1666 if (strcmp (method_name, "ListSeats") == 0)
1668 GVariantBuilder seats;
1671 g_variant_builder_init (&seats, G_VARIANT_TYPE ("a(so)"));
1672 for (link = login1_seats; link; link = link->next)
1674 Login1Seat *seat = link->data;
1675 g_variant_builder_add (&seats, "(so)", seat->id, seat->path);
1677 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(so))", &seats));
1679 else if (strcmp (method_name, "CreateSession") == 0)
1681 /* Note: this is not the full CreateSession as used by logind, we just
1682 need one so our fake PAM stack can communicate with this service */
1683 Login1Session *session = create_login1_session (connection);
1684 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(so)", session->id, session->path));
1687 else if (strcmp (method_name, "LockSession") == 0)
1690 Login1Session *session;
1692 g_variant_get (parameters, "(&s)", &id);
1693 session = find_login1_session (id);
1696 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such session: %s", id);
1700 if (!session->locked)
1702 gchar *status = g_strdup_printf ("LOGIN1 LOCK-SESSION SESSION=%s", id);
1703 check_status (status);
1706 session->locked = TRUE;
1707 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1709 else if (strcmp (method_name, "UnlockSession") == 0)
1712 Login1Session *session;
1714 g_variant_get (parameters, "(&s)", &id);
1715 session = find_login1_session (id);
1718 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such session: %s", id);
1722 if (session->locked)
1724 gchar *status = g_strdup_printf ("LOGIN1 UNLOCK-SESSION SESSION=%s", id);
1725 check_status (status);
1728 session->locked = FALSE;
1729 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1731 else if (strcmp (method_name, "ActivateSession") == 0)
1734 Login1Session *session;
1736 g_variant_get (parameters, "(&s)", &id);
1737 session = find_login1_session (id);
1740 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such session: %s", id);
1744 gchar *status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", id);
1745 check_status (status);
1748 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1750 else if (strcmp (method_name, "CanReboot") == 0)
1752 check_status ("LOGIN1 CAN-REBOOT");
1753 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1755 else if (strcmp (method_name, "Reboot") == 0)
1757 gboolean interactive;
1758 g_variant_get (parameters, "(b)", &interactive);
1759 check_status ("LOGIN1 REBOOT");
1760 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1762 else if (strcmp (method_name, "CanPowerOff") == 0)
1764 check_status ("LOGIN1 CAN-POWER-OFF");
1765 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1767 else if (strcmp (method_name, "Suspend") == 0)
1769 gboolean interactive;
1770 g_variant_get (parameters, "(b)", &interactive);
1771 check_status ("LOGIN1 SUSPEND");
1772 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1774 else if (strcmp (method_name, "CanSuspend") == 0)
1776 check_status ("LOGIN1 CAN-SUSPEND");
1777 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1779 else if (strcmp (method_name, "PowerOff") == 0)
1781 gboolean interactive;
1782 g_variant_get (parameters, "(b)", &interactive);
1783 check_status ("LOGIN1 POWER-OFF");
1784 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1786 else if (strcmp (method_name, "CanHibernate") == 0)
1788 check_status ("LOGIN1 CAN-HIBERNATE");
1789 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1791 else if (strcmp (method_name, "Hibernate") == 0)
1793 gboolean interactive;
1794 g_variant_get (parameters, "(b)", &interactive);
1795 check_status ("LOGIN1 HIBERNATE");
1796 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1799 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1803 login1_name_acquired_cb (GDBusConnection *connection,
1807 const gchar *login1_interface =
1809 " <interface name='org.freedesktop.login1.Manager'>"
1810 " <method name='ListSeats'>"
1811 " <arg name='seats' type='a(so)' direction='out'/>"
1813 " <method name='CreateSession'>"
1814 " <arg name='id' type='s' direction='out'/>"
1815 " <arg name='path' type='o' direction='out'/>"
1817 " <method name='LockSession'>"
1818 " <arg name='id' type='s' direction='in'/>"
1820 " <method name='UnlockSession'>"
1821 " <arg name='id' type='s' direction='in'/>"
1823 " <method name='ActivateSession'>"
1824 " <arg name='id' type='s' direction='in'/>"
1826 " <method name='CanReboot'>"
1827 " <arg name='result' direction='out' type='s'/>"
1829 " <method name='Reboot'>"
1830 " <arg name='interactive' direction='in' type='b'/>"
1832 " <method name='CanPowerOff'>"
1833 " <arg name='result' direction='out' type='s'/>"
1835 " <method name='PowerOff'>"
1836 " <arg name='interactive' direction='in' type='b'/>"
1838 " <method name='CanSuspend'>"
1839 " <arg name='result' direction='out' type='s'/>"
1841 " <method name='Suspend'>"
1842 " <arg name='interactive' direction='in' type='b'/>"
1844 " <method name='CanHibernate'>"
1845 " <arg name='result' direction='out' type='s'/>"
1847 " <method name='Hibernate'>"
1848 " <arg name='interactive' direction='in' type='b'/>"
1850 " <signal name='SeatNew'>"
1851 " <arg name='seat' type='so'/>"
1853 " <signal name='SeatRemoved'>"
1854 " <arg name='seat' type='so'/>"
1858 static const GDBusInterfaceVTable login1_vtable =
1862 GDBusNodeInfo *login1_info;
1863 GError *error = NULL;
1865 login1_info = g_dbus_node_info_new_for_xml (login1_interface, &error);
1867 g_warning ("Failed to parse login1 D-Bus interface: %s", error->message);
1868 g_clear_error (&error);
1871 g_dbus_connection_register_object (connection,
1872 "/org/freedesktop/login1",
1873 login1_info->interfaces[0],
1878 g_warning ("Failed to register login1 service: %s", error->message);
1879 g_clear_error (&error);
1880 g_dbus_node_info_unref (login1_info);
1882 /* We always have seat0 */
1883 add_login1_seat (connection, "seat0", FALSE);
1886 if (service_count == 0)
1891 start_login1_daemon (void)
1894 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1895 "org.freedesktop.login1",
1896 G_BUS_NAME_OWNER_FLAGS_NONE,
1898 login1_name_acquired_cb,
1904 static AccountsUser *
1905 get_accounts_user_by_uid (guint uid)
1909 for (link = accounts_users; link; link = link->next)
1911 AccountsUser *u = link->data;
1919 static AccountsUser *
1920 get_accounts_user_by_name (const gchar *username)
1924 for (link = accounts_users; link; link = link->next)
1926 AccountsUser *u = link->data;
1927 if (strcmp (u->user_name, username) == 0)
1935 accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal)
1937 GError *error = NULL;
1939 user->hidden = hidden;
1941 if (user->hidden && user->id != 0)
1943 g_dbus_connection_unregister_object (accounts_connection, user->id);
1944 g_dbus_connection_emit_signal (accounts_connection,
1946 "/org/freedesktop/Accounts",
1947 "org.freedesktop.Accounts",
1949 g_variant_new ("(o)", user->path),
1952 g_warning ("Failed to emit UserDeleted: %s", error->message);
1953 g_clear_error (&error);
1957 if (!user->hidden && user->id == 0)
1959 user->id = g_dbus_connection_register_object (accounts_connection,
1961 user_info->interfaces[0],
1967 g_warning ("Failed to register user: %s", error->message);
1968 g_clear_error (&error);
1970 g_dbus_connection_emit_signal (accounts_connection,
1972 "/org/freedesktop/Accounts",
1973 "org.freedesktop.Accounts",
1975 g_variant_new ("(o)", user->path),
1978 g_warning ("Failed to emit UserAdded: %s", error->message);
1979 g_clear_error (&error);
1984 load_passwd_file (void)
1986 gchar *path, *data, **lines;
1987 gchar **user_filter = NULL;
1990 if (g_key_file_has_key (config, "test-runner-config", "accounts-service-user-filter", NULL))
1994 filter = g_key_file_get_string (config, "test-runner-config", "accounts-service-user-filter", NULL);
1995 user_filter = g_strsplit (filter, " ", -1);
1999 path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL);
2000 g_file_get_contents (path, &data, NULL, NULL);
2002 lines = g_strsplit (data, "\n", -1);
2005 for (i = 0; lines[i]; i++)
2009 gchar *user_name, *real_name;
2010 AccountsUser *user = NULL;
2012 fields = g_strsplit (lines[i], ":", -1);
2013 if (fields == NULL || g_strv_length (fields) < 7)
2015 g_strfreev (fields);
2019 user_name = fields[0];
2020 uid = atoi (fields[2]);
2021 real_name = fields[4];
2023 user = get_accounts_user_by_uid (uid);
2027 GKeyFile *dmrc_file;
2029 user = g_malloc0 (sizeof (AccountsUser));
2030 accounts_users = g_list_append (accounts_users, user);
2032 /* Only allow users in whitelist */
2033 user->hidden = FALSE;
2038 user->hidden = TRUE;
2039 for (j = 0; user_filter[j] != NULL; j++)
2040 if (strcmp (user_name, user_filter[j]) == 0)
2041 user->hidden = FALSE;
2044 dmrc_file = g_key_file_new ();
2045 path = g_build_filename (temp_dir, "home", user_name, ".dmrc", NULL);
2046 g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_NONE, NULL);
2050 user->user_name = g_strdup (user_name);
2051 user->real_name = g_strdup (real_name);
2052 user->home_directory = g_build_filename (temp_dir, "home", user_name, NULL);
2053 user->language = g_key_file_get_string (dmrc_file, "Desktop", "Language", NULL);
2054 /* DMRC contains a locale, strip the codeset off it to get the language */
2057 gchar *c = strchr (user->language, '.');
2061 user->xsession = g_key_file_get_string (dmrc_file, "Desktop", "Session", NULL);
2062 user->layouts = g_key_file_get_string_list (dmrc_file, "X-Accounts", "Layouts", NULL, NULL);
2065 user->layouts = g_malloc (sizeof (gchar *) * 2);
2066 user->layouts[0] = g_key_file_get_string (dmrc_file, "Desktop", "Layout", NULL);
2067 user->layouts[1] = NULL;
2069 user->has_messages = g_key_file_get_boolean (dmrc_file, "X-Accounts", "HasMessages", NULL);
2070 user->path = g_strdup_printf ("/org/freedesktop/Accounts/User%d", uid);
2071 accounts_user_set_hidden (user, user->hidden, FALSE);
2073 g_key_file_free (dmrc_file);
2076 g_strfreev (fields);
2083 handle_accounts_call (GDBusConnection *connection,
2084 const gchar *sender,
2085 const gchar *object_path,
2086 const gchar *interface_name,
2087 const gchar *method_name,
2088 GVariant *parameters,
2089 GDBusMethodInvocation *invocation,
2092 if (strcmp (method_name, "ListCachedUsers") == 0)
2094 GVariantBuilder builder;
2097 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
2099 load_passwd_file ();
2100 for (link = accounts_users; link; link = link->next)
2102 AccountsUser *user = link->data;
2103 if (!user->hidden && user->uid >= 1000)
2104 g_variant_builder_add_value (&builder, g_variant_new_object_path (user->path));
2107 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(ao)", &builder));
2109 else if (strcmp (method_name, "FindUserByName") == 0)
2111 AccountsUser *user = NULL;
2114 g_variant_get (parameters, "(&s)", &user_name);
2116 load_passwd_file ();
2117 user = get_accounts_user_by_name (user_name);
2121 accounts_user_set_hidden (user, FALSE, TRUE);
2122 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", user->path));
2125 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such user: %s", user_name);
2128 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
2132 handle_user_call (GDBusConnection *connection,
2133 const gchar *sender,
2134 const gchar *object_path,
2135 const gchar *interface_name,
2136 const gchar *method_name,
2137 GVariant *parameters,
2138 GDBusMethodInvocation *invocation,
2141 AccountsUser *user = user_data;
2143 if (strcmp (method_name, "SetXSession") == 0)
2147 g_variant_get (parameters, "(&s)", &xsession);
2149 g_free (user->xsession);
2150 user->xsession = g_strdup (xsession);
2152 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
2154 /* And notify others that it took */
2155 g_dbus_connection_emit_signal (accounts_connection,
2158 "org.freedesktop.Accounts.User",
2160 g_variant_new ("()"),
2164 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
2168 handle_user_get_property (GDBusConnection *connection,
2169 const gchar *sender,
2170 const gchar *object_path,
2171 const gchar *interface_name,
2172 const gchar *property_name,
2176 AccountsUser *user = user_data;
2178 if (strcmp (property_name, "UserName") == 0)
2179 return g_variant_new_string (user->user_name);
2180 else if (strcmp (property_name, "RealName") == 0)
2181 return g_variant_new_string (user->real_name);
2182 else if (strcmp (property_name, "HomeDirectory") == 0)
2183 return g_variant_new_string (user->home_directory);
2184 else if (strcmp (property_name, "SystemAccount") == 0)
2185 return g_variant_new_boolean (user->uid < 1000);
2186 else if (strcmp (property_name, "BackgroundFile") == 0)
2187 return g_variant_new_string (user->background ? user->background : "");
2188 else if (strcmp (property_name, "Language") == 0)
2189 return g_variant_new_string (user->language ? user->language : "");
2190 else if (strcmp (property_name, "IconFile") == 0)
2191 return g_variant_new_string (user->image ? user->image : "");
2192 else if (strcmp (property_name, "Shell") == 0)
2193 return g_variant_new_string ("/bin/sh");
2194 else if (strcmp (property_name, "Uid") == 0)
2195 return g_variant_new_uint64 (user->uid);
2196 else if (strcmp (property_name, "XSession") == 0)
2197 return g_variant_new_string (user->xsession ? user->xsession : "");
2198 else if (strcmp (property_name, "XKeyboardLayouts") == 0)
2200 if (user->layouts != NULL)
2201 return g_variant_new_strv ((const gchar * const *) user->layouts, -1);
2203 return g_variant_new_strv (NULL, 0);
2205 else if (strcmp (property_name, "XHasMessages") == 0)
2206 return g_variant_new_boolean (user->has_messages);
2212 accounts_name_acquired_cb (GDBusConnection *connection,
2216 const gchar *accounts_interface =
2218 " <interface name='org.freedesktop.Accounts'>"
2219 " <method name='ListCachedUsers'>"
2220 " <arg name='user' direction='out' type='ao'/>"
2222 " <method name='FindUserByName'>"
2223 " <arg name='name' direction='in' type='s'/>"
2224 " <arg name='user' direction='out' type='o'/>"
2226 " <signal name='UserAdded'>"
2227 " <arg name='user' type='o'/>"
2229 " <signal name='UserDeleted'>"
2230 " <arg name='user' type='o'/>"
2234 static const GDBusInterfaceVTable accounts_vtable =
2236 handle_accounts_call,
2238 const gchar *user_interface =
2240 " <interface name='org.freedesktop.Accounts.User'>"
2241 " <method name='SetXSession'>"
2242 " <arg name='x_session' direction='in' type='s'/>"
2244 " <property name='UserName' type='s' access='read'/>"
2245 " <property name='RealName' type='s' access='read'/>"
2246 " <property name='HomeDirectory' type='s' access='read'/>"
2247 " <property name='SystemAccount' type='b' access='read'/>"
2248 " <property name='BackgroundFile' type='s' access='read'/>"
2249 " <property name='Language' type='s' access='read'/>"
2250 " <property name='IconFile' type='s' access='read'/>"
2251 " <property name='Shell' type='s' access='read'/>"
2252 " <property name='Uid' type='t' access='read'/>"
2253 " <property name='XSession' type='s' access='read'/>"
2254 " <property name='XKeyboardLayouts' type='as' access='read'/>"
2255 " <property name='XHasMessages' type='b' access='read'/>"
2256 " <signal name='Changed' />"
2259 GError *error = NULL;
2261 accounts_connection = connection;
2263 accounts_info = g_dbus_node_info_new_for_xml (accounts_interface, &error);
2265 g_warning ("Failed to parse D-Bus interface: %s", error->message);
2266 g_clear_error (&error);
2269 user_info = g_dbus_node_info_new_for_xml (user_interface, &error);
2271 g_warning ("Failed to parse D-Bus interface: %s", error->message);
2272 g_clear_error (&error);
2275 g_dbus_connection_register_object (connection,
2276 "/org/freedesktop/Accounts",
2277 accounts_info->interfaces[0],
2283 g_warning ("Failed to register accounts service: %s", error->message);
2284 g_clear_error (&error);
2285 g_dbus_node_info_unref (accounts_info);
2288 if (service_count == 0)
2293 start_accounts_service_daemon (void)
2296 g_bus_own_name (G_BUS_TYPE_SYSTEM,
2297 "org.freedesktop.Accounts",
2298 G_BUS_NAME_OWNER_FLAGS_NONE,
2299 accounts_name_acquired_cb,
2313 signal_cb (gpointer user_data)
2315 g_print ("Caught signal, quitting\n");
2316 quit (EXIT_FAILURE);
2321 properties_changed_cb (GDBusConnection *connection,
2322 const gchar *sender_name,
2323 const gchar *object_path,
2324 const gchar *interface_name,
2325 const gchar *signal_name,
2326 GVariant *parameters,
2329 const gchar *interface, *name;
2332 GVariantIter *changed_properties, *invalidated_properties;
2335 g_variant_get (parameters, "(&sa{sv}as)", &interface, &changed_properties, &invalidated_properties);
2337 status = g_string_new ("RUNNER DBUS-PROPERTIES-CHANGED");
2338 g_string_append_printf (status, " PATH=%s", object_path);
2339 g_string_append_printf (status, " INTERFACE=%s", interface);
2340 for (i = 0; g_variant_iter_loop (changed_properties, "{&sv}", &name, &value); i++)
2343 g_string_append (status, " CHANGED=");
2345 g_string_append (status, ",");
2346 g_string_append (status, name);
2347 if (g_variant_is_of_type (value, G_VARIANT_TYPE ("ao")))
2352 g_variant_iter_init (&iter, value);
2353 while (g_variant_iter_loop (&iter, "&o", &path))
2354 g_string_append_printf (status, ":%s", path);
2357 for (i = 0; g_variant_iter_loop (invalidated_properties, "&s", &name); i++)
2360 g_string_append (status, " INVALIDATED=");
2362 g_string_append (status, ",");
2363 g_string_append (status, name);
2366 check_status (status->str);
2367 g_string_free (status, TRUE);
2371 dbus_signal_cb (GDBusConnection *connection,
2372 const gchar *sender_name,
2373 const gchar *object_path,
2374 const gchar *interface_name,
2375 const gchar *signal_name,
2376 GVariant *parameters,
2381 status = g_string_new ("RUNNER DBUS-SIGNAL");
2382 g_string_append_printf (status, " PATH=%s", object_path);
2383 g_string_append_printf (status, " INTERFACE=%s", interface_name);
2384 g_string_append_printf (status, " NAME=%s", signal_name);
2386 check_status (status->str);
2387 g_string_free (status, TRUE);
2391 main (int argc, char **argv)
2395 gchar *greeter = NULL, *script_name, *config_file, *additional_system_config;
2396 gchar *additional_config, *path, *path1, *path2, *ld_preload, *ld_library_path, *home_dir;
2397 GString *passwd_data, *group_data;
2398 GSource *status_source;
2400 GError *error = NULL;
2402 #if !defined(GLIB_VERSION_2_36)
2406 loop = g_main_loop_new (NULL, FALSE);
2408 g_unix_signal_add (SIGINT, signal_cb, NULL);
2409 g_unix_signal_add (SIGTERM, signal_cb, NULL);
2411 children = g_hash_table_new (g_direct_hash, g_direct_equal);
2415 g_printerr ("Usage %s SCRIPT-NAME GREETER\n", argv[0]);
2416 quit (EXIT_FAILURE);
2418 script_name = argv[1];
2419 config_file = g_strdup_printf ("%s.conf", script_name);
2420 config_path = g_build_filename (SRCDIR, "tests", "scripts", config_file, NULL);
2421 g_free (config_file);
2423 config = g_key_file_new ();
2424 g_key_file_load_from_file (config, config_path, G_KEY_FILE_NONE, NULL);
2426 load_script (config_path);
2428 if (!getcwd (cwd, 1024))
2430 g_critical ("Error getting current directory: %s", strerror (errno));
2431 quit (EXIT_FAILURE);
2434 /* Don't contact our X server */
2435 g_unsetenv ("DISPLAY");
2437 /* Don't let XDG vars from system affect tests */
2438 g_unsetenv ("XDG_CONFIG_DIRS");
2439 g_unsetenv ("XDG_DATA_DIRS");
2441 /* Override system calls */
2442 ld_preload = g_build_filename (BUILDDIR, "tests", "src", ".libs", "libsystem.so", NULL);
2443 g_setenv ("LD_PRELOAD", ld_preload, TRUE);
2444 g_free (ld_preload);
2446 /* Run test programs */
2447 path = g_strdup_printf ("%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, g_getenv ("PATH"));
2448 g_setenv ("PATH", path, TRUE);
2451 /* Use locally built libraries */
2452 path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL);
2453 path2 = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL);
2454 ld_library_path = g_strdup_printf ("%s:%s", path1, path2);
2457 g_setenv ("LD_LIBRARY_PATH", ld_library_path, TRUE);
2458 g_free (ld_library_path);
2459 path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL);
2460 g_setenv ("GI_TYPELIB_PATH", path1, TRUE);
2463 /* Run in a temporary directory inside the build directory */
2464 /* Note we have to pick a name that is short since Unix sockets in this directory have a 108 character limit on their paths */
2469 name = g_strdup_printf (".r%d", i);
2471 temp_dir = g_build_filename ("/tmp", name, NULL);
2473 if (!g_file_test (temp_dir, G_FILE_TEST_EXISTS))
2477 g_mkdir_with_parents (temp_dir, 0755);
2478 g_setenv ("LIGHTDM_TEST_ROOT", temp_dir, TRUE);
2480 /* Open socket for status */
2481 /* Note we have to pick a socket name that is short since there is a 108 character limit on the name */
2482 status_socket_name = g_build_filename (temp_dir, ".s", NULL);
2483 unlink (status_socket_name);
2484 status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
2486 g_warning ("Error creating status socket %s: %s", status_socket_name, error->message);
2487 g_clear_error (&error);
2490 GSocketAddress *address;
2493 address = g_unix_socket_address_new (status_socket_name);
2494 result = g_socket_bind (status_socket, address, FALSE, &error);
2495 g_object_unref (address);
2497 g_warning ("Error binding status socket %s: %s", status_socket_name, error->message);
2498 g_clear_error (&error);
2501 result = g_socket_listen (status_socket, &error);
2503 g_warning ("Error listening on status socket %s: %s", status_socket_name, error->message);
2504 g_clear_error (&error);
2508 g_object_unref (status_socket);
2509 status_socket = NULL;
2513 quit (EXIT_FAILURE);
2514 status_source = g_socket_create_source (status_socket, G_IO_IN, NULL);
2515 g_source_set_callback (status_source, status_connect_cb, NULL, NULL);
2516 g_source_attach (status_source, NULL);
2518 /* Set up a skeleton file system */
2519 g_mkdir_with_parents (g_strdup_printf ("%s/etc", temp_dir), 0755);
2520 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share", temp_dir), 0755);
2521 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/sessions", temp_dir), 0755);
2522 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/remote-sessions", temp_dir), 0755);
2523 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/greeters", temp_dir), 0755);
2524 g_mkdir_with_parents (g_strdup_printf ("%s/tmp", temp_dir), 0755);
2525 g_mkdir_with_parents (g_strdup_printf ("%s/var/lib/lightdm-data", temp_dir), 0755);
2526 g_mkdir_with_parents (g_strdup_printf ("%s/var/run", temp_dir), 0755);
2527 g_mkdir_with_parents (g_strdup_printf ("%s/var/log", temp_dir), 0755);
2529 /* Copy over the configuration */
2530 g_mkdir_with_parents (g_strdup_printf ("%s/etc/lightdm", temp_dir), 0755);
2531 if (!g_key_file_has_key (config, "test-runner-config", "have-config", NULL) || g_key_file_get_boolean (config, "test-runner-config", "have-config", NULL))
2532 if (system (g_strdup_printf ("cp %s %s/etc/lightdm/lightdm.conf", config_path, temp_dir)))
2533 perror ("Failed to copy configuration");
2535 additional_system_config = g_key_file_get_string (config, "test-runner-config", "additional-system-config", NULL);
2536 if (additional_system_config)
2540 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/lightdm.conf.d", temp_dir), 0755);
2542 files = g_strsplit (additional_system_config, " ", -1);
2543 for (i = 0; files[i]; i++)
2544 if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/usr/share/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir)))
2545 perror ("Failed to copy configuration");
2549 additional_config = g_key_file_get_string (config, "test-runner-config", "additional-config", NULL);
2550 if (additional_config)
2554 g_mkdir_with_parents (g_strdup_printf ("%s/etc/xdg/lightdm/lightdm.conf.d", temp_dir), 0755);
2556 files = g_strsplit (additional_config, " ", -1);
2557 for (i = 0; files[i]; i++)
2558 if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/etc/xdg/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir)))
2559 perror ("Failed to copy configuration");
2563 if (g_key_file_has_key (config, "test-runner-config", "shared-data-dirs", NULL))
2569 dir_string = g_key_file_get_string (config, "test-runner-config", "shared-data-dirs", NULL);
2570 dirs = g_strsplit (dir_string, " ", -1);
2571 g_free (dir_string);
2573 for (i = 0; dirs[i]; i++)
2575 gchar **fields = g_strsplit (dirs[i], ":", -1);
2576 if (g_strv_length (fields) == 4)
2578 gchar *path = g_strdup_printf ("%s/var/lib/lightdm-data/%s", temp_dir, fields[0]);
2579 int uid = g_ascii_strtoll (fields[1], NULL, 10);
2580 int gid = g_ascii_strtoll (fields[2], NULL, 10);
2581 int mode = g_ascii_strtoll (fields[3], NULL, 8);
2582 g_mkdir (path, mode);
2583 g_chmod (path, mode); /* mkdir filters by umask, so make sure we have what we want */
2584 if (chown (path, uid, gid) < 0)
2585 g_warning ("chown (%s) failed: %s", path, strerror (errno));
2588 g_strfreev (fields);
2594 /* Always copy the script */
2595 if (system (g_strdup_printf ("cp %s %s/script", config_path, temp_dir)))
2596 perror ("Failed to copy configuration");
2598 /* Copy over the greeter files */
2599 if (system (g_strdup_printf ("cp %s/sessions/* %s/usr/share/lightdm/sessions", DATADIR, temp_dir)))
2600 perror ("Failed to copy sessions");
2601 if (system (g_strdup_printf ("cp %s/remote-sessions/* %s/usr/share/lightdm/remote-sessions", DATADIR, temp_dir)))
2602 perror ("Failed to copy remote sessions");
2603 if (system (g_strdup_printf ("cp %s/greeters/* %s/usr/share/lightdm/greeters", DATADIR, temp_dir)))
2604 perror ("Failed to copy greeters");
2606 /* Set up the default greeter */
2607 path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL);
2608 greeter = g_strdup_printf ("%s.desktop", argv[2]);
2609 if (symlink (greeter, path) < 0)
2611 g_printerr ("Failed to make greeter symlink %s->%s: %s\n", path, greeter, strerror (errno));
2612 quit (EXIT_FAILURE);
2617 home_dir = g_build_filename (temp_dir, "home", NULL);
2619 /* Make fake users */
2629 {"root", "", "root", 0},
2630 /* Unprivileged account for greeters */
2631 {"lightdm", "", "", 100},
2632 /* These accounts have a password */
2633 {"have-password1", "password", "Password User 1", 1000},
2634 {"have-password2", "password", "Password User 2", 1001},
2635 {"have-password3", "password", "Password User 3", 1002},
2636 {"have-password4", "password", "Password User 4", 1003},
2637 /* This account always prompts for a password, even if using the lightdm-autologin service */
2638 {"always-password", "password", "Password User 4", 1004},
2639 /* These accounts have no password */
2640 {"no-password1", "", "No Password User 1", 1005},
2641 {"no-password2", "", "No Password User 2", 1006},
2642 {"no-password3", "", "No Password User 3", 1007},
2643 {"no-password4", "", "No Password User 4", 1008},
2644 /* This account has a keyboard layout */
2645 {"have-layout", "", "Layout User", 1009},
2646 /* This account has a set of keyboard layouts */
2647 {"have-layouts", "", "Layouts User", 1010},
2648 /* This account has a language set */
2649 {"have-language", "", "Language User", 1011},
2650 /* This account has a preconfigured session */
2651 {"have-session", "", "Session User", 1012},
2652 /* This account has the home directory mounted on login */
2653 {"mount-home-dir", "", "Mounted Home Dir User", 1013},
2654 /* This account is denied access */
2655 {"denied", "", "Denied User", 1014},
2656 /* This account has expired */
2657 {"expired", "", "Expired User", 1015},
2658 /* This account needs a password change */
2659 {"new-authtok", "", "New Token User", 1016},
2660 /* This account is switched to change-user2 when authentication succeeds */
2661 {"change-user1", "", "Change User 1", 1017},
2662 {"change-user2", "", "Change User 2", 1018},
2663 /* This account switches to invalid-user when authentication succeeds */
2664 {"change-user-invalid", "", "Invalid Change User", 1019},
2665 /* This account crashes on authentication */
2666 {"crash-authenticate", "", "Crash Auth User", 1020},
2667 /* This account shows an informational prompt on login */
2668 {"info-prompt", "password", "Info Prompt", 1021},
2669 /* This account shows multiple informational prompts on login */
2670 {"multi-info-prompt","password", "Multi Info Prompt", 1022},
2671 /* This account uses two factor authentication */
2672 {"two-factor", "password", "Two Factor", 1023},
2673 /* This account has a special group */
2674 {"group-member", "password", "Group Member", 1024},
2675 /* This account has the home directory created when the session starts */
2676 {"make-home-dir", "", "Make Home Dir User", 1025},
2677 /* This account fails to open a session */
2678 {"session-error", "password", "Session Error", 1026},
2679 /* This account can't establish credentials */
2680 {"cred-error", "password", "Cred Error", 1027},
2681 /* This account has expired credentials */
2682 {"cred-expired", "password", "Cred Expired", 1028},
2683 /* This account has cannot access their credentials */
2684 {"cred-unavail", "password", "Cred Unavail", 1029},
2685 /* This account sends informational messages for each PAM function that is called */
2686 {"log-pam", "password", "Log PAM", 1030},
2687 /* This account shows multiple prompts on login */
2688 {"multi-prompt", "password", "Multi Prompt", 1031},
2689 /* This account has an existing corrupt X authority */
2690 {"corrupt-xauth", "password", "Corrupt Xauthority", 1032},
2691 /* User to test properties */
2692 {"prop-user", "", "TEST", 1033},
2693 {NULL, NULL, NULL, 0}
2695 passwd_data = g_string_new ("");
2696 group_data = g_string_new ("");
2697 for (i = 0; users[i].user_name; i++)
2699 GKeyFile *dmrc_file;
2700 gboolean save_dmrc = FALSE;
2702 if (strcmp (users[i].user_name, "mount-home-dir") != 0 && strcmp (users[i].user_name, "make-home-dir") != 0)
2704 path = g_build_filename (home_dir, users[i].user_name, NULL);
2705 g_mkdir_with_parents (path, 0755);
2706 if (chown (path, users[i].uid, users[i].uid) < 0)
2707 g_debug ("chown (%s) failed: %s", path, strerror (errno));
2711 dmrc_file = g_key_file_new ();
2712 if (strcmp (users[i].user_name, "have-session") == 0)
2714 g_key_file_set_string (dmrc_file, "Desktop", "Session", "alternative");
2717 if (strcmp (users[i].user_name, "have-layout") == 0)
2719 g_key_file_set_string (dmrc_file, "Desktop", "Layout", "us");
2722 if (strcmp (users[i].user_name, "have-layouts") == 0)
2724 g_key_file_set_string (dmrc_file, "Desktop", "Layout", "ru");
2725 g_key_file_set_string (dmrc_file, "X-Accounts", "Layouts", "fr\toss;ru;");
2728 if (strcmp (users[i].user_name, "have-language") == 0)
2730 g_key_file_set_string (dmrc_file, "Desktop", "Language", "en_AU.utf8");
2738 path = g_build_filename (home_dir, users[i].user_name, ".dmrc", NULL);
2739 data = g_key_file_to_data (dmrc_file, NULL, NULL);
2740 g_file_set_contents (path, data, -1, NULL);
2745 g_key_file_free (dmrc_file);
2747 /* Write corrupt X authority file */
2748 if (strcmp (users[i].user_name, "corrupt-xauth") == 0)
2750 gchar data[1] = { 0xFF };
2752 path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL);
2753 g_file_set_contents (path, data, 1, NULL);
2754 chmod (path, S_IRUSR | S_IWUSR);
2758 /* Add passwd file entry */
2759 g_string_append_printf (passwd_data, "%s:%s:%d:%d:%s:%s/home/%s:/bin/sh\n", users[i].user_name, users[i].password, users[i].uid, users[i].uid, users[i].real_name, temp_dir, users[i].user_name);
2761 /* Add group file entry */
2762 g_string_append_printf (group_data, "%s:x:%d:%s\n", users[i].user_name, users[i].uid, users[i].user_name);
2764 path = g_build_filename (temp_dir, "etc", "passwd", NULL);
2765 g_file_set_contents (path, passwd_data->str, -1, NULL);
2767 g_string_free (passwd_data, TRUE);
2769 /* Add an extra test group */
2770 g_string_append_printf (group_data, "test-group:x:111:\n");
2772 path = g_build_filename (temp_dir, "etc", "group", NULL);
2773 g_file_set_contents (path, group_data->str, -1, NULL);
2775 g_string_free (group_data, TRUE);
2777 if (g_key_file_has_key (config, "test-runner-config", "timeout", NULL))
2778 status_timeout_ms = g_key_file_get_integer (config, "test-runner-config", "timeout", NULL) * 1000;
2780 /* Start D-Bus services */
2781 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-upower", NULL))
2782 start_upower_daemon ();
2783 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-console-kit", NULL))
2784 start_console_kit_daemon ();
2785 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-login1", NULL))
2786 start_login1_daemon ();
2787 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-accounts-service", NULL))
2788 start_accounts_service_daemon ();
2790 /* Listen for daemon bus events */
2791 if (g_key_file_get_boolean (config, "test-runner-config", "log-dbus", NULL))
2793 g_dbus_connection_signal_subscribe (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
2794 "org.freedesktop.DisplayManager",
2795 "org.freedesktop.DBus.Properties",
2796 "PropertiesChanged",
2799 G_DBUS_SIGNAL_FLAGS_NONE,
2800 properties_changed_cb,
2803 g_dbus_connection_signal_subscribe (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
2804 "org.freedesktop.DisplayManager",
2805 "org.freedesktop.DisplayManager",
2809 G_DBUS_SIGNAL_FLAGS_NONE,
2815 g_main_loop_run (loop);
2817 return EXIT_FAILURE;