summaryrefslogtreecommitdiff
path: root/command.c
blob: 869c0a6763c6c825384ec36b9cacf5d1262491ef (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "nfcommander.h"
#include "command.h"
#include "config.h"
#include "reader.h"

#define COMMAND_ALIVE 1
#define COMMAND_STOPPED 2

#define INPUT_BUFFER_MAX 2048

typedef struct {
	uint8_t flags;
	pid_t child;
	int iopipe[2];
	nfc_tag_info_t* tag;

	size_t input_head;
	uint8_t input[INPUT_BUFFER_MAX];
} command_t;

static size_t ncommands = 0;
static command_t* commands = NULL;

//TODO timeouting / SIGKILL

static int command_scan_input(command_t* cmd, size_t bytes){
	size_t n = 0;

	//check for line ending
	for(n = 0; n < bytes; n++){
		if(cmd->input[cmd->input_head + n] == '\r' || 
				cmd->input[cmd->input_head + n] == '\n'){
			//terminate string
			cmd->input[cmd->input_head + n] = 0;
			//handle line
			if(!strncmp((char*) cmd->input, "UPDATE ", strlen("UPDATE "))){
				if(cmd->tag && strlen((char*) (cmd->input + 7)) < cmd->tag->dynamic_max){
					free(cmd->tag->dynamic_data);
					cmd->tag->dynamic_data = (uint8_t*) strdup((char*) (cmd->input + 7));
					cmd->tag->dynamic_length = strlen((char*) cmd->tag->dynamic_data);
					reader_write(cmd->tag, TAG_WRITE_DYNAMIC);
				}
				else{
					printf("Failed to update tag dynamic data\n");
				}
			}

			//find beginning of next line
			n++;
			for(; n < bytes && !isprint(cmd->input[cmd->input_head + n]); n++){
			}

			if(n == bytes){
				cmd->input_head = 0;
				return 0;
			}

			//move back
			memmove(cmd->input, cmd->input + cmd->input_head + n, bytes - n);
			cmd->input_head = 0;

			//try to find another sentence
			return command_scan_input(cmd, bytes - n);
		}
		else if(!isprint(cmd->input[cmd->input_head + n])){
			cmd->input[cmd->input_head + n] = ' ';
		}
	}

	if(cmd->input_head + bytes > INPUT_BUFFER_MAX - 10){
		//line too long - ignore it entirely until the next linebreak
		cmd->input_head = 1;
		cmd->input[0] = 0; //fail any test for UPDATE
		return 0;
	}

	//no newline
	cmd->input_head += bytes;
	return 0;
}

int command_handle(int fd){
	size_t n = 0;
	ssize_t bytes;

	//find matching command
	for(n = 0; n < ncommands; n++){
		if(commands[n].flags & COMMAND_ALIVE
				&& fd == commands[n].iopipe[0]){
			bytes = read(fd, commands[n].input + commands[n].input_head,
					sizeof(commands[n].input) - commands[n].input_head - 1);
			if(bytes < 0){
				perror("cmd/recv");
			}
			else if(bytes == 0){
				printf("Command %lu pipe closed\n", n);
				core_manage_fd(commands[n].iopipe[0], 0, system_command);
				close(commands[n].iopipe[0]);
				commands[n].iopipe[0] = -1;
				commands[n].input_head = 0;
			}
			else{
				//printf("%ld bytes from command %lu\n", bytes, n);
				command_scan_input(commands + n, bytes);
			}
			return 0;
		}
	}

	printf("Unhandled command fd\n");
	return 1;
}

void command_reap(){
	int wait_status;
	pid_t status;
	size_t n;

	do {
		status = waitpid(-1, &wait_status, WNOHANG);
		if(status < 0 && errno != ECHILD){
			perror("cmd/reap");
		}
		if(status <= 0){
			return;
		}

		//find matching command
		if(status){
			for(n = 0; n < ncommands; n++){
				if(commands[n].child == status){
					commands[n].flags = 0;
					commands[n].child = -1;
					if(commands[n].iopipe[0] >= 0){
						core_manage_fd(commands[n].iopipe[0], 0, system_command);
						close(commands[n].iopipe[0]);
						commands[n].iopipe[0] = -1;
					}
					printf("Command %lu terminated\n", n);
				}
			}
		}
	} while(status);
}

static int command_spawn(command_t* cmd, char* command_name, char* command_static){
	char* workdir = config_get("command", "chdir");
	char* handler = config_get("command", "handler");
	char tag_uid[12] = "", dynsize[10] = "";
	size_t bytes;
	char* dynamic = (cmd->tag->dynamic_length) ? calloc(cmd->tag->dynamic_length + 1, sizeof(uint8_t)) : NULL;

	printf("Starting command %s, static %s\n", command_name, command_static);
	if(pipe(cmd->iopipe)){
		perror("cmd/pipe");
		return 1;
	}

	if(fcntl(cmd->iopipe[0], F_SETFD, FD_CLOEXEC)){
		perror("cmd/fcntl");
	}

	cmd->child = fork();
	switch(cmd->child){
		case -1:
			perror("cmd/fork");
			return 1;
		case 0:
			//child
			if(workdir && chdir(workdir)){
				perror("child/chdir");
				_exit(1);
			}

			//make child a session leader
			setpgrp();

			//connect stdout
			while((dup2(cmd->iopipe[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {
			}
			close(cmd->iopipe[1]);

			//set environment
			bytes = snprintf(tag_uid, sizeof(tag_uid), "%02X%02X%02X%02X",
					cmd->tag->uid[0], cmd->tag->uid[1],
					cmd->tag->uid[2], cmd->tag->uid[3]);
			if(cmd->tag->uid_length > 4){
				bytes += snprintf(tag_uid + bytes, sizeof(tag_uid) - bytes, "%02X%02X%02X",
						cmd->tag->uid[4], cmd->tag->uid[5],
						cmd->tag->uid[6]);
			}
			snprintf(dynsize, sizeof(dynsize), "%ld", cmd->tag->dynamic_max);
			setenv("TAG_UID", tag_uid, 1);
			setenv("MAX_DATA_LENGTH", dynsize, 1);

			if(dynamic){
				memcpy(dynamic, cmd->tag->dynamic_data, cmd->tag->dynamic_length);
			}

			//run actual command
			if(handler){
				execl(handler, handler, command_name, command_static, dynamic, NULL);
			}
			else{
				execl(command_name, command_name, command_static, dynamic, NULL);
			}
			perror("child/exec");
			_exit(1);
		default:
			//parent
			cmd->flags |= COMMAND_ALIVE;
			close(cmd->iopipe[1]);
			core_manage_fd(cmd->iopipe[0], 1, system_command);
	}

	return 0;
}

int command_start(nfc_tag_info_t* info){
	size_t n = 0, p = ncommands;
	uint8_t* command_name = NULL, *command_static = NULL;

	for(n = 0; n < ncommands; n++){
		//if command already in list, do nothing
		if(commands[n].tag == info){
			//this should not normally happen
			return 0;
		}

		//mark available slot
		if(!commands[n].tag && !commands[n].flags){
			p = n;
		}
	}

	//cant run without an actual command
	if(!info->static_data || !info->static_length){
		printf("Tag contains no command data\n");
		return 0;
	}

	//parse out command data
	command_name = calloc(info->static_length + 1, sizeof(uint8_t));
	if(!command_name){
		printf("Failed to allocate memory\n");
		return 1;
	}
	memcpy(command_name, info->static_data, info->static_length);
	for(n = 0; n < info->static_length; n++){
		if(!isalnum(command_name[n])){
			command_name[n] = 0;
			if(info->static_length - n > 1){
				command_static = command_name + n + 1;
			}
			break;
		}
	}

	if(!strlen((char*) command_name)){
		printf("Tag contains invalid command data\n");
		free(command_name);
		return 1;
	}

	if(p == ncommands){
		commands = realloc(commands, (p + 1) * sizeof(command_t));
		if(!commands){
			ncommands = 0;
			printf("Failed to allocate memory\n");
			free(command_name);
			return 1;
		}
		memset(commands + p, 0, sizeof(command_t));
		ncommands++;
	}

	commands[p].tag = info;
	commands[p].input_head = 0;
	if(command_spawn(commands + p, (char*) command_name, (char*) command_static)){
		//TODO clean up and free command instance
	}

	free(command_name);
	return 0;
}

static void command_stop_internal(size_t n){
	//send SIGTERM to process group
	if(kill(-commands[n].child, (commands[n].flags & COMMAND_STOPPED) ? SIGKILL : SIGTERM)){
		perror("cmd/stop");
	}
	commands[n].flags |= COMMAND_STOPPED;
}

int command_stop(nfc_tag_info_t* info){
	size_t n = 0;

	for(n = 0; n < ncommands; n++){
		if(commands[n].tag == info){
			commands[n].tag = NULL;
			if(commands[n].flags & COMMAND_ALIVE){
				command_stop_internal(n);
			}
		}
	}

	return 0;
}

void command_free(){
	size_t n = 0;

	printf("Shutting down any remaining commands\n");
	for(n = 0; n < ncommands; n++){
		while(commands[n].flags & COMMAND_ALIVE){
			command_stop_internal(n);
			command_reap();
		}
	}

	ncommands = 0;
	free(commands);
	commands = NULL;
}