blob: 8ec79c148fe682e09020bf1e44646f0907293fd6 [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>
Stephen Smalley8290d102012-01-13 08:53:56 -05007#include <selinux/selinux.h>
Stephen Smalley8290d102012-01-13 08:53:56 -05008
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009static void print_uid(uid_t uid)
10{
11 struct passwd *pw = getpwuid(uid);
12
13 if (pw) {
14 printf("%d(%s)", uid, pw->pw_name);
15 } else {
16 printf("%d",uid);
17 }
18}
19
20static void print_gid(gid_t gid)
21{
22 struct group *gr = getgrgid(gid);
23 if (gr) {
24 printf("%d(%s)", gid, gr->gr_name);
25 } else {
26 printf("%d",gid);
27 }
28}
29
30int id_main(int argc, char **argv)
31{
32 gid_t list[64];
33 int n, max;
Stephen Smalley8290d102012-01-13 08:53:56 -050034 char *secctx;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
36 max = getgroups(64, list);
37 if (max < 0) max = 0;
38
39 printf("uid=");
40 print_uid(getuid());
41 printf(" gid=");
42 print_gid(getgid());
43 if (max) {
44 printf(" groups=");
45 print_gid(list[0]);
46 for(n = 1; n < max; n++) {
47 printf(",");
48 print_gid(list[n]);
49 }
50 }
Stephen Smalley8290d102012-01-13 08:53:56 -050051 if (getcon(&secctx) == 0) {
52 printf(" context=%s", secctx);
53 free(secctx);
54 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055 printf("\n");
56 return 0;
57}