1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <unistd.h>
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/types.h>
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <pwd.h>
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <grp.h>
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
88290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#ifdef HAVE_SELINUX
98290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <selinux/selinux.h>
108290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#endif
118290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void print_uid(uid_t uid)
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct passwd *pw = getpwuid(uid);
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (pw) {
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("%d(%s)", uid, pw->pw_name);
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("%d",uid);
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void print_gid(gid_t gid)
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct group *gr = getgrgid(gid);
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (gr) {
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("%d(%s)", gid, gr->gr_name);
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("%d",gid);
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint id_main(int argc, char **argv)
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    gid_t list[64];
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int n, max;
378290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#ifdef HAVE_SELINUX
388290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    char *secctx;
398290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#endif
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    max = getgroups(64, list);
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (max < 0) max = 0;
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    printf("uid=");
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    print_uid(getuid());
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    printf(" gid=");
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    print_gid(getgid());
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (max) {
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf(" groups=");
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        print_gid(list[0]);
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        for(n = 1; n < max; n++) {
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            printf(",");
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            print_gid(list[n]);
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
568290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#ifdef HAVE_SELINUX
578290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    if (getcon(&secctx) == 0) {
588290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        printf(" context=%s", secctx);
598290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        free(secctx);
608290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    }
618290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#endif
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    printf("\n");
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
65