mirror of
				https://github.com/golang/go.git
				synced 2025-10-31 08:40:55 +00:00 
			
		
		
		
	syscall: add test for Flock_t roundtrip
See CL 9962 for the rationale. Change-Id: I73c714fce258430eea1e61d3835f5c8e9014ca1f Signed-off-by: Shenghou Ma <minux@golang.org> Reviewed-on: https://go-review.googlesource.com/9925 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
		
							parent
							
								
									38631846bf
								
							
						
					
					
						commit
						ed8ae79282
					
				
					 1 changed files with 47 additions and 12 deletions
				
			
		|  | @ -60,20 +60,55 @@ func _() { | ||||||
| 
 | 
 | ||||||
| // TestFcntlFlock tests whether the file locking structure matches | // TestFcntlFlock tests whether the file locking structure matches | ||||||
| // the calling convention of each kernel. | // the calling convention of each kernel. | ||||||
|  | // On some Linux systems, glibc uses another set of values for the | ||||||
|  | // commands and translates them to the correct value that the kernel | ||||||
|  | // expects just before the actual fcntl syscall. As Go uses raw | ||||||
|  | // syscalls directly, it must use the real value, not the glibc value. | ||||||
|  | // Thus this test also verifies that the Flock_t structure can be | ||||||
|  | // roundtripped with F_SETLK and F_GETLK. | ||||||
| func TestFcntlFlock(t *testing.T) { | func TestFcntlFlock(t *testing.T) { | ||||||
| 	name := filepath.Join(os.TempDir(), "TestFcntlFlock") |  | ||||||
| 	fd, err := syscall.Open(name, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatalf("Open failed: %v", err) |  | ||||||
| 	} |  | ||||||
| 	defer syscall.Unlink(name) |  | ||||||
| 	defer syscall.Close(fd) |  | ||||||
| 	flock := syscall.Flock_t{ | 	flock := syscall.Flock_t{ | ||||||
| 		Type:  syscall.F_RDLCK, | 		Type:  syscall.F_WRLCK, | ||||||
| 		Start: 0, Len: 0, Whence: 1, | 		Start: 31415, Len: 271828, Whence: 1, | ||||||
| 	} | 	} | ||||||
| 	if err := syscall.FcntlFlock(uintptr(fd), syscall.F_GETLK, &flock); err != nil { | 	if os.Getenv("GO_WANT_HELPER_PROCESS") == "" { | ||||||
| 		t.Fatalf("FcntlFlock failed: %v", err) | 		// parent | ||||||
|  | 		name := filepath.Join(os.TempDir(), "TestFcntlFlock") | ||||||
|  | 		fd, err := syscall.Open(name, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0) | ||||||
|  | 		if err != nil { | ||||||
|  | 			t.Fatalf("Open failed: %v", err) | ||||||
|  | 		} | ||||||
|  | 		defer syscall.Unlink(name) | ||||||
|  | 		defer syscall.Close(fd) | ||||||
|  | 		if err := syscall.Ftruncate(fd, 1<<20); err != nil { | ||||||
|  | 			t.Fatalf("Ftruncate(1<<20) failed: %v", err) | ||||||
|  | 		} | ||||||
|  | 		if err := syscall.FcntlFlock(uintptr(fd), syscall.F_SETLK, &flock); err != nil { | ||||||
|  | 			t.Fatalf("FcntlFlock(F_SETLK) failed: %v", err) | ||||||
|  | 		} | ||||||
|  | 		cmd := exec.Command(os.Args[0], "-test.run=^TestFcntlFlock$") | ||||||
|  | 		cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") | ||||||
|  | 		cmd.ExtraFiles = []*os.File{os.NewFile(uintptr(fd), name)} | ||||||
|  | 		out, err := cmd.CombinedOutput() | ||||||
|  | 		if len(out) > 0 || err != nil { | ||||||
|  | 			t.Fatalf("child process: %q, %v", out, err) | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 		// child | ||||||
|  | 		got := flock | ||||||
|  | 		// make sure the child lock is conflicting with the parent lock | ||||||
|  | 		got.Start-- | ||||||
|  | 		got.Len++ | ||||||
|  | 		if err := syscall.FcntlFlock(3, syscall.F_GETLK, &got); err != nil { | ||||||
|  | 			t.Fatalf("FcntlFlock(F_GETLK) failed: %v", err) | ||||||
|  | 		} | ||||||
|  | 		flock.Pid = int32(syscall.Getppid()) | ||||||
|  | 		// Linux kernel always set Whence to 0 | ||||||
|  | 		flock.Whence = 0 | ||||||
|  | 		if got.Type == flock.Type && got.Start == flock.Start && got.Len == flock.Len && got.Pid == flock.Pid && got.Whence == flock.Whence { | ||||||
|  | 			os.Exit(0) | ||||||
|  | 		} | ||||||
|  | 		t.Fatalf("FcntlFlock got %v, want %v", got, flock) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -121,7 +156,7 @@ func TestPassFD(t *testing.T) { | ||||||
| 	defer readFile.Close() | 	defer readFile.Close() | ||||||
| 
 | 
 | ||||||
| 	cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir) | 	cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir) | ||||||
| 	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} | 	cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") | ||||||
| 	cmd.ExtraFiles = []*os.File{writeFile} | 	cmd.ExtraFiles = []*os.File{writeFile} | ||||||
| 
 | 
 | ||||||
| 	out, err := cmd.CombinedOutput() | 	out, err := cmd.CombinedOutput() | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Shenghou Ma
						Shenghou Ma