forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.go
123 lines (107 loc) · 4.02 KB
/
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
package vz
/*
#cgo darwin CFLAGS: -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization.h"
*/
import "C"
import (
"os"
"runtime"
)
// 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 {
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 intialize 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.
func NewFileHandleSerialPortAttachment(read, write *os.File) *FileHandleSerialPortAttachment {
attachment := &FileHandleSerialPortAttachment{
pointer: pointer{
ptr: C.newVZFileHandleSerialPortAttachment(
C.int(read.Fd()),
C.int(write.Fd()),
),
},
}
runtime.SetFinalizer(attachment, func(self *FileHandleSerialPortAttachment) {
self.Release()
})
return attachment
}
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.
func NewFileSerialPortAttachment(path string, shouldAppend bool) (*FileSerialPortAttachment, error) {
cpath := charWithGoString(path)
defer cpath.Free()
nserr := newNSErrorAsNil()
nserrPtr := nserr.Ptr()
attachment := &FileSerialPortAttachment{
pointer: pointer{
ptr: C.newVZFileSerialPortAttachment(
cpath.CString(),
C.bool(shouldAppend),
&nserrPtr,
),
},
}
if err := newNSError(nserrPtr); err != nil {
return nil, err
}
runtime.SetFinalizer(attachment, func(self *FileSerialPortAttachment) {
self.Release()
})
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.
func NewVirtioConsoleDeviceSerialPortConfiguration(attachment SerialPortAttachment) *VirtioConsoleDeviceSerialPortConfiguration {
config := &VirtioConsoleDeviceSerialPortConfiguration{
pointer: pointer{
ptr: C.newVZVirtioConsoleDeviceSerialPortConfiguration(
attachment.Ptr(),
),
},
}
runtime.SetFinalizer(config, func(self *VirtioConsoleDeviceSerialPortConfiguration) {
self.Release()
})
return config
}