Skip to content

Commit 9d4e42a

Browse files
Riku Voipiobradfitz
authored andcommitted
x/sys/unix: add Dup2 wrapper for arm64/ppc64
arm64 doesn't have a Dup2 syscall, instead Dup3 is supposed to be used. Since Dup3 is linux-specific, provide a wrapper to make writing portable code easier. Updates golang/go#10235 To verify it, added a testcase for Dup and Dup2. Change-Id: I066bb60d62b2bd64d7ba0fdfbb334ce2213c78e9 Reviewed-on: https://go-review.googlesource.com/20178 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Riku Voipio <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 7a56174 commit 9d4e42a

5 files changed

Lines changed: 60 additions & 0 deletions

File tree

unix/syscall_linux_arm64.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ func InotifyInit() (fd int, err error) {
152152
return InotifyInit1(0)
153153
}
154154

155+
func Dup2(oldfd int, newfd int) (err error) {
156+
return Dup3(oldfd, newfd, 0)
157+
}
158+
155159
// TODO(dfc): constants that should be in zsysnum_linux_arm64.go, remove
156160
// these when the deprecated syscalls that the syscall package relies on
157161
// are removed.

unix/syscall_linux_ppc64x.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package unix
99

10+
//sys Dup2(oldfd int, newfd int) (err error)
1011
//sys Fchown(fd int, uid int, gid int) (err error)
1112
//sys Fstat(fd int, stat *Stat_t) (err error)
1213
//sys Fstatfs(fd int, buf *Statfs_t) (err error)

unix/syscall_unix_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,38 @@ func TestSeekFailure(t *testing.T) {
316316
t.Fatalf("Seek(-1, 0, 0) return error with empty message")
317317
}
318318
}
319+
320+
func TestDup(t *testing.T) {
321+
file, err := ioutil.TempFile("", "TestDup")
322+
if err != nil {
323+
t.Fatalf("Tempfile failed: %v", err)
324+
}
325+
defer os.Remove(file.Name())
326+
defer file.Close()
327+
f := int(file.Fd())
328+
329+
newFd, err := unix.Dup(f)
330+
if err != nil {
331+
t.Fatalf("Dup: %v", err)
332+
}
333+
334+
err = unix.Dup2(newFd, newFd+1)
335+
if err != nil {
336+
t.Fatalf("Dup2: %v", err)
337+
}
338+
339+
b1 := []byte("Test123")
340+
b2 := make([]byte, 7)
341+
_, err = unix.Write(newFd+1, b1)
342+
if err != nil {
343+
t.Fatalf("Write to dup2 fd failed: %v", err)
344+
}
345+
_, err = unix.Seek(f, 0, 0)
346+
_, err = unix.Read(f, b2)
347+
if err != nil {
348+
t.Fatalf("Read back failed: %v", err)
349+
}
350+
if string(b1) != string(b2) {
351+
t.Errorf("Dup: stdout write not in file, expected %v, got %v", string(b1), string(b2))
352+
}
353+
}

unix/zsyscall_linux_ppc64.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,16 @@ func Munlockall() (err error) {
12161216

12171217
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
12181218

1219+
func Dup2(oldfd int, newfd int) (err error) {
1220+
_, _, e1 := Syscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
1221+
if e1 != 0 {
1222+
err = errnoErr(e1)
1223+
}
1224+
return
1225+
}
1226+
1227+
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
1228+
12191229
func Fchown(fd int, uid int, gid int) (err error) {
12201230
_, _, e1 := Syscall(SYS_FCHOWN, uintptr(fd), uintptr(uid), uintptr(gid))
12211231
if e1 != 0 {

unix/zsyscall_linux_ppc64le.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,16 @@ func Munlockall() (err error) {
12161216

12171217
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
12181218

1219+
func Dup2(oldfd int, newfd int) (err error) {
1220+
_, _, e1 := Syscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
1221+
if e1 != 0 {
1222+
err = errnoErr(e1)
1223+
}
1224+
return
1225+
}
1226+
1227+
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
1228+
12191229
func Fchown(fd int, uid int, gid int) (err error) {
12201230
_, _, e1 := Syscall(SYS_FCHOWN, uintptr(fd), uintptr(uid), uintptr(gid))
12211231
if e1 != 0 {

0 commit comments

Comments
 (0)