-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
serial_console.go
144 lines (124 loc) · 4.59 KB
/
serial_console.go
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
package vz
/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization_11.h"
*/
import "C"
import (
"os"
"github.com/Code-Hex/vz/v3/internal/objc"
)
// SerialPortAttachment interface for a serial port attachment.
//
// A serial port attachment defines how the virtual machine's serial port interfaces with the host system.
type SerialPortAttachment interface {
objc.NSObject
serialPortAttachment()
}
type baseSerialPortAttachment struct{}
func (*baseSerialPortAttachment) serialPortAttachment() {}
var _ SerialPortAttachment = (*FileHandleSerialPortAttachment)(nil)
// FileHandleSerialPortAttachment defines a serial port attachment from a file handle.
//
// Data written to fileHandleForReading goes to the guest. Data sent from the guest appears on fileHandleForWriting.
// see: https://developer.apple.com/documentation/virtualization/vzfilehandleserialportattachment?language=objc
type FileHandleSerialPortAttachment struct {
*pointer
*baseSerialPortAttachment
}
// NewFileHandleSerialPortAttachment initialize the FileHandleSerialPortAttachment from file handles.
//
// read parameter is an *os.File for reading from the file.
// write parameter is an *os.File for writing to the file.
//
// This is only supported on macOS 11 and newer, error will
// be returned on older versions.
func NewFileHandleSerialPortAttachment(read, write *os.File) (*FileHandleSerialPortAttachment, error) {
if err := macOSAvailable(11); err != nil {
return nil, err
}
attachment := &FileHandleSerialPortAttachment{
pointer: objc.NewPointer(
C.newVZFileHandleSerialPortAttachment(
C.int(read.Fd()),
C.int(write.Fd()),
),
),
}
objc.SetFinalizer(attachment, func(self *FileHandleSerialPortAttachment) {
objc.Release(self)
})
return attachment, nil
}
var _ SerialPortAttachment = (*FileSerialPortAttachment)(nil)
// FileSerialPortAttachment defines a serial port attachment from a file.
//
// Any data sent by the guest on the serial interface is written to the file.
// No data is sent to the guest over serial with this attachment.
// see: https://developer.apple.com/documentation/virtualization/vzfileserialportattachment?language=objc
type FileSerialPortAttachment struct {
*pointer
*baseSerialPortAttachment
}
// NewFileSerialPortAttachment initialize the FileSerialPortAttachment from a path of a file.
// If error is not nil, used to report errors if intialization fails.
//
// - path of the file for the attachment on the local file system.
// - shouldAppend True if the file should be opened in append mode, false otherwise.
// When a file is opened in append mode, writing to that file will append to the end of it.
//
// This is only supported on macOS 11 and newer, error will
// be returned on older versions.
func NewFileSerialPortAttachment(path string, shouldAppend bool) (*FileSerialPortAttachment, error) {
if err := macOSAvailable(11); err != nil {
return nil, err
}
cpath := charWithGoString(path)
defer cpath.Free()
nserrPtr := newNSErrorAsNil()
attachment := &FileSerialPortAttachment{
pointer: objc.NewPointer(
C.newVZFileSerialPortAttachment(
cpath.CString(),
C.bool(shouldAppend),
&nserrPtr,
),
),
}
if err := newNSError(nserrPtr); err != nil {
return nil, err
}
objc.SetFinalizer(attachment, func(self *FileSerialPortAttachment) {
objc.Release(self)
})
return attachment, nil
}
// VirtioConsoleDeviceSerialPortConfiguration represents Virtio Console Serial Port Device.
//
// The device creates a console which enables communication between the host and the guest through the Virtio interface.
// The device sets up a single port on the Virtio console device.
// see: https://developer.apple.com/documentation/virtualization/vzvirtioconsoledeviceserialportconfiguration?language=objc
type VirtioConsoleDeviceSerialPortConfiguration struct {
*pointer
}
// NewVirtioConsoleDeviceSerialPortConfiguration creates a new NewVirtioConsoleDeviceSerialPortConfiguration.
//
// This is only supported on macOS 11 and newer, error will
// be returned on older versions.
func NewVirtioConsoleDeviceSerialPortConfiguration(attachment SerialPortAttachment) (*VirtioConsoleDeviceSerialPortConfiguration, error) {
if err := macOSAvailable(11); err != nil {
return nil, err
}
config := &VirtioConsoleDeviceSerialPortConfiguration{
pointer: objc.NewPointer(
C.newVZVirtioConsoleDeviceSerialPortConfiguration(
objc.Ptr(attachment),
),
),
}
objc.SetFinalizer(config, func(self *VirtioConsoleDeviceSerialPortConfiguration) {
objc.Release(self)
})
return config, nil
}