3 * $Id: file.c,v 1.4 2001/02/03 20:30:03 mdw Exp $
5 * File source and target
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 2001/02/03 20:30:03 mdw
33 * Support re-reading config files on SIGHUP.
35 * Revision 1.3 1999/12/22 15:43:47 mdw
38 * Revision 1.2 1999/10/22 22:47:13 mdw
39 * New (empty) endpoint method required.
41 * Revision 1.1 1999/07/26 23:33:32 mdw
42 * New sources and targets.
46 /*----- Header files ------------------------------------------------------*/
56 #include <sys/types.h>
61 #include <mLib/alloc.h>
62 #include <mLib/dstr.h>
63 #include <mLib/fdflags.h>
76 /*----- Data structures ---------------------------------------------------*/
78 /* --- File specification --- */
80 typedef struct fspec {
88 #define FTYPE_GUESS 0u
93 /* --- File options --- */
95 typedef struct fopts {
99 /* --- File data block --- */
101 typedef struct fdata {
107 /* --- File source block --- */
109 typedef struct fsource {
114 /* --- File target block --- */
116 typedef struct ftarget {
121 /*----- Static variables --------------------------------------------------*/
123 static fopts file_opts = { O_TRUNC };
125 static reffd *rstdin, *rstdout;
128 /*----- File endpoint operations ------------------------------------------*/
130 /* --- @wclose@ --- */
132 static void fept_wclose(endpt *e)
140 /* --- @close@ --- */
142 static void fept_close(endpt *e)
152 /* --- Endpoint operations table --- */
154 static endpt_ops fept_ops = { 0, 0, fept_wclose, fept_close };
156 /*----- General operations on sources and targets -------------------------*/
158 /* --- @file_null@ --- *
160 * Arguments: @void *p@ = an uninteresting argument
164 * Use: Removes the reference to @rnull@ when the file closes.
167 static void file_null(void *p)
172 /* --- @file_nullref@ --- *
176 * Returns: A reference to a file descriptor open on /dev/null.
178 * Use: Obtains a file descriptor for /dev/null. The reference
179 * @rnull@ is `weak', so that I only have a descriptor open when
180 * I actually need one.
183 static reffd *file_nullref(void)
189 if ((n = open("/dev/null", O_RDWR)) < 0) {
190 fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno));
193 fdflags(n, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
194 rnull = reffd_init(n);
195 reffd_handler(rnull, file_null, 0);
200 /* --- @file_fspec@ --- *
202 * Arguments: @fspec *f@ = pointer to filespec to fill in
203 * @scanner *sc@ = pointer to scanner to read from
207 * Use: Reads in a file spec.
210 static void file_fspec(fspec *f, scanner *sc)
213 unsigned type = FTYPE_GUESS;
216 /* --- Set up references to @stdin@ and @stdout@ --- */
219 fdflags(STDIN_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
220 fdflags(STDOUT_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
221 rstdin = reffd_init(STDIN_FILENO);
222 rstdout = reffd_init(STDOUT_FILENO);
225 /* --- Try to get an access type --- */
232 if (sc->t == CTOK_WORD) {
233 int i = conf_enum(sc, "fd,name,null", c ? ENUM_ABBREV : ENUM_NONE,
236 if (type && sc->t == ':')
240 /* --- Check for a valid file descriptor name --- */
242 if (sc->t == CTOK_WORD && type != FTYPE_NAME && type != FTYPE_NULL) {
243 if (strcmp(sc->d.buf, "stdin") == 0) {
246 } else if (strcmp(sc->d.buf, "stdout") == 0) {
252 else if (isdigit((unsigned char)*sc->d.buf)) {
253 int nfd = atoi(sc->d.buf);
255 case STDIN_FILENO: fd = rstdin; REFFD_INC(rstdin); break;
256 case STDOUT_FILENO: fd = rstdout; REFFD_INC(rstdout); break;
258 fd = reffd_init(nfd);
259 fdflags(nfd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
265 /* --- Sort out what to do as a result --- */
267 if (type == FTYPE_NULL)
268 f->type = FTYPE_NULL;
273 } else if (type == FTYPE_FD) {
274 if (sc->t == CTOK_WORD)
275 error(sc, "bad file descriptor `%s'", sc->d.buf);
277 error(sc, "parse error, expected file descriptor");
280 conf_name(sc, '/', &d);
281 f->type = FTYPE_NAME;
282 f->u.name = xstrdup(d.buf);
289 static void file_read(fdata *f, scanner *sc)
291 file_fspec(&f->in, sc);
294 if (f->out.type == FTYPE_FD && f->out.u.fd == rstdin)
295 f->out.u.fd = rstdout;
298 file_fspec(&f->out, sc);
300 f->fa = fattr_global;
304 /* --- @print@ --- */
306 static void file_pfspec(fspec *f, dstr *d)
310 switch (f->u.fd->fd) {
312 dstr_puts(d, "stdin");
315 dstr_puts(d, "stdout");
318 dstr_putf(d, "fd:%i", f->u.fd->fd);
323 dstr_putf(d, "name:%s", f->u.name);
326 dstr_puts(d, "null");
331 static void file_desc(fdata *f, dstr *d)
333 dstr_puts(d, "file ");
334 file_pfspec(&f->in, d);
336 file_pfspec(&f->out, d);
339 /* --- @option@ --- */
341 static int file_option(fdata *f, scanner *sc)
343 fopts *fo = f ? &f->fo : &file_opts;
345 CONF_BEGIN(sc, "file", "file")
347 /* --- Handle file-specific options --- */
349 if (strcmp(sc->d.buf, "create") == 0) {
353 switch (conf_enum(sc, "no,yes", ENUM_ABBREV, "create status")) {
354 case 0: fo->of &= ~O_CREAT; break;
355 case 1: fo->of |= O_CREAT; break;
360 else if (strcmp(sc->d.buf, "open") == 0) {
364 fo->of &= ~(O_TRUNC | O_APPEND | O_EXCL);
365 switch (conf_enum(sc, "no,truncate,append",
366 ENUM_ABBREV, "open status")) {
367 case 0: fo->of |= O_EXCL; break;
368 case 1: fo->of |= O_TRUNC; break;
369 case 2: fo->of |= O_APPEND; break;
374 /* --- See if it's a file attribute --- */
377 fattr *fa = f ? &f->fa : &fattr_global;
378 if (fattr_option(sc, fa))
382 /* --- Nobody understood it --- */
387 /* --- @endpt@ --- */
389 static endpt *file_endpt(fdata *f, const char *desc)
391 reffd *in = 0, *out = 0;
394 /* --- Make the input file first --- */
396 switch (f->in.type) {
406 if ((fd = open(f->in.u.name, O_RDONLY | O_NONBLOCK)) < 0) {
407 fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
408 desc, f->in.u.name, strerror(errno));
415 /* --- Make the output file second --- */
417 switch (f->out.type) {
419 out = file_nullref();
426 int m = f->fo.of | O_WRONLY | O_NONBLOCK;
429 /* --- First try creating the file --- *
431 * This lets me know whether to set the file attributes or not. It
432 * also stands a chance of noticing people playing silly beggars with
437 (fd = open(f->out.u.name, m | O_EXCL, f->fa.mode)) < 0 &&
438 (errno != EEXIST || (m & O_EXCL))) {
439 fw_log(-1, "[%s] couldn't create `%s': %s",
440 desc, f->out.u.name, strerror(errno));
446 if (fattr_apply(f->out.u.name, &f->fa)) {
447 fw_log(-1, "[%s] couldn't apply file attributes to `%s': %s",
448 desc, f->out.u.name, strerror(errno));
450 } else if ((fd = open(f->out.u.name, m & ~O_CREAT)) < 0) {
451 fw_log(-1, "[%s] couldn't open `%s': %s",
452 desc, f->out.u.name, strerror(errno));
457 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
458 out = reffd_init(fd);
462 /* --- Set up the endpoint --- */
475 /* --- @destroy@ --- */
477 static void file_destroy(fdata *f)
479 if (f->in.type == FTYPE_NAME)
481 if (f->out.type == FTYPE_NAME)
485 /*----- File source description -------------------------------------------*/
487 /* --- @option@ --- */
489 static int fsource_option(source *s, scanner *sc)
491 fsource *fs = (fsource *)s;
492 return (file_option(fs ? &fs->f : 0, sc));
497 static source *fsource_read(scanner *sc)
501 if (!conf_prefix(sc, "file"))
503 fs = CREATE(fsource);
504 fs->s.ops = &fsource_ops;
506 file_read(&fs->f, sc);
510 /* --- @attach@ --- */
512 static void fsource_destroy(source */*s*/);
514 static void fsource_attach(source *s, scanner *sc, target *t)
516 fsource *fs = (fsource *)s;
519 /* --- Set up the source description string --- */
523 file_desc(&fs->f, &d);
524 dstr_puts(&d, " -> ");
525 dstr_puts(&d, t->desc);
526 fs->s.desc = xstrdup(d.buf);
530 /* --- And then create the endpoints --- */
532 if ((ee = t->ops->create(t, fs->s.desc)) == 0)
534 if ((e = file_endpt(&fs->f, fs->s.desc)) == 0) {
540 /* --- Dispose of the source and target now --- */
544 fsource_destroy(&fs->s);
547 /* --- @destroy@ --- */
549 static void fsource_destroy(source *s)
551 fsource *fs = (fsource *)s;
553 file_destroy(&fs->f);
557 /* --- File source operations --- */
559 source_ops fsource_ops = {
561 fsource_option, fsource_read, fsource_attach, fsource_destroy
564 /*----- File target description -------------------------------------------*/
566 /* --- @option@ --- */
568 static int ftarget_option(target *t, scanner *sc)
570 ftarget *ft = (ftarget *)t;
571 return (file_option(ft ? &ft->f : 0, sc));
576 static target *ftarget_read(scanner *sc)
581 if (!conf_prefix(sc, "file"))
583 ft = CREATE(ftarget);
584 ft->t.ops = &ftarget_ops;
585 file_read(&ft->f, sc);
586 file_desc(&ft->f, &d);
587 ft->t.desc = xstrdup(d.buf);
592 /* --- @create@ --- */
594 static endpt *ftarget_create(target *t, const char *desc)
596 ftarget *ft = (ftarget *)t;
597 endpt *e = file_endpt(&ft->f, desc);
601 /* --- @destroy@ --- */
603 static void ftarget_destroy(target *t)
605 ftarget *ft = (ftarget *)t;
606 file_destroy(&ft->f);
611 /* --- File target operations --- */
613 target_ops ftarget_ops = {
615 ftarget_option, ftarget_read, ftarget_create, ftarget_destroy
618 /*----- That's all, folks -------------------------------------------------*/