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 GDBusConnection *ck_connection = NULL;
85 static GDBusNodeInfo *ck_session_info;
93 static GList *ck_sessions = NULL;
94 static gint ck_session_index = 0;
95 static void handle_ck_session_call (GDBusConnection *connection,
97 const gchar *object_path,
98 const gchar *interface_name,
99 const gchar *method_name,
100 GVariant *parameters,
101 GDBusMethodInvocation *invocation,
103 static const GDBusInterfaceVTable ck_session_vtable =
105 handle_ck_session_call,
115 static GList *login1_sessions = NULL;
116 static gint login1_session_index = 0;
123 static GList *status_clients = NULL;
125 static void ready (void);
126 static void quit (int status);
127 static void check_status (const gchar *status);
128 static AccountsUser *get_accounts_user_by_uid (guint uid);
129 static AccountsUser *get_accounts_user_by_name (const gchar *username);
130 static void accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal);
133 kill_timeout_cb (gpointer data)
135 Process *process = data;
137 process->kill_timeout = 0;
139 if (getenv ("DEBUG"))
140 g_print ("Sending SIGKILL to process %d\n", process->pid);
141 kill (process->pid, SIGKILL);
147 stop_process (Process *process)
149 if (process->kill_timeout != 0)
152 if (getenv ("DEBUG"))
153 g_print ("Sending SIGTERM to process %d\n", process->pid);
154 kill (process->pid, SIGTERM);
155 process->kill_timeout = g_timeout_add (KILL_TIMEOUT, kill_timeout_cb, process);
159 process_exit_cb (GPid pid, gint status, gpointer data)
164 if (getenv ("DEBUG"))
166 if (WIFEXITED (status))
167 g_print ("Process %d exited with status %d\n", pid, WEXITSTATUS (status));
169 g_print ("Process %d terminated with signal %d\n", pid, WTERMSIG (status));
172 if (lightdm_process && pid == lightdm_process->pid)
174 process = lightdm_process;
175 lightdm_process = NULL;
176 if (WIFEXITED (status))
177 status_text = g_strdup_printf ("RUNNER DAEMON-EXIT STATUS=%d", WEXITSTATUS (status));
179 status_text = g_strdup_printf ("RUNNER DAEMON-TERMINATE SIGNAL=%d", WTERMSIG (status));
180 check_status (status_text);
184 process = g_hash_table_lookup (children, GINT_TO_POINTER (pid));
187 g_hash_table_remove (children, GINT_TO_POINTER (pid));
190 if (process->kill_timeout)
191 g_source_remove (process->kill_timeout);
192 process->kill_timeout = 0;
194 /* Quit once all children have stopped */
200 watch_process (pid_t pid)
204 process = g_malloc0 (sizeof (Process));
206 process->kill_timeout = 0;
208 if (getenv ("DEBUG"))
209 g_print ("Watching process %d\n", process->pid);
210 g_child_watch_add (process->pid, process_exit_cb, NULL);
221 exit_status = status;
224 /* Stop all the children */
225 g_hash_table_iter_init (&iter, children);
230 if (!g_hash_table_iter_next (&iter, &key, &value))
233 stop_process ((Process *)value);
236 /* Don't quit until all children are stopped */
237 if (g_hash_table_size (children) > 0)
240 /* Stop the daemon */
243 stop_process (lightdm_process);
247 if (status_socket_name)
248 unlink (status_socket_name);
250 if (temp_dir && getenv ("DEBUG") == NULL)
252 gchar *command = g_strdup_printf ("rm -rf %s", temp_dir);
253 if (system (command))
254 perror ("Failed to delete temp directory");
261 fail (const gchar *event, const gchar *expected)
268 g_printerr ("Command line: %s", test_runner_command);
269 g_printerr ("Events:\n");
270 for (link = statuses; link; link = link->next)
271 g_printerr (" %s\n", (gchar *)link->data);
273 g_printerr (" %s\n", event);
275 g_printerr (" ^^^ expected \"%s\"\n", expected);
277 g_printerr ("^^^ expected nothing\n");
283 get_prefix (const gchar *text)
288 prefix = g_strdup (text);
289 for (i = 0; prefix[i] != '\0' && prefix[i] != ' '; i++);
296 get_script_line (const gchar *prefix)
300 for (link = script; link; link = link->next)
302 ScriptLine *line = link->data;
304 /* Ignore lines with other prefixes */
310 p = get_prefix (line->text);
311 matches = strcmp (prefix, p) == 0;
326 stop_loop (gpointer user_data)
328 g_main_loop_quit ((GMainLoop *)user_data);
329 return G_SOURCE_REMOVE;
333 handle_command (const gchar *command)
340 while (*c && !isspace (*c))
342 name = g_strdup_printf ("%.*s", (int) (c - command), command);
344 params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
348 gchar *param_name, *param_value;
353 while (*c && !isspace (*c) && *c != '=')
358 param_name = g_strdup_printf ("%.*s", (int) (c - start), start);
367 gboolean escaped = FALSE;
371 value = g_string_new ("");
378 g_string_append_c (value, '\\');
384 else if (!escaped && *c == '\"')
387 g_string_append_c (value, *c);
390 param_value = value->str;
391 g_string_free (value, FALSE);
398 while (*c && !isspace (*c))
400 param_value = g_strdup_printf ("%.*s", (int) (c - start), start);
404 param_value = g_strdup ("");
406 g_hash_table_insert (params, param_name, param_value);
409 if (strcmp (name, "START-DAEMON") == 0)
411 GString *command_line;
412 gchar **lightdm_argv;
414 GError *error = NULL;
416 command_line = g_string_new ("lightdm");
417 if (getenv ("DEBUG"))
418 g_string_append (command_line, " --debug");
419 g_string_append_printf (command_line, " --cache-dir %s/cache", temp_dir);
421 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",
422 g_getenv ("PATH"), g_getenv ("LD_PRELOAD"), g_getenv ("LD_LIBRARY_PATH"), g_getenv ("LIGHTDM_TEST_ROOT"), g_getenv ("DBUS_SESSION_BUS_ADDRESS"),
425 if (!g_shell_parse_argv (command_line->str, NULL, &lightdm_argv, &error))
427 g_warning ("Error parsing command line: %s", error->message);
430 g_clear_error (&error);
432 if (!g_spawn_async (NULL, lightdm_argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &lightdm_pid, &error))
434 g_warning ("Error launching LightDM: %s", error->message);
437 g_clear_error (&error);
438 lightdm_process = watch_process (lightdm_pid);
440 check_status ("RUNNER DAEMON-START");
442 else if (strcmp (name, "WAIT") == 0)
444 /* Use a main loop so that our DBus functions are still responsive */
445 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
446 g_timeout_add_seconds (1, stop_loop, loop);
447 g_main_loop_run (loop);
448 g_main_loop_unref (loop);
450 else if (strcmp (name, "LIST-SEATS") == 0)
452 GVariant *result, *value;
458 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
459 "org.freedesktop.DisplayManager",
460 "/org/freedesktop/DisplayManager",
461 "org.freedesktop.DBus.Properties",
463 g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Seats"),
464 G_VARIANT_TYPE ("(v)"),
465 G_DBUS_CALL_FLAGS_NONE,
470 status = g_string_new ("RUNNER LIST-SEATS SEATS=");
471 g_variant_get (result, "(v)", &value);
472 g_variant_get (value, "ao", &iter);
473 while (g_variant_iter_loop (iter, "&o", &path))
476 g_string_append (status, ",");
477 g_string_append (status, path);
480 g_variant_unref (value);
481 g_variant_unref (result);
483 check_status (status->str);
484 g_string_free (status, TRUE);
486 else if (strcmp (name, "LIST-SESSIONS") == 0)
488 GVariant *result, *value;
494 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
495 "org.freedesktop.DisplayManager",
496 "/org/freedesktop/DisplayManager",
497 "org.freedesktop.DBus.Properties",
499 g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Sessions"),
500 G_VARIANT_TYPE ("(v)"),
501 G_DBUS_CALL_FLAGS_NONE,
506 status = g_string_new ("RUNNER LIST-SESSIONS SESSIONS=");
507 g_variant_get (result, "(v)", &value);
508 g_variant_get (value, "ao", &iter);
509 while (g_variant_iter_loop (iter, "&o", &path))
512 g_string_append (status, ",");
513 g_string_append (status, path);
516 g_variant_unref (value);
517 g_variant_unref (result);
519 check_status (status->str);
520 g_string_free (status, TRUE);
522 else if (strcmp (name, "SWITCH-TO-GREETER") == 0)
524 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
525 "org.freedesktop.DisplayManager",
526 "/org/freedesktop/DisplayManager/Seat0",
527 "org.freedesktop.DisplayManager.Seat",
529 g_variant_new ("()"),
530 G_VARIANT_TYPE ("()"),
531 G_DBUS_CALL_FLAGS_NONE,
535 check_status ("RUNNER SWITCH-TO-GREETER");
537 else if (strcmp (name, "SWITCH-TO-USER") == 0)
539 gchar *status_text, *username;
541 username = g_hash_table_lookup (params, "USERNAME");
542 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
543 "org.freedesktop.DisplayManager",
544 "/org/freedesktop/DisplayManager/Seat0",
545 "org.freedesktop.DisplayManager.Seat",
547 g_variant_new ("(ss)", username, ""),
548 G_VARIANT_TYPE ("()"),
549 G_DBUS_CALL_FLAGS_NONE,
553 status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s", username);
554 check_status (status_text);
555 g_free (status_text);
557 else if (strcmp (name, "SWITCH-TO-GUEST") == 0)
559 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
560 "org.freedesktop.DisplayManager",
561 "/org/freedesktop/DisplayManager/Seat0",
562 "org.freedesktop.DisplayManager.Seat",
564 g_variant_new ("(s)", ""),
565 G_VARIANT_TYPE ("()"),
566 G_DBUS_CALL_FLAGS_NONE,
570 check_status ("RUNNER SWITCH-TO-GUEST");
572 else if (strcmp (name, "STOP-DAEMON") == 0)
573 stop_process (lightdm_process);
574 // FIXME: Make generic RUN-COMMAND
575 else if (strcmp (name, "START-XSERVER") == 0)
577 gchar *xserver_args, *command_line;
581 GError *error = NULL;
583 xserver_args = g_hash_table_lookup (params, "ARGS");
586 command_line = g_strdup_printf ("%s/tests/src/X %s", BUILDDIR, xserver_args);
588 if (!g_shell_parse_argv (command_line, NULL, &argv, &error) ||
589 !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error))
591 g_printerr ("Error starting X server: %s", error->message);
596 process = watch_process (pid);
597 g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process);
600 else if (strcmp (name, "START-VNC-CLIENT") == 0)
602 gchar *vnc_client_args, *command_line;
606 GError *error = NULL;
608 vnc_client_args = g_hash_table_lookup (params, "ARGS");
609 if (!vnc_client_args)
610 vnc_client_args = "";
611 command_line = g_strdup_printf ("%s/tests/src/vnc-client %s", BUILDDIR, vnc_client_args);
613 if (!g_shell_parse_argv (command_line, NULL, &argv, &error) ||
614 !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error))
616 g_printerr ("Error starting VNC client: %s", error->message);
621 process = watch_process (pid);
622 g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process);
625 else if (strcmp (name, "ADD-USER") == 0)
627 gchar *status_text, *username;
630 username = g_hash_table_lookup (params, "USERNAME");
631 user = get_accounts_user_by_name (username);
633 accounts_user_set_hidden (user, FALSE, TRUE);
635 g_warning ("Unknown user %s", username);
637 status_text = g_strdup_printf ("RUNNER ADD-USER USERNAME=%s", username);
638 check_status (status_text);
639 g_free (status_text);
641 else if (strcmp (name, "UPDATE-USER") == 0)
643 GString *status_text;
646 GError *error = NULL;
648 status_text = g_string_new ("RUNNER UPDATE-USER USERNAME=");
650 username = g_hash_table_lookup (params, "USERNAME");
651 g_string_append (status_text, username);
652 user = get_accounts_user_by_name (username);
655 if (g_hash_table_lookup (params, "NAME"))
657 user->user_name = g_strdup (g_hash_table_lookup (params, "NAME"));
658 g_string_append_printf (status_text, " NAME=%s", user->user_name);
660 if (g_hash_table_lookup (params, "REAL-NAME"))
662 user->real_name = g_strdup (g_hash_table_lookup (params, "REAL-NAME"));
663 g_string_append_printf (status_text, " REAL-NAME=%s", user->real_name);
665 if (g_hash_table_lookup (params, "HOME-DIRECTORY"))
667 user->home_directory = g_strdup (g_hash_table_lookup (params, "HOME-DIRECTORY"));
668 g_string_append_printf (status_text, " HOME-DIRECTORY=%s", user->home_directory);
670 if (g_hash_table_lookup (params, "IMAGE"))
672 user->image = g_strdup (g_hash_table_lookup (params, "IMAGE"));
673 g_string_append_printf (status_text, " IMAGE=%s", user->image);
675 if (g_hash_table_lookup (params, "BACKGROUND"))
677 user->background = g_strdup (g_hash_table_lookup (params, "BACKGROUND"));
678 g_string_append_printf (status_text, " BACKGROUND=%s", user->background);
680 if (g_hash_table_lookup (params, "LANGUAGE"))
682 user->language = g_strdup (g_hash_table_lookup (params, "LANGUAGE"));
683 g_string_append_printf (status_text, " LANGUAGE=%s", user->language);
685 if (g_hash_table_lookup (params, "LAYOUTS"))
687 const gchar *value = g_hash_table_lookup (params, "LAYOUTS");
688 user->layouts = g_strsplit (value, ";", -1);
689 g_string_append_printf (status_text, " LAYOUTS=%s", value);
691 if (g_hash_table_lookup (params, "HAS-MESSAGES"))
693 user->has_messages = g_strcmp0 (g_hash_table_lookup (params, "HAS-MESSAGES"), "TRUE") == 0;
694 g_string_append_printf (status_text, " HAS-MESSAGES=%s", user->has_messages ? "TRUE" : "FALSE");
696 if (g_hash_table_lookup (params, "SESSION"))
698 user->xsession = g_strdup (g_hash_table_lookup (params, "SESSION"));
699 g_string_append_printf (status_text, " SESSION=%s", user->xsession);
703 g_warning ("Unknown user %s", username);
705 g_dbus_connection_emit_signal (accounts_connection,
708 "org.freedesktop.Accounts.User",
710 g_variant_new ("()"),
713 g_warning ("Failed to emit Changed: %s", error->message);
714 g_clear_error (&error);
716 check_status (status_text->str);
717 g_string_free (status_text, TRUE);
719 else if (strcmp (name, "DELETE-USER") == 0)
721 gchar *status_text, *username;
724 username = g_hash_table_lookup (params, "USERNAME");
725 user = get_accounts_user_by_name (username);
727 accounts_user_set_hidden (user, TRUE, TRUE);
729 g_warning ("Unknown user %s", username);
731 status_text = g_strdup_printf ("RUNNER DELETE-USER USERNAME=%s", username);
732 check_status (status_text);
733 g_free (status_text);
735 /* Forward to external processes */
736 else if (g_str_has_prefix (name, "SESSION-") ||
737 g_str_has_prefix (name, "GREETER-") ||
738 g_str_has_prefix (name, "XSERVER-") ||
739 strcmp (name, "UNITY-SYSTEM-COMPOSITOR") == 0)
742 for (link = status_clients; link; link = link->next)
744 StatusClient *client = link->data;
746 GError *error = NULL;
748 length = strlen (command);
749 if (g_socket_send (client->socket, (gchar *) &length, sizeof (length), NULL, &error) < 0 ||
750 g_socket_send (client->socket, command, strlen (command), NULL, &error) < 0)
751 g_printerr ("Failed to write to client socket: %s\n", error->message);
752 g_clear_error (&error);
757 g_printerr ("Unknown command '%s'\n", name);
762 g_hash_table_unref (params);
768 /* Stop daemon if requested */
773 /* Commands start with an asterisk */
774 line = get_script_line (NULL);
775 if (!line || line->text[0] != '*')
778 statuses = g_list_append (statuses, g_strdup (line->text));
781 handle_command (line->text + 1);
784 /* Stop at the end of the script */
785 if (get_script_line (NULL) == NULL)
790 status_timeout_cb (gpointer data)
794 line = get_script_line (NULL);
795 fail ("(timeout)", line ? line->text : NULL);
801 check_status (const gchar *status)
804 gboolean result = FALSE;
810 statuses = g_list_append (statuses, g_strdup (status));
812 if (getenv ("DEBUG"))
813 g_print ("%s\n", status);
815 /* Try and match against expected */
816 prefix = get_prefix (status);
817 line = get_script_line (prefix);
821 gchar *full_pattern = g_strdup_printf ("^%s$", line->text);
822 result = g_regex_match_simple (full_pattern, status, 0, 0);
823 g_free (full_pattern);
829 line = get_script_line (NULL);
830 fail (NULL, line ? line->text : NULL);
836 /* Restart timeout */
838 g_source_remove (status_timeout);
839 status_timeout = g_timeout_add (status_timeout_ms, status_timeout_cb, NULL);
845 status_message_cb (GSocket *socket, GIOCondition condition, StatusClient *client)
850 GError *error = NULL;
852 n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
854 n_read = g_socket_receive (socket, buffer, length, NULL, &error);
856 g_warning ("Error reading from socket: %s", error->message);
857 g_clear_error (&error);
860 status_clients = g_list_remove (status_clients, client);
861 g_object_unref (client->socket);
867 buffer[n_read] = '\0';
868 check_status (buffer);
875 status_connect_cb (gpointer data)
878 GError *error = NULL;
880 socket = g_socket_accept (status_socket, NULL, &error);
882 g_warning ("Failed to accept status connection: %s", error->message);
883 g_clear_error (&error);
886 StatusClient *client;
888 client = g_malloc0 (sizeof (StatusClient));
889 client->socket = socket;
890 client->source = g_socket_create_source (socket, G_IO_IN, NULL);
891 status_clients = g_list_append (status_clients, client);
893 g_source_set_callback (client->source, (GSourceFunc) status_message_cb, client, NULL);
894 g_source_attach (client->source, NULL);
901 load_script (const gchar *filename)
904 gchar *data, **lines;
906 if (!g_file_get_contents (filename, &data, NULL, NULL))
908 g_printerr ("Unable to load script: %s\n", filename);
912 lines = g_strsplit (data, "\n", -1);
915 /* Load lines with #? prefix as expected behaviour */
916 for (i = 0; lines[i]; i++)
918 gchar *text = g_strstrip (lines[i]);
919 if (g_str_has_prefix (text, "#?"))
922 line = g_malloc0 (sizeof (ScriptLine));
923 line->text = g_strdup (text + 2);
925 script = g_list_append (script, line);
932 handle_upower_call (GDBusConnection *connection,
934 const gchar *object_path,
935 const gchar *interface_name,
936 const gchar *method_name,
937 GVariant *parameters,
938 GDBusMethodInvocation *invocation,
941 if (strcmp (method_name, "SuspendAllowed") == 0)
943 check_status ("UPOWER SUSPEND-ALLOWED");
944 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
946 else if (strcmp (method_name, "Suspend") == 0)
948 check_status ("UPOWER SUSPEND");
949 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
951 else if (strcmp (method_name, "HibernateAllowed") == 0)
953 check_status ("UPOWER HIBERNATE-ALLOWED");
954 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
956 else if (strcmp (method_name, "Hibernate") == 0)
958 check_status ("UPOWER HIBERNATE");
959 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
962 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
966 upower_name_acquired_cb (GDBusConnection *connection,
970 const gchar *upower_interface =
972 " <interface name='org.freedesktop.UPower'>"
973 " <method name='SuspendAllowed'>"
974 " <arg name='allowed' direction='out' type='b'/>"
976 " <method name='Suspend'/>"
977 " <method name='HibernateAllowed'>"
978 " <arg name='allowed' direction='out' type='b'/>"
980 " <method name='Hibernate'/>"
983 static const GDBusInterfaceVTable upower_vtable =
987 GDBusNodeInfo *upower_info;
988 GError *error = NULL;
990 upower_info = g_dbus_node_info_new_for_xml (upower_interface, &error);
992 g_warning ("Failed to parse D-Bus interface: %s", error->message);
993 g_clear_error (&error);
996 g_dbus_connection_register_object (connection,
997 "/org/freedesktop/UPower",
998 upower_info->interfaces[0],
1003 g_warning ("Failed to register UPower service: %s", error->message);
1004 g_clear_error (&error);
1005 g_dbus_node_info_unref (upower_info);
1008 if (service_count == 0)
1013 start_upower_daemon (void)
1016 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1017 "org.freedesktop.UPower",
1018 G_BUS_NAME_OWNER_FLAGS_NONE,
1019 upower_name_acquired_cb,
1027 open_ck_session (GVariant *params)
1034 GError *error = NULL;
1036 session = g_malloc0 (sizeof (CKSession));
1037 ck_sessions = g_list_append (ck_sessions, session);
1039 cookie = g_string_new ("ck-cookie");
1040 g_variant_get (params, "a(sv)", &iter);
1041 while (g_variant_iter_loop (iter, "(&sv)", &name, &value))
1043 if (strcmp (name, "x11-display") == 0)
1045 const gchar *display;
1046 g_variant_get (value, "&s", &display);
1047 g_string_append_printf (cookie, "-x%s", display);
1051 session->cookie = cookie->str;
1052 g_string_free (cookie, FALSE);
1053 session->path = g_strdup_printf ("/org/freedesktop/ConsoleKit/Session%d", ck_session_index++);
1054 session->id = g_dbus_connection_register_object (ck_connection,
1056 ck_session_info->interfaces[0],
1062 g_warning ("Failed to register CK Session: %s", error->message);
1063 g_clear_error (&error);
1069 handle_ck_call (GDBusConnection *connection,
1070 const gchar *sender,
1071 const gchar *object_path,
1072 const gchar *interface_name,
1073 const gchar *method_name,
1074 GVariant *parameters,
1075 GDBusMethodInvocation *invocation,
1078 if (strcmp (method_name, "CanRestart") == 0)
1080 check_status ("CONSOLE-KIT CAN-RESTART");
1081 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1083 else if (strcmp (method_name, "CanStop") == 0)
1085 check_status ("CONSOLE-KIT CAN-STOP");
1086 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1088 else if (strcmp (method_name, "CloseSession") == 0)
1089 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1090 else if (strcmp (method_name, "OpenSession") == 0)
1092 GVariantBuilder params;
1093 g_variant_builder_init (¶ms, G_VARIANT_TYPE ("a(sv)"));
1094 CKSession *session = open_ck_session (g_variant_builder_end (¶ms));
1095 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", session->cookie));
1097 else if (strcmp (method_name, "OpenSessionWithParameters") == 0)
1099 CKSession *session = open_ck_session (g_variant_get_child_value (parameters, 0));
1100 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", session->cookie));
1102 else if (strcmp (method_name, "GetSessionForCookie") == 0)
1107 g_variant_get (parameters, "(&s)", &cookie);
1109 for (link = ck_sessions; link; link = link->next)
1111 CKSession *session = link->data;
1112 if (strcmp (session->cookie, cookie) == 0)
1114 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", session->path));
1119 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to find session for cookie");
1121 else if (strcmp (method_name, "Restart") == 0)
1123 check_status ("CONSOLE-KIT RESTART");
1124 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1126 else if (strcmp (method_name, "Stop") == 0)
1128 check_status ("CONSOLE-KIT STOP");
1129 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1132 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1136 handle_ck_session_call (GDBusConnection *connection,
1137 const gchar *sender,
1138 const gchar *object_path,
1139 const gchar *interface_name,
1140 const gchar *method_name,
1141 GVariant *parameters,
1142 GDBusMethodInvocation *invocation,
1145 CKSession *session = user_data;
1147 if (strcmp (method_name, "Lock") == 0)
1149 if (!session->locked)
1150 check_status ("CONSOLE-KIT LOCK-SESSION");
1151 session->locked = TRUE;
1152 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1154 else if (strcmp (method_name, "Unlock") == 0)
1156 if (session->locked)
1157 check_status ("CONSOLE-KIT UNLOCK-SESSION");
1158 session->locked = FALSE;
1159 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1161 else if (strcmp (method_name, "Activate") == 0)
1163 gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie);
1164 check_status (status);
1167 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1170 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1174 ck_name_acquired_cb (GDBusConnection *connection,
1178 const gchar *ck_interface =
1180 " <interface name='org.freedesktop.ConsoleKit.Manager'>"
1181 " <method name='CanRestart'>"
1182 " <arg name='can_restart' direction='out' type='b'/>"
1184 " <method name='CanStop'>"
1185 " <arg name='can_stop' direction='out' type='b'/>"
1187 " <method name='CloseSession'>"
1188 " <arg name='cookie' direction='in' type='s'/>"
1189 " <arg name='result' direction='out' type='b'/>"
1191 " <method name='OpenSession'>"
1192 " <arg name='cookie' direction='out' type='s'/>"
1194 " <method name='OpenSessionWithParameters'>"
1195 " <arg name='parameters' direction='in' type='a(sv)'/>"
1196 " <arg name='cookie' direction='out' type='s'/>"
1198 " <method name='GetSessionForCookie'>"
1199 " <arg name='cookie' direction='in' type='s'/>"
1200 " <arg name='ssid' direction='out' type='o'/>"
1202 " <method name='Restart'/>"
1203 " <method name='Stop'/>"
1204 " <signal name='SeatAdded'>"
1205 " <arg name='seat' type='o'/>"
1207 " <signal name='SeatRemoved'>"
1208 " <arg name='seat' type='o'/>"
1212 static const GDBusInterfaceVTable ck_vtable =
1216 const gchar *ck_session_interface =
1218 " <interface name='org.freedesktop.ConsoleKit.Session'>"
1219 " <method name='Lock'/>"
1220 " <method name='Unlock'/>"
1221 " <method name='Activate'/>"
1224 GDBusNodeInfo *ck_info;
1225 GError *error = NULL;
1227 ck_connection = connection;
1229 ck_info = g_dbus_node_info_new_for_xml (ck_interface, &error);
1231 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1232 g_clear_error (&error);
1235 ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface, &error);
1237 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1238 g_clear_error (&error);
1239 if (!ck_session_info)
1241 g_dbus_connection_register_object (connection,
1242 "/org/freedesktop/ConsoleKit/Manager",
1243 ck_info->interfaces[0],
1248 g_warning ("Failed to register console kit service: %s", error->message);
1249 g_clear_error (&error);
1250 g_dbus_node_info_unref (ck_info);
1253 if (service_count == 0)
1258 start_console_kit_daemon (void)
1261 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1262 "org.freedesktop.ConsoleKit",
1263 G_BUS_NAME_OWNER_FLAGS_NONE,
1265 ck_name_acquired_cb,
1272 handle_login1_session_call (GDBusConnection *connection,
1273 const gchar *sender,
1274 const gchar *object_path,
1275 const gchar *interface_name,
1276 const gchar *method_name,
1277 GVariant *parameters,
1278 GDBusMethodInvocation *invocation,
1281 Login1Session *session = user_data;
1283 if (strcmp (method_name, "Lock") == 0)
1285 if (!session->locked)
1286 check_status ("LOGIN1 LOCK-SESSION");
1287 session->locked = TRUE;
1288 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1290 else if (strcmp (method_name, "Unlock") == 0)
1292 if (session->locked)
1293 check_status ("LOGIN1 UNLOCK-SESSION");
1294 session->locked = FALSE;
1295 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1297 else if (strcmp (method_name, "Activate") == 0)
1299 gchar *status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", strrchr (object_path, '/') + 1);
1300 check_status (status);
1303 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1306 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1309 static Login1Session *
1310 open_login1_session (GDBusConnection *connection,
1313 Login1Session *session;
1314 GError *error = NULL;
1315 GDBusNodeInfo *login1_session_info;
1317 const gchar *login1_session_interface =
1319 " <interface name='org.freedesktop.login1.Session'>"
1320 " <method name='Lock'/>"
1321 " <method name='Unlock'/>"
1322 " <method name='Activate'/>"
1325 static const GDBusInterfaceVTable login1_session_vtable =
1327 handle_login1_session_call,
1330 session = g_malloc0 (sizeof (Login1Session));
1331 login1_sessions = g_list_append (login1_sessions, session);
1333 session->path = g_strdup_printf("/org/freedesktop/login1/Session/c%d",
1334 login1_session_index++);
1338 login1_session_info = g_dbus_node_info_new_for_xml (login1_session_interface,
1341 g_warning ("Failed to parse login1 session D-Bus interface: %s",
1343 g_clear_error (&error);
1344 if (!login1_session_info)
1347 g_dbus_connection_register_object (connection,
1349 login1_session_info->interfaces[0],
1350 &login1_session_vtable,
1355 g_warning ("Failed to register login1 session: %s", error->message);
1356 g_clear_error (&error);
1357 g_dbus_node_info_unref (login1_session_info);
1364 handle_login1_call (GDBusConnection *connection,
1365 const gchar *sender,
1366 const gchar *object_path,
1367 const gchar *interface_name,
1368 const gchar *method_name,
1369 GVariant *parameters,
1370 GDBusMethodInvocation *invocation,
1374 if (strcmp (method_name, "GetSessionByPID") == 0)
1376 /* Look for a session with our PID, and create one if we don't have one
1380 Login1Session *ret = NULL;
1382 g_variant_get (parameters, "(u)", &pid);
1384 for (link = login1_sessions; link; link = link->next)
1386 Login1Session *session;
1387 session = link->data;
1388 if (session->pid == pid)
1396 ret = open_login1_session (connection, parameters);
1398 g_dbus_method_invocation_return_value (invocation,
1399 g_variant_new("(o)", ret->path));
1402 else if (strcmp (method_name, "CanReboot") == 0)
1404 check_status ("LOGIN1 CAN-REBOOT");
1405 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1407 else if (strcmp (method_name, "Reboot") == 0)
1409 gboolean interactive;
1410 g_variant_get (parameters, "(b)", &interactive);
1411 check_status ("LOGIN1 REBOOT");
1412 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1414 else if (strcmp (method_name, "CanPowerOff") == 0)
1416 check_status ("LOGIN1 CAN-POWER-OFF");
1417 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1419 else if (strcmp (method_name, "Suspend") == 0)
1421 gboolean interactive;
1422 g_variant_get (parameters, "(b)", &interactive);
1423 check_status ("LOGIN1 SUSPEND");
1424 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1426 else if (strcmp (method_name, "CanSuspend") == 0)
1428 check_status ("LOGIN1 CAN-SUSPEND");
1429 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1431 else if (strcmp (method_name, "PowerOff") == 0)
1433 gboolean interactive;
1434 g_variant_get (parameters, "(b)", &interactive);
1435 check_status ("LOGIN1 POWER-OFF");
1436 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1438 else if (strcmp (method_name, "CanHibernate") == 0)
1440 check_status ("LOGIN1 CAN-HIBERNATE");
1441 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1443 else if (strcmp (method_name, "Hibernate") == 0)
1445 gboolean interactive;
1446 g_variant_get (parameters, "(b)", &interactive);
1447 check_status ("LOGIN1 HIBERNATE");
1448 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1451 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1455 login1_name_acquired_cb (GDBusConnection *connection,
1459 const gchar *login1_interface =
1461 " <interface name='org.freedesktop.login1.Manager'>"
1462 " <method name='GetSessionByPID'>"
1463 " <arg name='pid' type='u' direction='in'/>"
1464 " <arg name='session' type='o' direction='out'/>"
1466 " <method name='CanReboot'>"
1467 " <arg name='result' direction='out' type='s'/>"
1469 " <method name='Reboot'>"
1470 " <arg name='interactive' direction='in' type='b'/>"
1472 " <method name='CanPowerOff'>"
1473 " <arg name='result' direction='out' type='s'/>"
1475 " <method name='PowerOff'>"
1476 " <arg name='interactive' direction='in' type='b'/>"
1478 " <method name='CanSuspend'>"
1479 " <arg name='result' direction='out' type='s'/>"
1481 " <method name='Suspend'>"
1482 " <arg name='interactive' direction='in' type='b'/>"
1484 " <method name='CanHibernate'>"
1485 " <arg name='result' direction='out' type='s'/>"
1487 " <method name='Hibernate'>"
1488 " <arg name='interactive' direction='in' type='b'/>"
1492 static const GDBusInterfaceVTable login1_vtable =
1496 GDBusNodeInfo *login1_info;
1497 GError *error = NULL;
1499 login1_info = g_dbus_node_info_new_for_xml (login1_interface, &error);
1501 g_warning ("Failed to parse login1 D-Bus interface: %s", error->message);
1502 g_clear_error (&error);
1505 g_dbus_connection_register_object (connection,
1506 "/org/freedesktop/login1",
1507 login1_info->interfaces[0],
1512 g_warning ("Failed to register login1 service: %s", error->message);
1513 g_clear_error (&error);
1514 g_dbus_node_info_unref (login1_info);
1517 if (service_count == 0)
1522 start_login1_daemon (void)
1525 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1526 "org.freedesktop.login1",
1527 G_BUS_NAME_OWNER_FLAGS_NONE,
1529 login1_name_acquired_cb,
1535 static AccountsUser *
1536 get_accounts_user_by_uid (guint uid)
1540 for (link = accounts_users; link; link = link->next)
1542 AccountsUser *u = link->data;
1550 static AccountsUser *
1551 get_accounts_user_by_name (const gchar *username)
1555 for (link = accounts_users; link; link = link->next)
1557 AccountsUser *u = link->data;
1558 if (strcmp (u->user_name, username) == 0)
1566 accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal)
1568 GError *error = NULL;
1570 user->hidden = hidden;
1572 if (user->hidden && user->id != 0)
1574 g_dbus_connection_unregister_object (accounts_connection, user->id);
1575 g_dbus_connection_emit_signal (accounts_connection,
1577 "/org/freedesktop/Accounts",
1578 "org.freedesktop.Accounts",
1580 g_variant_new ("(o)", user->path),
1583 g_warning ("Failed to emit UserDeleted: %s", error->message);
1584 g_clear_error (&error);
1588 if (!user->hidden && user->id == 0)
1590 user->id = g_dbus_connection_register_object (accounts_connection,
1592 user_info->interfaces[0],
1598 g_warning ("Failed to register user: %s", error->message);
1599 g_clear_error (&error);
1601 g_dbus_connection_emit_signal (accounts_connection,
1603 "/org/freedesktop/Accounts",
1604 "org.freedesktop.Accounts",
1606 g_variant_new ("(o)", user->path),
1609 g_warning ("Failed to emit UserAdded: %s", error->message);
1610 g_clear_error (&error);
1615 load_passwd_file (void)
1617 gchar *path, *data, **lines;
1618 gchar **user_filter = NULL;
1621 if (g_key_file_has_key (config, "test-runner-config", "accounts-service-user-filter", NULL))
1625 filter = g_key_file_get_string (config, "test-runner-config", "accounts-service-user-filter", NULL);
1626 user_filter = g_strsplit (filter, " ", -1);
1630 path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL);
1631 g_file_get_contents (path, &data, NULL, NULL);
1633 lines = g_strsplit (data, "\n", -1);
1636 for (i = 0; lines[i]; i++)
1640 gchar *user_name, *real_name;
1641 AccountsUser *user = NULL;
1643 fields = g_strsplit (lines[i], ":", -1);
1644 if (fields == NULL || g_strv_length (fields) < 7)
1646 g_strfreev (fields);
1650 user_name = fields[0];
1651 uid = atoi (fields[2]);
1652 real_name = fields[4];
1654 user = get_accounts_user_by_uid (uid);
1658 GKeyFile *dmrc_file;
1660 user = g_malloc0 (sizeof (AccountsUser));
1661 accounts_users = g_list_append (accounts_users, user);
1663 /* Only allow users in whitelist */
1664 user->hidden = FALSE;
1669 user->hidden = TRUE;
1670 for (j = 0; user_filter[j] != NULL; j++)
1671 if (strcmp (user_name, user_filter[j]) == 0)
1672 user->hidden = FALSE;
1675 dmrc_file = g_key_file_new ();
1676 path = g_build_filename (temp_dir, "home", user_name, ".dmrc", NULL);
1677 g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_NONE, NULL);
1681 user->user_name = g_strdup (user_name);
1682 user->real_name = g_strdup (real_name);
1683 user->home_directory = g_build_filename (temp_dir, "home", user_name, NULL);
1684 user->language = g_key_file_get_string (dmrc_file, "Desktop", "Language", NULL);
1685 /* DMRC contains a locale, strip the codeset off it to get the language */
1688 gchar *c = strchr (user->language, '.');
1692 user->xsession = g_key_file_get_string (dmrc_file, "Desktop", "Session", NULL);
1693 user->layouts = g_key_file_get_string_list (dmrc_file, "X-Accounts", "Layouts", NULL, NULL);
1696 user->layouts = g_malloc (sizeof (gchar *) * 2);
1697 user->layouts[0] = g_key_file_get_string (dmrc_file, "Desktop", "Layout", NULL);
1698 user->layouts[1] = NULL;
1700 user->has_messages = g_key_file_get_boolean (dmrc_file, "X-Accounts", "HasMessages", NULL);
1701 user->path = g_strdup_printf ("/org/freedesktop/Accounts/User%d", uid);
1702 accounts_user_set_hidden (user, user->hidden, FALSE);
1704 g_key_file_free (dmrc_file);
1707 g_strfreev (fields);
1714 handle_accounts_call (GDBusConnection *connection,
1715 const gchar *sender,
1716 const gchar *object_path,
1717 const gchar *interface_name,
1718 const gchar *method_name,
1719 GVariant *parameters,
1720 GDBusMethodInvocation *invocation,
1723 if (strcmp (method_name, "ListCachedUsers") == 0)
1725 GVariantBuilder builder;
1728 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
1730 load_passwd_file ();
1731 for (link = accounts_users; link; link = link->next)
1733 AccountsUser *user = link->data;
1734 if (!user->hidden && user->uid >= 1000)
1735 g_variant_builder_add_value (&builder, g_variant_new_object_path (user->path));
1738 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(ao)", &builder));
1740 else if (strcmp (method_name, "FindUserByName") == 0)
1742 AccountsUser *user = NULL;
1745 g_variant_get (parameters, "(&s)", &user_name);
1747 load_passwd_file ();
1748 user = get_accounts_user_by_name (user_name);
1752 accounts_user_set_hidden (user, FALSE, TRUE);
1753 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", user->path));
1756 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such user: %s", user_name);
1759 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1763 handle_user_call (GDBusConnection *connection,
1764 const gchar *sender,
1765 const gchar *object_path,
1766 const gchar *interface_name,
1767 const gchar *method_name,
1768 GVariant *parameters,
1769 GDBusMethodInvocation *invocation,
1772 AccountsUser *user = user_data;
1774 if (strcmp (method_name, "SetXSession") == 0)
1778 g_variant_get (parameters, "(&s)", &xsession);
1780 g_free (user->xsession);
1781 user->xsession = g_strdup (xsession);
1783 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1785 /* And notify others that it took */
1786 g_dbus_connection_emit_signal (accounts_connection,
1789 "org.freedesktop.Accounts.User",
1791 g_variant_new ("()"),
1795 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1799 handle_user_get_property (GDBusConnection *connection,
1800 const gchar *sender,
1801 const gchar *object_path,
1802 const gchar *interface_name,
1803 const gchar *property_name,
1807 AccountsUser *user = user_data;
1809 if (strcmp (property_name, "UserName") == 0)
1810 return g_variant_new_string (user->user_name);
1811 else if (strcmp (property_name, "RealName") == 0)
1812 return g_variant_new_string (user->real_name);
1813 else if (strcmp (property_name, "HomeDirectory") == 0)
1814 return g_variant_new_string (user->home_directory);
1815 else if (strcmp (property_name, "SystemAccount") == 0)
1816 return g_variant_new_boolean (user->uid < 1000);
1817 else if (strcmp (property_name, "BackgroundFile") == 0)
1818 return g_variant_new_string (user->background ? user->background : "");
1819 else if (strcmp (property_name, "Language") == 0)
1820 return g_variant_new_string (user->language ? user->language : "");
1821 else if (strcmp (property_name, "IconFile") == 0)
1822 return g_variant_new_string (user->image ? user->image : "");
1823 else if (strcmp (property_name, "Shell") == 0)
1824 return g_variant_new_string ("/bin/sh");
1825 else if (strcmp (property_name, "Uid") == 0)
1826 return g_variant_new_uint64 (user->uid);
1827 else if (strcmp (property_name, "XSession") == 0)
1828 return g_variant_new_string (user->xsession ? user->xsession : "");
1829 else if (strcmp (property_name, "XKeyboardLayouts") == 0)
1831 if (user->layouts != NULL)
1832 return g_variant_new_strv ((const gchar * const *) user->layouts, -1);
1834 return g_variant_new_strv (NULL, 0);
1836 else if (strcmp (property_name, "XHasMessages") == 0)
1837 return g_variant_new_boolean (user->has_messages);
1843 accounts_name_acquired_cb (GDBusConnection *connection,
1847 const gchar *accounts_interface =
1849 " <interface name='org.freedesktop.Accounts'>"
1850 " <method name='ListCachedUsers'>"
1851 " <arg name='user' direction='out' type='ao'/>"
1853 " <method name='FindUserByName'>"
1854 " <arg name='name' direction='in' type='s'/>"
1855 " <arg name='user' direction='out' type='o'/>"
1857 " <signal name='UserAdded'>"
1858 " <arg name='user' type='o'/>"
1860 " <signal name='UserDeleted'>"
1861 " <arg name='user' type='o'/>"
1865 static const GDBusInterfaceVTable accounts_vtable =
1867 handle_accounts_call,
1869 const gchar *user_interface =
1871 " <interface name='org.freedesktop.Accounts.User'>"
1872 " <method name='SetXSession'>"
1873 " <arg name='x_session' direction='in' type='s'/>"
1875 " <property name='UserName' type='s' access='read'/>"
1876 " <property name='RealName' type='s' access='read'/>"
1877 " <property name='HomeDirectory' type='s' access='read'/>"
1878 " <property name='SystemAccount' type='b' access='read'/>"
1879 " <property name='BackgroundFile' type='s' access='read'/>"
1880 " <property name='Language' type='s' access='read'/>"
1881 " <property name='IconFile' type='s' access='read'/>"
1882 " <property name='Shell' type='s' access='read'/>"
1883 " <property name='Uid' type='t' access='read'/>"
1884 " <property name='XSession' type='s' access='read'/>"
1885 " <property name='XKeyboardLayouts' type='as' access='read'/>"
1886 " <property name='XHasMessages' type='b' access='read'/>"
1887 " <signal name='Changed' />"
1890 GError *error = NULL;
1892 accounts_connection = connection;
1894 accounts_info = g_dbus_node_info_new_for_xml (accounts_interface, &error);
1896 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1897 g_clear_error (&error);
1900 user_info = g_dbus_node_info_new_for_xml (user_interface, &error);
1902 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1903 g_clear_error (&error);
1906 g_dbus_connection_register_object (connection,
1907 "/org/freedesktop/Accounts",
1908 accounts_info->interfaces[0],
1914 g_warning ("Failed to register accounts service: %s", error->message);
1915 g_clear_error (&error);
1916 g_dbus_node_info_unref (accounts_info);
1919 if (service_count == 0)
1924 start_accounts_service_daemon (void)
1927 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1928 "org.freedesktop.Accounts",
1929 G_BUS_NAME_OWNER_FLAGS_NONE,
1930 accounts_name_acquired_cb,
1944 signal_cb (gpointer user_data)
1946 g_print ("Caught signal, quitting\n");
1947 quit (EXIT_FAILURE);
1952 main (int argc, char **argv)
1956 gchar *greeter = NULL, *script_name, *config_file, *additional_system_config;
1957 gchar *additional_config, *path, *path1, *path2, *ld_preload, *ld_library_path, *home_dir;
1958 GString *passwd_data, *group_data;
1959 GSource *status_source;
1961 GError *error = NULL;
1963 #if !defined(GLIB_VERSION_2_36)
1967 loop = g_main_loop_new (NULL, FALSE);
1969 g_unix_signal_add (SIGINT, signal_cb, NULL);
1970 g_unix_signal_add (SIGTERM, signal_cb, NULL);
1972 children = g_hash_table_new (g_direct_hash, g_direct_equal);
1976 g_printerr ("Usage %s SCRIPT-NAME GREETER\n", argv[0]);
1977 quit (EXIT_FAILURE);
1979 script_name = argv[1];
1980 config_file = g_strdup_printf ("%s.conf", script_name);
1981 config_path = g_build_filename (SRCDIR, "tests", "scripts", config_file, NULL);
1982 g_free (config_file);
1984 config = g_key_file_new ();
1985 g_key_file_load_from_file (config, config_path, G_KEY_FILE_NONE, NULL);
1987 load_script (config_path);
1989 if (!getcwd (cwd, 1024))
1991 g_critical ("Error getting current directory: %s", strerror (errno));
1992 quit (EXIT_FAILURE);
1995 /* Don't contact our X server */
1996 g_unsetenv ("DISPLAY");
1998 /* Override system calls */
1999 ld_preload = g_build_filename (BUILDDIR, "tests", "src", ".libs", "libsystem.so", NULL);
2000 g_setenv ("LD_PRELOAD", ld_preload, TRUE);
2001 g_free (ld_preload);
2003 /* Run test programs */
2004 path = g_strdup_printf ("%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, g_getenv ("PATH"));
2005 g_setenv ("PATH", path, TRUE);
2008 /* Use locally built libraries */
2009 path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL);
2010 path2 = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL);
2011 ld_library_path = g_strdup_printf ("%s:%s", path1, path2);
2014 g_setenv ("LD_LIBRARY_PATH", ld_library_path, TRUE);
2015 g_free (ld_library_path);
2016 path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL);
2017 g_setenv ("GI_TYPELIB_PATH", path1, TRUE);
2020 /* Run in a temporary directory inside the build directory */
2021 /* Note we have to pick a name that is short since Unix sockets in this directory have a 108 character limit on their paths */
2026 name = g_strdup_printf (".r%d", i);
2028 temp_dir = g_build_filename ("/tmp", name, NULL);
2030 if (!g_file_test (temp_dir, G_FILE_TEST_EXISTS))
2034 g_mkdir_with_parents (temp_dir, 0755);
2035 g_setenv ("LIGHTDM_TEST_ROOT", temp_dir, TRUE);
2037 /* Open socket for status */
2038 /* Note we have to pick a socket name that is short since there is a 108 character limit on the name */
2039 status_socket_name = g_build_filename (temp_dir, ".s", NULL);
2040 unlink (status_socket_name);
2041 status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
2043 g_warning ("Error creating status socket %s: %s", status_socket_name, error->message);
2044 g_clear_error (&error);
2047 GSocketAddress *address;
2050 address = g_unix_socket_address_new (status_socket_name);
2051 result = g_socket_bind (status_socket, address, FALSE, &error);
2052 g_object_unref (address);
2054 g_warning ("Error binding status socket %s: %s", status_socket_name, error->message);
2055 g_clear_error (&error);
2058 result = g_socket_listen (status_socket, &error);
2060 g_warning ("Error listening on status socket %s: %s", status_socket_name, error->message);
2061 g_clear_error (&error);
2065 g_object_unref (status_socket);
2066 status_socket = NULL;
2070 quit (EXIT_FAILURE);
2071 status_source = g_socket_create_source (status_socket, G_IO_IN, NULL);
2072 g_source_set_callback (status_source, status_connect_cb, NULL, NULL);
2073 g_source_attach (status_source, NULL);
2075 /* Set up a skeleton file system */
2076 g_mkdir_with_parents (g_strdup_printf ("%s/etc", temp_dir), 0755);
2077 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share", temp_dir), 0755);
2078 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/sessions", temp_dir), 0755);
2079 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/remote-sessions", temp_dir), 0755);
2080 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/greeters", temp_dir), 0755);
2081 g_mkdir_with_parents (g_strdup_printf ("%s/tmp", temp_dir), 0755);
2082 g_mkdir_with_parents (g_strdup_printf ("%s/var/lib/lightdm-data", temp_dir), 0755);
2083 g_mkdir_with_parents (g_strdup_printf ("%s/var/run", temp_dir), 0755);
2084 g_mkdir_with_parents (g_strdup_printf ("%s/var/log", temp_dir), 0755);
2086 /* Copy over the configuration */
2087 g_mkdir_with_parents (g_strdup_printf ("%s/etc/lightdm", temp_dir), 0755);
2088 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))
2089 if (system (g_strdup_printf ("cp %s %s/etc/lightdm/lightdm.conf", config_path, temp_dir)))
2090 perror ("Failed to copy configuration");
2092 additional_system_config = g_key_file_get_string (config, "test-runner-config", "additional-system-config", NULL);
2093 if (additional_system_config)
2097 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/lightdm.conf.d", temp_dir), 0755);
2099 files = g_strsplit (additional_system_config, " ", -1);
2100 for (i = 0; files[i]; i++)
2101 if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/usr/share/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir)))
2102 perror ("Failed to copy configuration");
2106 additional_config = g_key_file_get_string (config, "test-runner-config", "additional-config", NULL);
2107 if (additional_config)
2111 g_mkdir_with_parents (g_strdup_printf ("%s/etc/lightdm/lightdm.conf.d", temp_dir), 0755);
2113 files = g_strsplit (additional_config, " ", -1);
2114 for (i = 0; files[i]; i++)
2115 if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/etc/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir)))
2116 perror ("Failed to copy configuration");
2120 if (g_key_file_has_key (config, "test-runner-config", "shared-data-dirs", NULL))
2126 dir_string = g_key_file_get_string (config, "test-runner-config", "shared-data-dirs", NULL);
2127 dirs = g_strsplit (dir_string, " ", -1);
2128 g_free (dir_string);
2130 for (i = 0; dirs[i]; i++)
2132 gchar **fields = g_strsplit (dirs[i], ":", -1);
2133 if (g_strv_length (fields) == 4)
2135 gchar *path = g_strdup_printf ("%s/var/lib/lightdm-data/%s", temp_dir, fields[0]);
2136 int uid = g_ascii_strtoll (fields[1], NULL, 10);
2137 int gid = g_ascii_strtoll (fields[2], NULL, 10);
2138 int mode = g_ascii_strtoll (fields[3], NULL, 8);
2139 g_mkdir (path, mode);
2140 g_chmod (path, mode); /* mkdir filters by umask, so make sure we have what we want */
2141 if (chown (path, uid, gid) < 0)
2142 g_warning ("chown (%s) failed: %s", path, strerror (errno));
2145 g_strfreev (fields);
2151 /* Always copy the script */
2152 if (system (g_strdup_printf ("cp %s %s/script", config_path, temp_dir)))
2153 perror ("Failed to copy configuration");
2155 /* Copy over the greeter files */
2156 if (system (g_strdup_printf ("cp %s/sessions/* %s/usr/share/lightdm/sessions", DATADIR, temp_dir)))
2157 perror ("Failed to copy sessions");
2158 if (system (g_strdup_printf ("cp %s/remote-sessions/* %s/usr/share/lightdm/remote-sessions", DATADIR, temp_dir)))
2159 perror ("Failed to copy remote sessions");
2160 if (system (g_strdup_printf ("cp %s/greeters/* %s/usr/share/lightdm/greeters", DATADIR, temp_dir)))
2161 perror ("Failed to copy greeters");
2163 /* Set up the default greeter */
2164 path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL);
2165 greeter = g_strdup_printf ("%s.desktop", argv[2]);
2166 if (symlink (greeter, path) < 0)
2168 g_printerr ("Failed to make greeter symlink %s->%s: %s\n", path, greeter, strerror (errno));
2169 quit (EXIT_FAILURE);
2174 home_dir = g_build_filename (temp_dir, "home", NULL);
2176 /* Make fake users */
2186 {"root", "", "root", 0},
2187 /* Unprivileged account for greeters */
2188 {"lightdm", "", "", 100},
2189 /* These accounts have a password */
2190 {"have-password1", "password", "Password User 1", 1000},
2191 {"have-password2", "password", "Password User 2", 1001},
2192 {"have-password3", "password", "Password User 3", 1002},
2193 {"have-password4", "password", "Password User 4", 1003},
2194 /* This account always prompts for a password, even if using the lightdm-autologin service */
2195 {"always-password", "password", "Password User 4", 1004},
2196 /* These accounts have no password */
2197 {"no-password1", "", "No Password User 1", 1005},
2198 {"no-password2", "", "No Password User 2", 1006},
2199 {"no-password3", "", "No Password User 3", 1007},
2200 {"no-password4", "", "No Password User 4", 1008},
2201 /* This account has a keyboard layout */
2202 {"have-layout", "", "Layout User", 1009},
2203 /* This account has a set of keyboard layouts */
2204 {"have-layouts", "", "Layouts User", 1010},
2205 /* This account has a language set */
2206 {"have-language", "", "Language User", 1011},
2207 /* This account has a preconfigured session */
2208 {"have-session", "", "Session User", 1012},
2209 /* This account has the home directory mounted on login */
2210 {"mount-home-dir", "", "Mounted Home Dir User", 1013},
2211 /* This account is denied access */
2212 {"denied", "", "Denied User", 1014},
2213 /* This account has expired */
2214 {"expired", "", "Expired User", 1015},
2215 /* This account needs a password change */
2216 {"new-authtok", "", "New Token User", 1016},
2217 /* This account is switched to change-user2 when authentication succeeds */
2218 {"change-user1", "", "Change User 1", 1017},
2219 {"change-user2", "", "Change User 2", 1018},
2220 /* This account switches to invalid-user when authentication succeeds */
2221 {"change-user-invalid", "", "Invalid Change User", 1019},
2222 /* This account crashes on authentication */
2223 {"crash-authenticate", "", "Crash Auth User", 1020},
2224 /* This account shows an informational prompt on login */
2225 {"info-prompt", "password", "Info Prompt", 1021},
2226 /* This account shows multiple informational prompts on login */
2227 {"multi-info-prompt","password", "Multi Info Prompt", 1022},
2228 /* This account uses two factor authentication */
2229 {"two-factor", "password", "Two Factor", 1023},
2230 /* This account has a special group */
2231 {"group-member", "password", "Group Member", 1024},
2232 /* This account has the home directory created when the session starts */
2233 {"make-home-dir", "", "Make Home Dir User", 1025},
2234 /* This account fails to open a session */
2235 {"session-error", "password", "Session Error", 1026},
2236 /* This account can't establish credentials */
2237 {"cred-error", "password", "Cred Error", 1027},
2238 /* This account has expired credentials */
2239 {"cred-expired", "password", "Cred Expired", 1028},
2240 /* This account has cannot access their credentials */
2241 {"cred-unavail", "password", "Cred Unavail", 1029},
2242 /* This account sends informational messages for each PAM function that is called */
2243 {"log-pam", "password", "Log PAM", 1030},
2244 /* This account shows multiple prompts on login */
2245 {"multi-prompt", "password", "Multi Prompt", 1031},
2246 /* This account has an existing corrupt X authority */
2247 {"corrupt-xauth", "password", "Corrupt Xauthority", 1032},
2248 /* User to test properties */
2249 {"prop-user", "", "TEST", 1033},
2250 {NULL, NULL, NULL, 0}
2252 passwd_data = g_string_new ("");
2253 group_data = g_string_new ("");
2254 for (i = 0; users[i].user_name; i++)
2256 GKeyFile *dmrc_file;
2257 gboolean save_dmrc = FALSE;
2259 if (strcmp (users[i].user_name, "mount-home-dir") != 0 && strcmp (users[i].user_name, "make-home-dir") != 0)
2261 path = g_build_filename (home_dir, users[i].user_name, NULL);
2262 g_mkdir_with_parents (path, 0755);
2263 if (chown (path, users[i].uid, users[i].uid) < 0)
2264 g_debug ("chown (%s) failed: %s", path, strerror (errno));
2268 dmrc_file = g_key_file_new ();
2269 if (strcmp (users[i].user_name, "have-session") == 0)
2271 g_key_file_set_string (dmrc_file, "Desktop", "Session", "alternative");
2274 if (strcmp (users[i].user_name, "have-layout") == 0)
2276 g_key_file_set_string (dmrc_file, "Desktop", "Layout", "us");
2279 if (strcmp (users[i].user_name, "have-layouts") == 0)
2281 g_key_file_set_string (dmrc_file, "Desktop", "Layout", "ru");
2282 g_key_file_set_string (dmrc_file, "X-Accounts", "Layouts", "fr\toss;ru;");
2285 if (strcmp (users[i].user_name, "have-language") == 0)
2287 g_key_file_set_string (dmrc_file, "Desktop", "Language", "en_AU.utf8");
2295 path = g_build_filename (home_dir, users[i].user_name, ".dmrc", NULL);
2296 data = g_key_file_to_data (dmrc_file, NULL, NULL);
2297 g_file_set_contents (path, data, -1, NULL);
2302 g_key_file_free (dmrc_file);
2304 /* Write corrupt X authority file */
2305 if (strcmp (users[i].user_name, "corrupt-xauth") == 0)
2307 gchar data[1] = { 0xFF };
2309 path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL);
2310 g_file_set_contents (path, data, 1, NULL);
2311 chmod (path, S_IRUSR | S_IWUSR);
2315 /* Add passwd file entry */
2316 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);
2318 /* Add group file entry */
2319 g_string_append_printf (group_data, "%s:x:%d:%s\n", users[i].user_name, users[i].uid, users[i].user_name);
2321 path = g_build_filename (temp_dir, "etc", "passwd", NULL);
2322 g_file_set_contents (path, passwd_data->str, -1, NULL);
2324 g_string_free (passwd_data, TRUE);
2326 /* Add an extra test group */
2327 g_string_append_printf (group_data, "test-group:x:111:\n");
2329 path = g_build_filename (temp_dir, "etc", "group", NULL);
2330 g_file_set_contents (path, group_data->str, -1, NULL);
2332 g_string_free (group_data, TRUE);
2334 if (g_key_file_has_key (config, "test-runner-config", "timeout", NULL))
2335 status_timeout_ms = g_key_file_get_integer (config, "test-runner-config", "timeout", NULL) * 1000;
2337 /* Start D-Bus services */
2338 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-upower", NULL))
2339 start_upower_daemon ();
2340 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-console-kit", NULL))
2341 start_console_kit_daemon ();
2342 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-login1", NULL))
2343 start_login1_daemon ();
2344 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-accounts-service", NULL))
2345 start_accounts_service_daemon ();
2347 g_main_loop_run (loop);
2349 return EXIT_FAILURE;