summaryrefslogtreecommitdiff
path: root/tests/src
diff options
context:
space:
mode:
authorRobert Ancell <robert.ancell@canonical.com>2017-09-27 17:35:55 -0400
committerRobert Ancell <robert.ancell@canonical.com>2017-09-27 17:35:55 -0400
commit90c4278a35e4666e7c830fedc8826cd332a1753b (patch)
treef6625149b7e8da1573cfab7ed46c0a99523216eb /tests/src
parent2b7eaa44d401a0a187917e53385d079e688f5172 (diff)
downloadlightdm-git-90c4278a35e4666e7c830fedc8826cd332a1753b.tar.gz
Use g_auto to automatically free memory
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/X.c62
-rw-r--r--tests/src/Xmir.c31
-rw-r--r--tests/src/Xvnc.c36
-rw-r--r--tests/src/dbus-env.c18
-rw-r--r--tests/src/guest-account.c3
-rw-r--r--tests/src/initctl.c3
-rw-r--r--tests/src/libsystem.c243
-rw-r--r--tests/src/status.c62
-rw-r--r--tests/src/test-gobject-greeter.c82
-rw-r--r--tests/src/test-qt-greeter.cpp3
-rw-r--r--tests/src/test-runner.c664
-rw-r--r--tests/src/test-script-hook.c3
-rw-r--r--tests/src/test-session.c56
-rw-r--r--tests/src/unity-system-compositor.c5
-rw-r--r--tests/src/vnc-client.c31
-rw-r--r--tests/src/x-server.c18
-rw-r--r--tests/src/xdmcp-client.c42
17 files changed, 514 insertions, 848 deletions
diff --git a/tests/src/X.c b/tests/src/X.c
index a367a9d4..cda567a2 100644
--- a/tests/src/X.c
+++ b/tests/src/X.c
@@ -67,10 +67,8 @@ cleanup (void)
{
if (lock_path)
unlink (lock_path);
- if (xserver)
- g_object_unref (xserver);
- if (xdmcp_client)
- g_object_unref (xdmcp_client);
+ g_clear_object (&xserver);
+ g_clear_object (&xdmcp_client);
}
static void
@@ -136,14 +134,13 @@ data_to_string (guint8 *data, gsize data_length)
static void
xdmcp_accept_cb (XDMCPClient *client, XDMCPAccept *message)
{
- gchar *authentication_data_text, *authorization_data_text;
+ 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);
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);
- g_free (authentication_data_text);
- g_free (authorization_data_text);
xdmcp_session_id = message->session_id;
@@ -156,11 +153,10 @@ xdmcp_accept_cb (XDMCPClient *client, XDMCPAccept *message)
static void
xdmcp_decline_cb (XDMCPClient *client, XDMCPDecline *message)
{
- gchar *authentication_data_text;
+ g_autofree gchar *authentication_data_text = NULL;
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);
- g_free (authentication_data_text);
}
static void
@@ -232,7 +228,7 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "SEND-QUERY") == 0)
{
const gchar *authentication_names_list;
- gchar **authentication_names;
+ g_auto(GStrv) authentication_names = NULL;
if (!xdmcp_client_start (xdmcp_client))
quit (EXIT_FAILURE);
@@ -243,15 +239,15 @@ request_cb (const gchar *name, GHashTable *params)
authentication_names = g_strsplit (authentication_names_list, " ", -1);
xdmcp_client_send_query (xdmcp_client, authentication_names);
- g_strfreev (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;
- gchar **list, **authorization_names;
- guint8 *authentication_data;
+ 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;
@@ -281,7 +277,6 @@ request_cb (const gchar *name, GHashTable *params)
for (i = 0; i < list_length; i++)
addresses[i] = g_inet_address_new_from_string (list[i]);
addresses[i] = NULL;
- g_strfreev (list);
authentication_data_length = strlen (authentication_data_text) / 2;
authentication_data = malloc (authentication_data_length);
@@ -296,8 +291,6 @@ request_cb (const gchar *name, GHashTable *params)
authentication_name,
authentication_data, authentication_data_length,
authorization_names, mfid);
- g_free (authentication_data);
- g_strfreev (authorization_names);
}
else if (strcmp (name, "SEND-MANAGE") == 0)
@@ -352,16 +345,16 @@ int
main (int argc, char **argv)
{
int i;
- gchar **tokens;
- char *pid_string;
+ g_auto(GStrv) tokens = NULL;
+ g_autofree gchar *pid_string = NULL;
gboolean do_xdmcp = FALSE;
guint xdmcp_port = 0;
- gchar *xdmcp_host = NULL;
- gchar *seat = NULL;
- gchar *mir_id = NULL;
- gchar *lock_filename;
+ const gchar *xdmcp_host = NULL;
+ const gchar *seat = NULL;
+ const gchar *mir_id = NULL;
+ g_autofree gchar *lock_filename = NULL;
int lock_file;
- GString *status_text;
+ g_autoptr(GString) status_text = NULL;
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
@@ -382,7 +375,6 @@ main (int argc, char **argv)
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;
- g_strfreev (tokens);
/* TCP listening default changed in 1.17.0 */
listen_tcp = version_compare (1, 17) < 0;
@@ -530,7 +522,6 @@ main (int argc, char **argv)
if (mir_id != NULL)
g_string_append_printf (status_text, " MIR-ID=%s", mir_id);
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
if (g_key_file_has_key (config, "test-xserver-config", "return-value", NULL))
{
@@ -541,26 +532,24 @@ main (int argc, char **argv)
lock_filename = g_strdup_printf (".X%d-lock", display_number);
lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL);
- g_free (lock_filename);
lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444);
if (lock_file < 0)
{
- char *lock_contents = NULL;
+ g_autofree gchar *lock_contents = NULL;
if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL))
{
- gchar *proc_filename;
+ g_autofree gchar *proc_filename = NULL;
pid_t pid;
pid = atol (lock_contents);
- g_free (lock_contents);
proc_filename = g_strdup_printf ("/proc/%d", pid);
if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS))
{
- gchar *socket_dir;
- gchar *socket_filename;
- gchar *socket_path;
+ 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_mkdir_with_parents (socket_dir, 0755);
@@ -571,12 +560,7 @@ main (int argc, char **argv)
g_printerr ("Breaking lock on non-existant process %d\n", pid);
unlink (lock_path);
unlink (socket_path);
-
- g_free (socket_dir);
- g_free (socket_filename);
- g_free (socket_path);
}
- g_free (proc_filename);
lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444);
}
@@ -588,8 +572,7 @@ main (int argc, char **argv)
"Server is already active for display %d\n"
" If this server is no longer running, remove %s\n"
" and start again.\n", display_number, lock_path);
- g_free (lock_path);
- lock_path = NULL;
+ g_clear_pointer (&lock_path, g_free);
return EXIT_FAILURE;
}
pid_string = g_strdup_printf ("%10ld", (long) getpid ());
@@ -598,7 +581,6 @@ main (int argc, char **argv)
g_warning ("Error writing PID file: %s", strerror (errno));
return EXIT_FAILURE;
}
- g_free (pid_string);
if (!x_server_start (xserver))
return EXIT_FAILURE;
diff --git a/tests/src/Xmir.c b/tests/src/Xmir.c
index 5baa4921..6e8a0f7b 100644
--- a/tests/src/Xmir.c
+++ b/tests/src/Xmir.c
@@ -41,8 +41,7 @@ cleanup (void)
{
if (lock_path)
unlink (lock_path);
- if (xserver)
- g_object_unref (xserver);
+ g_clear_object (&xserver);
}
static void
@@ -115,12 +114,12 @@ int
main (int argc, char **argv)
{
int i;
- char *pid_string;
+ g_autofree gchar *pid_string = NULL;
gchar *seat = NULL;
gchar *mir_id = NULL;
- gchar *lock_filename;
+ g_autofree gchar *lock_filename = NULL;
int lock_file;
- GString *status_text;
+ g_autoptr(GString) status_text = NULL;
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
@@ -219,7 +218,6 @@ main (int argc, char **argv)
if (mir_id != NULL)
g_string_append_printf (status_text, " MIR-ID=%s", mir_id);
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
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);
@@ -233,26 +231,24 @@ main (int argc, char **argv)
lock_filename = g_strdup_printf (".X%d-lock", display_number);
lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL);
- g_free (lock_filename);
lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444);
if (lock_file < 0)
{
- char *lock_contents = NULL;
+ g_autofree gchar *lock_contents = NULL;
if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL))
{
- gchar *proc_filename;
+ g_autofree gchar *proc_filename = NULL;
pid_t pid;
pid = atol (lock_contents);
- g_free (lock_contents);
proc_filename = g_strdup_printf ("/proc/%d", pid);
if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS))
{
- gchar *socket_dir;
- gchar *socket_filename;
- gchar *socket_path;
+ 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_mkdir_with_parents (socket_dir, 0755);
@@ -263,12 +259,7 @@ main (int argc, char **argv)
g_printerr ("Breaking lock on non-existant process %d\n", pid);
unlink (lock_path);
unlink (socket_path);
-
- g_free (socket_dir);
- g_free (socket_filename);
- g_free (socket_path);
}
- g_free (proc_filename);
lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444);
}
@@ -280,8 +271,7 @@ main (int argc, char **argv)
"Server is already active for display %d\n"
" If this server is no longer running, remove %s\n"
" and start again.\n", display_number, lock_path);
- g_free (lock_path);
- lock_path = NULL;
+ g_clear_pointer (&lock_path, g_free);
return EXIT_FAILURE;
}
pid_string = g_strdup_printf ("%10ld", (long) getpid ());
@@ -290,7 +280,6 @@ main (int argc, char **argv)
g_warning ("Error writing PID file: %s", strerror (errno));
return EXIT_FAILURE;
}
- g_free (pid_string);
if (!x_server_start (xserver))
return EXIT_FAILURE;
diff --git a/tests/src/Xvnc.c b/tests/src/Xvnc.c
index 56dada4f..9459c224 100644
--- a/tests/src/Xvnc.c
+++ b/tests/src/Xvnc.c
@@ -38,8 +38,7 @@ cleanup (void)
{
if (lock_path)
unlink (lock_path);
- if (xserver)
- g_object_unref (xserver);
+ g_clear_object (&xserver);
}
static void
@@ -91,12 +90,11 @@ vnc_data_cb (GIOChannel *channel, GIOCondition condition, gpointer data)
gchar buffer[1024];
gsize n_read;
GIOStatus status;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
status = g_io_channel_read_chars (channel, buffer, 1023, &n_read, &error);
if (error)
g_warning ("Error reading from VNC client: %s", error->message);
- g_clear_error (&error);
if (status == G_IO_STATUS_NORMAL)
{
@@ -142,12 +140,12 @@ int
main (int argc, char **argv)
{
int i;
- char *pid_string;
+ g_autofree gchar *pid_string = NULL;
gboolean use_inetd = FALSE;
gboolean has_option = FALSE;
- gchar *geometry = g_strdup ("640x480");
+ const gchar *geometry = "640x480";
gint depth = 8;
- gchar *lock_filename;
+ g_autofree gchar *lock_filename = NULL;
int lock_file;
#if !defined(GLIB_VERSION_2_36)
@@ -184,8 +182,7 @@ main (int argc, char **argv)
}
else if (strcmp (arg, "-geometry") == 0)
{
- g_free (geometry);
- geometry = g_strdup (argv[i+1]);
+ geometry = argv[i+1];
i++;
}
else if (strcmp (arg, "-depth") == 0)
@@ -241,26 +238,24 @@ main (int argc, char **argv)
lock_filename = g_strdup_printf (".X%d-lock", display_number);
lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL);
- g_free (lock_filename);
lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444);
if (lock_file < 0)
{
- char *lock_contents = NULL;
+ g_autofree gchar *lock_contents = NULL;
if (g_file_get_contents (lock_path, &lock_contents, NULL, NULL))
{
- gchar *proc_filename;
+ g_autofree gchar *proc_filename = NULL;
pid_t pid;
pid = atol (lock_contents);
- g_free (lock_contents);
proc_filename = g_strdup_printf ("/proc/%d", pid);
if (!g_file_test (proc_filename, G_FILE_TEST_EXISTS))
{
- gchar *socket_dir;
- gchar *socket_filename;
- gchar *socket_path;
+ 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_mkdir_with_parents (socket_dir, 0755);
@@ -271,12 +266,7 @@ main (int argc, char **argv)
g_printerr ("Breaking lock on non-existant process %d\n", pid);
unlink (lock_path);
unlink (socket_path);
-
- g_free (socket_dir);
- g_free (socket_filename);
- g_free (socket_path);
}
- g_free (proc_filename);
lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444);
}
@@ -288,8 +278,7 @@ main (int argc, char **argv)
"Server is already active for display %d\n"
" If this server is no longer running, remove %s\n"
" and start again.\n", display_number, lock_path);
- g_free (lock_path);
- lock_path = NULL;
+ g_clear_pointer (&lock_path, g_free);
return EXIT_FAILURE;
}
pid_string = g_strdup_printf ("%10ld", (long) getpid ());
@@ -298,7 +287,6 @@ main (int argc, char **argv)
g_warning ("Error writing PID file: %s", strerror (errno));
return EXIT_FAILURE;
}
- g_free (pid_string);
if (!x_server_start (xserver))
return EXIT_FAILURE;
diff --git a/tests/src/dbus-env.c b/tests/src/dbus-env.c
index d99d73ae..a988b68b 100644
--- a/tests/src/dbus-env.c
+++ b/tests/src/dbus-env.c
@@ -13,7 +13,7 @@ create_bus (const gchar *config_file, GPid *pid)
gchar *command, address[1024];
gchar **argv;
ssize_t n_read;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (pipe (name_pipe) < 0)
{
@@ -26,7 +26,6 @@ create_bus (const gchar *config_file, GPid *pid)
g_warning ("Error parsing command line: %s", error->message);
exit (EXIT_FAILURE);
}
- g_clear_error (&error);
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);
@@ -50,18 +49,19 @@ create_bus (const gchar *config_file, GPid *pid)
int
main (int argc, char **argv)
{
- gchar *conf_file, *system_bus_address, *session_bus_address;
+ 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;
- conf_file = g_build_filename (DATADIR, "system.conf", NULL);
- system_bus_address = create_bus (conf_file, &system_bus_pid);
- g_free (conf_file);
+ system_conf_file = g_build_filename (DATADIR, "system.conf", NULL);
+ system_bus_address = create_bus (system_conf_file, &system_bus_pid);
g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", system_bus_address, TRUE);
- conf_file = g_build_filename (DATADIR, "session.conf", NULL);
- session_bus_address = create_bus (conf_file, &session_bus_pid);
- g_free (conf_file);
+ session_conf_file = g_build_filename (DATADIR, "session.conf", NULL);
+ session_bus_address = create_bus (session_conf_file, &session_bus_pid);
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_address, TRUE);
child_pid = fork ();
diff --git a/tests/src/guest-account.c b/tests/src/guest-account.c
index 655f34b9..0df3f092 100644
--- a/tests/src/guest-account.c
+++ b/tests/src/guest-account.c
@@ -46,14 +46,13 @@ main (int argc, char **argv)
{
while (fgets (line, 1024, passwd))
{
- gchar **tokens = g_strsplit (line, ":", -1);
+ g_auto(GStrv) tokens = g_strsplit (line, ":", -1);
if (g_strv_length (tokens) >= 3)
{
gint uid = atoi (tokens[2]);
if (uid > max_uid)
max_uid = uid;
}
- g_strfreev (tokens);
}
fclose (passwd);
}
diff --git a/tests/src/initctl.c b/tests/src/initctl.c
index 600cea98..f7732c0b 100644
--- a/tests/src/initctl.c
+++ b/tests/src/initctl.c
@@ -16,14 +16,13 @@ main (int argc, char **argv)
if (g_key_file_get_boolean (config, "test-initctl-config", "report-events", NULL))
{
- GString *status_text;
+ g_autoptr(GString) status_text = NULL;
int i;
status_text = g_string_new ("INIT");
for (i = 1; i < argc; i++)
g_string_append_printf (status_text, " %s", argv[i]);
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
}
return EXIT_SUCCESS;
diff --git a/tests/src/libsystem.c b/tests/src/libsystem.c
index dadcaf59..5959995e 100644
--- a/tests/src/libsystem.c
+++ b/tests/src/libsystem.c
@@ -107,7 +107,7 @@ int
getgroups (int size, gid_t list[])
{
const gchar *group_list;
- gchar **groups;
+ g_auto(GStrv) groups = NULL;
gint groups_length;
/* Get groups we are a member of */
@@ -129,7 +129,6 @@ getgroups (int size, gid_t list[])
for (i = 0; groups[i]; i++)
list[i] = atoi (groups[i]);
}
- g_free (groups);
return groups_length;
}
@@ -138,7 +137,7 @@ int
setgroups (size_t size, const gid_t *list)
{
size_t i;
- GString *group_list;
+ g_autoptr(GString) group_list = NULL;
group_list = g_string_new ("");
for (i = 0; i < size; i++)
@@ -148,7 +147,6 @@ setgroups (size_t size, const gid_t *list)
g_string_append_printf (group_list, "%d", list[i]);
}
g_setenv ("LIGHTDM_TEST_GROUPS", group_list->str, TRUE);
- g_string_free (group_list, TRUE);
return 0;
}
@@ -229,7 +227,7 @@ 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);
- gchar *new_path = NULL;
+ g_autofree gchar *new_path = NULL;
int fd;
_open = (int (*)(const char *pathname, int flags, mode_t mode)) dlsym (RTLD_NEXT, func);
@@ -246,7 +244,6 @@ open_wrapper (const char *func, const char *pathname, int flags, mode_t mode)
new_path = redirect_path (pathname);
fd = _open (new_path, flags, mode);
- g_free (new_path);
return fd;
}
@@ -283,14 +280,13 @@ FILE *
fopen (const char *path, const char *mode)
{
FILE *(*_fopen) (const char *pathname, const char *mode);
- gchar *new_path = NULL;
+ g_autofree gchar *new_path = NULL;
FILE *result;
_fopen = (FILE *(*)(const char *pathname, const char *mode)) dlsym (RTLD_NEXT, "fopen");
new_path = redirect_path (path);
result = _fopen (new_path, mode);
- g_free (new_path);
return result;
}
@@ -299,14 +295,13 @@ int
unlinkat (int dirfd, const char *pathname, int flags)
{
int (*_unlinkat) (int dirfd, const char *pathname, int flags);
- gchar *new_path = NULL;
+ g_autofree gchar *new_path = NULL;
int result;
_unlinkat = (int (*)(int dirfd, const char *pathname, int flags)) dlsym (RTLD_NEXT, "unlinkat");
new_path = redirect_path (pathname);
result = _unlinkat (dirfd, new_path, flags);
- g_free (new_path);
return result;
}
@@ -315,14 +310,13 @@ int
creat (const char *pathname, mode_t mode)
{
int (*_creat) (const char *pathname, mode_t mode);
- gchar *new_path = NULL;
+ g_autofree gchar *new_path = NULL;
int result;
_creat = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "creat");
new_path = redirect_path (pathname);
result = _creat (new_path, mode);
- g_free (new_path);
return result;
}
@@ -331,14 +325,13 @@ int
creat64 (const char *pathname, mode_t mode)
{
int (*_creat64) (const char *pathname, mode_t mode);
- gchar *new_path = NULL;
+ g_autofree gchar *new_path = NULL;
int result;
_creat64 = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "creat64");
new_path = redirect_path (pathname);
result = _creat64 (new_path, mode);
- g_free (new_path);
return result;
}
@@ -347,8 +340,7 @@ int
access (const char *pathname, int mode)
{
int (*_access) (const char *pathname, int mode);
- gchar *new_path = NULL;
- int ret;
+ g_autofree gchar *new_path = NULL;
_access = (int (*)(const char *pathname, int mode)) dlsym (RTLD_NEXT, "access");
@@ -358,138 +350,103 @@ access (const char *pathname, int mode)
return F_OK;
new_path = redirect_path (pathname);
- ret = _access (new_path, mode);
- g_free (new_path);
-
- return ret;
+ return _access (new_path, mode);
}
int
stat (const char *path, struct stat *buf)
{
int (*_stat) (const char *path, struct stat *buf);
- gchar *new_path = NULL;
- int ret;
+ g_autofree gchar *new_path = NULL;
_stat = (int (*)(const char *path, struct stat *buf)) dlsym (RTLD_NEXT, "stat");
new_path = redirect_path (path);
- ret = _stat (new_path, buf);
- g_free (new_path);
-
- return ret;
+ return _stat (new_path, buf);
}
int
stat64 (const char *path, struct stat64 *buf)
{
int (*_stat64) (const char *path, struct stat64 *buf);
- gchar *new_path = NULL;
- int ret;
+ g_autofree gchar *new_path = NULL;
_stat64 = (int (*)(const char *path, struct stat64 *buf)) dlsym (RTLD_NEXT, "stat64");
new_path = redirect_path (path);
- ret = _stat64 (new_path, buf);
- g_free (new_path);
-
- return ret;
+ 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);
- gchar *new_path = NULL;
- int ret;
+ g_autofree gchar *new_path = NULL;
___xstat = (int (*)(int version, const char *path, struct stat *buf)) dlsym (RTLD_NEXT, "__xstat");
new_path = redirect_path (path);
- ret = ___xstat (version, new_path, buf);
- g_free (new_path);
-
- return ret;
+ 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);
- gchar *new_path = NULL;
- int ret;
+ g_autofree gchar *new_path = NULL;
___xstat64 = (int (*)(int version, const char *path, struct stat64 *buf)) dlsym (RTLD_NEXT, "__xstat64");
new_path = redirect_path (path);
- ret = ___xstat64 (version, new_path, buf);
- g_free (new_path);
-
- return ret;
+ 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);
- gchar *new_path = NULL;
- int ret;
+ g_autofree gchar *new_path = NULL;
___fxstatat = (int (*)(int ver, int dirfd, const char *pathname, struct stat *buf, int flags)) dlsym (RTLD_NEXT, "__fxstatat");
new_path = redirect_path (pathname);
- ret = ___fxstatat (ver, dirfd, new_path, buf, flags);
- g_free (new_path);
-
- return ret;
+ 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);
- gchar *new_path = NULL;
- int ret;
+ g_autofree gchar *new_path = NULL;
___fxstatat64 = (int (*)(int ver, int dirfd, const char *pathname, struct stat64 *buf, int flags)) dlsym (RTLD_NEXT, "__fxstatat64");
new_path = redirect_path (pathname);
- ret = ___fxstatat64 (ver, dirfd, new_path, buf, flags);
- g_free (new_path);
-
- return ret;
+ return ___fxstatat64 (ver, dirfd, new_path, buf, flags);
}
DIR *
opendir (const char *name)
{
DIR *(*_opendir) (const char *name);
- gchar *new_path = NULL;
- DIR *result;
+ g_autofree gchar *new_path = NULL;
_opendir = (DIR *(*)(const char *name)) dlsym (RTLD_NEXT, "opendir");
new_path = redirect_path (name);
- result = _opendir (new_path);
- g_free (new_path);
-
- return result;
+ return _opendir (new_path);
}
int
mkdir (const char *pathname, mode_t mode)
{
int (*_mkdir) (const char *pathname, mode_t mode);
- gchar *new_path = NULL;
- int result;
+ g_autofree gchar *new_path = NULL;
_mkdir = (int (*)(const char *pathname, mode_t mode)) dlsym (RTLD_NEXT, "mkdir");
new_path = redirect_path (pathname);
- result = _mkdir (new_path, mode);
- g_free (new_path);
-
- return result;
+ return _mkdir (new_path, mode);
}
int
@@ -503,16 +460,12 @@ int
chmod (const char *path, mode_t mode)
{
int (*_chmod) (const char *path, mode_t mode);
- gchar *new_path = NULL;
- int result;
+ g_autofree gchar *new_path = NULL;
_chmod = (int (*)(const char *path, mode_t mode)) dlsym (RTLD_NEXT, "chmod");
new_path = redirect_path (path);
- result = _chmod (new_path, mode);
- g_free (new_path);
-
- return result;
+ return _chmod (new_path, mode);
}
int
@@ -566,8 +519,10 @@ ioctl (int d, unsigned long request, ...)
static void
add_port_redirect (int requested_port, int redirected_port)
{
- GKeyFile *file;
- gchar *path, *name, *data;
+ 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);
@@ -575,32 +530,25 @@ add_port_redirect (int requested_port, int redirected_port)
name = g_strdup_printf ("%d", requested_port);
g_key_file_set_integer (file, name, "redirected", redirected_port);
- g_free (name);
data = g_key_file_to_data (file, NULL, NULL);
g_file_set_contents (path, data, -1, NULL);
- g_free (data);
- g_free (path);
-
- g_key_file_free (file);
}
static int
find_port_redirect (int port)
{
- GKeyFile *file;
- gchar *path, *name;
+ 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_key_file_load_from_file (file, path, G_KEY_FILE_NONE, NULL);
- g_free (path);
name = g_strdup_printf ("%d", port);
redirected_port = g_key_file_get_integer (file, name, "redirected", NULL);
- g_free (name);
- g_key_file_free (file);
return redirected_port;
}
@@ -625,10 +573,9 @@ bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
path = ((const struct sockaddr_un *) addr)->sun_path;
if (path[0] != '\0')
{
- gchar *new_path = redirect_path (path);
+ g_autofree gchar *new_path = redirect_path (path);
memcpy (&temp_addr_un, addr, sizeof (struct sockaddr_un));
strncpy (temp_addr_un.sun_path, new_path, sizeof (temp_addr_un.sun_path) - 1);
- g_free (new_path);
modified_addr = (struct sockaddr *) &temp_addr_un;
}
break;
@@ -703,10 +650,9 @@ connect (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
path = ((const struct sockaddr_un *) addr)->sun_path;
if (path[0] != '\0')
{
- gchar *new_path = redirect_path (path);
+ g_autofree gchar *new_path = redirect_path (path);
memcpy (&temp_addr_un, addr, addrlen);
strncpy (temp_addr_un.sun_path, new_path, sizeof (temp_addr_un.sun_path) - 1);
- g_free (new_path);
modified_addr = (struct sockaddr *) &temp_addr_un;
}
break;
@@ -754,10 +700,9 @@ sendto (int sockfd, const void *buf, size_t len, int flags, const struct sockadd
path = ((const struct sockaddr_un *) dest_addr)->sun_path;
if (path[0] != '\0')
{
- gchar *new_path = redirect_path (path);
+ g_autofree gchar *new_path = redirect_path (path);
memcpy (&temp_addr_un, dest_addr, sizeof (struct sockaddr_un));
strncpy (temp_addr_un.sun_path, new_path, sizeof (temp_addr_un.sun_path) - 1);
- g_free (new_path);
modified_addr = (struct sockaddr *) &temp_addr_un;
}
break;
@@ -815,30 +760,29 @@ free_user (gpointer data)
static void
load_passwd_file (void)
{
- gchar *path, *data = NULL, **lines;
+ g_autofree gchar *path = NULL;
+ g_autofree gchar *data = NULL;
+ g_auto(GStrv) lines = NULL;
gint i;
- GError *error = NULL;
+ 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_file_get_contents (path, &data, NULL, &error);
- g_free (path);
- if (error)
+ if (!g_file_get_contents (path, &data, NULL, &error))
+ {
g_warning ("Error loading passwd file: %s", error->message);
- g_clear_error (&error);
-
- if (!data)
return;
+ }
lines = g_strsplit (data, "\n", -1);
- g_free (data);
for (i = 0; lines[i]; i++)
{
- gchar *line, **fields;
+ const gchar *line;
+ g_auto(GStrv) fields = NULL;
line = g_strstrip (lines[i]);
fields = g_strsplit (line, ":", -1);
@@ -855,9 +799,7 @@ load_passwd_file (void)
entry->pw_shell = g_strdup (fields[6]);
user_entries = g_list_append (user_entries, entry);
}
- g_strfreev (fields);
}
- g_strfreev (lines);
}
struct passwd *
@@ -944,29 +886,28 @@ free_group (gpointer data)
static void
load_group_file (void)
{
- gchar *path, *data = NULL, **lines;
+ g_autofree gchar *path = NULL;
+ g_autofree gchar *data = NULL;
+ g_auto(GStrv) lines = NULL;
gint i;
- GError *error = NULL;
+ 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_file_get_contents (path, &data, NULL, &error);
- g_free (path);
- if (error)
+ if (!g_file_get_contents (path, &data, NULL, &error))
+ {
g_warning ("Error loading group file: %s", error->message);
- g_clear_error (&error);
-
- if (!data)
return;
+ }
lines = g_strsplit (data, "\n", -1);
- g_free (data);
for (i = 0; lines[i]; i++)
{
- gchar *line, **fields;
+ const gchar *line;
+ g_auto(GStrv) fields = NULL;
line = g_strstrip (lines[i]);
fields = g_strsplit (line, ":", -1);
@@ -980,9 +921,7 @@ load_group_file (void)
entry->gr_mem = g_strsplit (fields[3], ",", -1);
group_entries = g_list_append (group_entries, entry);
}
- g_strfreev (fields);
}
- g_strfreev (lines);
}
struct group *
@@ -1040,7 +979,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))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s START", handle->id);
@@ -1048,7 +987,6 @@ pam_start (const char *service_name, const char *user, const struct pam_conv *co
if (user)
g_string_append_printf (status, " USER=%s", user);
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
handle->service_name = strdup (service_name);
@@ -1073,7 +1011,7 @@ pam_authenticate (pam_handle_t *pamh, int flags)
connect_status ();
if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s AUTHENTICATE", pamh->id);
@@ -1083,7 +1021,6 @@ pam_authenticate (pam_handle_t *pamh, int flags)
g_string_append (status, " DISALLOW_NULL_AUTHTOK");
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
if (strcmp (pamh->service_name, "test-remote") == 0)
@@ -1437,13 +1374,13 @@ pam_get_item (const pam_handle_t *pamh, int item_type, const void **item)
int
pam_open_session (pam_handle_t *pamh, int flags)
{
- GVariant *result;
- GError *error = NULL;
+ g_autoptr(GVariant) result = NULL;
+ g_autoptr(GError) error = NULL;
connect_status ();
if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s OPEN-SESSION", pamh->id);
@@ -1451,7 +1388,6 @@ pam_open_session (pam_handle_t *pamh, int flags)
g_string_append (status, " SILENT");
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
if (strcmp (pamh->user, "session-error") == 0)
@@ -1478,18 +1414,15 @@ pam_open_session (pam_handle_t *pamh, int flags)
&error);
if (result)
{
- gchar *e;
+ g_autofree gchar *e = NULL;
const gchar *id;
g_variant_get (result, "(&so)", &id, NULL);
e = g_strdup_printf ("XDG_SESSION_ID=%s", id);
pam_putenv (pamh, e);
- g_free (e);
- g_variant_unref (result);
}
else
g_printerr ("Failed to create logind session: %s\n", error->message);
- g_clear_error (&error);
return PAM_SUCCESS;
}
@@ -1500,7 +1433,7 @@ pam_close_session (pam_handle_t *pamh, int flags)
connect_status ();
if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s CLOSE-SESSION", pamh->id);
@@ -1508,7 +1441,6 @@ pam_close_session (pam_handle_t *pamh, int flags)
g_string_append (status, " SILENT");
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
return PAM_SUCCESS;
@@ -1520,7 +1452,7 @@ pam_acct_mgmt (pam_handle_t *pamh, int flags)
connect_status ();
if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s ACCT-MGMT", pamh->id);
@@ -1530,7 +1462,6 @@ pam_acct_mgmt (pam_handle_t *pamh, int flags)
g_string_append (status, " DISALLOW_NULL_AUTHTOK");
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
if (!pamh->user)
@@ -1557,7 +1488,7 @@ pam_chauthtok (pam_handle_t *pamh, int flags)
connect_status ();
if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s CHAUTHTOK", pamh->id);
@@ -1567,7 +1498,6 @@ pam_chauthtok (pam_handle_t *pamh, int flags)
g_string_append (status, " CHANGE_EXPIRED_AUTHTOK");
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
msg = malloc (sizeof (struct pam_message *) * 1);
@@ -1603,12 +1533,12 @@ pam_chauthtok (pam_handle_t *pamh, int flags)
int
pam_setcred (pam_handle_t *pamh, int flags)
{
- gchar *e;
+ g_autofree gchar *e = NULL;
connect_status ();
if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s SETCRED", pamh->id);
@@ -1624,13 +1554,11 @@ pam_setcred (pam_handle_t *pamh, int flags)
g_string_append (status, " REFRESH_CRED");
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
/* 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"));
pam_putenv (pamh, e);
- g_free (e);
if (strcmp (pamh->user, "cred-error") == 0)
return PAM_CRED_ERR;
@@ -1675,12 +1603,11 @@ pam_end (pam_handle_t *pamh, int pam_status)
connect_status ();
if (g_key_file_get_boolean (config, "test-pam", "log-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("");
g_string_append_printf (status, "%s END", pamh->id);
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
free (pamh->id);
@@ -1786,7 +1713,7 @@ pututxline (const struct utmpx *ut)
connect_status ();
if (g_key_file_get_boolean (config, "test-utmp-config", "check-events", NULL))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("UTMP");
switch (ut->ut_type)
@@ -1815,7 +1742,6 @@ pututxline (const struct utmpx *ut)
if (ut->ut_host)
g_string_append_printf (status, " HOST=%s", ut->ut_host);
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
return (struct utmpx *)ut;
@@ -1832,7 +1758,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))
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("WTMP");
g_string_append_printf (status, " FILE=%s", wtmp_file);
@@ -1862,7 +1788,6 @@ updwtmp (const char *wtmp_file, const struct utmp *ut)
if (ut->ut_host)
g_string_append_printf (status, " HOST=%s", ut->ut_host);
status_notify ("%s", status->str);
- g_string_free (status, TRUE);
}
}
@@ -1877,8 +1802,6 @@ xcb_connection_t *
xcb_connect_to_display_with_auth_info (const char *display, xcb_auth_info_t *auth, int *screen)
{
xcb_connection_t *c;
- gchar *socket_path;
- GError *error = NULL;
c = malloc (sizeof (xcb_connection_t));
c->display = g_strdup (display);
@@ -1891,32 +1814,32 @@ 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 (error)
- g_printerr ("%s\n", error->message);
- g_clear_error (&error);
if (c->socket == NULL)
+ {
+ g_printerr ("%s\n", error->message);
c->error = XCB_CONN_ERROR;
+ }
}
if (c->error == 0)
{
- gchar *d;
- GSocketAddress *address;
+ 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);
- g_free (d);
address = g_unix_socket_address_new (socket_path);
if (!g_socket_connect (c->socket, address, NULL, &error))
- c->error = XCB_CONN_ERROR;
- g_object_unref (address);
- if (error)
+ {
g_printerr ("Failed to connect to X socket %s: %s\n", socket_path, error->message);
- g_free (socket_path);
- g_clear_error (&error);
+ c->error = XCB_CONN_ERROR;
+ }
}
// FIXME: Send auth info
@@ -1964,7 +1887,7 @@ 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)
{
- gchar *type_string;
+ g_autofree gchar *type_string = NULL;
connect_status ();
if (!g_key_file_get_boolean (config, "test-audit-config", "check-events", NULL))
@@ -1986,8 +1909,6 @@ audit_log_acct_message (int audit_fd, int type, const char *pgname,
status_notify ("AUDIT LOG-ACCT TYPE=%s PGNAME=%s OP=%s NAME=%s ID=%u HOST=%s ADDR=%s TTY=%s RESULT=%d",
type_string, pgname ? pgname : "", op ? op : "", name ? name : "", id, host ? host : "", addr ? addr : "", tty ? tty : "", result);
- g_free (type_string);
-
return 1;
}
diff --git a/tests/src/status.c b/tests/src/status.c
index 08258685..02d66e40 100644
--- a/tests/src/status.c
+++ b/tests/src/status.c
@@ -21,10 +21,11 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
ssize_t n_read;
const gchar *c, *start;
int l;
- gchar *id, *name = NULL;
+ g_autofree gchar *id = NULL;
+ g_autofree gchar *name = NULL;
gboolean id_matches;
- GHashTable *params;
- GError *error = NULL;
+ g_autoptr(GHashTable) params = NULL;
+ g_autoptr(GError) error = NULL;
n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
if (n_read > 0)
@@ -36,7 +37,6 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
else
g_warning ("Error reading from socket: %s", error->message);
}
- g_clear_error (&error);
if (n_read == 0)
{
if (request_func)
@@ -58,7 +58,6 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
}
id = g_strdup_printf ("%.*s", l, start);
id_matches = g_strcmp0 (id, filter_id) == 0;
- g_free (id);
if (!id_matches)
return TRUE;
@@ -77,7 +76,8 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
while (TRUE)
{
const gchar *start;
- gchar *param_name, *param_value;
+ g_autofree gchar *param_name = NULL;
+ g_autofree gchar *param_value = NULL;
while (isspace (*c))
c++;
@@ -97,7 +97,7 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
if (*c == '\"')
{
gboolean escaped = FALSE;
- GString *value;
+ g_autoptr(GString) value = NULL;
c++;
value = g_string_new ("");
@@ -119,8 +119,7 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
g_string_append_c (value, *c);
c++;
}
- param_value = value->str;
- g_string_free (value, FALSE);
+ param_value = g_strdup (value->str);
if (*c == '\"')
c++;
}
@@ -135,48 +134,38 @@ status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
else
param_value = g_strdup ("");
- g_hash_table_insert (params, param_name, param_value);
+ g_hash_table_insert (params, g_steal_pointer (&param_name), g_steal_pointer (&param_value));
}
request_func (name, params);
- g_free (name);
- g_hash_table_unref (params);
-
return TRUE;
}
gboolean
status_connect (StatusRequestFunc request_cb, const gchar *id)
{
- gchar *path;
- GSocketAddress *address;
- gboolean result;
+ g_autofree gchar *path = NULL;
+ g_autoptr(GSocketAddress) address = NULL;
GSource *source;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
request_func = request_cb;
filter_id = g_strdup (id);
status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
- if (error)
- g_printerr ("Unable to open socket for status: %s\n", error->message);
- g_clear_error (&error);
if (!status_socket)
+ {
+ g_printerr ("Unable to open socket for status: %s\n", error->message);
return FALSE;
+ }
path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".s", NULL);
address = g_unix_socket_address_new (path);
- result = g_socket_connect (status_socket, address, NULL, &error);
- g_object_unref (address);
- if (error)
- g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message);
- g_clear_error (&error);
- g_free (path);
- if (!result)
+ if (!g_socket_connect (status_socket, address, NULL, &error))
{
- g_object_unref (status_socket);
- status_socket = NULL;
+ g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message);
+ g_clear_object (&status_socket);
return FALSE;
}
@@ -190,8 +179,6 @@ status_connect (StatusRequestFunc request_cb, const gchar *id)
void
status_notify (const gchar *format, ...)
{
- gboolean written = FALSE;
-
gchar status[1024];
va_list ap;
@@ -201,17 +188,16 @@ status_notify (const gchar *format, ...)
if (status_socket)
{
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
int length;
length = strlen (status);
- written = g_socket_send (status_socket, (gchar *) &length, sizeof (length), NULL, &error) == sizeof (length) &&
- g_socket_send (status_socket, status, strlen (status), NULL, &error) == strlen (status);
- if (error)
+ 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);
- g_clear_error (&error);
+ else
+ return;
}
- if (!written)
- g_printerr ("%s\n", status);
+ g_printerr ("%s\n", status);
}
diff --git a/tests/src/test-gobject-greeter.c b/tests/src/test-gobject-greeter.c
index c7535ac6..455a1321 100644
--- a/tests/src/test-gobject-greeter.c
+++ b/tests/src/test-gobject-greeter.c
@@ -114,56 +114,52 @@ static void
start_session_finished (GObject *object, GAsyncResult *result, gpointer data)
{
LightDMGreeter *greeter = LIGHTDM_GREETER (object);
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (!lightdm_greeter_start_session_finish (greeter, result, &error))
status_notify ("%s SESSION-FAILED ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
}
static void
write_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data)
{
LightDMGreeter *greeter = LIGHTDM_GREETER (object);
- gchar *dir, *path, *test_data;
+ g_autofree gchar *dir = NULL;
+ g_autofree gchar *path = NULL;
+ g_autofree gchar *test_data = data;
FILE *f;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
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);
- g_clear_error (&error);
return;
}
path = g_build_filename (dir, "data", NULL);
- test_data = data;
if (!(f = fopen (path, "w")) || fprintf (f, "%s", test_data) < 0)
status_notify ("%s WRITE-SHARED-DATA ERROR=%s", greeter_id, strerror (errno));
else
status_notify ("%s WRITE-SHARED-DATA RESULT=TRUE", greeter_id);
- g_free (test_data);
if (f)
fclose (f);
- g_free (path);
- g_free (dir);
}
static void
read_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data)
{
LightDMGreeter *greeter = LIGHTDM_GREETER (object);
- gchar *dir, *path;
- gchar *contents = NULL;
- GError *error = NULL;
+ 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);
if (!dir)
{
status_notify ("%s READ-SHARED-DATA ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
return;
}
@@ -172,16 +168,11 @@ read_shared_data_finished (GObject *object, GAsyncResult *result, gpointer data)
status_notify ("%s READ-SHARED-DATA DATA=%s", greeter_id, contents);
else
status_notify ("%s READ-SHARED-DATA ERROR=%s", greeter_id, error->message);
- g_free (path);
- g_free (contents);
- g_clear_error (&error);
}
static void
request_cb (const gchar *name, GHashTable *params)
{
- GError *error = NULL;
-
if (!name)
{
g_main_loop_quit (loop);
@@ -193,56 +184,44 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "AUTHENTICATE") == 0)
{
+ g_autoptr(GError) error = NULL;
if (!lightdm_greeter_authenticate (greeter, g_hash_table_lookup (params, "USERNAME"), &error))
- {
status_notify ("%s FAIL-AUTHENTICATE ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "AUTHENTICATE-GUEST") == 0)
{
+ g_autoptr(GError) error = NULL;
if (!lightdm_greeter_authenticate_as_guest (greeter, &error))
- {
status_notify ("%s FAIL-AUTHENTICATE-GUEST ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "AUTHENTICATE-AUTOLOGIN") == 0)
{
+ g_autoptr(GError) error = NULL;
if (!lightdm_greeter_authenticate_autologin (greeter, &error))
- {
status_notify ("%s FAIL-AUTHENTICATE-AUTOLOGIN ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "AUTHENTICATE-REMOTE") == 0)
{
+ g_autoptr(GError) error = NULL;
if (!lightdm_greeter_authenticate_remote (greeter, g_hash_table_lookup (params, "SESSION"), NULL, &error))
- {
status_notify ("%s FAIL-AUTHENTICATE-REMOTE ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "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", greeter_id, error->message);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "CANCEL-AUTHENTICATION") == 0)
{
+ g_autoptr(GError) error = NULL;
if (!lightdm_greeter_cancel_authentication (greeter, &error))
- {
status_notify ("%s FAIL-CANCEL-AUTHENTICATION ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "START-SESSION") == 0)
@@ -280,9 +259,9 @@ request_cb (const gchar *name, GHashTable *params)
LightDMUser *user;
const gchar *username, *image, *background, *language, *layout, *session;
const gchar * const * layouts;
- gchar **fields = NULL;
- gchar *layouts_text;
- GString *status_text;
+ 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");
@@ -330,11 +309,8 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (fields[i], "UID") == 0)
g_string_append_printf (status_text, " UID=%d", lightdm_user_get_uid (user));
}
- g_strfreev (fields);
- g_free (layouts_text);
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
}
else if (strcmp (name, "LOG-USER-LIST") == 0)
@@ -369,10 +345,9 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "SUSPEND") == 0)
{
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (!lightdm_suspend (&error))
status_notify ("%s FAIL-SUSPEND", greeter_id);
- g_clear_error (&error);
}
else if (strcmp (name, "GET-CAN-HIBERNATE") == 0)
@@ -383,10 +358,9 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "HIBERNATE") == 0)
{
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (!lightdm_hibernate (&error))
status_notify ("%s FAIL-HIBERNATE", greeter_id);
- g_clear_error (&error);
}
else if (strcmp (name, "GET-CAN-RESTART") == 0)
@@ -397,10 +371,9 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "RESTART") == 0)
{
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (!lightdm_restart (&error))
status_notify ("%s FAIL-RESTART", greeter_id);
- g_clear_error (&error);
}
else if (strcmp (name, "GET-CAN-SHUTDOWN") == 0)
@@ -411,10 +384,9 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "SHUTDOWN") == 0)
{
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (!lightdm_shutdown (&error))
status_notify ("%s FAIL-SHUTDOWN", greeter_id);
- g_clear_error (&error);
}
}
@@ -434,12 +406,11 @@ static void
connect_finished (GObject *object, GAsyncResult *result, gpointer data)
{
LightDMGreeter *greeter = LIGHTDM_GREETER (object);
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (!lightdm_greeter_connect_to_daemon_finish (greeter, result, &error))
{
status_notify ("%s FAIL-CONNECT-DAEMON ERROR=%s", greeter_id, error->message);
- g_clear_error (&error);
exit_code = EXIT_FAILURE;
g_main_loop_quit (loop);
return;
@@ -453,8 +424,9 @@ connect_finished (GObject *object, GAsyncResult *result, gpointer data)
int
main (int argc, char **argv)
{
- gchar *display, *xdg_seat, *xdg_vtnr, *xdg_session_cookie, *xdg_session_class, *xdg_session_type, *mir_server_host_socket, *mir_vt, *mir_id, *path;
- GString *status_text;
+ 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 ();
@@ -505,12 +477,10 @@ main (int argc, char **argv)
if (mir_vt > 0)
g_string_append_printf (status_text, " MIR_SERVER_VT=%s", mir_vt);
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
config = g_key_file_new ();
path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL);
g_key_file_load_from_file (config, path, G_KEY_FILE_NONE, NULL);
- g_free (path);
if (g_key_file_get_boolean (config, "test-greeter-config", "exit-on-startup", NULL))
{
diff --git a/tests/src/test-qt-greeter.cpp b/tests/src/test-qt-greeter.cpp
index d43a9960..87edc729 100644
--- a/tests/src/test-qt-greeter.cpp
+++ b/tests/src/test-qt-greeter.cpp
@@ -250,7 +250,7 @@ int
main(int argc, char *argv[])
{
gchar *display, *xdg_seat, *xdg_vtnr, *xdg_session_cookie, *xdg_session_class;
- GString *status_text;
+ g_autoptr(GString) status_text = NULL;
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
@@ -294,7 +294,6 @@ main(int argc, char *argv[])
if (xdg_session_class)
g_string_append_printf (status_text, " XDG_SESSION_CLASS=%s", xdg_session_class);
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
config = new QSettings (g_build_filename (getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), QSettings::IniFormat);
diff --git a/tests/src/test-runner.c b/tests/src/test-runner.c
index 70d10a7a..4ad0bc13 100644
--- a/tests/src/test-runner.c
+++ b/tests/src/test-runner.c
@@ -321,12 +321,11 @@ get_script_line (const gchar *prefix)
/* Ignore lines with other prefixes */
if (prefix)
{
- gchar *p;
+ g_autofree gchar *p = NULL;
gboolean matches;
p = get_prefix (line->text);
matches = strcmp (prefix, p) == 0;
- g_free (p);
if (!matches)
continue;
@@ -349,74 +348,60 @@ stop_loop (gpointer user_data)
static void
switch_to_greeter_done_cb (GObject *bus, GAsyncResult *result, gpointer data)
{
- GVariant *r;
- GError *error = NULL;
+ g_autoptr(GVariant) r = NULL;
+ g_autoptr(GError) error = NULL;
r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error);
- if (error)
- g_warning ("Failed to switch to greeter: %s\n", error->message);
- g_clear_error (&error);
-
if (r)
- {
check_status ("RUNNER SWITCH-TO-GREETER");
- g_variant_unref (r);
- }
else
+ {
+ g_warning ("Failed to switch to greeter: %s\n", error->message);
check_status ("RUNNER SWITCH-TO-GREETER FAILED");
+ }
}
static void
switch_to_user_done_cb (GObject *bus, GAsyncResult *result, gpointer data)
{
- GVariant *r;
- GError *error = NULL;
- gchar *username = data, *status_text;
+ 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);
- if (error)
- g_warning ("Failed to switch to user: %s\n", error->message);
- g_clear_error (&error);
-
if (r)
- {
status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s", username);
- g_variant_unref (r);
- }
else
+ {
+ g_warning ("Failed to switch to user: %s\n", error->message);
status_text = g_strdup_printf ("RUNNER SWITCH-TO-USER USERNAME=%s FAILED", username);
+ }
check_status (status_text);
-
- g_free (status_text);
- g_free (username);
}
static void
switch_to_guest_done_cb (GObject *bus, GAsyncResult *result, gpointer data)
{
- GVariant *r;
- GError *error = NULL;
+ g_autoptr(GVariant) r = NULL;
+ g_autoptr(GError) error = NULL;
r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (bus), result, &error);
- if (error)
- g_warning ("Failed to switch to guest: %s\n", error->message);
- g_clear_error (&error);
-
if (r)
- {
check_status ("RUNNER SWITCH-TO-GUEST");
- g_variant_unref (r);
- }
else
+ {
+ g_warning ("Failed to switch to guest: %s\n", error->message);
check_status ("RUNNER SWITCH-TO-GUEST FAILED");
+ }
}
static void
handle_command (const gchar *command)
{
const gchar *c;
- gchar *name = NULL;
- GHashTable *params;
+ g_autofree gchar *name = NULL;
+ g_autoptr(GHashTable) params = NULL;
c = command;
while (*c && !isspace (*c))
@@ -447,7 +432,7 @@ handle_command (const gchar *command)
if (*c == '\"')
{
gboolean escaped = FALSE;
- GString *value;
+ g_autoptr(GString) value = NULL;
c++;
value = g_string_new ("");
@@ -469,8 +454,7 @@ handle_command (const gchar *command)
g_string_append_c (value, *c);
c++;
}
- param_value = value->str;
- g_string_free (value, FALSE);
+ param_value = g_strdup (value->str);
if (*c == '\"')
c++;
}
@@ -493,7 +477,7 @@ handle_command (const gchar *command)
GString *command_line;
gchar **lightdm_argv;
pid_t lightdm_pid;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
command_line = g_string_new ("lightdm");
if (getenv ("DEBUG"))
@@ -509,20 +493,19 @@ handle_command (const gchar *command)
g_warning ("Error parsing command line: %s", error->message);
quit (EXIT_FAILURE);
}
- g_clear_error (&error);
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);
quit (EXIT_FAILURE);
}
- g_clear_error (&error);
lightdm_process = watch_process (lightdm_pid);
check_status ("RUNNER DAEMON-START");
}
else if (strcmp (name, "WAIT") == 0)
{
+ g_autoptr(GMainLoop) loop = NULL;
const gchar *v;
int duration;
@@ -532,14 +515,13 @@ handle_command (const gchar *command)
status_timeout = 0;
/* Use a main loop so that our DBus functions are still responsive */
- GMainLoop *loop = g_main_loop_new (NULL, FALSE);
+ loop = g_main_loop_new (NULL, FALSE);
v = g_hash_table_lookup (params, "DURATION");
duration = v ? atoi (v) : 1;
if (duration < 1)
duration = 1;
g_timeout_add_seconds (duration, stop_loop, loop);
g_main_loop_run (loop);
- g_main_loop_unref (loop);
/* Restart status timeout */
status_timeout = g_timeout_add (status_timeout_ms, status_timeout_cb, NULL);
@@ -560,7 +542,7 @@ handle_command (const gchar *command)
}
else if (strcmp (name, "ADD-LOCAL-X-SEAT") == 0)
{
- GVariant *result;
+ g_autoptr(GVariant) result = NULL;
const gchar *v;
v = g_hash_table_lookup (params, "DISPLAY");
@@ -575,7 +557,6 @@ handle_command (const gchar *command)
G_MAXINT,
NULL,
NULL);
- g_variant_unref (result);
}
else if (strcmp (name, "UPDATE-SEAT") == 0)
{
@@ -588,7 +569,7 @@ handle_command (const gchar *command)
{
const gchar *v;
GVariantBuilder invalidated_properties;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
g_variant_builder_init (&invalidated_properties, G_VARIANT_TYPE_ARRAY);
@@ -612,16 +593,14 @@ handle_command (const gchar *command)
g_variant_builder_add (&invalidated_properties, "s", "ActiveSession");
}
- g_dbus_connection_emit_signal (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
- NULL,
- seat->path,
- "org.freedesktop.DBus.Properties",
- "PropertiesChanged",
- g_variant_new ("(sa{sv}as)", "org.freedesktop.login1.Seat", NULL, &invalidated_properties),
- &error);
- if (error)
+ if (!g_dbus_connection_emit_signal (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
+ NULL,
+ seat->path,
+ "org.freedesktop.DBus.Properties",
+ "PropertiesChanged",
+ g_variant_new ("(sa{sv}as)", "org.freedesktop.login1.Seat", NULL, &invalidated_properties),
+ &error))
g_warning ("Failed to emit PropertiesChanged: %s", error->message);
- g_clear_error (&error);
}
}
else if (strcmp (name, "REMOVE-SEAT") == 0)
@@ -632,9 +611,9 @@ handle_command (const gchar *command)
}
else if (strcmp (name, "LIST-SEATS") == 0)
{
- GVariant *result, *value;
- GString *status;
- GError *error = NULL;
+ 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",
@@ -651,6 +630,7 @@ handle_command (const gchar *command)
status = g_string_new ("RUNNER LIST-SEATS");
if (result)
{
+ g_autoptr(GVariant) value = NULL;
GVariantIter *iter;
const gchar *path;
int i = 0;
@@ -665,8 +645,6 @@ handle_command (const gchar *command)
g_string_append (status, path);
i++;
}
- g_variant_unref (value);
- g_variant_unref (result);
}
else
{
@@ -674,17 +652,15 @@ handle_command (const gchar *command)
g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN");
else
g_string_append_printf (status, " ERROR=%s", error->message);
- g_clear_error (&error);
}
check_status (status->str);
- g_string_free (status, TRUE);
}
else if (strcmp (name, "LIST-SESSIONS") == 0)
{
- GVariant *result, *value;
- GString *status;
- GError *error = NULL;
+ 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",
@@ -701,6 +677,7 @@ handle_command (const gchar *command)
status = g_string_new ("RUNNER LIST-SESSIONS");
if (result)
{
+ g_autoptr(GVariant) value = NULL;
GVariantIter *iter;
const gchar *path;
int i = 0;
@@ -716,8 +693,6 @@ handle_command (const gchar *command)
g_string_append (status, path);
i++;
}
- g_variant_unref (value);
- g_variant_unref (result);
}
else
{
@@ -725,17 +700,15 @@ handle_command (const gchar *command)
g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN");
else
g_string_append_printf (status, " ERROR=%s", error->message);
- g_clear_error (&error);
}
check_status (status->str);
- g_string_free (status, TRUE);
}
else if (strcmp (name, "SEAT-CAN-SWITCH") == 0)
{
- GVariant *result, *value;
- GString *status;
- GError *error = NULL;
+ 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",
@@ -752,10 +725,9 @@ handle_command (const gchar *command)
status = g_string_new ("RUNNER SEAT-CAN-SWITCH");
if (result)
{
+ g_autoptr(GVariant) value = NULL;
g_variant_get (result, "(v)", &value);
g_string_append_printf (status, " CAN-SWITCH=%s", g_variant_get_boolean (value) ? "TRUE" : "FALSE");
- g_variant_unref (value);
- g_variant_unref (result);
}
else
{
@@ -763,17 +735,15 @@ handle_command (const gchar *command)
g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN");
else
g_string_append_printf (status, " ERROR=%s", error->message);
- g_clear_error (&error);
}
check_status (status->str);
- g_string_free (status, TRUE);
}
else if (strcmp (name, "SEAT-HAS-GUEST-ACCOUNT") == 0)
{
- GVariant *result, *value;
- GString *status;
- GError *error = NULL;
+ 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",
@@ -790,10 +760,9 @@ handle_command (const gchar *command)
status = g_string_new ("RUNNER SEAT-HAS-GUEST-ACCOUNT");
if (result)
{
+ g_autoptr(GVariant) value = NULL;
g_variant_get (result, "(v)", &value);
g_string_append_printf (status, " HAS-GUEST-ACCOUNT=%s", g_variant_get_boolean (value) ? "TRUE" : "FALSE");
- g_variant_unref (value);
- g_variant_unref (result);
}
else
{
@@ -801,11 +770,9 @@ handle_command (const gchar *command)
g_string_append_printf (status, " ERROR=SERVICE_UNKNOWN");
else
g_string_append_printf (status, " ERROR=%s", error->message);
- g_clear_error (&error);
}
check_status (status->str);
- g_string_free (status, TRUE);
}
else if (strcmp (name, "SWITCH-TO-GREETER") == 0)
{
@@ -864,7 +831,7 @@ handle_command (const gchar *command)
gchar **argv;
GPid pid;
Process *process;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
xserver_args = g_hash_table_lookup (params, "ARGS");
if (!xserver_args)
@@ -889,7 +856,7 @@ handle_command (const gchar *command)
gchar **argv;
GPid pid;
Process *process;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
vnc_client_args = g_hash_table_lookup (params, "ARGS");
if (!vnc_client_args)
@@ -910,8 +877,9 @@ handle_command (const gchar *command)
}
else if (strcmp (name, "ADD-USER") == 0)
{
- gchar *status_text, *username;
+ 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);
@@ -922,14 +890,12 @@ handle_command (const gchar *command)
status_text = g_strdup_printf ("RUNNER ADD-USER USERNAME=%s", username);
check_status (status_text);
- g_free (status_text);
}
else if (strcmp (name, "UPDATE-USER") == 0)
{
- GString *status_text;
gchar *username;
AccountsUser *user;
- GError *error = NULL;
+ g_autoptr(GString) status_text = NULL;
status_text = g_string_new ("RUNNER UPDATE-USER USERNAME=");
@@ -938,6 +904,8 @@ handle_command (const gchar *command)
user = get_accounts_user_by_name (username);
if (user)
{
+ g_autoptr(GError) error = NULL;
+
if (g_hash_table_lookup (params, "NAME"))
{
user->user_name = g_strdup (g_hash_table_lookup (params, "NAME"));
@@ -985,28 +953,25 @@ handle_command (const gchar *command)
g_string_append_printf (status_text, " SESSION=%s", user->xsession);
}
- g_dbus_connection_emit_signal (accounts_connection,
- NULL,
- user->path,
- "org.freedesktop.Accounts.User",
- "Changed",
- g_variant_new ("()"),
- &error);
+ if (!g_dbus_connection_emit_signal (accounts_connection,
+ NULL,
+ user->path,
+ "org.freedesktop.Accounts.User",
+ "Changed",
+ g_variant_new ("()"),
+ &error))
+ g_warning ("Failed to emit Changed: %s", error->message);
}
else
g_warning ("Unknown user %s", username);
- if (error)
- g_warning ("Failed to emit Changed: %s", error->message);
- g_clear_error (&error);
-
check_status (status_text->str);
- g_string_free (status_text, TRUE);
}
else if (strcmp (name, "DELETE-USER") == 0)
{
- gchar *status_text, *username;
+ 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);
@@ -1017,12 +982,12 @@ handle_command (const gchar *command)
status_text = g_strdup_printf ("RUNNER DELETE-USER USERNAME=%s", username);
check_status (status_text);
- g_free (status_text);
}
else if (strcmp (name, "UNLOCK-SESSION") == 0)
{
- gchar *status_text, *id;
+ const gchar *id;
Login1Session *session;
+ g_autofree gchar *status_text = NULL;
id = g_hash_table_lookup (params, "SESSION");
session = find_login1_session (id);
@@ -1037,7 +1002,6 @@ handle_command (const gchar *command)
status_text = g_strdup_printf ("RUNNER UNLOCK-SESSION SESSION=%s", id);
check_status (status_text);
- g_free (status_text);
}
/* Forward to external processes */
else if (g_str_has_prefix (name, "SESSION-") ||
@@ -1052,13 +1016,12 @@ handle_command (const gchar *command)
{
StatusClient *client = link->data;
int length;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
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);
- g_clear_error (&error);
}
}
else
@@ -1066,9 +1029,6 @@ handle_command (const gchar *command)
g_printerr ("Unknown command '%s'\n", name);
quit (EXIT_FAILURE);
}
-
- g_free (name);
- g_hash_table_unref (params);
}
static void
@@ -1113,7 +1073,7 @@ check_status (const gchar *status)
{
ScriptLine *line;
gboolean result = FALSE;
- gchar *prefix;
+ g_autofree gchar *prefix = NULL;
if (stop)
return;
@@ -1126,12 +1086,10 @@ check_status (const gchar *status)
/* Try and match against expected */
prefix = get_prefix (status);
line = get_script_line (prefix);
- g_free (prefix);
if (line)
{
- gchar *full_pattern = g_strdup_printf ("^%s$", line->text);
+ g_autofree gchar *full_pattern = g_strdup_printf ("^%s$", line->text);
result = g_regex_match_simple (full_pattern, status, 0, 0);
- g_free (full_pattern);
}
if (!result)
@@ -1158,19 +1116,18 @@ status_message_cb (GSocket *socket, GIOCondition condition, StatusClient *client
int length;
gchar buffer[1024];
ssize_t n_read;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
if (n_read > 0)
n_read = g_socket_receive (socket, buffer, length, NULL, &error);
- if (error)
+ if (n_read < 0)
{
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED))
n_read = 0;
else
g_warning ("Error reading from socket: %s", error->message);
}
- g_clear_error (&error);
if (n_read == 0)
{
status_clients = g_list_remove (status_clients, client);
@@ -1191,12 +1148,9 @@ static gboolean
status_connect_cb (gpointer data)
{
GSocket *socket;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
socket = g_socket_accept (status_socket, NULL, &error);
- if (error)
- g_warning ("Failed to accept status connection: %s", error->message);
- g_clear_error (&error);
if (socket)
{
StatusClient *client;
@@ -1209,6 +1163,8 @@ status_connect_cb (gpointer data)
g_source_set_callback (client->source, (GSourceFunc) status_message_cb, client, NULL);
g_source_attach (client->source, NULL);
}
+ else
+ g_warning ("Failed to accept status connection: %s", error->message);
return TRUE;
}
@@ -1217,7 +1173,8 @@ static void
load_script (const gchar *filename)
{
int i;
- gchar *data, **lines;
+ g_autofree gchar *data = NULL;
+ g_auto(GStrv) lines = NULL;
if (!g_file_get_contents (filename, &data, NULL, NULL))
{
@@ -1226,7 +1183,6 @@ load_script (const gchar *filename)
}
lines = g_strsplit (data, "\n", -1);
- g_free (data);
/* Load lines with #? prefix as expected behaviour */
for (i = 0; lines[i]; i++)
@@ -1241,7 +1197,6 @@ load_script (const gchar *filename)
script = g_list_append (script, line);
}
}
- g_strfreev (lines);
}
static void
@@ -1300,25 +1255,22 @@ upower_name_acquired_cb (GDBusConnection *connection,
{
handle_upower_call,
};
- GDBusNodeInfo *upower_info;
- GError *error = NULL;
+ g_autoptr(GDBusNodeInfo) upower_info = NULL;
+ g_autoptr(GError) error = NULL;
upower_info = g_dbus_node_info_new_for_xml (upower_interface, &error);
- if (error)
- g_warning ("Failed to parse D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!upower_info)
+ {
+ g_warning ("Failed to parse D-Bus interface: %s", error->message);
return;
- g_dbus_connection_register_object (connection,
- "/org/freedesktop/UPower",
- upower_info->interfaces[0],
- &upower_vtable,
- NULL, NULL,
- &error);
- if (error)
+ }
+ if (g_dbus_connection_register_object (connection,
+ "/org/freedesktop/UPower",
+ upower_info->interfaces[0],
+ &upower_vtable,
+ NULL, NULL,
+ &error) == 0)
g_warning ("Failed to register UPower service: %s", error->message);
- g_clear_error (&error);
- g_dbus_node_info_unref (upower_info);
service_count--;
if (service_count == 0)
@@ -1343,11 +1295,11 @@ static CKSession *
open_ck_session (GDBusConnection *connection, GVariant *params)
{
CKSession *session;
- GString *cookie;
+ g_autoptr(GString) cookie = NULL;
GVariantIter *iter;
const gchar *name;
GVariant *value;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
session = g_malloc0 (sizeof (CKSession));
ck_sessions = g_list_append (ck_sessions, session);
@@ -1364,8 +1316,7 @@ open_ck_session (GDBusConnection *connection, GVariant *params)
}
}
- session->cookie = cookie->str;
- g_string_free (cookie, FALSE);
+ session->cookie = g_strdup (cookie->str);
session->path = g_strdup_printf ("/org/freedesktop/ConsoleKit/Session%d", ck_session_index++);
session->id = g_dbus_connection_register_object (connection,
session->path,
@@ -1374,9 +1325,8 @@ open_ck_session (GDBusConnection *connection, GVariant *params)
session,
NULL,
&error);
- if (error)
+ if (session->id == 0)
g_warning ("Failed to register CK Session: %s", error->message);
- g_clear_error (&error);
return session;
}
@@ -1500,9 +1450,8 @@ handle_ck_session_call (GDBusConnection *connection,
}
else if (strcmp (method_name, "Activate") == 0)
{
- gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie);
+ g_autofree gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie);
check_status (status);
- g_free (status);
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
}
@@ -1584,34 +1533,31 @@ ck_name_acquired_cb (GDBusConnection *connection,
" <method name='Activate'/>"
" </interface>"
"</node>";
- GDBusNodeInfo *ck_info;
- GError *error = NULL;
+ g_autoptr(GDBusNodeInfo) ck_info = NULL;
+ g_autoptr(GError) error = NULL;
ck_info = g_dbus_node_info_new_for_xml (ck_interface, &error);
- if (error)
- g_warning ("Failed to parse D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!ck_info)
+ {
+ g_warning ("Failed to parse D-Bus interface: %s", error->message);
return;
+ }
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
ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface, &error);
- if (error)
- g_warning ("Failed to parse D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!ck_session_info)
+ {
+ g_warning ("Failed to parse D-Bus interface: %s", error->message);
return;
- g_dbus_connection_register_object (connection,
- "/org/freedesktop/ConsoleKit/Manager",
- ck_info->interfaces[0],
- &ck_vtable,
- NULL, NULL,
- &error);
- if (error)
+ }
+ if (g_dbus_connection_register_object (connection,
+ "/org/freedesktop/ConsoleKit/Manager",
+ ck_info->interfaces[0],
+ &ck_vtable,
+ NULL, NULL,
+ &error) == 0)
g_warning ("Failed to register console kit service: %s", error->message);
- g_clear_error (&error);
- g_dbus_node_info_unref (ck_info);
service_count--;
if (service_count == 0)
@@ -1666,14 +1612,8 @@ handle_login1_seat_get_property (GDBusConnection *connection,
{
if (seat->active_session)
{
- gchar *path;
- GVariant *ret;
-
- path = g_strdup_printf ("/org/freedesktop/login1/session/%s", seat->active_session);
- ret = g_variant_new ("(so)", seat->active_session, path);
- g_free (path);
-
- return ret;
+ g_autofree gchar *path = g_strdup_printf ("/org/freedesktop/login1/session/%s", seat->active_session);
+ return g_variant_new ("(so)", seat->active_session, path);
}
else
return NULL;
@@ -1686,8 +1626,8 @@ static Login1Seat *
add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_signal)
{
Login1Seat *seat;
- GError *error = NULL;
- GDBusNodeInfo *login1_seat_info;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GDBusNodeInfo) login1_seat_info = NULL;
const gchar *login1_seat_interface =
"<node>"
@@ -1713,36 +1653,32 @@ add_login1_seat (GDBusConnection *connection, const gchar *id, gboolean emit_sig
seat->active_session = NULL;
login1_seat_info = g_dbus_node_info_new_for_xml (login1_seat_interface, &error);
- if (error)
- g_warning ("Failed to parse login1 seat D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!login1_seat_info)
+ {
+ g_warning ("Failed to parse login1 seat D-Bus interface: %s", error->message);
return NULL;
+ }
- g_dbus_connection_register_object (connection,
- seat->path,
- login1_seat_info->interfaces[0],
- &login1_seat_vtable,
- seat,
- NULL,
- &error);
- if (error)
+ if (g_dbus_connection_register_object (connection,
+ seat->path,
+ login1_seat_info->interfaces[0],
+ &login1_seat_vtable,
+ seat,
+ NULL,
+ &error) == 0)
g_warning ("Failed to register login1 seat: %s", error->message);
- g_clear_error (&error);
- g_dbus_node_info_unref (login1_seat_info);
if (emit_signal)
{
- g_dbus_connection_emit_signal (connection,
- NULL,
- "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- "SeatNew",
- g_variant_new ("(so)", seat->id, seat->path),
- &error);
- if (error)
- g_warning ("Failed to emit SeatNew: %s", error->message);
- g_clear_error (&error);
+ g_autoptr(GError) e = NULL;
+ if (!g_dbus_connection_emit_signal (connection,
+ NULL,
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ "SeatNew",
+ g_variant_new ("(so)", seat->id, seat->path),
+ &e))
+ g_warning ("Failed to emit SeatNew: %s", e->message);
}
return seat;
@@ -1768,22 +1704,20 @@ static void
remove_login1_seat (GDBusConnection *connection, const gchar *id)
{
Login1Seat *seat;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
seat = find_login1_seat (id);
if (!seat)
return;
- g_dbus_connection_emit_signal (connection,
- NULL,
- "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- "SeatRemoved",
- g_variant_new ("(so)", seat->id, seat->path),
- &error);
- if (error)
+ if (!g_dbus_connection_emit_signal (connection,
+ NULL,
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ "SeatRemoved",
+ g_variant_new ("(so)", seat->id, seat->path),
+ &error))
g_warning ("Failed to emit SeatNew: %s", error->message);
- g_clear_error (&error);
login1_seats = g_list_remove (login1_seats, seat);
g_free (seat->id);
@@ -1810,8 +1744,8 @@ static Login1Session *
create_login1_session (GDBusConnection *connection)
{
Login1Session *session;
- GError *error = NULL;
- GDBusNodeInfo *login1_session_info;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GDBusNodeInfo) login1_session_info = NULL;
const gchar *login1_session_interface =
"<node>"
@@ -1830,23 +1764,20 @@ create_login1_session (GDBusConnection *connection)
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);
- if (error)
- g_warning ("Failed to parse login1 session D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!login1_session_info)
+ {
+ g_warning ("Failed to parse login1 session D-Bus interface: %s", error->message);
return NULL;
+ }
- g_dbus_connection_register_object (connection,
- session->path,
- login1_session_info->interfaces[0],
- &login1_session_vtable,
- session,
- NULL,
- &error);
- if (error)
+ 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);
- g_clear_error (&error);
- g_dbus_node_info_unref (login1_session_info);
return session;
}
@@ -1912,9 +1843,8 @@ handle_login1_call (GDBusConnection *connection,
if (!session->locked)
{
- gchar *status = g_strdup_printf ("LOGIN1 LOCK-SESSION SESSION=%s", id);
+ g_autofree gchar *status = g_strdup_printf ("LOGIN1 LOCK-SESSION SESSION=%s", id);
check_status (status);
- g_free (status);
}
session->locked = TRUE;
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
@@ -1934,9 +1864,8 @@ handle_login1_call (GDBusConnection *connection,
if (session->locked)
{
- gchar *status = g_strdup_printf ("LOGIN1 UNLOCK-SESSION SESSION=%s", id);
+ g_autofree gchar *status = g_strdup_printf ("LOGIN1 UNLOCK-SESSION SESSION=%s", id);
check_status (status);
- g_free (status);
}
session->locked = FALSE;
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
@@ -1945,6 +1874,7 @@ handle_login1_call (GDBusConnection *connection,
{
const gchar *id;
Login1Session *session;
+ g_autofree gchar *status = NULL;
g_variant_get (parameters, "(&s)", &id);
session = find_login1_session (id);
@@ -1954,9 +1884,8 @@ handle_login1_call (GDBusConnection *connection,
return;
}
- gchar *status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", id);
+ status = g_strdup_printf ("LOGIN1 ACTIVATE-SESSION SESSION=%s", id);
check_status (status);
- g_free (status);
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
}
@@ -1975,9 +1904,8 @@ handle_login1_call (GDBusConnection *connection,
if (g_key_file_get_boolean (config, "test-runner-config", "log-login1-terminate", NULL))
{
- gchar *status = g_strdup_printf ("LOGIN1 TERMINATE-SESSION SESSION=%s", id);
+ g_autofree gchar *status = g_strdup_printf ("LOGIN1 TERMINATE-SESSION SESSION=%s", id);
check_status (status);
- g_free (status);
}
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
@@ -2097,26 +2025,23 @@ login1_name_acquired_cb (GDBusConnection *connection,
{
handle_login1_call,
};
- GDBusNodeInfo *login1_info;
+ g_autoptr(GDBusNodeInfo) login1_info = NULL;
Login1Seat *seat0;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
login1_info = g_dbus_node_info_new_for_xml (login1_interface, &error);
- if (error)
- g_warning ("Failed to parse login1 D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!login1_info)
+ {
+ g_warning ("Failed to parse login1 D-Bus interface: %s", error->message);
return;
- g_dbus_connection_register_object (connection,
- "/org/freedesktop/login1",
- login1_info->interfaces[0],
- &login1_vtable,
- NULL, NULL,
- &error);
- if (error)
+ }
+ if (g_dbus_connection_register_object (connection,
+ "/org/freedesktop/login1",
+ login1_info->interfaces[0],
+ &login1_vtable,
+ NULL, NULL,
+ &error) == 0)
g_warning ("Failed to register login1 service: %s", error->message);
- g_clear_error (&error);
- g_dbus_node_info_unref (login1_info);
/* We always have seat0 */
seat0 = add_login1_seat (connection, "seat0", FALSE);
@@ -2177,87 +2102,79 @@ get_accounts_user_by_name (const gchar *username)
static void
accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal)
{
- GError *error = NULL;
-
user->hidden = hidden;
if (user->hidden && user->id != 0)
{
+ g_autoptr(GError) error = NULL;
+
g_dbus_connection_unregister_object (accounts_connection, user->id);
- g_dbus_connection_emit_signal (accounts_connection,
- NULL,
- "/org/freedesktop/Accounts",
- "org.freedesktop.Accounts",
- "UserDeleted",
- g_variant_new ("(o)", user->path),
- &error);
- if (error)
+ if (!g_dbus_connection_emit_signal (accounts_connection,
+ NULL,
+ "/org/freedesktop/Accounts",
+ "org.freedesktop.Accounts",
+ "UserDeleted",
+ g_variant_new ("(o)", user->path),
+ &error))
g_warning ("Failed to emit UserDeleted: %s", error->message);
- g_clear_error (&error);
user->id = 0;
}
if (!user->hidden && user->id == 0)
{
+ g_autoptr(GError) register_error = NULL;
+ g_autoptr(GError) emit_error = NULL;
+
user->id = g_dbus_connection_register_object (accounts_connection,
user->path,
user_info->interfaces[0],
&user_vtable,
user,
NULL,
- &error);
- if (error)
- g_warning ("Failed to register user: %s", error->message);
- g_clear_error (&error);
+ &register_error);
+ if (user->id == 0)
+ g_warning ("Failed to register user: %s", register_error->message);
- g_dbus_connection_emit_signal (accounts_connection,
- NULL,
- "/org/freedesktop/Accounts",
- "org.freedesktop.Accounts",
- "UserAdded",
- g_variant_new ("(o)", user->path),
- &error);
- if (error)
- g_warning ("Failed to emit UserAdded: %s", error->message);
- g_clear_error (&error);
+ if (!g_dbus_connection_emit_signal (accounts_connection,
+ NULL,
+ "/org/freedesktop/Accounts",
+ "org.freedesktop.Accounts",
+ "UserAdded",
+ g_variant_new ("(o)", user->path),
+ &emit_error))
+ g_warning ("Failed to emit UserAdded: %s", emit_error->message);
}
}
static void
load_passwd_file (void)
{
- gchar *path, *data, **lines;
+ g_autofree gchar *path = NULL;
+ g_autofree gchar *data = NULL;
+ g_auto(GStrv) lines = NULL;
gchar **user_filter = NULL;
int i;
if (g_key_file_has_key (config, "test-runner-config", "accounts-service-user-filter", NULL))
{
- gchar *filter;
-
- filter = g_key_file_get_string (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);
- g_free (filter);
}
path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "etc", "passwd", NULL);
g_file_get_contents (path, &data, NULL, NULL);
- g_free (path);
lines = g_strsplit (data, "\n", -1);
- g_free (data);
for (i = 0; lines[i]; i++)
{
- gchar **fields;
+ g_auto(GStrv) fields = NULL;
guint uid;
- gchar *user_name, *real_name;
+ const gchar *user_name, *real_name;
AccountsUser *user = NULL;
fields = g_strsplit (lines[i], ":", -1);
if (fields == NULL || g_strv_length (fields) < 7)
- {
- g_strfreev (fields);
continue;
- }
user_name = fields[0];
uid = atoi (fields[2]);
@@ -2266,8 +2183,8 @@ load_passwd_file (void)
user = get_accounts_user_by_uid (uid);
if (!user)
{
- gchar *path;
- GKeyFile *dmrc_file;
+ g_autofree gchar *path = NULL;
+ g_autoptr(GKeyFile) dmrc_file = NULL;
user = g_malloc0 (sizeof (AccountsUser));
accounts_users = g_list_append (accounts_users, user);
@@ -2287,7 +2204,6 @@ load_passwd_file (void)
dmrc_file = g_key_file_new ();
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);
- g_free (path);
user->uid = uid;
user->user_name = g_strdup (user_name);
@@ -2312,14 +2228,8 @@ load_passwd_file (void)
user->has_messages = g_key_file_get_boolean (dmrc_file, "X-Accounts", "HasMessages", NULL);
user->path = g_strdup_printf ("/org/freedesktop/Accounts/User%d", uid);
accounts_user_set_hidden (user, user->hidden, FALSE);
-
- g_key_file_free (dmrc_file);
}
-
- g_strfreev (fields);
}
-
- g_strfreev (lines);
}
static void
@@ -2499,32 +2409,30 @@ accounts_name_acquired_cb (GDBusConnection *connection,
" <signal name='Changed' />"
" </interface>"
"</node>";
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
accounts_connection = connection;
accounts_info = g_dbus_node_info_new_for_xml (accounts_interface, &error);
- if (error)
- g_warning ("Failed to parse D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!accounts_info)
+ {
+ g_warning ("Failed to parse D-Bus interface: %s", error->message);
return;
+ }
user_info = g_dbus_node_info_new_for_xml (user_interface, &error);
- if (error)
- g_warning ("Failed to parse D-Bus interface: %s", error->message);
- g_clear_error (&error);
if (!user_info)
+ {
+ g_warning ("Failed to parse D-Bus interface: %s", error->message);
return;
- g_dbus_connection_register_object (connection,
- "/org/freedesktop/Accounts",
- accounts_info->interfaces[0],
- &accounts_vtable,
- NULL,
- NULL,
- &error);
- if (error)
+ }
+ if (g_dbus_connection_register_object (connection,
+ "/org/freedesktop/Accounts",
+ accounts_info->interfaces[0],
+ &accounts_vtable,
+ NULL,
+ NULL,
+ &error) == 0)
g_warning ("Failed to register accounts service: %s", error->message);
- g_clear_error (&error);
g_dbus_node_info_unref (accounts_info);
service_count--;
@@ -2570,7 +2478,7 @@ properties_changed_cb (GDBusConnection *connection,
gpointer user_data)
{
const gchar *interface, *name;
- GString *status;
+ g_autoptr(GString) status = NULL;
GVariant *value;
GVariantIter *changed_properties, *invalidated_properties;
int i;
@@ -2607,7 +2515,6 @@ properties_changed_cb (GDBusConnection *connection,
}
check_status (status->str);
- g_string_free (status, TRUE);
}
static void
@@ -2619,7 +2526,7 @@ dbus_signal_cb (GDBusConnection *connection,
GVariant *parameters,
gpointer user_data)
{
- GString *status;
+ g_autoptr(GString) status = NULL;
status = g_string_new ("RUNNER DBUS-SIGNAL");
g_string_append_printf (status, " PATH=%s", object_path);
@@ -2627,7 +2534,6 @@ dbus_signal_cb (GDBusConnection *connection,
g_string_append_printf (status, " NAME=%s", signal_name);
check_status (status->str);
- g_string_free (status, TRUE);
}
int
@@ -2635,12 +2541,27 @@ main (int argc, char **argv)
{
GMainLoop *loop;
int i;
- gchar *greeter = NULL, *script_name, *config_file, *additional_system_config;
- gchar *additional_config, *path, *path1, *path2, *ld_preload, *ld_library_path, *home_dir;
- GString *passwd_data, *group_data;
+ 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];
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
@@ -2661,7 +2582,6 @@ main (int argc, char **argv)
script_name = argv[1];
config_file = g_strdup_printf ("%s.conf", script_name);
config_path = g_build_filename (SRCDIR, "tests", "scripts", config_file, NULL);
- g_free (config_file);
config = g_key_file_new ();
g_key_file_load_from_file (config, config_path, G_KEY_FILE_NONE, NULL);
@@ -2684,35 +2604,26 @@ main (int argc, char **argv)
/* Override system calls */
ld_preload = g_build_filename (BUILDDIR, "tests", "src", ".libs", "libsystem.so", NULL);
g_setenv ("LD_PRELOAD", ld_preload, TRUE);
- g_free (ld_preload);
/* 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_setenv ("PATH", path, TRUE);
- g_free (path);
/* Use locally built libraries */
- path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", ".libs", NULL);
- path2 = g_build_filename (BUILDDIR, "liblightdm-qt", ".libs", NULL);
- ld_library_path = g_strdup_printf ("%s:%s", path1, path2);
- g_free (path1);
- g_free (path2);
+ 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_setenv ("LD_LIBRARY_PATH", ld_library_path, TRUE);
- g_free (ld_library_path);
- path1 = g_build_filename (BUILDDIR, "liblightdm-gobject", NULL);
- g_setenv ("GI_TYPELIB_PATH", path1, TRUE);
- g_free (path1);
+ 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;
while (TRUE) {
- gchar *name;
-
- name = g_strdup_printf (".r%d", i);
+ g_autofree gchar *name = g_strdup_printf (".r%d", i);
g_free (temp_dir);
temp_dir = g_build_filename ("/tmp", name, NULL);
- g_free (name);
if (!g_file_test (temp_dir, G_FILE_TEST_EXISTS))
break;
i++;
@@ -2725,35 +2636,19 @@ main (int argc, char **argv)
status_socket_name = g_build_filename (temp_dir, ".s", NULL);
unlink (status_socket_name);
status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
- if (error)
- g_warning ("Error creating status socket %s: %s", status_socket_name, error->message);
- g_clear_error (&error);
- if (status_socket)
- {
- GSocketAddress *address;
- gboolean result;
-
- address = g_unix_socket_address_new (status_socket_name);
- result = g_socket_bind (status_socket, address, FALSE, &error);
- g_object_unref (address);
- if (error)
- g_warning ("Error binding status socket %s: %s", status_socket_name, error->message);
- g_clear_error (&error);
- if (result)
- {
- result = g_socket_listen (status_socket, &error);
- if (error)
- g_warning ("Error listening on status socket %s: %s", status_socket_name, error->message);
- g_clear_error (&error);
- }
- if (!result)
- {
- g_object_unref (status_socket);
- status_socket = NULL;
- }
- }
if (!status_socket)
+ {
+ g_warning ("Error creating status socket %s: %s", status_socket_name, error->message);
quit (EXIT_FAILURE);
+ }
+
+ 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);
g_source_set_callback (status_source, status_connect_cb, NULL, NULL);
g_source_attach (status_source, NULL);
@@ -2781,7 +2676,7 @@ main (int argc, char **argv)
additional_system_config = g_key_file_get_string (config, "test-runner-config", "additional-system-config", NULL);
if (additional_system_config)
{
- gchar **files;
+ g_auto(GStrv) files = NULL;
g_mkdir_with_parents (g_strdup_printf ("%s/usr/share/lightdm/lightdm.conf.d", temp_dir), 0755);
@@ -2789,13 +2684,12 @@ main (int argc, char **argv)
for (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");
- g_strfreev (files);
}
additional_config = g_key_file_get_string (config, "test-runner-config", "additional-config", NULL);
if (additional_config)
{
- gchar **files;
+ g_auto(GStrv) files = NULL;
g_mkdir_with_parents (g_strdup_printf ("%s/etc/xdg/lightdm/lightdm.conf.d", temp_dir), 0755);
@@ -2803,25 +2697,23 @@ main (int argc, char **argv)
for (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");
- g_strfreev (files);
}
if (g_key_file_has_key (config, "test-runner-config", "shared-data-dirs", NULL))
{
- gchar *dir_string;
- gchar **dirs;
+ 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_free (dir_string);
for (i = 0; dirs[i]; i++)
{
- gchar **fields = g_strsplit (dirs[i], ":", -1);
+ g_auto(GStrv) fields = g_strsplit (dirs[i], ":", -1);
if (g_strv_length (fields) == 4)
{
- gchar *path = g_strdup_printf ("%s/var/lib/lightdm-data/%s", temp_dir, fields[0]);
+ g_autofree gchar *path = g_strdup_printf ("%s/var/lib/lightdm-data/%s", temp_dir, fields[0]);
int uid = g_ascii_strtoll (fields[1], NULL, 10);
int gid = g_ascii_strtoll (fields[2], NULL, 10);
int mode = g_ascii_strtoll (fields[3], NULL, 8);
@@ -2829,12 +2721,8 @@ main (int argc, char **argv)
g_chmod (path, mode); /* mkdir filters by umask, so make sure we have what we want */
if (chown (path, uid, gid) < 0)
g_warning ("chown (%s) failed: %s", path, strerror (errno));
- g_free (path);
}
- g_strfreev (fields);
}
-
- g_strfreev (dirs);
}
/* Always copy the script */
@@ -2850,15 +2738,13 @@ main (int argc, char **argv)
perror ("Failed to copy greeters");
/* Set up the default greeter */
- path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL);
+ greeter_path = g_build_filename (temp_dir, "usr", "share", "lightdm", "greeters", "default.desktop", NULL);
greeter = g_strdup_printf ("%s.desktop", argv[2]);
- if (symlink (greeter, path) < 0)
+ if (symlink (greeter, greeter_path) < 0)
{
- g_printerr ("Failed to make greeter symlink %s->%s: %s\n", path, greeter, strerror (errno));
+ g_printerr ("Failed to make greeter symlink %s->%s: %s\n", greeter_path, greeter, strerror (errno));
quit (EXIT_FAILURE);
}
- g_free (path);
- g_free (greeter);
home_dir = g_build_filename (temp_dir, "home", NULL);
@@ -2942,16 +2828,15 @@ main (int argc, char **argv)
group_data = g_string_new ("");
for (i = 0; users[i].user_name; i++)
{
- GKeyFile *dmrc_file;
+ 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)
{
- path = g_build_filename (home_dir, users[i].user_name, NULL);
+ g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, NULL);
g_mkdir_with_parents (path, 0755);
if (chown (path, users[i].uid, users[i].uid) < 0)
g_debug ("chown (%s) failed: %s", path, strerror (errno));
- g_free (path);
}
dmrc_file = g_key_file_new ();
@@ -2979,26 +2864,19 @@ main (int argc, char **argv)
if (save_dmrc)
{
- gchar *data;
-
- path = g_build_filename (home_dir, users[i].user_name, ".dmrc", NULL);
- data = g_key_file_to_data (dmrc_file, NULL, NULL);
+ g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, ".dmrc", NULL);
+ g_autofree gchar *data = g_key_file_to_data (dmrc_file, NULL, NULL);
g_file_set_contents (path, data, -1, NULL);
- g_free (data);
- g_free (path);
}
- g_key_file_free (dmrc_file);
-
/* Write corrupt X authority file */
if (strcmp (users[i].user_name, "corrupt-xauth") == 0)
{
gchar data[1] = { 0xFF };
- path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL);
+ g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, ".Xauthority", NULL);
g_file_set_contents (path, data, 1, NULL);
chmod (path, S_IRUSR | S_IWUSR);
- g_free (path);
}
/* Add passwd file entry */
@@ -3007,18 +2885,14 @@ 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);
}
- path = g_build_filename (temp_dir, "etc", "passwd", NULL);
- g_file_set_contents (path, passwd_data->str, -1, NULL);
- g_free (path);
- g_string_free (passwd_data, TRUE);
+ 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");
- path = g_build_filename (temp_dir, "etc", "group", NULL);
- g_file_set_contents (path, group_data->str, -1, NULL);
- g_free (path);
- g_string_free (group_data, TRUE);
+ 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))
status_timeout_ms = g_key_file_get_integer (config, "test-runner-config", "timeout", NULL) * 1000;
diff --git a/tests/src/test-script-hook.c b/tests/src/test-script-hook.c
index e0d72084..abaf8543 100644
--- a/tests/src/test-script-hook.c
+++ b/tests/src/test-script-hook.c
@@ -9,7 +9,7 @@ static GKeyFile *config;
int
main (int argc, char **argv)
{
- GString *status_text;
+ g_autoptr(GString) status_text = NULL;
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
@@ -31,7 +31,6 @@ main (int argc, char **argv)
if (g_getenv ("USER"))
g_string_append_printf (status_text, " USER=%s", g_getenv ("USER"));
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
if (argc > 2)
return atoi (argv[2]);
diff --git a/tests/src/test-session.c b/tests/src/test-session.c
index 8323b2fc..0e368164 100644
--- a/tests/src/test-session.c
+++ b/tests/src/test-session.c
@@ -72,8 +72,6 @@ authentication_complete_cb (LightDMGreeter *greeter)
static void
request_cb (const gchar *name, GHashTable *params)
{
- GError *error = NULL;
-
if (!name)
{
g_main_loop_quit (loop);
@@ -122,7 +120,7 @@ request_cb (const gchar *name, GHashTable *params)
{
int n_groups, i;
gid_t *groups;
- GString *group_list;
+ g_autoptr(GString) group_list = NULL;
n_groups = getgroups (0, NULL);
if (n_groups < 0)
@@ -146,7 +144,6 @@ request_cb (const gchar *name, GHashTable *params)
g_string_append_printf (group_list, "%d", groups[i]);
}
status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
- g_string_free (group_list, TRUE);
free (groups);
}
@@ -166,15 +163,13 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "READ") == 0)
{
const gchar *name = g_hash_table_lookup (params, "FILE");
- gchar *contents = NULL;
- GError *error = NULL;
+ 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
status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
- g_free (contents);
- g_clear_error (&error);
}
else if (strcmp (name, "LIST-UNKNOWN-FILE-DESCRIPTORS") == 0)
@@ -182,16 +177,15 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "CHECK-X-AUTHORITY") == 0)
{
- gchar *xauthority;
+ g_autofree gchar *xauthority = NULL;
GStatBuf file_info;
- GString *mode_string;
+ g_autoptr(GString) mode_string = NULL;
xauthority = g_strdup (g_getenv ("XAUTHORITY"));
if (!xauthority)
xauthority = g_build_filename (g_get_home_dir (), ".Xauthority", NULL);
g_stat (xauthority, &file_info);
- g_free (xauthority);
mode_string = g_string_new ("");
g_string_append_c (mode_string, file_info.st_mode & S_IRUSR ? 'r' : '-');
@@ -204,18 +198,17 @@ request_cb (const gchar *name, GHashTable *params)
g_string_append_c (mode_string, file_info.st_mode & S_IWOTH ? 'w' : '-');
g_string_append_c (mode_string, file_info.st_mode & S_IXOTH ? 'x' : '-');
status_notify ("%s CHECK-X-AUTHORITY MODE=%s", session_id, mode_string->str);
- g_string_free (mode_string, TRUE);
}
else if (strcmp (name, "WRITE-SHARED-DATA") == 0)
{
const gchar *data = g_hash_table_lookup (params, "DATA");
- gchar *dir;
+ const gchar *dir;
dir = getenv ("XDG_GREETER_DATA_DIR");
if (dir)
{
- gchar *path;
+ g_autofree gchar *path = NULL;
FILE *f;
path = g_build_filename (dir, "data", NULL);
@@ -226,7 +219,6 @@ request_cb (const gchar *name, GHashTable *params)
if (f)
fclose (f);
- g_free (path);
}
else
status_notify ("%s WRITE-SHARED-DATA ERROR=NO_XDG_GREETER_DATA_DIR", session_id);
@@ -234,23 +226,20 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "READ-SHARED-DATA") == 0)
{
- gchar *dir;
+ const gchar *dir;
dir = getenv ("XDG_GREETER_DATA_DIR");
if (dir)
{
- gchar *path;
- gchar *contents = NULL;
- GError *error = NULL;
+ g_autofree gchar *path = 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
status_notify ("%s WRITE-SHARED-DATA ERROR=%s", session_id, error->message);
- g_free (path);
- g_free (contents);
- g_clear_error (&error);
}
else
status_notify ("%s WRITE-SHARED-DATA ERROR=NO_XDG_GREETER_DATA_DIR", session_id);
@@ -259,7 +248,7 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "GREETER-START") == 0)
{
int timeout;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
g_assert (greeter == NULL);
greeter = lightdm_greeter_new ();
@@ -269,10 +258,7 @@ request_cb (const gchar *name, GHashTable *params)
if (lightdm_greeter_connect_to_daemon_sync (greeter, &error))
status_notify ("%s GREETER-STARTED", session_id);
else
- {
status_notify ("%s GREETER-FAILED ERROR=%s", session_id, error->message);
- g_clear_error (&error);
- }
if (lightdm_greeter_get_select_user_hint (greeter))
status_notify ("%s GREETER-SELECT-USER-HINT USERNAME=%s", session_id, lightdm_greeter_get_select_user_hint (greeter));
@@ -310,29 +296,26 @@ request_cb (const gchar *name, GHashTable *params)
else if (strcmp (name, "GREETER-AUTHENTICATE") == 0)
{
+ g_autoptr(GError) error = NULL;
+
if (!lightdm_greeter_authenticate (greeter, g_hash_table_lookup (params, "USERNAME"), &error))
- {
status_notify ("%s FAIL-AUTHENTICATE ERROR=%s", session_id, error->message);
- g_clear_error (&error);
- }
}
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);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "GREETER-START-SESSION") == 0)
{
+ g_autoptr(GError) error = NULL;
+
if (!lightdm_greeter_start_session_sync (greeter, g_hash_table_lookup (params, "SESSION"), &error))
- {
status_notify ("%s FAIL-START-SESSION ERROR=%s", session_id, error->message);
- g_clear_error (&error);
- }
}
else if (strcmp (name, "GREETER-STOP") == 0)
@@ -346,7 +329,7 @@ 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;
- GString *status_text;
+ g_autoptr(GString) status_text = NULL;
int fd, open_max;
display = getenv ("DISPLAY");
@@ -422,7 +405,6 @@ main (int argc, char **argv)
g_string_append_printf (status_text, " NAME=%s", argv[1]);
g_string_append_printf (status_text, " USER=%s", getenv ("USER"));
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
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);
diff --git a/tests/src/unity-system-compositor.c b/tests/src/unity-system-compositor.c
index f138aaa3..49da86d0 100644
--- a/tests/src/unity-system-compositor.c
+++ b/tests/src/unity-system-compositor.c
@@ -71,7 +71,7 @@ read_message_cb (GIOChannel *channel, GIOCondition condition, gpointer data)
gsize n_read;
guint16 id;
guint16 payload_length;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (g_io_channel_read_chars (channel, header, 4, &n_read, &error) != G_IO_STATUS_NORMAL)
{
@@ -144,7 +144,7 @@ int
main (int argc, char **argv)
{
int i;
- GString *status_text;
+ g_autoptr(GString) status_text = NULL;
gboolean test = FALSE, container = FALSE;
int vt_number = -1;
const gchar *file = NULL;
@@ -206,7 +206,6 @@ main (int argc, char **argv)
if (container)
g_string_append (status_text, " CONTAINER=TRUE");
status_notify ("%s", status_text->str);
- g_string_free (status_text, TRUE);
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);
diff --git a/tests/src/vnc-client.c b/tests/src/vnc-client.c
index f70cd568..be94315b 100644
--- a/tests/src/vnc-client.c
+++ b/tests/src/vnc-client.c
@@ -12,9 +12,9 @@ static GKeyFile *config;
int
main (int argc, char **argv)
{
- GError *error = NULL;
- GSocket *socket;
- GSocketAddress *address;
+ 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;
@@ -33,27 +33,26 @@ 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);
- if (error)
- g_warning ("Unable to make VNC socket: %s", error->message);
- g_clear_error (&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_object_unref (address);
- if (error)
- g_warning ("Unable to connect VNC socket: %s", error->message);
- g_clear_error (&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);
- if (error)
- g_warning ("Unable to receive on VNC socket: %s", error->message);
- g_clear_error (&error);
if (n_read <= 0)
+ {
+ g_warning ("Unable to receive on VNC socket: %s", error->message);
return EXIT_FAILURE;
+ }
buffer[n_read] = '\0';
if (g_str_has_suffix (buffer, "\n"))
@@ -62,11 +61,11 @@ main (int argc, char **argv)
snprintf (buffer, 1024, "RFB 003.003\n");
n_sent = g_socket_send (socket, buffer, strlen (buffer), NULL, &error);
- if (error)
- g_warning ("Unable to send on VNC socket: %s", error->message);
- g_clear_error (&error);
if (n_sent != strlen (buffer))
+ {
+ g_warning ("Unable to send on VNC socket: %s", error->message);
return EXIT_FAILURE;
+ }
return EXIT_SUCCESS;
}
diff --git a/tests/src/x-server.c b/tests/src/x-server.c
index d756c58c..57ec6ecf 100644
--- a/tests/src/x-server.c
+++ b/tests/src/x-server.c
@@ -51,25 +51,19 @@ static guint x_client_signals[X_CLIENT_LAST_SIGNAL] = { 0 };
void
x_client_send_failed (XClient *client, const gchar *reason)
{
- gchar *message;
-
- message = g_strdup_printf ("FAILED:%s", reason);
+ g_autofree gchar *message = g_strdup_printf ("FAILED:%s", reason);
errno = 0;
if (send (g_io_channel_unix_get_fd (client->priv->channel), message, strlen (message), 0) != strlen (message))
g_printerr ("Failed to send FAILED: %s\n", strerror (errno));
- g_free (message);
}
void
x_client_send_success (XClient *client)
{
- gchar *message;
-
- message = g_strdup ("SUCCESS");
+ g_autofree gchar *message = g_strdup ("SUCCESS");
errno = 0;
if (send (g_io_channel_unix_get_fd (client->priv->channel), message, strlen (message), 0) != strlen (message))
g_printerr ("Failed to send SUCCESS: %s\n", strerror (errno));
- g_free (message);
}
void
@@ -121,12 +115,11 @@ socket_connect_cb (GIOChannel *channel, GIOCondition condition, gpointer data)
XServer *server = data;
GSocket *data_socket;
XClient *client;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
data_socket = g_socket_accept (server->priv->socket, NULL, &error);
if (error)
g_warning ("Error accepting connection: %s", strerror (errno));
- g_clear_error (&error);
if (!data_socket)
return FALSE;
@@ -145,12 +138,11 @@ socket_connect_cb (GIOChannel *channel, GIOCondition condition, gpointer data)
gboolean
x_server_start (XServer *server)
{
- gchar *name;
- GError *error = NULL;
+ g_autofree gchar *name = NULL;
+ g_autoptr(GError) error = NULL;
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_free (name);
server->priv->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
if (!server->priv->socket ||
diff --git a/tests/src/xdmcp-client.c b/tests/src/xdmcp-client.c
index 84b0b599..275cdc4d 100644
--- a/tests/src/xdmcp-client.c
+++ b/tests/src/xdmcp-client.c
@@ -57,14 +57,13 @@ static void
xdmcp_write (XDMCPClient *client, const guint8 *buffer, gssize buffer_length)
{
gssize n_written;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
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)
g_warning ("Partial write for XDMCP request, wrote %zi, expected %zi", n_written, buffer_length);
- g_clear_error (&error);
}
static void
@@ -292,8 +291,7 @@ xdmcp_client_start (XDMCPClient *client)
{
GSocketConnectable *address;
GSocketAddressEnumerator *enumerator;
- gboolean result;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
if (client->priv->socket)
return TRUE;
@@ -301,41 +299,32 @@ xdmcp_client_start (XDMCPClient *client)
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);
- g_clear_error (&error);
if (!client->priv->socket)
return FALSE;
address = g_network_address_new (client->priv->host, client->priv->port);
enumerator = g_socket_connectable_enumerate (address);
- result = FALSE;
while (TRUE)
{
- GSocketAddress *socket_address;
- GError *e = NULL;
+ g_autoptr(GSocketAddress) socket_address = NULL;
+ g_autoptr(GError) e = NULL;
socket_address = g_socket_address_enumerator_next (enumerator, NULL, &e);
if (e)
g_warning ("Failed to get socket address: %s", e->message);
- g_clear_error (&e);
if (!socket_address)
- break;
+ return FALSE;
- result = g_socket_connect (client->priv->socket, socket_address, NULL, error ? NULL : &error);
- g_object_unref (socket_address);
- if (result)
+ if (!g_socket_connect (client->priv->socket, socket_address, NULL, &e))
{
- g_clear_error (&error);
- break;
+ g_warning ("Unable to connect XDMCP socket: %s", error->message);
+ continue;
}
- }
- if (error)
- g_warning ("Unable to connect XDMCP socket: %s", error->message);
- if (!result)
- return FALSE;
- g_io_add_watch (g_io_channel_unix_new (g_socket_get_fd (client->priv->socket)), G_IO_IN, xdmcp_data_cb, client);
+ g_io_add_watch (g_io_channel_unix_new (g_socket_get_fd (client->priv->socket)), G_IO_IN, xdmcp_data_cb, client);
- return TRUE;
+ return TRUE;
+ }
}
GInetAddress *
@@ -500,11 +489,10 @@ static void
xdmcp_client_finalize (GObject *object)
{
XDMCPClient *client = (XDMCPClient *) object;
- g_free (client->priv->host);
- if (client->priv->socket)
- g_object_unref (client->priv->socket);
- g_free (client->priv->authorization_name);
- g_free (client->priv->authorization_data);
+ g_clear_pointer (&client->priv->host, g_free);
+ g_clear_object (&client->priv->socket);
+ g_clear_pointer (&client->priv->authorization_name, g_free);
+ g_clear_pointer (&client->priv->authorization_data, g_free);
}
static void