Skip to content

MacOS implements pivot_root(), but differently from Linux #737

@alejandro-colomar

Description

@alejandro-colomar

The recent commit 5015b05 improved portability of syscalls by using SYS_xxx instead of __NR_xxx.

This had a few unexpected side effects:

  • Since only Linux documents the existence of pivot_root(2), we thought that its existence guaranteed the existence of a header that only exists in GNU/Linux systems: <mntent.h>. Now that the configure script detects the existence of SYS_pivot_root in MacOS, the code tries to include <mntent.h>.

  • The implementation of MacOS's pivot_root() is very different from the one in Linux. Even worse: it's undocumented; there's no pivot_root(2) manual page in MacOS. See details for MacOS's pivot_root() below.

The easy solution for the first problem is testing for the existence of the header before including it. Just a minor problem.

The second one is a more important problem. We should make sure that we only use pivot_root(2) in Linux. So we should wrap uses of the syscall with #if (NXT_LINUX) or similar. NXT_HAVE_PIVOT_ROOT is not enough. Maybe we should rename it to NXT_HAVE_LINUX_PIVOT_ROOT, and fix the test accordingly.


pivot_root() in MacOS:

$ grepc -k SYS_pivot_root / 2>/dev/null
/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/syscall.h:577:#define	SYS_pivot_root     537
/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/sys/syscall.h:577:#define	SYS_pivot_root     537
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/syscall.h:577:#define	SYS_pivot_root     537
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/sys/syscall.h:577:#define	SYS_pivot_root     537
/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/syscall.h:577:#define	SYS_pivot_root     537
/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/sys/syscall.h:577:#define	SYS_pivot_root     537
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/syscall.h:577:#define	SYS_pivot_root     537
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/sys/syscall.h:577:#define	SYS_pivot_root     537
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/syscall.h:577:#define	SYS_pivot_root     537
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/sys/syscall.h:577:#define	SYS_pivot_root     537
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/syscall.h:577:#define	SYS_pivot_root     537
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/sys/syscall.h:577:#define	SYS_pivot_root     537
$ grepc -k pivot_root / 2>/dev/null
/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/sysproto.h:2977:int pivot_root(struct proc *, struct pivot_root_args *, int *);
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/sysproto.h:2990:int pivot_root(struct proc *, struct pivot_root_args *, int *);
/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/sysproto.h:3012:int pivot_root(struct proc *, struct pivot_root_args *, int *);
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/sysproto.h:2977:int pivot_root(struct proc *, struct pivot_root_args *, int *);
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/sysproto.h:2990:int pivot_root(struct proc *, struct pivot_root_args *, int *);
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/sysproto.h:3012:int pivot_root(struct proc *, struct pivot_root_args *, int *);

exists but is undocumented:

$ man -k pivot_root
pivot_root: nothing appropriate
$ man -K pivot_root
$ echo $?
1

And most importantly, it differs from Linux's pivot_root(2):

SYNOPSIS
       #include <sys/syscall.h>      /* Definition of SYS_* constants */
       #include <unistd.h>

       int syscall(SYS_pivot_root, const char *new_root, const char *put_old);

       Note: glibc provides no wrapper for pivot_root(), necessitating the use
       of syscall(2).

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions