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 run_lightdm (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 if (getenv ("DEBUG"))
138 g_print ("Sending SIGKILL to process %d\n", process->pid);
139 kill (process->pid, SIGKILL);
144 stop_process (Process *process)
146 if (process->kill_timeout != 0)
149 if (getenv ("DEBUG"))
150 g_print ("Sending SIGTERM to process %d\n", process->pid);
151 kill (process->pid, SIGTERM);
152 process->kill_timeout = g_timeout_add (KILL_TIMEOUT, kill_timeout_cb, process);
156 process_exit_cb (GPid pid, gint status, gpointer data)
161 if (getenv ("DEBUG"))
163 if (WIFEXITED (status))
164 g_print ("Process %d exited with status %d\n", pid, WEXITSTATUS (status));
166 g_print ("Process %d terminated with signal %d\n", pid, WTERMSIG (status));
169 if (lightdm_process && pid == lightdm_process->pid)
171 process = lightdm_process;
172 lightdm_process = NULL;
173 if (WIFEXITED (status))
174 status_text = g_strdup_printf ("RUNNER DAEMON-EXIT STATUS=%d", WEXITSTATUS (status));
176 status_text = g_strdup_printf ("RUNNER DAEMON-TERMINATE SIGNAL=%d", WTERMSIG (status));
177 check_status (status_text);
181 process = g_hash_table_lookup (children, GINT_TO_POINTER (pid));
184 g_hash_table_remove (children, GINT_TO_POINTER (pid));
187 if (process->kill_timeout)
188 g_source_remove (process->kill_timeout);
189 process->kill_timeout = 0;
191 /* Quit once all children have stopped */
197 watch_process (pid_t pid)
201 process = g_malloc0 (sizeof (Process));
203 process->kill_timeout = 0;
205 if (getenv ("DEBUG"))
206 g_print ("Watching process %d\n", process->pid);
207 g_child_watch_add (process->pid, process_exit_cb, NULL);
218 exit_status = status;
221 /* Stop all the children */
222 g_hash_table_iter_init (&iter, children);
227 if (!g_hash_table_iter_next (&iter, &key, &value))
230 stop_process ((Process *)value);
233 /* Don't quit until all children are stopped */
234 if (g_hash_table_size (children) > 0)
237 /* Stop the daemon */
240 stop_process (lightdm_process);
244 if (status_socket_name)
245 unlink (status_socket_name);
247 if (temp_dir && getenv ("DEBUG") == NULL)
249 gchar *command = g_strdup_printf ("rm -rf %s", temp_dir);
250 if (system (command))
251 perror ("Failed to delete temp directory");
258 fail (const gchar *event, const gchar *expected)
265 g_printerr ("Command line: %s", test_runner_command);
266 g_printerr ("Events:\n");
267 for (link = statuses; link; link = link->next)
268 g_printerr (" %s\n", (gchar *)link->data);
270 g_printerr (" %s\n", event);
272 g_printerr (" ^^^ expected \"%s\"\n", expected);
274 g_printerr ("^^^ expected nothing\n");
280 get_prefix (const gchar *text)
285 prefix = g_strdup (text);
286 for (i = 0; prefix[i] != '\0' && prefix[i] != ' '; i++);
293 get_script_line (const gchar *prefix)
297 for (link = script; link; link = link->next)
299 ScriptLine *line = link->data;
301 /* Ignore lines with other prefixes */
307 p = get_prefix (line->text);
308 matches = strcmp (prefix, p) == 0;
323 stop_loop (gpointer user_data)
325 g_main_loop_quit ((GMainLoop *)user_data);
326 return G_SOURCE_REMOVE;
330 handle_command (const gchar *command)
337 while (*c && !isspace (*c))
339 name = g_strdup_printf ("%.*s", (int) (c - command), command);
341 params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
345 gchar *param_name, *param_value;
350 while (*c && !isspace (*c) && *c != '=')
355 param_name = g_strdup_printf ("%.*s", (int) (c - start), start);
364 gboolean escaped = FALSE;
368 value = g_string_new ("");
375 g_string_append_c (value, '\\');
381 else if (!escaped && *c == '\"')
384 g_string_append_c (value, *c);
387 param_value = value->str;
388 g_string_free (value, FALSE);
395 while (*c && !isspace (*c))
397 param_value = g_strdup_printf ("%.*s", (int) (c - start), start);
401 param_value = g_strdup ("");
403 g_hash_table_insert (params, param_name, param_value);
406 if (strcmp (name, "WAIT") == 0)
408 /* Use a main loop so that our DBus functions are still responsive */
409 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
410 g_timeout_add_seconds (1, stop_loop, loop);
411 g_main_loop_run (loop);
412 g_main_loop_unref (loop);
414 else if (strcmp (name, "LIST-SHARED-DATA-DIRS") == 0)
419 GList *paths = NULL, *link;
422 shared_dir = g_strdup_printf ("%s/var/lib/lightdm-data", temp_dir);
423 dir = g_dir_open (shared_dir, 0, NULL);
424 while ((path = g_dir_read_name (dir)))
426 gchar *full_path = g_build_filename (shared_dir, path, NULL);
427 paths = g_list_insert_sorted (paths, full_path, (GCompareFunc)g_strcmp0);
432 status = g_string_new ("RUNNER LIST-SHARED-DATA-DIRS DIRS=");
433 for (link = paths; link; link = link->next)
435 path = (const gchar *)link->data;
437 if (g_stat (path, &buf) != 0)
441 g_string_append (status, ",");
442 gchar *basename = g_path_get_basename (path);
443 g_string_append_printf (status, "%s:%u:%u:0%o", basename,
444 buf.st_uid, buf.st_gid,
445 buf.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO));
448 g_list_free_full (paths, g_free);
450 check_status (status->str);
451 g_string_free (status, TRUE);
453 else if (strcmp (name, "LIST-SEATS") == 0)
455 GVariant *result, *value;
461 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
462 "org.freedesktop.DisplayManager",
463 "/org/freedesktop/DisplayManager",
464 "org.freedesktop.DBus.Properties",
466 g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Seats"),
467 G_VARIANT_TYPE ("(v)"),
468 G_DBUS_CALL_FLAGS_NONE,
473 status = g_string_new ("RUNNER LIST-SEATS SEATS=");
474 g_variant_get (result, "(v)", &value);
475 g_variant_get (value, "ao", &iter);
476 while (g_variant_iter_loop (iter, "&o", &path))
479 g_string_append (status, ",");
480 g_string_append (status, path);
483 g_variant_unref (value);
484 g_variant_unref (result);
486 check_status (status->str);
487 g_string_free (status, TRUE);
489 else if (strcmp (name, "LIST-SESSIONS") == 0)
491 GVariant *result, *value;
497 result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
498 "org.freedesktop.DisplayManager",
499 "/org/freedesktop/DisplayManager",
500 "org.freedesktop.DBus.Properties",
502 g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Sessions"),
503 G_VARIANT_TYPE ("(v)"),
504 G_DBUS_CALL_FLAGS_NONE,
509 status = g_string_new ("RUNNER LIST-SESSIONS SESSIONS=");
510 g_variant_get (result, "(v)", &value);
511 g_variant_get (value, "ao", &iter);
512 while (g_variant_iter_loop (iter, "&o", &path))
515 g_string_append (status, ",");
516 g_string_append (status, path);
519 g_variant_unref (value);
520 g_variant_unref (result);
522 check_status (status->str);
523 g_string_free (status, TRUE);
525 else if (strcmp (name, "SWITCH-TO-GREETER") == 0)
527 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
528 "org.freedesktop.DisplayManager",
529 "/org/freedesktop/DisplayManager/Seat0",
530 "org.freedesktop.DisplayManager.Seat",
532 g_variant_new ("()"),
533 G_VARIANT_TYPE ("()"),
534 G_DBUS_CALL_FLAGS_NONE,
538 check_status ("RUNNER SWITCH-TO-GREETER");
540 else if (strcmp (name, "SWITCH-TO-USER") == 0)
542 gchar *status_text, *username;
544 username = g_hash_table_lookup (params, "USERNAME");
545 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
546 "org.freedesktop.DisplayManager",
547 "/org/freedesktop/DisplayManager/Seat0",
548 "org.freedesktop.DisplayManager.Seat",
550 g_variant_new ("(ss)", username, ""),
551 G_VARIANT_TYPE ("()"),
552 G_DBUS_CALL_FLAGS_NONE,
556 status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s", username);
557 check_status (status_text);
558 g_free (status_text);
560 else if (strcmp (name, "SWITCH-TO-GUEST") == 0)
562 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
563 "org.freedesktop.DisplayManager",
564 "/org/freedesktop/DisplayManager/Seat0",
565 "org.freedesktop.DisplayManager.Seat",
567 g_variant_new ("(s)", ""),
568 G_VARIANT_TYPE ("()"),
569 G_DBUS_CALL_FLAGS_NONE,
573 check_status ("RUNNER SWITCH-TO-GUEST");
575 else if (strcmp (name, "STOP-DAEMON") == 0)
576 stop_process (lightdm_process);
577 // FIXME: Make generic RUN-COMMAND
578 else if (strcmp (name, "START-XSERVER") == 0)
580 gchar *xserver_args, *command_line;
584 GError *error = NULL;
586 xserver_args = g_hash_table_lookup (params, "ARGS");
589 command_line = g_strdup_printf ("%s/tests/src/X %s", BUILDDIR, xserver_args);
591 if (!g_shell_parse_argv (command_line, NULL, &argv, &error) ||
592 !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error))
594 g_printerr ("Error starting X server: %s", error->message);
599 process = watch_process (pid);
600 g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process);
603 else if (strcmp (name, "START-VNC-CLIENT") == 0)
605 gchar *vnc_client_args, *command_line;
609 GError *error = NULL;
611 vnc_client_args = g_hash_table_lookup (params, "ARGS");
612 if (!vnc_client_args)
613 vnc_client_args = "";
614 command_line = g_strdup_printf ("%s/tests/src/vnc-client %s", BUILDDIR, vnc_client_args);
616 if (!g_shell_parse_argv (command_line, NULL, &argv, &error) ||
617 !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error))
619 g_printerr ("Error starting VNC client: %s", error->message);
624 process = watch_process (pid);
625 g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process);
628 else if (strcmp (name, "ADD-USER") == 0)
630 gchar *status_text, *username;
633 username = g_hash_table_lookup (params, "USERNAME");
634 user = get_accounts_user_by_name (username);
636 accounts_user_set_hidden (user, FALSE, TRUE);
638 g_warning ("Unknown user %s", username);
640 status_text = g_strdup_printf ("RUNNER ADD-USER USERNAME=%s", username);
641 check_status (status_text);
642 g_free (status_text);
644 else if (strcmp (name, "UPDATE-USER") == 0)
646 GString *status_text;
649 GError *error = NULL;
651 status_text = g_string_new ("RUNNER UPDATE-USER USERNAME=");
653 username = g_hash_table_lookup (params, "USERNAME");
654 g_string_append (status_text, username);
655 user = get_accounts_user_by_name (username);
658 if (g_hash_table_lookup (params, "NAME"))
660 user->user_name = g_strdup (g_hash_table_lookup (params, "NAME"));
661 g_string_append_printf (status_text, " NAME=%s", user->user_name);
663 if (g_hash_table_lookup (params, "REAL-NAME"))
665 user->real_name = g_strdup (g_hash_table_lookup (params, "REAL-NAME"));
666 g_string_append_printf (status_text, " REAL-NAME=%s", user->real_name);
668 if (g_hash_table_lookup (params, "HOME-DIRECTORY"))
670 user->home_directory = g_strdup (g_hash_table_lookup (params, "HOME-DIRECTORY"));
671 g_string_append_printf (status_text, " HOME-DIRECTORY=%s", user->home_directory);
673 if (g_hash_table_lookup (params, "IMAGE"))
675 user->image = g_strdup (g_hash_table_lookup (params, "IMAGE"));
676 g_string_append_printf (status_text, " IMAGE=%s", user->image);
678 if (g_hash_table_lookup (params, "BACKGROUND"))
680 user->background = g_strdup (g_hash_table_lookup (params, "BACKGROUND"));
681 g_string_append_printf (status_text, " BACKGROUND=%s", user->background);
683 if (g_hash_table_lookup (params, "LANGUAGE"))
685 user->language = g_strdup (g_hash_table_lookup (params, "LANGUAGE"));
686 g_string_append_printf (status_text, " LANGUAGE=%s", user->language);
688 if (g_hash_table_lookup (params, "LAYOUTS"))
690 const gchar *value = g_hash_table_lookup (params, "LAYOUTS");
691 user->layouts = g_strsplit (value, ";", -1);
692 g_string_append_printf (status_text, " LAYOUTS=%s", value);
694 if (g_hash_table_lookup (params, "HAS-MESSAGES"))
696 user->has_messages = g_strcmp0 (g_hash_table_lookup (params, "HAS-MESSAGES"), "TRUE") == 0;
697 g_string_append_printf (status_text, " HAS-MESSAGES=%s", user->has_messages ? "TRUE" : "FALSE");
699 if (g_hash_table_lookup (params, "SESSION"))
701 user->xsession = g_strdup (g_hash_table_lookup (params, "SESSION"));
702 g_string_append_printf (status_text, " SESSION=%s", user->xsession);
706 g_warning ("Unknown user %s", username);
708 g_dbus_connection_emit_signal (accounts_connection,
711 "org.freedesktop.Accounts.User",
713 g_variant_new ("()"),
716 g_warning ("Failed to emit Changed: %s", error->message);
717 g_clear_error (&error);
719 check_status (status_text->str);
720 g_string_free (status_text, TRUE);
722 else if (strcmp (name, "DELETE-USER") == 0)
724 gchar *status_text, *username;
727 username = g_hash_table_lookup (params, "USERNAME");
728 user = get_accounts_user_by_name (username);
730 accounts_user_set_hidden (user, TRUE, TRUE);
732 g_warning ("Unknown user %s", username);
734 status_text = g_strdup_printf ("RUNNER DELETE-USER USERNAME=%s", username);
735 check_status (status_text);
736 g_free (status_text);
738 /* Forward to external processes */
739 else if (g_str_has_prefix (name, "SESSION-") ||
740 g_str_has_prefix (name, "GREETER-") ||
741 g_str_has_prefix (name, "XSERVER-") ||
742 strcmp (name, "UNITY-SYSTEM-COMPOSITOR") == 0)
745 for (link = status_clients; link; link = link->next)
747 StatusClient *client = link->data;
749 GError *error = NULL;
751 length = strlen (command);
752 if (g_socket_send (client->socket, (gchar *) &length, sizeof (length), NULL, &error) < 0 ||
753 g_socket_send (client->socket, command, strlen (command), NULL, &error) < 0)
754 g_printerr ("Failed to write to client socket: %s\n", error->message);
755 g_clear_error (&error);
760 g_printerr ("Unknown command '%s'\n", name);
765 g_hash_table_unref (params);
771 /* Stop daemon if requested */
776 /* Commands start with an asterisk */
777 line = get_script_line (NULL);
778 if (!line || line->text[0] != '*')
781 statuses = g_list_append (statuses, g_strdup (line->text));
784 handle_command (line->text + 1);
787 /* Stop at the end of the script */
788 if (get_script_line (NULL) == NULL)
793 status_timeout_cb (gpointer data)
797 line = get_script_line (NULL);
798 fail ("(timeout)", line ? line->text : NULL);
804 check_status (const gchar *status)
807 gboolean result = FALSE;
813 statuses = g_list_append (statuses, g_strdup (status));
815 if (getenv ("DEBUG"))
816 g_print ("%s\n", status);
818 /* Try and match against expected */
819 prefix = get_prefix (status);
820 line = get_script_line (prefix);
824 gchar *full_pattern = g_strdup_printf ("^%s$", line->text);
825 result = g_regex_match_simple (full_pattern, status, 0, 0);
826 g_free (full_pattern);
832 line = get_script_line (NULL);
833 fail (NULL, line ? line->text : NULL);
839 /* Restart timeout */
840 g_source_remove (status_timeout);
841 status_timeout = g_timeout_add (status_timeout_ms, status_timeout_cb, NULL);
847 status_message_cb (GSocket *socket, GIOCondition condition, StatusClient *client)
852 GError *error = NULL;
854 n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
856 n_read = g_socket_receive (socket, buffer, length, NULL, &error);
858 g_warning ("Error reading from socket: %s", error->message);
859 g_clear_error (&error);
862 status_clients = g_list_remove (status_clients, client);
863 g_object_unref (client->socket);
869 buffer[n_read] = '\0';
870 check_status (buffer);
877 status_connect_cb (gpointer data)
880 GError *error = NULL;
882 socket = g_socket_accept (status_socket, NULL, &error);
884 g_warning ("Failed to accept status connection: %s", error->message);
885 g_clear_error (&error);
888 StatusClient *client;
890 client = g_malloc0 (sizeof (StatusClient));
891 client->socket = socket;
892 client->source = g_socket_create_source (socket, G_IO_IN, NULL);
893 status_clients = g_list_append (status_clients, client);
895 g_source_set_callback (client->source, (GSourceFunc) status_message_cb, client, NULL);
896 g_source_attach (client->source, NULL);
903 load_script (const gchar *filename)
906 gchar *data, **lines;
908 if (!g_file_get_contents (filename, &data, NULL, NULL))
910 g_printerr ("Unable to load script: %s\n", filename);
914 lines = g_strsplit (data, "\n", -1);
917 /* Load lines with #? prefix as expected behaviour */
918 for (i = 0; lines[i]; i++)
920 gchar *text = g_strstrip (lines[i]);
921 if (g_str_has_prefix (text, "#?"))
924 line = g_malloc0 (sizeof (ScriptLine));
925 line->text = g_strdup (text + 2);
927 script = g_list_append (script, line);
934 handle_upower_call (GDBusConnection *connection,
936 const gchar *object_path,
937 const gchar *interface_name,
938 const gchar *method_name,
939 GVariant *parameters,
940 GDBusMethodInvocation *invocation,
943 if (strcmp (method_name, "SuspendAllowed") == 0)
945 check_status ("UPOWER SUSPEND-ALLOWED");
946 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
948 else if (strcmp (method_name, "Suspend") == 0)
950 check_status ("UPOWER SUSPEND");
951 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
953 else if (strcmp (method_name, "HibernateAllowed") == 0)
955 check_status ("UPOWER HIBERNATE-ALLOWED");
956 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
958 else if (strcmp (method_name, "Hibernate") == 0)
960 check_status ("UPOWER HIBERNATE");
961 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
964 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
968 upower_name_acquired_cb (GDBusConnection *connection,
972 const gchar *upower_interface =
974 " <interface name='org.freedesktop.UPower'>"
975 " <method name='SuspendAllowed'>"
976 " <arg name='allowed' direction='out' type='b'/>"
978 " <method name='Suspend'/>"
979 " <method name='HibernateAllowed'>"
980 " <arg name='allowed' direction='out' type='b'/>"
982 " <method name='Hibernate'/>"
985 static const GDBusInterfaceVTable upower_vtable =
989 GDBusNodeInfo *upower_info;
990 GError *error = NULL;
992 upower_info = g_dbus_node_info_new_for_xml (upower_interface, &error);
994 g_warning ("Failed to parse D-Bus interface: %s", error->message);
995 g_clear_error (&error);
998 g_dbus_connection_register_object (connection,
999 "/org/freedesktop/UPower",
1000 upower_info->interfaces[0],
1005 g_warning ("Failed to register UPower service: %s", error->message);
1006 g_clear_error (&error);
1007 g_dbus_node_info_unref (upower_info);
1010 if (service_count == 0)
1015 start_upower_daemon (void)
1018 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1019 "org.freedesktop.UPower",
1020 G_BUS_NAME_OWNER_FLAGS_NONE,
1021 upower_name_acquired_cb,
1029 open_ck_session (GVariant *params)
1036 GError *error = NULL;
1038 session = g_malloc0 (sizeof (CKSession));
1039 ck_sessions = g_list_append (ck_sessions, session);
1041 cookie = g_string_new ("ck-cookie");
1042 g_variant_get (params, "a(sv)", &iter);
1043 while (g_variant_iter_loop (iter, "(&sv)", &name, &value))
1045 if (strcmp (name, "x11-display") == 0)
1047 const gchar *display;
1048 g_variant_get (value, "&s", &display);
1049 g_string_append_printf (cookie, "-x%s", display);
1053 session->cookie = cookie->str;
1054 g_string_free (cookie, FALSE);
1055 session->path = g_strdup_printf ("/org/freedesktop/ConsoleKit/Session%d", ck_session_index++);
1056 session->id = g_dbus_connection_register_object (ck_connection,
1058 ck_session_info->interfaces[0],
1064 g_warning ("Failed to register CK Session: %s", error->message);
1065 g_clear_error (&error);
1071 handle_ck_call (GDBusConnection *connection,
1072 const gchar *sender,
1073 const gchar *object_path,
1074 const gchar *interface_name,
1075 const gchar *method_name,
1076 GVariant *parameters,
1077 GDBusMethodInvocation *invocation,
1080 if (strcmp (method_name, "CanRestart") == 0)
1082 check_status ("CONSOLE-KIT CAN-RESTART");
1083 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1085 else if (strcmp (method_name, "CanStop") == 0)
1087 check_status ("CONSOLE-KIT CAN-STOP");
1088 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1090 else if (strcmp (method_name, "CloseSession") == 0)
1091 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
1092 else if (strcmp (method_name, "OpenSession") == 0)
1094 GVariantBuilder params;
1095 g_variant_builder_init (¶ms, G_VARIANT_TYPE ("a(sv)"));
1096 CKSession *session = open_ck_session (g_variant_builder_end (¶ms));
1097 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", session->cookie));
1099 else if (strcmp (method_name, "OpenSessionWithParameters") == 0)
1101 CKSession *session = open_ck_session (g_variant_get_child_value (parameters, 0));
1102 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", session->cookie));
1104 else if (strcmp (method_name, "GetSessionForCookie") == 0)
1109 g_variant_get (parameters, "(&s)", &cookie);
1111 for (link = ck_sessions; link; link = link->next)
1113 CKSession *session = link->data;
1114 if (strcmp (session->cookie, cookie) != 0)
1116 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", session->path));
1121 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to find session for cookie");
1123 else if (strcmp (method_name, "Restart") == 0)
1125 check_status ("CONSOLE-KIT RESTART");
1126 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1128 else if (strcmp (method_name, "Stop") == 0)
1130 check_status ("CONSOLE-KIT STOP");
1131 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1134 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1138 handle_ck_session_call (GDBusConnection *connection,
1139 const gchar *sender,
1140 const gchar *object_path,
1141 const gchar *interface_name,
1142 const gchar *method_name,
1143 GVariant *parameters,
1144 GDBusMethodInvocation *invocation,
1147 CKSession *session = user_data;
1149 if (strcmp (method_name, "Lock") == 0)
1151 if (!session->locked)
1152 check_status ("CONSOLE-KIT LOCK-SESSION");
1153 session->locked = TRUE;
1154 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1156 else if (strcmp (method_name, "Unlock") == 0)
1158 if (session->locked)
1159 check_status ("CONSOLE-KIT UNLOCK-SESSION");
1160 session->locked = FALSE;
1161 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1164 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1168 ck_name_acquired_cb (GDBusConnection *connection,
1172 const gchar *ck_interface =
1174 " <interface name='org.freedesktop.ConsoleKit.Manager'>"
1175 " <method name='CanRestart'>"
1176 " <arg name='can_restart' direction='out' type='b'/>"
1178 " <method name='CanStop'>"
1179 " <arg name='can_stop' direction='out' type='b'/>"
1181 " <method name='CloseSession'>"
1182 " <arg name='cookie' direction='in' type='s'/>"
1183 " <arg name='result' direction='out' type='b'/>"
1185 " <method name='OpenSession'>"
1186 " <arg name='cookie' direction='out' type='s'/>"
1188 " <method name='OpenSessionWithParameters'>"
1189 " <arg name='parameters' direction='in' type='a(sv)'/>"
1190 " <arg name='cookie' direction='out' type='s'/>"
1192 " <method name='GetSessionForCookie'>"
1193 " <arg name='cookie' direction='in' type='s'/>"
1194 " <arg name='ssid' direction='out' type='o'/>"
1196 " <method name='Restart'/>"
1197 " <method name='Stop'/>"
1198 " <signal name='SeatAdded'>"
1199 " <arg name='seat' type='o'/>"
1201 " <signal name='SeatRemoved'>"
1202 " <arg name='seat' type='o'/>"
1206 static const GDBusInterfaceVTable ck_vtable =
1210 const gchar *ck_session_interface =
1212 " <interface name='org.freedesktop.ConsoleKit.Session'>"
1213 " <method name='Lock'/>"
1214 " <method name='Unlock'/>"
1217 GDBusNodeInfo *ck_info;
1218 GError *error = NULL;
1220 ck_connection = connection;
1222 ck_info = g_dbus_node_info_new_for_xml (ck_interface, &error);
1224 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1225 g_clear_error (&error);
1228 ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface, &error);
1230 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1231 g_clear_error (&error);
1232 if (!ck_session_info)
1234 g_dbus_connection_register_object (connection,
1235 "/org/freedesktop/ConsoleKit/Manager",
1236 ck_info->interfaces[0],
1241 g_warning ("Failed to register console kit service: %s", error->message);
1242 g_clear_error (&error);
1243 g_dbus_node_info_unref (ck_info);
1246 if (service_count == 0)
1251 start_console_kit_daemon (void)
1254 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1255 "org.freedesktop.ConsoleKit",
1256 G_BUS_NAME_OWNER_FLAGS_NONE,
1257 ck_name_acquired_cb,
1265 handle_login1_session_call (GDBusConnection *connection,
1266 const gchar *sender,
1267 const gchar *object_path,
1268 const gchar *interface_name,
1269 const gchar *method_name,
1270 GVariant *parameters,
1271 GDBusMethodInvocation *invocation,
1274 Login1Session *session = user_data;
1276 if (strcmp (method_name, "Lock") == 0)
1278 if (!session->locked)
1279 check_status ("LOGIN1 LOCK-SESSION");
1280 session->locked = TRUE;
1281 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1283 else if (strcmp (method_name, "Unlock") == 0)
1285 if (session->locked)
1286 check_status ("LOGIN1 UNLOCK-SESSION");
1287 session->locked = FALSE;
1288 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1291 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1294 static Login1Session *
1295 open_login1_session (GDBusConnection *connection,
1298 Login1Session *session;
1299 GError *error = NULL;
1300 GDBusNodeInfo *login1_session_info;
1302 const gchar *login1_session_interface =
1304 " <interface name='org.freedesktop.login1.Session'>"
1305 " <method name='Lock'/>"
1306 " <method name='Unlock'/>"
1309 static const GDBusInterfaceVTable login1_session_vtable =
1311 handle_login1_session_call,
1314 session = g_malloc0 (sizeof (Login1Session));
1315 login1_sessions = g_list_append (login1_sessions, session);
1317 session->path = g_strdup_printf("/org/freedesktop/login1/Session/c%d",
1318 login1_session_index++);
1322 login1_session_info = g_dbus_node_info_new_for_xml (login1_session_interface,
1325 g_warning ("Failed to parse login1 session D-Bus interface: %s",
1327 g_clear_error (&error);
1328 if (!login1_session_info)
1331 g_dbus_connection_register_object (connection,
1333 login1_session_info->interfaces[0],
1334 &login1_session_vtable,
1339 g_warning ("Failed to register login1 session: %s", error->message);
1340 g_clear_error (&error);
1341 g_dbus_node_info_unref (login1_session_info);
1348 handle_login1_call (GDBusConnection *connection,
1349 const gchar *sender,
1350 const gchar *object_path,
1351 const gchar *interface_name,
1352 const gchar *method_name,
1353 GVariant *parameters,
1354 GDBusMethodInvocation *invocation,
1358 if (strcmp (method_name, "GetSessionByPID") == 0)
1360 /* Look for a session with our PID, and create one if we don't have one
1364 Login1Session *ret = NULL;
1366 g_variant_get (parameters, "(u)", &pid);
1368 for (link = login1_sessions; link; link = link->next)
1370 Login1Session *session;
1371 session = link->data;
1372 if (session->pid == pid)
1380 ret = open_login1_session (connection, parameters);
1382 g_dbus_method_invocation_return_value (invocation,
1383 g_variant_new("(o)", ret->path));
1386 else if (strcmp (method_name, "CanReboot") == 0)
1388 check_status ("LOGIN1 CAN-REBOOT");
1389 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1391 else if (strcmp (method_name, "Reboot") == 0)
1393 gboolean interactive;
1394 g_variant_get (parameters, "(b)", &interactive);
1395 check_status ("LOGIN1 REBOOT");
1396 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1398 else if (strcmp (method_name, "CanPowerOff") == 0)
1400 check_status ("LOGIN1 CAN-POWER-OFF");
1401 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1403 else if (strcmp (method_name, "Suspend") == 0)
1405 gboolean interactive;
1406 g_variant_get (parameters, "(b)", &interactive);
1407 check_status ("LOGIN1 SUSPEND");
1408 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1410 else if (strcmp (method_name, "CanSuspend") == 0)
1412 check_status ("LOGIN1 CAN-SUSPEND");
1413 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1415 else if (strcmp (method_name, "PowerOff") == 0)
1417 gboolean interactive;
1418 g_variant_get (parameters, "(b)", &interactive);
1419 check_status ("LOGIN1 POWER-OFF");
1420 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1422 else if (strcmp (method_name, "CanHibernate") == 0)
1424 check_status ("LOGIN1 CAN-HIBERNATE");
1425 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "yes"));
1427 else if (strcmp (method_name, "Hibernate") == 0)
1429 gboolean interactive;
1430 g_variant_get (parameters, "(b)", &interactive);
1431 check_status ("LOGIN1 HIBERNATE");
1432 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1435 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1439 login1_name_acquired_cb (GDBusConnection *connection,
1443 const gchar *login1_interface =
1445 " <interface name='org.freedesktop.login1.Manager'>"
1446 " <method name='GetSessionByPID'>"
1447 " <arg name='pid' type='u' direction='in'/>"
1448 " <arg name='session' type='o' direction='out'/>"
1450 " <method name='CanReboot'>"
1451 " <arg name='result' direction='out' type='s'/>"
1453 " <method name='Reboot'>"
1454 " <arg name='interactive' direction='in' type='b'/>"
1456 " <method name='CanPowerOff'>"
1457 " <arg name='result' direction='out' type='s'/>"
1459 " <method name='PowerOff'>"
1460 " <arg name='interactive' direction='in' type='b'/>"
1462 " <method name='CanSuspend'>"
1463 " <arg name='result' direction='out' type='s'/>"
1465 " <method name='Suspend'>"
1466 " <arg name='interactive' direction='in' type='b'/>"
1468 " <method name='CanHibernate'>"
1469 " <arg name='result' direction='out' type='s'/>"
1471 " <method name='Hibernate'>"
1472 " <arg name='interactive' direction='in' type='b'/>"
1476 static const GDBusInterfaceVTable login1_vtable =
1480 GDBusNodeInfo *login1_info;
1481 GError *error = NULL;
1483 login1_info = g_dbus_node_info_new_for_xml (login1_interface, &error);
1485 g_warning ("Failed to parse login1 D-Bus interface: %s", error->message);
1486 g_clear_error (&error);
1489 g_dbus_connection_register_object (connection,
1490 "/org/freedesktop/login1",
1491 login1_info->interfaces[0],
1496 g_warning ("Failed to register login1 service: %s", error->message);
1497 g_clear_error (&error);
1498 g_dbus_node_info_unref (login1_info);
1501 if (service_count == 0)
1506 start_login1_daemon (void)
1509 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1510 "org.freedesktop.login1",
1511 G_BUS_NAME_OWNER_FLAGS_NONE,
1512 login1_name_acquired_cb,
1519 static AccountsUser *
1520 get_accounts_user_by_uid (guint uid)
1524 for (link = accounts_users; link; link = link->next)
1526 AccountsUser *u = link->data;
1534 static AccountsUser *
1535 get_accounts_user_by_name (const gchar *username)
1539 for (link = accounts_users; link; link = link->next)
1541 AccountsUser *u = link->data;
1542 if (strcmp (u->user_name, username) == 0)
1550 accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal)
1552 GError *error = NULL;
1554 user->hidden = hidden;
1556 if (user->hidden && user->id != 0)
1558 g_dbus_connection_unregister_object (accounts_connection, user->id);
1559 g_dbus_connection_emit_signal (accounts_connection,
1561 "/org/freedesktop/Accounts",
1562 "org.freedesktop.Accounts",
1564 g_variant_new ("(o)", user->path),
1567 g_warning ("Failed to emit UserDeleted: %s", error->message);
1568 g_clear_error (&error);
1572 if (!user->hidden && user->id == 0)
1574 user->id = g_dbus_connection_register_object (accounts_connection,
1576 user_info->interfaces[0],
1582 g_warning ("Failed to register user: %s", error->message);
1583 g_clear_error (&error);
1585 g_dbus_connection_emit_signal (accounts_connection,
1587 "/org/freedesktop/Accounts",
1588 "org.freedesktop.Accounts",
1590 g_variant_new ("(o)", user->path),
1593 g_warning ("Failed to emit UserAdded: %s", error->message);
1594 g_clear_error (&error);
1599 load_passwd_file (void)
1601 gchar *path, *data, **lines;
1602 gchar **user_filter = NULL;
1605 if (g_key_file_has_key (config, "test-runner-config", "accounts-service-user-filter", NULL))
1609 filter = g_key_file_get_string (config, "test-runner-config", "accounts-service-user-filter", NULL);
1610 user_filter = g_strsplit (filter, " ", -1);
1614 path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL);
1615 g_file_get_contents (path, &data, NULL, NULL);
1617 lines = g_strsplit (data, "\n", -1);
1620 for (i = 0; lines[i]; i++)
1624 gchar *user_name, *real_name;
1625 AccountsUser *user = NULL;
1627 fields = g_strsplit (lines[i], ":", -1);
1628 if (fields == NULL || g_strv_length (fields) < 7)
1630 g_strfreev (fields);
1634 user_name = fields[0];
1635 uid = atoi (fields[2]);
1636 real_name = fields[4];
1638 user = get_accounts_user_by_uid (uid);
1642 GKeyFile *dmrc_file;
1644 user = g_malloc0 (sizeof (AccountsUser));
1645 accounts_users = g_list_append (accounts_users, user);
1647 /* Only allow users in whitelist */
1648 user->hidden = FALSE;
1653 user->hidden = TRUE;
1654 for (j = 0; user_filter[j] != NULL; j++)
1655 if (strcmp (user_name, user_filter[j]) == 0)
1656 user->hidden = FALSE;
1659 dmrc_file = g_key_file_new ();
1660 path = g_build_filename (temp_dir, "home", user_name, ".dmrc", NULL);
1661 g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_NONE, NULL);
1665 user->user_name = g_strdup (user_name);
1666 user->real_name = g_strdup (real_name);
1667 user->home_directory = g_build_filename (temp_dir, "home", user_name, NULL);
1668 user->language = g_key_file_get_string (dmrc_file, "Desktop", "Language", NULL);
1669 /* DMRC contains a locale, strip the codeset off it to get the language */
1672 gchar *c = strchr (user->language, '.');
1676 user->xsession = g_key_file_get_string (dmrc_file, "Desktop", "Session", NULL);
1677 user->layouts = g_key_file_get_string_list (dmrc_file, "X-Accounts", "Layouts", NULL, NULL);
1680 user->layouts = g_malloc (sizeof (gchar *) * 2);
1681 user->layouts[0] = g_key_file_get_string (dmrc_file, "Desktop", "Layout", NULL);
1682 user->layouts[1] = NULL;
1684 user->has_messages = g_key_file_get_boolean (dmrc_file, "X-Accounts", "HasMessages", NULL);
1685 user->path = g_strdup_printf ("/org/freedesktop/Accounts/User%d", uid);
1686 accounts_user_set_hidden (user, user->hidden, FALSE);
1688 g_key_file_free (dmrc_file);
1691 g_strfreev (fields);
1698 handle_accounts_call (GDBusConnection *connection,
1699 const gchar *sender,
1700 const gchar *object_path,
1701 const gchar *interface_name,
1702 const gchar *method_name,
1703 GVariant *parameters,
1704 GDBusMethodInvocation *invocation,
1707 if (strcmp (method_name, "ListCachedUsers") == 0)
1709 GVariantBuilder builder;
1712 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
1714 load_passwd_file ();
1715 for (link = accounts_users; link; link = link->next)
1717 AccountsUser *user = link->data;
1718 if (!user->hidden && user->uid >= 1000)
1719 g_variant_builder_add_value (&builder, g_variant_new_object_path (user->path));
1722 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(ao)", &builder));
1724 else if (strcmp (method_name, "FindUserByName") == 0)
1726 AccountsUser *user = NULL;
1729 g_variant_get (parameters, "(&s)", &user_name);
1731 load_passwd_file ();
1732 user = get_accounts_user_by_name (user_name);
1736 accounts_user_set_hidden (user, FALSE, TRUE);
1737 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", user->path));
1740 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such user: %s", user_name);
1743 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1747 handle_user_call (GDBusConnection *connection,
1748 const gchar *sender,
1749 const gchar *object_path,
1750 const gchar *interface_name,
1751 const gchar *method_name,
1752 GVariant *parameters,
1753 GDBusMethodInvocation *invocation,
1756 AccountsUser *user = user_data;
1758 if (strcmp (method_name, "SetXSession") == 0)
1762 g_variant_get (parameters, "(&s)", &xsession);
1764 g_free (user->xsession);
1765 user->xsession = g_strdup (xsession);
1767 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1769 /* And notify others that it took */
1770 g_dbus_connection_emit_signal (accounts_connection,
1773 "org.freedesktop.Accounts.User",
1775 g_variant_new ("()"),
1779 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name);
1783 handle_user_get_property (GDBusConnection *connection,
1784 const gchar *sender,
1785 const gchar *object_path,
1786 const gchar *interface_name,
1787 const gchar *property_name,
1791 AccountsUser *user = user_data;
1793 if (strcmp (property_name, "UserName") == 0)
1794 return g_variant_new_string (user->user_name);
1795 else if (strcmp (property_name, "RealName") == 0)
1796 return g_variant_new_string (user->real_name);
1797 else if (strcmp (property_name, "HomeDirectory") == 0)
1798 return g_variant_new_string (user->home_directory);
1799 else if (strcmp (property_name, "SystemAccount") == 0)
1800 return g_variant_new_boolean (user->uid < 1000);
1801 else if (strcmp (property_name, "BackgroundFile") == 0)
1802 return g_variant_new_string (user->background ? user->background : "");
1803 else if (strcmp (property_name, "Language") == 0)
1804 return g_variant_new_string (user->language ? user->language : "");
1805 else if (strcmp (property_name, "IconFile") == 0)
1806 return g_variant_new_string (user->image ? user->image : "");
1807 else if (strcmp (property_name, "Shell") == 0)
1808 return g_variant_new_string ("/bin/sh");
1809 else if (strcmp (property_name, "Uid") == 0)
1810 return g_variant_new_uint64 (user->uid);
1811 else if (strcmp (property_name, "XSession") == 0)
1812 return g_variant_new_string (user->xsession ? user->xsession : "");
1813 else if (strcmp (property_name, "XKeyboardLayouts") == 0)
1815 if (user->layouts != NULL)
1816 return g_variant_new_strv ((const gchar * const *) user->layouts, -1);
1818 return g_variant_new_strv (NULL, 0);
1820 else if (strcmp (property_name, "XHasMessages") == 0)
1821 return g_variant_new_boolean (user->has_messages);
1827 accounts_name_acquired_cb (GDBusConnection *connection,
1831 const gchar *accounts_interface =
1833 " <interface name='org.freedesktop.Accounts'>"
1834 " <method name='ListCachedUsers'>"
1835 " <arg name='user' direction='out' type='ao'/>"
1837 " <method name='FindUserByName'>"
1838 " <arg name='name' direction='in' type='s'/>"
1839 " <arg name='user' direction='out' type='o'/>"
1841 " <signal name='UserAdded'>"
1842 " <arg name='user' type='o'/>"
1844 " <signal name='UserDeleted'>"
1845 " <arg name='user' type='o'/>"
1849 static const GDBusInterfaceVTable accounts_vtable =
1851 handle_accounts_call,
1853 const gchar *user_interface =
1855 " <interface name='org.freedesktop.Accounts.User'>"
1856 " <method name='SetXSession'>"
1857 " <arg name='x_session' direction='in' type='s'/>"
1859 " <property name='UserName' type='s' access='read'/>"
1860 " <property name='RealName' type='s' access='read'/>"
1861 " <property name='HomeDirectory' type='s' access='read'/>"
1862 " <property name='SystemAccount' type='b' access='read'/>"
1863 " <property name='BackgroundFile' type='s' access='read'/>"
1864 " <property name='Language' type='s' access='read'/>"
1865 " <property name='IconFile' type='s' access='read'/>"
1866 " <property name='Shell' type='s' access='read'/>"
1867 " <property name='Uid' type='t' access='read'/>"
1868 " <property name='XSession' type='s' access='read'/>"
1869 " <property name='XKeyboardLayouts' type='as' access='read'/>"
1870 " <property name='XHasMessages' type='b' access='read'/>"
1871 " <signal name='Changed' />"
1874 GError *error = NULL;
1876 accounts_connection = connection;
1878 accounts_info = g_dbus_node_info_new_for_xml (accounts_interface, &error);
1880 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1881 g_clear_error (&error);
1884 user_info = g_dbus_node_info_new_for_xml (user_interface, &error);
1886 g_warning ("Failed to parse D-Bus interface: %s", error->message);
1887 g_clear_error (&error);
1890 g_dbus_connection_register_object (connection,
1891 "/org/freedesktop/Accounts",
1892 accounts_info->interfaces[0],
1898 g_warning ("Failed to register accounts service: %s", error->message);
1899 g_clear_error (&error);
1900 g_dbus_node_info_unref (accounts_info);
1903 if (service_count == 0)
1908 start_accounts_service_daemon (void)
1911 g_bus_own_name (G_BUS_TYPE_SYSTEM,
1912 "org.freedesktop.Accounts",
1913 G_BUS_NAME_OWNER_FLAGS_NONE,
1914 accounts_name_acquired_cb,
1924 GString *command_line;
1925 gchar **lightdm_argv;
1927 GError *error = NULL;
1931 status_timeout = g_timeout_add (status_timeout_ms, status_timeout_cb, NULL);
1933 command_line = g_string_new ("lightdm");
1934 if (getenv ("DEBUG"))
1935 g_string_append (command_line, " --debug");
1936 g_string_append_printf (command_line, " --cache-dir %s/cache", temp_dir);
1938 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",
1939 g_getenv ("PATH"), g_getenv ("LD_PRELOAD"), g_getenv ("LD_LIBRARY_PATH"), g_getenv ("LIGHTDM_TEST_ROOT"), g_getenv ("DBUS_SESSION_BUS_ADDRESS"),
1942 if (!g_shell_parse_argv (command_line->str, NULL, &lightdm_argv, &error))
1944 g_warning ("Error parsing command line: %s", error->message);
1945 quit (EXIT_FAILURE);
1947 g_clear_error (&error);
1949 if (!g_spawn_async (NULL, lightdm_argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &lightdm_pid, &error))
1951 g_warning ("Error launching LightDM: %s", error->message);
1952 quit (EXIT_FAILURE);
1954 g_clear_error (&error);
1955 lightdm_process = watch_process (lightdm_pid);
1957 check_status ("RUNNER DAEMON-START");
1961 signal_cb (gpointer user_data)
1963 g_print ("Caught signal, quitting\n");
1964 quit (EXIT_FAILURE);
1969 main (int argc, char **argv)
1973 gchar *greeter = NULL, *script_name, *config_file, *additional_system_config;
1974 gchar *additional_config, *path, *path1, *path2, *ld_preload, *ld_library_path, *home_dir;
1975 GString *passwd_data, *group_data;
1976 GSource *status_source;
1978 GError *error = NULL;
1980 #if !defined(GLIB_VERSION_2_36)
1984 loop = g_main_loop_new (NULL, FALSE);
1986 g_unix_signal_add (SIGINT, signal_cb, NULL);
1987 g_unix_signal_add (SIGTERM, signal_cb, NULL);
1989 children = g_hash_table_new (g_direct_hash, g_direct_equal);
1993 g_printerr ("Usage %s SCRIPT-NAME GREETER\n", argv[0]);
1994 quit (EXIT_FAILURE);
1996 script_name = argv[1];
1997 config_file = g_strdup_printf ("%s.conf", script_name);
1998 config_path = g_build_filename (SRCDIR, "tests", "scripts", config_file, NULL);
1999 g_free (config_file);
2001 config = g_key_file_new ();
2002 g_key_file_load_from_file (config, config_path, G_KEY_FILE_NONE, NULL);
2004 load_script (config_path);
2006 if (!getcwd (cwd, 1024))
2008 g_critical ("Error getting current directory: %s", strerror (errno));
2009 quit (EXIT_FAILURE);
2012 /* Don't contact our X server */
2013 g_unsetenv ("DISPLAY");
2015 /* Override system calls */
2016 ld_preload = g_build_filename (BUILDDIR, "tests", "src", ".libs", "libsystem.so", NULL);
2017 g_setenv ("LD_PRELOAD", ld_preload, TRUE);
2018 g_free (ld_preload);
2020 /* Run test programs */
2021 path = g_strdup_printf ("%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, g_getenv ("PATH"));
2022 g_setenv ("PATH", path, TRUE);
2025 /* Use locally built libraries */
2026 path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL);
2027 path2 = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL);
2028 ld_library_path = g_strdup_printf ("%s:%s", path1, path2);
2031 g_setenv ("LD_LIBRARY_PATH", ld_library_path, TRUE);
2032 g_free (ld_library_path);
2033 path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL);
2034 g_setenv ("GI_TYPELIB_PATH", path1, TRUE);
2037 /* Run in a temporary directory inside the build directory */
2038 /* Note we have to pick a name that is short since Unix sockets in this directory have a 108 character limit on their paths */
2043 name = g_strdup_printf (".r%d", i);
2045 temp_dir = g_build_filename ("/tmp", name, NULL);
2047 if (!g_file_test (temp_dir, G_FILE_TEST_EXISTS))
2051 g_mkdir_with_parents (temp_dir, 0755);
2052 g_setenv ("LIGHTDM_TEST_ROOT", temp_dir, TRUE);
2054 /* Open socket for status */
2055 /* Note we have to pick a socket name that is short since there is a 108 character limit on the name */
2056 status_socket_name = g_build_filename (temp_dir, ".s", NULL);
2057 unlink (status_socket_name);
2058 status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
2060 g_warning ("Error creating status socket %s: %s", status_socket_name, error->message);
2061 g_clear_error (&error);
2064 GSocketAddress *address;
2067 address = g_unix_socket_address_new (status_socket_name);
2068 result = g_socket_bind (status_socket, address, FALSE, &error);
2069 g_object_unref (address);
2071 g_warning ("Error binding status socket %s: %s", status_socket_name, error->message);
2072 g_clear_error (&error);
2075 result = g_socket_listen (status_socket, &error);
2077 g_warning ("Error listening on status socket %s: %s", status_socket_name, error->message);
2078 g_clear_error (&error);
2082 g_object_unref (status_socket);
2083 status_socket = NULL;
2087 quit (EXIT_FAILURE);
2088 status_source = g_socket_create_source (status_socket, G_IO_IN, NULL);
2089 g_source_set_callback (status_source, status_connect_cb, NULL, NULL);
2090 g_source_attach (status_source, NULL);
2092 /* Set up a skeleton file system */
2093 g_mkdir_with_parents (g_strdup_printf ("%s/etc", temp_dir), 0755);
2094 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share", temp_dir), 0755);
2095 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/sessions", temp_dir), 0755);
2096 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/remote-sessions", temp_dir), 0755);
2097 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/greeters", temp_dir), 0755);
2098 g_mkdir_with_parents (g_strdup_printf ("%s/tmp", temp_dir), 0755);
2099 g_mkdir_with_parents (g_strdup_printf ("%s/var/lib/lightdm-data", temp_dir), 0755);
2100 g_mkdir_with_parents (g_strdup_printf ("%s/var/run", temp_dir), 0755);
2101 g_mkdir_with_parents (g_strdup_printf ("%s/var/log", temp_dir), 0755);
2103 /* Copy over the configuration */
2104 g_mkdir_with_parents (g_strdup_printf ("%s/etc/lightdm", temp_dir), 0755);
2105 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))
2106 if (system (g_strdup_printf ("cp %s %s/etc/lightdm/lightdm.conf", config_path, temp_dir)))
2107 perror ("Failed to copy configuration");
2109 additional_system_config = g_key_file_get_string (config, "test-runner-config", "additional-system-config", NULL);
2110 if (additional_system_config)
2114 g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/lightdm.conf.d", temp_dir), 0755);
2116 files = g_strsplit (additional_system_config, " ", -1);
2117 for (i = 0; files[i]; i++)
2118 if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/usr/share/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir)))
2119 perror ("Failed to copy configuration");
2123 additional_config = g_key_file_get_string (config, "test-runner-config", "additional-config", NULL);
2124 if (additional_config)
2128 g_mkdir_with_parents (g_strdup_printf ("%s/etc/lightdm/lightdm.conf.d", temp_dir), 0755);
2130 files = g_strsplit (additional_config, " ", -1);
2131 for (i = 0; files[i]; i++)
2132 if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/etc/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir)))
2133 perror ("Failed to copy configuration");
2137 if (g_key_file_has_key (config, "test-runner-config", "shared-data-dirs", NULL))
2143 dir_string = g_key_file_get_string (config, "test-runner-config", "shared-data-dirs", NULL);
2144 dirs = g_strsplit (dir_string, " ", -1);
2145 g_free (dir_string);
2147 for (i = 0; dirs[i]; i++)
2149 gchar **fields = g_strsplit (dirs[i], ":", -1);
2150 if (g_strv_length (fields) == 4)
2152 gchar *path = g_strdup_printf ("%s/var/lib/lightdm-data/%s", temp_dir, fields[0]);
2153 int uid = g_ascii_strtoll (fields[1], NULL, 10);
2154 int gid = g_ascii_strtoll (fields[2], NULL, 10);
2155 int mode = g_ascii_strtoll (fields[3], NULL, 8);
2156 g_mkdir (path, mode);
2157 g_chmod (path, mode); /* mkdir filters by umask, so make sure we have what we want */
2158 if (chown (path, uid, gid) < 0)
2159 g_warning ("chown (%s) failed: %s", path, strerror (errno));
2162 g_strfreev (fields);
2168 /* Always copy the script */
2169 if (system (g_strdup_printf ("cp %s %s/script", config_path, temp_dir)))
2170 perror ("Failed to copy configuration");
2172 /* Copy over the greeter files */
2173 if (system (g_strdup_printf ("cp %s/sessions/* %s/usr/share/lightdm/sessions", DATADIR, temp_dir)))
2174 perror ("Failed to copy sessions");
2175 if (system (g_strdup_printf ("cp %s/remote-sessions/* %s/usr/share/lightdm/remote-sessions", DATADIR, temp_dir)))
2176 perror ("Failed to copy remote sessions");
2177 if (system (g_strdup_printf ("cp %s/greeters/* %s/usr/share/lightdm/greeters", DATADIR, temp_dir)))
2178 perror ("Failed to copy greeters");
2180 /* Set up the default greeter */
2181 path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL);
2182 greeter = g_strdup_printf ("%s.desktop", argv[2]);
2183 if (symlink (greeter, path) < 0)
2185 g_printerr ("Failed to make greeter symlink %s->%s: %s\n", path, greeter, strerror (errno));
2186 quit (EXIT_FAILURE);
2191 home_dir = g_build_filename (temp_dir, "home", NULL);
2193 /* Make fake users */
2203 {"root", "", "root", 0},
2204 /* Unprivileged account for greeters */
2205 {"lightdm", "", "", 100},
2206 /* These accounts have a password */
2207 {"have-password1", "password", "Password User 1", 1000},
2208 {"have-password2", "password", "Password User 2", 1001},
2209 {"have-password3", "password", "Password User 3", 1002},
2210 {"have-password4", "password", "Password User 4", 1003},
2211 /* This account always prompts for a password, even if using the lightdm-autologin service */
2212 {"always-password", "password", "Password User 4", 1004},
2213 /* These accounts have no password */
2214 {"no-password1", "", "No Password User 1", 1005},
2215 {"no-password2", "", "No Password User 2", 1006},
2216 {"no-password3", "", "No Password User 3", 1007},
2217 {"no-password4", "", "No Password User 4", 1008},
2218 /* This account has a keyboard layout */
2219 {"have-layout", "", "Layout User", 1009},
2220 /* This account has a set of keyboard layouts */
2221 {"have-layouts", "", "Layouts User", 1010},
2222 /* This account has a language set */
2223 {"have-language", "", "Language User", 1011},
2224 /* This account has a preconfigured session */
2225 {"have-session", "", "Session User", 1012},
2226 /* This account has the home directory mounted on login */
2227 {"mount-home-dir", "", "Mounted Home Dir User", 1013},
2228 /* This account is denied access */
2229 {"denied", "", "Denied User", 1014},
2230 /* This account has expired */
2231 {"expired", "", "Expired User", 1015},
2232 /* This account needs a password change */
2233 {"new-authtok", "", "New Token User", 1016},
2234 /* This account is switched to change-user2 when authentication succeeds */
2235 {"change-user1", "", "Change User 1", 1017},
2236 {"change-user2", "", "Change User 2", 1018},
2237 /* This account switches to invalid-user when authentication succeeds */
2238 {"change-user-invalid", "", "Invalid Change User", 1019},
2239 /* This account crashes on authentication */
2240 {"crash-authenticate", "", "Crash Auth User", 1020},
2241 /* This account shows an informational prompt on login */
2242 {"info-prompt", "password", "Info Prompt", 1021},
2243 /* This account shows multiple informational prompts on login */
2244 {"multi-info-prompt","password", "Multi Info Prompt", 1022},
2245 /* This account uses two factor authentication */
2246 {"two-factor", "password", "Two Factor", 1023},
2247 /* This account has a special group */
2248 {"group-member", "password", "Group Member", 1024},
2249 /* This account has the home directory created when the session starts */
2250 {"make-home-dir", "", "Make Home Dir User", 1025},
2251 /* This account fails to open a session */
2252 {"session-error", "password", "Session Error", 1026},
2253 /* This account can't establish credentials */
2254 {"cred-error", "password", "Cred Error", 1027},
2255 /* This account has expired credentials */
2256 {"cred-expired", "password", "Cred Expired", 1028},
2257 /* This account has cannot access their credentials */
2258 {"cred-unavail", "password", "Cred Unavail", 1029},
2259 /* This account sends informational messages for each PAM function that is called */
2260 {"log-pam", "password", "Log PAM", 1030},
2261 /* This account shows multiple prompts on login */
2262 {"multi-prompt", "password", "Multi Prompt", 1031},
2263 /* This account has an existing corrupt X authority */
2264 {"corrupt-xauth", "password", "Corrupt Xauthority", 1032},
2265 /* User to test properties */
2266 {"prop-user", "", "TEST", 1033},
2267 {NULL, NULL, NULL, 0}
2269 passwd_data = g_string_new ("");
2270 group_data = g_string_new ("");
2271 for (i = 0; users[i].user_name; i++)
2273 GKeyFile *dmrc_file;
2274 gboolean save_dmrc = FALSE;
2276 if (strcmp (users[i].user_name, "mount-home-dir") != 0 && strcmp (users[i].user_name, "make-home-dir") != 0)
2278 path = g_build_filename (home_dir, users[i].user_name, NULL);
2279 g_mkdir_with_parents (path, 0755);
2280 if (chown (path, users[i].uid, users[i].uid) < 0)
2281 g_debug ("chown (%s) failed: %s", path, strerror (errno));
2285 dmrc_file = g_key_file_new ();
2286 if (strcmp (users[i].user_name, "have-session") == 0)
2288 g_key_file_set_string (dmrc_file, "Desktop", "Session", "alternative");
2291 if (strcmp (users[i].user_name, "have-layout") == 0)
2293 g_key_file_set_string (dmrc_file, "Desktop", "Layout", "us");
2296 if (strcmp (users[i].user_name, "have-layouts") == 0)
2298 g_key_file_set_string (dmrc_file, "Desktop", "Layout", "ru");
2299 g_key_file_set_string (dmrc_file, "X-Accounts", "Layouts", "fr\toss;ru;");
2302 if (strcmp (users[i].user_name, "have-language") == 0)
2304 g_key_file_set_string (dmrc_file, "Desktop", "Language", "en_AU.utf8");
2312 path = g_build_filename (home_dir, users[i].user_name, ".dmrc", NULL);
2313 data = g_key_file_to_data (dmrc_file, NULL, NULL);
2314 g_file_set_contents (path, data, -1, NULL);
2319 g_key_file_free (dmrc_file);
2321 /* Write corrupt X authority file */
2322 if (strcmp (users[i].user_name, "corrupt-xauth") == 0)
2324 gchar data[1] = { 0xFF };
2326 path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL);
2327 g_file_set_contents (path, data, 1, NULL);
2328 chmod (path, S_IRUSR | S_IWUSR);
2332 /* Add passwd file entry */
2333 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);
2335 /* Add group file entry */
2336 g_string_append_printf (group_data, "%s:x:%d:%s\n", users[i].user_name, users[i].uid, users[i].user_name);
2338 path = g_build_filename (temp_dir, "etc", "passwd", NULL);
2339 g_file_set_contents (path, passwd_data->str, -1, NULL);
2341 g_string_free (passwd_data, TRUE);
2343 /* Add an extra test group */
2344 g_string_append_printf (group_data, "test-group:x:111:\n");
2346 path = g_build_filename (temp_dir, "etc", "group", NULL);
2347 g_file_set_contents (path, group_data->str, -1, NULL);
2349 g_string_free (group_data, TRUE);
2351 if (g_key_file_has_key (config, "test-runner-config", "timeout", NULL))
2352 status_timeout_ms = g_key_file_get_integer (config, "test-runner-config", "timeout", NULL) * 1000;
2354 /* Start D-Bus services */
2355 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-upower", NULL))
2356 start_upower_daemon ();
2357 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-console-kit", NULL))
2358 start_console_kit_daemon ();
2359 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-login1", NULL))
2360 start_login1_daemon ();
2361 if (!g_key_file_get_boolean (config, "test-runner-config", "disable-accounts-service", NULL))
2362 start_accounts_service_daemon ();
2364 g_main_loop_run (loop);
2366 return EXIT_FAILURE;