VirtualBox

Ignore:
Timestamp:
Jul 22, 2010 2:42:06 PM (15 years ago)
Author:
vboxsync
Message:

VBoxService/VMInfo: Better utmp lookup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo.cpp

    r30966 r30999  
    249249    utmp *ut_user;
    250250    uint32_t cUtmpEntries = 0;
    251 
    252     /* Count all users to allocate a sufficient big array. */
    253     while ((ut_user = getutent()))
    254     {
     251    uint32_t cListSize = 32;
     252
     253        /* Allocate a first array to hold 32 users max. */
     254        char **aUsers = (char**)RTMemAllocZ(cListSize * sizeof(char*));
     255        if (aUsers == NULL)
     256                rc = VERR_NO_MEMORY;
     257
     258        /* Process all entries in the utmp file. */
     259    while (   (ut_user = getutent())
     260           && RT_SUCCESS(rc))
     261    {
     262                VBoxServiceVerbose(1, "bar: %s\n", ut_user->ut_user);
     263               
     264                if (cUtmpEntries > cListSize)
     265                {
     266                        cListSize += 32;
     267                        aUsers = (char**)RTMemRealloc(aUsers, cListSize * sizeof(char*));
     268                        AssertPtrBreakStmt(aUsers, rc = VERR_NO_MEMORY);
     269                }
     270               
    255271        /* Make sure we don't add user names which are not
    256272         * part of type USER_PROCESS. */
    257273        if (ut_user->ut_type == USER_PROCESS)
    258             cUtmpEntries++;
    259     }
    260     /** @todo r=bird: Guess what happens if someone logs in right now.
    261      *        Better do all in a single loop and dynamically resize arrays. */
    262 
    263     if (cUtmpEntries) /* Do we have some users at all? */
    264     {
    265         setutent(); /* Rewind utmp file pointer to start from beginning. */
    266 
    267         /* Build up array with logged in users. */
    268         char **aUsers = (char**)RTMemAlloc(cUtmpEntries * sizeof(char*));
    269         if (aUsers)
    270         {
    271             /* Store user in array. */
    272             while (   (ut_user = getutent())
    273                    && RT_SUCCESS(rc))
    274             {
    275                 if (ut_user->ut_type == USER_PROCESS)
    276                 {
    277                     bool fFound = false;
    278                     for (uint32_t u = 0; u < cUsersInList && !fFound; u++)
    279                         fFound = (strcmp((const char*)aUsers[u], ut_user->ut_user) == 0) ? true : false;
    280 
    281                     if (!fFound)
    282 /** @todo r=bird: RTStrAAppend on an uninitailized variable. Didn't use
    283  *        RTMemAllocZ!. */
    284                         rc = RTStrAAppend(&aUsers[cUsersInList++], (const char*)ut_user->ut_user);
    285                 }
    286             }
    287 
    288             /* Build final user list. */
    289             for (uint32_t u = 0; u < cUsersInList; u++)
    290             {
    291                 if (u > 0)
    292                 {
    293                     rc = RTStrAAppend(&pszUserList, ",");
    294                     AssertRCBreakStmt(rc, RTStrFree(pszUserList));
    295                 }
    296                 rc = RTStrAAppend(&pszUserList, (const char*)aUsers[u]);
    297                 AssertRCBreakStmt(rc, RTStrFree(pszUserList));
    298             }
    299 
    300             /* Cleanup. */
    301             for (uint32_t u = 0; u < cUsersInList; u++)
    302                 RTStrFree(aUsers[u]);
    303             RTMemFree(aUsers);
    304         }
    305         else
    306             rc = VERR_NO_MEMORY;
    307     }
     274        {               
     275                        cUtmpEntries++;
     276                       
     277                        bool fFound = false;
     278                        for (uint32_t u = 0; u < cUsersInList && !fFound; u++)
     279                                fFound = (strcmp((const char*)aUsers[u], ut_user->ut_user) == 0) ? true : false;
     280
     281            if (!fFound)
     282                                rc = RTStrAAppend(&aUsers[cUsersInList++], (const char*)ut_user->ut_user);
     283                }
     284    }
     285   
     286    /* Build final user list. */
     287        for (uint32_t u = 0; u < cUsersInList; u++)
     288        {
     289                if (u > 0)
     290                {
     291                        rc = RTStrAAppend(&pszUserList, ",");
     292                        AssertRCBreakStmt(rc, RTStrFree(pszUserList));
     293                }
     294                rc = RTStrAAppend(&pszUserList, (const char*)aUsers[u]);
     295                AssertRCBreakStmt(rc, RTStrFree(pszUserList));
     296        }
     297
     298        /* Cleanup. */
     299        for (uint32_t u = 0; u < cUsersInList; u++)
     300                RTStrFree(aUsers[u]);
     301        RTMemFree(aUsers); 
     302   
    308303    endutent(); /* Close utmp file. */
    309304#endif
Note: See TracChangeset for help on using the changeset viewer.

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