00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "process_executor.h"
00037 #include "process.h"
00038 #include <pthread.h>
00039 #include <signal.h>
00040
00041 using namespace zmm;
00042
00043 ProcessExecutor::ProcessExecutor(String command, Ref<Array<StringBase> > arglist)
00044 {
00045 #define MAX_ARGS 255
00046 char *argv[MAX_ARGS];
00047
00048 argv[0] = command.c_str();
00049 int apos = 0;
00050
00051 for (int i = 0; i < arglist->size(); i++)
00052 {
00053 argv[++apos] = arglist->get(i)->data;
00054 if (apos >= MAX_ARGS-1)
00055 break;
00056 }
00057 argv[++apos] = NULL;
00058
00059 exit_status = 0;
00060
00061 process_id = fork();
00062
00063 switch (process_id)
00064 {
00065 case -1:
00066 throw _Exception(_("Failed to launch process ") + command);
00067
00068 case 0:
00069 sigset_t mask_set;
00070 pthread_sigmask(SIG_SETMASK, &mask_set, NULL);
00071 log_debug("Launching process: %s\n", command.c_str());
00072 execvp(command.c_str(), argv);
00073 default:
00074 break;
00075 }
00076
00077 log_debug("Launched process %s, pid: %d\n", command.c_str(), process_id);
00078 }
00079
00080 bool ProcessExecutor::isAlive()
00081 {
00082 return is_alive(process_id, &exit_status);
00083 }
00084
00085 bool ProcessExecutor::kill()
00086 {
00087 return kill_proc(process_id);
00088 }
00089
00090 int ProcessExecutor::getStatus()
00091 {
00092 is_alive(process_id, &exit_status);
00093 return exit_status;
00094 }
00095
00096 ProcessExecutor::~ProcessExecutor()
00097 {
00098 kill();
00099 }