Skip to content

Commit c1299fa

Browse files
committed
Isolation: Switch to fork(2) & unshare(2) on Linux.
On GitHub, @razvanphp & @hbernaciak both reported issues running the APCu PHP module under Unit. When using this module they were seeing errors like 'apcu_fetch(): Failed to acquire read lock' However when running APCu under php-fpm, everything was fine. The issue turned out to be due to our use of SYS_clone breaking the pthreads(7) API used by APCu. Even if we had been using glibc's clone(2) wrapper we would still have run into problems due to a known issue there. Essentially the problem is when using clone, glibc doesn't update the TID cache, so the child ends up having the same TID as the parent and that is used in various parts of pthreads(7) such as in the various locking primitives, so when APCu was grabbing a lock it ended up using the TID of the main unit process (rather than that of the php application processes that was grabbing the lock). So due to the above what was happening was when one of the application processes went to grab either a read or write lock, the lock was actually being attributed to the main unit process. If a process had acquired the write lock, then if a process tried to acquire a read or write lock then glibc would return EDEADLK due to detecting a deadlock situation due to thinking the process already held the write lock when in fact it didn't. It seems the right way to do this is via fork(2) and unshare(2). We already use fork(2) on other platforms. This requires a few tricks to keep the essence of the processes the same as before when using clone 1) We use the prctl(2) PR_SET_CHILD_SUBREAPER option (if its available, since Linux 3.4) to make the main unit process inherit prototype processes after a double fork(2), rather than them being reparented to 'init'. This avoids needing to ^C twice to fully exit unit when running in the foreground. It's probably also better if they maintain their parent child relationship where possible. 2) We use a double fork(2) technique on the prototype processes to ensure they themselves end up in a new PID namespace as PID 1 (when CLONE_NEWPID is being used). When using unshare(CLONE_NEWPID), the calling process is _not_ placed in the namespace (as discussed in pid_namespaces(7)). It only sets things up so that subsequent children are placed in a PID namespace. Having the prototype processes as PID 1 in the new PID namespace is probably a good thing and matches the behaviour of clone(2). Also, some isolation tests break if the prototype process is not PID 1. 3) Due to the above double fork(2) the main unit process looses track of the prototype process ID, which it needs to know. To solve this, we employ a simple pipe(2) between the main unit and prototype processes and pass the prototype grandchild PID from the parent of the second fork(2) before exiting. This needs to be done from the parent and not the grandchild, as the grandchild will see itself having a PID of 1 while the main process needs its externally visible PID. Link: <https://www.php.net/manual/en/book.apcu.php> Link: <https://sourceware.org/bugzilla/show_bug.cgi?id=21793> Closes: <#694> Reviewed-by: Alejandro Colomar <[email protected]> Signed-off-by: Andrew Clayton <[email protected]>
1 parent a83354f commit c1299fa

File tree

1 file changed

+247
-9
lines changed

1 file changed

+247
-9
lines changed

src/nxt_process.c

Lines changed: 247 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
#endif
2828

2929

30+
#if (NXT_HAVE_LINUX_NS)
31+
static nxt_int_t nxt_process_pipe_timer(nxt_fd_t fd, short event);
32+
static nxt_int_t nxt_process_check_pid_status(const nxt_fd_t *gc_pipe);
33+
static nxt_pid_t nxt_process_recv_pid(const nxt_fd_t *pid_pipe,
34+
const nxt_fd_t *gc_pipe);
35+
static void nxt_process_send_pid(const nxt_fd_t *pid_pipe, nxt_pid_t pid);
36+
static nxt_int_t nxt_process_unshare(nxt_task_t *task, nxt_process_t *process,
37+
nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, nxt_bool_t use_pidns);
38+
static nxt_int_t nxt_process_init_pidns(nxt_task_t *task,
39+
const nxt_process_t *process, nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe,
40+
nxt_bool_t *use_pidns);
41+
#endif
42+
3043
static nxt_pid_t nxt_process_create(nxt_task_t *task, nxt_process_t *process);
3144
static nxt_int_t nxt_process_do_start(nxt_task_t *task, nxt_process_t *process);
3245
static nxt_int_t nxt_process_whoami(nxt_task_t *task, nxt_process_t *process);
@@ -311,6 +324,217 @@ nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process)
311324
}
312325

313326

327+
#if (NXT_HAVE_LINUX_NS)
328+
329+
static nxt_int_t
330+
nxt_process_pipe_timer(nxt_fd_t fd, short event)
331+
{
332+
int ret;
333+
sigset_t mask;
334+
struct pollfd pfd;
335+
336+
static const struct timespec ts = { .tv_sec = 5 };
337+
338+
/*
339+
* Temporarily block the signals we are handling, (except
340+
* for SIGINT & SIGTERM) so that ppoll(2) doesn't get
341+
* interrupted. After ppoll(2) returns, our old sigmask
342+
* will be back in effect and any pending signals will be
343+
* delivered.
344+
*
345+
* This is because while the kernel ppoll syscall updates
346+
* the struct timespec with the time remaining if it got
347+
* interrupted with EINTR, the glibc wrapper hides this
348+
* from us so we have no way of knowing how long to retry
349+
* the ppoll(2) for and if we just retry with the same
350+
* timeout we could find ourselves in an infinite loop.
351+
*/
352+
pthread_sigmask(SIG_SETMASK, NULL, &mask);
353+
sigdelset(&mask, SIGINT);
354+
sigdelset(&mask, SIGTERM);
355+
356+
pfd.fd = fd;
357+
pfd.events = event;
358+
359+
ret = ppoll(&pfd, 1, &ts, &mask);
360+
if (ret <= 0 || (ret == 1 && pfd.revents & POLLERR)) {
361+
return NXT_ERROR;
362+
}
363+
364+
return NXT_OK;
365+
}
366+
367+
368+
static nxt_int_t
369+
nxt_process_check_pid_status(const nxt_fd_t *gc_pipe)
370+
{
371+
int8_t status;
372+
ssize_t ret;
373+
374+
close(gc_pipe[1]);
375+
376+
ret = nxt_process_pipe_timer(gc_pipe[0], POLLIN);
377+
if (ret == NXT_OK) {
378+
ret = read(gc_pipe[0], &status, sizeof(int8_t));
379+
}
380+
381+
if (ret <= 0) {
382+
status = -1;
383+
}
384+
385+
close(gc_pipe[0]);
386+
387+
return status;
388+
}
389+
390+
391+
static nxt_pid_t
392+
nxt_process_recv_pid(const nxt_fd_t *pid_pipe, const nxt_fd_t *gc_pipe)
393+
{
394+
int8_t status;
395+
ssize_t ret;
396+
nxt_pid_t pid;
397+
398+
close(pid_pipe[1]);
399+
close(gc_pipe[0]);
400+
401+
status = 0;
402+
403+
ret = nxt_process_pipe_timer(pid_pipe[0], POLLIN);
404+
if (ret == NXT_OK) {
405+
ret = read(pid_pipe[0], &pid, sizeof(nxt_pid_t));
406+
}
407+
408+
if (ret <= 0) {
409+
status = -1;
410+
pid = -1;
411+
}
412+
413+
write(gc_pipe[1], &status, sizeof(int8_t));
414+
415+
close(pid_pipe[0]);
416+
close(gc_pipe[1]);
417+
418+
return pid;
419+
}
420+
421+
422+
static void
423+
nxt_process_send_pid(const nxt_fd_t *pid_pipe, nxt_pid_t pid)
424+
{
425+
nxt_int_t ret;
426+
427+
close(pid_pipe[0]);
428+
429+
ret = nxt_process_pipe_timer(pid_pipe[1], POLLOUT);
430+
if (ret == NXT_OK) {
431+
write(pid_pipe[1], &pid, sizeof(nxt_pid_t));
432+
}
433+
434+
close(pid_pipe[1]);
435+
}
436+
437+
438+
static nxt_int_t
439+
nxt_process_unshare(nxt_task_t *task, nxt_process_t *process,
440+
nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe,
441+
nxt_bool_t use_pidns)
442+
{
443+
int ret;
444+
nxt_pid_t pid;
445+
446+
if (process->isolation.clone.flags == 0) {
447+
return NXT_OK;
448+
}
449+
450+
ret = unshare(process->isolation.clone.flags);
451+
if (nxt_slow_path(ret == -1)) {
452+
nxt_alert(task, "unshare() failed for %s %E", process->name,
453+
nxt_errno);
454+
455+
if (use_pidns) {
456+
nxt_pipe_close(task, gc_pipe);
457+
nxt_pipe_close(task, pid_pipe);
458+
}
459+
460+
return NXT_ERROR;
461+
}
462+
463+
if (!use_pidns) {
464+
return NXT_OK;
465+
}
466+
467+
/*
468+
* PID namespace requested. Employ a double fork(2) technique
469+
* so that the prototype process will be placed into the new
470+
* namespace and end up with PID 1 (as before with clone).
471+
*/
472+
pid = fork();
473+
if (nxt_slow_path(pid < 0)) {
474+
nxt_alert(task, "fork() failed for %s %E", process->name, nxt_errno);
475+
nxt_pipe_close(task, gc_pipe);
476+
nxt_pipe_close(task, pid_pipe);
477+
478+
return NXT_ERROR;
479+
480+
} else if (pid > 0) {
481+
nxt_pipe_close(task, gc_pipe);
482+
nxt_process_send_pid(pid_pipe, pid);
483+
484+
_exit(EXIT_SUCCESS);
485+
}
486+
487+
nxt_pipe_close(task, pid_pipe);
488+
ret = nxt_process_check_pid_status(gc_pipe);
489+
if (ret == -1) {
490+
return NXT_ERROR;
491+
}
492+
493+
return NXT_OK;
494+
}
495+
496+
497+
static nxt_int_t
498+
nxt_process_init_pidns(nxt_task_t *task, const nxt_process_t *process,
499+
nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe,
500+
nxt_bool_t *use_pidns)
501+
{
502+
int ret;
503+
504+
*use_pidns = 0;
505+
506+
#if (NXT_HAVE_CLONE_NEWPID)
507+
*use_pidns = nxt_is_pid_isolated(process);
508+
#endif
509+
510+
if (!*use_pidns) {
511+
return NXT_OK;
512+
}
513+
514+
ret = nxt_pipe_create(task, pid_pipe, 0, 0);
515+
if (nxt_slow_path(ret == NXT_ERROR)) {
516+
return NXT_ERROR;
517+
}
518+
519+
ret = nxt_pipe_create(task, gc_pipe, 0, 0);
520+
if (nxt_slow_path(ret == NXT_ERROR)) {
521+
return NXT_ERROR;
522+
}
523+
524+
#if (NXT_HAVE_PR_SET_CHILD_SUBREAPER)
525+
ret = prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0);
526+
if (nxt_slow_path(ret == -1)) {
527+
nxt_alert(task, "prctl(PR_SET_CHILD_SUBREAPER) failed for %s %E",
528+
process->name, nxt_errno);
529+
}
530+
#endif
531+
532+
return NXT_OK;
533+
}
534+
535+
#endif /* NXT_HAVE_LINUX_NS */
536+
537+
314538
static nxt_pid_t
315539
nxt_process_create(nxt_task_t *task, nxt_process_t *process)
316540
{
@@ -319,22 +543,31 @@ nxt_process_create(nxt_task_t *task, nxt_process_t *process)
319543
nxt_runtime_t *rt;
320544

321545
#if (NXT_HAVE_LINUX_NS)
322-
pid = nxt_clone(SIGCHLD | process->isolation.clone.flags);
323-
if (nxt_slow_path(pid < 0)) {
324-
nxt_alert(task, "clone() failed for %s %E", process->name, nxt_errno);
325-
return pid;
546+
nxt_fd_t pid_pipe[2], gc_pipe[2];
547+
nxt_bool_t use_pidns;
548+
549+
ret = nxt_process_init_pidns(task, process, pid_pipe, gc_pipe, &use_pidns);
550+
if (ret == NXT_ERROR) {
551+
return -1;
326552
}
327-
#else
553+
#endif
554+
328555
pid = fork();
329556
if (nxt_slow_path(pid < 0)) {
330557
nxt_alert(task, "fork() failed for %s %E", process->name, nxt_errno);
331558
return pid;
332559
}
333-
#endif
334560

335561
if (pid == 0) {
336562
/* Child. */
337563

564+
#if (NXT_HAVE_LINUX_NS)
565+
ret = nxt_process_unshare(task, process, pid_pipe, gc_pipe, use_pidns);
566+
if (ret == NXT_ERROR) {
567+
_exit(EXIT_FAILURE);
568+
}
569+
#endif
570+
338571
ret = nxt_process_child_fixup(task, process);
339572
if (nxt_slow_path(ret != NXT_OK)) {
340573
nxt_process_quit(task, 1);
@@ -355,10 +588,15 @@ nxt_process_create(nxt_task_t *task, nxt_process_t *process)
355588

356589
/* Parent. */
357590

358-
#if (NXT_HAVE_LINUX_NS)
359-
nxt_debug(task, "clone(%s): %PI", process->name, pid);
360-
#else
361591
nxt_debug(task, "fork(%s): %PI", process->name, pid);
592+
593+
#if (NXT_HAVE_LINUX_NS)
594+
if (use_pidns) {
595+
pid = nxt_process_recv_pid(pid_pipe, gc_pipe);
596+
if (pid == -1) {
597+
return pid;
598+
}
599+
}
362600
#endif
363601

364602
process->pid = pid;

0 commit comments

Comments
 (0)