Example #1
0
/* Change umask to ARGV[0] (octal value).  Without arguments, print
   the value of current umask. */
error_t
cmd_umask (pid_t pid, mach_port_t msgport, int argc, char *argv[])
{
  error_t err;
  mode_t umask;
  task_t task;
  process_t proc = getproc ();

  err = proc_pid2task (proc, pid, &task);
  if (err)
    return err;
  if (argc)
    {
      umask = strtol(argv[0], 0, 8);
      err = msg_set_init_int (msgport, task, INIT_UMASK, umask);
    }
  else
    {
      err = msg_get_init_int (msgport, task, INIT_UMASK, &umask);
      if (!err)
	printf ("%d: %03o\n", pid, umask);
    }
  mach_port_deallocate (mach_task_self (), task);
  return err;
}
Example #2
0
/* Clear environment. */
error_t
cmd_clearenv (pid_t pid, mach_port_t msgport, int argc, char *argv[])
{
  error_t err;
  task_t task;
  process_t proc = getproc ();

  err = proc_pid2task (proc, pid, &task);
  if (err)
    return err;
  err = msg_set_environment (msgport, task, 0, 0);
  mach_port_deallocate (mach_task_self (), task);
  return err;
}
Example #3
0
/* Set environment variable ARGV[0] to the value ARGV[1]. */
error_t
cmd_setenv (pid_t pid, mach_port_t msgport, int argc, char *argv[])
{
  error_t err;
  task_t task;
  process_t proc = getproc ();

  err = proc_pid2task (proc, pid, &task);
  if (err)
    return err;
  err = msg_set_env_variable (msgport, task, argv[0], argv[1], 1);
  mach_port_deallocate (mach_task_self (), task);
  return err;
}
Example #4
0
/* Set port associated to file descriptor FD of process PID, whose
   message port is MSGPORT, to FILE.  Used by
   cmd_{setfd,stdin,stdout,stderr}. */
error_t
do_setfd (pid_t pid, mach_port_t msgport, size_t fd, file_t file)
{
  error_t err;
  task_t task;
  process_t proc = getproc ();

  err = proc_pid2task (proc, pid, &task);
  if (err)
    return err;
  err = msg_set_fd (msgport, task, fd, file, MACH_MSG_TYPE_COPY_SEND);
  mach_port_deallocate (mach_task_self (), file);
  mach_port_deallocate (mach_task_self (), task);
  return err;
}
Example #5
0
/* Print current root directory */
error_t
cmd_getroot (pid_t pid, mach_port_t msgport, int argc, char *argv[])
{
  error_t err;
  file_t dir;
  task_t task;
  process_t proc = getproc ();

  err = proc_pid2task (proc, pid, &task);
  if (err)
    return err;
  err = msg_get_init_port (msgport, task, INIT_PORT_CRDIR, &dir);
  if (err)
    {
      mach_port_deallocate (mach_task_self (), task);
      return err;
    }
  printf ("%d: %s\n", pid,
	  _hurd_canonicalize_directory_name_internal(dir, NULL, 0));
  mach_port_deallocate (mach_task_self (), dir);
  mach_port_deallocate (mach_task_self (), task);
  return 0;
}
Example #6
0
/* Change current root directory to ARGV[0]. */
error_t
cmd_chcrdir (pid_t pid, mach_port_t msgport, int argc, char *argv[])
{
  error_t err;
  file_t dir;
  task_t task;
  process_t proc = getproc ();

  dir = file_name_lookup (argv[0], 0, 0);
  if (dir == MACH_PORT_NULL)
    return errno;
  err = proc_pid2task (proc, pid, &task);
  if (err)
    {
      mach_port_deallocate (mach_task_self (), dir);
      return err;
    }
  err = msg_set_init_port (msgport, task, INIT_PORT_CRDIR, dir,
			   MACH_MSG_TYPE_COPY_SEND);
  mach_port_deallocate (mach_task_self (), dir);
  mach_port_deallocate (mach_task_self (), task);
  return err;
}
Example #7
0
/* Change current working directory to current root directory. */
error_t
cmd_cdroot (pid_t pid, mach_port_t msgport, int argc, char *argv[])
{
  error_t err;
  file_t dir;
  task_t task;
  process_t proc = getproc ();

  err = proc_pid2task (proc, pid, &task);
  if (err)
    return err;
  err = msg_get_init_port (msgport, task, INIT_PORT_CRDIR, &dir);
  if (err)
    {
      mach_port_deallocate (mach_task_self (), task);
      return err;
    }
  err = msg_set_init_port (msgport, task, INIT_PORT_CWDIR, dir,
			   MACH_MSG_TYPE_COPY_SEND);
  mach_port_deallocate (mach_task_self (), dir);
  mach_port_deallocate (mach_task_self (), task);
  return err;
}
Example #8
0
/* Return the task corresponding to the user argument ARG, exiting with an
   appriate error message if we can't.  */
static task_t
parse_task (char *arg)
{
  error_t err;
  task_t task;
  char *arg_end;
  pid_t pid = strtoul (arg, &arg_end, 10);
  static process_t proc = MACH_PORT_NULL;

  if (*arg == '\0' || *arg_end != '\0')
    error (10, 0, "%s: Invalid process id", arg);

  if (proc == MACH_PORT_NULL)
    proc = getproc ();

  err = proc_pid2task (proc, pid, &task);
  if (err)
    error (11, err, "%s", arg);
  else if (task == MACH_PORT_NULL)
    error (11, 0, "%s: Process %d is dead and has no task", arg, (int) pid);

  return task;
}
Example #9
0
/* Parse our options...	 */
error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  error_t err;
  switch (key)
    {
    case 'v':
      verbose = 1;
      break;

    case 'n':
      numeric = 1;
      break;

    case 's':
      subsystem = 1;
      break;

#define SELECT_TARGET(target)					\
      if (setup_target)						\
	argp_error (state, "Multiple targets specified.");	\
      setup_target = target;

    case OPT_TARGET_TASK:
      SELECT_TARGET (setup_task_target);
      break;

    case OPT_TARGET_THREAD:
      SELECT_TARGET (setup_thread_target);
      break;

    case OPT_TARGET_PROC:
      SELECT_TARGET (setup_proc_target);
      break;

    case OPT_TARGET_AUTH:
      SELECT_TARGET (setup_auth_target);
      break;

    case OPT_TARGET_EXTRACT:;
      process_t proc;
      pid_t pid;
      char *end;

      pid = strtol (arg, &end, 10);
      if (arg == end || *end != '.')
        argp_error (state, "Expected format PID.PORT, got `%s'.", arg);

      arg = end + 1;
      extract_target_port = strtol (arg, &end, 10);
      if (arg == end || *end != '\0')
        argp_error (state, "Expected format PORT, got `%s'.", arg);

      proc = getproc ();
      err = proc_pid2task (proc, pid, &extract_target_task);
      if (err)
        argp_failure (state, 1, err,
                      "Could not get task of process %d", pid);

      extract_target_type = MACH_MSG_TYPE_COPY_SEND; /* XXX */
      SELECT_TARGET (setup_extract_target);
      break;

    case ARGP_KEY_ARG:
      SELECT_TARGET (setup_hurd_target);
      setup_argument = arg;
      break;
#undef SELECT_TARGET

    case ARGP_KEY_NO_ARGS:
      if (setup_target == NULL)
	argp_usage (state);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}
Example #10
0
File: ids.c Project: GNUHurdTR/hurd
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;
}