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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/**
* mds — A micro-display server
* Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mds-server.h"
#include "config.h"
#include <libmdsserver/linked-list.h>
#include <libmdsserver/fd-table.h>
#include <libmdsserver/mds-message.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <unistd.h>
#include <pwd.h>
#include <errno.h>
#include <pthread.h>
#include <sys/socket.h>
#define MDS_SERVER_VARS_VERSION 0
/**
* Number of elements in `argv`
*/
static int argc;
/**
* Command line arguments
*/
static char** argv;
/**
* The program run state, 1 when running,
* 0 when shutting down
*/
static volatile sig_atomic_t running = 1;
/**
* Non-zero when the program is about to re-exec
*/
static volatile sig_atomic_t reexecing = 0;
/**
* The number of running slaves
*/
static int running_slaves = 0;
/**
* Mutex for slave data
*/
static pthread_mutex_t slave_mutex;
/**
* Condition for slave data
*/
static pthread_cond_t slave_cond;
/**
* Map from client socket file descriptor to all information (client_t)
*/
static fd_table_t client_map;
/**
* List of client information (client_t)
*/
static linked_list_t client_list;
/**
* Entry point of the server
*
* @param argc_ Number of elements in `argv_`
* @param argv_ Command line arguments
* @return Non-zero on error
*/
int main(int argc_, char** argv_)
{
int is_respawn = -1;
int socket_fd = -1;
int unparsed_args_ptr = 1;
char* unparsed_args[ARGC_LIMIT + LIBEXEC_ARGC_EXTRA_LIMIT + 1];
int i;
pid_t pid;
pthread_t _slave_thread;
argc = argc_;
argv = argv_;
/* Drop privileges like it's hot. */
if ((geteuid() == getuid() ? 0 : seteuid(getuid())) ||
(getegid() == getgid() ? 0 : setegid(getgid())))
{
perror(*argv);
return 1;
}
/* Sanity check the number of command line arguments. */
if (argc > ARGC_LIMIT + LIBEXEC_ARGC_EXTRA_LIMIT)
{
fprintf(stderr,
"%s: that number of arguments is ridiculous, I will not allow it.\n",
*argv);
return 1;
}
/* TODO: support re-exec */
/* Parse command line arguments. */
for (i = 1; i < argc; i++)
{
char* arg = argv[i];
if (!strcmp(arg, "--initial-spawn")) /* Initial spawn? */
if (is_respawn == 1)
{
fprintf(stderr,
"%s: conflicting arguments %s and %s cannot be combined.\n",
*argv, "--initial-spawn", "--respawn");
return 1;
}
else
is_respawn = 0;
else if (!strcmp(arg, "--respawn")) /* Respawning after crash? */
if (is_respawn == 0)
{
fprintf(stderr,
"%s: conflicting arguments %s and %s cannot be combined.\n",
*argv, "--initial-spawn", "--respawn");
return 1;
}
else
is_respawn = 1;
else if (strstr(arg, "--socket-fd=") == arg) /* Socket file descriptor. */
{
long int r;
char* endptr;
if (socket_fd != -1)
{
fprintf(stderr, "%s: duplicate declaration of %s.\n", *argv, "--socket-fd");
return -1;
}
arg += strlen("--socket-fd=");
r = strtol(arg, &endptr, 10);
if ((*argv == '\0') || isspace(*argv) ||
(endptr - arg != (ssize_t)strlen(arg))
|| (r < 0) || (r > INT_MAX))
{
fprintf(stderr, "%s: invalid value for %s: %s.\n", *argv, "--socket-fd", arg);
return 1;
}
socket_fd = (int)r;
}
else
/* Not recognised, it is probably for another server. */
unparsed_args[unparsed_args_ptr++] = arg;
}
unparsed_args[unparsed_args_ptr] = NULL;
/* Check that manditory arguments have been specified. */
if (is_respawn < 0)
{
fprintf(stderr,
"%s: missing state argument, require either %s or %s.\n",
*argv, "--initial-spawn", "--respawn");
return 1;
}
if (socket_fd < 0)
{
fprintf(stderr, "%s: missing socket file descriptor argument.\n", *argv);
return 1;
}
if (is_respawn == 0)
{
/* Run mdsinitrc. */
pid = fork();
if (pid == (pid_t)-1)
{
perror(*argv);
return 1;
}
if (pid == 0) /* Child process exec:s, the parent continues without waiting for it. */
{
run_initrc(unparsed_args);
return 1;
}
}
/* Create list and table of clients. */
if (fd_table_create(&client_map))
{
perror(*argv);
fd_table_destroy(&client_map, NULL, NULL);
return 1;
}
if (linked_list_create(&client_list, 32))
{
perror(*argv);
linked_list_destroy(&client_list);
return 1;
}
/* Create mutex and condition for slave counter. */
pthread_mutex_init(&slave_mutex, NULL);
pthread_cond_init(&slave_cond, NULL);
/* TODO make the server update without all slaves dying on SIGUSR1 */
/* Accepting incoming connections. */
while (running && (reexecing == 0))
{
/* Accept connection. */
int client_fd = accept(socket_fd, NULL, NULL);
/* Handle errors and shutdown. */
if (client_fd == -1)
{
switch (errno)
{
case EINTR:
/* Interrupted. */
if (reexecing)
goto reexec;
break;
case ECONNABORTED:
case EINVAL:
/* Closing. */
running = 0;
break;
default:
/* Error. */
perror(*argv);
break;
}
continue;
}
/* Increase number of running slaves. */
pthread_mutex_lock(&slave_mutex);
running_slaves++;
pthread_mutex_unlock(&slave_mutex);
/* Start slave thread. */
errno = pthread_create(&_slave_thread, NULL, slave_loop, (void*)(intptr_t)client_fd);
if (errno)
{
perror(*argv);
pthread_mutex_lock(&slave_mutex);
running_slaves--;
pthread_mutex_unlock(&slave_mutex);
}
}
if (reexecing)
goto reexec;
/* Wait for all slaves to close. */
pthread_mutex_lock(&slave_mutex);
while (running_slaves > 0)
pthread_cond_wait(&slave_cond, &slave_mutex);
pthread_mutex_unlock(&slave_mutex);
/* Release resources. */
fd_table_destroy(&client_map, NULL, NULL);
linked_list_destroy(&client_list);
return 0;
reexec:
/* Join with all slaves threads. */
pthread_mutex_lock(&slave_mutex);
while (running_slaves > 0)
pthread_cond_wait(&slave_cond, &slave_mutex);
pthread_mutex_unlock(&slave_mutex);
/* TODO: marshal and exec */
/* Returning non-zero is important, otherwise the server cannot
be respawn if the re-exec fails. */
perror(*argv);
return 1;
}
/**
* Master function for slave threads
*
* @param data Input data
* @return Outout data
*/
void* slave_loop(void* data)
{
int socket_fd = (int)(intptr_t)data;
ssize_t entry = LINKED_LIST_UNUSED;
client_t* information;
size_t tmp;
int r;
/* Create information table. */
information = malloc(sizeof(client_t));
if (information == NULL)
{
perror(*argv);
goto fail;
}
/* Add to list of clients. */
pthread_mutex_lock(&slave_mutex);
entry = linked_list_insert_end(&client_list, (size_t)(void*)information);
if (entry == LINKED_LIST_UNUSED)
{
perror(*argv);
pthread_mutex_unlock(&slave_mutex);
goto fail;
}
/* Add client to table. */
tmp = fd_table_put(&client_map, socket_fd, (size_t)(void*)information);
pthread_mutex_unlock(&slave_mutex);
if ((tmp == 0) && errno)
{
perror(*argv);
goto fail;
}
/* Fill information table. */
information->list_entry = entry;
information->socket_fd = socket_fd;
information->open = 1;
if (mds_message_initialise(&(information->message)))
{
perror(*argv);
goto fail;
}
/* Fetch messages from the slave. */
while (reexecing == 0)
{
r = mds_message_read(&(information->message), socket_fd);
if (r == 0)
{
/* TODO */
}
else
if (r == -2)
{
fprintf(stderr, "%s: corrupt message received.\n", *argv);
goto fail;
}
else if (errno == ECONNRESET)
{
r = mds_message_read(&(information->message), socket_fd);
information->open = 0;
if (r == 0)
{
/* TODO */
}
/* Connection closed. */
break;
}
else if (errno == EINTR)
{
/* Stop the thread if we are re-exec:ing the server. */
if (reexecing)
goto reexec;
}
else
{
perror(*argv);
goto fail;
}
}
/* Stop the thread if we are re-exec:ing the server. */
if (reexecing)
goto reexec;
fail:
/* Close socket and free resources. */
close(socket_fd);
if (information != NULL)
{
mds_message_destroy(&(information->message));
free(information);
}
fd_table_remove(&client_map, socket_fd);
/* Unlist client and decrease the slave count. */
pthread_mutex_lock(&slave_mutex);
if (entry != LINKED_LIST_UNUSED)
linked_list_remove(&client_list, entry);
running_slaves--;
pthread_cond_signal(&slave_cond);
pthread_mutex_unlock(&slave_mutex);
return NULL;
reexec:
/* Tell the master thread that the slave has closed,
this is done because re-exec causes a race-condition
between the acception of a slave and the execution
of the the slave thread. */
pthread_mutex_lock(&slave_mutex);
running_slaves--;
pthread_cond_signal(&slave_cond);
pthread_mutex_unlock(&slave_mutex);
return NULL;
}
/**
* Read an environment variable, but handle it as undefined if empty
*
* @param var The environment variable's name
* @return The environment variable's value, `NULL` if empty or not defined
*/
char* getenv_nonempty(const char* var)
{
char* rc = getenv(var);
if ((rc == NULL) || (*rc == '\0'))
return NULL;
return rc;
}
/**
* Exec into the mdsinitrc script
*
* @param args The arguments to the child process
*/
void run_initrc(char** args)
{
char pathname[PATH_MAX];
struct passwd* pwd;
char* env;
char* home;
args[0] = pathname;
/* Test $XDG_CONFIG_HOME. */
if ((env = getenv_nonempty("XDG_CONFIG_HOME")) != NULL)
{
snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.%s", env, INITRC_FILE);
execv(args[0], args);
}
/* Test $HOME. */
if ((env = getenv_nonempty("HOME")) != NULL)
{
snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.config/%s", env, INITRC_FILE);
execv(args[0], args);
snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.%s", env, INITRC_FILE);
execv(args[0], args);
}
/* Test ~. */
pwd = getpwuid(getuid()); /* Ignore error. */
if (pwd != NULL)
{
home = pwd->pw_dir;
if ((home != NULL) && (*home != '\0'))
{
snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.config/%s", home, INITRC_FILE);
execv(args[0], args);
snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/.%s", home, INITRC_FILE);
execv(args[0], args);
}
}
/* Test $XDG_CONFIG_DIRS. */
if ((env = getenv_nonempty("XDG_CONFIG_DIRS")) != NULL)
{
char* begin = env;
char* end;
int len;
for (;;)
{
end = strchrnul(begin, ':');
len = (int)(end - begin);
if (len > 0)
{
snprintf(pathname, sizeof(pathname) / sizeof(char), "%.*s/%s", len, begin, INITRC_FILE);
execv(args[0], args);
}
if (*end == '\0')
break;
begin = end + 1;
}
}
/* Test /etc. */
snprintf(pathname, sizeof(pathname) / sizeof(char), "%s/%s", SYSCONFDIR, INITRC_FILE);
execv(args[0], args);
/* Everything failed. */
fprintf(stderr, "%s: unable to run %s file, you might as well kill me.\n", *argv, INITRC_FILE);
}
|