~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to lib/procMgr/procMgrPosix.c

  • Committer: Package Import Robot
  • Author(s): Nate Muench
  • Date: 2012-06-20 15:59:51 UTC
  • mfrom: (1.4.8)
  • Revision ID: package-import@ubuntu.com-20120620155951-6rupmpb0f70b52zr
Tags: 2012.05.21-724730-0ubuntu1
* Merging upstream version 2012.05.21-724730.
  - Fixes building against the current Quantal kernel. (LP: #1000344)
  - Fixes Quantal installation issues. (LP: #1019031)

* Sync with Debian
  - Updating to debhelper version 9.
  - Updating to standards version 3.9.3.
  - Updating copyright file machine-readable format version 1.0.
  - Building without multiarch paths for now

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
#include "dynarray.h"
82
82
#include "su.h"
83
83
#include "str.h"
 
84
#include "strutil.h"
84
85
#include "fileIO.h"
85
86
#include "codeset.h"
86
87
#include "unicode.h"
126
127
 
127
128
#if defined(__APPLE__)
128
129
static int ProcMgrGetCommandLineArgs(long pid,
129
 
                                     DynBuf *argsBuf);
 
130
                                     DynBuf *argsBuf,
 
131
                                     char **procCmdName);
130
132
#endif
131
133
 
132
134
#ifdef sun
192
194
 *----------------------------------------------------------------------
193
195
 */
194
196
 
195
 
#if !defined(sun) && !defined(__FreeBSD__) && !defined(__APPLE__)
 
197
#if defined(linux)
196
198
int
197
199
ProcMgr_ReadProcFile(int fd,                       // IN
198
200
                     char **contents)              // OUT
246
248
   return size;
247
249
}
248
250
 
249
 
#endif   // !sun && !FreeBSD && !APPLE
250
251
 
251
252
/*
252
253
 *----------------------------------------------------------------------
266
267
 *----------------------------------------------------------------------
267
268
 */
268
269
 
269
 
#if defined(linux)
270
270
ProcMgrProcInfoArray *
271
271
ProcMgr_ListProcesses(void)
272
272
{
281
281
 
282
282
   procList = Util_SafeCalloc(1, sizeof *procList);
283
283
   ProcMgrProcInfoArray_Init(procList, 0);
284
 
   procInfo.procCmd = NULL;
 
284
   procInfo.procCmdName = NULL;
 
285
   procInfo.procCmdLine = NULL;
285
286
   procInfo.procOwner = NULL;
286
287
 
287
288
   /*
355
356
      unsigned long long dummy;
356
357
      unsigned long long relativeStartTime;
357
358
      char *stringBegin;
 
359
      char *cmdNameBegin;
 
360
      Bool cmdNameLookup = TRUE;
358
361
 
359
362
      /*
360
363
       * We only care about dirs that look like processes.
406
409
          */
407
410
         for (replaceLoop = 0 ; replaceLoop < (numRead - 1) ; replaceLoop++) {
408
411
            if ('\0' == cmdLineTemp[replaceLoop]) {
 
412
               if (cmdNameLookup) {
 
413
                  /*
 
414
                   * Store the command name.
 
415
                   * Find the last path separator, to get the cmd name.
 
416
                   * If no separator is found, then use the whole name.
 
417
                   */
 
418
                  cmdNameBegin = strrchr(cmdLineTemp, '/');
 
419
                  if (NULL == cmdNameBegin) {
 
420
                     cmdNameBegin = cmdLineTemp;
 
421
                  } else {
 
422
                     /*
 
423
                      * Skip over the last separator.
 
424
                      */
 
425
                     cmdNameBegin++;
 
426
                  }
 
427
                  procInfo.procCmdName = Unicode_Alloc(cmdNameBegin, STRING_ENCODING_DEFAULT);
 
428
                  cmdNameLookup = FALSE;
 
429
               }
409
430
               cmdLineTemp[replaceLoop] = ' ';
410
431
            }
411
432
         }
456
477
               *(copyItr++) = *(nameStart++);
457
478
            }
458
479
            *copyItr = '\0';
 
480
            /*
 
481
             * Store the command name.
 
482
             */
 
483
            procInfo.procCmdName = Unicode_Alloc(cmdLineTemp, STRING_ENCODING_DEFAULT);
459
484
         }
460
485
      }
461
486
 
529
554
       * Store the command line string pointer in dynbuf.
530
555
       */
531
556
      if (cmdLineTemp) {
532
 
         procInfo.procCmd = Unicode_Alloc(cmdLineTemp, STRING_ENCODING_DEFAULT);
 
557
         procInfo.procCmdLine = Unicode_Alloc(cmdLineTemp, STRING_ENCODING_DEFAULT);
533
558
      } else {
534
 
         procInfo.procCmd = Unicode_Alloc("", STRING_ENCODING_UTF8);
 
559
         procInfo.procCmdLine = Unicode_Alloc("", STRING_ENCODING_UTF8);
535
560
      }
536
561
 
537
562
      /*
560
585
                 __FUNCTION__);
561
586
         goto abort;
562
587
      }
563
 
      procInfo.procCmd = NULL;
 
588
      procInfo.procCmdName = NULL;
 
589
      procInfo.procCmdLine = NULL;
564
590
      procInfo.procOwner = NULL;
565
591
 
566
592
next_entry:
575
601
abort:
576
602
   closedir(dir);
577
603
 
578
 
   free(procInfo.procCmd);
 
604
   free(procInfo.procCmdName);
 
605
   free(procInfo.procCmdLine);
579
606
   free(procInfo.procOwner);
580
607
 
581
608
   if (failed) {
621
648
   int flag=0;
622
649
 
623
650
   procList = Util_SafeCalloc(1, sizeof *procList);
624
 
   procInfo.procCmd = NULL;
 
651
   procInfo.procCmdName = NULL;
 
652
   procInfo.procCmdLine = NULL;
625
653
   procInfo.procOwner = NULL;
626
654
 
627
655
   /*
658
686
   for (i = 0; i < nentries; ++i, ++kp) {
659
687
      struct passwd *pwd;
660
688
      char **cmdLineTemp = NULL;
 
689
      char *cmdNameBegin = NULL;
 
690
      Bool cmdNameLookup = TRUE;
661
691
 
662
692
      /*
663
 
       * Store the pid of the process
 
693
       * Store the pid of the process.
664
694
       */
665
695
      procInfo.procId = kp->ki_pid;
666
696
 
673
703
                           : Unicode_Alloc(pwd->pw_name, STRING_ENCODING_DEFAULT);
674
704
 
675
705
      /*
676
 
       * Store the command line string of the process
 
706
       * If the command name in the kinfo_proc struct is strictly less than the
 
707
       * maximum allowed size, then we can save it right now. Else we shall
 
708
       * need to try and parse it from the entire command line.
 
709
       */
 
710
      if (strlen(kp->ki_comm) + 1 < sizeof kp->ki_comm) {
 
711
         procInfo.procCmdName = Unicode_Alloc(kp->ki_comm, STRING_ENCODING_DEFAULT);
 
712
         cmdNameLookup = FALSE;
 
713
      }
 
714
 
 
715
      /*
 
716
       * Store the command line string of the process.
677
717
       */
678
718
      cmdLineTemp = kvm_getargv(kd, kp, 0);
679
719
      if (cmdLineTemp != NULL) {
680
 
         // flatten the argument list to get cmd & all params
 
720
         /*
 
721
          * Flatten the argument list to get cmd & all params.
 
722
          */
681
723
         DynBuf dbuf;
682
724
 
683
725
         DynBuf_Init(&dbuf);
687
729
                       __FUNCTION__);
688
730
               goto abort;
689
731
            }
 
732
            if (cmdNameLookup) {
 
733
               /*
 
734
                * Store the command name of the process.
 
735
                * Find the last path separator, to get the cmd name.
 
736
                * If no separator is found, then use the whole name.
 
737
                */
 
738
               cmdNameBegin = strrchr(*cmdLineTemp, '/');
 
739
               if (NULL == cmdNameBegin) {
 
740
                  cmdNameBegin = *cmdLineTemp;
 
741
               } else {
 
742
                  /*
 
743
                   * Skip over the last separator.
 
744
                   */
 
745
                  cmdNameBegin++;
 
746
               }
 
747
               procInfo.procCmdName = Unicode_Alloc(cmdNameBegin, STRING_ENCODING_DEFAULT);
 
748
               cmdNameLookup = FALSE;
 
749
            }
690
750
            cmdLineTemp++;
691
751
            if (*cmdLineTemp != NULL) {
692
 
               // add the whitespace between arguments
 
752
               /*
 
753
                * Add the whitespace between arguments.
 
754
                */
693
755
               if (!DynBuf_Append(&dbuf, " ", 1)) {
694
756
                  Warning("%s: failed to append ' ' in DynBuf - no memory\n",
695
757
                          __FUNCTION__);
697
759
               }
698
760
            }
699
761
         }
700
 
         // add the NUL term
 
762
         /*
 
763
          * Add the NUL term.
 
764
          */
701
765
         if (!DynBuf_Append(&dbuf, "", 1)) {
702
766
            Warning("%s: failed to append NUL in DynBuf - out of memory\n",
703
767
                    __FUNCTION__);
704
768
            goto abort;
705
769
         }
706
770
         DynBuf_Trim(&dbuf);
707
 
         procInfo.procCmd = DynBuf_Detach(&dbuf);
 
771
         procInfo.procCmdLine = DynBuf_Detach(&dbuf);
708
772
         DynBuf_Destroy(&dbuf);
709
773
      } else {
710
 
         procInfo.procCmd = Unicode_Alloc(kp->ki_comm, STRING_ENCODING_DEFAULT);
 
774
         procInfo.procCmdLine = Unicode_Alloc(kp->ki_comm, STRING_ENCODING_DEFAULT);
 
775
         if (cmdNameLookup) {
 
776
            procInfo.procCmdName = Unicode_Alloc(kp->ki_comm, STRING_ENCODING_DEFAULT);
 
777
            cmdNameLookup = FALSE;
 
778
         }
711
779
      }
712
780
 
713
781
      /*
719
787
       * Store the process info pointer into a list buffer.
720
788
       */
721
789
      *ProcMgrProcInfoArray_AddressOf(procList, i) = procInfo;
722
 
      procInfo.procCmd = NULL;
 
790
      procInfo.procCmdLine = NULL;
 
791
      procInfo.procCmdName = NULL;
723
792
      procInfo.procOwner = NULL;
724
793
 
725
794
   } // for nentries
731
800
      kvm_close(kd);
732
801
   }
733
802
 
734
 
   free(procInfo.procCmd);
 
803
   free(procInfo.procCmdLine);
 
804
   free(procInfo.procCmdName);
735
805
   free(procInfo.procOwner);
736
806
 
737
807
   if (failed) {
756
826
 * Results:
757
827
 *      Number of arguments retrieved.
758
828
 *      Buffer is returned with argument names, if any.
 
829
 *      Command name is returned.
759
830
 *
760
831
 * Side effects:
761
832
 *
764
835
 
765
836
static int
766
837
ProcMgrGetCommandLineArgs(long pid,               // IN:  process id
767
 
                          DynBuf *argsBuf)        // OUT: Buffer with arguments
 
838
                          DynBuf *argsBuf,        // OUT: Buffer with arguments
 
839
                          char **procCmdName)     // OUT: Command name string
768
840
{
769
841
   int argCount = 0;
770
842
   int argNum;
771
843
   char *argUnicode = NULL;
 
844
   char *cmdNameBegin = NULL;
772
845
   char *cmdLineRaw = NULL;
773
846
   char *cmdLineTemp;
774
847
   char *cmdLineEnd;
776
849
   size_t maxargsSize;
777
850
   int maxargsName[] = {CTL_KERN, KERN_ARGMAX};
778
851
   int argName[] = {CTL_KERN, KERN_PROCARGS2, pid};
 
852
   Bool cmdNameLookup = TRUE;
 
853
   Bool failed = TRUE;
 
854
 
 
855
   if (NULL != procCmdName) {
 
856
      *procCmdName = NULL;
 
857
   }
779
858
 
780
859
   /*
781
860
    * Get the sysctl kern argmax.
876
955
            break;
877
956
         }
878
957
         ++argCount;
 
958
         /*
 
959
          * If this is the command name, retrieve it.
 
960
          */
 
961
         if (NULL != procCmdName && cmdNameLookup) {
 
962
            /*
 
963
             * Store the command name of the process.
 
964
             * Find the last path separator, to get the cmd name.
 
965
             * If no separator is found, then use the whole name.
 
966
             */
 
967
            cmdNameBegin = strrchr(cmdLineTemp, '/');
 
968
            if (NULL == cmdNameBegin) {
 
969
               cmdNameBegin = cmdLineTemp;
 
970
            } else {
 
971
               /*
 
972
                * Skip over the last separator.
 
973
                */
 
974
               cmdNameBegin++;
 
975
            }
 
976
            *procCmdName = Unicode_Alloc(cmdNameBegin, STRING_ENCODING_DEFAULT);
 
977
            cmdNameLookup = FALSE;
 
978
         }
879
979
      }
880
980
      /*
881
981
       * Skip over the current arg that we just saved.
895
995
   }
896
996
   DynBuf_Trim(argsBuf);
897
997
 
 
998
   failed = FALSE;
898
999
abort:
899
1000
   free(cmdLineRaw);
900
1001
   free(argUnicode);
 
1002
   if (failed) {
 
1003
      if (NULL != procCmdName) {
 
1004
         free(*procCmdName);
 
1005
         *procCmdName = NULL;
 
1006
      }
 
1007
      argCount = 0;
 
1008
   }
901
1009
 
902
1010
   return argCount;
903
1011
}
934
1042
   int procName[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
935
1043
 
936
1044
   procList = Util_SafeCalloc(1, sizeof *procList);
937
 
   procInfo.procCmd = NULL;
 
1045
   procInfo.procCmdLine = NULL;
 
1046
   procInfo.procCmdName = NULL;
938
1047
   procInfo.procOwner = NULL;
939
1048
 
940
1049
   /*
946
1055
      goto abort;
947
1056
   }
948
1057
   nentries = (int)(procsize / sizeof *kp);
 
1058
 
949
1059
   /*
950
1060
    * Get the list of process info structs.
951
1061
    */
955
1065
               __FUNCTION__, errno);
956
1066
      goto abort;
957
1067
   }
 
1068
 
958
1069
   /*
959
1070
    * Recalculate the number of entries as they may have changed.
960
1071
    */
961
1072
   if (0 >= (nentries = (int)(procsize / sizeof *kp))) {
962
1073
      goto abort;
963
1074
   }
 
1075
 
964
1076
   /*
965
1077
    * Pre-allocate the dynamic array of required size.
966
1078
    */
969
1081
              __FUNCTION__);
970
1082
      goto abort;
971
1083
   }
 
1084
 
972
1085
   /*
973
1086
    * Iterate through the list of process entries
974
1087
    */
978
1091
      struct passwd pw;
979
1092
      struct passwd *ppw = &pw;
980
1093
      int error;
 
1094
      int argCount = 0;
 
1095
      Bool cmdNameLookup = TRUE;
981
1096
 
982
1097
      /*
983
1098
       * Store the pid of the process
984
1099
       */
985
1100
      procInfo.procId = kptmp->kp_proc.p_pid;
 
1101
 
986
1102
      /*
987
1103
       * Store the owner of the process.
988
1104
       */
993
1109
                           : Unicode_Alloc(ppw->pw_name, STRING_ENCODING_DEFAULT);
994
1110
 
995
1111
      /*
 
1112
       * If the command name in the kinfo_proc struct is strictly less than the
 
1113
       * maximum allowed size, then we can save it right now. Else we shall
 
1114
       * need to try and parse it from the entire command line.
 
1115
       */
 
1116
      if (strlen(kptmp->kp_proc.p_comm) + 1 < sizeof kptmp->kp_proc.p_comm) {
 
1117
         procInfo.procCmdName = Unicode_Alloc(kptmp->kp_proc.p_comm, STRING_ENCODING_DEFAULT);
 
1118
         cmdNameLookup = FALSE;
 
1119
      }
 
1120
 
 
1121
      /*
996
1122
       * Store the command line arguments of the process.
997
1123
       * If no arguments are found, use the full command name.
998
1124
       */
999
1125
      DynBuf_Init(&argsBuf);
1000
 
      if (ProcMgrGetCommandLineArgs(kptmp->kp_proc.p_pid, &argsBuf) > 0) {
1001
 
         procInfo.procCmd = DynBuf_Detach(&argsBuf);
1002
 
      } else {
1003
 
         procInfo.procCmd = Unicode_Alloc(kptmp->kp_proc.p_comm, STRING_ENCODING_DEFAULT);
 
1126
      if (cmdNameLookup) {
 
1127
         argCount = ProcMgrGetCommandLineArgs(kptmp->kp_proc.p_pid, &argsBuf, &procInfo.procCmdName);
 
1128
      } else {
 
1129
         argCount = ProcMgrGetCommandLineArgs(kptmp->kp_proc.p_pid, &argsBuf, NULL);
 
1130
      }
 
1131
      if (0 < argCount) {
 
1132
         procInfo.procCmdLine = DynBuf_Detach(&argsBuf);
 
1133
         /*
 
1134
          * cmdName would have been filled up by the function ProcMgrGetCommandLineArgs().
 
1135
          */
 
1136
         cmdNameLookup = FALSE;
 
1137
      } else {
 
1138
         procInfo.procCmdLine = Unicode_Alloc(kptmp->kp_proc.p_comm, STRING_ENCODING_DEFAULT);
 
1139
         if (cmdNameLookup) {
 
1140
            procInfo.procCmdName = Unicode_Alloc(kptmp->kp_proc.p_comm, STRING_ENCODING_DEFAULT);
 
1141
            cmdNameLookup = FALSE;
 
1142
         }
1004
1143
      }
1005
1144
      DynBuf_Destroy(&argsBuf);
 
1145
 
1006
1146
      /*
1007
1147
       * Store the start time of the process
1008
1148
       */
1009
1149
      procInfo.procStartTime = kptmp->kp_proc.p_starttime.tv_sec;
 
1150
 
1010
1151
      /*
1011
1152
       * Store the process info pointer into a list buffer.
1012
1153
       */
1013
1154
      *ProcMgrProcInfoArray_AddressOf(procList, i) = procInfo;
1014
 
      procInfo.procCmd = NULL;
 
1155
 
 
1156
      procInfo.procCmdLine = NULL;
 
1157
      procInfo.procCmdName = NULL;
1015
1158
      procInfo.procOwner = NULL;
1016
1159
 
1017
1160
   } // nentries
1020
1163
 
1021
1164
abort:
1022
1165
   free(kp);
1023
 
   free(procInfo.procCmd);
 
1166
   free(procInfo.procCmdLine);
 
1167
   free(procInfo.procCmdName);
1024
1168
   free(procInfo.procOwner);
1025
1169
 
1026
1170
   if (failed) {
1060
1204
 
1061
1205
   procCount = ProcMgrProcInfoArray_Count(procList);
1062
1206
   for (i = 0; i < procCount; i++) {
1063
 
      ProcMgrProcInfo *procInfo;
1064
 
 
1065
 
      procInfo = ProcMgrProcInfoArray_AddressOf(procList, i);
1066
 
      free(procInfo->procCmd);
 
1207
      ProcMgrProcInfo *procInfo = ProcMgrProcInfoArray_AddressOf(procList, i);
 
1208
      free(procInfo->procCmdName);
 
1209
      free(procInfo->procCmdLine);
1067
1210
      free(procInfo->procOwner);
1068
1211
   }
1069
1212