1# -*- Autoconf -*- 2 3# SYNOPSIS 4# 5# AX_CHECK_UTS_NAMESPACE 6# 7# DESCRIPTION 8# 9# This macro checks whether the local system supports Linux UTS namespaces. 10# Also requires user namespaces to be available, so that non-root users 11# can enter the namespace. 12# If so, it calls AC_DEFINE(HAVE_UTS_NAMESPACE). 13 14AC_DEFUN([AX_CHECK_UTS_NAMESPACE],[dnl 15 AC_CACHE_CHECK([whether UTS namespaces are supported], 16 ax_cv_uts_namespace,[ 17 AC_LANG_PUSH([C]) 18 AC_RUN_IFELSE([AC_LANG_SOURCE([[ 19#define _GNU_SOURCE 20#include <sched.h> 21#include <signal.h> 22#include <stdio.h> 23#include <string.h> 24#include <fcntl.h> 25#include <unistd.h> 26#include <sys/types.h> 27#include <sys/wait.h> 28 29int utsfn(void *d) { 30 char buffer[1024]; 31 const char *name = "autoconftest"; 32 int rc = sethostname(name, strlen(name)); 33 if (rc != 0) return 1; 34 gethostname(buffer, 1024); 35 return (strcmp(buffer, name) != 0); 36} 37 38char st2[1024*1024]; 39int fn(void *d) { 40 pid_t child; 41 int rc, status; 42 usleep(100000); /* synchronize by sleep */ 43 if (getuid() != 0) return 1; 44 child = clone(utsfn, st2 + 1024*1024, CLONE_NEWUTS|SIGCHLD, 0); 45 if (child < 0) return 1; 46 rc = waitpid(child, &status, 0); 47 if (rc <= 0) return 1; 48 if (!WIFEXITED(status)) return 1; 49 return WEXITSTATUS(status); 50} 51char st[1024*1024]; 52int main() { 53 char buffer[1024]; 54 int rc, status, fd; 55 pid_t child = clone(fn, st + 1024*1024, CLONE_NEWUSER|SIGCHLD, 0); 56 if (child < 0) return 1; 57 58 sprintf(buffer, "/proc/%d/uid_map", child); 59 fd = open(buffer, O_CREAT|O_WRONLY|O_TRUNC, 0755); 60 sprintf(buffer, "0 %d 1\n", getuid()); 61 write(fd, buffer, strlen(buffer)); 62 close(fd); 63 64 rc = waitpid(child, &status, 0); 65 if (rc <= 0) return 1; 66 if (!WIFEXITED(status)) return 1; 67 return WEXITSTATUS(status); 68} 69]]) 70 ],[ax_cv_uts_namespace=yes],[ax_cv_uts_namespace=no],[ax_cv_uts_namespace=no]) 71 AC_LANG_POP([C]) 72 ]) 73 if test "$ax_cv_uts_namespace" = yes; then 74 AC_DEFINE([HAVE_UTS_NAMESPACE],[1],[Whether UTS namespaces are available]) 75 fi 76]) # AX_CHECK_UTS_NAMESPACE 77