port-move.scm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (define-module (mach port-move)
  2. #:export (port-insert-right! port-extract-right!)
  3. #:use-module (mach msg-type)
  4. #:use-module (mach ffi)
  5. #:use-module (mach task)
  6. #:use-module (mach types))
  7. ;; Documentation: (mach)Ports and other Tasks
  8. (define-ffi ("mach_port_insert_right" %mach-port-insert-right!)
  9. ;; One of kern:success, kern:invalid-task, kern:invalid-value,
  10. ;; kern:name-exist, kern:invalid-capability, kern:right-exists,
  11. ;; kern:user-references-overlow, kern:resource-shortage,
  12. ;; or an error code from 'mach_msg'.
  13. (err identity)
  14. (task ffi:ipc-space %unwrap-ipc-space)
  15. (port-name ffi:mach-port %unwrap-mach-port)
  16. (right ffi:mach-port %unwrap-mach-port)
  17. (right-type ffi:msg-type-name %unwrap-msg-type-name))
  18. (define* (port-insert-right! port-name right right-type
  19. #:optional (task (task-self)))
  20. "Insert the specified port right into the target task @var{task}
  21. (by default, the caller's task). The port will be named
  22. @var{port-name} (a @code{ffi:mach-port} in the target task, and the right
  23. @var{right} (a @code{ffi:mach-port}) will be taken from the caller.
  24. @var{port-name} and @var{right} may not be @code{%port:null} or
  25. @code{%port:dead}.
  26. @var{right-type} must be a @code{ffi:msg-type-name} denoting a kind of
  27. port right to insert.
  28. TODO document error codes."
  29. (%mach-port-insert-right! task port-name right right-type))
  30. (define-ffi ("mach_port_extract_right" %mach-port-extract-right)
  31. (err identity)
  32. (task ffi:ipc-space %unwrap-ipc-space)
  33. (name ffi:mach-port %unwrap-mach-port)
  34. (desired-type ffi:msg-type-name %unwrap-msg-type-name)
  35. (right ffi:pointer identity)
  36. (acquired-type ffi:pointer identity))
  37. (define* (port-extract-right! name desired-type #:optional (task (task-self)))
  38. "Extracts a port right @var{name} from the target task @var{task} and
  39. returns it to the caller as if the task sent the right voluntarily.
  40. @var{desired-type} must be a @code{ffi:msg-type-name} denoting a kind of
  41. port right to extract.
  42. TODO document error codes and return values. What about acquired-type?"
  43. ;; Any initial value will do.
  44. (with-foreign-variables*
  45. ((acquired-right %acquired-right ffi:mach-port %port:null)
  46. (acquired-type %acquired-type ffi:msg-type-name 0))
  47. (let ((return-code
  48. (%mach-port-extract-right task name desired-type
  49. %acquired-right %acquired-type)))
  50. ;; TODO figure out the select-error macro
  51. (select-error return-code (values acquired-right acquired-type)))))