Back to index

lightdm  1.3.2
status.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <glib.h>
00004 #include <gio/gio.h>
00005 #include <gio/gunixsocketaddress.h>
00006 
00007 #include "status.h"
00008 
00009 static GSocket *status_socket = NULL;
00010 static StatusRequestFunc request_func = NULL;
00011 
00012 static gboolean
00013 status_request_cb (GSocket *socket, GIOCondition condition, gpointer data)
00014 {
00015     int length;
00016     gchar buffer[1024];
00017     ssize_t n_read;
00018     GError *error = NULL;  
00019 
00020     n_read = g_socket_receive (socket, (gchar *)&length, sizeof (length), NULL, &error);
00021     if (n_read > 0)
00022         n_read = g_socket_receive (socket, buffer, length, NULL, &error);
00023     if (n_read == 0)
00024         return FALSE;
00025     if (error)
00026         g_warning ("Error reading from socket: %s", error->message);
00027     g_clear_error (&error);
00028 
00029     if (n_read > 0 && request_func)
00030     {
00031         buffer[n_read] = '\0';
00032         request_func (buffer);
00033     }
00034 
00035     return TRUE;
00036 }
00037 
00038 void
00039 status_connect (StatusRequestFunc request_cb)
00040 {
00041     gchar *path;
00042     GSocketAddress *address;
00043     gboolean result;
00044     GSource *source;
00045     GError *error = NULL;
00046 
00047     request_func = request_cb;
00048 
00049     status_socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
00050     if (error)
00051         g_printerr ("Unable to open socket for status: %s\n", error->message);
00052     g_clear_error (&error);
00053     if (!status_socket)
00054         return;
00055 
00056     path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), ".status-socket", NULL);
00057     address = g_unix_socket_address_new (path);
00058     result = g_socket_connect (status_socket, address, NULL, &error);
00059     g_object_unref (address);
00060     if (error)
00061         g_printerr ("Failed to connect to status socket %s: %s\n", path, error->message);
00062     g_clear_error (&error);
00063     g_free (path);
00064     if (!result)
00065         return;
00066 
00067     source = g_socket_create_source (status_socket, G_IO_IN, NULL);
00068     g_source_set_callback (source, (GSourceFunc) status_request_cb, NULL, NULL);
00069     g_source_attach (source, NULL);   
00070 }
00071 
00072 void
00073 status_notify (const gchar *format, ...)
00074 {
00075     gchar status[1024];
00076     va_list ap;
00077 
00078     va_start (ap, format);
00079     vsnprintf (status, 1024, format, ap);
00080     va_end (ap);
00081 
00082     if (status_socket)
00083     {
00084         GError *error = NULL;
00085         int length;
00086 
00087         length = strlen (status);
00088         g_socket_send (status_socket, (gchar *) &length, sizeof (length), NULL, &error);
00089         g_socket_send (status_socket, status, strlen (status), NULL, &error);
00090         if (error)
00091             g_printerr ("Failed to write to status socket: %s\n", error->message);
00092         g_clear_error (&error);
00093     }
00094     else
00095         g_printerr ("%s\n", status);
00096 }