1 | /* $Id: RTProcIsRunningByName-linux.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTProcIsRunningByName, Linux implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
31 | #include <iprt/process.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/dir.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 |
|
---|
39 | #include <unistd.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | RTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
|
---|
43 | {
|
---|
44 | /*
|
---|
45 | * Quick validation.
|
---|
46 | */
|
---|
47 | if (!pszName)
|
---|
48 | return false;
|
---|
49 |
|
---|
50 | bool const fWithPath = RTPathHavePath(pszName);
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Enumerate /proc.
|
---|
54 | */
|
---|
55 | PRTDIR pDir;
|
---|
56 | int rc = RTDirOpen(&pDir, "/proc");
|
---|
57 | AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
|
---|
58 | if (RT_SUCCESS(rc))
|
---|
59 | {
|
---|
60 | RTDIRENTRY DirEntry;
|
---|
61 | while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
|
---|
62 | {
|
---|
63 | /*
|
---|
64 | * Filter numeric directory entries only.
|
---|
65 | */
|
---|
66 | if ( DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY
|
---|
67 | && RTStrToUInt32(DirEntry.szName) > 0)
|
---|
68 | {
|
---|
69 | /*
|
---|
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].
|
---|
74 | */
|
---|
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 | }
|
---|
96 | if (RT_SUCCESS(rc))
|
---|
97 | {
|
---|
98 | /*
|
---|
99 | * We are interested on the file name part only.
|
---|
100 | */
|
---|
101 | char const *pszProcName = fWithPath ? szExe : RTPathFilename(szExe);
|
---|
102 | if (RTStrCmp(pszProcName, pszName) == 0)
|
---|
103 | {
|
---|
104 | /* Found it! */
|
---|
105 | RTDirClose(pDir);
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | RTDirClose(pDir);
|
---|
112 | }
|
---|
113 |
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 |
|
---|