-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexecute_script.c
175 lines (143 loc) · 3.75 KB
/
execute_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/select.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "common.h"
#include "execute_script.h"
int execute_script(
const char* directory,
const char* script_name,
const char*const* appended_params,
const char*const* params,
read_t stdin_fn, void* stdin_obj,
write_t stdout_fn, void* stdout_obj
) {
const char** argv = NULL;
int ppcount=0;
int i;
int pcount=0;
char script_path[EXECFUSE_MAX_PATHLEN];
int child_stdout = -1;
int child_stdin = -1;
int to_be_written = -1;
int to_be_read = -1;
sigset_t mask;
int childpid = -1;
if (appended_params) {
for (i = 0; appended_params[i]; ++i)
++ppcount;
}
if (params) {
for (i = 0; params[i]; ++i)
++pcount;
}
argv = (const char**)malloc((ppcount + pcount + 2) * sizeof(char*));
assert(argv != NULL);
sprintf(script_path, "%s/%s", directory, script_name);
argv[0] = script_name;
for (i = 0; i < pcount; ++i)
argv[i+1] = params[i];
for (i = 0; i < ppcount; ++i)
argv[i+1+pcount] = appended_params[i];
argv[1 + ppcount + pcount]=NULL;
if (stdin_fn)
{int p[2]; int ret = pipe(p); assert(ret==0); to_be_written=p[1]; child_stdin =p[0];}
if (stdout_fn)
{int p[2]; int ret = pipe(p); assert(ret==0); to_be_read =p[0]; child_stdout=p[1];}
sigemptyset(&mask);
sigaddset(&mask, SIGPIPE);
sigprocmask(SIG_BLOCK, &mask, NULL);
childpid = fork();
if(!childpid) {
/* in child process */
if (child_stdout != -1) {
close(to_be_read);
dup2(child_stdout, 1);
close(child_stdout);
}
if (child_stdin != -1) {
close(to_be_written);
dup2(child_stdin, 0);
close(child_stdin);
}
execv(script_path, (char**)argv);
_exit(ENOSYS);
} else {
/* in parent process */
int maxfd = -1;
int status, exit_code;
char buf[EXECFUSE_CHUNKSIZE];
fd_set rfds, wfds;
if (child_stdout != -1) {
close(child_stdout);
}
if (child_stdin != -1) {
close(child_stdin);
}
/* Event loop for feeding the script with input data, reading output and reading exit code */
if (to_be_written != -1 && maxfd < to_be_written) maxfd = to_be_written;
if (to_be_read != -1 && maxfd < to_be_read ) maxfd = to_be_read ;
++maxfd;
for (;;) {
int ret;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
if (to_be_read != -1) FD_SET(to_be_read, &rfds);
if (to_be_written != -1) FD_SET(to_be_written, &wfds);
if (to_be_read == -1 && to_be_written == -1)
break;
ret = select(maxfd+1, &rfds, &wfds, NULL, NULL);
if (ret == -1) {
if (errno==EINTR || errno==EAGAIN) continue;
break;
}
if (to_be_read != -1 && FD_ISSET(to_be_read, &rfds)) {
ret = read(to_be_read, buf, sizeof buf);
if (ret == -1) {
if (errno == EINTR || errno==EAGAIN) continue;
}
if (ret == 0 || ret == -1) {
close(to_be_read);
to_be_read = -1;
} else {
int ret2 = (*stdout_fn)(stdout_obj, buf, ret);
if (ret2 < ret) {
close(to_be_read);
to_be_read = -1;
}
}
}
if (to_be_written != -1 && FD_ISSET(to_be_written, &wfds)) {
ret = (*stdin_fn)(stdin_obj, buf, sizeof buf);
if (ret==-1) {
if (errno == EINTR || errno==EAGAIN) continue;
}
if (!ret || ret == -1) {
close(to_be_written);
to_be_written=-1;
} else {
ret = write(to_be_written, buf, ret);
if (ret == -1) {
if (errno == EINTR || errno==EAGAIN) continue;
}
if (ret == 0 || ret == -1) {
close(to_be_written);
to_be_read = -1;
}
}
}
}
if (to_be_read != -1) close(to_be_read);
if (to_be_written != -1) close(to_be_read);
waitpid(childpid, &status, 0);
exit_code = WEXITSTATUS(status);
free(argv);
return exit_code;
}
}