diff options
-rw-r--r-- | tests/src/X.c | 117 | ||||
-rw-r--r-- | tests/src/Xmir.c | 38 | ||||
-rw-r--r-- | tests/src/Xvnc.c | 43 | ||||
-rw-r--r-- | tests/src/dbus-env.c | 35 | ||||
-rw-r--r-- | tests/src/guest-account.c | 30 | ||||
-rw-r--r-- | tests/src/initctl.c | 7 | ||||
-rw-r--r-- | tests/src/libsystem.c | 487 | ||||
-rw-r--r-- | tests/src/status.c | 59 | ||||
-rw-r--r-- | tests/src/test-gobject-greeter.c | 96 | ||||
-rw-r--r-- | tests/src/test-greeter-wrapper.c | 4 | ||||
-rw-r--r-- | tests/src/test-guest-wrapper.c | 4 | ||||
-rw-r--r-- | tests/src/test-runner.c | 831 | ||||
-rw-r--r-- | tests/src/test-script-hook.c | 4 | ||||
-rw-r--r-- | tests/src/test-session.c | 75 | ||||
-rw-r--r-- | tests/src/unity-system-compositor.c | 28 | ||||
-rw-r--r-- | tests/src/vnc-client.c | 21 | ||||
-rw-r--r-- | tests/src/x-authority.c | 37 | ||||
-rw-r--r-- | tests/src/x-common.c | 29 | ||||
-rw-r--r-- | tests/src/x-server.c | 20 | ||||
-rw-r--r-- | tests/src/xdmcp-client.c | 128 |
20 files changed, 754 insertions, 1339 deletions
diff --git a/tests/src/X.c b/tests/src/X.c index 74bcbedf..6ac4e1c1 100644 --- a/tests/src/X.c +++ b/tests/src/X.c @@ -119,13 +119,11 @@ xdmcp_unwilling_cb (XDMCPClient *client, XDMCPUnwilling *message) static gchar * data_to_string (guint8 *data, gsize data_length) { - static gchar hex_char[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - gchar *text; + gchar *text = malloc (data_length * 2 + 1); gsize i; - - text = malloc (data_length * 2 + 1); for (i = 0; i < data_length; i++) { + static gchar hex_char[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; text[i*2] = hex_char [data[i] >> 4]; text[i*2 + 1] = hex_char [data[i] & 0xF]; } @@ -137,11 +135,8 @@ data_to_string (guint8 *data, gsize data_length) static void xdmcp_accept_cb (XDMCPClient *client, XDMCPAccept *message) { - g_autofree gchar *authentication_data_text = NULL; - g_autofree gchar *authorization_data_text = NULL; - - authentication_data_text = data_to_string (message->authentication_data, message->authentication_data_length); - authorization_data_text = data_to_string (message->authorization_data, message->authorization_data_length); + g_autofree gchar *authentication_data_text = data_to_string (message->authentication_data, message->authentication_data_length); + g_autofree gchar *authorization_data_text = data_to_string (message->authorization_data, message->authorization_data_length); status_notify ("%s GOT-ACCEPT SESSION-ID=%d AUTHENTICATION-NAME=\"%s\" AUTHENTICATION-DATA=%s AUTHORIZATION-NAME=\"%s\" AUTHORIZATION-DATA=%s", id, message->session_id, message->authentication_name, authentication_data_text, message->authorization_name, authorization_data_text); @@ -156,9 +151,7 @@ xdmcp_accept_cb (XDMCPClient *client, XDMCPAccept *message) static void xdmcp_decline_cb (XDMCPClient *client, XDMCPDecline *message) { - g_autofree gchar *authentication_data_text = NULL; - - authentication_data_text = data_to_string (message->authentication_data, message->authentication_data_length); + g_autofree gchar *authentication_data_text = data_to_string (message->authentication_data, message->authentication_data_length); status_notify ("%s GOT-DECLINE STATUS=\"%s\" AUTHENTICATION-NAME=\"%s\" AUTHENTICATION-DATA=%s", id, message->status, message->authentication_name, authentication_data_text); } @@ -227,9 +220,7 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "INDICATE-READY") == 0) { - void *handler; - - handler = signal (SIGUSR1, SIG_IGN); + void *handler = signal (SIGUSR1, SIG_IGN); if (handler == SIG_IGN) { status_notify ("%s INDICATE-READY", id); @@ -240,63 +231,58 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "SEND-QUERY") == 0) { - const gchar *authentication_names_list; - g_auto(GStrv) authentication_names = NULL; - if (!xdmcp_client_start (xdmcp_client)) quit (EXIT_FAILURE); - authentication_names_list = g_hash_table_lookup (params, "AUTHENTICATION-NAMES"); + const gchar *authentication_names_list = g_hash_table_lookup (params, "AUTHENTICATION-NAMES"); if (!authentication_names_list) authentication_names_list = ""; - authentication_names = g_strsplit (authentication_names_list, " ", -1); + g_auto(GStrv) authentication_names = g_strsplit (authentication_names_list, " ", -1); xdmcp_client_send_query (xdmcp_client, authentication_names); } else if (strcmp (name, "SEND-REQUEST") == 0) { - const gchar *text, *addresses_list, *authentication_name, *authentication_data_text, *authorization_names_list, *mfid; int request_display_number = display_number; - g_auto(GStrv) list = NULL; - g_auto(GStrv) authorization_names = NULL; - g_autofree guint8 *authentication_data = NULL; - gsize authentication_data_length, list_length; - gint i; - GInetAddress **addresses; - - text = g_hash_table_lookup (params, "DISPLAY-NUMBER"); + const gchar *text = g_hash_table_lookup (params, "DISPLAY-NUMBER"); if (text) request_display_number = atoi (text); - addresses_list = g_hash_table_lookup (params, "ADDRESSES"); + + const gchar *addresses_list = g_hash_table_lookup (params, "ADDRESSES"); if (!addresses_list) addresses_list = ""; - authentication_name = g_hash_table_lookup (params, "AUTHENTICATION-NAME"); + + const gchar *authentication_name = g_hash_table_lookup (params, "AUTHENTICATION-NAME"); if (!authentication_name) authentication_name = ""; - authentication_data_text = g_hash_table_lookup (params, "AUTHENTICATION-DATA"); + + const gchar *authentication_data_text = g_hash_table_lookup (params, "AUTHENTICATION-DATA"); if (!authentication_data_text) authentication_data_text = ""; - authorization_names_list = g_hash_table_lookup (params, "AUTHORIZATION-NAMES"); + + const gchar *authorization_names_list = g_hash_table_lookup (params, "AUTHORIZATION-NAMES"); if (!authorization_names_list) authorization_names_list = ""; - mfid = g_hash_table_lookup (params, "MFID"); + + const gchar *mfid = g_hash_table_lookup (params, "MFID"); if (!mfid) mfid = ""; - list = g_strsplit (addresses_list, " ", -1); - list_length = g_strv_length (list); - addresses = g_malloc (sizeof (GInetAddress *) * (list_length + 1)); + g_auto(GStrv) list = g_strsplit (addresses_list, " ", -1); + gsize list_length = g_strv_length (list); + GInetAddress **addresses = g_malloc (sizeof (GInetAddress *) * (list_length + 1)); + gsize i; for (i = 0; i < list_length; i++) addresses[i] = g_inet_address_new_from_string (list[i]); addresses[i] = NULL; - authentication_data_length = strlen (authentication_data_text) / 2; - authentication_data = malloc (authentication_data_length); - for (i = 0; i < authentication_data_length; i++) + gsize authentication_data_length = strlen (authentication_data_text) / 2; + g_autofree guint8 *authentication_data = malloc (authentication_data_length); + for (gsize i = 0; i < authentication_data_length; i++) authentication_data[i] = get_nibble (authentication_data_text[i*2]) << 4 | get_nibble (authentication_data_text[i*2+1]); - authorization_names = g_strsplit (authorization_names_list, " ", -1); + g_auto(GStrv) authorization_names = g_strsplit (authorization_names_list, " ", -1); xdmcp_client_send_request (xdmcp_client, request_display_number, @@ -304,24 +290,27 @@ request_cb (const gchar *name, GHashTable *params) authentication_name, authentication_data, authentication_data_length, authorization_names, mfid); + for (gsize i = 0; addresses[i] != NULL; i++) + g_object_unref (addresses[i]); + g_free (addresses); } else if (strcmp (name, "SEND-MANAGE") == 0) { - const char *text, *display_class; guint32 session_id = xdmcp_session_id; - guint16 manage_display_number = display_number; - - text = g_hash_table_lookup (params, "SESSION-ID"); + const gchar *text = g_hash_table_lookup (params, "SESSION-ID"); if (text) session_id = atoi (text); + + guint16 manage_display_number = display_number; text = g_hash_table_lookup (params, "DISPLAY-NUMBER"); if (text) manage_display_number = atoi (text); - display_class = g_hash_table_lookup (params, "DISPLAY-CLASS"); + const gchar *display_class = g_hash_table_lookup (params, "DISPLAY-CLASS"); if (!display_class) display_class = ""; + xdmcp_client_send_manage (xdmcp_client, session_id, manage_display_number, @@ -330,13 +319,12 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "SEND-KEEP-ALIVE") == 0) { - const char *text; - guint32 session_id = xdmcp_session_id; guint16 keep_alive_display_number = display_number; - - text = g_hash_table_lookup (params, "DISPLAY-NUMBER"); + const gchar *text = g_hash_table_lookup (params, "DISPLAY-NUMBER"); if (text) keep_alive_display_number = atoi (text); + + guint32 session_id = xdmcp_session_id; text = g_hash_table_lookup (params, "SESSION-ID"); if (text) session_id = atoi (text); @@ -357,18 +345,6 @@ version_compare (int major, int minor) int main (int argc, char **argv) { - int i; - g_auto(GStrv) tokens = NULL; - g_autofree gchar *pid_string = NULL; - gboolean do_xdmcp = FALSE; - guint xdmcp_port = 0; - const gchar *xdmcp_host = NULL; - const gchar *seat = NULL; - const gchar *mir_id = NULL; - g_autofree gchar *lock_filename = NULL; - int lock_file; - g_autoptr(GString) status_text = NULL; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif @@ -385,14 +361,19 @@ main (int argc, char **argv) xorg_version = g_key_file_get_string (config, "test-xserver-config", "version", NULL); if (!xorg_version) xorg_version = g_strdup ("1.17.0"); - tokens = g_strsplit (xorg_version, ".", -1); + g_auto(GStrv) tokens = g_strsplit (xorg_version, ".", -1); xorg_version_major = g_strv_length (tokens) > 0 ? atoi (tokens[0]) : 0; xorg_version_minor = g_strv_length (tokens) > 1 ? atoi (tokens[1]) : 0; /* TCP listening default changed in 1.17.0 */ listen_tcp = version_compare (1, 17) < 0; - for (i = 1; i < argc; i++) + gboolean do_xdmcp = FALSE; + guint xdmcp_port = 0; + const gchar *xdmcp_host = NULL; + const gchar *seat = NULL; + const gchar *mir_id = NULL; + for (int i = 1; i < argc; i++) { char *arg = argv[i]; @@ -523,7 +504,7 @@ main (int argc, char **argv) g_signal_connect (xserver, X_SERVER_SIGNAL_CLIENT_DISCONNECTED, G_CALLBACK (client_disconnected_cb), NULL); g_signal_connect (xserver, X_SERVER_SIGNAL_RESET, G_CALLBACK (reset_cb), NULL); - status_text = g_string_new (""); + g_autoptr(GString) status_text = g_string_new (""); g_string_printf (status_text, "%s START", id); if (config_file) g_string_append_printf (status_text, " CONFIG=%s", config_file); @@ -548,9 +529,9 @@ main (int argc, char **argv) return return_value; } - lock_filename = g_strdup_printf (".X%d-lock", display_number); + g_autofree gchar *lock_filename = g_strdup_printf (".X%d-lock", display_number); lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL); - lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); + int lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); if (lock_file < 0) { g_autofree gchar *lock_contents = NULL; @@ -593,7 +574,7 @@ main (int argc, char **argv) g_clear_pointer (&lock_path, g_free); return EXIT_FAILURE; } - pid_string = g_strdup_printf ("%10ld", (long) getpid ()); + g_autofree gchar *pid_string = g_strdup_printf ("%10ld", (long) getpid ()); if (write (lock_file, pid_string, strlen (pid_string)) < 0) { g_warning ("Error writing PID file: %s", strerror (errno)); diff --git a/tests/src/Xmir.c b/tests/src/Xmir.c index 6e8a0f7b..5b207b48 100644 --- a/tests/src/Xmir.c +++ b/tests/src/Xmir.c @@ -113,14 +113,6 @@ request_cb (const gchar *name, GHashTable *params) int main (int argc, char **argv) { - int i; - g_autofree gchar *pid_string = NULL; - gchar *seat = NULL; - gchar *mir_id = NULL; - g_autofree gchar *lock_filename = NULL; - int lock_file; - g_autoptr(GString) status_text = NULL; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif @@ -131,7 +123,9 @@ main (int argc, char **argv) g_unix_signal_add (SIGTERM, sigterm_cb, NULL); g_unix_signal_add (SIGHUP, sighup_cb, NULL); - for (i = 1; i < argc; i++) + gchar *seat = NULL; + gchar *mir_id = NULL; + for (int i = 1; i < argc; i++) { char *arg = argv[i]; @@ -209,7 +203,7 @@ main (int argc, char **argv) g_signal_connect (xserver, X_SERVER_SIGNAL_CLIENT_CONNECTED, G_CALLBACK (client_connected_cb), NULL); g_signal_connect (xserver, X_SERVER_SIGNAL_CLIENT_DISCONNECTED, G_CALLBACK (client_disconnected_cb), NULL); - status_text = g_string_new (""); + g_autoptr(GString) status_text = g_string_new (""); g_string_printf (status_text, "%s START", id); if (vt_number >= 0) g_string_append_printf (status_text, " VT=%d", vt_number); @@ -229,32 +223,24 @@ main (int argc, char **argv) return return_value; } - lock_filename = g_strdup_printf (".X%d-lock", display_number); + g_autofree gchar *lock_filename = g_strdup_printf (".X%d-lock", display_number); lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL); - lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); + int lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); if (lock_file < 0) { g_autofree gchar *lock_contents = NULL; - if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL)) { - g_autofree gchar *proc_filename = NULL; - pid_t pid; + pid_t pid = atol (lock_contents); - pid = atol (lock_contents); - - proc_filename = g_strdup_printf ("/proc/%d", pid); + g_autofree gchar *proc_filename = g_strdup_printf ("/proc/%d", pid); if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS)) { - g_autofree gchar *socket_dir = NULL; - g_autofree gchar *socket_filename = NULL; - g_autofree gchar *socket_path = NULL; - - socket_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", ".X11-unix", NULL); + g_autofree gchar *socket_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", ".X11-unix", NULL); g_mkdir_with_parents (socket_dir, 0755); - socket_filename = g_strdup_printf ("X%d", display_number); - socket_path = g_build_filename (socket_dir, socket_filename, NULL); + g_autofree gchar *socket_filename = g_strdup_printf ("X%d", display_number); + g_autofree gchar *socket_path = g_build_filename (socket_dir, socket_filename, NULL); g_printerr ("Breaking lock on non-existant process %d\n", pid); unlink (lock_path); @@ -274,7 +260,7 @@ main (int argc, char **argv) g_clear_pointer (&lock_path, g_free); return EXIT_FAILURE; } - pid_string = g_strdup_printf ("%10ld", (long) getpid ()); + g_autofree gchar *pid_string = g_strdup_printf ("%10ld", (long) getpid ()); if (write (lock_file, pid_string, strlen (pid_string)) < 0) { g_warning ("Error writing PID file: %s", strerror (errno)); diff --git a/tests/src/Xvnc.c b/tests/src/Xvnc.c index 9459c224..c63de81a 100644 --- a/tests/src/Xvnc.c +++ b/tests/src/Xvnc.c @@ -89,10 +89,8 @@ vnc_data_cb (GIOChannel *channel, GIOCondition condition, gpointer data) { gchar buffer[1024]; gsize n_read; - GIOStatus status; g_autoptr(GError) error = NULL; - - status = g_io_channel_read_chars (channel, buffer, 1023, &n_read, &error); + GIOStatus status = g_io_channel_read_chars (channel, buffer, 1023, &n_read, &error); if (error) g_warning ("Error reading from VNC client: %s", error->message); @@ -139,15 +137,6 @@ request_cb (const gchar *name, GHashTable *params) int main (int argc, char **argv) { - int i; - g_autofree gchar *pid_string = NULL; - gboolean use_inetd = FALSE; - gboolean has_option = FALSE; - const gchar *geometry = "640x480"; - gint depth = 8; - g_autofree gchar *lock_filename = NULL; - int lock_file; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif @@ -158,7 +147,11 @@ main (int argc, char **argv) g_unix_signal_add (SIGTERM, sigterm_cb, NULL); g_unix_signal_add (SIGHUP, sighup_cb, NULL); - for (i = 1; i < argc; i++) + gboolean use_inetd = FALSE; + gboolean has_option = FALSE; + const gchar *geometry = "640x480"; + gint depth = 8; + for (int i = 1; i < argc; i++) { char *arg = argv[i]; @@ -236,32 +229,24 @@ main (int argc, char **argv) return EXIT_FAILURE; } - lock_filename = g_strdup_printf (".X%d-lock", display_number); + g_autofree gchar *lock_filename = g_strdup_printf (".X%d-lock", display_number); lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL); - lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); + int lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444); if (lock_file < 0) { g_autofree gchar *lock_contents = NULL; - if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL)) { - g_autofree gchar *proc_filename = NULL; - pid_t pid; - - pid = atol (lock_contents); + pid_t pid = atol (lock_contents); - proc_filename = g_strdup_printf ("/proc/%d", pid); + g_autofree gchar *proc_filename = g_strdup_printf ("/proc/%d", pid); if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS)) { - g_autofree gchar *socket_dir = NULL; - g_autofree gchar *socket_filename = NULL; - g_autofree gchar *socket_path = NULL; - - socket_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", ".X11-unix", NULL); + g_autofree gchar *socket_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", ".X11-unix", NULL); g_mkdir_with_parents (socket_dir, 0755); - socket_filename = g_strdup_printf ("X%d", display_number); - socket_path = g_build_filename (socket_dir, socket_filename, NULL); + g_autofree gchar *socket_filename = g_strdup_printf ("X%d", display_number); + g_autofree gchar *socket_path = g_build_filename (socket_dir, socket_filename, NULL); g_printerr ("Breaking lock on non-existant process %d\n", pid); unlink (lock_path); @@ -281,7 +266,7 @@ main (int argc, char **argv) g_clear_pointer (&lock_path, g_free); return EXIT_FAILURE; } - pid_string = g_strdup_printf ("%10ld", (long) getpid ()); + g_autofree gchar *pid_string = g_strdup_printf ("%10ld", (long) getpid ()); if (write (lock_file, pid_string, strlen (pid_string)) < 0) { g_warning ("Error writing PID file: %s", strerror (errno)); diff --git a/tests/src/dbus-env.c b/tests/src/dbus-env.c index a988b68b..1d864aaf 100644 --- a/tests/src/dbus-env.c +++ b/tests/src/dbus-env.c @@ -10,29 +10,30 @@ static gchar * create_bus (const gchar *config_file, GPid *pid) { int name_pipe[2]; - gchar *command, address[1024]; - gchar **argv; - ssize_t n_read; - g_autoptr(GError) error = NULL; - if (pipe (name_pipe) < 0) { g_warning ("Error creating pipe: %s", strerror (errno)); exit (EXIT_FAILURE); } - command = g_strdup_printf ("dbus-daemon --config-file=%s --print-address=%d", config_file, name_pipe[1]); + + g_autofree gchar *command = g_strdup_printf ("dbus-daemon --config-file=%s --print-address=%d", config_file, name_pipe[1]); + + gchar **argv; + g_autoptr(GError) error = NULL; if (!g_shell_parse_argv (command, NULL, &argv, &error)) { g_warning ("Error parsing command line: %s", error->message); exit (EXIT_FAILURE); } + if (!g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, pid, &error)) { g_warning ("Error launching D-Bus: %s", error->message); exit (EXIT_FAILURE); } - n_read = read (name_pipe[0], address, 1023); + gchar address[1024]; + ssize_t n_read = read (name_pipe[0], address, 1023); if (n_read < 0) { g_warning ("Error reading D-Bus address: %s", strerror (errno)); @@ -49,27 +50,23 @@ create_bus (const gchar *config_file, GPid *pid) int main (int argc, char **argv) { - g_autofree gchar *system_conf_file = NULL; - g_autofree gchar *session_conf_file = NULL; - g_autofree gchar *system_bus_address = NULL; - g_autofree gchar *session_bus_address = NULL; - GPid system_bus_pid, session_bus_pid, child_pid; - int status; - - system_conf_file = g_build_filename (DATADIR, "system.conf", NULL); - system_bus_address = create_bus (system_conf_file, &system_bus_pid); + g_autofree gchar *system_conf_file = g_build_filename (DATADIR, "system.conf", NULL); + GPid system_bus_pid; + g_autofree gchar *system_bus_address = create_bus (system_conf_file, &system_bus_pid); g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", system_bus_address, TRUE); - session_conf_file = g_build_filename (DATADIR, "session.conf", NULL); - session_bus_address = create_bus (session_conf_file, &session_bus_pid); + g_autofree gchar *session_conf_file = g_build_filename (DATADIR, "session.conf", NULL); + GPid session_bus_pid; + g_autofree gchar *session_bus_address = create_bus (session_conf_file, &session_bus_pid); g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_address, TRUE); - child_pid = fork (); + GPid child_pid = fork (); if (child_pid == 0) { execvp (argv[1], argv + 1); _exit (EXIT_FAILURE); } + int status; waitpid (child_pid, &status, 0); kill (session_bus_pid, SIGTERM); diff --git a/tests/src/guest-account.c b/tests/src/guest-account.c index 0df3f092..5d3baf11 100644 --- a/tests/src/guest-account.c +++ b/tests/src/guest-account.c @@ -12,8 +12,6 @@ static GKeyFile *config; int main (int argc, char **argv) { - gchar *passwd_path; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif @@ -23,27 +21,25 @@ main (int argc, char **argv) config = g_key_file_new (); g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL); - passwd_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); + g_autofree gchar *passwd_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); if (argc == 2 && strcmp (argv[1], "add") == 0) { - gchar *home_dir, *username, line[1024]; - gint max_uid = 1000; - FILE *passwd; - /* Create a unique name */ - home_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "home", "guest-XXXXXX", NULL); + g_autofree gchar *home_dir = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "home", "guest-XXXXXX", NULL); if (!mkdtemp (home_dir)) { g_printerr ("Failed to create home directory %s: %s\n", home_dir, strerror (errno)); return EXIT_FAILURE; } - username = strrchr (home_dir, '/') + 1; + const gchar *username = strrchr (home_dir, '/') + 1; /* Get the largest UID */ - passwd = fopen (passwd_path, "r"); + gint max_uid = 1000; + FILE *passwd = fopen (passwd_path, "r"); if (passwd) { + gchar line[1024]; while (fgets (line, 1024, passwd)) { g_auto(GStrv) tokens = g_strsplit (line, ":", -1); @@ -71,20 +67,18 @@ main (int argc, char **argv) } else if (argc == 3 && strcmp (argv[1], "remove") == 0) { - gchar *username, *path, *prefix, line[1024]; - FILE *passwd, *new_passwd; - - username = argv[2]; + const gchar *username = argv[2]; status_notify ("GUEST-ACCOUNT REMOVE USERNAME=%s", username); /* Open a new file for writing */ - passwd = fopen (passwd_path, "r"); - path = g_strdup_printf ("%s~", passwd_path); - new_passwd = fopen (path, "w"); + FILE *passwd = fopen (passwd_path, "r"); + g_autofree gchar *path = g_strdup_printf ("%s~", passwd_path); + FILE *new_passwd = fopen (path, "w"); /* Copy the old file, omitting our entry */ - prefix = g_strdup_printf ("%s:", username); + g_autofree gchar *prefix = g_strdup_printf ("%s:", username); + gchar line[1024]; while (fgets (line, 1024, passwd)) { if (!g_str_has_prefix (line, prefix)) diff --git a/tests/src/initctl.c b/tests/src/initctl.c index f7732c0b..da377968 100644 --- a/tests/src/initctl.c +++ b/tests/src/initctl.c @@ -16,11 +16,8 @@ main (int argc, char **argv) if (g_key_file_get_boolean (config, "test-initctl-config", "report-events", NULL)) { - g_autoptr(GString) status_text = NULL; - int i; - - status_text = g_string_new ("INIT"); - for (i = 1; i < argc; i++) + g_autoptr(GString) status_text = g_string_new ("INIT"); + for (int i = 1; i < argc; i++) g_string_append_printf (status_text, " %s", argv[i]); status_notify ("%s", status_text->str); } diff --git a/tests/src/libsystem.c b/tests/src/libsystem.c index 62fe6480..b47b4523 100644 --- a/tests/src/libsystem.c +++ b/tests/src/libsystem.c @@ -106,27 +106,21 @@ initgroups (const char *user, gid_t group) int getgroups (int size, gid_t list[]) { - const gchar *group_list; - g_auto(GStrv) groups = NULL; - gint groups_length; - /* Get groups we are a member of */ - group_list = g_getenv ("LIGHTDM_TEST_GROUPS"); + const gchar *group_list = g_getenv ("LIGHTDM_TEST_GROUPS"); if (!group_list) group_list = ""; - groups = g_strsplit (group_list, ",", -1); - groups_length = g_strv_length (groups); + g_auto(GStrv) groups = g_strsplit (group_list, ",", -1); + gint groups_length = g_strv_length (groups); if (size != 0) { - int i; - if (groups_length > size) { errno = EINVAL; return -1; } - for (i = 0; groups[i]; i++) + for (int i = 0; groups[i]; i++) list[i] = atoi (groups[i]); } @@ -136,11 +130,8 @@ getgroups (int size, gid_t list[]) int setgroups (size_t size, const gid_t *list) { - size_t i; - g_autoptr(GString) group_list = NULL; - - group_list = g_string_new (""); - for (i = 0; i < size; i++) + g_autoptr(GString) group_list = g_string_new (""); + for (size_t i = 0; i < size; i++) { if (i != 0) g_string_append (group_list, ","); @@ -226,11 +217,7 @@ redirect_path (const gchar *path) static int open_wrapper (const char *func, const char *pathname, int flags, mode_t mode) { - int (*_open) (const char *pathname, int flags, mode_t mode); - g_autofree gchar *new_path = NULL; - int fd; - - _open = (int (*)(const char *pathname, int flags, mode_t mode)) dlsym (RTLD_NEXT, func); + int (*_open) (const char *pathname, int flags, mode_t mode) = dlsym (RTLD_NEXT, func); if (strcmp (pathname, "/dev/tty0") == 0) { @@ -242,10 +229,8 @@ open_wrapper (const char *func, const char *pathname, int flags, mode_t mode) return tty_fd; } - new_path = redirect_path (pathname); - fd = _open (new_path, flags, mode); - - return fd; + g_autofree gchar *new_path = redirect_path (pathname); + return _open (new_path, flags, mode); } int @@ -279,173 +264,122 @@ open64 (const char *pathname, int flags, ...) FILE * fopen (const char *path, const char *mode) { - FILE *(*_fopen) (const char *pathname, const char *mode); - g_autofree gchar *new_path = NULL; - FILE *result; - - _fopen = (FILE *(*)(const char *pathname, const char *mode)) dlsym (RTLD_NEXT, "fopen"); + FILE *(*_fopen) (const char *pathname, const char *mode) = dlsym (RTLD_NEXT, "fopen"); - new_path = redirect_path (path); - result = _fopen (new_path, mode); - - return result; + g_autofree gchar *new_path = redirect_path (path); + return _fopen (new_path, mode); } int unlinkat (int dirfd, const char *pathname, int flags) { - int (*_unlinkat) (int dirfd, const char *pathname, int flags); - g_autofree gchar *new_path = NULL; - int result; - - _unlinkat = (int (*)(int dirfd, const char *pathname, int flags)) dlsym (RTLD_NEXT, "unlinkat"); + int (*_unlinkat) (int dirfd, const char *pathname, int flags) = dlsym (RTLD_NEXT, "unlinkat"); - new_path = redirect_path (pathname); - result = _unlinkat (dirfd, new_path, flags); - - return result; + g_autofree gchar *new_path = redirect_path (pathname); + return _unlinkat (dirfd, new_path, flags); } int creat (const char *pathname, mode_t mode) { - int (*_creat) (const char *pathname, mode_t mode); - g_autofree gchar *new_path = NULL; - int result; - - _creat = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "creat"); + int (*_creat) (const char *pathname, mode_t mode) = dlsym (RTLD_NEXT, "creat"); - new_path = redirect_path (pathname); - result = _creat (new_path, mode); - - return result; + g_autofree gchar *new_path = redirect_path (pathname); + return _creat (new_path, mode); } int creat64 (const char *pathname, mode_t mode) { - int (*_creat64) (const char *pathname, mode_t mode); - g_autofree gchar *new_path = NULL; - int result; - - _creat64 = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "creat64"); + int (*_creat64) (const char *pathname, mode_t mode) = dlsym (RTLD_NEXT, "creat64"); - new_path = redirect_path (pathname); - result = _creat64 (new_path, mode); - - return result; + g_autofree gchar *new_path = redirect_path (pathname); + return _creat64 (new_path, mode); } int access (const char *pathname, int mode) { - int (*_access) (const char *pathname, int mode); - g_autofree gchar *new_path = NULL; - - _access = (int (*)(const char *pathname, int mode)) dlsym (RTLD_NEXT, "access"); + int (*_access) (const char *pathname, int mode) = dlsym (RTLD_NEXT, "access"); if (strcmp (pathname, "/dev/tty0") == 0) return F_OK; if (strcmp (pathname, "/sys/class/tty/tty0/active") == 0) return F_OK; - new_path = redirect_path (pathname); + g_autofree gchar *new_path = redirect_path (pathname); return _access (new_path, mode); } int stat (const char *path, struct stat *buf) { - int (*_stat) (const char *path, struct stat *buf); - g_autofree gchar *new_path = NULL; + int (*_stat) (const char *path, struct stat *buf) = dlsym (RTLD_NEXT, "stat"); - _stat = (int (*)(const char *path, struct stat *buf)) dlsym (RTLD_NEXT, "stat"); - - new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); return _stat (new_path, buf); } int stat64 (const char *path, struct stat64 *buf) { - int (*_stat64) (const char *path, struct stat64 *buf); - g_autofree gchar *new_path = NULL; - - _stat64 = (int (*)(const char *path, struct stat64 *buf)) dlsym (RTLD_NEXT, "stat64"); + int (*_stat64) (const char *path, struct stat64 *buf) = dlsym (RTLD_NEXT, "stat64"); - new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); return _stat64 (new_path, buf); } int __xstat (int version, const char *path, struct stat *buf) { - int (*___xstat) (int version, const char *path, struct stat *buf); - g_autofree gchar *new_path = NULL; + int (*___xstat) (int version, const char *path, struct stat *buf) = dlsym (RTLD_NEXT, "__xstat"); - ___xstat = (int (*)(int version, const char *path, struct stat *buf)) dlsym (RTLD_NEXT, "__xstat"); - - new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); return ___xstat (version, new_path, buf); } int __xstat64 (int version, const char *path, struct stat64 *buf) { - int (*___xstat64) (int version, const char *path, struct stat64 *buf); - g_autofree gchar *new_path = NULL; - - ___xstat64 = (int (*)(int version, const char *path, struct stat64 *buf)) dlsym (RTLD_NEXT, "__xstat64"); + int (*___xstat64) (int version, const char *path, struct stat64 *buf) = dlsym (RTLD_NEXT, "__xstat64"); - new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); return ___xstat64 (version, new_path, buf); } int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *buf, int flags) { - int (*___fxstatat) (int ver, int dirfd, const char *pathname, struct stat *buf, int flags); - g_autofree gchar *new_path = NULL; + int (*___fxstatat) (int ver, int dirfd, const char *pathname, struct stat *buf, int flags) = dlsym (RTLD_NEXT, "__fxstatat"); - ___fxstatat = (int (*)(int ver, int dirfd, const char *pathname, struct stat *buf, int flags)) dlsym (RTLD_NEXT, "__fxstatat"); - - new_path = redirect_path (pathname); + g_autofree gchar *new_path = redirect_path (pathname); return ___fxstatat (ver, dirfd, new_path, buf, flags); } int __fxstatat64(int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags) { - int (*___fxstatat64) (int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags); - g_autofree gchar *new_path = NULL; - - ___fxstatat64 = (int (*)(int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags)) dlsym (RTLD_NEXT, "__fxstatat64"); + int (*___fxstatat64) (int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags) = dlsym (RTLD_NEXT, "__fxstatat64"); - new_path = redirect_path (pathname); + g_autofree gchar *new_path = redirect_path (pathname); return ___fxstatat64 (ver, dirfd, new_path, buf, flags); } DIR * opendir (const char *name) { - DIR *(*_opendir) (const char *name); - g_autofree gchar *new_path = NULL; + DIR *(*_opendir) (const char *name) = dlsym (RTLD_NEXT, "opendir"); - _opendir = (DIR *(*)(const char *name)) dlsym (RTLD_NEXT, "opendir"); - - new_path = redirect_path (name); + g_autofree gchar *new_path = redirect_path (name); return _opendir (new_path); } int mkdir (const char *pathname, mode_t mode) { - int (*_mkdir) (const char *pathname, mode_t mode); - g_autofree gchar *new_path = NULL; - - _mkdir = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "mkdir"); + int (*_mkdir) (const char *pathname, mode_t mode) = dlsym (RTLD_NEXT, "mkdir"); - new_path = redirect_path (pathname); + g_autofree gchar *new_path = redirect_path (pathname); return _mkdir (new_path, mode); } @@ -459,38 +393,31 @@ chown (const char *pathname, uid_t owner, gid_t group) int chmod (const char *path, mode_t mode) { - int (*_chmod) (const char *path, mode_t mode); - g_autofree gchar *new_path = NULL; + int (*_chmod) (const char *path, mode_t mode) = dlsym (RTLD_NEXT, "chmod"); - _chmod = (int (*)(const char *path, mode_t mode)) dlsym (RTLD_NEXT, "chmod"); - - new_path = redirect_path (path); + g_autofree gchar *new_path = redirect_path (path); return _chmod (new_path, mode); } int ioctl (int d, unsigned long request, ...) { - int (*_ioctl) (int d, int request, ...); + int (*_ioctl) (int d, int request, ...) = dlsym (RTLD_NEXT, "ioctl"); - _ioctl = (int (*)(int d, int request, ...)) dlsym (RTLD_NEXT, "ioctl"); if (d > 0 && d == tty_fd) { - struct vt_stat *vt_state; - int vt; va_list ap; - switch (request) { case VT_GETSTATE: va_start (ap, request); - vt_state = va_arg (ap, struct vt_stat *); + struct vt_stat *vt_state = va_arg (ap, struct vt_stat *); va_end (ap); vt_state->v_active = active_vt; break; case VT_ACTIVATE: va_start (ap, request); - vt = va_arg (ap, int); + int vt = va_arg (ap, int); va_end (ap); if (vt != active_vt) { @@ -507,10 +434,9 @@ ioctl (int d, unsigned long request, ...) else { va_list ap; - void *data; va_start (ap, request); - data = va_arg (ap, void *); + void *data = va_arg (ap, void *); va_end (ap); return _ioctl (d, request, data); } @@ -519,54 +445,39 @@ ioctl (int d, unsigned long request, ...) static void add_port_redirect (int requested_port, int redirected_port) { - g_autoptr(GKeyFile) file = NULL; - g_autofree gchar *path = NULL; - g_autofree gchar *name = NULL; - g_autofree gchar *data = NULL; - - file = g_key_file_new (); - path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".port-redirects", NULL); + g_autoptr(GKeyFile) file = g_key_file_new (); + g_autofree gchar *path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".port-redirects", NULL); g_key_file_load_from_file (file, path, G_KEY_FILE_NONE, NULL); - name = g_strdup_printf ("%d", requested_port); + g_autofree gchar *name = g_strdup_printf ("%d", requested_port); g_key_file_set_integer (file, name, "redirected", redirected_port); - data = g_key_file_to_data (file, NULL, NULL); + g_autofree gchar *data = g_key_file_to_data (file, NULL, NULL); g_file_set_contents (path, data, -1, NULL); } static int find_port_redirect (int port) { - g_autoptr(GKeyFile) file = NULL; - g_autofree gchar *path = NULL; - g_autofree gchar *name = NULL; - int redirected_port; - - file = g_key_file_new (); - path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".port-redirects", NULL); + g_autoptr(GKeyFile) file = g_key_file_new (); + g_autofree gchar *path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".port-redirects", NULL); g_key_file_load_from_file (file, path, G_KEY_FILE_NONE, NULL); - name = g_strdup_printf ("%d", port); - redirected_port = g_key_file_get_integer (file, name, "redirected", NULL); - - return redirected_port; + g_autofree gchar *name = g_strdup_printf ("%d", port); + return g_key_file_get_integer (file, name, "redirected", NULL); } int bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen) { - int port = 0, redirected_port = 0; - const char *path; - int (*_bind) (int sockfd, const struct sockaddr *addr, socklen_t addrlen); + int (*_bind) (int sockfd, const struct sockaddr *addr, socklen_t addrlen) = dlsym (RTLD_NEXT, "bind"); + const struct sockaddr *modified_addr = addr; struct sockaddr_in temp_addr_in; struct sockaddr_in6 temp_addr_in6; struct sockaddr_un temp_addr_un; - int retval; - - _bind = (int (*)(int sockfd, const struct sockaddr *addr, socklen_t addrlen)) dlsym (RTLD_NEXT, "bind"); - + int port = 0, redirected_port = 0; + const char *path; switch (addr->sa_family) { case AF_UNIX: @@ -601,7 +512,7 @@ bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen) break; } - retval = _bind (sockfd, modified_addr, addrlen); + int retval = _bind (sockfd, modified_addr, addrlen); socklen_t temp_addr_len; switch (addr->sa_family) @@ -634,16 +545,14 @@ bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen) int connect (int sockfd, const struct sockaddr *addr, socklen_t addrlen) { - int port, redirected_port; - const char *path; + int (*_connect) (int sockfd, const struct sockaddr *addr, socklen_t addrlen) = dlsym (RTLD_NEXT, "connect"); + const struct sockaddr *modified_addr = addr; struct sockaddr_in temp_addr_in; struct sockaddr_in6 temp_addr_in6; struct sockaddr_un temp_addr_un; - int (*_connect) (int sockfd, const struct sockaddr *addr, socklen_t addrlen); - - _connect = (int (*)(int sockfd, const struct sockaddr *addr, socklen_t addrlen)) dlsym (RTLD_NEXT, "connect"); - + int port = 0, redirected_port = 0; + const char *path; switch (addr->sa_family) { case AF_UNIX: @@ -684,16 +593,14 @@ connect (int sockfd, const struct sockaddr *addr, socklen_t addrlen) ssize_t sendto (int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { + ssize_t (*_sendto) (int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) = dlsym (RTLD_NEXT, "sendto"); + int port, redirected_port; const char *path; const struct sockaddr *modified_addr = dest_addr; struct sockaddr_in temp_addr_in; struct sockaddr_in6 temp_addr_in6; struct sockaddr_un temp_addr_un; - ssize_t (*_sendto) (int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); - - _sendto = (ssize_t (*)(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)) dlsym (RTLD_NEXT, "sendto"); - switch (dest_addr->sa_family) { case AF_UNIX: @@ -734,12 +641,10 @@ sendto (int sockfd, const void *buf, size_t len, int flags, const struct sockadd int close (int fd) { - int (*_close) (int fd); - if (fd > 0 && fd == tty_fd) return 0; - _close = (int (*)(int fd)) dlsym (RTLD_NEXT, "close"); + int (*_close) (int fd) = dlsym (RTLD_NEXT, "close"); return _close (fd); } #endif @@ -760,32 +665,25 @@ free_user (gpointer data) static void load_passwd_file (void) { - g_autofree gchar *path = NULL; - g_autofree gchar *data = NULL; - g_auto(GStrv) lines = NULL; - gint i; - g_autoptr(GError) error = NULL; - g_list_free_full (user_entries, free_user); user_entries = NULL; getpwent_link = NULL; - path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); + g_autofree gchar *path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); + g_autofree gchar *data = NULL; + g_autoptr(GError) error = NULL; if (!g_file_get_contents (path, &data, NULL, &error)) { g_warning ("Error loading passwd file: %s", error->message); return; } - lines = g_strsplit (data, "\n", -1); + g_auto(GStrv) lines = g_strsplit (data, "\n", -1); - for (i = 0; lines[i]; i++) + for (gint i = 0; lines[i]; i++) { - const gchar *line; - g_auto(GStrv) fields = NULL; - - line = g_strstrip (lines[i]); - fields = g_strsplit (line, ":", -1); + const gchar *line = g_strstrip (lines[i]); + g_auto(GStrv) fields = g_strsplit (line, ":", -1); if (g_strv_length (fields) == 7) { struct passwd *entry = malloc (sizeof (struct passwd)); @@ -837,39 +735,31 @@ endpwent (void) struct passwd * getpwnam (const char *name) { - GList *link; - load_passwd_file (); - for (link = user_entries; link; link = link->next) + for (GList *link = user_entries; link; link = link->next) { struct passwd *entry = link->data; if (strcmp (entry->pw_name, name) == 0) - break; + return entry; } - if (!link) - return NULL; - return link->data; + return NULL; } struct passwd * getpwuid (uid_t uid) { - GList *link; - load_passwd_file (); - for (link = user_entries; link; link = link->next) + for (GList *link = user_entries; link; link = link->next) { struct passwd *entry = link->data; if (entry->pw_uid == uid) - break; + return entry; } - if (!link) - return NULL; - return link->data; + return NULL; } static void @@ -886,31 +776,24 @@ free_group (gpointer data) static void load_group_file (void) { - g_autofree gchar *path = NULL; - g_autofree gchar *data = NULL; - g_auto(GStrv) lines = NULL; - gint i; - g_autoptr(GError) error = NULL; - g_list_free_full (group_entries, free_group); group_entries = NULL; - path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "group", NULL); + g_autofree gchar *path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "group", NULL); + g_autofree gchar *data = NULL; + g_autoptr(GError) error = NULL; if (!g_file_get_contents (path, &data, NULL, &error)) { g_warning ("Error loading group file: %s", error->message); return; } - lines = g_strsplit (data, "\n", -1); + g_auto(GStrv) lines = g_strsplit (data, "\n", -1); - for (i = 0; lines[i]; i++) + for (gint i = 0; lines[i]; i++) { - const gchar *line; - g_auto(GStrv) fields = NULL; - - line = g_strstrip (lines[i]); - fields = g_strsplit (line, ":", -1); + const gchar *line = g_strstrip (lines[i]); + g_auto(GStrv) fields = g_strsplit (line, ":", -1); if (g_strv_length (fields) == 4) { struct group *entry = malloc (sizeof (struct group)); @@ -927,47 +810,37 @@ load_group_file (void) struct group * getgrnam (const char *name) { - GList *link; - load_group_file (); - for (link = group_entries; link; link = link->next) + for (GList *link = group_entries; link; link = link->next) { struct group *entry = link->data; if (strcmp (entry->gr_name, name) == 0) - break; + return entry; } - if (!link) - return NULL; - return link->data; + return NULL; } struct group * getgrgid (gid_t gid) { - GList *link; - load_group_file (); - for (link = group_entries; link; link = link->next) + for (GList *link = group_entries; link; link = link->next) { struct group *entry = link->data; if (entry->gr_gid == gid) - break; + return entry; } - if (!link) - return NULL; - return link->data; + return NULL; } int pam_start (const char *service_name, const char *user, const struct pam_conv *conversation, pam_handle_t **pamh) { - pam_handle_t *handle; - - handle = *pamh = malloc (sizeof (pam_handle_t)); + pam_handle_t *handle = *pamh = malloc (sizeof (pam_handle_t)); if (handle == NULL) return PAM_BUF_ERR; @@ -979,9 +852,7 @@ pam_start (const char *service_name, const char *user, const struct pam_conv *co connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s START", handle->id); g_string_append_printf (status, " SERVICE=%s", service_name); if (user) @@ -1005,15 +876,10 @@ pam_start (const char *service_name, const char *user, const struct pam_conv *co int pam_authenticate (pam_handle_t *pamh, int flags) { - struct passwd *entry; - gboolean password_matches = FALSE; - connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s AUTHENTICATE", pamh->id); if (flags & PAM_SILENT) g_string_append (status, " SILENT"); @@ -1023,17 +889,15 @@ pam_authenticate (pam_handle_t *pamh, int flags) status_notify ("%s", status->str); } + gboolean password_matches = FALSE; if (strcmp (pamh->service_name, "test-remote") == 0) { - int result; - struct pam_message **msg; - struct pam_response *resp = NULL; - - msg = malloc (sizeof (struct pam_message *) * 1); + struct pam_message **msg = malloc (sizeof (struct pam_message *) * 1); msg[0] = malloc (sizeof (struct pam_message)); msg[0]->msg_style = PAM_PROMPT_ECHO_ON; msg[0]->msg = "remote-login:"; - result = pamh->conversation.conv (1, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); + struct pam_response *resp = NULL; + int result = pamh->conversation.conv (1, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); free (msg[0]); free (msg); if (result != PAM_SUCCESS) @@ -1088,15 +952,12 @@ pam_authenticate (pam_handle_t *pamh, int flags) /* Prompt for username */ if (pamh->user == NULL) { - int result; - struct pam_message **msg; - struct pam_response *resp = NULL; - - msg = malloc (sizeof (struct pam_message *) * 1); + struct pam_message **msg = malloc (sizeof (struct pam_message *) * 1); msg[0] = malloc (sizeof (struct pam_message)); msg[0]->msg_style = PAM_PROMPT_ECHO_ON; msg[0]->msg = LOGIN_PROMPT; - result = pamh->conversation.conv (1, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); + struct pam_response *resp = NULL; + int result = pamh->conversation.conv (1, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); free (msg[0]); free (msg); if (result != PAM_SUCCESS) @@ -1120,18 +981,15 @@ pam_authenticate (pam_handle_t *pamh, int flags) kill (getpid (), SIGSEGV); /* Look up password database */ - entry = getpwnam (pamh->user); + struct passwd *entry = getpwnam (pamh->user); /* Prompt for password if required */ if (entry && strcmp (pamh->user, "always-password") != 0 && (strcmp (pamh->service_name, "lightdm-autologin") == 0 || strcmp (entry->pw_passwd, "") == 0)) password_matches = TRUE; else { - int i, n_messages = 0, password_index, result; - struct pam_message **msg; - struct pam_response *resp = NULL; - - msg = malloc (sizeof (struct pam_message *) * 5); + struct pam_message **msg = malloc (sizeof (struct pam_message *) * 5); + int n_messages = 0; if (strcmp (pamh->user, "info-prompt") == 0) { msg[n_messages] = malloc (sizeof (struct pam_message)); @@ -1164,10 +1022,11 @@ pam_authenticate (pam_handle_t *pamh, int flags) msg[n_messages] = malloc (sizeof (struct pam_message)); msg[n_messages]->msg_style = PAM_PROMPT_ECHO_OFF; msg[n_messages]->msg = "Password:"; - password_index = n_messages; + int password_index = n_messages; n_messages++; - result = pamh->conversation.conv (n_messages, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); - for (i = 0; i < n_messages; i++) + struct pam_response *resp = NULL; + int result = pamh->conversation.conv (n_messages, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); + for (int i = 0; i < n_messages; i++) free (msg[i]); free (msg); if (result != PAM_SUCCESS) @@ -1187,7 +1046,7 @@ pam_authenticate (pam_handle_t *pamh, int flags) if (password_matches && strcmp (pamh->user, "multi-prompt") == 0) password_matches = strcmp ("blue", resp[0].resp) == 0; - for (i = 0; i < n_messages; i++) + for (int i = 0; i < n_messages; i++) { if (resp[i].resp) free (resp[i].resp); @@ -1249,7 +1108,6 @@ static const char * get_env_value (const char *name_value, const char *name) { int j; - for (j = 0; name[j] && name_value[j] && name[j] == name_value[j]; j++); if (name[j] == '\0' && name_value[j] == '=') return &name_value[j + 1]; @@ -1260,19 +1118,16 @@ get_env_value (const char *name_value, const char *name) int pam_putenv (pam_handle_t *pamh, const char *name_value) { - int i; - gchar *name; - - name = strdup (name_value); - for (i = 0; name[i]; i++) + g_autofree char *name = strdup (name_value); + for (int i = 0; name[i]; i++) if (name[i] == '=') name[i] = '\0'; + int i; for (i = 0; pamh->envlist[i]; i++) { if (get_env_value (pamh->envlist[i], name)) break; } - free (name); if (pamh->envlist[i]) { @@ -1292,12 +1147,9 @@ pam_putenv (pam_handle_t *pamh, const char *name_value) const char * pam_getenv (pam_handle_t *pamh, const char *name) { - int i; - - for (i = 0; pamh->envlist[i]; i++) + for (int i = 0; pamh->envlist[i]; i++) { - const char *value; - value = get_env_value (pamh->envlist[i], name); + const char *value = get_env_value (pamh->envlist[i], name); if (value) return value; } @@ -1374,15 +1226,10 @@ pam_get_item (const pam_handle_t *pamh, int item_type, const void **item) int pam_open_session (pam_handle_t *pamh, int flags) { - g_autoptr(GVariant) result = NULL; - g_autoptr(GError) error = NULL; - connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s OPEN-SESSION", pamh->id); if (flags & PAM_SILENT) g_string_append (status, " SILENT"); @@ -1395,30 +1242,28 @@ pam_open_session (pam_handle_t *pamh, int flags) if (strcmp (pamh->user, "make-home-dir") == 0) { - struct passwd *entry; - entry = getpwnam (pamh->user); + struct passwd *entry = getpwnam (pamh->user); g_mkdir_with_parents (entry->pw_dir, 0755); } /* Open logind session */ - result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), - "org.freedesktop.login1", - "/org/freedesktop/login1", - "org.freedesktop.login1.Manager", - "CreateSession", - g_variant_new ("()", ""), - G_VARIANT_TYPE ("(so)"), - G_DBUS_CALL_FLAGS_NONE, - G_MAXINT, - NULL, - &error); + g_autoptr(GError) error = NULL; + g_autoptr(GVariant) result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "CreateSession", + g_variant_new ("()", ""), + G_VARIANT_TYPE ("(so)"), + G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, + NULL, + &error); if (result) { - g_autofree gchar *e = NULL; const gchar *id; - g_variant_get (result, "(&so)", &id, NULL); - e = g_strdup_printf ("XDG_SESSION_ID=%s", id); + g_autofree gchar *e = g_strdup_printf ("XDG_SESSION_ID=%s", id); pam_putenv (pamh, e); } else @@ -1433,9 +1278,7 @@ pam_close_session (pam_handle_t *pamh, int flags) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s CLOSE-SESSION", pamh->id); if (flags & PAM_SILENT) g_string_append (status, " SILENT"); @@ -1452,9 +1295,7 @@ pam_acct_mgmt (pam_handle_t *pamh, int flags) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s ACCT-MGMT", pamh->id); if (flags & PAM_SILENT) g_string_append (status, " SILENT"); @@ -1480,17 +1321,10 @@ pam_acct_mgmt (pam_handle_t *pamh, int flags) int pam_chauthtok (pam_handle_t *pamh, int flags) { - struct passwd *entry; - int result; - struct pam_message **msg; - struct pam_response *resp = NULL; - connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s CHAUTHTOK", pamh->id); if (flags & PAM_SILENT) g_string_append (status, " SILENT"); @@ -1500,14 +1334,15 @@ pam_chauthtok (pam_handle_t *pamh, int flags) status_notify ("%s", status->str); } - msg = malloc (sizeof (struct pam_message *) * 1); + struct pam_message **msg = malloc (sizeof (struct pam_message *) * 1); msg[0] = malloc (sizeof (struct pam_message)); msg[0]->msg_style = PAM_PROMPT_ECHO_OFF; if ((flags & PAM_CHANGE_EXPIRED_AUTHTOK) != 0) msg[0]->msg = "Enter new password (expired):"; else msg[0]->msg = "Enter new password:"; - result = pamh->conversation.conv (1, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); + struct pam_response *resp = NULL; + int result = pamh->conversation.conv (1, (const struct pam_message **) msg, &resp, pamh->conversation.appdata_ptr); free (msg[0]); free (msg); if (result != PAM_SUCCESS) @@ -1522,7 +1357,7 @@ pam_chauthtok (pam_handle_t *pamh, int flags) } /* Update password database */ - entry = getpwnam (pamh->user); + struct passwd *entry = getpwnam (pamh->user); free (entry->pw_passwd); entry->pw_passwd = resp[0].resp; free (resp); @@ -1533,14 +1368,10 @@ pam_chauthtok (pam_handle_t *pamh, int flags) int pam_setcred (pam_handle_t *pamh, int flags) { - g_autofree gchar *e = NULL; - connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s SETCRED", pamh->id); if (flags & PAM_SILENT) g_string_append (status, " SILENT"); @@ -1557,7 +1388,7 @@ pam_setcred (pam_handle_t *pamh, int flags) } /* Put the test directories into the path */ - e = g_strdup_printf ("PATH=%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, pam_getenv (pamh, "PATH")); + g_autofree gchar *e = g_strdup_printf ("PATH=%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, pam_getenv (pamh, "PATH")); pam_putenv (pamh, e); if (strcmp (pamh->user, "cred-error") == 0) @@ -1570,17 +1401,13 @@ pam_setcred (pam_handle_t *pamh, int flags) /* Join special groups if requested */ if (strcmp (pamh->user, "group-member") == 0 && flags & PAM_ESTABLISH_CRED) { - struct group *group; - gid_t *groups; - int groups_length; - - group = getgrnam ("test-group"); + struct group *group = getgrnam ("test-group"); if (group) { - groups_length = getgroups (0, NULL); + int groups_length = getgroups (0, NULL); if (groups_length < 0) return PAM_SYSTEM_ERR; - groups = malloc (sizeof (gid_t) * (groups_length + 1)); + gid_t *groups = malloc (sizeof (gid_t) * (groups_length + 1)); groups_length = getgroups (groups_length, groups); if (groups_length < 0) return PAM_SYSTEM_ERR; @@ -1603,9 +1430,7 @@ pam_end (pam_handle_t *pamh, int pam_status) connect_status (); if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new (""); + g_autoptr(GString) status = g_string_new (""); g_string_append_printf (status, "%s END", pamh->id); status_notify ("%s", status->str); } @@ -1713,9 +1538,7 @@ pututxline (const struct utmpx *ut) connect_status (); if (g_key_file_get_boolean (config, "test-utmp-config", "check-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new ("UTMP"); + g_autoptr(GString) status = g_string_new ("UTMP"); switch (ut->ut_type) { case INIT_PROCESS: @@ -1758,9 +1581,7 @@ updwtmp (const char *wtmp_file, const struct utmp *ut) connect_status (); if (g_key_file_get_boolean (config, "test-utmp-config", "check-events", NULL)) { - g_autoptr(GString) status = NULL; - - status = g_string_new ("WTMP"); + g_autoptr(GString) status = g_string_new ("WTMP"); g_string_append_printf (status, " FILE=%s", wtmp_file); switch (ut->ut_type) { @@ -1801,9 +1622,7 @@ struct xcb_connection_t xcb_connection_t * xcb_connect_to_display_with_auth_info (const char *display, xcb_auth_info_t *auth, int *screen) { - xcb_connection_t *c; - - c = malloc (sizeof (xcb_connection_t)); + xcb_connection_t *c = malloc (sizeof (xcb_connection_t)); c->display = g_strdup (display); c->error = 0; @@ -1815,7 +1634,6 @@ xcb_connect_to_display_with_auth_info (const char *display, xcb_auth_info_t *aut if (c->error == 0) { g_autoptr(GError) error = NULL; - c->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (c->socket == NULL) { @@ -1826,15 +1644,11 @@ xcb_connect_to_display_with_auth_info (const char *display, xcb_auth_info_t *aut if (c->error == 0) { - g_autofree gchar *d = NULL; - g_autofree gchar *socket_path = NULL; - g_autoptr(GSocketAddress) address = NULL; - g_autoptr(GError) error = NULL; - /* Skip the hostname, we'll assume it's localhost */ - d = g_strdup_printf (".x%s", strchr (display, ':')); - socket_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), d, NULL); - address = g_unix_socket_address_new (socket_path); + g_autofree gchar *d = g_strdup_printf (".x%s", strchr (display, ':')); + g_autofree gchar *socket_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), d, NULL); + g_autoptr(GSocketAddress) address = g_unix_socket_address_new (socket_path); + g_autoptr(GError) error = NULL; if (!g_socket_connect (c->socket, address, NULL, &error)) { g_printerr ("Failed to connect to X socket %s: %s\n", socket_path, error->message); @@ -1890,12 +1704,11 @@ audit_log_acct_message (int audit_fd, int type, const char *pgname, const char *op, const char *name, unsigned int id, const char *host, const char *addr, const char *tty, int result) { - g_autofree gchar *type_string = NULL; - connect_status (); if (!g_key_file_get_boolean (config, "test-audit-config", "check-events", NULL)) return 1; + g_autofree gchar *type_string = NULL; switch (type) { case AUDIT_USER_LOGIN: diff --git a/tests/src/status.c b/tests/src/status.c index 02d66e40..bc399603 100644 --- a/tests/src/status.c +++ b/tests/src/status.c @@ -17,17 +17,9 @@ static gboolean status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) { int length; - gchar buffer[1024]; - ssize_t n_read; - const gchar *c, *start; - int l; - g_autofree gchar *id = NULL; - g_autofree gchar *name = NULL; - gboolean id_matches; - g_autoptr(GHashTable) params = NULL; g_autoptr(GError) error = NULL; - - n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error); + ssize_t n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error); + gchar buffer[1024]; if (n_read > 0) n_read = g_socket_receive (socket, buffer, length, NULL, &error); if (error) @@ -48,17 +40,16 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) return TRUE; buffer[n_read] = '\0'; - c = buffer; - start = c; - l = 0; + const gchar *c = buffer; + const gchar *start = c; + int l = 0; while (*c && !isspace (*c)) { c++; l++; } - id = g_strdup_printf ("%.*s", l, start); - id_matches = g_strcmp0 (id, filter_id) == 0; - if (!id_matches) + g_autofree gchar *id = g_strdup_printf ("%.*s", l, start); + if (g_strcmp0 (id, filter_id) != 0) return TRUE; while (isspace (*c)) @@ -70,24 +61,21 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) c++; l++; } - name = g_strdup_printf ("%.*s", l, start); + g_autofree gchar *name = g_strdup_printf ("%.*s", l, start); - params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + g_autoptr(GHashTable) params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); while (TRUE) { - const gchar *start; - g_autofree gchar *param_name = NULL; - g_autofree gchar *param_value = NULL; - while (isspace (*c)) c++; - start = c; + const gchar *start = c; while (*c && !isspace (*c) && *c != '=') c++; if (*c == '\0') break; - param_name = g_strdup_printf ("%.*s", (int) (c - start), start); + g_autofree gchar *param_name = g_strdup_printf ("%.*s", (int) (c - start), start); + g_autofree gchar *param_value = NULL; if (*c == '=') { @@ -96,11 +84,9 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) c++; if (*c == '\"') { - gboolean escaped = FALSE; - g_autoptr(GString) value = NULL; - c++; - value = g_string_new (""); + g_autoptr(GString) value = g_string_new (""); + gboolean escaped = FALSE; while (*c) { if (*c == '\\') @@ -145,14 +131,10 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data) gboolean status_connect (StatusRequestFunc request_cb, const gchar *id) { - g_autofree gchar *path = NULL; - g_autoptr(GSocketAddress) address = NULL; - GSource *source; - g_autoptr(GError) error = NULL; - request_func = request_cb; filter_id = g_strdup (id); + g_autoptr(GError) error = NULL; status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (!status_socket) { @@ -160,8 +142,8 @@ status_connect (StatusRequestFunc request_cb, const gchar *id) return FALSE; } - path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".s", NULL); - address = g_unix_socket_address_new (path); + g_autofree gchar *path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".s", NULL); + g_autoptr(GSocketAddress) address = g_unix_socket_address_new (path); if (!g_socket_connect (status_socket, address, NULL, &error)) { g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message); @@ -169,7 +151,7 @@ status_connect (StatusRequestFunc request_cb, const gchar *id) return FALSE; } - source = g_socket_create_source (status_socket, G_IO_IN, NULL); + GSource *source = g_socket_create_source (status_socket, G_IO_IN, NULL); g_source_set_callback (source, (GSourceFunc) status_request_cb, NULL, NULL); g_source_attach (source, NULL); @@ -181,17 +163,14 @@ status_notify (const gchar *format, ...) { gchar status[1024]; va_list ap; - va_start (ap, format); vsnprintf (status, 1024, format, ap); va_end (ap); if (status_socket) { + int length = strlen (status); g_autoptr(GError) error = NULL; - int length; - - length = strlen (status); if (g_socket_send (status_socket, (gchar *) &length, sizeof (length), NULL, &error) < 0 || g_socket_send (status_socket, status, strlen (status), NULL, &error) < 0) g_printerr ("Failed to write to status socket: %s\n", error->message); diff --git a/tests/src/test-gobject-greeter.c b/tests/src/test-gobject-greeter.c index 455a1321..43b8b96f 100644 --- a/tests/src/test-gobject-greeter.c +++ b/tests/src/test-gobject-greeter.c @@ -124,20 +124,18 @@ static void write_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data) { LightDMGreeter *greeter = LIGHTDM_GREETER (object); - g_autofree gchar *dir = NULL; - g_autofree gchar *path = NULL; g_autofree gchar *test_data = data; - FILE *f; - g_autoptr(GError) error = NULL; - dir = lightdm_greeter_ensure_shared_data_dir_finish (greeter, result, &error); + g_autoptr(GError) error = NULL; + g_autofree gchar *dir = lightdm_greeter_ensure_shared_data_dir_finish (greeter, result, &error); if (!dir) { status_notify ("%s WRITE-SHARED-DATA ERROR=%s", greeter_id, error->message); return; } - path = g_build_filename (dir, "data", NULL); + g_autofree gchar *path = g_build_filename (dir, "data", NULL); + FILE *f; if (!(f = fopen (path, "w")) || fprintf (f, "%s", test_data) < 0) status_notify ("%s WRITE-SHARED-DATA ERROR=%s", greeter_id, strerror (errno)); else @@ -151,19 +149,17 @@ static void read_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data) { LightDMGreeter *greeter = LIGHTDM_GREETER (object); - g_autofree gchar *dir = NULL; - g_autofree gchar *path = NULL; - g_autofree gchar *contents = NULL; - g_autoptr(GError) error = NULL; - dir = lightdm_greeter_ensure_shared_data_dir_finish (greeter, result, &error); + g_autoptr(GError) error = NULL; + g_autofree gchar *dir = lightdm_greeter_ensure_shared_data_dir_finish (greeter, result, &error); if (!dir) { status_notify ("%s READ-SHARED-DATA ERROR=%s", greeter_id, error->message); return; } - path = g_build_filename (dir, "data", NULL); + g_autofree gchar *path = g_build_filename (dir, "data", NULL); + g_autofree gchar *contents = NULL; if (g_file_get_contents (path, &contents, NULL, &error)) status_notify ("%s READ-SHARED-DATA DATA=%s", greeter_id, contents); else @@ -244,11 +240,8 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "WATCH-USER") == 0) { - LightDMUser *user; - const gchar *username; - - username = g_hash_table_lookup (params, "USERNAME"); - user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username); + const gchar *username = g_hash_table_lookup (params, "USERNAME"); + LightDMUser *user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username); if (user) g_signal_connect (user, LIGHTDM_SIGNAL_USER_CHANGED, G_CALLBACK (user_changed_cb), NULL); status_notify ("%s WATCH-USER USERNAME=%s", greeter_id, username); @@ -256,15 +249,8 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "LOG-USER") == 0) { - LightDMUser *user; - const gchar *username, *image, *background, *language, *layout, *session; - const gchar * const * layouts; + const gchar *username = g_hash_table_lookup (params, "USERNAME"); g_auto(GStrv) fields = NULL; - g_autofree gchar *layouts_text = NULL; - g_autoptr(GString) status_text = NULL; - int i; - - username = g_hash_table_lookup (params, "USERNAME"); if (g_hash_table_lookup (params, "FIELDS")) fields = g_strsplit (g_hash_table_lookup (params, "FIELDS"), ",", -1); if (!fields) @@ -273,18 +259,18 @@ request_cb (const gchar *name, GHashTable *params) fields[0] = NULL; } - user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username); - image = lightdm_user_get_image (user); - background = lightdm_user_get_background (user); - language = lightdm_user_get_language (user); - layout = lightdm_user_get_layout (user); - layouts = lightdm_user_get_layouts (user); - layouts_text = g_strjoinv (";", (gchar **) layouts); - session = lightdm_user_get_session (user); + LightDMUser *user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username); + const gchar *image = lightdm_user_get_image (user); + const gchar *background = lightdm_user_get_background (user); + const gchar *language = lightdm_user_get_language (user); + const gchar *layout = lightdm_user_get_layout (user); + const gchar * const * layouts = lightdm_user_get_layouts (user); + g_autofree gchar *layouts_text = g_strjoinv (";", (gchar **) layouts); + const gchar *session = lightdm_user_get_session (user); - status_text = g_string_new (""); + g_autoptr(GString) status_text = g_string_new (""); g_string_append_printf (status_text, "%s LOG-USER USERNAME=%s", greeter_id, username); - for (i = 0; fields[i]; i++) + for (int i = 0; fields[i]; i++) { if (strcmp (fields[i], "REAL-NAME") == 0) g_string_append_printf (status_text, " REAL-NAME=%s", lightdm_user_get_real_name (user)); @@ -315,10 +301,8 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "LOG-USER-LIST") == 0) { - GList *users, *link; - - users = lightdm_user_list_get_users (lightdm_user_list_get_instance ()); - for (link = users; link; link = link->next) + GList *users = lightdm_user_list_get_users (lightdm_user_list_get_instance ()); + for (GList *link = users; link; link = link->next) { LightDMUser *user = link->data; status_notify ("%s LOG-USER USERNAME=%s", greeter_id, lightdm_user_get_name (user)); @@ -327,10 +311,8 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "LOG-SESSIONS") == 0) { - GList *sessions, *link; - - sessions = lightdm_get_sessions (); - for (link = sessions; link; link = link->next) + GList *sessions = lightdm_get_sessions (); + for (GList *link = sessions; link; link = link->next) { LightDMSession *session = link->data; status_notify ("%s LOG-SESSION KEY=%s", greeter_id, lightdm_session_get_key (session)); @@ -424,23 +406,19 @@ connect_finished (GObject *object, GAsyncResult *result, gpointer data) int main (int argc, char **argv) { - const gchar *display, *xdg_seat, *xdg_vtnr, *xdg_session_cookie, *xdg_session_class, *xdg_session_type, *mir_server_host_socket, *mir_vt, *mir_id; - g_autofree gchar *path = NULL; - g_autoptr(GString) status_text = NULL; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif - display = getenv ("DISPLAY"); - xdg_seat = getenv ("XDG_SEAT"); - xdg_vtnr = getenv ("XDG_VTNR"); - xdg_session_cookie = getenv ("XDG_SESSION_COOKIE"); - xdg_session_class = getenv ("XDG_SESSION_CLASS"); - xdg_session_type = getenv ("XDG_SESSION_TYPE"); - mir_server_host_socket = getenv ("MIR_SERVER_HOST_SOCKET"); - mir_vt = getenv ("MIR_SERVER_VT"); - mir_id = getenv ("MIR_SERVER_NAME"); + const gchar *display = getenv ("DISPLAY"); + const gchar *xdg_seat = getenv ("XDG_SEAT"); + const gchar *xdg_vtnr = getenv ("XDG_VTNR"); + const gchar *xdg_session_cookie = getenv ("XDG_SESSION_COOKIE"); + const gchar *xdg_session_class = getenv ("XDG_SESSION_CLASS"); + const gchar *xdg_session_type = getenv ("XDG_SESSION_TYPE"); + const gchar *mir_server_host_socket = getenv ("MIR_SERVER_HOST_SOCKET"); + const gchar *mir_vt = getenv ("MIR_SERVER_VT"); + const gchar *mir_id = getenv ("MIR_SERVER_NAME"); if (display) { if (display[0] == ':') @@ -464,7 +442,7 @@ main (int argc, char **argv) status_connect (request_cb, greeter_id); - status_text = g_string_new (""); + g_autoptr(GString) status_text = g_string_new (""); g_string_printf (status_text, "%s START", greeter_id); if (xdg_seat) g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat); @@ -479,10 +457,10 @@ main (int argc, char **argv) status_notify ("%s", status_text->str); config = g_key_file_new (); - path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL); + g_autofree gchar *path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL); g_key_file_load_from_file (config, path, G_KEY_FILE_NONE, NULL); - if (g_key_file_get_boolean (config, "test-greeter-config", "exit-on-startup", NULL)) + if (g_key_file_get_boolean (config, "test-greeter-config", "exit-on-startup", NULL)) { int return_value = g_key_file_get_integer (config, "test-greeter-config", "return-value", NULL); status_notify ("%s EXIT CODE=%d", greeter_id, return_value); diff --git a/tests/src/test-greeter-wrapper.c b/tests/src/test-greeter-wrapper.c index dae274fa..3907fb65 100644 --- a/tests/src/test-greeter-wrapper.c +++ b/tests/src/test-greeter-wrapper.c @@ -6,11 +6,9 @@ int main (int argc, char **argv) { - gchar *display; - status_connect (NULL, NULL); - display = getenv ("DISPLAY"); + const gchar *display = getenv ("DISPLAY"); if (display == NULL) status_notify ("GREETER-WRAPPER-? START"); else if (display[0] == ':') diff --git a/tests/src/test-guest-wrapper.c b/tests/src/test-guest-wrapper.c index 614acd6e..2bfe76c9 100644 --- a/tests/src/test-guest-wrapper.c +++ b/tests/src/test-guest-wrapper.c @@ -6,11 +6,9 @@ int main (int argc, char **argv) { - gchar *display; - status_connect (NULL, NULL); - display = getenv ("DISPLAY"); + const gchar *display = getenv ("DISPLAY"); if (display == NULL) status_notify ("GUEST-WRAPPER-? START"); else if (display[0] == ':') diff --git a/tests/src/test-runner.c b/tests/src/test-runner.c index 0062a15e..290cc8b0 100644 --- a/tests/src/test-runner.c +++ b/tests/src/test-runner.c @@ -141,9 +141,6 @@ stop_process (Process *process) static void process_exit_cb (GPid pid, gint status, gpointer data) { - Process *process; - gchar *status_text; - if (getenv ("DEBUG")) { if (WIFEXITED (status)) @@ -152,10 +149,12 @@ process_exit_cb (GPid pid, gint status, gpointer data) g_print ("Process %d terminated with signal %d\n", pid, WTERMSIG (status)); } + Process *process; if (lightdm_process && pid == lightdm_process->pid) { process = lightdm_process; lightdm_process = NULL; + g_autofree gchar *status_text; if (WIFEXITED (status)) status_text = g_strdup_printf ("RUNNER DAEMON-EXIT STATUS=%d", WEXITSTATUS (status)); else @@ -182,9 +181,7 @@ process_exit_cb (GPid pid, gint status, gpointer data) static Process * watch_process (pid_t pid) { - Process *process; - - process = g_malloc0 (sizeof (Process)); + Process *process = g_malloc0 (sizeof (Process)); process->pid = pid; process->kill_timeout = 0; @@ -198,18 +195,16 @@ watch_process (pid_t pid) static void quit (int status) { - GHashTableIter iter; - if (!stop) exit_status = status; stop = TRUE; /* Stop all the children */ + GHashTableIter iter; g_hash_table_iter_init (&iter, children); while (TRUE) { gpointer key, value; - if (!g_hash_table_iter_next (&iter, &key, &value)) break; @@ -232,7 +227,7 @@ quit (int status) if (temp_dir && getenv ("DEBUG") == NULL) { - gchar *command = g_strdup_printf ("rm -rf %s", temp_dir); + g_autofree gchar *command = g_strdup_printf ("rm -rf %s", temp_dir); if (system (command)) perror ("Failed to delete temp directory"); } @@ -243,14 +238,12 @@ quit (int status) static void fail (const gchar *event, const gchar *expected) { - GList *link; - if (stop) return; g_printerr ("Command line: %s", test_runner_command); g_printerr ("Events:\n"); - for (link = statuses; link; link = link->next) + for (GList *link = statuses; link; link = link->next) g_printerr (" %s\n", (gchar *)link->data); if (event) g_printerr (" %s\n", event); @@ -265,35 +258,26 @@ fail (const gchar *event, const gchar *expected) static gchar * get_prefix (const gchar *text) { - gchar *prefix; + g_autofree gchar *prefix = g_strdup (text); gint i; - - prefix = g_strdup (text); for (i = 0; prefix[i] != '\0' && prefix[i] != ' '; i++); prefix[i] = '\0'; - return prefix; + return g_steal_pointer (&prefix); } static ScriptLine * get_script_line (const gchar *prefix) { - GList *link; - - for (link = script; link; link = link->next) + for (GList *link = script; link; link = link->next) { ScriptLine *line = link->data; /* Ignore lines with other prefixes */ if (prefix) { - g_autofree gchar *p = NULL; - gboolean matches; - - p = get_prefix (line->text); - matches = strcmp (prefix, p) == 0; - - if (!matches) + g_autofree gchar *p = get_prefix (line->text); + if (strcmp (prefix, p) != 0) continue; } @@ -314,10 +298,8 @@ stop_loop (gpointer user_data) static void switch_to_greeter_done_cb (GObject *bus, GAsyncResult *result, gpointer data) { - g_autoptr(GVariant) r = NULL; g_autoptr(GError) error = NULL; - - r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); + g_autoptr(GVariant) r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); if (r) check_status ("RUNNER SWITCH-TO-GREETER"); else @@ -330,12 +312,11 @@ switch_to_greeter_done_cb (GObject *bus, GAsyncResult *result, gpointer data) static void switch_to_user_done_cb (GObject *bus, GAsyncResult *result, gpointer data) { - g_autoptr(GVariant) r = NULL; g_autofree gchar *username = data; - g_autofree gchar *status_text = NULL; - g_autoptr(GError) error = NULL; - r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); + g_autoptr(GError) error = NULL; + g_autoptr(GVariant) r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); + g_autofree gchar *status_text = NULL; if (r) status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s", username); else @@ -349,10 +330,8 @@ switch_to_user_done_cb (GObject *bus, GAsyncResult *result, gpointer data) static void switch_to_guest_done_cb (GObject *bus, GAsyncResult *result, gpointer data) { - g_autoptr(GVariant) r = NULL; g_autoptr(GError) error = NULL; - - r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); + g_autoptr(GVariant) r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error); if (r) check_status ("RUNNER SWITCH-TO-GUEST"); else @@ -365,31 +344,24 @@ switch_to_guest_done_cb (GObject *bus, GAsyncResult *result, gpointer data) static void handle_command (const gchar *command) { - const gchar *c; - g_autofree gchar *name = NULL; - g_autoptr(GHashTable) params = NULL; - - c = command; + const gchar *c = command; while (*c && !isspace (*c)) c++; - name = g_strdup_printf ("%.*s", (int) (c - command), command); + g_autofree gchar *name = g_strdup_printf ("%.*s", (int) (c - command), command); - params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + g_autoptr(GHashTable) params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); while (TRUE) { - const gchar *start; - gchar *param_name, *param_value; - while (isspace (*c)) c++; - start = c; + const gchar *start = c; while (*c && !isspace (*c) && *c != '=') c++; if (*c == '\0') break; - param_name = g_strdup_printf ("%.*s", (int) (c - start), start); - + gchar *param_name = g_strdup_printf ("%.*s", (int) (c - start), start); + gchar *param_value; if (*c == '=') { c++; @@ -440,12 +412,7 @@ handle_command (const gchar *command) if (strcmp (name, "START-DAEMON") == 0) { - GString *command_line; - gchar **lightdm_argv; - pid_t lightdm_pid; - g_autoptr(GError) error = NULL; - - command_line = g_string_new ("lightdm"); + GString *command_line = g_string_new ("lightdm"); if (getenv ("DEBUG")) g_string_append (command_line, " --debug"); g_string_append_printf (command_line, " --cache-dir %s/cache", temp_dir); @@ -454,12 +421,15 @@ handle_command (const gchar *command) g_getenv ("PATH"), g_getenv ("LD_PRELOAD"), g_getenv ("LD_LIBRARY_PATH"), g_getenv ("LIGHTDM_TEST_ROOT"), g_getenv ("DBUS_SESSION_BUS_ADDRESS"), command_line->str); + gchar **lightdm_argv; + g_autoptr(GError) error = NULL; if (!g_shell_parse_argv (command_line->str, NULL, &lightdm_argv, &error)) { g_warning ("Error parsing command line: %s", error->message); quit (EXIT_FAILURE); } + pid_t lightdm_pid; if (!g_spawn_async (NULL, lightdm_argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &lightdm_pid, &error)) { g_warning ("Error launching LightDM: %s", error->message); @@ -471,19 +441,15 @@ handle_command (const gchar *command) } else if (strcmp (name, "WAIT") == 0) { - g_autoptr(GMainLoop) loop = NULL; - const gchar *v; - int duration; - /* Stop status timeout */ if (status_timeout) g_source_remove (status_timeout); status_timeout = 0; /* Use a main loop so that our DBus functions are still responsive */ - loop = g_main_loop_new (NULL, FALSE); - v = g_hash_table_lookup (params, "DURATION"); - duration = v ? atoi (v) : 1; + g_autoptr(GMainLoop) loop = g_main_loop_new (NULL, FALSE); + const gchar *v = g_hash_table_lookup (params, "DURATION"); + int duration = v ? atoi (v) : 1; if (duration < 1) duration = 1; g_timeout_add_seconds (duration, stop_loop, loop); @@ -494,12 +460,9 @@ handle_command (const gchar *command) } else if (strcmp (name, "ADD-SEAT") == 0) { - const gchar *id, *v; - Login1Seat *seat; - - id = g_hash_table_lookup (params, "ID"); - seat = add_login1_seat (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), id, TRUE); - v = g_hash_table_lookup (params, "CAN-GRAPHICAL"); + const gchar *id = g_hash_table_lookup (params, "ID"); + Login1Seat *seat = add_login1_seat (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), id, TRUE); + const gchar *v = g_hash_table_lookup (params, "CAN-GRAPHICAL"); if (v) seat->can_graphical = strcmp (v, "TRUE") == 0; v = g_hash_table_lookup (params, "CAN-MULTI-SESSION"); @@ -508,38 +471,29 @@ handle_command (const gchar *command) } else if (strcmp (name, "ADD-LOCAL-X-SEAT") == 0) { - g_autoptr(GVariant) result = NULL; - const gchar *v; - - v = g_hash_table_lookup (params, "DISPLAY"); - result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), - "org.freedesktop.DisplayManager", - "/org/freedesktop/DisplayManager", - "org.freedesktop.DisplayManager", - "AddLocalXSeat", - g_variant_new ("(i)", v ? atoi (v) : -1), - G_VARIANT_TYPE ("(o)"), - G_DBUS_CALL_FLAGS_NONE, - G_MAXINT, - NULL, - NULL); + const gchar *v = g_hash_table_lookup (params, "DISPLAY"); + g_autoptr(GVariant) result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), + "org.freedesktop.DisplayManager", + "/org/freedesktop/DisplayManager", + "org.freedesktop.DisplayManager", + "AddLocalXSeat", + g_variant_new ("(i)", v ? atoi (v) : -1), + G_VARIANT_TYPE ("(o)"), + G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, + NULL, + NULL); } else if (strcmp (name, "UPDATE-SEAT") == 0) { - Login1Seat *seat; - const gchar *id; - - id = g_hash_table_lookup (params, "ID"); - seat = find_login1_seat (id); + const gchar *id = g_hash_table_lookup (params, "ID"); + Login1Seat *seat = find_login1_seat (id); if (seat) { - const gchar *v; GVariantBuilder invalidated_properties; - g_autoptr(GError) error = NULL; - g_variant_builder_init (&invalidated_properties, G_VARIANT_TYPE_ARRAY); - v = g_hash_table_lookup (params, "CAN-GRAPHICAL"); + const gchar *v = g_hash_table_lookup (params, "CAN-GRAPHICAL"); if (v) { seat->can_graphical = strcmp (v, "TRUE") == 0; @@ -559,6 +513,7 @@ handle_command (const gchar *command) g_variant_builder_add (&invalidated_properties, "s", "ActiveSession"); } + g_autoptr(GError) error = NULL; if (!g_dbus_connection_emit_signal (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), NULL, seat->path, @@ -571,39 +526,37 @@ handle_command (const gchar *command) } else if (strcmp (name, "REMOVE-SEAT") == 0) { - const gchar *id; - id = g_hash_table_lookup (params, "ID"); + const gchar *id = g_hash_table_lookup (params, "ID"); remove_login1_seat (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), id); } else if (strcmp (name, "LIST-SEATS") == 0) { - g_autoptr(GVariant) result = NULL; - g_autoptr(GString) status = NULL; g_autoptr(GError) error = NULL; - - result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), - "org.freedesktop.DisplayManager", - "/org/freedesktop/DisplayManager", - "org.freedesktop.DBus.Properties", - "Get", - g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Seats"), - G_VARIANT_TYPE ("(v)"), - G_DBUS_CALL_FLAGS_NONE, - G_MAXINT, - NULL, - &error); - - status = g_string_new ("RUNNER LIST-SEATS"); + g_autoptr(GVariant) result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), + "org.freedesktop.DisplayManager", + "/org/freedesktop/DisplayManager", + "org.freedesktop.DBus.Properties", + "Get", + g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Seats"), + G_VARIANT_TYPE ("(v)"), + G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, + NULL, + &error); + + g_autoptr(GString) status = g_string_new ("RUNNER LIST-SEATS"); if (result) { + g_string_append (status, " SEATS="); + g_autoptr(GVariant) value = NULL; + g_variant_get (result, "(v)", &value); + GVariantIter *iter; + g_variant_get (value, "ao", &iter); + const gchar *path; int i = 0; - - g_string_append (status, " SEATS="); - g_variant_get (result, "(v)", &value); - g_variant_get (value, "ao", &iter); while (g_variant_iter_loop (iter, "&o", &path)) { if (i != 0) @@ -624,34 +577,32 @@ handle_command (const gchar *command) } else if (strcmp (name, "LIST-SESSIONS") == 0) { - g_autoptr(GVariant) result = NULL; - g_autoptr(GString) status = NULL; g_autoptr(GError) error = NULL; - - result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), - "org.freedesktop.DisplayManager", - "/org/freedesktop/DisplayManager", - "org.freedesktop.DBus.Properties", - "Get", - g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Sessions"), - G_VARIANT_TYPE ("(v)"), - G_DBUS_CALL_FLAGS_NONE, - G_MAXINT, - NULL, - &error); - - status = g_string_new ("RUNNER LIST-SESSIONS"); + g_autoptr(GVariant) result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), + "org.freedesktop.DisplayManager", + "/org/freedesktop/DisplayManager", + "org.freedesktop.DBus.Properties", + "Get", + g_variant_new ("(ss)", "org.freedesktop.DisplayManager", "Sessions"), + G_VARIANT_TYPE ("(v)"), + G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, + NULL, + &error); + + g_autoptr(GString) status = g_string_new ("RUNNER LIST-SESSIONS"); if (result) { - g_autoptr(GVariant) value = NULL; - GVariantIter *iter; - const gchar *path; - int i = 0; - g_string_append (status, " SESSIONS="); + g_autoptr(GVariant) value = NULL; g_variant_get (result, "(v)", &value); + + GVariantIter *iter; g_variant_get (value, "ao", &iter); + + const gchar *path; + int i = 0; while (g_variant_iter_loop (iter, "&o", &path)) { if (i != 0) @@ -672,23 +623,20 @@ handle_command (const gchar *command) } else if (strcmp (name, "SEAT-CAN-SWITCH") == 0) { - g_autoptr(GVariant) result = NULL; - g_autoptr(GString) status = NULL; g_autoptr(GError) error = NULL; - - result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), - "org.freedesktop.DisplayManager", - "/org/freedesktop/DisplayManager/Seat0", - "org.freedesktop.DBus.Properties", - "Get", - g_variant_new ("(ss)", "org.freedesktop.DisplayManager.Seat", "CanSwitch"), - G_VARIANT_TYPE ("(v)"), - G_DBUS_CALL_FLAGS_NONE, - G_MAXINT, - NULL, - &error); - - status = g_string_new ("RUNNER SEAT-CAN-SWITCH"); + g_autoptr(GVariant) result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), + "org.freedesktop.DisplayManager", + "/org/freedesktop/DisplayManager/Seat0", + "org.freedesktop.DBus.Properties", + "Get", + g_variant_new ("(ss)", "org.freedesktop.DisplayManager.Seat", "CanSwitch"), + G_VARIANT_TYPE ("(v)"), + G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, + NULL, + &error); + + g_autoptr(GString) status = g_string_new ("RUNNER SEAT-CAN-SWITCH"); if (result) { g_autoptr(GVariant) value = NULL; @@ -707,23 +655,20 @@ handle_command (const gchar *command) } else if (strcmp (name, "SEAT-HAS-GUEST-ACCOUNT") == 0) { - g_autoptr(GVariant) result = NULL; - g_autoptr(GString) status = NULL; g_autoptr(GError) error = NULL; - - result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), - "org.freedesktop.DisplayManager", - "/org/freedesktop/DisplayManager/Seat0", - "org.freedesktop.DBus.Properties", - "Get", - g_variant_new ("(ss)", "org.freedesktop.DisplayManager.Seat", "HasGuestAccount"), - G_VARIANT_TYPE ("(v)"), - G_DBUS_CALL_FLAGS_NONE, - G_MAXINT, - NULL, - &error); - - status = g_string_new ("RUNNER SEAT-HAS-GUEST-ACCOUNT"); + g_autoptr(GVariant) result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), + "org.freedesktop.DisplayManager", + "/org/freedesktop/DisplayManager/Seat0", + "org.freedesktop.DBus.Properties", + "Get", + g_variant_new ("(ss)", "org.freedesktop.DisplayManager.Seat", "HasGuestAccount"), + G_VARIANT_TYPE ("(v)"), + G_DBUS_CALL_FLAGS_NONE, + G_MAXINT, + NULL, + &error); + + g_autoptr(GString) status = g_string_new ("RUNNER SEAT-HAS-GUEST-ACCOUNT"); if (result) { g_autoptr(GVariant) value = NULL; @@ -757,9 +702,7 @@ handle_command (const gchar *command) } else if (strcmp (name, "SWITCH-TO-USER") == 0) { - const gchar *username; - - username = g_hash_table_lookup (params, "USERNAME"); + const gchar *username = g_hash_table_lookup (params, "USERNAME"); g_dbus_connection_call (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), "org.freedesktop.DisplayManager", "/org/freedesktop/DisplayManager/Seat0", @@ -793,17 +736,14 @@ handle_command (const gchar *command) // FIXME: Make generic RUN-COMMAND else if (strcmp (name, "START-XSERVER") == 0) { - gchar *xserver_args, *command_line; - gchar **argv; - GPid pid; - Process *process; - g_autoptr(GError) error = NULL; - - xserver_args = g_hash_table_lookup (params, "ARGS"); + const gchar *xserver_args = g_hash_table_lookup (params, "ARGS"); if (!xserver_args) xserver_args = ""; - command_line = g_strdup_printf ("%s/tests/src/X %s", BUILDDIR, xserver_args); + g_autofree gchar *command_line = g_strdup_printf ("%s/tests/src/X %s", BUILDDIR, xserver_args); + gchar **argv; + GPid pid; + g_autoptr(GError) error = NULL; if (!g_shell_parse_argv (command_line, NULL, &argv, &error) || !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error)) { @@ -812,23 +752,20 @@ handle_command (const gchar *command) } else { - process = watch_process (pid); + Process *process = watch_process (pid); g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process); } } else if (strcmp (name, "START-VNC-CLIENT") == 0) { - gchar *vnc_client_args, *command_line; - gchar **argv; - GPid pid; - Process *process; - g_autoptr(GError) error = NULL; - - vnc_client_args = g_hash_table_lookup (params, "ARGS"); + const gchar *vnc_client_args = g_hash_table_lookup (params, "ARGS"); if (!vnc_client_args) vnc_client_args = ""; - command_line = g_strdup_printf ("%s/tests/src/vnc-client %s", BUILDDIR, vnc_client_args); + g_autofree gchar *command_line = g_strdup_printf ("%s/tests/src/vnc-client %s", BUILDDIR, vnc_client_args); + gchar **argv; + GPid pid; + g_autoptr(GError) error = NULL; if (!g_shell_parse_argv (command_line, NULL, &argv, &error) || !g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error)) { @@ -837,37 +774,29 @@ handle_command (const gchar *command) } else { - process = watch_process (pid); + Process *process = watch_process (pid); g_hash_table_insert (children, GINT_TO_POINTER (process->pid), process); } } else if (strcmp (name, "ADD-USER") == 0) { - const gchar *username; - AccountsUser *user; - g_autofree gchar *status_text = NULL; - - username = g_hash_table_lookup (params, "USERNAME"); - user = get_accounts_user_by_name (username); + const gchar *username = g_hash_table_lookup (params, "USERNAME"); + AccountsUser *user = get_accounts_user_by_name (username); if (user) accounts_user_set_hidden (user, FALSE, TRUE); else g_warning ("Unknown user %s", username); - status_text = g_strdup_printf ("RUNNER ADD-USER USERNAME=%s", username); + g_autofree gchar *status_text = g_strdup_printf ("RUNNER ADD-USER USERNAME=%s", username); check_status (status_text); } else if (strcmp (name, "UPDATE-USER") == 0) { - gchar *username; - AccountsUser *user; - g_autoptr(GString) status_text = NULL; + g_autoptr(GString) status_text = g_string_new ("RUNNER UPDATE-USER USERNAME="); - status_text = g_string_new ("RUNNER UPDATE-USER USERNAME="); - - username = g_hash_table_lookup (params, "USERNAME"); + const gchar *username = g_hash_table_lookup (params, "USERNAME"); g_string_append (status_text, username); - user = get_accounts_user_by_name (username); + AccountsUser *user = get_accounts_user_by_name (username); if (user) { g_autoptr(GError) error = NULL; @@ -935,28 +864,20 @@ handle_command (const gchar *command) } else if (strcmp (name, "DELETE-USER") == 0) { - const gchar *username; - AccountsUser *user; - g_autofree gchar *status_text = NULL; - - username = g_hash_table_lookup (params, "USERNAME"); - user = get_accounts_user_by_name (username); + const gchar *username = g_hash_table_lookup (params, "USERNAME"); + AccountsUser *user = get_accounts_user_by_name (username); if (user) accounts_user_set_hidden (user, TRUE, TRUE); else g_warning ("Unknown user %s", username); - status_text = g_strdup_printf ("RUNNER DELETE-USER USERNAME=%s", username); + g_autofree gchar *status_text = g_strdup_printf ("RUNNER DELETE-USER USERNAME=%s", username); check_status (status_text); } else if (strcmp (name, "UNLOCK-SESSION") == 0) { - const gchar *id; - Login1Session *session; - g_autofree gchar *status_text = NULL; - - id = g_hash_table_lookup (params, "SESSION"); - session = find_login1_session (id); + const gchar *id = g_hash_table_lookup (params, "SESSION"); + Login1Session *session = find_login1_session (id); if (session) { if (!session->locked) @@ -966,7 +887,7 @@ handle_command (const gchar *command) else g_warning ("Unknown session %s", id); - status_text = g_strdup_printf ("RUNNER UNLOCK-SESSION SESSION=%s", id); + g_autofree gchar *status_text = g_strdup_printf ("RUNNER UNLOCK-SESSION SESSION=%s", id); check_status (status_text); } /* Forward to external processes */ @@ -977,14 +898,12 @@ handle_command (const gchar *command) g_str_has_prefix (name, "XVNC-") || strcmp (name, "UNITY-SYSTEM-COMPOSITOR") == 0) { - GList *link; - for (link = status_clients; link; link = link->next) + for (GList *link = status_clients; link; link = link->next) { StatusClient *client = link->data; - int length; - g_autoptr(GError) error = NULL; - length = strlen (command); + g_autoptr(GError) error = NULL; + int length = strlen (command); if (g_socket_send (client->socket, (gchar *) &length, sizeof (length), NULL, &error) < 0 || g_socket_send (client->socket, command, strlen (command), NULL, &error) < 0) g_printerr ("Failed to write to client socket: %s\n", error->message); @@ -1003,10 +922,8 @@ run_commands (void) /* Stop daemon if requested */ while (TRUE) { - ScriptLine *line; - /* Commands start with an asterisk */ - line = get_script_line (NULL); + ScriptLine *line = get_script_line (NULL); if (!line || line->text[0] != '*') break; @@ -1024,11 +941,9 @@ run_commands (void) static gboolean status_timeout_cb (gpointer data) { - ScriptLine *line; - status_timeout = 0; - line = get_script_line (NULL); + ScriptLine *line = get_script_line (NULL); fail ("(timeout)", line ? line->text : NULL); return G_SOURCE_REMOVE; @@ -1037,10 +952,6 @@ status_timeout_cb (gpointer data) static void check_status (const gchar *status) { - ScriptLine *line; - gboolean result = FALSE; - g_autofree gchar *prefix = NULL; - if (stop) return; @@ -1050,8 +961,9 @@ check_status (const gchar *status) g_print ("%s\n", status); /* Try and match against expected */ - prefix = get_prefix (status); - line = get_script_line (prefix); + g_autofree gchar *prefix = get_prefix (status); + gboolean result = FALSE; + ScriptLine *line = get_script_line (prefix); if (line) { g_autofree gchar *full_pattern = g_strdup_printf ("^%s$", line->text); @@ -1080,11 +992,9 @@ static gboolean status_message_cb (GSocket *socket, GIOCondition condition, StatusClient *client) { int length; - gchar buffer[1024]; - ssize_t n_read; g_autoptr(GError) error = NULL; - - n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error); + ssize_t n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error); + gchar buffer[1024]; if (n_read > 0) n_read = g_socket_receive (socket, buffer, length, NULL, &error); if (n_read < 0) @@ -1113,15 +1023,11 @@ status_message_cb (GSocket *socket, GIOCondition condition, StatusClient *client static gboolean status_connect_cb (gpointer data) { - GSocket *socket; g_autoptr(GError) error = NULL; - - socket = g_socket_accept (status_socket, NULL, &error); + GSocket *socket = g_socket_accept (status_socket, NULL, &error); if (socket) { - StatusClient *client; - - client = g_malloc0 (sizeof (StatusClient)); + StatusClient *client = g_malloc0 (sizeof (StatusClient)); client->socket = socket; client->source = g_socket_create_source (socket, G_IO_IN, NULL); status_clients = g_list_append (status_clients, client); @@ -1138,26 +1044,22 @@ status_connect_cb (gpointer data) static void load_script (const gchar *filename) { - int i; g_autofree gchar *data = NULL; - g_auto(GStrv) lines = NULL; - if (!g_file_get_contents (filename, &data, NULL, NULL)) { g_printerr ("Unable to load script: %s\n", filename); quit (EXIT_FAILURE); } - lines = g_strsplit (data, "\n", -1); + g_auto(GStrv) lines = g_strsplit (data, "\n", -1); /* Load lines with #? prefix as expected behaviour */ - for (i = 0; lines[i]; i++) + for (int i = 0; lines[i]; i++) { gchar *text = g_strstrip (lines[i]); if (g_str_has_prefix (text, "#?")) { - ScriptLine *line; - line = g_malloc0 (sizeof (ScriptLine)); + ScriptLine *line = g_malloc0 (sizeof (ScriptLine)); line->text = g_strdup (text + 2); line->done = FALSE; script = g_list_append (script, line); @@ -1217,19 +1119,17 @@ upower_name_acquired_cb (GDBusConnection *connection, " <method name='Hibernate'/>" " </interface>" "</node>"; - static const GDBusInterfaceVTable upower_vtable = - { - handle_upower_call, - }; - g_autoptr(GDBusNodeInfo) upower_info = NULL; g_autoptr(GError) error = NULL; - - upower_info = g_dbus_node_info_new_for_xml (upower_interface, &error); + g_autoptr(GDBusNodeInfo) upower_info = g_dbus_node_info_new_for_xml (upower_interface, &error); if (!upower_info) { g_warning ("Failed to parse D-Bus interface: %s", error->message); return; } + static const GDBusInterfaceVTable upower_vtable = + { + handle_upower_call, + }; if (g_dbus_connection_register_object (connection, "/org/freedesktop/UPower", upower_info->interfaces[0], @@ -1301,12 +1201,30 @@ handle_ck_session_call (GDBusConnection *connection, static CKSession * open_ck_session (GDBusConnection *connection, GVariant *params) { - CKSession *session; - g_autoptr(GString) cookie = NULL; + g_autoptr(GDBusNodeInfo) ck_session_info = NULL; + static const GDBusInterfaceVTable ck_session_vtable = + { + handle_ck_session_call, + }; + + CKSession *session = g_malloc0 (sizeof (CKSession)); + ck_sessions = g_list_append (ck_sessions, session); + + g_autoptr(GString) cookie = g_string_new ("ck-cookie"); GVariantIter *iter; + g_variant_get (params, "a(sv)", &iter); const gchar *name; GVariant *value; - g_autoptr(GError) error = NULL; + while (g_variant_iter_loop (iter, "(&sv)", &name, &value)) + { + if (strcmp (name, "x11-display") == 0) + { + const gchar *display; + g_variant_get (value, "&s", &display); + g_string_append_printf (cookie, "-x%s", display); + } + } + const gchar *ck_session_interface_old = "<node>" " <interface name='org.freedesktop.ConsoleKit.Session'>" @@ -1326,27 +1244,7 @@ open_ck_session (GDBusConnection *connection, GVariant *params) " <method name='Activate'/>" " </interface>" "</node>"; - g_autoptr(GDBusNodeInfo) ck_session_info = NULL; - static const GDBusInterfaceVTable ck_session_vtable = - { - handle_ck_session_call, - }; - - session = g_malloc0 (sizeof (CKSession)); - ck_sessions = g_list_append (ck_sessions, session); - - cookie = g_string_new ("ck-cookie"); - g_variant_get (params, "a(sv)", &iter); - while (g_variant_iter_loop (iter, "(&sv)", &name, &value)) - { - if (strcmp (name, "x11-display") == 0) - { - const gchar *display; - g_variant_get (value, "&s", &display); - g_string_append_printf (cookie, "-x%s", display); - } - } - + g_autoptr(GError) error = NULL; if (g_key_file_get_boolean (config, "test-runner-config", "ck-no-xdg-runtime", NULL)) ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface_old, &error); else @@ -1415,12 +1313,10 @@ handle_ck_call (GDBusConnection *connection, } else if (strcmp (method_name, "GetSessionForCookie") == 0) { - GList *link; - gchar *cookie; - + const gchar *cookie; g_variant_get (parameters, "(&s)", &cookie); - for (link = ck_sessions; link; link = link->next) + for (GList *link = ck_sessions; link; link = link->next) { CKSession *session = link->data; if (strcmp (session->cookie, cookie) == 0) @@ -1507,19 +1403,17 @@ ck_name_acquired_cb (GDBusConnection *connection, " </signal>" " </interface>" "</node>"; - static const GDBusInterfaceVTable ck_vtable = - { - handle_ck_call, - }; - g_autoptr(GDBusNodeInfo) ck_info = NULL; g_autoptr(GError) error = NULL; - - ck_info = g_dbus_node_info_new_for_xml (ck_interface, &error); + g_autoptr(GDBusNodeInfo) ck_info = g_dbus_node_info_new_for_xml (ck_interface, &error); if (!ck_info) { g_warning ("Failed to parse D-Bus interface: %s", error->message); return; } + static const GDBusInterfaceVTable ck_vtable = + { + handle_ck_call, + }; if (g_dbus_connection_register_object (connection, "/org/freedesktop/ConsoleKit/Manager", ck_info->interfaces[0], @@ -1594,9 +1488,13 @@ handle_login1_seat_get_property (GDBusConnection *connection, static Login1Seat * add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_signal) { - Login1Seat *seat; - g_autoptr(GError) error = NULL; - g_autoptr(GDBusNodeInfo) login1_seat_info = NULL; + Login1Seat *seat = g_malloc0 (sizeof (Login1Seat)); + login1_seats = g_list_append (login1_seats, seat); + seat->id = g_strdup (id); + seat->path = g_strdup_printf ("/org/freedesktop/login1/seat/%s", seat->id); + seat->can_graphical = TRUE; + seat->can_multi_session = TRUE; + seat->active_session = NULL; const gchar *login1_seat_interface = "<node>" @@ -1607,27 +1505,19 @@ add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_sig " <property name='Id' type='s' access='read'/>" " </interface>" "</node>"; - static const GDBusInterfaceVTable login1_seat_vtable = - { - handle_login1_seat_call, - handle_login1_seat_get_property, - }; - - seat = g_malloc0 (sizeof (Login1Seat)); - login1_seats = g_list_append (login1_seats, seat); - seat->id = g_strdup (id); - seat->path = g_strdup_printf ("/org/freedesktop/login1/seat/%s", seat->id); - seat->can_graphical = TRUE; - seat->can_multi_session = TRUE; - seat->active_session = NULL; - - login1_seat_info = g_dbus_node_info_new_for_xml (login1_seat_interface, &error); + g_autoptr(GError) error = NULL; + g_autoptr(GDBusNodeInfo) login1_seat_info = g_dbus_node_info_new_for_xml (login1_seat_interface, &error); if (!login1_seat_info) { g_warning ("Failed to parse login1 seat D-Bus interface: %s", error->message); return NULL; } + static const GDBusInterfaceVTable login1_seat_vtable = + { + handle_login1_seat_call, + handle_login1_seat_get_property, + }; if (g_dbus_connection_register_object (connection, seat->path, login1_seat_info->interfaces[0], @@ -1656,12 +1546,9 @@ add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_sig static Login1Seat * find_login1_seat (const gchar *id) { - Login1Seat *seat; - GList *link; - - for (link = login1_seats; link; link = link->next) + for (GList *link = login1_seats; link; link = link->next) { - seat = link->data; + Login1Seat *seat = link->data; if (strcmp (seat->id, id) == 0) return seat; } @@ -1672,13 +1559,11 @@ find_login1_seat (const gchar *id) static void remove_login1_seat (GDBusConnection *connection, const gchar *id) { - Login1Seat *seat; - g_autoptr(GError) error = NULL; - - seat = find_login1_seat (id); + Login1Seat *seat = find_login1_seat (id); if (!seat) return; + g_autoptr(GError) error = NULL; if (!g_dbus_connection_emit_signal (connection, NULL, "/org/freedesktop/login1", @@ -1712,40 +1597,36 @@ handle_login1_session_call (GDBusConnection *connection, static Login1Session * create_login1_session (GDBusConnection *connection) { - Login1Session *session; - g_autoptr(GError) error = NULL; - g_autoptr(GDBusNodeInfo) login1_session_info = NULL; + Login1Session *session = g_malloc0 (sizeof (Login1Session)); + login1_sessions = g_list_append (login1_sessions, session); + + session->id = g_strdup_printf ("c%d", login1_session_index++); + session->path = g_strdup_printf ("/org/freedesktop/login1/Session/%s", session->id); const gchar *login1_session_interface = "<node>" " <interface name='org.freedesktop.login1.Session'>" " </interface>" "</node>"; - static const GDBusInterfaceVTable login1_session_vtable = - { - handle_login1_session_call, - }; - - session = g_malloc0 (sizeof (Login1Session)); - login1_sessions = g_list_append (login1_sessions, session); - - session->id = g_strdup_printf ("c%d", login1_session_index++); - session->path = g_strdup_printf ("/org/freedesktop/login1/Session/%s", session->id); - - login1_session_info = g_dbus_node_info_new_for_xml (login1_session_interface, &error); + g_autoptr(GError) error = NULL; + g_autoptr(GDBusNodeInfo) login1_session_info = g_dbus_node_info_new_for_xml (login1_session_interface, &error); if (!login1_session_info) { g_warning ("Failed to parse login1 session D-Bus interface: %s", error->message); return NULL; } - if (g_dbus_connection_register_object (connection, - session->path, - login1_session_info->interfaces[0], - &login1_session_vtable, - session, - NULL, - &error) == 0) + static const GDBusInterfaceVTable login1_session_vtable = + { + handle_login1_session_call, + }; + if (g_dbus_connection_register_object (connection, + session->path, + login1_session_info->interfaces[0], + &login1_session_vtable, + session, + NULL, + &error) == 0) g_warning ("Failed to register login1 session: %s", error->message); return session; @@ -1754,9 +1635,7 @@ create_login1_session (GDBusConnection *connection) static Login1Session * find_login1_session (const gchar *id) { - GList *link; - - for (link = login1_sessions; link; link = link->next) + for (GList *link = login1_sessions; link; link = link->next) { Login1Session *session = link->data; if (strcmp (session->id, id) == 0) @@ -1779,10 +1658,8 @@ handle_login1_call (GDBusConnection *connection, if (strcmp (method_name, "ListSeats") == 0) { GVariantBuilder seats; - GList *link; - g_variant_builder_init (&seats, G_VARIANT_TYPE ("a(so)")); - for (link = login1_seats; link; link = link->next) + for (GList *link = login1_seats; link; link = link->next) { Login1Seat *seat = link->data; g_variant_builder_add (&seats, "(so)", seat->id, seat->path); @@ -1800,10 +1677,8 @@ handle_login1_call (GDBusConnection *connection, else if (strcmp (method_name, "LockSession") == 0) { const gchar *id; - Login1Session *session; - g_variant_get (parameters, "(&s)", &id); - session = find_login1_session (id); + Login1Session *session = find_login1_session (id); if (!session) { g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such session: %s", id); @@ -1821,10 +1696,8 @@ handle_login1_call (GDBusConnection *connection, else if (strcmp (method_name, "UnlockSession") == 0) { const gchar *id; - Login1Session *session; - g_variant_get (parameters, "(&s)", &id); - session = find_login1_session (id); + Login1Session *session = find_login1_session (id); if (!session) { g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such session: %s", id); @@ -1842,18 +1715,15 @@ handle_login1_call (GDBusConnection *connection, else if (strcmp (method_name, "ActivateSession") == 0) { const gchar *id; - Login1Session *session; - g_autofree gchar *status = NULL; - g_variant_get (parameters, "(&s)", &id); - session = find_login1_session (id); + Login1Session *session = find_login1_session (id); if (!session) { g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such session: %s", id); return; } - status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", id); + g_autofree gchar *status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", id); check_status (status); g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); @@ -1861,10 +1731,8 @@ handle_login1_call (GDBusConnection *connection, else if (strcmp (method_name, "TerminateSession") == 0) { const gchar *id; - Login1Session *session; - g_variant_get (parameters, "(&s)", &id); - session = find_login1_session (id); + Login1Session *session = find_login1_session (id); if (!session) { g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such session: %s", id); @@ -1990,20 +1858,17 @@ login1_name_acquired_cb (GDBusConnection *connection, " </signal>" " </interface>" "</node>"; - static const GDBusInterfaceVTable login1_vtable = - { - handle_login1_call, - }; - g_autoptr(GDBusNodeInfo) login1_info = NULL; - Login1Seat *seat0; g_autoptr(GError) error = NULL; - - login1_info = g_dbus_node_info_new_for_xml (login1_interface, &error); + g_autoptr(GDBusNodeInfo) login1_info = g_dbus_node_info_new_for_xml (login1_interface, &error); if (!login1_info) { g_warning ("Failed to parse login1 D-Bus interface: %s", error->message); return; } + static const GDBusInterfaceVTable login1_vtable = + { + handle_login1_call, + }; if (g_dbus_connection_register_object (connection, "/org/freedesktop/login1", login1_info->interfaces[0], @@ -2013,7 +1878,7 @@ login1_name_acquired_cb (GDBusConnection *connection, g_warning ("Failed to register login1 service: %s", error->message); /* We always have seat0 */ - seat0 = add_login1_seat (connection, "seat0", FALSE); + Login1Seat *seat0 = add_login1_seat (connection, "seat0", FALSE); if (g_key_file_has_key (config, "test-runner-config", "seat0-can-graphical", NULL)) seat0->can_graphical = g_key_file_get_boolean (config, "test-runner-config", "seat0-can-graphical", NULL); if (g_key_file_has_key (config, "test-runner-config", "seat0-can-multi-session", NULL)) @@ -2041,9 +1906,7 @@ start_login1_daemon (void) static AccountsUser * get_accounts_user_by_uid (guint uid) { - GList *link; - - for (link = accounts_users; link; link = link->next) + for (GList *link = accounts_users; link; link = link->next) { AccountsUser *u = link->data; if (u->uid == uid) @@ -2056,9 +1919,7 @@ get_accounts_user_by_uid (guint uid) static AccountsUser * get_accounts_user_by_name (const gchar *username) { - GList *link; - - for (link = accounts_users; link; link = link->next) + for (GList *link = accounts_users; link; link = link->next) { AccountsUser *u = link->data; if (strcmp (u->user_name, username) == 0) @@ -2082,8 +1943,7 @@ handle_user_call (GDBusConnection *connection, if (strcmp (method_name, "SetXSession") == 0) { - gchar *xsession; - + const gchar *xsession; g_variant_get (parameters, "(&s)", &xsession); g_free (user->xsession); @@ -2210,25 +2070,18 @@ accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_sig " <property name='KeyboardLayouts' type='as' access='read'/>" " </interface>" "</node>"; - g_autoptr(GDBusNodeInfo) user_info = NULL; g_autoptr(GError) error = NULL; - static const GDBusInterfaceVTable user_vtable = - { - handle_user_call, - handle_user_get_property, - }; - static const GDBusInterfaceVTable user_extra_vtable = - { - NULL, - handle_user_get_extra_property, - }; - - user_info = g_dbus_node_info_new_for_xml (user_interface, &error); + g_autoptr(GDBusNodeInfo) user_info = g_dbus_node_info_new_for_xml (user_interface, &error); if (!user_info) { g_warning ("Failed to parse D-Bus interface: %s", error->message); return; } + static const GDBusInterfaceVTable user_vtable = + { + handle_user_call, + handle_user_get_property, + }; user->id = g_dbus_connection_register_object (accounts_connection, user->path, user_info->interfaces[0], @@ -2241,6 +2094,11 @@ accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_sig g_warning ("Failed to register user: %s", error->message); return; } + static const GDBusInterfaceVTable user_extra_vtable = + { + NULL, + handle_user_get_extra_property, + }; user->extra_id = g_dbus_connection_register_object (accounts_connection, user->path, user_info->interfaces[1], @@ -2268,43 +2126,31 @@ accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_sig static void load_passwd_file (void) { - g_autofree gchar *path = NULL; - g_autofree gchar *data = NULL; - g_auto(GStrv) lines = NULL; - gchar **user_filter = NULL; - int i; - + g_auto(GStrv) user_filter = NULL; if (g_key_file_has_key (config, "test-runner-config", "accounts-service-user-filter", NULL)) { g_autofree gchar *filter = g_key_file_get_string (config, "test-runner-config", "accounts-service-user-filter", NULL); user_filter = g_strsplit (filter, " ", -1); } - path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); + g_autofree gchar *path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL); + g_autofree gchar *data = NULL; g_file_get_contents (path, &data, NULL, NULL); - lines = g_strsplit (data, "\n", -1); + g_auto(GStrv) lines = g_strsplit (data, "\n", -1); - for (i = 0; lines[i]; i++) + for (int i = 0; lines[i]; i++) { - g_auto(GStrv) fields = NULL; - guint uid; - const gchar *user_name, *real_name; - AccountsUser *user = NULL; - - fields = g_strsplit (lines[i], ":", -1); + g_auto(GStrv) fields = g_strsplit (lines[i], ":", -1); if (fields == NULL || g_strv_length (fields) < 7) continue; - user_name = fields[0]; - uid = atoi (fields[2]); - real_name = fields[4]; + const gchar *user_name = fields[0]; + guint uid = atoi (fields[2]); + const gchar *real_name = fields[4]; - user = get_accounts_user_by_uid (uid); + AccountsUser *user = get_accounts_user_by_uid (uid); if (!user) { - g_autofree gchar *path = NULL; - g_autoptr(GKeyFile) dmrc_file = NULL; - user = g_malloc0 (sizeof (AccountsUser)); accounts_users = g_list_append (accounts_users, user); @@ -2312,16 +2158,14 @@ load_passwd_file (void) user->hidden = FALSE; if (user_filter) { - int j; - user->hidden = TRUE; - for (j = 0; user_filter[j] != NULL; j++) + for (int j = 0; user_filter[j] != NULL; j++) if (strcmp (user_name, user_filter[j]) == 0) user->hidden = FALSE; } - dmrc_file = g_key_file_new (); - path = g_build_filename (temp_dir, "home", user_name, ".dmrc", NULL); + g_autoptr(GKeyFile) dmrc_file = g_key_file_new (); + g_autofree gchar *path = g_build_filename (temp_dir, "home", user_name, ".dmrc", NULL); g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_NONE, NULL); user->uid = uid; @@ -2364,12 +2208,10 @@ handle_accounts_call (GDBusConnection *connection, if (strcmp (method_name, "ListCachedUsers") == 0) { GVariantBuilder builder; - GList *link; - g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao")); load_passwd_file (); - for (link = accounts_users; link; link = link->next) + for (GList *link = accounts_users; link; link = link->next) { AccountsUser *user = link->data; if (!user->hidden && user->uid >= 1000) @@ -2380,13 +2222,11 @@ handle_accounts_call (GDBusConnection *connection, } else if (strcmp (method_name, "FindUserByName") == 0) { - AccountsUser *user = NULL; - gchar *user_name; - + const gchar *user_name; g_variant_get (parameters, "(&s)", &user_name); load_passwd_file (); - user = get_accounts_user_by_name (user_name); + AccountsUser *user = get_accounts_user_by_name (user_name); if (user) { if (user->hidden) @@ -2405,6 +2245,8 @@ accounts_name_acquired_cb (GDBusConnection *connection, const gchar *name, gpointer user_data) { + accounts_connection = connection; + const gchar *accounts_interface = "<node>" " <interface name='org.freedesktop.Accounts'>" @@ -2423,21 +2265,17 @@ accounts_name_acquired_cb (GDBusConnection *connection, " </signal>" " </interface>" "</node>"; - g_autoptr(GDBusNodeInfo) accounts_info = NULL; - static const GDBusInterfaceVTable accounts_vtable = - { - handle_accounts_call, - }; g_autoptr(GError) error = NULL; - - accounts_connection = connection; - - accounts_info = g_dbus_node_info_new_for_xml (accounts_interface, &error); + g_autoptr(GDBusNodeInfo) accounts_info = g_dbus_node_info_new_for_xml (accounts_interface, &error); if (!accounts_info) { g_warning ("Failed to parse D-Bus interface: %s", error->message); return; } + static const GDBusInterfaceVTable accounts_vtable = + { + handle_accounts_call, + }; if (g_dbus_connection_register_object (connection, "/org/freedesktop/Accounts", accounts_info->interfaces[0], @@ -2492,18 +2330,17 @@ properties_changed_cb (GDBusConnection *connection, GVariant *parameters, gpointer user_data) { - const gchar *interface, *name; - g_autoptr(GString) status = NULL; - GVariant *value; + const gchar *interface; GVariantIter *changed_properties, *invalidated_properties; - int i; - g_variant_get (parameters, "(&sa{sv}as)", &interface, &changed_properties, &invalidated_properties); - status = g_string_new ("RUNNER DBUS-PROPERTIES-CHANGED"); + g_autoptr(GString) status = g_string_new ("RUNNER DBUS-PROPERTIES-CHANGED"); g_string_append_printf (status, " PATH=%s", object_path); g_string_append_printf (status, " INTERFACE=%s", interface); - for (i = 0; g_variant_iter_loop (changed_properties, "{&sv}", &name, &value); i++) + + const gchar *name; + GVariant *value; + for (int i = 0; g_variant_iter_loop (changed_properties, "{&sv}", &name, &value); i++) { if (i == 0) g_string_append (status, " CHANGED="); @@ -2513,14 +2350,13 @@ properties_changed_cb (GDBusConnection *connection, if (g_variant_is_of_type (value, G_VARIANT_TYPE ("ao"))) { GVariantIter iter; - const gchar *path; - g_variant_iter_init (&iter, value); + const gchar *path; while (g_variant_iter_loop (&iter, "&o", &path)) g_string_append_printf (status, ":%s", path); } } - for (i = 0; g_variant_iter_loop (invalidated_properties, "&s", &name); i++) + for (int i = 0; g_variant_iter_loop (invalidated_properties, "&s", &name); i++) { if (i == 0) g_string_append (status, " INVALIDATED="); @@ -2541,9 +2377,7 @@ dbus_signal_cb (GDBusConnection *connection, GVariant *parameters, gpointer user_data) { - g_autoptr(GString) status = NULL; - - status = g_string_new ("RUNNER DBUS-SIGNAL"); + g_autoptr(GString) status = g_string_new ("RUNNER DBUS-SIGNAL"); g_string_append_printf (status, " PATH=%s", object_path); g_string_append_printf (status, " INTERFACE=%s", interface_name); g_string_append_printf (status, " NAME=%s", signal_name); @@ -2554,35 +2388,11 @@ dbus_signal_cb (GDBusConnection *connection, int main (int argc, char **argv) { - GMainLoop *loop; - int i; - g_autofree gchar *greeter = NULL; - const gchar *script_name; - g_autofree gchar *config_file = NULL; - g_autofree gchar *additional_system_config = NULL; - g_autofree gchar *additional_config = NULL; - g_autofree gchar *greeter_path = NULL; - g_autofree gchar *ld_library_path = NULL; - g_autofree gchar *home_dir = NULL; - g_autofree gchar *ld_preload = NULL; - g_autofree gchar *path = NULL; - g_autofree gchar *lightdm_gobject_path = NULL; - g_autofree gchar *lightdm_qt_path = NULL; - g_autofree gchar *gi_typelib_path = NULL; - g_autofree gchar *passwd_path = NULL; - g_autofree gchar *group_path = NULL; - g_autoptr(GString) passwd_data = NULL; - g_autoptr(GString) group_data = NULL; - g_autoptr(GSocketAddress) address = NULL; - GSource *status_source; - gchar cwd[1024]; - g_autoptr(GError) error = NULL; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif - loop = g_main_loop_new (NULL, FALSE); + g_autoptr(GMainLoop) loop = g_main_loop_new (NULL, FALSE); g_unix_signal_add (SIGINT, signal_cb, NULL); g_unix_signal_add (SIGTERM, signal_cb, NULL); @@ -2594,8 +2404,8 @@ main (int argc, char **argv) g_printerr ("Usage %s SCRIPT-NAME GREETER\n", argv[0]); quit (EXIT_FAILURE); } - script_name = argv[1]; - config_file = g_strdup_printf ("%s.conf", script_name); + const gchar *script_name = argv[1]; + g_autofree gchar *config_file = g_strdup_printf ("%s.conf", script_name); config_path = g_build_filename (SRCDIR, "tests", "scripts", config_file, NULL); config = g_key_file_new (); @@ -2603,6 +2413,7 @@ main (int argc, char **argv) load_script (config_path); + gchar cwd[1024]; if (!getcwd (cwd, 1024)) { g_critical ("Error getting current directory: %s", strerror (errno)); @@ -2617,24 +2428,24 @@ main (int argc, char **argv) g_unsetenv ("XDG_DATA_DIRS"); /* Override system calls */ - ld_preload = g_build_filename (BUILDDIR, "tests", "src", ".libs", "libsystem.so", NULL); + g_autofree gchar *ld_preload = g_build_filename (BUILDDIR, "tests", "src", ".libs", "libsystem.so", NULL); g_setenv ("LD_PRELOAD", ld_preload, TRUE); /* Run test programs */ - path = g_strdup_printf ("%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, g_getenv ("PATH")); + g_autofree gchar *path = g_strdup_printf ("%s/tests/src/.libs:%s/tests/src:%s/tests/src:%s/src:%s", BUILDDIR, BUILDDIR, SRCDIR, BUILDDIR, g_getenv ("PATH")); g_setenv ("PATH", path, TRUE); /* Use locally built libraries */ - lightdm_gobject_path = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL); - lightdm_qt_path = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL); - ld_library_path = g_strdup_printf ("%s:%s", lightdm_gobject_path, lightdm_qt_path); + g_autofree gchar *lightdm_gobject_path = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL); + g_autofree gchar *lightdm_qt_path = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL); + g_autofree gchar *ld_library_path = g_strdup_printf ("%s:%s", lightdm_gobject_path, lightdm_qt_path); g_setenv ("LD_LIBRARY_PATH", ld_library_path, TRUE); - gi_typelib_path = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL); + g_autofree gchar *gi_typelib_path = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL); g_setenv ("GI_TYPELIB_PATH", gi_typelib_path, TRUE); /* Run in a temporary directory inside the build directory */ /* Note we have to pick a name that is short since Unix sockets in this directory have a 108 character limit on their paths */ - i = 0; + int i = 0; while (TRUE) { g_autofree gchar *name = g_strdup_printf (".r%d", i); g_free (temp_dir); @@ -2650,6 +2461,7 @@ main (int argc, char **argv) /* Note we have to pick a socket name that is short since there is a 108 character limit on the name */ status_socket_name = g_build_filename (temp_dir, ".s", NULL); unlink (status_socket_name); + g_autoptr(GError) error = NULL; status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (!status_socket) { @@ -2657,14 +2469,14 @@ main (int argc, char **argv) quit (EXIT_FAILURE); } - address = g_unix_socket_address_new (status_socket_name); + g_autoptr(GSocketAddress) address = g_unix_socket_address_new (status_socket_name); if (!g_socket_bind (status_socket, address, FALSE, &error) || !g_socket_listen (status_socket, &error)) { g_warning ("Error binding/listening status socket %s: %s", status_socket_name, error->message); g_clear_object (&status_socket); } - status_source = g_socket_create_source (status_socket, G_IO_IN, NULL); + GSource *status_source = g_socket_create_source (status_socket, G_IO_IN, NULL); g_source_set_callback (status_source, status_connect_cb, NULL, NULL); g_source_attach (status_source, NULL); @@ -2688,42 +2500,34 @@ main (int argc, char **argv) if (system (g_strdup_printf ("cp %s/tests/data/keys.conf %s/etc/lightdm/", SRCDIR, temp_dir))) perror ("Failed to copy key configuration"); - additional_system_config = g_key_file_get_string (config, "test-runner-config", "additional-system-config", NULL); + g_autofree gchar *additional_system_config = g_key_file_get_string (config, "test-runner-config", "additional-system-config", NULL); if (additional_system_config) { - g_auto(GStrv) files = NULL; - g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/lightdm.conf.d", temp_dir), 0755); - files = g_strsplit (additional_system_config, " ", -1); - for (i = 0; files[i]; i++) + g_auto(GStrv) files = g_strsplit (additional_system_config, " ", -1); + for (int i = 0; files[i]; i++) if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/usr/share/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir))) perror ("Failed to copy configuration"); } - additional_config = g_key_file_get_string (config, "test-runner-config", "additional-config", NULL); + g_autofree gchar *additional_config = g_key_file_get_string (config, "test-runner-config", "additional-config", NULL); if (additional_config) { - g_auto(GStrv) files = NULL; - g_mkdir_with_parents (g_strdup_printf ("%s/etc/xdg/lightdm/lightdm.conf.d", temp_dir), 0755); - files = g_strsplit (additional_config, " ", -1); - for (i = 0; files[i]; i++) + g_auto(GStrv) files = g_strsplit (additional_config, " ", -1); + for (int i = 0; files[i]; i++) if (system (g_strdup_printf ("cp %s/tests/scripts/%s %s/etc/xdg/lightdm/lightdm.conf.d", SRCDIR, files[i], temp_dir))) perror ("Failed to copy configuration"); } if (g_key_file_has_key (config, "test-runner-config", "shared-data-dirs", NULL)) { - g_autofree gchar *dir_string = NULL; - g_auto(GStrv) dirs = NULL; - gint i; - - dir_string = g_key_file_get_string (config, "test-runner-config", "shared-data-dirs", NULL); - dirs = g_strsplit (dir_string, " ", -1); + g_autofree gchar *dir_string = g_key_file_get_string (config, "test-runner-config", "shared-data-dirs", NULL); + g_auto(GStrv) dirs = g_strsplit (dir_string, " ", -1); - for (i = 0; dirs[i]; i++) + for (int i = 0; dirs[i]; i++) { g_auto(GStrv) fields = g_strsplit (dirs[i], ":", -1); if (g_strv_length (fields) == 4) @@ -2753,15 +2557,15 @@ main (int argc, char **argv) perror ("Failed to copy greeters"); /* Set up the default greeter */ - greeter_path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL); - greeter = g_strdup_printf ("%s.desktop", argv[2]); + g_autofree gchar *greeter_path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL); + g_autofree gchar *greeter = g_strdup_printf ("%s.desktop", argv[2]); if (symlink (greeter, greeter_path) < 0) { g_printerr ("Failed to make greeter symlink %s->%s: %s\n", greeter_path, greeter, strerror (errno)); quit (EXIT_FAILURE); } - home_dir = g_build_filename (temp_dir, "home", NULL); + g_autofree gchar *home_dir = g_build_filename (temp_dir, "home", NULL); /* Make fake users */ struct @@ -2839,13 +2643,10 @@ main (int argc, char **argv) {"prop-user", "", "TEST", 1033}, {NULL, NULL, NULL, 0} }; - passwd_data = g_string_new (""); - group_data = g_string_new (""); - for (i = 0; users[i].user_name; i++) + g_autoptr(GString) passwd_data = g_string_new (""); + g_autoptr(GString) group_data = g_string_new (""); + for (int i = 0; users[i].user_name; i++) { - g_autoptr(GKeyFile) dmrc_file = NULL; - gboolean save_dmrc = FALSE; - if (strcmp (users[i].user_name, "mount-home-dir") != 0 && strcmp (users[i].user_name, "make-home-dir") != 0) { g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, NULL); @@ -2854,7 +2655,8 @@ main (int argc, char **argv) g_debug ("chown (%s) failed: %s", path, strerror (errno)); } - dmrc_file = g_key_file_new (); + g_autoptr(GKeyFile) dmrc_file = g_key_file_new (); + gboolean save_dmrc = FALSE; if (strcmp (users[i].user_name, "have-session") == 0) { g_key_file_set_string (dmrc_file, "Desktop", "Session", "alternative"); @@ -2887,9 +2689,8 @@ main (int argc, char **argv) /* Write corrupt X authority file */ if (strcmp (users[i].user_name, "corrupt-xauth") == 0) { - gchar data[1] = { 0xFF }; - g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL); + gchar data[1] = { 0xFF }; g_file_set_contents (path, data, 1, NULL); chmod (path, S_IRUSR | S_IWUSR); } @@ -2900,13 +2701,13 @@ main (int argc, char **argv) /* Add group file entry */ g_string_append_printf (group_data, "%s:x:%d:%s\n", users[i].user_name, users[i].uid, users[i].user_name); } - passwd_path = g_build_filename (temp_dir, "etc", "passwd", NULL); + g_autofree gchar *passwd_path = g_build_filename (temp_dir, "etc", "passwd", NULL); g_file_set_contents (passwd_path, passwd_data->str, -1, NULL); /* Add an extra test group */ g_string_append_printf (group_data, "test-group:x:111:\n"); - group_path = g_build_filename (temp_dir, "etc", "group", NULL); + g_autofree gchar *group_path = g_build_filename (temp_dir, "etc", "group", NULL); g_file_set_contents (group_path, group_data->str, -1, NULL); if (g_key_file_has_key (config, "test-runner-config", "timeout", NULL)) diff --git a/tests/src/test-script-hook.c b/tests/src/test-script-hook.c index abaf8543..534fcd89 100644 --- a/tests/src/test-script-hook.c +++ b/tests/src/test-script-hook.c @@ -9,8 +9,6 @@ static GKeyFile *config; int main (int argc, char **argv) { - g_autoptr(GString) status_text = NULL; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif @@ -26,7 +24,7 @@ main (int argc, char **argv) return EXIT_FAILURE; } - status_text = g_string_new ("SCRIPT-HOOK"); + g_autoptr(GString) status_text = g_string_new ("SCRIPT-HOOK"); g_string_append_printf (status_text, " %s", argv[1]); if (g_getenv ("USER")) g_string_append_printf (status_text, " USER=%s", g_getenv ("USER")); diff --git a/tests/src/test-session.c b/tests/src/test-session.c index 0e368164..2082b181 100644 --- a/tests/src/test-session.c +++ b/tests/src/test-session.c @@ -118,20 +118,16 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "LIST-GROUPS") == 0) { - int n_groups, i; - gid_t *groups; - g_autoptr(GString) group_list = NULL; - - n_groups = getgroups (0, NULL); + int n_groups = getgroups (0, NULL); if (n_groups < 0) { g_printerr ("Failed to get groups: %s", strerror (errno)); n_groups = 0; } - groups = malloc (sizeof (gid_t) * n_groups); + gid_t *groups = malloc (sizeof (gid_t) * n_groups); n_groups = getgroups (n_groups, groups); - group_list = g_string_new (""); - for (i = 0; i < n_groups; i++) + g_autoptr(GString) group_list = g_string_new (""); + for (int i = 0; i < n_groups; i++) { struct group *group; @@ -163,9 +159,9 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "READ") == 0) { const gchar *name = g_hash_table_lookup (params, "FILE"); + g_autofree gchar *contents = NULL; g_autoptr(GError) error = NULL; - if (g_file_get_contents (name, &contents, NULL, &error)) status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents); else @@ -177,17 +173,14 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "CHECK-X-AUTHORITY") == 0) { - g_autofree gchar *xauthority = NULL; - GStatBuf file_info; - g_autoptr(GString) mode_string = NULL; - - xauthority = g_strdup (g_getenv ("XAUTHORITY")); + g_autofree gchar *xauthority = g_strdup (g_getenv ("XAUTHORITY")); if (!xauthority) xauthority = g_build_filename (g_get_home_dir (), ".Xauthority", NULL); + GStatBuf file_info; g_stat (xauthority, &file_info); - mode_string = g_string_new (""); + g_autoptr(GString) mode_string = g_string_new (""); g_string_append_c (mode_string, file_info.st_mode & S_IRUSR ? 'r' : '-'); g_string_append_c (mode_string, file_info.st_mode & S_IWUSR ? 'w' : '-'); g_string_append_c (mode_string, file_info.st_mode & S_IXUSR ? 'x' : '-'); @@ -203,9 +196,8 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "WRITE-SHARED-DATA") == 0) { const gchar *data = g_hash_table_lookup (params, "DATA"); - const gchar *dir; - dir = getenv ("XDG_GREETER_DATA_DIR"); + const gchar *dir = getenv ("XDG_GREETER_DATA_DIR"); if (dir) { g_autofree gchar *path = NULL; @@ -226,16 +218,12 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "READ-SHARED-DATA") == 0) { - const gchar *dir; - - dir = getenv ("XDG_GREETER_DATA_DIR"); + const gchar *dir = getenv ("XDG_GREETER_DATA_DIR"); if (dir) { - g_autofree gchar *path = NULL; + g_autofree gchar *path = g_build_filename (dir, "data", NULL); g_autofree gchar *contents = NULL; g_autoptr(GError) error = NULL; - - path = g_build_filename (dir, "data", NULL); if (g_file_get_contents (path, &contents, NULL, &error)) status_notify ("%s READ-SHARED-DATA DATA=%s", session_id, contents); else @@ -247,14 +235,12 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "GREETER-START") == 0) { - int timeout; - g_autoptr(GError) error = NULL; - g_assert (greeter == NULL); greeter = lightdm_greeter_new (); g_signal_connect (greeter, LIGHTDM_GREETER_SIGNAL_SHOW_MESSAGE, G_CALLBACK (show_message_cb), NULL); g_signal_connect (greeter, LIGHTDM_GREETER_SIGNAL_SHOW_PROMPT, G_CALLBACK (show_prompt_cb), NULL); g_signal_connect (greeter, LIGHTDM_GREETER_SIGNAL_AUTHENTICATION_COMPLETE, G_CALLBACK (authentication_complete_cb), NULL); + g_autoptr(GError) error = NULL; if (lightdm_greeter_connect_to_daemon_sync (greeter, &error)) status_notify ("%s GREETER-STARTED", session_id); else @@ -274,7 +260,7 @@ request_cb (const gchar *name, GHashTable *params) status_notify ("%s GREETER-SHOW-MANUAL-LOGIN-HINT", session_id); if (!lightdm_greeter_get_show_remote_login_hint (greeter)) status_notify ("%s GREETER-SHOW-REMOTE-LOGIN-HINT=FALSE", session_id); - timeout = lightdm_greeter_get_autologin_timeout_hint (greeter); + int timeout = lightdm_greeter_get_autologin_timeout_hint (greeter); if (lightdm_greeter_get_autologin_user_hint (greeter)) { if (timeout != 0) @@ -305,7 +291,6 @@ request_cb (const gchar *name, GHashTable *params) else if (strcmp (name, "GREETER-RESPOND") == 0) { g_autoptr(GError) error = NULL; - if (!lightdm_greeter_respond (greeter, g_hash_table_lookup (params, "TEXT"), &error)) status_notify ("%s FAIL-RESPOND ERROR=%s", session_id, error->message); } @@ -328,22 +313,18 @@ request_cb (const gchar *name, GHashTable *params) int main (int argc, char **argv) { - gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_greeter_data_dir, *xdg_session_cookie, *xdg_session_class, *xdg_session_type, *xdg_session_desktop, *mir_server_host_socket, *mir_vt, *mir_id; - g_autoptr(GString) status_text = NULL; - int fd, open_max; - - display = getenv ("DISPLAY"); - xdg_seat = getenv ("XDG_SEAT"); - xdg_vtnr = getenv ("XDG_VTNR"); - xdg_current_desktop = getenv ("XDG_CURRENT_DESKTOP"); - xdg_greeter_data_dir = getenv ("XDG_GREETER_DATA_DIR"); - xdg_session_cookie = getenv ("XDG_SESSION_COOKIE"); - xdg_session_class = getenv ("XDG_SESSION_CLASS"); - xdg_session_type = getenv ("XDG_SESSION_TYPE"); - xdg_session_desktop = getenv ("XDG_SESSION_DESKTOP"); - mir_server_host_socket = getenv ("MIR_SERVER_HOST_SOCKET"); - mir_vt = getenv ("MIR_SERVER_VT"); - mir_id = getenv ("MIR_SERVER_NAME"); + const gchar *display = getenv ("DISPLAY"); + const gchar *xdg_seat = getenv ("XDG_SEAT"); + const gchar *xdg_vtnr = getenv ("XDG_VTNR"); + const gchar *xdg_current_desktop = getenv ("XDG_CURRENT_DESKTOP"); + const gchar *xdg_greeter_data_dir = getenv ("XDG_GREETER_DATA_DIR"); + const gchar *xdg_session_cookie = getenv ("XDG_SESSION_COOKIE"); + const gchar *xdg_session_class = getenv ("XDG_SESSION_CLASS"); + const gchar *xdg_session_type = getenv ("XDG_SESSION_TYPE"); + const gchar *xdg_session_desktop = getenv ("XDG_SESSION_DESKTOP"); + const gchar *mir_server_host_socket = getenv ("MIR_SERVER_HOST_SOCKET"); + const gchar *mir_vt = getenv ("MIR_SERVER_VT"); + const gchar *mir_id = getenv ("MIR_SERVER_NAME"); if (display) { if (display[0] == ':') @@ -361,8 +342,8 @@ main (int argc, char **argv) session_id = g_strdup ("SESSION-UNKNOWN"); open_fds = g_string_new (""); - open_max = sysconf (_SC_OPEN_MAX); - for (fd = STDERR_FILENO + 1; fd < open_max; fd++) + int open_max = sysconf (_SC_OPEN_MAX); + for (int fd = STDERR_FILENO + 1; fd < open_max; fd++) { if (fcntl (fd, F_GETFD) >= 0) g_string_append_printf (open_fds, "%d,", fd); @@ -381,7 +362,7 @@ main (int argc, char **argv) status_connect (request_cb, session_id); - status_text = g_string_new (""); + g_autoptr(GString) status_text = g_string_new (""); g_string_printf (status_text, "%s START", session_id); if (xdg_seat) g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat); diff --git a/tests/src/unity-system-compositor.c b/tests/src/unity-system-compositor.c index 49da86d0..2d7bb3e6 100644 --- a/tests/src/unity-system-compositor.c +++ b/tests/src/unity-system-compositor.c @@ -49,10 +49,8 @@ typedef enum static void write_message (guint16 id, const guint8 *payload, guint16 payload_length) { - guint8 *data; gsize data_length = 4 + payload_length; - - data = g_malloc (data_length); + guint8 *data = g_malloc (data_length); data[0] = id >> 8; data[1] = id & 0xFF; data[2] = payload_length >> 8; @@ -67,12 +65,9 @@ write_message (guint16 id, const guint8 *payload, guint16 payload_length) static gboolean read_message_cb (GIOChannel *channel, GIOCondition condition, gpointer data) { - gchar header[4], *payload; + gchar header[4]; gsize n_read; - guint16 id; - guint16 payload_length; g_autoptr(GError) error = NULL; - if (g_io_channel_read_chars (channel, header, 4, &n_read, &error) != G_IO_STATUS_NORMAL) { g_printerr ("Failed to read header: %s\n", error->message); @@ -83,9 +78,9 @@ read_message_cb (GIOChannel *channel, GIOCondition condition, gpointer data) g_printerr ("Short read for header, %zi instead of expected 4\n", n_read); return FALSE; } - id = header[0] << 8 | header[1]; - payload_length = header[2] << 8 | header[3]; - payload = g_malloc0 (payload_length + 1); + guint16 id = header[0] << 8 | header[1]; + guint16 payload_length = header[2] << 8 | header[3]; + gchar *payload = g_malloc0 (payload_length + 1); if (g_io_channel_read_chars (channel, payload, payload_length, &n_read, &error) != G_IO_STATUS_NORMAL) { g_printerr ("Failed to read payload: %s\n", error->message); @@ -143,12 +138,6 @@ request_cb (const gchar *name, GHashTable *params) int main (int argc, char **argv) { - int i; - g_autoptr(GString) status_text = NULL; - gboolean test = FALSE, container = FALSE; - int vt_number = -1; - const gchar *file = NULL; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif @@ -160,7 +149,10 @@ main (int argc, char **argv) status_connect (request_cb, "UNITY-SYSTEM-COMPOSITOR"); - for (i = 1; i < argc; i++) + gboolean test = FALSE, container = FALSE; + int vt_number = -1; + const gchar *file = NULL; + for (int i = 1; i < argc; i++) { char *arg = argv[i]; @@ -194,7 +186,7 @@ main (int argc, char **argv) g_io_add_watch (g_io_channel_unix_new (from_dm_fd), G_IO_IN, read_message_cb, NULL); - status_text = g_string_new ("UNITY-SYSTEM-COMPOSITOR START"); + g_autoptr(GString) status_text = g_string_new ("UNITY-SYSTEM-COMPOSITOR START"); if (file) g_string_append_printf (status_text, " FILE=%s", file); if (vt_number >= 0) diff --git a/tests/src/vnc-client.c b/tests/src/vnc-client.c index 7a49b9ad..f12be1fb 100644 --- a/tests/src/vnc-client.c +++ b/tests/src/vnc-client.c @@ -12,13 +12,6 @@ static GKeyFile *config; int main (int argc, char **argv) { - g_autoptr(GError) error = NULL; - g_autoptr(GSocket) socket = NULL; - g_autoptr(GSocketAddress) address = NULL; - gboolean result; - gchar buffer[1024]; - gssize n_read, n_sent; - #if !defined(GLIB_VERSION_2_36) g_type_init (); #endif @@ -32,22 +25,24 @@ main (int argc, char **argv) status_notify ("VNC-CLIENT CONNECT"); - socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, &error); + g_autoptr(GError) error = NULL; + g_autoptr(GSocket) socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, &error); if (!socket) { g_warning ("Unable to make VNC socket: %s", error->message); return EXIT_FAILURE; } - address = g_inet_socket_address_new (g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4), 5900); - result = g_socket_connect (socket, address, NULL, &error); + g_autoptr(GSocketAddress) address = g_inet_socket_address_new (g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4), 5900); + gboolean result = g_socket_connect (socket, address, NULL, &error); if (!result) { g_warning ("Unable to connect VNC socket: %s", error->message); return EXIT_FAILURE; } - n_read = g_socket_receive (socket, buffer, 1023, NULL, &error); + gchar buffer[1024]; + gssize n_read = g_socket_receive (socket, buffer, 1023, NULL, &error); if (n_read <= 0) { g_warning ("Unable to receive on VNC socket: %s", error->message); @@ -60,7 +55,7 @@ main (int argc, char **argv) status_notify ("VNC-CLIENT CONNECTED VERSION=\"%s\"", buffer); snprintf (buffer, 1024, "RFB 003.003\n"); - n_sent = g_socket_send (socket, buffer, strlen (buffer), NULL, &error); + gssize n_sent = g_socket_send (socket, buffer, strlen (buffer), NULL, &error); if (n_sent != strlen (buffer)) { g_warning ("Unable to send on VNC socket: %s", error->message); @@ -69,7 +64,7 @@ main (int argc, char **argv) while (TRUE) { - n_read = g_socket_receive (socket, buffer, 1023, NULL, &error); + gssize n_read = g_socket_receive (socket, buffer, 1023, NULL, &error); if (n_read < 0) { g_warning ("Unable to receive on VNC socket: %s", error->message); diff --git a/tests/src/x-authority.c b/tests/src/x-authority.c index ca46ee7a..adba92c4 100644 --- a/tests/src/x-authority.c +++ b/tests/src/x-authority.c @@ -33,21 +33,17 @@ x_authority_load (XAuthority *authority, const gchar *filename, GError **error) { guint8 *xauth_data; gsize xauth_length; - gsize offset = 0; - if (!g_file_get_contents (filename, (gchar **) &xauth_data, &xauth_length, error)) return FALSE; + gsize offset = 0; while (offset < xauth_length) { - XAuthorityRecord *record; - guint16 length; - - record = g_object_new (x_authority_record_get_type (), NULL); + XAuthorityRecord *record = g_object_new (x_authority_record_get_type (), NULL); record->priv->family = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset); record->priv->address_length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset); record->priv->address = read_string8 (xauth_data, xauth_length, record->priv->address_length, &offset); - length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset); + guint16 length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset); record->priv->number = (gchar *) read_string8 (xauth_data, xauth_length, length, &offset); length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset); record->priv->authorization_name = (gchar *) read_string8 (xauth_data, xauth_length, length, &offset); @@ -63,9 +59,7 @@ x_authority_load (XAuthority *authority, const gchar *filename, GError **error) XAuthorityRecord * x_authority_match_local (XAuthority *authority, const gchar *authorization_name) { - GList *link; - - for (link = authority->priv->records; link; link = link->next) + for (GList *link = authority->priv->records; link; link = link->next) { XAuthorityRecord *record = link->data; @@ -82,9 +76,7 @@ x_authority_match_local (XAuthority *authority, const gchar *authorization_name) XAuthorityRecord * x_authority_match_localhost (XAuthority *authority, const gchar *authorization_name) { - GList *link; - - for (link = authority->priv->records; link; link = link->next) + for (GList *link = authority->priv->records; link; link = link->next) { XAuthorityRecord *record = link->data; @@ -101,11 +93,7 @@ x_authority_match_localhost (XAuthority *authority, const gchar *authorization_n XAuthorityRecord * x_authority_match_inet (XAuthority *authority, GInetAddress *address, const gchar *authorization_name) { - GList *link; guint16 family; - gssize address_data_length; - const guint8 *address_data; - switch (g_inet_address_get_family (address)) { case G_SOCKET_FAMILY_IPV4: @@ -118,13 +106,11 @@ x_authority_match_inet (XAuthority *authority, GInetAddress *address, const gcha return NULL; } - address_data_length = g_inet_address_get_native_size (address); - address_data = g_inet_address_to_bytes (address); - for (link = authority->priv->records; link; link = link->next) + gssize address_data_length = g_inet_address_get_native_size (address); + const guint8 *address_data = g_inet_address_to_bytes (address); + for (GList *link = authority->priv->records; link; link = link->next) { XAuthorityRecord *record = link->data; - int i; - gboolean matches = TRUE; if (strcmp (record->priv->authorization_name, authorization_name) != 0) continue; @@ -138,7 +124,8 @@ x_authority_match_inet (XAuthority *authority, GInetAddress *address, const gcha if (record->priv->address_length != address_data_length) continue; - for (i = 0; i < address_data_length; i++) + gboolean matches = TRUE; + for (int i = 0; i < address_data_length; i++) { if (address_data[i] != record->priv->address[i]) { @@ -189,15 +176,13 @@ x_authority_record_get_authorization_data (XAuthorityRecord *record) gboolean x_authority_record_check_cookie (XAuthorityRecord *record, const guint8 *cookie_data, guint16 cookie_data_length) { - guint16 i; - if (strcmp (record->priv->authorization_name, "MIT-MAGIC-COOKIE-1") != 0) return FALSE; if (cookie_data_length != record->priv->authorization_data_length) return FALSE; - for (i = 0; i < cookie_data_length; i++) + for (guint16 i = 0; i < cookie_data_length; i++) if (cookie_data[i] != record->priv->authorization_data[i]) return FALSE; diff --git a/tests/src/x-common.c b/tests/src/x-common.c index 82249cd5..695a6a51 100644 --- a/tests/src/x-common.c +++ b/tests/src/x-common.c @@ -28,10 +28,8 @@ read_card8 (const guint8 *buffer, gsize buffer_length, gsize *offset) guint16 read_card16 (const guint8 *buffer, gsize buffer_length, guint8 byte_order, gsize *offset) { - guint8 a, b; - - a = read_card8 (buffer, buffer_length, offset); - b = read_card8 (buffer, buffer_length, offset); + guint8 a = read_card8 (buffer, buffer_length, offset); + guint8 b = read_card8 (buffer, buffer_length, offset); if (byte_order == X_BYTE_ORDER_MSB) return a << 8 | b; else @@ -41,12 +39,10 @@ read_card16 (const guint8 *buffer, gsize buffer_length, guint8 byte_order, gsize guint32 read_card32 (const guint8 *buffer, gsize buffer_length, guint8 byte_order, gsize *offset) { - guint8 a, b, c, d; - - a = read_card8 (buffer, buffer_length, offset); - b = read_card8 (buffer, buffer_length, offset); - c = read_card8 (buffer, buffer_length, offset); - d = read_card8 (buffer, buffer_length, offset); + guint8 a = read_card8 (buffer, buffer_length, offset); + guint8 b = read_card8 (buffer, buffer_length, offset); + guint8 c = read_card8 (buffer, buffer_length, offset); + guint8 d = read_card8 (buffer, buffer_length, offset); if (byte_order == X_BYTE_ORDER_MSB) return a << 24 | b << 16 | c << 8 | d; else @@ -56,10 +52,8 @@ read_card32 (const guint8 *buffer, gsize buffer_length, guint8 byte_order, gsize guint8 * read_string8 (const guint8 *buffer, gsize buffer_length, gsize string_length, gsize *offset) { - guint8 *string; + guint8 *string = g_malloc (string_length + 1); int i; - - string = g_malloc (string_length + 1); for (i = 0; i < string_length; i++) string[i] = read_card8 (buffer, buffer_length, offset); string[i] = '\0'; @@ -75,8 +69,7 @@ read_string (const guint8 *buffer, gsize buffer_length, gsize string_length, gsi gchar * read_padded_string (const guint8 *buffer, gsize buffer_length, gsize string_length, gsize *offset) { - guint8 *value; - value = read_string8 (buffer, buffer_length, string_length, offset); + guint8 *value = read_string8 (buffer, buffer_length, string_length, offset); read_padding (pad (string_length), offset); return (gchar *) value; } @@ -93,8 +86,7 @@ write_card8 (guint8 *buffer, gsize buffer_length, guint8 value, gsize *offset) void write_padding (guint8 *buffer, gsize buffer_length, gsize length, gsize *offset) { - gsize i; - for (i = 0; i < length; i++) + for (gsize i = 0; i < length; i++) write_card8 (buffer, buffer_length, 0, offset); } @@ -135,8 +127,7 @@ write_card32 (guint8 *buffer, gsize buffer_length, guint8 byte_order, guint32 va void write_string8 (guint8 *buffer, gsize buffer_length, const guint8 *value, gsize value_length, gsize *offset) { - gsize i; - for (i = 0; i < value_length; i++) + for (gsize i = 0; i < value_length; i++) write_card8 (buffer, buffer_length, value[i], offset); } diff --git a/tests/src/x-server.c b/tests/src/x-server.c index 2c9446a7..e1537e83 100644 --- a/tests/src/x-server.c +++ b/tests/src/x-server.c @@ -106,9 +106,9 @@ static gboolean client_read_cb (GIOChannel *channel, GIOCondition condition, gpointer data) { XClient *client = data; + g_autofree gchar *d = NULL; gsize d_length; - if (g_io_channel_read_to_end (channel, &d, &d_length, NULL) == G_IO_STATUS_NORMAL && d_length == 0) { XServer *server = client->priv->server; @@ -131,20 +131,18 @@ static gboolean socket_connect_cb (GIOChannel *channel, GIOCondition condition, gpointer data) { XServer *server = data; - GSocket *data_socket; - XClient *client; - g_autoptr(GError) error = NULL; - data_socket = g_socket_accept (server->priv->socket, NULL, &error); + g_autoptr(GError) error = NULL; + g_autoptr(GSocket) data_socket = g_socket_accept (server->priv->socket, NULL, &error); if (error) g_warning ("Error accepting connection: %s", strerror (errno)); if (!data_socket) return FALSE; - client = g_object_new (x_client_get_type (), NULL); + XClient *client = g_object_new (x_client_get_type (), NULL); client->priv->server = server; - client->priv->socket = data_socket; - client->priv->channel = g_io_channel_unix_new (g_socket_get_fd (data_socket)); + client->priv->socket = g_steal_pointer (&data_socket); + client->priv->channel = g_io_channel_unix_new (g_socket_get_fd (client->priv->socket)); g_io_add_watch (client->priv->channel, G_IO_IN | G_IO_HUP, client_read_cb, client); g_hash_table_insert (server->priv->clients, client->priv->channel, client); @@ -156,12 +154,10 @@ socket_connect_cb (GIOChannel *channel, GIOCondition condition, gpointer data) gboolean x_server_start (XServer *server) { - g_autofree gchar *name = NULL; - g_autoptr(GError) error = NULL; - - name = g_strdup_printf (".x:%d", server->priv->display_number); + g_autofree gchar *name = g_strdup_printf (".x:%d", server->priv->display_number); server->priv->socket_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), name, NULL); + g_autoptr(GError) error = NULL; server->priv->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); if (!server->priv->socket || !g_socket_bind (server->priv->socket, g_unix_socket_address_new (server->priv->socket_path), TRUE, &error) || diff --git a/tests/src/xdmcp-client.c b/tests/src/xdmcp-client.c index d616de21..99d789d8 100644 --- a/tests/src/xdmcp-client.c +++ b/tests/src/xdmcp-client.c @@ -44,11 +44,11 @@ struct XDMCPClientPrivate enum { XDMCP_CLIENT_WILLING, - XDMCP_CLIENT_UNWILLING, + XDMCP_CLIENT_UNWILLING, XDMCP_CLIENT_ACCEPT, XDMCP_CLIENT_DECLINE, XDMCP_CLIENT_FAILED, - XDMCP_CLIENT_ALIVE, + XDMCP_CLIENT_ALIVE, XDMCP_CLIENT_LAST_SIGNAL }; static guint xdmcp_client_signals[XDMCP_CLIENT_LAST_SIGNAL] = { 0 }; @@ -56,10 +56,8 @@ static guint xdmcp_client_signals[XDMCP_CLIENT_LAST_SIGNAL] = { 0 }; static void xdmcp_write (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) { - gssize n_written; g_autoptr(GError) error = NULL; - - n_written = g_socket_send (client->priv->socket, (const gchar *) buffer, buffer_length, NULL, &error); + gssize n_written = g_socket_send (client->priv->socket, (const gchar *) buffer, buffer_length, NULL, &error); if (n_written < 0) g_warning ("Failed to send XDMCP request: %s", error->message); else if (n_written != buffer_length) @@ -69,13 +67,10 @@ xdmcp_write (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) static void decode_willing (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) { - XDMCPWilling *message; - gsize offset = 0; - guint16 length; - - message = g_malloc0 (sizeof (XDMCPWilling)); + XDMCPWilling *message = g_malloc0 (sizeof (XDMCPWilling)); - length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); + gsize offset = 0; + guint16 length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->authentication_name = read_string (buffer, buffer_length, length, &offset); length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->hostname = read_string (buffer, buffer_length, length, &offset); @@ -93,13 +88,10 @@ decode_willing (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) static void decode_unwilling (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) { - XDMCPUnwilling *message; - gsize offset = 0; - guint16 length; - - message = g_malloc0 (sizeof (XDMCPUnwilling)); + XDMCPUnwilling *message = g_malloc0 (sizeof (XDMCPUnwilling)); - length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); + gsize offset = 0; + guint16 length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->hostname = read_string (buffer, buffer_length, length, &offset); length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->status = read_string (buffer, buffer_length, length, &offset); @@ -114,14 +106,11 @@ decode_unwilling (XDMCPClient *client, const guint8 *buffer, gssize buffer_lengt static void decode_accept (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) { - XDMCPAccept *message; - gsize offset = 0; - guint16 length; - - message = g_malloc (sizeof (XDMCPAccept)); + XDMCPAccept *message = g_malloc (sizeof (XDMCPAccept)); + gsize offset = 0; message->session_id = read_card32 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); - length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); + guint16 length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->authentication_name = read_string (buffer, buffer_length, length, &offset); message->authentication_data_length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->authentication_data = read_string8 (buffer, buffer_length, message->authentication_data_length, &offset); @@ -133,7 +122,7 @@ decode_accept (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) g_signal_emit (client, xdmcp_client_signals[XDMCP_CLIENT_ACCEPT], 0, message); g_free (message->authentication_name); - g_free (message->authentication_data); + g_free (message->authentication_data); g_free (message->authorization_name); g_free (message->authorization_data); g_free (message); @@ -142,13 +131,10 @@ decode_accept (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) static void decode_decline (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) { - XDMCPDecline *message; - gsize offset = 0; - guint16 length; - - message = g_malloc0 (sizeof (XDMCPDecline)); + XDMCPDecline *message = g_malloc0 (sizeof (XDMCPDecline)); - length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); + gsize offset = 0; + guint16 length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->status = read_string (buffer, buffer_length, length, &offset); length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->authentication_name = read_string (buffer, buffer_length, length, &offset); @@ -159,21 +145,18 @@ decode_decline (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) g_free (message->status); g_free (message->authentication_name); - g_free (message->authentication_data); + g_free (message->authentication_data); g_free (message); } static void decode_failed (XDMCPClient *client, const guint8 *buffer, gssize buffer_length) { - XDMCPFailed *message; - gsize offset = 0; - guint16 length; - - message = g_malloc0 (sizeof (XDMCPFailed)); + XDMCPFailed *message = g_malloc0 (sizeof (XDMCPFailed)); + gsize offset = 0; message->session_id = read_card32 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); - length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); + guint16 length = read_card16 (buffer, buffer_length, X_BYTE_ORDER_MSB, &offset); message->status = read_string (buffer, buffer_length, length, &offset); g_signal_emit (client, xdmcp_client_signals[XDMCP_CLIENT_FAILED], 0, message); @@ -202,10 +185,9 @@ static gboolean xdmcp_data_cb (GIOChannel *channel, GIOCondition condition, gpointer data) { XDMCPClient *client = data; - guint8 buffer[MAXIMUM_REQUEST_LENGTH]; - gssize n_read; - n_read = recv (g_io_channel_unix_get_fd (channel), buffer, MAXIMUM_REQUEST_LENGTH, 0); + guint8 buffer[MAXIMUM_REQUEST_LENGTH]; + gssize n_read = recv (g_io_channel_unix_get_fd (channel), buffer, MAXIMUM_REQUEST_LENGTH, 0); if (n_read < 0) g_warning ("Error reading from XDMCP socket: %s", strerror (errno)); else if (n_read == 0) @@ -216,11 +198,9 @@ xdmcp_data_cb (GIOChannel *channel, GIOCondition condition, gpointer data) else { gsize offset = 0; - guint16 version, opcode, length; - - version = read_card16 (buffer, n_read, X_BYTE_ORDER_MSB, &offset); - opcode = read_card16 (buffer, n_read, X_BYTE_ORDER_MSB, &offset); - length = read_card16 (buffer, n_read, X_BYTE_ORDER_MSB, &offset); + guint16 version = read_card16 (buffer, n_read, X_BYTE_ORDER_MSB, &offset); + guint16 opcode = read_card16 (buffer, n_read, X_BYTE_ORDER_MSB, &offset); + guint16 length = read_card16 (buffer, n_read, X_BYTE_ORDER_MSB, &offset); if (version != 1) { @@ -289,27 +269,22 @@ xdmcp_client_set_port (XDMCPClient *client, guint16 port) gboolean xdmcp_client_start (XDMCPClient *client) { - GSocketConnectable *address; - GSocketAddressEnumerator *enumerator; - g_autoptr(GError) error = NULL; - if (client->priv->socket) return TRUE; + g_autoptr(GError) error = NULL; client->priv->socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &error); if (error) g_warning ("Error creating XDMCP socket: %s", error->message); if (!client->priv->socket) return FALSE; - address = g_network_address_new (client->priv->host, client->priv->port); - enumerator = g_socket_connectable_enumerate (address); + GSocketConnectable *address = g_network_address_new (client->priv->host, client->priv->port); + GSocketAddressEnumerator *enumerator = g_socket_connectable_enumerate (address); while (TRUE) { - g_autoptr(GSocketAddress) socket_address = NULL; g_autoptr(GError) e = NULL; - - socket_address = g_socket_address_enumerator_next (enumerator, NULL, &e); + g_autoptr(GSocketAddress) socket_address = g_socket_address_enumerator_next (enumerator, NULL, &e); if (e) g_warning ("Failed to get socket address: %s", e->message); if (!socket_address) @@ -330,12 +305,10 @@ xdmcp_client_start (XDMCPClient *client) GInetAddress * xdmcp_client_get_local_address (XDMCPClient *client) { - GSocketAddress *socket_address; - if (!client->priv->socket) return NULL; - socket_address = g_socket_get_local_address (client->priv->socket, NULL); + GSocketAddress *socket_address = g_socket_get_local_address (client->priv->socket, NULL); return g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (socket_address)); } @@ -350,11 +323,11 @@ static void send_query (XDMCPClient *client, guint16 opcode, gchar **authentication_names) { guint8 buffer[MAXIMUM_REQUEST_LENGTH]; - gsize length, offset = 0, n_names = 0; - gchar **name; + gsize offset = 0; - length = 1; - for (name = authentication_names; authentication_names && *name; name++) + gsize length = 1; + gsize n_names = 0; + for (gchar **name = authentication_names; authentication_names && *name; name++) { length += 2 + strlen (*name); n_names++; @@ -364,10 +337,10 @@ send_query (XDMCPClient *client, guint16 opcode, gchar **authentication_names) write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, opcode, &offset); write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, length, &offset); write_card8 (buffer, MAXIMUM_REQUEST_LENGTH, n_names, &offset); - for (name = authentication_names; authentication_names && *name; name++) + for (gchar **name = authentication_names; authentication_names && *name; name++) { write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, strlen (*name), &offset); - write_string (buffer, MAXIMUM_REQUEST_LENGTH, *name, &offset); + write_string (buffer, MAXIMUM_REQUEST_LENGTH, *name, &offset); } xdmcp_write (client, buffer, offset); } @@ -398,40 +371,37 @@ xdmcp_client_send_request (XDMCPClient *client, const guint8 *authentication_data, guint16 authentication_data_length, gchar **authorization_names, const gchar *mfid) { - guint8 buffer[MAXIMUM_REQUEST_LENGTH]; - gsize length = 0, offset = 0, n_addresses = 0, n_names = 0; - GInetAddress **address; - gchar **name; - - length = 11 + strlen (authentication_name) + authentication_data_length + strlen (mfid); - for (address = addresses; *address; address++) + gsize length = 11 + strlen (authentication_name) + authentication_data_length + strlen (mfid); + gsize n_addresses = 0; + for (GInetAddress **address = addresses; *address; address++) { gssize native_address_length = g_inet_address_get_native_size (*address); length += 4 + native_address_length; n_addresses++; } - for (name = authorization_names; *name; name++) + gsize n_names = 0; + for (gchar **name = authorization_names; *name; name++) { length += 2 + strlen (*name); n_names++; } + guint8 buffer[MAXIMUM_REQUEST_LENGTH]; + gsize offset = 0; + write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, XDMCP_VERSION, &offset); write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, XDMCP_Request, &offset); write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, length, &offset); write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, display_number, &offset); write_card8 (buffer, MAXIMUM_REQUEST_LENGTH, n_addresses, &offset); - for (address = addresses; *address; address++) + for (GInetAddress **address = addresses; *address; address++) write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, 0, &offset); /* FamilyInternet */ write_card8 (buffer, MAXIMUM_REQUEST_LENGTH, n_addresses, &offset); - for (address = addresses; *address; address++) + for (GInetAddress **address = addresses; *address; address++) { - gssize native_address_length; - const guint8 *native_address; - - native_address_length = g_inet_address_get_native_size (*address); - native_address = g_inet_address_to_bytes (*address); + gssize native_address_length = g_inet_address_get_native_size (*address); + const guint8 *native_address = g_inet_address_to_bytes (*address); write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, native_address_length, &offset); write_string8 (buffer, MAXIMUM_REQUEST_LENGTH, native_address, native_address_length, &offset); } @@ -440,7 +410,7 @@ xdmcp_client_send_request (XDMCPClient *client, write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, authentication_data_length, &offset); write_string8 (buffer, MAXIMUM_REQUEST_LENGTH, authentication_data, authentication_data_length, &offset); write_card8 (buffer, MAXIMUM_REQUEST_LENGTH, n_names, &offset); - for (name = authorization_names; *name; name++) + for (gchar **name = authorization_names; *name; name++) { write_card16 (buffer, MAXIMUM_REQUEST_LENGTH, X_BYTE_ORDER_MSB, strlen (*name), &offset); write_string (buffer, MAXIMUM_REQUEST_LENGTH, *name, &offset); |