~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to lib/procMgr/procMgrPosix.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-10-23 15:32:00 UTC
  • mfrom: (1.1.2 upstream) (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20081023153200-gc1bfx89hj35c799
Tags: 2008.10.10-123053-2
* Correcting typo in dh_installinit call.
* Downgrading depends on module-assistant to recommends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
295
295
      numRead = read(cmdFd, cmdLineTemp, sizeof cmdLineTemp - sizeof(char));
296
296
      close(cmdFd);
297
297
 
298
 
      if (1 > numRead) {
299
 
         continue;
300
 
      }
301
 
      for (replaceLoop = 0 ; replaceLoop < (numRead - 1) ; replaceLoop++) {
302
 
         if ('\0' == cmdLineTemp[replaceLoop]) {
303
 
            cmdLineTemp[replaceLoop] = ' ';
 
298
      if (numRead > 0) {
 
299
         for (replaceLoop = 0 ; replaceLoop < (numRead - 1) ; replaceLoop++) {
 
300
            if ('\0' == cmdLineTemp[replaceLoop]) {
 
301
               cmdLineTemp[replaceLoop] = ' ';
 
302
            }
 
303
         }
 
304
      } else {
 
305
         /*
 
306
          * Some procs don't have a command line text, so read a name from
 
307
          * the 'status' file (should be the first line). If unable to get a name,
 
308
          * the process is still real, so it should be included in the list, just 
 
309
          * without a name.
 
310
          */
 
311
         cmdFd = -1;
 
312
         numRead = 0;
 
313
 
 
314
         if (snprintf(cmdFilePath,
 
315
                      sizeof cmdFilePath,
 
316
                      "/proc/%s/status",
 
317
                      ent->d_name) != -1) {
 
318
            cmdFd = open(cmdFilePath, O_RDONLY);
 
319
         }
 
320
         if (cmdFd != -1) {
 
321
            numRead = read(cmdFd, cmdLineTemp, sizeof(cmdLineTemp) - sizeof(char));
 
322
            close(cmdFd);
 
323
 
 
324
            if (numRead < 0) {
 
325
               cmdLineTemp[0] = '\0';
 
326
            } else {
 
327
               cmdLineTemp[numRead] = '\0';
 
328
            }
 
329
         }
 
330
         if (numRead > 0) {
 
331
            /* 
 
332
             * Extract the part with just the name, by reading until the first
 
333
             * space, then reading the next non-space word after that, and
 
334
             * ignoring everything else. The format looks like this:
 
335
             *     "^Name:[ \t]*(.*)$"
 
336
             * for example:
 
337
             *     "Name:    nfsd"
 
338
             */
 
339
            const char* nameStart = NULL;
 
340
            char* copyItr = NULL;
 
341
 
 
342
            /* Skip non-whitespace. */
 
343
            for (nameStart = cmdLineTemp; *nameStart && 
 
344
                                          *nameStart != ' ' &&
 
345
                                          *nameStart != '\t' &&
 
346
                                          *nameStart != '\n'; ++nameStart);
 
347
            /* Skip whitespace. */
 
348
            for (;*nameStart && 
 
349
                  (*nameStart == ' ' ||
 
350
                   *nameStart == '\t' ||
 
351
                   *nameStart == '\n'); ++nameStart);
 
352
            /* Copy the name to the start of the string and null term it. */
 
353
            for (copyItr = cmdLineTemp; *nameStart && *nameStart != '\n';) {
 
354
               *(copyItr++) = *(nameStart++);
 
355
            }
 
356
            *copyItr = '\0';
304
357
         }
305
358
      }
306
359
 
313
366
       * bytes from /proc/#/cmdline -- we left just enough space to add
314
367
       * NUL termination at the end.
315
368
       */
316
 
      cmdLineTemp[numRead] = '\0';
 
369
      if (numRead < 0) {
 
370
         cmdLineTemp[0] = '\0';
 
371
      } else {
 
372
         cmdLineTemp[numRead] = '\0';
 
373
      }
317
374
 
318
375
      /*
319
376
       * Get the inode information for this process.  This gives us
1333
1390
      goto failure;
1334
1391
   }
1335
1392
   // now user
1336
 
   ret = setresuid(ppw->pw_gid, ppw->pw_gid, 0);
 
1393
   ret = setresuid(ppw->pw_uid, ppw->pw_uid, 0);
1337
1394
   if (ret < 0) {
1338
1395
      Warning("Failed to setresuid() for user %s\n", user);
1339
1396
      goto failure;
1389
1446
   }
1390
1447
 
1391
1448
   // first change back user
1392
 
   ret = setresuid(ppw->pw_gid, ppw->pw_gid, 0);
 
1449
   ret = setresuid(ppw->pw_uid, ppw->pw_uid, 0);
1393
1450
   if (ret < 0) {
1394
1451
      Warning("Failed to setresuid() for root\n");
1395
1452
      return FALSE;