RTProcIsRunningByName-linux.cpp revision c7814cf6e1240a519cbec0441e033d0e2470ed00
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync/* $Id$ */
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync/** @file
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * IPRT - RTProcIsRunningByName, Linux implementation.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync */
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync/*
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * Copyright (C) 2009-2010 Oracle Corporation
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * available from http://www.virtualbox.org. This file is free software;
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * General Public License (GPL) as published by the Free Software
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * The contents of this file may alternatively be used under the terms
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * of the Common Development and Distribution License Version 1.0
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * VirtualBox OSE distribution, in which case the provisions of the
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * CDDL are applicable instead of those of the GPL.
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync *
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * You may elect to license modified versions of this file under the
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * terms and conditions of either the GPL or the CDDL or both.
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync */
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync/*******************************************************************************
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync* Header Files *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync*******************************************************************************/
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync#define LOG_GROUP RTLOGGROUP_PROCESS
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync#include <iprt/process.h>
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync#include <iprt/string.h>
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync#include <iprt/dir.h>
#include <iprt/path.h>
#include <iprt/stream.h>
#include <iprt/param.h>
#include <iprt/assert.h>
#include <unistd.h>
RTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
{
/*
* Quick validation.
*/
if (!pszName)
return false;
bool const fWithPath = RTPathHavePath(pszName);
/*
* Enumerate /proc.
*/
PRTDIR pDir;
int rc = RTDirOpen(&pDir, "/proc");
AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
if (RT_SUCCESS(rc))
{
RTDIRENTRY DirEntry;
while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
{
/*
* Filter numeric directory entries only.
*/
if ( DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY
&& RTStrToUInt32(DirEntry.szName) > 0)
{
/*
* Try readlink on exe first since it's more faster and reliable.
* Fall back on reading the first line in cmdline if that fails
* (access errors typically). cmdline is unreliable as it might
* contain whatever the execv caller passes as argv[0].
*/
char szName[RTPATH_MAX];
RTStrPrintf(szName, sizeof(szName), "/proc/%s/exe", &DirEntry.szName[0]);
char szExe[RTPATH_MAX];
int cchLink = readlink(szName, szExe, sizeof(szExe) - 1);
if ( cchLink > 0
&& (size_t)cchLink < sizeof(szExe))
{
szExe[cchLink] = '\0';
rc = VINF_SUCCESS;
}
else
{
RTStrPrintf(szName, sizeof(szName), "/proc/%s/cmdline", &DirEntry.szName[0]);
PRTSTREAM pStream;
rc = RTStrmOpen(szName, "r", &pStream);
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStream, szExe, sizeof(szExe));
RTStrmClose(pStream);
}
}
if (RT_SUCCESS(rc))
{
/*
* We are interested on the file name part only.
*/
char const *pszProcName = fWithPath ? szExe : RTPathFilename(szExe);
if (RTStrCmp(pszProcName, pszName) == 0)
{
/* Found it! */
RTDirClose(pDir);
return true;
}
}
}
}
RTDirClose(pDir);
}
return false;
}