-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.c
140 lines (113 loc) · 3.34 KB
/
script.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "script.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#define MAXSIGNAL 31 /* biggest signal under linux */
static FILE *rcvfp;
static FILE *sendfp = NULL;
static int sendfd;
static int pid;
void syserr(char *msg) {
fprintf(stderr, "ERROR: %s (%s)\n", msg, strerror(errno));
exit(1);
}
void childsig(x)
int x;
{
extern char *strsignal();
fprintf(stderr,"child caught (%d) %s\n", x, strsignal(x));
}
int scriptopen(char* prog, char *arg1, char *arg2, char *pmsg)
{
int pfdout[2], pfdin[2];
char *msg=NULL;
int err=0;
/*
if (scriptfeed("reset \n")) {
printf("running\n");
return (1);
} else {
printf("starting\n");
}
*/
do { /* context for error checking */
if(pipe(pfdout) == -1) { err++; msg="pipe 1"; break; };
if(pipe(pfdin) == -1) { err++; msg="pipe 2"; break; };
} while(0); if (err) return(0);
switch(pid=fork()) { /* split off into parent-child */
case -1: /* error */
syserr("fork");
break;
case 0: /* in child */
do { /* context for error checking */
if (close(0) == -1) {err++; msg="close 3" ; break;};
if (dup(pfdout[0]) != 0) {err++; msg="dup 4" ; break;};
if (close(1) == -1) {err++; msg="close 5" ; break;};
if (dup(pfdin[1]) != 1) {err++; msg="dup 6" ; break;};
if (close(pfdout[0]) == -1) {err++; msg="close 7" ; break;};
if (close(pfdout[1]) == -1) {err++; msg="close 8" ; break;};
if (close(pfdin[0]) == -1) {err++; msg="close 9" ; break;};
if (close(pfdin[1]) == -1) {err++; msg="close 10"; break;};
execlp(prog, prog, arg1, arg2, NULL); /* thus passes control to child */
err++; msg="execlp"; break; /* should only get here if execlp fails */
} while (0); if (err) return(0);
break;
}
/* from here on down we are in parent */
/* printf("parent: child pid = %d\n", pid); */
do { /* context for error checking */
if (close(pfdout[0]) == -1) {err++; msg="close 11" ; break;};
if (close(pfdin[1]) == -1) {err++; msg="close 12" ; break;};
sendfd=pfdout[1];
} while (0);
if ((rcvfp=fdopen(pfdin[0],"r")) == NULL) {err++; msg="fdopen rcvfp";};
if ((sendfp=fdopen(pfdout[1], "w")) == NULL) {err++; msg="fdopen sendfp";};
pmsg = msg;
if (err) return(0);
return(1); /* success */
}
void onalarm() /* kill child process on alarm */
{
fprintf(stderr, "child killed\n");
kill(pid, SIGQUIT);
kill(pid, SIGKILL);
}
int scriptclose() {
/* return(0); */
close(sendfd); /* send EOF */
sendfp = NULL;
return(1);
}
int scriptread(char *buf, int n, int timeout) {
int status;
close(sendfd); /* send EOF */
if (timeout != 0) {
signal(SIGALRM, onalarm);
alarm(timeout);
}
if (wait(&status) == -1 || (status & 0177) != 0) {
fprintf(stderr,"killed subprocess\n");
return(0);
} else {
fgets(buf, n, rcvfp);
kill(pid, SIGKILL); /* not interested in more output */
return(1);
}
}
int scriptfeed(char *msg) {
if (sendfp == NULL) {
return(0);
}
if (fprintf(sendfp, "%s",msg) < 0) {
return(0);
} else {
fflush(sendfp);
return (1);
}
}