11cb0ef41Sopenharmony_ci# -*- Autoconf -*-
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci# SYNOPSIS
41cb0ef41Sopenharmony_ci#
51cb0ef41Sopenharmony_ci#   AX_CHECK_USER_NAMESPACE
61cb0ef41Sopenharmony_ci#
71cb0ef41Sopenharmony_ci# DESCRIPTION
81cb0ef41Sopenharmony_ci#
91cb0ef41Sopenharmony_ci#   This macro checks whether the local system supports Linux user namespaces.
101cb0ef41Sopenharmony_ci#   If so, it calls AC_DEFINE(HAVE_USER_NAMESPACE).
111cb0ef41Sopenharmony_ci#
121cb0ef41Sopenharmony_ci# Copyright (C) The c-ares team
131cb0ef41Sopenharmony_ci# SPDX-License-Identifier: MIT
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciAC_DEFUN([AX_CHECK_USER_NAMESPACE],[dnl
161cb0ef41Sopenharmony_ci AC_CACHE_CHECK([whether user namespaces are supported],
171cb0ef41Sopenharmony_ci  ax_cv_user_namespace,[
181cb0ef41Sopenharmony_ci  AC_LANG_PUSH([C])
191cb0ef41Sopenharmony_ci  AC_RUN_IFELSE([AC_LANG_SOURCE([[
201cb0ef41Sopenharmony_ci#define _GNU_SOURCE
211cb0ef41Sopenharmony_ci#include <fcntl.h>
221cb0ef41Sopenharmony_ci#include <sched.h>
231cb0ef41Sopenharmony_ci#include <signal.h>
241cb0ef41Sopenharmony_ci#include <stdio.h>
251cb0ef41Sopenharmony_ci#include <string.h>
261cb0ef41Sopenharmony_ci#include <sys/types.h>
271cb0ef41Sopenharmony_ci#include <sys/wait.h>
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciint userfn(void *d) {
301cb0ef41Sopenharmony_ci  usleep(100000);  /* synchronize by sleep */
311cb0ef41Sopenharmony_ci  return (getuid() != 0);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_cichar userst[1024*1024];
341cb0ef41Sopenharmony_ciint main() {
351cb0ef41Sopenharmony_ci  char buffer[1024];
361cb0ef41Sopenharmony_ci  int rc, status, fd;
371cb0ef41Sopenharmony_ci  pid_t child = clone(userfn, userst + 1024*1024, CLONE_NEWUSER|SIGCHLD, 0);
381cb0ef41Sopenharmony_ci  if (child < 0) return 1;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  snprintf(buffer, sizeof(buffer), "/proc/%d/uid_map", child);
411cb0ef41Sopenharmony_ci  fd = open(buffer, O_CREAT|O_WRONLY|O_TRUNC, 0755);
421cb0ef41Sopenharmony_ci  snprintf(buffer, sizeof(buffer), "0 %d 1\n", getuid());
431cb0ef41Sopenharmony_ci  write(fd, buffer, strlen(buffer));
441cb0ef41Sopenharmony_ci  close(fd);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  rc = waitpid(child, &status, 0);
471cb0ef41Sopenharmony_ci  if (rc <= 0) return 1;
481cb0ef41Sopenharmony_ci  if (!WIFEXITED(status)) return 1;
491cb0ef41Sopenharmony_ci  return WEXITSTATUS(status);
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci  ]])],[ax_cv_user_namespace=yes],[ax_cv_user_namespace=no],[ax_cv_user_namespace=no])
521cb0ef41Sopenharmony_ci AC_LANG_POP([C])
531cb0ef41Sopenharmony_ci ])
541cb0ef41Sopenharmony_ci if test "$ax_cv_user_namespace" = yes; then
551cb0ef41Sopenharmony_ci   AC_DEFINE([HAVE_USER_NAMESPACE],[1],[Whether user namespaces are available])
561cb0ef41Sopenharmony_ci fi
571cb0ef41Sopenharmony_ci]) # AX_CHECK_USER_NAMESPACE
58