Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/osutil/osutil_unix.go
2609 views
1
//go:build !windows
2
3
// SPDX-FileCopyrightText: Copyright The Lima Authors
4
// SPDX-License-Identifier: Apache-2.0
5
6
package osutil
7
8
import (
9
"bytes"
10
"context"
11
"errors"
12
"fmt"
13
"os"
14
"os/exec"
15
"strings"
16
"syscall"
17
18
"golang.org/x/sys/unix"
19
)
20
21
func Dup2(oldfd, newfd int) (err error) {
22
return unix.Dup2(oldfd, newfd)
23
}
24
25
func SignalName(sig os.Signal) string {
26
return unix.SignalName(sig.(syscall.Signal))
27
}
28
29
func Sysctl(ctx context.Context, name string) (string, error) {
30
var stderrBuf bytes.Buffer
31
cmd := exec.CommandContext(ctx, "sysctl", "-n", name)
32
cmd.Stderr = &stderrBuf
33
stdout, err := cmd.Output()
34
if err != nil {
35
return "", fmt.Errorf("failed to run %v: %w (stdout=%q, stderr=%q)", cmd.Args, err,
36
string(stdout), stderrBuf.String())
37
}
38
return strings.TrimSuffix(string(stdout), "\n"), nil
39
}
40
41
func IsEACCES(err error) bool {
42
return errors.Is(err, unix.EACCES)
43
}
44
45