Files
DarkflameServer/dCommon/Process.h
Aaron Kimbrell 4936e8103a fix(master): servers master starts no longer inherit its sockets
The child closes every descriptor above 2 before exec, so master's UDP
ports are released when master stops. Process::Run closes them too.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 20:38:15 -06:00

32 lines
1.5 KiB
C++

#pragma once
#include <string>
#include <string_view>
#include <vector>
// Run external programs without a shell: arguments are passed as a vector, so nothing in them is interpreted
// ($(...), backticks, ;, quotes, redirections all stay literal).
namespace Process {
/**
* Run a program and wait for it.
* @param arguments The program (looked up on PATH when it has no slash) followed by its arguments
* @param stdoutFile Where the program's standard output goes; empty discards it
* @param stderrFile Where the program's error output goes; empty discards it
* @return The program's exit code, or -1 when it could not be started or was killed by a signal
*/
int Run(const std::vector<std::string>& arguments, const std::string& stdoutFile = "", const std::string& stderrFile = "");
// Whether text contains characters a shell uses to run or chain commands (quotes, $, `, ;, |, &, <, >, newlines).
// Parentheses, spaces and backslashes are allowed: they are common in real folder names.
// Used to refuse suspicious paths even though Run never uses a shell.
bool HasShellMetacharacters(std::string_view text);
/**
* For a forked child just before exec: closes every file descriptor from `first` up, so the program it starts
* does not keep the parent's sockets (a listening port stays held after the parent stops otherwise).
* Only calls close_range or close, so it is safe between fork and exec. Does nothing on Windows, where
* CreateProcess is called without handle inheritance.
*/
void CloseInheritedFds(int first = 3);
}