github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/sys/syscall.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package sys 21 22 import ( 23 "os" 24 "syscall" 25 "unsafe" 26 ) 27 28 // FlagID can be passed to chown-ish functions to mean "no change", 29 // and can be returned from getuid-ish functions to mean "not found". 30 const FlagID = 1<<32 - 1 31 32 // UserID is the type of the system's user identifiers (in C, uid_t). 33 // 34 // We give it its own explicit type so you don't have to remember that 35 // it's a uint32 (which lead to the bug this package fixes in the 36 // first place) 37 type UserID uint32 38 39 // GroupID is the type of the system's group identifiers (in C, gid_t). 40 type GroupID uint32 41 42 // uid_t is an unsigned 32-bit integer in linux right now. 43 // so syscall.Gete?[ug]id are wrong, and break in 32 bits 44 // (see https://github.com/golang/go/issues/22739) 45 func Getuid() UserID { 46 return UserID(getid(_SYS_GETUID)) 47 } 48 49 func Geteuid() UserID { 50 return UserID(getid(_SYS_GETEUID)) 51 } 52 53 func Getgid() GroupID { 54 return GroupID(getid(_SYS_GETGID)) 55 } 56 57 func Getegid() GroupID { 58 return GroupID(getid(_SYS_GETEGID)) 59 } 60 61 func getid(id uintptr) uint32 { 62 // these are documented as not failing, but see golang#22924 63 r0, _, errno := syscall.RawSyscall(id, 0, 0, 0) 64 if errno != 0 { 65 return uint32(-errno) 66 } 67 return uint32(r0) 68 } 69 70 func Chown(f *os.File, uid UserID, gid GroupID) error { 71 return Fchown(int(f.Fd()), uid, gid) 72 } 73 74 func Fchown(fd int, uid UserID, gid GroupID) error { 75 _, _, errno := syscall.Syscall(syscall.SYS_FCHOWN, uintptr(fd), uintptr(uid), uintptr(gid)) 76 if errno == 0 { 77 return nil 78 } 79 return errno 80 } 81 82 func ChownPath(path string, uid UserID, gid GroupID) error { 83 AT_FDCWD := -100 // also written as -0x64 in ztypes_linux_*.go (but -100 in sys_linux_*.s, and /usr/include/linux/fcntl.h) 84 return FchownAt(uintptr(AT_FDCWD), path, uid, gid, 0) 85 } 86 87 func FchownAt(dirfd uintptr, path string, uid UserID, gid GroupID, flags int) error { 88 p0, err := syscall.BytePtrFromString(path) 89 if err != nil { 90 return err 91 } 92 _, _, errno := syscall.Syscall6(_SYS_FCHOWNAT, dirfd, uintptr(unsafe.Pointer(p0)), uintptr(uid), uintptr(gid), uintptr(flags), 0) 93 if errno == 0 { 94 return nil 95 } 96 return errno 97 } 98 99 // As of Go 1.9, the O_PATH constant does not seem to be declared 100 // uniformly over all archtiectures. 101 const O_PATH = 0x200000 102 103 func FcntlGetFl(fd int) (int, error) { 104 flags, _, errno := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), uintptr(syscall.F_GETFL), 0) 105 if errno != 0 { 106 return 0, errno 107 } 108 return int(flags), nil 109 }