Skip to content

Commit 413d286

Browse files
Merge pull request #50 from samuelkarp/freebsd
add cgo-based FreeBSD support
2 parents 65c9061 + 6ebf471 commit 413d286

File tree

7 files changed

+177
-5
lines changed

7 files changed

+177
-5
lines changed

console_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux solaris
1+
// +build linux solaris freebsd
22

33
/*
44
Copyright The containerd Authors.

console_unix.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919
package console
2020

2121
import (
22-
"os"
23-
2422
"golang.org/x/sys/unix"
2523
)
2624

2725
// NewPty creates a new pty pair
2826
// The master is returned as the first console and a string
2927
// with the path to the pty slave is returned as the second
3028
func NewPty() (Console, string, error) {
31-
f, err := os.OpenFile("/dev/ptmx", unix.O_RDWR|unix.O_NOCTTY|unix.O_CLOEXEC, 0)
29+
f, err := openpt()
3230
if err != nil {
3331
return nil, "", err
3432
}

pty_freebsd_cgo.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build freebsd,cgo
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package console
20+
21+
import (
22+
"fmt"
23+
"os"
24+
)
25+
26+
/*
27+
#include <fcntl.h>
28+
#include <stdlib.h>
29+
#include <unistd.h>
30+
*/
31+
import "C"
32+
33+
// openpt allocates a new pseudo-terminal and establishes a connection with its
34+
// control device.
35+
func openpt() (*os.File, error) {
36+
fd, err := C.posix_openpt(C.O_RDWR)
37+
if err != nil {
38+
return nil, fmt.Errorf("posix_openpt: %w", err)
39+
}
40+
if _, err := C.grantpt(fd); err != nil {
41+
C.close(fd)
42+
return nil, fmt.Errorf("grantpt: %w", err)
43+
}
44+
return os.NewFile(uintptr(fd), ""), nil
45+
}

pty_freebsd_nocgo.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// +build freebsd,!cgo
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package console
20+
21+
//
22+
// Implementing the functions below requires cgo support. Non-cgo stubs
23+
// versions are defined below to enable cross-compilation of source code
24+
// that depends on these functions, but the resultant cross-compiled
25+
// binaries cannot actually be used. If the stub function(s) below are
26+
// actually invoked they will display an error message and cause the
27+
// calling process to exit.
28+
//
29+
30+
func openpt() (*os.File, error) {
31+
panic("openpt() support requires cgo.")
32+
}

pty_unix.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// +build darwin linux netbsd openbsd solaris
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package console
20+
21+
import (
22+
"os"
23+
24+
"golang.org/x/sys/unix"
25+
)
26+
27+
// openpt allocates a new pseudo-terminal by opening the /dev/ptmx device
28+
func openpt() (*os.File, error) {
29+
return os.OpenFile("/dev/ptmx", unix.O_RDWR|unix.O_NOCTTY|unix.O_CLOEXEC, 0)
30+
}

tc_freebsd.go renamed to tc_freebsd_cgo.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build freebsd,cgo
2+
13
/*
24
Copyright The containerd Authors.
35
@@ -23,15 +25,25 @@ import (
2325
"golang.org/x/sys/unix"
2426
)
2527

28+
/*
29+
#include <stdlib.h>
30+
#include <unistd.h>
31+
*/
32+
import "C"
33+
2634
const (
2735
cmdTcGet = unix.TIOCGETA
2836
cmdTcSet = unix.TIOCSETA
2937
)
3038

3139
// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
3240
// unlockpt should be called before opening the slave side of a pty.
33-
// This does not exist on FreeBSD, it does not allocate controlling terminals on open
3441
func unlockpt(f *os.File) error {
42+
fd := C.int(f.Fd())
43+
if _, err := C.unlockpt(fd); err != nil {
44+
C.close(fd)
45+
return fmt.Errorf("unlockpt: %w", err)
46+
}
3547
return nil
3648
}
3749

tc_freebsd_nocgo.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// +build freebsd,!cgo
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package console
20+
21+
import (
22+
"fmt"
23+
"os"
24+
25+
"golang.org/x/sys/unix"
26+
)
27+
28+
const (
29+
cmdTcGet = unix.TIOCGETA
30+
cmdTcSet = unix.TIOCSETA
31+
)
32+
33+
//
34+
// Implementing the functions below requires cgo support. Non-cgo stubs
35+
// versions are defined below to enable cross-compilation of source code
36+
// that depends on these functions, but the resultant cross-compiled
37+
// binaries cannot actually be used. If the stub function(s) below are
38+
// actually invoked they will display an error message and cause the
39+
// calling process to exit.
40+
//
41+
42+
// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
43+
// unlockpt should be called before opening the slave side of a pty.
44+
func unlockpt(f *os.File) error {
45+
panic("unlockpt() support requires cgo.")
46+
}
47+
48+
// ptsname retrieves the name of the first available pts for the given master.
49+
func ptsname(f *os.File) (string, error) {
50+
n, err := unix.IoctlGetInt(int(f.Fd()), unix.TIOCGPTN)
51+
if err != nil {
52+
return "", err
53+
}
54+
return fmt.Sprintf("/dev/pts/%d", n), nil
55+
}

0 commit comments

Comments
 (0)