OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef CRASHPAD_UTIL_MACH_MACH_MESSAGE_H_ |
| 16 #define CRASHPAD_UTIL_MACH_MACH_MESSAGE_H_ |
| 17 |
| 18 #include <mach/mach.h> |
| 19 #include <stdint.h> |
| 20 |
| 21 #include <limits> |
| 22 |
| 23 namespace crashpad { |
| 24 |
| 25 //! \brief The time before which a MachMessageWithDeadline() call should |
| 26 //! complete. |
| 27 //! |
| 28 //! A value of this type may be one of the special constants |
| 29 //! #kMachMessageNonblocking or #kMachMessageWaitIndefinitely. Any other values |
| 30 //! should be produced by calling MachMessageDeadlineFromTimeout(). |
| 31 //! |
| 32 //! Internally, these are currently specified on the same time base as |
| 33 //! ClockMonotonicNanoseconds(), although this is an implementation detail. |
| 34 using MachMessageDeadline = uint64_t; |
| 35 |
| 36 //! \brief Special constants used as \ref MachMessageDeadline values. |
| 37 enum : MachMessageDeadline { |
| 38 //! \brief MachMessageWithDeadline() should not block at all in its operation. |
| 39 kMachMessageNonblocking = 0, |
| 40 |
| 41 //! \brief MachMessageWithDeadline() should wait indefinitely for the |
| 42 //! requested operation to complete. |
| 43 kMachMessageWaitIndefinitely = |
| 44 std::numeric_limits<MachMessageDeadline>::max(), |
| 45 }; |
| 46 |
| 47 //! \brief Computes the deadline for a specified timeout value. |
| 48 //! |
| 49 //! While deadlines exist on an absolute time scale, timeouts are relative. This |
| 50 //! function calculates the deadline as \a timeout_ms milliseconds after it |
| 51 //! executes. |
| 52 //! |
| 53 //! If \a timeout_ms is `0`, this function will return #kMachMessageNonblocking. |
| 54 MachMessageDeadline MachMessageDeadlineFromTimeout( |
| 55 mach_msg_timeout_t timeout_ms); |
| 56 |
| 57 //! \brief Runs `mach_msg()` with a deadline, as opposed to a timeout. |
| 58 //! |
| 59 //! This function is similar to `mach_msg()`, with the following differences: |
| 60 //! - The `timeout` parameter has been replaced by \a deadline. The deadline |
| 61 //! applies uniformly to a call that is requested to both send and receive |
| 62 //! a message. |
| 63 //! - The `MACH_SEND_TIMEOUT` and `MACH_RCV_TIMEOUT` bits in \a options are |
| 64 //! not used. Timeouts are specified by the \a deadline argument. |
| 65 //! - The `send_size` parameter has been removed. Its value is implied by |
| 66 //! \a message when \a options contains `MACH_SEND_MSG`. |
| 67 //! - The \a run_even_if_expired parameter has been added. |
| 68 //! |
| 69 //! Like the `mach_msg()` wrapper in `libsyscall`, this function will retry |
| 70 //! operations when experiencing `MACH_SEND_INTERRUPTED` and |
| 71 //! `MACH_RCV_INTERRUPTED`, unless \a options contains `MACH_SEND_INTERRUPT` or |
| 72 //! `MACH_RCV_INTERRUPT`. Unlike `mach_msg()`, which restarts the call with the |
| 73 //! full timeout when this occurs, this function continues enforcing the |
| 74 //! user-specified \a deadline. |
| 75 //! |
| 76 //! Except as noted, the parameters and return value are identical to those of |
| 77 //! `mach_msg()`. |
| 78 //! |
| 79 //! \param[in] deadline The time by which this call should complete. If the |
| 80 //! deadline is exceeded, this call will return `MACH_SEND_TIMED_OUT` or |
| 81 //! `MACH_RCV_TIMED_OUT`. |
| 82 //! \param[in] run_even_if_expired If `true`, a deadline that is expired when |
| 83 //! this function is called will be treated as though a deadline of |
| 84 //! #kMachMessageNonblocking had been specified. When `false`, an expired |
| 85 //! deadline will result in a `MACH_SEND_TIMED_OUT` or `MACH_RCV_TIMED_OUT` |
| 86 //! return value, even if the deadline is already expired when the function |
| 87 //! is called. |
| 88 mach_msg_return_t MachMessageWithDeadline(mach_msg_header_t* message, |
| 89 mach_msg_option_t options, |
| 90 mach_msg_size_t receive_size, |
| 91 mach_port_name_t receive_port, |
| 92 MachMessageDeadline deadline, |
| 93 mach_port_name_t notify_port, |
| 94 bool run_even_if_expired); |
| 95 |
| 96 //! \brief Initializes a reply message for a MIG server routine based on its |
| 97 //! corresponding request. |
| 98 //! |
| 99 //! If a request is handled by a server routine, it may be necessary to revise |
| 100 //! some of the fields set by this function, such as `msgh_size` and any fields |
| 101 //! defined in a routine’s reply structure type. |
| 102 //! |
| 103 //! \param[in] in_header The request message to base the reply on. |
| 104 //! \param[out] out_header The reply message to initialize. \a out_header will |
| 105 //! be treated as a `mig_reply_error_t*` and all of its fields will be set |
| 106 //! except for `RetCode`, which must be set by SetMIGReplyError(). This |
| 107 //! argument is accepted as a `mach_msg_header_t*` instead of a |
| 108 //! `mig_reply_error_t*` because that is the type that callers are expected |
| 109 //! to possess in the C API. |
| 110 void PrepareMIGReplyFromRequest(const mach_msg_header_t* in_header, |
| 111 mach_msg_header_t* out_header); |
| 112 |
| 113 //! \brief Sets the error code in a reply message for a MIG server routine. |
| 114 //! |
| 115 //! \param[inout] out_header The reply message to operate on. \a out_header will |
| 116 //! be treated as a `mig_reply_error_t*` and its `RetCode` field will be |
| 117 //! set. This argument is accepted as a `mach_msg_header_t*` instead of a |
| 118 //! `mig_reply_error_t*` because that is the type that callers are expected |
| 119 //! to possess in the C API. |
| 120 //! \param[in] error The error code to store in \a out_header. |
| 121 //! |
| 122 //! \sa PrepareMIGReplyFromRequest() |
| 123 void SetMIGReplyError(mach_msg_header_t* out_header, kern_return_t error); |
| 124 |
| 125 //! \brief Returns a Mach message trailer for a message that has been received. |
| 126 //! |
| 127 //! This function must only be called on Mach messages that have been received |
| 128 //! via the Mach messaging interface, such as `mach_msg()`. Messages constructed |
| 129 //! for sending do not contain trailers. |
| 130 //! |
| 131 //! \param[in] header A pointer to a received Mach message. |
| 132 //! |
| 133 //! \return A pointer to the trailer following the received Mach message’s body. |
| 134 //! The contents of the trailer depend on the options provided to |
| 135 //! `mach_msg()` or a similar function when the message was received. |
| 136 const mach_msg_trailer_t* MachMessageTrailerFromHeader( |
| 137 const mach_msg_header_t* header); |
| 138 |
| 139 } // namespace crashpad |
| 140 |
| 141 #endif // CRASHPAD_UTIL_MACH_MACH_MESSAGE_H_ |
OLD | NEW |