forked from stripydog/kplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.c
370 lines (332 loc) · 11.2 KB
/
fileio.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* fileio.c
* This file is part of kplex
* Copyright Keith Young 2012 - 2016
* For copying information see the file COPYING distributed with this software
*
* This file contains code for i/o from files (incl stdin/stdout)
*/
#include "kplex.h"
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <pwd.h>
#include <grp.h>
struct if_file {
int fd;
char *filename;
size_t qsize;
};
/*
* Duplicate struct if_file
* Args: if_file to be duplicated
* Returns: pointer to new if_file
*/
void *ifdup_file(void *iff)
{
struct if_file *newif;
if ((newif = (struct if_file *) malloc(sizeof(struct if_file)))
== (struct if_file *) NULL)
return(NULL);
memset ((void *)newif,0,sizeof(struct if_file));
/* Read/Write only supported for stdin/stdout so don't allocate fp
* And don't bother to duplicate filename
*/
return(newif);
}
void cleanup_file(iface_t *ifa)
{
struct if_file *iff = (struct if_file *) ifa->info;
if (iff->fd >= 0)
close(iff->fd);
if (iff->filename)
free(iff->filename);
}
void write_file(iface_t *ifa)
{
struct if_file *ifc = (struct if_file *) ifa->info;
senblk_t *sptr;
int usereturn=flag_test(ifa,F_NOCR)?0:1;
int data=0;
int cnt=1;
struct iovec iov[2];
/* ifc->fd will only be < 0 if we're opening a FIFO.
*/
if (ifc->fd < 0) {
if ((ifc->fd=open(ifc->filename,O_WRONLY)) < 0) {
logerr(errno,"Failed to open FIFO %s for writing\n",ifc->filename);
iface_thread_exit(errno);
}
if (init_q(ifa,ifc->qsize) < 0) {
logerr(errno,"Could not create queue for FIFO %s",ifc->filename);
iface_thread_exit(errno);
}
DEBUG(3,"%s opened FIFO %s for writing",ifa->name,ifc->filename);
}
if (ifa->tagflags) {
if ((iov[0].iov_base=malloc(TAGMAX)) == NULL) {
logerr(errno,"%s: Disabing tag output",ifa->name);
ifa->tagflags=0;
} else {
cnt=2;
data=1;
}
}
for(;;) {
if ((sptr = next_senblk(ifa->q)) == NULL) {
break;
}
if (senfilter(sptr,ifa->ofilter)) {
senblk_free(sptr,ifa->q);
continue;
}
if (!usereturn) {
sptr->data[sptr->len-2] = '\n';
sptr->len--;
}
if (ifa->tagflags)
if ((iov[0].iov_len = gettag(ifa,iov[0].iov_base,sptr)) == 0) {
logerr(errno,"%s: Disabing tag output",ifa->name);
ifa->tagflags=0;
cnt=1;
data=0;
free(iov[0].iov_base);
}
iov[data].iov_base=sptr->data;
iov[data].iov_len=sptr->len;
if (writev(ifc->fd,iov,cnt) <0) {
if (!(flag_test(ifa,F_PERSIST) && errno == EPIPE) ) {
logerr(errno,"%s: write failed",ifa->name);
break;
}
if ((ifc->fd=open(ifc->filename,O_WRONLY)) < 0) {
logerr(errno,"%s: failed to re-open %s",ifa->name,
ifc->filename);
break;
}
DEBUG(4,"%s: reconnected to FIFO %s",ifa->name,ifc->filename);
}
senblk_free(sptr,ifa->q);
}
if (cnt == 2)
free(iov[0].iov_base);
iface_thread_exit(errno);
}
void file_read_wrapper(iface_t *ifa)
{
struct if_file *ifc = (struct if_file *) ifa->info;
/* Create FILE stream here to allow for non-blocking opening FIFOs */
if (ifc->fd == -1) {
if ((ifc->fd = open(ifc->filename,O_RDONLY)) < 0) {
logerr(errno,"Failed to open FIFO %s for reading\n",ifc->filename);
iface_thread_exit(errno);
} else {
DEBUG(3,"%s: opened %s for reading",ifa->name,ifc->filename);
}
}
do_read(ifa);
}
ssize_t read_file(iface_t *ifa, char *buf)
{
struct if_file *ifc = (struct if_file *) ifa->info;
ssize_t nread;
for(;;) {
if ((nread=read(ifc->fd,buf,BUFSIZ)) <=0) {
if (!flag_test(ifa,F_PERSIST))
break;
close(ifc->fd);
if ((ifc->fd=open(ifc->filename,O_RDONLY)) < 0) {
logerr(errno,"Failed to re-open FIFO %s for reading\n",
ifc->filename);
break;
}
DEBUG(4,"%s: re-opened %s for reading",ifa->name,ifc->filename);
continue;
} else
break;
}
return nread;
}
iface_t *init_file (iface_t *ifa)
{
struct if_file *ifc;
struct kopts *opt;
struct stat statbuf;
int ret;
int append=0;
uid_t uid=-1;
gid_t gid=-1;
struct passwd *owner;
struct group *group;
mode_t tperm,perm=0;
char *cp;
if ((ifc = (struct if_file *)malloc(sizeof(struct if_file))) == NULL) {
logerr(errno,"Could not allocate memory");
return(NULL);
}
memset ((void *)ifc,0,sizeof(struct if_file));
ifc->qsize=DEFQSIZE;
ifc->fd=-1;
ifa->info = (void *) ifc;
for(opt=ifa->options;opt;opt=opt->next) {
if (!strcasecmp(opt->var,"filename")) {
if (strcmp(opt->val,"-"))
if ((ifc->filename=strdup(opt->val)) == NULL) {
logerr(errno,"Failed to duplicate argument string");
return(NULL);
}
} else if (!strcasecmp(opt->var,"qsize")) {
if (!(ifc->qsize=atoi(opt->val))) {
logerr(0,"Invalid queue size specified: %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"append")) {
if (!strcasecmp(opt->val,"yes")) {
append++;
} else if (!strcasecmp(opt->val,"no")) {
append = 0;
} else {
logerr(0,"Invalid option \"append=%s\"",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"owner")) {
if ((owner=getpwnam(opt->val)) == NULL) {
logerr(0,"No such user '%s'",opt->val);
return(NULL);
}
uid=owner->pw_uid;
} else if (!strcasecmp(opt->var,"group")) {
if ((group=getgrnam(opt->val)) == NULL) {
logerr(0,"No such group '%s'",opt->val);
return(NULL);
}
gid=group->gr_gid;
}
else if (!strcasecmp(opt->var,"perm")) {
for (cp=opt->val;*cp;cp++) {
if (*cp >= '0' && *cp < '8') {
perm <<=3;
perm += (*cp-'0');
} else {
perm = 0;
break;
}
}
perm &= ACCESSPERMS;
if (perm == 0) {
logerr(0,"Invalid permissions for tty device \'%s\'",opt->val);
return 0;
}
} else {
logerr(0,"Unknown interface option %s\n",opt->var);
return(NULL);
}
}
/* We do allow use of stdin and stdout, but not if they're connected to
* a terminal. This allows re-direction in background mode
*/
if (ifc->filename == NULL) {
if (flag_test(ifa,F_PERSIST)) {
logerr(0,"Can't use persist mode with stdin/stdout");
return(NULL);
}
if (((ifa->direction != IN) &&
(((struct if_engine *)ifa->lists->engine->info)->flags &
K_NOSTDOUT)) ||
((ifa->direction != OUT) &&
(((struct if_engine *)ifa->lists->engine->info)->flags &
K_NOSTDIN))) {
logerr(0,"Can't use terminal stdin/stdout in background mode");
return(NULL);
}
if (ifa->direction == IN) {
ifc->fd = STDIN_FILENO;
DEBUG(3,"%s: using stdin",ifa->name);
} else {
ifc->fd = STDOUT_FILENO;
DEBUG(3,"%s: using %s",ifa->name,
(ifa->direction==OUT)?"stdout":"stdin/stdout");
}
} else {
if (ifa->direction == BOTH) {
logerr(0,"Bi-directional file I/O only supported for stdin/stdout");
return(NULL);
}
if ((ret=stat(ifc->filename,&statbuf)) < 0) {
if (ifa->direction != OUT) {
logerr(errno,"stat %s",ifc->filename);
return(NULL);
}
}
if ((ret == 0) && S_ISFIFO(statbuf.st_mode)) {
/* Special rules for FIFOs. Opening here would hang for a reading
* interface with no writer. Given that we're single threaded here,
* that would be bad
*/
if (access(ifc->filename,(ifa->direction==IN)?R_OK:W_OK) != 0) {
logerr(errno,"Could not access %s",ifc->filename);
return(NULL);
}
} else {
if (flag_test(ifa,F_PERSIST)) {
logerr(0,"Can't use persist mode on %s: Not a FIFO",
ifc->filename);
return(NULL);
}
if (perm)
tperm=umask(0);
errno=0;
/* If file is for output and doesn't currently exist...*/
if (ifa->direction != IN && (ifc->fd=open(ifc->filename,
O_WRONLY|O_CREAT|O_EXCL|((append)?O_APPEND:0),
(perm)?perm:0664)) >= 0) {
if (gid != 0 || uid != -1) {
if (chown(ifc->filename,uid,gid) < 0) {
logerr(errno, "Failed to set ownership or group on output file %s",ifc->filename);
return(NULL);
}
}
DEBUG(3,"%s: created %s for output",ifa->name,ifc->filename);
} else {
if (errno && errno != EEXIST) {
logerr(errno,"Failed to create file %s",ifc->filename);
return(NULL);
}
/* file is for input or already exists */
if ((ifc->fd=open(ifc->filename,(ifa->direction==IN)?O_RDONLY:
(O_WRONLY|((append)?O_APPEND:O_TRUNC)))) < 0) {
logerr(errno,"Failed to open file %s",ifc->filename);
return(NULL);
}
DEBUG(3,"%s: opened %s for %s",ifa->name,ifc->filename,
(ifa->direction==IN)?"input":"output");
}
/* reset umask: not really necessary */
if (perm)
(void) umask(tperm);
}
}
free_options(ifa->options);
ifa->write=write_file;
ifa->read=file_read_wrapper;
ifa->readbuf=read_file;
ifa->cleanup=cleanup_file;
if (ifa->direction != IN && ifc->fd >= 0)
if (init_q(ifa, ifc->qsize)< 0) {
logerr(0,"Could not create queue");
cleanup_file(ifa);
return(NULL);
}
if (ifa->direction == BOTH) {
if ((ifa->next=ifdup(ifa)) == NULL) {
logerr(0,"Interface duplication failed");
cleanup_file(ifa);
return(NULL);
}
ifa->direction=OUT;
ifa->pair->direction=IN;
ifc = (struct if_file *) ifa->pair->info;
ifc->fd=STDIN_FILENO;
}
return(ifa);
}