3 * $Id: file.c,v 1.5 2002/02/22 23:43:32 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.5 2002/02/22 23:43:32 mdw
33 * Call @xfree@ rather than @free@.
35 * Revision 1.4 2001/02/03 20:30:03 mdw
36 * Support re-reading config files on SIGHUP.
38 * Revision 1.3 1999/12/22 15:43:47 mdw
41 * Revision 1.2 1999/10/22 22:47:13 mdw
42 * New (empty) endpoint method required.
44 * Revision 1.1 1999/07/26 23:33:32 mdw
45 * New sources and targets.
49 /*----- Header files ------------------------------------------------------*/
59 #include <sys/types.h>
64 #include <mLib/alloc.h>
65 #include <mLib/dstr.h>
66 #include <mLib/fdflags.h>
79 /*----- Data structures ---------------------------------------------------*/
81 /* --- File specification --- */
83 typedef struct fspec
{
91 #define FTYPE_GUESS 0u
96 /* --- File options --- */
98 typedef struct fopts
{
102 /* --- File data block --- */
104 typedef struct fdata
{
110 /* --- File source block --- */
112 typedef struct fsource
{
117 /* --- File target block --- */
119 typedef struct ftarget
{
124 /*----- Static variables --------------------------------------------------*/
126 static fopts file_opts
= { O_TRUNC
};
128 static reffd
*rstdin
, *rstdout
;
131 /*----- File endpoint operations ------------------------------------------*/
133 /* --- @wclose@ --- */
135 static void fept_wclose(endpt
*e
)
143 /* --- @close@ --- */
145 static void fept_close(endpt
*e
)
155 /* --- Endpoint operations table --- */
157 static endpt_ops fept_ops
= { 0, 0, fept_wclose
, fept_close
};
159 /*----- General operations on sources and targets -------------------------*/
161 /* --- @file_null@ --- *
163 * Arguments: @void *p@ = an uninteresting argument
167 * Use: Removes the reference to @rnull@ when the file closes.
170 static void file_null(void *p
)
175 /* --- @file_nullref@ --- *
179 * Returns: A reference to a file descriptor open on /dev/null.
181 * Use: Obtains a file descriptor for /dev/null. The reference
182 * @rnull@ is `weak', so that I only have a descriptor open when
183 * I actually need one.
186 static reffd
*file_nullref(void)
192 if ((n
= open("/dev/null", O_RDWR
)) < 0) {
193 fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno
));
196 fdflags(n
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
197 rnull
= reffd_init(n
);
198 reffd_handler(rnull
, file_null
, 0);
203 /* --- @file_fspec@ --- *
205 * Arguments: @fspec *f@ = pointer to filespec to fill in
206 * @scanner *sc@ = pointer to scanner to read from
210 * Use: Reads in a file spec.
213 static void file_fspec(fspec
*f
, scanner
*sc
)
216 unsigned type
= FTYPE_GUESS
;
219 /* --- Set up references to @stdin@ and @stdout@ --- */
222 fdflags(STDIN_FILENO
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
223 fdflags(STDOUT_FILENO
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
224 rstdin
= reffd_init(STDIN_FILENO
);
225 rstdout
= reffd_init(STDOUT_FILENO
);
228 /* --- Try to get an access type --- */
235 if (sc
->t
== CTOK_WORD
) {
236 int i
= conf_enum(sc
, "fd,name,null", c ? ENUM_ABBREV
: ENUM_NONE
,
239 if (type
&& sc
->t
== ':')
243 /* --- Check for a valid file descriptor name --- */
245 if (sc
->t
== CTOK_WORD
&& type
!= FTYPE_NAME
&& type
!= FTYPE_NULL
) {
246 if (strcmp(sc
->d
.buf
, "stdin") == 0) {
249 } else if (strcmp(sc
->d
.buf
, "stdout") == 0) {
255 else if (isdigit((unsigned char)*sc
->d
.buf
)) {
256 int nfd
= atoi(sc
->d
.buf
);
258 case STDIN_FILENO
: fd
= rstdin
; REFFD_INC(rstdin
); break;
259 case STDOUT_FILENO
: fd
= rstdout
; REFFD_INC(rstdout
); break;
261 fd
= reffd_init(nfd
);
262 fdflags(nfd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
268 /* --- Sort out what to do as a result --- */
270 if (type
== FTYPE_NULL
)
271 f
->type
= FTYPE_NULL
;
276 } else if (type
== FTYPE_FD
) {
277 if (sc
->t
== CTOK_WORD
)
278 error(sc
, "bad file descriptor `%s'", sc
->d
.buf
);
280 error(sc
, "parse error, expected file descriptor");
283 conf_name(sc
, '/', &d
);
284 f
->type
= FTYPE_NAME
;
285 f
->u
.name
= xstrdup(d
.buf
);
292 static void file_read(fdata
*f
, scanner
*sc
)
294 file_fspec(&f
->in
, sc
);
297 if (f
->out
.type
== FTYPE_FD
&& f
->out
.u
.fd
== rstdin
)
298 f
->out
.u
.fd
= rstdout
;
301 file_fspec(&f
->out
, sc
);
303 f
->fa
= fattr_global
;
307 /* --- @print@ --- */
309 static void file_pfspec(fspec
*f
, dstr
*d
)
313 switch (f
->u
.fd
->fd
) {
315 dstr_puts(d
, "stdin");
318 dstr_puts(d
, "stdout");
321 dstr_putf(d
, "fd:%i", f
->u
.fd
->fd
);
326 dstr_putf(d
, "name:%s", f
->u
.name
);
329 dstr_puts(d
, "null");
334 static void file_desc(fdata
*f
, dstr
*d
)
336 dstr_puts(d
, "file ");
337 file_pfspec(&f
->in
, d
);
339 file_pfspec(&f
->out
, d
);
342 /* --- @option@ --- */
344 static int file_option(fdata
*f
, scanner
*sc
)
346 fopts
*fo
= f ?
&f
->fo
: &file_opts
;
348 CONF_BEGIN(sc
, "file", "file")
350 /* --- Handle file-specific options --- */
352 if (strcmp(sc
->d
.buf
, "create") == 0) {
356 switch (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "create status")) {
357 case 0: fo
->of
&= ~O_CREAT
; break;
358 case 1: fo
->of
|= O_CREAT
; break;
363 else if (strcmp(sc
->d
.buf
, "open") == 0) {
367 fo
->of
&= ~(O_TRUNC
| O_APPEND
| O_EXCL
);
368 switch (conf_enum(sc
, "no,truncate,append",
369 ENUM_ABBREV
, "open status")) {
370 case 0: fo
->of
|= O_EXCL
; break;
371 case 1: fo
->of
|= O_TRUNC
; break;
372 case 2: fo
->of
|= O_APPEND
; break;
377 /* --- See if it's a file attribute --- */
380 fattr
*fa
= f ?
&f
->fa
: &fattr_global
;
381 if (fattr_option(sc
, fa
))
385 /* --- Nobody understood it --- */
390 /* --- @endpt@ --- */
392 static endpt
*file_endpt(fdata
*f
, const char *desc
)
394 reffd
*in
= 0, *out
= 0;
397 /* --- Make the input file first --- */
399 switch (f
->in
.type
) {
409 if ((fd
= open(f
->in
.u
.name
, O_RDONLY
| O_NONBLOCK
)) < 0) {
410 fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
411 desc
, f
->in
.u
.name
, strerror(errno
));
418 /* --- Make the output file second --- */
420 switch (f
->out
.type
) {
422 out
= file_nullref();
429 int m
= f
->fo
.of
| O_WRONLY
| O_NONBLOCK
;
432 /* --- First try creating the file --- *
434 * This lets me know whether to set the file attributes or not. It
435 * also stands a chance of noticing people playing silly beggars with
440 (fd
= open(f
->out
.u
.name
, m
| O_EXCL
, f
->fa
.mode
)) < 0 &&
441 (errno
!= EEXIST
|| (m
& O_EXCL
))) {
442 fw_log(-1, "[%s] couldn't create `%s': %s",
443 desc
, f
->out
.u
.name
, strerror(errno
));
449 if (fattr_apply(f
->out
.u
.name
, &f
->fa
)) {
450 fw_log(-1, "[%s] couldn't apply file attributes to `%s': %s",
451 desc
, f
->out
.u
.name
, strerror(errno
));
453 } else if ((fd
= open(f
->out
.u
.name
, m
& ~O_CREAT
)) < 0) {
454 fw_log(-1, "[%s] couldn't open `%s': %s",
455 desc
, f
->out
.u
.name
, strerror(errno
));
460 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
461 out
= reffd_init(fd
);
465 /* --- Set up the endpoint --- */
478 /* --- @destroy@ --- */
480 static void file_destroy(fdata
*f
)
482 if (f
->in
.type
== FTYPE_NAME
)
484 if (f
->out
.type
== FTYPE_NAME
)
485 xfree(f
->out
.u
.name
);
488 /*----- File source description -------------------------------------------*/
490 /* --- @option@ --- */
492 static int fsource_option(source
*s
, scanner
*sc
)
494 fsource
*fs
= (fsource
*)s
;
495 return (file_option(fs ?
&fs
->f
: 0, sc
));
500 static source
*fsource_read(scanner
*sc
)
504 if (!conf_prefix(sc
, "file"))
506 fs
= CREATE(fsource
);
507 fs
->s
.ops
= &fsource_ops
;
509 file_read(&fs
->f
, sc
);
513 /* --- @attach@ --- */
515 static void fsource_destroy(source */
*s*/
);
517 static void fsource_attach(source
*s
, scanner
*sc
, target
*t
)
519 fsource
*fs
= (fsource
*)s
;
522 /* --- Set up the source description string --- */
526 file_desc(&fs
->f
, &d
);
527 dstr_puts(&d
, " -> ");
528 dstr_puts(&d
, t
->desc
);
529 fs
->s
.desc
= xstrdup(d
.buf
);
533 /* --- And then create the endpoints --- */
535 if ((ee
= t
->ops
->create(t
, fs
->s
.desc
)) == 0)
537 if ((e
= file_endpt(&fs
->f
, fs
->s
.desc
)) == 0) {
543 /* --- Dispose of the source and target now --- */
547 fsource_destroy(&fs
->s
);
550 /* --- @destroy@ --- */
552 static void fsource_destroy(source
*s
)
554 fsource
*fs
= (fsource
*)s
;
556 file_destroy(&fs
->f
);
560 /* --- File source operations --- */
562 source_ops fsource_ops
= {
564 fsource_option
, fsource_read
, fsource_attach
, fsource_destroy
567 /*----- File target description -------------------------------------------*/
569 /* --- @option@ --- */
571 static int ftarget_option(target
*t
, scanner
*sc
)
573 ftarget
*ft
= (ftarget
*)t
;
574 return (file_option(ft ?
&ft
->f
: 0, sc
));
579 static target
*ftarget_read(scanner
*sc
)
584 if (!conf_prefix(sc
, "file"))
586 ft
= CREATE(ftarget
);
587 ft
->t
.ops
= &ftarget_ops
;
588 file_read(&ft
->f
, sc
);
589 file_desc(&ft
->f
, &d
);
590 ft
->t
.desc
= xstrdup(d
.buf
);
595 /* --- @create@ --- */
597 static endpt
*ftarget_create(target
*t
, const char *desc
)
599 ftarget
*ft
= (ftarget
*)t
;
600 endpt
*e
= file_endpt(&ft
->f
, desc
);
604 /* --- @destroy@ --- */
606 static void ftarget_destroy(target
*t
)
608 ftarget
*ft
= (ftarget
*)t
;
609 file_destroy(&ft
->f
);
614 /* --- File target operations --- */
616 target_ops ftarget_ops
= {
618 ftarget_option
, ftarget_read
, ftarget_create
, ftarget_destroy
621 /*----- That's all, folks -------------------------------------------------*/