6 #include <gio/gunixsocketaddress.h>
12 static GSocket *status_socket = NULL;
13 static StatusRequestFunc request_func = NULL;
14 static gchar *filter_id = NULL;
17 status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
22 const gchar *c, *start;
24 gchar *id, *name = NULL;
29 n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
31 n_read = g_socket_receive (socket, buffer, length, NULL, &error);
34 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED))
37 g_warning ("Error reading from socket: %s", error->message);
39 g_clear_error (&error);
43 request_func (NULL, NULL);
47 if (n_read <= 0 || !request_func)
50 buffer[n_read] = '\0';
54 while (*c && !isspace (*c))
59 id = g_strdup_printf ("%.*s", l, start);
60 id_matches = g_strcmp0 (id, filter_id) == 0;
69 while (*c && !isspace (*c))
74 name = g_strdup_printf ("%.*s", l, start);
76 params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
80 gchar *param_name, *param_value;
85 while (*c && !isspace (*c) && *c != '=')
90 param_name = g_strdup_printf ("%.*s", (int) (c - start), start);
99 gboolean escaped = FALSE;
103 value = g_string_new ("");
110 g_string_append_c (value, '\\');
116 else if (!escaped && *c == '\"')
119 g_string_append_c (value, *c);
122 param_value = value->str;
123 g_string_free (value, FALSE);
130 while (*c && !isspace (*c))
132 param_value = g_strdup_printf ("%.*s", (int) (c - start), start);
136 param_value = g_strdup ("");
138 g_hash_table_insert (params, param_name, param_value);
141 request_func (name, params);
144 g_hash_table_unref (params);
150 status_connect (StatusRequestFunc request_cb, const gchar *id)
153 GSocketAddress *address;
156 GError *error = NULL;
158 request_func = request_cb;
159 filter_id = g_strdup (id);
161 status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
163 g_printerr ("Unable to open socket for status: %s\n", error->message);
164 g_clear_error (&error);
168 path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".s", NULL);
169 address = g_unix_socket_address_new (path);
170 result = g_socket_connect (status_socket, address, NULL, &error);
171 g_object_unref (address);
173 g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message);
174 g_clear_error (&error);
178 g_object_unref (status_socket);
179 status_socket = NULL;
183 source = g_socket_create_source (status_socket, G_IO_IN, NULL);
184 g_source_set_callback (source, (GSourceFunc) status_request_cb, NULL, NULL);
185 g_source_attach (source, NULL);
191 status_notify (const gchar *format, ...)
193 gboolean written = FALSE;
198 va_start (ap, format);
199 vsnprintf (status, 1024, format, ap);
204 GError *error = NULL;
207 length = strlen (status);
208 written = g_socket_send (status_socket, (gchar *) &length, sizeof (length), NULL, &error) == sizeof (length) &&
209 g_socket_send (status_socket, status, strlen (status), NULL, &error) == strlen (status);
211 g_printerr ("Failed to write to status socket: %s\n", error->message);
212 g_clear_error (&error);
216 g_printerr ("%s\n", status);