123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639 |
- #include "precompile.h"
- #include "osutil.h"
- #include "memutil.h"
- #include "strutil.h"
- #include "toolkit.h"
- #ifdef _WIN32
- #include <Pdh.h>
- #include <TlHelp32.h>
- #define DIV (1024 * 1024)
- #define DAY_DIV (24 * 60 * 60)
- #define HOURS_DIV (60 * 60)
- #define MINUS_DIV (60)
- TOOLKIT_API int osutil_detect_unique_app(char** pNames, int nNum, int* alive, alive_process_info* alive_process_arr)
- {
- HANDLE hSnapshot;
- int rc = TRUE;
- DWORD dwCurProcID = GetCurrentProcessId();
- int count = 0;
- if (alive) *alive = 0;
- hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSnapshot) {
- PROCESSENTRY32 pe;
- pe.dwSize = sizeof(pe);
- if (Process32First(hSnapshot, &pe)) {
- do {
- int i;
- for (i = 0; i < nNum; i++) {
- if (stricmp(&pe.szExeFile[0], pNames[i]) == 0 && pe.th32ProcessID != dwCurProcID) {
- rc = FALSE;
- count++;
- break;
- }
- }
- } while (Process32Next(hSnapshot, &pe));
- }
- CloseHandle(hSnapshot);
- }
- if (alive) *alive = count;
- return rc;
- }
- TOOLKIT_API int osutil_restart_system()
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
- BOOL fResult;
- // Get the current process token handle so we can get shutdown privilege.
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
- fprintf(stderr, "get proc token fail");
- return -1;
- }
- // Get the LUID for shutdown privilege.
- LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
- tkp.PrivilegeCount = 1; // one privilege to set
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- // Get shutdown privilege for this process.
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
- // Cannot test the return value of AdjustTokenPrivileges.
- if (GetLastError() != ERROR_SUCCESS) {
- fprintf(stderr, "adjust proc token privilege fail");
- return -1;
- }
- // Display the shutdown dialog box and start the countdown.
- fResult = InitiateSystemShutdown(
- NULL, // shut down local computer
- NULL, // message for user
- 0, // time-out period, in seconds
- FALSE, // ask user to close apps
- TRUE); // reboot after shutdown
- if (!fResult) {
- fprintf(stderr, "request windows reboot fail");
- return -1;
- }
- // Disable shutdown privilege.
- tkp.Privileges[0].Attributes = 0;
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
- return 0;
- }
- TOOLKIT_API void osutil_terminate_related_process(char** process_array, const int array_size, int force)
- {
- const DWORD dwCurProcessID = GetCurrentProcessId();
- DWORD relateProcessIDs[256];
- DWORD dwIdx = 0;
- char szCmd[256];
- DWORD relateProcessNum = 0;
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- memset(relateProcessIDs, 0, sizeof(relateProcessIDs));
- if (hSnapshot)
- {
- PROCESSENTRY32 pe;
- pe.dwSize = sizeof(pe);
- if (Process32First(hSnapshot, &pe)) {
- do {
- int i;
- for (i = 0; i < array_size; i++) {
- if (stricmp(&pe.szExeFile[0], process_array[i]) == 0 && pe.th32ProcessID != dwCurProcessID) {
- relateProcessIDs[relateProcessNum++] = pe.th32ProcessID;
- break;
- }
- }
- } while (Process32Next(hSnapshot, &pe));
- }
- CloseHandle(hSnapshot);
- }
-
- for (dwIdx = 0; dwIdx < relateProcessNum; ++dwIdx) {
- sprintf_s(szCmd, 256, "TASKKILL /PID %d /F", relateProcessIDs[dwIdx]);
- WinExec(szCmd, SW_HIDE);
- }
- }
- TOOLKIT_API int osutil_uname(tk_utsname_t* buffer)
- {
- return TOOLKIT_UNKNOWN;
- }
- TOOLKIT_API int osutil_is32r64_platform()
- {
- int isWow64 = -1;
- typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
- BOOL bIsWow64 = FALSE;
- LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
- if (NULL != fnIsWow64Process) {
- if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64)) {
- return -1;
- }
- else {
- isWow64 = bIsWow64 ? 1 : 0;
- }
- }
- return isWow64;
- }
- TOOLKIT_API ostuile_void_ptr osutil_sure_redirect_32sys_in_wow64()
- {
- ostuile_void_ptr value = NULL;
- if (osutil_is32r64_platform() == 1) {
- if (!Wow64DisableWow64FsRedirection(&value)) {
- value = NULL;
- }
- }
- return value;
- }
- TOOLKIT_API void osutil_reset_redirect_32sys_in_wow64(ostuile_void_ptr* value)
- {
- if (value != NULL && (*value) != NULL) {
- Wow64RevertWow64FsRedirection(*value);
- }
- return;
- }
- #else
- #include <unistd.h>
- #include <dirent.h>
- #include <sys/types.h> // for opendir(), readdir(), closedir()
- #include <sys/stat.h> // for stat()
- #include <strings.h>
- #include <signal.h>
- #include <winpr/wtypes.h>
- #include <sys/reboot.h>
- #include <sys/utsname.h>
- #include <sys/sysinfo.h>
- #define DEFAULT_PROCESS_CMDLINE_SIZE 56
- #define DEFAULT_PROCESS_PATH_SIZE 8192
- #define DEFAULT_PROCESS_NAME_SIZE 1024
- static inline int is_num(const char* chars)
- {
- for (; *chars; chars++)
- if (*chars < '0' || *chars > '9')
- return 0; // false
- return 1; // true
- }
- static int execute_cmd_return_result(const char* cmd, char* result)
- {
- char buf_ps[1024];
- char ps[1024] = { 0 };
- FILE* ptr;
- strcpy(ps, cmd);
- if ((ptr = popen(ps, "r")) != NULL) {
- while (fgets(buf_ps, 1024, ptr) != NULL) {
- strcat(result, buf_ps);
- if (strlen(result) > 1024)
- break;
- }
- int len = strlen(result);
- int i;
- for (i = len - 1; i >= 0 && result[i] == '\n'; --i) {
- result[i] = '\0';
- }
- pclose(ptr);
- return 0;
- } else {
- printf("popen %s error\n", ps);
- return -1;
- }
- }
- static int private_get_running_process_ids(int* alive, alive_process_info* alive_process_arr, const char* process_name)
- {
- int ret = 0, capacity, count, idx;
- char content[1025];
- char cmd[256];
- const int cur_pid = (int)getpid();
- memset(content, 0, sizeof(content));
- capacity = *alive;
- *alive = 0;
- idx = count = 0;
- sprintf(cmd, "pgrep -u root -d '|' %s", process_name);
- ret = execute_cmd_return_result(cmd, content);
- if (ret == 0 && strlen(content) > 0) {
- char** elems = strsplit(content, "|");
- if (elems != NULL) {
- char** p;
- for (p = elems; *p != NULL; ++p) {
- int pid;
- pid = atoi(*p);
- if(pid == cur_pid)
- continue;
- printf("%d: %s, %d\n", count, *p, pid);
-
- if (count < capacity) {
- alive_process_arr[count].pid = pid;
- strcpy(alive_process_arr[count].name, process_name);
- strcpy(alive_process_arr[count].path, process_name);
- idx++;
- }
- count++;
- }
- strfreev(elems);
- }
- }
- *alive = idx;
- return count;
- }
- #ifdef NEW_FEATURE
- TOOLKIT_API int osutil_detect_unique_app(char** pNames, int nNum, int* alive, alive_process_info* alive_process_arr)
- {
- int i = 0;
- int count = 0, capacity = 0;
- int real = 0;
- if (alive) {
- if (alive_process_arr) capacity = *alive;
- *alive = 0;
- }
- for (i = 0; i < nNum; ++i) {
- int cap = capacity - count;
- int result = private_get_running_process_ids(&cap, alive_process_arr + count, pNames[i]);
- count += cap;
- real += result;
- }
- if (alive) {
- *alive = real;
- }
- return (real > 0) ? FALSE : TRUE;
- }
- #else
- TOOLKIT_API int osutil_detect_unique_app(char** pNames, int nNum, int* alive, alive_process_info* alive_process_arr)
- {
- int rc = TRUE;
- DIR* dir_proc = NULL;
- struct dirent* dir_entity = NULL;
- int i = 0;
- int count = 0, capacity = 0;
- char cmdLinePath[DEFAULT_PROCESS_CMDLINE_SIZE] = { 0 };
- char processName[DEFAULT_PROCESS_NAME_SIZE] = { 0 };
- char processPath[DEFAULT_PROCESS_PATH_SIZE] = { 0 };
- pid_t pid = (pid_t)-1;
- const pid_t cur_pid = getpid();
- if (alive) {
- if (alive_process_arr) capacity = *alive;
- *alive = 0;
- }
- dir_proc = opendir("/proc/");
- if (dir_proc == NULL) {
- return FALSE;
- }
- while ((dir_entity = readdir(dir_proc))) {
- if (dir_entity->d_type == DT_DIR) {
- if (is_num(dir_entity->d_name)) {
- pid = (pid_t)atoi(dir_entity->d_name);
- if (pid == getpid()) {
- continue;
- }
- strcpy(cmdLinePath, "/proc/");
- strcat(cmdLinePath, dir_entity->d_name);
- strcat(cmdLinePath, "/cmdline");
- //printf("%s: open(%s)...\n", __FUNCTION__, cmdLinePath);
- FILE* fd_cmdline = fopen(cmdLinePath, "rt");
- if (fd_cmdline != NULL) {
- memset(processName, 0, DEFAULT_PROCESS_NAME_SIZE);
- memset(processPath, 0, DEFAULT_PROCESS_PATH_SIZE);
- ///**TODO(Gifur@1/10/2022): 读文件的所有内容并通过结束符0x00去拆分 */
- fscanf(fd_cmdline, "%s", processPath);
- //printf("%s: close(%p)...\n", __FUNCTION__, fd_cmdline);
- fclose(fd_cmdline);
- //printf("%s: close(%p) done!\n", __FUNCTION__, fd_cmdline);
- if (strrchr(processPath, '/')) {
- strcpy(processName, strrchr(processPath, '/') + 1);
- } else {
- strcpy(processName, processPath);
- }
- for (i = 0; i < nNum; i++) {
- if (strcasecmp(processName, pNames[i]) == 0) {
- printf("%s, %s, %d, %d\n", processPath, processName, pid, cur_pid);
- if (count < capacity) {
- if (strlen(processPath) > ALIVE_PROCESS_PATH_LEN - 1) {
- processPath[ALIVE_PROCESS_PATH_LEN - 1] = '\0';
- processPath[ALIVE_PROCESS_PATH_LEN - 2] = '.';
- processPath[ALIVE_PROCESS_PATH_LEN - 3] = '.';
- processPath[ALIVE_PROCESS_PATH_LEN - 4] = '.';
- }
- strcpy(alive_process_arr[count].path, processPath);
- alive_process_arr[count].pid = pid;
- strcpy(alive_process_arr[count].name, processName);
- }
- count++;
- rc = FALSE;
- break;
- }
- }
- } else {
- printf("%s: open(%s) failed %d!!\n", __FUNCTION__, cmdLinePath, errno);
- }
- }
- }
- if (!rc && !alive) {
- break;
- }
- }
- printf("%s: closedir(%p)...\n", __FUNCTION__, dir_proc);
- closedir(dir_proc);
- printf("%s: closedir(%p) done!\n", __FUNCTION__, dir_proc);
- if (alive) *alive = count;
- printf("%s: exit!\n", __FUNCTION__);
- return rc;
- }
- #endif
- TOOLKIT_API int osutil_restart_system()
- {
- int result = -1;
- sync();
- result = reboot(RB_AUTOBOOT);
- return result;
- }
- #ifdef NEW_FEATURE
- TOOLKIT_API void osutil_terminate_related_process(char** process_array, const int array_size, int force)
- {
- int i, j, k;
- const pid_t cur_pid = getpid();
- k = 0;
- for (i = 0; i < array_size; ++i) {
- int count = 50;
- alive_process_info processes[50];
- memset(processes, 0, sizeof(processes));
- int result = private_get_running_process_ids(&count, processes, process_array[i]);
- for (j = 0; j < count; ++j) {
- printf("kill %s, %s, %d, %d\n", processes[j].path, processes[j].name, processes[j].pid, cur_pid);
- int r = kill(processes[j].pid, SIGKILL);
- if (r != 0) {
- printf("kill of pid=%d failed:%d\n", processes[j].pid, errno);
- } else {
- k++;
- }
- }
- }
- printf("total kill process count: %d\n", k);
- }
- #else
- TOOLKIT_API void osutil_terminate_related_process(char** process_array, const int array_size, int force)
- {
- DIR* dir_proc = NULL;
- struct dirent* dir_entity = NULL;
- int i = 0;
- int count = 0;
- char cmdLinePath[DEFAULT_PROCESS_CMDLINE_SIZE] = { 0 };
- char processName[DEFAULT_PROCESS_NAME_SIZE] = { 0 };
- char processPath[DEFAULT_PROCESS_PATH_SIZE] = { 0 };
- const pid_t cur_pid = getpid();
- dir_proc = opendir("/proc/");
- if (dir_proc == NULL) {
- goto finished;
- }
- while ((dir_entity = readdir(dir_proc))) {
- if (dir_entity->d_type == DT_DIR) {
- if (is_num(dir_entity->d_name)) {
- pid_t pid = (pid_t)atoi(dir_entity->d_name);
- if (pid == getpid()) {
- continue;
- }
- strcpy(cmdLinePath, "/proc/");
- strcat(cmdLinePath, dir_entity->d_name);
- strcat(cmdLinePath, "/cmdline");
- FILE* fd_cmdline = fopen(cmdLinePath, "rt");
- if (fd_cmdline != NULL) {
- memset(processName, 0, DEFAULT_PROCESS_NAME_SIZE);
- memset(processPath, 0, DEFAULT_PROCESS_PATH_SIZE);
- fscanf(fd_cmdline, "%s", processPath);
- fclose(fd_cmdline);
- if (strrchr(processPath, '/')) {
- strcpy(processName, strrchr(processPath, '/') + 1);
- } else {
- strcpy(processName, processPath);
- }
- for (i = 0; i < array_size; i++) {
- if (strcasecmp(processName, process_array[i]) == 0) {
- printf("kill %s, %s, %d, %d, type: %s\n", processPath, processName, pid, cur_pid, !!force ? "SIGKILL" : "SIGTERM");
- int r = kill(pid, !!force ? SIGKILL : SIGTERM);
- if (r != 0) {
- printf("kill of pid=%d failed:%d\n", pid, errno);
- }
- else {
- Sleep(200);
- count++;
- }
- break;
- }
- }
- } else if(errno != 2) {
- printf("%s: open(%s) failed %d!!\n", __FUNCTION__, cmdLinePath, errno);
- }
- }
- }
- }
- closedir(dir_proc);
- finished:
- printf("total kill process count: %d\n", count);
- }
- #endif
- /*cat /ect/os-release*/
- TOOLKIT_API int osutil_uname(tk_utsname_t* buffer)
- {
- struct utsname buf;
- int r;
- if (buffer == NULL)
- return TOOLKIT_EINVAL;
- if (uname(&buf) == -1) {
- r = TOOLKIT__ERR(errno);
- goto error;
- }
- r = strscpy(buffer->sysname, buf.sysname, sizeof(buffer->sysname));
- if (r == TOOLKIT_E2BIG)
- goto error;
- r = strscpy(buffer->release, buf.release, sizeof(buffer->release));
- if (r == TOOLKIT_E2BIG)
- goto error;
- r = strscpy(buffer->version, buf.version, sizeof(buffer->version));
- if (r == TOOLKIT_E2BIG)
- goto error;
- r = strscpy(buffer->machine, buf.machine, sizeof(buffer->machine));
- if (r == TOOLKIT_E2BIG)
- goto error;
- return 0;
- error:
- buffer->sysname[0] = '\0';
- buffer->release[0] = '\0';
- buffer->version[0] = '\0';
- buffer->machine[0] = '\0';
-
- return r;
- }
- TOOLKIT_API int osutil_is32r64_platform()
- {
- return 0;
- }
- TOOLKIT_API ostuile_void_ptr osutil_sure_redirect_32sys_in_wow64()
- {
- ostuile_void_ptr value = NULL;
- return value;
- }
- TOOLKIT_API void osutil_reset_redirect_32sys_in_wow64(ostuile_void_ptr* value)
- {
- return;
- }
- #endif
- TOOLKIT_API int osutil_shutdown_system()
- {
- //system("poweroff");
- system("shutdown -h now");
- return 0;
- }
- TOOLKIT_API int osutil_system_boot_time(tk_systime_t* ptr_boot_time)
- {
- int result = -1;
-
- #if defined(_MSC_VER)
- const char* SystemElapsedQuery = "\\System\\System Up Time";
- PDH_STATUS Status;
- HQUERY Query = NULL;
- HCOUNTER hcElapsedTimeCount;
- /** 杀毒软件会对附件这几个系统调用函数有所监控,导致有时会耗费5s时间 Gifur@20221019]*/
- Status = PdhOpenQuery(NULL, NULL, &Query);
- PDH_FMT_COUNTERVALUE counterValue;
- if (Status != ERROR_SUCCESS) {
- return result;
- }
- Status = PdhAddCounter(Query, SystemElapsedQuery, NULL, &hcElapsedTimeCount);
- if (Status != ERROR_SUCCESS) {
- return result;
- }
- Status = PdhCollectQueryData(Query);
- if (Status != ERROR_SUCCESS) {
- goto Cleanup;
- }
- Status = PdhGetFormattedCounterValue(hcElapsedTimeCount, PDH_FMT_LARGE, NULL, &counterValue);
- if (Status == ERROR_SUCCESS) {
- FILETIME ftCurTime, ftStartTime;
- GetSystemTimeAsFileTime(&ftCurTime);
-
- ULARGE_INTEGER uliCurTime;
- uliCurTime.HighPart = ftCurTime.dwHighDateTime;
- uliCurTime.LowPart = ftCurTime.dwLowDateTime;
- uliCurTime.QuadPart -= counterValue.largeValue * 1e7;
- ftStartTime.dwHighDateTime = uliCurTime.HighPart;
- ftStartTime.dwLowDateTime = uliCurTime.LowPart;
- SYSTEMTIME stUTC, stLocal;
- FileTimeToSystemTime(&ftStartTime, &stUTC);
- SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
- ptr_boot_time->year = stLocal.wYear;
- ptr_boot_time->month = stLocal.wMonth;
- ptr_boot_time->dayofweek = stLocal.wDayOfWeek;
- ptr_boot_time->day = stLocal.wDay;
- ptr_boot_time->hour = stLocal.wHour;
- ptr_boot_time->minute = stLocal.wMinute;
- ptr_boot_time->second = stLocal.wSecond;
- ptr_boot_time->milli_seconds = stLocal.wMilliseconds;
- result = 0;
- }
- Cleanup:
- Status = PdhRemoveCounter(hcElapsedTimeCount);
- if (Query) {
- PdhCloseQuery(Query);
- }
- #else
- DWORD ticks = 0;
- struct sysinfo info;
- time_t curTime = 0;
- time_t bootTime = 0;
- struct tm* ptm = NULL;
- if (sysinfo(&info)) {
- return result;
- }
- time(&curTime);
- if (curTime > info.uptime) {
- bootTime = curTime - info.uptime;
- }
- else
- {
- bootTime = info.uptime - curTime;
- }
- ptm = localtime(&bootTime);
- ptr_boot_time->year = (WORD)(ptm->tm_year + 1900);
- ptr_boot_time->month = (WORD)(ptm->tm_mon + 1);
- ptr_boot_time->dayofweek = (WORD)ptm->tm_wday;
- ptr_boot_time->day = (WORD)ptm->tm_mday;
- ptr_boot_time->hour = (WORD)ptm->tm_hour;
- ptr_boot_time->minute = (WORD)ptm->tm_min;
- ptr_boot_time->second = (WORD)ptm->tm_sec;
- struct timespec ts;
- if (!clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) {//获取毫秒的数据不太准确
- ticks = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
- }
- ptr_boot_time->milli_seconds = (WORD)(ticks % 1000);
- result = 0;
- #endif //_MSC_VER
- return result;
- }
|