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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <syscall.h>
#include <unistd.h>
#include <string>
#include "gtest/gtest.h"
#include "absl/base/macros.h"
#include "absl/base/port.h"
#include "absl/flags/flag.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "test/syscalls/linux/socket_test_util.h"
#include "test/util/cleanup.h"
#include "test/util/eventfd_util.h"
#include "test/util/multiprocess_util.h"
#include "test/util/posix_error.h"
#include "test/util/save_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
#include "test/util/timer_util.h"
ABSL_FLAG(std::string, child_setlock_on, "",
"Contains the path to try to set a file lock on.");
ABSL_FLAG(bool, child_setlock_write, false,
"Whether to set a writable lock (otherwise readable)");
ABSL_FLAG(bool, blocking, false,
"Whether to set a blocking lock (otherwise non-blocking).");
ABSL_FLAG(bool, retry_eintr, false,
"Whether to retry in the subprocess on EINTR.");
ABSL_FLAG(uint64, child_setlock_start, 0, "The value of struct flock start");
ABSL_FLAG(uint64, child_setlock_len, 0, "The value of struct flock len");
ABSL_FLAG(int32, socket_fd, -1,
"A socket to use for communicating more state back "
"to the parent.");
namespace gvisor {
namespace testing {
// O_LARGEFILE as defined by Linux. glibc tries to be clever by setting it to 0
// because "it isn't needed", even though Linux can return it via F_GETFL.
constexpr int kOLargeFile = 00100000;
class FcntlLockTest : public ::testing::Test {
public:
void SetUp() override {
// Let's make a socket pair.
ASSERT_THAT(socketpair(AF_UNIX, SOCK_STREAM, 0, fds_), SyscallSucceeds());
}
void TearDown() override {
EXPECT_THAT(close(fds_[0]), SyscallSucceeds());
EXPECT_THAT(close(fds_[1]), SyscallSucceeds());
}
int64 GetSubprocessFcntlTimeInUsec() {
int64 ret = 0;
EXPECT_THAT(ReadFd(fds_[0], reinterpret_cast<void*>(&ret), sizeof(ret)),
SyscallSucceedsWithValue(sizeof(ret)));
return ret;
}
// The first fd will remain with the process creating the subprocess
// and the second will go to the subprocess.
int fds_[2] = {};
};
namespace {
PosixErrorOr<Cleanup> SubprocessLock(std::string const& path, bool for_write,
bool blocking, bool retry_eintr, int fd,
off_t start, off_t length, pid_t* child) {
std::vector<std::string> args = {
"/proc/self/exe", "--child_setlock_on", path,
"--child_setlock_start", absl::StrCat(start), "--child_setlock_len",
absl::StrCat(length), "--socket_fd", absl::StrCat(fd)};
if (for_write) {
args.push_back("--child_setlock_write");
}
if (blocking) {
args.push_back("--blocking");
}
if (retry_eintr) {
args.push_back("--retry_eintr");
}
int execve_errno = 0;
ASSIGN_OR_RETURN_ERRNO(
auto cleanup,
ForkAndExec("/proc/self/exe", ExecveArray(args.begin(), args.end()), {},
nullptr, child, &execve_errno));
if (execve_errno != 0) {
return PosixError(execve_errno, "execve");
}
return std::move(cleanup);
}
TEST(FcntlTest, SetCloExec) {
// Open an eventfd file descriptor with FD_CLOEXEC descriptor flag not set.
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, 0));
ASSERT_THAT(fcntl(fd.get(), F_GETFD), SyscallSucceedsWithValue(0));
// Set the FD_CLOEXEC flag.
ASSERT_THAT(fcntl(fd.get(), F_SETFD, FD_CLOEXEC), SyscallSucceeds());
ASSERT_THAT(fcntl(fd.get(), F_GETFD), SyscallSucceedsWithValue(FD_CLOEXEC));
}
TEST(FcntlTest, ClearCloExec) {
// Open an eventfd file descriptor with FD_CLOEXEC descriptor flag set.
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_CLOEXEC));
ASSERT_THAT(fcntl(fd.get(), F_GETFD), SyscallSucceedsWithValue(FD_CLOEXEC));
// Clear the FD_CLOEXEC flag.
ASSERT_THAT(fcntl(fd.get(), F_SETFD, 0), SyscallSucceeds());
ASSERT_THAT(fcntl(fd.get(), F_GETFD), SyscallSucceedsWithValue(0));
}
TEST(FcntlTest, IndependentDescriptorFlags) {
// Open an eventfd file descriptor with FD_CLOEXEC descriptor flag not set.
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, 0));
ASSERT_THAT(fcntl(fd.get(), F_GETFD), SyscallSucceedsWithValue(0));
// Duplicate the descriptor. Ensure that it also doesn't have FD_CLOEXEC.
FileDescriptor newfd = ASSERT_NO_ERRNO_AND_VALUE(fd.Dup());
ASSERT_THAT(fcntl(newfd.get(), F_GETFD), SyscallSucceedsWithValue(0));
// Set FD_CLOEXEC on the first FD.
ASSERT_THAT(fcntl(fd.get(), F_SETFD, FD_CLOEXEC), SyscallSucceeds());
ASSERT_THAT(fcntl(fd.get(), F_GETFD), SyscallSucceedsWithValue(FD_CLOEXEC));
// Ensure that the second FD is unaffected by the change on the first.
ASSERT_THAT(fcntl(newfd.get(), F_GETFD), SyscallSucceedsWithValue(0));
}
// All file description flags passed to open appear in F_GETFL.
TEST(FcntlTest, GetAllFlags) {
TempPath path = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
int flags = O_RDWR | O_DIRECT | O_SYNC | O_NONBLOCK | O_APPEND;
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(path.path(), flags));
// Linux forces O_LARGEFILE on all 64-bit kernels and gVisor's is 64-bit.
int expected = flags | kOLargeFile;
int rflags;
EXPECT_THAT(rflags = fcntl(fd.get(), F_GETFL), SyscallSucceeds());
EXPECT_EQ(rflags, expected);
}
TEST(FcntlTest, SetFlags) {
TempPath path = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(path.path(), 0));
int const flags = O_RDWR | O_DIRECT | O_SYNC | O_NONBLOCK | O_APPEND;
EXPECT_THAT(fcntl(fd.get(), F_SETFL, flags), SyscallSucceeds());
// Can't set O_RDWR or O_SYNC.
// Linux forces O_LARGEFILE on all 64-bit kernels and gVisor's is 64-bit.
int expected = O_DIRECT | O_NONBLOCK | O_APPEND | kOLargeFile;
int rflags;
EXPECT_THAT(rflags = fcntl(fd.get(), F_GETFL), SyscallSucceeds());
EXPECT_EQ(rflags, expected);
}
TEST_F(FcntlLockTest, SetLockBadFd) {
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// len 0 has a special meaning: lock all bytes despite how
// large the file grows.
fl.l_len = 0;
EXPECT_THAT(fcntl(-1, F_SETLK, &fl), SyscallFailsWithErrno(EBADF));
}
TEST_F(FcntlLockTest, SetLockPipe) {
int fds[2];
ASSERT_THAT(pipe(fds), SyscallSucceeds());
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd, but doesn't matter, we expect this to fail.
fl.l_len = 0;
EXPECT_THAT(fcntl(fds[0], F_SETLK, &fl), SyscallFailsWithErrno(EBADF));
EXPECT_THAT(close(fds[0]), SyscallSucceeds());
EXPECT_THAT(close(fds[1]), SyscallSucceeds());
}
TEST_F(FcntlLockTest, SetLockDir) {
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(dir.path(), O_RDONLY, 0666));
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
}
TEST_F(FcntlLockTest, SetLockBadOpenFlagsWrite) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY, 0666));
struct flock fl0;
fl0.l_type = F_WRLCK;
fl0.l_whence = SEEK_SET;
fl0.l_start = 0;
// Same as SetLockBadFd.
fl0.l_len = 0;
// Expect that setting a write lock using a read only file descriptor
// won't work.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl0), SyscallFailsWithErrno(EBADF));
}
TEST_F(FcntlLockTest, SetLockBadOpenFlagsRead) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY, 0666));
struct flock fl1;
fl1.l_type = F_RDLCK;
fl1.l_whence = SEEK_SET;
fl1.l_start = 0;
// Same as SetLockBadFd.
fl1.l_len = 0;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl1), SyscallFailsWithErrno(EBADF));
}
TEST_F(FcntlLockTest, SetLockUnlockOnNothing) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
}
TEST_F(FcntlLockTest, SetWriteLockSingleProc) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd0 =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
EXPECT_THAT(fcntl(fd0.get(), F_SETLK, &fl), SyscallSucceeds());
// Expect to be able to take the same lock on the same fd no problem.
EXPECT_THAT(fcntl(fd0.get(), F_SETLK, &fl), SyscallSucceeds());
FileDescriptor fd1 =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
// Expect to be able to take the same lock from a different fd but for
// the same process.
EXPECT_THAT(fcntl(fd1.get(), F_SETLK, &fl), SyscallSucceeds());
}
TEST_F(FcntlLockTest, SetReadLockMultiProc) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// spawn a child process to take a read lock on the same file.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), false /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetReadThenWriteLockMultiProc) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Assert that another process trying to lock on the same file will fail
// with EAGAIN. It's important that we keep the fd above open so that
// that the other process will contend with the lock.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), true /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == EAGAIN)
<< "Exited with code: " << status;
// Close the fd: we want to test that another process can acquire the
// lock after this point.
fd.reset();
// Assert that another process can now acquire the lock.
child_pid = 0;
auto cleanup2 = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), true /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetWriteThenReadLockMultiProc) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
// Same as SetReadThenWriteLockMultiProc.
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
// Same as SetReadThenWriteLockMultiProc.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Same as SetReadThenWriteLockMultiProc.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), false /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == EAGAIN)
<< "Exited with code: " << status;
// Same as SetReadThenWriteLockMultiProc.
fd.reset(); // Close the fd.
// Same as SetReadThenWriteLockMultiProc.
child_pid = 0;
auto cleanup2 = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), false /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetWriteLockMultiProc) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
// Same as SetReadThenWriteLockMultiProc.
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
// Same as SetReadWriteLockMultiProc.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Same as SetReadWriteLockMultiProc.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), true /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == EAGAIN)
<< "Exited with code: " << status;
fd.reset(); // Close the FD.
// Same as SetReadWriteLockMultiProc.
child_pid = 0;
auto cleanup2 = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), true /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetLockIsRegional) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 4096;
// Same as SetReadWriteLockMultiProc.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Same as SetReadWriteLockMultiProc.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), true /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_len, 0, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetLockUpgradeDowngrade) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
// Same as SetReadWriteLockMultiProc.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Upgrade to a write lock. This will prevent anyone else from taking
// the lock.
fl.l_type = F_WRLCK;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Same as SetReadWriteLockMultiProc.,
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), false /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == EAGAIN)
<< "Exited with code: " << status;
// Downgrade back to a read lock.
fl.l_type = F_RDLCK;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Do the same stint as before, but this time it should succeed.
child_pid = 0;
auto cleanup2 = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), false /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetLockDroppedOnClose) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
// While somewhat surprising, obtaining another fd to the same file and
// then closing it in this process drops *all* locks.
FileDescriptor other_fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
// Same as SetReadThenWriteLockMultiProc.
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
// Same as SetReadWriteLockMultiProc.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
other_fd.reset(); // Close.
// Expect to be able to get the lock, given that the close above dropped it.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(file.path(), true /* write lock */,
false /* nonblocking */, false /* no eintr retry */,
-1 /* no socket fd */, fl.l_start, fl.l_len, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetLockUnlock) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
// Setup two regional locks with different permissions.
struct flock fl0;
fl0.l_type = F_WRLCK;
fl0.l_whence = SEEK_SET;
fl0.l_start = 0;
fl0.l_len = 4096;
struct flock fl1;
fl1.l_type = F_RDLCK;
fl1.l_whence = SEEK_SET;
fl1.l_start = 4096;
// Same as SetLockBadFd.
fl1.l_len = 0;
// Set both region locks.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl0), SyscallSucceeds());
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl1), SyscallSucceeds());
// Another process should fail to take a read lock on the entire file
// due to the regional write lock.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(SubprocessLock(
file.path(), false /* write lock */, false /* nonblocking */,
false /* no eintr retry */, -1 /* no socket fd */, 0, 0, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == EAGAIN)
<< "Exited with code: " << status;
// Then only unlock the writable one. This should ensure that other
// processes can take any read lock that it wants.
fl0.l_type = F_UNLCK;
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl0), SyscallSucceeds());
// Another process should now succeed to get a read lock on the entire file.
child_pid = 0;
auto cleanup2 = ASSERT_NO_ERRNO_AND_VALUE(SubprocessLock(
file.path(), false /* write lock */, false /* nonblocking */,
false /* no eintr retry */, -1 /* no socket fd */, 0, 0, &child_pid));
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST_F(FcntlLockTest, SetLockAcrossRename) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
// Setup two regional locks with different permissions.
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
// Same as SetLockBadFd.
fl.l_len = 0;
// Set the region lock.
EXPECT_THAT(fcntl(fd.get(), F_SETLK, &fl), SyscallSucceeds());
// Rename the file to someplace nearby.
std::string const newpath = NewTempAbsPath();
EXPECT_THAT(rename(file.path().c_str(), newpath.c_str()), SyscallSucceeds());
// Another process should fail to take a read lock on the renamed file
// since we still have an open handle to the inode.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(
SubprocessLock(newpath, false /* write lock */, false /* nonblocking */,
false /* no eintr retry */, -1 /* no socket fd */,
fl.l_start, fl.l_len, &child_pid));
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == EAGAIN)
<< "Exited with code: " << status;
}
// NOTE: The blocking tests below aren't perfect. It's hard to assert exactly
// what the kernel did while handling a syscall. These tests are timing based
// because there really isn't any other reasonable way to assert that correct
// blocking behavior happened.
// This test will verify that blocking works as expected when another process
// holds a write lock when obtaining a write lock. This test will hold the lock
// for some amount of time and then wait for the second process to send over the
// socket_fd the amount of time it was blocked for before the lock succeeded.
TEST_F(FcntlLockTest, SetWriteLockThenBlockingWriteLock) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
// Take the write lock.
ASSERT_THAT(fcntl(fd.get(), F_SETLKW, &fl), SyscallSucceeds());
// Attempt to take the read lock in a sub process. This will immediately block
// so we will release our lock after some amount of time and then assert the
// amount of time the other process was blocked for.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(SubprocessLock(
file.path(), true /* write lock */, true /* Blocking Lock */,
true /* Retry on EINTR */, fds_[1] /* Socket fd for timing information */,
fl.l_start, fl.l_len, &child_pid));
// We will wait kHoldLockForSec before we release our lock allowing the
// subprocess to obtain it.
constexpr absl::Duration kHoldLockFor = absl::Seconds(5);
const int64 kMinBlockTimeUsec = absl::ToInt64Microseconds(absl::Seconds(1));
absl::SleepFor(kHoldLockFor);
// Unlock our write lock.
fl.l_type = F_UNLCK;
ASSERT_THAT(fcntl(fd.get(), F_SETLKW, &fl), SyscallSucceeds());
// Read the blocked time from the subprocess socket.
int64 subprocess_blocked_time_usec = GetSubprocessFcntlTimeInUsec();
// We must have been waiting at least kMinBlockTime.
EXPECT_GT(subprocess_blocked_time_usec, kMinBlockTimeUsec);
// The FCNTL write lock must always succeed as it will simply block until it
// can obtain the lock.
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
// This test will veirfy that blocking works as expected when another process
// holds a read lock when obtaining a write lock. This test will hold the lock
// for some amount of time and then wait for the second process to send over the
// socket_fd the amount of time it was blocked for before the lock succeeded.
TEST_F(FcntlLockTest, SetReadLockThenBlockingWriteLock) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
// Take the write lock.
ASSERT_THAT(fcntl(fd.get(), F_SETLKW, &fl), SyscallSucceeds());
// Attempt to take the read lock in a sub process. This will immediately block
// so we will release our lock after some amount of time and then assert the
// amount of time the other process was blocked for.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(SubprocessLock(
file.path(), true /* write lock */, true /* Blocking Lock */,
true /* Retry on EINTR */, fds_[1] /* Socket fd for timing information */,
fl.l_start, fl.l_len, &child_pid));
// We will wait kHoldLockForSec before we release our lock allowing the
// subprocess to obtain it.
constexpr absl::Duration kHoldLockFor = absl::Seconds(5);
const int64 kMinBlockTimeUsec = absl::ToInt64Microseconds(absl::Seconds(1));
absl::SleepFor(kHoldLockFor);
// Unlock our READ lock.
fl.l_type = F_UNLCK;
ASSERT_THAT(fcntl(fd.get(), F_SETLKW, &fl), SyscallSucceeds());
// Read the blocked time from the subprocess socket.
int64 subprocess_blocked_time_usec = GetSubprocessFcntlTimeInUsec();
// We must have been waiting at least kMinBlockTime.
EXPECT_GT(subprocess_blocked_time_usec, kMinBlockTimeUsec);
// The FCNTL write lock must always succeed as it will simply block until it
// can obtain the lock.
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
// This test will veirfy that blocking works as expected when another process
// holds a write lock when obtaining a read lock. This test will hold the lock
// for some amount of time and then wait for the second process to send over the
// socket_fd the amount of time it was blocked for before the lock succeeded.
TEST_F(FcntlLockTest, SetWriteLockThenBlockingReadLock) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
// Take the write lock.
ASSERT_THAT(fcntl(fd.get(), F_SETLKW, &fl), SyscallSucceeds());
// Attempt to take the read lock in a sub process. This will immediately block
// so we will release our lock after some amount of time and then assert the
// amount of time the other process was blocked for.
pid_t child_pid = 0;
auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(SubprocessLock(
file.path(), false /* read lock */, true /* Blocking Lock */,
true /* Retry on EINTR */, fds_[1] /* Socket fd for timing information */,
fl.l_start, fl.l_len, &child_pid));
// We will wait kHoldLockForSec before we release our lock allowing the
// subprocess to obtain it.
constexpr absl::Duration kHoldLockFor = absl::Seconds(5);
const int64 kMinBlockTimeUsec = absl::ToInt64Microseconds(absl::Seconds(1));
absl::SleepFor(kHoldLockFor);
// Unlock our write lock.
fl.l_type = F_UNLCK;
ASSERT_THAT(fcntl(fd.get(), F_SETLKW, &fl), SyscallSucceeds());
// Read the blocked time from the subprocess socket.
int64 subprocess_blocked_time_usec = GetSubprocessFcntlTimeInUsec();
// We must have been waiting at least kMinBlockTime.
EXPECT_GT(subprocess_blocked_time_usec, kMinBlockTimeUsec);
// The FCNTL read lock must always succeed as it will simply block until it
// can obtain the lock.
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
// This test will verify that when one process only holds a read lock that
// another will not block while obtaining a read lock when F_SETLKW is used.
TEST_F(FcntlLockTest, SetReadLockThenBlockingReadLock) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
// Take the READ lock.
ASSERT_THAT(fcntl(fd.get(), F_SETLKW, &fl), SyscallSucceeds());
// Attempt to take the read lock in a sub process. Since multiple processes
// can hold a read lock this should immediately return without blocking
// even though we used F_SETLKW in the subprocess.
pid_t child_pid = 0;
auto sp = ASSERT_NO_ERRNO_AND_VALUE(SubprocessLock(
file.path(), false /* read lock */, true /* Blocking Lock */,
true /* Retry on EINTR */, -1 /* No fd, should not block */, fl.l_start,
fl.l_len, &child_pid));
// We never release the lock and the subprocess should still obtain it without
// blocking for any period of time.
int status = 0;
ASSERT_THAT(RetryEINTR(waitpid)(child_pid, &status, 0), SyscallSucceeds());
EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0)
<< "Exited with code: " << status;
}
TEST(FcntlTest, GetO_ASYNC) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
int flag_fl = -1;
ASSERT_THAT(flag_fl = fcntl(s.get(), F_GETFL), SyscallSucceeds());
EXPECT_EQ(flag_fl & O_ASYNC, 0);
int flag_fd = -1;
ASSERT_THAT(flag_fd = fcntl(s.get(), F_GETFD), SyscallSucceeds());
EXPECT_EQ(flag_fd & O_ASYNC, 0);
}
TEST(FcntlTest, SetFlO_ASYNC) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
int before_fl = -1;
ASSERT_THAT(before_fl = fcntl(s.get(), F_GETFL), SyscallSucceeds());
int before_fd = -1;
ASSERT_THAT(before_fd = fcntl(s.get(), F_GETFD), SyscallSucceeds());
ASSERT_THAT(fcntl(s.get(), F_SETFL, before_fl | O_ASYNC), SyscallSucceeds());
int after_fl = -1;
ASSERT_THAT(after_fl = fcntl(s.get(), F_GETFL), SyscallSucceeds());
EXPECT_EQ(after_fl, before_fl | O_ASYNC);
int after_fd = -1;
ASSERT_THAT(after_fd = fcntl(s.get(), F_GETFD), SyscallSucceeds());
EXPECT_EQ(after_fd, before_fd);
}
TEST(FcntlTest, SetFdO_ASYNC) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
int before_fl = -1;
ASSERT_THAT(before_fl = fcntl(s.get(), F_GETFL), SyscallSucceeds());
int before_fd = -1;
ASSERT_THAT(before_fd = fcntl(s.get(), F_GETFD), SyscallSucceeds());
ASSERT_THAT(fcntl(s.get(), F_SETFD, before_fd | O_ASYNC), SyscallSucceeds());
int after_fl = -1;
ASSERT_THAT(after_fl = fcntl(s.get(), F_GETFL), SyscallSucceeds());
EXPECT_EQ(after_fl, before_fl);
int after_fd = -1;
ASSERT_THAT(after_fd = fcntl(s.get(), F_GETFD), SyscallSucceeds());
EXPECT_EQ(after_fd, before_fd);
}
TEST(FcntlTest, DupAfterO_ASYNC) {
FileDescriptor s1 = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
int before = -1;
ASSERT_THAT(before = fcntl(s1.get(), F_GETFL), SyscallSucceeds());
ASSERT_THAT(fcntl(s1.get(), F_SETFL, before | O_ASYNC), SyscallSucceeds());
FileDescriptor fd2 = ASSERT_NO_ERRNO_AND_VALUE(s1.Dup());
int after = -1;
ASSERT_THAT(after = fcntl(fd2.get(), F_GETFL), SyscallSucceeds());
EXPECT_EQ(after & O_ASYNC, O_ASYNC);
}
TEST(FcntlTest, GetOwn) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
EXPECT_EQ(syscall(__NR_fcntl, s.get(), F_GETOWN), 0);
MaybeSave();
}
TEST(FcntlTest, GetOwnEx) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
EXPECT_THAT(syscall(__NR_fcntl, s.get(), F_GETOWN_EX, &owner),
SyscallSucceedsWithValue(0));
}
TEST(FcntlTest, SetOwnExInvalidType) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
owner.type = __pid_type(-1);
EXPECT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &owner),
SyscallFailsWithErrno(EINVAL));
}
TEST(FcntlTest, SetOwnExInvalidTid) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
owner.type = F_OWNER_TID;
owner.pid = -1;
EXPECT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &owner),
SyscallFailsWithErrno(ESRCH));
}
TEST(FcntlTest, SetOwnExInvalidPid) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
owner.type = F_OWNER_PID;
owner.pid = -1;
EXPECT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &owner),
SyscallFailsWithErrno(ESRCH));
}
TEST(FcntlTest, SetOwnExInvalidPgrp) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
owner.type = F_OWNER_PGRP;
owner.pid = -1;
EXPECT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &owner),
SyscallFailsWithErrno(ESRCH));
}
TEST(FcntlTest, SetOwnExTid) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
owner.type = F_OWNER_TID;
EXPECT_THAT(owner.pid = syscall(__NR_gettid), SyscallSucceeds());
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &owner),
SyscallSucceeds());
EXPECT_EQ(syscall(__NR_fcntl, s.get(), F_GETOWN), owner.pid);
MaybeSave();
}
TEST(FcntlTest, SetOwnExPid) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
owner.type = F_OWNER_PID;
EXPECT_THAT(owner.pid = getpid(), SyscallSucceeds());
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &owner),
SyscallSucceeds());
EXPECT_EQ(syscall(__NR_fcntl, s.get(), F_GETOWN), owner.pid);
MaybeSave();
}
TEST(FcntlTest, SetOwnExPgrp) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex owner = {};
owner.type = F_OWNER_PGRP;
EXPECT_THAT(owner.pid = getpgrp(), SyscallSucceeds());
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &owner),
SyscallSucceeds());
// NOTE(igudger): I don't understand why, but this is flaky on Linux.
// GetOwnExPgrp (below) does not have this issue.
SKIP_IF(!IsRunningOnGvisor());
EXPECT_EQ(syscall(__NR_fcntl, s.get(), F_GETOWN), -owner.pid);
MaybeSave();
}
TEST(FcntlTest, GetOwnExTid) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex set_owner = {};
set_owner.type = F_OWNER_TID;
EXPECT_THAT(set_owner.pid = syscall(__NR_gettid), SyscallSucceeds());
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &set_owner),
SyscallSucceeds());
f_owner_ex got_owner = {};
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_GETOWN_EX, &got_owner),
SyscallSucceedsWithValue(0));
EXPECT_EQ(got_owner.type, set_owner.type);
EXPECT_EQ(got_owner.pid, set_owner.pid);
}
TEST(FcntlTest, GetOwnExPid) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex set_owner = {};
set_owner.type = F_OWNER_PID;
EXPECT_THAT(set_owner.pid = getpid(), SyscallSucceeds());
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &set_owner),
SyscallSucceeds());
f_owner_ex got_owner = {};
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_GETOWN_EX, &got_owner),
SyscallSucceedsWithValue(0));
EXPECT_EQ(got_owner.type, set_owner.type);
EXPECT_EQ(got_owner.pid, set_owner.pid);
}
TEST(FcntlTest, GetOwnExPgrp) {
FileDescriptor s = ASSERT_NO_ERRNO_AND_VALUE(
Socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
f_owner_ex set_owner = {};
set_owner.type = F_OWNER_PGRP;
EXPECT_THAT(set_owner.pid = getpgrp(), SyscallSucceeds());
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_SETOWN_EX, &set_owner),
SyscallSucceeds());
f_owner_ex got_owner = {};
ASSERT_THAT(syscall(__NR_fcntl, s.get(), F_GETOWN_EX, &got_owner),
SyscallSucceedsWithValue(0));
EXPECT_EQ(got_owner.type, set_owner.type);
EXPECT_EQ(got_owner.pid, set_owner.pid);
}
} // namespace
} // namespace testing
} // namespace gvisor
int main(int argc, char** argv) {
gvisor::testing::TestInit(&argc, &argv);
const std::string setlock_on = absl::GetFlag(FLAGS_child_setlock_on);
if (!setlock_on.empty()) {
int socket_fd = absl::GetFlag(FLAGS_socket_fd);
int fd = open(setlock_on.c_str(), O_RDWR, 0666);
if (fd == -1 && errno != 0) {
int err = errno;
std::cerr << "CHILD open " << setlock_on << " failed " << err
<< std::endl;
exit(err);
}
struct flock fl;
if (absl::GetFlag(FLAGS_child_setlock_write)) {
fl.l_type = F_WRLCK;
} else {
fl.l_type = F_RDLCK;
}
fl.l_whence = SEEK_SET;
fl.l_start = absl::GetFlag(FLAGS_child_setlock_start);
fl.l_len = absl::GetFlag(FLAGS_child_setlock_len);
// Test the fcntl, no need to log, the error is unambiguously
// from fcntl at this point.
int err = 0;
int ret = 0;
gvisor::testing::MonotonicTimer timer;
timer.Start();
do {
ret = fcntl(fd, absl::GetFlag(FLAGS_blocking) ? F_SETLKW : F_SETLK, &fl);
} while (absl::GetFlag(FLAGS_retry_eintr) && ret == -1 && errno == EINTR);
auto usec = absl::ToInt64Microseconds(timer.Duration());
if (ret == -1 && errno != 0) {
err = errno;
}
// If there is a socket fd let's send back the time in microseconds it took
// to execute this syscall.
if (socket_fd != -1) {
gvisor::testing::WriteFd(socket_fd, reinterpret_cast<void*>(&usec),
sizeof(usec));
close(socket_fd);
}
close(fd);
exit(err);
}
return RUN_ALL_TESTS();
}
|