| 1 | |
| 2 | |
| 3 | |
| 4 | package osutil |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io/fs" |
| 11 | "os" |
| 12 | "syscall" |
| 13 | |
| 14 | "golang.org/x/sys/windows" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | const UnixPathMax = 108 |
| 19 | |
| 20 | |
| 21 | type Stat struct { |
| 22 | Uid uint32 |
| 23 | Gid uint32 |
| 24 | } |
| 25 | |
| 26 | func SysStat(_ fs.FileInfo) (Stat, bool) { |
| 27 | return Stat{Uid: 0, Gid: 0}, false |
| 28 | } |
| 29 | |
| 30 | |
| 31 | const SigInt = Signal(2) |
| 32 | |
| 33 | |
| 34 | const SigTerm = Signal(15) |
| 35 | |
| 36 | |
| 37 | const SigKill = Signal(9) |
| 38 | |
| 39 | type Signal int |
| 40 | |
| 41 | func SysKill(pid int, _ Signal) error { |
| 42 | return windows.GenerateConsoleCtrlEvent(syscall.CTRL_BREAK_EVENT, uint32(pid)) |
| 43 | } |
| 44 | |
| 45 | |
| 46 | func ProcessAlive(pid int) bool { |
| 47 | h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid)) |
| 48 | if err != nil { |
| 49 | return false |
| 50 | } |
| 51 | var exitCode uint32 |
| 52 | err = windows.GetExitCodeProcess(h, &exitCode) |
| 53 | _ = windows.CloseHandle(h) |
| 54 | if err != nil { |
| 55 | return false |
| 56 | } |
| 57 | return exitCode == 259 |
| 58 | } |
| 59 | |
| 60 | func Dup2(_ int, _ syscall.Handle) error { |
| 61 | return errors.New("unimplemented") |
| 62 | } |
| 63 | |
| 64 | func SignalName(sig os.Signal) string { |
| 65 | switch sig { |
| 66 | case syscall.SIGINT: |
| 67 | return "SIGINT" |
| 68 | case syscall.SIGTERM: |
| 69 | return "SIGTERM" |
| 70 | default: |
| 71 | return fmt.Sprintf("Signal(%d)", sig) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func Sysctl(_ context.Context, _ string) (string, error) { |
| 76 | return "", errors.New("sysctl: unimplemented on Windows") |
| 77 | } |
| 78 | |
| 79 | func IsEACCES(err error) bool { |
| 80 | return errors.Is(err, syscall.ERROR_ACCESS_DENIED) || errors.Is(err, syscall.WSAEACCES) |
| 81 | } |