Unix: Refactor tracked files

We need access to resource in order to free it.
This commit is contained in:
Ondrej Zajicek (work) 2018-11-13 18:13:11 +01:00
parent d0b4597842
commit c68ba7d093
3 changed files with 30 additions and 14 deletions

View file

@ -41,9 +41,9 @@ syslog_name:
log_file: log_file:
text { text {
FILE *f = tracked_fopen(new_config->pool, $1, "a"); struct rfile *f = rf_open(new_config->pool, $1, "a");
if (!f) cf_error("Unable to open log file `%s': %m", $1); if (!f) cf_error("Unable to open log file '%s': %m", $1);
$$ = f; $$ = rf_file(f);
} }
| SYSLOG syslog_name { $$ = NULL; new_config->syslog_name = $2; } | SYSLOG syslog_name { $$ = NULL; new_config->syslog_name = $2; }
| STDERR { $$ = stderr; } | STDERR { $$ = stderr; }
@ -77,9 +77,9 @@ conf: mrtdump_base ;
mrtdump_base: mrtdump_base:
MRTDUMP PROTOCOLS mrtdump_mask ';' { new_config->proto_default_mrtdump = $3; } MRTDUMP PROTOCOLS mrtdump_mask ';' { new_config->proto_default_mrtdump = $3; }
| MRTDUMP text ';' { | MRTDUMP text ';' {
FILE *f = tracked_fopen(new_config->pool, $2, "a"); struct rfile *f = rf_open(new_config->pool, $2, "a");
if (!f) cf_error("Unable to open MRTDump file '%s': %m", $2); if (!f) cf_error("Unable to open MRTDump file '%s': %m", $2);
new_config->mrtdump_file = fileno(f); new_config->mrtdump_file = rf_fileno(f);
} }
; ;

View file

@ -55,6 +55,7 @@
this to gen small latencies */ this to gen small latencies */
#define MAX_RX_STEPS 4 #define MAX_RX_STEPS 4
/* /*
* Tracked Files * Tracked Files
*/ */
@ -89,17 +90,29 @@ static struct resclass rf_class = {
NULL NULL
}; };
void * struct rfile *
tracked_fopen(pool *p, char *name, char *mode) rf_open(pool *p, char *name, char *mode)
{ {
FILE *f = fopen(name, mode); FILE *f = fopen(name, mode);
if (f) if (!f)
{ return NULL;
struct rfile *r = ralloc(p, &rf_class); struct rfile *r = ralloc(p, &rf_class);
r->f = f; r->f = f;
} return r;
return f; }
void *
rf_file(struct rfile *f)
{
return f->f;
}
int
rf_fileno(struct rfile *f)
{
return fileno(f->f);
} }

View file

@ -14,6 +14,7 @@
struct pool; struct pool;
struct iface; struct iface;
struct birdsock; struct birdsock;
struct rfile;
/* main.c */ /* main.c */
@ -102,7 +103,9 @@ void io_init(void);
void io_loop(void); void io_loop(void);
void io_log_dump(void); void io_log_dump(void);
int sk_open_unix(struct birdsock *s, char *name); int sk_open_unix(struct birdsock *s, char *name);
void *tracked_fopen(struct pool *, char *name, char *mode); struct rfile *rf_open(struct pool *, char *name, char *mode);
void *rf_file(struct rfile *f);
int rf_fileno(struct rfile *f);
void test_old_bird(char *path); void test_old_bird(char *path);
/* krt.c bits */ /* krt.c bits */