unixsock_readmsg_cloexec.go 662 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2021 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. //go:build aix || darwin || freebsd || hurd || solaris
  5. package net
  6. import "syscall"
  7. const readMsgFlags = 0
  8. func setReadMsgCloseOnExec(oob []byte) {
  9. scms, err := syscall.ParseSocketControlMessage(oob)
  10. if err != nil {
  11. return
  12. }
  13. for _, scm := range scms {
  14. if scm.Header.Level == syscall.SOL_SOCKET && scm.Header.Type == syscall.SCM_RIGHTS {
  15. fds, err := syscall.ParseUnixRights(&scm)
  16. if err != nil {
  17. continue
  18. }
  19. for _, fd := range fds {
  20. syscall.CloseOnExec(fd)
  21. }
  22. }
  23. }
  24. }