| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #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 Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 8 | #ifdef HAVE_SELINUX | 
|  | 9 | #include <selinux/selinux.h> | 
|  | 10 | #endif | 
|  | 11 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 12 | static 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 |  | 
|  | 23 | static 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 |  | 
|  | 33 | int id_main(int argc, char **argv) | 
|  | 34 | { | 
|  | 35 | gid_t list[64]; | 
|  | 36 | int n, max; | 
| Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 37 | #ifdef HAVE_SELINUX | 
|  | 38 | char *secctx; | 
|  | 39 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 |  | 
|  | 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 Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 56 | #ifdef HAVE_SELINUX | 
|  | 57 | if (getcon(&secctx) == 0) { | 
|  | 58 | printf(" context=%s", secctx); | 
|  | 59 | free(secctx); | 
|  | 60 | } | 
|  | 61 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | printf("\n"); | 
|  | 63 | return 0; | 
|  | 64 | } |