summaryrefslogtreecommitdiff
path: root/src/local.c
blob: 36f1bbe639019910a434118d49790625744e1cc8 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * @file
 * @section AUTHORS
 *
 *  Authors:
 *       Eamon Walsh <ewalsh@tycho.nsa.gov>
 *
 * @section LICENSE
 *
 * This file is in the public domain.
 *
 * @section DESCRIPTION
 *
 * This is an optional part of linpicker-server that enables a debugging
 * interface over a local socket if the "enable-socket" configure-time option
 * is set (default: disabled).
 *
 * Currently this interface is for debugging (for example dumping the current
 * list of views), however this interface might find use in the future as a
 * way to control Linpicker programmatically.  For example commands could be
 * implemented to list the current guests and switch between them, display a
 * notification icon, etc.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "config.h"
#include "local.h"
#include "fd.h"
#include "view.h"
#include "sak.h"

#ifdef LOCALSOCK

static int sd;

static void
local_dump_views(void)
{
	view_debug_dump();
}

static void
local_set_sak(struct local_message *msg)
{
	struct buffer *b;

	b = buffer_lookup(msg->param1, 0);
	if (b)
		sak_register(msg->param2, b->bg_view->display);
}

static void
local_process(void *closure)
{
	struct local_message msg;
	int rc;

	rc = recv(sd, &msg, sizeof(msg), MSG_DONTWAIT);

	if (rc < 0) {
		if (errno == EAGAIN || errno == EINTR)
			return;
		FD_LOG(0, "recv() failed: %m\n");
		return;
	}
	else if (rc == sizeof(msg)) {
		switch (msg.type) {
		case LINPICKER_LOCAL_DUMP_VIEWS:
			local_dump_views();
			break;
		case LINPICKER_LOCAL_SET_SAK:
			local_set_sak(&msg);
			break;
		default:
			break;
		}
	}
	else
		FD_LOG(1, "Warning: partial local_message dropped\n");
}

int
local_init(int argc, char **argv)
{
	struct sockaddr_un addr;
	int rc;

	/* Listen on a local socket */
	sd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (sd < 0) {
		FD_LOG(0, "socket() failed: %m\n");
		return -1;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	memcpy(addr.sun_path, LINPICKER_SOCKNAME, sizeof(LINPICKER_SOCKNAME));

	rc = bind(sd, (struct sockaddr *)&addr, sizeof(addr));
	if (rc < 0) {
		FD_LOG(0, "bind() failed: %m\n");
		return -1;
	}

	return fd_set_handler(sd, local_process, NULL);
}

#else

int
local_init(int argc, char **argv)
{
	return 0;
}

#endif