1 const char loaders_rcs[] = "$Id: loaders.c,v 1.105 2016/05/25 10:50:55 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/loaders.c,v $
6 * Purpose : Functions to load and unload the various
7 * configuration files. Also contains code to manage
8 * the list of active loaders, and to automatically
9 * unload files that are no longer in use.
11 * Copyright : Written by and Copyright (C) 2001-2014 the
12 * Privoxy team. http://www.privoxy.org/
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 *********************************************************************/
43 #include <sys/types.h>
50 #if !defined(_WIN32) && !defined(__OS2__)
66 const char loaders_h_rcs[] = LOADERS_H_VERSION;
69 * Currently active files.
70 * These are also entered in the main linked list of files.
74 static struct file_list *current_trustfile = NULL;
75 #endif /* def FEATURE_TRUST */
78 static int load_one_re_filterfile(struct client_state *csp, int fileid);
81 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
82 NULL, NULL, NULL, NULL, NULL,
83 NULL, NULL, NULL, NULL, NULL
87 /*********************************************************************
91 * Description : Basically a mark and sweep garbage collector, it is run
92 * (by the parent thread) every once in a while to reclaim memory.
94 * It uses a mark and sweep strategy:
95 * 1) mark all files as inactive
97 * 2) check with each client:
98 * if it is active, mark its files as active
99 * if it is inactive, free its resources
101 * 3) free the resources of all of the files that
102 * are still marked as inactive (and are obsolete).
104 * N.B. files that are not obsolete don't have an unloader defined.
108 * Returns : The number of threads that are still active.
110 *********************************************************************/
111 unsigned int sweep(void)
113 struct file_list *fl, *nfl;
114 struct client_state *csp;
115 struct client_states *last_active, *client_list;
117 unsigned int active_threads = 0;
119 /* clear all of the file's active flags */
120 for (fl = files->next; NULL != fl; fl = fl->next)
125 last_active = clients;
126 client_list = clients->next;
128 while (NULL != client_list)
130 csp = &client_list->csp;
131 if (csp->flags & CSP_FLAG_ACTIVE)
133 /* Mark this client's files as active */
136 * Always have a configuration file.
137 * (Also note the slightly non-standard extra
140 csp->config->config_file_list->active = 1;
145 for (i = 0; i < MAX_AF_FILES; i++)
147 if (csp->actions_list[i])
149 csp->actions_list[i]->active = 1;
156 for (i = 0; i < MAX_AF_FILES; i++)
160 csp->rlist[i]->active = 1;
170 csp->tlist->active = 1;
172 #endif /* def FEATURE_TRUST */
176 last_active = client_list;
177 client_list = client_list->next;
181 * This client is not active. Free its resources.
184 last_active->next = client_list->next;
186 freez(csp->ip_addr_str);
187 #ifdef FEATURE_CLIENT_TAGS
188 freez(csp->client_address);
190 freez(csp->listen_addr_str);
191 freez(csp->client_iob->buf);
192 freez(csp->iob->buf);
193 freez(csp->error_message);
195 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
198 unload_forward_spec(csp->fwd);
200 free_http_request(csp->http);
202 destroy_list(csp->headers);
203 destroy_list(csp->tags);
205 free_current_action(csp->action);
207 #ifdef FEATURE_STATISTICS
209 if (csp->flags & CSP_FLAG_REJECTED)
213 #endif /* def FEATURE_STATISTICS */
217 client_list = last_active->next;
226 if ((0 == fl->active) && (NULL != fl->unloader))
228 nfl->next = fl->next;
230 (fl->unloader)(fl->f);
244 return active_threads;
249 /*********************************************************************
251 * Function : check_file_changed
253 * Description : Helper function to check if a file needs reloading.
254 * If "current" is still current, return it. Otherwise
255 * allocates a new (zeroed) "struct file_list", fills
256 * in the disk file name and timestamp, and returns it.
259 * 1 : current = The file_list currently being used - will
260 * be checked to see if it is out of date.
261 * May be NULL (which is treated as out of
263 * 2 : filename = Name of file to check.
264 * 3 : newfl = New file list. [Output only]
265 * This will be set to NULL, OR a struct
266 * file_list newly allocated on the
267 * heap, with the filename and lastmodified
268 * fields filled, and all others zeroed.
270 * Returns : If file unchanged: 0 (and sets newfl == NULL)
271 * If file changed: 1 and sets newfl != NULL
272 * On error: 1 and sets newfl == NULL
274 *********************************************************************/
275 int check_file_changed(const struct file_list * current,
276 const char * filename,
277 struct file_list ** newfl)
279 struct file_list *fs;
280 struct stat statbuf[1];
284 if (stat(filename, statbuf) < 0)
286 /* Error, probably file not found. */
291 && (current->lastmodified == statbuf->st_mtime)
292 && (0 == strcmp(current->filename, filename)))
297 fs = zalloc_or_die(sizeof(struct file_list));
298 fs->filename = strdup_or_die(filename);
299 fs->lastmodified = statbuf->st_mtime;
301 if (fs->filename == NULL)
303 /* Out of memory error */
312 /*********************************************************************
314 * Function : simple_read_line
316 * Description : Read a single line from a file and return it.
317 * This is basically a version of fgets() that malloc()s
318 * it's own line buffer. Note that the buffer will
319 * always be a multiple of BUFFER_SIZE bytes long.
320 * Therefore if you are going to keep the string for
321 * an extended period of time, you should probably
322 * strdup() it and free() the original, to save memory.
326 * 1 : dest = destination for newly malloc'd pointer to
327 * line data. Will be set to NULL on error.
328 * 2 : fp = File to read from
329 * 3 : newline = Standard for newlines in the file.
330 * Will be unchanged if it's value on input is not
332 * On output, may be changed from NEWLINE_UNKNOWN to
333 * actual convention in file.
335 * Returns : JB_ERR_OK on success
336 * JB_ERR_MEMORY on out-of-memory
337 * JB_ERR_FILE on EOF.
339 *********************************************************************/
340 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
343 size_t buflen = BUFFER_SIZE;
347 int realnewline = NEWLINE_UNKNOWN;
349 if (NULL == (buf = malloc(buflen)))
351 return JB_ERR_MEMORY;
357 * Character codes. If you have a weird compiler and the following are
358 * incorrect, you also need to fix NEWLINE() in loaders.h
360 #define CHAR_CR '\r' /* ASCII 13 */
361 #define CHAR_LF '\n' /* ASCII 10 */
382 else if (ch == CHAR_CR)
387 if (*newline == NEWLINE_UNKNOWN)
389 *newline = NEWLINE_DOS;
398 if (*newline == NEWLINE_UNKNOWN)
400 *newline = NEWLINE_MAC;
405 if (*newline == NEWLINE_UNKNOWN)
407 *newline = realnewline;
411 else if (ch == CHAR_LF)
415 if (*newline == NEWLINE_UNKNOWN)
417 *newline = NEWLINE_UNIX;
423 /* XXX: Why do we allow this anyway? */
433 buflen += BUFFER_SIZE;
434 if (NULL == (p = realloc(buf, buflen)))
437 return JB_ERR_MEMORY;
446 /*********************************************************************
448 * Function : edit_read_line
450 * Description : Read a single non-empty line from a file and return
451 * it. Trims comments, leading and trailing whitespace
452 * and respects escaping of newline and comment char.
453 * Provides the line in 2 alternative forms: raw and
455 * - raw is the raw data read from the file. If the
456 * line is not modified, then this should be written
458 * - prefix is any comments and blank lines that were
459 * read from the file. If the line is modified, then
460 * this should be written out to the file followed
461 * by the modified data. (If this string is non-empty
462 * then it will have a newline at the end).
463 * - data is the actual data that will be parsed
464 * further by appropriate routines.
465 * On EOF, the 3 strings will all be set to NULL and
466 * 0 will be returned.
469 * 1 : fp = File to read from
470 * 2 : raw_out = destination for newly malloc'd pointer to
471 * raw line data. May be NULL if you don't want it.
472 * 3 : prefix_out = destination for newly malloc'd pointer to
473 * comments. May be NULL if you don't want it.
474 * 4 : data_out = destination for newly malloc'd pointer to
475 * line data with comments and leading/trailing spaces
476 * removed, and line continuation performed. May be
477 * NULL if you don't want it.
478 * 5 : newline = Standard for newlines in the file.
479 * On input, set to value to use or NEWLINE_UNKNOWN.
480 * On output, may be changed from NEWLINE_UNKNOWN to
481 * actual convention in file. May be NULL if you
483 * 6 : line_number = Line number in file. In "lines" as
484 * reported by a text editor, not lines containing data.
486 * Returns : JB_ERR_OK on success
487 * JB_ERR_MEMORY on out-of-memory
488 * JB_ERR_FILE on EOF.
490 *********************************************************************/
491 jb_err edit_read_line(FILE *fp,
496 unsigned long *line_number)
498 char *p; /* Temporary pointer */
499 char *linebuf; /* Line read from file */
500 char *linestart; /* Start of linebuf, usually first non-whitespace char */
501 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
502 int is_empty = 1; /* Flag if not got any data yet */
503 char *raw = NULL; /* String to be stored in raw_out */
504 char *prefix = NULL; /* String to be stored in prefix_out */
505 char *data = NULL; /* String to be stored in data_out */
506 int scrapnewline; /* Used for (*newline) if newline==NULL */
507 jb_err rval = JB_ERR_OK;
510 assert(raw_out || data_out);
511 assert(newline == NULL
512 || *newline == NEWLINE_UNKNOWN
513 || *newline == NEWLINE_UNIX
514 || *newline == NEWLINE_DOS
515 || *newline == NEWLINE_MAC);
519 scrapnewline = NEWLINE_UNKNOWN;
520 newline = &scrapnewline;
523 /* Set output parameters to NULL */
537 /* Set string variables to new, empty strings. */
541 raw = strdup_or_die("");
545 prefix = strdup_or_die("");
549 data = strdup_or_die("");
552 /* Main loop. Loop while we need more data & it's not EOF. */
554 while ((contflag || is_empty)
555 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
563 string_append(&raw,linebuf);
564 if (string_append(&raw,NEWLINE(*newline)))
569 return JB_ERR_MEMORY;
573 /* Line continuation? Trim escape and set flag. */
574 p = linebuf + strlen(linebuf) - 1;
575 contflag = ((*linebuf != '\0') && (*p == '\\'));
581 /* Trim leading spaces if we're at the start of the line */
583 assert(NULL != data);
586 /* Trim leading spaces */
587 while (*linestart && isspace((int)(unsigned char)*linestart))
593 /* Handle comment characters. */
595 while ((p = strchr(p, '#')) != NULL)
597 /* Found a comment char.. */
598 if ((p != linebuf) && (*(p-1) == '\\'))
600 /* ..and it's escaped, left-shift the line over the escape. */
602 while ((*q = *(q + 1)) != '\0')
606 /* Now scan from just after the "#". */
610 /* Real comment. Save it... */
613 /* Special case: Line only contains a comment, so all the
614 * previous whitespace is considered part of the comment.
615 * Undo the whitespace skipping, if any.
622 string_append(&prefix,p);
623 if (string_append(&prefix, NEWLINE(*newline)))
628 return JB_ERR_MEMORY;
632 /* ... and chop off the rest of the line */
635 } /* END while (there's a # character) */
637 /* Write to the buffer */
641 if (string_append(&data, linestart))
646 return JB_ERR_MEMORY;
651 } /* END while(we need more data) */
653 /* Handle simple_read_line() errors - ignore EOF */
654 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
662 if (raw ? (*raw == '\0') : is_empty)
664 /* EOF and no data there. (Definition of "data" depends on whether
665 * the caller cares about "raw" or just "data").
676 /* Got at least some data */
678 /* Remove trailing whitespace */
691 *prefix_out = prefix;
710 /*********************************************************************
712 * Function : read_config_line
714 * Description : Read a single non-empty line from a file and return
715 * it. Trims comments, leading and trailing whitespace
716 * and respects escaping of newline and comment char.
719 * 1 : fp = File to read from
720 * 2 : linenum = linenumber in file
721 * 3 : buf = Pointer to a pointer to set to the data buffer.
723 * Returns : NULL on EOF or error
724 * Otherwise, returns buf.
726 *********************************************************************/
727 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
730 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
733 if (err == JB_ERR_MEMORY)
735 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
744 /*********************************************************************
746 * Function : unload_trustfile
748 * Description : Unloads a trustfile.
751 * 1 : f = the data structure associated with the trustfile.
755 *********************************************************************/
756 static void unload_trustfile(void *f)
758 struct block_spec *cur = (struct block_spec *)f;
759 struct block_spec *next;
765 free_pattern_spec(cur->url);
774 #ifdef FEATURE_GRACEFUL_TERMINATION
775 /*********************************************************************
777 * Function : unload_current_trust_file
779 * Description : Unloads current trust file - reset to state at
780 * beginning of program.
786 *********************************************************************/
787 void unload_current_trust_file(void)
789 if (current_trustfile)
791 current_trustfile->unloader = unload_trustfile;
792 current_trustfile = NULL;
795 #endif /* FEATURE_GRACEFUL_TERMINATION */
798 /*********************************************************************
800 * Function : load_trustfile
802 * Description : Read and parse a trustfile and add to files list.
805 * 1 : csp = Current client state (buffers, headers, etc...)
807 * Returns : 0 => Ok, everything else is an error.
809 *********************************************************************/
810 int load_trustfile(struct client_state *csp)
814 struct block_spec *b, *bl;
815 struct pattern_spec **tl;
819 struct file_list *fs;
820 unsigned long linenum = 0;
821 int trusted_referrers = 0;
823 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
825 /* No need to load */
826 csp->tlist = current_trustfile;
831 goto load_trustfile_error;
834 fs->f = bl = zalloc_or_die(sizeof(*bl));
836 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
838 goto load_trustfile_error;
840 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
842 tl = csp->config->trust_list;
844 while (read_config_line(fp, &linenum, &buf) != NULL)
863 while ((*p++ = *q++) != '\0')
869 /* skip blank lines */
876 /* allocate a new node */
877 b = zalloc_or_die(sizeof(*b));
879 /* add it to the list */
885 /* Save the URL pattern */
886 if (create_pattern_spec(b->url, buf))
889 goto load_trustfile_error;
893 * save a pointer to URL's spec in the list of trusted URL's, too
897 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
905 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
908 * FIXME: ... after Privoxy 3.0.4 is out.
910 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
911 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
912 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
913 MAX_TRUSTED_REFERRERS, trusted_referrers);
920 /* the old one is now obsolete */
921 if (current_trustfile)
923 current_trustfile->unloader = unload_trustfile;
926 fs->next = files->next;
928 current_trustfile = fs;
933 load_trustfile_error:
934 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
935 csp->config->trustfile);
940 #endif /* def FEATURE_TRUST */
943 /*********************************************************************
945 * Function : unload_re_filterfile
947 * Description : Unload the re_filter list by freeing all chained
948 * re_filterfile specs and their data.
951 * 1 : f = the data structure associated with the filterfile.
955 *********************************************************************/
956 static void unload_re_filterfile(void *f)
958 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
964 destroy_list(b->patterns);
965 pcrs_free_joblist(b->joblist);
967 freez(b->description);
976 /*********************************************************************
978 * Function : unload_forward_spec
980 * Description : Unload the forward spec settings by freeing all
981 * memory referenced by members and the memory for
985 * 1 : fwd = the forward spec.
989 *********************************************************************/
990 void unload_forward_spec(struct forward_spec *fwd)
992 free_pattern_spec(fwd->url);
993 freez(fwd->gateway_host);
994 freez(fwd->forward_host);
1001 #ifdef FEATURE_GRACEFUL_TERMINATION
1002 /*********************************************************************
1004 * Function : unload_current_re_filterfile
1006 * Description : Unloads current re_filter file - reset to state at
1007 * beginning of program.
1013 *********************************************************************/
1014 void unload_current_re_filterfile(void)
1018 for (i = 0; i < MAX_AF_FILES; i++)
1020 if (current_re_filterfile[i])
1022 current_re_filterfile[i]->unloader = unload_re_filterfile;
1023 current_re_filterfile[i] = NULL;
1030 /*********************************************************************
1032 * Function : load_re_filterfiles
1034 * Description : Loads all the filterfiles.
1035 * Generate a chained list of re_filterfile_spec's from
1036 * the "FILTER: " blocks, compiling all their substitutions
1037 * into chained lists of pcrs_job structs.
1040 * 1 : csp = Current client state (buffers, headers, etc...)
1042 * Returns : 0 => Ok, everything else is an error.
1044 *********************************************************************/
1045 int load_re_filterfiles(struct client_state *csp)
1050 for (i = 0; i < MAX_AF_FILES; i++)
1052 if (csp->config->re_filterfile[i])
1054 result = load_one_re_filterfile(csp, i);
1060 else if (current_re_filterfile[i])
1062 current_re_filterfile[i]->unloader = unload_re_filterfile;
1063 current_re_filterfile[i] = NULL;
1071 /*********************************************************************
1073 * Function : load_one_re_filterfile
1075 * Description : Load a re_filterfile.
1076 * Generate a chained list of re_filterfile_spec's from
1077 * the "FILTER: " blocks, compiling all their substitutions
1078 * into chained lists of pcrs_job structs.
1081 * 1 : csp = Current client state (buffers, headers, etc...)
1083 * Returns : 0 => Ok, everything else is an error.
1085 *********************************************************************/
1086 int load_one_re_filterfile(struct client_state *csp, int fileid)
1090 struct re_filterfile_spec *new_bl, *bl = NULL;
1091 struct file_list *fs;
1095 unsigned long linenum = 0;
1096 pcrs_job *dummy, *lastjob = NULL;
1099 * No need to reload if unchanged
1101 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1103 csp->rlist[fileid] = current_re_filterfile[fileid];
1108 goto load_re_filterfile_error;
1112 * Open the file or fail
1114 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1116 goto load_re_filterfile_error;
1119 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1124 while (read_config_line(fp, &linenum, &buf) != NULL)
1126 enum filter_type new_filter = FT_INVALID_FILTER;
1128 if (strncmp(buf, "FILTER:", 7) == 0)
1130 new_filter = FT_CONTENT_FILTER;
1132 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1134 new_filter = FT_SERVER_HEADER_FILTER;
1136 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1138 new_filter = FT_CLIENT_HEADER_FILTER;
1140 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1142 new_filter = FT_CLIENT_HEADER_TAGGER;
1144 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1146 new_filter = FT_SERVER_HEADER_TAGGER;
1148 #ifdef FEATURE_EXTERNAL_FILTERS
1149 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1151 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1156 * If this is the head of a new filter block, make it a
1157 * re_filterfile spec of its own and chain it to the list:
1159 if (new_filter != FT_INVALID_FILTER)
1161 new_bl = zalloc_or_die(sizeof(*bl));
1162 if (new_filter == FT_CONTENT_FILTER)
1164 new_bl->name = chomp(buf + 7);
1166 #ifdef FEATURE_EXTERNAL_FILTERS
1167 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1169 new_bl->name = chomp(buf + 16);
1174 new_bl->name = chomp(buf + 21);
1176 new_bl->type = new_filter;
1179 * If a filter description is available,
1180 * encode it to HTML and save it.
1182 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1184 *new_bl->description++ = '\0';
1185 new_bl->description = html_encode(chomp(new_bl->description));
1186 if (NULL == new_bl->description)
1188 new_bl->description = strdup_or_die("Out of memory while "
1189 "encoding filter description to HTML");
1194 new_bl->description = strdup_or_die("No description available");
1197 new_bl->name = strdup_or_die(chomp(new_bl->name));
1200 * If this is the first filter block, chain it
1201 * to the file_list rather than its (nonexistant)
1215 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1221 #ifdef FEATURE_EXTERNAL_FILTERS
1222 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1224 /* Save the code as "pattern", but do not compile anything. */
1225 if (bl->patterns->first != NULL)
1227 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1228 "Did you forget to escape a line break?",
1231 error = enlist(bl->patterns, buf);
1232 if (JB_ERR_MEMORY == error)
1234 log_error(LOG_LEVEL_FATAL,
1235 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1245 * Save the expression, make it a pcrs_job
1246 * and chain it into the current filter's joblist
1248 error = enlist(bl->patterns, buf);
1249 if (JB_ERR_MEMORY == error)
1251 log_error(LOG_LEVEL_FATAL,
1252 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1254 assert(JB_ERR_OK == error);
1256 if (pcrs_job_is_dynamic(buf))
1259 * Dynamic pattern that might contain variables
1260 * and has to be recompiled for every request
1262 if (bl->joblist != NULL)
1264 pcrs_free_joblist(bl->joblist);
1268 log_error(LOG_LEVEL_RE_FILTER,
1269 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1273 else if (bl->dynamic)
1276 * A previous job was dynamic and as we
1277 * recompile the whole filter anyway, it
1278 * makes no sense to compile this job now.
1280 log_error(LOG_LEVEL_RE_FILTER,
1281 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1286 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1288 log_error(LOG_LEVEL_ERROR,
1289 "Adding re_filter job \'%s\' to filter %s failed: %s",
1290 buf, bl->name, pcrs_strerror(error));
1296 if (bl->joblist == NULL)
1298 bl->joblist = dummy;
1300 else if (NULL != lastjob)
1302 lastjob->next = dummy;
1305 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1310 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1311 buf, csp->config->re_filterfile[fileid], linenum);
1319 * Schedule the now-obsolete old data for unloading
1321 if (NULL != current_re_filterfile[fileid])
1323 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1327 * Chain this file into the global list of loaded files
1329 fs->next = files->next;
1331 current_re_filterfile[fileid] = fs;
1332 csp->rlist[fileid] = fs;
1336 load_re_filterfile_error:
1337 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1338 csp->config->re_filterfile[fileid]);
1344 /*********************************************************************
1346 * Function : add_loader
1348 * Description : Called from `load_config'. Called once for each input
1349 * file found in config.
1352 * 1 : loader = pointer to a function that can parse and load
1353 * the appropriate config file.
1354 * 2 : config = The configuration_spec to add the loader to.
1358 *********************************************************************/
1359 void add_loader(int (*loader)(struct client_state *),
1360 struct configuration_spec * config)
1364 for (i = 0; i < NLOADERS; i++)
1366 if (config->loaders[i] == NULL)
1368 config->loaders[i] = loader;
1376 /*********************************************************************
1378 * Function : run_loader
1380 * Description : Called from `load_config' and `listen_loop'. This
1381 * function keeps the "csp" current with any file mods
1382 * since the last loop. If a file is unchanged, the
1383 * loader functions do NOT reload the file.
1386 * 1 : csp = Current client state (buffers, headers, etc...)
1387 * Must be non-null. Reads: "csp->config"
1388 * Writes: various data members.
1390 * Returns : 0 => Ok, everything else is an error.
1392 *********************************************************************/
1393 int run_loader(struct client_state *csp)
1398 for (i = 0; i < NLOADERS; i++)
1400 if (csp->config->loaders[i] == NULL)
1404 ret |= (csp->config->loaders[i])(csp);
1410 /*********************************************************************
1412 * Function : file_has_been_modified
1414 * Description : Helper function to check if a file has been changed
1417 * 1 : filename = The name of the file to check
1418 * 2 : last_known_modification = The time of the last known
1421 * Returns : TRUE if the file has been changed,
1424 *********************************************************************/
1425 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1427 struct stat statbuf[1];
1429 if (stat(filename, statbuf) < 0)
1431 /* Error, probably file not found which counts as change. */
1435 return (last_know_modification != statbuf->st_mtime);
1439 /*********************************************************************
1441 * Function : any_loaded_file_changed
1443 * Description : Helper function to check if any loaded file has been
1444 * changed since the time it has been loaded.
1446 * XXX: Should we cache the return value for x seconds?
1449 * 1 : files_to_check = List of files to check
1451 * Returns : TRUE if any file has been changed,
1454 *********************************************************************/
1455 int any_loaded_file_changed(const struct client_state *csp)
1457 const struct file_list *file_to_check = csp->config->config_file_list;
1460 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1465 for (i = 0; i < MAX_AF_FILES; i++)
1467 if (csp->actions_list[i])
1469 file_to_check = csp->actions_list[i];
1470 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1477 for (i = 0; i < MAX_AF_FILES; i++)
1481 file_to_check = csp->rlist[i];
1482 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1489 #ifdef FEATURE_TRUST
1492 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1497 #endif /* def FEATURE_TRUST */