exec-reauth.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <hurd.h>
  2. #include <mach.h>
  3. #include <unistd.h>
  4. #include <hurd/auth.h>
  5. #include <hurd/io.h>
  6. #include <hurd/process.h>
  7. #include <stdio.h>
  8. pid_t
  9. helper_exec_reauth (auth_t newauth, int secure,
  10. mach_port_t * ports, unsigned num_ports,
  11. mach_port_t * fds, unsigned num_fds,
  12. int is_empty, uid_t uid)
  13. {
  14. pid_t pid = fork ();
  15. if (pid == 0)
  16. {
  17. pid_t parent = getppid ();
  18. task_t parent_task = pid2task (parent);
  19. unsigned int i;
  20. mach_msg_type_name_t foo;
  21. mach_port_t new_ports[num_ports];
  22. mach_port_t new_fds[num_fds];
  23. mach_port_t newauth_new;
  24. for (i = 0; i < num_ports; ++i)
  25. {
  26. if (ports[i] == MACH_PORT_NULL)
  27. continue;
  28. mach_port_extract_right (parent_task, ports[i],
  29. MACH_MSG_TYPE_MOVE_SEND, &new_ports[i],
  30. &foo);
  31. }
  32. for (i = 0; i < num_fds; ++i)
  33. {
  34. if (fds[i] == MACH_PORT_NULL)
  35. continue;
  36. mach_port_extract_right (parent_task, fds[i],
  37. MACH_MSG_TYPE_MOVE_SEND, &new_fds[i],
  38. &foo);
  39. }
  40. mach_port_extract_right (parent_task, newauth, MACH_MSG_TYPE_MOVE_SEND,
  41. &newauth_new, &foo);
  42. error_t err =
  43. exec_reauth (newauth_new, secure, 0, new_ports, num_ports, new_fds,
  44. num_fds);
  45. proc_setowner (new_ports[INIT_PORT_PROC], uid, is_empty);
  46. mach_port_insert_right (parent_task, newauth, newauth_new,
  47. MACH_MSG_TYPE_MOVE_SEND);
  48. for (i = 0; i < num_ports; ++i)
  49. {
  50. if (ports[i] == MACH_PORT_NULL)
  51. continue;
  52. strerror (mach_port_insert_right
  53. (parent_task, new_ports[i], ports[i],
  54. MACH_MSG_TYPE_MOVE_SEND)));
  55. }
  56. for (i = 0; i < num_fds; ++i)
  57. {
  58. if (fds[i] == MACH_PORT_NULL)
  59. continue;
  60. strerror (mach_port_insert_right
  61. (parent_task, new_fds[i], fds[i],
  62. MACH_MSG_TYPE_MOVE_SEND)));
  63. }
  64. exit (err);
  65. }
  66. return (pid);
  67. }
  68. int
  69. exec_reauth_finished (pid_t pid)
  70. {
  71. int status;
  72. int ret = waitpid (pid, &status, WNOHANG);
  73. if (ret > 0)
  74. {
  75. status = WEXITSTATUS (status);
  76. }
  77. return (ret > 0);
  78. }