Changeset 17016 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Feb 23, 2009 12:50:07 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 43214
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/linux/RTProcIsRunningByName-linux.cpp
r17011 r17016 41 41 #include <iprt/assert.h> 42 42 43 #include <unistd.h> 44 43 45 44 46 RTR3DECL(bool) RTProcIsRunningByName(const char *pszName) … … 66 68 { 67 69 /* 68 * The first line of the 'cmdline' file is the argv[0] passed to 69 * execv, which is what we're interested in... (Alternatively we 70 * could try readlink 'exe'. Check what happens to set-uid procs.) 70 * Try readlink on exe first since it's more faster and reliable. 71 * Fall back on reading the first line in cmdline if that fails 72 * (access errors typically). cmdline is unreliable as it might 73 * contain whatever the execv caller passes as argv[0]. 71 74 */ 72 PRTSTREAM pStream; 73 rc = RTStrmOpenF("r", &pStream, "/proc/%s/cmdline", &DirEntry.szName[0]); 75 char szName[RTPATH_MAX]; 76 RTStrPrintf(szName, sizeof(szName), "/proc/%s/exe", &DirEntry.szName[0]); 77 char szExe[RTPATH_MAX]; 78 int cchLink = readlink(szName, szExe, sizeof(szExe) - 1); 79 if ( cchLink > 0 80 && (size_t)cchLink < sizeof(szExe)) 81 { 82 szExe[cchLink] = '\0'; 83 rc = VINF_SUCCESS; 84 } 85 else 86 { 87 RTStrPrintf(szName, sizeof(szName), "/proc/%s/cmdline", &DirEntry.szName[0]); 88 PRTSTREAM pStream; 89 rc = RTStrmOpen(szName, "r", &pStream); 90 if (RT_SUCCESS(rc)) 91 { 92 rc = RTStrmGetLine(pStream, szExe, sizeof(szExe)); 93 RTStrmClose(pStream); 94 } 95 } 74 96 if (RT_SUCCESS(rc)) 75 97 { 76 char szLine[RTPATH_MAX];77 rc = RTStrmGetLine(pStream, szLine, sizeof(szLine));78 RTStrmClose(pStream);79 if ( RT_SUCCESS(rc)80 || rc == VERR_BUFFER_OVERFLOW)98 /* 99 * We are interested on the file name part only. 100 */ 101 char const *pszFilename = RTPathFilename(szExe); 102 if (RTStrCmp(pszFilename, pszName) == 0) 81 103 { 82 /* 83 * We are interested on the file name part only. 84 */ 85 char const *pszFilename = RTPathFilename(szLine); 86 if (RTStrCmp(pszFilename, pszName) == 0) 87 { 88 /* Found it! */ 89 RTDirClose(pDir); 90 return true; 91 } 104 /* Found it! */ 105 RTDirClose(pDir); 106 return true; 92 107 } 93 108 }
Note:
See TracChangeset
for help on using the changeset viewer.