github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/os/user/cgo_lookup_cgo.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build cgo && !osusergo && unix && !android && !darwin 6 7 package user 8 9 import ( 10 "syscall" 11 ) 12 13 /* 14 #cgo solaris CFLAGS: -D_POSIX_PTHREAD_SEMANTICS 15 #cgo CFLAGS: -fno-stack-protector 16 #include <unistd.h> 17 #include <sys/types.h> 18 #include <pwd.h> 19 #include <grp.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 static struct passwd mygetpwuid_r(int uid, char *buf, size_t buflen, int *found, int *perr) { 24 struct passwd pwd; 25 struct passwd *result; 26 memset (&pwd, 0, sizeof(pwd)); 27 *perr = getpwuid_r(uid, &pwd, buf, buflen, &result); 28 *found = result != NULL; 29 return pwd; 30 } 31 32 static struct passwd mygetpwnam_r(const char *name, char *buf, size_t buflen, int *found, int *perr) { 33 struct passwd pwd; 34 struct passwd *result; 35 memset(&pwd, 0, sizeof(pwd)); 36 *perr = getpwnam_r(name, &pwd, buf, buflen, &result); 37 *found = result != NULL; 38 return pwd; 39 } 40 41 static struct group mygetgrgid_r(int gid, char *buf, size_t buflen, int *found, int *perr) { 42 struct group grp; 43 struct group *result; 44 memset(&grp, 0, sizeof(grp)); 45 *perr = getgrgid_r(gid, &grp, buf, buflen, &result); 46 *found = result != NULL; 47 return grp; 48 } 49 50 static struct group mygetgrnam_r(const char *name, char *buf, size_t buflen, int *found, int *perr) { 51 struct group grp; 52 struct group *result; 53 memset(&grp, 0, sizeof(grp)); 54 *perr = getgrnam_r(name, &grp, buf, buflen, &result); 55 *found = result != NULL; 56 return grp; 57 } 58 */ 59 import "C" 60 61 type _C_char = C.char 62 type _C_int = C.int 63 type _C_gid_t = C.gid_t 64 type _C_uid_t = C.uid_t 65 type _C_size_t = C.size_t 66 type _C_struct_group = C.struct_group 67 type _C_struct_passwd = C.struct_passwd 68 type _C_long = C.long 69 70 func _C_pw_uid(p *_C_struct_passwd) _C_uid_t { return p.pw_uid } 71 func _C_pw_uidp(p *_C_struct_passwd) *_C_uid_t { return &p.pw_uid } 72 func _C_pw_gid(p *_C_struct_passwd) _C_gid_t { return p.pw_gid } 73 func _C_pw_gidp(p *_C_struct_passwd) *_C_gid_t { return &p.pw_gid } 74 func _C_pw_name(p *_C_struct_passwd) *_C_char { return p.pw_name } 75 func _C_pw_gecos(p *_C_struct_passwd) *_C_char { return p.pw_gecos } 76 func _C_pw_dir(p *_C_struct_passwd) *_C_char { return p.pw_dir } 77 78 func _C_gr_gid(g *_C_struct_group) _C_gid_t { return g.gr_gid } 79 func _C_gr_name(g *_C_struct_group) *_C_char { return g.gr_name } 80 81 func _C_GoString(p *_C_char) string { return C.GoString(p) } 82 83 func _C_getpwnam_r(name *_C_char, buf *_C_char, size _C_size_t) (pwd _C_struct_passwd, found bool, errno syscall.Errno) { 84 var f, e _C_int 85 pwd = C.mygetpwnam_r(name, buf, size, &f, &e) 86 return pwd, f != 0, syscall.Errno(e) 87 } 88 89 func _C_getpwuid_r(uid _C_uid_t, buf *_C_char, size _C_size_t) (pwd _C_struct_passwd, found bool, errno syscall.Errno) { 90 var f, e _C_int 91 pwd = C.mygetpwuid_r(_C_int(uid), buf, size, &f, &e) 92 return pwd, f != 0, syscall.Errno(e) 93 } 94 95 func _C_getgrnam_r(name *_C_char, buf *_C_char, size _C_size_t) (grp _C_struct_group, found bool, errno syscall.Errno) { 96 var f, e _C_int 97 grp = C.mygetgrnam_r(name, buf, size, &f, &e) 98 return grp, f != 0, syscall.Errno(e) 99 } 100 101 func _C_getgrgid_r(gid _C_gid_t, buf *_C_char, size _C_size_t) (grp _C_struct_group, found bool, errno syscall.Errno) { 102 var f, e _C_int 103 grp = C.mygetgrgid_r(_C_int(gid), buf, size, &f, &e) 104 return grp, f != 0, syscall.Errno(e) 105 } 106 107 const ( 108 _C__SC_GETPW_R_SIZE_MAX = C._SC_GETPW_R_SIZE_MAX 109 _C__SC_GETGR_R_SIZE_MAX = C._SC_GETGR_R_SIZE_MAX 110 ) 111 112 func _C_sysconf(key _C_int) _C_long { return C.sysconf(key) }