kern_return_t S_exec_init (struct trivfs_protid *protid, auth_t auth, process_t proc) { mach_port_t host_priv, startup; error_t err; if (! protid || ! protid->isroot) return EPERM; _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], proc); /* Consume. */ _hurd_port_set (&_hurd_ports[INIT_PORT_AUTH], auth); /* Consume. */ /* Do initial setup with the proc server. */ _hurd_proc_init (save_argv, NULL, 0); procserver = getproc (); /* Have the proc server notify us when the canonical ints and ports change. This will generate an immediate callback giving us the initial boot-time canonical sets. */ { struct iouser *user; struct trivfs_protid *cred; mach_port_t right; err = iohelp_create_empty_iouser (&user); assert_perror (err); err = trivfs_open (fsys, user, 0, MACH_PORT_NULL, &cred); assert_perror (err); right = ports_get_send_right (cred); proc_execdata_notify (procserver, right, MACH_MSG_TYPE_COPY_SEND); mach_port_deallocate (mach_task_self (), right); } err = get_privileged_ports (&host_priv, NULL); assert_perror (err); proc_register_version (procserver, host_priv, "exec", "", HURD_VERSION); err = proc_getmsgport (procserver, 1, &startup); assert_perror (err); mach_port_deallocate (mach_task_self (), procserver); /* Call startup_essential task last; init assumes we are ready to run once we call it. */ err = startup_essential_task (startup, mach_task_self (), MACH_PORT_NULL, "exec", host_priv); assert_perror (err); mach_port_deallocate (mach_task_self (), startup); mach_port_deallocate (mach_task_self (), host_priv); return 0; }
/* Execute command CMD on process PID */ error_t do_cmd (pid_t pid, cmd_t cmd) { error_t err; mach_port_t msgport; process_t proc = getproc (); /* Get a msgport for PID, to which we can send requests. */ err = proc_getmsgport (proc, pid, &msgport); if (err) error (1, err, "%d: Cannot get process msgport", pid); err = (*cmd.f) (pid, msgport, cmd.num_args, cmd.args); if (err) error (2, err, "%d: Cannot execute command", pid); mach_port_deallocate (mach_task_self (), msgport); return 0; }
int main(int argc, char *argv[]) { error_t err; task_t task; mach_port_t msgport; int pid = -1; auth_t auth = getauth (); process_t proc = getproc (); struct ugids ugids = UGIDS_INIT; int show_eff = 0, show_avail = 0, show_uids = 0, show_gids = 0, terse = 0; int show_names = 0, show_values = 0; /* Print the given id vectors, using NAME for the prompt. */ void print_ids (struct idvec *uids, struct idvec *gids, char *name) { if (show_uids) { if (name && show_gids) printf ("%s uids: ", name); else if (show_gids) printf ("uids: "); else if (name) printf ("%s: ", name); printf ("%s\n", idvec_uids_rep (uids, show_values, show_names, " ")); } if (show_gids) { if (name && show_uids) printf ("%s gids: ", name); else if (show_uids) printf ("gids: "); else if (name) printf ("%s: ", name); printf ("%s\n", idvec_gids_rep (gids, show_values, show_names, " ")); } } /* Parse a command line option. */ error_t parse_opt (int key, char *arg, struct argp_state *state) { switch (key) { case 'e': show_eff = 1; break; case 'a': show_avail = 1; break; case 'u': show_uids = 1; break; case 'g': show_gids = 1; break; case 'n': show_names = 1; break; case 'v': show_values = 1; break; case 't': terse = 1; break; case ARGP_KEY_ARG: if (state->arg_num == 0) { pid = atoi (arg); break; } default: return ARGP_ERR_UNKNOWN; } return 0; } struct argp argp = {options, parse_opt, args_doc, doc}; argp_parse (&argp, argc, argv, 0, 0, 0); if (!show_eff && !show_avail) show_eff = show_avail = 1; if (!show_uids && !show_gids) show_uids = show_gids = 1; if (!show_names && !show_values) show_names = show_values = 1; if (pid < 0) /* We get our parent's authentication instead of our own because this program is usually installed setuid. This should work even if it's not installed setuid, using the auth port as authentication to the msg_get_init_port rpc. */ pid = getppid (); /* Get a msgport for PID, to which we can send requests. */ err = proc_getmsgport (proc, pid, &msgport); if (err) error (5, err, "%d: Cannot get process msgport", pid); /* Try to get the task port to use as authentication. */ err = proc_pid2task (proc, pid, &task); /* Now fetch the auth port; if we couldn't get the task port to use for authentication, we try the (old) auth port instead. */ 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 = ugids_merge_auth (&ugids, auth); if (err) error (10, err, "Cannot get authentication ids"); /* Print them. */ if (terse) /* Short output format. */ { /* Since we use ugids_rep to format the output, just clear any fields we don't want to show. */ if (! show_eff) { idvec_clear (&ugids.eff_uids); idvec_clear (&ugids.eff_gids); } if (! show_avail) { idvec_clear (&ugids.avail_uids); idvec_clear (&ugids.avail_gids); } if (! show_uids) { idvec_clear (&ugids.eff_uids); idvec_clear (&ugids.avail_uids); } if (! show_gids) { idvec_clear (&ugids.eff_gids); idvec_clear (&ugids.avail_gids); } printf ("%s\n", ugids_rep (&ugids, show_values, show_names, 0, " ","=")); } else /* Long output format */ { if (show_eff) print_ids (&ugids.eff_uids, &ugids.eff_gids, show_avail ? "effective" : 0); if (show_avail) print_ids (&ugids.avail_uids, &ugids.avail_gids, show_eff ? "available" : 0); } return 0; }