scoped_mach_port.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/mac/scoped_mach_port.h"
  5. #include "base/mac/mach_logging.h"
  6. namespace base::mac {
  7. namespace internal {
  8. // static
  9. void SendRightTraits::Free(mach_port_t port) {
  10. kern_return_t kr = mach_port_deallocate(mach_task_self(), port);
  11. MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
  12. << "ScopedMachSendRight mach_port_deallocate";
  13. }
  14. // static
  15. void ReceiveRightTraits::Free(mach_port_t port) {
  16. kern_return_t kr =
  17. mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
  18. MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
  19. << "ScopedMachReceiveRight mach_port_mod_refs";
  20. }
  21. // static
  22. void PortSetTraits::Free(mach_port_t port) {
  23. kern_return_t kr =
  24. mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_PORT_SET, -1);
  25. MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
  26. << "ScopedMachPortSet mach_port_mod_refs";
  27. }
  28. } // namespace internal
  29. bool CreateMachPort(ScopedMachReceiveRight* receive,
  30. ScopedMachSendRight* send,
  31. absl::optional<mach_port_msgcount_t> queue_limit) {
  32. mach_port_options_t options{};
  33. options.flags = (send != nullptr ? MPO_INSERT_SEND_RIGHT : 0);
  34. if (queue_limit.has_value()) {
  35. options.flags |= MPO_QLIMIT;
  36. options.mpl.mpl_qlimit = *queue_limit;
  37. }
  38. kern_return_t kr =
  39. mach_port_construct(mach_task_self(), &options, 0,
  40. ScopedMachReceiveRight::Receiver(*receive).get());
  41. if (kr != KERN_SUCCESS) {
  42. MACH_LOG(ERROR, kr) << "mach_port_construct";
  43. return false;
  44. }
  45. // Multiple rights are coalesced to the same name in a task, so assign the
  46. // send rights to the same name.
  47. if (send) {
  48. send->reset(receive->get());
  49. }
  50. return true;
  51. }
  52. ScopedMachSendRight RetainMachSendRight(mach_port_t port) {
  53. kern_return_t kr =
  54. mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, 1);
  55. if (kr == KERN_SUCCESS)
  56. return ScopedMachSendRight(port);
  57. MACH_DLOG(ERROR, kr) << "mach_port_mod_refs +1";
  58. return {};
  59. }
  60. } // namespace base::mac