aboutsummaryrefslogtreecommitdiff
path: root/alarmd/alarmd.c
blob: 42dd5e43148c74e32487965de8ae5aed330c9d5a (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* FIXME bother removing _GNU_SOURCE? It only gives struct ucred, which
 * is linux-only anyway */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h>
#include <uuid/uuid.h>
#include <sys/un.h>
#include <uthash.h>
#include <pthread.h>

#include "alarmd_lock.h"
#include "alarmd_proto.h"

struct alarm {
	UT_hash_handle hh;
	uuid_t uuid;
	char *name;
	bool is_raised;
	pid_t owner;
//	time_t last_change;
};

static struct alarm *alarms;

int8_t read_string(int sock, unsigned char (*buffer)[128])
{
	uint8_t length = 0;
	if (recv(sock, &length, sizeof(uint8_t), 0) < sizeof(uint8_t)) {
		return -1;
	}
	if (length >= 128) {
		return -1;
	}
	if (recv(sock, buffer, length, 0) < 0) {
		return -1;
	}
	(*buffer)[length] = '\0';
	return length;
}

int handle_register(int c_sock, pid_t owner)
{
	uuid_t uuid;
	unsigned char buffer[128];
	struct alarm *new_alarm = NULL;

	if (read_string(c_sock, &buffer) < 0) {
		return 1;
	}

	uuid_generate(uuid);
	//uuid_unparse(uuid, uuid_str);
	//fprintf(stderr, "Registering alarm %s with uuid %s\n", buffer, uuid_str);

	if (send(c_sock, uuid, sizeof(uuid_t), 0) != sizeof(uuid_t)) {
		perror("send");
		return 1;
	}

	if ((new_alarm = malloc(sizeof(*new_alarm))) == NULL) {
		perror("malloc");
		return 1;
	}

	new_alarm->name = strdup((const char*)buffer);
	memcpy(new_alarm->uuid, uuid, sizeof(new_alarm->uuid));
	new_alarm->is_raised = 0;
	new_alarm->owner = owner;

	alarmd_lock();
	HASH_ADD(hh, alarms, uuid, sizeof(uuid), new_alarm);
	alarmd_unlock();
	return 0;
}

int handle_deregister(int c_sock)
{
	uuid_t uuid;
	struct alarm *target = NULL;
//	char uuid_str[37];

	if (recv(c_sock, &uuid, sizeof(uuid_t), 0) < sizeof(uuid_t)) {
		return 1;
	}

//	uuid_unparse(uuid, uuid_str);
//	fprintf(stderr, "Deregistering alarm %s\n", uuid_str);

	alarmd_lock();
	HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target);
	if (target != NULL) {
		HASH_DEL(alarms, target);
		free(target);
	}
	alarmd_unlock();

	return 0;
}

int handle_set_raised_generic(int c_sock, int shall_raise)
{
	uuid_t uuid;
	size_t nread = 0;
	struct alarm *target = NULL;

	if ((nread = recv(c_sock, &uuid, sizeof(uuid), 0)) < sizeof(uuid)) {
		fprintf(stderr, "Received UUID too short\n");
		return 1;
	}

	alarmd_lock();
	HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target);
	if (target != NULL) {
		target->is_raised = shall_raise;
	}
	alarmd_unlock();
	return 0;
}

int handle_raise(int c_sock)
{
	return handle_set_raised_generic(c_sock, 1);
}

int handle_clear(int c_sock)
{
	return handle_set_raised_generic(c_sock, 0);
}

int handle_query(int c_sock)
{
	uint32_t count = 0;
	uint8_t length = 0;
	struct alarm *a = NULL;
	struct alarm *tmp = NULL;

	alarmd_lock();
	count = HASH_COUNT(alarms);

	if (send(c_sock, &count, sizeof(count), 0) < sizeof(count)) {
		perror("send");
	}

	HASH_ITER(hh, alarms, a, tmp) {
		/* FIXME factor out, idiot. also bounds check on strlen */
		length = strlen(a->name);
		if (send(c_sock, &length, sizeof(length), 0) < sizeof(length)) {
			perror("send");
		}
		if (send(c_sock, a->name, strlen(a->name), 0) < strlen(a->name)) {
			perror("send");
		}
		if (send(c_sock, &(a->is_raised), sizeof(a->is_raised), 0) < sizeof(a->is_raised)) {
			perror("send");
		}
		if (send(c_sock, &(a->owner), sizeof(a->owner), 0) < sizeof(a->owner)) {
			perror("send");
		}
	}
	alarmd_unlock();
	return 0;
}

void *handle_client(void *c_sock_)
{
	int c_sock = *((int*)c_sock_);
	uint32_t packet_type = 0;
	ssize_t nread = 0;
	unsigned int cred_len = 0;
	struct ucred cred;

	cred_len = sizeof(struct ucred);

	if (getsockopt(c_sock, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) {
		perror("getsockopt");
		close(c_sock);
	}

	while (1) {
		/* read packet type */
		nread = recv(c_sock, &packet_type, sizeof(packet_type), 0);
		if (nread < 0) {
			perror("recv");
			return NULL;
		} else if (nread == 0) {
			break;
		} else if (nread < sizeof(packet_type)) {
			fprintf(stderr, "Packet type too short\n");
			return NULL;
		}

		switch(packet_type) {
			case ALARMD_PACKET_TYPE_REGISTER:
				if (handle_register(c_sock, cred.pid)) {;
					close(c_sock);
				}
				break;
			case ALARMD_PACKET_TYPE_DEREGISTER:
				if (handle_deregister(c_sock)) {
					close(c_sock);
				}
				break;
			case ALARMD_PACKET_TYPE_RAISE:
				if (handle_raise(c_sock)) {
					close(c_sock);
				}
				break;
			case ALARMD_PACKET_TYPE_CLEAR:
				if (handle_clear(c_sock)) {
					close(c_sock);
				}
				break;
			case ALARMD_PACKET_TYPE_QUERY:
				if (handle_query(c_sock)) {
					close(c_sock);
				}
				break;
			default:
				fprintf(stderr, "Unknown packet type %d\n", packet_type);
				break;
		}
	}
	close(c_sock);
	return NULL;
}

int main(int argc, char **argv)
{
	int sock = 0;
	int c_sock = 0;
	socklen_t c_addr_l = 0;
	struct sockaddr_un local, c_addr;

	if (argc != 2) {
		fprintf(stderr, "Syntax: %s socket_name\n", argv[0]);
		return 1;
	}

	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		return 1;
	}

	local.sun_family = AF_UNIX;
	strcpy(local.sun_path, argv[1]);
	unlink(local.sun_path);
	if (bind(sock, (struct sockaddr *)&local, strlen(local.sun_path) + sizeof(local.sun_family)) < 0) {
		perror("bind");
		return 1;
	}

	if (listen(sock, 128) < 0) {
		perror("listen");
		return 1;
	}

	c_addr_l = sizeof(c_addr);
	while (1) {
		if ((c_sock = accept(sock, (struct sockaddr *)&c_addr, &c_addr_l)) < 0) {
			perror("accept");
			return 1;
		}
		pthread_t thread;
		pthread_create(&thread, NULL, handle_client, &c_sock);
	}
	fprintf(stderr, "Shutting down\n");
	close(sock);
}