|
1 // Copyright 2009 The Go Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style |
|
3 // license that can be found in the LICENSE file. |
|
4 |
|
5 // Windows system calls. |
|
6 |
|
7 package windows |
|
8 |
|
9 import ( |
|
10 errorspkg "errors" |
|
11 "fmt" |
|
12 "runtime" |
|
13 "strings" |
|
14 "sync" |
|
15 "syscall" |
|
16 "time" |
|
17 "unicode/utf16" |
|
18 "unsafe" |
|
19 |
|
20 "golang.org/x/sys/internal/unsafeheader" |
|
21 ) |
|
22 |
|
23 type Handle uintptr |
|
24 type HWND uintptr |
|
25 |
|
26 const ( |
|
27 InvalidHandle = ^Handle(0) |
|
28 InvalidHWND = ^HWND(0) |
|
29 |
|
30 // Flags for DefineDosDevice. |
|
31 DDD_EXACT_MATCH_ON_REMOVE = 0x00000004 |
|
32 DDD_NO_BROADCAST_SYSTEM = 0x00000008 |
|
33 DDD_RAW_TARGET_PATH = 0x00000001 |
|
34 DDD_REMOVE_DEFINITION = 0x00000002 |
|
35 |
|
36 // Return values for GetDriveType. |
|
37 DRIVE_UNKNOWN = 0 |
|
38 DRIVE_NO_ROOT_DIR = 1 |
|
39 DRIVE_REMOVABLE = 2 |
|
40 DRIVE_FIXED = 3 |
|
41 DRIVE_REMOTE = 4 |
|
42 DRIVE_CDROM = 5 |
|
43 DRIVE_RAMDISK = 6 |
|
44 |
|
45 // File system flags from GetVolumeInformation and GetVolumeInformationByHandle. |
|
46 FILE_CASE_SENSITIVE_SEARCH = 0x00000001 |
|
47 FILE_CASE_PRESERVED_NAMES = 0x00000002 |
|
48 FILE_FILE_COMPRESSION = 0x00000010 |
|
49 FILE_DAX_VOLUME = 0x20000000 |
|
50 FILE_NAMED_STREAMS = 0x00040000 |
|
51 FILE_PERSISTENT_ACLS = 0x00000008 |
|
52 FILE_READ_ONLY_VOLUME = 0x00080000 |
|
53 FILE_SEQUENTIAL_WRITE_ONCE = 0x00100000 |
|
54 FILE_SUPPORTS_ENCRYPTION = 0x00020000 |
|
55 FILE_SUPPORTS_EXTENDED_ATTRIBUTES = 0x00800000 |
|
56 FILE_SUPPORTS_HARD_LINKS = 0x00400000 |
|
57 FILE_SUPPORTS_OBJECT_IDS = 0x00010000 |
|
58 FILE_SUPPORTS_OPEN_BY_FILE_ID = 0x01000000 |
|
59 FILE_SUPPORTS_REPARSE_POINTS = 0x00000080 |
|
60 FILE_SUPPORTS_SPARSE_FILES = 0x00000040 |
|
61 FILE_SUPPORTS_TRANSACTIONS = 0x00200000 |
|
62 FILE_SUPPORTS_USN_JOURNAL = 0x02000000 |
|
63 FILE_UNICODE_ON_DISK = 0x00000004 |
|
64 FILE_VOLUME_IS_COMPRESSED = 0x00008000 |
|
65 FILE_VOLUME_QUOTAS = 0x00000020 |
|
66 |
|
67 // Flags for LockFileEx. |
|
68 LOCKFILE_FAIL_IMMEDIATELY = 0x00000001 |
|
69 LOCKFILE_EXCLUSIVE_LOCK = 0x00000002 |
|
70 |
|
71 // Return value of SleepEx and other APC functions |
|
72 WAIT_IO_COMPLETION = 0x000000C0 |
|
73 ) |
|
74 |
|
75 // StringToUTF16 is deprecated. Use UTF16FromString instead. |
|
76 // If s contains a NUL byte this function panics instead of |
|
77 // returning an error. |
|
78 func StringToUTF16(s string) []uint16 { |
|
79 a, err := UTF16FromString(s) |
|
80 if err != nil { |
|
81 panic("windows: string with NUL passed to StringToUTF16") |
|
82 } |
|
83 return a |
|
84 } |
|
85 |
|
86 // UTF16FromString returns the UTF-16 encoding of the UTF-8 string |
|
87 // s, with a terminating NUL added. If s contains a NUL byte at any |
|
88 // location, it returns (nil, syscall.EINVAL). |
|
89 func UTF16FromString(s string) ([]uint16, error) { |
|
90 if strings.IndexByte(s, 0) != -1 { |
|
91 return nil, syscall.EINVAL |
|
92 } |
|
93 return utf16.Encode([]rune(s + "\x00")), nil |
|
94 } |
|
95 |
|
96 // UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, |
|
97 // with a terminating NUL and any bytes after the NUL removed. |
|
98 func UTF16ToString(s []uint16) string { |
|
99 for i, v := range s { |
|
100 if v == 0 { |
|
101 s = s[:i] |
|
102 break |
|
103 } |
|
104 } |
|
105 return string(utf16.Decode(s)) |
|
106 } |
|
107 |
|
108 // StringToUTF16Ptr is deprecated. Use UTF16PtrFromString instead. |
|
109 // If s contains a NUL byte this function panics instead of |
|
110 // returning an error. |
|
111 func StringToUTF16Ptr(s string) *uint16 { return &StringToUTF16(s)[0] } |
|
112 |
|
113 // UTF16PtrFromString returns pointer to the UTF-16 encoding of |
|
114 // the UTF-8 string s, with a terminating NUL added. If s |
|
115 // contains a NUL byte at any location, it returns (nil, syscall.EINVAL). |
|
116 func UTF16PtrFromString(s string) (*uint16, error) { |
|
117 a, err := UTF16FromString(s) |
|
118 if err != nil { |
|
119 return nil, err |
|
120 } |
|
121 return &a[0], nil |
|
122 } |
|
123 |
|
124 // UTF16PtrToString takes a pointer to a UTF-16 sequence and returns the corresponding UTF-8 encoded string. |
|
125 // If the pointer is nil, it returns the empty string. It assumes that the UTF-16 sequence is terminated |
|
126 // at a zero word; if the zero word is not present, the program may crash. |
|
127 func UTF16PtrToString(p *uint16) string { |
|
128 if p == nil { |
|
129 return "" |
|
130 } |
|
131 if *p == 0 { |
|
132 return "" |
|
133 } |
|
134 |
|
135 // Find NUL terminator. |
|
136 n := 0 |
|
137 for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; n++ { |
|
138 ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p)) |
|
139 } |
|
140 |
|
141 return string(utf16.Decode(unsafe.Slice(p, n))) |
|
142 } |
|
143 |
|
144 func Getpagesize() int { return 4096 } |
|
145 |
|
146 // NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention. |
|
147 // This is useful when interoperating with Windows code requiring callbacks. |
|
148 // The argument is expected to be a function with with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr. |
|
149 func NewCallback(fn interface{}) uintptr { |
|
150 return syscall.NewCallback(fn) |
|
151 } |
|
152 |
|
153 // NewCallbackCDecl converts a Go function to a function pointer conforming to the cdecl calling convention. |
|
154 // This is useful when interoperating with Windows code requiring callbacks. |
|
155 // The argument is expected to be a function with with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr. |
|
156 func NewCallbackCDecl(fn interface{}) uintptr { |
|
157 return syscall.NewCallbackCDecl(fn) |
|
158 } |
|
159 |
|
160 // windows api calls |
|
161 |
|
162 //sys GetLastError() (lasterr error) |
|
163 //sys LoadLibrary(libname string) (handle Handle, err error) = LoadLibraryW |
|
164 //sys LoadLibraryEx(libname string, zero Handle, flags uintptr) (handle Handle, err error) = LoadLibraryExW |
|
165 //sys FreeLibrary(handle Handle) (err error) |
|
166 //sys GetProcAddress(module Handle, procname string) (proc uintptr, err error) |
|
167 //sys GetModuleFileName(module Handle, filename *uint16, size uint32) (n uint32, err error) = kernel32.GetModuleFileNameW |
|
168 //sys GetModuleHandleEx(flags uint32, moduleName *uint16, module *Handle) (err error) = kernel32.GetModuleHandleExW |
|
169 //sys SetDefaultDllDirectories(directoryFlags uint32) (err error) |
|
170 //sys SetDllDirectory(path string) (err error) = kernel32.SetDllDirectoryW |
|
171 //sys GetVersion() (ver uint32, err error) |
|
172 //sys FormatMessage(flags uint32, msgsrc uintptr, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) = FormatMessageW |
|
173 //sys ExitProcess(exitcode uint32) |
|
174 //sys IsWow64Process(handle Handle, isWow64 *bool) (err error) = IsWow64Process |
|
175 //sys IsWow64Process2(handle Handle, processMachine *uint16, nativeMachine *uint16) (err error) = IsWow64Process2? |
|
176 //sys CreateFile(name *uint16, access uint32, mode uint32, sa *SecurityAttributes, createmode uint32, attrs uint32, templatefile Handle) (handle Handle, err error) [failretval==InvalidHandle] = CreateFileW |
|
177 //sys CreateNamedPipe(name *uint16, flags uint32, pipeMode uint32, maxInstances uint32, outSize uint32, inSize uint32, defaultTimeout uint32, sa *SecurityAttributes) (handle Handle, err error) [failretval==InvalidHandle] = CreateNamedPipeW |
|
178 //sys ConnectNamedPipe(pipe Handle, overlapped *Overlapped) (err error) |
|
179 //sys GetNamedPipeInfo(pipe Handle, flags *uint32, outSize *uint32, inSize *uint32, maxInstances *uint32) (err error) |
|
180 //sys GetNamedPipeHandleState(pipe Handle, state *uint32, curInstances *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32, userName *uint16, maxUserNameSize uint32) (err error) = GetNamedPipeHandleStateW |
|
181 //sys SetNamedPipeHandleState(pipe Handle, state *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32) (err error) = SetNamedPipeHandleState |
|
182 //sys readFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error) = ReadFile |
|
183 //sys writeFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error) = WriteFile |
|
184 //sys GetOverlappedResult(handle Handle, overlapped *Overlapped, done *uint32, wait bool) (err error) |
|
185 //sys SetFilePointer(handle Handle, lowoffset int32, highoffsetptr *int32, whence uint32) (newlowoffset uint32, err error) [failretval==0xffffffff] |
|
186 //sys CloseHandle(handle Handle) (err error) |
|
187 //sys GetStdHandle(stdhandle uint32) (handle Handle, err error) [failretval==InvalidHandle] |
|
188 //sys SetStdHandle(stdhandle uint32, handle Handle) (err error) |
|
189 //sys findFirstFile1(name *uint16, data *win32finddata1) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstFileW |
|
190 //sys findNextFile1(handle Handle, data *win32finddata1) (err error) = FindNextFileW |
|
191 //sys FindClose(handle Handle) (err error) |
|
192 //sys GetFileInformationByHandle(handle Handle, data *ByHandleFileInformation) (err error) |
|
193 //sys GetFileInformationByHandleEx(handle Handle, class uint32, outBuffer *byte, outBufferLen uint32) (err error) |
|
194 //sys SetFileInformationByHandle(handle Handle, class uint32, inBuffer *byte, inBufferLen uint32) (err error) |
|
195 //sys GetCurrentDirectory(buflen uint32, buf *uint16) (n uint32, err error) = GetCurrentDirectoryW |
|
196 //sys SetCurrentDirectory(path *uint16) (err error) = SetCurrentDirectoryW |
|
197 //sys CreateDirectory(path *uint16, sa *SecurityAttributes) (err error) = CreateDirectoryW |
|
198 //sys RemoveDirectory(path *uint16) (err error) = RemoveDirectoryW |
|
199 //sys DeleteFile(path *uint16) (err error) = DeleteFileW |
|
200 //sys MoveFile(from *uint16, to *uint16) (err error) = MoveFileW |
|
201 //sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW |
|
202 //sys LockFileEx(file Handle, flags uint32, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *Overlapped) (err error) |
|
203 //sys UnlockFileEx(file Handle, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *Overlapped) (err error) |
|
204 //sys GetComputerName(buf *uint16, n *uint32) (err error) = GetComputerNameW |
|
205 //sys GetComputerNameEx(nametype uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW |
|
206 //sys SetEndOfFile(handle Handle) (err error) |
|
207 //sys GetSystemTimeAsFileTime(time *Filetime) |
|
208 //sys GetSystemTimePreciseAsFileTime(time *Filetime) |
|
209 //sys GetTimeZoneInformation(tzi *Timezoneinformation) (rc uint32, err error) [failretval==0xffffffff] |
|
210 //sys CreateIoCompletionPort(filehandle Handle, cphandle Handle, key uintptr, threadcnt uint32) (handle Handle, err error) |
|
211 //sys GetQueuedCompletionStatus(cphandle Handle, qty *uint32, key *uintptr, overlapped **Overlapped, timeout uint32) (err error) |
|
212 //sys PostQueuedCompletionStatus(cphandle Handle, qty uint32, key uintptr, overlapped *Overlapped) (err error) |
|
213 //sys CancelIo(s Handle) (err error) |
|
214 //sys CancelIoEx(s Handle, o *Overlapped) (err error) |
|
215 //sys CreateProcess(appName *uint16, commandLine *uint16, procSecurity *SecurityAttributes, threadSecurity *SecurityAttributes, inheritHandles bool, creationFlags uint32, env *uint16, currentDir *uint16, startupInfo *StartupInfo, outProcInfo *ProcessInformation) (err error) = CreateProcessW |
|
216 //sys CreateProcessAsUser(token Token, appName *uint16, commandLine *uint16, procSecurity *SecurityAttributes, threadSecurity *SecurityAttributes, inheritHandles bool, creationFlags uint32, env *uint16, currentDir *uint16, startupInfo *StartupInfo, outProcInfo *ProcessInformation) (err error) = advapi32.CreateProcessAsUserW |
|
217 //sys initializeProcThreadAttributeList(attrlist *ProcThreadAttributeList, attrcount uint32, flags uint32, size *uintptr) (err error) = InitializeProcThreadAttributeList |
|
218 //sys deleteProcThreadAttributeList(attrlist *ProcThreadAttributeList) = DeleteProcThreadAttributeList |
|
219 //sys updateProcThreadAttribute(attrlist *ProcThreadAttributeList, flags uint32, attr uintptr, value unsafe.Pointer, size uintptr, prevvalue unsafe.Pointer, returnedsize *uintptr) (err error) = UpdateProcThreadAttribute |
|
220 //sys OpenProcess(desiredAccess uint32, inheritHandle bool, processId uint32) (handle Handle, err error) |
|
221 //sys ShellExecute(hwnd Handle, verb *uint16, file *uint16, args *uint16, cwd *uint16, showCmd int32) (err error) [failretval<=32] = shell32.ShellExecuteW |
|
222 //sys GetWindowThreadProcessId(hwnd HWND, pid *uint32) (tid uint32, err error) = user32.GetWindowThreadProcessId |
|
223 //sys GetShellWindow() (shellWindow HWND) = user32.GetShellWindow |
|
224 //sys MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret int32, err error) [failretval==0] = user32.MessageBoxW |
|
225 //sys ExitWindowsEx(flags uint32, reason uint32) (err error) = user32.ExitWindowsEx |
|
226 //sys shGetKnownFolderPath(id *KNOWNFOLDERID, flags uint32, token Token, path **uint16) (ret error) = shell32.SHGetKnownFolderPath |
|
227 //sys TerminateProcess(handle Handle, exitcode uint32) (err error) |
|
228 //sys GetExitCodeProcess(handle Handle, exitcode *uint32) (err error) |
|
229 //sys GetStartupInfo(startupInfo *StartupInfo) (err error) = GetStartupInfoW |
|
230 //sys GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error) |
|
231 //sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error) |
|
232 //sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff] |
|
233 //sys waitForMultipleObjects(count uint32, handles uintptr, waitAll bool, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff] = WaitForMultipleObjects |
|
234 //sys GetTempPath(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPathW |
|
235 //sys CreatePipe(readhandle *Handle, writehandle *Handle, sa *SecurityAttributes, size uint32) (err error) |
|
236 //sys GetFileType(filehandle Handle) (n uint32, err error) |
|
237 //sys CryptAcquireContext(provhandle *Handle, container *uint16, provider *uint16, provtype uint32, flags uint32) (err error) = advapi32.CryptAcquireContextW |
|
238 //sys CryptReleaseContext(provhandle Handle, flags uint32) (err error) = advapi32.CryptReleaseContext |
|
239 //sys CryptGenRandom(provhandle Handle, buflen uint32, buf *byte) (err error) = advapi32.CryptGenRandom |
|
240 //sys GetEnvironmentStrings() (envs *uint16, err error) [failretval==nil] = kernel32.GetEnvironmentStringsW |
|
241 //sys FreeEnvironmentStrings(envs *uint16) (err error) = kernel32.FreeEnvironmentStringsW |
|
242 //sys GetEnvironmentVariable(name *uint16, buffer *uint16, size uint32) (n uint32, err error) = kernel32.GetEnvironmentVariableW |
|
243 //sys SetEnvironmentVariable(name *uint16, value *uint16) (err error) = kernel32.SetEnvironmentVariableW |
|
244 //sys ExpandEnvironmentStrings(src *uint16, dst *uint16, size uint32) (n uint32, err error) = kernel32.ExpandEnvironmentStringsW |
|
245 //sys CreateEnvironmentBlock(block **uint16, token Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock |
|
246 //sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock |
|
247 //sys getTickCount64() (ms uint64) = kernel32.GetTickCount64 |
|
248 //sys SetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error) |
|
249 //sys GetFileAttributes(name *uint16) (attrs uint32, err error) [failretval==INVALID_FILE_ATTRIBUTES] = kernel32.GetFileAttributesW |
|
250 //sys SetFileAttributes(name *uint16, attrs uint32) (err error) = kernel32.SetFileAttributesW |
|
251 //sys GetFileAttributesEx(name *uint16, level uint32, info *byte) (err error) = kernel32.GetFileAttributesExW |
|
252 //sys GetCommandLine() (cmd *uint16) = kernel32.GetCommandLineW |
|
253 //sys CommandLineToArgv(cmd *uint16, argc *int32) (argv *[8192]*[8192]uint16, err error) [failretval==nil] = shell32.CommandLineToArgvW |
|
254 //sys LocalFree(hmem Handle) (handle Handle, err error) [failretval!=0] |
|
255 //sys LocalAlloc(flags uint32, length uint32) (ptr uintptr, err error) |
|
256 //sys SetHandleInformation(handle Handle, mask uint32, flags uint32) (err error) |
|
257 //sys FlushFileBuffers(handle Handle) (err error) |
|
258 //sys GetFullPathName(path *uint16, buflen uint32, buf *uint16, fname **uint16) (n uint32, err error) = kernel32.GetFullPathNameW |
|
259 //sys GetLongPathName(path *uint16, buf *uint16, buflen uint32) (n uint32, err error) = kernel32.GetLongPathNameW |
|
260 //sys GetShortPathName(longpath *uint16, shortpath *uint16, buflen uint32) (n uint32, err error) = kernel32.GetShortPathNameW |
|
261 //sys GetFinalPathNameByHandle(file Handle, filePath *uint16, filePathSize uint32, flags uint32) (n uint32, err error) = kernel32.GetFinalPathNameByHandleW |
|
262 //sys CreateFileMapping(fhandle Handle, sa *SecurityAttributes, prot uint32, maxSizeHigh uint32, maxSizeLow uint32, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateFileMappingW |
|
263 //sys MapViewOfFile(handle Handle, access uint32, offsetHigh uint32, offsetLow uint32, length uintptr) (addr uintptr, err error) |
|
264 //sys UnmapViewOfFile(addr uintptr) (err error) |
|
265 //sys FlushViewOfFile(addr uintptr, length uintptr) (err error) |
|
266 //sys VirtualLock(addr uintptr, length uintptr) (err error) |
|
267 //sys VirtualUnlock(addr uintptr, length uintptr) (err error) |
|
268 //sys VirtualAlloc(address uintptr, size uintptr, alloctype uint32, protect uint32) (value uintptr, err error) = kernel32.VirtualAlloc |
|
269 //sys VirtualFree(address uintptr, size uintptr, freetype uint32) (err error) = kernel32.VirtualFree |
|
270 //sys VirtualProtect(address uintptr, size uintptr, newprotect uint32, oldprotect *uint32) (err error) = kernel32.VirtualProtect |
|
271 //sys VirtualProtectEx(process Handle, address uintptr, size uintptr, newProtect uint32, oldProtect *uint32) (err error) = kernel32.VirtualProtectEx |
|
272 //sys VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQuery |
|
273 //sys VirtualQueryEx(process Handle, address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQueryEx |
|
274 //sys ReadProcessMemory(process Handle, baseAddress uintptr, buffer *byte, size uintptr, numberOfBytesRead *uintptr) (err error) = kernel32.ReadProcessMemory |
|
275 //sys WriteProcessMemory(process Handle, baseAddress uintptr, buffer *byte, size uintptr, numberOfBytesWritten *uintptr) (err error) = kernel32.WriteProcessMemory |
|
276 //sys TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) = mswsock.TransmitFile |
|
277 //sys ReadDirectoryChanges(handle Handle, buf *byte, buflen uint32, watchSubTree bool, mask uint32, retlen *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) = kernel32.ReadDirectoryChangesW |
|
278 //sys FindFirstChangeNotification(path string, watchSubtree bool, notifyFilter uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.FindFirstChangeNotificationW |
|
279 //sys FindNextChangeNotification(handle Handle) (err error) |
|
280 //sys FindCloseChangeNotification(handle Handle) (err error) |
|
281 //sys CertOpenSystemStore(hprov Handle, name *uint16) (store Handle, err error) = crypt32.CertOpenSystemStoreW |
|
282 //sys CertOpenStore(storeProvider uintptr, msgAndCertEncodingType uint32, cryptProv uintptr, flags uint32, para uintptr) (handle Handle, err error) = crypt32.CertOpenStore |
|
283 //sys CertEnumCertificatesInStore(store Handle, prevContext *CertContext) (context *CertContext, err error) [failretval==nil] = crypt32.CertEnumCertificatesInStore |
|
284 //sys CertAddCertificateContextToStore(store Handle, certContext *CertContext, addDisposition uint32, storeContext **CertContext) (err error) = crypt32.CertAddCertificateContextToStore |
|
285 //sys CertCloseStore(store Handle, flags uint32) (err error) = crypt32.CertCloseStore |
|
286 //sys CertDeleteCertificateFromStore(certContext *CertContext) (err error) = crypt32.CertDeleteCertificateFromStore |
|
287 //sys CertDuplicateCertificateContext(certContext *CertContext) (dupContext *CertContext) = crypt32.CertDuplicateCertificateContext |
|
288 //sys PFXImportCertStore(pfx *CryptDataBlob, password *uint16, flags uint32) (store Handle, err error) = crypt32.PFXImportCertStore |
|
289 //sys CertGetCertificateChain(engine Handle, leaf *CertContext, time *Filetime, additionalStore Handle, para *CertChainPara, flags uint32, reserved uintptr, chainCtx **CertChainContext) (err error) = crypt32.CertGetCertificateChain |
|
290 //sys CertFreeCertificateChain(ctx *CertChainContext) = crypt32.CertFreeCertificateChain |
|
291 //sys CertCreateCertificateContext(certEncodingType uint32, certEncoded *byte, encodedLen uint32) (context *CertContext, err error) [failretval==nil] = crypt32.CertCreateCertificateContext |
|
292 //sys CertFreeCertificateContext(ctx *CertContext) (err error) = crypt32.CertFreeCertificateContext |
|
293 //sys CertVerifyCertificateChainPolicy(policyOID uintptr, chain *CertChainContext, para *CertChainPolicyPara, status *CertChainPolicyStatus) (err error) = crypt32.CertVerifyCertificateChainPolicy |
|
294 //sys CertGetNameString(certContext *CertContext, nameType uint32, flags uint32, typePara unsafe.Pointer, name *uint16, size uint32) (chars uint32) = crypt32.CertGetNameStringW |
|
295 //sys CertFindExtension(objId *byte, countExtensions uint32, extensions *CertExtension) (ret *CertExtension) = crypt32.CertFindExtension |
|
296 //sys CertFindCertificateInStore(store Handle, certEncodingType uint32, findFlags uint32, findType uint32, findPara unsafe.Pointer, prevCertContext *CertContext) (cert *CertContext, err error) [failretval==nil] = crypt32.CertFindCertificateInStore |
|
297 //sys CertFindChainInStore(store Handle, certEncodingType uint32, findFlags uint32, findType uint32, findPara unsafe.Pointer, prevChainContext *CertChainContext) (certchain *CertChainContext, err error) [failretval==nil] = crypt32.CertFindChainInStore |
|
298 //sys CryptAcquireCertificatePrivateKey(cert *CertContext, flags uint32, parameters unsafe.Pointer, cryptProvOrNCryptKey *Handle, keySpec *uint32, callerFreeProvOrNCryptKey *bool) (err error) = crypt32.CryptAcquireCertificatePrivateKey |
|
299 //sys CryptQueryObject(objectType uint32, object unsafe.Pointer, expectedContentTypeFlags uint32, expectedFormatTypeFlags uint32, flags uint32, msgAndCertEncodingType *uint32, contentType *uint32, formatType *uint32, certStore *Handle, msg *Handle, context *unsafe.Pointer) (err error) = crypt32.CryptQueryObject |
|
300 //sys CryptDecodeObject(encodingType uint32, structType *byte, encodedBytes *byte, lenEncodedBytes uint32, flags uint32, decoded unsafe.Pointer, decodedLen *uint32) (err error) = crypt32.CryptDecodeObject |
|
301 //sys CryptProtectData(dataIn *DataBlob, name *uint16, optionalEntropy *DataBlob, reserved uintptr, promptStruct *CryptProtectPromptStruct, flags uint32, dataOut *DataBlob) (err error) = crypt32.CryptProtectData |
|
302 //sys CryptUnprotectData(dataIn *DataBlob, name **uint16, optionalEntropy *DataBlob, reserved uintptr, promptStruct *CryptProtectPromptStruct, flags uint32, dataOut *DataBlob) (err error) = crypt32.CryptUnprotectData |
|
303 //sys WinVerifyTrustEx(hwnd HWND, actionId *GUID, data *WinTrustData) (ret error) = wintrust.WinVerifyTrustEx |
|
304 //sys RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno error) = advapi32.RegOpenKeyExW |
|
305 //sys RegCloseKey(key Handle) (regerrno error) = advapi32.RegCloseKey |
|
306 //sys RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegQueryInfoKeyW |
|
307 //sys RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegEnumKeyExW |
|
308 //sys RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) = advapi32.RegQueryValueExW |
|
309 //sys RegNotifyChangeKeyValue(key Handle, watchSubtree bool, notifyFilter uint32, event Handle, asynchronous bool) (regerrno error) = advapi32.RegNotifyChangeKeyValue |
|
310 //sys GetCurrentProcessId() (pid uint32) = kernel32.GetCurrentProcessId |
|
311 //sys ProcessIdToSessionId(pid uint32, sessionid *uint32) (err error) = kernel32.ProcessIdToSessionId |
|
312 //sys GetConsoleMode(console Handle, mode *uint32) (err error) = kernel32.GetConsoleMode |
|
313 //sys SetConsoleMode(console Handle, mode uint32) (err error) = kernel32.SetConsoleMode |
|
314 //sys GetConsoleScreenBufferInfo(console Handle, info *ConsoleScreenBufferInfo) (err error) = kernel32.GetConsoleScreenBufferInfo |
|
315 //sys setConsoleCursorPosition(console Handle, position uint32) (err error) = kernel32.SetConsoleCursorPosition |
|
316 //sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW |
|
317 //sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW |
|
318 //sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot |
|
319 //sys Module32First(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW |
|
320 //sys Module32Next(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32NextW |
|
321 //sys Process32First(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32FirstW |
|
322 //sys Process32Next(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32NextW |
|
323 //sys Thread32First(snapshot Handle, threadEntry *ThreadEntry32) (err error) |
|
324 //sys Thread32Next(snapshot Handle, threadEntry *ThreadEntry32) (err error) |
|
325 //sys DeviceIoControl(handle Handle, ioControlCode uint32, inBuffer *byte, inBufferSize uint32, outBuffer *byte, outBufferSize uint32, bytesReturned *uint32, overlapped *Overlapped) (err error) |
|
326 // This function returns 1 byte BOOLEAN rather than the 4 byte BOOL. |
|
327 //sys CreateSymbolicLink(symlinkfilename *uint16, targetfilename *uint16, flags uint32) (err error) [failretval&0xff==0] = CreateSymbolicLinkW |
|
328 //sys CreateHardLink(filename *uint16, existingfilename *uint16, reserved uintptr) (err error) [failretval&0xff==0] = CreateHardLinkW |
|
329 //sys GetCurrentThreadId() (id uint32) |
|
330 //sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateEventW |
|
331 //sys CreateEventEx(eventAttrs *SecurityAttributes, name *uint16, flags uint32, desiredAccess uint32) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateEventExW |
|
332 //sys OpenEvent(desiredAccess uint32, inheritHandle bool, name *uint16) (handle Handle, err error) = kernel32.OpenEventW |
|
333 //sys SetEvent(event Handle) (err error) = kernel32.SetEvent |
|
334 //sys ResetEvent(event Handle) (err error) = kernel32.ResetEvent |
|
335 //sys PulseEvent(event Handle) (err error) = kernel32.PulseEvent |
|
336 //sys CreateMutex(mutexAttrs *SecurityAttributes, initialOwner bool, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateMutexW |
|
337 //sys CreateMutexEx(mutexAttrs *SecurityAttributes, name *uint16, flags uint32, desiredAccess uint32) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateMutexExW |
|
338 //sys OpenMutex(desiredAccess uint32, inheritHandle bool, name *uint16) (handle Handle, err error) = kernel32.OpenMutexW |
|
339 //sys ReleaseMutex(mutex Handle) (err error) = kernel32.ReleaseMutex |
|
340 //sys SleepEx(milliseconds uint32, alertable bool) (ret uint32) = kernel32.SleepEx |
|
341 //sys CreateJobObject(jobAttr *SecurityAttributes, name *uint16) (handle Handle, err error) = kernel32.CreateJobObjectW |
|
342 //sys AssignProcessToJobObject(job Handle, process Handle) (err error) = kernel32.AssignProcessToJobObject |
|
343 //sys TerminateJobObject(job Handle, exitCode uint32) (err error) = kernel32.TerminateJobObject |
|
344 //sys SetErrorMode(mode uint32) (ret uint32) = kernel32.SetErrorMode |
|
345 //sys ResumeThread(thread Handle) (ret uint32, err error) [failretval==0xffffffff] = kernel32.ResumeThread |
|
346 //sys SetPriorityClass(process Handle, priorityClass uint32) (err error) = kernel32.SetPriorityClass |
|
347 //sys GetPriorityClass(process Handle) (ret uint32, err error) = kernel32.GetPriorityClass |
|
348 //sys QueryInformationJobObject(job Handle, JobObjectInformationClass int32, JobObjectInformation uintptr, JobObjectInformationLength uint32, retlen *uint32) (err error) = kernel32.QueryInformationJobObject |
|
349 //sys SetInformationJobObject(job Handle, JobObjectInformationClass uint32, JobObjectInformation uintptr, JobObjectInformationLength uint32) (ret int, err error) |
|
350 //sys GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupID uint32) (err error) |
|
351 //sys GetProcessId(process Handle) (id uint32, err error) |
|
352 //sys QueryFullProcessImageName(proc Handle, flags uint32, exeName *uint16, size *uint32) (err error) = kernel32.QueryFullProcessImageNameW |
|
353 //sys OpenThread(desiredAccess uint32, inheritHandle bool, threadId uint32) (handle Handle, err error) |
|
354 //sys SetProcessPriorityBoost(process Handle, disable bool) (err error) = kernel32.SetProcessPriorityBoost |
|
355 //sys GetProcessWorkingSetSizeEx(hProcess Handle, lpMinimumWorkingSetSize *uintptr, lpMaximumWorkingSetSize *uintptr, flags *uint32) |
|
356 //sys SetProcessWorkingSetSizeEx(hProcess Handle, dwMinimumWorkingSetSize uintptr, dwMaximumWorkingSetSize uintptr, flags uint32) (err error) |
|
357 //sys GetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error) |
|
358 //sys SetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error) |
|
359 //sys GetActiveProcessorCount(groupNumber uint16) (ret uint32) |
|
360 //sys GetMaximumProcessorCount(groupNumber uint16) (ret uint32) |
|
361 //sys EnumWindows(enumFunc uintptr, param unsafe.Pointer) (err error) = user32.EnumWindows |
|
362 //sys EnumChildWindows(hwnd HWND, enumFunc uintptr, param unsafe.Pointer) = user32.EnumChildWindows |
|
363 //sys GetClassName(hwnd HWND, className *uint16, maxCount int32) (copied int32, err error) = user32.GetClassNameW |
|
364 //sys GetDesktopWindow() (hwnd HWND) = user32.GetDesktopWindow |
|
365 //sys GetForegroundWindow() (hwnd HWND) = user32.GetForegroundWindow |
|
366 //sys IsWindow(hwnd HWND) (isWindow bool) = user32.IsWindow |
|
367 //sys IsWindowUnicode(hwnd HWND) (isUnicode bool) = user32.IsWindowUnicode |
|
368 //sys IsWindowVisible(hwnd HWND) (isVisible bool) = user32.IsWindowVisible |
|
369 //sys GetGUIThreadInfo(thread uint32, info *GUIThreadInfo) (err error) = user32.GetGUIThreadInfo |
|
370 //sys GetLargePageMinimum() (size uintptr) |
|
371 |
|
372 // Volume Management Functions |
|
373 //sys DefineDosDevice(flags uint32, deviceName *uint16, targetPath *uint16) (err error) = DefineDosDeviceW |
|
374 //sys DeleteVolumeMountPoint(volumeMountPoint *uint16) (err error) = DeleteVolumeMountPointW |
|
375 //sys FindFirstVolume(volumeName *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeW |
|
376 //sys FindFirstVolumeMountPoint(rootPathName *uint16, volumeMountPoint *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeMountPointW |
|
377 //sys FindNextVolume(findVolume Handle, volumeName *uint16, bufferLength uint32) (err error) = FindNextVolumeW |
|
378 //sys FindNextVolumeMountPoint(findVolumeMountPoint Handle, volumeMountPoint *uint16, bufferLength uint32) (err error) = FindNextVolumeMountPointW |
|
379 //sys FindVolumeClose(findVolume Handle) (err error) |
|
380 //sys FindVolumeMountPointClose(findVolumeMountPoint Handle) (err error) |
|
381 //sys GetDiskFreeSpaceEx(directoryName *uint16, freeBytesAvailableToCaller *uint64, totalNumberOfBytes *uint64, totalNumberOfFreeBytes *uint64) (err error) = GetDiskFreeSpaceExW |
|
382 //sys GetDriveType(rootPathName *uint16) (driveType uint32) = GetDriveTypeW |
|
383 //sys GetLogicalDrives() (drivesBitMask uint32, err error) [failretval==0] |
|
384 //sys GetLogicalDriveStrings(bufferLength uint32, buffer *uint16) (n uint32, err error) [failretval==0] = GetLogicalDriveStringsW |
|
385 //sys GetVolumeInformation(rootPathName *uint16, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationW |
|
386 //sys GetVolumeInformationByHandle(file Handle, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationByHandleW |
|
387 //sys GetVolumeNameForVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16, bufferlength uint32) (err error) = GetVolumeNameForVolumeMountPointW |
|
388 //sys GetVolumePathName(fileName *uint16, volumePathName *uint16, bufferLength uint32) (err error) = GetVolumePathNameW |
|
389 //sys GetVolumePathNamesForVolumeName(volumeName *uint16, volumePathNames *uint16, bufferLength uint32, returnLength *uint32) (err error) = GetVolumePathNamesForVolumeNameW |
|
390 //sys QueryDosDevice(deviceName *uint16, targetPath *uint16, max uint32) (n uint32, err error) [failretval==0] = QueryDosDeviceW |
|
391 //sys SetVolumeLabel(rootPathName *uint16, volumeName *uint16) (err error) = SetVolumeLabelW |
|
392 //sys SetVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16) (err error) = SetVolumeMountPointW |
|
393 //sys InitiateSystemShutdownEx(machineName *uint16, message *uint16, timeout uint32, forceAppsClosed bool, rebootAfterShutdown bool, reason uint32) (err error) = advapi32.InitiateSystemShutdownExW |
|
394 //sys SetProcessShutdownParameters(level uint32, flags uint32) (err error) = kernel32.SetProcessShutdownParameters |
|
395 //sys GetProcessShutdownParameters(level *uint32, flags *uint32) (err error) = kernel32.GetProcessShutdownParameters |
|
396 //sys clsidFromString(lpsz *uint16, pclsid *GUID) (ret error) = ole32.CLSIDFromString |
|
397 //sys stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int32) (chars int32) = ole32.StringFromGUID2 |
|
398 //sys coCreateGuid(pguid *GUID) (ret error) = ole32.CoCreateGuid |
|
399 //sys CoTaskMemFree(address unsafe.Pointer) = ole32.CoTaskMemFree |
|
400 //sys CoInitializeEx(reserved uintptr, coInit uint32) (ret error) = ole32.CoInitializeEx |
|
401 //sys CoUninitialize() = ole32.CoUninitialize |
|
402 //sys CoGetObject(name *uint16, bindOpts *BIND_OPTS3, guid *GUID, functionTable **uintptr) (ret error) = ole32.CoGetObject |
|
403 //sys getProcessPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetProcessPreferredUILanguages |
|
404 //sys getThreadPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetThreadPreferredUILanguages |
|
405 //sys getUserPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetUserPreferredUILanguages |
|
406 //sys getSystemPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetSystemPreferredUILanguages |
|
407 //sys findResource(module Handle, name uintptr, resType uintptr) (resInfo Handle, err error) = kernel32.FindResourceW |
|
408 //sys SizeofResource(module Handle, resInfo Handle) (size uint32, err error) = kernel32.SizeofResource |
|
409 //sys LoadResource(module Handle, resInfo Handle) (resData Handle, err error) = kernel32.LoadResource |
|
410 //sys LockResource(resData Handle) (addr uintptr, err error) = kernel32.LockResource |
|
411 |
|
412 // Version APIs |
|
413 //sys GetFileVersionInfoSize(filename string, zeroHandle *Handle) (bufSize uint32, err error) = version.GetFileVersionInfoSizeW |
|
414 //sys GetFileVersionInfo(filename string, handle uint32, bufSize uint32, buffer unsafe.Pointer) (err error) = version.GetFileVersionInfoW |
|
415 //sys VerQueryValue(block unsafe.Pointer, subBlock string, pointerToBufferPointer unsafe.Pointer, bufSize *uint32) (err error) = version.VerQueryValueW |
|
416 |
|
417 // Process Status API (PSAPI) |
|
418 //sys EnumProcesses(processIds []uint32, bytesReturned *uint32) (err error) = psapi.EnumProcesses |
|
419 //sys EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) = psapi.EnumProcessModules |
|
420 //sys EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) = psapi.EnumProcessModulesEx |
|
421 //sys GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) = psapi.GetModuleInformation |
|
422 //sys GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) = psapi.GetModuleFileNameExW |
|
423 //sys GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) = psapi.GetModuleBaseNameW |
|
424 //sys QueryWorkingSetEx(process Handle, pv uintptr, cb uint32) (err error) = psapi.QueryWorkingSetEx |
|
425 |
|
426 // NT Native APIs |
|
427 //sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb |
|
428 //sys rtlGetVersion(info *OsVersionInfoEx) (ntstatus error) = ntdll.RtlGetVersion |
|
429 //sys rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers |
|
430 //sys RtlGetCurrentPeb() (peb *PEB) = ntdll.RtlGetCurrentPeb |
|
431 //sys RtlInitUnicodeString(destinationString *NTUnicodeString, sourceString *uint16) = ntdll.RtlInitUnicodeString |
|
432 //sys RtlInitString(destinationString *NTString, sourceString *byte) = ntdll.RtlInitString |
|
433 //sys NtCreateFile(handle *Handle, access uint32, oa *OBJECT_ATTRIBUTES, iosb *IO_STATUS_BLOCK, allocationSize *int64, attributes uint32, share uint32, disposition uint32, options uint32, eabuffer uintptr, ealength uint32) (ntstatus error) = ntdll.NtCreateFile |
|
434 //sys NtCreateNamedPipeFile(pipe *Handle, access uint32, oa *OBJECT_ATTRIBUTES, iosb *IO_STATUS_BLOCK, share uint32, disposition uint32, options uint32, typ uint32, readMode uint32, completionMode uint32, maxInstances uint32, inboundQuota uint32, outputQuota uint32, timeout *int64) (ntstatus error) = ntdll.NtCreateNamedPipeFile |
|
435 //sys NtSetInformationFile(handle Handle, iosb *IO_STATUS_BLOCK, inBuffer *byte, inBufferLen uint32, class uint32) (ntstatus error) = ntdll.NtSetInformationFile |
|
436 //sys RtlDosPathNameToNtPathName(dosName *uint16, ntName *NTUnicodeString, ntFileNamePart *uint16, relativeName *RTL_RELATIVE_NAME) (ntstatus error) = ntdll.RtlDosPathNameToNtPathName_U_WithStatus |
|
437 //sys RtlDosPathNameToRelativeNtPathName(dosName *uint16, ntName *NTUnicodeString, ntFileNamePart *uint16, relativeName *RTL_RELATIVE_NAME) (ntstatus error) = ntdll.RtlDosPathNameToRelativeNtPathName_U_WithStatus |
|
438 //sys RtlDefaultNpAcl(acl **ACL) (ntstatus error) = ntdll.RtlDefaultNpAcl |
|
439 //sys NtQueryInformationProcess(proc Handle, procInfoClass int32, procInfo unsafe.Pointer, procInfoLen uint32, retLen *uint32) (ntstatus error) = ntdll.NtQueryInformationProcess |
|
440 //sys NtSetInformationProcess(proc Handle, procInfoClass int32, procInfo unsafe.Pointer, procInfoLen uint32) (ntstatus error) = ntdll.NtSetInformationProcess |
|
441 //sys NtQuerySystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32, retLen *uint32) (ntstatus error) = ntdll.NtQuerySystemInformation |
|
442 //sys NtSetSystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32) (ntstatus error) = ntdll.NtSetSystemInformation |
|
443 //sys RtlAddFunctionTable(functionTable *RUNTIME_FUNCTION, entryCount uint32, baseAddress uintptr) (ret bool) = ntdll.RtlAddFunctionTable |
|
444 //sys RtlDeleteFunctionTable(functionTable *RUNTIME_FUNCTION) (ret bool) = ntdll.RtlDeleteFunctionTable |
|
445 |
|
446 // Desktop Window Manager API (Dwmapi) |
|
447 //sys DwmGetWindowAttribute(hwnd HWND, attribute uint32, value unsafe.Pointer, size uint32) (ret error) = dwmapi.DwmGetWindowAttribute |
|
448 //sys DwmSetWindowAttribute(hwnd HWND, attribute uint32, value unsafe.Pointer, size uint32) (ret error) = dwmapi.DwmSetWindowAttribute |
|
449 |
|
450 // syscall interface implementation for other packages |
|
451 |
|
452 // GetCurrentProcess returns the handle for the current process. |
|
453 // It is a pseudo handle that does not need to be closed. |
|
454 // The returned error is always nil. |
|
455 // |
|
456 // Deprecated: use CurrentProcess for the same Handle without the nil |
|
457 // error. |
|
458 func GetCurrentProcess() (Handle, error) { |
|
459 return CurrentProcess(), nil |
|
460 } |
|
461 |
|
462 // CurrentProcess returns the handle for the current process. |
|
463 // It is a pseudo handle that does not need to be closed. |
|
464 func CurrentProcess() Handle { return Handle(^uintptr(1 - 1)) } |
|
465 |
|
466 // GetCurrentThread returns the handle for the current thread. |
|
467 // It is a pseudo handle that does not need to be closed. |
|
468 // The returned error is always nil. |
|
469 // |
|
470 // Deprecated: use CurrentThread for the same Handle without the nil |
|
471 // error. |
|
472 func GetCurrentThread() (Handle, error) { |
|
473 return CurrentThread(), nil |
|
474 } |
|
475 |
|
476 // CurrentThread returns the handle for the current thread. |
|
477 // It is a pseudo handle that does not need to be closed. |
|
478 func CurrentThread() Handle { return Handle(^uintptr(2 - 1)) } |
|
479 |
|
480 // GetProcAddressByOrdinal retrieves the address of the exported |
|
481 // function from module by ordinal. |
|
482 func GetProcAddressByOrdinal(module Handle, ordinal uintptr) (proc uintptr, err error) { |
|
483 r0, _, e1 := syscall.Syscall(procGetProcAddress.Addr(), 2, uintptr(module), ordinal, 0) |
|
484 proc = uintptr(r0) |
|
485 if proc == 0 { |
|
486 err = errnoErr(e1) |
|
487 } |
|
488 return |
|
489 } |
|
490 |
|
491 func Exit(code int) { ExitProcess(uint32(code)) } |
|
492 |
|
493 func makeInheritSa() *SecurityAttributes { |
|
494 var sa SecurityAttributes |
|
495 sa.Length = uint32(unsafe.Sizeof(sa)) |
|
496 sa.InheritHandle = 1 |
|
497 return &sa |
|
498 } |
|
499 |
|
500 func Open(path string, mode int, perm uint32) (fd Handle, err error) { |
|
501 if len(path) == 0 { |
|
502 return InvalidHandle, ERROR_FILE_NOT_FOUND |
|
503 } |
|
504 pathp, err := UTF16PtrFromString(path) |
|
505 if err != nil { |
|
506 return InvalidHandle, err |
|
507 } |
|
508 var access uint32 |
|
509 switch mode & (O_RDONLY | O_WRONLY | O_RDWR) { |
|
510 case O_RDONLY: |
|
511 access = GENERIC_READ |
|
512 case O_WRONLY: |
|
513 access = GENERIC_WRITE |
|
514 case O_RDWR: |
|
515 access = GENERIC_READ | GENERIC_WRITE |
|
516 } |
|
517 if mode&O_CREAT != 0 { |
|
518 access |= GENERIC_WRITE |
|
519 } |
|
520 if mode&O_APPEND != 0 { |
|
521 access &^= GENERIC_WRITE |
|
522 access |= FILE_APPEND_DATA |
|
523 } |
|
524 sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE) |
|
525 var sa *SecurityAttributes |
|
526 if mode&O_CLOEXEC == 0 { |
|
527 sa = makeInheritSa() |
|
528 } |
|
529 var createmode uint32 |
|
530 switch { |
|
531 case mode&(O_CREAT|O_EXCL) == (O_CREAT | O_EXCL): |
|
532 createmode = CREATE_NEW |
|
533 case mode&(O_CREAT|O_TRUNC) == (O_CREAT | O_TRUNC): |
|
534 createmode = CREATE_ALWAYS |
|
535 case mode&O_CREAT == O_CREAT: |
|
536 createmode = OPEN_ALWAYS |
|
537 case mode&O_TRUNC == O_TRUNC: |
|
538 createmode = TRUNCATE_EXISTING |
|
539 default: |
|
540 createmode = OPEN_EXISTING |
|
541 } |
|
542 var attrs uint32 = FILE_ATTRIBUTE_NORMAL |
|
543 if perm&S_IWRITE == 0 { |
|
544 attrs = FILE_ATTRIBUTE_READONLY |
|
545 } |
|
546 h, e := CreateFile(pathp, access, sharemode, sa, createmode, attrs, 0) |
|
547 return h, e |
|
548 } |
|
549 |
|
550 func Read(fd Handle, p []byte) (n int, err error) { |
|
551 var done uint32 |
|
552 e := ReadFile(fd, p, &done, nil) |
|
553 if e != nil { |
|
554 if e == ERROR_BROKEN_PIPE { |
|
555 // NOTE(brainman): work around ERROR_BROKEN_PIPE is returned on reading EOF from stdin |
|
556 return 0, nil |
|
557 } |
|
558 return 0, e |
|
559 } |
|
560 return int(done), nil |
|
561 } |
|
562 |
|
563 func Write(fd Handle, p []byte) (n int, err error) { |
|
564 if raceenabled { |
|
565 raceReleaseMerge(unsafe.Pointer(&ioSync)) |
|
566 } |
|
567 var done uint32 |
|
568 e := WriteFile(fd, p, &done, nil) |
|
569 if e != nil { |
|
570 return 0, e |
|
571 } |
|
572 return int(done), nil |
|
573 } |
|
574 |
|
575 func ReadFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error { |
|
576 err := readFile(fd, p, done, overlapped) |
|
577 if raceenabled { |
|
578 if *done > 0 { |
|
579 raceWriteRange(unsafe.Pointer(&p[0]), int(*done)) |
|
580 } |
|
581 raceAcquire(unsafe.Pointer(&ioSync)) |
|
582 } |
|
583 return err |
|
584 } |
|
585 |
|
586 func WriteFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error { |
|
587 if raceenabled { |
|
588 raceReleaseMerge(unsafe.Pointer(&ioSync)) |
|
589 } |
|
590 err := writeFile(fd, p, done, overlapped) |
|
591 if raceenabled && *done > 0 { |
|
592 raceReadRange(unsafe.Pointer(&p[0]), int(*done)) |
|
593 } |
|
594 return err |
|
595 } |
|
596 |
|
597 var ioSync int64 |
|
598 |
|
599 func Seek(fd Handle, offset int64, whence int) (newoffset int64, err error) { |
|
600 var w uint32 |
|
601 switch whence { |
|
602 case 0: |
|
603 w = FILE_BEGIN |
|
604 case 1: |
|
605 w = FILE_CURRENT |
|
606 case 2: |
|
607 w = FILE_END |
|
608 } |
|
609 hi := int32(offset >> 32) |
|
610 lo := int32(offset) |
|
611 // use GetFileType to check pipe, pipe can't do seek |
|
612 ft, _ := GetFileType(fd) |
|
613 if ft == FILE_TYPE_PIPE { |
|
614 return 0, syscall.EPIPE |
|
615 } |
|
616 rlo, e := SetFilePointer(fd, lo, &hi, w) |
|
617 if e != nil { |
|
618 return 0, e |
|
619 } |
|
620 return int64(hi)<<32 + int64(rlo), nil |
|
621 } |
|
622 |
|
623 func Close(fd Handle) (err error) { |
|
624 return CloseHandle(fd) |
|
625 } |
|
626 |
|
627 var ( |
|
628 Stdin = getStdHandle(STD_INPUT_HANDLE) |
|
629 Stdout = getStdHandle(STD_OUTPUT_HANDLE) |
|
630 Stderr = getStdHandle(STD_ERROR_HANDLE) |
|
631 ) |
|
632 |
|
633 func getStdHandle(stdhandle uint32) (fd Handle) { |
|
634 r, _ := GetStdHandle(stdhandle) |
|
635 return r |
|
636 } |
|
637 |
|
638 const ImplementsGetwd = true |
|
639 |
|
640 func Getwd() (wd string, err error) { |
|
641 b := make([]uint16, 300) |
|
642 n, e := GetCurrentDirectory(uint32(len(b)), &b[0]) |
|
643 if e != nil { |
|
644 return "", e |
|
645 } |
|
646 return string(utf16.Decode(b[0:n])), nil |
|
647 } |
|
648 |
|
649 func Chdir(path string) (err error) { |
|
650 pathp, err := UTF16PtrFromString(path) |
|
651 if err != nil { |
|
652 return err |
|
653 } |
|
654 return SetCurrentDirectory(pathp) |
|
655 } |
|
656 |
|
657 func Mkdir(path string, mode uint32) (err error) { |
|
658 pathp, err := UTF16PtrFromString(path) |
|
659 if err != nil { |
|
660 return err |
|
661 } |
|
662 return CreateDirectory(pathp, nil) |
|
663 } |
|
664 |
|
665 func Rmdir(path string) (err error) { |
|
666 pathp, err := UTF16PtrFromString(path) |
|
667 if err != nil { |
|
668 return err |
|
669 } |
|
670 return RemoveDirectory(pathp) |
|
671 } |
|
672 |
|
673 func Unlink(path string) (err error) { |
|
674 pathp, err := UTF16PtrFromString(path) |
|
675 if err != nil { |
|
676 return err |
|
677 } |
|
678 return DeleteFile(pathp) |
|
679 } |
|
680 |
|
681 func Rename(oldpath, newpath string) (err error) { |
|
682 from, err := UTF16PtrFromString(oldpath) |
|
683 if err != nil { |
|
684 return err |
|
685 } |
|
686 to, err := UTF16PtrFromString(newpath) |
|
687 if err != nil { |
|
688 return err |
|
689 } |
|
690 return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING) |
|
691 } |
|
692 |
|
693 func ComputerName() (name string, err error) { |
|
694 var n uint32 = MAX_COMPUTERNAME_LENGTH + 1 |
|
695 b := make([]uint16, n) |
|
696 e := GetComputerName(&b[0], &n) |
|
697 if e != nil { |
|
698 return "", e |
|
699 } |
|
700 return string(utf16.Decode(b[0:n])), nil |
|
701 } |
|
702 |
|
703 func DurationSinceBoot() time.Duration { |
|
704 return time.Duration(getTickCount64()) * time.Millisecond |
|
705 } |
|
706 |
|
707 func Ftruncate(fd Handle, length int64) (err error) { |
|
708 curoffset, e := Seek(fd, 0, 1) |
|
709 if e != nil { |
|
710 return e |
|
711 } |
|
712 defer Seek(fd, curoffset, 0) |
|
713 _, e = Seek(fd, length, 0) |
|
714 if e != nil { |
|
715 return e |
|
716 } |
|
717 e = SetEndOfFile(fd) |
|
718 if e != nil { |
|
719 return e |
|
720 } |
|
721 return nil |
|
722 } |
|
723 |
|
724 func Gettimeofday(tv *Timeval) (err error) { |
|
725 var ft Filetime |
|
726 GetSystemTimeAsFileTime(&ft) |
|
727 *tv = NsecToTimeval(ft.Nanoseconds()) |
|
728 return nil |
|
729 } |
|
730 |
|
731 func Pipe(p []Handle) (err error) { |
|
732 if len(p) != 2 { |
|
733 return syscall.EINVAL |
|
734 } |
|
735 var r, w Handle |
|
736 e := CreatePipe(&r, &w, makeInheritSa(), 0) |
|
737 if e != nil { |
|
738 return e |
|
739 } |
|
740 p[0] = r |
|
741 p[1] = w |
|
742 return nil |
|
743 } |
|
744 |
|
745 func Utimes(path string, tv []Timeval) (err error) { |
|
746 if len(tv) != 2 { |
|
747 return syscall.EINVAL |
|
748 } |
|
749 pathp, e := UTF16PtrFromString(path) |
|
750 if e != nil { |
|
751 return e |
|
752 } |
|
753 h, e := CreateFile(pathp, |
|
754 FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil, |
|
755 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) |
|
756 if e != nil { |
|
757 return e |
|
758 } |
|
759 defer CloseHandle(h) |
|
760 a := NsecToFiletime(tv[0].Nanoseconds()) |
|
761 w := NsecToFiletime(tv[1].Nanoseconds()) |
|
762 return SetFileTime(h, nil, &a, &w) |
|
763 } |
|
764 |
|
765 func UtimesNano(path string, ts []Timespec) (err error) { |
|
766 if len(ts) != 2 { |
|
767 return syscall.EINVAL |
|
768 } |
|
769 pathp, e := UTF16PtrFromString(path) |
|
770 if e != nil { |
|
771 return e |
|
772 } |
|
773 h, e := CreateFile(pathp, |
|
774 FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil, |
|
775 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) |
|
776 if e != nil { |
|
777 return e |
|
778 } |
|
779 defer CloseHandle(h) |
|
780 a := NsecToFiletime(TimespecToNsec(ts[0])) |
|
781 w := NsecToFiletime(TimespecToNsec(ts[1])) |
|
782 return SetFileTime(h, nil, &a, &w) |
|
783 } |
|
784 |
|
785 func Fsync(fd Handle) (err error) { |
|
786 return FlushFileBuffers(fd) |
|
787 } |
|
788 |
|
789 func Chmod(path string, mode uint32) (err error) { |
|
790 p, e := UTF16PtrFromString(path) |
|
791 if e != nil { |
|
792 return e |
|
793 } |
|
794 attrs, e := GetFileAttributes(p) |
|
795 if e != nil { |
|
796 return e |
|
797 } |
|
798 if mode&S_IWRITE != 0 { |
|
799 attrs &^= FILE_ATTRIBUTE_READONLY |
|
800 } else { |
|
801 attrs |= FILE_ATTRIBUTE_READONLY |
|
802 } |
|
803 return SetFileAttributes(p, attrs) |
|
804 } |
|
805 |
|
806 func LoadGetSystemTimePreciseAsFileTime() error { |
|
807 return procGetSystemTimePreciseAsFileTime.Find() |
|
808 } |
|
809 |
|
810 func LoadCancelIoEx() error { |
|
811 return procCancelIoEx.Find() |
|
812 } |
|
813 |
|
814 func LoadSetFileCompletionNotificationModes() error { |
|
815 return procSetFileCompletionNotificationModes.Find() |
|
816 } |
|
817 |
|
818 func WaitForMultipleObjects(handles []Handle, waitAll bool, waitMilliseconds uint32) (event uint32, err error) { |
|
819 // Every other win32 array API takes arguments as "pointer, count", except for this function. So we |
|
820 // can't declare it as a usual [] type, because mksyscall will use the opposite order. We therefore |
|
821 // trivially stub this ourselves. |
|
822 |
|
823 var handlePtr *Handle |
|
824 if len(handles) > 0 { |
|
825 handlePtr = &handles[0] |
|
826 } |
|
827 return waitForMultipleObjects(uint32(len(handles)), uintptr(unsafe.Pointer(handlePtr)), waitAll, waitMilliseconds) |
|
828 } |
|
829 |
|
830 // net api calls |
|
831 |
|
832 const socket_error = uintptr(^uint32(0)) |
|
833 |
|
834 //sys WSAStartup(verreq uint32, data *WSAData) (sockerr error) = ws2_32.WSAStartup |
|
835 //sys WSACleanup() (err error) [failretval==socket_error] = ws2_32.WSACleanup |
|
836 //sys WSAIoctl(s Handle, iocc uint32, inbuf *byte, cbif uint32, outbuf *byte, cbob uint32, cbbr *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) [failretval==socket_error] = ws2_32.WSAIoctl |
|
837 //sys socket(af int32, typ int32, protocol int32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.socket |
|
838 //sys sendto(s Handle, buf []byte, flags int32, to unsafe.Pointer, tolen int32) (err error) [failretval==socket_error] = ws2_32.sendto |
|
839 //sys recvfrom(s Handle, buf []byte, flags int32, from *RawSockaddrAny, fromlen *int32) (n int32, err error) [failretval==-1] = ws2_32.recvfrom |
|
840 //sys Setsockopt(s Handle, level int32, optname int32, optval *byte, optlen int32) (err error) [failretval==socket_error] = ws2_32.setsockopt |
|
841 //sys Getsockopt(s Handle, level int32, optname int32, optval *byte, optlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockopt |
|
842 //sys bind(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.bind |
|
843 //sys connect(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.connect |
|
844 //sys getsockname(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockname |
|
845 //sys getpeername(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getpeername |
|
846 //sys listen(s Handle, backlog int32) (err error) [failretval==socket_error] = ws2_32.listen |
|
847 //sys shutdown(s Handle, how int32) (err error) [failretval==socket_error] = ws2_32.shutdown |
|
848 //sys Closesocket(s Handle) (err error) [failretval==socket_error] = ws2_32.closesocket |
|
849 //sys AcceptEx(ls Handle, as Handle, buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, recvd *uint32, overlapped *Overlapped) (err error) = mswsock.AcceptEx |
|
850 //sys GetAcceptExSockaddrs(buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, lrsa **RawSockaddrAny, lrsalen *int32, rrsa **RawSockaddrAny, rrsalen *int32) = mswsock.GetAcceptExSockaddrs |
|
851 //sys WSARecv(s Handle, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSARecv |
|
852 //sys WSASend(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSASend |
|
853 //sys WSARecvFrom(s Handle, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, from *RawSockaddrAny, fromlen *int32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSARecvFrom |
|
854 //sys WSASendTo(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to *RawSockaddrAny, tolen int32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSASendTo |
|
855 //sys WSASocket(af int32, typ int32, protocol int32, protoInfo *WSAProtocolInfo, group uint32, flags uint32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.WSASocketW |
|
856 //sys GetHostByName(name string) (h *Hostent, err error) [failretval==nil] = ws2_32.gethostbyname |
|
857 //sys GetServByName(name string, proto string) (s *Servent, err error) [failretval==nil] = ws2_32.getservbyname |
|
858 //sys Ntohs(netshort uint16) (u uint16) = ws2_32.ntohs |
|
859 //sys GetProtoByName(name string) (p *Protoent, err error) [failretval==nil] = ws2_32.getprotobyname |
|
860 //sys DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status error) = dnsapi.DnsQuery_W |
|
861 //sys DnsRecordListFree(rl *DNSRecord, freetype uint32) = dnsapi.DnsRecordListFree |
|
862 //sys DnsNameCompare(name1 *uint16, name2 *uint16) (same bool) = dnsapi.DnsNameCompare_W |
|
863 //sys GetAddrInfoW(nodename *uint16, servicename *uint16, hints *AddrinfoW, result **AddrinfoW) (sockerr error) = ws2_32.GetAddrInfoW |
|
864 //sys FreeAddrInfoW(addrinfo *AddrinfoW) = ws2_32.FreeAddrInfoW |
|
865 //sys GetIfEntry(pIfRow *MibIfRow) (errcode error) = iphlpapi.GetIfEntry |
|
866 //sys GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode error) = iphlpapi.GetAdaptersInfo |
|
867 //sys SetFileCompletionNotificationModes(handle Handle, flags uint8) (err error) = kernel32.SetFileCompletionNotificationModes |
|
868 //sys WSAEnumProtocols(protocols *int32, protocolBuffer *WSAProtocolInfo, bufferLength *uint32) (n int32, err error) [failretval==-1] = ws2_32.WSAEnumProtocolsW |
|
869 //sys WSAGetOverlappedResult(h Handle, o *Overlapped, bytes *uint32, wait bool, flags *uint32) (err error) = ws2_32.WSAGetOverlappedResult |
|
870 //sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses |
|
871 //sys GetACP() (acp uint32) = kernel32.GetACP |
|
872 //sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar |
|
873 //sys getBestInterfaceEx(sockaddr unsafe.Pointer, pdwBestIfIndex *uint32) (errcode error) = iphlpapi.GetBestInterfaceEx |
|
874 |
|
875 // For testing: clients can set this flag to force |
|
876 // creation of IPv6 sockets to return EAFNOSUPPORT. |
|
877 var SocketDisableIPv6 bool |
|
878 |
|
879 type RawSockaddrInet4 struct { |
|
880 Family uint16 |
|
881 Port uint16 |
|
882 Addr [4]byte /* in_addr */ |
|
883 Zero [8]uint8 |
|
884 } |
|
885 |
|
886 type RawSockaddrInet6 struct { |
|
887 Family uint16 |
|
888 Port uint16 |
|
889 Flowinfo uint32 |
|
890 Addr [16]byte /* in6_addr */ |
|
891 Scope_id uint32 |
|
892 } |
|
893 |
|
894 type RawSockaddr struct { |
|
895 Family uint16 |
|
896 Data [14]int8 |
|
897 } |
|
898 |
|
899 type RawSockaddrAny struct { |
|
900 Addr RawSockaddr |
|
901 Pad [100]int8 |
|
902 } |
|
903 |
|
904 type Sockaddr interface { |
|
905 sockaddr() (ptr unsafe.Pointer, len int32, err error) // lowercase; only we can define Sockaddrs |
|
906 } |
|
907 |
|
908 type SockaddrInet4 struct { |
|
909 Port int |
|
910 Addr [4]byte |
|
911 raw RawSockaddrInet4 |
|
912 } |
|
913 |
|
914 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, int32, error) { |
|
915 if sa.Port < 0 || sa.Port > 0xFFFF { |
|
916 return nil, 0, syscall.EINVAL |
|
917 } |
|
918 sa.raw.Family = AF_INET |
|
919 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) |
|
920 p[0] = byte(sa.Port >> 8) |
|
921 p[1] = byte(sa.Port) |
|
922 sa.raw.Addr = sa.Addr |
|
923 return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil |
|
924 } |
|
925 |
|
926 type SockaddrInet6 struct { |
|
927 Port int |
|
928 ZoneId uint32 |
|
929 Addr [16]byte |
|
930 raw RawSockaddrInet6 |
|
931 } |
|
932 |
|
933 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, int32, error) { |
|
934 if sa.Port < 0 || sa.Port > 0xFFFF { |
|
935 return nil, 0, syscall.EINVAL |
|
936 } |
|
937 sa.raw.Family = AF_INET6 |
|
938 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) |
|
939 p[0] = byte(sa.Port >> 8) |
|
940 p[1] = byte(sa.Port) |
|
941 sa.raw.Scope_id = sa.ZoneId |
|
942 sa.raw.Addr = sa.Addr |
|
943 return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil |
|
944 } |
|
945 |
|
946 type RawSockaddrUnix struct { |
|
947 Family uint16 |
|
948 Path [UNIX_PATH_MAX]int8 |
|
949 } |
|
950 |
|
951 type SockaddrUnix struct { |
|
952 Name string |
|
953 raw RawSockaddrUnix |
|
954 } |
|
955 |
|
956 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) { |
|
957 name := sa.Name |
|
958 n := len(name) |
|
959 if n > len(sa.raw.Path) { |
|
960 return nil, 0, syscall.EINVAL |
|
961 } |
|
962 if n == len(sa.raw.Path) && name[0] != '@' { |
|
963 return nil, 0, syscall.EINVAL |
|
964 } |
|
965 sa.raw.Family = AF_UNIX |
|
966 for i := 0; i < n; i++ { |
|
967 sa.raw.Path[i] = int8(name[i]) |
|
968 } |
|
969 // length is family (uint16), name, NUL. |
|
970 sl := int32(2) |
|
971 if n > 0 { |
|
972 sl += int32(n) + 1 |
|
973 } |
|
974 if sa.raw.Path[0] == '@' { |
|
975 sa.raw.Path[0] = 0 |
|
976 // Don't count trailing NUL for abstract address. |
|
977 sl-- |
|
978 } |
|
979 |
|
980 return unsafe.Pointer(&sa.raw), sl, nil |
|
981 } |
|
982 |
|
983 type RawSockaddrBth struct { |
|
984 AddressFamily [2]byte |
|
985 BtAddr [8]byte |
|
986 ServiceClassId [16]byte |
|
987 Port [4]byte |
|
988 } |
|
989 |
|
990 type SockaddrBth struct { |
|
991 BtAddr uint64 |
|
992 ServiceClassId GUID |
|
993 Port uint32 |
|
994 |
|
995 raw RawSockaddrBth |
|
996 } |
|
997 |
|
998 func (sa *SockaddrBth) sockaddr() (unsafe.Pointer, int32, error) { |
|
999 family := AF_BTH |
|
1000 sa.raw = RawSockaddrBth{ |
|
1001 AddressFamily: *(*[2]byte)(unsafe.Pointer(&family)), |
|
1002 BtAddr: *(*[8]byte)(unsafe.Pointer(&sa.BtAddr)), |
|
1003 Port: *(*[4]byte)(unsafe.Pointer(&sa.Port)), |
|
1004 ServiceClassId: *(*[16]byte)(unsafe.Pointer(&sa.ServiceClassId)), |
|
1005 } |
|
1006 return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil |
|
1007 } |
|
1008 |
|
1009 func (rsa *RawSockaddrAny) Sockaddr() (Sockaddr, error) { |
|
1010 switch rsa.Addr.Family { |
|
1011 case AF_UNIX: |
|
1012 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) |
|
1013 sa := new(SockaddrUnix) |
|
1014 if pp.Path[0] == 0 { |
|
1015 // "Abstract" Unix domain socket. |
|
1016 // Rewrite leading NUL as @ for textual display. |
|
1017 // (This is the standard convention.) |
|
1018 // Not friendly to overwrite in place, |
|
1019 // but the callers below don't care. |
|
1020 pp.Path[0] = '@' |
|
1021 } |
|
1022 |
|
1023 // Assume path ends at NUL. |
|
1024 // This is not technically the Linux semantics for |
|
1025 // abstract Unix domain sockets--they are supposed |
|
1026 // to be uninterpreted fixed-size binary blobs--but |
|
1027 // everyone uses this convention. |
|
1028 n := 0 |
|
1029 for n < len(pp.Path) && pp.Path[n] != 0 { |
|
1030 n++ |
|
1031 } |
|
1032 bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n] |
|
1033 sa.Name = string(bytes) |
|
1034 return sa, nil |
|
1035 |
|
1036 case AF_INET: |
|
1037 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa)) |
|
1038 sa := new(SockaddrInet4) |
|
1039 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) |
|
1040 sa.Port = int(p[0])<<8 + int(p[1]) |
|
1041 sa.Addr = pp.Addr |
|
1042 return sa, nil |
|
1043 |
|
1044 case AF_INET6: |
|
1045 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa)) |
|
1046 sa := new(SockaddrInet6) |
|
1047 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) |
|
1048 sa.Port = int(p[0])<<8 + int(p[1]) |
|
1049 sa.ZoneId = pp.Scope_id |
|
1050 sa.Addr = pp.Addr |
|
1051 return sa, nil |
|
1052 } |
|
1053 return nil, syscall.EAFNOSUPPORT |
|
1054 } |
|
1055 |
|
1056 func Socket(domain, typ, proto int) (fd Handle, err error) { |
|
1057 if domain == AF_INET6 && SocketDisableIPv6 { |
|
1058 return InvalidHandle, syscall.EAFNOSUPPORT |
|
1059 } |
|
1060 return socket(int32(domain), int32(typ), int32(proto)) |
|
1061 } |
|
1062 |
|
1063 func SetsockoptInt(fd Handle, level, opt int, value int) (err error) { |
|
1064 v := int32(value) |
|
1065 return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&v)), int32(unsafe.Sizeof(v))) |
|
1066 } |
|
1067 |
|
1068 func Bind(fd Handle, sa Sockaddr) (err error) { |
|
1069 ptr, n, err := sa.sockaddr() |
|
1070 if err != nil { |
|
1071 return err |
|
1072 } |
|
1073 return bind(fd, ptr, n) |
|
1074 } |
|
1075 |
|
1076 func Connect(fd Handle, sa Sockaddr) (err error) { |
|
1077 ptr, n, err := sa.sockaddr() |
|
1078 if err != nil { |
|
1079 return err |
|
1080 } |
|
1081 return connect(fd, ptr, n) |
|
1082 } |
|
1083 |
|
1084 func GetBestInterfaceEx(sa Sockaddr, pdwBestIfIndex *uint32) (err error) { |
|
1085 ptr, _, err := sa.sockaddr() |
|
1086 if err != nil { |
|
1087 return err |
|
1088 } |
|
1089 return getBestInterfaceEx(ptr, pdwBestIfIndex) |
|
1090 } |
|
1091 |
|
1092 func Getsockname(fd Handle) (sa Sockaddr, err error) { |
|
1093 var rsa RawSockaddrAny |
|
1094 l := int32(unsafe.Sizeof(rsa)) |
|
1095 if err = getsockname(fd, &rsa, &l); err != nil { |
|
1096 return |
|
1097 } |
|
1098 return rsa.Sockaddr() |
|
1099 } |
|
1100 |
|
1101 func Getpeername(fd Handle) (sa Sockaddr, err error) { |
|
1102 var rsa RawSockaddrAny |
|
1103 l := int32(unsafe.Sizeof(rsa)) |
|
1104 if err = getpeername(fd, &rsa, &l); err != nil { |
|
1105 return |
|
1106 } |
|
1107 return rsa.Sockaddr() |
|
1108 } |
|
1109 |
|
1110 func Listen(s Handle, n int) (err error) { |
|
1111 return listen(s, int32(n)) |
|
1112 } |
|
1113 |
|
1114 func Shutdown(fd Handle, how int) (err error) { |
|
1115 return shutdown(fd, int32(how)) |
|
1116 } |
|
1117 |
|
1118 func WSASendto(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to Sockaddr, overlapped *Overlapped, croutine *byte) (err error) { |
|
1119 var rsa unsafe.Pointer |
|
1120 var l int32 |
|
1121 if to != nil { |
|
1122 rsa, l, err = to.sockaddr() |
|
1123 if err != nil { |
|
1124 return err |
|
1125 } |
|
1126 } |
|
1127 return WSASendTo(s, bufs, bufcnt, sent, flags, (*RawSockaddrAny)(unsafe.Pointer(rsa)), l, overlapped, croutine) |
|
1128 } |
|
1129 |
|
1130 func LoadGetAddrInfo() error { |
|
1131 return procGetAddrInfoW.Find() |
|
1132 } |
|
1133 |
|
1134 var connectExFunc struct { |
|
1135 once sync.Once |
|
1136 addr uintptr |
|
1137 err error |
|
1138 } |
|
1139 |
|
1140 func LoadConnectEx() error { |
|
1141 connectExFunc.once.Do(func() { |
|
1142 var s Handle |
|
1143 s, connectExFunc.err = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) |
|
1144 if connectExFunc.err != nil { |
|
1145 return |
|
1146 } |
|
1147 defer CloseHandle(s) |
|
1148 var n uint32 |
|
1149 connectExFunc.err = WSAIoctl(s, |
|
1150 SIO_GET_EXTENSION_FUNCTION_POINTER, |
|
1151 (*byte)(unsafe.Pointer(&WSAID_CONNECTEX)), |
|
1152 uint32(unsafe.Sizeof(WSAID_CONNECTEX)), |
|
1153 (*byte)(unsafe.Pointer(&connectExFunc.addr)), |
|
1154 uint32(unsafe.Sizeof(connectExFunc.addr)), |
|
1155 &n, nil, 0) |
|
1156 }) |
|
1157 return connectExFunc.err |
|
1158 } |
|
1159 |
|
1160 func connectEx(s Handle, name unsafe.Pointer, namelen int32, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) (err error) { |
|
1161 r1, _, e1 := syscall.Syscall9(connectExFunc.addr, 7, uintptr(s), uintptr(name), uintptr(namelen), uintptr(unsafe.Pointer(sendBuf)), uintptr(sendDataLen), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), 0, 0) |
|
1162 if r1 == 0 { |
|
1163 if e1 != 0 { |
|
1164 err = error(e1) |
|
1165 } else { |
|
1166 err = syscall.EINVAL |
|
1167 } |
|
1168 } |
|
1169 return |
|
1170 } |
|
1171 |
|
1172 func ConnectEx(fd Handle, sa Sockaddr, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) error { |
|
1173 err := LoadConnectEx() |
|
1174 if err != nil { |
|
1175 return errorspkg.New("failed to find ConnectEx: " + err.Error()) |
|
1176 } |
|
1177 ptr, n, err := sa.sockaddr() |
|
1178 if err != nil { |
|
1179 return err |
|
1180 } |
|
1181 return connectEx(fd, ptr, n, sendBuf, sendDataLen, bytesSent, overlapped) |
|
1182 } |
|
1183 |
|
1184 var sendRecvMsgFunc struct { |
|
1185 once sync.Once |
|
1186 sendAddr uintptr |
|
1187 recvAddr uintptr |
|
1188 err error |
|
1189 } |
|
1190 |
|
1191 func loadWSASendRecvMsg() error { |
|
1192 sendRecvMsgFunc.once.Do(func() { |
|
1193 var s Handle |
|
1194 s, sendRecvMsgFunc.err = Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) |
|
1195 if sendRecvMsgFunc.err != nil { |
|
1196 return |
|
1197 } |
|
1198 defer CloseHandle(s) |
|
1199 var n uint32 |
|
1200 sendRecvMsgFunc.err = WSAIoctl(s, |
|
1201 SIO_GET_EXTENSION_FUNCTION_POINTER, |
|
1202 (*byte)(unsafe.Pointer(&WSAID_WSARECVMSG)), |
|
1203 uint32(unsafe.Sizeof(WSAID_WSARECVMSG)), |
|
1204 (*byte)(unsafe.Pointer(&sendRecvMsgFunc.recvAddr)), |
|
1205 uint32(unsafe.Sizeof(sendRecvMsgFunc.recvAddr)), |
|
1206 &n, nil, 0) |
|
1207 if sendRecvMsgFunc.err != nil { |
|
1208 return |
|
1209 } |
|
1210 sendRecvMsgFunc.err = WSAIoctl(s, |
|
1211 SIO_GET_EXTENSION_FUNCTION_POINTER, |
|
1212 (*byte)(unsafe.Pointer(&WSAID_WSASENDMSG)), |
|
1213 uint32(unsafe.Sizeof(WSAID_WSASENDMSG)), |
|
1214 (*byte)(unsafe.Pointer(&sendRecvMsgFunc.sendAddr)), |
|
1215 uint32(unsafe.Sizeof(sendRecvMsgFunc.sendAddr)), |
|
1216 &n, nil, 0) |
|
1217 }) |
|
1218 return sendRecvMsgFunc.err |
|
1219 } |
|
1220 |
|
1221 func WSASendMsg(fd Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlapped *Overlapped, croutine *byte) error { |
|
1222 err := loadWSASendRecvMsg() |
|
1223 if err != nil { |
|
1224 return err |
|
1225 } |
|
1226 r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.sendAddr, 6, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(flags), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine))) |
|
1227 if r1 == socket_error { |
|
1228 err = errnoErr(e1) |
|
1229 } |
|
1230 return err |
|
1231 } |
|
1232 |
|
1233 func WSARecvMsg(fd Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *Overlapped, croutine *byte) error { |
|
1234 err := loadWSASendRecvMsg() |
|
1235 if err != nil { |
|
1236 return err |
|
1237 } |
|
1238 r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.recvAddr, 5, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(unsafe.Pointer(bytesReceived)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)), 0) |
|
1239 if r1 == socket_error { |
|
1240 err = errnoErr(e1) |
|
1241 } |
|
1242 return err |
|
1243 } |
|
1244 |
|
1245 // Invented structures to support what package os expects. |
|
1246 type Rusage struct { |
|
1247 CreationTime Filetime |
|
1248 ExitTime Filetime |
|
1249 KernelTime Filetime |
|
1250 UserTime Filetime |
|
1251 } |
|
1252 |
|
1253 type WaitStatus struct { |
|
1254 ExitCode uint32 |
|
1255 } |
|
1256 |
|
1257 func (w WaitStatus) Exited() bool { return true } |
|
1258 |
|
1259 func (w WaitStatus) ExitStatus() int { return int(w.ExitCode) } |
|
1260 |
|
1261 func (w WaitStatus) Signal() Signal { return -1 } |
|
1262 |
|
1263 func (w WaitStatus) CoreDump() bool { return false } |
|
1264 |
|
1265 func (w WaitStatus) Stopped() bool { return false } |
|
1266 |
|
1267 func (w WaitStatus) Continued() bool { return false } |
|
1268 |
|
1269 func (w WaitStatus) StopSignal() Signal { return -1 } |
|
1270 |
|
1271 func (w WaitStatus) Signaled() bool { return false } |
|
1272 |
|
1273 func (w WaitStatus) TrapCause() int { return -1 } |
|
1274 |
|
1275 // Timespec is an invented structure on Windows, but here for |
|
1276 // consistency with the corresponding package for other operating systems. |
|
1277 type Timespec struct { |
|
1278 Sec int64 |
|
1279 Nsec int64 |
|
1280 } |
|
1281 |
|
1282 func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } |
|
1283 |
|
1284 func NsecToTimespec(nsec int64) (ts Timespec) { |
|
1285 ts.Sec = nsec / 1e9 |
|
1286 ts.Nsec = nsec % 1e9 |
|
1287 return |
|
1288 } |
|
1289 |
|
1290 // TODO(brainman): fix all needed for net |
|
1291 |
|
1292 func Accept(fd Handle) (nfd Handle, sa Sockaddr, err error) { return 0, nil, syscall.EWINDOWS } |
|
1293 |
|
1294 func Recvfrom(fd Handle, p []byte, flags int) (n int, from Sockaddr, err error) { |
|
1295 var rsa RawSockaddrAny |
|
1296 l := int32(unsafe.Sizeof(rsa)) |
|
1297 n32, err := recvfrom(fd, p, int32(flags), &rsa, &l) |
|
1298 n = int(n32) |
|
1299 if err != nil { |
|
1300 return |
|
1301 } |
|
1302 from, err = rsa.Sockaddr() |
|
1303 return |
|
1304 } |
|
1305 |
|
1306 func Sendto(fd Handle, p []byte, flags int, to Sockaddr) (err error) { |
|
1307 ptr, l, err := to.sockaddr() |
|
1308 if err != nil { |
|
1309 return err |
|
1310 } |
|
1311 return sendto(fd, p, int32(flags), ptr, l) |
|
1312 } |
|
1313 |
|
1314 func SetsockoptTimeval(fd Handle, level, opt int, tv *Timeval) (err error) { return syscall.EWINDOWS } |
|
1315 |
|
1316 // The Linger struct is wrong but we only noticed after Go 1. |
|
1317 // sysLinger is the real system call structure. |
|
1318 |
|
1319 // BUG(brainman): The definition of Linger is not appropriate for direct use |
|
1320 // with Setsockopt and Getsockopt. |
|
1321 // Use SetsockoptLinger instead. |
|
1322 |
|
1323 type Linger struct { |
|
1324 Onoff int32 |
|
1325 Linger int32 |
|
1326 } |
|
1327 |
|
1328 type sysLinger struct { |
|
1329 Onoff uint16 |
|
1330 Linger uint16 |
|
1331 } |
|
1332 |
|
1333 type IPMreq struct { |
|
1334 Multiaddr [4]byte /* in_addr */ |
|
1335 Interface [4]byte /* in_addr */ |
|
1336 } |
|
1337 |
|
1338 type IPv6Mreq struct { |
|
1339 Multiaddr [16]byte /* in6_addr */ |
|
1340 Interface uint32 |
|
1341 } |
|
1342 |
|
1343 func GetsockoptInt(fd Handle, level, opt int) (int, error) { |
|
1344 v := int32(0) |
|
1345 l := int32(unsafe.Sizeof(v)) |
|
1346 err := Getsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&v)), &l) |
|
1347 return int(v), err |
|
1348 } |
|
1349 |
|
1350 func SetsockoptLinger(fd Handle, level, opt int, l *Linger) (err error) { |
|
1351 sys := sysLinger{Onoff: uint16(l.Onoff), Linger: uint16(l.Linger)} |
|
1352 return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&sys)), int32(unsafe.Sizeof(sys))) |
|
1353 } |
|
1354 |
|
1355 func SetsockoptInet4Addr(fd Handle, level, opt int, value [4]byte) (err error) { |
|
1356 return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&value[0])), 4) |
|
1357 } |
|
1358 func SetsockoptIPMreq(fd Handle, level, opt int, mreq *IPMreq) (err error) { |
|
1359 return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(mreq)), int32(unsafe.Sizeof(*mreq))) |
|
1360 } |
|
1361 func SetsockoptIPv6Mreq(fd Handle, level, opt int, mreq *IPv6Mreq) (err error) { |
|
1362 return syscall.EWINDOWS |
|
1363 } |
|
1364 |
|
1365 func Getpid() (pid int) { return int(GetCurrentProcessId()) } |
|
1366 |
|
1367 func FindFirstFile(name *uint16, data *Win32finddata) (handle Handle, err error) { |
|
1368 // NOTE(rsc): The Win32finddata struct is wrong for the system call: |
|
1369 // the two paths are each one uint16 short. Use the correct struct, |
|
1370 // a win32finddata1, and then copy the results out. |
|
1371 // There is no loss of expressivity here, because the final |
|
1372 // uint16, if it is used, is supposed to be a NUL, and Go doesn't need that. |
|
1373 // For Go 1.1, we might avoid the allocation of win32finddata1 here |
|
1374 // by adding a final Bug [2]uint16 field to the struct and then |
|
1375 // adjusting the fields in the result directly. |
|
1376 var data1 win32finddata1 |
|
1377 handle, err = findFirstFile1(name, &data1) |
|
1378 if err == nil { |
|
1379 copyFindData(data, &data1) |
|
1380 } |
|
1381 return |
|
1382 } |
|
1383 |
|
1384 func FindNextFile(handle Handle, data *Win32finddata) (err error) { |
|
1385 var data1 win32finddata1 |
|
1386 err = findNextFile1(handle, &data1) |
|
1387 if err == nil { |
|
1388 copyFindData(data, &data1) |
|
1389 } |
|
1390 return |
|
1391 } |
|
1392 |
|
1393 func getProcessEntry(pid int) (*ProcessEntry32, error) { |
|
1394 snapshot, err := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) |
|
1395 if err != nil { |
|
1396 return nil, err |
|
1397 } |
|
1398 defer CloseHandle(snapshot) |
|
1399 var procEntry ProcessEntry32 |
|
1400 procEntry.Size = uint32(unsafe.Sizeof(procEntry)) |
|
1401 if err = Process32First(snapshot, &procEntry); err != nil { |
|
1402 return nil, err |
|
1403 } |
|
1404 for { |
|
1405 if procEntry.ProcessID == uint32(pid) { |
|
1406 return &procEntry, nil |
|
1407 } |
|
1408 err = Process32Next(snapshot, &procEntry) |
|
1409 if err != nil { |
|
1410 return nil, err |
|
1411 } |
|
1412 } |
|
1413 } |
|
1414 |
|
1415 func Getppid() (ppid int) { |
|
1416 pe, err := getProcessEntry(Getpid()) |
|
1417 if err != nil { |
|
1418 return -1 |
|
1419 } |
|
1420 return int(pe.ParentProcessID) |
|
1421 } |
|
1422 |
|
1423 // TODO(brainman): fix all needed for os |
|
1424 func Fchdir(fd Handle) (err error) { return syscall.EWINDOWS } |
|
1425 func Link(oldpath, newpath string) (err error) { return syscall.EWINDOWS } |
|
1426 func Symlink(path, link string) (err error) { return syscall.EWINDOWS } |
|
1427 |
|
1428 func Fchmod(fd Handle, mode uint32) (err error) { return syscall.EWINDOWS } |
|
1429 func Chown(path string, uid int, gid int) (err error) { return syscall.EWINDOWS } |
|
1430 func Lchown(path string, uid int, gid int) (err error) { return syscall.EWINDOWS } |
|
1431 func Fchown(fd Handle, uid int, gid int) (err error) { return syscall.EWINDOWS } |
|
1432 |
|
1433 func Getuid() (uid int) { return -1 } |
|
1434 func Geteuid() (euid int) { return -1 } |
|
1435 func Getgid() (gid int) { return -1 } |
|
1436 func Getegid() (egid int) { return -1 } |
|
1437 func Getgroups() (gids []int, err error) { return nil, syscall.EWINDOWS } |
|
1438 |
|
1439 type Signal int |
|
1440 |
|
1441 func (s Signal) Signal() {} |
|
1442 |
|
1443 func (s Signal) String() string { |
|
1444 if 0 <= s && int(s) < len(signals) { |
|
1445 str := signals[s] |
|
1446 if str != "" { |
|
1447 return str |
|
1448 } |
|
1449 } |
|
1450 return "signal " + itoa(int(s)) |
|
1451 } |
|
1452 |
|
1453 func LoadCreateSymbolicLink() error { |
|
1454 return procCreateSymbolicLinkW.Find() |
|
1455 } |
|
1456 |
|
1457 // Readlink returns the destination of the named symbolic link. |
|
1458 func Readlink(path string, buf []byte) (n int, err error) { |
|
1459 fd, err := CreateFile(StringToUTF16Ptr(path), GENERIC_READ, 0, nil, OPEN_EXISTING, |
|
1460 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, 0) |
|
1461 if err != nil { |
|
1462 return -1, err |
|
1463 } |
|
1464 defer CloseHandle(fd) |
|
1465 |
|
1466 rdbbuf := make([]byte, MAXIMUM_REPARSE_DATA_BUFFER_SIZE) |
|
1467 var bytesReturned uint32 |
|
1468 err = DeviceIoControl(fd, FSCTL_GET_REPARSE_POINT, nil, 0, &rdbbuf[0], uint32(len(rdbbuf)), &bytesReturned, nil) |
|
1469 if err != nil { |
|
1470 return -1, err |
|
1471 } |
|
1472 |
|
1473 rdb := (*reparseDataBuffer)(unsafe.Pointer(&rdbbuf[0])) |
|
1474 var s string |
|
1475 switch rdb.ReparseTag { |
|
1476 case IO_REPARSE_TAG_SYMLINK: |
|
1477 data := (*symbolicLinkReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer)) |
|
1478 p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0])) |
|
1479 s = UTF16ToString(p[data.PrintNameOffset/2 : (data.PrintNameLength-data.PrintNameOffset)/2]) |
|
1480 case IO_REPARSE_TAG_MOUNT_POINT: |
|
1481 data := (*mountPointReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer)) |
|
1482 p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0])) |
|
1483 s = UTF16ToString(p[data.PrintNameOffset/2 : (data.PrintNameLength-data.PrintNameOffset)/2]) |
|
1484 default: |
|
1485 // the path is not a symlink or junction but another type of reparse |
|
1486 // point |
|
1487 return -1, syscall.ENOENT |
|
1488 } |
|
1489 n = copy(buf, []byte(s)) |
|
1490 |
|
1491 return n, nil |
|
1492 } |
|
1493 |
|
1494 // GUIDFromString parses a string in the form of |
|
1495 // "{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" into a GUID. |
|
1496 func GUIDFromString(str string) (GUID, error) { |
|
1497 guid := GUID{} |
|
1498 str16, err := syscall.UTF16PtrFromString(str) |
|
1499 if err != nil { |
|
1500 return guid, err |
|
1501 } |
|
1502 err = clsidFromString(str16, &guid) |
|
1503 if err != nil { |
|
1504 return guid, err |
|
1505 } |
|
1506 return guid, nil |
|
1507 } |
|
1508 |
|
1509 // GenerateGUID creates a new random GUID. |
|
1510 func GenerateGUID() (GUID, error) { |
|
1511 guid := GUID{} |
|
1512 err := coCreateGuid(&guid) |
|
1513 if err != nil { |
|
1514 return guid, err |
|
1515 } |
|
1516 return guid, nil |
|
1517 } |
|
1518 |
|
1519 // String returns the canonical string form of the GUID, |
|
1520 // in the form of "{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}". |
|
1521 func (guid GUID) String() string { |
|
1522 var str [100]uint16 |
|
1523 chars := stringFromGUID2(&guid, &str[0], int32(len(str))) |
|
1524 if chars <= 1 { |
|
1525 return "" |
|
1526 } |
|
1527 return string(utf16.Decode(str[:chars-1])) |
|
1528 } |
|
1529 |
|
1530 // KnownFolderPath returns a well-known folder path for the current user, specified by one of |
|
1531 // the FOLDERID_ constants, and chosen and optionally created based on a KF_ flag. |
|
1532 func KnownFolderPath(folderID *KNOWNFOLDERID, flags uint32) (string, error) { |
|
1533 return Token(0).KnownFolderPath(folderID, flags) |
|
1534 } |
|
1535 |
|
1536 // KnownFolderPath returns a well-known folder path for the user token, specified by one of |
|
1537 // the FOLDERID_ constants, and chosen and optionally created based on a KF_ flag. |
|
1538 func (t Token) KnownFolderPath(folderID *KNOWNFOLDERID, flags uint32) (string, error) { |
|
1539 var p *uint16 |
|
1540 err := shGetKnownFolderPath(folderID, flags, t, &p) |
|
1541 if err != nil { |
|
1542 return "", err |
|
1543 } |
|
1544 defer CoTaskMemFree(unsafe.Pointer(p)) |
|
1545 return UTF16PtrToString(p), nil |
|
1546 } |
|
1547 |
|
1548 // RtlGetVersion returns the version of the underlying operating system, ignoring |
|
1549 // manifest semantics but is affected by the application compatibility layer. |
|
1550 func RtlGetVersion() *OsVersionInfoEx { |
|
1551 info := &OsVersionInfoEx{} |
|
1552 info.osVersionInfoSize = uint32(unsafe.Sizeof(*info)) |
|
1553 // According to documentation, this function always succeeds. |
|
1554 // The function doesn't even check the validity of the |
|
1555 // osVersionInfoSize member. Disassembling ntdll.dll indicates |
|
1556 // that the documentation is indeed correct about that. |
|
1557 _ = rtlGetVersion(info) |
|
1558 return info |
|
1559 } |
|
1560 |
|
1561 // RtlGetNtVersionNumbers returns the version of the underlying operating system, |
|
1562 // ignoring manifest semantics and the application compatibility layer. |
|
1563 func RtlGetNtVersionNumbers() (majorVersion, minorVersion, buildNumber uint32) { |
|
1564 rtlGetNtVersionNumbers(&majorVersion, &minorVersion, &buildNumber) |
|
1565 buildNumber &= 0xffff |
|
1566 return |
|
1567 } |
|
1568 |
|
1569 // GetProcessPreferredUILanguages retrieves the process preferred UI languages. |
|
1570 func GetProcessPreferredUILanguages(flags uint32) ([]string, error) { |
|
1571 return getUILanguages(flags, getProcessPreferredUILanguages) |
|
1572 } |
|
1573 |
|
1574 // GetThreadPreferredUILanguages retrieves the thread preferred UI languages for the current thread. |
|
1575 func GetThreadPreferredUILanguages(flags uint32) ([]string, error) { |
|
1576 return getUILanguages(flags, getThreadPreferredUILanguages) |
|
1577 } |
|
1578 |
|
1579 // GetUserPreferredUILanguages retrieves information about the user preferred UI languages. |
|
1580 func GetUserPreferredUILanguages(flags uint32) ([]string, error) { |
|
1581 return getUILanguages(flags, getUserPreferredUILanguages) |
|
1582 } |
|
1583 |
|
1584 // GetSystemPreferredUILanguages retrieves the system preferred UI languages. |
|
1585 func GetSystemPreferredUILanguages(flags uint32) ([]string, error) { |
|
1586 return getUILanguages(flags, getSystemPreferredUILanguages) |
|
1587 } |
|
1588 |
|
1589 func getUILanguages(flags uint32, f func(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) error) ([]string, error) { |
|
1590 size := uint32(128) |
|
1591 for { |
|
1592 var numLanguages uint32 |
|
1593 buf := make([]uint16, size) |
|
1594 err := f(flags, &numLanguages, &buf[0], &size) |
|
1595 if err == ERROR_INSUFFICIENT_BUFFER { |
|
1596 continue |
|
1597 } |
|
1598 if err != nil { |
|
1599 return nil, err |
|
1600 } |
|
1601 buf = buf[:size] |
|
1602 if numLanguages == 0 || len(buf) == 0 { // GetProcessPreferredUILanguages may return numLanguages==0 with "\0\0" |
|
1603 return []string{}, nil |
|
1604 } |
|
1605 if buf[len(buf)-1] == 0 { |
|
1606 buf = buf[:len(buf)-1] // remove terminating null |
|
1607 } |
|
1608 languages := make([]string, 0, numLanguages) |
|
1609 from := 0 |
|
1610 for i, c := range buf { |
|
1611 if c == 0 { |
|
1612 languages = append(languages, string(utf16.Decode(buf[from:i]))) |
|
1613 from = i + 1 |
|
1614 } |
|
1615 } |
|
1616 return languages, nil |
|
1617 } |
|
1618 } |
|
1619 |
|
1620 func SetConsoleCursorPosition(console Handle, position Coord) error { |
|
1621 return setConsoleCursorPosition(console, *((*uint32)(unsafe.Pointer(&position)))) |
|
1622 } |
|
1623 |
|
1624 func (s NTStatus) Errno() syscall.Errno { |
|
1625 return rtlNtStatusToDosErrorNoTeb(s) |
|
1626 } |
|
1627 |
|
1628 func langID(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) } |
|
1629 |
|
1630 func (s NTStatus) Error() string { |
|
1631 b := make([]uint16, 300) |
|
1632 n, err := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_ARGUMENT_ARRAY, modntdll.Handle(), uint32(s), langID(LANG_ENGLISH, SUBLANG_ENGLISH_US), b, nil) |
|
1633 if err != nil { |
|
1634 return fmt.Sprintf("NTSTATUS 0x%08x", uint32(s)) |
|
1635 } |
|
1636 // trim terminating \r and \n |
|
1637 for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- { |
|
1638 } |
|
1639 return string(utf16.Decode(b[:n])) |
|
1640 } |
|
1641 |
|
1642 // NewNTUnicodeString returns a new NTUnicodeString structure for use with native |
|
1643 // NT APIs that work over the NTUnicodeString type. Note that most Windows APIs |
|
1644 // do not use NTUnicodeString, and instead UTF16PtrFromString should be used for |
|
1645 // the more common *uint16 string type. |
|
1646 func NewNTUnicodeString(s string) (*NTUnicodeString, error) { |
|
1647 var u NTUnicodeString |
|
1648 s16, err := UTF16PtrFromString(s) |
|
1649 if err != nil { |
|
1650 return nil, err |
|
1651 } |
|
1652 RtlInitUnicodeString(&u, s16) |
|
1653 return &u, nil |
|
1654 } |
|
1655 |
|
1656 // Slice returns a uint16 slice that aliases the data in the NTUnicodeString. |
|
1657 func (s *NTUnicodeString) Slice() []uint16 { |
|
1658 var slice []uint16 |
|
1659 hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice)) |
|
1660 hdr.Data = unsafe.Pointer(s.Buffer) |
|
1661 hdr.Len = int(s.Length) |
|
1662 hdr.Cap = int(s.MaximumLength) |
|
1663 return slice |
|
1664 } |
|
1665 |
|
1666 func (s *NTUnicodeString) String() string { |
|
1667 return UTF16ToString(s.Slice()) |
|
1668 } |
|
1669 |
|
1670 // NewNTString returns a new NTString structure for use with native |
|
1671 // NT APIs that work over the NTString type. Note that most Windows APIs |
|
1672 // do not use NTString, and instead UTF16PtrFromString should be used for |
|
1673 // the more common *uint16 string type. |
|
1674 func NewNTString(s string) (*NTString, error) { |
|
1675 var nts NTString |
|
1676 s8, err := BytePtrFromString(s) |
|
1677 if err != nil { |
|
1678 return nil, err |
|
1679 } |
|
1680 RtlInitString(&nts, s8) |
|
1681 return &nts, nil |
|
1682 } |
|
1683 |
|
1684 // Slice returns a byte slice that aliases the data in the NTString. |
|
1685 func (s *NTString) Slice() []byte { |
|
1686 var slice []byte |
|
1687 hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice)) |
|
1688 hdr.Data = unsafe.Pointer(s.Buffer) |
|
1689 hdr.Len = int(s.Length) |
|
1690 hdr.Cap = int(s.MaximumLength) |
|
1691 return slice |
|
1692 } |
|
1693 |
|
1694 func (s *NTString) String() string { |
|
1695 return ByteSliceToString(s.Slice()) |
|
1696 } |
|
1697 |
|
1698 // FindResource resolves a resource of the given name and resource type. |
|
1699 func FindResource(module Handle, name, resType ResourceIDOrString) (Handle, error) { |
|
1700 var namePtr, resTypePtr uintptr |
|
1701 var name16, resType16 *uint16 |
|
1702 var err error |
|
1703 resolvePtr := func(i interface{}, keep **uint16) (uintptr, error) { |
|
1704 switch v := i.(type) { |
|
1705 case string: |
|
1706 *keep, err = UTF16PtrFromString(v) |
|
1707 if err != nil { |
|
1708 return 0, err |
|
1709 } |
|
1710 return uintptr(unsafe.Pointer(*keep)), nil |
|
1711 case ResourceID: |
|
1712 return uintptr(v), nil |
|
1713 } |
|
1714 return 0, errorspkg.New("parameter must be a ResourceID or a string") |
|
1715 } |
|
1716 namePtr, err = resolvePtr(name, &name16) |
|
1717 if err != nil { |
|
1718 return 0, err |
|
1719 } |
|
1720 resTypePtr, err = resolvePtr(resType, &resType16) |
|
1721 if err != nil { |
|
1722 return 0, err |
|
1723 } |
|
1724 resInfo, err := findResource(module, namePtr, resTypePtr) |
|
1725 runtime.KeepAlive(name16) |
|
1726 runtime.KeepAlive(resType16) |
|
1727 return resInfo, err |
|
1728 } |
|
1729 |
|
1730 func LoadResourceData(module, resInfo Handle) (data []byte, err error) { |
|
1731 size, err := SizeofResource(module, resInfo) |
|
1732 if err != nil { |
|
1733 return |
|
1734 } |
|
1735 resData, err := LoadResource(module, resInfo) |
|
1736 if err != nil { |
|
1737 return |
|
1738 } |
|
1739 ptr, err := LockResource(resData) |
|
1740 if err != nil { |
|
1741 return |
|
1742 } |
|
1743 h := (*unsafeheader.Slice)(unsafe.Pointer(&data)) |
|
1744 h.Data = unsafe.Pointer(ptr) |
|
1745 h.Len = int(size) |
|
1746 h.Cap = int(size) |
|
1747 return |
|
1748 } |
|
1749 |
|
1750 // PSAPI_WORKING_SET_EX_BLOCK contains extended working set information for a page. |
|
1751 type PSAPI_WORKING_SET_EX_BLOCK uint64 |
|
1752 |
|
1753 // Valid returns the validity of this page. |
|
1754 // If this bit is 1, the subsequent members are valid; otherwise they should be ignored. |
|
1755 func (b PSAPI_WORKING_SET_EX_BLOCK) Valid() bool { |
|
1756 return (b & 1) == 1 |
|
1757 } |
|
1758 |
|
1759 // ShareCount is the number of processes that share this page. The maximum value of this member is 7. |
|
1760 func (b PSAPI_WORKING_SET_EX_BLOCK) ShareCount() uint64 { |
|
1761 return b.intField(1, 3) |
|
1762 } |
|
1763 |
|
1764 // Win32Protection is the memory protection attributes of the page. For a list of values, see |
|
1765 // https://docs.microsoft.com/en-us/windows/win32/memory/memory-protection-constants |
|
1766 func (b PSAPI_WORKING_SET_EX_BLOCK) Win32Protection() uint64 { |
|
1767 return b.intField(4, 11) |
|
1768 } |
|
1769 |
|
1770 // Shared returns the shared status of this page. |
|
1771 // If this bit is 1, the page can be shared. |
|
1772 func (b PSAPI_WORKING_SET_EX_BLOCK) Shared() bool { |
|
1773 return (b & (1 << 15)) == 1 |
|
1774 } |
|
1775 |
|
1776 // Node is the NUMA node. The maximum value of this member is 63. |
|
1777 func (b PSAPI_WORKING_SET_EX_BLOCK) Node() uint64 { |
|
1778 return b.intField(16, 6) |
|
1779 } |
|
1780 |
|
1781 // Locked returns the locked status of this page. |
|
1782 // If this bit is 1, the virtual page is locked in physical memory. |
|
1783 func (b PSAPI_WORKING_SET_EX_BLOCK) Locked() bool { |
|
1784 return (b & (1 << 22)) == 1 |
|
1785 } |
|
1786 |
|
1787 // LargePage returns the large page status of this page. |
|
1788 // If this bit is 1, the page is a large page. |
|
1789 func (b PSAPI_WORKING_SET_EX_BLOCK) LargePage() bool { |
|
1790 return (b & (1 << 23)) == 1 |
|
1791 } |
|
1792 |
|
1793 // Bad returns the bad status of this page. |
|
1794 // If this bit is 1, the page is has been reported as bad. |
|
1795 func (b PSAPI_WORKING_SET_EX_BLOCK) Bad() bool { |
|
1796 return (b & (1 << 31)) == 1 |
|
1797 } |
|
1798 |
|
1799 // intField extracts an integer field in the PSAPI_WORKING_SET_EX_BLOCK union. |
|
1800 func (b PSAPI_WORKING_SET_EX_BLOCK) intField(start, length int) uint64 { |
|
1801 var mask PSAPI_WORKING_SET_EX_BLOCK |
|
1802 for pos := start; pos < start+length; pos++ { |
|
1803 mask |= (1 << pos) |
|
1804 } |
|
1805 |
|
1806 masked := b & mask |
|
1807 return uint64(masked >> start) |
|
1808 } |
|
1809 |
|
1810 // PSAPI_WORKING_SET_EX_INFORMATION contains extended working set information for a process. |
|
1811 type PSAPI_WORKING_SET_EX_INFORMATION struct { |
|
1812 // The virtual address. |
|
1813 VirtualAddress Pointer |
|
1814 // A PSAPI_WORKING_SET_EX_BLOCK union that indicates the attributes of the page at VirtualAddress. |
|
1815 VirtualAttributes PSAPI_WORKING_SET_EX_BLOCK |
|
1816 } |