1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:47:44 +00:00

Add some basic setgroups(), getgroups() and initgroups().

Also teach /bin/id to print the user's supplemental groups.
This commit is contained in:
Andreas Kling 2018-11-07 01:38:51 +01:00
parent d3bd3791cb
commit a7f1d892a9
12 changed files with 167 additions and 19 deletions

View file

@ -1,7 +1,8 @@
#include <LibC/unistd.h>
#include <LibC/stdio.h>
#include <LibC/pwd.h>
#include <LibC/grp.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <alloca.h>
int main(int c, char** v)
{
@ -11,7 +12,28 @@ int main(int c, char** v)
struct passwd* pw = getpwuid(uid);
struct group* gr = getgrgid(gid);
printf("uid=%u(%s), gid=%u(%s)\n", uid, pw ? pw->pw_name : "n/a", gid, gr ? gr->gr_name : "n/a", getpid());
printf("uid=%u(%s), gid=%u(%s)", uid, pw ? pw->pw_name : "n/a", gid, gr ? gr->gr_name : "n/a", getpid());
int extra_gid_count = getgroups(0, nullptr);
if (extra_gid_count) {
auto* extra_gids = (gid_t*)alloca(extra_gid_count * sizeof(gid_t));
int rc = getgroups(extra_gid_count, extra_gids);
if (rc < 0) {
perror("\ngetgroups");
return 1;
}
printf(",");
for (int g = 0; g < extra_gid_count; ++g) {
auto* gr = getgrgid(extra_gids[g]);
if (gr)
printf("%u(%s)", extra_gids[g], gr->gr_name);
else
printf("%u", extra_gids[g]);
if (g != extra_gid_count - 1)
printf(",");
}
}
printf("\n");
return 0;
}