-
Notifications
You must be signed in to change notification settings - Fork 3
/
posix.c
2649 lines (2435 loc) · 81.2 KB
/
posix.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) 2016-2018 Nuxi, https://nuxi.nl/
//
// SPDX-License-Identifier: BSD-2-Clause
#include "config.h"
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <cloudabi_syscalls_info.h>
#include "emulate.h"
#include "futex.h"
#include "locking.h"
#include "numeric_limits.h"
#include "posix.h"
#include "random.h"
#include "refcount.h"
#include "rights.h"
#include "str.h"
#include "tidpool.h"
#include "tls.h"
// struct iovec must have the same layout as cloudabi_iovec_t.
static_assert(offsetof(struct iovec, iov_base) ==
offsetof(cloudabi_iovec_t, buf),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_base) ==
sizeof(((cloudabi_iovec_t *)0)->buf),
"Size mismatch");
static_assert(offsetof(struct iovec, iov_len) ==
offsetof(cloudabi_iovec_t, buf_len),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_len) ==
sizeof(((cloudabi_iovec_t *)0)->buf_len),
"Size mismatch");
static_assert(sizeof(struct iovec) == sizeof(cloudabi_iovec_t),
"Size mismatch");
// struct iovec must have the same layout as cloudabi_ciovec_t.
static_assert(offsetof(struct iovec, iov_base) ==
offsetof(cloudabi_ciovec_t, buf),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_base) ==
sizeof(((cloudabi_ciovec_t *)0)->buf),
"Size mismatch");
static_assert(offsetof(struct iovec, iov_len) ==
offsetof(cloudabi_ciovec_t, buf_len),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_len) ==
sizeof(((cloudabi_ciovec_t *)0)->buf_len),
"Size mismatch");
static_assert(sizeof(struct iovec) == sizeof(cloudabi_ciovec_t),
"Size mismatch");
// Current thread's file descriptor table.
static _Thread_local struct fd_table *curfds;
// Current thread's identifier.
_Thread_local cloudabi_tid_t curtid;
// Converts a POSIX error code to a CloudABI error code.
static cloudabi_errno_t convert_errno(int error) {
static const cloudabi_errno_t errors[] = {
#define X(v) [v] = CLOUDABI_##v
X(E2BIG),
X(EACCES),
X(EADDRINUSE),
X(EADDRNOTAVAIL),
X(EAFNOSUPPORT),
X(EAGAIN),
X(EALREADY),
X(EBADF),
X(EBADMSG),
X(EBUSY),
X(ECANCELED),
X(ECHILD),
X(ECONNABORTED),
X(ECONNREFUSED),
X(ECONNRESET),
X(EDEADLK),
X(EDESTADDRREQ),
X(EDOM),
X(EDQUOT),
X(EEXIST),
X(EFAULT),
X(EFBIG),
X(EHOSTUNREACH),
X(EIDRM),
X(EILSEQ),
X(EINPROGRESS),
X(EINTR),
X(EINVAL),
X(EIO),
X(EISCONN),
X(EISDIR),
X(ELOOP),
X(EMFILE),
X(EMLINK),
X(EMSGSIZE),
X(EMULTIHOP),
X(ENAMETOOLONG),
X(ENETDOWN),
X(ENETRESET),
X(ENETUNREACH),
X(ENFILE),
X(ENOBUFS),
X(ENODEV),
X(ENOENT),
X(ENOEXEC),
X(ENOLCK),
X(ENOLINK),
X(ENOMEM),
X(ENOMSG),
X(ENOPROTOOPT),
X(ENOSPC),
X(ENOSYS),
#ifdef ENOTCAPABLE
X(ENOTCAPABLE),
#endif
X(ENOTCONN),
X(ENOTDIR),
X(ENOTEMPTY),
X(ENOTRECOVERABLE),
X(ENOTSOCK),
X(ENOTSUP),
X(ENOTTY),
X(ENXIO),
X(EOVERFLOW),
X(EOWNERDEAD),
X(EPERM),
X(EPIPE),
X(EPROTO),
X(EPROTONOSUPPORT),
X(EPROTOTYPE),
X(ERANGE),
X(EROFS),
X(ESPIPE),
X(ESRCH),
X(ESTALE),
X(ETIMEDOUT),
X(ETXTBSY),
X(EXDEV),
#undef X
#if EOPNOTSUPP != ENOTSUP
[EOPNOTSUPP] = CLOUDABI_ENOTSUP,
#endif
#if EWOULDBLOCK != EAGAIN
[EWOULDBLOCK] = CLOUDABI_EAGAIN,
#endif
};
if (error < 0 || (size_t)error >= sizeof(errors) / sizeof(errors[0]) ||
errors[error] == 0)
return CLOUDABI_ENOSYS;
return errors[error];
}
// Converts a POSIX timespec to a CloudABI timestamp.
static cloudabi_timestamp_t convert_timespec(const struct timespec *ts) {
if (ts->tv_sec < 0)
return 0;
if ((cloudabi_timestamp_t)ts->tv_sec >= UINT64_MAX / 1000000000)
return UINT64_MAX;
return (cloudabi_timestamp_t)ts->tv_sec * 1000000000 + ts->tv_nsec;
}
// Converts a CloudABI clock identifier to a POSIX clock identifier.
static bool convert_clockid(cloudabi_clockid_t in, clockid_t *out) {
switch (in) {
case CLOUDABI_CLOCK_MONOTONIC:
*out = CLOCK_MONOTONIC;
return true;
case CLOUDABI_CLOCK_PROCESS_CPUTIME_ID:
*out = CLOCK_PROCESS_CPUTIME_ID;
return true;
case CLOUDABI_CLOCK_REALTIME:
*out = CLOCK_REALTIME;
return true;
case CLOUDABI_CLOCK_THREAD_CPUTIME_ID:
*out = CLOCK_THREAD_CPUTIME_ID;
return true;
default:
return false;
}
}
static cloudabi_errno_t sys_clock_res_get(cloudabi_clockid_t clock_id,
cloudabi_timestamp_t *resolution) {
clockid_t nclock_id;
if (!convert_clockid(clock_id, &nclock_id))
return CLOUDABI_EINVAL;
struct timespec ts;
if (clock_getres(nclock_id, &ts) < 0)
return convert_errno(errno);
*resolution = convert_timespec(&ts);
return 0;
}
static cloudabi_errno_t sys_clock_time_get(cloudabi_clockid_t clock_id,
cloudabi_timestamp_t precision,
cloudabi_timestamp_t *time) {
clockid_t nclock_id;
if (!convert_clockid(clock_id, &nclock_id))
return CLOUDABI_EINVAL;
struct timespec ts;
if (clock_gettime(nclock_id, &ts) < 0)
return convert_errno(errno);
*time = convert_timespec(&ts);
return 0;
}
static cloudabi_errno_t sys_condvar_signal(_Atomic(cloudabi_condvar_t) *
condvar,
cloudabi_scope_t scope,
cloudabi_nthreads_t nwaiters) {
return futex_op_condvar_signal(condvar, scope, nwaiters);
}
struct fd_object {
struct refcount refcount;
cloudabi_filetype_t type;
int number;
union {
// Data associated with directory file descriptors.
struct {
struct mutex lock; // Lock to protect members below.
DIR *handle; // Directory handle.
cloudabi_dircookie_t offset; // Offset of the directory.
} directory;
};
};
struct fd_entry {
struct fd_object *object;
cloudabi_rights_t rights_base;
cloudabi_rights_t rights_inheriting;
};
void fd_table_init(struct fd_table *ft) {
rwlock_init(&ft->lock);
ft->entries = NULL;
ft->size = 0;
ft->used = 0;
curfds = ft;
}
// Looks up a file descriptor table entry by number and required rights.
static cloudabi_errno_t fd_table_get_entry(struct fd_table *ft,
cloudabi_fd_t fd,
cloudabi_rights_t rights_base,
cloudabi_rights_t rights_inheriting,
struct fd_entry **ret)
REQUIRES_SHARED(ft->lock) {
// Test for file descriptor existence.
if (fd >= ft->size)
return CLOUDABI_EBADF;
struct fd_entry *fe = &ft->entries[fd];
if (fe->object == NULL)
return CLOUDABI_EBADF;
// Validate rights.
if ((~fe->rights_base & rights_base) != 0 ||
(~fe->rights_inheriting & rights_inheriting) != 0)
return CLOUDABI_ENOTCAPABLE;
*ret = fe;
return 0;
}
// Grows the file descriptor table to a required lower bound and a
// minimum number of free file descriptor table entries.
static bool fd_table_grow(struct fd_table *ft, size_t min, size_t incr)
REQUIRES_EXCLUSIVE(ft->lock) {
if (ft->size <= min || ft->size < (ft->used + incr) * 2) {
// Keep on doubling the table size until we've met our constraints.
size_t size = ft->size == 0 ? 1 : ft->size;
while (size <= min || size < (ft->used + incr) * 2)
size *= 2;
// Grow the file descriptor table's allocation.
struct fd_entry *entries = realloc(ft->entries, sizeof(*entries) * size);
if (entries == NULL)
return false;
// Mark all new file descriptors as unused.
for (size_t i = ft->size; i < size; ++i)
entries[i].object = NULL;
ft->entries = entries;
ft->size = size;
}
return true;
}
// Allocates a new file descriptor object.
static cloudabi_errno_t fd_object_new(cloudabi_filetype_t type,
struct fd_object **fo)
TRYLOCKS_SHARED(0, (*fo)->refcount) {
*fo = malloc(sizeof(**fo));
if (*fo == NULL)
return CLOUDABI_ENOMEM;
refcount_init(&(*fo)->refcount, 1);
(*fo)->type = type;
(*fo)->number = -1;
return 0;
}
// Attaches a file descriptor to the file descriptor table.
static void fd_table_attach(struct fd_table *ft, cloudabi_fd_t fd,
struct fd_object *fo, cloudabi_rights_t rights_base,
cloudabi_rights_t rights_inheriting)
REQUIRES_EXCLUSIVE(ft->lock) CONSUMES(fo->refcount) {
assert(ft->size > fd && "File descriptor table too small");
struct fd_entry *fe = &ft->entries[fd];
assert(fe->object == NULL && "Attempted to overwrite an existing descriptor");
fe->object = fo;
fe->rights_base = rights_base;
fe->rights_inheriting = rights_inheriting;
++ft->used;
assert(ft->size >= ft->used * 2 && "File descriptor too full");
}
// Detaches a file descriptor from the file descriptor table.
static void fd_table_detach(struct fd_table *ft, cloudabi_fd_t fd,
struct fd_object **fo) REQUIRES_EXCLUSIVE(ft->lock)
PRODUCES((*fo)->refcount) {
assert(ft->size > fd && "File descriptor table too small");
struct fd_entry *fe = &ft->entries[fd];
*fo = fe->object;
assert(*fo != NULL && "Attempted to detach nonexistent descriptor");
fe->object = NULL;
assert(ft->used > 0 && "Reference count mismatch");
--ft->used;
}
// Determines the type of a file descriptor and its maximum set of
// rights that should be attached to it.
static cloudabi_errno_t fd_determine_type_rights(
int fd, cloudabi_filetype_t *type, cloudabi_rights_t *rights_base,
cloudabi_rights_t *rights_inheriting) {
struct stat sb;
if (fstat(fd, &sb) < 0)
return convert_errno(errno);
if (S_ISBLK(sb.st_mode)) {
*type = CLOUDABI_FILETYPE_BLOCK_DEVICE;
*rights_base = RIGHTS_BLOCK_DEVICE_BASE;
*rights_inheriting = RIGHTS_BLOCK_DEVICE_INHERITING;
} else if (S_ISCHR(sb.st_mode)) {
*type = CLOUDABI_FILETYPE_CHARACTER_DEVICE;
#if CONFIG_HAS_ISATTY
if (isatty(fd)) {
*rights_base = RIGHTS_TTY_BASE;
*rights_inheriting = RIGHTS_TTY_INHERITING;
} else
#endif
{
*rights_base = RIGHTS_CHARACTER_DEVICE_BASE;
*rights_inheriting = RIGHTS_CHARACTER_DEVICE_INHERITING;
}
} else if (S_ISDIR(sb.st_mode)) {
*type = CLOUDABI_FILETYPE_DIRECTORY;
*rights_base = RIGHTS_DIRECTORY_BASE;
*rights_inheriting = RIGHTS_DIRECTORY_INHERITING;
} else if (S_ISREG(sb.st_mode)) {
*type = CLOUDABI_FILETYPE_REGULAR_FILE;
*rights_base = RIGHTS_REGULAR_FILE_BASE;
*rights_inheriting = RIGHTS_REGULAR_FILE_INHERITING;
} else if (S_ISSOCK(sb.st_mode)) {
int socktype;
socklen_t socktypelen = sizeof(socktype);
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &socktype, &socktypelen) < 0)
return convert_errno(errno);
switch (socktype) {
case SOCK_DGRAM:
*type = CLOUDABI_FILETYPE_SOCKET_DGRAM;
break;
case SOCK_STREAM:
*type = CLOUDABI_FILETYPE_SOCKET_STREAM;
break;
default:
return CLOUDABI_EINVAL;
}
*rights_base = RIGHTS_SOCKET_BASE;
*rights_inheriting = RIGHTS_SOCKET_INHERITING;
} else if (S_ISFIFO(sb.st_mode)) {
*type = CLOUDABI_FILETYPE_SOCKET_STREAM;
*rights_base = RIGHTS_SOCKET_BASE;
*rights_inheriting = RIGHTS_SOCKET_INHERITING;
#ifdef S_TYPEISSHM
} else if (S_TYPEISSHM(&sb)) {
*type = CLOUDABI_FILETYPE_SHARED_MEMORY;
*rights_base = RIGHTS_SHARED_MEMORY_BASE;
*rights_inheriting = RIGHTS_SHARED_MEMORY_INHERITING;
#endif
} else {
return CLOUDABI_EINVAL;
}
// Strip off read/write bits based on the access mode.
switch (fcntl(fd, F_GETFL) & O_ACCMODE) {
case O_RDONLY:
*rights_base &= ~CLOUDABI_RIGHT_FD_WRITE;
break;
case O_WRONLY:
*rights_base &= ~CLOUDABI_RIGHT_FD_READ;
break;
}
return 0;
}
// Returns the underlying file descriptor number of a file descriptor
// object. This function can only be applied to objects that have an
// underlying file descriptor number.
static int fd_number(const struct fd_object *fo) {
int number = fo->number;
assert(number >= 0 && "fd_number() called on virtual file descriptor");
return number;
}
// Lowers the reference count on a file descriptor object. When the
// reference count reaches zero, its resources are cleaned up.
static void fd_object_release(struct fd_object *fo) UNLOCKS(fo->refcount) {
if (refcount_release(&fo->refcount)) {
switch (fo->type) {
case CLOUDABI_FILETYPE_DIRECTORY:
// For directories we may keep track of a DIR object. Calling
// closedir() on it also closes the underlying file descriptor.
mutex_destroy(&fo->directory.lock);
if (fo->directory.handle == NULL) {
close(fd_number(fo));
} else {
closedir(fo->directory.handle);
}
break;
default:
close(fd_number(fo));
break;
}
free(fo);
}
}
// Inserts an already existing file descriptor into the file descriptor
// table.
bool fd_table_insert_existing(struct fd_table *ft, cloudabi_fd_t in, int out) {
cloudabi_filetype_t type;
cloudabi_rights_t rights_base, rights_inheriting;
if (fd_determine_type_rights(out, &type, &rights_base, &rights_inheriting) !=
0)
return false;
struct fd_object *fo;
cloudabi_errno_t error = fd_object_new(type, &fo);
if (error != 0)
return false;
fo->number = out;
if (type == CLOUDABI_FILETYPE_DIRECTORY) {
mutex_init(&fo->directory.lock);
fo->directory.handle = NULL;
}
// Grow the file descriptor table if needed.
rwlock_wrlock(&ft->lock);
if (!fd_table_grow(ft, in, 1)) {
rwlock_unlock(&ft->lock);
fd_object_release(fo);
return false;
}
fd_table_attach(ft, in, fo, rights_base, rights_inheriting);
rwlock_unlock(&ft->lock);
return true;
}
// Picks an unused slot from the file descriptor table.
static cloudabi_fd_t fd_table_unused(struct fd_table *ft)
REQUIRES_SHARED(ft->lock) {
assert(ft->size > ft->used && "File descriptor table has no free slots");
for (;;) {
cloudabi_fd_t fd = random_uniform(ft->size);
if (ft->entries[fd].object == NULL)
return fd;
}
}
// Inserts a file descriptor object into an unused slot of the file
// descriptor table.
static cloudabi_errno_t fd_table_insert(struct fd_table *ft,
struct fd_object *fo,
cloudabi_rights_t rights_base,
cloudabi_rights_t rights_inheriting,
cloudabi_fd_t *out)
REQUIRES_UNLOCKED(ft->lock) UNLOCKS(fo->refcount) {
// Grow the file descriptor table if needed.
rwlock_wrlock(&ft->lock);
if (!fd_table_grow(ft, 0, 1)) {
rwlock_unlock(&ft->lock);
fd_object_release(fo);
return convert_errno(errno);
}
*out = fd_table_unused(ft);
fd_table_attach(ft, *out, fo, rights_base, rights_inheriting);
rwlock_unlock(&ft->lock);
return 0;
}
// Inserts a numerical file descriptor into the file descriptor table.
static cloudabi_errno_t fd_table_insert_fd(struct fd_table *ft, int in,
cloudabi_filetype_t type,
cloudabi_rights_t rights_base,
cloudabi_rights_t rights_inheriting,
cloudabi_fd_t *out)
REQUIRES_UNLOCKED(ft->lock) {
struct fd_object *fo;
cloudabi_errno_t error = fd_object_new(type, &fo);
if (error != 0) {
close(in);
return error;
}
fo->number = in;
if (type == CLOUDABI_FILETYPE_DIRECTORY) {
mutex_init(&fo->directory.lock);
fo->directory.handle = NULL;
}
return fd_table_insert(ft, fo, rights_base, rights_inheriting, out);
}
// Inserts a pair of numerical file descriptors into the file descriptor
// table.
static cloudabi_errno_t fd_table_insert_fdpair(
struct fd_table *ft, const int *in, cloudabi_filetype_t type,
cloudabi_rights_t rights_base1, cloudabi_rights_t rights_base2,
cloudabi_rights_t rights_inheriting, cloudabi_fd_t *out1,
cloudabi_fd_t *out2) REQUIRES_UNLOCKED(ft->lock) {
struct fd_object *fo1;
cloudabi_errno_t error = fd_object_new(type, &fo1);
if (error != 0) {
close(in[0]);
close(in[1]);
return error;
}
fo1->number = in[0];
struct fd_object *fo2;
error = fd_object_new(type, &fo2);
if (error != 0) {
fd_object_release(fo1);
close(in[1]);
return error;
}
fo2->number = in[1];
// Grow the file descriptor table if needed.
rwlock_wrlock(&ft->lock);
if (!fd_table_grow(ft, 0, 2)) {
rwlock_unlock(&ft->lock);
fd_object_release(fo1);
fd_object_release(fo2);
return convert_errno(errno);
}
*out1 = fd_table_unused(ft);
fd_table_attach(ft, *out1, fo1, rights_base1, rights_inheriting);
*out2 = fd_table_unused(ft);
fd_table_attach(ft, *out2, fo2, rights_base2, rights_inheriting);
rwlock_unlock(&ft->lock);
return 0;
}
static cloudabi_errno_t sys_fd_close(cloudabi_fd_t fd) {
// Validate the file descriptor.
struct fd_table *ft = curfds;
rwlock_wrlock(&ft->lock);
struct fd_entry *fe;
cloudabi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe);
if (error != 0) {
rwlock_unlock(&ft->lock);
return error;
}
// Remove it from the file descriptor table.
struct fd_object *fo;
fd_table_detach(ft, fd, &fo);
rwlock_unlock(&ft->lock);
fd_object_release(fo);
return 0;
}
static cloudabi_errno_t sys_fd_create1(cloudabi_filetype_t type,
cloudabi_fd_t *fd) {
switch (type) {
case CLOUDABI_FILETYPE_SHARED_MEMORY: {
#ifdef SHM_ANON
int nfd = shm_open(SHM_ANON, O_RDWR, 0666);
if (nfd < 0)
return convert_errno(errno);
#else
int nfd;
for (;;) {
unsigned int i;
random_buf(&i, sizeof(i));
char buf[64];
snprintf(buf, sizeof(buf), "/anon%u", i);
nfd = shm_open(buf, O_RDWR | O_EXCL | O_CREAT, 0700);
if (nfd < 0) {
if (errno == EEXIST)
continue;
return convert_errno(errno);
}
shm_unlink(buf);
break;
}
#endif
return fd_table_insert_fd(curfds, nfd, type, RIGHTS_SHARED_MEMORY_BASE,
RIGHTS_SHARED_MEMORY_INHERITING, fd);
}
default:
return CLOUDABI_EINVAL;
}
}
static cloudabi_errno_t fd_create_socketpair(cloudabi_filetype_t type,
int socktype, cloudabi_fd_t *fd1,
cloudabi_fd_t *fd2) {
int fds[2];
if (socketpair(AF_UNIX, socktype, 0, fds) < 0)
return convert_errno(errno);
return fd_table_insert_fdpair(curfds, fds, type, RIGHTS_SOCKET_BASE,
RIGHTS_SOCKET_BASE, RIGHTS_SOCKET_INHERITING,
fd1, fd2);
}
static cloudabi_errno_t sys_fd_create2(cloudabi_filetype_t type,
cloudabi_fd_t *fd1, cloudabi_fd_t *fd2) {
switch (type) {
case CLOUDABI_FILETYPE_SOCKET_DGRAM:
return fd_create_socketpair(type, SOCK_DGRAM, fd1, fd2);
case CLOUDABI_FILETYPE_SOCKET_STREAM:
return fd_create_socketpair(type, SOCK_STREAM, fd1, fd2);
default:
return CLOUDABI_EINVAL;
}
}
// Look up a file descriptor object in a locked file descriptor table
// and increases its reference count.
static cloudabi_errno_t fd_object_get_locked(
struct fd_object **fo, struct fd_table *ft, cloudabi_fd_t fd,
cloudabi_rights_t rights_base, cloudabi_rights_t rights_inheriting)
TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount) REQUIRES_EXCLUSIVE(ft->lock) {
// Test whether the file descriptor number is valid.
struct fd_entry *fe;
cloudabi_errno_t error =
fd_table_get_entry(ft, fd, rights_base, rights_inheriting, &fe);
if (error != 0)
return error;
// Increase the reference count on the file descriptor object. A copy
// of the rights are also stored, so callers can still access those if
// needed.
*fo = fe->object;
refcount_acquire(&(*fo)->refcount);
return 0;
}
// Temporarily locks the file descriptor table to look up a file
// descriptor object, increases its reference count and drops the lock.
static cloudabi_errno_t fd_object_get(struct fd_object **fo, cloudabi_fd_t fd,
cloudabi_rights_t rights_base,
cloudabi_rights_t rights_inheriting)
TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount) {
struct fd_table *ft = curfds;
rwlock_rdlock(&ft->lock);
cloudabi_errno_t error =
fd_object_get_locked(fo, ft, fd, rights_base, rights_inheriting);
rwlock_unlock(&ft->lock);
return error;
}
static cloudabi_errno_t sys_fd_datasync(cloudabi_fd_t fd) {
struct fd_object *fo;
cloudabi_errno_t error =
fd_object_get(&fo, fd, CLOUDABI_RIGHT_FD_DATASYNC, 0);
if (error != 0)
return error;
#if CONFIG_HAS_FDATASYNC
int ret = fdatasync(fd_number(fo));
#else
int ret = fsync(fd_number(fo));
#endif
fd_object_release(fo);
if (ret < 0)
return convert_errno(errno);
return 0;
}
static cloudabi_errno_t sys_fd_dup(cloudabi_fd_t from, cloudabi_fd_t *fd) {
struct fd_table *ft = curfds;
rwlock_wrlock(&ft->lock);
struct fd_entry *fe;
cloudabi_errno_t error = fd_table_get_entry(ft, from, 0, 0, &fe);
if (error != 0) {
rwlock_unlock(&ft->lock);
return error;
}
// Grow the file descriptor table if needed.
if (!fd_table_grow(ft, 0, 1)) {
rwlock_unlock(&ft->lock);
return convert_errno(errno);
}
// Attach it to a new place in the table.
*fd = fd_table_unused(ft);
refcount_acquire(&fe->object->refcount);
fd_table_attach(ft, *fd, fe->object, fe->rights_base, fe->rights_inheriting);
rwlock_unlock(&ft->lock);
return 0;
}
static cloudabi_errno_t sys_fd_pread(cloudabi_fd_t fd,
const cloudabi_iovec_t *iov, size_t iovcnt,
cloudabi_filesize_t offset,
size_t *nread) {
if (iovcnt == 0)
return CLOUDABI_EINVAL;
struct fd_object *fo;
cloudabi_errno_t error = fd_object_get(
&fo, fd, CLOUDABI_RIGHT_FD_READ | CLOUDABI_RIGHT_FD_SEEK, 0);
if (error != 0)
return error;
#if CONFIG_HAS_PREADV
ssize_t len =
preadv(fd_number(fo), (const struct iovec *)iov, iovcnt, offset);
fd_object_release(fo);
if (len < 0)
return convert_errno(errno);
*nread = len;
return 0;
#else
if (iovcnt == 1) {
ssize_t len = pread(fd_number(fo), iov->buf, iov->buf_len, offset);
fd_object_release(fo);
if (len < 0)
return convert_errno(errno);
*nread = len;
return 0;
} else {
// Allocate a single buffer to fit all data.
size_t totalsize = 0;
for (size_t i = 0; i < iovcnt; ++i)
totalsize += iov[i].buf_len;
char *buf = malloc(totalsize);
if (buf == NULL) {
fd_object_release(fo);
return CLOUDABI_ENOMEM;
}
// Perform a single read operation.
ssize_t len = pread(fd_number(fo), buf, totalsize, offset);
fd_object_release(fo);
if (len < 0) {
free(buf);
return convert_errno(errno);
}
// Copy data back to vectors.
size_t bufoff = 0;
for (size_t i = 0; i < iovcnt; ++i) {
if (bufoff + iov[i].buf_len < len) {
memcpy(iov[i].buf, buf + bufoff, iov[i].buf_len);
bufoff += iov[i].buf_len;
} else {
memcpy(iov[i].buf, buf + bufoff, len - bufoff);
break;
}
}
free(buf);
*nread = len;
return 0;
}
#endif
}
static cloudabi_errno_t sys_fd_pwrite(cloudabi_fd_t fd,
const cloudabi_ciovec_t *iov,
size_t iovcnt, cloudabi_filesize_t offset,
size_t *nwritten) {
if (iovcnt == 0)
return CLOUDABI_EINVAL;
struct fd_object *fo;
cloudabi_errno_t error = fd_object_get(
&fo, fd, CLOUDABI_RIGHT_FD_WRITE | CLOUDABI_RIGHT_FD_SEEK, 0);
if (error != 0)
return error;
ssize_t len;
#if CONFIG_HAS_PWRITEV
len = pwritev(fd_number(fo), (const struct iovec *)iov, iovcnt, offset);
#else
if (iovcnt == 1) {
len = pwrite(fd_number(fo), iov->buf, iov->buf_len, offset);
} else {
// Allocate a single buffer to fit all data.
size_t totalsize = 0;
for (size_t i = 0; i < iovcnt; ++i)
totalsize += iov[i].buf_len;
char *buf = malloc(totalsize);
if (buf == NULL) {
fd_object_release(fo);
return CLOUDABI_ENOMEM;
}
size_t bufoff = 0;
for (size_t i = 0; i < iovcnt; ++i) {
memcpy(buf + bufoff, iov[i].buf, iov[i].buf_len);
bufoff += iov[i].buf_len;
}
// Perform a single write operation.
len = pwrite(fd_number(fo), buf, totalsize, offset);
free(buf);
}
#endif
fd_object_release(fo);
if (len < 0)
return convert_errno(errno);
*nwritten = len;
return 0;
}
static cloudabi_errno_t sys_fd_read(cloudabi_fd_t fd,
const cloudabi_iovec_t *iov, size_t iovcnt,
size_t *nread) {
struct fd_object *fo;
cloudabi_errno_t error = fd_object_get(&fo, fd, CLOUDABI_RIGHT_FD_READ, 0);
if (error != 0)
return error;
ssize_t len = readv(fd_number(fo), (const struct iovec *)iov, iovcnt);
fd_object_release(fo);
if (len < 0)
return convert_errno(errno);
*nread = len;
return 0;
}
static cloudabi_errno_t sys_fd_replace(cloudabi_fd_t from, cloudabi_fd_t to) {
struct fd_table *ft = curfds;
rwlock_wrlock(&ft->lock);
struct fd_entry *fe_from;
cloudabi_errno_t error = fd_table_get_entry(ft, from, 0, 0, &fe_from);
if (error != 0) {
rwlock_unlock(&ft->lock);
return error;
}
struct fd_entry *fe_to;
error = fd_table_get_entry(ft, to, 0, 0, &fe_to);
if (error != 0) {
rwlock_unlock(&ft->lock);
return error;
}
struct fd_object *fo;
fd_table_detach(ft, to, &fo);
refcount_acquire(&fe_from->object->refcount);
fd_table_attach(ft, to, fe_from->object, fe_from->rights_base,
fe_from->rights_inheriting);
rwlock_unlock(&ft->lock);
fd_object_release(fo);
return 0;
}
static cloudabi_errno_t sys_fd_seek(cloudabi_fd_t fd,
cloudabi_filedelta_t offset,
cloudabi_whence_t whence,
cloudabi_filesize_t *newoffset) {
int nwhence;
switch (whence) {
case CLOUDABI_WHENCE_CUR:
nwhence = SEEK_CUR;
break;
case CLOUDABI_WHENCE_END:
nwhence = SEEK_END;
break;
case CLOUDABI_WHENCE_SET:
nwhence = SEEK_SET;
break;
default:
return CLOUDABI_EINVAL;
}
struct fd_object *fo;
cloudabi_errno_t error =
fd_object_get(&fo, fd,
offset == 0 && whence == CLOUDABI_WHENCE_CUR
? CLOUDABI_RIGHT_FD_TELL
: CLOUDABI_RIGHT_FD_SEEK | CLOUDABI_RIGHT_FD_TELL,
0);
if (error != 0)
return error;
off_t ret = lseek(fd_number(fo), offset, nwhence);
fd_object_release(fo);
if (ret < 0)
return convert_errno(errno);
*newoffset = ret;
return 0;
}
static cloudabi_errno_t sys_fd_stat_get(cloudabi_fd_t fd,
cloudabi_fdstat_t *buf) {
struct fd_table *ft = curfds;
rwlock_rdlock(&ft->lock);
struct fd_entry *fe;
cloudabi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe);
if (error != 0) {
rwlock_unlock(&ft->lock);
return error;
}
// Extract file descriptor type and rights.
struct fd_object *fo = fe->object;
*buf = (cloudabi_fdstat_t){
.fs_filetype = fo->type,
.fs_rights_base = fe->rights_base,
.fs_rights_inheriting = fe->rights_inheriting,
};
// Fetch file descriptor flags.
int ret;
switch (fo->type) {
default:
ret = fcntl(fd_number(fo), F_GETFL);
break;
}
rwlock_unlock(&ft->lock);
if (ret < 0)
return convert_errno(errno);
if ((ret & O_APPEND) != 0)
buf->fs_flags |= CLOUDABI_FDFLAG_APPEND;
#ifdef O_DSYNC
if ((ret & O_DSYNC) != 0)
buf->fs_flags |= CLOUDABI_FDFLAG_DSYNC;
#endif
if ((ret & O_NONBLOCK) != 0)
buf->fs_flags |= CLOUDABI_FDFLAG_NONBLOCK;
#ifdef O_RSYNC
if ((ret & O_RSYNC) != 0)
buf->fs_flags |= CLOUDABI_FDFLAG_RSYNC;
#endif
if ((ret & O_SYNC) != 0)
buf->fs_flags |= CLOUDABI_FDFLAG_SYNC;
return 0;
}
static cloudabi_errno_t sys_fd_stat_put(cloudabi_fd_t fd,
const cloudabi_fdstat_t *buf,
cloudabi_fdsflags_t flags) {
switch (flags) {
case CLOUDABI_FDSTAT_FLAGS: {
int noflags = 0;
if ((buf->fs_flags & CLOUDABI_FDFLAG_APPEND) != 0)
noflags |= O_APPEND;
if ((buf->fs_flags & CLOUDABI_FDFLAG_DSYNC) != 0)
#ifdef O_DSYNC
noflags |= O_DSYNC;
#else
noflags |= O_SYNC;
#endif
if ((buf->fs_flags & CLOUDABI_FDFLAG_NONBLOCK) != 0)
noflags |= O_NONBLOCK;
if ((buf->fs_flags & CLOUDABI_FDFLAG_RSYNC) != 0)
#ifdef O_RSYNC
noflags |= O_RSYNC;