| 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 syscall |
| 8 |
|
| 9 |
import ( |
| 10 |
errorspkg "errors" |
| 11 |
"internal/asan" |
| 12 |
"internal/bytealg" |
| 13 |
"internal/msan" |
| 14 |
"internal/oserror" |
| 15 |
"internal/race" |
| 16 |
"internal/strconv" |
| 17 |
"sync" |
| 18 |
"unsafe" |
| 19 |
) |
| 20 |
|
| 21 |
type Handle uintptr |
| 22 |
|
| 23 |
const InvalidHandle = ^Handle(0) |
| 24 |
|
| 25 |
// StringToUTF16 returns the UTF-16 encoding of the UTF-8 string s, |
| 26 |
// with a terminating NUL added. If s contains a NUL byte this |
| 27 |
// function panics instead of returning an error. |
| 28 |
// |
| 29 |
// Deprecated: Use [UTF16FromString] instead. |
| 30 |
func StringToUTF16(s string) []uint16 { |
| 31 |
a, err := UTF16FromString(s) |
| 32 |
if err != nil { |
| 33 |
panic("syscall: string with NUL passed to StringToUTF16") |
| 34 |
} |
| 35 |
return a |
| 36 |
} |
| 37 |
|
| 38 |
// UTF16FromString returns the UTF-16 encoding of the UTF-8 string |
| 39 |
// s, with a terminating NUL added. If s contains a NUL byte at any |
| 40 |
// location, it returns (nil, [EINVAL]). Unpaired surrogates |
| 41 |
// are encoded using WTF-8. |
| 42 |
func UTF16FromString(s string) ([]uint16, error) { |
| 43 |
if bytealg.IndexByteString(s, 0) != -1 { |
| 44 |
return nil, EINVAL |
| 45 |
} |
| 46 |
// Valid UTF-8 characters between 1 and 3 bytes require one uint16. |
| 47 |
// Valid UTF-8 characters of 4 bytes require two uint16. |
| 48 |
// Bytes with invalid UTF-8 encoding require maximum one uint16 per byte. |
| 49 |
// So the number of UTF-8 code units (len(s)) is always greater or |
| 50 |
// equal than the number of UTF-16 code units. |
| 51 |
// Also account for the terminating NUL character. |
| 52 |
buf := make([]uint16, 0, len(s)+1) |
| 53 |
buf = encodeWTF16(s, buf) |
| 54 |
return append(buf, 0), nil |
| 55 |
} |
| 56 |
|
| 57 |
// UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, |
| 58 |
// with a terminating NUL removed. Unpaired surrogates are decoded |
| 59 |
// using WTF-8 instead of UTF-8 encoding. |
| 60 |
func UTF16ToString(s []uint16) string { |
| 61 |
maxLen := 0 |
| 62 |
for i, v := range s { |
| 63 |
if v == 0 { |
| 64 |
s = s[0:i] |
| 65 |
break |
| 66 |
} |
| 67 |
switch { |
| 68 |
case v <= rune1Max: |
| 69 |
maxLen += 1 |
| 70 |
case v <= rune2Max: |
| 71 |
maxLen += 2 |
| 72 |
default: |
| 73 |
// r is a non-surrogate that decodes to 3 bytes, |
| 74 |
// or is an unpaired surrogate (also 3 bytes in WTF-8), |
| 75 |
// or is one half of a valid surrogate pair. |
| 76 |
// If it is half of a pair, we will add 3 for the second surrogate |
| 77 |
// (total of 6) and overestimate by 2 bytes for the pair, |
| 78 |
// since the resulting rune only requires 4 bytes. |
| 79 |
maxLen += 3 |
| 80 |
} |
| 81 |
} |
| 82 |
buf := decodeWTF16(s, make([]byte, 0, maxLen)) |
| 83 |
return unsafe.String(unsafe.SliceData(buf), len(buf)) |
| 84 |
} |
| 85 |
|
| 86 |
// utf16PtrToString is like UTF16ToString, but takes *uint16 |
| 87 |
// as a parameter instead of []uint16. |
| 88 |
func utf16PtrToString(p *uint16) string { |
| 89 |
if p == nil { |
| 90 |
return "" |
| 91 |
} |
| 92 |
end := unsafe.Pointer(p) |
| 93 |
n := 0 |
| 94 |
for *(*uint16)(end) != 0 { |
| 95 |
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p)) |
| 96 |
n++ |
| 97 |
} |
| 98 |
return UTF16ToString(unsafe.Slice(p, n)) |
| 99 |
} |
| 100 |
|
| 101 |
// StringToUTF16Ptr returns pointer to the UTF-16 encoding of |
| 102 |
// the UTF-8 string s, with a terminating NUL added. If s |
| 103 |
// contains a NUL byte this function panics instead of |
| 104 |
// returning an error. |
| 105 |
// |
| 106 |
// Deprecated: Use [UTF16PtrFromString] instead. |
| 107 |
func StringToUTF16Ptr(s string) *uint16 { return &StringToUTF16(s)[0] } |
| 108 |
|
| 109 |
// UTF16PtrFromString returns pointer to the UTF-16 encoding of |
| 110 |
// the UTF-8 string s, with a terminating NUL added. If s |
| 111 |
// contains a NUL byte at any location, it returns (nil, EINVAL). |
| 112 |
// Unpaired surrogates are encoded using WTF-8. |
| 113 |
func UTF16PtrFromString(s string) (*uint16, error) { |
| 114 |
a, err := UTF16FromString(s) |
| 115 |
if err != nil { |
| 116 |
return nil, err |
| 117 |
} |
| 118 |
return &a[0], nil |
| 119 |
} |
| 120 |
|
| 121 |
// Errno is the Windows error number. |
| 122 |
// |
| 123 |
// Errno values can be tested against error values using [errors.Is]. |
| 124 |
// For example: |
| 125 |
// |
| 126 |
// _, _, err := syscall.Syscall(...) |
| 127 |
// if errors.Is(err, fs.ErrNotExist) ... |
| 128 |
type Errno uintptr |
| 129 |
|
| 130 |
func langid(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) } |
| 131 |
|
| 132 |
// FormatMessage is deprecated (msgsrc should be uintptr, not uint32, but can |
| 133 |
// not be changed due to the Go 1 compatibility guarantee). |
| 134 |
// |
| 135 |
// Deprecated: Use FormatMessage from golang.org/x/sys/windows instead. |
| 136 |
func FormatMessage(flags uint32, msgsrc uint32, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) { |
| 137 |
return formatMessage(flags, uintptr(msgsrc), msgid, langid, buf, args) |
| 138 |
} |
| 139 |
|
| 140 |
var errnoErrorCache sync.Map |
| 141 |
|
| 142 |
func (e Errno) Error() string { |
| 143 |
// deal with special go errors |
| 144 |
idx := int(e - APPLICATION_ERROR) |
| 145 |
if 0 <= idx && idx < len(errors) { |
| 146 |
return errors[idx] |
| 147 |
} |
| 148 |
|
| 149 |
cache := false |
| 150 |
switch e { |
| 151 |
case ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND: |
| 152 |
if cached, ok := errnoErrorCache.Load(e); ok { |
| 153 |
return cached.(string) |
| 154 |
} |
| 155 |
cache = true |
| 156 |
} |
| 157 |
|
| 158 |
result := e.error() |
| 159 |
if cache { |
| 160 |
errnoErrorCache.Store(e, result) |
| 161 |
} |
| 162 |
return result |
| 163 |
} |
| 164 |
|
| 165 |
func (e Errno) error() string { |
| 166 |
// ask windows for the remaining errors |
| 167 |
var flags uint32 = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_IGNORE_INSERTS |
| 168 |
b := make([]uint16, 300) |
| 169 |
n, err := formatMessage(flags, 0, uint32(e), langid(LANG_ENGLISH, SUBLANG_ENGLISH_US), b, nil) |
| 170 |
if err != nil { |
| 171 |
n, err = formatMessage(flags, 0, uint32(e), 0, b, nil) |
| 172 |
if err != nil { |
| 173 |
return "winapi error #" + strconv.Itoa(int(e)) |
| 174 |
} |
| 175 |
} |
| 176 |
// trim terminating \r and \n |
| 177 |
for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- { |
| 178 |
} |
| 179 |
return UTF16ToString(b[:n]) |
| 180 |
} |
| 181 |
|
| 182 |
const ( |
| 183 |
_ERROR_NOT_ENOUGH_MEMORY = Errno(8) |
| 184 |
_ERROR_NOT_SUPPORTED = Errno(50) |
| 185 |
_ERROR_BAD_NETPATH = Errno(53) |
| 186 |
_ERROR_CALL_NOT_IMPLEMENTED = Errno(120) |
| 187 |
) |
| 188 |
|
| 189 |
func (e Errno) Is(target error) bool { |
| 190 |
switch target { |
| 191 |
case oserror.ErrPermission: |
| 192 |
return e == ERROR_ACCESS_DENIED || |
| 193 |
e == EACCES || |
| 194 |
e == EPERM |
| 195 |
case oserror.ErrExist: |
| 196 |
return e == ERROR_ALREADY_EXISTS || |
| 197 |
e == ERROR_DIR_NOT_EMPTY || |
| 198 |
e == ERROR_FILE_EXISTS || |
| 199 |
e == EEXIST || |
| 200 |
e == ENOTEMPTY |
| 201 |
case oserror.ErrNotExist: |
| 202 |
return e == ERROR_FILE_NOT_FOUND || |
| 203 |
e == _ERROR_BAD_NETPATH || |
| 204 |
e == ERROR_PATH_NOT_FOUND || |
| 205 |
e == ENOENT |
| 206 |
case errorspkg.ErrUnsupported: |
| 207 |
return e == _ERROR_NOT_SUPPORTED || |
| 208 |
e == _ERROR_CALL_NOT_IMPLEMENTED || |
| 209 |
e == ENOSYS || |
| 210 |
e == ENOTSUP || |
| 211 |
e == EOPNOTSUPP || |
| 212 |
e == EWINDOWS |
| 213 |
} |
| 214 |
return false |
| 215 |
} |
| 216 |
|
| 217 |
func (e Errno) Temporary() bool { |
| 218 |
return e == EINTR || e == EMFILE || e.Timeout() |
| 219 |
} |
| 220 |
|
| 221 |
func (e Errno) Timeout() bool { |
| 222 |
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT |
| 223 |
} |
| 224 |
|
| 225 |
// Implemented in runtime/syscall_windows.go. |
| 226 |
func compileCallback(fn any, cleanstack bool) uintptr |
| 227 |
|
| 228 |
// NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention. |
| 229 |
// This is useful when interoperating with Windows code requiring callbacks. |
| 230 |
// The argument is expected to be a function with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr. |
| 231 |
// Only a limited number of callbacks may be created in a single Go process, and any memory allocated |
| 232 |
// for these callbacks is never released. |
| 233 |
// Between NewCallback and NewCallbackCDecl, at least 1024 callbacks can always be created. |
| 234 |
func NewCallback(fn any) uintptr { |
| 235 |
return compileCallback(fn, true) |
| 236 |
} |
| 237 |
|
| 238 |
// NewCallbackCDecl converts a Go function to a function pointer conforming to the cdecl calling convention. |
| 239 |
// This is useful when interoperating with Windows code requiring callbacks. |
| 240 |
// The argument is expected to be a function with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr. |
| 241 |
// Only a limited number of callbacks may be created in a single Go process, and any memory allocated |
| 242 |
// for these callbacks is never released. |
| 243 |
// Between NewCallback and NewCallbackCDecl, at least 1024 callbacks can always be created. |
| 244 |
func NewCallbackCDecl(fn any) uintptr { |
| 245 |
return compileCallback(fn, false) |
| 246 |
} |
| 247 |
|
| 248 |
// windows api calls |
| 249 |
|
| 250 |
//sys GetLastError() (lasterr error) |
| 251 |
//sys LoadLibrary(libname string) (handle Handle, err error) = LoadLibraryW |
| 252 |
//sys FreeLibrary(handle Handle) (err error) |
| 253 |
//sys GetProcAddress(module Handle, procname string) (proc uintptr, err error) |
| 254 |
//sys GetVersion() (ver uint32, err error) |
| 255 |
//sys formatMessage(flags uint32, msgsrc uintptr, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) = FormatMessageW |
| 256 |
//sys ExitProcess(exitcode uint32) |
| 257 |
//sys createFile(name *uint16, access uint32, mode uint32, sa *SecurityAttributes, createmode uint32, attrs uint32, templatefile int32) (handle Handle, err error) [failretval == InvalidHandle || e1 == ERROR_ALREADY_EXISTS ] = CreateFileW |
| 258 |
//sys readFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error) = ReadFile |
| 259 |
//sys writeFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error) = WriteFile |
| 260 |
//sys SetFilePointer(handle Handle, lowoffset int32, highoffsetptr *int32, whence uint32) (newlowoffset uint32, err error) [failretval==0xffffffff] |
| 261 |
//sys CloseHandle(handle Handle) (err error) |
| 262 |
//sys GetStdHandle(stdhandle int) (handle Handle, err error) [failretval==InvalidHandle] |
| 263 |
//sys findFirstFile1(name *uint16, data *win32finddata1) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstFileW |
| 264 |
//sys findNextFile1(handle Handle, data *win32finddata1) (err error) = FindNextFileW |
| 265 |
//sys FindClose(handle Handle) (err error) |
| 266 |
//sys GetFileInformationByHandle(handle Handle, data *ByHandleFileInformation) (err error) |
| 267 |
//sys GetCurrentDirectory(buflen uint32, buf *uint16) (n uint32, err error) = GetCurrentDirectoryW |
| 268 |
//sys SetCurrentDirectory(path *uint16) (err error) = SetCurrentDirectoryW |
| 269 |
//sys CreateDirectory(path *uint16, sa *SecurityAttributes) (err error) = CreateDirectoryW |
| 270 |
//sys RemoveDirectory(path *uint16) (err error) = RemoveDirectoryW |
| 271 |
//sys DeleteFile(path *uint16) (err error) = DeleteFileW |
| 272 |
//sys MoveFile(from *uint16, to *uint16) (err error) = MoveFileW |
| 273 |
//sys GetComputerName(buf *uint16, n *uint32) (err error) = GetComputerNameW |
| 274 |
//sys SetEndOfFile(handle Handle) (err error) |
| 275 |
//sys GetSystemTimeAsFileTime(time *Filetime) |
| 276 |
//sys GetTimeZoneInformation(tzi *Timezoneinformation) (rc uint32, err error) [failretval==0xffffffff] |
| 277 |
//sys createIoCompletionPort(filehandle Handle, cphandle Handle, key uintptr, threadcnt uint32) (handle Handle, err error) = CreateIoCompletionPort |
| 278 |
//sys getQueuedCompletionStatus(cphandle Handle, qty *uint32, key *uintptr, overlapped **Overlapped, timeout uint32) (err error) = GetQueuedCompletionStatus |
| 279 |
//sys postQueuedCompletionStatus(cphandle Handle, qty uint32, key uintptr, overlapped *Overlapped) (err error) = PostQueuedCompletionStatus |
| 280 |
//sys CancelIo(s Handle) (err error) |
| 281 |
//sys CancelIoEx(s Handle, o *Overlapped) (err error) |
| 282 |
//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 |
| 283 |
//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 |
| 284 |
//sys OpenProcess(da uint32, inheritHandle bool, pid uint32) (handle Handle, err error) |
| 285 |
//sys TerminateProcess(handle Handle, exitcode uint32) (err error) |
| 286 |
//sys GetExitCodeProcess(handle Handle, exitcode *uint32) (err error) |
| 287 |
//sys getStartupInfo(startupInfo *StartupInfo) = GetStartupInfoW |
| 288 |
//sys GetCurrentProcess() (pseudoHandle Handle, err error) |
| 289 |
//sys GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error) |
| 290 |
//sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error) |
| 291 |
//sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff] |
| 292 |
//sys GetTempPath(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPathW |
| 293 |
//sys CreatePipe(readhandle *Handle, writehandle *Handle, sa *SecurityAttributes, size uint32) (err error) |
| 294 |
//sys GetFileType(filehandle Handle) (n uint32, err error) |
| 295 |
//sys CryptAcquireContext(provhandle *Handle, container *uint16, provider *uint16, provtype uint32, flags uint32) (err error) = advapi32.CryptAcquireContextW |
| 296 |
//sys CryptReleaseContext(provhandle Handle, flags uint32) (err error) = advapi32.CryptReleaseContext |
| 297 |
//sys CryptGenRandom(provhandle Handle, buflen uint32, buf *byte) (err error) = advapi32.CryptGenRandom |
| 298 |
//sys GetEnvironmentStrings() (envs *uint16, err error) [failretval==nil] = kernel32.GetEnvironmentStringsW |
| 299 |
//sys FreeEnvironmentStrings(envs *uint16) (err error) = kernel32.FreeEnvironmentStringsW |
| 300 |
//sys GetEnvironmentVariable(name *uint16, buffer *uint16, size uint32) (n uint32, err error) = kernel32.GetEnvironmentVariableW |
| 301 |
//sys SetEnvironmentVariable(name *uint16, value *uint16) (err error) = kernel32.SetEnvironmentVariableW |
| 302 |
//sys SetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error) |
| 303 |
//sys GetFileAttributes(name *uint16) (attrs uint32, err error) [failretval==INVALID_FILE_ATTRIBUTES] = kernel32.GetFileAttributesW |
| 304 |
//sys SetFileAttributes(name *uint16, attrs uint32) (err error) = kernel32.SetFileAttributesW |
| 305 |
//sys GetFileAttributesEx(name *uint16, level uint32, info *byte) (err error) = kernel32.GetFileAttributesExW |
| 306 |
//sys GetCommandLine() (cmd *uint16) = kernel32.GetCommandLineW |
| 307 |
//sys CommandLineToArgv(cmd *uint16, argc *int32) (argv *[8192]*[8192]uint16, err error) [failretval==nil] = shell32.CommandLineToArgvW |
| 308 |
//sys LocalFree(hmem Handle) (handle Handle, err error) [failretval!=0] |
| 309 |
//sys localAlloc(flags uint32, length uint32) (ptr uintptr, err error) = kernel32.LocalAlloc |
| 310 |
//sys SetHandleInformation(handle Handle, mask uint32, flags uint32) (err error) |
| 311 |
//sys FlushFileBuffers(handle Handle) (err error) |
| 312 |
//sys GetFullPathName(path *uint16, buflen uint32, buf *uint16, fname **uint16) (n uint32, err error) = kernel32.GetFullPathNameW |
| 313 |
//sys GetLongPathName(path *uint16, buf *uint16, buflen uint32) (n uint32, err error) = kernel32.GetLongPathNameW |
| 314 |
//sys GetShortPathName(longpath *uint16, shortpath *uint16, buflen uint32) (n uint32, err error) = kernel32.GetShortPathNameW |
| 315 |
//sys CreateFileMapping(fhandle Handle, sa *SecurityAttributes, prot uint32, maxSizeHigh uint32, maxSizeLow uint32, name *uint16) (handle Handle, err error) = kernel32.CreateFileMappingW |
| 316 |
//sys MapViewOfFile(handle Handle, access uint32, offsetHigh uint32, offsetLow uint32, length uintptr) (addr uintptr, err error) |
| 317 |
//sys UnmapViewOfFile(addr uintptr) (err error) |
| 318 |
//sys FlushViewOfFile(addr uintptr, length uintptr) (err error) |
| 319 |
//sys VirtualLock(addr uintptr, length uintptr) (err error) |
| 320 |
//sys VirtualUnlock(addr uintptr, length uintptr) (err error) |
| 321 |
//sys TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) = mswsock.TransmitFile |
| 322 |
//sys ReadDirectoryChanges(handle Handle, buf *byte, buflen uint32, watchSubTree bool, mask uint32, retlen *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) = kernel32.ReadDirectoryChangesW |
| 323 |
//sys CertOpenSystemStore(hprov Handle, name *uint16) (store Handle, err error) = crypt32.CertOpenSystemStoreW |
| 324 |
//sys CertOpenStore(storeProvider uintptr, msgAndCertEncodingType uint32, cryptProv uintptr, flags uint32, para uintptr) (handle Handle, err error) = crypt32.CertOpenStore |
| 325 |
//sys CertEnumCertificatesInStore(store Handle, prevContext *CertContext) (context *CertContext, err error) [failretval==nil] = crypt32.CertEnumCertificatesInStore |
| 326 |
//sys CertAddCertificateContextToStore(store Handle, certContext *CertContext, addDisposition uint32, storeContext **CertContext) (err error) = crypt32.CertAddCertificateContextToStore |
| 327 |
//sys CertCloseStore(store Handle, flags uint32) (err error) = crypt32.CertCloseStore |
| 328 |
//sys CertGetCertificateChain(engine Handle, leaf *CertContext, time *Filetime, additionalStore Handle, para *CertChainPara, flags uint32, reserved uintptr, chainCtx **CertChainContext) (err error) = crypt32.CertGetCertificateChain |
| 329 |
//sys CertFreeCertificateChain(ctx *CertChainContext) = crypt32.CertFreeCertificateChain |
| 330 |
//sys CertCreateCertificateContext(certEncodingType uint32, certEncoded *byte, encodedLen uint32) (context *CertContext, err error) [failretval==nil] = crypt32.CertCreateCertificateContext |
| 331 |
//sys CertFreeCertificateContext(ctx *CertContext) (err error) = crypt32.CertFreeCertificateContext |
| 332 |
//sys CertVerifyCertificateChainPolicy(policyOID uintptr, chain *CertChainContext, para *CertChainPolicyPara, status *CertChainPolicyStatus) (err error) = crypt32.CertVerifyCertificateChainPolicy |
| 333 |
//sys RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno error) = advapi32.RegOpenKeyExW |
| 334 |
//sys RegCloseKey(key Handle) (regerrno error) = advapi32.RegCloseKey |
| 335 |
//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 |
| 336 |
//sys regEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegEnumKeyExW |
| 337 |
//sys RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) = advapi32.RegQueryValueExW |
| 338 |
//sys getCurrentProcessId() (pid uint32) = kernel32.GetCurrentProcessId |
| 339 |
//sys GetConsoleMode(console Handle, mode *uint32) (err error) = kernel32.GetConsoleMode |
| 340 |
//sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW |
| 341 |
//sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW |
| 342 |
//sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot |
| 343 |
//sys Process32First(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32FirstW |
| 344 |
//sys Process32Next(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32NextW |
| 345 |
//sys DeviceIoControl(handle Handle, ioControlCode uint32, inBuffer *byte, inBufferSize uint32, outBuffer *byte, outBufferSize uint32, bytesReturned *uint32, overlapped *Overlapped) (err error) |
| 346 |
//sys setFileInformationByHandle(handle Handle, fileInformationClass uint32, buf unsafe.Pointer, bufsize uint32) (err error) = kernel32.SetFileInformationByHandle |
| 347 |
// This function returns 1 byte BOOLEAN rather than the 4 byte BOOL. |
| 348 |
//sys CreateSymbolicLink(symlinkfilename *uint16, targetfilename *uint16, flags uint32) (err error) [failretval&0xff==0] = CreateSymbolicLinkW |
| 349 |
//sys CreateHardLink(filename *uint16, existingfilename *uint16, reserved uintptr) (err error) [failretval&0xff==0] = CreateHardLinkW |
| 350 |
//sys initializeProcThreadAttributeList(attrlist *_PROC_THREAD_ATTRIBUTE_LIST, attrcount uint32, flags uint32, size *uintptr) (err error) = InitializeProcThreadAttributeList |
| 351 |
//sys deleteProcThreadAttributeList(attrlist *_PROC_THREAD_ATTRIBUTE_LIST) = DeleteProcThreadAttributeList |
| 352 |
//sys updateProcThreadAttribute(attrlist *_PROC_THREAD_ATTRIBUTE_LIST, flags uint32, attr uintptr, value unsafe.Pointer, size uintptr, prevvalue unsafe.Pointer, returnedsize *uintptr) (err error) = UpdateProcThreadAttribute |
| 353 |
//sys getFinalPathNameByHandle(file Handle, filePath *uint16, filePathSize uint32, flags uint32) (n uint32, err error) [n == 0 || n >= filePathSize] = kernel32.GetFinalPathNameByHandleW |
| 354 |
|
| 355 |
// syscall interface implementation for other packages |
| 356 |
|
| 357 |
func makeInheritSa() *SecurityAttributes { |
| 358 |
var sa SecurityAttributes |
| 359 |
sa.Length = uint32(unsafe.Sizeof(sa)) |
| 360 |
sa.InheritHandle = 1 |
| 361 |
return &sa |
| 362 |
} |
| 363 |
|
| 364 |
func Open(name string, flag int, perm uint32) (fd Handle, err error) { |
| 365 |
if len(name) == 0 { |
| 366 |
return InvalidHandle, ERROR_FILE_NOT_FOUND |
| 367 |
} |
| 368 |
namep, err := UTF16PtrFromString(name) |
| 369 |
if err != nil { |
| 370 |
return InvalidHandle, err |
| 371 |
} |
| 372 |
accessFlags := flag & (O_RDONLY | O_WRONLY | O_RDWR) |
| 373 |
var access uint32 |
| 374 |
switch accessFlags { |
| 375 |
case O_RDONLY: |
| 376 |
access = GENERIC_READ |
| 377 |
case O_WRONLY: |
| 378 |
access = GENERIC_WRITE |
| 379 |
case O_RDWR: |
| 380 |
access = GENERIC_READ | GENERIC_WRITE |
| 381 |
} |
| 382 |
if flag&O_CREAT != 0 { |
| 383 |
access |= GENERIC_WRITE |
| 384 |
} |
| 385 |
if flag&O_APPEND != 0 { |
| 386 |
// Remove GENERIC_WRITE unless O_TRUNC is set, in which case we need it to truncate the file. |
| 387 |
// We can't just remove FILE_WRITE_DATA because GENERIC_WRITE without FILE_WRITE_DATA |
| 388 |
// starts appending at the beginning of the file rather than at the end. |
| 389 |
if flag&O_TRUNC == 0 { |
| 390 |
access &^= GENERIC_WRITE |
| 391 |
} |
| 392 |
// Set all access rights granted by GENERIC_WRITE except for FILE_WRITE_DATA. |
| 393 |
access |= FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | _FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE |
| 394 |
} |
| 395 |
sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE) |
| 396 |
var sa *SecurityAttributes |
| 397 |
if flag&O_CLOEXEC == 0 { |
| 398 |
sa = makeInheritSa() |
| 399 |
} |
| 400 |
var attrs uint32 = FILE_ATTRIBUTE_NORMAL |
| 401 |
if perm&S_IWRITE == 0 { |
| 402 |
attrs = FILE_ATTRIBUTE_READONLY |
| 403 |
} |
| 404 |
// fileFlags contains the high 12 bits of flag. |
| 405 |
// This bit range can be used by the caller to specify the file flags |
| 406 |
// passed to CreateFile. It is an error to use if the bits can't be |
| 407 |
// mapped to the supported FILE_FLAG_* constants. |
| 408 |
if fileFlags := uint32(flag) & fileFlagsMask; fileFlags&^validFileFlagsMask == 0 { |
| 409 |
attrs |= fileFlags |
| 410 |
} else { |
| 411 |
return InvalidHandle, oserror.ErrInvalid |
| 412 |
} |
| 413 |
|
| 414 |
switch accessFlags { |
| 415 |
case O_WRONLY, O_RDWR: |
| 416 |
// Unix doesn't allow opening a directory with O_WRONLY |
| 417 |
// or O_RDWR, so we don't set the flag in that case, |
| 418 |
// which will make CreateFile fail with ERROR_ACCESS_DENIED. |
| 419 |
// We will map that to EISDIR if the file is a directory. |
| 420 |
default: |
| 421 |
// We might be opening a directory for reading, |
| 422 |
// and CreateFile requires FILE_FLAG_BACKUP_SEMANTICS |
| 423 |
// to work with directories. |
| 424 |
attrs |= FILE_FLAG_BACKUP_SEMANTICS |
| 425 |
} |
| 426 |
if flag&O_SYNC != 0 { |
| 427 |
attrs |= _FILE_FLAG_WRITE_THROUGH |
| 428 |
} |
| 429 |
// We don't use CREATE_ALWAYS, because when opening a file with |
| 430 |
// FILE_ATTRIBUTE_READONLY these will replace an existing file |
| 431 |
// with a new, read-only one. See https://go.dev/issue/38225. |
| 432 |
// |
| 433 |
// Instead, we ftruncate the file after opening when O_TRUNC is set. |
| 434 |
var createmode uint32 |
| 435 |
switch { |
| 436 |
case flag&(O_CREAT|O_EXCL) == (O_CREAT | O_EXCL): |
| 437 |
createmode = CREATE_NEW |
| 438 |
attrs |= FILE_FLAG_OPEN_REPARSE_POINT // don't follow symlinks |
| 439 |
case flag&O_CREAT == O_CREAT: |
| 440 |
createmode = OPEN_ALWAYS |
| 441 |
default: |
| 442 |
createmode = OPEN_EXISTING |
| 443 |
} |
| 444 |
h, err := createFile(namep, access, sharemode, sa, createmode, attrs, 0) |
| 445 |
if h == InvalidHandle { |
| 446 |
if err == ERROR_ACCESS_DENIED && (attrs&FILE_FLAG_BACKUP_SEMANTICS == 0) { |
| 447 |
// We should return EISDIR when we are trying to open a directory with write access. |
| 448 |
fa, e1 := GetFileAttributes(namep) |
| 449 |
if e1 == nil && fa&FILE_ATTRIBUTE_DIRECTORY != 0 { |
| 450 |
err = EISDIR |
| 451 |
} |
| 452 |
} |
| 453 |
return h, err |
| 454 |
} |
| 455 |
if flag&o_DIRECTORY != 0 { |
| 456 |
// Check if the file is a directory, else return ENOTDIR. |
| 457 |
var fi ByHandleFileInformation |
| 458 |
if err := GetFileInformationByHandle(h, &fi); err != nil { |
| 459 |
CloseHandle(h) |
| 460 |
return InvalidHandle, err |
| 461 |
} |
| 462 |
if fi.FileAttributes&FILE_ATTRIBUTE_DIRECTORY == 0 { |
| 463 |
CloseHandle(h) |
| 464 |
return InvalidHandle, ENOTDIR |
| 465 |
} |
| 466 |
} |
| 467 |
// Ignore O_TRUNC if the file has just been created. |
| 468 |
if flag&O_TRUNC == O_TRUNC && |
| 469 |
(createmode == OPEN_EXISTING || (createmode == OPEN_ALWAYS && err == ERROR_ALREADY_EXISTS)) { |
| 470 |
err = Ftruncate(h, 0) |
| 471 |
if err == _ERROR_INVALID_PARAMETER { |
| 472 |
// ERROR_INVALID_PARAMETER means truncation is not supported on this file handle. |
| 473 |
// Unix's O_TRUNC specification says to ignore O_TRUNC on named pipes and terminal devices. |
| 474 |
// We do the same here. |
| 475 |
if t, err1 := GetFileType(h); err1 == nil && (t == FILE_TYPE_PIPE || t == FILE_TYPE_CHAR) { |
| 476 |
err = nil |
| 477 |
} |
| 478 |
} |
| 479 |
if err != nil { |
| 480 |
CloseHandle(h) |
| 481 |
return InvalidHandle, err |
| 482 |
} |
| 483 |
} |
| 484 |
return h, nil |
| 485 |
} |
| 486 |
|
| 487 |
func Read(fd Handle, p []byte) (n int, err error) { |
| 488 |
var done uint32 |
| 489 |
e := ReadFile(fd, p, &done, nil) |
| 490 |
if e != nil { |
| 491 |
if e == ERROR_BROKEN_PIPE { |
| 492 |
// NOTE(brainman): work around ERROR_BROKEN_PIPE is returned on reading EOF from stdin |
| 493 |
return 0, nil |
| 494 |
} |
| 495 |
return 0, e |
| 496 |
} |
| 497 |
return int(done), nil |
| 498 |
} |
| 499 |
|
| 500 |
func Write(fd Handle, p []byte) (n int, err error) { |
| 501 |
var done uint32 |
| 502 |
e := WriteFile(fd, p, &done, nil) |
| 503 |
if e != nil { |
| 504 |
return 0, e |
| 505 |
} |
| 506 |
return int(done), nil |
| 507 |
} |
| 508 |
|
| 509 |
func ReadFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error { |
| 510 |
err := readFile(fd, p, done, overlapped) |
| 511 |
if race.Enabled { |
| 512 |
if *done > 0 { |
| 513 |
race.WriteRange(unsafe.Pointer(&p[0]), int(*done)) |
| 514 |
} |
| 515 |
race.Acquire(unsafe.Pointer(&ioSync)) |
| 516 |
} |
| 517 |
if msan.Enabled && *done > 0 { |
| 518 |
msan.Write(unsafe.Pointer(&p[0]), uintptr(*done)) |
| 519 |
} |
| 520 |
if asan.Enabled && *done > 0 { |
| 521 |
asan.Write(unsafe.Pointer(&p[0]), uintptr(*done)) |
| 522 |
} |
| 523 |
return err |
| 524 |
} |
| 525 |
|
| 526 |
func WriteFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error { |
| 527 |
if race.Enabled { |
| 528 |
race.ReleaseMerge(unsafe.Pointer(&ioSync)) |
| 529 |
} |
| 530 |
err := writeFile(fd, p, done, overlapped) |
| 531 |
if race.Enabled && *done > 0 { |
| 532 |
race.ReadRange(unsafe.Pointer(&p[0]), int(*done)) |
| 533 |
} |
| 534 |
if msan.Enabled && *done > 0 { |
| 535 |
msan.Read(unsafe.Pointer(&p[0]), uintptr(*done)) |
| 536 |
} |
| 537 |
if asan.Enabled && *done > 0 { |
| 538 |
asan.Read(unsafe.Pointer(&p[0]), uintptr(*done)) |
| 539 |
} |
| 540 |
return err |
| 541 |
} |
| 542 |
|
| 543 |
var ioSync int64 |
| 544 |
|
| 545 |
var procSetFilePointerEx = modkernel32.NewProc("SetFilePointerEx") |
| 546 |
|
| 547 |
const ptrSize = unsafe.Sizeof(uintptr(0)) |
| 548 |
|
| 549 |
// setFilePointerEx calls SetFilePointerEx. |
| 550 |
// See https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfilepointerex |
| 551 |
func setFilePointerEx(handle Handle, distToMove int64, newFilePointer *int64, whence uint32) error { |
| 552 |
var e1 Errno |
| 553 |
if unsafe.Sizeof(uintptr(0)) == 8 { |
| 554 |
_, _, e1 = Syscall6(procSetFilePointerEx.Addr(), 4, uintptr(handle), uintptr(distToMove), uintptr(unsafe.Pointer(newFilePointer)), uintptr(whence), 0, 0) |
| 555 |
} else { |
| 556 |
// distToMove is a LARGE_INTEGER, which is 64 bits. |
| 557 |
_, _, e1 = Syscall6(procSetFilePointerEx.Addr(), 5, uintptr(handle), uintptr(distToMove), uintptr(distToMove>>32), uintptr(unsafe.Pointer(newFilePointer)), uintptr(whence), 0) |
| 558 |
} |
| 559 |
if e1 != 0 { |
| 560 |
return errnoErr(e1) |
| 561 |
} |
| 562 |
return nil |
| 563 |
} |
| 564 |
|
| 565 |
func Seek(fd Handle, offset int64, whence int) (newoffset int64, err error) { |
| 566 |
var w uint32 |
| 567 |
switch whence { |
| 568 |
case 0: |
| 569 |
w = FILE_BEGIN |
| 570 |
case 1: |
| 571 |
w = FILE_CURRENT |
| 572 |
case 2: |
| 573 |
w = FILE_END |
| 574 |
} |
| 575 |
err = setFilePointerEx(fd, offset, &newoffset, w) |
| 576 |
return |
| 577 |
} |
| 578 |
|
| 579 |
func Close(fd Handle) (err error) { |
| 580 |
return CloseHandle(fd) |
| 581 |
} |
| 582 |
|
| 583 |
var ( |
| 584 |
Stdin = getStdHandle(STD_INPUT_HANDLE) |
| 585 |
Stdout = getStdHandle(STD_OUTPUT_HANDLE) |
| 586 |
Stderr = getStdHandle(STD_ERROR_HANDLE) |
| 587 |
) |
| 588 |
|
| 589 |
func getStdHandle(h int) (fd Handle) { |
| 590 |
r, _ := GetStdHandle(h) |
| 591 |
return r |
| 592 |
} |
| 593 |
|
| 594 |
const ImplementsGetwd = true |
| 595 |
|
| 596 |
func Getwd() (wd string, err error) { |
| 597 |
b := make([]uint16, 300) |
| 598 |
// The path of the current directory may not fit in the initial 300-word |
| 599 |
// buffer when long path support is enabled. The current directory may also |
| 600 |
// change between subsequent calls of GetCurrentDirectory. As a result, we |
| 601 |
// need to retry the call in a loop until the current directory fits, each |
| 602 |
// time with a bigger buffer. |
| 603 |
for { |
| 604 |
n, e := GetCurrentDirectory(uint32(len(b)), &b[0]) |
| 605 |
if e != nil { |
| 606 |
return "", e |
| 607 |
} |
| 608 |
if int(n) <= len(b) { |
| 609 |
return UTF16ToString(b[:n]), nil |
| 610 |
} |
| 611 |
b = make([]uint16, n) |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
func Chdir(path string) (err error) { |
| 616 |
pathp, err := UTF16PtrFromString(path) |
| 617 |
if err != nil { |
| 618 |
return err |
| 619 |
} |
| 620 |
return SetCurrentDirectory(pathp) |
| 621 |
} |
| 622 |
|
| 623 |
func Mkdir(path string, mode uint32) (err error) { |
| 624 |
pathp, err := UTF16PtrFromString(path) |
| 625 |
if err != nil { |
| 626 |
return err |
| 627 |
} |
| 628 |
return CreateDirectory(pathp, nil) |
| 629 |
} |
| 630 |
|
| 631 |
func Rmdir(path string) (err error) { |
| 632 |
pathp, err := UTF16PtrFromString(path) |
| 633 |
if err != nil { |
| 634 |
return err |
| 635 |
} |
| 636 |
return RemoveDirectory(pathp) |
| 637 |
} |
| 638 |
|
| 639 |
func Unlink(path string) (err error) { |
| 640 |
pathp, err := UTF16PtrFromString(path) |
| 641 |
if err != nil { |
| 642 |
return err |
| 643 |
} |
| 644 |
return DeleteFile(pathp) |
| 645 |
} |
| 646 |
|
| 647 |
func Rename(oldpath, newpath string) (err error) { |
| 648 |
from, err := UTF16PtrFromString(oldpath) |
| 649 |
if err != nil { |
| 650 |
return err |
| 651 |
} |
| 652 |
to, err := UTF16PtrFromString(newpath) |
| 653 |
if err != nil { |
| 654 |
return err |
| 655 |
} |
| 656 |
return MoveFile(from, to) |
| 657 |
} |
| 658 |
|
| 659 |
func ComputerName() (name string, err error) { |
| 660 |
var n uint32 = MAX_COMPUTERNAME_LENGTH + 1 |
| 661 |
b := make([]uint16, n) |
| 662 |
e := GetComputerName(&b[0], &n) |
| 663 |
if e != nil { |
| 664 |
return "", e |
| 665 |
} |
| 666 |
return UTF16ToString(b[:n]), nil |
| 667 |
} |
| 668 |
|
| 669 |
func Ftruncate(fd Handle, length int64) (err error) { |
| 670 |
type _FILE_END_OF_FILE_INFO struct { |
| 671 |
EndOfFile int64 |
| 672 |
} |
| 673 |
const FileEndOfFileInfo = 6 |
| 674 |
var info _FILE_END_OF_FILE_INFO |
| 675 |
info.EndOfFile = length |
| 676 |
return setFileInformationByHandle(fd, FileEndOfFileInfo, unsafe.Pointer(&info), uint32(unsafe.Sizeof(info))) |
| 677 |
} |
| 678 |
|
| 679 |
func Gettimeofday(tv *Timeval) (err error) { |
| 680 |
var ft Filetime |
| 681 |
GetSystemTimeAsFileTime(&ft) |
| 682 |
*tv = NsecToTimeval(ft.Nanoseconds()) |
| 683 |
return nil |
| 684 |
} |
| 685 |
|
| 686 |
func Pipe(p []Handle) (err error) { |
| 687 |
if len(p) != 2 { |
| 688 |
return EINVAL |
| 689 |
} |
| 690 |
var r, w Handle |
| 691 |
e := CreatePipe(&r, &w, makeInheritSa(), 0) |
| 692 |
if e != nil { |
| 693 |
return e |
| 694 |
} |
| 695 |
p[0] = r |
| 696 |
p[1] = w |
| 697 |
return nil |
| 698 |
} |
| 699 |
|
| 700 |
func Utimes(path string, tv []Timeval) (err error) { |
| 701 |
if len(tv) != 2 { |
| 702 |
return EINVAL |
| 703 |
} |
| 704 |
pathp, e := UTF16PtrFromString(path) |
| 705 |
if e != nil { |
| 706 |
return e |
| 707 |
} |
| 708 |
h, e := CreateFile(pathp, |
| 709 |
FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil, |
| 710 |
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) |
| 711 |
if e != nil { |
| 712 |
return e |
| 713 |
} |
| 714 |
defer Close(h) |
| 715 |
a := Filetime{} |
| 716 |
w := Filetime{} |
| 717 |
if tv[0].Nanoseconds() != 0 { |
| 718 |
a = NsecToFiletime(tv[0].Nanoseconds()) |
| 719 |
} |
| 720 |
if tv[0].Nanoseconds() != 0 { |
| 721 |
w = NsecToFiletime(tv[1].Nanoseconds()) |
| 722 |
} |
| 723 |
return SetFileTime(h, nil, &a, &w) |
| 724 |
} |
| 725 |
|
| 726 |
// This matches the value in os/file_windows.go. |
| 727 |
const _UTIME_OMIT = -1 |
| 728 |
|
| 729 |
func UtimesNano(path string, ts []Timespec) (err error) { |
| 730 |
if len(ts) != 2 { |
| 731 |
return EINVAL |
| 732 |
} |
| 733 |
pathp, e := UTF16PtrFromString(path) |
| 734 |
if e != nil { |
| 735 |
return e |
| 736 |
} |
| 737 |
h, e := CreateFile(pathp, |
| 738 |
FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil, |
| 739 |
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) |
| 740 |
if e != nil { |
| 741 |
return e |
| 742 |
} |
| 743 |
defer Close(h) |
| 744 |
a := Filetime{} |
| 745 |
w := Filetime{} |
| 746 |
if ts[0].Nsec != _UTIME_OMIT { |
| 747 |
a = NsecToFiletime(TimespecToNsec(ts[0])) |
| 748 |
} |
| 749 |
if ts[1].Nsec != _UTIME_OMIT { |
| 750 |
w = NsecToFiletime(TimespecToNsec(ts[1])) |
| 751 |
} |
| 752 |
return SetFileTime(h, nil, &a, &w) |
| 753 |
} |
| 754 |
|
| 755 |
func Fsync(fd Handle) (err error) { |
| 756 |
return FlushFileBuffers(fd) |
| 757 |
} |
| 758 |
|
| 759 |
func Chmod(path string, mode uint32) (err error) { |
| 760 |
p, e := UTF16PtrFromString(path) |
| 761 |
if e != nil { |
| 762 |
return e |
| 763 |
} |
| 764 |
attrs, e := GetFileAttributes(p) |
| 765 |
if e != nil { |
| 766 |
return e |
| 767 |
} |
| 768 |
if mode&S_IWRITE != 0 { |
| 769 |
attrs &^= FILE_ATTRIBUTE_READONLY |
| 770 |
} else { |
| 771 |
attrs |= FILE_ATTRIBUTE_READONLY |
| 772 |
} |
| 773 |
return SetFileAttributes(p, attrs) |
| 774 |
} |
| 775 |
|
| 776 |
func LoadCancelIoEx() error { |
| 777 |
return procCancelIoEx.Find() |
| 778 |
} |
| 779 |
|
| 780 |
func LoadSetFileCompletionNotificationModes() error { |
| 781 |
return procSetFileCompletionNotificationModes.Find() |
| 782 |
} |
| 783 |
|
| 784 |
// net api calls |
| 785 |
|
| 786 |
const socket_error = uintptr(^uint32(0)) |
| 787 |
|
| 788 |
//sys WSAStartup(verreq uint32, data *WSAData) (sockerr error) = ws2_32.WSAStartup |
| 789 |
//sys WSACleanup() (err error) [failretval==socket_error] = ws2_32.WSACleanup |
| 790 |
//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 |
| 791 |
//sys socket(af int32, typ int32, protocol int32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.socket |
| 792 |
//sys Setsockopt(s Handle, level int32, optname int32, optval *byte, optlen int32) (err error) [failretval==socket_error] = ws2_32.setsockopt |
| 793 |
//sys Getsockopt(s Handle, level int32, optname int32, optval *byte, optlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockopt |
| 794 |
//sys bind(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.bind |
| 795 |
//sys connect(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.connect |
| 796 |
//sys getsockname(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockname |
| 797 |
//sys getpeername(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getpeername |
| 798 |
//sys listen(s Handle, backlog int32) (err error) [failretval==socket_error] = ws2_32.listen |
| 799 |
//sys shutdown(s Handle, how int32) (err error) [failretval==socket_error] = ws2_32.shutdown |
| 800 |
//sys Closesocket(s Handle) (err error) [failretval==socket_error] = ws2_32.closesocket |
| 801 |
//sys AcceptEx(ls Handle, as Handle, buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, recvd *uint32, overlapped *Overlapped) (err error) = mswsock.AcceptEx |
| 802 |
//sys GetAcceptExSockaddrs(buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, lrsa **RawSockaddrAny, lrsalen *int32, rrsa **RawSockaddrAny, rrsalen *int32) = mswsock.GetAcceptExSockaddrs |
| 803 |
//sys WSARecv(s Handle, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSARecv |
| 804 |
//sys WSASend(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSASend |
| 805 |
//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 |
| 806 |
//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 |
| 807 |
//sys GetHostByName(name string) (h *Hostent, err error) [failretval==nil] = ws2_32.gethostbyname |
| 808 |
//sys GetServByName(name string, proto string) (s *Servent, err error) [failretval==nil] = ws2_32.getservbyname |
| 809 |
//sys Ntohs(netshort uint16) (u uint16) = ws2_32.ntohs |
| 810 |
//sys GetProtoByName(name string) (p *Protoent, err error) [failretval==nil] = ws2_32.getprotobyname |
| 811 |
//sys DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status error) = dnsapi.DnsQuery_W |
| 812 |
//sys DnsRecordListFree(rl *DNSRecord, freetype uint32) = dnsapi.DnsRecordListFree |
| 813 |
//sys DnsNameCompare(name1 *uint16, name2 *uint16) (same bool) = dnsapi.DnsNameCompare_W |
| 814 |
//sys GetAddrInfoW(nodename *uint16, servicename *uint16, hints *AddrinfoW, result **AddrinfoW) (sockerr error) = ws2_32.GetAddrInfoW |
| 815 |
//sys FreeAddrInfoW(addrinfo *AddrinfoW) = ws2_32.FreeAddrInfoW |
| 816 |
//sys GetIfEntry(pIfRow *MibIfRow) (errcode error) = iphlpapi.GetIfEntry |
| 817 |
//sys GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode error) = iphlpapi.GetAdaptersInfo |
| 818 |
//sys SetFileCompletionNotificationModes(handle Handle, flags uint8) (err error) = kernel32.SetFileCompletionNotificationModes |
| 819 |
//sys WSAEnumProtocols(protocols *int32, protocolBuffer *WSAProtocolInfo, bufferLength *uint32) (n int32, err error) [failretval==-1] = ws2_32.WSAEnumProtocolsW |
| 820 |
|
| 821 |
// For testing: clients can set this flag to force |
| 822 |
// creation of IPv6 sockets to return [EAFNOSUPPORT]. |
| 823 |
var SocketDisableIPv6 bool |
| 824 |
|
| 825 |
type RawSockaddrInet4 struct { |
| 826 |
Family uint16 |
| 827 |
Port uint16 |
| 828 |
Addr [4]byte /* in_addr */ |
| 829 |
Zero [8]uint8 |
| 830 |
} |
| 831 |
|
| 832 |
type RawSockaddrInet6 struct { |
| 833 |
Family uint16 |
| 834 |
Port uint16 |
| 835 |
Flowinfo uint32 |
| 836 |
Addr [16]byte /* in6_addr */ |
| 837 |
Scope_id uint32 |
| 838 |
} |
| 839 |
|
| 840 |
type RawSockaddr struct { |
| 841 |
Family uint16 |
| 842 |
Data [14]int8 |
| 843 |
} |
| 844 |
|
| 845 |
type RawSockaddrAny struct { |
| 846 |
Addr RawSockaddr |
| 847 |
Pad [100]int8 |
| 848 |
} |
| 849 |
|
| 850 |
type Sockaddr interface { |
| 851 |
sockaddr() (ptr unsafe.Pointer, len int32, err error) // lowercase; only we can define Sockaddrs |
| 852 |
} |
| 853 |
|
| 854 |
type SockaddrInet4 struct { |
| 855 |
Port int |
| 856 |
Addr [4]byte |
| 857 |
raw RawSockaddrInet4 |
| 858 |
} |
| 859 |
|
| 860 |
func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, int32, error) { |
| 861 |
if sa.Port < 0 || sa.Port > 0xFFFF { |
| 862 |
return nil, 0, EINVAL |
| 863 |
} |
| 864 |
sa.raw.Family = AF_INET |
| 865 |
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) |
| 866 |
p[0] = byte(sa.Port >> 8) |
| 867 |
p[1] = byte(sa.Port) |
| 868 |
sa.raw.Addr = sa.Addr |
| 869 |
return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil |
| 870 |
} |
| 871 |
|
| 872 |
type SockaddrInet6 struct { |
| 873 |
Port int |
| 874 |
ZoneId uint32 |
| 875 |
Addr [16]byte |
| 876 |
raw RawSockaddrInet6 |
| 877 |
} |
| 878 |
|
| 879 |
func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, int32, error) { |
| 880 |
if sa.Port < 0 || sa.Port > 0xFFFF { |
| 881 |
return nil, 0, EINVAL |
| 882 |
} |
| 883 |
sa.raw.Family = AF_INET6 |
| 884 |
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) |
| 885 |
p[0] = byte(sa.Port >> 8) |
| 886 |
p[1] = byte(sa.Port) |
| 887 |
sa.raw.Scope_id = sa.ZoneId |
| 888 |
sa.raw.Addr = sa.Addr |
| 889 |
return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil |
| 890 |
} |
| 891 |
|
| 892 |
type RawSockaddrUnix struct { |
| 893 |
Family uint16 |
| 894 |
Path [UNIX_PATH_MAX]int8 |
| 895 |
} |
| 896 |
|
| 897 |
type SockaddrUnix struct { |
| 898 |
Name string |
| 899 |
raw RawSockaddrUnix |
| 900 |
} |
| 901 |
|
| 902 |
func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) { |
| 903 |
name := sa.Name |
| 904 |
n := len(name) |
| 905 |
if n > len(sa.raw.Path) { |
| 906 |
return nil, 0, EINVAL |
| 907 |
} |
| 908 |
// Abstract addresses start with NUL. |
| 909 |
// '@' is also a valid way to specify abstract addresses. |
| 910 |
isAbstract := n > 0 && (name[0] == '@' || name[0] == '\x00') |
| 911 |
|
| 912 |
// Non-abstract named addresses are NUL terminated. |
| 913 |
// The length can't use the full capacity as we need to add NUL. |
| 914 |
if n == len(sa.raw.Path) && !isAbstract { |
| 915 |
return nil, 0, EINVAL |
| 916 |
} |
| 917 |
sa.raw.Family = AF_UNIX |
| 918 |
for i := 0; i < n; i++ { |
| 919 |
sa.raw.Path[i] = int8(name[i]) |
| 920 |
} |
| 921 |
// Length is family + name (+ NUL if non-abstract). |
| 922 |
// Family is of type uint16 (2 bytes). |
| 923 |
sl := int32(2 + n) |
| 924 |
if isAbstract { |
| 925 |
// Abstract addresses are not NUL terminated. |
| 926 |
// We rewrite '@' prefix to NUL here. |
| 927 |
sa.raw.Path[0] = 0 |
| 928 |
} else if n > 0 { |
| 929 |
// Add NUL for non-abstract named addresses. |
| 930 |
sl++ |
| 931 |
} |
| 932 |
|
| 933 |
return unsafe.Pointer(&sa.raw), sl, nil |
| 934 |
} |
| 935 |
|
| 936 |
func (rsa *RawSockaddrAny) Sockaddr() (Sockaddr, error) { |
| 937 |
switch rsa.Addr.Family { |
| 938 |
case AF_UNIX: |
| 939 |
pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) |
| 940 |
sa := new(SockaddrUnix) |
| 941 |
if pp.Path[0] == 0 { |
| 942 |
// "Abstract" Unix domain socket. |
| 943 |
// Rewrite leading NUL as @ for textual display. |
| 944 |
// (This is the standard convention.) |
| 945 |
// Not friendly to overwrite in place, |
| 946 |
// but the callers below don't care. |
| 947 |
pp.Path[0] = '@' |
| 948 |
} |
| 949 |
|
| 950 |
// Assume path ends at NUL. |
| 951 |
// This is not technically the Linux semantics for |
| 952 |
// abstract Unix domain sockets--they are supposed |
| 953 |
// to be uninterpreted fixed-size binary blobs--but |
| 954 |
// everyone uses this convention. |
| 955 |
n := 0 |
| 956 |
for n < len(pp.Path) && pp.Path[n] != 0 { |
| 957 |
n++ |
| 958 |
} |
| 959 |
sa.Name = string(unsafe.Slice((*byte)(unsafe.Pointer(&pp.Path[0])), n)) |
| 960 |
return sa, nil |
| 961 |
|
| 962 |
case AF_INET: |
| 963 |
pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa)) |
| 964 |
sa := new(SockaddrInet4) |
| 965 |
p := (*[2]byte)(unsafe.Pointer(&pp.Port)) |
| 966 |
sa.Port = int(p[0])<<8 + int(p[1]) |
| 967 |
sa.Addr = pp.Addr |
| 968 |
return sa, nil |
| 969 |
|
| 970 |
case AF_INET6: |
| 971 |
pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa)) |
| 972 |
sa := new(SockaddrInet6) |
| 973 |
p := (*[2]byte)(unsafe.Pointer(&pp.Port)) |
| 974 |
sa.Port = int(p[0])<<8 + int(p[1]) |
| 975 |
sa.ZoneId = pp.Scope_id |
| 976 |
sa.Addr = pp.Addr |
| 977 |
return sa, nil |
| 978 |
} |
| 979 |
return nil, EAFNOSUPPORT |
| 980 |
} |
| 981 |
|
| 982 |
func Socket(domain, typ, proto int) (fd Handle, err error) { |
| 983 |
if domain == AF_INET6 && SocketDisableIPv6 { |
| 984 |
return InvalidHandle, EAFNOSUPPORT |
| 985 |
} |
| 986 |
return socket(int32(domain), int32(typ), int32(proto)) |
| 987 |
} |
| 988 |
|
| 989 |
func SetsockoptInt(fd Handle, level, opt int, value int) (err error) { |
| 990 |
v := int32(value) |
| 991 |
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&v)), int32(unsafe.Sizeof(v))) |
| 992 |
} |
| 993 |
|
| 994 |
func Bind(fd Handle, sa Sockaddr) (err error) { |
| 995 |
ptr, n, err := sa.sockaddr() |
| 996 |
if err != nil { |
| 997 |
return err |
| 998 |
} |
| 999 |
return bind(fd, ptr, n) |
| 1000 |
} |
| 1001 |
|
| 1002 |
func Connect(fd Handle, sa Sockaddr) (err error) { |
| 1003 |
ptr, n, err := sa.sockaddr() |
| 1004 |
if err != nil { |
| 1005 |
return err |
| 1006 |
} |
| 1007 |
return connect(fd, ptr, n) |
| 1008 |
} |
| 1009 |
|
| 1010 |
func Getsockname(fd Handle) (sa Sockaddr, err error) { |
| 1011 |
var rsa RawSockaddrAny |
| 1012 |
l := int32(unsafe.Sizeof(rsa)) |
| 1013 |
if err = getsockname(fd, &rsa, &l); err != nil { |
| 1014 |
return |
| 1015 |
} |
| 1016 |
return rsa.Sockaddr() |
| 1017 |
} |
| 1018 |
|
| 1019 |
func Getpeername(fd Handle) (sa Sockaddr, err error) { |
| 1020 |
var rsa RawSockaddrAny |
| 1021 |
l := int32(unsafe.Sizeof(rsa)) |
| 1022 |
if err = getpeername(fd, &rsa, &l); err != nil { |
| 1023 |
return |
| 1024 |
} |
| 1025 |
return rsa.Sockaddr() |
| 1026 |
} |
| 1027 |
|
| 1028 |
func Listen(s Handle, n int) (err error) { |
| 1029 |
return listen(s, int32(n)) |
| 1030 |
} |
| 1031 |
|
| 1032 |
func Shutdown(fd Handle, how int) (err error) { |
| 1033 |
return shutdown(fd, int32(how)) |
| 1034 |
} |
| 1035 |
|
| 1036 |
func WSASendto(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to Sockaddr, overlapped *Overlapped, croutine *byte) (err error) { |
| 1037 |
var rsa unsafe.Pointer |
| 1038 |
var len int32 |
| 1039 |
if to != nil { |
| 1040 |
rsa, len, err = to.sockaddr() |
| 1041 |
if err != nil { |
| 1042 |
return err |
| 1043 |
} |
| 1044 |
} |
| 1045 |
r1, _, e1 := Syscall9(procWSASendTo.Addr(), 9, uintptr(s), uintptr(unsafe.Pointer(bufs)), uintptr(bufcnt), uintptr(unsafe.Pointer(sent)), uintptr(flags), uintptr(unsafe.Pointer(rsa)), uintptr(len), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine))) |
| 1046 |
if r1 == socket_error { |
| 1047 |
if e1 != 0 { |
| 1048 |
err = errnoErr(e1) |
| 1049 |
} else { |
| 1050 |
err = EINVAL |
| 1051 |
} |
| 1052 |
} |
| 1053 |
return err |
| 1054 |
} |
| 1055 |
|
| 1056 |
func wsaSendtoInet4(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to *SockaddrInet4, overlapped *Overlapped, croutine *byte) (err error) { |
| 1057 |
rsa, len, err := to.sockaddr() |
| 1058 |
if err != nil { |
| 1059 |
return err |
| 1060 |
} |
| 1061 |
r1, _, e1 := Syscall9(procWSASendTo.Addr(), 9, uintptr(s), uintptr(unsafe.Pointer(bufs)), uintptr(bufcnt), uintptr(unsafe.Pointer(sent)), uintptr(flags), uintptr(unsafe.Pointer(rsa)), uintptr(len), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine))) |
| 1062 |
if r1 == socket_error { |
| 1063 |
if e1 != 0 { |
| 1064 |
err = errnoErr(e1) |
| 1065 |
} else { |
| 1066 |
err = EINVAL |
| 1067 |
} |
| 1068 |
} |
| 1069 |
return err |
| 1070 |
} |
| 1071 |
|
| 1072 |
func wsaSendtoInet6(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to *SockaddrInet6, overlapped *Overlapped, croutine *byte) (err error) { |
| 1073 |
rsa, len, err := to.sockaddr() |
| 1074 |
if err != nil { |
| 1075 |
return err |
| 1076 |
} |
| 1077 |
r1, _, e1 := Syscall9(procWSASendTo.Addr(), 9, uintptr(s), uintptr(unsafe.Pointer(bufs)), uintptr(bufcnt), uintptr(unsafe.Pointer(sent)), uintptr(flags), uintptr(unsafe.Pointer(rsa)), uintptr(len), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine))) |
| 1078 |
if r1 == socket_error { |
| 1079 |
if e1 != 0 { |
| 1080 |
err = errnoErr(e1) |
| 1081 |
} else { |
| 1082 |
err = EINVAL |
| 1083 |
} |
| 1084 |
} |
| 1085 |
return err |
| 1086 |
} |
| 1087 |
|
| 1088 |
func LoadGetAddrInfo() error { |
| 1089 |
return procGetAddrInfoW.Find() |
| 1090 |
} |
| 1091 |
|
| 1092 |
var connectExFunc struct { |
| 1093 |
once sync.Once |
| 1094 |
addr uintptr |
| 1095 |
err error |
| 1096 |
} |
| 1097 |
|
| 1098 |
func LoadConnectEx() error { |
| 1099 |
connectExFunc.once.Do(func() { |
| 1100 |
var s Handle |
| 1101 |
s, connectExFunc.err = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) |
| 1102 |
if connectExFunc.err != nil { |
| 1103 |
return |
| 1104 |
} |
| 1105 |
defer CloseHandle(s) |
| 1106 |
var n uint32 |
| 1107 |
connectExFunc.err = WSAIoctl(s, |
| 1108 |
SIO_GET_EXTENSION_FUNCTION_POINTER, |
| 1109 |
(*byte)(unsafe.Pointer(&WSAID_CONNECTEX)), |
| 1110 |
uint32(unsafe.Sizeof(WSAID_CONNECTEX)), |
| 1111 |
(*byte)(unsafe.Pointer(&connectExFunc.addr)), |
| 1112 |
uint32(unsafe.Sizeof(connectExFunc.addr)), |
| 1113 |
&n, nil, 0) |
| 1114 |
}) |
| 1115 |
return connectExFunc.err |
| 1116 |
} |
| 1117 |
|
| 1118 |
func connectEx(s Handle, name unsafe.Pointer, namelen int32, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) (err error) { |
| 1119 |
r1, _, e1 := 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) |
| 1120 |
if r1 == 0 { |
| 1121 |
if e1 != 0 { |
| 1122 |
err = error(e1) |
| 1123 |
} else { |
| 1124 |
err = EINVAL |
| 1125 |
} |
| 1126 |
} |
| 1127 |
return |
| 1128 |
} |
| 1129 |
|
| 1130 |
func ConnectEx(fd Handle, sa Sockaddr, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) error { |
| 1131 |
err := LoadConnectEx() |
| 1132 |
if err != nil { |
| 1133 |
return errorspkg.New("failed to find ConnectEx: " + err.Error()) |
| 1134 |
} |
| 1135 |
ptr, n, err := sa.sockaddr() |
| 1136 |
if err != nil { |
| 1137 |
return err |
| 1138 |
} |
| 1139 |
return connectEx(fd, ptr, n, sendBuf, sendDataLen, bytesSent, overlapped) |
| 1140 |
} |
| 1141 |
|
| 1142 |
// Invented structures to support what package os expects. |
| 1143 |
type Rusage struct { |
| 1144 |
CreationTime Filetime |
| 1145 |
ExitTime Filetime |
| 1146 |
KernelTime Filetime |
| 1147 |
UserTime Filetime |
| 1148 |
} |
| 1149 |
|
| 1150 |
type WaitStatus struct { |
| 1151 |
ExitCode uint32 |
| 1152 |
} |
| 1153 |
|
| 1154 |
func (w WaitStatus) Exited() bool { return true } |
| 1155 |
|
| 1156 |
func (w WaitStatus) ExitStatus() int { return int(w.ExitCode) } |
| 1157 |
|
| 1158 |
func (w WaitStatus) Signal() Signal { return -1 } |
| 1159 |
|
| 1160 |
func (w WaitStatus) CoreDump() bool { return false } |
| 1161 |
|
| 1162 |
func (w WaitStatus) Stopped() bool { return false } |
| 1163 |
|
| 1164 |
func (w WaitStatus) Continued() bool { return false } |
| 1165 |
|
| 1166 |
func (w WaitStatus) StopSignal() Signal { return -1 } |
| 1167 |
|
| 1168 |
func (w WaitStatus) Signaled() bool { return false } |
| 1169 |
|
| 1170 |
func (w WaitStatus) TrapCause() int { return -1 } |
| 1171 |
|
| 1172 |
// Timespec is an invented structure on Windows, but here for |
| 1173 |
// consistency with the syscall package for other operating systems. |
| 1174 |
type Timespec struct { |
| 1175 |
Sec int64 |
| 1176 |
Nsec int64 |
| 1177 |
} |
| 1178 |
|
| 1179 |
func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } |
| 1180 |
|
| 1181 |
func NsecToTimespec(nsec int64) (ts Timespec) { |
| 1182 |
ts.Sec = nsec / 1e9 |
| 1183 |
ts.Nsec = nsec % 1e9 |
| 1184 |
return |
| 1185 |
} |
| 1186 |
|
| 1187 |
// TODO(brainman): fix all needed for net |
| 1188 |
|
| 1189 |
func Accept(fd Handle) (nfd Handle, sa Sockaddr, err error) { return 0, nil, EWINDOWS } |
| 1190 |
func Recvfrom(fd Handle, p []byte, flags int) (n int, from Sockaddr, err error) { |
| 1191 |
return 0, nil, EWINDOWS |
| 1192 |
} |
| 1193 |
func Sendto(fd Handle, p []byte, flags int, to Sockaddr) (err error) { return EWINDOWS } |
| 1194 |
func SetsockoptTimeval(fd Handle, level, opt int, tv *Timeval) (err error) { return EWINDOWS } |
| 1195 |
|
| 1196 |
// The Linger struct is wrong but we only noticed after Go 1. |
| 1197 |
// sysLinger is the real system call structure. |
| 1198 |
|
| 1199 |
// BUG(brainman): The definition of Linger is not appropriate for direct use |
| 1200 |
// with Setsockopt and Getsockopt. |
| 1201 |
// Use SetsockoptLinger instead. |
| 1202 |
|
| 1203 |
type Linger struct { |
| 1204 |
Onoff int32 |
| 1205 |
Linger int32 |
| 1206 |
} |
| 1207 |
|
| 1208 |
type sysLinger struct { |
| 1209 |
Onoff uint16 |
| 1210 |
Linger uint16 |
| 1211 |
} |
| 1212 |
|
| 1213 |
type IPMreq struct { |
| 1214 |
Multiaddr [4]byte /* in_addr */ |
| 1215 |
Interface [4]byte /* in_addr */ |
| 1216 |
} |
| 1217 |
|
| 1218 |
type IPv6Mreq struct { |
| 1219 |
Multiaddr [16]byte /* in6_addr */ |
| 1220 |
Interface uint32 |
| 1221 |
} |
| 1222 |
|
| 1223 |
func GetsockoptInt(fd Handle, level, opt int) (int, error) { |
| 1224 |
optval := int32(0) |
| 1225 |
optlen := int32(unsafe.Sizeof(optval)) |
| 1226 |
err := Getsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&optval)), &optlen) |
| 1227 |
return int(optval), err |
| 1228 |
} |
| 1229 |
|
| 1230 |
func SetsockoptLinger(fd Handle, level, opt int, l *Linger) (err error) { |
| 1231 |
sys := sysLinger{Onoff: uint16(l.Onoff), Linger: uint16(l.Linger)} |
| 1232 |
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&sys)), int32(unsafe.Sizeof(sys))) |
| 1233 |
} |
| 1234 |
|
| 1235 |
func SetsockoptInet4Addr(fd Handle, level, opt int, value [4]byte) (err error) { |
| 1236 |
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&value[0])), 4) |
| 1237 |
} |
| 1238 |
func SetsockoptIPMreq(fd Handle, level, opt int, mreq *IPMreq) (err error) { |
| 1239 |
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(mreq)), int32(unsafe.Sizeof(*mreq))) |
| 1240 |
} |
| 1241 |
func SetsockoptIPv6Mreq(fd Handle, level, opt int, mreq *IPv6Mreq) (err error) { |
| 1242 |
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(mreq)), int32(unsafe.Sizeof(*mreq))) |
| 1243 |
} |
| 1244 |
|
| 1245 |
func Getpid() (pid int) { return int(getCurrentProcessId()) } |
| 1246 |
|
| 1247 |
func FindFirstFile(name *uint16, data *Win32finddata) (handle Handle, err error) { |
| 1248 |
// NOTE(rsc): The Win32finddata struct is wrong for the system call: |
| 1249 |
// the two paths are each one uint16 short. Use the correct struct, |
| 1250 |
// a win32finddata1, and then copy the results out. |
| 1251 |
// There is no loss of expressivity here, because the final |
| 1252 |
// uint16, if it is used, is supposed to be a NUL, and Go doesn't need that. |
| 1253 |
// For Go 1.1, we might avoid the allocation of win32finddata1 here |
| 1254 |
// by adding a final Bug [2]uint16 field to the struct and then |
| 1255 |
// adjusting the fields in the result directly. |
| 1256 |
var data1 win32finddata1 |
| 1257 |
handle, err = findFirstFile1(name, &data1) |
| 1258 |
if err == nil { |
| 1259 |
copyFindData(data, &data1) |
| 1260 |
} |
| 1261 |
return |
| 1262 |
} |
| 1263 |
|
| 1264 |
func FindNextFile(handle Handle, data *Win32finddata) (err error) { |
| 1265 |
var data1 win32finddata1 |
| 1266 |
err = findNextFile1(handle, &data1) |
| 1267 |
if err == nil { |
| 1268 |
copyFindData(data, &data1) |
| 1269 |
} |
| 1270 |
return |
| 1271 |
} |
| 1272 |
|
| 1273 |
func getProcessEntry(pid int) (*ProcessEntry32, error) { |
| 1274 |
snapshot, err := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) |
| 1275 |
if err != nil { |
| 1276 |
return nil, err |
| 1277 |
} |
| 1278 |
defer CloseHandle(snapshot) |
| 1279 |
var procEntry ProcessEntry32 |
| 1280 |
procEntry.Size = uint32(unsafe.Sizeof(procEntry)) |
| 1281 |
if err = Process32First(snapshot, &procEntry); err != nil { |
| 1282 |
return nil, err |
| 1283 |
} |
| 1284 |
for { |
| 1285 |
if procEntry.ProcessID == uint32(pid) { |
| 1286 |
return &procEntry, nil |
| 1287 |
} |
| 1288 |
err = Process32Next(snapshot, &procEntry) |
| 1289 |
if err != nil { |
| 1290 |
return nil, err |
| 1291 |
} |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
func Getppid() (ppid int) { |
| 1296 |
pe, err := getProcessEntry(Getpid()) |
| 1297 |
if err != nil { |
| 1298 |
return -1 |
| 1299 |
} |
| 1300 |
return int(pe.ParentProcessID) |
| 1301 |
} |
| 1302 |
|
| 1303 |
func fdpath(fd Handle, buf []uint16) ([]uint16, error) { |
| 1304 |
const ( |
| 1305 |
FILE_NAME_NORMALIZED = 0 |
| 1306 |
VOLUME_NAME_DOS = 0 |
| 1307 |
) |
| 1308 |
for { |
| 1309 |
n, err := getFinalPathNameByHandle(fd, &buf[0], uint32(len(buf)), FILE_NAME_NORMALIZED|VOLUME_NAME_DOS) |
| 1310 |
if err == nil { |
| 1311 |
buf = buf[:n] |
| 1312 |
break |
| 1313 |
} |
| 1314 |
if err != _ERROR_NOT_ENOUGH_MEMORY { |
| 1315 |
return nil, err |
| 1316 |
} |
| 1317 |
buf = append(buf, make([]uint16, n-uint32(len(buf)))...) |
| 1318 |
} |
| 1319 |
return buf, nil |
| 1320 |
} |
| 1321 |
|
| 1322 |
func Fchdir(fd Handle) (err error) { |
| 1323 |
var buf [MAX_PATH + 1]uint16 |
| 1324 |
path, err := fdpath(fd, buf[:]) |
| 1325 |
if err != nil { |
| 1326 |
return err |
| 1327 |
} |
| 1328 |
// When using VOLUME_NAME_DOS, the path is always prefixed by "\\?\". |
| 1329 |
// That prefix tells the Windows APIs to disable all string parsing and to send |
| 1330 |
// the string that follows it straight to the file system. |
| 1331 |
// Although SetCurrentDirectory and GetCurrentDirectory do support the "\\?\" prefix, |
| 1332 |
// some other Windows APIs don't. If the prefix is not removed here, it will leak |
| 1333 |
// to Getwd, and we don't want such a general-purpose function to always return a |
| 1334 |
// path with the "\\?\" prefix after Fchdir is called. |
| 1335 |
// The downside is that APIs that do support it will parse the path and try to normalize it, |
| 1336 |
// when it's already normalized. |
| 1337 |
if len(path) >= 4 && path[0] == '\\' && path[1] == '\\' && path[2] == '?' && path[3] == '\\' { |
| 1338 |
path = path[4:] |
| 1339 |
} |
| 1340 |
return SetCurrentDirectory(&path[0]) |
| 1341 |
} |
| 1342 |
|
| 1343 |
// TODO(brainman): fix all needed for os |
| 1344 |
func Link(oldpath, newpath string) (err error) { return EWINDOWS } |
| 1345 |
func Symlink(path, link string) (err error) { return EWINDOWS } |
| 1346 |
|
| 1347 |
func Fchmod(fd Handle, mode uint32) (err error) { return EWINDOWS } |
| 1348 |
func Chown(path string, uid int, gid int) (err error) { return EWINDOWS } |
| 1349 |
func Lchown(path string, uid int, gid int) (err error) { return EWINDOWS } |
| 1350 |
func Fchown(fd Handle, uid int, gid int) (err error) { return EWINDOWS } |
| 1351 |
|
| 1352 |
func Getuid() (uid int) { return -1 } |
| 1353 |
func Geteuid() (euid int) { return -1 } |
| 1354 |
func Getgid() (gid int) { return -1 } |
| 1355 |
func Getegid() (egid int) { return -1 } |
| 1356 |
func Getgroups() (gids []int, err error) { return nil, EWINDOWS } |
| 1357 |
|
| 1358 |
type Signal int |
| 1359 |
|
| 1360 |
func (s Signal) Signal() {} |
| 1361 |
|
| 1362 |
func (s Signal) String() string { |
| 1363 |
if 0 <= s && int(s) < len(signals) { |
| 1364 |
str := signals[s] |
| 1365 |
if str != "" { |
| 1366 |
return str |
| 1367 |
} |
| 1368 |
} |
| 1369 |
return "signal " + strconv.Itoa(int(s)) |
| 1370 |
} |
| 1371 |
|
| 1372 |
func LoadCreateSymbolicLink() error { |
| 1373 |
return procCreateSymbolicLinkW.Find() |
| 1374 |
} |
| 1375 |
|
| 1376 |
// Readlink returns the destination of the named symbolic link. |
| 1377 |
func Readlink(path string, buf []byte) (n int, err error) { |
| 1378 |
pathp, err := UTF16PtrFromString(path) |
| 1379 |
if err != nil { |
| 1380 |
return -1, err |
| 1381 |
} |
| 1382 |
fd, err := CreateFile(pathp, GENERIC_READ, 0, nil, OPEN_EXISTING, |
| 1383 |
FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, 0) |
| 1384 |
if err != nil { |
| 1385 |
return -1, err |
| 1386 |
} |
| 1387 |
defer CloseHandle(fd) |
| 1388 |
|
| 1389 |
rdbbuf := make([]byte, MAXIMUM_REPARSE_DATA_BUFFER_SIZE) |
| 1390 |
var bytesReturned uint32 |
| 1391 |
err = DeviceIoControl(fd, FSCTL_GET_REPARSE_POINT, nil, 0, &rdbbuf[0], uint32(len(rdbbuf)), &bytesReturned, nil) |
| 1392 |
if err != nil { |
| 1393 |
return -1, err |
| 1394 |
} |
| 1395 |
|
| 1396 |
rdb := (*reparseDataBuffer)(unsafe.Pointer(&rdbbuf[0])) |
| 1397 |
var s string |
| 1398 |
switch rdb.ReparseTag { |
| 1399 |
case IO_REPARSE_TAG_SYMLINK: |
| 1400 |
data := (*symbolicLinkReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer)) |
| 1401 |
p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0])) |
| 1402 |
s = UTF16ToString(p[data.SubstituteNameOffset/2 : (data.SubstituteNameOffset+data.SubstituteNameLength)/2]) |
| 1403 |
if data.Flags&_SYMLINK_FLAG_RELATIVE == 0 { |
| 1404 |
if len(s) >= 4 && s[:4] == `\??\` { |
| 1405 |
s = s[4:] |
| 1406 |
switch { |
| 1407 |
case len(s) >= 2 && s[1] == ':': // \??\C:\foo\bar |
| 1408 |
// do nothing |
| 1409 |
case len(s) >= 4 && s[:4] == `UNC\`: // \??\UNC\foo\bar |
| 1410 |
s = `\\` + s[4:] |
| 1411 |
default: |
| 1412 |
// unexpected; do nothing |
| 1413 |
} |
| 1414 |
} else { |
| 1415 |
// unexpected; do nothing |
| 1416 |
} |
| 1417 |
} |
| 1418 |
case _IO_REPARSE_TAG_MOUNT_POINT: |
| 1419 |
data := (*mountPointReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer)) |
| 1420 |
p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0])) |
| 1421 |
s = UTF16ToString(p[data.SubstituteNameOffset/2 : (data.SubstituteNameOffset+data.SubstituteNameLength)/2]) |
| 1422 |
if len(s) >= 4 && s[:4] == `\??\` { // \??\C:\foo\bar |
| 1423 |
s = s[4:] |
| 1424 |
} else { |
| 1425 |
// unexpected; do nothing |
| 1426 |
} |
| 1427 |
default: |
| 1428 |
// the path is not a symlink or junction but another type of reparse |
| 1429 |
// point |
| 1430 |
return -1, ENOENT |
| 1431 |
} |
| 1432 |
n = copy(buf, []byte(s)) |
| 1433 |
|
| 1434 |
return n, nil |
| 1435 |
} |
| 1436 |
|
| 1437 |
// Deprecated: CreateIoCompletionPort has the wrong function signature. Use x/sys/windows.CreateIoCompletionPort. |
| 1438 |
func CreateIoCompletionPort(filehandle Handle, cphandle Handle, key uint32, threadcnt uint32) (Handle, error) { |
| 1439 |
return createIoCompletionPort(filehandle, cphandle, uintptr(key), threadcnt) |
| 1440 |
} |
| 1441 |
|
| 1442 |
// Deprecated: GetQueuedCompletionStatus has the wrong function signature. Use x/sys/windows.GetQueuedCompletionStatus. |
| 1443 |
func GetQueuedCompletionStatus(cphandle Handle, qty *uint32, key *uint32, overlapped **Overlapped, timeout uint32) error { |
| 1444 |
var ukey uintptr |
| 1445 |
var pukey *uintptr |
| 1446 |
if key != nil { |
| 1447 |
ukey = uintptr(*key) |
| 1448 |
pukey = &ukey |
| 1449 |
} |
| 1450 |
err := getQueuedCompletionStatus(cphandle, qty, pukey, overlapped, timeout) |
| 1451 |
if key != nil { |
| 1452 |
*key = uint32(ukey) |
| 1453 |
if uintptr(*key) != ukey && err == nil { |
| 1454 |
err = errorspkg.New("GetQueuedCompletionStatus returned key overflow") |
| 1455 |
} |
| 1456 |
} |
| 1457 |
return err |
| 1458 |
} |
| 1459 |
|
| 1460 |
// Deprecated: PostQueuedCompletionStatus has the wrong function signature. Use x/sys/windows.PostQueuedCompletionStatus. |
| 1461 |
func PostQueuedCompletionStatus(cphandle Handle, qty uint32, key uint32, overlapped *Overlapped) error { |
| 1462 |
return postQueuedCompletionStatus(cphandle, qty, uintptr(key), overlapped) |
| 1463 |
} |
| 1464 |
|
| 1465 |
// newProcThreadAttributeList allocates a new [procThreadAttributeListContainer], with the requested maximum number of attributes. |
| 1466 |
func newProcThreadAttributeList(maxAttrCount uint32) (*procThreadAttributeListContainer, error) { |
| 1467 |
var size uintptr |
| 1468 |
err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size) |
| 1469 |
if err != ERROR_INSUFFICIENT_BUFFER { |
| 1470 |
if err == nil { |
| 1471 |
return nil, errorspkg.New("unable to query buffer size from InitializeProcThreadAttributeList") |
| 1472 |
} |
| 1473 |
return nil, err |
| 1474 |
} |
| 1475 |
const LMEM_FIXED = 0 |
| 1476 |
alloc, err := localAlloc(LMEM_FIXED, uint32(size)) |
| 1477 |
if err != nil { |
| 1478 |
return nil, err |
| 1479 |
} |
| 1480 |
// size is guaranteed to be ≥1 by InitializeProcThreadAttributeList. |
| 1481 |
al := &procThreadAttributeListContainer{data: (*_PROC_THREAD_ATTRIBUTE_LIST)(unsafe.Pointer(alloc))} |
| 1482 |
err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size) |
| 1483 |
if err != nil { |
| 1484 |
return nil, err |
| 1485 |
} |
| 1486 |
al.pointers = make([]unsafe.Pointer, 0, maxAttrCount) |
| 1487 |
return al, err |
| 1488 |
} |
| 1489 |
|
| 1490 |
// Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute. |
| 1491 |
func (al *procThreadAttributeListContainer) update(attribute uintptr, value unsafe.Pointer, size uintptr) error { |
| 1492 |
al.pointers = append(al.pointers, value) |
| 1493 |
return updateProcThreadAttribute(al.data, 0, attribute, value, size, nil, nil) |
| 1494 |
} |
| 1495 |
|
| 1496 |
// Delete frees ProcThreadAttributeList's resources. |
| 1497 |
func (al *procThreadAttributeListContainer) delete() { |
| 1498 |
deleteProcThreadAttributeList(al.data) |
| 1499 |
LocalFree(Handle(unsafe.Pointer(al.data))) |
| 1500 |
al.data = nil |
| 1501 |
al.pointers = nil |
| 1502 |
} |
| 1503 |
|
| 1504 |
// List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx. |
| 1505 |
func (al *procThreadAttributeListContainer) list() *_PROC_THREAD_ATTRIBUTE_LIST { |
| 1506 |
return al.data |
| 1507 |
} |
| 1508 |
|
| 1509 |
// RegEnumKeyEx enumerates the subkeys of an open registry key. |
| 1510 |
// Each call retrieves information about one subkey. name is |
| 1511 |
// a buffer that should be large enough to hold the name of the |
| 1512 |
// subkey plus a null terminating character. nameLen is its |
| 1513 |
// length. On return, nameLen will contain the actual length of the |
| 1514 |
// subkey. |
| 1515 |
// |
| 1516 |
// Should name not be large enough to hold the subkey, this function |
| 1517 |
// will return ERROR_MORE_DATA, and must be called again with an |
| 1518 |
// appropriately sized buffer. |
| 1519 |
// |
| 1520 |
// reserved must be nil. class and classLen behave like name and nameLen |
| 1521 |
// but for the class of the subkey, except that they are optional. |
| 1522 |
// lastWriteTime, if not nil, will be populated with the time the subkey |
| 1523 |
// was last written. |
| 1524 |
// |
| 1525 |
// The caller must enumerate all subkeys in order. That is |
| 1526 |
// RegEnumKeyEx must be called with index starting at 0, incrementing |
| 1527 |
// the index until the function returns ERROR_NO_MORE_ITEMS, or with |
| 1528 |
// the index of the last subkey (obtainable from RegQueryInfoKey), |
| 1529 |
// decrementing until index 0 is enumerated. |
| 1530 |
// |
| 1531 |
// Successive calls to this API must happen on the same OS thread, |
| 1532 |
// so call [runtime.LockOSThread] before calling this function. |
| 1533 |
func RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) { |
| 1534 |
return regEnumKeyEx(key, index, name, nameLen, reserved, class, classLen, lastWriteTime) |
| 1535 |
} |
| 1536 |
|
| 1537 |
func GetStartupInfo(startupInfo *StartupInfo) error { |
| 1538 |
getStartupInfo(startupInfo) |
| 1539 |
return nil |
| 1540 |
} |
| 1541 |
|
| 1542 |
func CreateFile(name *uint16, access uint32, mode uint32, sa *SecurityAttributes, createmode uint32, attrs uint32, templatefile int32) (handle Handle, err error) { |
| 1543 |
handle, err = createFile(name, access, mode, sa, createmode, attrs, templatefile) |
| 1544 |
if handle != InvalidHandle { |
| 1545 |
// CreateFileW can return ERROR_ALREADY_EXISTS with a valid handle. |
| 1546 |
// We only want to return an error if the handle is invalid. |
| 1547 |
err = nil |
| 1548 |
} |
| 1549 |
return handle, err |
| 1550 |
} |