2014-04-18 13:23:56 -07:00
|
|
|
//! C definitions used by libnative that don't belong in liblibc
|
|
|
|
|
2018-08-29 08:21:01 -05:00
|
|
|
#![allow(nonstandard_style)]
|
2016-02-20 08:18:02 +01:00
|
|
|
#![cfg_attr(test, allow(dead_code))]
|
2019-12-21 13:16:18 +02:00
|
|
|
#![unstable(issue = "none", feature = "windows_c")]
|
2014-04-18 13:23:56 -07:00
|
|
|
|
2022-07-21 11:37:03 +01:00
|
|
|
use crate::ffi::CStr;
|
2022-01-06 02:47:36 +00:00
|
|
|
use crate::mem;
|
2023-01-19 23:21:21 -08:00
|
|
|
use crate::os::raw::{c_char, c_long, c_longlong, c_uint, c_ulong, c_ushort};
|
2022-03-03 11:07:23 -08:00
|
|
|
use crate::os::windows::io::{BorrowedHandle, HandleOrInvalid, HandleOrNull};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::ptr;
|
2022-03-01 14:46:41 -08:00
|
|
|
use core::ffi::NonZero_c_ulong;
|
2019-02-11 04:23:21 +09:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
use libc::{c_void, size_t, wchar_t};
|
2015-12-02 17:07:29 -08:00
|
|
|
|
2023-01-19 23:21:21 -08:00
|
|
|
pub use crate::os::raw::c_int;
|
|
|
|
|
2021-04-30 15:56:28 +01:00
|
|
|
#[path = "c/errors.rs"] // c.rs is included from two places so we need to specify this
|
|
|
|
mod errors;
|
|
|
|
pub use errors::*;
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub use self::EXCEPTION_DISPOSITION::*;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub use self::FILE_INFO_BY_HANDLE_CLASS::*;
|
|
|
|
|
2021-04-29 11:40:14 +02:00
|
|
|
pub type DWORD_PTR = ULONG_PTR;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type DWORD = c_ulong;
|
2021-03-03 11:36:07 +00:00
|
|
|
pub type NonZeroDWORD = NonZero_c_ulong;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type HANDLE = LPVOID;
|
|
|
|
pub type HINSTANCE = HANDLE;
|
|
|
|
pub type HMODULE = HINSTANCE;
|
2017-09-06 20:40:34 +01:00
|
|
|
pub type HRESULT = LONG;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type BOOL = c_int;
|
|
|
|
pub type BYTE = u8;
|
|
|
|
pub type BOOLEAN = BYTE;
|
|
|
|
pub type GROUP = c_uint;
|
|
|
|
pub type LARGE_INTEGER = c_longlong;
|
|
|
|
pub type LONG = c_long;
|
|
|
|
pub type UINT = c_uint;
|
|
|
|
pub type WCHAR = u16;
|
|
|
|
pub type USHORT = c_ushort;
|
|
|
|
pub type SIZE_T = usize;
|
|
|
|
pub type WORD = u16;
|
|
|
|
pub type CHAR = c_char;
|
2022-01-06 02:47:36 +00:00
|
|
|
pub type CCHAR = c_char;
|
2017-05-06 15:46:16 +01:00
|
|
|
pub type ULONG_PTR = usize;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type ULONG = c_ulong;
|
2020-10-06 00:34:15 +02:00
|
|
|
pub type NTSTATUS = LONG;
|
|
|
|
pub type ACCESS_MASK = DWORD;
|
2015-11-02 16:23:22 -08:00
|
|
|
|
|
|
|
pub type LPBOOL = *mut BOOL;
|
|
|
|
pub type LPBYTE = *mut BYTE;
|
2023-01-19 23:21:21 -08:00
|
|
|
pub type LPCCH = *const CHAR;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type LPCSTR = *const CHAR;
|
2023-01-19 23:21:21 -08:00
|
|
|
pub type LPCWCH = *const WCHAR;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type LPCWSTR = *const WCHAR;
|
2023-01-19 23:21:21 -08:00
|
|
|
pub type LPCVOID = *const c_void;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type LPDWORD = *mut DWORD;
|
|
|
|
pub type LPHANDLE = *mut HANDLE;
|
|
|
|
pub type LPOVERLAPPED = *mut OVERLAPPED;
|
|
|
|
pub type LPPROCESS_INFORMATION = *mut PROCESS_INFORMATION;
|
|
|
|
pub type LPSECURITY_ATTRIBUTES = *mut SECURITY_ATTRIBUTES;
|
|
|
|
pub type LPSTARTUPINFO = *mut STARTUPINFO;
|
2023-01-19 23:21:21 -08:00
|
|
|
pub type LPSTR = *mut CHAR;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type LPVOID = *mut c_void;
|
|
|
|
pub type LPWCH = *mut WCHAR;
|
|
|
|
pub type LPWIN32_FIND_DATAW = *mut WIN32_FIND_DATAW;
|
|
|
|
pub type LPWSADATA = *mut WSADATA;
|
|
|
|
pub type LPWSAPROTOCOL_INFO = *mut WSAPROTOCOL_INFO;
|
|
|
|
pub type LPWSTR = *mut WCHAR;
|
2015-11-16 17:36:14 -08:00
|
|
|
pub type LPFILETIME = *mut FILETIME;
|
2021-04-29 11:40:14 +02:00
|
|
|
pub type LPSYSTEM_INFO = *mut SYSTEM_INFO;
|
2019-02-08 20:42:34 +01:00
|
|
|
pub type LPWSABUF = *mut WSABUF;
|
|
|
|
pub type LPWSAOVERLAPPED = *mut c_void;
|
|
|
|
pub type LPWSAOVERLAPPED_COMPLETION_ROUTINE = *mut c_void;
|
2022-09-06 10:49:34 +01:00
|
|
|
pub type BCRYPT_ALG_HANDLE = LPVOID;
|
2015-11-02 16:23:22 -08:00
|
|
|
|
|
|
|
pub type PCONDITION_VARIABLE = *mut CONDITION_VARIABLE;
|
|
|
|
pub type PLARGE_INTEGER = *mut c_longlong;
|
|
|
|
pub type PSRWLOCK = *mut SRWLOCK;
|
2022-10-08 20:19:21 +02:00
|
|
|
pub type LPINIT_ONCE = *mut INIT_ONCE;
|
2015-11-02 16:23:22 -08:00
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
pub type SOCKET = crate::os::windows::raw::SOCKET;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type socklen_t = c_int;
|
|
|
|
pub type ADDRESS_FAMILY = USHORT;
|
|
|
|
|
|
|
|
pub const TRUE: BOOL = 1;
|
|
|
|
pub const FALSE: BOOL = 0;
|
|
|
|
|
2021-05-19 23:34:15 +01:00
|
|
|
pub const CSTR_LESS_THAN: c_int = 1;
|
|
|
|
pub const CSTR_EQUAL: c_int = 2;
|
|
|
|
pub const CSTR_GREATER_THAN: c_int = 3;
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FILE_ATTRIBUTE_READONLY: DWORD = 0x1;
|
|
|
|
pub const FILE_ATTRIBUTE_DIRECTORY: DWORD = 0x10;
|
|
|
|
pub const FILE_ATTRIBUTE_REPARSE_POINT: DWORD = 0x400;
|
2022-02-14 12:20:40 +00:00
|
|
|
pub const INVALID_FILE_ATTRIBUTES: DWORD = DWORD::MAX;
|
2016-02-20 08:18:02 +01:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FILE_SHARE_DELETE: DWORD = 0x4;
|
|
|
|
pub const FILE_SHARE_READ: DWORD = 0x1;
|
|
|
|
pub const FILE_SHARE_WRITE: DWORD = 0x2;
|
2016-02-20 08:18:02 +01:00
|
|
|
|
2022-01-22 14:50:08 +00:00
|
|
|
pub const FILE_OPEN: ULONG = 0x00000001;
|
2022-01-06 02:47:36 +00:00
|
|
|
pub const FILE_OPEN_REPARSE_POINT: ULONG = 0x200000;
|
|
|
|
pub const OBJ_DONT_REPARSE: ULONG = 0x1000;
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const CREATE_ALWAYS: DWORD = 2;
|
|
|
|
pub const CREATE_NEW: DWORD = 1;
|
|
|
|
pub const OPEN_ALWAYS: DWORD = 4;
|
|
|
|
pub const OPEN_EXISTING: DWORD = 3;
|
|
|
|
pub const TRUNCATE_EXISTING: DWORD = 5;
|
2016-01-13 18:08:08 +01:00
|
|
|
|
2022-01-06 02:47:36 +00:00
|
|
|
pub const FILE_LIST_DIRECTORY: DWORD = 0x1;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FILE_WRITE_DATA: DWORD = 0x00000002;
|
2016-01-13 18:08:08 +01:00
|
|
|
pub const FILE_APPEND_DATA: DWORD = 0x00000004;
|
|
|
|
pub const FILE_WRITE_EA: DWORD = 0x00000010;
|
|
|
|
pub const FILE_WRITE_ATTRIBUTES: DWORD = 0x00000100;
|
2022-01-06 02:47:36 +00:00
|
|
|
pub const DELETE: DWORD = 0x10000;
|
2016-01-13 18:08:08 +01:00
|
|
|
pub const READ_CONTROL: DWORD = 0x00020000;
|
|
|
|
pub const SYNCHRONIZE: DWORD = 0x00100000;
|
|
|
|
pub const GENERIC_READ: DWORD = 0x80000000;
|
|
|
|
pub const GENERIC_WRITE: DWORD = 0x40000000;
|
|
|
|
pub const STANDARD_RIGHTS_WRITE: DWORD = READ_CONTROL;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FILE_GENERIC_WRITE: DWORD = STANDARD_RIGHTS_WRITE
|
|
|
|
| FILE_WRITE_DATA
|
|
|
|
| FILE_WRITE_ATTRIBUTES
|
|
|
|
| FILE_WRITE_EA
|
|
|
|
| FILE_APPEND_DATA
|
|
|
|
| SYNCHRONIZE;
|
|
|
|
|
2016-02-20 08:18:02 +01:00
|
|
|
pub const FILE_FLAG_OPEN_REPARSE_POINT: DWORD = 0x00200000;
|
|
|
|
pub const FILE_FLAG_BACKUP_SEMANTICS: DWORD = 0x02000000;
|
2016-01-13 18:08:08 +01:00
|
|
|
pub const SECURITY_SQOS_PRESENT: DWORD = 0x00100000;
|
|
|
|
|
2016-02-27 14:15:19 -08:00
|
|
|
pub const FIONBIO: c_ulong = 0x8004667e;
|
|
|
|
|
2022-06-12 14:10:18 -07:00
|
|
|
pub const MAX_PATH: usize = 260;
|
|
|
|
|
2022-10-21 17:57:22 +01:00
|
|
|
pub const FILE_TYPE_PIPE: u32 = 3;
|
|
|
|
|
2023-01-19 23:21:21 -08:00
|
|
|
pub const CP_UTF8: DWORD = 65001;
|
|
|
|
pub const MB_ERR_INVALID_CHARS: DWORD = 0x08;
|
|
|
|
pub const WC_ERR_INVALID_CHARS: DWORD = 0x80;
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy)]
|
|
|
|
pub struct WIN32_FIND_DATAW {
|
|
|
|
pub dwFileAttributes: DWORD,
|
|
|
|
pub ftCreationTime: FILETIME,
|
|
|
|
pub ftLastAccessTime: FILETIME,
|
|
|
|
pub ftLastWriteTime: FILETIME,
|
|
|
|
pub nFileSizeHigh: DWORD,
|
|
|
|
pub nFileSizeLow: DWORD,
|
|
|
|
pub dwReserved0: DWORD,
|
|
|
|
pub dwReserved1: DWORD,
|
|
|
|
pub cFileName: [wchar_t; 260], // #define MAX_PATH 260
|
|
|
|
pub cAlternateFileName: [wchar_t; 14],
|
|
|
|
}
|
|
|
|
impl Clone for WIN32_FIND_DATAW {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const WSA_FLAG_OVERLAPPED: DWORD = 0x01;
|
2019-05-27 16:51:29 +02:00
|
|
|
pub const WSA_FLAG_NO_HANDLE_INHERIT: DWORD = 0x80;
|
2015-11-02 16:23:22 -08:00
|
|
|
|
|
|
|
pub const WSADESCRIPTION_LEN: usize = 256;
|
|
|
|
pub const WSASYS_STATUS_LEN: usize = 128;
|
|
|
|
pub const WSAPROTOCOL_LEN: DWORD = 255;
|
|
|
|
pub const INVALID_SOCKET: SOCKET = !0;
|
|
|
|
|
|
|
|
pub const MAX_PROTOCOL_CHAIN: DWORD = 7;
|
|
|
|
|
2015-04-08 09:50:50 -07:00
|
|
|
pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x900a8;
|
|
|
|
pub const IO_REPARSE_TAG_SYMLINK: DWORD = 0xa000000c;
|
|
|
|
pub const IO_REPARSE_TAG_MOUNT_POINT: DWORD = 0xa0000003;
|
2016-02-13 20:44:37 +01:00
|
|
|
pub const SYMLINK_FLAG_RELATIVE: DWORD = 0x00000001;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FSCTL_SET_REPARSE_POINT: DWORD = 0x900a4;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const SYMBOLIC_LINK_FLAG_DIRECTORY: DWORD = 0x1;
|
2017-01-08 16:35:13 +05:30
|
|
|
pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: DWORD = 0x2;
|
Deprecate std::fs::soft_link in favor of platform-specific versions
On Windows, when you create a symbolic link you must specify whether it
points to a directory or a file, even if it is created dangling, while
on Unix, the same symbolic link could point to a directory, a file, or
nothing at all. Furthermore, on Windows special privilege is necessary
to use a symbolic link, while on Unix, you can generally create a
symbolic link in any directory you have write privileges to.
This means that it is unlikely to be able to use symbolic links purely
portably; anyone who uses them will need to think about the cross
platform implications. This means that using platform-specific APIs
will make it easier to see where code will need to differ between the
platforms, rather than trying to provide some kind of compatibility
wrapper.
Furthermore, `soft_link` has no precedence in any other API, so to avoid
confusion, move back to the more standard `symlink` terminology.
Create a `std::os::unix::symlink` for the Unix version that is
destination type agnostic, as well as `std::os::windows::{symlink_file,
symlink_dir}` for Windows.
Because this is a stable API, leave a compatibility wrapper in
`std::fs::soft_link`, which calls `symlink` on Unix and `symlink_file`
on Windows, preserving the existing behavior of `soft_link`.
2015-04-09 03:22:44 -04:00
|
|
|
|
2015-02-24 23:27:20 -08:00
|
|
|
// Note that these are not actually HANDLEs, just values to pass to GetStdHandle
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const STD_INPUT_HANDLE: DWORD = -10i32 as DWORD;
|
|
|
|
pub const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
|
|
|
|
pub const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
|
|
|
|
|
|
|
|
pub const PROGRESS_CONTINUE: DWORD = 0;
|
2015-02-24 23:27:20 -08:00
|
|
|
|
2017-09-06 20:40:34 +01:00
|
|
|
pub const E_NOTIMPL: HRESULT = 0x80004001u32 as HRESULT;
|
|
|
|
|
2022-03-22 01:24:55 -04:00
|
|
|
pub const INVALID_HANDLE_VALUE: HANDLE = ptr::invalid_mut(!0);
|
2015-11-02 16:23:22 -08:00
|
|
|
|
2017-05-03 10:19:23 -07:00
|
|
|
pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
|
2017-05-03 10:19:23 -07:00
|
|
|
pub const FORMAT_MESSAGE_FROM_HMODULE: DWORD = 0x00000800;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
|
|
|
|
|
|
|
|
pub const TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
pub const DLL_THREAD_DETACH: DWORD = 3;
|
|
|
|
pub const DLL_PROCESS_DETACH: DWORD = 0;
|
|
|
|
|
|
|
|
pub const INFINITE: DWORD = !0;
|
|
|
|
|
|
|
|
pub const DUPLICATE_SAME_ACCESS: DWORD = 0x00000002;
|
|
|
|
|
|
|
|
pub const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE { ptr: ptr::null_mut() };
|
|
|
|
pub const SRWLOCK_INIT: SRWLOCK = SRWLOCK { ptr: ptr::null_mut() };
|
2022-10-08 20:19:21 +02:00
|
|
|
pub const INIT_ONCE_STATIC_INIT: INIT_ONCE = INIT_ONCE { ptr: ptr::null_mut() };
|
|
|
|
|
|
|
|
pub const INIT_ONCE_INIT_FAILED: DWORD = 0x00000004;
|
2015-06-29 09:46:35 -07:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const DETACHED_PROCESS: DWORD = 0x00000008;
|
|
|
|
pub const CREATE_NEW_PROCESS_GROUP: DWORD = 0x00000200;
|
|
|
|
pub const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400;
|
|
|
|
pub const STARTF_USESTDHANDLES: DWORD = 0x00000100;
|
|
|
|
|
|
|
|
pub const AF_INET: c_int = 2;
|
|
|
|
pub const AF_INET6: c_int = 23;
|
|
|
|
pub const SD_BOTH: c_int = 2;
|
|
|
|
pub const SD_RECEIVE: c_int = 0;
|
|
|
|
pub const SD_SEND: c_int = 1;
|
|
|
|
pub const SOCK_DGRAM: c_int = 2;
|
|
|
|
pub const SOCK_STREAM: c_int = 1;
|
2021-05-28 20:32:42 +01:00
|
|
|
pub const SOCKET_ERROR: c_int = -1;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const SOL_SOCKET: c_int = 0xffff;
|
2021-08-30 13:02:15 -04:00
|
|
|
pub const SO_LINGER: c_int = 0x0080;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub const SO_RCVTIMEO: c_int = 0x1006;
|
|
|
|
pub const SO_SNDTIMEO: c_int = 0x1005;
|
2016-02-27 21:05:32 -08:00
|
|
|
pub const IPPROTO_IP: c_int = 0;
|
|
|
|
pub const IPPROTO_TCP: c_int = 6;
|
|
|
|
pub const IPPROTO_IPV6: c_int = 41;
|
|
|
|
pub const TCP_NODELAY: c_int = 0x0001;
|
|
|
|
pub const IP_TTL: c_int = 4;
|
|
|
|
pub const IPV6_V6ONLY: c_int = 27;
|
|
|
|
pub const SO_ERROR: c_int = 0x1007;
|
|
|
|
pub const SO_BROADCAST: c_int = 0x0020;
|
|
|
|
pub const IP_MULTICAST_LOOP: c_int = 11;
|
|
|
|
pub const IPV6_MULTICAST_LOOP: c_int = 11;
|
|
|
|
pub const IP_MULTICAST_TTL: c_int = 10;
|
|
|
|
pub const IP_ADD_MEMBERSHIP: c_int = 12;
|
|
|
|
pub const IP_DROP_MEMBERSHIP: c_int = 13;
|
|
|
|
pub const IPV6_ADD_MEMBERSHIP: c_int = 12;
|
|
|
|
pub const IPV6_DROP_MEMBERSHIP: c_int = 13;
|
2017-01-10 19:11:56 -08:00
|
|
|
pub const MSG_PEEK: c_int = 0x2;
|
2016-02-27 21:05:32 -08:00
|
|
|
|
2021-08-30 13:02:15 -04:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct linger {
|
|
|
|
pub l_onoff: c_ushort,
|
|
|
|
pub l_linger: c_ushort,
|
|
|
|
}
|
|
|
|
|
2016-02-27 21:05:32 -08:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct ip_mreq {
|
|
|
|
pub imr_multiaddr: in_addr,
|
|
|
|
pub imr_interface: in_addr,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct ipv6_mreq {
|
|
|
|
pub ipv6mr_multiaddr: in6_addr,
|
|
|
|
pub ipv6mr_interface: c_uint,
|
|
|
|
}
|
2015-11-02 16:23:22 -08:00
|
|
|
|
|
|
|
pub const VOLUME_NAME_DOS: DWORD = 0x0;
|
|
|
|
pub const MOVEFILE_REPLACE_EXISTING: DWORD = 1;
|
|
|
|
|
|
|
|
pub const FILE_BEGIN: DWORD = 0;
|
|
|
|
pub const FILE_CURRENT: DWORD = 1;
|
|
|
|
pub const FILE_END: DWORD = 2;
|
|
|
|
|
|
|
|
pub const WAIT_OBJECT_0: DWORD = 0x00000000;
|
2017-01-05 22:47:09 -08:00
|
|
|
pub const WAIT_TIMEOUT: DWORD = 258;
|
2017-08-26 19:36:46 -07:00
|
|
|
pub const WAIT_FAILED: DWORD = 0xFFFFFFFF;
|
2015-11-02 16:23:22 -08:00
|
|
|
|
2016-02-12 10:29:25 -08:00
|
|
|
pub const PIPE_ACCESS_INBOUND: DWORD = 0x00000001;
|
std: Don't pass overlapped handles to processes
This commit fixes a mistake introduced in #31618 where overlapped handles were
leaked to child processes on Windows. On Windows once a handle is in overlapped
mode it should always have I/O executed with an instance of `OVERLAPPED`. Most
child processes, however, are not prepared to have their stdio handles in
overlapped mode as they don't use `OVERLAPPED` on reads/writes to the handle.
Now we haven't had any odd behavior in Rust up to this point, and the original
bug was introduced almost a year ago. I believe this is because it turns out
that if you *don't* pass an `OVERLAPPED` then the system will [supply one for
you][link]. In this case everything will go awry if you concurrently operate on
the handle. In Rust, however, the stdio handles are always locked, and there's
no way to not use them unlocked in libstd. Due to that change we've always had
synchronized access to these handles, which means that Rust programs typically
"just work".
Conversely, though, this commit fixes the test case included, which exhibits
behavior that other programs Rust spawns may attempt to execute. Namely, the
stdio handles may be concurrently used and having them in overlapped mode wreaks
havoc.
[link]: https://blogs.msdn.microsoft.com/oldnewthing/20121012-00/?p=6343
Closes #38811
2017-01-04 15:32:39 -08:00
|
|
|
pub const PIPE_ACCESS_OUTBOUND: DWORD = 0x00000002;
|
2016-02-12 10:29:25 -08:00
|
|
|
pub const FILE_FLAG_FIRST_PIPE_INSTANCE: DWORD = 0x00080000;
|
|
|
|
pub const FILE_FLAG_OVERLAPPED: DWORD = 0x40000000;
|
|
|
|
pub const PIPE_WAIT: DWORD = 0x00000000;
|
|
|
|
pub const PIPE_TYPE_BYTE: DWORD = 0x00000000;
|
|
|
|
pub const PIPE_REJECT_REMOTE_CLIENTS: DWORD = 0x00000008;
|
|
|
|
pub const PIPE_READMODE_BYTE: DWORD = 0x00000000;
|
|
|
|
|
2017-07-04 23:46:24 -07:00
|
|
|
pub const FD_SETSIZE: usize = 64;
|
|
|
|
|
2018-07-30 14:02:09 +10:00
|
|
|
pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;
|
|
|
|
|
2020-10-06 00:34:15 +02:00
|
|
|
pub const STATUS_SUCCESS: NTSTATUS = 0x00000000;
|
2022-01-06 02:47:36 +00:00
|
|
|
pub const STATUS_DELETE_PENDING: NTSTATUS = 0xc0000056_u32 as _;
|
|
|
|
pub const STATUS_INVALID_PARAMETER: NTSTATUS = 0xc000000d_u32 as _;
|
|
|
|
|
2022-03-30 10:38:22 +01:00
|
|
|
pub const STATUS_PENDING: NTSTATUS = 0x103 as _;
|
|
|
|
pub const STATUS_END_OF_FILE: NTSTATUS = 0xC0000011_u32 as _;
|
|
|
|
|
2022-01-06 02:47:36 +00:00
|
|
|
// Equivalent to the `NT_SUCCESS` C preprocessor macro.
|
|
|
|
// See: https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values
|
|
|
|
pub fn nt_success(status: NTSTATUS) -> bool {
|
|
|
|
status >= 0
|
|
|
|
}
|
2020-10-06 00:34:15 +02:00
|
|
|
|
2022-09-08 09:38:14 +01:00
|
|
|
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
|
2021-04-11 14:55:47 +02:00
|
|
|
|
2022-01-06 02:47:36 +00:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct UNICODE_STRING {
|
|
|
|
pub Length: u16,
|
|
|
|
pub MaximumLength: u16,
|
|
|
|
pub Buffer: *mut u16,
|
|
|
|
}
|
|
|
|
impl UNICODE_STRING {
|
|
|
|
pub fn from_ref(slice: &[u16]) -> Self {
|
|
|
|
let len = slice.len() * mem::size_of::<u16>();
|
|
|
|
Self { Length: len as _, MaximumLength: len as _, Buffer: slice.as_ptr() as _ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct OBJECT_ATTRIBUTES {
|
|
|
|
pub Length: ULONG,
|
|
|
|
pub RootDirectory: HANDLE,
|
|
|
|
pub ObjectName: *const UNICODE_STRING,
|
|
|
|
pub Attributes: ULONG,
|
|
|
|
pub SecurityDescriptor: *mut c_void,
|
|
|
|
pub SecurityQualityOfService: *mut c_void,
|
|
|
|
}
|
|
|
|
impl Default for OBJECT_ATTRIBUTES {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
Length: mem::size_of::<Self>() as _,
|
|
|
|
RootDirectory: ptr::null_mut(),
|
|
|
|
ObjectName: ptr::null_mut(),
|
|
|
|
Attributes: 0,
|
|
|
|
SecurityDescriptor: ptr::null_mut(),
|
|
|
|
SecurityQualityOfService: ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[repr(C)]
|
2022-03-28 08:42:59 +01:00
|
|
|
union IO_STATUS_BLOCK_union {
|
|
|
|
Status: NTSTATUS,
|
|
|
|
Pointer: *mut c_void,
|
2022-01-06 02:47:36 +00:00
|
|
|
}
|
2022-03-28 08:42:59 +01:00
|
|
|
impl Default for IO_STATUS_BLOCK_union {
|
2022-01-06 02:47:36 +00:00
|
|
|
fn default() -> Self {
|
2022-07-06 16:38:35 +01:00
|
|
|
let mut this = Self { Pointer: ptr::null_mut() };
|
|
|
|
this.Status = STATUS_PENDING;
|
|
|
|
this
|
2022-01-06 02:47:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 08:42:59 +01:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct IO_STATUS_BLOCK {
|
|
|
|
u: IO_STATUS_BLOCK_union,
|
|
|
|
pub Information: usize,
|
|
|
|
}
|
2022-07-06 16:38:35 +01:00
|
|
|
impl IO_STATUS_BLOCK {
|
|
|
|
pub fn status(&self) -> NTSTATUS {
|
|
|
|
// SAFETY: If `self.u.Status` was set then this is obviously safe.
|
|
|
|
// If `self.u.Pointer` was set then this is the equivalent to converting
|
|
|
|
// the pointer to an integer, which is also safe.
|
|
|
|
// Currently the only safe way to construct `IO_STATUS_BLOCK` outside of
|
|
|
|
// this module is to call the `default` method, which sets the `Status`.
|
|
|
|
unsafe { self.u.Status }
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 02:47:36 +00:00
|
|
|
|
2022-04-04 19:37:11 +01:00
|
|
|
pub type LPOVERLAPPED_COMPLETION_ROUTINE = unsafe extern "system" fn(
|
2022-03-30 08:02:21 +01:00
|
|
|
dwErrorCode: DWORD,
|
2022-11-04 20:05:22 -07:00
|
|
|
dwNumberOfBytesTransferred: DWORD,
|
2022-03-30 08:02:21 +01:00
|
|
|
lpOverlapped: *mut OVERLAPPED,
|
|
|
|
);
|
|
|
|
|
2022-03-30 10:38:22 +01:00
|
|
|
type IO_APC_ROUTINE = unsafe extern "system" fn(
|
|
|
|
ApcContext: *mut c_void,
|
|
|
|
IoStatusBlock: *mut IO_STATUS_BLOCK,
|
|
|
|
Reserved: ULONG,
|
|
|
|
);
|
|
|
|
|
2014-06-08 21:55:17 +02:00
|
|
|
#[repr(C)]
|
2017-08-11 09:01:46 +08:00
|
|
|
#[cfg(not(target_pointer_width = "64"))]
|
2014-04-18 13:23:56 -07:00
|
|
|
pub struct WSADATA {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub wVersion: WORD,
|
|
|
|
pub wHighVersion: WORD,
|
2015-01-01 17:40:24 +13:00
|
|
|
pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
|
|
|
|
pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
|
2014-04-18 13:23:56 -07:00
|
|
|
pub iMaxSockets: u16,
|
|
|
|
pub iMaxUdpDg: u16,
|
2014-06-25 12:47:34 -07:00
|
|
|
pub lpVendorInfo: *mut u8,
|
2014-04-18 13:23:56 -07:00
|
|
|
}
|
2014-08-07 04:05:00 -04:00
|
|
|
#[repr(C)]
|
2017-08-11 09:01:46 +08:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2014-08-07 04:05:00 -04:00
|
|
|
pub struct WSADATA {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub wVersion: WORD,
|
|
|
|
pub wHighVersion: WORD,
|
2014-08-07 04:05:00 -04:00
|
|
|
pub iMaxSockets: u16,
|
|
|
|
pub iMaxUdpDg: u16,
|
|
|
|
pub lpVendorInfo: *mut u8,
|
2015-01-01 17:40:24 +13:00
|
|
|
pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
|
|
|
|
pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
|
2014-08-07 04:05:00 -04:00
|
|
|
}
|
2014-04-18 13:23:56 -07:00
|
|
|
|
2020-02-23 18:18:45 +01:00
|
|
|
#[derive(Copy, Clone)]
|
2019-02-08 20:42:34 +01:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct WSABUF {
|
|
|
|
pub len: ULONG,
|
|
|
|
pub buf: *mut CHAR,
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:29:51 -08:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct WSAPROTOCOL_INFO {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub dwServiceFlags1: DWORD,
|
|
|
|
pub dwServiceFlags2: DWORD,
|
|
|
|
pub dwServiceFlags3: DWORD,
|
|
|
|
pub dwServiceFlags4: DWORD,
|
|
|
|
pub dwProviderFlags: DWORD,
|
2015-02-11 15:29:51 -08:00
|
|
|
pub ProviderId: GUID,
|
2015-11-02 16:23:22 -08:00
|
|
|
pub dwCatalogEntryId: DWORD,
|
2015-02-11 15:29:51 -08:00
|
|
|
pub ProtocolChain: WSAPROTOCOLCHAIN,
|
2015-11-02 16:23:22 -08:00
|
|
|
pub iVersion: c_int,
|
|
|
|
pub iAddressFamily: c_int,
|
|
|
|
pub iMaxSockAddr: c_int,
|
|
|
|
pub iMinSockAddr: c_int,
|
|
|
|
pub iSocketType: c_int,
|
|
|
|
pub iProtocol: c_int,
|
|
|
|
pub iProtocolMaxOffset: c_int,
|
|
|
|
pub iNetworkByteOrder: c_int,
|
|
|
|
pub iSecurityScheme: c_int,
|
|
|
|
pub dwMessageSize: DWORD,
|
|
|
|
pub dwProviderReserved: DWORD,
|
2015-02-17 09:47:49 -05:00
|
|
|
pub szProtocol: [u16; (WSAPROTOCOL_LEN as usize) + 1],
|
2015-02-11 15:29:51 -08:00
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
#[repr(C)]
|
2015-11-02 16:23:22 -08:00
|
|
|
#[derive(Copy, Clone)]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub struct WIN32_FILE_ATTRIBUTE_DATA {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub dwFileAttributes: DWORD,
|
|
|
|
pub ftCreationTime: FILETIME,
|
|
|
|
pub ftLastAccessTime: FILETIME,
|
|
|
|
pub ftLastWriteTime: FILETIME,
|
|
|
|
pub nFileSizeHigh: DWORD,
|
|
|
|
pub nFileSizeLow: DWORD,
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2016-02-20 08:18:02 +01:00
|
|
|
#[allow(dead_code)] // we only use some variants
|
2015-01-27 12:20:58 -08:00
|
|
|
pub enum FILE_INFO_BY_HANDLE_CLASS {
|
|
|
|
FileBasicInfo = 0,
|
|
|
|
FileStandardInfo = 1,
|
|
|
|
FileNameInfo = 2,
|
|
|
|
FileRenameInfo = 3,
|
|
|
|
FileDispositionInfo = 4,
|
|
|
|
FileAllocationInfo = 5,
|
|
|
|
FileEndOfFileInfo = 6,
|
|
|
|
FileStreamInfo = 7,
|
|
|
|
FileCompressionInfo = 8,
|
|
|
|
FileAttributeTagInfo = 9,
|
|
|
|
FileIdBothDirectoryInfo = 10, // 0xA
|
|
|
|
FileIdBothDirectoryRestartInfo = 11, // 0xB
|
|
|
|
FileIoPriorityHintInfo = 12, // 0xC
|
|
|
|
FileRemoteProtocolInfo = 13, // 0xD
|
|
|
|
FileFullDirectoryInfo = 14, // 0xE
|
|
|
|
FileFullDirectoryRestartInfo = 15, // 0xF
|
|
|
|
FileStorageInfo = 16, // 0x10
|
|
|
|
FileAlignmentInfo = 17, // 0x11
|
|
|
|
FileIdInfo = 18, // 0x12
|
|
|
|
FileIdExtdDirectoryInfo = 19, // 0x13
|
|
|
|
FileIdExtdDirectoryRestartInfo = 20, // 0x14
|
2022-01-06 02:47:36 +00:00
|
|
|
FileDispositionInfoEx = 21, // 0x15, Windows 10 version 1607
|
2015-01-27 12:20:58 -08:00
|
|
|
MaximumFileInfoByHandlesClass,
|
|
|
|
}
|
|
|
|
|
2022-09-01 04:17:36 +01:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct FILE_ATTRIBUTE_TAG_INFO {
|
|
|
|
pub FileAttributes: DWORD,
|
|
|
|
pub ReparseTag: DWORD,
|
|
|
|
}
|
|
|
|
|
2022-01-06 02:47:36 +00:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct FILE_DISPOSITION_INFO {
|
|
|
|
pub DeleteFile: BOOLEAN,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const FILE_DISPOSITION_DELETE: DWORD = 0x1;
|
|
|
|
pub const FILE_DISPOSITION_POSIX_SEMANTICS: DWORD = 0x2;
|
|
|
|
pub const FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE: DWORD = 0x10;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct FILE_DISPOSITION_INFO_EX {
|
|
|
|
pub Flags: DWORD,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct FILE_ID_BOTH_DIR_INFO {
|
|
|
|
pub NextEntryOffset: DWORD,
|
|
|
|
pub FileIndex: DWORD,
|
|
|
|
pub CreationTime: LARGE_INTEGER,
|
|
|
|
pub LastAccessTime: LARGE_INTEGER,
|
|
|
|
pub LastWriteTime: LARGE_INTEGER,
|
|
|
|
pub ChangeTime: LARGE_INTEGER,
|
|
|
|
pub EndOfFile: LARGE_INTEGER,
|
|
|
|
pub AllocationSize: LARGE_INTEGER,
|
|
|
|
pub FileAttributes: DWORD,
|
|
|
|
pub FileNameLength: DWORD,
|
|
|
|
pub EaSize: DWORD,
|
|
|
|
pub ShortNameLength: CCHAR,
|
|
|
|
pub ShortName: [WCHAR; 12],
|
|
|
|
pub FileId: LARGE_INTEGER,
|
|
|
|
pub FileName: [WCHAR; 1],
|
|
|
|
}
|
2016-11-19 09:53:54 -08:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct FILE_BASIC_INFO {
|
|
|
|
pub CreationTime: LARGE_INTEGER,
|
|
|
|
pub LastAccessTime: LARGE_INTEGER,
|
|
|
|
pub LastWriteTime: LARGE_INTEGER,
|
|
|
|
pub ChangeTime: LARGE_INTEGER,
|
|
|
|
pub FileAttributes: DWORD,
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct FILE_END_OF_FILE_INFO {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub EndOfFile: LARGE_INTEGER,
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:36:01 -07:00
|
|
|
/// NB: Use carefully! In general using this as a reference is likely to get the
|
|
|
|
/// provenance wrong for the `rest` field!
|
2015-04-08 09:50:50 -07:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct REPARSE_DATA_BUFFER {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub ReparseTag: c_uint,
|
|
|
|
pub ReparseDataLength: c_ushort,
|
|
|
|
pub Reserved: c_ushort,
|
2015-04-08 09:50:50 -07:00
|
|
|
pub rest: (),
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:36:01 -07:00
|
|
|
/// NB: Use carefully! In general using this as a reference is likely to get the
|
|
|
|
/// provenance wrong for the `PathBuffer` field!
|
2015-04-08 09:50:50 -07:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct SYMBOLIC_LINK_REPARSE_BUFFER {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub SubstituteNameOffset: c_ushort,
|
|
|
|
pub SubstituteNameLength: c_ushort,
|
|
|
|
pub PrintNameOffset: c_ushort,
|
|
|
|
pub PrintNameLength: c_ushort,
|
|
|
|
pub Flags: c_ulong,
|
|
|
|
pub PathBuffer: WCHAR,
|
2015-04-08 09:50:50 -07:00
|
|
|
}
|
|
|
|
|
2016-02-13 20:44:37 +01:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct MOUNT_POINT_REPARSE_BUFFER {
|
|
|
|
pub SubstituteNameOffset: c_ushort,
|
|
|
|
pub SubstituteNameLength: c_ushort,
|
|
|
|
pub PrintNameOffset: c_ushort,
|
|
|
|
pub PrintNameLength: c_ushort,
|
|
|
|
pub PathBuffer: WCHAR,
|
|
|
|
}
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
pub type LPPROGRESS_ROUTINE = crate::option::Option<
|
|
|
|
unsafe extern "system" fn(
|
2015-11-02 16:23:22 -08:00
|
|
|
TotalFileSize: LARGE_INTEGER,
|
|
|
|
TotalBytesTransferred: LARGE_INTEGER,
|
|
|
|
StreamSize: LARGE_INTEGER,
|
|
|
|
StreamBytesTransferred: LARGE_INTEGER,
|
2015-07-10 04:54:00 -04:00
|
|
|
dwStreamNumber: DWORD,
|
|
|
|
dwCallbackReason: DWORD,
|
|
|
|
hSourceFile: HANDLE,
|
|
|
|
hDestinationFile: HANDLE,
|
|
|
|
lpData: LPVOID,
|
|
|
|
) -> DWORD,
|
|
|
|
>;
|
2015-06-26 09:30:35 -07:00
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct CONDITION_VARIABLE {
|
|
|
|
pub ptr: LPVOID,
|
|
|
|
}
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct SRWLOCK {
|
|
|
|
pub ptr: LPVOID,
|
|
|
|
}
|
2022-10-08 20:19:21 +02:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct INIT_ONCE {
|
|
|
|
pub ptr: LPVOID,
|
|
|
|
}
|
2015-06-26 09:30:35 -07:00
|
|
|
|
2015-07-09 10:45:40 -07:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct REPARSE_MOUNTPOINT_DATA_BUFFER {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub ReparseTag: DWORD,
|
|
|
|
pub ReparseDataLength: DWORD,
|
|
|
|
pub Reserved: WORD,
|
|
|
|
pub ReparseTargetLength: WORD,
|
|
|
|
pub ReparseTargetMaximumLength: WORD,
|
|
|
|
pub Reserved1: WORD,
|
|
|
|
pub ReparseTarget: WCHAR,
|
2015-07-09 10:45:40 -07:00
|
|
|
}
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct GUID {
|
|
|
|
pub Data1: DWORD,
|
|
|
|
pub Data2: WORD,
|
|
|
|
pub Data3: WORD,
|
|
|
|
pub Data4: [BYTE; 8],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct WSAPROTOCOLCHAIN {
|
|
|
|
pub ChainLen: c_int,
|
|
|
|
pub ChainEntries: [DWORD; MAX_PROTOCOL_CHAIN as usize],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct SECURITY_ATTRIBUTES {
|
|
|
|
pub nLength: DWORD,
|
|
|
|
pub lpSecurityDescriptor: LPVOID,
|
|
|
|
pub bInheritHandle: BOOL,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct PROCESS_INFORMATION {
|
|
|
|
pub hProcess: HANDLE,
|
|
|
|
pub hThread: HANDLE,
|
|
|
|
pub dwProcessId: DWORD,
|
|
|
|
pub dwThreadId: DWORD,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct STARTUPINFO {
|
|
|
|
pub cb: DWORD,
|
|
|
|
pub lpReserved: LPWSTR,
|
|
|
|
pub lpDesktop: LPWSTR,
|
|
|
|
pub lpTitle: LPWSTR,
|
|
|
|
pub dwX: DWORD,
|
|
|
|
pub dwY: DWORD,
|
|
|
|
pub dwXSize: DWORD,
|
|
|
|
pub dwYSize: DWORD,
|
|
|
|
pub dwXCountChars: DWORD,
|
|
|
|
pub dwYCountCharts: DWORD,
|
|
|
|
pub dwFillAttribute: DWORD,
|
|
|
|
pub dwFlags: DWORD,
|
|
|
|
pub wShowWindow: WORD,
|
|
|
|
pub cbReserved2: WORD,
|
|
|
|
pub lpReserved2: LPBYTE,
|
|
|
|
pub hStdInput: HANDLE,
|
|
|
|
pub hStdOutput: HANDLE,
|
|
|
|
pub hStdError: HANDLE,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct SOCKADDR {
|
|
|
|
pub sa_family: ADDRESS_FAMILY,
|
|
|
|
pub sa_data: [CHAR; 14],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2022-06-18 20:00:21 -07:00
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
2015-11-02 16:23:22 -08:00
|
|
|
pub struct FILETIME {
|
|
|
|
pub dwLowDateTime: DWORD,
|
|
|
|
pub dwHighDateTime: DWORD,
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:40:14 +02:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct SYSTEM_INFO {
|
|
|
|
pub wProcessorArchitecture: WORD,
|
|
|
|
pub wReserved: WORD,
|
|
|
|
pub dwPageSize: DWORD,
|
|
|
|
pub lpMinimumApplicationAddress: LPVOID,
|
|
|
|
pub lpMaximumApplicationAddress: LPVOID,
|
|
|
|
pub dwActiveProcessorMask: DWORD_PTR,
|
|
|
|
pub dwNumberOfProcessors: DWORD,
|
|
|
|
pub dwProcessorType: DWORD,
|
|
|
|
pub dwAllocationGranularity: DWORD,
|
|
|
|
pub wProcessorLevel: WORD,
|
|
|
|
pub wProcessorRevision: WORD,
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct OVERLAPPED {
|
|
|
|
pub Internal: *mut c_ulong,
|
|
|
|
pub InternalHigh: *mut c_ulong,
|
|
|
|
pub Offset: DWORD,
|
|
|
|
pub OffsetHigh: DWORD,
|
|
|
|
pub hEvent: HANDLE,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2016-02-20 08:18:02 +01:00
|
|
|
#[allow(dead_code)] // we only use some variants
|
2015-11-02 16:23:22 -08:00
|
|
|
pub enum ADDRESS_MODE {
|
|
|
|
AddrMode1616,
|
|
|
|
AddrMode1632,
|
|
|
|
AddrModeReal,
|
|
|
|
AddrModeFlat,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct SOCKADDR_STORAGE_LH {
|
|
|
|
pub ss_family: ADDRESS_FAMILY,
|
|
|
|
pub __ss_pad1: [CHAR; 6],
|
|
|
|
pub __ss_align: i64,
|
|
|
|
pub __ss_pad2: [CHAR; 112],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct ADDRINFOA {
|
|
|
|
pub ai_flags: c_int,
|
|
|
|
pub ai_family: c_int,
|
|
|
|
pub ai_socktype: c_int,
|
|
|
|
pub ai_protocol: c_int,
|
|
|
|
pub ai_addrlen: size_t,
|
|
|
|
pub ai_canonname: *mut c_char,
|
|
|
|
pub ai_addr: *mut SOCKADDR,
|
|
|
|
pub ai_next: *mut ADDRINFOA,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct sockaddr_in {
|
|
|
|
pub sin_family: ADDRESS_FAMILY,
|
|
|
|
pub sin_port: USHORT,
|
|
|
|
pub sin_addr: in_addr,
|
|
|
|
pub sin_zero: [CHAR; 8],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct sockaddr_in6 {
|
|
|
|
pub sin6_family: ADDRESS_FAMILY,
|
|
|
|
pub sin6_port: USHORT,
|
|
|
|
pub sin6_flowinfo: c_ulong,
|
|
|
|
pub sin6_addr: in6_addr,
|
|
|
|
pub sin6_scope_id: c_ulong,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct in_addr {
|
|
|
|
pub s_addr: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct in6_addr {
|
|
|
|
pub s6_addr: [u8; 16],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
2016-02-20 08:18:02 +01:00
|
|
|
#[allow(dead_code)] // we only use some variants
|
2015-11-02 16:23:22 -08:00
|
|
|
pub enum EXCEPTION_DISPOSITION {
|
|
|
|
ExceptionContinueExecution,
|
|
|
|
ExceptionContinueSearch,
|
|
|
|
ExceptionNestedException,
|
|
|
|
ExceptionCollidedUnwind,
|
|
|
|
}
|
|
|
|
|
2017-07-04 23:46:24 -07:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy)]
|
|
|
|
pub struct fd_set {
|
|
|
|
pub fd_count: c_uint,
|
|
|
|
pub fd_array: [SOCKET; FD_SETSIZE],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for fd_set {
|
|
|
|
fn clone(&self) -> fd_set {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct timeval {
|
|
|
|
pub tv_sec: c_long,
|
|
|
|
pub tv_usec: c_long,
|
|
|
|
}
|
|
|
|
|
2022-10-27 16:49:59 +01:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct CONSOLE_READCONSOLE_CONTROL {
|
|
|
|
pub nLength: ULONG,
|
|
|
|
pub nInitialChars: ULONG,
|
|
|
|
pub dwCtrlWakeupMask: ULONG,
|
|
|
|
pub dwControlKeyState: ULONG,
|
|
|
|
}
|
|
|
|
pub type PCONSOLE_READCONSOLE_CONTROL = *mut CONSOLE_READCONSOLE_CONTROL;
|
|
|
|
|
2021-05-06 04:04:19 +01:00
|
|
|
// Desktop specific functions & types
|
2019-05-27 16:41:52 +02:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(not(target_vendor = "uwp"))] {
|
2019-05-27 17:16:20 +02:00
|
|
|
pub const EXCEPTION_CONTINUE_SEARCH: LONG = 0;
|
|
|
|
pub const EXCEPTION_STACK_OVERFLOW: DWORD = 0xc00000fd;
|
|
|
|
pub const EXCEPTION_MAXIMUM_PARAMETERS: usize = 15;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct EXCEPTION_RECORD {
|
|
|
|
pub ExceptionCode: DWORD,
|
|
|
|
pub ExceptionFlags: DWORD,
|
|
|
|
pub ExceptionRecord: *mut EXCEPTION_RECORD,
|
|
|
|
pub ExceptionAddress: LPVOID,
|
|
|
|
pub NumberParameters: DWORD,
|
2021-05-06 04:04:19 +01:00
|
|
|
pub ExceptionInformation: [LPVOID; EXCEPTION_MAXIMUM_PARAMETERS],
|
2019-05-27 17:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum CONTEXT {}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct EXCEPTION_POINTERS {
|
|
|
|
pub ExceptionRecord: *mut EXCEPTION_RECORD,
|
|
|
|
pub ContextRecord: *mut CONTEXT,
|
|
|
|
}
|
|
|
|
|
2021-05-06 04:04:19 +01:00
|
|
|
pub type PVECTORED_EXCEPTION_HANDLER =
|
|
|
|
extern "system" fn(ExceptionInfo: *mut EXCEPTION_POINTERS) -> LONG;
|
2019-05-27 17:16:20 +02:00
|
|
|
|
2019-05-27 17:13:04 +02:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct BY_HANDLE_FILE_INFORMATION {
|
|
|
|
pub dwFileAttributes: DWORD,
|
|
|
|
pub ftCreationTime: FILETIME,
|
|
|
|
pub ftLastAccessTime: FILETIME,
|
|
|
|
pub ftLastWriteTime: FILETIME,
|
|
|
|
pub dwVolumeSerialNumber: DWORD,
|
|
|
|
pub nFileSizeHigh: DWORD,
|
|
|
|
pub nFileSizeLow: DWORD,
|
|
|
|
pub nNumberOfLinks: DWORD,
|
|
|
|
pub nFileIndexHigh: DWORD,
|
|
|
|
pub nFileIndexLow: DWORD,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type LPBY_HANDLE_FILE_INFORMATION = *mut BY_HANDLE_FILE_INFORMATION;
|
|
|
|
|
2019-05-27 16:51:29 +02:00
|
|
|
pub const HANDLE_FLAG_INHERIT: DWORD = 0x00000001;
|
|
|
|
|
2019-05-27 17:07:33 +02:00
|
|
|
pub const TOKEN_READ: DWORD = 0x20008;
|
|
|
|
|
2021-05-06 04:04:19 +01:00
|
|
|
#[link(name = "advapi32")]
|
2019-05-27 16:41:52 +02:00
|
|
|
extern "system" {
|
2023-02-14 19:32:32 +00:00
|
|
|
// Forbidden when targeting UWP
|
|
|
|
#[link_name = "SystemFunction036"]
|
|
|
|
pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;
|
|
|
|
|
2021-05-06 04:04:19 +01:00
|
|
|
// Allowed but unused by UWP
|
|
|
|
pub fn OpenProcessToken(
|
|
|
|
ProcessHandle: HANDLE,
|
|
|
|
DesiredAccess: DWORD,
|
|
|
|
TokenHandle: *mut HANDLE,
|
|
|
|
) -> BOOL;
|
|
|
|
}
|
2019-05-27 17:15:27 +02:00
|
|
|
|
2021-05-06 04:04:19 +01:00
|
|
|
#[link(name = "userenv")]
|
|
|
|
extern "system" {
|
|
|
|
// Allowed but unused by UWP
|
|
|
|
pub fn GetUserProfileDirectoryW(
|
|
|
|
hToken: HANDLE,
|
|
|
|
lpProfileDir: LPWSTR,
|
|
|
|
lpcchSize: *mut DWORD,
|
|
|
|
) -> BOOL;
|
|
|
|
}
|
2019-05-27 17:15:27 +02:00
|
|
|
|
2021-05-06 04:04:19 +01:00
|
|
|
#[link(name = "kernel32")]
|
|
|
|
extern "system" {
|
2019-05-27 17:07:33 +02:00
|
|
|
// Allowed but unused by UWP
|
2021-05-06 04:04:19 +01:00
|
|
|
pub fn GetFileInformationByHandle(
|
|
|
|
hFile: HANDLE,
|
|
|
|
lpFileInformation: LPBY_HANDLE_FILE_INFORMATION,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) -> BOOL;
|
|
|
|
pub fn AddVectoredExceptionHandler(
|
|
|
|
FirstHandler: ULONG,
|
|
|
|
VectoredHandler: PVECTORED_EXCEPTION_HANDLER,
|
|
|
|
) -> LPVOID;
|
|
|
|
pub fn CreateHardLinkW(
|
|
|
|
lpSymlinkFileName: LPCWSTR,
|
|
|
|
lpTargetFileName: LPCWSTR,
|
|
|
|
lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL;
|
2021-10-30 12:20:50 +01:00
|
|
|
pub fn GetWindowsDirectoryW(lpBuffer: LPWSTR, uSize: UINT) -> UINT;
|
2019-05-27 16:41:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UWP specific functions & types
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(target_vendor = "uwp")] {
|
2019-05-27 17:13:04 +02:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct FILE_STANDARD_INFO {
|
|
|
|
pub AllocationSize: LARGE_INTEGER,
|
|
|
|
pub EndOfFile: LARGE_INTEGER,
|
2019-07-31 10:56:56 +07:00
|
|
|
pub NumberOfLinks: DWORD,
|
2019-05-27 17:13:04 +02:00
|
|
|
pub DeletePending: BOOLEAN,
|
|
|
|
pub Directory: BOOLEAN,
|
|
|
|
}
|
2019-05-27 16:41:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shared between Desktop & UWP
|
2021-05-06 04:04:19 +01:00
|
|
|
|
2021-04-30 16:24:49 +01:00
|
|
|
#[link(name = "kernel32")]
|
2014-04-18 13:23:56 -07:00
|
|
|
extern "system" {
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn GetCurrentProcessId() -> DWORD;
|
2014-05-07 11:06:15 -07:00
|
|
|
|
2022-10-27 16:49:59 +01:00
|
|
|
pub fn ReadConsoleW(
|
|
|
|
hConsoleInput: HANDLE,
|
|
|
|
lpBuffer: LPVOID,
|
|
|
|
nNumberOfCharsToRead: DWORD,
|
|
|
|
lpNumberOfCharsRead: LPDWORD,
|
|
|
|
pInputControl: PCONSOLE_READCONSOLE_CONTROL,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn WriteConsoleW(
|
|
|
|
hConsoleOutput: HANDLE,
|
|
|
|
lpBuffer: LPCVOID,
|
|
|
|
nNumberOfCharsToWrite: DWORD,
|
|
|
|
lpNumberOfCharsWritten: LPDWORD,
|
|
|
|
lpReserved: LPVOID,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
|
|
|
|
|
2021-10-30 12:20:50 +01:00
|
|
|
pub fn GetSystemDirectoryW(lpBuffer: LPWSTR, uSize: UINT) -> UINT;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn RemoveDirectoryW(lpPathName: LPCWSTR) -> BOOL;
|
|
|
|
pub fn SetFileAttributesW(lpFileName: LPCWSTR, dwFileAttributes: DWORD) -> BOOL;
|
2022-06-18 20:00:21 -07:00
|
|
|
pub fn SetFileTime(
|
|
|
|
hFile: BorrowedHandle<'_>,
|
|
|
|
lpCreationTime: Option<&FILETIME>,
|
|
|
|
lpLastAccessTime: Option<&FILETIME>,
|
|
|
|
lpLastWriteTime: Option<&FILETIME>,
|
|
|
|
) -> BOOL;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn SetLastError(dwErrCode: DWORD);
|
2021-08-06 00:15:22 +01:00
|
|
|
pub fn GetCommandLineW() -> LPWSTR;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn GetTempPathW(nBufferLength: DWORD, lpBuffer: LPCWSTR) -> DWORD;
|
|
|
|
pub fn GetCurrentProcess() -> HANDLE;
|
|
|
|
pub fn GetCurrentThread() -> HANDLE;
|
|
|
|
pub fn GetStdHandle(which: DWORD) -> HANDLE;
|
|
|
|
pub fn ExitProcess(uExitCode: c_uint) -> !;
|
|
|
|
pub fn DeviceIoControl(
|
|
|
|
hDevice: HANDLE,
|
|
|
|
dwIoControlCode: DWORD,
|
|
|
|
lpInBuffer: LPVOID,
|
|
|
|
nInBufferSize: DWORD,
|
|
|
|
lpOutBuffer: LPVOID,
|
|
|
|
nOutBufferSize: DWORD,
|
|
|
|
lpBytesReturned: LPDWORD,
|
|
|
|
lpOverlapped: LPOVERLAPPED,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn CreateThread(
|
|
|
|
lpThreadAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
dwStackSize: SIZE_T,
|
|
|
|
lpStartAddress: extern "system" fn(*mut c_void) -> DWORD,
|
|
|
|
lpParameter: LPVOID,
|
|
|
|
dwCreationFlags: DWORD,
|
|
|
|
lpThreadId: LPDWORD,
|
2022-03-03 11:07:23 -08:00
|
|
|
) -> HandleOrNull;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
|
|
|
|
pub fn SwitchToThread() -> BOOL;
|
|
|
|
pub fn Sleep(dwMilliseconds: DWORD);
|
2022-03-30 08:02:21 +01:00
|
|
|
pub fn SleepEx(dwMilliseconds: DWORD, bAlertable: BOOL) -> DWORD;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn GetProcessId(handle: HANDLE) -> DWORD;
|
|
|
|
pub fn CopyFileExW(
|
|
|
|
lpExistingFileName: LPCWSTR,
|
|
|
|
lpNewFileName: LPCWSTR,
|
2015-07-10 04:54:00 -04:00
|
|
|
lpProgressRoutine: LPPROGRESS_ROUTINE,
|
2015-11-02 16:23:22 -08:00
|
|
|
lpData: LPVOID,
|
2015-07-10 04:54:00 -04:00
|
|
|
pbCancel: LPBOOL,
|
2015-11-02 16:23:22 -08:00
|
|
|
dwCopyFlags: DWORD,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn FormatMessageW(
|
|
|
|
flags: DWORD,
|
|
|
|
lpSrc: LPVOID,
|
|
|
|
msgId: DWORD,
|
|
|
|
langId: DWORD,
|
|
|
|
buf: LPWSTR,
|
|
|
|
nsize: DWORD,
|
|
|
|
args: *const c_void,
|
|
|
|
) -> DWORD;
|
|
|
|
pub fn TlsAlloc() -> DWORD;
|
|
|
|
pub fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
|
|
|
|
pub fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
|
2022-10-08 20:19:21 +02:00
|
|
|
pub fn TlsFree(dwTlsIndex: DWORD) -> BOOL;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn GetLastError() -> DWORD;
|
|
|
|
pub fn QueryPerformanceFrequency(lpFrequency: *mut LARGE_INTEGER) -> BOOL;
|
|
|
|
pub fn QueryPerformanceCounter(lpPerformanceCount: *mut LARGE_INTEGER) -> BOOL;
|
|
|
|
pub fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: LPDWORD) -> BOOL;
|
|
|
|
pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) -> BOOL;
|
|
|
|
pub fn CreateProcessW(
|
|
|
|
lpApplicationName: LPCWSTR,
|
|
|
|
lpCommandLine: LPWSTR,
|
|
|
|
lpProcessAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
lpThreadAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
bInheritHandles: BOOL,
|
|
|
|
dwCreationFlags: DWORD,
|
|
|
|
lpEnvironment: LPVOID,
|
|
|
|
lpCurrentDirectory: LPCWSTR,
|
|
|
|
lpStartupInfo: LPSTARTUPINFO,
|
|
|
|
lpProcessInformation: LPPROCESS_INFORMATION,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn GetEnvironmentVariableW(n: LPCWSTR, v: LPWSTR, nsize: DWORD) -> DWORD;
|
|
|
|
pub fn SetEnvironmentVariableW(n: LPCWSTR, v: LPCWSTR) -> BOOL;
|
|
|
|
pub fn GetEnvironmentStringsW() -> LPWCH;
|
|
|
|
pub fn FreeEnvironmentStringsW(env_ptr: LPWCH) -> BOOL;
|
|
|
|
pub fn GetModuleFileNameW(hModule: HMODULE, lpFilename: LPWSTR, nSize: DWORD) -> DWORD;
|
|
|
|
pub fn CreateDirectoryW(
|
|
|
|
lpPathName: LPCWSTR,
|
|
|
|
lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn DeleteFileW(lpPathName: LPCWSTR) -> BOOL;
|
|
|
|
pub fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: LPWSTR) -> DWORD;
|
|
|
|
pub fn SetCurrentDirectoryW(lpPathName: LPCWSTR) -> BOOL;
|
|
|
|
pub fn DuplicateHandle(
|
|
|
|
hSourceProcessHandle: HANDLE,
|
|
|
|
hSourceHandle: HANDLE,
|
|
|
|
hTargetProcessHandle: HANDLE,
|
|
|
|
lpTargetHandle: LPHANDLE,
|
|
|
|
dwDesiredAccess: DWORD,
|
|
|
|
bInheritHandle: BOOL,
|
|
|
|
dwOptions: DWORD,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn ReadFile(
|
2022-03-03 11:07:23 -08:00
|
|
|
hFile: BorrowedHandle<'_>,
|
2015-11-02 16:23:22 -08:00
|
|
|
lpBuffer: LPVOID,
|
|
|
|
nNumberOfBytesToRead: DWORD,
|
|
|
|
lpNumberOfBytesRead: LPDWORD,
|
|
|
|
lpOverlapped: LPOVERLAPPED,
|
|
|
|
) -> BOOL;
|
2022-03-30 08:02:21 +01:00
|
|
|
pub fn ReadFileEx(
|
|
|
|
hFile: BorrowedHandle<'_>,
|
|
|
|
lpBuffer: LPVOID,
|
|
|
|
nNumberOfBytesToRead: DWORD,
|
|
|
|
lpOverlapped: LPOVERLAPPED,
|
|
|
|
lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn WriteFileEx(
|
|
|
|
hFile: BorrowedHandle<'_>,
|
|
|
|
lpBuffer: LPVOID,
|
|
|
|
nNumberOfBytesToWrite: DWORD,
|
|
|
|
lpOverlapped: LPOVERLAPPED,
|
|
|
|
lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE,
|
|
|
|
) -> BOOL;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn CloseHandle(hObject: HANDLE) -> BOOL;
|
|
|
|
pub fn MoveFileExW(lpExistingFileName: LPCWSTR, lpNewFileName: LPCWSTR, dwFlags: DWORD)
|
|
|
|
-> BOOL;
|
|
|
|
pub fn SetFilePointerEx(
|
|
|
|
hFile: HANDLE,
|
|
|
|
liDistanceToMove: LARGE_INTEGER,
|
|
|
|
lpNewFilePointer: PLARGE_INTEGER,
|
|
|
|
dwMoveMethod: DWORD,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn FlushFileBuffers(hFile: HANDLE) -> BOOL;
|
|
|
|
pub fn CreateFileW(
|
|
|
|
lpFileName: LPCWSTR,
|
|
|
|
dwDesiredAccess: DWORD,
|
|
|
|
dwShareMode: DWORD,
|
|
|
|
lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
dwCreationDisposition: DWORD,
|
|
|
|
dwFlagsAndAttributes: DWORD,
|
|
|
|
hTemplateFile: HANDLE,
|
2022-03-03 11:07:23 -08:00
|
|
|
) -> HandleOrInvalid;
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn FindFirstFileW(fileName: LPCWSTR, findFileData: LPWIN32_FIND_DATAW) -> HANDLE;
|
|
|
|
pub fn FindNextFileW(findFile: HANDLE, findFileData: LPWIN32_FIND_DATAW) -> BOOL;
|
|
|
|
pub fn FindClose(findFile: HANDLE) -> BOOL;
|
|
|
|
|
|
|
|
pub fn GetProcAddress(handle: HMODULE, name: LPCSTR) -> *mut c_void;
|
Resolve DLL imports at CRT startup, not on demand
On Windows, libstd uses GetProcAddress to locate some DLL imports, so
that libstd can run on older versions of Windows. If a given DLL import
is not present, then libstd uses other behavior (such as fallback
implementations).
This commit uses a feature of the Windows CRT to do these DLL imports
during module initialization, before main() (or DllMain()) is called.
This is the ideal time to resolve imports, because the module is
effectively single-threaded at that point; no other threads can
touch the data or code of the module that is being initialized.
This avoids several problems. First, it makes the cost of performing
the DLL import lookups deterministic. Right now, the DLL imports are
done on demand, which means that application threads _might_ have to
do the DLL import during some time-sensitive operation. This is a
small source of unpredictability. Since threads can race, it's even
possible to have more than one thread running the same redundant
DLL lookup.
This commit also removes using the heap to allocate strings, during
the DLL lookups.
2021-01-27 12:16:03 -08:00
|
|
|
pub fn GetModuleHandleA(lpModuleName: LPCSTR) -> HMODULE;
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
|
|
|
|
|
2015-11-16 17:36:14 -08:00
|
|
|
pub fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: LPFILETIME);
|
2021-04-29 11:40:14 +02:00
|
|
|
pub fn GetSystemInfo(lpSystemInfo: LPSYSTEM_INFO);
|
2016-02-12 10:29:25 -08:00
|
|
|
|
|
|
|
pub fn CreateEventW(
|
|
|
|
lpEventAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
bManualReset: BOOL,
|
|
|
|
bInitialState: BOOL,
|
|
|
|
lpName: LPCWSTR,
|
|
|
|
) -> HANDLE;
|
|
|
|
pub fn WaitForMultipleObjects(
|
|
|
|
nCount: DWORD,
|
|
|
|
lpHandles: *const HANDLE,
|
|
|
|
bWaitAll: BOOL,
|
|
|
|
dwMilliseconds: DWORD,
|
|
|
|
) -> DWORD;
|
|
|
|
pub fn CreateNamedPipeW(
|
|
|
|
lpName: LPCWSTR,
|
|
|
|
dwOpenMode: DWORD,
|
|
|
|
dwPipeMode: DWORD,
|
|
|
|
nMaxInstances: DWORD,
|
|
|
|
nOutBufferSize: DWORD,
|
|
|
|
nInBufferSize: DWORD,
|
|
|
|
nDefaultTimeOut: DWORD,
|
|
|
|
lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
) -> HANDLE;
|
|
|
|
pub fn CancelIo(handle: HANDLE) -> BOOL;
|
|
|
|
pub fn GetOverlappedResult(
|
|
|
|
hFile: HANDLE,
|
|
|
|
lpOverlapped: LPOVERLAPPED,
|
|
|
|
lpNumberOfBytesTransferred: LPDWORD,
|
|
|
|
bWait: BOOL,
|
|
|
|
) -> BOOL;
|
2021-01-21 12:13:35 -08:00
|
|
|
pub fn CreateSymbolicLinkW(
|
|
|
|
lpSymlinkFileName: LPCWSTR,
|
|
|
|
lpTargetFileName: LPCWSTR,
|
|
|
|
dwFlags: DWORD,
|
|
|
|
) -> BOOLEAN;
|
|
|
|
pub fn GetFinalPathNameByHandleW(
|
|
|
|
hFile: HANDLE,
|
|
|
|
lpszFilePath: LPCWSTR,
|
|
|
|
cchFilePath: DWORD,
|
|
|
|
dwFlags: DWORD,
|
|
|
|
) -> DWORD;
|
2022-01-06 02:47:36 +00:00
|
|
|
pub fn GetFileInformationByHandleEx(
|
|
|
|
hFile: HANDLE,
|
|
|
|
fileInfoClass: FILE_INFO_BY_HANDLE_CLASS,
|
|
|
|
lpFileInformation: LPVOID,
|
|
|
|
dwBufferSize: DWORD,
|
|
|
|
) -> BOOL;
|
2021-01-21 12:13:35 -08:00
|
|
|
pub fn SetFileInformationByHandle(
|
|
|
|
hFile: HANDLE,
|
|
|
|
FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
|
|
|
|
lpFileInformation: LPVOID,
|
|
|
|
dwBufferSize: DWORD,
|
|
|
|
) -> BOOL;
|
2022-10-21 17:57:22 +01:00
|
|
|
pub fn GetFileType(hfile: HANDLE) -> DWORD;
|
2021-01-21 12:13:35 -08:00
|
|
|
pub fn SleepConditionVariableSRW(
|
|
|
|
ConditionVariable: PCONDITION_VARIABLE,
|
|
|
|
SRWLock: PSRWLOCK,
|
|
|
|
dwMilliseconds: DWORD,
|
|
|
|
Flags: ULONG,
|
|
|
|
) -> BOOL;
|
|
|
|
|
|
|
|
pub fn WakeConditionVariable(ConditionVariable: PCONDITION_VARIABLE);
|
|
|
|
pub fn WakeAllConditionVariable(ConditionVariable: PCONDITION_VARIABLE);
|
|
|
|
|
|
|
|
pub fn AcquireSRWLockExclusive(SRWLock: PSRWLOCK);
|
|
|
|
pub fn AcquireSRWLockShared(SRWLock: PSRWLOCK);
|
|
|
|
pub fn ReleaseSRWLockExclusive(SRWLock: PSRWLOCK);
|
|
|
|
pub fn ReleaseSRWLockShared(SRWLock: PSRWLOCK);
|
|
|
|
pub fn TryAcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> BOOLEAN;
|
|
|
|
pub fn TryAcquireSRWLockShared(SRWLock: PSRWLOCK) -> BOOLEAN;
|
2021-05-19 23:34:15 +01:00
|
|
|
|
2022-10-08 20:19:21 +02:00
|
|
|
pub fn InitOnceBeginInitialize(
|
|
|
|
lpInitOnce: LPINIT_ONCE,
|
|
|
|
dwFlags: DWORD,
|
|
|
|
fPending: LPBOOL,
|
|
|
|
lpContext: *mut LPVOID,
|
|
|
|
) -> BOOL;
|
|
|
|
pub fn InitOnceComplete(lpInitOnce: LPINIT_ONCE, dwFlags: DWORD, lpContext: LPVOID) -> BOOL;
|
|
|
|
|
2021-05-19 23:34:15 +01:00
|
|
|
pub fn CompareStringOrdinal(
|
|
|
|
lpString1: LPCWSTR,
|
|
|
|
cchCount1: c_int,
|
|
|
|
lpString2: LPCWSTR,
|
|
|
|
cchCount2: c_int,
|
|
|
|
bIgnoreCase: BOOL,
|
|
|
|
) -> c_int;
|
2021-09-22 14:11:40 +01:00
|
|
|
pub fn GetFullPathNameW(
|
|
|
|
lpFileName: LPCWSTR,
|
|
|
|
nBufferLength: DWORD,
|
|
|
|
lpBuffer: LPWSTR,
|
|
|
|
lpFilePart: *mut LPWSTR,
|
|
|
|
) -> DWORD;
|
2022-02-14 12:20:40 +00:00
|
|
|
pub fn GetFileAttributesW(lpFileName: LPCWSTR) -> DWORD;
|
2023-01-19 23:21:21 -08:00
|
|
|
|
|
|
|
pub fn MultiByteToWideChar(
|
|
|
|
CodePage: UINT,
|
|
|
|
dwFlags: DWORD,
|
|
|
|
lpMultiByteStr: LPCCH,
|
|
|
|
cbMultiByte: c_int,
|
|
|
|
lpWideCharStr: LPWSTR,
|
|
|
|
cchWideChar: c_int,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn WideCharToMultiByte(
|
|
|
|
CodePage: UINT,
|
|
|
|
dwFlags: DWORD,
|
|
|
|
lpWideCharStr: LPCWCH,
|
|
|
|
cchWideChar: c_int,
|
|
|
|
lpMultiByteStr: LPSTR,
|
|
|
|
cbMultiByte: c_int,
|
|
|
|
lpDefaultChar: LPCCH,
|
|
|
|
lpUsedDefaultChar: LPBOOL,
|
|
|
|
) -> c_int;
|
2014-07-04 10:45:28 -04:00
|
|
|
}
|
2015-06-26 09:30:35 -07:00
|
|
|
|
2021-05-06 04:04:19 +01:00
|
|
|
#[link(name = "ws2_32")]
|
|
|
|
extern "system" {
|
|
|
|
pub fn WSAStartup(wVersionRequested: WORD, lpWSAData: LPWSADATA) -> c_int;
|
|
|
|
pub fn WSACleanup() -> c_int;
|
|
|
|
pub fn WSAGetLastError() -> c_int;
|
|
|
|
pub fn WSADuplicateSocketW(
|
|
|
|
s: SOCKET,
|
|
|
|
dwProcessId: DWORD,
|
|
|
|
lpProtocolInfo: LPWSAPROTOCOL_INFO,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn WSASend(
|
|
|
|
s: SOCKET,
|
|
|
|
lpBuffers: LPWSABUF,
|
|
|
|
dwBufferCount: DWORD,
|
|
|
|
lpNumberOfBytesSent: LPDWORD,
|
|
|
|
dwFlags: DWORD,
|
|
|
|
lpOverlapped: LPWSAOVERLAPPED,
|
|
|
|
lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn WSARecv(
|
|
|
|
s: SOCKET,
|
|
|
|
lpBuffers: LPWSABUF,
|
|
|
|
dwBufferCount: DWORD,
|
|
|
|
lpNumberOfBytesRecvd: LPDWORD,
|
|
|
|
lpFlags: LPDWORD,
|
|
|
|
lpOverlapped: LPWSAOVERLAPPED,
|
|
|
|
lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn WSASocketW(
|
|
|
|
af: c_int,
|
|
|
|
kind: c_int,
|
|
|
|
protocol: c_int,
|
|
|
|
lpProtocolInfo: LPWSAPROTOCOL_INFO,
|
|
|
|
g: GROUP,
|
|
|
|
dwFlags: DWORD,
|
|
|
|
) -> SOCKET;
|
|
|
|
pub fn ioctlsocket(s: SOCKET, cmd: c_long, argp: *mut c_ulong) -> c_int;
|
|
|
|
pub fn closesocket(socket: SOCKET) -> c_int;
|
|
|
|
pub fn recv(socket: SOCKET, buf: *mut c_void, len: c_int, flags: c_int) -> c_int;
|
|
|
|
pub fn send(socket: SOCKET, buf: *const c_void, len: c_int, flags: c_int) -> c_int;
|
|
|
|
pub fn recvfrom(
|
|
|
|
socket: SOCKET,
|
|
|
|
buf: *mut c_void,
|
|
|
|
len: c_int,
|
|
|
|
flags: c_int,
|
|
|
|
addr: *mut SOCKADDR,
|
|
|
|
addrlen: *mut c_int,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn sendto(
|
|
|
|
socket: SOCKET,
|
|
|
|
buf: *const c_void,
|
|
|
|
len: c_int,
|
|
|
|
flags: c_int,
|
|
|
|
addr: *const SOCKADDR,
|
|
|
|
addrlen: c_int,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn shutdown(socket: SOCKET, how: c_int) -> c_int;
|
|
|
|
pub fn accept(socket: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET;
|
|
|
|
pub fn getsockopt(
|
|
|
|
s: SOCKET,
|
|
|
|
level: c_int,
|
|
|
|
optname: c_int,
|
|
|
|
optval: *mut c_char,
|
|
|
|
optlen: *mut c_int,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn setsockopt(
|
|
|
|
s: SOCKET,
|
|
|
|
level: c_int,
|
|
|
|
optname: c_int,
|
|
|
|
optval: *const c_void,
|
|
|
|
optlen: c_int,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn getsockname(socket: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> c_int;
|
|
|
|
pub fn getpeername(socket: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> c_int;
|
|
|
|
pub fn bind(socket: SOCKET, address: *const SOCKADDR, address_len: socklen_t) -> c_int;
|
|
|
|
pub fn listen(socket: SOCKET, backlog: c_int) -> c_int;
|
|
|
|
pub fn connect(socket: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int;
|
|
|
|
pub fn getaddrinfo(
|
|
|
|
node: *const c_char,
|
|
|
|
service: *const c_char,
|
|
|
|
hints: *const ADDRINFOA,
|
|
|
|
res: *mut *mut ADDRINFOA,
|
|
|
|
) -> c_int;
|
|
|
|
pub fn freeaddrinfo(res: *mut ADDRINFOA);
|
|
|
|
pub fn select(
|
|
|
|
nfds: c_int,
|
|
|
|
readfds: *mut fd_set,
|
|
|
|
writefds: *mut fd_set,
|
|
|
|
exceptfds: *mut fd_set,
|
|
|
|
timeout: *const timeval,
|
|
|
|
) -> c_int;
|
|
|
|
}
|
|
|
|
|
2021-04-11 14:55:47 +02:00
|
|
|
#[link(name = "bcrypt")]
|
|
|
|
extern "system" {
|
|
|
|
// >= Vista / Server 2008
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
|
|
|
|
pub fn BCryptGenRandom(
|
2022-09-06 10:49:34 +01:00
|
|
|
hAlgorithm: BCRYPT_ALG_HANDLE,
|
2021-04-11 14:55:47 +02:00
|
|
|
pBuffer: *mut u8,
|
|
|
|
cbBuffer: ULONG,
|
|
|
|
dwFlags: ULONG,
|
|
|
|
) -> NTSTATUS;
|
|
|
|
}
|
|
|
|
|
2023-02-20 09:45:55 +00:00
|
|
|
#[link(name = "ntdll")]
|
|
|
|
extern "system" {
|
|
|
|
pub fn NtCreateFile(
|
|
|
|
FileHandle: *mut HANDLE,
|
|
|
|
DesiredAccess: ACCESS_MASK,
|
|
|
|
ObjectAttributes: *const OBJECT_ATTRIBUTES,
|
|
|
|
IoStatusBlock: *mut IO_STATUS_BLOCK,
|
|
|
|
AllocationSize: *mut i64,
|
|
|
|
FileAttributes: ULONG,
|
|
|
|
ShareAccess: ULONG,
|
|
|
|
CreateDisposition: ULONG,
|
|
|
|
CreateOptions: ULONG,
|
|
|
|
EaBuffer: *mut c_void,
|
|
|
|
EaLength: ULONG,
|
|
|
|
) -> NTSTATUS;
|
|
|
|
pub fn NtReadFile(
|
|
|
|
FileHandle: BorrowedHandle<'_>,
|
|
|
|
Event: HANDLE,
|
|
|
|
ApcRoutine: Option<IO_APC_ROUTINE>,
|
|
|
|
ApcContext: *mut c_void,
|
|
|
|
IoStatusBlock: &mut IO_STATUS_BLOCK,
|
|
|
|
Buffer: *mut crate::mem::MaybeUninit<u8>,
|
|
|
|
Length: ULONG,
|
|
|
|
ByteOffset: Option<&LARGE_INTEGER>,
|
|
|
|
Key: Option<&ULONG>,
|
|
|
|
) -> NTSTATUS;
|
|
|
|
pub fn NtWriteFile(
|
|
|
|
FileHandle: BorrowedHandle<'_>,
|
|
|
|
Event: HANDLE,
|
|
|
|
ApcRoutine: Option<IO_APC_ROUTINE>,
|
|
|
|
ApcContext: *mut c_void,
|
|
|
|
IoStatusBlock: &mut IO_STATUS_BLOCK,
|
|
|
|
Buffer: *const u8,
|
|
|
|
Length: ULONG,
|
|
|
|
ByteOffset: Option<&LARGE_INTEGER>,
|
|
|
|
Key: Option<&ULONG>,
|
|
|
|
) -> NTSTATUS;
|
|
|
|
pub fn RtlNtStatusToDosError(Status: NTSTATUS) -> ULONG;
|
|
|
|
}
|
|
|
|
|
2017-09-06 20:40:34 +01:00
|
|
|
// Functions that aren't available on every version of Windows that we support,
|
|
|
|
// but we still use them and just provide some form of a fallback implementation.
|
2022-07-21 11:37:03 +01:00
|
|
|
compat_fn_with_fallback! {
|
|
|
|
pub static KERNEL32: &CStr = ansi_str!("kernel32");
|
2015-06-26 09:30:35 -07:00
|
|
|
|
2021-01-21 12:13:35 -08:00
|
|
|
// >= Win10 1607
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
|
2017-09-06 20:40:34 +01:00
|
|
|
pub fn SetThreadDescription(hThread: HANDLE,
|
|
|
|
lpThreadDescription: LPCWSTR) -> HRESULT {
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); E_NOTIMPL
|
|
|
|
}
|
2021-01-21 12:13:35 -08:00
|
|
|
|
|
|
|
// >= Win8 / Server 2012
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
|
2020-03-09 17:40:31 +02:00
|
|
|
pub fn GetSystemTimePreciseAsFileTime(lpSystemTimeAsFileTime: LPFILETIME)
|
|
|
|
-> () {
|
|
|
|
GetSystemTimeAsFileTime(lpSystemTimeAsFileTime)
|
|
|
|
}
|
2021-10-16 18:23:42 -10:00
|
|
|
|
2021-10-26 17:49:55 -07:00
|
|
|
// >= Win11 / Server 2022
|
2021-10-16 18:23:42 -10:00
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a
|
|
|
|
pub fn GetTempPath2W(nBufferLength: DWORD, lpBuffer: LPCWSTR) -> DWORD {
|
|
|
|
GetTempPathW(nBufferLength, lpBuffer)
|
|
|
|
}
|
2015-06-26 09:30:35 -07:00
|
|
|
}
|
2021-01-21 12:13:35 -08:00
|
|
|
|
2022-08-18 07:34:25 +01:00
|
|
|
compat_fn_optional! {
|
|
|
|
crate::sys::compat::load_synch_functions();
|
|
|
|
pub fn WaitOnAddress(
|
|
|
|
Address: LPVOID,
|
|
|
|
CompareAddress: LPVOID,
|
|
|
|
AddressSize: SIZE_T,
|
|
|
|
dwMilliseconds: DWORD
|
|
|
|
);
|
|
|
|
pub fn WakeByAddressSingle(Address: LPVOID);
|
2020-10-06 00:34:15 +02:00
|
|
|
}
|
2020-10-06 00:34:15 +02:00
|
|
|
|
2022-07-21 11:37:03 +01:00
|
|
|
compat_fn_with_fallback! {
|
|
|
|
pub static NTDLL: &CStr = ansi_str!("ntdll");
|
|
|
|
|
2020-10-06 00:34:15 +02:00
|
|
|
pub fn NtCreateKeyedEvent(
|
|
|
|
KeyedEventHandle: LPHANDLE,
|
|
|
|
DesiredAccess: ACCESS_MASK,
|
|
|
|
ObjectAttributes: LPVOID,
|
|
|
|
Flags: ULONG
|
|
|
|
) -> NTSTATUS {
|
|
|
|
panic!("keyed events not available")
|
|
|
|
}
|
|
|
|
pub fn NtReleaseKeyedEvent(
|
|
|
|
EventHandle: HANDLE,
|
|
|
|
Key: LPVOID,
|
|
|
|
Alertable: BOOLEAN,
|
|
|
|
Timeout: PLARGE_INTEGER
|
|
|
|
) -> NTSTATUS {
|
|
|
|
panic!("keyed events not available")
|
|
|
|
}
|
|
|
|
pub fn NtWaitForKeyedEvent(
|
|
|
|
EventHandle: HANDLE,
|
|
|
|
Key: LPVOID,
|
|
|
|
Alertable: BOOLEAN,
|
|
|
|
Timeout: PLARGE_INTEGER
|
|
|
|
) -> NTSTATUS {
|
|
|
|
panic!("keyed events not available")
|
|
|
|
}
|
|
|
|
}
|