VirtualBox

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

Last change on this file since 17003 was 17003, checked in by vboxsync, 16 years ago

iprt: Reading the exe link doesn't work on root processes. So use the content of the cmdline file instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1/* $Id: RTProcIsRunningByName-linux.cpp 17003 2009-02-23 11:04:55Z 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/stream.h>
40#include <iprt/param.h>
41#include <iprt/assert.h>
42
43#include <unistd.h>
44
45
46RTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
47{
48 /*
49 * Quick validation.
50 */
51 if (!pszName)
52 return false;
53
54 PRTDIR pDir;
55 int rc = RTDirOpen(&pDir, "/proc");
56 AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
57 if (RT_SUCCESS(rc))
58 {
59 RTDIRENTRY DirEntry;
60 while(RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
61 {
62 /*
63 * Filter numeric directory entries only.
64 */
65 if (DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY &&
66 RTStrToUInt32(DirEntry.szName) > 0)
67 {
68 /*
69 * Build the path to the proc cmdline file of this process.
70 */
71 char *pszPath;
72 RTStrAPrintf(&pszPath, "/proc/%s/cmdline", DirEntry.szName);
73 PRTSTREAM pStream;
74 rc = RTStrmOpen(pszPath, "r", &pStream);
75 if(RT_SUCCESS(rc))
76 {
77 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)
87 {
88 /*
89 * Clean up
90 */
91 RTStrmClose(pStream);
92 RTStrFree(pszPath);
93 RTDirClose(pDir);
94 return true;
95 }
96 RTStrmClose(pStream);
97 }
98 RTStrFree(pszPath);
99 }
100 }
101 RTDirClose(pDir);
102 }
103
104 return false;
105}
106
Note: See TracBrowser for help on using the repository browser.

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