| 1 |
// Copyright (c) 2017, Andrey Nering <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
//go:build unix |
| 5 |
|
| 6 |
package interp |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"errors" |
| 11 |
"os/user" |
| 12 |
"strconv" |
| 13 |
"syscall" |
| 14 |
|
| 15 |
"golang.org/x/sys/unix" |
| 16 |
"mvdan.cc/sh/v3/syntax" |
| 17 |
) |
| 18 |
|
| 19 |
func mkfifo(path string, mode uint32) error { |
| 20 |
return unix.Mkfifo(path, mode) |
| 21 |
} |
| 22 |
|
| 23 |
// defaultAccess is similar to checking the permission bits from [io/fs.FileInfo], |
| 24 |
// but it also takes into account the current user's role. |
| 25 |
func defaultAccess(ctx context.Context, path string, mode AccessMode) error { |
| 26 |
return unix.Access(path, uint32(mode)) |
| 27 |
} |
| 28 |
|
| 29 |
// unTestOwnOrGrp implements the -O and -G unary tests. If the file does not |
| 30 |
// exist, or the current user cannot be retrieved, returns false. |
| 31 |
func (r *Runner) unTestOwnOrGrp(ctx context.Context, op syntax.UnTestOperator, x string) bool { |
| 32 |
info, err := r.stat(ctx, x) |
| 33 |
if err != nil { |
| 34 |
return false |
| 35 |
} |
| 36 |
u, err := user.Current() |
| 37 |
if err != nil { |
| 38 |
return false |
| 39 |
} |
| 40 |
if op == syntax.TsUsrOwn { |
| 41 |
uid, _ := strconv.Atoi(u.Uid) |
| 42 |
return uint32(uid) == info.Sys().(*syscall.Stat_t).Uid |
| 43 |
} |
| 44 |
gid, _ := strconv.Atoi(u.Gid) |
| 45 |
return uint32(gid) == info.Sys().(*syscall.Stat_t).Gid |
| 46 |
} |
| 47 |
|
| 48 |
type waitStatus = syscall.WaitStatus |
| 49 |
|
| 50 |
// isENOEXEC reports whether the kernel refused to execute a file |
| 51 |
// with ENOEXEC, e.g. a script without a shebang line. |
| 52 |
func isENOEXEC(err error) bool { return errors.Is(err, syscall.ENOEXEC) } |
| 53 |
|
| 54 |
// isETXTBSY reports whether the kernel refused to execute a file |
| 55 |
// with ETXTBSY, i.e. a process holds it open for writing. |
| 56 |
func isETXTBSY(err error) bool { return errors.Is(err, syscall.ETXTBSY) } |