-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathlibos_handle.c
1035 lines (825 loc) · 29.8 KB
/
libos_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
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
/* 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 "libos_checkpoint.h"
#include "libos_fs.h"
#include "libos_fs_lock.h"
#include "libos_handle.h"
#include "libos_internal.h"
#include "libos_lock.h"
#include "libos_thread.h"
#include "pal.h"
#include "stat.h"
#include "toml_utils.h"
static struct libos_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 libos_handle
#include "memmgr.h"
static MEM_MGR handle_mgr = NULL;
#define INIT_HANDLE_MAP_SIZE 32
void maybe_lock_pos_handle(struct libos_handle* hdl) {
if (hdl->seekable)
lock(&hdl->pos_lock);
}
void maybe_unlock_pos_handle(struct libos_handle* hdl) {
if (hdl->seekable)
unlock(&hdl->pos_lock);
}
int open_executable(struct libos_handle* hdl, const char* path) {
struct libos_dentry* dent = NULL;
lock(&g_dcache_lock);
int ret = path_lookupat(/*start=*/NULL, path, LOOKUP_FOLLOW, &dent);
if (ret < 0) {
goto out;
}
if (dent->inode->type != S_IFREG) {
ret = -EACCES;
goto out;
}
if (!(dent->inode->perm & S_IXUSR)) {
ret = -EACCES;
goto out;
}
ret = dentry_open(hdl, dent, O_RDONLY);
if (ret < 0) {
goto out;
}
ret = 0;
out:
unlock(&g_dcache_lock);
if (dent)
put_dentry(dent);
return ret;
}
int init_exec_handle(const char* const* argv, char*** out_new_argv) {
lock(&g_process.fs_lock);
if (g_process.exec) {
/* `g_process.exec` handle is already initialized if we did execve. See
* `libos_syscall_execve_rtld`. */
unlock(&g_process.fs_lock);
*out_new_argv = NULL;
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 libos_handle* exec_handle = 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'");
ret = -EINVAL;
goto out;
}
if (!entrypoint) {
log_error("'libos.entrypoint' must be specified in the manifest");
ret = -EINVAL;
goto out;
}
exec_path = entrypoint;
if (strstartswith(exec_path, URI_PREFIX_FILE)) {
/* Technically we could skip this check, but it's something easy to confuse with
* loader.entrypoint, so better to have this handled nicely for the users. */
log_error("'libos.entrypoint' should be an in-Gramine path, not URI.");
ret = -EINVAL;
goto out;
}
char** new_argv = NULL;
ret = load_and_check_exec(exec_path, argv, &exec_handle, &new_argv);
if (ret < 0) {
goto out;
}
lock(&g_process.fs_lock);
g_process.exec = exec_handle;
get_handle(exec_handle);
unlock(&g_process.fs_lock);
*out_new_argv = new_argv;
ret = 0;
out:
free(entrypoint);
if (exec_handle)
put_handle(exec_handle);
return ret;
}
static struct libos_handle_map* get_new_handle_map(uint32_t size);
static int __init_handle(struct libos_fd_handle** fdhdl, uint32_t fd, struct libos_handle* hdl,
int fd_flags);
static int __enlarge_handle_map(struct libos_handle_map* map, uint32_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;
}
/* after fork, in the new child process, `libos_init` is run, hence this function too - but
* forked process will get its RLIMIT_NOFILE from the checkpoint */
assert(g_pal_public_state);
if (g_pal_public_state->parent_process)
return 0;
assert(g_manifest_root);
int64_t fds_limit_init64;
int ret = toml_int_in(g_manifest_root, "sys.fds.limit",
/*defaultval=*/get_rlimit_cur(RLIMIT_NOFILE),
&fds_limit_init64);
if (ret < 0) {
log_error("Cannot parse 'sys.fds.limit'");
return -EINVAL;
}
if (fds_limit_init64 < 0) {
log_error("'sys.fds.limit' is negative (%ld)", fds_limit_init64);
return -EINVAL;
}
set_rlimit_cur(RLIMIT_NOFILE, (uint64_t)fds_limit_init64);
return 0;
}
int init_std_handles(void) {
int ret;
struct libos_thread* thread = get_cur_thread();
if (thread->handle_map)
return 0;
struct libos_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. */
rwlock_write_lock(&handle_map->lock);
if (handle_map->fd_size < 3) {
ret = __enlarge_handle_map(handle_map, INIT_HANDLE_MAP_SIZE);
if (ret < 0) {
rwlock_write_unlock(&handle_map->lock);
return ret;
}
}
/* initialize stdin */
if (!HANDLE_ALLOCATED(handle_map->map[0])) {
struct libos_handle* stdin_hdl = get_new_handle();
if (!stdin_hdl) {
rwlock_write_unlock(&handle_map->lock);
return -ENOMEM;
}
ret = open_namei(stdin_hdl, /*start=*/NULL, "/dev/tty", O_RDONLY, LOOKUP_FOLLOW,
/*found=*/NULL);
if (ret < 0) {
rwlock_write_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 libos_handle* stdout_hdl = get_new_handle();
if (!stdout_hdl) {
rwlock_write_unlock(&handle_map->lock);
return -ENOMEM;
}
ret = open_namei(stdout_hdl, /*start=*/NULL, "/dev/tty", O_WRONLY | O_APPEND, LOOKUP_FOLLOW,
/*found=*/NULL);
if (ret < 0) {
rwlock_write_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 libos_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;
rwlock_write_unlock(&handle_map->lock);
return 0;
}
struct libos_handle* __get_fd_handle(uint32_t fd, int* fd_flags, struct libos_handle_map* map) {
assert(map);
assert(rwlock_is_read_locked(&map->lock) || rwlock_is_write_locked(&map->lock));
struct libos_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 libos_handle* get_fd_handle(uint32_t fd, int* fd_flags, struct libos_handle_map* map) {
map = map ?: get_thread_handle_map(NULL);
assert(map);
struct libos_handle* hdl = NULL;
rwlock_read_lock(&map->lock);
if ((hdl = __get_fd_handle(fd, fd_flags, map)))
get_handle(hdl);
rwlock_read_unlock(&map->lock);
return hdl;
}
static struct libos_handle* __detach_fd_handle(struct libos_fd_handle* fd, int* flags,
struct libos_handle_map* map) {
assert(rwlock_is_write_locked(&map->lock));
struct libos_handle* handle = NULL;
if (HANDLE_ALLOCATED(fd)) {
uint32_t vfd = fd->vfd;
handle = fd->handle;
int handle_fd = vfd;
if (flags)
*flags = fd->flags;
fd->vfd = FD_NULL;
fd->handle = NULL;
fd->flags = 0;
if (vfd == map->fd_top)
do {
if (vfd == 0) {
map->fd_top = FD_NULL;
break;
}
map->fd_top = vfd - 1;
vfd--;
} while (!HANDLE_ALLOCATED(map->map[vfd]));
delete_epoll_items_for_fd(handle_fd, handle);
}
return handle;
}
static int clear_posix_locks(struct libos_handle* handle) {
int ret = 0;
if (handle) {
lock(&handle->lock);
if (handle->dentry) {
/* Clear file (POSIX) locks for a file. We are required to do that every time a FD is
* closed, even if the process holds other handles for that file, or duplicated FDs for
* the same handle. */
struct libos_file_lock file_lock = {
.family = FILE_LOCK_POSIX,
.type = F_UNLCK,
.start = 0,
.end = FS_LOCK_EOF,
.pid = g_process.pid,
};
ret = file_lock_set(handle->dentry, &file_lock, /*block=*/false);
if (ret < 0) {
log_warning("error releasing locks: %s", unix_strerror(ret));
}
}
unlock(&handle->lock);
}
return ret;
}
struct libos_handle* detach_fd_handle(uint32_t fd, int* flags,
struct libos_handle_map* handle_map) {
struct libos_handle* handle = NULL;
if (!handle_map && !(handle_map = get_thread_handle_map(NULL)))
return NULL;
rwlock_write_lock(&handle_map->lock);
if (fd < handle_map->fd_size)
handle = __detach_fd_handle(handle_map->map[fd], flags, handle_map);
rwlock_write_unlock(&handle_map->lock);
(void)clear_posix_locks(handle);
return handle;
}
struct libos_handle* get_new_handle(void) {
struct libos_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 libos_handle));
refcount_set(&new_handle->ref_count, 1);
if (!create_lock(&new_handle->lock)) {
free_mem_obj_to_mgr(handle_mgr, new_handle);
return NULL;
}
if (!create_lock(&new_handle->pos_lock)) {
destroy_lock(&new_handle->lock);
free_mem_obj_to_mgr(handle_mgr, new_handle);
return NULL;
}
INIT_LISTP(&new_handle->epoll_items);
new_handle->epoll_items_count = 0;
static uint32_t local_id_counter = 0;
uint32_t next_id_counter = __atomic_add_fetch(&local_id_counter, 1, __ATOMIC_RELAXED);
if (!next_id_counter) {
/* overflow of local_id_counter, this may lead to aliasing of different handles and is
* potentially a security vulnerability, so just terminate the whole process */
log_error("overflow when allocating a handle ID, not safe to proceed");
BUG();
}
new_handle->id = ((uint64_t)g_process.pid << 32) | next_id_counter;
new_handle->created_by_process = true;
return new_handle;
}
static int __init_handle(struct libos_fd_handle** fdhdl, uint32_t fd, struct libos_handle* hdl,
int fd_flags) {
struct libos_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 libos_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(uint32_t fd, struct libos_handle* hdl, int fd_flags,
struct libos_handle_map* handle_map, bool find_free) {
int ret;
if (!handle_map && !(handle_map = get_thread_handle_map(NULL)))
return -EBADF;
rwlock_write_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) {
uint32_t new_size = handle_map->fd_size;
if (new_size == 0)
new_size = INIT_HANDLE_MAP_SIZE;
while (new_size <= fd) {
if (__builtin_mul_overflow(new_size, 2, &new_size)) {
ret = -ENFILE;
goto out;
}
}
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:
rwlock_write_unlock(&handle_map->lock);
return ret;
}
int set_new_fd_handle(struct libos_handle* hdl, int fd_flags, struct libos_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(uint32_t fd, struct libos_handle* hdl, int fd_flags,
struct libos_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(uint32_t fd, struct libos_handle* hdl, int fd_flags,
struct libos_handle_map* handle_map) {
return __set_new_fd_handle(fd, hdl, fd_flags, handle_map, /*find_first=*/true);
}
void get_handle(struct libos_handle* hdl) {
refcount_inc(&hdl->ref_count);
}
static void destroy_handle(struct libos_handle* hdl) {
destroy_lock(&hdl->lock);
destroy_lock(&hdl->pos_lock);
free_mem_obj_to_mgr(handle_mgr, hdl);
}
static int clear_flock_locks(struct libos_handle* hdl) {
/* Clear flock (BSD) locks for a file. We are required to do that when the handle is closed. */
int ret = 0;
if (hdl) {
lock(&hdl->lock);
if (hdl->dentry && hdl->created_by_process) {
assert(hdl->ref_count == 0);
struct libos_file_lock file_lock = {
.family = FILE_LOCK_FLOCK,
.type = F_UNLCK,
.handle_id = hdl->id,
};
int ret = file_lock_set(hdl->dentry, &file_lock, /*block=*/false);
if (ret < 0) {
log_warning("error releasing locks: %s", unix_strerror(ret));
}
}
unlock(&hdl->lock);
}
return ret;
}
void put_handle(struct libos_handle* hdl) {
refcount_t ref_count = refcount_dec(&hdl->ref_count);
if (!ref_count) {
assert(hdl->epoll_items_count == 0);
assert(LISTP_EMPTY(&hdl->epoll_items));
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);
free(hdl->uri);
if (hdl->pal_handle) {
PalObjectDestroy(hdl->pal_handle);
hdl->pal_handle = NULL;
}
if (hdl->dentry) { /* no locking needed as no other reference exists */
(void)clear_flock_locks(hdl);
put_dentry(hdl->dentry);
}
if (hdl->inode)
put_inode(hdl->inode);
destroy_handle(hdl);
}
}
static struct libos_handle_map* get_new_handle_map(uint32_t size) {
struct libos_handle_map* handle_map = calloc(1, sizeof(struct libos_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 (!rwlock_create(&handle_map->lock)) {
free(handle_map->map);
free(handle_map);
return NULL;
}
refcount_set(&handle_map->ref_count, 1);
return handle_map;
}
static int __enlarge_handle_map(struct libos_handle_map* map, uint32_t size) {
assert(rwlock_is_write_locked(&map->lock));
if (size <= map->fd_size)
return 0;
struct libos_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 libos_handle_map** new, struct libos_handle_map* old_map) {
rwlock_read_lock(&old_map->lock);
/* allocate a new handle mapping with the same size as
the old one */
struct libos_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 (uint32_t i = 0; i <= old_map->fd_top; i++) {
struct libos_fd_handle* fd_old = old_map->map[i];
struct libos_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 libos_handle* hdl = fd_old->handle;
get_handle(hdl);
fd_new = malloc(sizeof(struct libos_fd_handle));
if (!fd_new) {
for (uint32_t j = 0; j < i; j++) {
put_handle(new_map->map[j]->handle);
free(new_map->map[j]);
}
rwlock_read_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:
rwlock_read_unlock(&old_map->lock);
*new = new_map;
return 0;
}
void get_handle_map(struct libos_handle_map* map) {
refcount_inc(&map->ref_count);
}
void put_handle_map(struct libos_handle_map* map) {
refcount_t ref_count = refcount_dec(&map->ref_count);
if (!ref_count) {
if (map->fd_top == FD_NULL)
goto done;
for (uint32_t i = 0; i <= map->fd_top; i++) {
if (!map->map[i])
continue;
if (map->map[i]->vfd != FD_NULL) {
struct libos_handle* handle = map->map[i]->handle;
if (handle)
put_handle(handle);
}
free(map->map[i]);
}
done:
rwlock_destroy(&map->lock);
free(map->map);
free(map);
}
}
static int walk_handle_map_writable(int (*callback)(struct libos_fd_handle*,
struct libos_handle_map*),
struct libos_handle_map* map) {
int ret = 0;
rwlock_write_lock(&map->lock);
for (uint32_t i = 0; map->fd_top != FD_NULL && i <= map->fd_top; i++) {
if (!HANDLE_ALLOCATED(map->map[i]))
continue;
if ((ret = (*callback)(map->map[i], map)) < 0)
break;
}
rwlock_write_unlock(&map->lock);
return ret;
}
static int detach_fd(struct libos_fd_handle* fd_hdl, struct libos_handle_map* map) {
struct libos_handle* hdl = __detach_fd_handle(fd_hdl, NULL, map);
put_handle(hdl);
return 0;
}
void detach_all_fds(void) {
struct libos_handle_map* handle_map = get_thread_handle_map(NULL);
assert(handle_map);
/* TODO: either this should check the return value or the iterator function should not halt on
* errors. */
walk_handle_map_writable(&detach_fd, handle_map);
}
void close_cloexec_handles(struct libos_handle_map* map) {
rwlock_write_lock(&map->lock);
for (uint32_t i = 0; map->fd_top != FD_NULL && i <= map->fd_top; i++) {
struct libos_fd_handle* fd_hdl = map->map[i];
if (!HANDLE_ALLOCATED(fd_hdl))
continue;
if (fd_hdl->flags & FD_CLOEXEC) {
struct libos_handle* hdl = __detach_fd_handle(fd_hdl, NULL, map);
rwlock_write_unlock(&map->lock);
(void)clear_posix_locks(hdl);
put_handle(hdl);
rwlock_write_lock(&map->lock);
}
}
rwlock_write_unlock(&map->lock);
}
void close_handle_range(uint32_t first, uint32_t last, bool cloexec) {
struct libos_handle_map* handle_map = get_thread_handle_map(NULL);
rwlock_write_lock(&handle_map->lock);
for (uint32_t i = first; handle_map->fd_top != FD_NULL && i <= handle_map->fd_top && i <= last;
i++) {
struct libos_fd_handle* fd_hdl = handle_map->map[i];
if (!HANDLE_ALLOCATED(fd_hdl))
continue;
if (cloexec) {
fd_hdl->flags |= FD_CLOEXEC;
} else {
struct libos_handle* hdl = __detach_fd_handle(fd_hdl, NULL, handle_map);
rwlock_write_unlock(&handle_map->lock);
(void)clear_posix_locks(hdl);
put_handle(hdl);
rwlock_write_lock(&handle_map->lock);
}
}
rwlock_write_unlock(&handle_map->lock);
}
BEGIN_CP_FUNC(handle) {
__UNUSED(size);
assert(size == sizeof(struct libos_handle));
struct libos_handle* hdl = (struct libos_handle*)obj;
struct libos_handle* new_hdl = NULL;
size_t off = GET_FROM_CP_MAP(obj);
if (!off) {
off = ADD_CP_OFFSET(sizeof(struct libos_handle));
ADD_TO_CP_MAP(obj, off);
new_hdl = (struct libos_handle*)(base + off);
if (hdl->type == TYPE_SOCK) {
/* We need this lock taken before `hdl->lock`. This checkpointing mess needs to be
* untangled. */
lock(&hdl->info.sock.lock);
}
/* TODO: this lock is not released on errors. The main problem here is that `DO_CP` can
* just return... */
lock(&hdl->lock);
*new_hdl = *hdl;
new_hdl->created_by_process = false;
new_hdl->dentry = NULL;
refcount_set(&new_hdl->ref_count, 0);
clear_lock(&new_hdl->lock);
clear_lock(&new_hdl->pos_lock);
DO_CP(fs, hdl->fs, &new_hdl->fs);
if (hdl->uri)
DO_CP_MEMBER(str, hdl, new_hdl, uri);
if (hdl->is_dir) {
/*
* 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;
}
if (hdl->dentry) {
DO_CP_MEMBER(dentry, hdl, new_hdl, dentry);
}
/* This list is created empty and all necessary references are added when checkpointing
* items lists from specific epoll handles. See `epoll_items_list` checkpointing in
* `libos_epoll.c` for more details. */
INIT_LISTP(&new_hdl->epoll_items);
new_hdl->epoll_items_count = 0;
/* TODO: move this into epoll specific `checkout` callback.
* It's impossible at the moment, because `DO_CP` is a macro that can be only used inside
* BEGIN_CP_FUNC. */
if (hdl->type == TYPE_EPOLL) {
struct libos_epoll_handle* epoll = &new_hdl->info.epoll;
clear_lock(&epoll->lock);
INIT_LISTP(&epoll->waiters);
INIT_LISTP(&epoll->items);
epoll->items_count = 0;
DO_CP(epoll_items_list, hdl, new_hdl);
}
if (hdl->type == TYPE_SOCK) {
PAL_HANDLE pal_handle = __atomic_load_n(&hdl->info.sock.pal_handle, __ATOMIC_ACQUIRE);
new_hdl->info.sock.pal_handle = NULL;
if (pal_handle) {
struct libos_palhdl_entry* entry;
DO_CP(palhdl_ptr, &pal_handle, &entry);
entry->phandle = &new_hdl->info.sock.pal_handle;
}
}
if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->checkout) {
int ret = hdl->fs->fs_ops->checkout(new_hdl);
if (ret < 0) {
return ret;
}
}
if (new_hdl->pal_handle) {
struct libos_palhdl_entry* entry;
DO_CP(palhdl_ptr, &hdl->pal_handle, &entry);
entry->phandle = &new_hdl->pal_handle;
}
unlock(&hdl->lock);
if (hdl->type == TYPE_SOCK) {
unlock(&hdl->info.sock.lock);
}
if (hdl->inode) {
/* NOTE: Checkpointing `inode` will take `inode->lock`, so we need to do it after
* `hdl->lock` is released. */
DO_CP_MEMBER(inode, hdl, new_hdl, inode);
}
ADD_CP_FUNC_ENTRY(off);
} else {
new_hdl = (struct libos_handle*)(base + off);
}
if (objp)
*objp = (void*)new_hdl;
}
END_CP_FUNC(handle)
BEGIN_RS_FUNC(handle) {
struct libos_handle* hdl = (void*)(base + GET_CP_FUNC_ENTRY());
__UNUSED(offset);
CP_REBASE(hdl->fs);
CP_REBASE(hdl->dentry);
CP_REBASE(hdl->inode);
CP_REBASE(hdl->epoll_items);
if (!create_lock(&hdl->lock)) {
return -ENOMEM;
}
if (!create_lock(&hdl->pos_lock)) {
return -ENOMEM;
}
if (hdl->dentry) {
get_dentry(hdl->dentry);
}
if (hdl->inode) {
get_inode(hdl->inode);
}
/* TODO: move this to epoll specific `checkin` callback. */
switch (hdl->type) {
case TYPE_EPOLL:;
struct libos_epoll_handle* epoll = &hdl->info.epoll;
if (!create_lock(&epoll->lock)) {
return -ENOMEM;
}
CP_REBASE(epoll->waiters);
/* `epoll->items` is rebased in epoll_items_list RS_FUNC. */
break;
default:
break;
}
if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->checkin) {
int ret = hdl->fs->fs_ops->checkin(hdl);
if (ret < 0)
return ret;
}
}
END_RS_FUNC(handle)
BEGIN_CP_FUNC(fd_handle) {
__UNUSED(size);
assert(size == sizeof(struct libos_fd_handle));
struct libos_fd_handle* fdhdl = (struct libos_fd_handle*)obj;
struct libos_fd_handle* new_fdhdl = NULL;
size_t off = ADD_CP_OFFSET(sizeof(struct libos_fd_handle));
new_fdhdl = (struct libos_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 libos_handle_map));
struct libos_handle_map* handle_map = (struct libos_handle_map*)obj;
struct libos_handle_map* new_handle_map = NULL;
struct libos_fd_handle** ptr_array;
rwlock_read_lock(&handle_map->lock);
int fd_size = handle_map->fd_top != FD_NULL ? handle_map->fd_top + 1 : 0;
size = sizeof(struct libos_handle_map) + (sizeof(struct libos_fd_handle*) * fd_size);
size_t off = GET_FROM_CP_MAP(obj);
if (!off) {
off = ADD_CP_OFFSET(size);
new_handle_map = (struct libos_handle_map*)(base + off);
*new_handle_map = *handle_map;
ptr_array = (void*)new_handle_map + sizeof(struct libos_handle_map);
new_handle_map->fd_size = fd_size;
new_handle_map->map = fd_size ? ptr_array : NULL;
refcount_set(&new_handle_map->ref_count, 0);
new_handle_map->lock = (struct libos_rwlock){0};
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 libos_handle_map*)(base + off);
}
rwlock_read_unlock(&handle_map->lock);