PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/filesystems/grabfs/GetPID.c

http://macfuse.googlecode.com/
C | 136 lines | 104 code | 29 blank | 3 comment | 34 complexity | 83963e87bdab1f7af42b83b5cad93cf4 MD5 | raw file
  1/*
  2 * From Apple DTS Sample Code.
  3 */
  4
  5#include "GetPID.h"
  6
  7#include <errno.h>
  8#include <string.h>
  9#include <sys/sysctl.h>
 10
 11int
 12GetAllPIDsForProcessName(const char*        ProcessName, 
 13                         pid_t              ArrayOfReturnedPIDs[], 
 14                         const unsigned int NumberOfPossiblePIDsInArray, 
 15                         unsigned int*      NumberOfMatchesFound,
 16                         int*               SysctlError)
 17{
 18    int    mib[6] = { 0,0,0,0,0,0 };
 19    int    SuccessfullyGotProcessInformation;
 20    size_t sizeOfBufferRequired = 0;
 21    int    error = 0;
 22    long   NumberOfRunningProcesses = 0;
 23    int    Counter = 0;
 24    struct kinfo_proc* BSDProcessInformationStructure = NULL;
 25    pid_t  CurrentExaminedProcessPID = 0;
 26    char*  CurrentExaminedProcessName = NULL;
 27
 28    if (ProcessName == NULL) {
 29        return kInvalidArgumentsError;
 30    }
 31
 32    if (ArrayOfReturnedPIDs == NULL) {
 33        return kInvalidArgumentsError;
 34    }
 35
 36    if (NumberOfPossiblePIDsInArray <= 0) {
 37        return kInvalidArgumentsError;
 38    }
 39
 40    if (NumberOfMatchesFound == NULL) {
 41        return kInvalidArgumentsError;
 42    }
 43    
 44    memset(ArrayOfReturnedPIDs, 0, NumberOfPossiblePIDsInArray * sizeof(pid_t));
 45        
 46    *NumberOfMatchesFound = 0;
 47
 48    if (SysctlError != NULL) {
 49        *SysctlError = 0;
 50    }
 51
 52    mib[0] = CTL_KERN;
 53    mib[1] = KERN_PROC;
 54    mib[2] = KERN_PROC_ALL;
 55
 56    SuccessfullyGotProcessInformation = FALSE;
 57    
 58    while (SuccessfullyGotProcessInformation == FALSE) {
 59        error = sysctl(mib, 3, NULL, &sizeOfBufferRequired, NULL, NULL);
 60        if (error != 0) {
 61            if (SysctlError != NULL) {
 62                *SysctlError = errno;
 63            } 
 64            return kErrorGettingSizeOfBufferRequired;
 65        }
 66    
 67        BSDProcessInformationStructure =
 68            (struct kinfo_proc*)malloc(sizeOfBufferRequired);
 69
 70        if (BSDProcessInformationStructure == NULL) {
 71            if (SysctlError != NULL) {
 72                *SysctlError = ENOMEM;
 73            } 
 74
 75            return kUnableToAllocateMemoryForBuffer;
 76        }
 77    
 78        error = sysctl(mib, 3, BSDProcessInformationStructure,
 79                       &sizeOfBufferRequired, NULL, NULL);
 80    
 81        if (error == 0) {
 82            SuccessfullyGotProcessInformation = TRUE;
 83        } else {
 84            free(BSDProcessInformationStructure); 
 85        }
 86    }
 87
 88    NumberOfRunningProcesses = sizeOfBufferRequired / sizeof(struct kinfo_proc);  
 89    for (Counter = 0; Counter < NumberOfRunningProcesses; Counter++) {
 90        CurrentExaminedProcessPID =
 91            BSDProcessInformationStructure[Counter].kp_proc.p_pid; 
 92    
 93        CurrentExaminedProcessName =
 94            BSDProcessInformationStructure[Counter].kp_proc.p_comm; 
 95        
 96        if ((CurrentExaminedProcessPID > 0) &&
 97            ((strncmp(CurrentExaminedProcessName, ProcessName, MAXCOMLEN) == 0))
 98           ) {	
 99
100            if ((*NumberOfMatchesFound + 1) > NumberOfPossiblePIDsInArray) {
101                free(BSDProcessInformationStructure);
102                return(kPIDBufferOverrunError);
103            }
104        
105            ArrayOfReturnedPIDs[*NumberOfMatchesFound] =
106                CurrentExaminedProcessPID;
107            
108            *NumberOfMatchesFound = *NumberOfMatchesFound + 1;
109        }
110    }
111
112    free(BSDProcessInformationStructure);
113
114    if (*NumberOfMatchesFound == 0) {
115        return kCouldNotFindRequestedProcess;
116    } else {
117        return kSuccess;
118    }
119}
120
121int
122GetPIDForProcessName(const char* ProcessName)
123{
124    pid_t        PIDArray[1] = { 0 };
125    int          Error = 0;
126    unsigned int NumberOfMatches = 0;  
127
128    Error = GetAllPIDsForProcessName(ProcessName, PIDArray, 1,
129                                     &NumberOfMatches, NULL);
130    
131    if ((Error == 0) && (NumberOfMatches == 1)) {
132        return (int)PIDArray[0];
133    } else {
134        return -1;
135    }
136}