2012-09-18 23:51:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Process Thread Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2014-12-02 10:53:58 +03:00
|
|
|
* Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2012-09-18 23:51:33 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-09-18 23:51:33 +04:00
|
|
|
|
|
|
|
#include <winpr/handle.h>
|
2014-12-02 10:53:58 +03:00
|
|
|
#include "../handle/nonehandle.h"
|
2012-09-18 23:51:33 +04:00
|
|
|
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CreateProcessA
|
|
|
|
* CreateProcessW
|
|
|
|
* CreateProcessAsUserA
|
|
|
|
* CreateProcessAsUserW
|
|
|
|
* ExitProcess
|
|
|
|
* GetCurrentProcess
|
|
|
|
* GetCurrentProcessId
|
|
|
|
* GetExitCodeProcess
|
|
|
|
* GetProcessHandleCount
|
|
|
|
* GetProcessId
|
|
|
|
* GetProcessIdOfThread
|
|
|
|
* GetProcessMitigationPolicy
|
|
|
|
* GetProcessTimes
|
|
|
|
* GetProcessVersion
|
|
|
|
* OpenProcess
|
|
|
|
* OpenProcessToken
|
|
|
|
* ProcessIdToSessionId
|
|
|
|
* SetProcessAffinityUpdateMode
|
|
|
|
* SetProcessMitigationPolicy
|
|
|
|
* SetProcessShutdownParameters
|
|
|
|
* TerminateProcess
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2022-09-23 16:31:41 +03:00
|
|
|
#include <winpr/assert.h>
|
2013-09-23 01:23:00 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-09-24 19:03:42 +04:00
|
|
|
#include <winpr/path.h>
|
2013-09-23 22:44:59 +04:00
|
|
|
#include <winpr/environment.h>
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2013-09-24 21:08:29 +04:00
|
|
|
#include <grp.h>
|
|
|
|
|
2013-10-25 18:03:57 +04:00
|
|
|
#include <signal.h>
|
2022-09-23 16:31:41 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif /* __linux__ */
|
2012-09-18 23:51:33 +04:00
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
#include "thread.h"
|
|
|
|
|
2013-09-24 19:03:42 +04:00
|
|
|
#include "../security/security.h"
|
2012-09-18 23:51:33 +04:00
|
|
|
|
2015-07-02 12:21:09 +03:00
|
|
|
#ifndef NSIG
|
|
|
|
#ifdef SIGMAX
|
|
|
|
#define NSIG SIGMAX
|
|
|
|
#else
|
|
|
|
#define NSIG 64
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-09-24 19:03:42 +04:00
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* If the file name does not contain a directory path, the system searches for the executable file
|
|
|
|
* in the following sequence:
|
2013-09-24 19:03:42 +04:00
|
|
|
*
|
|
|
|
* 1) The directory from which the application loaded.
|
|
|
|
* 2) The current directory for the parent process.
|
2019-11-06 17:24:51 +03:00
|
|
|
* 3) The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of
|
|
|
|
* this directory. 4) The 16-bit Windows system directory. There is no function that obtains the
|
|
|
|
* path of this directory, but it is searched. The name of this directory is System. 5) The Windows
|
|
|
|
* directory. Use the GetWindowsDirectory function to get the path of this directory. 6) The
|
|
|
|
* directories that are listed in the PATH environment variable. Note that this function does not
|
|
|
|
* search the per-application path specified by the App Paths registry key. To include this
|
|
|
|
* per-application path in the search sequence, use the ShellExecute function.
|
2013-09-24 19:03:42 +04:00
|
|
|
*/
|
|
|
|
|
2015-10-23 19:17:14 +03:00
|
|
|
static char* FindApplicationPath(char* application)
|
2013-09-24 19:03:42 +04:00
|
|
|
{
|
2017-03-03 14:37:27 +03:00
|
|
|
LPCSTR pathName = "PATH";
|
2013-09-24 19:03:42 +04:00
|
|
|
char* path;
|
|
|
|
char* save;
|
|
|
|
DWORD nSize;
|
|
|
|
LPSTR lpSystemPath;
|
|
|
|
char* filename = NULL;
|
|
|
|
|
|
|
|
if (!application)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (application[0] == '/')
|
2013-10-31 13:43:59 +04:00
|
|
|
return _strdup(application);
|
2013-09-24 19:03:42 +04:00
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
nSize = GetEnvironmentVariableA(pathName, NULL, 0);
|
2013-09-24 19:03:42 +04:00
|
|
|
|
|
|
|
if (!nSize)
|
2015-08-28 11:31:22 +03:00
|
|
|
return _strdup(application);
|
2013-09-24 19:03:42 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
lpSystemPath = (LPSTR)malloc(nSize);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!lpSystemPath)
|
|
|
|
return NULL;
|
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
if (GetEnvironmentVariableA(pathName, lpSystemPath, nSize) != nSize - 1)
|
|
|
|
{
|
|
|
|
free(lpSystemPath);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-24 19:03:42 +04:00
|
|
|
|
|
|
|
save = NULL;
|
|
|
|
path = strtok_s(lpSystemPath, ":", &save);
|
|
|
|
|
|
|
|
while (path)
|
|
|
|
{
|
|
|
|
filename = GetCombinedPath(path, application);
|
|
|
|
|
2021-05-31 12:42:03 +03:00
|
|
|
if (winpr_PathFileExists(filename))
|
2013-09-24 19:03:42 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(filename);
|
|
|
|
filename = NULL;
|
|
|
|
path = strtok_s(NULL, ":", &save);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(lpSystemPath);
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2014-12-07 03:15:49 +03:00
|
|
|
static HANDLE CreateProcessHandle(pid_t pid);
|
|
|
|
static BOOL ProcessHandleCloseHandle(HANDLE handle);
|
2014-12-02 11:08:10 +03:00
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags, LPCSTR lpApplicationName,
|
|
|
|
LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
|
|
|
|
DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo,
|
|
|
|
LPPROCESS_INFORMATION lpProcessInformation)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2013-09-23 01:23:00 +04:00
|
|
|
pid_t pid;
|
|
|
|
int numArgs;
|
2013-11-21 23:06:21 +04:00
|
|
|
LPSTR* pArgs = NULL;
|
|
|
|
char** envp = NULL;
|
2013-10-31 13:19:55 +04:00
|
|
|
char* filename = NULL;
|
2014-12-02 10:53:58 +03:00
|
|
|
HANDLE thread;
|
2014-12-02 11:08:10 +03:00
|
|
|
HANDLE process;
|
2013-09-24 19:03:42 +04:00
|
|
|
WINPR_ACCESS_TOKEN* token;
|
2013-09-23 22:44:59 +04:00
|
|
|
LPTCH lpszEnvironmentBlock;
|
2013-11-21 23:06:21 +04:00
|
|
|
BOOL ret = FALSE;
|
2015-07-02 12:21:09 +03:00
|
|
|
sigset_t oldSigMask;
|
|
|
|
sigset_t newSigMask;
|
|
|
|
BOOL restoreSigMask = FALSE;
|
2013-09-23 01:23:00 +04:00
|
|
|
numArgs = 0;
|
2013-09-23 22:44:59 +04:00
|
|
|
lpszEnvironmentBlock = NULL;
|
2020-11-18 09:51:45 +03:00
|
|
|
/* https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
|
|
|
|
*/
|
|
|
|
if (lpCommandLine)
|
|
|
|
pArgs = CommandLineToArgvA(lpCommandLine, &numArgs);
|
|
|
|
else
|
|
|
|
pArgs = CommandLineToArgvA(lpApplicationName, &numArgs);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!pArgs)
|
|
|
|
return FALSE;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
token = (WINPR_ACCESS_TOKEN*)hToken;
|
2013-09-24 19:03:42 +04:00
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
if (lpEnvironment)
|
|
|
|
{
|
|
|
|
envp = EnvironmentBlockToEnvpA(lpEnvironment);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lpszEnvironmentBlock = GetEnvironmentStrings();
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-07-02 13:05:46 +03:00
|
|
|
if (!lpszEnvironmentBlock)
|
2015-04-07 15:38:09 +03:00
|
|
|
goto finish;
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
envp = EnvironmentBlockToEnvpA(lpszEnvironmentBlock);
|
|
|
|
}
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!envp)
|
|
|
|
goto finish;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2013-09-24 19:03:42 +04:00
|
|
|
filename = FindApplicationPath(pArgs[0]);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2013-11-21 23:06:21 +04:00
|
|
|
if (NULL == filename)
|
|
|
|
goto finish;
|
2013-09-24 19:03:42 +04:00
|
|
|
|
2015-07-02 12:21:09 +03:00
|
|
|
/* block all signals so that the child can safely reset the caller's handlers */
|
|
|
|
sigfillset(&newSigMask);
|
|
|
|
restoreSigMask = !pthread_sigmask(SIG_SETMASK, &newSigMask, &oldSigMask);
|
2013-09-24 21:08:29 +04:00
|
|
|
/* fork and exec */
|
|
|
|
pid = fork();
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2013-09-24 21:08:29 +04:00
|
|
|
if (pid < 0)
|
|
|
|
{
|
|
|
|
/* fork failure */
|
2013-11-21 23:06:21 +04:00
|
|
|
goto finish;
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
/* child process */
|
2015-07-02 12:21:09 +03:00
|
|
|
#ifndef __sun
|
|
|
|
int maxfd;
|
|
|
|
#endif
|
|
|
|
int fd;
|
|
|
|
int sig;
|
2022-10-13 17:25:36 +03:00
|
|
|
sigset_t set = { 0 };
|
|
|
|
struct sigaction act = { 0 };
|
2015-07-02 12:21:09 +03:00
|
|
|
/* set default signal handlers */
|
2015-07-02 13:25:48 +03:00
|
|
|
act.sa_handler = SIG_DFL;
|
2015-07-02 12:21:09 +03:00
|
|
|
act.sa_flags = 0;
|
|
|
|
sigemptyset(&act.sa_mask);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-07-02 12:21:09 +03:00
|
|
|
for (sig = 1; sig < NSIG; sig++)
|
|
|
|
sigaction(sig, &act, NULL);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-07-02 12:21:09 +03:00
|
|
|
/* unblock all signals */
|
|
|
|
sigfillset(&set);
|
|
|
|
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
|
|
|
|
|
2015-08-28 11:31:22 +03:00
|
|
|
if (lpStartupInfo)
|
|
|
|
{
|
|
|
|
int handle_fd;
|
|
|
|
handle_fd = winpr_Handle_getFd(lpStartupInfo->hStdOutput);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-08-28 11:31:22 +03:00
|
|
|
if (handle_fd != -1)
|
|
|
|
dup2(handle_fd, STDOUT_FILENO);
|
|
|
|
|
|
|
|
handle_fd = winpr_Handle_getFd(lpStartupInfo->hStdError);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-08-28 11:31:22 +03:00
|
|
|
if (handle_fd != -1)
|
|
|
|
dup2(handle_fd, STDERR_FILENO);
|
|
|
|
|
|
|
|
handle_fd = winpr_Handle_getFd(lpStartupInfo->hStdInput);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2015-08-28 11:31:22 +03:00
|
|
|
if (handle_fd != -1)
|
|
|
|
dup2(handle_fd, STDIN_FILENO);
|
|
|
|
}
|
|
|
|
|
2013-10-02 19:21:56 +04:00
|
|
|
#ifdef __sun
|
2015-08-28 11:31:22 +03:00
|
|
|
closefrom(3);
|
2013-10-02 19:21:56 +04:00
|
|
|
#else
|
|
|
|
#ifdef F_MAXFD // on some BSD derivates
|
2015-08-28 11:31:22 +03:00
|
|
|
maxfd = fcntl(0, F_MAXFD);
|
2013-10-02 19:21:56 +04:00
|
|
|
#else
|
2015-08-28 11:31:22 +03:00
|
|
|
maxfd = sysconf(_SC_OPEN_MAX);
|
2013-10-02 19:21:56 +04:00
|
|
|
#endif
|
2018-10-24 14:17:14 +03:00
|
|
|
|
|
|
|
for (fd = 3; fd < maxfd; fd++)
|
2015-08-28 11:31:22 +03:00
|
|
|
close(fd);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2013-10-02 19:21:56 +04:00
|
|
|
#endif // __sun
|
2013-09-24 21:08:29 +04:00
|
|
|
|
|
|
|
if (token)
|
|
|
|
{
|
|
|
|
if (token->GroupId)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
int rc = setgid((gid_t)token->GroupId);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2014-11-16 17:59:26 +03:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
initgroups(token->Username, (gid_t)token->GroupId);
|
2014-11-16 17:59:26 +03:00
|
|
|
}
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token->UserId)
|
2021-06-16 15:43:07 +03:00
|
|
|
{
|
|
|
|
int rc = setuid((uid_t)token->UserId);
|
|
|
|
if (rc != 0)
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:05:46 +03:00
|
|
|
/* TODO: add better cwd handling and error checking */
|
|
|
|
if (lpCurrentDirectory && strlen(lpCurrentDirectory) > 0)
|
2021-06-16 15:43:07 +03:00
|
|
|
{
|
|
|
|
int rc = chdir(lpCurrentDirectory);
|
|
|
|
if (rc != 0)
|
|
|
|
goto finish;
|
|
|
|
}
|
2015-07-02 13:05:46 +03:00
|
|
|
|
2013-09-24 21:08:29 +04:00
|
|
|
if (execve(filename, pArgs, envp) < 0)
|
|
|
|
{
|
2013-11-21 23:06:21 +04:00
|
|
|
/* execve failed - end the process */
|
|
|
|
_exit(1);
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* parent process */
|
|
|
|
}
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2014-12-02 11:08:10 +03:00
|
|
|
process = CreateProcessHandle(pid);
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
if (!process)
|
2013-11-21 23:06:21 +04:00
|
|
|
{
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2014-12-02 10:53:58 +03:00
|
|
|
thread = CreateNoneHandle();
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
if (!thread)
|
2013-11-21 23:06:21 +04:00
|
|
|
{
|
2014-12-07 03:04:30 +03:00
|
|
|
ProcessHandleCloseHandle(process);
|
2013-11-21 23:06:21 +04:00
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2014-12-02 11:08:10 +03:00
|
|
|
lpProcessInformation->hProcess = process;
|
2014-12-02 10:53:58 +03:00
|
|
|
lpProcessInformation->hThread = thread;
|
2019-11-06 17:24:51 +03:00
|
|
|
lpProcessInformation->dwProcessId = (DWORD)pid;
|
|
|
|
lpProcessInformation->dwThreadId = (DWORD)pid;
|
2013-11-21 23:06:21 +04:00
|
|
|
ret = TRUE;
|
|
|
|
finish:
|
2015-07-02 12:21:09 +03:00
|
|
|
|
|
|
|
/* restore caller's original signal mask */
|
|
|
|
if (restoreSigMask)
|
|
|
|
pthread_sigmask(SIG_SETMASK, &oldSigMask, NULL);
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(filename);
|
2021-12-20 15:29:48 +03:00
|
|
|
free(pArgs);
|
2013-09-24 19:03:42 +04:00
|
|
|
|
|
|
|
if (lpszEnvironmentBlock)
|
|
|
|
FreeEnvironmentStrings(lpszEnvironmentBlock);
|
|
|
|
|
|
|
|
if (envp)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (envp[i])
|
|
|
|
{
|
|
|
|
free(envp[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(envp);
|
|
|
|
}
|
|
|
|
|
2013-11-21 23:06:21 +04:00
|
|
|
return ret;
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
BOOL CreateProcessA(LPCSTR lpApplicationName, LPSTR lpCommandLine,
|
|
|
|
LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
2019-11-06 17:24:51 +03:00
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
|
|
|
|
DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
|
|
|
|
LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
2013-09-24 08:07:48 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return _CreateProcessExA(NULL, 0, lpApplicationName, lpCommandLine, lpProcessAttributes,
|
2018-10-24 14:17:14 +03:00
|
|
|
lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
|
|
|
|
lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
|
2013-09-24 08:07:48 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
BOOL CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
|
|
|
|
LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
2019-11-06 17:24:51 +03:00
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
|
|
|
|
DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
|
|
|
|
LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2022-09-28 17:07:04 +03:00
|
|
|
return FALSE;
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
BOOL CreateProcessAsUserA(HANDLE hToken, LPCSTR lpApplicationName, LPSTR lpCommandLine,
|
|
|
|
LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
2019-11-06 17:24:51 +03:00
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
|
|
|
|
DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
|
|
|
|
LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return _CreateProcessExA(hToken, 0, lpApplicationName, lpCommandLine, lpProcessAttributes,
|
2018-10-24 14:17:14 +03:00
|
|
|
lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
|
|
|
|
lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
BOOL CreateProcessAsUserW(HANDLE hToken, LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
|
|
|
|
LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
2019-11-06 17:24:51 +03:00
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
|
|
|
|
DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
|
|
|
|
LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2022-09-28 17:07:04 +03:00
|
|
|
return FALSE;
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
BOOL CreateProcessWithLogonA(LPCSTR lpUsername, LPCSTR lpDomain, LPCSTR lpPassword,
|
2019-11-06 17:24:51 +03:00
|
|
|
DWORD dwLogonFlags, LPCSTR lpApplicationName, LPSTR lpCommandLine,
|
|
|
|
DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
|
|
|
|
LPSTARTUPINFOA lpStartupInfo,
|
|
|
|
LPPROCESS_INFORMATION lpProcessInformation)
|
2013-09-24 06:39:28 +04:00
|
|
|
{
|
2022-09-28 17:07:04 +03:00
|
|
|
return FALSE;
|
2013-09-24 06:39:28 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
BOOL CreateProcessWithLogonW(LPCWSTR lpUsername, LPCWSTR lpDomain, LPCWSTR lpPassword,
|
2019-11-06 17:24:51 +03:00
|
|
|
DWORD dwLogonFlags, LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
|
|
|
|
DWORD dwCreationFlags, LPVOID lpEnvironment,
|
2018-10-24 14:17:14 +03:00
|
|
|
LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
|
|
|
|
LPPROCESS_INFORMATION lpProcessInformation)
|
2013-09-24 06:39:28 +04:00
|
|
|
{
|
2022-09-28 17:07:04 +03:00
|
|
|
return FALSE;
|
2013-09-24 06:39:28 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL CreateProcessWithTokenA(HANDLE hToken, DWORD dwLogonFlags, LPCSTR lpApplicationName,
|
|
|
|
LPSTR lpCommandLine, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo,
|
|
|
|
LPPROCESS_INFORMATION lpProcessInformation)
|
2013-09-24 06:39:28 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return _CreateProcessExA(NULL, 0, lpApplicationName, lpCommandLine, NULL, NULL, FALSE,
|
|
|
|
dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo,
|
|
|
|
lpProcessInformation);
|
2013-09-24 06:39:28 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL CreateProcessWithTokenW(HANDLE hToken, DWORD dwLogonFlags, LPCWSTR lpApplicationName,
|
|
|
|
LPWSTR lpCommandLine, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
2018-10-24 14:17:14 +03:00
|
|
|
LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
|
|
|
|
LPPROCESS_INFORMATION lpProcessInformation)
|
2013-09-24 06:39:28 +04:00
|
|
|
{
|
2022-09-28 17:07:04 +03:00
|
|
|
return FALSE;
|
2013-09-24 06:39:28 +04:00
|
|
|
}
|
|
|
|
|
2013-09-24 22:35:03 +04:00
|
|
|
VOID ExitProcess(UINT uExitCode)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
exit((int)uExitCode);
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
BOOL GetExitCodeProcess(HANDLE hProcess, LPDWORD lpExitCode)
|
|
|
|
{
|
|
|
|
WINPR_PROCESS* process;
|
|
|
|
|
|
|
|
if (!hProcess)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!lpExitCode)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
process = (WINPR_PROCESS*)hProcess;
|
2013-09-23 22:44:59 +04:00
|
|
|
*lpExitCode = process->dwExitCode;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-20 05:32:14 +04:00
|
|
|
HANDLE _GetCurrentProcess(VOID)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetCurrentProcessId(VOID)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return ((DWORD)getpid());
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode)
|
|
|
|
{
|
2013-09-24 22:35:03 +04:00
|
|
|
WINPR_PROCESS* process;
|
2019-11-06 17:24:51 +03:00
|
|
|
process = (WINPR_PROCESS*)hProcess;
|
2013-09-24 22:35:03 +04:00
|
|
|
|
2015-02-09 16:07:14 +03:00
|
|
|
if (!process || (process->pid <= 0))
|
2013-09-24 22:35:03 +04:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (kill(process->pid, SIGTERM))
|
|
|
|
return FALSE;
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-10-23 19:17:14 +03:00
|
|
|
static BOOL ProcessHandleCloseHandle(HANDLE handle)
|
2014-12-02 11:08:10 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_PROCESS* process = (WINPR_PROCESS*)handle;
|
2022-09-23 16:31:41 +03:00
|
|
|
WINPR_ASSERT(process);
|
|
|
|
if (process->fd >= 0)
|
|
|
|
{
|
|
|
|
close(process->fd);
|
|
|
|
process->fd = -1;
|
|
|
|
}
|
2014-12-02 11:08:10 +03:00
|
|
|
free(process);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL ProcessHandleIsHandle(HANDLE handle)
|
|
|
|
{
|
2022-04-27 16:56:39 +03:00
|
|
|
return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_PROCESS, FALSE);
|
2014-12-02 11:08:10 +03:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
static int ProcessGetFd(HANDLE handle)
|
2014-12-02 11:08:10 +03:00
|
|
|
{
|
2018-10-24 14:17:14 +03:00
|
|
|
WINPR_PROCESS* process = (WINPR_PROCESS*)handle;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!ProcessHandleIsHandle(handle))
|
|
|
|
return -1;
|
|
|
|
|
2022-09-23 16:31:41 +03:00
|
|
|
return process->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD ProcessCleanupHandle(HANDLE handle)
|
|
|
|
{
|
|
|
|
WINPR_PROCESS* process = (WINPR_PROCESS*)handle;
|
|
|
|
|
|
|
|
WINPR_ASSERT(process);
|
|
|
|
if (process->fd > 0)
|
|
|
|
{
|
|
|
|
if (waitpid(process->pid, &process->status, WNOHANG) == process->pid)
|
|
|
|
process->dwExitCode = (DWORD)process->status;
|
|
|
|
}
|
|
|
|
return WAIT_OBJECT_0;
|
2014-12-02 11:08:10 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static HANDLE_OPS ops = { ProcessHandleIsHandle,
|
|
|
|
ProcessHandleCloseHandle,
|
|
|
|
ProcessGetFd,
|
2022-09-23 16:31:41 +03:00
|
|
|
ProcessCleanupHandle, /* CleanupHandle */
|
2019-11-06 17:24:51 +03:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2022-04-13 10:34:05 +03:00
|
|
|
NULL,
|
2019-11-06 17:24:51 +03:00
|
|
|
NULL };
|
2015-03-16 02:29:37 +03:00
|
|
|
|
2022-09-23 16:31:41 +03:00
|
|
|
static int _pidfd_open(pid_t pid)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
#if !defined(__NR_pidfd_open)
|
|
|
|
#define __NR_pidfd_open 434
|
|
|
|
#endif /* __NR_pidfd_open */
|
|
|
|
|
|
|
|
#ifndef PIDFD_NONBLOCK
|
|
|
|
#define PIDFD_NONBLOCK O_NONBLOCK
|
|
|
|
#endif /* PIDFD_NONBLOCK */
|
|
|
|
|
|
|
|
int fd = syscall(__NR_pidfd_open, pid, PIDFD_NONBLOCK);
|
|
|
|
if (fd < 0 && errno == EINVAL)
|
|
|
|
{
|
|
|
|
/* possibly PIDFD_NONBLOCK is not supported, let's try to create a pidfd and set it
|
|
|
|
* non blocking afterward */
|
|
|
|
int flags;
|
|
|
|
fd = syscall(__NR_pidfd_open, pid, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-02 11:08:10 +03:00
|
|
|
HANDLE CreateProcessHandle(pid_t pid)
|
|
|
|
{
|
|
|
|
WINPR_PROCESS* process;
|
2019-11-06 17:24:51 +03:00
|
|
|
process = (WINPR_PROCESS*)calloc(1, sizeof(WINPR_PROCESS));
|
2014-12-02 11:08:10 +03:00
|
|
|
|
|
|
|
if (!process)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
process->pid = pid;
|
2022-04-27 16:56:39 +03:00
|
|
|
process->common.Type = HANDLE_TYPE_PROCESS;
|
|
|
|
process->common.ops = &ops;
|
2022-09-23 16:31:41 +03:00
|
|
|
process->fd = _pidfd_open(pid);
|
|
|
|
if (process->fd >= 0)
|
|
|
|
process->common.Mode = WINPR_FD_READ;
|
2014-12-02 11:08:10 +03:00
|
|
|
return (HANDLE)process;
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
#endif
|