3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * IPRT - RTProcIsRunningByName, Linux implementation.
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2009-2010 Oracle Corporation
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * available from http://www.virtualbox.org. This file is free software;
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * you can redistribute it and/or modify it under the terms of the GNU
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * General Public License (GPL) as published by the Free Software
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * The contents of this file may alternatively be used under the terms
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * of the Common Development and Distribution License Version 1.0
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * VirtualBox OSE distribution, in which case the provisions of the
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * CDDL are applicable instead of those of the GPL.
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * You may elect to license modified versions of this file under the
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * terms and conditions of either the GPL or the CDDL or both.
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync/*******************************************************************************
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync* Header Files *
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync*******************************************************************************/
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsyncRTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * Quick validation.
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync return false;
bcf07bde9af0195f280432a83a1370c2c5424347vboxsync * Enumerate /proc.
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync AssertMsgRCReturn(rc, ("RTDirOpen on /proc failed: rc=%Rrc\n", rc), false);
bdff0e400f2c47a55235db3f869058538d54afa6vboxsync while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync * Filter numeric directory entries only.
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync * Try readlink on exe first since it's more faster and reliable.
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync * Fall back on reading the first line in cmdline if that fails
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync * (access errors typically). cmdline is unreliable as it might
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync * contain whatever the execv caller passes as argv[0].
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync RTStrPrintf(szName, sizeof(szName), "/proc/%s/exe", &DirEntry.szName[0]);
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync int cchLink = readlink(szName, szExe, sizeof(szExe) - 1);
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync RTStrPrintf(szName, sizeof(szName), "/proc/%s/cmdline", &DirEntry.szName[0]);
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync * We are interested on the file name part only.
bcf07bde9af0195f280432a83a1370c2c5424347vboxsync char const *pszProcName = fWithPath ? szExe : RTPathFilename(szExe);
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync /* Found it! */
7c9b13ed27d21ab52e83783b784fb41ebac52872vboxsync return true;
3cc33d1e7558a0f4a04b59bfe914a24d0b9a2329vboxsync return false;