儲存庫 vbox 的更動 17011
- 時間撮記:
- 2009-2-23 下午12:27:38 (16 年 以前)
- 位置:
- trunk/src/VBox/Runtime
- 檔案:
-
- 修改 3 筆資料
圖例:
- 未更動
- 新增
- 刪除
-
trunk/src/VBox/Runtime/generic/RTProcIsRunningByName-generic.cpp
r16999 r17011 32 32 * Header Files * 33 33 *******************************************************************************/ 34 #include <iprt/string.h>35 34 #include <iprt/process.h> 36 35 -
trunk/src/VBox/Runtime/r3/linux/RTProcIsRunningByName-linux.cpp
r17003 r17011 33 33 *******************************************************************************/ 34 34 #define LOG_GROUP RTLOGGROUP_PROCESS 35 #include <iprt/process.h> 35 36 #include <iprt/string.h> 36 #include <iprt/process.h>37 37 #include <iprt/dir.h> 38 38 #include <iprt/path.h> … … 40 40 #include <iprt/param.h> 41 41 #include <iprt/assert.h> 42 43 #include <unistd.h>44 42 45 43 … … 52 50 return false; 53 51 52 bool fFoundIt = false; 54 53 PRTDIR pDir; 55 54 int rc = RTDirOpen(&pDir, "/proc"); … … 58 57 { 59 58 RTDIRENTRY DirEntry; 60 while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))59 while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL))) 61 60 { 62 61 /* 63 62 * Filter numeric directory entries only. 64 63 */ 65 if ( DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY &&66 RTStrToUInt32(DirEntry.szName) > 0)64 if ( DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY 65 && RTStrToUInt32(DirEntry.szName) > 0) 67 66 { 68 67 /* 69 * Build the path to the proc cmdline file of this process. 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 71 */ 71 char *pszPath;72 RTStrAPrintf(&pszPath, "/proc/%s/cmdline", DirEntry.szName);73 72 PRTSTREAM pStream; 74 rc = RTStrmOpen (pszPath, "r", &pStream);75 if (RT_SUCCESS(rc))73 rc = RTStrmOpenF("r", &pStream, "/proc/%s/cmdline", &DirEntry.szName[0]); 74 if (RT_SUCCESS(rc)) 76 75 { 77 76 char szLine[RTPATH_MAX]; 78 /* 79 * The fist line should be the application path always. 80 */ 81 RTStrmGetLine(pStream, szLine, sizeof(szLine)); 82 /* 83 * We are interested on the file name part only. 84 */ 85 char *pszFilename = RTPathFilename(szLine); 86 if (RTStrCmp(pszFilename, pszName) == 0) 77 rc = RTStrmGetLine(pStream, szLine, sizeof(szLine)); 78 RTStrmClose(pStream); 79 if ( RT_SUCCESS(rc) 80 || rc == VERR_BUFFER_OVERFLOW) 87 81 { 88 82 /* 89 * Clean up83 * We are interested on the file name part only. 90 84 */ 91 RTStrmClose(pStream); 92 RTStrFree(pszPath); 93 RTDirClose(pDir); 94 return true; 85 char const *pszFilename = RTPathFilename(szLine); 86 if (RTStrCmp(pszFilename, pszName) == 0) 87 { 88 /* Found it! */ 89 RTDirClose(pDir); 90 return true; 91 } 95 92 } 96 RTStrmClose(pStream);97 93 } 98 RTStrFree(pszPath);99 94 } 100 95 } -
trunk/src/VBox/Runtime/testcase/tstRTProcIsRunningByName.cpp
r16999 r17011 29 29 */ 30 30 31 32 31 /******************************************************************************* 33 32 * Header Files * 34 33 *******************************************************************************/ 34 #include <iprt/process.h> 35 35 #include <iprt/initterm.h> 36 36 #include <iprt/stream.h> 37 37 #include <iprt/err.h> 38 #include <iprt/process.h>39 38 #include <iprt/param.h> 40 39 #include <iprt/path.h> … … 44 43 int main(int argc, char **argv) 45 44 { 45 int cErrors = 0; 46 46 47 RTR3Init(); 48 RTPrintf("tstRTPRocIsRunningByName: TESTING...\n"); 47 49 50 /* 51 * Test 1: Check for a definitely not running process. 52 */ 48 53 int rc = VERR_GENERAL_FAILURE; 49 50 54 char szExecPath[RTPATH_MAX] = { "vbox-5b05e1ff-6ae2-4d10-885a-7d25018c4c5b" }; 51 /* Check for a definitely not running process. */52 RTPrintf("tstRTProcIsRunningByName: Searching for a not running process with the name %s...\n", szExecPath);53 55 bool fRunning = RTProcIsRunningByName(szExecPath); 54 56 if (!fRunning) 57 RTPrintf("tstRTProcIsRunningByName: Process '%s' is not running (expected).\n", szExecPath); 58 else 55 59 { 56 RTPrintf("tstRTProcIsRunningByName: Success!\n"); 57 rc = VINF_SUCCESS; 60 RTPrintf("tstRTProcIsRunningByName: FAILURE - '%s' is running! (test 1)\n", szExecPath); 61 cErrors++; 62 } 63 64 /* 65 * Test 2: Check for our own process. 66 */ 67 if (RTProcGetExecutableName(szExecPath, RTPATH_MAX)) 68 { 69 /* Strip any path components */ 70 char *pszFilename = RTPathFilename(szExecPath); 71 if (pszFilename) 72 { 73 bool fRunning = RTProcIsRunningByName(pszFilename); 74 if (fRunning) 75 RTPrintf("tstRTProcIsRunningByName: Process '%s' (self) is running\n", pszFilename); 76 else 77 { 78 RTPrintf("tstRTProcIsRunningByName: FAILURE - Process '%s' (self) is not running!\n", pszFilename); 79 cErrors++; 80 } 81 } 82 else 83 { 84 RTPrintf("tstRTProcIsRunningByName: FAILURE - RTPathFilename failed!\n"); 85 cErrors++; 86 } 58 87 } 59 88 else 60 RTPrintf("tstRTProcIsRunningByName: Expected failure but got success!\n");61 /* If the first test succeeded, check for a running process. For that we62 * use the name of this testcase. */63 if (RT_SUCCESS(rc))64 89 { 65 /* Reset */ 66 rc = VERR_GENERAL_FAILURE; 67 if (RTProcGetExecutableName(szExecPath, RTPATH_MAX)) 68 { 69 /* Strip any path components */ 70 char *pszFilename; 71 if ((pszFilename = RTPathFilename(szExecPath))) 72 { 73 RTPrintf("tstRTProcIsRunningByName: Searching for a running process with the name %s...\n", pszFilename); 74 bool fRunning = RTProcIsRunningByName(pszFilename); 75 if (fRunning) 76 { 77 RTPrintf("tstRTProcIsRunningByName: Success!\n"); 78 rc = VINF_SUCCESS; 79 } 80 else 81 RTPrintf("tstRTProcIsRunningByName: Expected success but got failure!\n"); 82 } 83 else 84 RTPrintf("tstRTProcIsRunningByName: RTPathFilename failed!\n", rc); 85 } 86 else 87 RTPrintf("tstRTProcIsRunningByName: RTProcGetExecutableName failed!\n", rc); 90 RTPrintf("tstRTProcIsRunningByName: FAILURE - RTProcGetExecutableName failed!\n"); 91 cErrors++; 88 92 } 89 93 90 return RT_SUCCESS(rc) ? 0 : 1; 94 /* 95 * Summary. 96 */ 97 if (!cErrors) 98 RTPrintf("tstRTProcIsRunningByName: SUCCESS\n"); 99 else 100 RTPrintf("tstRTProcIsRunningByName: FAILURE - %d errors\n", cErrors); 101 return cErrors ? 1 : 0; 91 102 }
注意:
瀏覽 TracChangeset
來幫助您使用更動檢視器