1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
|
/* $Id: pipe-posix.cpp $ */
/** @file
* IPRT - Anonymous Pipes, POSIX Implementation.
*/
/*
* Copyright (C) 2010-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/pipe.h>
#include "internal/iprt.h"
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/poll.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include "internal/magics.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <signal.h>
#ifdef RT_OS_LINUX
# include <sys/syscall.h>
#endif
#ifdef RT_OS_SOLARIS
# include <sys/filio.h>
#endif
#include "internal/pipe.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
typedef struct RTPIPEINTERNAL
{
/** Magic value (RTPIPE_MAGIC). */
uint32_t u32Magic;
/** The file descriptor. */
int fd;
/** Set if this is the read end, clear if it's the write end. */
bool fRead;
/** RTPipeFromNative: Leave it open on RTPipeClose. */
bool fLeaveOpen;
/** Atomically operated state variable.
*
* - Bits 0 thru 29 - Users of the new mode.
* - Bit 30 - The pipe mode, set indicates blocking.
* - Bit 31 - Set when we're switching the mode.
*/
uint32_t volatile u32State;
} RTPIPEINTERNAL;
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** @name RTPIPEINTERNAL::u32State defines
* @{ */
#define RTPIPE_POSIX_BLOCKING UINT32_C(0x40000000)
#define RTPIPE_POSIX_SWITCHING UINT32_C(0x80000000)
#define RTPIPE_POSIX_SWITCHING_BIT 31
#define RTPIPE_POSIX_USERS_MASK UINT32_C(0x3fffffff)
/** @} */
/**
* Wrapper for calling pipe2() or pipe().
*
* When using pipe2() the returned handles are marked close-on-exec and does
* not risk racing process creation calls on other threads.
*
* @returns See pipe().
* @param paFds See pipe().
* @param piNewPipeSyscall Where to cache which call we should used. -1 if
* pipe(), 1 if pipe2(), 0 if not yet decided.
*/
static int my_pipe_wrapper(int *paFds, int *piNewPipeSyscall)
{
if (*piNewPipeSyscall >= 0)
{
#if defined(RT_OS_LINUX) && defined(__NR_pipe2) && defined(O_CLOEXEC)
long rc = syscall(__NR_pipe2, paFds, O_CLOEXEC);
if (rc >= 0)
{
if (*piNewPipeSyscall == 0)
*piNewPipeSyscall = 1;
return (int)rc;
}
#endif
*piNewPipeSyscall = -1;
}
return pipe(paFds);
}
RTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags)
{
AssertPtrReturn(phPipeRead, VERR_INVALID_POINTER);
AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
AssertReturn(!(fFlags & ~RTPIPE_C_VALID_MASK), VERR_INVALID_PARAMETER);
/*
* Create the pipe and clear/set the close-on-exec flag as required.
*/
int aFds[2] = {-1, -1};
static int s_iNewPipeSyscall = 0;
if (my_pipe_wrapper(aFds, &s_iNewPipeSyscall))
return RTErrConvertFromErrno(errno);
int rc = VINF_SUCCESS;
if (s_iNewPipeSyscall > 0)
{
/* created with close-on-exec set. */
if (fFlags & RTPIPE_C_INHERIT_READ)
{
if (fcntl(aFds[0], F_SETFD, 0))
rc = RTErrConvertFromErrno(errno);
}
if (fFlags & RTPIPE_C_INHERIT_WRITE)
{
if (fcntl(aFds[1], F_SETFD, 0))
rc = RTErrConvertFromErrno(errno);
}
}
else
{
/* created with close-on-exec cleared. */
if (!(fFlags & RTPIPE_C_INHERIT_READ))
{
if (fcntl(aFds[0], F_SETFD, FD_CLOEXEC))
rc = RTErrConvertFromErrno(errno);
}
if (!(fFlags & RTPIPE_C_INHERIT_WRITE))
{
if (fcntl(aFds[1], F_SETFD, FD_CLOEXEC))
rc = RTErrConvertFromErrno(errno);
}
}
if (RT_SUCCESS(rc))
{
/*
* Create the two handles.
*/
RTPIPEINTERNAL *pThisR = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
if (pThisR)
{
RTPIPEINTERNAL *pThisW = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
if (pThisW)
{
pThisR->u32Magic = RTPIPE_MAGIC;
pThisW->u32Magic = RTPIPE_MAGIC;
pThisR->fd = aFds[0];
pThisW->fd = aFds[1];
pThisR->fRead = true;
pThisW->fRead = false;
pThisR->fLeaveOpen = false;
pThisW->fLeaveOpen = false;
pThisR->u32State = RTPIPE_POSIX_BLOCKING;
pThisW->u32State = RTPIPE_POSIX_BLOCKING;
*phPipeRead = pThisR;
*phPipeWrite = pThisW;
/*
* Before we leave, make sure to shut up SIGPIPE.
*/
signal(SIGPIPE, SIG_IGN);
return VINF_SUCCESS;
}
RTMemFree(pThisR);
rc = VERR_NO_MEMORY;
}
else
rc = VERR_NO_MEMORY;
}
close(aFds[0]);
close(aFds[1]);
return rc;
}
RTDECL(int) RTPipeCloseEx(RTPIPE hPipe, bool fLeaveOpen)
{
RTPIPEINTERNAL *pThis = hPipe;
if (pThis == NIL_RTPIPE)
return VINF_SUCCESS;
AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
/*
* Do the cleanup.
*/
AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTPIPE_MAGIC, RTPIPE_MAGIC), VERR_INVALID_HANDLE);
int fd = pThis->fd;
pThis->fd = -1;
if (!fLeaveOpen && !pThis->fLeaveOpen)
close(fd);
if (ASMAtomicReadU32(&pThis->u32State) & RTPIPE_POSIX_USERS_MASK)
{
AssertFailed();
RTThreadSleep(1);
}
RTMemFree(pThis);
return VINF_SUCCESS;
}
RTDECL(int) RTPipeClose(RTPIPE hPipe)
{
return RTPipeCloseEx(hPipe, false /*fLeaveOpen*/);
}
RTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
{
AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK_FN), VERR_INVALID_PARAMETER);
AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
/*
* Get and validate the pipe handle info.
*/
int hNative = (int)hNativePipe;
struct stat st;
AssertReturn(fstat(hNative, &st) == 0, RTErrConvertFromErrno(errno));
AssertMsgReturn(S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode), ("%#x (%o)\n", st.st_mode, st.st_mode), VERR_INVALID_HANDLE);
int fFd = fcntl(hNative, F_GETFL, 0);
AssertReturn(fFd != -1, VERR_INVALID_HANDLE);
AssertMsgReturn( (fFd & O_ACCMODE) == (fFlags & RTPIPE_N_READ ? O_RDONLY : O_WRONLY)
|| (fFd & O_ACCMODE) == O_RDWR /* Solaris creates bi-directional pipes. */
, ("%#x\n", fFd), VERR_INVALID_HANDLE);
/*
* Create the handle.
*/
RTPIPEINTERNAL *pThis = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
if (!pThis)
return VERR_NO_MEMORY;
pThis->u32Magic = RTPIPE_MAGIC;
pThis->fd = hNative;
pThis->fRead = RT_BOOL(fFlags & RTPIPE_N_READ);
pThis->fLeaveOpen = RT_BOOL(fFlags & RTPIPE_N_LEAVE_OPEN);
pThis->u32State = fFd & O_NONBLOCK ? 0 : RTPIPE_POSIX_BLOCKING;
/*
* Fix up inheritability and shut up SIGPIPE and we're done.
*/
if (fcntl(hNative, F_SETFD, fFlags & RTPIPE_N_INHERIT ? 0 : FD_CLOEXEC) == 0)
{
signal(SIGPIPE, SIG_IGN);
*phPipe = pThis;
return VINF_SUCCESS;
}
int rc = RTErrConvertFromErrno(errno);
RTMemFree(pThis);
return rc;
}
RTDECL(RTHCINTPTR) RTPipeToNative(RTPIPE hPipe)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, -1);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, -1);
return pThis->fd;
}
/**
* Prepare blocking mode.
*
* @returns VINF_SUCCESS
* @retval VERR_WRONG_ORDER
* @retval VERR_INTERNAL_ERROR_4
*
* @param pThis The pipe handle.
*/
static int rtPipeTryBlocking(RTPIPEINTERNAL *pThis)
{
/*
* Update the state.
*/
for (;;)
{
uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
uint32_t const u32StateOld = u32State;
uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
if (u32State & RTPIPE_POSIX_BLOCKING)
{
AssertReturn(cUsers < RTPIPE_POSIX_USERS_MASK / 2, VERR_INTERNAL_ERROR_4);
u32State &= ~RTPIPE_POSIX_USERS_MASK;
u32State |= cUsers + 1;
if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
{
if (u32State & RTPIPE_POSIX_SWITCHING)
break;
return VINF_SUCCESS;
}
}
else if (cUsers == 0)
{
u32State = 1 | RTPIPE_POSIX_SWITCHING | RTPIPE_POSIX_BLOCKING;
if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
break;
}
else
return VERR_WRONG_ORDER;
ASMNopPause();
}
/*
* Do the switching.
*/
int fFlags = fcntl(pThis->fd, F_GETFL, 0);
if (fFlags != -1)
{
if ( !(fFlags & O_NONBLOCK)
|| fcntl(pThis->fd, F_SETFL, fFlags & ~O_NONBLOCK) != -1)
{
ASMAtomicBitClear(&pThis->u32State, RTPIPE_POSIX_SWITCHING_BIT);
return VINF_SUCCESS;
}
}
ASMAtomicDecU32(&pThis->u32State);
return RTErrConvertFromErrno(errno);
}
/**
* Prepare non-blocking mode.
*
* @returns VINF_SUCCESS
* @retval VERR_WRONG_ORDER
* @retval VERR_INTERNAL_ERROR_4
*
* @param pThis The pipe handle.
*/
static int rtPipeTryNonBlocking(RTPIPEINTERNAL *pThis)
{
/*
* Update the state.
*/
for (;;)
{
uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
uint32_t const u32StateOld = u32State;
uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
if (!(u32State & RTPIPE_POSIX_BLOCKING))
{
AssertReturn(cUsers < RTPIPE_POSIX_USERS_MASK / 2, VERR_INTERNAL_ERROR_4);
u32State &= ~RTPIPE_POSIX_USERS_MASK;
u32State |= cUsers + 1;
if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
{
if (u32State & RTPIPE_POSIX_SWITCHING)
break;
return VINF_SUCCESS;
}
}
else if (cUsers == 0)
{
u32State = 1 | RTPIPE_POSIX_SWITCHING;
if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
break;
}
else
return VERR_WRONG_ORDER;
ASMNopPause();
}
/*
* Do the switching.
*/
int fFlags = fcntl(pThis->fd, F_GETFL, 0);
if (fFlags != -1)
{
if ( (fFlags & O_NONBLOCK)
|| fcntl(pThis->fd, F_SETFL, fFlags | O_NONBLOCK) != -1)
{
ASMAtomicBitClear(&pThis->u32State, RTPIPE_POSIX_SWITCHING_BIT);
return VINF_SUCCESS;
}
}
ASMAtomicDecU32(&pThis->u32State);
return RTErrConvertFromErrno(errno);
}
/**
* Checks if the read pipe has a HUP condition.
*
* @returns true if HUP, false if no.
* @param pThis The pipe handle (read).
*/
static bool rtPipePosixHasHup(RTPIPEINTERNAL *pThis)
{
Assert(pThis->fRead);
struct pollfd PollFd;
RT_ZERO(PollFd);
PollFd.fd = pThis->fd;
PollFd.events = POLLHUP;
return poll(&PollFd, 1, 0) >= 1
&& (PollFd.revents & POLLHUP);
}
RTDECL(int) RTPipeRead(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pcbRead);
AssertPtr(pvBuf);
int rc = rtPipeTryNonBlocking(pThis);
if (RT_SUCCESS(rc))
{
ssize_t cbRead = read(pThis->fd, pvBuf, RT_MIN(cbToRead, SSIZE_MAX));
if (cbRead >= 0)
{
if (cbRead || !cbToRead || !rtPipePosixHasHup(pThis))
*pcbRead = cbRead;
else
rc = VERR_BROKEN_PIPE;
}
else if (errno == EAGAIN)
{
*pcbRead = 0;
rc = VINF_TRY_AGAIN;
}
else
rc = RTErrConvertFromErrno(errno);
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeReadBlocking(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pvBuf);
int rc = rtPipeTryBlocking(pThis);
if (RT_SUCCESS(rc))
{
size_t cbTotalRead = 0;
while (cbToRead > 0)
{
ssize_t cbRead = read(pThis->fd, pvBuf, RT_MIN(cbToRead, SSIZE_MAX));
if (cbRead < 0)
{
rc = RTErrConvertFromErrno(errno);
break;
}
if (!cbRead && rtPipePosixHasHup(pThis))
{
rc = VERR_BROKEN_PIPE;
break;
}
/* advance */
pvBuf = (char *)pvBuf + cbRead;
cbTotalRead += cbRead;
cbToRead -= cbRead;
}
if (pcbRead)
{
*pcbRead = cbTotalRead;
if ( RT_FAILURE(rc)
&& cbTotalRead
&& rc != VERR_INVALID_POINTER)
rc = VINF_SUCCESS;
}
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeWrite(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pcbWritten);
AssertPtr(pvBuf);
int rc = rtPipeTryNonBlocking(pThis);
if (RT_SUCCESS(rc))
{
if (cbToWrite)
{
ssize_t cbWritten = write(pThis->fd, pvBuf, RT_MIN(cbToWrite, SSIZE_MAX));
if (cbWritten >= 0)
*pcbWritten = cbWritten;
else if (errno == EAGAIN)
{
*pcbWritten = 0;
rc = VINF_TRY_AGAIN;
}
else
rc = RTErrConvertFromErrno(errno);
}
else
*pcbWritten = 0;
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pvBuf);
AssertPtrNull(pcbWritten);
int rc = rtPipeTryBlocking(pThis);
if (RT_SUCCESS(rc))
{
size_t cbTotalWritten = 0;
while (cbToWrite > 0)
{
ssize_t cbWritten = write(pThis->fd, pvBuf, RT_MIN(cbToWrite, SSIZE_MAX));
if (cbWritten < 0)
{
rc = RTErrConvertFromErrno(errno);
break;
}
/* advance */
pvBuf = (char const *)pvBuf + cbWritten;
cbTotalWritten += cbWritten;
cbToWrite -= cbWritten;
}
if (pcbWritten)
{
*pcbWritten = cbTotalWritten;
if ( RT_FAILURE(rc)
&& cbTotalWritten
&& rc != VERR_INVALID_POINTER)
rc = VINF_SUCCESS;
}
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeFlush(RTPIPE hPipe)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
if (fsync(pThis->fd))
{
if (errno == EINVAL || errno == ENOTSUP)
return VERR_NOT_SUPPORTED;
return RTErrConvertFromErrno(errno);
}
return VINF_SUCCESS;
}
RTDECL(int) RTPipeSelectOne(RTPIPE hPipe, RTMSINTERVAL cMillies)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
struct pollfd PollFd;
RT_ZERO(PollFd);
PollFd.fd = pThis->fd;
PollFd.events = POLLHUP | POLLERR;
if (pThis->fRead)
PollFd.events |= POLLIN | POLLPRI;
else
PollFd.events |= POLLOUT;
int timeout;
if ( cMillies == RT_INDEFINITE_WAIT
|| cMillies >= INT_MAX /* lazy bird */)
timeout = -1;
else
timeout = cMillies;
int rc = poll(&PollFd, 1, timeout);
if (rc == -1)
return RTErrConvertFromErrno(errno);
return rc > 0 ? VINF_SUCCESS : VERR_TIMEOUT;
}
RTDECL(int) RTPipeQueryReadable(RTPIPE hPipe, size_t *pcbReadable)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(pThis->fRead, VERR_PIPE_NOT_READ);
AssertPtrReturn(pcbReadable, VERR_INVALID_POINTER);
int cb = 0;
int rc = ioctl(pThis->fd, FIONREAD, &cb);
if (rc != -1)
{
AssertStmt(cb >= 0, cb = 0);
*pcbReadable = cb;
return VINF_SUCCESS;
}
rc = errno;
if (rc == ENOTTY)
rc = VERR_NOT_SUPPORTED;
else
rc = RTErrConvertFromErrno(rc);
return rc;
}
RTDECL(int) RTPipeQueryInfo(RTPIPE hPipe, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, 0);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, 0);
rtPipeFakeQueryInfo(pObjInfo, enmAddAttr, pThis->fRead);
if (pThis->fRead)
{
int cb = 0;
int rc = ioctl(pThis->fd, FIONREAD, &cb);
if (rc >= 0)
pObjInfo->cbObject = cb;
}
#ifdef FIONSPACE
else
{
int cb = 0;
int rc = ioctl(pThis->fd, FIONSPACE, &cb);
if (rc >= 0)
pObjInfo->cbObject = cb;
}
#endif
/** @todo Check this out on linux, solaris and darwin... (Currently going by a
* FreeBSD manpage.) */
struct stat St;
if (fstat(pThis->fd, &St))
{
pObjInfo->cbAllocated = St.st_blksize;
if ( enmAddAttr == RTFSOBJATTRADD_NOTHING
|| enmAddAttr == RTFSOBJATTRADD_UNIX)
{
pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
pObjInfo->Attr.u.Unix.INodeId = St.st_ino;
pObjInfo->Attr.u.Unix.INodeIdDevice = St.st_dev;
}
}
/** @todo error handling? */
return VINF_SUCCESS;
}
int rtPipePollGetHandle(RTPIPE hPipe, uint32_t fEvents, PRTHCINTPTR phNative)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!(fEvents & RTPOLL_EVT_READ) || pThis->fRead, VERR_INVALID_PARAMETER);
AssertReturn(!(fEvents & RTPOLL_EVT_WRITE) || !pThis->fRead, VERR_INVALID_PARAMETER);
*phNative = pThis->fd;
return VINF_SUCCESS;
}
|