summaryrefslogtreecommitdiff
path: root/src/lib/os/user.go
blob: 194aa50e445133c6b4e2e6f5b7c26bcb174f4b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// User ids etc.

package os

import (
	"syscall";
	"os";
	"unsafe";
)

// Getuid returns the numeric user id of the caller.
func Getuid() int {
	u, r2, e := syscall.Syscall(syscall.SYS_GETUID, 0, 0, 0);
	return int(u);
}

// Geteuid returns the numeric effective user id of the caller.
func Geteuid() int {
	u, r2, e := syscall.Syscall(syscall.SYS_GETEUID, 0, 0, 0);
	return int(u);
}

// Getgid returns the numeric group id of the caller.
func Getgid() int {
	g, r2, e := syscall.Syscall(syscall.SYS_GETGID, 0, 0, 0);
	return int(g);
}

// Getegid returns the numeric effective group id of the caller.
func Getegid() int {
	g, r2, e := syscall.Syscall(syscall.SYS_GETEGID, 0, 0, 0);
	return int(g);
}

// Getgroups returns a list of the numeric ids of groups that the caller belongs to.
func Getgroups() ([]int, os.Error) {
	// first call asks how many there are.
	r1, r2, err := syscall.Syscall(syscall.SYS_GETGROUPS, 0, 0, 0);
	if err != 0 {
		return nil, ErrnoToError(err);
	}

	// Sanity check group count.
	// On Linux, max is 1<<16; on BSD, OS X, max is 16.
	if r1 < 0 || r1 > 1<<20 {
		return nil, EINVAL;
	}
	a := make([]int, r1);
	if r1 > 0 {
		tmp := make([]uint32, r1);
		r1, r2, err = syscall.Syscall(syscall.SYS_GETGROUPS, r1, int64(uintptr(unsafe.Pointer(&tmp[0]))), 0);
		if err != 0 {
			return nil, ErrnoToError(err);
		}
		for i := 0; i < len(a); i++ {
			a[i] = int(tmp[i]);
		}
	}
	return a[0:r1], nil;
}