1 | /* $Id: RTProcIsRunningByName-linux.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTProcIsRunningByName, Linux implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
32 | #include <iprt/process.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/dir.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/stream.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 |
|
---|
40 | #include <unistd.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | RTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
|
---|
44 | {
|
---|
45 | /*
|
---|
46 | * Quick validation.
|
---|
47 | */
|
---|
48 | if (!pszName)
|
---|
49 | return false;
|
---|
50 |
|
---|
51 | bool const fWithPath = RTPathHavePath(pszName);
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Enumerate /proc.
|
---|
55 | */
|
---|
56 | RTDIR hDir;
|
---|
57 | int rc = RTDirOpen(&hDir, "/proc");
|
---|
58 | AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
|
---|
59 | if (RT_SUCCESS(rc))
|
---|
60 | {
|
---|
61 | RTDIRENTRY DirEntry;
|
---|
62 | while (RT_SUCCESS(RTDirRead(hDir, &DirEntry, NULL)))
|
---|
63 | {
|
---|
64 | /*
|
---|
65 | * Filter numeric directory entries only.
|
---|
66 | */
|
---|
67 | if ( ( DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY
|
---|
68 | || DirEntry.enmType == RTDIRENTRYTYPE_UNKNOWN)
|
---|
69 | && RTStrToUInt32(DirEntry.szName) > 0)
|
---|
70 | {
|
---|
71 | /*
|
---|
72 | * Try readlink on exe first since it's more faster and reliable.
|
---|
73 | * Fall back on reading the first line in cmdline if that fails
|
---|
74 | * (access errors typically). cmdline is unreliable as it might
|
---|
75 | * contain whatever the execv caller passes as argv[0].
|
---|
76 | */
|
---|
77 | char szName[RTPATH_MAX];
|
---|
78 | RTStrPrintf(szName, sizeof(szName), "/proc/%s/exe", &DirEntry.szName[0]);
|
---|
79 | char szExe[RTPATH_MAX];
|
---|
80 | int cchLink = readlink(szName, szExe, sizeof(szExe) - 1);
|
---|
81 | if ( cchLink > 0
|
---|
82 | && (size_t)cchLink < sizeof(szExe))
|
---|
83 | {
|
---|
84 | szExe[cchLink] = '\0';
|
---|
85 | rc = VINF_SUCCESS;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | RTStrPrintf(szName, sizeof(szName), "/proc/%s/cmdline", &DirEntry.szName[0]);
|
---|
90 | PRTSTREAM pStream;
|
---|
91 | rc = RTStrmOpen(szName, "r", &pStream);
|
---|
92 | if (RT_SUCCESS(rc))
|
---|
93 | {
|
---|
94 | rc = RTStrmGetLine(pStream, szExe, sizeof(szExe));
|
---|
95 | RTStrmClose(pStream);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | * We are interested on the file name part only.
|
---|
102 | */
|
---|
103 | char const *pszProcName = fWithPath ? szExe : RTPathFilename(szExe);
|
---|
104 | if (RTStrCmp(pszProcName, pszName) == 0)
|
---|
105 | {
|
---|
106 | /* Found it! */
|
---|
107 | RTDirClose(hDir);
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | RTDirClose(hDir);
|
---|
114 | }
|
---|
115 |
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|