summaryrefslogtreecommitdiff
path: root/src/local.c
blob: 74bf53cb4efe448200644136f78acff7c96f1086 (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
/**
 * @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"

#ifdef LOCALSOCK

static int sd;

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

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

	if (rc < 0) {
		if (errno == EAGAIN || errno == EINTR)
			return;
		FD_LOG(0, "recv() failed: %m\n");
		return;
	} else if (rc == 1) {
		switch (msg) {
		case LINPICKER_LOCAL_DUMP_VIEWS:
			view_debug_dump();
			break;
		default:
			break;
		}
	}
}

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