| 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 #include <sys/types.h> |
| 21 |
| 22 namespace crashpad { |
| 23 |
| 24 //! \brief A Mach message option specifying that an audit trailer should be |
| 25 //! delivered during a receive operation. |
| 26 //! |
| 27 //! This constant is provided because the macros normally used to request this |
| 28 //! behavior are cumbersome. |
| 29 const mach_msg_option_t kMachMessageReceiveAuditTrailer = |
| 30 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0) | |
| 31 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); |
| 32 |
| 33 //! \brief Special constants used as `mach_msg_timeout_t` values. |
| 34 enum : mach_msg_timeout_t { |
| 35 //! \brief When passed to MachMessageDeadlineFromTimeout(), that function will |
| 36 //! return #kMachMessageDeadlineNonblocking. |
| 37 kMachMessageTimeoutNonblocking = 0, |
| 38 |
| 39 //! \brief When passed to MachMessageDeadlineFromTimeout(), that function will |
| 40 //! return #kMachMessageDeadlineWaitIndefinitely. |
| 41 kMachMessageTimeoutWaitIndefinitely = 0xffffffff, |
| 42 }; |
| 43 |
| 44 //! \brief The time before which a MachMessageWithDeadline() call should |
| 45 //! complete. |
| 46 //! |
| 47 //! A value of this type may be one of the special constants |
| 48 //! #kMachMessageDeadlineNonblocking or #kMachMessageDeadlineWaitIndefinitely. |
| 49 //! Any other values should be produced by calling |
| 50 //! MachMessageDeadlineFromTimeout(). |
| 51 //! |
| 52 //! Internally, these are currently specified on the same time base as |
| 53 //! ClockMonotonicNanoseconds(), although this is an implementation detail. |
| 54 using MachMessageDeadline = uint64_t; |
| 55 |
| 56 //! \brief Special constants used as \ref MachMessageDeadline values. |
| 57 enum : MachMessageDeadline { |
| 58 //! \brief MachMessageWithDeadline() should not block at all in its operation. |
| 59 kMachMessageDeadlineNonblocking = 0, |
| 60 |
| 61 //! \brief MachMessageWithDeadline() should wait indefinitely for the |
| 62 //! requested operation to complete. |
| 63 kMachMessageDeadlineWaitIndefinitely = 0xffffffffffffffff, |
| 64 }; |
| 65 |
| 66 //! \brief Computes the deadline for a specified timeout value. |
| 67 //! |
| 68 //! While deadlines exist on an absolute time scale, timeouts are relative. This |
| 69 //! function calculates the deadline as \a timeout_ms milliseconds after it |
| 70 //! executes. |
| 71 //! |
| 72 //! If \a timeout_ms is #kMachMessageDeadlineNonblocking, this function will |
| 73 //! return #kMachMessageDeadlineNonblocking. If \a timeout_ms is |
| 74 //! #kMachMessageTimeoutWaitIndefinitely, this function will return |
| 75 //! #kMachMessageDeadlineWaitIndefinitely. |
| 76 MachMessageDeadline MachMessageDeadlineFromTimeout( |
| 77 mach_msg_timeout_t timeout_ms); |
| 78 |
| 79 //! \brief Runs `mach_msg()` with a deadline, as opposed to a timeout. |
| 80 //! |
| 81 //! This function is similar to `mach_msg()`, with the following differences: |
| 82 //! - The `timeout` parameter has been replaced by \a deadline. The deadline |
| 83 //! applies uniformly to a call that is requested to both send and receive |
| 84 //! a message. |
| 85 //! - The `MACH_SEND_TIMEOUT` and `MACH_RCV_TIMEOUT` bits in \a options are |
| 86 //! not used. Timeouts are specified by the \a deadline argument. |
| 87 //! - The `send_size` parameter has been removed. Its value is implied by |
| 88 //! \a message when \a options contains `MACH_SEND_MSG`. |
| 89 //! - The \a run_even_if_expired parameter has been added. |
| 90 //! |
| 91 //! Like the `mach_msg()` wrapper in `libsyscall`, this function will retry |
| 92 //! operations when experiencing `MACH_SEND_INTERRUPTED` and |
| 93 //! `MACH_RCV_INTERRUPTED`, unless \a options contains `MACH_SEND_INTERRUPT` or |
| 94 //! `MACH_RCV_INTERRUPT`. Unlike `mach_msg()`, which restarts the call with the |
| 95 //! full timeout when this occurs, this function continues enforcing the |
| 96 //! user-specified \a deadline. |
| 97 //! |
| 98 //! Except as noted, the parameters and return value are identical to those of |
| 99 //! `mach_msg()`. |
| 100 //! |
| 101 //! \param[in] deadline The time by which this call should complete. If the |
| 102 //! deadline is exceeded, this call will return `MACH_SEND_TIMED_OUT` or |
| 103 //! `MACH_RCV_TIMED_OUT`. |
| 104 //! \param[in] run_even_if_expired If `true`, a deadline that is expired when |
| 105 //! this function is called will be treated as though a deadline of |
| 106 //! #kMachMessageDeadlineNonblocking had been specified. When `false`, an |
| 107 //! expired deadline will result in a `MACH_SEND_TIMED_OUT` or |
| 108 //! `MACH_RCV_TIMED_OUT` return value, even if the deadline is already |
| 109 //! expired when the function is called. |
| 110 mach_msg_return_t MachMessageWithDeadline(mach_msg_header_t* message, |
| 111 mach_msg_option_t options, |
| 112 mach_msg_size_t receive_size, |
| 113 mach_port_name_t receive_port, |
| 114 MachMessageDeadline deadline, |
| 115 mach_port_name_t notify_port, |
| 116 bool run_even_if_expired); |
| 117 |
| 118 //! \brief Initializes a reply message for a MIG server routine based on its |
| 119 //! corresponding request. |
| 120 //! |
| 121 //! If a request is handled by a server routine, it may be necessary to revise |
| 122 //! some of the fields set by this function, such as `msgh_size` and any fields |
| 123 //! defined in a routine’s reply structure type. |
| 124 //! |
| 125 //! \param[in] in_header The request message to base the reply on. |
| 126 //! \param[out] out_header The reply message to initialize. \a out_header will |
| 127 //! be treated as a `mig_reply_error_t*` and all of its fields will be set |
| 128 //! except for `RetCode`, which must be set by SetMIGReplyError(). This |
| 129 //! argument is accepted as a `mach_msg_header_t*` instead of a |
| 130 //! `mig_reply_error_t*` because that is the type that callers are expected |
| 131 //! to possess in the C API. |
| 132 void PrepareMIGReplyFromRequest(const mach_msg_header_t* in_header, |
| 133 mach_msg_header_t* out_header); |
| 134 |
| 135 //! \brief Sets the error code in a reply message for a MIG server routine. |
| 136 //! |
| 137 //! \param[inout] out_header The reply message to operate on. \a out_header will |
| 138 //! be treated as a `mig_reply_error_t*` and its `RetCode` field will be |
| 139 //! set. This argument is accepted as a `mach_msg_header_t*` instead of a |
| 140 //! `mig_reply_error_t*` because that is the type that callers are expected |
| 141 //! to possess in the C API. |
| 142 //! \param[in] error The error code to store in \a out_header. |
| 143 //! |
| 144 //! \sa PrepareMIGReplyFromRequest() |
| 145 void SetMIGReplyError(mach_msg_header_t* out_header, kern_return_t error); |
| 146 |
| 147 //! \brief Returns a Mach message trailer for a message that has been received. |
| 148 //! |
| 149 //! This function must only be called on Mach messages that have been received |
| 150 //! via the Mach messaging interface, such as `mach_msg()`. Messages constructed |
| 151 //! for sending do not contain trailers. |
| 152 //! |
| 153 //! \param[in] header A pointer to a received Mach message. |
| 154 //! |
| 155 //! \return A pointer to the trailer following the received Mach message’s body. |
| 156 //! The contents of the trailer depend on the options provided to |
| 157 //! `mach_msg()` or a similar function when the message was received. |
| 158 const mach_msg_trailer_t* MachMessageTrailerFromHeader( |
| 159 const mach_msg_header_t* header); |
| 160 |
| 161 //! \brief Returns the process ID of a Mach message’s sender from its audit |
| 162 //! trailer. |
| 163 //! |
| 164 //! For the audit trailer to be present, the message must have been received |
| 165 //! with #kMachMessageReceiveAuditTrailer or its macro equivalent specified in |
| 166 //! the receive options. |
| 167 //! |
| 168 //! If the kernel is the message’s sender, a process ID of `0` will be returned. |
| 169 //! |
| 170 //! \param[in] trailer The trailer received with a Mach message. |
| 171 //! |
| 172 //! \return The process ID of the message’s sender, or `-1` on failure with a |
| 173 //! message logged. It is considered a failure for \a trailer to not contain |
| 174 //! audit information. |
| 175 pid_t AuditPIDFromMachMessageTrailer(const mach_msg_trailer_t* trailer); |
| 176 |
| 177 //! \brief Destroys or deallocates a Mach port received in a Mach message. |
| 178 //! |
| 179 //! This function disposes of port rights received in a Mach message. Receive |
| 180 //! rights will be destroyed with `mach_port_mod_refs()`. Send and send-once |
| 181 //! rights will be deallocated with `mach_port_deallocate()`. |
| 182 //! |
| 183 //! \param[in] port The port to destroy or deallocate. |
| 184 //! \param[in] port_right_type The right type held for \a port: |
| 185 //! `MACH_MSG_TYPE_PORT_RECEIVE`, `MACH_MSG_TYPE_PORT_SEND`, or |
| 186 //! `MACH_MSG_TYPE_PORT_SEND_ONCE`. |
| 187 //! |
| 188 //! \return `true` on success, or `false` on failure with a message logged. |
| 189 bool MachMessageDestroyReceivedPort(mach_port_t port, |
| 190 mach_msg_type_name_t port_right_type); |
| 191 |
| 192 } // namespace crashpad |
| 193 |
| 194 #endif // CRASHPAD_UTIL_MACH_MACH_MESSAGE_H_ |
| OLD | NEW |