1# -*- Autoconf -*-
2
3# SYNOPSIS
4#
5#   AX_CHECK_USER_NAMESPACE
6#
7# DESCRIPTION
8#
9#   This macro checks whether the local system supports Linux user namespaces.
10#   If so, it calls AC_DEFINE(HAVE_USER_NAMESPACE).
11#
12# Copyright (C) The c-ares team
13# SPDX-License-Identifier: MIT
14
15AC_DEFUN([AX_CHECK_USER_NAMESPACE],[dnl
16 AC_CACHE_CHECK([whether user namespaces are supported],
17  ax_cv_user_namespace,[
18  AC_LANG_PUSH([C])
19  AC_RUN_IFELSE([AC_LANG_SOURCE([[
20#define _GNU_SOURCE
21#include <fcntl.h>
22#include <sched.h>
23#include <signal.h>
24#include <stdio.h>
25#include <string.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28
29int userfn(void *d) {
30  usleep(100000);  /* synchronize by sleep */
31  return (getuid() != 0);
32}
33char userst[1024*1024];
34int main() {
35  char buffer[1024];
36  int rc, status, fd;
37  pid_t child = clone(userfn, userst + 1024*1024, CLONE_NEWUSER|SIGCHLD, 0);
38  if (child < 0) return 1;
39
40  snprintf(buffer, sizeof(buffer), "/proc/%d/uid_map", child);
41  fd = open(buffer, O_CREAT|O_WRONLY|O_TRUNC, 0755);
42  snprintf(buffer, sizeof(buffer), "0 %d 1\n", getuid());
43  write(fd, buffer, strlen(buffer));
44  close(fd);
45
46  rc = waitpid(child, &status, 0);
47  if (rc <= 0) return 1;
48  if (!WIFEXITED(status)) return 1;
49  return WEXITSTATUS(status);
50}
51  ]])],[ax_cv_user_namespace=yes],[ax_cv_user_namespace=no],[ax_cv_user_namespace=no])
52 AC_LANG_POP([C])
53 ])
54 if test "$ax_cv_user_namespace" = yes; then
55   AC_DEFINE([HAVE_USER_NAMESPACE],[1],[Whether user namespaces are available])
56 fi
57]) # AX_CHECK_USER_NAMESPACE
58