1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/loaders.c,v $
5 * Purpose : Functions to load and unload the various
6 * configuration files. Also contains code to manage
7 * the list of active loaders, and to automatically
8 * unload files that are no longer in use.
10 * Copyright : Written by and Copyright (C) 2001-2014 the
11 * Privoxy team. https://www.privoxy.org/
13 * Based on the Internet Junkbuster originally written
14 * by and Copyright (C) 1997 Anonymous Coders and
15 * Junkbusters Corporation. http://www.junkbusters.com
17 * This program is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General
19 * Public License as published by the Free Software
20 * Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the GNU General Public
27 * License for more details.
29 * The GNU General Public License should be included with
30 * this file. If not, you can view it at
31 * http://www.gnu.org/copyleft/gpl.html
32 * or write to the Free Software Foundation, Inc., 59
33 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 *********************************************************************/
42 #include <sys/types.h>
66 * Currently active files.
67 * These are also entered in the main linked list of files.
71 static struct file_list *current_trustfile = NULL;
72 #endif /* def FEATURE_TRUST */
75 static int load_one_re_filterfile(struct client_state *csp, int fileid);
78 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
79 NULL, NULL, NULL, NULL, NULL,
80 NULL, NULL, NULL, NULL, NULL
83 /*********************************************************************
85 * Function : free_csp_resources
87 * Description : Frees memory referenced by the csp that isn't
88 * shared with other csps.
91 * 1 : csp = Current client state (buffers, headers, etc...)
95 *********************************************************************/
96 void free_csp_resources(struct client_state *csp)
98 freez(csp->ip_addr_str);
99 #ifdef FEATURE_CLIENT_TAGS
100 freez(csp->client_address);
102 freez(csp->listen_addr_str);
103 freez(csp->client_iob->buf);
104 freez(csp->iob->buf);
105 freez(csp->error_message);
107 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
110 unload_forward_spec(csp->fwd);
112 free_http_request(csp->http);
114 destroy_list(csp->headers);
115 #ifdef FEATURE_HTTPS_INSPECTION
116 destroy_list(csp->https_headers);
118 destroy_list(csp->tags);
119 #ifdef FEATURE_CLIENT_TAGS
120 destroy_list(csp->client_tags);
123 free_current_action(csp->action);
126 /*********************************************************************
130 * Description : Basically a mark and sweep garbage collector, it is run
131 * (by the parent thread) every once in a while to reclaim memory.
133 * It uses a mark and sweep strategy:
134 * 1) mark all files as inactive
136 * 2) check with each client:
137 * if it is active, mark its files as active
138 * if it is inactive, free its resources
140 * 3) free the resources of all of the files that
141 * are still marked as inactive (and are obsolete).
143 * N.B. files that are not obsolete don't have an unloader defined.
147 * Returns : The number of threads that are still active.
149 *********************************************************************/
150 unsigned int sweep(void)
152 struct file_list *fl, *nfl;
153 struct client_state *csp;
154 struct client_states *last_active, *client_list;
156 unsigned int active_threads = 0;
158 /* clear all of the file's active flags */
159 for (fl = files->next; NULL != fl; fl = fl->next)
164 last_active = clients;
165 client_list = clients->next;
167 while (NULL != client_list)
169 csp = &client_list->csp;
170 if (csp->flags & CSP_FLAG_ACTIVE)
172 /* Mark this client's files as active */
175 * Always have a configuration file.
176 * (Also note the slightly non-standard extra
179 csp->config->config_file_list->active = 1;
184 for (i = 0; i < MAX_AF_FILES; i++)
186 if (csp->actions_list[i])
188 csp->actions_list[i]->active = 1;
195 for (i = 0; i < MAX_AF_FILES; i++)
199 csp->rlist[i]->active = 1;
209 csp->tlist->active = 1;
211 #endif /* def FEATURE_TRUST */
215 last_active = client_list;
216 client_list = client_list->next;
220 * This client is not active. Free its resources.
223 last_active->next = client_list->next;
225 #ifdef FEATURE_STATISTICS
227 if (csp->flags & CSP_FLAG_REJECTED)
231 #endif /* def FEATURE_STATISTICS */
235 client_list = last_active->next;
244 if ((0 == fl->active) && (NULL != fl->unloader))
246 nfl->next = fl->next;
248 (fl->unloader)(fl->f);
262 return active_threads;
267 /*********************************************************************
269 * Function : check_file_changed
271 * Description : Helper function to check if a file needs reloading.
272 * If "current" is still current, return it. Otherwise
273 * allocates a new (zeroed) "struct file_list", fills
274 * in the disk file name and timestamp, and returns it.
277 * 1 : current = The file_list currently being used - will
278 * be checked to see if it is out of date.
279 * May be NULL (which is treated as out of
281 * 2 : filename = Name of file to check.
282 * 3 : newfl = New file list. [Output only]
283 * This will be set to NULL, OR a struct
284 * file_list newly allocated on the
285 * heap, with the filename and lastmodified
286 * fields filled, and all others zeroed.
288 * Returns : If file unchanged: 0 (and sets newfl == NULL)
289 * If file changed: 1 and sets newfl != NULL
290 * On error: 1 and sets newfl == NULL
292 *********************************************************************/
293 int check_file_changed(const struct file_list * current,
294 const char * filename,
295 struct file_list ** newfl)
297 struct file_list *fs;
298 struct stat statbuf[1];
302 if (stat(filename, statbuf) < 0)
304 /* Error, probably file not found. */
309 && (current->lastmodified == statbuf->st_mtime)
310 && (0 == strcmp(current->filename, filename)))
315 fs = zalloc_or_die(sizeof(struct file_list));
316 fs->filename = strdup_or_die(filename);
317 fs->lastmodified = statbuf->st_mtime;
324 /*********************************************************************
326 * Function : simple_read_line
328 * Description : Read a single line from a file and return it.
329 * This is basically a version of fgets() that malloc()s
330 * it's own line buffer. Note that the buffer will
331 * always be a multiple of BUFFER_SIZE bytes long.
332 * Therefore if you are going to keep the string for
333 * an extended period of time, you should probably
334 * strdup() it and free() the original, to save memory.
338 * 1 : dest = destination for newly malloc'd pointer to
339 * line data. Will be set to NULL on error.
340 * 2 : fp = File to read from
341 * 3 : newline = Standard for newlines in the file.
342 * Will be unchanged if it's value on input is not
344 * On output, may be changed from NEWLINE_UNKNOWN to
345 * actual convention in file.
347 * Returns : JB_ERR_OK on success
348 * JB_ERR_MEMORY on out-of-memory
349 * JB_ERR_FILE on EOF.
351 *********************************************************************/
352 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
355 size_t buflen = BUFFER_SIZE;
359 int realnewline = NEWLINE_UNKNOWN;
361 if (NULL == (buf = malloc(buflen)))
363 return JB_ERR_MEMORY;
369 * Character codes. If you have a weird compiler and the following are
370 * incorrect, you also need to fix NEWLINE() in loaders.h
372 #define CHAR_CR '\r' /* ASCII 13 */
373 #define CHAR_LF '\n' /* ASCII 10 */
394 else if (ch == CHAR_CR)
399 if (*newline == NEWLINE_UNKNOWN)
401 *newline = NEWLINE_DOS;
410 if (*newline == NEWLINE_UNKNOWN)
412 *newline = NEWLINE_MAC;
417 if (*newline == NEWLINE_UNKNOWN)
419 *newline = realnewline;
423 else if (ch == CHAR_LF)
427 if (*newline == NEWLINE_UNKNOWN)
429 *newline = NEWLINE_UNIX;
435 /* XXX: Why do we allow this anyway? */
445 buflen += BUFFER_SIZE;
446 if (NULL == (p = realloc(buf, buflen)))
449 return JB_ERR_MEMORY;
458 /*********************************************************************
460 * Function : edit_read_line
462 * Description : Read a single non-empty line from a file and return
463 * it. Trims comments, leading and trailing whitespace
464 * and respects escaping of newline and comment char.
465 * Provides the line in 2 alternative forms: raw and
467 * - raw is the raw data read from the file. If the
468 * line is not modified, then this should be written
470 * - prefix is any comments and blank lines that were
471 * read from the file. If the line is modified, then
472 * this should be written out to the file followed
473 * by the modified data. (If this string is non-empty
474 * then it will have a newline at the end).
475 * - data is the actual data that will be parsed
476 * further by appropriate routines.
477 * On EOF, the 3 strings will all be set to NULL and
478 * 0 will be returned.
481 * 1 : fp = File to read from
482 * 2 : raw_out = destination for newly malloc'd pointer to
483 * raw line data. May be NULL if you don't want it.
484 * 3 : prefix_out = destination for newly malloc'd pointer to
485 * comments. May be NULL if you don't want it.
486 * 4 : data_out = destination for newly malloc'd pointer to
487 * line data with comments and leading/trailing spaces
488 * removed, and line continuation performed. May be
489 * NULL if you don't want it.
490 * 5 : newline = Standard for newlines in the file.
491 * On input, set to value to use or NEWLINE_UNKNOWN.
492 * On output, may be changed from NEWLINE_UNKNOWN to
493 * actual convention in file. May be NULL if you
495 * 6 : line_number = Line number in file. In "lines" as
496 * reported by a text editor, not lines containing data.
498 * Returns : JB_ERR_OK on success
499 * JB_ERR_MEMORY on out-of-memory
500 * JB_ERR_FILE on EOF.
502 *********************************************************************/
503 jb_err edit_read_line(FILE *fp,
508 unsigned long *line_number)
510 char *p; /* Temporary pointer */
511 char *linebuf; /* Line read from file */
512 char *linestart; /* Start of linebuf, usually first non-whitespace char */
513 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
514 int is_empty = 1; /* Flag if not got any data yet */
515 char *raw = NULL; /* String to be stored in raw_out */
516 char *prefix = NULL; /* String to be stored in prefix_out */
517 char *data = NULL; /* String to be stored in data_out */
518 int scrapnewline; /* Used for (*newline) if newline==NULL */
519 jb_err rval = JB_ERR_OK;
522 assert(raw_out || data_out);
523 assert(newline == NULL
524 || *newline == NEWLINE_UNKNOWN
525 || *newline == NEWLINE_UNIX
526 || *newline == NEWLINE_DOS
527 || *newline == NEWLINE_MAC);
531 scrapnewline = NEWLINE_UNKNOWN;
532 newline = &scrapnewline;
535 /* Set output parameters to NULL */
549 /* Set string variables to new, empty strings. */
553 raw = strdup_or_die("");
557 prefix = strdup_or_die("");
561 data = strdup_or_die("");
564 /* Main loop. Loop while we need more data & it's not EOF. */
566 while ((contflag || is_empty)
567 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
575 string_append(&raw,linebuf);
576 if (string_append(&raw,NEWLINE(*newline)))
581 return JB_ERR_MEMORY;
585 /* Line continuation? Trim escape and set flag. */
586 p = linebuf + strlen(linebuf) - 1;
587 contflag = ((*linebuf != '\0') && (*p == '\\'));
593 /* Trim leading spaces if we're at the start of the line */
595 assert(NULL != data);
598 /* Trim leading spaces */
599 while (*linestart && isspace((int)(unsigned char)*linestart))
605 /* Handle comment characters. */
607 while ((p = strchr(p, '#')) != NULL)
609 /* Found a comment char.. */
610 if ((p != linebuf) && (*(p-1) == '\\'))
612 /* ..and it's escaped, left-shift the line over the escape. */
614 while ((*q = *(q + 1)) != '\0')
618 /* Now scan from just after the "#". */
622 /* Real comment. Save it... */
625 /* Special case: Line only contains a comment, so all the
626 * previous whitespace is considered part of the comment.
627 * Undo the whitespace skipping, if any.
634 string_append(&prefix,p);
635 if (string_append(&prefix, NEWLINE(*newline)))
640 return JB_ERR_MEMORY;
644 /* ... and chop off the rest of the line */
647 } /* END while (there's a # character) */
649 /* Write to the buffer */
653 if (string_append(&data, linestart))
658 return JB_ERR_MEMORY;
663 } /* END while(we need more data) */
665 /* Handle simple_read_line() errors - ignore EOF */
666 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
674 if (raw ? (*raw == '\0') : is_empty)
676 /* EOF and no data there. (Definition of "data" depends on whether
677 * the caller cares about "raw" or just "data").
688 /* Got at least some data */
690 /* Remove trailing whitespace */
703 *prefix_out = prefix;
722 /*********************************************************************
724 * Function : read_config_line
726 * Description : Read a single non-empty line from a file and return
727 * it. Trims comments, leading and trailing whitespace
728 * and respects escaping of newline and comment char.
731 * 1 : fp = File to read from
732 * 2 : linenum = linenumber in file
733 * 3 : buf = Pointer to a pointer to set to the data buffer.
735 * Returns : NULL on EOF or error
736 * Otherwise, returns buf.
738 *********************************************************************/
739 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
742 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
745 if (err == JB_ERR_MEMORY)
747 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
756 /*********************************************************************
758 * Function : unload_trustfile
760 * Description : Unloads a trustfile.
763 * 1 : f = the data structure associated with the trustfile.
767 *********************************************************************/
768 static void unload_trustfile(void *f)
770 struct block_spec *cur = (struct block_spec *)f;
771 struct block_spec *next;
777 free_pattern_spec(cur->url);
786 #ifdef FEATURE_GRACEFUL_TERMINATION
787 /*********************************************************************
789 * Function : unload_current_trust_file
791 * Description : Unloads current trust file - reset to state at
792 * beginning of program.
798 *********************************************************************/
799 void unload_current_trust_file(void)
801 if (current_trustfile)
803 current_trustfile->unloader = unload_trustfile;
804 current_trustfile = NULL;
807 #endif /* FEATURE_GRACEFUL_TERMINATION */
810 /*********************************************************************
812 * Function : load_trustfile
814 * Description : Read and parse a trustfile and add to files list.
817 * 1 : csp = Current client state (buffers, headers, etc...)
819 * Returns : 0 => Ok, everything else is an error.
821 *********************************************************************/
822 int load_trustfile(struct client_state *csp)
826 struct block_spec *b, *bl;
827 struct pattern_spec **tl;
831 struct file_list *fs;
832 unsigned long linenum = 0;
833 int trusted_referrers = 0;
835 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
837 /* No need to load */
838 csp->tlist = current_trustfile;
843 goto load_trustfile_error;
846 fs->f = bl = zalloc_or_die(sizeof(*bl));
848 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
850 goto load_trustfile_error;
852 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
854 tl = csp->config->trust_list;
856 while (read_config_line(fp, &linenum, &buf) != NULL)
875 while ((*p++ = *q++) != '\0')
881 /* skip blank lines */
888 /* allocate a new node */
889 b = zalloc_or_die(sizeof(*b));
891 /* add it to the list */
897 /* Save the URL pattern */
898 if (create_pattern_spec(b->url, buf))
901 goto load_trustfile_error;
905 * save a pointer to URL's spec in the list of trusted URL's, too
909 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
917 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
920 * FIXME: ... after Privoxy 3.0.4 is out.
922 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
923 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
924 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
925 MAX_TRUSTED_REFERRERS, trusted_referrers);
932 /* the old one is now obsolete */
933 if (current_trustfile)
935 current_trustfile->unloader = unload_trustfile;
938 fs->next = files->next;
940 current_trustfile = fs;
945 load_trustfile_error:
946 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
947 csp->config->trustfile);
952 #endif /* def FEATURE_TRUST */
955 /*********************************************************************
957 * Function : unload_re_filterfile
959 * Description : Unload the re_filter list by freeing all chained
960 * re_filterfile specs and their data.
963 * 1 : f = the data structure associated with the filterfile.
967 *********************************************************************/
968 static void unload_re_filterfile(void *f)
970 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
976 destroy_list(b->patterns);
977 pcrs_free_joblist(b->joblist);
979 freez(b->description);
988 /*********************************************************************
990 * Function : unload_forward_spec
992 * Description : Unload the forward spec settings by freeing all
993 * memory referenced by members and the memory for
997 * 1 : fwd = the forward spec.
1001 *********************************************************************/
1002 void unload_forward_spec(struct forward_spec *fwd)
1004 free_pattern_spec(fwd->url);
1005 freez(fwd->gateway_host);
1006 freez(fwd->forward_host);
1007 freez(fwd->auth_username);
1008 freez(fwd->auth_password);
1015 #ifdef FEATURE_GRACEFUL_TERMINATION
1016 /*********************************************************************
1018 * Function : unload_current_re_filterfile
1020 * Description : Unloads current re_filter file - reset to state at
1021 * beginning of program.
1027 *********************************************************************/
1028 void unload_current_re_filterfile(void)
1032 for (i = 0; i < MAX_AF_FILES; i++)
1034 if (current_re_filterfile[i])
1036 current_re_filterfile[i]->unloader = unload_re_filterfile;
1037 current_re_filterfile[i] = NULL;
1044 /*********************************************************************
1046 * Function : load_re_filterfiles
1048 * Description : Loads all the filterfiles.
1049 * Generate a chained list of re_filterfile_spec's from
1050 * the "FILTER: " blocks, compiling all their substitutions
1051 * into chained lists of pcrs_job structs.
1054 * 1 : csp = Current client state (buffers, headers, etc...)
1056 * Returns : 0 => Ok, everything else is an error.
1058 *********************************************************************/
1059 int load_re_filterfiles(struct client_state *csp)
1064 for (i = 0; i < MAX_AF_FILES; i++)
1066 if (csp->config->re_filterfile[i])
1068 result = load_one_re_filterfile(csp, i);
1074 else if (current_re_filterfile[i])
1076 current_re_filterfile[i]->unloader = unload_re_filterfile;
1077 current_re_filterfile[i] = NULL;
1085 /*********************************************************************
1087 * Function : load_one_re_filterfile
1089 * Description : Load a re_filterfile.
1090 * Generate a chained list of re_filterfile_spec's from
1091 * the "FILTER: " blocks, compiling all their substitutions
1092 * into chained lists of pcrs_job structs.
1095 * 1 : csp = Current client state (buffers, headers, etc...)
1097 * Returns : 0 => Ok, everything else is an error.
1099 *********************************************************************/
1100 int load_one_re_filterfile(struct client_state *csp, int fileid)
1104 struct re_filterfile_spec *new_bl, *bl = NULL;
1105 struct file_list *fs;
1108 unsigned long linenum = 0;
1109 pcrs_job *dummy, *lastjob = NULL;
1112 * No need to reload if unchanged
1114 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1116 csp->rlist[fileid] = current_re_filterfile[fileid];
1121 goto load_re_filterfile_error;
1125 * Open the file or fail
1127 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1129 goto load_re_filterfile_error;
1132 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1137 while (read_config_line(fp, &linenum, &buf) != NULL)
1139 enum filter_type new_filter = FT_INVALID_FILTER;
1141 if (strncmp(buf, "FILTER:", 7) == 0)
1143 new_filter = FT_CONTENT_FILTER;
1145 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1147 new_filter = FT_SERVER_HEADER_FILTER;
1149 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1151 new_filter = FT_CLIENT_HEADER_FILTER;
1153 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1155 new_filter = FT_CLIENT_HEADER_TAGGER;
1157 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1159 new_filter = FT_SERVER_HEADER_TAGGER;
1161 #ifdef FEATURE_EXTERNAL_FILTERS
1162 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1164 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1169 * If this is the head of a new filter block, make it a
1170 * re_filterfile spec of its own and chain it to the list:
1172 if (new_filter != FT_INVALID_FILTER)
1174 new_bl = zalloc_or_die(sizeof(*bl));
1175 if (new_filter == FT_CONTENT_FILTER)
1177 new_bl->name = chomp(buf + 7);
1179 #ifdef FEATURE_EXTERNAL_FILTERS
1180 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1182 new_bl->name = chomp(buf + 16);
1187 new_bl->name = chomp(buf + 21);
1189 new_bl->type = new_filter;
1192 * If a filter description is available,
1193 * encode it to HTML and save it.
1195 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1197 *new_bl->description++ = '\0';
1198 new_bl->description = html_encode(chomp(new_bl->description));
1199 if (NULL == new_bl->description)
1201 new_bl->description = strdup_or_die("Out of memory while "
1202 "encoding filter description to HTML");
1207 new_bl->description = strdup_or_die("No description available");
1210 new_bl->name = strdup_or_die(chomp(new_bl->name));
1213 * If this is the first filter block, chain it
1214 * to the file_list rather than its (nonexistent)
1228 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1229 #ifdef FEATURE_EXTENDED_STATISTICS
1230 register_filter_for_statistics(bl->name);
1236 #ifdef FEATURE_EXTERNAL_FILTERS
1237 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1240 /* Save the code as "pattern", but do not compile anything. */
1241 if (bl->patterns->first != NULL)
1243 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobs. "
1244 "Did you forget to escape a line break?",
1247 jb_error = enlist(bl->patterns, buf);
1248 if (JB_ERR_MEMORY == jb_error)
1250 log_error(LOG_LEVEL_FATAL,
1251 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1263 * Save the expression, make it a pcrs_job
1264 * and chain it into the current filter's joblist
1266 jb_error = enlist(bl->patterns, buf);
1267 if (JB_ERR_MEMORY == jb_error)
1269 log_error(LOG_LEVEL_FATAL,
1270 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1272 assert(JB_ERR_OK == jb_error);
1274 if (pcrs_job_is_dynamic(buf))
1277 * Dynamic pattern that might contain variables
1278 * and has to be recompiled for every request
1280 if (bl->joblist != NULL)
1282 pcrs_free_joblist(bl->joblist);
1286 log_error(LOG_LEVEL_RE_FILTER,
1287 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1291 else if (bl->dynamic)
1294 * A previous job was dynamic and as we
1295 * recompile the whole filter anyway, it
1296 * makes no sense to compile this job now.
1298 log_error(LOG_LEVEL_RE_FILTER,
1299 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1304 if ((dummy = pcrs_compile_command(buf, &pcrs_error)) == NULL)
1306 log_error(LOG_LEVEL_ERROR,
1307 "Adding re_filter job \'%s\' to filter %s failed: %s",
1308 buf, bl->name, pcrs_strerror(pcrs_error));
1314 if (bl->joblist == NULL)
1316 bl->joblist = dummy;
1318 else if (NULL != lastjob)
1320 lastjob->next = dummy;
1323 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1328 log_error(LOG_LEVEL_ERROR,
1329 "Ignoring job %s outside filter block in %s, line %lu",
1330 buf, csp->config->re_filterfile[fileid], linenum);
1338 * Schedule the now-obsolete old data for unloading
1340 if (NULL != current_re_filterfile[fileid])
1342 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1346 * Chain this file into the global list of loaded files
1348 fs->next = files->next;
1350 current_re_filterfile[fileid] = fs;
1351 csp->rlist[fileid] = fs;
1355 load_re_filterfile_error:
1356 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1357 csp->config->re_filterfile[fileid]);
1363 /*********************************************************************
1365 * Function : add_loader
1367 * Description : Called from `load_config'. Called once for each input
1368 * file found in config.
1371 * 1 : loader = pointer to a function that can parse and load
1372 * the appropriate config file.
1373 * 2 : config = The configuration_spec to add the loader to.
1377 *********************************************************************/
1378 void add_loader(int (*loader)(struct client_state *),
1379 struct configuration_spec * config)
1383 for (i = 0; i < NLOADERS; i++)
1385 if (config->loaders[i] == NULL)
1387 config->loaders[i] = loader;
1395 /*********************************************************************
1397 * Function : run_loader
1399 * Description : Called from `load_config' and `listen_loop'. This
1400 * function keeps the "csp" current with any file mods
1401 * since the last loop. If a file is unchanged, the
1402 * loader functions do NOT reload the file.
1405 * 1 : csp = Current client state (buffers, headers, etc...)
1406 * Must be non-null. Reads: "csp->config"
1407 * Writes: various data members.
1409 * Returns : 0 => Ok, everything else is an error.
1411 *********************************************************************/
1412 int run_loader(struct client_state *csp)
1417 for (i = 0; i < NLOADERS; i++)
1419 if (csp->config->loaders[i] == NULL)
1423 ret |= (csp->config->loaders[i])(csp);
1429 /*********************************************************************
1431 * Function : file_has_been_modified
1433 * Description : Helper function to check if a file has been changed
1436 * 1 : filename = The name of the file to check
1437 * 2 : last_known_modification = The time of the last known
1440 * Returns : TRUE if the file has been changed,
1443 *********************************************************************/
1444 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1446 struct stat statbuf[1];
1448 if (stat(filename, statbuf) < 0)
1450 /* Error, probably file not found which counts as change. */
1454 return (last_know_modification != statbuf->st_mtime);
1458 /*********************************************************************
1460 * Function : any_loaded_file_changed
1462 * Description : Helper function to check if any loaded file has been
1463 * changed since the time it has been loaded.
1465 * XXX: Should we cache the return value for x seconds?
1468 * 1 : files_to_check = List of files to check
1470 * Returns : TRUE if any file has been changed,
1473 *********************************************************************/
1474 int any_loaded_file_changed(const struct client_state *csp)
1476 const struct file_list *file_to_check = csp->config->config_file_list;
1479 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1484 for (i = 0; i < MAX_AF_FILES; i++)
1486 if (csp->actions_list[i])
1488 file_to_check = csp->actions_list[i];
1489 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1496 for (i = 0; i < MAX_AF_FILES; i++)
1500 file_to_check = csp->rlist[i];
1501 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1508 #ifdef FEATURE_TRUST
1511 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1516 #endif /* def FEATURE_TRUST */