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 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <errno.h>
00029 #include <string.h>
00030 #include <limits.h>
00031 #include <unistd.h>
00032 #include <sys/types.h>
00033 #include <sys/wait.h>
00034
00035 #include "testrunnerlite.h"
00036 #include "executor.h"
00037 #include "testdefinitionprocessor.h"
00038 #include "remote_executor.h"
00039 #include "log.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #define SSHCMD "/usr/bin/ssh"
00064 #define SSHCMDARGS "-o StrictHostKeyChecking=no",\
00065 "-o PasswordAuthentication=no"
00066 #define SSHCMDARGS_STR "-o StrictHostKeyChecking=no " \
00067 "-o PasswordAuthentication=no"
00068
00069
00070 LOCAL char *unique_id = NULL;
00071
00072
00073 #define UNIQUE_ID_FMT "%d"
00074 #define PID_FILE_FMT "/var/tmp/testrunner-lite-%s.%d.pid"
00075 #define UNIQUE_ID_MAX_LEN (HOST_NAME_MAX + 10 + 1 + 1)
00076 #define PID_FILE_MAX_LEN (30 + UNIQUE_ID_MAX_LEN + 10 + 1 + 1)
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00097 void ssh_executor_init (const char *hostname)
00098 {
00099 int ret, status;
00100 pid_t pid;
00101 char *cmd = "echo '#!/bin/sh' > /tmp/mypid.sh;"
00102 " echo 'echo $PPID' >> /tmp/mypid.sh; chmod +x /tmp/mypid.sh";
00103 unique_id = (char *)malloc (UNIQUE_ID_MAX_LEN);
00104 ret = gethostname(unique_id, HOST_NAME_MAX);
00105 if (ret) {
00106 LOG_MSG(LOG_ERR, "Failed to get host name: %s",
00107 strerror (errno));
00108 strcpy (unique_id, "foo");
00109 }
00110 sprintf (unique_id + strlen(unique_id), UNIQUE_ID_FMT, getpid());
00111
00112 LOG_MSG(LOG_DEBUG, "unique_id set to %s", unique_id);
00113
00114 pid = fork();
00115 if (pid > 0) {
00116 waitpid(pid, &status, 0);
00117 return;
00118 }
00119 if (pid < 0)
00120 return;
00121
00122
00123 ret = execl(SSHCMD, SSHCMD, SSHCMDARGS, hostname,
00124 cmd, (char*)NULL);
00125
00126 }
00127
00133 int ssh_execute (const char *hostname, const char *command)
00134 {
00135 int ret;
00136 char *cmd;
00137 char *casename;
00138 char *setname;
00139 int stepnum;
00140
00141
00142
00143
00144
00145 casename = current_case_name();
00146 stepnum = current_step_num();
00147 setname = current_set_name();
00148
00149 cmd = (char *)malloc (PID_FILE_MAX_LEN + 130 + strlen (command)
00150 + strlen (casename) + strlen (setname));
00151 if (!cmd) {
00152 fprintf (stderr, "%s: could not allocate memory for "
00153 "command %s\n", __FUNCTION__, command);
00154 }
00155 if (strlen (casename) && strlen (setname))
00156 sprintf (cmd, "logger set:%s-case:%s-step:%d;/tmp/mypid.sh > "
00157 PID_FILE_FMT
00158 ";source .profile > /dev/null; %s",
00159 setname, casename, stepnum, unique_id, getpid(), command);
00160 else
00161 sprintf (cmd, "/tmp/mypid.sh > "
00162 PID_FILE_FMT
00163 ";source .profile > /dev/null; %s",
00164 unique_id, getpid(), command);
00165
00166
00167 ret = execl(SSHCMD, SSHCMD, SSHCMDARGS, hostname,
00168 cmd, (char*)NULL);
00169
00170
00171 return ret;
00172 }
00173
00178 int ssh_check_conn (const char *hostname)
00179 {
00180 int ret;
00181 char cmd[1024];
00182
00183 sprintf (cmd, "%s %s %s echo foo", SSHCMD, SSHCMDARGS_STR, hostname);
00184 ret = system (cmd);
00185 return ret;
00186 }
00187
00192 int ssh_kill (const char *hostname, pid_t id)
00193 {
00194 int ret;
00195 pid_t pid;
00196 char cmd [PID_FILE_MAX_LEN * 3 + 80];
00197 char file [PID_FILE_MAX_LEN];
00198 int status;
00199
00200 pid = fork();
00201 if (pid > 0) {
00202 waitpid(pid, &status, 0);
00203 return status;
00204 }
00205 if (pid < 0)
00206 return 1;
00207
00208 sprintf(file, PID_FILE_FMT, unique_id, id);
00209 sprintf (cmd, "[ -f %1$s ] && pkill -9 -P $(cat %1$s); rm -f %1$s",
00210 file);
00211
00212 ret = execl(SSHCMD, SSHCMD, SSHCMDARGS, hostname,
00213 cmd, (char*)NULL);
00214
00215 return ret;
00216 }
00217
00222 void ssh_clean (const char *hostname, pid_t id)
00223 {
00224 int ret, status;
00225 pid_t pid;
00226 char cmd [PID_FILE_MAX_LEN + 80];
00227
00228 sprintf (cmd, "rm -f " PID_FILE_FMT, unique_id, id);
00229
00230 pid = fork();
00231
00232 if (pid) {
00233 waitpid(pid, &status, 0);
00234 return;
00235 }
00236
00237
00238 ret = execl(SSHCMD, SSHCMD, SSHCMDARGS, hostname,
00239 cmd, (char*)NULL);
00240
00241 return;
00242 }
00243
00247 void ssh_executor_close (const char *hostname)
00248 {
00249 free (unique_id);
00250 return;
00251 }
00252
00253
00254
00255
00256
00257