github.com/tych0/umoci@v0.4.2/third_party/shared/util.go (about)

     1  /*
     2   * lxd: daemon based on liblxd with a REST API
     3   * Copyright (C) 2015-2017 LXD Authors
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  // This code was copied from https://github.com/lxc/lxd, which is available
    19  // under the the Apache 2.0 license (as noted above). The version of this code
    20  // comes from the tag lxd-2.21 at /shared/util.go.
    21  
    22  package shared
    23  
    24  import (
    25  	"bufio"
    26  	"fmt"
    27  	"os"
    28  )
    29  
    30  // RunningInUserNS returns whether the current process is (likely) inside a
    31  // user namespace. This has a possible false-negative (where it will return
    32  // false while inside a user namespace if it was intentionally configured to be
    33  // confusing to programs like this).
    34  func RunningInUserNS() bool {
    35  	file, err := os.Open("/proc/self/uid_map")
    36  	if err != nil {
    37  		return false
    38  	}
    39  	defer file.Close()
    40  
    41  	buf := bufio.NewReader(file)
    42  	l, _, err := buf.ReadLine()
    43  	if err != nil {
    44  		return false
    45  	}
    46  
    47  	line := string(l)
    48  	var a, b, c int64
    49  	fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
    50  	if a == 0 && b == 0 && c == 4294967295 {
    51  		return false
    52  	}
    53  	return true
    54  }