Recreates the pipe and event handles from the provided parameters, signals the event, and returns a file descriptor wrapped around the pipe handle. This function is called in the child process only.
| 8157 | // signals the event, and returns a file descriptor wrapped around the pipe |
| 8158 | // handle. This function is called in the child process only. |
| 8159 | int GetStatusFileDescriptor(unsigned int parent_process_id, |
| 8160 | size_t write_handle_as_size_t, |
| 8161 | size_t event_handle_as_size_t) { |
| 8162 | AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE, |
| 8163 | FALSE, // Non-inheritable. |
| 8164 | parent_process_id)); |
| 8165 | if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) { |
| 8166 | DeathTestAbort("Unable to open parent process " + |
| 8167 | StreamableToString(parent_process_id)); |
| 8168 | } |
| 8169 | |
| 8170 | // TODO(vladl@google.com): Replace the following check with a |
| 8171 | // compile-time assertion when available. |
| 8172 | GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t)); |
| 8173 | |
| 8174 | const HANDLE write_handle = |
| 8175 | reinterpret_cast<HANDLE>(write_handle_as_size_t); |
| 8176 | HANDLE dup_write_handle; |
| 8177 | |
| 8178 | // The newly initialized handle is accessible only in in the parent |
| 8179 | // process. To obtain one accessible within the child, we need to use |
| 8180 | // DuplicateHandle. |
| 8181 | if (!::DuplicateHandle(parent_process_handle.Get(), write_handle, |
| 8182 | ::GetCurrentProcess(), &dup_write_handle, |
| 8183 | 0x0, // Requested privileges ignored since |
| 8184 | // DUPLICATE_SAME_ACCESS is used. |
| 8185 | FALSE, // Request non-inheritable handler. |
| 8186 | DUPLICATE_SAME_ACCESS)) { |
| 8187 | DeathTestAbort("Unable to duplicate the pipe handle " + |
| 8188 | StreamableToString(write_handle_as_size_t) + |
| 8189 | " from the parent process " + |
| 8190 | StreamableToString(parent_process_id)); |
| 8191 | } |
| 8192 | |
| 8193 | const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t); |
| 8194 | HANDLE dup_event_handle; |
| 8195 | |
| 8196 | if (!::DuplicateHandle(parent_process_handle.Get(), event_handle, |
| 8197 | ::GetCurrentProcess(), &dup_event_handle, |
| 8198 | 0x0, |
| 8199 | FALSE, |
| 8200 | DUPLICATE_SAME_ACCESS)) { |
| 8201 | DeathTestAbort("Unable to duplicate the event handle " + |
| 8202 | StreamableToString(event_handle_as_size_t) + |
| 8203 | " from the parent process " + |
| 8204 | StreamableToString(parent_process_id)); |
| 8205 | } |
| 8206 | |
| 8207 | const int write_fd = |
| 8208 | ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND); |
| 8209 | if (write_fd == -1) { |
| 8210 | DeathTestAbort("Unable to convert pipe handle " + |
| 8211 | StreamableToString(write_handle_as_size_t) + |
| 8212 | " to a file descriptor"); |
| 8213 | } |
| 8214 | |
| 8215 | // Signals the parent that the write end of the pipe has been acquired |
| 8216 | // so the parent can release its own write end. |
no test coverage detected