📄 smpd_launch_process.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. */#include "smpd.h"#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#ifdef HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#ifdef HAVE_SYS_STAT_H/* This is needed for umask */#include <sys/stat.h>#endif#include <stdlib.h>#ifdef HAVE_ERRNO_H#include <errno.h>#endif#ifdef HAVE_STRING_H#include <string.h>#endif#ifdef HAVE_SIGNAL_H#include <signal.h>#endif#ifdef HAVE_WINDOWS_H#define MAX_ERROR_LENGTH 512#undef FCNAME#define FCNAME "smpd_clear_process_registry"int smpd_clear_process_registry(){#if 0 if (SHDeleteKey(HKEY_LOCAL_MACHINE, SMPD_REGISTRY_KEY "\\process") != ERROR_SUCCESS) { /* It's ok if the key does not exist */ } return SMPD_SUCCESS;#endif HKEY tkey; DWORD dwLen, result; int i; DWORD dwNumSubKeys, dwMaxSubKeyLen; char pid_str[256]; char err_msg[MAX_ERROR_LENGTH] = ""; smpd_enter_fn(FCNAME); result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SMPD_REGISTRY_KEY "\\process", 0, KEY_ALL_ACCESS, &tkey); if (result != ERROR_SUCCESS) { if (result != ERROR_PATH_NOT_FOUND) { smpd_err_printf("Unable to open the " SMPD_REGISTRY_KEY "\\process registry key, error %d\n", result); smpd_exit_fn(FCNAME); return SMPD_FAIL; } return SMPD_SUCCESS; } result = RegQueryInfoKey(tkey, NULL, NULL, NULL, &dwNumSubKeys, &dwMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, NULL); if (result != ERROR_SUCCESS) { RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_FAIL; } if (dwMaxSubKeyLen > 256) { smpd_err_printf("Error: Invalid process subkeys, max length is too large: %d\n", dwMaxSubKeyLen); RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_FAIL; } if (dwNumSubKeys == 0) { result = RegCloseKey(tkey); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: RegCloseKey(HKEY_LOCAL_MACHINE\\" SMPD_REGISTRY_KEY "\\process) failed, %s\n", err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } result = RegDeleteKey(HKEY_LOCAL_MACHINE, SMPD_REGISTRY_KEY "\\process"); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: Unable to remove the HKEY_LOCAL_MACHINE\\" SMPD_REGISTRY_KEY "\\process registry key, %s\n", err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } /* count backwards so keys can be removed */ for (i=dwNumSubKeys-1; i>=0; i--) { dwLen = 256; result = RegEnumKeyEx(tkey, i, pid_str, &dwLen, NULL, NULL, NULL, NULL); if (result != ERROR_SUCCESS) { smpd_err_printf("Error: Unable to enumerate the %d subkey in the " SMPD_REGISTRY_KEY "\\process registry key\n", i); RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_FAIL; } result = RegDeleteKey(tkey, pid_str); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: RegDeleteKey(HKEY_LOCAL_MACHINE\\" SMPD_REGISTRY_KEY "\\process\\%s) failed, %s\n", pid_str, err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } } result = RegCloseKey(tkey); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: RegCloseKey(HKEY_LOCAL_MACHINE\\" SMPD_REGISTRY_KEY ") failed, %s\n", err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } result = RegDeleteKey(HKEY_LOCAL_MACHINE, SMPD_REGISTRY_KEY "\\process"); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: Unable to remove the HKEY_LOCAL_MACHINE\\" SMPD_REGISTRY_KEY "\\process registry key, %s\n", err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_validate_process_registry"int smpd_validate_process_registry(){ int error; HKEY tkey; DWORD dwLen, result; int i; DWORD dwNumSubKeys, dwMaxSubKeyLen; char pid_str[100]; int pid; HANDLE hTemp; char err_msg[MAX_ERROR_LENGTH] = ""; smpd_enter_fn(FCNAME); result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SMPD_REGISTRY_KEY "\\process", 0, KEY_ALL_ACCESS, &tkey); if (result != ERROR_SUCCESS) { if (result != ERROR_PATH_NOT_FOUND) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Unable to open the smpd\\process registry key, error %d, %s\n", result, err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } return SMPD_SUCCESS; } result = RegQueryInfoKey(tkey, NULL, NULL, NULL, &dwNumSubKeys, &dwMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, NULL); if (result != ERROR_SUCCESS) { RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_FAIL; } if (dwMaxSubKeyLen > 100) { smpd_err_printf("Error: Invalid process subkeys, max length is too large: %d\n", dwMaxSubKeyLen); RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_FAIL; } if (dwNumSubKeys == 0) { RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } /* count backwards so keys can be removed */ for (i=dwNumSubKeys-1; i>=0; i--) { dwLen = 100; result = RegEnumKeyEx(tkey, i, pid_str, &dwLen, NULL, NULL, NULL, NULL); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: Unable to enumerate the %d subkey in the smpd\\process registry key, %s\n", i, err_msg); RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_FAIL; } pid = atoi(pid_str); printf("pid = %d\n", pid);fflush(stdout); hTemp = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); if (hTemp == NULL) { error = GetLastError(); if (error == ERROR_INVALID_PARAMETER) { RegDeleteKey(tkey, pid_str); } /* else { printf("error = %d\n", error); } */ } else { CloseHandle(hTemp); } } result = RegCloseKey(tkey); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: RegCloseKey(HKEY_LOCAL_MACHINE\\" SMPD_REGISTRY_KEY "\\process) failed, %s\n", err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_process_to_registry"int smpd_process_to_registry(smpd_process_t *process, char *actual_exe){ HKEY tkey; DWORD len, result; char name[1024]; char err_msg[MAX_ERROR_LENGTH] = ""; smpd_enter_fn(FCNAME); if (process == NULL) { smpd_dbg_printf("NULL process passed to smpd_process_to_registry.\n"); return SMPD_FAIL; } len = snprintf(name, 1024, SMPD_REGISTRY_KEY "\\process\\%d", process->pid); if (len < 0 || len > 1023) { smpd_dbg_printf("unable to create a string of the registry key.\n"); return SMPD_FAIL; } result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, name, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &tkey, NULL); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Unable to open the HKEY_LOCAL_MACHINE\\%s registry key, error %d, %s\n", name, result, err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } len = (DWORD)(strlen(actual_exe)+1); result = RegSetValueEx(tkey, "exe", 0, REG_SZ, (const BYTE *)actual_exe, len); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Unable to write the process registry value 'exe:%s', error %d, %s\n", process->exe, result, err_msg); RegCloseKey(tkey); smpd_exit_fn(FCNAME); return SMPD_FAIL; } result = RegCloseKey(tkey); if (result != ERROR_SUCCESS) { smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Error: RegCloseKey(HKEY_LOCAL_MACHINE\\" SMPD_REGISTRY_KEY "\\process\\%d) failed, %s\n", process->pid, err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_process_from_registry"int smpd_process_from_registry(smpd_process_t *process){ DWORD len, result; char name[1024]; char err_msg[MAX_ERROR_LENGTH] = ""; smpd_enter_fn(FCNAME); if (process == NULL) { smpd_exit_fn(FCNAME); return SMPD_FAIL; } len = snprintf(name, 1024, SMPD_REGISTRY_KEY "\\process\\%d", process->pid); if (len < 0 || len > 1023) { smpd_exit_fn(FCNAME); return SMPD_FAIL; } result = RegDeleteKey(HKEY_LOCAL_MACHINE, name); if (result != ERROR_SUCCESS) { if (result == ERROR_FILE_NOT_FOUND || result == ERROR_PATH_NOT_FOUND) { /* Key already deleted, return success */ smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } smpd_translate_win_error(result, err_msg, MAX_ERROR_LENGTH, NULL); smpd_err_printf("Unable to delete the HKEY_LOCAL_MACHINE\\%s registry key, error %d\n", name, result, err_msg); smpd_exit_fn(FCNAME); return SMPD_FAIL; } smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_get_user_handle"int smpd_get_user_handle(char *account, char *domain, char *password, HANDLE *handle_ptr){ HANDLE hUser; int error; int num_tries = 3; smpd_enter_fn(FCNAME); if (domain) { smpd_dbg_printf("LogonUser(%s\\%s)\n", domain, account); } else { smpd_dbg_printf("LogonUser(%s)\n", account); } /* logon the user */ while (!LogonUser( account, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hUser)) { error = GetLastError(); if (error == ERROR_NO_LOGON_SERVERS) { if (num_tries) Sleep(250); else { *handle_ptr = INVALID_HANDLE_VALUE; smpd_exit_fn(FCNAME); return error; } num_tries--; } else { *handle_ptr = INVALID_HANDLE_VALUE; smpd_exit_fn(FCNAME); return error; } } *handle_ptr = hUser; smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_get_user_name"int smpd_get_user_name(char *account, char *domain, char *full_domain){ DWORD len; char name[SMPD_MAX_ACCOUNT_LENGTH]; char *separator; size_t i; *account = '\0'; if (domain != NULL) *domain = '\0'; if (full_domain != NULL) *full_domain = '\0'; len = 100; if (GetUserNameEx(NameSamCompatible, name, &len)) { for (i=0; i<strlen(name); i++) { name[i] = (char)(tolower(name[i])); } separator = strchr(name, '\\'); if (separator) { *separator = '\0'; separator++; } if (domain != NULL) strcpy(domain, name); strcpy(account, separator); } if (full_domain != NULL) { len = 100; if (GetUserNameEx(NameDnsDomain, name, &len)) { for (i=0; i<strlen(name); i++) { name[i] = (char)(tolower(name[i])); } separator = strchr(name, '\\'); if (separator) { *separator = '\0'; strcpy(full_domain, name); } } } smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "SetEnvironmentVariables"static void SetEnvironmentVariables(char *bEnv){ char name[SMPD_MAX_ENV_LENGTH], equals[3], value[SMPD_MAX_ENV_LENGTH]; smpd_enter_fn(FCNAME); for (;;) { name[0] = '\0'; equals[0] = '\0'; value[0] = '\0'; if (MPIU_Str_get_string(&bEnv, name, SMPD_MAX_ENV_LENGTH) != MPIU_STR_SUCCESS) break; if (name[0] == '\0') break; if (MPIU_Str_get_string(&bEnv, equals, 3) != MPIU_STR_SUCCESS) break; if (equals[0] == '\0') break; if (MPIU_Str_get_string(&bEnv, value, SMPD_MAX_ENV_LENGTH) != MPIU_STR_SUCCESS) break; smpd_dbg_printf("setting environment variable: <%s> = <%s>\n", name, value); SetEnvironmentVariable(name, value); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -