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.h"
00037
00038 #include <stdio.h>
00039 #include <unistd.h>
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042 #include <fcntl.h>
00043 #include <signal.h>
00044 #include <sys/wait.h>
00045
00046 #include <string.h>
00047 #include <errno.h>
00048
00049 #include "config_manager.h"
00050
00051 using namespace zmm;
00052
00053 #define BUF_SIZE 256
00054
00055 String run_simple_process(String prog, String param, String input)
00056 {
00057 FILE *file;
00058 int fd;
00059
00060
00061 char temp_in[] = "mt_in_XXXXXX";
00062 char temp_out[] = "mt_out_XXXXXX";
00063
00064 Ref<ConfigManager> cfg = ConfigManager::getInstance();
00065 String input_file = tempName(cfg->getOption(CFG_SERVER_TMPDIR), temp_in);
00066 fd = open(input_file.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
00067 if (fd == -1)
00068 {
00069 log_debug("Failed to open input file %s: %s\n", input_file.c_str(),
00070 strerror(errno));
00071 throw _Exception(_("Failed to open input file ") + input_file +_(" ") +
00072 strerror(errno));
00073 }
00074 ssize_t ret = write(fd, input.c_str(), input.length());
00075 close(fd);
00076 if (ret < input.length())
00077 {
00078
00079 log_debug("Failed to write to %s: %s\n", input.c_str(),
00080 strerror(errno));
00081 throw _Exception(_("Failed to write to ") + input + ": " +
00082 strerror(errno));
00083 }
00084
00085
00086 String output_file = tempName(cfg->getOption(CFG_SERVER_TMPDIR), temp_out);
00087 fd = open(output_file.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
00088 if (fd == -1)
00089 {
00090 log_debug("Failed to open output file %s: %s\n", output_file.c_str(),
00091 strerror(errno));
00092 throw _Exception(_("Failed to open output file ")+ input_file +_(" ") +
00093 strerror(errno));
00094 }
00095 close(fd);
00096
00097
00098 String command = prog + " " + param + " < " + input_file +
00099 " > " + output_file;
00100 log_debug("running %s\n", command.c_str());
00101 int sysret = system(command.c_str());
00102 if (sysret == -1)
00103 {
00104 log_debug("Failed to execute: %s\n", command.c_str());
00105 throw _Exception(_("Failed to execute: ") + command);
00106 }
00107
00108
00109 file = fopen(output_file.c_str(), "r");
00110 if (!file)
00111 {
00112 log_debug("Could not open output file %s: %s\n", output_file.c_str(),
00113 strerror(errno));
00114 throw _Exception(_("Failed to open output file ")+output_file +_(" ") +
00115 strerror(errno));
00116 }
00117 Ref<StringBuffer> output(new StringBuffer());
00118
00119 int bytesRead;
00120 char buf[BUF_SIZE];
00121 while (1)
00122 {
00123 bytesRead = fread(buf, 1, BUF_SIZE, file);
00124 if(bytesRead > 0)
00125 *output << String(buf, bytesRead);
00126 else
00127 break;
00128 }
00129 fclose(file);
00130
00131
00132 unlink(input_file.c_str());
00133 unlink(output_file.c_str());
00134
00135 return output->toString();
00136 }
00137
00138
00139 bool is_alive(pid_t pid, int *status)
00140 {
00141 if (waitpid(pid, status, WNOHANG) == 0)
00142 return true;
00143
00144 return false;
00145 }
00146
00147 bool kill_proc(pid_t kill_pid)
00148 {
00149 if (is_alive(kill_pid))
00150 {
00151 log_debug("KILLING TERM PID: %d\n", kill_pid);
00152 kill(kill_pid, SIGTERM);
00153 sleep(1);
00154 }
00155 else
00156 return true;
00157
00158 if (is_alive(kill_pid))
00159 {
00160 log_debug("KILLING INT PID: %d\n", kill_pid);
00161 kill(kill_pid, SIGINT);
00162 sleep(1);
00163 }
00164 else
00165 return true;
00166
00167 if (is_alive(kill_pid))
00168 {
00169 log_debug("KILLING KILL PID: %d\n", kill_pid);
00170 kill(kill_pid, SIGKILL);
00171 sleep(1);
00172 }
00173 else
00174 return true;
00175
00176 if (is_alive(kill_pid))
00177 return false;
00178
00179 return true;
00180 }