VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/RTProcIsRunningByName-linux.cpp@ 62584

Last change on this file since 62584 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/* $Id: RTProcIsRunningByName-linux.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - RTProcIsRunningByName, Linux implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2016 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
43RTR3DECL(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 PRTDIR pDir;
57 int rc = RTDirOpen(&pDir, "/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(pDir, &DirEntry, NULL)))
63 {
64 /*
65 * Filter numeric directory entries only.
66 */
67 if ( DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY
68 && RTStrToUInt32(DirEntry.szName) > 0)
69 {
70 /*
71 * Try readlink on exe first since it's more faster and reliable.
72 * Fall back on reading the first line in cmdline if that fails
73 * (access errors typically). cmdline is unreliable as it might
74 * contain whatever the execv caller passes as argv[0].
75 */
76 char szName[RTPATH_MAX];
77 RTStrPrintf(szName, sizeof(szName), "/proc/%s/exe", &DirEntry.szName[0]);
78 char szExe[RTPATH_MAX];
79 int cchLink = readlink(szName, szExe, sizeof(szExe) - 1);
80 if ( cchLink > 0
81 && (size_t)cchLink < sizeof(szExe))
82 {
83 szExe[cchLink] = '\0';
84 rc = VINF_SUCCESS;
85 }
86 else
87 {
88 RTStrPrintf(szName, sizeof(szName), "/proc/%s/cmdline", &DirEntry.szName[0]);
89 PRTSTREAM pStream;
90 rc = RTStrmOpen(szName, "r", &pStream);
91 if (RT_SUCCESS(rc))
92 {
93 rc = RTStrmGetLine(pStream, szExe, sizeof(szExe));
94 RTStrmClose(pStream);
95 }
96 }
97 if (RT_SUCCESS(rc))
98 {
99 /*
100 * We are interested on the file name part only.
101 */
102 char const *pszProcName = fWithPath ? szExe : RTPathFilename(szExe);
103 if (RTStrCmp(pszProcName, pszName) == 0)
104 {
105 /* Found it! */
106 RTDirClose(pDir);
107 return true;
108 }
109 }
110 }
111 }
112 RTDirClose(pDir);
113 }
114
115 return false;
116}
117
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette