comparison vendor/golang.org/x/sys/windows/syscall_windows.go @ 68:4b79810863f6 draft

Ready to release 0.6.0 Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>
author yakumo.izuru
date Wed, 13 Sep 2023 10:49:50 +0000
parents 787b5ee0289d
children
comparison
equal deleted inserted replaced
67:4edfa07d5fe0 68:4b79810863f6
136 n := 0 136 n := 0
137 for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; n++ { 137 for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; n++ {
138 ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p)) 138 ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p))
139 } 139 }
140 140
141 var s []uint16 141 return string(utf16.Decode(unsafe.Slice(p, n)))
142 h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
143 h.Data = unsafe.Pointer(p)
144 h.Len = n
145 h.Cap = n
146
147 return string(utf16.Decode(s))
148 } 142 }
149 143
150 func Getpagesize() int { return 4096 } 144 func Getpagesize() int { return 4096 }
151 145
152 // NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention. 146 // NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention.
362 //sys SetProcessWorkingSetSizeEx(hProcess Handle, dwMinimumWorkingSetSize uintptr, dwMaximumWorkingSetSize uintptr, flags uint32) (err error) 356 //sys SetProcessWorkingSetSizeEx(hProcess Handle, dwMinimumWorkingSetSize uintptr, dwMaximumWorkingSetSize uintptr, flags uint32) (err error)
363 //sys GetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error) 357 //sys GetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
364 //sys SetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error) 358 //sys SetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
365 //sys GetActiveProcessorCount(groupNumber uint16) (ret uint32) 359 //sys GetActiveProcessorCount(groupNumber uint16) (ret uint32)
366 //sys GetMaximumProcessorCount(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
367 370
368 // Volume Management Functions 371 // Volume Management Functions
369 //sys DefineDosDevice(flags uint32, deviceName *uint16, targetPath *uint16) (err error) = DefineDosDeviceW 372 //sys DefineDosDevice(flags uint32, deviceName *uint16, targetPath *uint16) (err error) = DefineDosDeviceW
370 //sys DeleteVolumeMountPoint(volumeMountPoint *uint16) (err error) = DeleteVolumeMountPointW 373 //sys DeleteVolumeMountPoint(volumeMountPoint *uint16) (err error) = DeleteVolumeMountPointW
371 //sys FindFirstVolume(volumeName *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeW 374 //sys FindFirstVolume(volumeName *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeW
415 //sys EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) = psapi.EnumProcessModules 418 //sys EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) = psapi.EnumProcessModules
416 //sys EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) = psapi.EnumProcessModulesEx 419 //sys EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) = psapi.EnumProcessModulesEx
417 //sys GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) = psapi.GetModuleInformation 420 //sys GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) = psapi.GetModuleInformation
418 //sys GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) = psapi.GetModuleFileNameExW 421 //sys GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) = psapi.GetModuleFileNameExW
419 //sys GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) = psapi.GetModuleBaseNameW 422 //sys GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) = psapi.GetModuleBaseNameW
423 //sys QueryWorkingSetEx(process Handle, pv uintptr, cb uint32) (err error) = psapi.QueryWorkingSetEx
420 424
421 // NT Native APIs 425 // NT Native APIs
422 //sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb 426 //sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb
423 //sys rtlGetVersion(info *OsVersionInfoEx) (ntstatus error) = ntdll.RtlGetVersion 427 //sys rtlGetVersion(info *OsVersionInfoEx) (ntstatus error) = ntdll.RtlGetVersion
424 //sys rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers 428 //sys rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers
436 //sys NtQuerySystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32, retLen *uint32) (ntstatus error) = ntdll.NtQuerySystemInformation 440 //sys NtQuerySystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32, retLen *uint32) (ntstatus error) = ntdll.NtQuerySystemInformation
437 //sys NtSetSystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32) (ntstatus error) = ntdll.NtSetSystemInformation 441 //sys NtSetSystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32) (ntstatus error) = ntdll.NtSetSystemInformation
438 //sys RtlAddFunctionTable(functionTable *RUNTIME_FUNCTION, entryCount uint32, baseAddress uintptr) (ret bool) = ntdll.RtlAddFunctionTable 442 //sys RtlAddFunctionTable(functionTable *RUNTIME_FUNCTION, entryCount uint32, baseAddress uintptr) (ret bool) = ntdll.RtlAddFunctionTable
439 //sys RtlDeleteFunctionTable(functionTable *RUNTIME_FUNCTION) (ret bool) = ntdll.RtlDeleteFunctionTable 443 //sys RtlDeleteFunctionTable(functionTable *RUNTIME_FUNCTION) (ret bool) = ntdll.RtlDeleteFunctionTable
440 444
445 // Desktop Window Manager API (Dwmapi)
446 //sys DwmGetWindowAttribute(hwnd HWND, attribute uint32, value unsafe.Pointer, size uint32) (ret error) = dwmapi.DwmGetWindowAttribute
447 //sys DwmSetWindowAttribute(hwnd HWND, attribute uint32, value unsafe.Pointer, size uint32) (ret error) = dwmapi.DwmSetWindowAttribute
448
441 // syscall interface implementation for other packages 449 // syscall interface implementation for other packages
442 450
443 // GetCurrentProcess returns the handle for the current process. 451 // GetCurrentProcess returns the handle for the current process.
444 // It is a pseudo handle that does not need to be closed. 452 // It is a pseudo handle that does not need to be closed.
445 // The returned error is always nil. 453 // The returned error is always nil.
745 FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil, 753 FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
746 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) 754 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
747 if e != nil { 755 if e != nil {
748 return e 756 return e
749 } 757 }
750 defer Close(h) 758 defer CloseHandle(h)
751 a := NsecToFiletime(tv[0].Nanoseconds()) 759 a := NsecToFiletime(tv[0].Nanoseconds())
752 w := NsecToFiletime(tv[1].Nanoseconds()) 760 w := NsecToFiletime(tv[1].Nanoseconds())
753 return SetFileTime(h, nil, &a, &w) 761 return SetFileTime(h, nil, &a, &w)
754 } 762 }
755 763
765 FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil, 773 FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
766 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) 774 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
767 if e != nil { 775 if e != nil {
768 return e 776 return e
769 } 777 }
770 defer Close(h) 778 defer CloseHandle(h)
771 a := NsecToFiletime(TimespecToNsec(ts[0])) 779 a := NsecToFiletime(TimespecToNsec(ts[0]))
772 w := NsecToFiletime(TimespecToNsec(ts[1])) 780 w := NsecToFiletime(TimespecToNsec(ts[1]))
773 return SetFileTime(h, nil, &a, &w) 781 return SetFileTime(h, nil, &a, &w)
774 } 782 }
775 783
969 } 977 }
970 978
971 return unsafe.Pointer(&sa.raw), sl, nil 979 return unsafe.Pointer(&sa.raw), sl, nil
972 } 980 }
973 981
982 type RawSockaddrBth struct {
983 AddressFamily [2]byte
984 BtAddr [8]byte
985 ServiceClassId [16]byte
986 Port [4]byte
987 }
988
989 type SockaddrBth struct {
990 BtAddr uint64
991 ServiceClassId GUID
992 Port uint32
993
994 raw RawSockaddrBth
995 }
996
997 func (sa *SockaddrBth) sockaddr() (unsafe.Pointer, int32, error) {
998 family := AF_BTH
999 sa.raw = RawSockaddrBth{
1000 AddressFamily: *(*[2]byte)(unsafe.Pointer(&family)),
1001 BtAddr: *(*[8]byte)(unsafe.Pointer(&sa.BtAddr)),
1002 Port: *(*[4]byte)(unsafe.Pointer(&sa.Port)),
1003 ServiceClassId: *(*[16]byte)(unsafe.Pointer(&sa.ServiceClassId)),
1004 }
1005 return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil
1006 }
1007
974 func (rsa *RawSockaddrAny) Sockaddr() (Sockaddr, error) { 1008 func (rsa *RawSockaddrAny) Sockaddr() (Sockaddr, error) {
975 switch rsa.Addr.Family { 1009 switch rsa.Addr.Family {
976 case AF_UNIX: 1010 case AF_UNIX:
977 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) 1011 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
978 sa := new(SockaddrUnix) 1012 sa := new(SockaddrUnix)
1079 func Shutdown(fd Handle, how int) (err error) { 1113 func Shutdown(fd Handle, how int) (err error) {
1080 return shutdown(fd, int32(how)) 1114 return shutdown(fd, int32(how))
1081 } 1115 }
1082 1116
1083 func WSASendto(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to Sockaddr, overlapped *Overlapped, croutine *byte) (err error) { 1117 func WSASendto(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to Sockaddr, overlapped *Overlapped, croutine *byte) (err error) {
1084 rsa, l, err := to.sockaddr() 1118 var rsa unsafe.Pointer
1085 if err != nil { 1119 var l int32
1086 return err 1120 if to != nil {
1121 rsa, l, err = to.sockaddr()
1122 if err != nil {
1123 return err
1124 }
1087 } 1125 }
1088 return WSASendTo(s, bufs, bufcnt, sent, flags, (*RawSockaddrAny)(unsafe.Pointer(rsa)), l, overlapped, croutine) 1126 return WSASendTo(s, bufs, bufcnt, sent, flags, (*RawSockaddrAny)(unsafe.Pointer(rsa)), l, overlapped, croutine)
1089 } 1127 }
1090 1128
1091 func LoadGetAddrInfo() error { 1129 func LoadGetAddrInfo() error {
1705 h.Data = unsafe.Pointer(ptr) 1743 h.Data = unsafe.Pointer(ptr)
1706 h.Len = int(size) 1744 h.Len = int(size)
1707 h.Cap = int(size) 1745 h.Cap = int(size)
1708 return 1746 return
1709 } 1747 }
1748
1749 // PSAPI_WORKING_SET_EX_BLOCK contains extended working set information for a page.
1750 type PSAPI_WORKING_SET_EX_BLOCK uint64
1751
1752 // Valid returns the validity of this page.
1753 // If this bit is 1, the subsequent members are valid; otherwise they should be ignored.
1754 func (b PSAPI_WORKING_SET_EX_BLOCK) Valid() bool {
1755 return (b & 1) == 1
1756 }
1757
1758 // ShareCount is the number of processes that share this page. The maximum value of this member is 7.
1759 func (b PSAPI_WORKING_SET_EX_BLOCK) ShareCount() uint64 {
1760 return b.intField(1, 3)
1761 }
1762
1763 // Win32Protection is the memory protection attributes of the page. For a list of values, see
1764 // https://docs.microsoft.com/en-us/windows/win32/memory/memory-protection-constants
1765 func (b PSAPI_WORKING_SET_EX_BLOCK) Win32Protection() uint64 {
1766 return b.intField(4, 11)
1767 }
1768
1769 // Shared returns the shared status of this page.
1770 // If this bit is 1, the page can be shared.
1771 func (b PSAPI_WORKING_SET_EX_BLOCK) Shared() bool {
1772 return (b & (1 << 15)) == 1
1773 }
1774
1775 // Node is the NUMA node. The maximum value of this member is 63.
1776 func (b PSAPI_WORKING_SET_EX_BLOCK) Node() uint64 {
1777 return b.intField(16, 6)
1778 }
1779
1780 // Locked returns the locked status of this page.
1781 // If this bit is 1, the virtual page is locked in physical memory.
1782 func (b PSAPI_WORKING_SET_EX_BLOCK) Locked() bool {
1783 return (b & (1 << 22)) == 1
1784 }
1785
1786 // LargePage returns the large page status of this page.
1787 // If this bit is 1, the page is a large page.
1788 func (b PSAPI_WORKING_SET_EX_BLOCK) LargePage() bool {
1789 return (b & (1 << 23)) == 1
1790 }
1791
1792 // Bad returns the bad status of this page.
1793 // If this bit is 1, the page is has been reported as bad.
1794 func (b PSAPI_WORKING_SET_EX_BLOCK) Bad() bool {
1795 return (b & (1 << 31)) == 1
1796 }
1797
1798 // intField extracts an integer field in the PSAPI_WORKING_SET_EX_BLOCK union.
1799 func (b PSAPI_WORKING_SET_EX_BLOCK) intField(start, length int) uint64 {
1800 var mask PSAPI_WORKING_SET_EX_BLOCK
1801 for pos := start; pos < start+length; pos++ {
1802 mask |= (1 << pos)
1803 }
1804
1805 masked := b & mask
1806 return uint64(masked >> start)
1807 }
1808
1809 // PSAPI_WORKING_SET_EX_INFORMATION contains extended working set information for a process.
1810 type PSAPI_WORKING_SET_EX_INFORMATION struct {
1811 // The virtual address.
1812 VirtualAddress Pointer
1813 // A PSAPI_WORKING_SET_EX_BLOCK union that indicates the attributes of the page at VirtualAddress.
1814 VirtualAttributes PSAPI_WORKING_SET_EX_BLOCK
1815 }