#include "control_service.h" #include "control_commands.h" #include "conf.h" #include #include #include #include static GSocketService *ss = NULL; static GSocketAddress *address; static gboolean service_incoming(GSocketService *service, GSocketConnection *connection, GObject *source_object, gpointer user_data) { g_debug("local service got incoming connection"); GSocket *socket = g_socket_connection_get_socket(connection); gchar buffer[0x400]; gssize size = g_socket_receive(socket, buffer, 0x400, NULL, NULL); gchar *pos = g_strstr_len(buffer, size, "\r"); if(pos == NULL) { pos = g_strstr_len(buffer, size, "\n"); } if(pos == NULL) { g_warning("EOL not found"); return FALSE; } *pos = '\0'; control_commands_handle(connection, buffer); return FALSE; } gboolean control_service_start() { gchar *sockname = conf_get_string("audist", "control_socket"); if(sockname == NULL) { g_warning("control_socket not set in config, can't start control service"); return FALSE; } address = g_unix_socket_address_new(sockname); ss = g_threaded_socket_service_new(2); if(g_socket_listener_add_address((GSocketListener*)ss, address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, NULL, NULL) == FALSE) { g_error("g_socket_listener_add_socket() failed"); } g_signal_connect(ss, "incoming", (GCallback)service_incoming, NULL); g_socket_service_start((GSocketService*)ss); return TRUE; } void control_service_stop() { g_socket_service_stop((GSocketService*)ss); g_unlink(g_unix_socket_address_get_path((GUnixSocketAddress*)address)); g_object_unref(ss); g_object_unref(address); }