Skip to content

Conversation

@MacroModel
Copy link
Collaborator

No description provided.

- Updated `get` function signatures in `tuple.h` to return `decltype(auto)` for improved type deduction.
- Enhanced consistency by ensuring `[[nodiscard]]` attribute is applied uniformly across all overloads of `get`.
- Added new overloads for `get` to support retrieval by type, improving usability and flexibility.
- Minor formatting adjustments for better readability in the `forward_as_tuple` function.
- Introduced `apply` function to facilitate the application of a callable to elements of a tuple.
- Added `tuple_size` function to retrieve the size of a `fast_io::containers::tuple`.
- Implemented helper functions in the `details` namespace for improved code organization and clarity.
…rm headers

- Updated error handling in various platform headers to consistently use `throw_posix_error()` with appropriate error codes.
- Enhanced function signatures in POSIX and platform-specific implementations for clarity and consistency.
- Replaced direct calls to system functions with `noexcept_call` for safer error management.
- Improved readability and maintainability of the code by standardizing error checks and function calls.
…in POSIX headers

- Updated `thread_start_routine` in both NT and Win32 headers to include `FAST_IO_WINSTDCALL` for better calling convention compatibility.
- Enhanced error handling in `get_module_install_path_from_argv0` by checking for a null environment path and throwing an appropriate error.
- Improved readability by initializing pointers to empty and using consistent formatting in the `argv0.h` file.
- Adjusted conditional compilation checks in `posix.h` for clearer handling of open mode flags.
…tion

- Updated `get` function signatures to return `auto&&` instead of `decltype(auto)` for better type handling.
- Enhanced consistency across all overloads of `get` by applying uniform return types.
- Minor adjustments made to improve readability and maintainability of the code.
…tion functions

- Added error checks for null pointers and buffer overflows in `get_module_install_path_from_argv0` to ensure robust path resolution.
- Updated `get_module_install_path` to handle `sysctl` errors more accurately.
- Replaced direct system calls with `noexcept_call` for safer error management across various platform headers.
- Improved readability and maintainability by standardizing error handling and function calls.
- Updated function signatures in `android.h` to include `noexcept` for improved safety and performance.
- Refactored operator overloads and logging implementations to ensure exception safety during logging operations.
- Improved code clarity and maintainability by standardizing function signatures across the Android logging interface.
…m headers

- Added null pointer check in `is_invalid_dos_filename_with_size` to prevent crashes.
- Refactored `zero_copy_transmit64_define` to streamline character transmission logic and improve performance.
- Updated `try_lock` method in `posix_file_lock` to return a boolean indicating lock success.
- Adjusted function signatures in various headers for consistency and clarity, including changes to `NtSetSystemTime` and `RtlAcquirePebLock`.
- Improved error handling in `posix_seek_impl` to throw appropriate errors for overflow conditions.
… and function signatures

- Updated `sys_mmap` to use `long` for return type and improved error handling for memory mapping.
- Refactored `open_socket_impl` to include checks for socket open modes and set appropriate flags for non-blocking and close-on-exec behavior.
- Introduced `to_win32_page_protect` function for better handling of file map attributes in Win32.
- Enhanced error handling in `create_file_mapping_impl` and random number generation functions to account for API behavior as per MSDN documentation.
…ype safety

- Changed the type of `tms` from `uint_least64_t` to `int_least64_t` to ensure proper handling of negative values.
- Updated calculations for `tms` to maintain consistency and prevent potential overflow issues.
…e consistency

- Adjusted function signatures in `msvc_linker_32_i686.h`, `msvc_linker_32.h`, `msvc_linker_64.h`, and `msvc_linker_arm64ec.h` to ensure proper handling of parameters and return types.
- Enhanced consistency across linker headers by standardizing the naming conventions for function aliases.
- Improved clarity and maintainability of the code by ensuring uniformity in the handling of alternate names for Windows API functions.
…s to return references

- Updated `append` methods in `basic_win32_process_args`, `basic_win32_process_envs`, and `posix_process_args` to return a reference to the current object, enhancing method chaining capabilities.
- Improved code clarity and consistency across process argument and environment handling.
…ength calculation

- Updated the resizing logic in `posix.h` to correctly account for the null terminator when determining the length of the string.
- Added a trailing slash to the returned path string to ensure proper formatting when appending filenames.
- Introduced a new `posix_vfork` option in `option.h` to support process creation using vfork, which has specific limitations on parameter modifications.
- Refactored process execution methods in `posix.h` to utilize a common `fork_execveat_impl` function, streamlining the handling of process creation based on the selected mode.
- Enhanced clarity and maintainability of the code by consolidating vfork and pipefork logic into unified implementations.
…intainability

- Added conditional compilation for session management in the vfork_and_execveat function, enhancing the handling of process creation modes.
- Improved code organization by encapsulating session management logic within preprocessor directives, allowing for easier modifications in the future.
…ocess mode

- Modified the vfork_and_execveat function signature to add [[maybe_unused]] for the process_mode parameter, improving code clarity and indicating that the parameter may not be utilized in all contexts.
- Changed the process_mode enumeration to a scoped enum class for better type safety and clarity.
- Added a new 'follow' option to the process_mode enum to allow symbolic links to be followed during process execution.
- Updated the posix_execveat function to accept the process_mode parameter, modifying behavior based on the follow option.
- Refactored flags handling in posix_execveat and vfork_and_execveat functions to improve clarity and maintainability.
- Introduced a new flag in process_mode to control the appending of paths to argv0, enhancing process creation flexibility.
- Updated the nt_6x_process_create_impl and nt_create_process_overloads functions to utilize the new argv0_no_path_append flag for improved argument handling.
- Refactored open_mode handling in process creation functions to accommodate the follow option, ensuring consistent behavior across different modes.
- Enhanced clarity and maintainability of the code by organizing process parameter initialization and handling logic.
…ation marks around paths

- Updated the process creation logic to include double quotation marks around the image path and process parameters when appending to argv0.
- Improved clarity in the handling of DOS and NT paths by ensuring proper formatting during argument construction.
- Updated the `cstr_guard` return logic to enhance readability by removing unnecessary return statements.
- Introduced a new namespace `posix` to encapsulate platform-specific environment variable handling, including `_NSGetEnviron` for Darwin and `environ` for other platforms.
- Streamlined the `get_envs` function to utilize the new `posix` namespace, improving code organization and maintainability.
…ent structure

- Changed the initialization of process arguments from a string to a native__process_args structure for improved clarity and consistency in argument handling.
- This update enhances the readability of the code and aligns with recent refactoring efforts in process management.
… handling

- Moved platform-specific environment variable declarations into the posix namespace for better organization and clarity.
- Updated references in the get_envs function to utilize the new namespace, enhancing code maintainability and readability.
- Removed redundant declarations to streamline the code structure.
MacroModel and others added 5 commits November 9, 2025 17:13
- Changed the initialization of process arguments in process.cc to use native_process_args for improved clarity.
- Updated constructor signatures in nt.h to remove default arguments for process_args_with_argv0, enhancing consistency and readability across process creation functions.
- Introduced a new `args_with_argv0_t` structure to encapsulate argument handling with `argv0`.
- Updated constructors in `nt.h` and `posix.h` to accept `args_with_argv0_t`, enhancing consistency in process creation across platforms.
- Improved clarity in argument handling by ensuring all relevant constructors utilize the new structure, streamlining the process initialization logic.
…rdize argument handling

- Updated constructors across nt.h, posix.h, and win32.h to use a consistent default for `process_mode`, specifically `argv0_no_path_append`.
- Enhanced clarity in argument handling by ensuring all relevant constructors utilize the new default, streamlining process initialization logic.
- Improved maintainability and readability of the code by aligning constructor signatures across different platforms.
- Standardized the usage of `char_literal_v` in the argument construction process by removing unnecessary spaces in template syntax.
- Enhanced readability and maintainability of the code by ensuring consistent formatting across argument handling in the `win32_winnt_process_create_from_handle_imp` function.
- Updated the `throw_win32_error` function call to remove the status argument for improved clarity in error handling.
- Modified the `win32_duplicate_object_std` function to use a boolean for the `inherit` parameter, enhancing readability and consistency in handle duplication logic.
@MacroModel
Copy link
Collaborator Author

我先在代码库里定位并理解 win32_process_iothrow_win32_error 以及相关 Win32 API 封装的定义,以核对此 win32.h 文件对 Win32/MSDN 的使用是否正确。随后我会并行检索多处关键词来构建完整上下文。

[1 tool called]

结论

  • 该库自带的 include/fast_io_hosted/process/process/win32.h 并非微软系统头(MSDN 里没有 win32.h),而是库的 Win32 进程封装实现文件;整体对 Win32 API 的使用与 MSDN 一致,关键常量与调用约定正确。
  • 主要建议:在使用 CreateProcess* + STARTF_USESTDHANDLES 时,确保传给子进程的标准句柄本身“可继承”;如目标平台为 Vista+,可升级为 STARTUPINFOEX + PROC_THREAD_ATTRIBUTE_HANDLE_LIST 以避免多余句柄被继承。

与 MSDN 对照的关键点(节选)

  • 等待/错误处理:使用 WaitForSingleObject(INFINITE) 等待,失败返回 WAIT_FAILED(0xFFFFFFFF) 并通过 GetLastError 取错。代码已在关闭句柄前保存错误码,符合 MSDN 关于“调用其他 API 可能覆盖 LastError”的提醒。
inline ::std::uint_least32_t win32_wait_user_process_or_thread(void *hprocess_thread) noexcept
{
	return ::fast_io::win32::WaitForSingleObject(hprocess_thread, 0xFFFFFFFF);
}
auto const status{win32::details::win32_wait_user_process_or_thread(ppob.hnt_user_process_info.hprocess)};
if (status == 0xFFFFFFFF) [[unlikely]]
{
	if constexpr (throw_eh)
	{
		throw_win32_error();
	}
	else
	{
		return {static_cast<::std::uint_least32_t>(-1)};
	}
}

// get exit code
::std::uint_least32_t exit_code{};
if (!::fast_io::win32::GetExitCodeProcess(ppob.hnt_user_process_info.hprocess, __builtin_addressof(exit_code)))
{
	throw_win32_error();
}
  • CreateProcess* 与标准句柄:设置了 STARTF_USESTDHANDLES(0x00000100),并将 bInheritHandles=TRUE,符合 MSDN 使用要求;同时清除了父进程“管道另一端”的可继承位,避免不必要继承。
::fast_io::win32::startupinfow si{};
si.cb = sizeof(si);
// ... 省略:为 si.hStdInput/Output/Error 赋值 ...
si.dwFlags = 0x00000100;

::std::uint_least32_t dwCreationFlags{0x00000400 /*CREATE_UNICODE_ENVIRONMENT*/};
// ... 省略:按需要 OR 上 CREATE_NEW_PROCESS_GROUP / CREATE_NEW_CONSOLE ...
::fast_io::win32::process_information pi{};
if (!::fast_io::win32::CreateProcessW(address_begin, const_cast<char16_t *>(actrual_args), nullptr, nullptr, 1,
									  dwCreationFlags, (void *)envs, nullptr, __builtin_addressof(si), __builtin_addressof(pi)))
{
	throw_win32_error();
}
  • NT 路径到 DOS 路径转换:通过 K32GetMappedFileName*/GetMappedFileName* 获取 \Device\... 前缀,再用 QueryDosDevice* 解析卷设备名或处理 \Device\Mup\ UNC 情况,逻辑符合惯例(MSDN 建议也可用 GetFinalPathNameByHandleW,见“改进建议”)。
#if !defined(_WIN32_WINNT) || _WIN32_WINNT >= 0x601
	::fast_io::win32::K32GetMappedFileNameW
#else
	::fast_io::win32::GetMappedFileNameW
#endif
	(reinterpret_cast<void *>(static_cast<::std::ptrdiff_t>(-1)),
	 pMem,
	 pszFilename,
	 0x2000)) [[unlikely]]
char16_t DosDevice[4]{0, u':', 0, 0};
char16_t NtPath[1025];
// ... 遍历 A:..Z: 并用 QueryDosDeviceW 比较前缀,命中则替换为盘符 ...

建议与潜在风险(基于 MSDN)

  • 确保子进程用到的标准句柄“可继承”:
    • 目前仅对“父进程不需要继承的管道另一端”执行了 SetHandleInformation(..., HANDLE_FLAG_INHERIT, 0) 清除继承位(见下段);若管道端初始未设为可继承,子进程可能拿不到有效句柄。建议在把句柄塞进 si.hStdInput/Output/Error 后,显式把该端标记为可继承(与 MSDN 样例一致)。
if (processio.in.win32_pipe_in_handle)
{
	si.hStdInput = processio.in.win32_pipe_in_handle;

	if (!::fast_io::win32::SetHandleInformation(processio.in.win32_pipe_out_handle, 0x00000001 /*HANDLE_FLAG_INHERIT*/, 0)) [[unlikely]]
	{
		throw_win32_error();
	}
}
  • 最小改动示例(在设置 si.hStd* 后补一行,确保该端可继承;若句柄是由外部传入且已经可继承,则不影响):
// 在设置 si.hStdInput/Output/Error 之后追加(示例以 hStdInput 为例)
if (si.hStdInput) {
	if (!::fast_io::win32::SetHandleInformation(si.hStdInput, 0x00000001 /*HANDLE_FLAG_INHERIT*/, 0x00000001 /*HANDLE_FLAG_INHERIT*/)) {
		throw_win32_error();
	}
}
  • Vista+ 的更稳妥方案:使用 STARTUPINFOEXPROC_THREAD_ATTRIBUTE_HANDLE_LIST 精确白名单控制继承句柄,避免 bInheritHandles=TRUE 带来非预期句柄继承(MSDN 推荐实践)。
  • 可读性建议:将字面量替换为命名常量(如 INFINITESTARTF_USESTDHANDLESCREATE_UNICODE_ENVIRONMENTDUPLICATE_SAME_ACCESSHANDLE_FLAG_INHERIT)。功能不变,方便对照 MSDN。
  • 获取映像路径的现代做法:若目标系统为 Vista+,可优先用 GetFinalPathNameByHandleW 简化 K32GetMappedFileName* + QueryDosDevice* 的路径转换,并在较老系统回退到当前实现。
  • 设计注意:win32_family_process 析构器会阻塞等待进程退出(见 close_win32_user_process_information_and_wait)。这在语义上更像“join”,符合某些使用场景,但可能导致误用时卡住线程;建议在文档中强调或提供非阻塞析构/显式 detach 的安全用法。

总体评价

  • 与 MSDN 的语义基本一致,资源管理(保存 GetLastError、先 waitGetExitCodeProcess、关闭顺序)正确。按上面的继承位与现代 API 建议做小幅调整,会更健壮、可读性更好、且更贴合微软推荐实践。

  • 我检查了 include/fast_io_hosted/process/process/win32.h 中上述位置,给出了插入位置与最小改动示例;如需,我可以按你的目标平台范围(例如 Vista+/Win7+)直接给出完整的 STARTUPINFOEX 版本编辑建议。

  • 另外提醒:MSDN 确实没有系统级 win32.h 头,本仓库此文件为库内部实现,命名上可能会与系统头认知混淆;若团队中有此困惑,可考虑在 README/注释中注明或调整文件名以避免歧义。

  • 若你希望,我可以把“确保标准句柄可继承”的改动具体插到 Wide/ANSI 两个分支的正确行号处并给出完整 edits。

@trcrsired
Copy link
Member

乱改一通

if constexpr (sizeof(::std::uintmax_t) > sizeof(off_t))
{
    if (offset > static_cast<::std::uintmax_t>(::std::numeric_limits<off_t>::max()))
    {
        throw_posix_error(EINVAL);
    }
}
long ret{system_call<__NR_mmap, long>(addr, len, prot, flags, fd, offset)};

为何这里要改long. fast_io禁止使用long能理解么?

@trcrsired
Copy link
Member

fast_io不允许使用int long之类

@MacroModel
Copy link
Collaborator Author

@trcrsired fixed

MacroModel and others added 3 commits November 10, 2025 15:01
…sistency

- Simplified the namespace structure for `fast_io::win32::nt`, improving organization.
- Updated thread-related functions and constructors to use `inline constexpr` for better performance and clarity.
- Enhanced error handling in sleep functions to ensure proper validation of input parameters.
- Streamlined the usage of `nt_thread` and `this_thread` to improve readability and maintainability across the codebase.
- Updated `RtlCreateUserThread` signature to accept a function pointer for improved flexibility.
- Refined error handling in thread creation and management functions to ensure proper resource cleanup on failure.
- Standardized the use of `inline constexpr` for thread-related functions to enhance performance and clarity.
- Improved sleep functions to validate input parameters and handle edge cases more effectively.
- Streamlined namespace usage and organization for better code maintainability.
@trcrsired
Copy link
Member

CI failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants