summaryrefslogtreecommitdiff
path: root/src/nshd/hackers_git/name2gid.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2015-08-29 23:19:55 -0600
committerLuke Shumaker <lukeshu@sbcglobal.net>2015-08-29 23:19:55 -0600
commit3bca4faef65f9ba97abad49505e10c675894c559 (patch)
tree4f067137658d0e1e2e676be64b12a9d85ea23dd5 /src/nshd/hackers_git/name2gid.go
parent5fd4b54f3a833bdfabf067c4abafa56c11e02ab1 (diff)
stuff
Diffstat (limited to 'src/nshd/hackers_git/name2gid.go')
-rw-r--r--src/nshd/hackers_git/name2gid.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/nshd/hackers_git/name2gid.go b/src/nshd/hackers_git/name2gid.go
new file mode 100644
index 0000000..b33e6aa
--- /dev/null
+++ b/src/nshd/hackers_git/name2gid.go
@@ -0,0 +1,41 @@
+package hackers_git
+
+import (
+ "syscall"
+ "unsafe"
+)
+
+//#include <stdlib.h> /* free */
+//#include <unistd.h> /* sysconf */
+//#include <grp.h> /* getgrnam_r */
+import "C"
+
+func name2gid(name string) (gid int32) {
+ nameC := C.CString(name)
+ defer C.free(unsafe.Pointer(nameC))
+
+ gid = -1
+ buflen := C.sysconf(C._SC_GETGR_R_SIZE_MAX)
+ if buflen < 1 {
+ buflen = 1024
+ }
+ buf := make([]byte, buflen)
+ var grp C.struct_group
+ var ret *C.struct_group
+ for {
+ success, errno := C.getgrnam_r(nameC, &grp, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(buflen), &ret)
+ if success >= 0 {
+ break
+ }
+ if errno == syscall.ERANGE {
+ buflen += 256
+ buf = make([]byte, buflen)
+ } else {
+ return
+ }
+ }
+ if ret != nil {
+ gid = int32(ret.gr_gid)
+ }
+ return
+}