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;
407
410
for (replaceLoop = 0 ; replaceLoop < (numRead - 1) ; replaceLoop++) {
408
411
if ('\0' == cmdLineTemp[replaceLoop]) {
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.
418
cmdNameBegin = strrchr(cmdLineTemp, '/');
419
if (NULL == cmdNameBegin) {
420
cmdNameBegin = cmdLineTemp;
423
* Skip over the last separator.
427
procInfo.procCmdName = Unicode_Alloc(cmdNameBegin, STRING_ENCODING_DEFAULT);
428
cmdNameLookup = FALSE;
409
430
cmdLineTemp[replaceLoop] = ' ';
529
554
* Store the command line string pointer in dynbuf.
531
556
if (cmdLineTemp) {
532
procInfo.procCmd = Unicode_Alloc(cmdLineTemp, STRING_ENCODING_DEFAULT);
557
procInfo.procCmdLine = Unicode_Alloc(cmdLineTemp, STRING_ENCODING_DEFAULT);
534
procInfo.procCmd = Unicode_Alloc("", STRING_ENCODING_UTF8);
559
procInfo.procCmdLine = Unicode_Alloc("", STRING_ENCODING_UTF8);
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;
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;
663
* Store the pid of the process
693
* Store the pid of the process.
665
695
procInfo.procId = kp->ki_pid;
673
703
: Unicode_Alloc(pwd->pw_name, STRING_ENCODING_DEFAULT);
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.
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;
716
* Store the command line string of the process.
678
718
cmdLineTemp = kvm_getargv(kd, kp, 0);
679
719
if (cmdLineTemp != NULL) {
680
// flatten the argument list to get cmd & all params
721
* Flatten the argument list to get cmd & all params.
683
725
DynBuf_Init(&dbuf);
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.
738
cmdNameBegin = strrchr(*cmdLineTemp, '/');
739
if (NULL == cmdNameBegin) {
740
cmdNameBegin = *cmdLineTemp;
743
* Skip over the last separator.
747
procInfo.procCmdName = Unicode_Alloc(cmdNameBegin, STRING_ENCODING_DEFAULT);
748
cmdNameLookup = FALSE;
691
751
if (*cmdLineTemp != NULL) {
692
// add the whitespace between arguments
753
* Add the whitespace between arguments.
693
755
if (!DynBuf_Append(&dbuf, " ", 1)) {
694
756
Warning("%s: failed to append ' ' in DynBuf - no memory\n",
701
765
if (!DynBuf_Append(&dbuf, "", 1)) {
702
766
Warning("%s: failed to append NUL in DynBuf - out of memory\n",
706
770
DynBuf_Trim(&dbuf);
707
procInfo.procCmd = DynBuf_Detach(&dbuf);
771
procInfo.procCmdLine = DynBuf_Detach(&dbuf);
708
772
DynBuf_Destroy(&dbuf);
710
procInfo.procCmd = Unicode_Alloc(kp->ki_comm, STRING_ENCODING_DEFAULT);
774
procInfo.procCmdLine = Unicode_Alloc(kp->ki_comm, STRING_ENCODING_DEFAULT);
776
procInfo.procCmdName = Unicode_Alloc(kp->ki_comm, STRING_ENCODING_DEFAULT);
777
cmdNameLookup = FALSE;
719
787
* Store the process info pointer into a list buffer.
721
789
*ProcMgrProcInfoArray_AddressOf(procList, i) = procInfo;
722
procInfo.procCmd = NULL;
790
procInfo.procCmdLine = NULL;
791
procInfo.procCmdName = NULL;
723
792
procInfo.procOwner = NULL;
725
794
} // for nentries
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
769
841
int argCount = 0;
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;
855
if (NULL != procCmdName) {
781
860
* Get the sysctl kern argmax.
959
* If this is the command name, retrieve it.
961
if (NULL != procCmdName && cmdNameLookup) {
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.
967
cmdNameBegin = strrchr(cmdLineTemp, '/');
968
if (NULL == cmdNameBegin) {
969
cmdNameBegin = cmdLineTemp;
972
* Skip over the last separator.
976
*procCmdName = Unicode_Alloc(cmdNameBegin, STRING_ENCODING_DEFAULT);
977
cmdNameLookup = FALSE;
881
981
* Skip over the current arg that we just saved.
934
1042
int procName[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
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;
978
1091
struct passwd pw;
979
1092
struct passwd *ppw = &pw;
1095
Bool cmdNameLookup = TRUE;
983
1098
* Store the pid of the process
985
1100
procInfo.procId = kptmp->kp_proc.p_pid;
987
1103
* Store the owner of the process.
993
1109
: Unicode_Alloc(ppw->pw_name, STRING_ENCODING_DEFAULT);
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.
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;
996
1122
* Store the command line arguments of the process.
997
1123
* If no arguments are found, use the full command name.
999
1125
DynBuf_Init(&argsBuf);
1000
if (ProcMgrGetCommandLineArgs(kptmp->kp_proc.p_pid, &argsBuf) > 0) {
1001
procInfo.procCmd = DynBuf_Detach(&argsBuf);
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);
1129
argCount = ProcMgrGetCommandLineArgs(kptmp->kp_proc.p_pid, &argsBuf, NULL);
1132
procInfo.procCmdLine = DynBuf_Detach(&argsBuf);
1134
* cmdName would have been filled up by the function ProcMgrGetCommandLineArgs().
1136
cmdNameLookup = FALSE;
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;
1005
1144
DynBuf_Destroy(&argsBuf);
1007
1147
* Store the start time of the process
1009
1149
procInfo.procStartTime = kptmp->kp_proc.p_starttime.tv_sec;
1011
1152
* Store the process info pointer into a list buffer.
1013
1154
*ProcMgrProcInfoArray_AddressOf(procList, i) = procInfo;
1014
procInfo.procCmd = NULL;
1156
procInfo.procCmdLine = NULL;
1157
procInfo.procCmdName = NULL;
1015
1158
procInfo.procOwner = NULL;
1061
1205
procCount = ProcMgrProcInfoArray_Count(procList);
1062
1206
for (i = 0; i < procCount; i++) {
1063
ProcMgrProcInfo *procInfo;
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);