-
-
Notifications
You must be signed in to change notification settings - Fork 605
/
main.cc
2751 lines (2302 loc) · 63.5 KB
/
main.cc
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) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
/*
* Copyright (c) 2005-2007, Kohsuke Ohtani
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/sendfile.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#define open __open_variadic
#define fcntl __fcntl_variadic
#include <fcntl.h>
#undef open
#undef fcntl
#include <dlfcn.h>
#include <osv/prex.h>
#include <osv/export.h>
#include <osv/vnode.h>
#include <osv/stubbing.hh>
#include <osv/ioctl.h>
#include <osv/trace.hh>
#include <osv/run.hh>
#include <osv/mount.h>
#include <drivers/console.hh>
#include "vfs.h"
#include "libc/internal/libc.h"
#include <algorithm>
#include <unordered_map>
#include <sys/file.h>
#include "fs/fs.hh"
#include "libc/libc.hh"
#include <mntent.h>
#include <sys/mman.h>
#include <osv/clock.hh>
#include <api/utime.h>
#include <chrono>
#include "drivers/zfs.hh"
#include "bsd/porting/shrinker.h"
using namespace std;
#ifdef DEBUG_VFS
int vfs_debug = VFSDB_FLAGS;
#endif
std::atomic<mode_t> global_umask{S_IWGRP | S_IWOTH};
static inline mode_t apply_umask(mode_t mode)
{
return mode & ~global_umask.load(std::memory_order_relaxed);
}
TRACEPOINT(trace_vfs_open, "\"%s\" 0x%x 0%0o", const char*, int, mode_t);
TRACEPOINT(trace_vfs_open_ret, "%d", int);
TRACEPOINT(trace_vfs_open_err, "%d", int);
struct task *main_task; /* we only have a single process */
extern "C" OSV_LIBC_API
int open(const char *pathname, int flags, ...)
{
mode_t mode = 0;
if (flags & O_CREAT) {
va_list ap;
va_start(ap, flags);
mode = apply_umask(va_arg(ap, mode_t));
va_end(ap);
}
trace_vfs_open(pathname, flags, mode);
struct task *t = main_task;
char path[PATH_MAX];
struct file *fp;
int fd, error;
int acc;
acc = 0;
switch (flags & O_ACCMODE) {
case O_RDONLY:
acc = VREAD;
break;
case O_WRONLY:
acc = VWRITE;
break;
case O_RDWR:
acc = VREAD | VWRITE;
break;
}
error = task_conv(t, pathname, acc, path);
if (error)
goto out_errno;
error = sys_open(path, flags, mode, &fp);
if (error)
goto out_errno;
error = fdalloc(fp, &fd);
if (error)
goto out_fput;
fdrop(fp);
trace_vfs_open_ret(fd);
return fd;
out_fput:
fdrop(fp);
out_errno:
errno = error;
trace_vfs_open_err(error);
return -1;
}
LFS64(open);
static int vfs_fun_at(int dirfd, const char *pathname, std::function<int(const char *)> fun)
{
struct file *fp;
int error = fget(dirfd, &fp);
if (error) {
errno = error;
return -1;
}
struct vnode *vp = fp->f_dentry->d_vnode;
vn_lock(vp);
std::unique_ptr<char []> up (new char[PATH_MAX]);
char *p = up.get();
/* build absolute path */
strlcpy(p, fp->f_dentry->d_mount->m_path, PATH_MAX);
strlcat(p, fp->f_dentry->d_path, PATH_MAX);
if (pathname) {
strlcat(p, "/", PATH_MAX);
strlcat(p, pathname, PATH_MAX);
}
error = fun(p);
vn_unlock(vp);
fdrop(fp);
return error;
}
static int vfs_fun_at2(int dirfd, const char *pathname, std::function<int(const char *)> fun)
{
if (!pathname) {
errno = EINVAL;
return -1;
}
if (pathname[0] == '/' || dirfd == AT_FDCWD) {
return fun(pathname);
}
return vfs_fun_at(dirfd, pathname, fun);
}
OSV_LIBC_API
int openat(int dirfd, const char *pathname, int flags, ...)
{
mode_t mode = 0;
if (flags & O_CREAT) {
va_list ap;
va_start(ap, flags);
mode = apply_umask(va_arg(ap, mode_t));
va_end(ap);
}
return vfs_fun_at2(dirfd, pathname, [flags, mode](const char *path) {
return open(path, flags, mode);
});
}
LFS64(openat);
// open() has an optional third argument, "mode", which is only needed in
// some cases (when the O_CREAT mode is used). As a safety feature, recent
// versions of Glibc add a feature where open() with two arguments is replaced
// by a call to __open_2(), which verifies it isn't called with O_CREATE.
extern "C" OSV_LIBC_API
int __open_2(const char *pathname, int flags)
{
assert(!(flags & O_CREAT));
return open(pathname, flags, 0);
}
extern "C" OSV_LIBC_API
int __open64_2(const char *file, int flags)
{
if (flags & O_CREAT) {
errno = EINVAL;
return -1;
}
return open64(file, flags);
}
OSV_LIBC_API
int creat(const char *pathname, mode_t mode)
{
return open(pathname, O_CREAT|O_WRONLY|O_TRUNC, mode);
}
LFS64(creat);
TRACEPOINT(trace_vfs_close, "%d", int);
TRACEPOINT(trace_vfs_close_ret, "");
TRACEPOINT(trace_vfs_close_err, "%d", int);
OSV_LIBC_API
int close(int fd)
{
int error;
trace_vfs_close(fd);
error = fdclose(fd);
if (error)
goto out_errno;
trace_vfs_close_ret();
return 0;
out_errno:
trace_vfs_close_err(error);
errno = error;
return -1;
}
TRACEPOINT(trace_vfs_mknod, "\"%s\" 0%0o 0x%x", const char*, mode_t, dev_t);
TRACEPOINT(trace_vfs_mknod_ret, "");
TRACEPOINT(trace_vfs_mknod_err, "%d", int);
extern "C" OSV_LIBC_API
int __xmknod(int ver, const char *pathname, mode_t mode, dev_t *dev)
{
assert(ver == 0); // On x86-64 Linux, _MKNOD_VER_LINUX is 0.
struct task *t = main_task;
char path[PATH_MAX];
int error;
trace_vfs_mknod(pathname, mode, *dev);
if ((error = task_conv(t, pathname, VWRITE, path)) != 0)
goto out_errno;
error = sys_mknod(path, mode);
if (error)
goto out_errno;
trace_vfs_mknod_ret();
return 0;
out_errno:
trace_vfs_mknod_err(error);
errno = error;
return -1;
}
OSV_LIBC_API
int mknod(const char *pathname, mode_t mode, dev_t dev)
{
return __xmknod(0, pathname, mode, &dev);
}
TRACEPOINT(trace_vfs_lseek, "%d 0x%x %d", int, off_t, int);
TRACEPOINT(trace_vfs_lseek_ret, "0x%x", off_t);
TRACEPOINT(trace_vfs_lseek_err, "%d", int);
OSV_LIBC_API
off_t lseek(int fd, off_t offset, int whence)
{
struct file *fp;
off_t org;
int error;
trace_vfs_lseek(fd, offset, whence);
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_lseek(fp, offset, whence, &org);
fdrop(fp);
if (error)
goto out_errno;
trace_vfs_lseek_ret(org);
return org;
out_errno:
trace_vfs_lseek_err(error);
errno = error;
return -1;
}
LFS64(lseek);
TRACEPOINT(trace_vfs_pread, "%d %p 0x%x 0x%x", int, void*, size_t, off_t);
TRACEPOINT(trace_vfs_pread_ret, "0x%x", ssize_t);
TRACEPOINT(trace_vfs_pread_err, "%d", int);
// In BSD's internal implementation of read() and write() code, for example
// sosend_generic(), a partial read or write returns both an EWOULDBLOCK error
// *and* a non-zero number of written bytes. In that case, we need to zero the
// error, so the system call appear a successful partial read/write.
// In FreeBSD, dofilewrite() and dofileread() (sys_generic.c) do this too.
static inline bool has_error(int error, int bytes)
{
return error && (
(bytes == 0) ||
(error != EWOULDBLOCK && error != EINTR && error != ERESTART));
}
OSV_LIBC_API
ssize_t pread(int fd, void *buf, size_t count, off_t offset)
{
trace_vfs_pread(fd, buf, count, offset);
struct iovec iov = {
.iov_base = buf,
.iov_len = count,
};
struct file *fp;
size_t bytes;
int error;
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_read(fp, &iov, 1, offset, &bytes);
fdrop(fp);
if (has_error(error, bytes))
goto out_errno;
trace_vfs_pread_ret(bytes);
return bytes;
out_errno:
trace_vfs_pread_err(error);
errno = error;
return -1;
}
LFS64(pread);
OSV_LIBC_API
ssize_t read(int fd, void *buf, size_t count)
{
return pread(fd, buf, count, -1);
}
TRACEPOINT(trace_vfs_pwrite, "%d %p 0x%x 0x%x", int, const void*, size_t, off_t);
TRACEPOINT(trace_vfs_pwrite_ret, "0x%x", ssize_t);
TRACEPOINT(trace_vfs_pwrite_err, "%d", int);
OSV_LIBC_API
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
{
trace_vfs_pwrite(fd, buf, count, offset);
struct iovec iov = {
.iov_base = (void *)buf,
.iov_len = count,
};
struct file *fp;
size_t bytes;
int error;
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_write(fp, &iov, 1, offset, &bytes);
fdrop(fp);
if (has_error(error, bytes))
goto out_errno;
trace_vfs_pwrite_ret(bytes);
return bytes;
out_errno:
trace_vfs_pwrite_err(error);
errno = error;
return -1;
}
LFS64(pwrite);
OSV_LIBC_API
ssize_t write(int fd, const void *buf, size_t count)
{
return pwrite(fd, buf, count, -1);
}
OSV_LIBC_API
ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
struct file *fp;
size_t bytes;
int error;
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_read(fp, iov, iovcnt, offset, &bytes);
fdrop(fp);
if (has_error(error, bytes))
goto out_errno;
return bytes;
out_errno:
errno = error;
return -1;
}
OSV_LIBC_API
ssize_t readv(int fd, const struct iovec *iov, int iovcnt)
{
return preadv(fd, iov, iovcnt, -1);
}
TRACEPOINT(trace_vfs_pwritev, "%d %p 0x%x 0x%x", int, const struct iovec*, int, off_t);
TRACEPOINT(trace_vfs_pwritev_ret, "0x%x", ssize_t);
TRACEPOINT(trace_vfs_pwritev_err, "%d", int);
OSV_LIBC_API
ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
struct file *fp;
size_t bytes;
int error;
trace_vfs_pwritev(fd, iov, iovcnt, offset);
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_write(fp, iov, iovcnt, offset, &bytes);
fdrop(fp);
if (has_error(error, bytes))
goto out_errno;
trace_vfs_pwritev_ret(bytes);
return bytes;
out_errno:
trace_vfs_pwritev_err(error);
errno = error;
return -1;
}
OSV_LIBC_API
ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
{
return pwritev(fd, iov, iovcnt, -1);
}
TRACEPOINT(trace_vfs_ioctl, "%d 0x%x", int, unsigned long);
TRACEPOINT(trace_vfs_ioctl_ret, "");
TRACEPOINT(trace_vfs_ioctl_err, "%d", int);
OSV_LIBC_API
int ioctl(int fd, unsigned long int request, ...)
{
struct file *fp;
int error;
va_list ap;
void* arg;
trace_vfs_ioctl(fd, request);
/* glibc ABI provides a variadic prototype for ioctl so we need to agree
* with it, since we now include sys/ioctl.h
* read the first argument and pass it to sys_ioctl() */
va_start(ap, request);
arg = va_arg(ap, void*);
va_end(ap);
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_ioctl(fp, request, arg);
fdrop(fp);
if (error)
goto out_errno;
trace_vfs_ioctl_ret();
return 0;
out_errno:
trace_vfs_ioctl_err(error);
errno = error;
return -1;
}
TRACEPOINT(trace_vfs_fsync, "%d", int);
TRACEPOINT(trace_vfs_fsync_ret, "");
TRACEPOINT(trace_vfs_fsync_err, "%d", int);
OSV_LIBC_API
int fsync(int fd)
{
struct file *fp;
int error;
trace_vfs_fsync(fd);
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_fsync(fp);
fdrop(fp);
if (error)
goto out_errno;
trace_vfs_fsync_ret();
return 0;
out_errno:
trace_vfs_fsync_err(error);
errno = error;
return -1;
}
OSV_LIBC_API
int fdatasync(int fd)
{
// TODO: See if we can do less than fsync().
return fsync(fd);
}
TRACEPOINT(trace_vfs_fstat, "%d %p", int, struct stat*);
TRACEPOINT(trace_vfs_fstat_ret, "");
TRACEPOINT(trace_vfs_fstat_err, "%d", int);
extern "C" OSV_LIBC_API
int __fxstat(int ver, int fd, struct stat *st)
{
struct file *fp;
int error;
trace_vfs_fstat(fd, st);
error = fget(fd, &fp);
if (error)
goto out_errno;
error = sys_fstat(fp, st);
fdrop(fp);
if (error)
goto out_errno;
trace_vfs_fstat_ret();
return 0;
out_errno:
trace_vfs_fstat_err(error);
errno = error;
return -1;
}
LFS64(__fxstat);
extern "C" OSV_LIBC_API
int fstat(int fd, struct stat *st)
{
return __fxstat(1, fd, st);
}
extern "C" OSV_LIBC_API
int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *st,
int flags)
{
if (!pathname) {
errno = EINVAL;
return -1;
}
if (pathname[0] == '/' || dirfd == AT_FDCWD) {
return stat(pathname, st);
}
// If AT_EMPTY_PATH and pathname is an empty string, fstatat() operates on
// dirfd itself, and in that case it doesn't have to be a directory.
if ((flags & AT_EMPTY_PATH) && !pathname[0]) {
return fstat(dirfd, st);
}
return vfs_fun_at(dirfd, pathname, [flags,st](const char *absolute_path) {
if (flags & AT_SYMLINK_NOFOLLOW) {
return lstat(absolute_path, st);
}
else {
return stat(absolute_path, st);
}
});
}
LFS64(__fxstatat);
extern "C" OSV_LIBC_API
int fstatat(int dirfd, const char *path, struct stat *st, int flags)
{
return __fxstatat(1, dirfd, path, st, flags);
}
extern "C" OSV_LIBC_API
int flock(int fd, int operation)
{
if (!fileref_from_fd(fd)) {
return libc_error(EBADF);
}
switch (operation) {
case LOCK_SH:
case LOCK_SH | LOCK_NB:
case LOCK_EX:
case LOCK_EX | LOCK_NB:
case LOCK_UN:
break;
default:
return libc_error(EINVAL);
}
return 0;
}
TRACEPOINT(trace_vfs_readdir, "%d %p", int, dirent*);
TRACEPOINT(trace_vfs_readdir_ret, "");
TRACEPOINT(trace_vfs_readdir_err, "%d", int);
struct __dirstream
{
int fd;
};
OSV_LIBC_API
DIR *opendir(const char *path)
{
DIR *dir = new DIR;
if (!dir)
return libc_error_ptr<DIR>(ENOMEM);
dir->fd = open(path, O_RDONLY);
if (dir->fd < 0) {
delete dir;
return nullptr;
}
return dir;
}
OSV_LIBC_API
DIR *fdopendir(int fd)
{
DIR *dir;
struct stat st;
if (fstat(fd, &st) < 0) {
return nullptr;
}
if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return nullptr;
}
dir = new DIR;
dir->fd = fd;
return dir;
}
OSV_LIBC_API
int dirfd(DIR *dirp)
{
if (!dirp) {
return libc_error(EINVAL);
}
return dirp->fd;
}
OSV_LIBC_API
int closedir(DIR *dir)
{
close(dir->fd);
delete dir;
return 0;
}
OSV_LIBC_API
struct dirent *readdir(DIR *dir)
{
static __thread struct dirent entry, *result;
int ret;
ret = readdir_r(dir, &entry, &result);
if (ret)
return libc_error_ptr<struct dirent>(ret);
errno = 0;
return result;
}
OSV_LIBC_API
int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
{
int error;
struct file *fp;
trace_vfs_readdir(dir->fd, entry);
error = fget(dir->fd, &fp);
if (error) {
trace_vfs_readdir_err(error);
} else {
error = sys_readdir(fp, entry);
fdrop(fp);
if (error) {
trace_vfs_readdir_err(error);
} else {
trace_vfs_readdir_ret();
}
}
// Our dirent has (like Linux) a d_reclen field, but a constant size.
entry->d_reclen = sizeof(*entry);
if (error) {
*result = nullptr;
} else {
*result = entry;
}
return error == ENOENT ? 0 : error;
}
// FIXME: in 64bit dirent64 and dirent are identical, so it's safe to alias
#undef readdir64_r
extern "C" OSV_LIBC_API
int readdir64_r(DIR *dir, struct dirent64 *entry,
struct dirent64 **result)
__attribute__((alias("readdir_r")));
#undef readdir64
extern "C" OSV_LIBC_API
struct dirent *readdir64(DIR *dir) __attribute__((alias("readdir")));
struct linux_dirent64 {
u64 d_ino;
s64 d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[];
};
#undef getdents64
extern "C"
ssize_t sys_getdents64(int fd, void *dirp, size_t count)
{
auto *dir = fdopendir(fd);
if (dir) {
// We have verified that fd points to a valid directory
// but we do NOT need the DIR handle so just delete it
delete dir;
struct file *fp;
int error = fget(fd, &fp);
if (error) {
errno = error;
return -1;
}
size_t bytes_read = 0;
off_t last_off = -1;
errno = 0;
// Iterate over as many entries as there is space in the buffer
// by directly calling sys_readdir()
struct dirent entry;
while ((error = sys_readdir(fp, &entry)) == 0) {
auto rec_len = offsetof(linux_dirent64, d_name) + strlen(entry.d_name) + 1;
if (rec_len <= count) {
auto *ldirent = static_cast<linux_dirent64*>(dirp + bytes_read);
ldirent->d_ino = entry.d_ino;
ldirent->d_off = entry.d_off;
ldirent->d_type = entry.d_type;
strcpy(ldirent->d_name, entry.d_name);
ldirent->d_reclen = rec_len;
count -= rec_len;
bytes_read += rec_len;
last_off = entry.d_off;
} else {
if (last_off >= 0)
sys_seekdir(fp, last_off);
break;
}
}
fdrop(fp);
if (error && error != ENOENT) {
errno = error;
return -1;
} else {
errno = 0;
return bytes_read;
}
} else {
return -1;
}
}
OSV_LIBC_API
void rewinddir(DIR *dirp)
{
struct file *fp;
auto error = fget(dirp->fd, &fp);
if (error) {
// POSIX specifies that what rewinddir() does in the case of error
// is undefined...
return;
}
sys_rewinddir(fp);
// Again, error code from sys_rewinddir() is ignored.
fdrop(fp);
}
OSV_LIBC_API
long telldir(DIR *dirp)
{
struct file *fp;
int error = fget(dirp->fd, &fp);
if (error) {
return libc_error(error);
}
long loc;
error = sys_telldir(fp, &loc);
fdrop(fp);
if (error) {
return libc_error(error);
}
return loc;
}
OSV_LIBC_API
void seekdir(DIR *dirp, long loc)
{
struct file *fp;
int error = fget(dirp->fd, &fp);
if (error) {
// POSIX specifies seekdir() cannot return errors.
return;
}
sys_seekdir(fp, loc);
// Again, error code from sys_seekdir() is ignored.
fdrop(fp);
}
TRACEPOINT(trace_vfs_mkdir, "\"%s\" 0%0o", const char*, mode_t);
TRACEPOINT(trace_vfs_mkdir_ret, "");
TRACEPOINT(trace_vfs_mkdir_err, "%d", int);
OSV_LIBC_API
int
mkdir(const char *pathname, mode_t mode)
{
struct task *t = main_task;
char path[PATH_MAX];
int error;
mode = apply_umask(mode);
trace_vfs_mkdir(pathname, mode);
if ((error = task_conv(t, pathname, VWRITE, path)) != 0)
goto out_errno;
error = sys_mkdir(path, mode);
if (error)
goto out_errno;
trace_vfs_mkdir_ret();
return 0;
out_errno:
trace_vfs_mkdir_err(error);
errno = error;
return -1;
}
OSV_LIBC_API
int mkdirat(int dirfd, const char *pathname, mode_t mode)
{
mode = apply_umask(mode);
return vfs_fun_at2(dirfd, pathname, [mode](const char *path) {
return mkdir(path, mode);
});
}
TRACEPOINT(trace_vfs_rmdir, "\"%s\"", const char*);
TRACEPOINT(trace_vfs_rmdir_ret, "");
TRACEPOINT(trace_vfs_rmdir_err, "%d", int);
OSV_LIBC_API
int rmdir(const char *pathname)
{
struct task *t = main_task;
char path[PATH_MAX];
int error;
trace_vfs_rmdir(pathname);
error = ENOENT;
if (pathname == nullptr)
goto out_errno;
if ((error = task_conv(t, pathname, VWRITE, path)) != 0)
goto out_errno;
error = sys_rmdir(path);
if (error)
goto out_errno;
trace_vfs_rmdir_ret();
return 0;
out_errno:
trace_vfs_rmdir_err(error);
errno = error;
return -1;
}
static void
get_last_component(const char *path, char *dst)
{
int pos = strlen(path) - 1;
while (pos >= 0 && path[pos] == '/')
pos--;
int component_end = pos;
while (pos >= 0 && path[pos] != '/')
pos--;
int component_start = pos + 1;
int len = component_end - component_start + 1;
memcpy(dst, path + component_start, len);
dst[len] = 0;
}
static bool null_or_empty(const char *str)
{