VirtualBox

Changeset 17016 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Feb 23, 2009 12:50:07 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
43214
Message:

RTProcIsRunningByName: Try /proc/N/exe first and /proc/N/cmdline is unreliable content wise.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/linux/RTProcIsRunningByName-linux.cpp

    r17011 r17016  
    4141#include <iprt/assert.h>
    4242
     43#include <unistd.h>
     44
    4345
    4446RTR3DECL(bool) RTProcIsRunningByName(const char *pszName)
     
    6668            {
    6769                /*
    68                  * The first line of the 'cmdline' file is the argv[0] passed to
    69                  * execv, which is what we're interested in... (Alternatively we
    70                  * could try readlink 'exe'. Check what happens to set-uid procs.)
     70                 * Try readlink on exe first since it's more faster and reliable.
     71                 * Fall back on reading the first line in cmdline if that fails
     72                 * (access errors typically). cmdline is unreliable as it might
     73                 * contain whatever the execv caller passes as argv[0].
    7174                 */
    72                 PRTSTREAM pStream;
    73                 rc = RTStrmOpenF("r", &pStream, "/proc/%s/cmdline", &DirEntry.szName[0]);
     75                char szName[RTPATH_MAX];
     76                RTStrPrintf(szName, sizeof(szName), "/proc/%s/exe", &DirEntry.szName[0]);
     77                char szExe[RTPATH_MAX];
     78                int cchLink = readlink(szName, szExe, sizeof(szExe) - 1);
     79                if (    cchLink > 0
     80                    &&  (size_t)cchLink < sizeof(szExe))
     81                {
     82                    szExe[cchLink] = '\0';
     83                    rc = VINF_SUCCESS;
     84                }
     85                else
     86                {
     87                    RTStrPrintf(szName, sizeof(szName), "/proc/%s/cmdline", &DirEntry.szName[0]);
     88                    PRTSTREAM pStream;
     89                    rc = RTStrmOpen(szName, "r", &pStream);
     90                    if (RT_SUCCESS(rc))
     91                    {
     92                        rc = RTStrmGetLine(pStream, szExe, sizeof(szExe));
     93                        RTStrmClose(pStream);
     94                    }
     95                }
    7496                if (RT_SUCCESS(rc))
    7597                {
    76                     char szLine[RTPATH_MAX];
    77                     rc = RTStrmGetLine(pStream, szLine, sizeof(szLine));
    78                     RTStrmClose(pStream);
    79                     if (    RT_SUCCESS(rc)
    80                         ||  rc == VERR_BUFFER_OVERFLOW)
     98                    /*
     99                     * We are interested on the file name part only.
     100                     */
     101                    char const *pszFilename = RTPathFilename(szExe);
     102                    if (RTStrCmp(pszFilename, pszName) == 0)
    81103                    {
    82                         /*
    83                          * We are interested on the file name part only.
    84                          */
    85                         char const *pszFilename = RTPathFilename(szLine);
    86                         if (RTStrCmp(pszFilename, pszName) == 0)
    87                         {
    88                             /* Found it! */
    89                             RTDirClose(pDir);
    90                             return true;
    91                         }
     104                        /* Found it! */
     105                        RTDirClose(pDir);
     106                        return true;
    92107                    }
    93108                }
Note: See TracChangeset for help on using the changeset viewer.

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