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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/types.h>
#include <fcntl.h>
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "tpl.h"
#include "utarray.h"
char *path = "socket";
char prompt[100];
void usage(char *prog) {
fprintf(stderr, "usage: %s [-v] [-s|-S socket] [-f file]\n", prog);
fprintf(stderr, " -v verbose\n");
fprintf(stderr, " -s path to UNIX domain socket\n");
fprintf(stderr, " -S path to socket (abstract namespace)\n");
fprintf(stderr, " -f file to read commands from\n");
exit(-1);
}
int verbose;
char *file;
FILE *filef;
char buf[256]; // max line length
int fd; // connection to unix domain socket (control port)
/* the control port can became readable when we aren't expecting a response.
* this probably means the control port is shutting down. check for this.
* if found, confirm its EOF. any other I/O without EOF here would be a bug. */
int last_gasp(int fd) {
char buf[100];
fd_set rfds;
int sr,rc;
int flags;
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
do {
rc=read(fd,buf,sizeof(buf));
if (rc > 0) fprintf(stderr,"control port: %.*s\n", rc, buf);
else if (rc == 0) fprintf(stderr,"control port: closed\n");
} while(rc > 0);
fcntl(fd, F_SETFL, flags);
return (rc==-1) ? 0 : 1;
}
char *next_line() {
size_t len;
char *line=NULL,*tmp;
if (file) line=fgets(buf,sizeof(buf), filef);
else {
if (last_gasp(fd)) goto done; // before we block on stdin, see if cp closed
line=readline(prompt); // must free it
}
if (!line) goto done;
len = strlen(line);
if (file) {
/* fgets keeps trailing newline. null it out. if absent, line got truncated*/
if (buf[len-1] == '\n') buf[len-1]='\0';
else { fprintf(stderr, "line too long\n"); line=NULL; }
} else {
/* copy the mallocd readline buffer and free it */
tmp = line;
if (len+1 < sizeof(buf)) {memcpy(buf, line, len+1); line=buf; }
else { fprintf(stderr, "line too long\n"); line=NULL; }
free(tmp);
}
done:
if (file && !line) fclose(filef);
return line;
}
/* This little parsing function finds one word at a time from the
* input line. It supports double quotes to group words together. */
const int ws[256] = {[' ']=1, ['\t']=1};
char *find_word(char *c, char **start, char **end) {
int in_qot=0;
while ((*c != '\0') && ws[*c]) c++; // skip leading whitespace
if (*c == '"') { in_qot=1; c++; }
*start=c;
if (in_qot) {
while ((*c != '\0') && (*c != '"')) c++;
*end = c;
if (*c == '"') {
in_qot=0; c++;
if ((*c != '\0') && !ws[*c]) {
fprintf(stderr,"text follows quoted text without space\n"); return NULL;
}
}
else {fprintf(stderr,"quote mismatch\n"); return NULL;}
}
else {
while ((*c != '\0') && (*c != ' ')) {
if (*c == '"') {fprintf(stderr,"start-quote within word\n"); return NULL; }
c++;
}
*end = c;
}
return c;
}
char *slurp(char *file, size_t *len) {
struct stat s;
char *buf;
int fd;
if (stat(file, &s) == -1) {
fprintf(stderr,"can't stat %s: %s\n", file, strerror(errno));
return NULL;
}
*len = s.st_size;
if ( (fd = open(file, O_RDONLY)) == -1) {
fprintf(stderr,"can't open %s: %s\n", file, strerror(errno));
return NULL;
}
buf = malloc(*len);
if (buf) {
if (read(fd, buf,*len) != *len) {
fprintf(stderr,"read failure\n");
free(buf); buf=NULL;
}
}
close(fd);
return buf;
}
/* this helper finds <File and >File words in the command.
* It replaces <File with the contents of file or
* records the >File so we can store output to it later.*/
int redir(tpl_bin *bbuf, UT_array *of, int *need_free) {
char *c = (char*)bbuf->addr;
char *file;
int rc=1;
*need_free = 0;
if (bbuf->sz <= 1) goto done;
if ((*c != '<') && (*c != '>')) goto done;
file = strndup(c+1,bbuf->sz-1);
if (*c == '>') { utarray_push_back(of, &file); rc=0; goto done;}
if (*c == '<') {
bbuf->addr = slurp(file, &bbuf->sz);
*need_free = bbuf->addr ? 1 : 0;
rc = bbuf->addr ? 1 : -1;
}
free(file);
done:
return rc;
}
void write_file(tpl_bin *bbuf, char *file) {
int fd,rc;
fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd == -1) {
fprintf(stderr,"failed to open %s: %s\n", file, strerror(errno));
return;
}
rc = write(fd, bbuf->addr, bbuf->sz);
if (rc == bbuf->sz) {
if (verbose) fprintf(stderr,"Wrote %s: %zu bytes\n", file, bbuf->sz);
} else {
if (rc >=0) fprintf(stderr,"partial write to %s\n", file);
else fprintf(stderr,"error writing to %s: %s\n", file, strerror(errno));
}
close(fd);
}
int do_rqst(char *line, int fd, int *cr) {
char *c=line, *start=NULL, *end=NULL, **f, *file;
tpl_node *tn=NULL,*tr=NULL;
UT_array *of; /* optional output files for command response channels */
int i, rc = -1, need_free, rr;
tpl_bin bbuf;
utarray_new(of, &ut_str_icd);
tn = tpl_map("A(B)", &bbuf);
*cr = 0;
/* parse the line into argv style words, pack and transmit the request */
while(*c != '\0') {
if ( (c = find_word(c,&start,&end)) == NULL) goto done;
//fprintf(stderr,"[%.*s]\n", (int)(end-start), start);
assert(start && end);
bbuf.addr = start;
bbuf.sz = end-start;
if ( (rr = redir(&bbuf,of,&need_free)) > 0) tpl_pack(tn,1);
else if (rr < 0) goto done; // rr==0 requires no action
if (need_free) free(bbuf.addr);
start = end = NULL;
}
if (tpl_dump(tn, TPL_FD, fd) == -1) goto done;
/* get the reply */
tr = tpl_map("iA(B)", cr, &bbuf);
if (tpl_load(tr, TPL_FD, fd) == -1) {
fprintf(stderr,"error receiving server response\n");
goto done;
}
tpl_unpack(tr,0);
if (*cr) printf("non-zero exit status: %d\n", *cr);
if ( (i = tpl_Alen(tr,1)) > 1) printf("%u buffers received\n",i);
/* unpack reply buffers. print the first. put the requested ones into files.*/
for(i=0; tpl_unpack(tr,1) > 0; i++) {
if (i==0 || verbose) printf("%.*s", (int)bbuf.sz, (char*)bbuf.addr);
if ( (f = (char**)utarray_eltptr(of,i))) write_file(&bbuf,*f);
if (bbuf.addr) free(bbuf.addr);
}
rc = 0;
done:
utarray_free(of);
if (tn) tpl_free(tn);
if (tr) tpl_free(tr);
return rc;
}
int main(int argc, char *argv[]) {
struct sockaddr_un addr;
int opt,rc,cr;
tpl_bin bbuf;
char *line;
while ( (opt = getopt(argc, argv, "v+f:s:S:")) != -1) {
switch (opt) {
case 'v': verbose++; break;
case 'f': file = strdup(optarg); break;
case 's': path = strdup(optarg); break;
case 'S': path = calloc(strlen(optarg)+2,1); strcpy(path+1,optarg); break;
default: usage(argv[0]); break;
}
}
if (optind < argc) usage(argv[0]);
snprintf(prompt,sizeof(prompt),"%s> ", path);
using_history();
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket error");
exit(-1);
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path)-1);
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("connect error");
exit(-1);
}
if (file && !(filef = fopen(file,"r"))) {
perror("fopen error");
exit(-1);
}
while ( (line=next_line()) != NULL) {
add_history(line);
if (do_rqst(line,fd,&cr) == -1) break;
}
clear_history();
return 0;
}
|