/* Add a process to the list =============================================== */ pid_t Downhill_Process_Add(HANDLE process_Handle) { int process_Index; /* Make sure the table is there */ if (downhill_Process_Init() == FALSE) { return -1; } /* Run through each pid */ for (process_Index = 0;process_Index < DOWNHILL_PROCESS_MAX; process_Index++) { /* If we find an empty slot (or they're re-adding the pid), add the info */ if ((downhill_Process_Info->pid[process_Index] == -1) || (downhill_Process_Info->pid[process_Index] == (pid_t)process_Handle)) { downhill_Process_Info->pid[process_Index] = (pid_t)process_Handle; downhill_Process_Info->sigged[process_Index] = FALSE; return (pid_t)process_Handle; } } return 0; }
/* Get the status of any process =========================================== */ pid_t downhill_Process_GetStatusAny(int* process_Status) { int loop_Index; static int process_Index = 0; pid_t process_Id; /* Make sure the info exists */ if (downhill_Process_Init() == FALSE) { return -1; } /* Check any processes */ for (loop_Index = 0;loop_Index < DOWNHILL_PROCESS_MAX;loop_Index++) { /* Check this process */ if (downhill_Process_Info->pid[process_Index] != -1) { process_Id = downhill_Process_GetStatus( downhill_Process_Info->pid[process_Index], process_Status); /* Handle how things came out */ if (process_Id == -1) { downhill_Process_Info->pid[process_Index] = -1; } else if (process_Id > 0) { return process_Id; } } process_Index++; if (process_Index == DOWNHILL_PROCESS_MAX) { process_Index = 0; } } return 0; }
/* Count the number of active children ===================================== */ static int downhill_Process_CountChildren(void) { int process_Index; int child_Count = 0; /* Make sure we're inited */ if (downhill_Process_Init() == FALSE) { return 0; } /* Count the number of active children */ for (process_Index = 0;process_Index < DOWNHILL_PROCESS_MAX; process_Index++) { if (downhill_Process_Info->pid[process_Index] != -1) { child_Count++; } } /* And return it */ return child_Count; }
/* Run a program in the background ========================================= */ static HANDLE downhill_Process_Forkexec(char* exec_Name,char* exec_Argv[], HANDLE file_Handle[],DWORD exec_Flags) { char exec_Command[ARG_MAX]; STARTUPINFO exec_Startup; PROCESS_INFORMATION exec_Info; int exec_Index; int exec_Inherit = FALSE; /* Make sure the table is there */ if (downhill_Process_Init() == FALSE) { return INVALID_HANDLE_VALUE; } /* Build the command */ strcpy(exec_Command,exec_Name); if (exec_Argv != NULL) { for (exec_Index = 1;exec_Argv[exec_Index] != NULL;exec_Index++) { strcat(exec_Command," "); strcat(exec_Command,exec_Argv[exec_Index]); } } /* Run the command */ memset(&exec_Startup,0,sizeof(STARTUPINFO)); exec_Startup.cb = sizeof(STARTUPINFO); if (file_Handle != NULL) { if (file_Handle[0] != INVALID_HANDLE_VALUE) { exec_Startup.hStdInput = file_Handle[0]; exec_Inherit = TRUE; exec_Startup.dwFlags = STARTF_USESTDHANDLES; } if (file_Handle[1] != INVALID_HANDLE_VALUE) { exec_Startup.hStdOutput = file_Handle[1]; exec_Inherit = TRUE; exec_Startup.dwFlags = STARTF_USESTDHANDLES; } if (file_Handle[2] != INVALID_HANDLE_VALUE) { exec_Startup.hStdError = file_Handle[2]; exec_Inherit = TRUE; exec_Startup.dwFlags = STARTF_USESTDHANDLES; } } if (!CreateProcess(NULL,exec_Command,NULL,NULL,exec_Inherit,exec_Flags, NULL,NULL,&exec_Startup,&exec_Info)) { return INVALID_HANDLE_VALUE; } /* Clean up the thread */ CloseHandle(exec_Info.hThread); /* Return the result */ return exec_Info.hProcess; }
/* Run a program in the background ========================================= */ static HANDLE downhill_Process_Forkexec(char* exec_Name, char* exec_Argv[], HANDLE file_Handle[], DWORD exec_Flags, char* exec_Env[]) { char exec_Command[DOWNHILL_ARG_MAX]; STARTUPINFO exec_Startup; PROCESS_INFORMATION exec_Info; int exec_Index; int exec_Inherit = FALSE; char *exec_Envp = NULL; /* Make sure the table is there */ if (downhill_Process_Init() == FALSE) { return INVALID_HANDLE_VALUE; } /* Build the command */ strcpy(exec_Command, exec_Name); if (exec_Argv != NULL) { /* * If we were asked to, check every arguement for spaces * and place single quotes around it if there are. */ for (exec_Index = 1; exec_Argv[exec_Index] != NULL; exec_Index++) { BOOLEAN end_quote = FALSE; if (strchr(exec_Argv[exec_Index], ' ') != NULL) { strcat(exec_Command," '"); end_quote = TRUE; } else strcat(exec_Command," "); strcat(exec_Command,exec_Argv[exec_Index]); if (end_quote == TRUE) strcat(exec_Command,"'"); } } /* Run the command */ memset(&exec_Startup,0,sizeof(STARTUPINFO)); exec_Startup.cb = sizeof(STARTUPINFO); if (file_Handle != NULL) { if (file_Handle[0] != INVALID_HANDLE_VALUE) { exec_Startup.hStdInput = file_Handle[0]; exec_Inherit = TRUE; exec_Startup.dwFlags = STARTF_USESTDHANDLES; } if (file_Handle[1] != INVALID_HANDLE_VALUE) { exec_Startup.hStdOutput = file_Handle[1]; exec_Inherit = TRUE; exec_Startup.dwFlags = STARTF_USESTDHANDLES; } if (file_Handle[2] != INVALID_HANDLE_VALUE) { exec_Startup.hStdError = file_Handle[2]; exec_Inherit = TRUE; exec_Startup.dwFlags = STARTF_USESTDHANDLES; } } /* PW need to make the environment a list of null terminated * strings. */ if (exec_Env != NULL) { char **envp, *tmpp; int length = 1; for (envp = exec_Env; *envp != NULL; envp++) { length += (int)strlen(*envp) + 1; } if ((exec_Envp = (char *)_alloca(length)) != NULL) { for (envp = exec_Env, tmpp=exec_Envp; *envp != NULL; envp++) { strcpy(tmpp, *envp); tmpp += strlen(*envp) + 1; } *tmpp = '\0'; } } if (!CreateProcess(NULL, /* application name */ exec_Command, /* command line string */ NULL, /* process security attribs */ NULL, /* thread security attribs */ exec_Inherit, /* handle inheritance flag */ exec_Flags, /* creation flags */ exec_Envp, /* new environment block */ NULL, /* current directory name */ &exec_Startup, /* startup information */ &exec_Info)) /* process information */ { return INVALID_HANDLE_VALUE; } /* Clean up the thread */ CloseHandle(exec_Info.hThread); /* Return the result */ return exec_Info.hProcess; }