github.com/go-darwin/sys@v0.0.0-20220510002607-68fd01f054ca/testdata/testprog/syscalls_linux.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"os"
    11  	"syscall"
    12  )
    13  
    14  func gettid() int {
    15  	return syscall.Gettid()
    16  }
    17  
    18  func tidExists(tid int) (exists, supported bool) {
    19  	stat, err := os.ReadFile(fmt.Sprintf("/proc/self/task/%d/stat", tid))
    20  	if os.IsNotExist(err) {
    21  		return false, true
    22  	}
    23  	// Check if it's a zombie thread.
    24  	state := bytes.Fields(stat)[2]
    25  	return !(len(state) == 1 && state[0] == 'Z'), true
    26  }
    27  
    28  func getcwd() (string, error) {
    29  	if !syscall.ImplementsGetwd {
    30  		return "", nil
    31  	}
    32  	// Use the syscall to get the current working directory.
    33  	// This is imperative for checking for OS thread state
    34  	// after an unshare since os.Getwd might just check the
    35  	// environment, or use some other mechanism.
    36  	var buf [4096]byte
    37  	n, err := syscall.Getcwd(buf[:])
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	// Subtract one for null terminator.
    42  	return string(buf[:n-1]), nil
    43  }
    44  
    45  func unshareFs() error {
    46  	err := syscall.Unshare(syscall.CLONE_FS)
    47  	if err != nil {
    48  		errno, ok := err.(syscall.Errno)
    49  		if ok && errno == syscall.EPERM {
    50  			return errNotPermitted
    51  		}
    52  	}
    53  	return err
    54  }
    55  
    56  func chdir(path string) error {
    57  	return syscall.Chdir(path)
    58  }