blob: bc792887c12ac3c2263c79eca5a7c02450e5e2b0 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/types.h>
5#include <pwd.h>
6#include <grp.h>
7
Stephen Smalley8290d102012-01-13 08:53:56 -05008#ifdef HAVE_SELINUX
9#include <selinux/selinux.h>
10#endif
11
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012static void print_uid(uid_t uid)
13{
14 struct passwd *pw = getpwuid(uid);
15
16 if (pw) {
17 printf("%d(%s)", uid, pw->pw_name);
18 } else {
19 printf("%d",uid);
20 }
21}
22
23static void print_gid(gid_t gid)
24{
25 struct group *gr = getgrgid(gid);
26 if (gr) {
27 printf("%d(%s)", gid, gr->gr_name);
28 } else {
29 printf("%d",gid);
30 }
31}
32
33int id_main(int argc, char **argv)
34{
35 gid_t list[64];
36 int n, max;
Stephen Smalley8290d102012-01-13 08:53:56 -050037#ifdef HAVE_SELINUX
38 char *secctx;
39#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
41 max = getgroups(64, list);
42 if (max < 0) max = 0;
43
44 printf("uid=");
45 print_uid(getuid());
46 printf(" gid=");
47 print_gid(getgid());
48 if (max) {
49 printf(" groups=");
50 print_gid(list[0]);
51 for(n = 1; n < max; n++) {
52 printf(",");
53 print_gid(list[n]);
54 }
55 }
Stephen Smalley8290d102012-01-13 08:53:56 -050056#ifdef HAVE_SELINUX
57 if (getcon(&secctx) == 0) {
58 printf(" context=%s", secctx);
59 free(secctx);
60 }
61#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062 printf("\n");
63 return 0;
64}