diff options
-rw-r--r-- | utils/hurdids.c | 172 |
1 files changed, 102 insertions, 70 deletions
diff --git a/utils/hurdids.c b/utils/hurdids.c index b647b4ce..39dee9d1 100644 --- a/utils/hurdids.c +++ b/utils/hurdids.c @@ -29,7 +29,7 @@ #include <version.h> #include <error.h> -#include <hurd/id.h> +#include <idvec.h> const char *argp_program_version = STANDARD_HURD_VERSION (hurdids); @@ -43,8 +43,9 @@ static struct argp_option options[] = {"ids", 'i', 0, 0, "Show numeric uids/gids"}, {0} }; -static char *args_doc = 0; -static char *doc = "Show hurd uids/gids."; +static char *args_doc = "[PID]"; +static char *doc = "Show hurd uids/gids." +"\vIf PID is suppplied, show ids in that process."; /* ---------------------------------------------------------------- */ @@ -52,8 +53,70 @@ void main(int argc, char *argv[]) { error_t err; + int pid = -1; + auth_t auth = getauth (); + process_t proc = getproc (); int show_eff = 0, show_avail = 0, show_uids = 0, show_gids = 0; int show_names = 0, show_ids = 0; + struct idvec euids = { 0 }, egids = { 0 }; + struct idvec auids = { 0 }, agids = { 0 }; + + /* Print a given id vector, using NAME for the prompt. */ + void print_ids (struct idvec *uids, struct idvec *gids, char *name) + { + int i; + + if (show_uids) + { + if (name && show_gids) + printf ("%s uids: ", name); + else if (show_gids) + printf ("uids: "); + else if (name) + printf ("%s: ", name); + + for (i = 0; i < uids->num; i++) + { + uid_t uid = uids->ids[i]; + struct passwd *pw = show_names ? getpwuid (uid) : 0; + if (i > 0) + putchar (' '); + if (pw) + if (show_ids) + printf ("%d(%s)", uid, pw->pw_name); + else + printf ("%s", pw->pw_name); + else + printf ("%d", uid); + } + putchar ('\n'); + } + if (show_gids) + { + if (name && show_uids) + printf ("%s gids: ", name); + else if (show_uids) + printf ("gids: "); + else if (name) + printf ("%s: ", name); + + for (i = 0; i < gids->num; i++) + { + gid_t gid = gids->ids[i]; + struct group *gr = show_names ? getgrgid (gid) : 0; + if (i > 0) + putchar (' '); + if (gr) + if (show_ids) + printf ("%d(%s)", gid, gr->gr_name); + else + printf ("%s", gr->gr_name); + else + printf ("%d", gid); + } + putchar ('\n'); + } + } /* Parse a command line option. */ error_t parse_opt (int key, char *arg, struct argp_state *state) @@ -66,6 +129,12 @@ main(int argc, char *argv[]) case 'g': show_gids = 1; break; case 'n': show_names = 1; break; case 'i': show_ids = 1; break; + case ARGP_KEY_ARG: + if (state->arg_num == 0) + { + pid = atoi (arg); + break; + } default: return ARGP_ERR_UNKNOWN; } @@ -83,75 +152,38 @@ main(int argc, char *argv[]) if (! show_names && ! show_ids) show_names = show_ids = 1; - mutex_lock (&_hurd_id.lock); - err = _hurd_check_ids (); - mutex_unlock (&_hurd_id.lock); + if (pid >= 0) + /* Get the auth port from PID instead of using our own. */ + { + mach_port_t msgport; + task_t task; + + err = proc_getmsgport (proc, pid, &msgport); + if (err) + error (5, err, "%d: Cannot get process msgport", pid); + + err = proc_pid2task (proc, pid, &task); + if (err) + err = msg_get_init_port (msgport, auth, INIT_PORT_AUTH, &auth); + else + err = msg_get_init_port (msgport, task, INIT_PORT_AUTH, &auth); + if (err) + error (6, err, "%d: Cannot get process authentication", pid); + + mach_port_deallocate (mach_task_self (), msgport); + mach_port_deallocate (mach_task_self (), task); + } + /* Get the ids that AUTH represents. */ + err = idvec_merge_auth (&euids, &auids, &egids, &agids, auth); if (err) - error (2, err, "Can't update ids"); - else - { - typedef typeof (_hurd_id.gen) *ids_t; - void print_ids (ids_t ids, char *name) - { - int i; + error (10, err, "Cannot get authentication ids"); - if (show_uids) - { - if (name && show_gids) - printf ("%s uids: ", name); - else if (show_gids) - printf ("uids: "); - else if (name) - printf ("%s: ", name); - - for (i = 0; i < ids->nuids; i++) - { - uid_t uid = ids->uids[i]; - struct passwd *pw = show_names ? getpwuid (uid) : 0; - if (i > 0) - putchar (' '); - if (pw) - if (show_ids) - printf ("%d(%s)", uid, pw->pw_name); - else - printf ("%s", pw->pw_name); - else - printf ("%d", uid); - } - putchar ('\n'); - } - if (show_gids) - { - if (name && show_uids) - printf ("%s gids: ", name); - else if (show_uids) - printf ("gids: "); - else if (name) - printf ("%s: ", name); - - for (i = 0; i < ids->ngids; i++) - { - gid_t gid = ids->gids[i]; - struct group *gr = show_names ? getgrgid (gid) : 0; - if (i > 0) - putchar (' '); - if (gr) - if (show_ids) - printf ("%d(%s)", gid, gr->gr_name); - else - printf ("%s", gr->gr_name); - else - printf ("%d", gid); - } - putchar ('\n'); - } - } - if (show_eff) - print_ids (&_hurd_id.gen, show_avail ? "effective" : 0); - if (show_avail) - print_ids (&_hurd_id.aux, show_eff ? "available" : 0); - } + /* Print them. */ + if (show_eff) + print_ids (&euids, &egids, show_avail ? "effective" : 0); + if (show_avail) + print_ids (&auids, &agids, show_eff ? "available" : 0); - exit(0); + exit (0); } |