This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
shim_handle.c
914 lines (722 loc) · 24.6 KB
/
shim_handle.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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University */
/*
* This file contains code to maintain bookkeeping for handles in library OS.
*/
#include "pal.h"
#include "pal_error.h"
#include "shim_checkpoint.h"
#include "shim_fs.h"
#include "shim_fs_lock.h"
#include "shim_handle.h"
#include "shim_internal.h"
#include "shim_lock.h"
#include "shim_thread.h"
#include "stat.h"
static struct shim_lock handle_mgr_lock;
#define HANDLE_MGR_ALLOC 32
#define SYSTEM_LOCK() lock(&handle_mgr_lock)
#define SYSTEM_UNLOCK() unlock(&handle_mgr_lock)
#define SYSTEM_LOCKED() locked(&handle_mgr_lock)
#define OBJ_TYPE struct shim_handle
#include "memmgr.h"
static MEM_MGR handle_mgr = NULL;
#define INIT_HANDLE_MAP_SIZE 32
//#define DEBUG_REF
static int init_tty_handle(struct shim_handle* hdl, bool write) {
int ret;
hdl->type = TYPE_DEV;
hdl->flags = write ? (O_WRONLY | O_APPEND) : O_RDONLY;
struct shim_dentry* dent = NULL;
if ((ret = path_lookupat(/*start=*/NULL, "/dev/tty", LOOKUP_FOLLOW, &dent)) < 0)
return ret;
ret = dent->fs->d_ops->open(hdl, dent, hdl->flags);
if (ret < 0)
return ret;
hdl->fs = dent->fs;
hdl->dentry = dent;
return 0;
}
int open_executable(struct shim_handle* hdl, const char* path) {
struct shim_dentry* dent = NULL;
int ret = path_lookupat(/*start=*/NULL, path, LOOKUP_FOLLOW, &dent);
if (ret < 0) {
goto out;
}
if (dent->type != S_IFREG) {
ret = -EACCES;
goto out;
}
if (!(dent->perm & S_IXUSR)) {
ret = -EACCES;
goto out;
}
ret = dentry_open(hdl, dent, O_RDONLY);
if (ret < 0) {
goto out;
}
ret = 0;
out:
if (dent)
put_dentry(dent);
return ret;
}
static int init_exec_handle(void) {
lock(&g_process.fs_lock);
if (g_process.exec) {
/* `g_process.exec` handle is already initialized if we did execve. See
* `shim_do_execve_rtld`. */
unlock(&g_process.fs_lock);
return 0;
}
unlock(&g_process.fs_lock);
/* Initialize `g_process.exec` based on `libos.entrypoint` manifest key. */
char* entrypoint = NULL;
const char* exec_path;
struct shim_handle* hdl = NULL;
int ret;
/* Initialize `g_process.exec` based on `libos.entrypoint` manifest key. */
assert(g_manifest_root);
ret = toml_string_in(g_manifest_root, "libos.entrypoint", &entrypoint);
if (ret < 0) {
log_error("Cannot parse 'libos.entrypoint'\n");
ret = -EINVAL;
goto out;
}
if (!entrypoint) {
log_error("'libos.entrypoint' must be specified in the manifest\n");
ret = -EINVAL;
goto out;
}
exec_path = entrypoint;
if (strstartswith(exec_path, URI_PREFIX_FILE)) {
/* TODO: change to error after some deprecation period */
log_error("'libos.entrypoint' is now a Graphene path, not URI. "
"Ignoring the 'file:' prefix.\n");
exec_path += strlen(URI_PREFIX_FILE);
}
hdl = get_new_handle();
if (!hdl) {
ret = -ENOMEM;
goto out;
}
ret = open_executable(hdl, exec_path);
if (ret < 0) {
log_error("init_exec_handle: error opening executable: %d\n", ret);
goto out;
}
lock(&g_process.fs_lock);
g_process.exec = hdl;
get_handle(hdl);
unlock(&g_process.fs_lock);
ret = 0;
out:
free(entrypoint);
if (hdl)
put_handle(hdl);
return ret;
}
static struct shim_handle_map* get_new_handle_map(FDTYPE size);
static int __init_handle(struct shim_fd_handle** fdhdl, FDTYPE fd, struct shim_handle* hdl,
int fd_flags);
static int __enlarge_handle_map(struct shim_handle_map* map, size_t size);
int init_handle(void) {
if (!create_lock(&handle_mgr_lock)) {
return -ENOMEM;
}
handle_mgr = create_mem_mgr(init_align_up(HANDLE_MGR_ALLOC));
if (!handle_mgr) {
return -ENOMEM;
}
return 0;
}
int init_important_handles(void) {
int ret;
struct shim_thread* thread = get_cur_thread();
if (thread->handle_map)
goto done;
struct shim_handle_map* handle_map = get_thread_handle_map(thread);
if (!handle_map) {
handle_map = get_new_handle_map(INIT_HANDLE_MAP_SIZE);
if (!handle_map)
return -ENOMEM;
set_handle_map(thread, handle_map);
put_handle_map(handle_map);
}
/* `handle_map` is set in current thread, no need to increase ref-count. */
lock(&handle_map->lock);
if (handle_map->fd_size < 3) {
ret = __enlarge_handle_map(handle_map, INIT_HANDLE_MAP_SIZE);
if (ret < 0) {
unlock(&handle_map->lock);
return ret;
}
}
/* initialize stdin */
if (!HANDLE_ALLOCATED(handle_map->map[0])) {
struct shim_handle* stdin_hdl = get_new_handle();
if (!stdin_hdl) {
unlock(&handle_map->lock);
return -ENOMEM;
}
if ((ret = init_tty_handle(stdin_hdl, /*write=*/false)) < 0) {
unlock(&handle_map->lock);
put_handle(stdin_hdl);
return ret;
}
__init_handle(&handle_map->map[0], /*fd=*/0, stdin_hdl, /*flags=*/0);
put_handle(stdin_hdl);
}
/* initialize stdout */
if (!HANDLE_ALLOCATED(handle_map->map[1])) {
struct shim_handle* stdout_hdl = get_new_handle();
if (!stdout_hdl) {
unlock(&handle_map->lock);
return -ENOMEM;
}
if ((ret = init_tty_handle(stdout_hdl, /*write=*/true)) < 0) {
unlock(&handle_map->lock);
put_handle(stdout_hdl);
return ret;
}
__init_handle(&handle_map->map[1], /*fd=*/1, stdout_hdl, /*flags=*/0);
put_handle(stdout_hdl);
}
/* initialize stderr as duplicate of stdout */
if (!HANDLE_ALLOCATED(handle_map->map[2])) {
struct shim_handle* stdout_hdl = handle_map->map[1]->handle;
__init_handle(&handle_map->map[2], /*fd=*/2, stdout_hdl, /*flags=*/0);
}
if (handle_map->fd_top == FD_NULL || handle_map->fd_top < 2)
handle_map->fd_top = 2;
unlock(&handle_map->lock);
done:
return init_exec_handle();
}
struct shim_handle* __get_fd_handle(FDTYPE fd, int* fd_flags, struct shim_handle_map* map) {
assert(locked(&map->lock));
struct shim_fd_handle* fd_handle = NULL;
if (map->fd_top != FD_NULL && fd <= map->fd_top) {
fd_handle = map->map[fd];
if (!HANDLE_ALLOCATED(fd_handle))
return NULL;
if (fd_flags)
*fd_flags = fd_handle->flags;
return fd_handle->handle;
}
return NULL;
}
struct shim_handle* get_fd_handle(FDTYPE fd, int* fd_flags, struct shim_handle_map* map) {
if (!map)
map = get_thread_handle_map(NULL);
struct shim_handle* hdl = NULL;
lock(&map->lock);
if ((hdl = __get_fd_handle(fd, fd_flags, map)))
get_handle(hdl);
unlock(&map->lock);
return hdl;
}
struct shim_handle* __detach_fd_handle(struct shim_fd_handle* fd, int* flags,
struct shim_handle_map* map) {
assert(locked(&map->lock));
struct shim_handle* handle = NULL;
if (HANDLE_ALLOCATED(fd)) {
int vfd = fd->vfd;
handle = fd->handle;
if (flags)
*flags = fd->flags;
fd->vfd = FD_NULL;
fd->handle = NULL;
fd->flags = 0;
if (vfd == map->fd_top)
do {
map->fd_top = vfd ? vfd - 1 : FD_NULL;
vfd--;
} while (vfd >= 0 && !HANDLE_ALLOCATED(map->map[vfd]));
}
return handle;
}
struct shim_handle* detach_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* handle_map) {
struct shim_handle* handle = NULL;
if (!handle_map && !(handle_map = get_thread_handle_map(NULL)))
return NULL;
lock(&handle_map->lock);
if (fd < handle_map->fd_size)
handle = __detach_fd_handle(handle_map->map[fd], flags, handle_map);
unlock(&handle_map->lock);
if (handle && handle->dentry) {
/* Clear POSIX locks for a file. We are required to do that every time a FD is closed
* closed, even if the process holds other handles for that file, or duplicated FDs for the
* same handle. */
struct posix_lock pl = {
.type = F_UNLCK,
.start = 0,
.end = FS_LOCK_EOF,
.pid = g_process.pid,
};
int ret = posix_lock_set(handle->dentry, &pl, /*block=*/false);
if (ret < 0)
log_warning("error releasing locks: %d\n", ret);
}
return handle;
}
struct shim_handle* get_new_handle(void) {
struct shim_handle* new_handle =
get_mem_obj_from_mgr_enlarge(handle_mgr, size_align_up(HANDLE_MGR_ALLOC));
if (!new_handle)
return NULL;
memset(new_handle, 0, sizeof(struct shim_handle));
REF_SET(new_handle->ref_count, 1);
if (!create_lock(&new_handle->lock)) {
free_mem_obj_to_mgr(handle_mgr, new_handle);
return NULL;
}
INIT_LISTP(&new_handle->epolls);
return new_handle;
}
static int __init_handle(struct shim_fd_handle** fdhdl, FDTYPE fd, struct shim_handle* hdl,
int fd_flags) {
struct shim_fd_handle* new_handle = *fdhdl;
assert((fd_flags & ~FD_CLOEXEC) == 0); // The only supported flag right now
if (!new_handle) {
new_handle = malloc(sizeof(struct shim_fd_handle));
if (!new_handle)
return -ENOMEM;
*fdhdl = new_handle;
}
new_handle->vfd = fd;
new_handle->flags = fd_flags;
get_handle(hdl);
new_handle->handle = hdl;
return 0;
}
/*
* Helper function for set_new_fd_handle*(). If find_free is true, tries to find the first free fd
* (starting from the provided one), otherwise, tries to use fd as-is.
*/
static int __set_new_fd_handle(FDTYPE fd, struct shim_handle* hdl, int fd_flags,
struct shim_handle_map* handle_map, bool find_free) {
int ret;
if (!handle_map && !(handle_map = get_thread_handle_map(NULL)))
return -EBADF;
lock(&handle_map->lock);
if (handle_map->fd_top != FD_NULL) {
assert(handle_map->map);
if (find_free) {
// find first free fd
while (fd <= handle_map->fd_top && HANDLE_ALLOCATED(handle_map->map[fd])) {
fd++;
}
} else {
// check if requested fd is occupied
if (fd <= handle_map->fd_top && HANDLE_ALLOCATED(handle_map->map[fd])) {
ret = -EBADF;
goto out;
}
}
}
if (fd >= get_rlimit_cur(RLIMIT_NOFILE)) {
ret = -EMFILE;
goto out;
}
// Enlarge handle_map->map (or allocate if necessary)
if (fd >= handle_map->fd_size) {
size_t new_size = handle_map->fd_size;
if (new_size == 0)
new_size = INIT_HANDLE_MAP_SIZE;
while (new_size <= fd)
new_size *= 2;
ret = __enlarge_handle_map(handle_map, new_size);
if (ret < 0)
goto out;
}
assert(handle_map->map);
assert(fd < handle_map->fd_size);
ret = __init_handle(&handle_map->map[fd], fd, hdl, fd_flags);
if (ret < 0)
goto out;
if (handle_map->fd_top == FD_NULL || fd > handle_map->fd_top)
handle_map->fd_top = fd;
ret = fd;
out:
unlock(&handle_map->lock);
return ret;
}
int set_new_fd_handle(struct shim_handle* hdl, int fd_flags, struct shim_handle_map* handle_map) {
return __set_new_fd_handle(0, hdl, fd_flags, handle_map, /*find_first=*/true);
}
int set_new_fd_handle_by_fd(FDTYPE fd, struct shim_handle* hdl, int fd_flags,
struct shim_handle_map* handle_map) {
return __set_new_fd_handle(fd, hdl, fd_flags, handle_map, /*find_first=*/false);
}
int set_new_fd_handle_above_fd(FDTYPE fd, struct shim_handle* hdl, int fd_flags,
struct shim_handle_map* handle_map) {
return __set_new_fd_handle(fd, hdl, fd_flags, handle_map, /*find_first=*/true);
}
static inline __attribute__((unused)) const char* __handle_name(struct shim_handle* hdl) {
if (!qstrempty(&hdl->uri))
return qstrgetstr(&hdl->uri);
if (hdl->dentry && !qstrempty(&hdl->dentry->name))
return qstrgetstr(&hdl->dentry->name);
if (hdl->fs)
return hdl->fs->name;
return "(unknown)";
}
void get_handle(struct shim_handle* hdl) {
#ifdef DEBUG_REF
int ref_count = REF_INC(hdl->ref_count);
log_debug("get handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl), ref_count);
#else
REF_INC(hdl->ref_count);
#endif
}
static void destroy_handle(struct shim_handle* hdl) {
destroy_lock(&hdl->lock);
free_mem_obj_to_mgr(handle_mgr, hdl);
}
void put_handle(struct shim_handle* hdl) {
int ref_count = REF_DEC(hdl->ref_count);
#ifdef DEBUG_REF
log_debug("put handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl), ref_count);
#endif
if (!ref_count) {
delete_from_epoll_handles(hdl);
if (hdl->is_dir) {
clear_directory_handle(hdl);
}
if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->close)
hdl->fs->fs_ops->close(hdl);
if (hdl->type == TYPE_SOCK && hdl->info.sock.peek_buffer) {
free(hdl->info.sock.peek_buffer);
hdl->info.sock.peek_buffer = NULL;
}
if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->hput)
hdl->fs->fs_ops->hput(hdl);
qstrfree(&hdl->uri);
if (hdl->pal_handle) {
#ifdef DEBUG_REF
log_debug("handle %p closes PAL handle %p\n", hdl, hdl->pal_handle);
#endif
DkObjectClose(hdl->pal_handle); // TODO: handle errors
hdl->pal_handle = NULL;
}
if (hdl->dentry)
put_dentry(hdl->dentry);
destroy_handle(hdl);
}
}
int get_file_size(struct shim_handle* hdl, uint64_t* size) {
if (!hdl->fs || !hdl->fs->fs_ops)
return -EINVAL;
if (hdl->fs->fs_ops->poll) {
off_t x = hdl->fs->fs_ops->poll(hdl, FS_POLL_SZ);
if (x < 0) {
return -EINVAL;
}
*size = (uint64_t)x;
return 0;
}
if (hdl->fs->fs_ops->hstat) {
struct stat stat;
int ret = hdl->fs->fs_ops->hstat(hdl, &stat);
if (ret < 0) {
return ret;
}
if (stat.st_size < 0) {
return -EINVAL;
}
*size = (uint64_t)stat.st_size;
return 0;
}
*size = 0;
return 0;
}
static struct shim_handle_map* get_new_handle_map(FDTYPE size) {
struct shim_handle_map* handle_map = calloc(1, sizeof(struct shim_handle_map));
if (!handle_map)
return NULL;
handle_map->map = calloc(size, sizeof(*handle_map->map));
if (!handle_map->map) {
free(handle_map);
return NULL;
}
handle_map->fd_top = FD_NULL;
handle_map->fd_size = size;
if (!create_lock(&handle_map->lock)) {
free(handle_map->map);
free(handle_map);
return NULL;
}
REF_SET(handle_map->ref_count, 1);
return handle_map;
}
static int __enlarge_handle_map(struct shim_handle_map* map, size_t size) {
assert(locked(&map->lock));
if (size <= map->fd_size)
return 0;
struct shim_fd_handle** new_map = calloc(size, sizeof(new_map[0]));
if (!new_map)
return -ENOMEM;
memcpy(new_map, map->map, map->fd_size * sizeof(new_map[0]));
free(map->map);
map->map = new_map;
map->fd_size = size;
return 0;
}
int dup_handle_map(struct shim_handle_map** new, struct shim_handle_map* old_map) {
lock(&old_map->lock);
/* allocate a new handle mapping with the same size as
the old one */
struct shim_handle_map* new_map = get_new_handle_map(old_map->fd_size);
if (!new_map)
return -ENOMEM;
new_map->fd_top = old_map->fd_top;
if (old_map->fd_top == FD_NULL)
goto done;
for (int i = 0; i <= old_map->fd_top; i++) {
struct shim_fd_handle* fd_old = old_map->map[i];
struct shim_fd_handle* fd_new;
/* now we go through the handle map and reassign each
of them being allocated */
if (HANDLE_ALLOCATED(fd_old)) {
/* first, get the handle to prevent it from being deleted */
struct shim_handle* hdl = fd_old->handle;
get_handle(hdl);
fd_new = malloc(sizeof(struct shim_fd_handle));
if (!fd_new) {
for (int j = 0; j < i; j++) {
put_handle(new_map->map[j]->handle);
free(new_map->map[j]);
}
unlock(&old_map->lock);
*new = NULL;
free(new_map);
return -ENOMEM;
}
/* DP: I assume we really need a deep copy of the handle map? */
new_map->map[i] = fd_new;
fd_new->vfd = fd_old->vfd;
fd_new->handle = hdl;
fd_new->flags = fd_old->flags;
}
}
done:
unlock(&old_map->lock);
*new = new_map;
return 0;
}
void get_handle_map(struct shim_handle_map* map) {
REF_INC(map->ref_count);
}
void put_handle_map(struct shim_handle_map* map) {
int ref_count = REF_DEC(map->ref_count);
if (!ref_count) {
if (map->fd_top == FD_NULL)
goto done;
for (int i = 0; i <= map->fd_top; i++) {
if (!map->map[i])
continue;
if (map->map[i]->vfd != FD_NULL) {
struct shim_handle* handle = map->map[i]->handle;
if (handle)
put_handle(handle);
}
free(map->map[i]);
}
done:
destroy_lock(&map->lock);
free(map->map);
free(map);
}
}
int walk_handle_map(int (*callback)(struct shim_fd_handle*, struct shim_handle_map*),
struct shim_handle_map* map) {
int ret = 0;
lock(&map->lock);
if (map->fd_top == FD_NULL)
goto done;
for (int i = 0; i <= map->fd_top; i++) {
if (!HANDLE_ALLOCATED(map->map[i]))
continue;
if ((ret = (*callback)(map->map[i], map)) < 0)
break;
}
done:
unlock(&map->lock);
return ret;
}
BEGIN_CP_FUNC(handle) {
__UNUSED(size);
assert(size == sizeof(struct shim_handle));
struct shim_handle* hdl = (struct shim_handle*)obj;
struct shim_handle* new_hdl = NULL;
size_t off = GET_FROM_CP_MAP(obj);
if (!off) {
off = ADD_CP_OFFSET(sizeof(struct shim_handle));
ADD_TO_CP_MAP(obj, off);
new_hdl = (struct shim_handle*)(base + off);
lock(&hdl->lock);
*new_hdl = *hdl;
if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->checkout)
hdl->fs->fs_ops->checkout(new_hdl);
new_hdl->dentry = NULL;
REF_SET(new_hdl->ref_count, 0);
clear_lock(&new_hdl->lock);
DO_CP(fs, hdl->fs, &new_hdl->fs);
DO_CP_IN_MEMBER(qstr, new_hdl, uri);
if (hdl->dentry) {
if (hdl->dentry->state & DENTRY_ISDIRECTORY) {
/*
* We don't checkpoint children dentries of a directory dentry, so the child process
* will need to list the directory again. However, we keep `dir_info.pos` unchanged
* so that `getdents/getdents64` will resume from the same place.
*/
new_hdl->dir_info.dents = NULL;
new_hdl->dir_info.count = 0;
}
DO_CP_MEMBER(dentry, hdl, new_hdl, dentry);
}
if (new_hdl->pal_handle) {
struct shim_palhdl_entry* entry;
DO_CP(palhdl, hdl->pal_handle, &entry);
entry->uri = &new_hdl->uri;
entry->phandle = &new_hdl->pal_handle;
}
INIT_LISTP(&new_hdl->epolls);
switch (hdl->type) {
case TYPE_FILE:
if (hdl->info.file.sync)
DO_CP(sync_handle, hdl->info.file.sync, &new_hdl->info.file.sync);
break;
case TYPE_EPOLL:
/* `new_hdl->info.epoll.fds_count` stays the same - copied above. */
DO_CP(epoll_item, &hdl->info.epoll.fds, &new_hdl->info.epoll.fds);
__atomic_store_n(&new_hdl->info.epoll.waiter_cnt, 0, __ATOMIC_RELAXED);
memset(&new_hdl->info.epoll.event, '\0', sizeof(new_hdl->info.epoll.event));
break;
case TYPE_SOCK:
/* no support for multiple processes sharing options/peek buffer of the socket */
new_hdl->info.sock.pending_options = NULL;
new_hdl->info.sock.peek_buffer = NULL;
break;
default:
break;
}
unlock(&hdl->lock);
ADD_CP_FUNC_ENTRY(off);
} else {
new_hdl = (struct shim_handle*)(base + off);
}
if (objp)
*objp = (void*)new_hdl;
}
END_CP_FUNC(handle)
BEGIN_RS_FUNC(handle) {
struct shim_handle* hdl = (void*)(base + GET_CP_FUNC_ENTRY());
__UNUSED(offset);
CP_REBASE(hdl->fs);
CP_REBASE(hdl->dentry);
CP_REBASE(hdl->epolls);
if (!create_lock(&hdl->lock)) {
return -ENOMEM;
}
if (hdl->dentry) {
get_dentry(hdl->dentry);
}
switch (hdl->type) {
case TYPE_FILE:
CP_REBASE(hdl->info.file.sync);
break;
case TYPE_EPOLL: {
int ret = create_event(&hdl->info.epoll.event);
if (ret < 0) {
return ret;
}
struct shim_epoll_item* epoll_item;
size_t count = 0;
LISTP_FOR_EACH_ENTRY(epoll_item, &hdl->info.epoll.fds, list) {
epoll_item->epoll = hdl;
count++;
}
assert(hdl->info.epoll.fds_count == count);
break;
}
default:
break;
}
if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->checkin)
hdl->fs->fs_ops->checkin(hdl);
}
END_RS_FUNC(handle)
BEGIN_CP_FUNC(fd_handle) {
__UNUSED(size);
assert(size == sizeof(struct shim_fd_handle));
struct shim_fd_handle* fdhdl = (struct shim_fd_handle*)obj;
struct shim_fd_handle* new_fdhdl = NULL;
size_t off = ADD_CP_OFFSET(sizeof(struct shim_fd_handle));
new_fdhdl = (struct shim_fd_handle*)(base + off);
*new_fdhdl = *fdhdl;
DO_CP(handle, fdhdl->handle, &new_fdhdl->handle);
ADD_CP_FUNC_ENTRY(off);
if (objp)
*objp = (void*)new_fdhdl;
}
END_CP_FUNC_NO_RS(fd_handle)
BEGIN_CP_FUNC(handle_map) {
__UNUSED(size);
assert(size >= sizeof(struct shim_handle_map));
struct shim_handle_map* handle_map = (struct shim_handle_map*)obj;
struct shim_handle_map* new_handle_map = NULL;
struct shim_fd_handle** ptr_array;
lock(&handle_map->lock);
int fd_size = handle_map->fd_top != FD_NULL ? handle_map->fd_top + 1 : 0;
size = sizeof(struct shim_handle_map) + (sizeof(struct shim_fd_handle*) * fd_size);
size_t off = GET_FROM_CP_MAP(obj);
if (!off) {
off = ADD_CP_OFFSET(size);
new_handle_map = (struct shim_handle_map*)(base + off);
*new_handle_map = *handle_map;
ptr_array = (void*)new_handle_map + sizeof(struct shim_handle_map);
new_handle_map->fd_size = fd_size;
new_handle_map->map = fd_size ? ptr_array : NULL;
REF_SET(new_handle_map->ref_count, 0);
clear_lock(&new_handle_map->lock);
for (int i = 0; i < fd_size; i++) {
if (HANDLE_ALLOCATED(handle_map->map[i]))
DO_CP(fd_handle, handle_map->map[i], &ptr_array[i]);
else
ptr_array[i] = NULL;
}
ADD_CP_FUNC_ENTRY(off);
} else {
new_handle_map = (struct shim_handle_map*)(base + off);
}
unlock(&handle_map->lock);
if (objp)
*objp = (void*)new_handle_map;
}
END_CP_FUNC(handle_map)
BEGIN_RS_FUNC(handle_map) {
struct shim_handle_map* handle_map = (void*)(base + GET_CP_FUNC_ENTRY());
__UNUSED(offset);
CP_REBASE(handle_map->map);
assert(handle_map->map);
DEBUG_RS("size=%d,top=%d", handle_map->fd_size, handle_map->fd_top);
if (!create_lock(&handle_map->lock)) {
return -ENOMEM;
}
lock(&handle_map->lock);
if (handle_map->fd_top != FD_NULL)
for (int i = 0; i <= handle_map->fd_top; i++) {
CP_REBASE(handle_map->map[i]);
if (HANDLE_ALLOCATED(handle_map->map[i])) {
CP_REBASE(handle_map->map[i]->handle);
struct shim_handle* hdl = handle_map->map[i]->handle;
assert(hdl);
get_handle(hdl);
DEBUG_RS("[%d]%s", i, qstrempty(&hdl->uri) ? hdl->fs_type : qstrgetstr(&hdl->uri));
}
}
unlock(&handle_map->lock);
}
END_RS_FUNC(handle_map)