bool process_is_same_executable(pid_t pid) { char resolved_them[PROC_PIDPATHINFO_MAXSIZE], resolved_me[PROC_PIDPATHINFO_MAXSIZE]; if (proc_pidpath(pid, resolved_them, sizeof(resolved_them)) <= 0 || proc_pidpath(getpid(), resolved_me, sizeof(resolved_me)) <= 0) return false; if (strcmp(resolved_them, resolved_me)) return false; return true; }
// get current directory string GetExecutableDir() { const int maxLen = 2048; char buf[ maxLen ]; #if defined(SORT_IN_LINUX) int c = readlink( "/proc/self/exe", buf, maxLen - 1 ); buf[c] = '/'; buf[c+1] = 0; #elif defined(SORT_IN_WINDOWS) // get the current module filename GetModuleFileNameA( NULL , buf , maxLen ); // remove the file name int len = strlen( buf ); for( int i = len - 1 ; i > 0 ; i-- ) { if( buf[i] == '\\' ) { buf[i+1] = 0; break; } } #elif defined(SORT_IN_MAC) pid_t pid = getpid(); int ret = proc_pidpath (pid, buf, sizeof(buf)); if ( ret <= 0 ) { LOG_ERROR<<"Can't get current directory."<<ENDL; return ""; } #endif return string(buf); }
void OSXGetAppFilename(osx_state *State) { // NOTE(casey): Never use MAX_PATH in code that is user-facing, because it // can be dangerous and lead to bad results. // pid_t PID = getpid(); int r = proc_pidpath(PID, State->AppFilename, sizeof(State->AppFilename)); if (r <= 0) { fprintf(stderr, "Error getting process path: pid %d: %s\n", PID, strerror(errno)); } else { printf("process pid: %d path: %s\n", PID, State->AppFilename); } State->OnePastLastAppFilenameSlash = State->AppFilename; for(char *Scan = State->AppFilename; *Scan; ++Scan) { if(*Scan == '/') { State->OnePastLastAppFilenameSlash = Scan + 1; } } }
Directory Directory::applicationDir() { #ifdef CAESARIA_PLATFORM_WIN unsigned int pathSize=512; ByteArray tmpPath; tmpPath.resize( pathSize ); GetModuleFileNameA( 0, tmpPath.data(), pathSize); Directory tmp( std::string( tmpPath.data() ) ); tmp = tmp.up(); return tmp; #elif defined(CAESARIA_PLATFORM_LINUX) char exe_path[PATH_MAX] = {0}; sprintf(exe_path, "/proc/%d/exe", ::getpid()); readlink(exe_path, exe_path, sizeof(exe_path)); vfs::Directory wdir = vfs::Path( exe_path ).directory(); //dir_path = dirname(exe_path); return wdir; /*#elif defined(CAESARIA_PLATFORM_HAIKU) char exe_path[PATH_MAX] = {0}; sprintf(exe_path, "/proc/%d/exe", getpid()); readlink(exe_path, exe_path, sizeof(exe_path)); dirname(exe_path); return Path( exe_path );*/ #elif defined(CAESARIA_PLATFORM_MACOSX) char exe_path[PROC_PIDPATHINFO_MAXSIZE]; int ret = proc_pidpath(getpid(), exe_path, sizeof(exe_path)); if (ret <= 0) { THROW("Cannot get application executable file path"); } return Path(dirname(exe_path)); #endif return Path( "." ); }
const char * exe_path() { static char buffer_exe_path[PROC_PIDPATHINFO_MAXSIZE] = "\0"; if(strcmp(buffer_exe_path, "") == 0) { proc_pidpath(getpid(), buffer_exe_path, sizeof(buffer_exe_path)); size_t path_length = 0; for(size_t i = 0; i < PROC_PIDPATHINFO_MAXSIZE; i++) { if(buffer_exe_path[i] == '\0') { path_length = i; break; } } size_t last_slash_index = 0; for(size_t i = 0; i < path_length; i++) { size_t r_i = (path_length - 1) - i; if(buffer_exe_path[r_i] == '/' || buffer_exe_path[r_i] == '\\') { last_slash_index = r_i; break; } } buffer_exe_path[last_slash_index + 1] = '\0'; } return buffer_exe_path; }
FileDir FileDir::getApplicationDir() { #ifdef WIN32 unsigned int pathSize=512; ByteArray tmpPath; tmpPath.resize( pathSize ); GetModuleFileName( 0, tmpPath.data(), pathSize); FilePath tmp = tmpPath.data(); tmp = tmp.getUpDir(); //delete tmpPath; return tmp; #elif defined(__linux__) char exe_path[PATH_MAX] = {0}; char * dir_path; sprintf(exe_path, "/proc/%d/exe", getpid()); readlink(exe_path, exe_path, sizeof(exe_path)); dir_path = dirname(exe_path); return FilePath(dir_path); #elif defined(__APPLE__) char exe_path[PROC_PIDPATHINFO_MAXSIZE]; int ret = proc_pidpath(getpid(), exe_path, sizeof(exe_path)); if (ret <= 0) { THROW("Cannot get application executable file path"); } return FilePath(dirname(exe_path)); #endif return FilePath( "." ); }
R_API char *r_sys_pid_to_path(int pid) { #if __WINDOWS__ // TODO: implement r_sys_pid_to_path on W32 return NULL; #elif __APPLE__ char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; int ret = proc_pidpath (pid, pathbuf, sizeof (pathbuf)); if (ret <= 0) return NULL; return strdup (pathbuf); #else int ret; char buf[128], pathbuf[1024]; #if __FreeBSD__ snprintf (buf, sizeof (buf), "/proc/%d/file", pid); #else snprintf (buf, sizeof (buf), "/proc/%d/exe", pid); #endif ret = readlink (buf, pathbuf, sizeof (pathbuf)-1); if (ret<1) return NULL; pathbuf[ret] = 0; return strdup (pathbuf); #endif }
inline std::string getProcPath(int pid) { char path[PROC_PIDPATHINFO_MAXSIZE] = "\0"; int bufsize = proc_pidpath(pid, path, sizeof(path)); if (bufsize <= 0) { path[0] = '\0'; } return std::string(path); }
pid_t get_pid_for_process (const char *procName) { int processCount = proc_listpids (PROC_ALL_PIDS, 0, NULL, 0) / sizeof (pid_t); if (processCount < 1) { printf ("Only found %d processes running!\n", processCount); return -1; } else if (gVerbose) { printf ("%i processes now running\n", processCount); } // Allocate a few extra slots in case new processes are spawned int allPidsSize = sizeof (pid_t) * (processCount + 3); pid_t *allPids = (pid_t *) malloc (allPidsSize); // re-set process_count in case the number of processes changed (got smaller; we won't do bigger) processCount = proc_listpids (PROC_ALL_PIDS, 0, allPids, allPidsSize) / sizeof (pid_t); int i; pid_t highestPid = 0; int matchCount = 0; for (i = 1; i < processCount; i++) { char pidPath[PATH_MAX]; int pidPathLen = proc_pidpath (allPids[i], pidPath, sizeof (pidPath)); if (pidPathLen == 0) continue; char *j = strrchr (pidPath, '/'); if ((j == NULL && strcmp (procName, pidPath) == 0) || (j != NULL && strcmp (j + 1, procName) == 0)) { matchCount++; if (allPids[i] > highestPid) highestPid = allPids[i]; } } free (allPids); if (matchCount == 0 && gVerbose) { printf ("Did not find process '%s'\n", procName); } if (matchCount > 1 && gVerbose) { printf ("Got %i matches for '%s'!, Defaulting to the highest-pid\n", matchCount, procName); } return highestPid; }
internal void SDLGetEXEFileName(sdl_state *State) { pid_t pid = getpid(); proc_pidpath(pid, State->EXEFileName, SDL_STATE_FILE_NAME_COUNT); State->OnePastLastEXEFileNameSlash = State->EXEFileName; for (char *Scan = State->EXEFileName; *Scan; ++Scan) { if (*Scan == '/') { State->OnePastLastEXEFileNameSlash = Scan + 1; } } }
std::string SQLUtility::getTestRootPath() const { int ret; pid_t pid; char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; pid = getpid(); ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf)); if (ret <= 0) EXPECT_TRUE(false) << "PID " << pid << ": proc_pidpath () " << strerror(errno); return splitFilePath(pathbuf).path; }
/** * When this method is called only pid and pidStr properties are set * (and they are required). * * Not thread safe, because it's private * * @return success/failure */ bool ProcessManagerMac::setProcessProperties(ProcessProperties & newProperties, pid_t pid, bool ppid_known, pid_t ppid, bool name_known, const std::string & name ) { newProperties.pid = pid; try { newProperties.pidStr = boost::lexical_cast< string >( pid ); } catch( const std::exception & ex ) { return false; } if( !ppid_known ) { // TODO unimplemented newProperties.ppid_known = false; return false; } else { newProperties.ppid = ppid; try { newProperties.ppidStr = boost::lexical_cast< string >( ppid ); } catch( const std::exception & ex ) { return false; } newProperties.ppid_known = true; } if( !name_known ) { char pathbuf[2048]; int ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf)); if ( ret <= 0 ) { _ldbg("Error while retrieveing executable path (proc_pidpath()) for PID %d: ", pid); _dbg(" %s\n", strerror(errno)); return false; } string filename(pathbuf); const size_t last_slash_idx = filename.find_last_of("/"); if (std::string::npos != last_slash_idx) { filename.erase(0, last_slash_idx + 1); } newProperties.name = filename; newProperties.name_known = true; } else { newProperties.name = name; newProperties.name_known = true; } //_dbg( "Path of %d is: %s", pid, pathbuf ); return true; }
const char * get_process_name_for_pid (pid_t pid) { char tmp_name[PATH_MAX]; if (proc_pidpath (pid, tmp_name, sizeof (tmp_name)) == 0) { printf ("Could not find process with pid of %d\n", (int) pid); exit (1); } if (strrchr (tmp_name, '/')) return strdup (strrchr (tmp_name, '/') + 1); else return strdup (tmp_name); }
/* works for Linux and Mac OS X */ void abspath(char * path, int sz) { path[0] = '\0'; #ifdef __MACH__ int iret = proc_pidpath(getpid(), path, sz-1); if (iret <= 0) { fprintf(stderr,"PID %d: proc_pid();\n",getpid()); fprintf(stderr," %s:\n", strerror(errno)); } #else readlink("/proc/self/exe",path,sz-1); #endif }
pid_t get_pid_for_process_name (const char *procname) { int process_count = proc_listpids (PROC_ALL_PIDS, 0, NULL, 0) / sizeof (pid_t); if (process_count < 1) { printf ("Only found %d processes running!\n", process_count); exit (1); } // Allocate a few extra slots in case new processes are spawned int all_pids_size = sizeof (pid_t) * (process_count + 3); pid_t *all_pids = (pid_t *) malloc (all_pids_size); // re-set process_count in case the number of processes changed (got smaller; we won't do bigger) process_count = proc_listpids (PROC_ALL_PIDS, 0, all_pids, all_pids_size) / sizeof (pid_t); int i; pid_t highest_pid = 0; int match_count = 0; for (i = 1; i < process_count; i++) { char pidpath[PATH_MAX]; int pidpath_len = proc_pidpath (all_pids[i], pidpath, sizeof (pidpath)); if (pidpath_len == 0) continue; char *j = strrchr (pidpath, '/'); if ((j == NULL && strcmp (procname, pidpath) == 0) || (j != NULL && strcmp (j + 1, procname) == 0)) { match_count++; if (all_pids[i] > highest_pid) highest_pid = all_pids[i]; } } free (all_pids); if (match_count == 0) { printf ("Did not find process '%s'.\n", procname); exit (1); } if (match_count > 1) { printf ("Warning: More than one process '%s'!\n", procname); printf (" defaulting to the highest-pid one, %d\n", highest_pid); } return highest_pid; }
R_API char *r_sys_pid_to_path(int pid) { #if __WINDOWS__ HANDLE psapi = LoadLibrary ("psapi.dll"); if (!psapi) { eprintf ("Error getting the handle to psapi.dll\n"); return NULL; } gpifn = GetProcAddress (psapi, "GetProcessImageFileNameA"); if (!gpifn) { eprintf ("Error getting the address of GetProcessImageFileNameA\n"); return NULL; } HANDLE handle = NULL; TCHAR filename[MAX_PATH]; handle = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); if (handle != NULL) { if (gpifn (handle, filename, MAX_PATH) == 0) { eprintf("Error calling GetProcessImageFileNameA\n"); CloseHandle (handle); return NULL; } else { CloseHandle (handle); return strdup (filename); } } return NULL; #elif __APPLE__ char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; pathbuf[0] = 0; int ret = proc_pidpath (pid, pathbuf, sizeof (pathbuf)); if (ret <= 0) return NULL; return strdup (pathbuf); #else int ret; char buf[128], pathbuf[1024]; #if __FreeBSD__ snprintf (buf, sizeof (buf), "/proc/%d/file", pid); #else snprintf (buf, sizeof (buf), "/proc/%d/exe", pid); #endif ret = readlink (buf, pathbuf, sizeof (pathbuf)-1); if (ret<1) return NULL; pathbuf[ret] = 0; return strdup (pathbuf); #endif }
static std::string getProcPath(int pid) { char path[PROC_PIDPATHINFO_MAXSIZE] = {0}; int bufsize = proc_pidpath(pid, path, sizeof(path)); if (bufsize <= 0) { if (getuid() == 0) { QueryData memory_map; genProcessMemoryMap(pid, memory_map, true); if (memory_map.size() > 0) { return memory_map[0]["path"]; } } path[0] = '\0'; } return std::string(path); }
static void mono_init_merp (const intptr_t crashed_pid, const char *signal, MonoStackHash *hashes, MERPStruct *merp, const char *version) { g_assert (mono_merp_enabled ()); // If these aren't set, icall wasn't made // don't do merp? / don't set the variable to use merp; g_assert (config.appBundleID); g_assert (config.appVersion); merp->bundleIDArg = config.appSignature; merp->versionArg = config.appVersion; merp->archArg = get_merp_arch (); merp->exceptionArg = parse_exception_type (signal); merp->serviceNameArg = config.appBundleID; // FIXME: Not really a posix way to associated a process with a single executable // path? Linux gets bogged down in /proc merp->servicePathArg = NULL; if (crashed_pid) { size_t servicePathSize = sizeof (gchar) * 1200; merp->servicePathArg = g_malloc0 (servicePathSize); int result = proc_pidpath (crashed_pid, (void *) merp->servicePathArg, 1200); if (result <= 0) { g_free ((void *) merp->servicePathArg); merp->servicePathArg = NULL; } } merp->moduleName = "Mono Exception"; merp->moduleVersion = version; merp->moduleOffset = 0; ERROR_DECL (error); merp->uiLidArg = ves_icall_System_Threading_Thread_current_lcid (error); mono_error_assert_ok (error); merp->osVersion = os_version_string (); // FIXME: THis is apple-only for now merp->systemManufacturer = "apple"; get_apple_model ((char *) merp->systemModel, sizeof (merp->systemModel)); merp->hashes = *hashes; }
gchar* mono_w32process_get_path (pid_t pid) { #if defined(__mono_ppc__) || !defined(TARGET_OSX) return mono_w32process_get_name (pid); #else gchar buf [PROC_PIDPATHINFO_MAXSIZE]; gint res; res = proc_pidpath (pid, buf, sizeof (buf)); if (res <= 0) return NULL; if (buf [0] == '\0') return NULL; return g_strdup (buf); #endif }
std::string get_current_process_hash() { std::string ret; pid_t pid = getpid(); char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; int pidsuccess = proc_pidpath (pid, pathbuf, sizeof(pathbuf)); if (pidsuccess > 0) { char buf[33]; mg_md5_file(buf, pathbuf); buf[32] = '\0'; ret = buf; } if (ret.length() != 32) { ret = std::string(32, '0'); } return ret; }
ISciterAPI* SAPI( ISciterAPI* ext ) { static ISciterAPI* _api = NULL; if( ext ) _api = ext; if( !_api ) { pid_t pid; char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; char folderpath[PROC_PIDPATHINFO_MAXSIZE]; pid = getpid(); proc_pidpath (pid, pathbuf, sizeof(pathbuf)); realpath(pathbuf, folderpath); *strrchr(folderpath, '/') = '\0'; void* lib_sciter_handle = dlopen(SCITER_DLL_NAME, RTLD_LOCAL|RTLD_LAZY); if( !lib_sciter_handle ) { const char* lookup_paths[] = { "/" SCITER_DLL_NAME, "/../Frameworks/" SCITER_DLL_NAME, // - (bundle folder)/Contents/Frameworks/ "/../../../" SCITER_DLL_NAME // aside of bundle folder (SDK samples) }; for( int n = 0; !lib_sciter_handle && n < 3; ++n ) { char tpath[PROC_PIDPATHINFO_MAXSIZE]; strcpy(tpath,folderpath); strcat(tpath, lookup_paths[n]); lib_sciter_handle = dlopen(tpath, RTLD_LOCAL|RTLD_LAZY); } } if (!lib_sciter_handle) { fprintf(stderr, "[%s] Unable to load library: %s\n", __FILE__, dlerror()); exit(EXIT_FAILURE); } SciterAPI_ptr sapi = (SciterAPI_ptr) dlsym(lib_sciter_handle, "SciterAPI"); if (!sapi) { fprintf(stderr,"[%s] Unable to get symbol: %s\n", __FILE__, dlerror()); exit(EXIT_FAILURE); } _api = sapi(); #if defined(__cplusplus) tiscript::ni( _api->TIScriptAPI() ); #endif } // assert(_api); return _api; }
bool GetKwmFilePath() { bool Result = false; char PathBuf[PROC_PIDPATHINFO_MAXSIZE]; pid_t Pid = getpid(); int Ret = proc_pidpath(Pid, PathBuf, sizeof(PathBuf)); if (Ret > 0) { KWMPath.FilePath = PathBuf; std::size_t Split = KWMPath.FilePath.find_last_of("/\\"); KWMPath.FilePath = KWMPath.FilePath.substr(0, Split); Result = true; } return Result; }
pid_t find_process(char* executable){ int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; size_t length = 0; int err; err = sysctl(name, 3, NULL, &length, NULL, 0); if(err){ perror(NULL); exit(0); } struct kinfo_proc* proc_list = malloc(length+0x10000); err = sysctl(name, 3, proc_list, &length, NULL, 0); if(err){ perror(NULL); exit(0); } int n_procs = length / sizeof(struct kinfo_proc); pid_t pid = 0; for(int i = 0; i < n_procs; i++){ pid = proc_list[i].kp_proc.p_pid; char path[PROC_PIDPATHINFO_MAXSIZE]; err = proc_pidpath(pid, path, sizeof(path)); if(err <= 0){ continue; } char* match = strstr(path, executable); if(match == NULL){ continue; } if((match + strlen(executable)) == (path + strlen(path))){ free(proc_list); return pid; } } free(proc_list); return 0; }
void CoreClrEmbedding::GetPathToBootstrapper(char* pathToBootstrapper, size_t bufferSize) { #ifdef EDGE_PLATFORM_APPLE ssize_t pathLength = proc_pidpath(getpid(), pathToBootstrapper, bufferSize); #elif defined(EDGE_PLATFORM_WINDOWS) DWORD dwBufferSize; SIZETToDWord(bufferSize, &dwBufferSize); size_t pathLength = GetModuleFileName(GetModuleHandle(NULL), pathToBootstrapper, dwBufferSize); #else ssize_t pathLength = readlink("/proc/self/exe", pathToBootstrapper, bufferSize); #endif assert(pathLength > 0); // ensure pathToBootstrapper is null terminated, readlink for example // will not null terminate it. pathToBootstrapper[pathLength] = '\0'; }
const char * kcm_client_get_execpath(kcm_client *client) { if (client->execpath[0] == '\0') { int ret = proc_pidpath(client->pid, client->execpath, sizeof(client->execpath)); if (ret != -1) client->execpath[sizeof(client->execpath) - 1] = '\0'; else { /* failed, lets not try again */ client->execpath[0] = 0x01; client->execpath[1] = 0x0; } } if (client->execpath[0] != '/') return NULL; return client->execpath; }
const char *lwan_get_config_path(char *path_buf, size_t path_buf_len) { char buffer[PATH_MAX]; if (proc_pidpath(getpid(), buffer, sizeof(buffer)) < 0) goto out; char *path = strrchr(buffer, '/'); if (!path) goto out; int ret = snprintf(path_buf, path_buf_len, "%s.conf", path + 1); if (ret < 0 || ret >= (int)path_buf_len) goto out; return path_buf; out: return "lwan.conf"; }
// This function is run once every time // hotkeys.so is recompiled. // // The path to Kwmc is set in KwmcFilePath // To bind a Kwmc command, simply set the // value of KwmcCommandToExecute. // // e.g KwmcCommandToExecute = "window -f prev" void GetKwmcFilePath() { if(!KwmcFilePath.empty()) return; char PathBuf[PROC_PIDPATHINFO_MAXSIZE]; pid_t Pid = getpid(); std::string Path; int Ret = proc_pidpath(Pid, PathBuf, sizeof(PathBuf)); if (Ret > 0) { Path = PathBuf; std::size_t Split = Path.find_last_of("/\\"); Path = Path.substr(0, Split); KwmcFilePath = Path + "/kwmc"; } }
//-------------------------------------------------------------- string BaseModel::getApplicationPath(){ // from http://stackoverflow.com/questions/799679/programatically-retrieving-the-absolute-path-of-an-os-x-command-line-app/1024933#1024933 if(applicationPath == ""){ int ret; pid_t pid; char pathbuf[1024]; pid = getpid(); ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf)); if(ret <= 0){ ofLogError() << "PID " << pid << " proc_pidpath(): " << strerror(errno); }else{ ofLogVerbose() << "proc " << pid << " path: " << pathbuf; } applicationPath = string(pathbuf); vector<string> pathParts = ofSplitString(applicationPath, "/"); applicationName = pathParts[pathParts.size() - 1]; } return applicationPath; }
/** Get the executable name of current process. */ int get_job_progname(char *res, int nres, int pid){ int ans=1; if(pid>0){ char buf[PATH_MAX]; if(proc_pidpath(pid, buf, sizeof(buf))>0){ strncpy(res, buf, nres); res[nres-1]=0; ans=0; } }else{ char path[PATH_MAX],path2[PATH_MAX]; uint32_t size=sizeof(path); if(_NSGetExecutablePath(path,&size)==0){ if(realpath(path,path2)){ strncpy(res, path2, nres); res[nres-1]=0; ans=0; } } } return ans; }
string SQLUtility::getTestRootPath() const { string result; #ifdef __linux__ char pathbuf[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", pathbuf, PATH_MAX); if (count <= 0) EXPECT_TRUE(false) << "readlink /proc/self/exe error: " << strerror(errno); result = string(pathbuf, count); #elif __APPLE__ int ret; pid_t pid; char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; pid = getpid(); ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf)); if (ret <= 0) EXPECT_TRUE(false) << "PID " << pid << ": proc_pidpath () " << strerror(errno); result = string(pathbuf); #endif return splitFilePath(result).path; }