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;";
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;"
00157 "sh < /tmp/mypid.sh > "
00158 PID_FILE_FMT
00159 ";source .profile > /dev/null; %s",
00160 setname, casename, stepnum, unique_id, getpid(), command);
00161 else
00162 sprintf (cmd, "sh < /tmp/mypid.sh > "
00163 PID_FILE_FMT
00164 ";source .profile > /dev/null; %s",
00165 unique_id, getpid(), command);
00166
00167
00168 ret = execl(SSHCMD, SSHCMD, SSHCMDARGS, hostname,
00169 cmd, (char*)NULL);
00170
00171
00172 return ret;
00173 }
00174
00179 int ssh_check_conn (const char *hostname)
00180 {
00181 int ret;
00182 char cmd[1024];
00183
00184 sprintf (cmd, "%s %s %s echo foo", SSHCMD, SSHCMDARGS_STR, hostname);
00185 ret = system (cmd);
00186 return ret;
00187 }
00188
00193 int ssh_kill (const char *hostname, pid_t id)
00194 {
00195 int ret;
00196 pid_t pid;
00197 char cmd [PID_FILE_MAX_LEN * 3 + 80];
00198 char file [PID_FILE_MAX_LEN];
00199 int status;
00200
00201 pid = fork();
00202 if (pid > 0) {
00203 waitpid(pid, &status, 0);
00204 return status;
00205 }
00206 if (pid < 0)
00207 return 1;
00208
00209 sprintf(file, PID_FILE_FMT, unique_id, id);
00210 sprintf (cmd, "[ -f %1$s ] && pkill -9 -P $(cat %1$s); rm -f %1$s",
00211 file);
00212
00213 ret = execl(SSHCMD, SSHCMD, SSHCMDARGS, hostname,
00214 cmd, (char*)NULL);
00215
00216 return ret;
00217 }
00218
00223 void ssh_clean (const char *hostname, pid_t id)
00224 {
00225 int ret, status;
00226 pid_t pid;
00227 char cmd [PID_FILE_MAX_LEN + 80];
00228
00229 sprintf (cmd, "rm -f " PID_FILE_FMT, unique_id, id);
00230
00231 pid = fork();
00232
00233 if (pid) {
00234 waitpid(pid, &status, 0);
00235 return;
00236 }
00237
00238
00239 ret = execl(SSHCMD, SSHCMD, SSHCMDARGS, hostname,
00240 cmd, (char*)NULL);
00241
00242 return;
00243 }
00244
00248 void ssh_executor_close (const char *hostname)
00249 {
00250 free (unique_id);
00251 return;
00252 }
00253
00254
00255
00256
00257
00258