/** * @brief * @param * @see * @remarks * @code * @endcode * @return **/ bool process::kill(_In_ DWORD exit_code) { _ASSERTE(true != _killed); if (true == _killed) return true; set_privilege(SE_DEBUG_NAME, TRUE); HANDLE h = NULL; do { h = OpenProcess(PROCESS_TERMINATE, FALSE, _pid); if (NULL == h) { log_err "OpenProcess() failed, pid = %u, gle = %u", _pid, GetLastError() log_end break; } if (!TerminateProcess(h, exit_code)) { log_err "TerminateProcess() failed, pid = %u, gle = %u", _pid, GetLastError() log_end break; }
void exec_command(char* cmd) { bool quote_flag; char** args; int arg_num; char* buf; char* p; char** p_args; char* server_path; size_t len; //Copy command buf = malloc(strlen(cmd) + 1); strcpy(buf, cmd); //Compute number of arguments for(quote_flag = false, arg_num = 1, p = buf; *p != '\0'; p++) { if(*p == '\"' || *p == '\'') { quote_flag = !quote_flag; } else if(*p == ' ' && !quote_flag) { arg_num++; } } //Spilt arguments args = malloc(sizeof(char*) * (arg_num + 1)); *args = buf; for(p_args = args + 1, quote_flag = false, p = buf; *p != '\0'; p++) { if(*p == '\"' || *p == '\'') { quote_flag = !quote_flag; } else if(*p == ' ' && !quote_flag) { *p = '\0'; p++; *p_args = p; p_args++; } } *p_args = NULL; //Set privilege len = cfg_get_mcserver_path(NULL, 0); server_path = malloc(len); cfg_get_mcserver_path(server_path, len); set_privilege(server_path); free(server_path); execvp(buf, args); return; }
/** * @brief * @param * @see * @remarks * @code * @endcode * @return */ HANDLE advanced_open_process(_In_ DWORD pid) { HANDLE ret = NULL; if (true != set_privilege(L"SeDebugPrivilege", true)) return ret; do { ret = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); if (NULL == ret) break; if (true != set_privilege(L"SeDebugPrivilege", false)) { CloseHandle(ret); ret = NULL; break; } } while (false); return ret; }
bool process::kill(_In_ DWORD exit_code) { _ASSERTE(true != _killed); if (true == _killed) return true; set_privilege(SE_DEBUG_NAME, TRUE); HANDLE h = NULL; do { h = OpenProcess(PROCESS_TERMINATE, FALSE, _pid); if (NULL == h) { printf("OpenProcess() failed, pid = %u, gle = %u\n", _pid, GetLastError()); break; } if (!TerminateProcess(h, exit_code)) { printf("TerminateProcess() failed, pid = %u, gle = %u\n", _pid, GetLastError()); break; } _killed = true; printf("pid = %u, %ws terminated\n", _pid, _process_name.c_str()); } while (false); if (NULL != h) { CloseHandle(h); } set_privilege(SE_DEBUG_NAME, FALSE); return (true == _killed) ? true : false; }
bool bio_sensor::init() { m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(BIO_SENSOR); if (!m_sensor_hal) { ERR("cannot load sensor_hal[%s]", sensor_base::get_name()); return false; } set_privilege(SENSOR_PRIVILEGE_INTERNAL); set_permission(SENSOR_PERMISSION_BIO); INFO("%s is created!\n", sensor_base::get_name()); return true; }
bool rv_raw_sensor::init() { m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(RV_RAW_SENSOR); if (!m_sensor_hal) { ERR("cannot load sensor_hal[%s]", sensor_base::get_name()); return false; } sensor_properties_t properties; if (!m_sensor_hal->get_properties(properties)) { ERR("sensor->get_properties() is failed!\n"); return false; } set_privilege(SENSOR_PRIVILEGE_INTERNAL); INFO("%s is created!\n", sensor_base::get_name()); return true; }
/* ANT_MEMORY::ALLOC() ------------------- Allocate large pages from the OS (and if this fails then use small pages). Under Windows large pages can fail if: 1. we are out of memory 2. the memory is fragmented 3. we don't have permissions to allocate large pages of memory 4. we're not on Vista (or later). to check whether or not you have permission to allocate large pages see: Control Panel->Administrative Tools->Local Security Settings->Local Policies->User Rights Assignment->Lock pages in memory */ void *ANT_memory::alloc(long long *size) { /* allocate memory from the operating system */ #ifdef _MSC_VER #ifdef PURIFY /* call malloc on every memory allocation */ if (allocated + *size > memory_ceiling) return NULL; // we've overflowed the soft limit else return ::malloc((size_t)*size); #else /* get a large chunk of memory from Windows */ void *answer = NULL; long long bytes = 0; /* First try using large page memory blocks */ #ifdef LARGE_MEMORY_PAGES if (has_large_pages) if (set_privilege(SE_LOCK_MEMORY_NAME, TRUE)) // try and get permission from the OS to allocate large pages { bytes = large_page_size * ((*size + large_page_size - 1) / large_page_size); if (allocated + bytes <= memory_ceiling) // check for overflow of the soft memory limt answer = VirtualAlloc(NULL, (size_t)bytes, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); set_privilege(SE_LOCK_MEMORY_NAME, FALSE); // drop back to the initial security level } #endif /* If we don't have large pages or large pages fail then fall back to small pages */ if (answer == NULL) { bytes = short_page_size * ((*size + short_page_size - 1) / short_page_size); if (allocated + bytes <= memory_ceiling) // check for overflow of the soft memory limt answer = VirtualAlloc(NULL, (size_t)bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); } if (answer == NULL) bytes = 0; // couldn't allocate any memory. else { /* This code is commented out at the moment because when I ran this over the INEX Wikipedia 2009 collection it caused a VISTA hang! It looks like you can't lock more than 4GB of memory - but this is a guess. */ // VirtualLock(answer, (size_t)bytes); // lock the pages in memory so that it can't page to disk } /* If we haven't been able to allocate memory then we're out of memory (either soft or hard) */ *size = bytes; // number of bytes we allocated return answer; #endif #else /* We're not on windows so call the C run time library malloc() */ if (allocated + *size > memory_ceiling) return NULL; // we've overflowed the soft limit else return ::malloc((size_t)*size); #endif }
bool cprocess_tree::build_process_tree() { _proc_map.clear(); bool ret = false; PROCESSENTRY32 proc_entry = { 0 }; HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snap == INVALID_HANDLE_VALUE) { printf("CreateToolhelp32Snapshot() failed, gle = %u\n", GetLastError()); return false; } if (true != set_privilege(SE_DEBUG_NAME, TRUE)) { printf("set_privilege(SE_DEBUG_NAME) failed\n"); } do { proc_entry.dwSize = sizeof(PROCESSENTRY32W); if (!Process32First(snap, &proc_entry)) { printf("CreateToolhelp32Snapshot() failed, gle = %u\n", GetLastError()); break; } do { FILETIME create_time = { 0 }; HANDLE process_handle = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, proc_entry.th32ProcessID ); if (NULL == process_handle) { } else { FILETIME dummy_time; if (!GetProcessTimes(process_handle, &create_time, &dummy_time, &dummy_time, &dummy_time)) { printf("GetProcessTimes() failed, gle = %u\n", GetLastError()); } CloseHandle(process_handle); process_handle = NULL; } add_process(proc_entry.th32ParentProcessID, proc_entry.th32ProcessID, create_time, proc_entry.szExeFile); } while (Process32Next(snap, &proc_entry)); } while (false); CloseHandle(snap); set_privilege(SE_DEBUG_NAME, FALSE); return true; }