-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrootfs.c
1375 lines (1109 loc) · 30.3 KB
/
grootfs.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) 2021 Alexander Larsson <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#define FUSE_USE_VERSION 26
#include "utils.h"
#include "grootfs.h"
#include <arpa/inet.h>
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <fuse.h>
#include <fuse_lowlevel.h>
#include <limits.h>
#include <signal.h>
#include <sys/eventfd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <sys/xattr.h>
typedef struct {
int basefd;
long max_uid;
long max_gid;
} GRootFS;
#define ST_MODE_PERM_MASK (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX)
#define GROOT_CUSTOM_XATTR_PREFIX "user.grootfs."
#define GROOT_DATA_XATTR "user.grootfs"
static GRootFS *
get_grootfs (void)
{
struct fuse_context *ctx = fuse_get_context ();
return (GRootFS * )ctx->private_data;
}
static inline const char *
ensure_relpath (const char *path)
{
path = path + strspn (path, "/");
if (*path == 0)
return ".";
return path;
}
static int
open_dirfd (const char *path)
{
GRootFS *fs = get_grootfs ();
path = ensure_relpath (path);
int dirfd = openat (fs->basefd, path, O_DIRECTORY | O_RDONLY, 0);
if (dirfd == -1)
return -errno;
return dirfd;
}
static int
open_parent_dirfd (const char *path,
char **out_basename)
{
int dirfd = -1;
autofree char *dirpath = xstrdup (ensure_relpath (path));
char *end = dirpath + strlen (dirpath);
const char *dir;
const char *base;
/* Remove any slashes at the end */
while (*end == '/' && end > dirpath)
{
*end = 0;
end--;
}
/* Find first slash (not at end), if any */
while (*end != '/' && end > dirpath)
end--;
if (end == dirpath)
{
base = dirpath;
dir = ".";
}
else
{
base = end + 1;
*end = 0;
dir = dirpath;
}
dirfd = open_dirfd (dir);
if (dirfd >= 0)
*out_basename = xstrdup (base);
return dirfd;
}
static char *
get_proc_fd_path (int dirfd,
const char *opt_file)
{
if (opt_file)
return xasprintf ("/proc/self/fd/%d/%s", dirfd, opt_file);
else
return xasprintf ("/proc/self/fd/%d", dirfd);
}
/* Computes the real file pemissions for a a faked file.
* In order to correctly do things like set permissions and
* execute/search the files we set everything to rw for the user,
* r-only for rest. For dirs we always set x, and mirror the user x
* bit for other files. */
static mode_t
get_real_mode (int is_dir,
int executable_default)
{
mode_t real_mode = S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
if (is_dir || executable_default)
real_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
return real_mode;
}
typedef enum {
GROOTFS_FLAGS_UID_SET = 1<<0,
GROOTFS_FLAGS_GID_SET = 1<<1,
GROOTFS_FLAGS_MODE_SET = 1<<2,
} GrootFSFlags;
typedef struct {
uint32_t flags;
uint32_t uid;
uint32_t gid;
uint32_t mode;
} GRootFSData;
static void
fake_data_htonl (const GRootFSData *data,
GRootFSData *data_dst)
{
data_dst->flags = htonl (data->flags);
data_dst->mode = htonl (data->mode);
data_dst->uid = htonl (data->uid);
data_dst->gid = htonl (data->gid);
}
static void
fake_data_ntohl (const GRootFSData *data,
GRootFSData *data_dst)
{
data_dst->flags = ntohl (data->flags);
data_dst->mode = ntohl (data->mode);
data_dst->uid = ntohl (data->uid);
data_dst->gid = ntohl (data->gid);
}
static void
apply_fake_data (struct stat *st_data,
const GRootFSData *data)
{
GRootFS *fs = get_grootfs ();
if (data->flags & GROOTFS_FLAGS_UID_SET)
st_data->st_uid = data->uid;
if (data->flags & GROOTFS_FLAGS_GID_SET)
st_data->st_gid = data->gid;
if (data->flags & GROOTFS_FLAGS_MODE_SET)
st_data->st_mode = (st_data->st_mode & (~ST_MODE_PERM_MASK)) | (data->mode & ST_MODE_PERM_MASK);
/* Don't expose nobody user or other weird things not useful in the namespace */
if (st_data->st_uid > fs->max_uid)
st_data->st_uid = 0;
if (st_data->st_gid > fs->max_gid)
st_data->st_gid = 0;
}
static int
get_fake_data (int dirfd,
const char *file,
int allow_noent,
GRootFSData *data)
{
autofree char *proc_file = get_proc_fd_path (dirfd, file);
ssize_t res;
res = lgetxattr (proc_file, "user.grootfs", data, sizeof (GRootFSData));
if (res == -1)
{
int errsv = errno;
/* Handle the case were there was no data set by returning all zeros */
if ((allow_noent && errsv == ENOENT) ||
errsv == ENODATA ||
errsv == ENOTSUP)
{
GRootFSData zero = {0};
*data = zero;
return 0;
}
if (errsv == ERANGE)
report ("Internal error: Wrong xattr size for file %s", file);
else
report ("Internal error: lgetxattr %s returned %s", file, strerror (errsv));
return -errsv;
}
if (res != sizeof (GRootFSData))
{
report ("Internal error: Wrong xattr size for file %s", file);
return -ERANGE;
}
fake_data_ntohl (data, data);
return 0;
}
static int
get_fake_dataf (int fd,
GRootFSData *data)
{
ssize_t res;
res = fgetxattr (fd, "user.grootfs", data, sizeof(GRootFSData));
if (res == -1)
{
int errsv = errno;
/* Handle the case were there was no data set by returning all zeros */
if (errsv == ENODATA ||
errsv == ENOTSUP)
{
GRootFSData zero = {0};
*data = zero;
return 0;
}
if (errsv == ERANGE)
report ("Internal error: Wrong xattr size for fd %d", fd);
else
report ("Internal error: fgetxattr %d returned %s", fd, strerror (errsv));
return -errsv;
}
if (res != sizeof (GRootFSData))
{
report ("Internal error: Wrong xattr size for fd %d", fd);
return -ERANGE;
}
fake_data_ntohl (data, data);
return 0;
}
static int
set_fake_data (int dirfd,
const char *file,
int ensure_exist,
const GRootFSData *data)
{
autofree char *proc_file = get_proc_fd_path (dirfd, file);
ssize_t res;
GRootFSData data2;
fake_data_htonl (data, &data2);
if (ensure_exist)
{
int fd = openat (dirfd, file, O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd == -1)
{
if (errno != EEXIST)
return -errno;
}
close (fd);
}
res = lsetxattr (proc_file, "user.grootfs", &data2, sizeof(GRootFSData), 0);
if (res == -1)
{
int errsv = errno;
report ("Internal error: lsetxattr %s returned %s", file, strerror (errsv));
return -errsv;
}
return 0;
}
static int
set_fake_dataf (int fd,
const GRootFSData *data)
{
ssize_t res;
GRootFSData data2;
fake_data_htonl (data, &data2);
res = fsetxattr (fd, "user.grootfs", &data2, sizeof(GRootFSData), 0);
if (res == -1)
{
int errsv = errno;
report ("Internal error: fsetxattr %d returned %s", fd, strerror (errsv));
return -errsv;
}
return 0;
}
typedef struct {
const char *path;
int fd;
int dirfd;
char *basename;
char *datafile;
bool exists;
struct stat st_data;
GRootFSData fake_data;
} GRootPathInfo;
#define GROOT_PATH_INFO_INIT { NULL, -1, -1 }
static void
groot_path_info_cleanup (GRootPathInfo *info)
{
if (info->dirfd != -1)
close (info->dirfd);
if (info->basename)
free (info->basename);
if (info->datafile)
free (info->datafile);
}
DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GRootPathInfo, groot_path_info_cleanup);
static int
_groot_path_info_init_base (GRootPathInfo *info, bool allow_noent)
{
info->exists = TRUE;
if (S_ISLNK (info->st_data.st_mode))
{
GRootFS *fs = get_grootfs ();
info->datafile = xasprintf (".groot.symlink.%lx_%lx",
info->st_data.st_dev, info->st_data.st_ino);
if (get_fake_data (fs->basefd, info->datafile, TRUE, &info->fake_data) != 0)
return -EIO;
}
else if (info->fd != -1)
{
if (get_fake_dataf (info->fd, &info->fake_data) != 0)
return -EIO;
}
else
{
if (get_fake_data (info->dirfd, info->basename, allow_noent, &info->fake_data) != 0)
return -EIO;
}
apply_fake_data (&info->st_data, &info->fake_data);
return 0;
}
static int
groot_path_info_init_path (GRootPathInfo *info, const char *path, bool allow_noent)
{
info->path = ensure_relpath (path);
info->dirfd = open_parent_dirfd (info->path, &info->basename);
if (info->dirfd < 0)
return -errno;
if (fstatat (info->dirfd, info->basename, &info->st_data, AT_SYMLINK_NOFOLLOW) == -1)
{
if (allow_noent)
return 0;
return -errno;
}
return _groot_path_info_init_base (info, allow_noent);
}
static int
groot_path_info_init_fd (GRootPathInfo *info, int fd)
{
info->fd = fd;
if (fstat (info->fd, &info->st_data) == -1)
return -errno;
return _groot_path_info_init_base (info, FALSE);
}
static int
groot_path_info_update_data (GRootPathInfo *info)
{
assert (info->exists); // TODO: Later handle non-exist for e.g. symlinks
if (info->datafile) /* A symlink with separate data file, could be path *or* fd backed */
{
GRootFS *fs = get_grootfs ();
if (set_fake_data (fs->basefd, info->datafile, TRUE, &info->fake_data) != 0)
return -EIO;
}
else if (info->fd != -1)
{
if (set_fake_dataf (info->fd, &info->fake_data) != 0)
return -EIO;
}
else
{
if (set_fake_data (info->dirfd, info->basename, FALSE, &info->fake_data) != 0)
return -EIO;
}
return 0;
}
static int
grootfs_getattr (const char *path, struct stat *st_data)
{
__debug__ (("getattr %s", path));
int res;
auto(GRootPathInfo) info = GROOT_PATH_INFO_INIT;
res = groot_path_info_init_path (&info, path, FALSE);
if (res != 0)
return res;
*st_data = info.st_data;
return 0;
}
static int
grootfs_fgetattr (const char *path, struct stat *st_data, struct fuse_file_info *fi)
{
__debug__ (("fgetattr %s", path));
int res;
auto(GRootPathInfo) info = GROOT_PATH_INFO_INIT;
res = groot_path_info_init_fd (&info, fi->fh);
if (res != 0)
return res;
*st_data = info.st_data;
return 0;
}
static int
grootfs_chmod (const char *path, mode_t mode)
{
__debug__ (("chmod %s %x", path, mode));
int res;
auto(GRootPathInfo) info = GROOT_PATH_INFO_INIT;
res = groot_path_info_init_path (&info, path, FALSE);
if (res != 0)
return res;
mode_t real_mode = get_real_mode (S_ISDIR (info.st_data.st_mode), (mode & S_IXUSR) != 0);
/* For permissions like execute to work for others, we set all the
permissions, to the users perms and strip out any extra perms. */
/* Note we can't use AT_SYMLINK_NOFOLLOW yet;
* https://marc.info/?l=linux-kernel&m=148830147803162&w=2
* https://marc.info/?l=linux-fsdevel&m=149193779929561&w=2
* Fuse always resolves the symlink and calls us on the target
*/
if (fchmodat (info.dirfd, info.basename, real_mode, 0) != 0)
return -errno;
info.fake_data.mode = mode & ST_MODE_PERM_MASK;
info.fake_data.flags |= GROOTFS_FLAGS_MODE_SET;
res = groot_path_info_update_data (&info);
if (res != 0)
return res;
return 0;
}
static int
grootfs_chown (const char *path, uid_t uid, gid_t gid)
{
__debug__ (("chown %s to %d %d", path, uid, gid));
int res;
auto(GRootPathInfo) info = GROOT_PATH_INFO_INIT;
res = groot_path_info_init_path (&info, path, FALSE);
if (res != 0)
return res;
if (uid != -1)
{
info.fake_data.uid = uid;
info.fake_data.flags |= GROOTFS_FLAGS_UID_SET;
}
if (gid != -1)
{
info.fake_data.gid = gid;
info.fake_data.flags |= GROOTFS_FLAGS_GID_SET;
}
res = groot_path_info_update_data (&info);
if (res != 0)
return res;
return 0;
}
static int
grootfs_readlink (const char *path, char *buf, size_t size)
{
GRootFS *fs = get_grootfs ();
int r;
__debug__ (("readlink %s", path));
path = ensure_relpath (path);
/* Note FUSE wants the string to be always nul-terminated, even if
* truncated.
*/
r = readlinkat (fs->basefd, path, buf, size - 1);
if (r == -1)
return -errno;
buf[r] = '\0';
return 0;
}
static int
grootfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
GRootFS *fs = get_grootfs ();
DIR *dp;
struct dirent *de;
int dfd;
__debug__ (("readdir %s", path));
path = ensure_relpath (path);
if (!*path)
{
dfd = fcntl (fs->basefd, F_DUPFD_CLOEXEC, 3);
if (dfd < 0)
return -errno;
lseek (dfd, 0, SEEK_SET);
}
else
{
dfd = openat (fs->basefd, path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY);
if (dfd == -1)
return -errno;
}
/* Transfers ownership of fd */
dp = fdopendir (dfd);
if (dp == NULL)
return -errno;
while ((de = readdir (dp)) != NULL)
{
struct stat st;
if (has_prefix (de->d_name, ".groot."))
continue;
memset (&st, 0, sizeof (st));
st.st_ino = de->d_ino;
// TODO: Ensure right mode if fake devnode/socket
st.st_mode = de->d_type << 12;
if (filler (buf, de->d_name, &st, 0))
break;
}
(void) closedir (dp);
return 0;
}
static int
grootfs_mknod (const char *path, mode_t mode, dev_t rdev)
{
__debug__ (("mknod %s %ld %ld", path, (long)mode, (long)rdev));
// TODO: Implement
return -EROFS;
}
static int
grootfs_mkdir (const char *path, mode_t mode)
{
__debug__ (("mkdir %s %x", path, mode));
int res;
autofree char *basename = NULL;
autofd int dirfd = open_parent_dirfd (path, &basename);
if (dirfd < 0)
return dirfd;
mode_t real_mode = get_real_mode (TRUE, FALSE);
if (mkdirat (dirfd, basename, real_mode) == -1)
return -errno;
/* mkdir succeeded, so its guaranteed to be a not-previously
existing dir, just set the fake data */
GRootFSData data = { 0 };
struct fuse_context *ctx = fuse_get_context ();
data.mode = mode & ST_MODE_PERM_MASK;
data.uid = ctx->uid;
data.gid = ctx->gid;
data.flags = GROOTFS_FLAGS_MODE_SET | GROOTFS_FLAGS_UID_SET | GROOTFS_FLAGS_GID_SET;
res = set_fake_data (dirfd, basename, FALSE, &data);
if (res != 0)
return res;
return 0;
}
static int
grootfs_unlink (const char *path)
{
__debug__ (("unlink %s", path));
int res;
auto(GRootPathInfo) info = GROOT_PATH_INFO_INIT;
res = groot_path_info_init_path (&info, path, FALSE);
if (res != 0)
return res;
if (unlinkat (info.dirfd, info.basename, 0) == -1)
return -errno;
/* When unlinking a symlink, also unlink symlink datafile.
* This should be fine because we can't hardlink symlinks, so its the last reference. */
if (info.datafile)
{
GRootFS *fs = get_grootfs ();
unlinkat (fs->basefd, info.datafile, 0);
}
return 0;
}
static int
grootfs_rmdir (const char *path)
{
GRootFS *fs = get_grootfs ();
__debug__ (("rmdir %s", path));
path = ensure_relpath (path);
if (unlinkat (fs->basefd, path, AT_REMOVEDIR) == -1)
return -errno;
return 0;
}
static int
grootfs_symlink (const char *from, const char *to)
{
GRootFS *fs = get_grootfs ();
__debug__ (("symlink %s %s", from, to));
to = ensure_relpath (to);
if (symlinkat (from, fs->basefd, to) == -1)
return -errno;
/* We created a new symlink file, set default ownership */
auto(GRootPathInfo) info = GROOT_PATH_INFO_INIT;
if (groot_path_info_init_path (&info, to, FALSE) == 0)
{
struct fuse_context *ctx = fuse_get_context ();
info.fake_data.uid = ctx->uid;
info.fake_data.gid = ctx->gid;
info.fake_data.flags = GROOTFS_FLAGS_UID_SET | GROOTFS_FLAGS_GID_SET;
groot_path_info_update_data (&info);
}
return 0;
}
static int
grootfs_rename (const char *from, const char *to)
{
GRootFS *fs = get_grootfs ();
__debug__ (("rename %s %s", from, to));
from = ensure_relpath (from);
to = ensure_relpath (to);
if (renameat (fs->basefd, from, fs->basefd, to) == -1)
return -errno;
return 0;
}
static int
grootfs_link (const char *from, const char *to)
{
GRootFS *fs = get_grootfs ();
__debug__ (("link %s %s", from, to));
from = ensure_relpath (from);
to = ensure_relpath (to);
if (linkat (fs->basefd, from, fs->basefd, to, 0) == -1)
return -errno;
return 0;
}
static int
grootfs_truncate (const char *path, off_t size)
{
GRootFS *fs = get_grootfs ();
__debug__ (("truncate %s", path));
path = ensure_relpath (path);
autofd int fd = openat (fs->basefd, path, O_NOFOLLOW|O_WRONLY);
if (fd == -1)
return -errno;
if (ftruncate (fd, size) == -1)
return -errno;
return 0;
}
static int
grootfs_ftruncate (const char *path, off_t size, struct fuse_file_info *fi)
{
__debug__ (("ftruncate %s", path));
if (ftruncate (fi->fh, size) == -1)
return -errno;
return 0;
}
static int
grootfs_utimens (const char *path, const struct timespec tv[2])
{
GRootFS *fs = get_grootfs ();
__debug__ (("utimens %s", path));
path = ensure_relpath (path);
if (utimensat (fs->basefd, path, tv, AT_SYMLINK_NOFOLLOW) == -1)
return -errno;
return 0;
}
static int
do_open (const char *path, mode_t mode, struct fuse_file_info *finfo)
{
GRootFS *fs = get_grootfs ();
int fd;
mode_t real_mode;
int o_creat = (O_CREAT & finfo->flags) != 0;
int o_excl = (O_EXCL & finfo->flags) != 0;
int created_file = o_creat;
int flags;
// TODO: Rewrite path for fake devnodes, etc
path = ensure_relpath (path);
real_mode = get_real_mode (FALSE, (mode & S_IXUSR) != 0);
flags = finfo->flags;
if (o_creat && !o_excl)
{
flags |= O_EXCL; /* We really need to know if the file was created or not, so we try EXCL first */
}
fd = openat (fs->basefd, path, flags, real_mode);
if (fd == -1 && o_creat && !o_excl && errno == EEXIST)
{
created_file = FALSE; /* We know the file existed */
/* We faked the o_excl, and it exists, retry again witout forced o_excl */
fd = openat (fs->basefd, path, finfo->flags, real_mode);
}
if (fd == -1)
return -errno;
if (created_file)
{
GRootFSData data = { 0 };
int res;
struct fuse_context *ctx = fuse_get_context ();
data.mode = mode & ST_MODE_PERM_MASK;
data.uid = ctx->uid;
data.gid = ctx->gid;
data.flags = GROOTFS_FLAGS_MODE_SET | GROOTFS_FLAGS_UID_SET | GROOTFS_FLAGS_GID_SET;
res = set_fake_dataf (fd, &data);
if (res != 0)
{
close (fd);
return res;
}
}
finfo->fh = fd;
return 0;
}
static int
grootfs_open (const char *path, struct fuse_file_info *finfo)
{
__debug__ (("open %s", path));
return do_open (path, 0, finfo);
}
static int
grootfs_create(const char *path, mode_t mode, struct fuse_file_info *finfo)
{
__debug__ (("create %s", path));
return do_open (path, mode, finfo);
}
static int
grootfs_read (const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *finfo)
{
int r;
r = pread (finfo->fh, buf, size, offset);
if (r == -1)
return -errno;
return r;
}
static int
grootfs_write (const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *finfo)
{
int r;
r = pwrite (finfo->fh, buf, size, offset);
if (r == -1)
return -errno;
return r;
}
static int
grootfs_statfs (const char *path, struct statvfs *st_buf)
{
GRootFS *fs = get_grootfs ();
if (fstatvfs (fs->basefd, st_buf) == -1)
return -errno;
return 0;
}
static int
grootfs_release (const char *path, struct fuse_file_info *finfo)
{
(void) close (finfo->fh);
return 0;
}
static int
grootfs_fsync (const char *path, int crap, struct fuse_file_info *finfo)
{
if (fsync (finfo->fh) == -1)
return -errno;
return 0;
}
static int
grootfs_access (const char *path, int mode)
{
GRootFS *fs = get_grootfs ();
__debug__ (("access %s", path));
path = ensure_relpath (path);
// TODO: Rewrite path for fake devnodes, etc
/* Apparently at least GNU coreutils rm calls `faccessat(W_OK)`
* before trying to do an unlink. So...we'll just lie about
* writable access here.
*/
if (faccessat (fs->basefd, path, mode, AT_SYMLINK_NOFOLLOW) == -1)
return -errno;
return 0;
}
static int
grootfs_setxattr (const char *path, const char *name, const char *value,
size_t size, int flags)
{
__debug__ (("setxattr %s %s", path, name));
autofree char *basename = NULL;
autofd int dirfd = open_parent_dirfd (path, &basename);
if (dirfd < 0)
return dirfd;
autofree char *proc_file = get_proc_fd_path (dirfd, basename);
autofree char *fake_name = xasprintf (GROOT_CUSTOM_XATTR_PREFIX"%s", name);
if (lsetxattr (proc_file, fake_name, value, size, flags) != 0)
return -errno;
return 0;
}
static int
grootfs_getxattr (const char *path, const char *name, char *value,
size_t size)
{
__debug__ (("getxattr %s %s", path, name));
autofree char *basename = NULL;
autofd int dirfd = open_parent_dirfd (path, &basename);
ssize_t res;
if (dirfd < 0)
return dirfd;
autofree char *proc_file = get_proc_fd_path (dirfd, basename);
autofree char *fake_name = xasprintf (GROOT_CUSTOM_XATTR_PREFIX"%s", name);
res = lgetxattr (proc_file, fake_name, value, size);
if (res == -1)
return -errno;
return res;
}
/*
* List the supported extended attributes.
*/
static int
grootfs_listxattr (const char *path, char *list, size_t size)
{
__debug__ (("listxattr %s", path));
autofree char *basename = NULL;
autofd int dirfd = open_parent_dirfd (path, &basename);
if (dirfd < 0)
return dirfd;
autofree char *proc_file = get_proc_fd_path (dirfd, basename);
char buf_data[4096];
autofree char *buf_free = NULL;
char *buf = buf_data;
size_t buf_size = sizeof(buf_data);