-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdm.c
404 lines (353 loc) · 10.6 KB
/
dm.c
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
/*
* dm - nodm X display manager
*
* Copyright 2011 Enrico Zini <[email protected]>
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "dm.h"
#include "common.h"
#include "log.h"
#include <wordexp.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
void nodm_display_manager_init(struct nodm_display_manager* dm)
{
nodm_xserver_init(&dm->srv);
nodm_xsession_init(&dm->session);
nodm_vt_init(&dm->vt);
dm->conf_minimum_session_time = atoi(getenv_with_default("NODM_MIN_SESSION_TIME", "60"));
dm->_srv_split_args = NULL;
dm->_srv_split_argv = NULL;
// Save original signal mask
if (sigprocmask(SIG_BLOCK, NULL, &dm->orig_signal_mask) == -1)
log_err("sigprocmask error: %m");
dm->srv.orig_signal_mask = dm->orig_signal_mask;
dm->session.orig_signal_mask = dm->orig_signal_mask;
}
void nodm_display_manager_cleanup(struct nodm_display_manager* dm)
{
// Restore original signal mask
if (sigprocmask(SIG_SETMASK, &dm->orig_signal_mask, NULL) == -1)
log_err("sigprocmask error: %m");
nodm_vt_stop(&dm->vt);
// Deallocate parsed arguments, if used
if (dm->_srv_split_args)
{
wordexp_t* we = (wordexp_t*)dm->_srv_split_args;
wordfree(we);
free(we);
dm->_srv_split_args = NULL;
}
if (dm->_srv_split_argv)
{
free(dm->_srv_split_argv);
dm->_srv_split_argv = NULL;
}
}
int nodm_display_manager_start(struct nodm_display_manager* dm)
{
int res = nodm_vt_start(&dm->vt);
if (res != E_SUCCESS) return res;
if (dm->vt.num != -1)
{
// Create the vtN argument
snprintf(dm->_vtarg, sizeof(dm->_vtarg), "vt%d", dm->vt.num);
// Append it to srv args
const char** s = dm->srv.argv;
while (*s) ++s;
*s++ = dm->_vtarg;
*s = NULL;
log_verb("allocated VT %d", dm->vt.num);
} else
log_verb("skipped VT allocation");
// Block all signals
sigset_t blockmask;
if (sigfillset(&blockmask) == -1)
{
log_err("sigfillset error: %m");
return E_PROGRAMMING;
}
if (sigprocmask(SIG_BLOCK, &blockmask, NULL) == -1)
{
log_err("sigprocmask error: %m");
return E_PROGRAMMING;
}
res = nodm_display_manager_restart(dm);
if (res != E_SUCCESS) return res;
return E_SUCCESS;
}
int nodm_display_manager_restart(struct nodm_display_manager* dm)
{
dm->last_session_start = time(NULL);
int res = nodm_xserver_start(&dm->srv);
if (res != E_SUCCESS) return res;
log_verb("X server is ready for connections");
res = nodm_xsession_start(&dm->session, &dm->srv);
if (res != E_SUCCESS) return res;
log_verb("X session has started");
return E_SUCCESS;
}
int nodm_display_manager_stop(struct nodm_display_manager* dm)
{
int res = nodm_xsession_stop(&dm->session);
if (res != E_SUCCESS) return res;
res = nodm_xserver_stop(&dm->srv);
if (res != E_SUCCESS) return res;
return E_SUCCESS;
}
// Signal handler for wait loop
static int quit_signal_caught = 0;
static void catch_signals (int sig)
{
++quit_signal_caught;
}
static int setup_quit_notification(sigset_t* origset)
{
/* Reset caught signal flag */
quit_signal_caught = 0;
struct sigaction action;
action.sa_handler = catch_signals;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
sigset_t ourset;
if (sigemptyset(&ourset)
|| sigaddset(&ourset, SIGTERM)
|| sigaddset(&ourset, SIGINT)
|| sigaddset(&ourset, SIGQUIT)
|| sigaction(SIGTERM, &action, NULL)
|| sigaction(SIGINT, &action, NULL)
|| sigaction(SIGQUIT, &action, NULL)
|| sigprocmask(SIG_UNBLOCK, &ourset, origset)
) {
log_err("signal operations error: %m");
return E_PROGRAMMING;
}
return E_SUCCESS;
}
static void shutdown_quit_notification(const sigset_t* origset)
{
if (sigprocmask(SIG_SETMASK, origset, NULL) == -1)
log_err("sigprocmask error: %m");
}
int nodm_display_manager_wait(struct nodm_display_manager* dm, int* session_status)
{
int res = E_SUCCESS;
// Catch the normal termination signals using 'catch_signals'
sigset_t origset;
res = setup_quit_notification(&origset);
if (res != E_SUCCESS) return res;
*session_status = -1;
while (true)
{
// Wait for one child to exit
int status;
pid_t child = waitpid(-1, &status, 0);
if (child == -1)
{
if (errno == EINTR)
{
if (quit_signal_caught)
{
log_info("shutdown signal received");
res = E_USER_QUIT;
goto cleanup;
}
else
continue;
}
else
{
log_warn("waitpid error: %m");
res = E_OS_ERROR;
goto cleanup;
}
}
if (child == dm->srv.pid)
{
// Server died
nodm_xserver_report_exit(&dm->srv, status);
res = E_X_SERVER_DIED;
goto cleanup;
} else if (child == dm->session.pid) {
// Session died
nodm_xsession_report_exit(&dm->session, status);
*session_status = status;
res = E_SESSION_DIED;
goto cleanup;
}
}
cleanup:
shutdown_quit_notification(&origset);
return res;
}
int nodm_display_manager_parse_xcmdline(struct nodm_display_manager* s, const char* xcmdline)
{
int return_code = E_SUCCESS;
char **argv = NULL;
// tokenize xoptions
wordexp_t* toks = (wordexp_t*)calloc(1, sizeof(wordexp_t));
switch (wordexp(xcmdline, toks, WRDE_NOCMD))
{
case 0: break;
case WRDE_NOSPACE:
return_code = E_OS_ERROR;
goto cleanup;
default:
toks->we_wordv = NULL;
return_code = E_BAD_ARG;
goto cleanup;
}
unsigned in_arg = 0;
unsigned argc = 0;
// +1 for the X server pathname, +1 for the display name,
// +1 for the VT number, +1 for the trailing NULL
argv =(char**)malloc((toks->we_wordc + 4) * sizeof(char*));
if (argv == NULL)
{
return_code = E_OS_ERROR;
goto cleanup;
}
// Server command
if (in_arg < toks->we_wordc &&
(toks->we_wordv[in_arg][0] == '/' || toks->we_wordv[in_arg][0] == '.'))
argv[argc++] = toks->we_wordv[in_arg++];
else
argv[argc++] = "/usr/bin/X";
// Server name
if (in_arg < toks->we_wordc &&
toks->we_wordv[in_arg][0] == ':' && isdigit(toks->we_wordv[in_arg][1]))
{
argv[argc] = toks->we_wordv[in_arg++];
s->srv.name = argv[argc];
++argc;
}
else
{
argv[argc] = ":0";
s->srv.name = argv[argc];
++argc;
}
// Copy other args
while (in_arg < toks->we_wordc)
{
int vtn;
if (sscanf(toks->we_wordv[in_arg], "vt%d", &vtn) == 1)
// if vtN has been provided by the caller, disable VT allocation
s->vt.conf_initial_vt = -1;
argv[argc++] = toks->we_wordv[in_arg++];
}
argv[argc] = NULL;
s->srv.argv = (const char**)argv;
s->_srv_split_argv = argv;
s->_srv_split_args = toks;
argv = NULL;
toks = NULL;
cleanup:
if (toks != NULL)
{
if (toks->we_wordv)
wordfree(toks);
free(toks);
}
if (argv != NULL)
free(argv);
return return_code;
}
void nodm_display_manager_dump_status(struct nodm_display_manager* dm)
{
nodm_xserver_dump_status(&dm->srv);
nodm_xsession_dump_status(&dm->session);
}
static int interruptible_sleep(int seconds)
{
int res = E_SUCCESS;
// Catch the normal termination signals using 'catch_signals'
sigset_t origset;
res = setup_quit_notification(&origset);
if (res != E_SUCCESS) return res;
struct timespec tosleep = { .tv_sec = seconds, .tv_nsec = 0 };
struct timespec remaining;
while (true)
{
int r = nanosleep(&tosleep, &remaining);
if (r != -1)
break;
else if (errno == EINTR)
{
if (quit_signal_caught)
{
res = E_USER_QUIT;
break;
} else
tosleep = remaining;
}
else
{
log_warn("sleep aborted: %m (ignoring error");
break;
}
}
shutdown_quit_notification(&origset);
return res;
}
int nodm_display_manager_wait_restart_loop(struct nodm_display_manager* dm)
{
static int retry_times[] = { 0, 0, 30, 30, 60, 60, -1 };
int restart_count = 0;
int res;
while (1)
{
int sstatus;
res = nodm_display_manager_wait(dm, &sstatus);
time_t end = time(NULL);
nodm_display_manager_stop(dm);
switch (res)
{
case E_X_SERVER_DIED:
break;
case E_SESSION_DIED:
break;
default:
return res;
}
/* Check if the session was too short */
if (end - dm->last_session_start < dm->conf_minimum_session_time)
{
if (retry_times[restart_count+1] != -1)
++restart_count;
}
else
restart_count = 0;
/* Sleep a bit if the session was too short */
if (retry_times[restart_count] > 0)
{
log_warn("session lasted less than %d seconds: sleeping %d seconds before restarting it",
dm->conf_minimum_session_time, retry_times[restart_count]);
res = interruptible_sleep(retry_times[restart_count]);
if (res != E_SUCCESS) return res;
}
log_info("restarting session");
res = nodm_display_manager_restart(dm);
if (res != E_SUCCESS) return res;
}
}