diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-02-08 13:03:45 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-02-08 13:03:45 +0900 |
| commit | e14923ae530dda0601c204fe17ac1956f54a45b0 (patch) | |
| tree | 1578bbc4c08d9afc94c4b009cd2bf64260574f05 /internal/proc/fd.go | |
| parent | 7aff3ead3afaa7461e0054fbef23fb571b4add0a (diff) | |
helper/proc: move package out of internal
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/proc/fd.go')
| -rw-r--r-- | internal/proc/fd.go | 45 |
1 files changed, 0 insertions, 45 deletions
diff --git a/internal/proc/fd.go b/internal/proc/fd.go deleted file mode 100644 index e99abb38..00000000 --- a/internal/proc/fd.go +++ /dev/null @@ -1,45 +0,0 @@ -package proc - -import ( - "encoding/gob" - "errors" - "os" - "strconv" -) - -var ( - ErrNotSet = errors.New("environment variable not set") - ErrInvalid = errors.New("bad file descriptor") -) - -// Setup appends the read end of a pipe for payload transmission and returns its fd. -func Setup(extraFiles *[]*os.File) (int, *gob.Encoder, error) { - if r, w, err := os.Pipe(); err != nil { - return -1, nil, err - } else { - fd := 3 + len(*extraFiles) - *extraFiles = append(*extraFiles, r) - return fd, gob.NewEncoder(w), nil - } -} - -// Receive retrieves payload pipe fd from the environment, -// receives its payload and returns the Close method of the pipe. -func Receive(key string, e any) (func() error, error) { - var setup *os.File - - if s, ok := os.LookupEnv(key); !ok { - return nil, ErrNotSet - } else { - if fd, err := strconv.Atoi(s); err != nil { - return nil, err - } else { - setup = os.NewFile(uintptr(fd), "setup") - if setup == nil { - return nil, ErrInvalid - } - } - } - - return func() error { return setup.Close() }, gob.NewDecoder(setup).Decode(e) -} |
