add rstart & keep_running M.Z.08.2001

This commit is contained in:
cvs
2001-08-16 10:26:48 +00:00
parent 0cda158849
commit a538361516
3 changed files with 165 additions and 1 deletions

View File

@ -26,7 +26,7 @@ libtecsl.a: $(CLI_OBJ)
ar cr libtecsl.a $(CLI_OBJ)
ranlib libtecsl.a
all: libtecsl.a bin/TecsServer bin/TecsClient
all: libtecsl.a bin/TecsServer bin/TecsClient rstart keep_running
tecs_plot.o: tecs_plot.f90
f90 -c -u -g tecs_plot.f90

43
tecs/keep_running.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
#define MAX_CNT 10
#define MIN_PERIOD 3600
int main(int argc, char *argv[]) {
int cnt, dif;
time_t tim1, tim2;
pid_t pid;
int status, i;
if (argc<2) return 0;
cnt = MAX_CNT;
while (cnt > 0) {
time(&tim1);
pid=vfork();
if (pid == 0) {
execvp(argv[1], argv+1);
perror("execvp");
exit(1);
}
waitpid(pid, &status, 0);
if (status==0) {
printf("Regular exit\n");
return 0;
}
time(&tim2);
dif = (int) difftime(tim2, tim1);
cnt += dif / MIN_PERIOD;
if (cnt > MAX_CNT) cnt = MAX_CNT;
cnt--;
printf("Status=%d. Died after %d sec", status, dif);
if (cnt > 0) {
printf(" - %d tries left - retry ...\n", cnt);
}
}
printf(" - too many restarts\n");
return -1;
}

121
tecs/rstart.c Normal file
View File

@ -0,0 +1,121 @@
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include "myc_str.h"
int rexec(char **host, int port, char *user, char *passwd, char *command, int *err_file_desc);
int wrtRd(int fd, char *cmd, char *res, int res_len) {
int i, p, l, cnt;
char buf[8192];
struct timeval tmo;
fd_set rmask;
if (cmd[0]!='\0') {
l=send(fd, cmd, strlen(cmd), 0);
if (l<0) return -1;
}
tmo.tv_sec=2;
i=1;
p=0;
while (i>0 && p<sizeof(buf)-1) {
FD_ZERO(&rmask);
FD_SET(fd, &rmask);
tmo.tv_usec=500000;
i=select(fd+1, &rmask, NULL, NULL, &tmo);
tmo.tv_sec=0;
if (i>0) {
l=recv(fd, buf+p, sizeof(buf)-p-1, 0);
if (l<0) return -1;
p=p+l;
if (l==0) i=0;
}
}
if (p >= res_len) p=res_len-1;
buf[p]='\0';
strcpy(res, buf);
return p;
}
int main(int argc, char *argv[]) {
int port, i, j, l, fd;
char *host, inst[32], cmd[256], buf[256], path[256];
char *instrHosts[]={
"DMC","lnsa05.psi.ch",
"TOPSI","lnsa07.psi.ch",
"SANS","lnsa10.psi.ch",
"HRPT","lnsa11.psi.ch",
"TRICS","lnsa13.psi.ch",
"AMOR","lnsa14.psi.ch",
"FOCUS","lnsa16.psi.ch",
"ZOLLIKER","lnsa15.psi.ch"
};
struct servent s;
s=*getservbyname("exec", "tcp");
i=0;
if (argc < 3) {
printf("not enough arguments\n");
return 0;
}
str_upcase(inst, argv[1]);
str_copy(cmd, "\n");
for (i=2; i<argc; i++) {
if (0==strcmp(argv[i], "sics")) {
str_append(cmd, "startsics\n");
} else if (0==strcmp(argv[i], "tecs")) {
str_append(cmd, "startecs\n");
} else {
printf("unknown server %s\n", argv[i]);
return 0;
}
}
buf[0]='\0';
host=NULL;
for (i=0; i<sizeof(instrHosts)/sizeof(instrHosts[0]); i+=2) {
if (0==strcmp(instrHosts[i], inst)) {
host=instrHosts[i+1];
} else if (0==strcmp(instrHosts[i+1], argv[1])) {
host=argv[1];
str_copy(inst,instrHosts[i]);
}
}
if (0==strcmp(inst, "ZOLLIKER")) {
str_copy(buf, "69kilo");
str_copy(inst, "zolliker");
} else if (host==NULL) {
printf("unknown instrument: %s\n", argv[1]);
return 0;
}
printf("login to %s\n", host);
if (buf[0]=='\0') {
str_copy(buf, inst);
str_append(buf, "LNS");
}
fd=rexec(&host, s.s_port, inst, buf, "rlogin 0", &i);
if (fd<0) {
perror("rexec"); return -1;
}
if (wrtRd(fd, "\n", buf, sizeof(buf)) < 0) goto Error;
if (wrtRd(fd, "\n", buf, sizeof(buf)) < 0) goto Error;
if (wrtRd(fd, "\n", buf, sizeof(buf)) < 0) goto Error;
if (wrtRd(fd, cmd, buf, sizeof(buf)) < 0) goto Error;
printf("%s\n", buf);
l=1;
while (l>0) {
if (l=wrtRd(fd, "", buf, sizeof(buf)) < 0) goto Error;
printf("%s", buf);
}
printf("\n");
i=10;
while (i>0 && wrtRd(fd, "logout\n", buf, sizeof(buf)) >= 0) {
i--;
}
close(fd);
return 0;
Error: perror("error in wrtRd"); return 0;
}