Debian Codegraph @

crt/aws-c-io/source/posix/pipe.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/io/pipe.h>
7
8 #include <aws/io/event_loop.h>
9 #include <aws/io/private/event_loop_impl.h>
10
11 #ifdef __GLIBC__
12 # define __USE_GNU
13 #endif
14
15 /* TODO: move this detection to CMAKE and a config header */
16 #if !defined(COMPAT_MODE) && defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || __GLIBC__ > 2)
17 # define HAVE_PIPE2 1
18 #else
19 # define HAVE_PIPE2 0
20 #endif
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 /* This isn't defined on ancient linux distros (breaking the builds).
27 * However, if this is a prebuild, we purposely build on an ancient system, but
28 * we want the kernel calls to still be the same as a modern build since that's likely the target of the application
29 * calling this code. Just define this if it isn't there already. GlibC and the kernel don't really care how the flag
30 * gets passed as long as it does.
31 */
32 #ifndef O_CLOEXEC
33 # define O_CLOEXEC 02000000
34 #endif
35
36 struct read_end_impl {
37 struct aws_allocator *alloc;
38 struct aws_io_handle handle;
39 struct aws_event_loop *event_loop;
40 aws_pipe_on_readable_fn *on_readable_user_callback;
41 void *on_readable_user_data;
42
43 /* Used in handshake for detecting whether user callback resulted in read-end being cleaned up.
44 * If clean_up() sees that the pointer is set, the bool it points to will get set true. */
45 bool *did_user_callback_clean_up_read_end;
46
47 bool is_subscribed;
48 };
49
50 struct pipe_write_request {
51 struct aws_byte_cursor original_cursor;
52 struct aws_byte_cursor cursor; /* tracks progress of write */
53 size_t num_bytes_written;
54 aws_pipe_on_write_completed_fn *user_callback;
55 void *user_data;
56 struct aws_linked_list_node list_node;
57
58 /* True if the write-end is cleaned up while the user callback is being invoked */
59 bool did_user_callback_clean_up_write_end;
60 };
61
62 struct write_end_impl {
63 struct aws_allocator *alloc;
64 struct aws_io_handle handle;
65 struct aws_event_loop *event_loop;
66 struct aws_linked_list write_list;
67
68 /* Valid while invoking user callback on a completed write request. */
69 struct pipe_write_request *currently_invoking_write_callback;
70
71 bool is_writable;
72
73 /* Future optimization idea: avoid an allocation on each write by keeping 1 pre-allocated pipe_write_request around
74 * and re-using it whenever possible */
75 };
76
77 static void s_write_end_on_event(
78 struct aws_event_loop *event_loop,
79 struct aws_io_handle *handle,
80 int events,
81 void *user_data);
82
83 static int s_translate_posix_error(int err) {
84 AWS_ASSERT(err);
85
86 switch (err) {
87 case EPIPE:
88 return AWS_IO_BROKEN_PIPE;
89 default:
90 return AWS_ERROR_SYS_CALL_FAILURE;
91 }
92 }
93
94 static int s_raise_posix_error(int err) {
95 return aws_raise_error(s_translate_posix_error(err));
96 }
97
98 AWS_IO_API int aws_open_nonblocking_posix_pipe(int pipe_fds[2]) {
99 int err;
100
101 #if HAVE_PIPE2
102 err = pipe2(pipe_fds, O_NONBLOCK | O_CLOEXEC);
103 if (err) {
104 return s_raise_posix_error(err);
105 }
106
107 return AWS_OP_SUCCESS;
108 #else
109 err = pipe(pipe_fds);
110 if (err) {
111 return s_raise_posix_error(err);
112 }
113
114 for (int i = 0; i < 2; ++i) {
115 int flags = fcntl(pipe_fds[i], F_GETFL);
116 if (flags == -1) {
117 s_raise_posix_error(err);
118 goto error;
119 }
120
121 flags |= O_NONBLOCK | O_CLOEXEC;
122 if (fcntl(pipe_fds[i], F_SETFL, flags) == -1) {
123 s_raise_posix_error(err);
124 goto error;
125 }
126 }
127
128 return AWS_OP_SUCCESS;
129 error:
130 close(pipe_fds[0]);
131 close(pipe_fds[1]);
132 return AWS_OP_ERR;
133 #endif
134 }
135
136 int aws_pipe_init(
137 struct aws_pipe_read_end *read_end,
138 struct aws_event_loop *read_end_event_loop,
139 struct aws_pipe_write_end *write_end,
140 struct aws_event_loop *write_end_event_loop,
141 struct aws_allocator *allocator) {
142
143 AWS_ASSERT(read_end);
144 AWS_ASSERT(read_end_event_loop);
145 AWS_ASSERT(write_end);
146 AWS_ASSERT(write_end_event_loop);
147 AWS_ASSERT(allocator);
148
149 AWS_ZERO_STRUCT(*read_end);
150 AWS_ZERO_STRUCT(*write_end);
151
152 struct read_end_impl *read_impl = NULL;
153 struct write_end_impl *write_impl = NULL;
154 int err;
155
156 /* Open pipe */
157 int pipe_fds[2];
158 err = aws_open_nonblocking_posix_pipe(pipe_fds);
159 if (err) {
160 return AWS_OP_ERR;
161 }
162
163 /* Init read-end */
164 read_impl = aws_mem_calloc(allocator, 1, sizeof(struct read_end_impl));
165 if (!read_impl) {
166 goto error;
167 }
168
169 read_impl->alloc = allocator;
170 read_impl->handle.data.fd = pipe_fds[0];
171 read_impl->event_loop = read_end_event_loop;
172
173 /* Init write-end */
174 write_impl = aws_mem_calloc(allocator, 1, sizeof(struct write_end_impl));
175 if (!write_impl) {
176 goto error;
177 }
178
179 write_impl->alloc = allocator;
180 write_impl->handle.data.fd = pipe_fds[1];
181 write_impl->event_loop = write_end_event_loop;
182 write_impl->is_writable = true; /* Assume pipe is writable to start. Even if it's not, things shouldn't break */
183 aws_linked_list_init(&write_impl->write_list);
184
185 read_end->impl_data = read_impl;
186 write_end->impl_data = write_impl;
187
188 err = aws_event_loop_subscribe_to_io_events(
189 write_end_event_loop, &write_impl->handle, AWS_IO_EVENT_TYPE_WRITABLE, s_write_end_on_event, write_end);
190 if (err) {
191 goto error;
192 }
193
194 return AWS_OP_SUCCESS;
195
196 error:
197 close(pipe_fds[0]);
198 close(pipe_fds[1]);
199
200 if (read_impl) {
201 aws_mem_release(allocator, read_impl);
202 }
203
204 if (write_impl) {
205 aws_mem_release(allocator, write_impl);
206 }
207
208 read_end->impl_data = NULL;
209 write_end->impl_data = NULL;
210
211 return AWS_OP_ERR;
212 }
213
214 int aws_pipe_clean_up_read_end(struct aws_pipe_read_end *read_end) {
215 struct read_end_impl *read_impl = read_end->impl_data;
216 if (!read_impl) {
217 return aws_raise_error(AWS_IO_BROKEN_PIPE);
218 }
219
220 if (!aws_event_loop_thread_is_callers_thread(read_impl->event_loop)) {
221 return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY);
222 }
223
224 if (read_impl->is_subscribed) {
225 int err = aws_pipe_unsubscribe_from_readable_events(read_end);
226 if (err) {
227 return AWS_OP_ERR;
228 }
229 }
230
231 /* If the event-handler is invoking a user callback, let it know that the read-end was cleaned up */
232 if (read_impl->did_user_callback_clean_up_read_end) {
233 *read_impl->did_user_callback_clean_up_read_end = true;
234 }
235
236 close(read_impl->handle.data.fd);
237
238 aws_mem_release(read_impl->alloc, read_impl);
239 AWS_ZERO_STRUCT(*read_end);
240 return AWS_OP_SUCCESS;
241 }
242
243 struct aws_event_loop *aws_pipe_get_read_end_event_loop(const struct aws_pipe_read_end *read_end) {
244 const struct read_end_impl *read_impl = read_end->impl_data;
245 if (!read_impl) {
246 aws_raise_error(AWS_IO_BROKEN_PIPE);
247 return NULL;
248 }
249
250 return read_impl->event_loop;
251 }
252
253 struct aws_event_loop *aws_pipe_get_write_end_event_loop(const struct aws_pipe_write_end *write_end) {
254 const struct write_end_impl *write_impl = write_end->impl_data;
255 if (!write_impl) {
256 aws_raise_error(AWS_IO_BROKEN_PIPE);
257 return NULL;
258 }
259
260 return write_impl->event_loop;
261 }
262
263 int aws_pipe_read(struct aws_pipe_read_end *read_end, struct aws_byte_buf *dst_buffer, size_t *num_bytes_read) {
264 AWS_ASSERT(dst_buffer && dst_buffer->buffer);
265
266 struct read_end_impl *read_impl = read_end->impl_data;
267 if (!read_impl) {
268 return aws_raise_error(AWS_IO_BROKEN_PIPE);
269 }
270
271 if (num_bytes_read) {
272 *num_bytes_read = 0;
273 }
274
275 size_t num_bytes_to_read = dst_buffer->capacity - dst_buffer->len;
276
277 ssize_t read_val = read(read_impl->handle.data.fd, dst_buffer->buffer + dst_buffer->len, num_bytes_to_read);
278
279 if (read_val < 0) {
280 int errno_value = errno; /* Always cache errno before potential side-effect */
281 if (errno_value == EAGAIN || errno_value == EWOULDBLOCK) {
282 return aws_raise_error(AWS_IO_READ_WOULD_BLOCK);
283 }
284 return s_raise_posix_error(errno_value);
285 }
286
287 /* Success */
288 dst_buffer->len += read_val;
289
290 if (num_bytes_read) {
291 *num_bytes_read = read_val;
292 }
293
294 return AWS_OP_SUCCESS;
295 }
296
297 static void s_read_end_on_event(
298 struct aws_event_loop *event_loop,
299 struct aws_io_handle *handle,
300 int events,
301 void *user_data) {
302
303 (void)event_loop;
304 (void)handle;
305
306 /* Note that it should be impossible for this to run after read-end has been unsubscribed or cleaned up */
307 struct aws_pipe_read_end *read_end = user_data;
308 struct read_end_impl *read_impl = read_end->impl_data;
309 AWS_ASSERT(read_impl);
310 AWS_ASSERT(read_impl->event_loop == event_loop);
311 AWS_ASSERT(&read_impl->handle == handle);
312 AWS_ASSERT(read_impl->is_subscribed);
313 AWS_ASSERT(events != 0);
314 AWS_ASSERT(read_impl->did_user_callback_clean_up_read_end == NULL);
315
316 /* Set up handshake, so we can be informed if the read-end is cleaned up while invoking a user callback */
317 bool did_user_callback_clean_up_read_end = false;
318 read_impl->did_user_callback_clean_up_read_end = &did_user_callback_clean_up_read_end;
319
320 /* If readable event received, tell user to try and read, even if "error" events have also occurred. */
321 if (events & AWS_IO_EVENT_TYPE_READABLE) {
322 read_impl->on_readable_user_callback(read_end, AWS_ERROR_SUCCESS, read_impl->on_readable_user_data);
323
324 if (did_user_callback_clean_up_read_end) {
325 return;
326 }
327
328 events &= ~AWS_IO_EVENT_TYPE_READABLE;
329 }
330
331 if (events) {
332 /* Check that user didn't unsubscribe in the previous callback */
333 if (read_impl->is_subscribed) {
334 read_impl->on_readable_user_callback(read_end, AWS_IO_BROKEN_PIPE, read_impl->on_readable_user_data);
335
336 if (did_user_callback_clean_up_read_end) {
337 return;
338 }
339 }
340 }
341
342 read_impl->did_user_callback_clean_up_read_end = NULL;
343 }
344
345 int aws_pipe_subscribe_to_readable_events(
346 struct aws_pipe_read_end *read_end,
347 aws_pipe_on_readable_fn *on_readable,
348 void *user_data) {
349
350 AWS_ASSERT(on_readable);
351
352 struct read_end_impl *read_impl = read_end->impl_data;
353 if (!read_impl) {
354 return aws_raise_error(AWS_IO_BROKEN_PIPE);
355 }
356
357 if (!aws_event_loop_thread_is_callers_thread(read_impl->event_loop)) {
358 return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY);
359 }
360
361 if (read_impl->is_subscribed) {
362 return aws_raise_error(AWS_ERROR_IO_ALREADY_SUBSCRIBED);
363 }
364
365 read_impl->is_subscribed = true;
366 read_impl->on_readable_user_callback = on_readable;
367 read_impl->on_readable_user_data = user_data;
368
369 int err = aws_event_loop_subscribe_to_io_events(
370 read_impl->event_loop, &read_impl->handle, AWS_IO_EVENT_TYPE_READABLE, s_read_end_on_event, read_end);
371 if (err) {
372 read_impl->is_subscribed = false;
373 read_impl->on_readable_user_callback = NULL;
374 read_impl->on_readable_user_data = NULL;
375
376 return AWS_OP_ERR;
377 }
378
379 return AWS_OP_SUCCESS;
380 }
381
382 int aws_pipe_unsubscribe_from_readable_events(struct aws_pipe_read_end *read_end) {
383 struct read_end_impl *read_impl = read_end->impl_data;
384 if (!read_impl) {
385 return aws_raise_error(AWS_IO_BROKEN_PIPE);
386 }
387
388 if (!aws_event_loop_thread_is_callers_thread(read_impl->event_loop)) {
389 return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY);
390 }
391
392 if (!read_impl->is_subscribed) {
393 return aws_raise_error(AWS_ERROR_IO_NOT_SUBSCRIBED);
394 }
395
396 int err = aws_event_loop_unsubscribe_from_io_events(read_impl->event_loop, &read_impl->handle);
397 if (err) {
398 return AWS_OP_ERR;
399 }
400
401 read_impl->is_subscribed = false;
402 read_impl->on_readable_user_callback = NULL;
403 read_impl->on_readable_user_data = NULL;
404
405 return AWS_OP_SUCCESS;
406 }
407
408 /* Pop front write request, invoke its callback, and delete it.
409 * Returns whether the callback resulted in the write-end getting cleaned up */
410 static bool s_write_end_complete_front_write_request(struct aws_pipe_write_end *write_end, int error_code) {
411 struct write_end_impl *write_impl = write_end->impl_data;
412
413 AWS_ASSERT(!aws_linked_list_empty(&write_impl->write_list));
414 struct aws_linked_list_node *node = aws_linked_list_pop_front(&write_impl->write_list);
415 struct pipe_write_request *request = AWS_CONTAINER_OF(node, struct pipe_write_request, list_node);
416
417 struct aws_allocator *alloc = write_impl->alloc;
418
419 /* Let the write-end know that a callback is in process, so the write-end can inform the callback
420 * whether it resulted in clean_up() being called. */
421 bool write_end_cleaned_up_during_callback = false;
422 struct pipe_write_request *prev_invoking_request = write_impl->currently_invoking_write_callback;
423 write_impl->currently_invoking_write_callback = request;
424
425 if (request->user_callback) {
426 request->user_callback(write_end, error_code, request->original_cursor, request->user_data);
427 write_end_cleaned_up_during_callback = request->did_user_callback_clean_up_write_end;
428 }
429
430 if (!write_end_cleaned_up_during_callback) {
431 write_impl->currently_invoking_write_callback = prev_invoking_request;
432 }
433
434 aws_mem_release(alloc, request);
435
436 return write_end_cleaned_up_during_callback;
437 }
438
439 /* Process write requests as long as the pipe remains writable */
440 static void s_write_end_process_requests(struct aws_pipe_write_end *write_end) {
441 struct write_end_impl *write_impl = write_end->impl_data;
442 AWS_ASSERT(write_impl);
443
444 while (!aws_linked_list_empty(&write_impl->write_list)) {
445 struct aws_linked_list_node *node = aws_linked_list_front(&write_impl->write_list);
446 struct pipe_write_request *request = AWS_CONTAINER_OF(node, struct pipe_write_request, list_node);
447
448 int completed_error_code = AWS_ERROR_SUCCESS;
449
450 if (request->cursor.len > 0) {
451 ssize_t write_val = write(write_impl->handle.data.fd, request->cursor.ptr, request->cursor.len);
452
453 if (write_val < 0) {
454 int errno_value = errno; /* Always cache errno before potential side-effect */
455 if (errno_value == EAGAIN || errno_value == EWOULDBLOCK) {
456 /* The pipe is no longer writable. Bail out */
457 write_impl->is_writable = false;
458 return;
459 }
460
461 /* A non-recoverable error occurred during this write */
462 completed_error_code = s_translate_posix_error(errno_value);
463
464 } else {
465 aws_byte_cursor_advance(&request->cursor, write_val);
466
467 if (request->cursor.len > 0) {
468 /* There was a partial write, loop again to try and write the rest. */
469 continue;
470 }
471 }
472 }
473
474 /* If we got this far in the loop, then the write request is complete.
475 * Note that the callback may result in the pipe being cleaned up. */
476 bool write_end_cleaned_up = s_write_end_complete_front_write_request(write_end, completed_error_code);
477 if (write_end_cleaned_up) {
478 /* Bail out! Any remaining requests were canceled during clean_up() */
479 return;
480 }
481 }
482 }
483
484 /* Handle events on the write-end's file handle */
485 static void s_write_end_on_event(
486 struct aws_event_loop *event_loop,
487 struct aws_io_handle *handle,
488 int events,
489 void *user_data) {
490
491 (void)event_loop;
492 (void)handle;
493
494 /* Note that it should be impossible for this to run after write-end has been unsubscribed or cleaned up */
495 struct aws_pipe_write_end *write_end = user_data;
496 struct write_end_impl *write_impl = write_end->impl_data;
497 AWS_ASSERT(write_impl);
498 AWS_ASSERT(write_impl->event_loop == event_loop);
499 AWS_ASSERT(&write_impl->handle == handle);
500
501 /* Only care about the writable event. */
502 if ((events & AWS_IO_EVENT_TYPE_WRITABLE) == 0) {
503 return;
504 }
505
506 write_impl->is_writable = true;
507
508 s_write_end_process_requests(write_end);
509 }
510
511 int aws_pipe_write(
512 struct aws_pipe_write_end *write_end,
513 struct aws_byte_cursor src_buffer,
514 aws_pipe_on_write_completed_fn *on_completed,
515 void *user_data) {
516
517 AWS_ASSERT(src_buffer.ptr);
518
519 struct write_end_impl *write_impl = write_end->impl_data;
520 if (!write_impl) {
521 return aws_raise_error(AWS_IO_BROKEN_PIPE);
522 }
523
524 if (!aws_event_loop_thread_is_callers_thread(write_impl->event_loop)) {
525 return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY);
526 }
527
528 struct pipe_write_request *request = aws_mem_calloc(write_impl->alloc, 1, sizeof(struct pipe_write_request));
529 if (!request) {
530 return AWS_OP_ERR;
531 }
532
533 request->original_cursor = src_buffer;
534 request->cursor = src_buffer;
535 request->user_callback = on_completed;
536 request->user_data = user_data;
537
538 aws_linked_list_push_back(&write_impl->write_list, &request->list_node);
539
540 /* If the pipe is writable, process the request (unless pipe is already in the middle of processing, which could
541 * happen if a this aws_pipe_write() call was made by another write's completion callback */
542 if (write_impl->is_writable && !write_impl->currently_invoking_write_callback) {
543 s_write_end_process_requests(write_end);
544 }
545
546 return AWS_OP_SUCCESS;
547 }
548
549 int aws_pipe_clean_up_write_end(struct aws_pipe_write_end *write_end) {
550 struct write_end_impl *write_impl = write_end->impl_data;
551 if (!write_impl) {
552 return aws_raise_error(AWS_IO_BROKEN_PIPE);
553 }
554
555 if (!aws_event_loop_thread_is_callers_thread(write_impl->event_loop)) {
556 return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY);
557 }
558
559 int err = aws_event_loop_unsubscribe_from_io_events(write_impl->event_loop, &write_impl->handle);
560 if (err) {
561 return AWS_OP_ERR;
562 }
563
564 close(write_impl->handle.data.fd);
565
566 /* Zero out write-end before invoking user callbacks so that it won't work anymore with public functions. */
567 AWS_ZERO_STRUCT(*write_end);
568
569 /* If a request callback is currently being invoked, let it know that the write-end was cleaned up */
570 if (write_impl->currently_invoking_write_callback) {
571 write_impl->currently_invoking_write_callback->did_user_callback_clean_up_write_end = true;
572 }
573
574 /* Force any outstanding write requests to complete with an error status. */
575 while (!aws_linked_list_empty(&write_impl->write_list)) {
576 struct aws_linked_list_node *node = aws_linked_list_pop_front(&write_impl->write_list);
577 struct pipe_write_request *request = AWS_CONTAINER_OF(node, struct pipe_write_request, list_node);
578 if (request->user_callback) {
579 request->user_callback(NULL, AWS_IO_BROKEN_PIPE, request->original_cursor, request->user_data);
580 }
581 aws_mem_release(write_impl->alloc, request);
582 }
583
584 aws_mem_release(write_impl->alloc, write_impl);
585 return AWS_OP_SUCCESS;
586 }