summaryrefslogtreecommitdiff
path: root/control_service.c
blob: 1dca7a9ce1a7ea82f77f345096985059a479ee1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "control_service.h"
#include "control_commands.h"
#include "conf.h"

#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>

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;

	while((size = g_socket_receive(socket, buffer, 0x400, NULL, NULL)) > 0) {
		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);
}