RTProcIsRunningByName-linux.cpp revision bdff0e400f2c47a55235db3f869058538d54afa6
1e40f57c72c881067b0314f898e1004211bb7650vboxsync/* $Id$ */
1e40f57c72c881067b0314f898e1004211bb7650vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - RTProcIsRunningByName, Linux implementation.
1e40f57c72c881067b0314f898e1004211bb7650vboxsync */
1e40f57c72c881067b0314f898e1004211bb7650vboxsync
1e40f57c72c881067b0314f898e1004211bb7650vboxsync/*
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
1e40f57c72c881067b0314f898e1004211bb7650vboxsync *
1e40f57c72c881067b0314f898e1004211bb7650vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1e40f57c72c881067b0314f898e1004211bb7650vboxsync * available from http://www.virtualbox.org. This file is free software;
1e40f57c72c881067b0314f898e1004211bb7650vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1e40f57c72c881067b0314f898e1004211bb7650vboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
1e40f57c72c881067b0314f898e1004211bb7650vboxsync *
1e40f57c72c881067b0314f898e1004211bb7650vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
d460750520be710f3920f12315652fb0ec19ef8fvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1e40f57c72c881067b0314f898e1004211bb7650vboxsync * additional information or have any questions.
1e40f57c72c881067b0314f898e1004211bb7650vboxsync */
1e40f57c72c881067b0314f898e1004211bb7650vboxsync
1e40f57c72c881067b0314f898e1004211bb7650vboxsync/*******************************************************************************
1e40f57c72c881067b0314f898e1004211bb7650vboxsync* Header Files *
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync*******************************************************************************/
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#define LOG_GROUP RTLOGGROUP_PROCESS
1e40f57c72c881067b0314f898e1004211bb7650vboxsync#include <iprt/process.h>
2f0d866e126dd288169fed591c259c1c6b4016e5vboxsync#include <iprt/string.h>
2f0d866e126dd288169fed591c259c1c6b4016e5vboxsync#include <iprt/dir.h>
1e40f57c72c881067b0314f898e1004211bb7650vboxsync#include <iprt/path.h>
1e40f57c72c881067b0314f898e1004211bb7650vboxsync#include <iprt/stream.h>
1e40f57c72c881067b0314f898e1004211bb7650vboxsync#include <iprt/param.h>
1e40f57c72c881067b0314f898e1004211bb7650vboxsync#include <iprt/assert.h>
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsyncRTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync{
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync /*
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * Quick validation.
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync */
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync if (!pszName)
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync return false;
1e40f57c72c881067b0314f898e1004211bb7650vboxsync
1e40f57c72c881067b0314f898e1004211bb7650vboxsync bool fFoundIt = false;
1e40f57c72c881067b0314f898e1004211bb7650vboxsync PRTDIR pDir;
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync int rc = RTDirOpen(&pDir, "/proc");
1e40f57c72c881067b0314f898e1004211bb7650vboxsync AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
1e40f57c72c881067b0314f898e1004211bb7650vboxsync if (RT_SUCCESS(rc))
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync {
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync RTDIRENTRY DirEntry;
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync {
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync /*
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * Filter numeric directory entries only.
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync */
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync if ( DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY
1e40f57c72c881067b0314f898e1004211bb7650vboxsync && RTStrToUInt32(DirEntry.szName) > 0)
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync {
1e40f57c72c881067b0314f898e1004211bb7650vboxsync /*
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * The first line of the 'cmdline' file is the argv[0] passed to
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * execv, which is what we're interested in... (Alternatively we
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync * could try readlink 'exe'. Check what happens to set-uid procs.)
1e40f57c72c881067b0314f898e1004211bb7650vboxsync */
1e40f57c72c881067b0314f898e1004211bb7650vboxsync PRTSTREAM pStream;
1e40f57c72c881067b0314f898e1004211bb7650vboxsync rc = RTStrmOpenF("r", &pStream, "/proc/%s/cmdline", &DirEntry.szName[0]);
1e40f57c72c881067b0314f898e1004211bb7650vboxsync if (RT_SUCCESS(rc))
1e40f57c72c881067b0314f898e1004211bb7650vboxsync {
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync char szLine[RTPATH_MAX];
1e40f57c72c881067b0314f898e1004211bb7650vboxsync rc = RTStrmGetLine(pStream, szLine, sizeof(szLine));
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync RTStrmClose(pStream);
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync if ( RT_SUCCESS(rc)
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync || rc == VERR_BUFFER_OVERFLOW)
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync {
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync /*
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * We are interested on the file name part only.
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync */
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync char const *pszFilename = RTPathFilename(szLine);
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync if (RTStrCmp(pszFilename, pszName) == 0)
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync {
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync /* Found it! */
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync RTDirClose(pDir);
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync return true;
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync }
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync }
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync }
1e40f57c72c881067b0314f898e1004211bb7650vboxsync }
1e40f57c72c881067b0314f898e1004211bb7650vboxsync }
1e40f57c72c881067b0314f898e1004211bb7650vboxsync RTDirClose(pDir);
1e40f57c72c881067b0314f898e1004211bb7650vboxsync }
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync return false;
1e40f57c72c881067b0314f898e1004211bb7650vboxsync}
1e40f57c72c881067b0314f898e1004211bb7650vboxsync
1e40f57c72c881067b0314f898e1004211bb7650vboxsync