RTProcIsRunningByName-linux.cpp revision bdff0e400f2c47a55235db3f869058538d54afa6
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - RTProcIsRunningByName, Linux implementation.
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
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.
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 * 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 * 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* Header Files *
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync*******************************************************************************/
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsyncRTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * Quick validation.
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync return false;
1e40f57c72c881067b0314f898e1004211bb7650vboxsync bool fFoundIt = false;
1e40f57c72c881067b0314f898e1004211bb7650vboxsync AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * Filter numeric directory entries only.
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 rc = RTStrmOpenF("r", &pStream, "/proc/%s/cmdline", &DirEntry.szName[0]);
1e40f57c72c881067b0314f898e1004211bb7650vboxsync rc = RTStrmGetLine(pStream, szLine, sizeof(szLine));
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync * We are interested on the file name part only.
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync /* Found it! */
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync return true;
443b47b0e450919f5012681d9ec4cdccc1ebab08vboxsync return false;