1 | /* $Id: RTProcIsRunningByName-linux.cpp 16999 2009-02-23 09:55:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTProcIsRunningByName, Linux implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/process.h>
|
---|
37 | #include <iprt/dir.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/param.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 |
|
---|
42 | #include <unistd.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | RTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
|
---|
46 | {
|
---|
47 | /*
|
---|
48 | * Quick validation.
|
---|
49 | */
|
---|
50 | if (!pszName)
|
---|
51 | return false;
|
---|
52 |
|
---|
53 | PRTDIR pDir;
|
---|
54 | int rc = RTDirOpen(&pDir, "/proc");
|
---|
55 | AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
|
---|
56 | if (RT_SUCCESS(rc))
|
---|
57 | {
|
---|
58 | RTDIRENTRY DirEntry;
|
---|
59 | while(RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
|
---|
60 | {
|
---|
61 | /*
|
---|
62 | * Filter numeric directory entries only.
|
---|
63 | */
|
---|
64 | if (DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY &&
|
---|
65 | RTStrToUInt32(DirEntry.szName) > 0)
|
---|
66 | {
|
---|
67 | /*
|
---|
68 | * Build the path to the proc exec file of this process.
|
---|
69 | */
|
---|
70 | char *pszPath;
|
---|
71 | RTStrAPrintf(&pszPath, "/proc/%s/exe", DirEntry.szName);
|
---|
72 | /*
|
---|
73 | * Read the file name of the link pointing at.
|
---|
74 | */
|
---|
75 | char szExe[RTPATH_MAX];
|
---|
76 | size_t cchExe = sizeof(szExe)-1;
|
---|
77 | int cchLink = readlink(pszPath, szExe, cchExe);
|
---|
78 | if (cchLink > 0 && (size_t)cchLink <= cchExe)
|
---|
79 | {
|
---|
80 | szExe[cchLink] = '\0';
|
---|
81 | /*
|
---|
82 | * We are interested on the file name part only.
|
---|
83 | */
|
---|
84 | char *pszFilename = RTPathFilename(szExe);
|
---|
85 | if (RTStrCmp(pszFilename, pszName) == 0)
|
---|
86 | {
|
---|
87 | /*
|
---|
88 | * Clean up
|
---|
89 | */
|
---|
90 | RTStrFree(pszPath);
|
---|
91 | RTDirClose(pDir);
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | RTStrFree(pszPath);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | RTDirClose(pDir);
|
---|
99 | }
|
---|
100 |
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 |
|
---|