Source file src/runtime/fds_unix.go

     1  // Copyright 2023 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  //go:build unix
     6  
     7  package runtime
     8  
     9  func checkfds() {
    10  	if islibrary || isarchive {
    11  		// If the program is actually a library, presumably being consumed by
    12  		// another program, we don't want to mess around with the file
    13  		// descriptors.
    14  		return
    15  	}
    16  
    17  	const (
    18  		// F_GETFD, EBADF, O_RDWR are standard across all unixes we support, so
    19  		// we define them here rather than in each of the OS specific files.
    20  		F_GETFD = 0x01
    21  		EBADF   = 0x09
    22  		O_RDWR  = 0x02
    23  	)
    24  
    25  	devNull := []byte("/dev/null\x00")
    26  	for i := 0; i < 3; i++ {
    27  		ret, errno := fcntl(int32(i), F_GETFD, 0)
    28  		if ret >= 0 {
    29  			continue
    30  		}
    31  
    32  		if errno != EBADF {
    33  			print("runtime: unexpected error while checking standard file descriptor ", i, ", errno=", errno, "\n")
    34  			throw("cannot open standard fds")
    35  		}
    36  
    37  		if ret := open(&devNull[0], O_RDWR, 0); ret < 0 {
    38  			print("runtime: standard file descriptor ", i, " closed, unable to open /dev/null, errno=", errno, "\n")
    39  			throw("cannot open standard fds")
    40  		} else if ret != int32(i) {
    41  			print("runtime: opened unexpected file descriptor ", ret, " when attempting to open ", i, "\n")
    42  			throw("cannot open standard fds")
    43  		}
    44  	}
    45  }
    46  

View as plain text