This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
fs.c
1338 lines (1134 loc) · 29.4 KB
/
fs.c
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <sys/syscall.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <limits.h>
#include "fs.h"
#include "constants.h"
#include "sha2.h"
#if XPPOLL == XPPOLL_KQUEUE
# include <sys/event.h>
# include <sys/time.h>
static int ppoll_kq = -1;
#endif
static void
fd_cleanup(void* arg)
{
xclose((intptr_t) arg);
}
void
cleanup_commit_close_fd(struct cleanup* cl, int fd)
{
cleanup_commit(cl, fd_cleanup, (void*) (intptr_t) (fd));
}
int
xopen(const char* pathname, int flags, mode_t mode)
{
struct cleanup* cl = cleanup_allocate();
int fd = open(pathname, flags | O_CLOEXEC, mode);
if (fd == -1)
die_errno("open(\"%s\")", pathname);
assert_cloexec(fd);
cleanup_commit_close_fd(cl, fd);
return fd;
}
int
try_xopen(const char* pathname, int flags, mode_t mode)
{
struct cleanup* cl = cleanup_allocate();
int fd = open(pathname, flags | O_CLOEXEC, mode);
if (fd == -1) {
cleanup_forget(cl);
return -1;
}
assert_cloexec(fd);
cleanup_commit_close_fd(cl, fd);
return fd;
}
void
xclose(int fd)
{
// If close fails with EIO or EINTR error, it still closes the FD.
// Only EBADF indicates a failure to close something.
if (close(fd) == -1 && errno == EBADF)
die_errno("close");
}
int
merge_O_CLOEXEC_into_fd_flags(int fd, int flags)
{
assert(flags == 0 || flags == O_CLOEXEC);
if (flags != 0) {
int fl = fcntl(fd, F_GETFD);
if (fl < 0 || fcntl(fd, F_SETFD, fl | FD_CLOEXEC) < 0)
return -1;
assert_cloexec(fd);
}
return 0;
}
__attribute__((unused))
static void
close_saving_errno(int fd)
{
int saved_errno = errno;
xclose(fd);
errno = saved_errno;
}
#ifndef NDEBUG
void
assert_cloexec(int fd)
{
int fl = fcntl(fd, F_GETFD);
assert(fl != -1);
assert(fl & FD_CLOEXEC);
}
#endif
#ifndef HAVE_PIPE2
int
pipe2(int fd[2], int flags)
{
int xfd[2];
if (pipe(xfd) < 0)
return -1;
for (int i = 0; i < 2; ++i)
if (merge_O_CLOEXEC_into_fd_flags(xfd[i], flags) < 0)
goto fail;
fd[0] = xfd[0];
fd[1] = xfd[1];
return 0;
fail:
close_saving_errno(xfd[0]);
close_saving_errno(xfd[1]);
return -1;
}
#endif
void
xpipe(int* read_end, int* write_end)
{
struct cleanup* cl[2];
cl[0] = cleanup_allocate();
cl[1] = cleanup_allocate();
int fd[2];
if (pipe2(fd, O_CLOEXEC) < 0)
die_errno("pipe2");
assert_cloexec(fd[0]);
assert_cloexec(fd[1]);
cleanup_commit_close_fd(cl[0], fd[0]);
cleanup_commit_close_fd(cl[1], fd[1]);
*read_end = fd[0];
*write_end = fd[1];
}
#if !defined(F_DUPFD_CLOEXEC) && defined(__linux__)
#define F_DUPFD_CLOEXEC 1030
#endif
int
xdup(int fd)
{
struct cleanup* cl = cleanup_allocate();
int newfd = fcntl(fd, F_DUPFD_CLOEXEC, fd);
if (newfd == -1)
die_errno("F_DUPFD_CLOEXEC(%d)", fd);
assert_cloexec(newfd);
cleanup_commit_close_fd(cl, newfd);
return newfd;
}
int
xdup3nc(int oldfd, int newfd, int flags)
{
int rc;
do {
rc = dup3(oldfd, newfd, flags);
} while (rc < 0 && errno == EINTR);
if (rc < 0)
die_errno("dup3(%d,%d,0x%x)", oldfd, newfd, (unsigned) flags);
return rc;
}
#ifdef HAVE_FOPENCOOKIE
typedef ssize_t custom_stream_ssize_t;
typedef size_t custom_stream_size_t;
#else
typedef int custom_stream_ssize_t;
typedef int custom_stream_size_t;
#endif
static void
xfopen_cleanup(void* arg)
{
fclose((FILE*) arg);
}
static int
xfdopen_fd(void* cookie)
{
return (int) (intptr_t) cookie;
}
struct xfd_op_ctx {
enum { XFD_OP_READ, XFD_OP_WRITE} op;
int fd;
void* buf;
size_t size;
ssize_t result;
};
static void
xfd_op_1(void* data)
{
struct xfd_op_ctx* ctx = data;
ssize_t ret;
if (ctx->op == XFD_OP_READ) {
do {
WITH_IO_SIGNALS_ALLOWED();
ret = read(ctx->fd, ctx->buf, ctx->size);
} while (ret == -1 && errno == EINTR);
if (ret == -1)
die_errno("read(%d)", ctx->fd);
} else {
assert(ctx->op == XFD_OP_WRITE);
do {
WITH_IO_SIGNALS_ALLOWED();
ret = write(ctx->fd, ctx->buf, ctx->size);
} while (ret == -1 && errno == EINTR);
if (ret == -1)
die_errno("write(%d)", ctx->fd);
}
ctx->result = ret;
}
static void
xfd_op(struct xfd_op_ctx* ctx)
{
bool old_die_on_quit = hack_die_on_quit;
hack_die_on_quit = true;
struct errinfo ei = {
.want_msg = true,
};
if (catch_error(xfd_op_1, ctx, &ei)) {
deferred_die(ei.err, "%s", ei.msg);
errno = ei.err < 0 ? EIO : ei.err;
ctx->result = -1;
}
hack_die_on_quit = old_die_on_quit;
}
// xfdopen_read and xfdopen_write are called from inside stdio
// machinery and must always return locally --- never longjmp! If we
// die inside one of these functions, we "defer" the die and actually
// longjmp at the next safe opportunity.
static custom_stream_ssize_t
xfdopen_read(void* cookie, char* buf, custom_stream_size_t size)
{
struct xfd_op_ctx ctx = {
.op = XFD_OP_READ,
.fd = xfdopen_fd(cookie),
.buf = buf,
.size = size
};
xfd_op(&ctx);
return ctx.result;
}
static custom_stream_ssize_t
xfdopen_write(void* cookie, const char* buf, custom_stream_size_t size)
{
struct xfd_op_ctx ctx = {
.op = XFD_OP_WRITE,
.fd = xfdopen_fd(cookie),
.buf = (void*) buf,
.size = size
};
xfd_op(&ctx);
return ctx.result;
}
FILE*
xfdopen(int fd, const char* mode)
{
struct cleanup* cl = cleanup_allocate();
FILE* f = NULL;
#if defined(HAVE_FOPENCOOKIE)
cookie_io_functions_t funcs = {
.read = xfdopen_read,
.write = xfdopen_write,
.seek = NULL,
.close = NULL,
};
f = fopencookie((void*) (intptr_t) fd, mode, funcs);
#elif defined(HAVE_FUNOPEN)
f = funopen((void*) (intptr_t) fd,
xfdopen_read,
xfdopen_write,
NULL,
NULL);
#else
# error This platform has no custom stdio stream support
#endif
if (f == NULL)
die_errno("fdopen");
cleanup_commit(cl, xfopen_cleanup, f);
return f;
}
// Like xdup, but return a structure that allows the fd to be
// individually closed.
struct fdh*
fdh_dup(int fd)
{
struct reslist* rl = reslist_create();
WITH_CURRENT_RESLIST(rl);
struct fdh* fdh = xalloc(sizeof (*fdh));
fdh->rl = rl;
fdh->fd = xdup(fd);
return fdh;
}
void
fdh_destroy(struct fdh* fdh)
{
reslist_destroy(fdh->rl);
}
int
xF_GETFL(int fd)
{
int flags = fcntl(fd, F_GETFL);
if (flags == -1)
die_errno("fcntl(%d, F_GETFL)", fd);
return flags;
}
void
xF_SETFL(int fd, int flags)
{
if (fcntl(fd, F_SETFL, flags) == -1)
die_errno("fcntl(%d, F_SETFL, %x)", fd, flags);
}
enum blocking_mode
fd_set_blocking_mode(int fd, enum blocking_mode mode)
{
int flags = fcntl(fd, F_GETFL);
enum blocking_mode old_mode;
flags = xF_GETFL(fd);
old_mode = (flags & O_NONBLOCK) ? non_blocking : blocking;
if (mode == non_blocking) {
flags |= O_NONBLOCK;
} else {
flags &= ~O_NONBLOCK;
}
xF_SETFL(fd, flags);
return old_mode;
}
static const char*
xttyname(int fd)
{
#ifndef __ANDROID__
return ttyname(fd);
#else
return xreadlink(xaprintf("/proc/self/fd/%d", fd));
#endif
}
void
hack_reopen_tty(int fd)
{
// We sometimes need O_NONBLOCK on our input and output streams,
// but O_NONBLOCK applies to the entire file object. If the file
// object happens to be a tty we've inherited, everything that
// uses that tty will start getting EAGAIN and all hell will break
// loose. Here, we reopen the tty so we can get a fresh file
// object and control the blocking mode separately.
SCOPED_RESLIST(rl_hack);
xdup3nc(xopen(xttyname(fd), O_RDWR | O_NOCTTY, 0), fd, O_CLOEXEC);
}
size_t
xread(int fd, void* buf, size_t sz)
{
ssize_t ret;
{
WITH_IO_SIGNALS_ALLOWED();
ret = read(fd, buf, sz);
}
if (ret < 0)
die_errno("read(%d)", fd);
return ret;
}
size_t
read_all(int fd, void* buf, size_t sz)
{
size_t nr_read = 0;
ssize_t ret;
char* pos = buf;
while (nr_read < sz) {
do {
WITH_IO_SIGNALS_ALLOWED();
ret = read(fd, &pos[nr_read], sz - nr_read);
} while (ret == -1 && errno == EINTR);
if (ret < 0)
die_errno("read(%d)", fd);
if (ret < 1)
break;
nr_read += ret;
}
return nr_read;
}
void
write_all(int fd, const void* buf, size_t sz)
{
size_t nr_written = 0;
ssize_t ret;
const char* pos = buf;
while (nr_written < sz) {
do {
WITH_IO_SIGNALS_ALLOWED();
ret = write(fd, &pos[nr_written], sz - nr_written);
} while (ret == -1 && errno == EINTR);
if (ret < 0)
die_errno("write(%d)", fd);
nr_written += ret;
}
}
void
write_all_v(int fd, const struct iovec* iov_in, int iovcnt)
{
ssize_t ret;
struct iovec iov_copy[iovcnt];
memcpy(iov_copy, iov_in, iovcnt * sizeof (struct iovec));
struct iovec* iov = &iov_copy[0];
struct iovec* iov_end = iov + iovcnt;
while (iov < iov_end) {
do {
WITH_IO_SIGNALS_ALLOWED();
ret = writev(fd, iov, iov_end - iov);
} while (ret == -1 && errno == EINTR);
if (ret < 0)
die_errno("writev(%d)", fd);
while (ret > 0) {
size_t chunk = XMIN(ret, iov->iov_len);
iov->iov_len -= chunk;
if (iov->iov_len == 0) {
iov++;
}
ret -= chunk;
}
while (iov < iov_end && iov->iov_len == 0)
++iov;
}
}
__attribute__((unused))
static int
timespec_to_ms(const struct timespec* ts)
{
static const int ns_per_ms = 1000000;
if (ts == NULL)
return -1;
if (ts->tv_sec > INT_MAX / 1000)
return INT_MAX;
return ts->tv_sec * 1000 + ts->tv_nsec / ns_per_ms;
}
#if XPPOLL == XPPOLL_LINUX_SYSCALL
int
xppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout_ts, const sigset_t *sigmask)
{
struct timespec timeout_local;
if (timeout_ts) {
memcpy(&timeout_local, timeout_ts, sizeof (*timeout_ts));
timeout_ts = &timeout_local;
}
return syscall(__NR_ppoll, fds, nfds, timeout_ts, sigmask, _NSIG/8);
}
#elif XPPOLL == XPPOLL_KQUEUE
int
xppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout_ts, const sigset_t *sigmask)
{
int ret = -1;
int kq = ppoll_kq;
assert(kq != -1); // Should have initialized in main()
sigset_t oldmask;
size_t nr_events = 0;
struct kevent* events = NULL;
struct kevent* revents = NULL;
int nret;
int nr_installed_events = 0;
int saved_errno;
struct stat si;
for (unsigned fdno = 0; fdno < nfds; ++fdno)
fds[fdno].revents = 0;
for (unsigned fdno = 0; fdno < nfds; ++fdno) {
if (fds[fdno].fd == -1)
continue;
if ((fds[fdno].events & (POLLIN|POLLOUT)) == 0)
continue;
if (fstat(fds[fdno].fd, &si) == -1)
return -1;
if ((si.st_mode & S_IFMT) == S_IFREG) {
// kqueue has bizarre and rarely useful semantics for
// regular files, so assume that disk files are always
// capable of IO.
fds[fdno].revents = fds[fdno].events & (POLLIN|POLLOUT);
return 1;
}
if (fds[fdno].events & POLLIN)
nr_events++;
if (fds[fdno].events & POLLOUT)
nr_events++;
}
for (int sig = 1; sigmask != NULL && sig < NSIG; ++sig)
if (!sigismember(sigmask, sig))
nr_events += 1;
if (nr_events > INT_MAX ||
nr_events > SIZE_MAX / sizeof (struct kevent))
{
errno = EINVAL;
goto out;
}
events = alloca(sizeof (*events) * nr_events);
memset(events, 0, sizeof (*events) * nr_events);
revents = alloca(sizeof (*revents) * nr_events);
int evno = 0;
for (int sig = 1; sigmask != NULL && sig < NSIG; ++sig) {
if (!sigismember(sigmask, sig)) {
struct kevent* kev = &events[evno++];
kev->ident = sig;
kev->flags = EV_ADD | EV_RECEIPT;
kev->filter = EVFILT_SIGNAL;
}
}
for (unsigned fdno = 0; fdno < nfds; ++fdno) {
if (fds[fdno].fd == -1)
continue;
if (fds[fdno].events & POLLIN) {
struct kevent* kev = &events[evno++];
kev->ident = fds[fdno].fd;
kev->flags = EV_ADD | EV_RECEIPT;
kev->filter = EVFILT_READ;
kev->udata = &fds[fdno];
}
if (fds[fdno].events & POLLOUT) {
struct kevent* kev = &events[evno++];
kev->ident = fds[fdno].fd;
kev->flags = EV_ADD | EV_RECEIPT;
kev->filter = EVFILT_WRITE;
kev->udata = &fds[fdno];
}
}
// Register event filters, but don't receive any events yet.
nret = kevent(kq, events, nr_events, revents, nr_events, 0);
if (nret < 0) {
if (errno != EINTR)
dbg("kevent itself failed: %d", nret);
goto out;
}
int nr_invalid_fds = 0;
ret = 0;
for (evno = 0; evno < nret; ++evno) {
if ((revents[evno].flags & EV_ERROR) == 0) {
dbg("did not get EV_ERROR from revents as expected: "
"flags: 0x%08x", (unsigned) revents[evno].flags);
abort();
}
if (revents[evno].data == 0) {
struct kevent* undo_event = &events[nr_installed_events++];
memset(undo_event, 0, sizeof (*undo_event));
undo_event->ident = revents[evno].ident;
undo_event->filter = revents[evno].filter;
undo_event->flags = EV_DELETE | EV_RECEIPT;
} else if (revents[evno].udata != NULL) {
struct pollfd* fd = revents[evno].udata;
fd->revents = POLLNVAL;
++nr_invalid_fds;
} else {
errno = revents[evno].data;
ret = -1;
}
}
if (ret != 0)
goto out;
ret = -1;
if (nr_invalid_fds > 0) {
ret = nr_invalid_fds;
goto out;
}
// EVFILT_SIGNAL doesn't trigger for signals that were already
// pending before kqueue(2), so we need to explicitly check
// whether any were pending. We allowed these signals to be
// delivered with SIG_SETMASK, so the EINTR return just reflects
// the signal delivery that already happened.
if (sigmask) {
sigset_t pending;
sigpending(&pending);
sigprocmask(SIG_SETMASK, sigmask, &oldmask);
for (int sig = 1; sig < NSIG; ++sig) {
if (sigismember(&pending, sig) && !sigismember(sigmask, sig)) {
sigprocmask(SIG_SETMASK, &oldmask, NULL);
errno = EINTR;
goto out;
}
}
}
nret = kevent(kq, NULL, 0, revents, nr_events, timeout_ts);
if (sigmask)
sigprocmask(SIG_SETMASK, &oldmask, NULL);
if (nret < 0) {
if (errno != EINTR)
dbg("kevent itself failed: %d", nret);
goto out;
}
int nr_happened = 0;
bool sig_happened = false;
for (evno = 0; evno < nret; ++evno) {
struct kevent* kev = &revents[evno];
if (kev->filter == EVFILT_READ ||
kev->filter == EVFILT_WRITE)
{
struct pollfd* p = kev->udata;
assert(p != NULL && p->fd == kev->ident);
if (p->revents == 0)
nr_happened += 1;
p->revents |= (kev->filter == EVFILT_READ ? POLLIN : POLLOUT);
}
if (kev->filter == EVFILT_SIGNAL) {
assert(kev->data != 0);
dbg("got signal %d", (int) kev->data);
sig_happened = true;
}
}
dbg("sig_happened:%d nr_happened:%d", (int)sig_happened, nr_happened);
if (sig_happened && !nr_happened) {
errno = EINTR;
goto out;
}
ret = nr_happened;
out:
saved_errno = errno;
do {
nret = kevent(kq, events, nr_installed_events,
revents, nr_events, NULL);
} while (nret == -1 && errno == EINTR);
if (nret < 0) {
dbg("deleting filters failed with errno %d", errno);
abort(); // Deleting filters should never fail
}
errno = saved_errno;
return ret;
}
#elif XPPOLL == XPPOLL_SYSTEM
int
xppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout_ts, const sigset_t *sigmask)
{
return ppoll(fds, nfds, timeout_ts, sigmask);
}
#elif XPPOLL == XPPOLL_STUPID_WRAPPER
int
xppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout_ts, const sigset_t *sigmask)
{
int ret;
sigset_t saved_sigmask;
sigprocmask(SIG_SETMASK, sigmask, &saved_sigmask);
ret = poll(fds, nfds, timespec_to_ms(timeout_ts));
sigprocmask(SIG_SETMASK, &saved_sigmask, NULL);
return ret;
}
#else
# error Y U no decent signal handling
#endif /* xppoll implementation */
int
xpoll(struct pollfd* fds, nfds_t nfds, int timeout)
{
WITH_IO_SIGNALS_ALLOWED();
int pollret;
do {
pollret = poll(fds, nfds, timeout);
} while (pollret == -1 && errno == EINTR);
return pollret;
}
#ifndef HAVE_DUP3
int
dup3(int oldfd, int newfd, int flags)
{
#ifdef __linux__
return syscall(__NR_dup3, oldfd, newfd, flags);
#else
if (oldfd == newfd) {
errno = EINVAL;
return -1;
}
int rc = dup2(oldfd, newfd);
if (rc < 0)
return -1;
if (merge_O_CLOEXEC_into_fd_flags(newfd, flags) < 0) {
close_saving_errno(newfd);
return -1;
}
return newfd;
#endif
}
#endif
#ifndef HAVE_MKOSTEMP
int
mkostemp(char *template, int flags)
{
int newfd = mkstemp(template);
if (newfd == -1)
return -1;
if (merge_O_CLOEXEC_into_fd_flags(newfd, flags) < 0) {
close_saving_errno(newfd);
return -1;
}
return newfd;
}
#endif
void
replace_stdin_stdout_with_dev_null(void)
{
SCOPED_RESLIST(rl);
int devnull = xopen("/dev/null", O_RDWR, 0);
xdup3nc(devnull, STDIN_FILENO, 0);
xdup3nc(devnull, STDOUT_FILENO, 0);
}
struct xnamed_tempfile_save {
char* name;
int fd;
};
static void
xnamed_tempfile_cleanup(void* arg)
{
struct xnamed_tempfile_save* save = arg;
if (save->fd != -1)
xclose(save->fd);
if (save->name)
(void) unlink(save->name);
}
int
xnamed_tempfile(const char** out_name)
{
struct xnamed_tempfile_save* save = xcalloc(sizeof (*save));
char* name = xaprintf("%s/fb-adb-XXXXXX", DEFAULT_TEMP_DIR);
struct cleanup* cl = cleanup_allocate();
cleanup_commit(cl, xnamed_tempfile_cleanup, save);
save->fd = mkostemp(name, O_CLOEXEC);
if (save->fd == -1)
die_errno("mkostemp");
save->name = name;
*out_name = name;
return save->fd;
}
void
allow_inherit(int fd)
{
int fl = fcntl(fd, F_GETFD);
if (fl < 0 || fcntl(fd, F_SETFD, fl &~ FD_CLOEXEC) < 0)
die_errno("fcntl");
}
char*
xreadlink(const char* path)
{
struct cleanup* cl = NULL;
char* buf = NULL;
size_t bufsz = 64;
ssize_t rc;
do {
bufsz *= 2;
if (bufsz > (size_t) SSIZE_MAX)
die(EINVAL, "readlink path too long");
if (cl) {
free(buf);
cleanup_forget(cl);
}
cl = cleanup_allocate();
buf = malloc(bufsz+1);
if (buf == NULL)
die_oom();
cleanup_commit(cl, free, buf);
rc = readlink(path, buf, bufsz);
} while (rc > 0 && rc == bufsz);
if (rc < 0)
die(errno, "readlink(\"%s\"): %s", path, strerror(errno));
buf[rc] = '\0';
return buf;
}
char*
xdirname(const char* path)
{
SCOPED_RESLIST(rl);
char* xpath = xstrdup(path);
char* ret = dirname(xpath);
WITH_CURRENT_RESLIST(rl->parent);
return xstrdup(ret);
}
char*
xbasename(const char* path)
{
SCOPED_RESLIST(rl);
char* xpath = xstrdup(path);
char* ret = basename(xpath);
WITH_CURRENT_RESLIST(rl->parent);
return xstrdup(ret);
}
#if !defined(HAVE_FALLOCATE) && \
defined(__linux__) && \
defined(__NR_fallocate) && ( \
defined(__ARM_EABI__) || \
defined(__i386__))
__attribute__((unused))
static int
xfallocate(int fd, int mode, uint64_t offset, uint64_t length)
{
# ifdef __ARM_EABI__
return syscall(__NR_fallocate, fd, mode,
(uint32_t)(offset >> 0),
(uint32_t)(offset >> 32),
(uint32_t)(length >> 0),
(uint32_t)(length >> 32));
# else
# ifdef __ANDROID__
if (api_level() <= 16) {
errno = EOPNOTSUPP; // Buggy syscall function
return -1;
}
# endif
return syscall(__NR_fallocate, fd, mode, offset, length);
# endif
}
# define HAVE_XFALLOCATE 1
#endif
#ifndef OFF_T_MAX
# if SIZEOF_OFF_T==8
# define OFF_T_MAX INT64_MAX
# elif SIZEOF_OFF_T==4
# define OFF_T_MAX INT32_MAX
# else
# error "bizarre system"
# endif
#endif
bool
fallocate_if_supported(int fd, uint64_t size)
{
int ret;
uint64_t max_size;
#if defined(HAVE_XFALLOCATE)
max_size = INT64_MAX;
#else
max_size = OFF_T_MAX;
#endif
if (size > max_size)
die(EINVAL, "file size too large");
#if HAVE_XFALLOCATE && SIZEOF_OFF_T==4
ret = xfallocate(fd, 0, 0, size);
#elif defined(HAVE_POSIX_FALLOCATE) && !defined(__GLIBC__)
// Use the Linux system call directly instead of posix_fallocate
// because glibc exceeds even its high standard of badness and
// tries to emulate posix_fallocate using goddamn pwrite on
// filesystem where rela fallocate isn't available.
// This emulation is unforgivable: it causes silent data loss!
//
// tl;dr: we can't trust posix_fallocate on glibc systems. How
// the hell do open source systems manage to boot?
ret = posix_fallocate(fd, 0, size);
#elif defined(HAVE_FALLOCATE)
ret = fallocate(fd, 0, 0, size);
#elif defined(HAVE_XFALLOCATE)
ret = xfallocate(fd, 0, 0, size);
#else
ret = -1;
errno = ENOSYS;
#endif
if (ret == -1) {
if (errno != ENOSYS && errno != EOPNOTSUPP)
die_errno("fallocate(%llu)", (unsigned long long) size);
return false;
}
return true;
}