1 const char loaders_rcs[] = "$Id: loaders.c,v 1.107 2017/06/26 12:17:43 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
86 /*********************************************************************
88 * Function : free_csp_resources
90 * Description : Frees memory referenced by the csp that isn't
91 * shared with other csps.
94 * 1 : csp = Current client state (buffers, headers, etc...)
98 *********************************************************************/
99 void free_csp_resources(struct client_state *csp)
101 freez(csp->ip_addr_str);
102 #ifdef FEATURE_CLIENT_TAGS
103 freez(csp->client_address);
105 freez(csp->listen_addr_str);
106 freez(csp->client_iob->buf);
107 freez(csp->iob->buf);
108 freez(csp->error_message);
110 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
113 unload_forward_spec(csp->fwd);
115 free_http_request(csp->http);
117 destroy_list(csp->headers);
118 destroy_list(csp->tags);
120 free_current_action(csp->action);
123 /*********************************************************************
127 * Description : Basically a mark and sweep garbage collector, it is run
128 * (by the parent thread) every once in a while to reclaim memory.
130 * It uses a mark and sweep strategy:
131 * 1) mark all files as inactive
133 * 2) check with each client:
134 * if it is active, mark its files as active
135 * if it is inactive, free its resources
137 * 3) free the resources of all of the files that
138 * are still marked as inactive (and are obsolete).
140 * N.B. files that are not obsolete don't have an unloader defined.
144 * Returns : The number of threads that are still active.
146 *********************************************************************/
147 unsigned int sweep(void)
149 struct file_list *fl, *nfl;
150 struct client_state *csp;
151 struct client_states *last_active, *client_list;
153 unsigned int active_threads = 0;
155 /* clear all of the file's active flags */
156 for (fl = files->next; NULL != fl; fl = fl->next)
161 last_active = clients;
162 client_list = clients->next;
164 while (NULL != client_list)
166 csp = &client_list->csp;
167 if (csp->flags & CSP_FLAG_ACTIVE)
169 /* Mark this client's files as active */
172 * Always have a configuration file.
173 * (Also note the slightly non-standard extra
176 csp->config->config_file_list->active = 1;
181 for (i = 0; i < MAX_AF_FILES; i++)
183 if (csp->actions_list[i])
185 csp->actions_list[i]->active = 1;
192 for (i = 0; i < MAX_AF_FILES; i++)
196 csp->rlist[i]->active = 1;
206 csp->tlist->active = 1;
208 #endif /* def FEATURE_TRUST */
212 last_active = client_list;
213 client_list = client_list->next;
217 * This client is not active. Free its resources.
220 last_active->next = client_list->next;
222 #ifdef FEATURE_STATISTICS
224 if (csp->flags & CSP_FLAG_REJECTED)
228 #endif /* def FEATURE_STATISTICS */
232 client_list = last_active->next;
241 if ((0 == fl->active) && (NULL != fl->unloader))
243 nfl->next = fl->next;
245 (fl->unloader)(fl->f);
259 return active_threads;
264 /*********************************************************************
266 * Function : check_file_changed
268 * Description : Helper function to check if a file needs reloading.
269 * If "current" is still current, return it. Otherwise
270 * allocates a new (zeroed) "struct file_list", fills
271 * in the disk file name and timestamp, and returns it.
274 * 1 : current = The file_list currently being used - will
275 * be checked to see if it is out of date.
276 * May be NULL (which is treated as out of
278 * 2 : filename = Name of file to check.
279 * 3 : newfl = New file list. [Output only]
280 * This will be set to NULL, OR a struct
281 * file_list newly allocated on the
282 * heap, with the filename and lastmodified
283 * fields filled, and all others zeroed.
285 * Returns : If file unchanged: 0 (and sets newfl == NULL)
286 * If file changed: 1 and sets newfl != NULL
287 * On error: 1 and sets newfl == NULL
289 *********************************************************************/
290 int check_file_changed(const struct file_list * current,
291 const char * filename,
292 struct file_list ** newfl)
294 struct file_list *fs;
295 struct stat statbuf[1];
299 if (stat(filename, statbuf) < 0)
301 /* Error, probably file not found. */
306 && (current->lastmodified == statbuf->st_mtime)
307 && (0 == strcmp(current->filename, filename)))
312 fs = zalloc_or_die(sizeof(struct file_list));
313 fs->filename = strdup_or_die(filename);
314 fs->lastmodified = statbuf->st_mtime;
316 if (fs->filename == NULL)
318 /* Out of memory error */
327 /*********************************************************************
329 * Function : simple_read_line
331 * Description : Read a single line from a file and return it.
332 * This is basically a version of fgets() that malloc()s
333 * it's own line buffer. Note that the buffer will
334 * always be a multiple of BUFFER_SIZE bytes long.
335 * Therefore if you are going to keep the string for
336 * an extended period of time, you should probably
337 * strdup() it and free() the original, to save memory.
341 * 1 : dest = destination for newly malloc'd pointer to
342 * line data. Will be set to NULL on error.
343 * 2 : fp = File to read from
344 * 3 : newline = Standard for newlines in the file.
345 * Will be unchanged if it's value on input is not
347 * On output, may be changed from NEWLINE_UNKNOWN to
348 * actual convention in file.
350 * Returns : JB_ERR_OK on success
351 * JB_ERR_MEMORY on out-of-memory
352 * JB_ERR_FILE on EOF.
354 *********************************************************************/
355 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
358 size_t buflen = BUFFER_SIZE;
362 int realnewline = NEWLINE_UNKNOWN;
364 if (NULL == (buf = malloc(buflen)))
366 return JB_ERR_MEMORY;
372 * Character codes. If you have a weird compiler and the following are
373 * incorrect, you also need to fix NEWLINE() in loaders.h
375 #define CHAR_CR '\r' /* ASCII 13 */
376 #define CHAR_LF '\n' /* ASCII 10 */
397 else if (ch == CHAR_CR)
402 if (*newline == NEWLINE_UNKNOWN)
404 *newline = NEWLINE_DOS;
413 if (*newline == NEWLINE_UNKNOWN)
415 *newline = NEWLINE_MAC;
420 if (*newline == NEWLINE_UNKNOWN)
422 *newline = realnewline;
426 else if (ch == CHAR_LF)
430 if (*newline == NEWLINE_UNKNOWN)
432 *newline = NEWLINE_UNIX;
438 /* XXX: Why do we allow this anyway? */
448 buflen += BUFFER_SIZE;
449 if (NULL == (p = realloc(buf, buflen)))
452 return JB_ERR_MEMORY;
461 /*********************************************************************
463 * Function : edit_read_line
465 * Description : Read a single non-empty line from a file and return
466 * it. Trims comments, leading and trailing whitespace
467 * and respects escaping of newline and comment char.
468 * Provides the line in 2 alternative forms: raw and
470 * - raw is the raw data read from the file. If the
471 * line is not modified, then this should be written
473 * - prefix is any comments and blank lines that were
474 * read from the file. If the line is modified, then
475 * this should be written out to the file followed
476 * by the modified data. (If this string is non-empty
477 * then it will have a newline at the end).
478 * - data is the actual data that will be parsed
479 * further by appropriate routines.
480 * On EOF, the 3 strings will all be set to NULL and
481 * 0 will be returned.
484 * 1 : fp = File to read from
485 * 2 : raw_out = destination for newly malloc'd pointer to
486 * raw line data. May be NULL if you don't want it.
487 * 3 : prefix_out = destination for newly malloc'd pointer to
488 * comments. May be NULL if you don't want it.
489 * 4 : data_out = destination for newly malloc'd pointer to
490 * line data with comments and leading/trailing spaces
491 * removed, and line continuation performed. May be
492 * NULL if you don't want it.
493 * 5 : newline = Standard for newlines in the file.
494 * On input, set to value to use or NEWLINE_UNKNOWN.
495 * On output, may be changed from NEWLINE_UNKNOWN to
496 * actual convention in file. May be NULL if you
498 * 6 : line_number = Line number in file. In "lines" as
499 * reported by a text editor, not lines containing data.
501 * Returns : JB_ERR_OK on success
502 * JB_ERR_MEMORY on out-of-memory
503 * JB_ERR_FILE on EOF.
505 *********************************************************************/
506 jb_err edit_read_line(FILE *fp,
511 unsigned long *line_number)
513 char *p; /* Temporary pointer */
514 char *linebuf; /* Line read from file */
515 char *linestart; /* Start of linebuf, usually first non-whitespace char */
516 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
517 int is_empty = 1; /* Flag if not got any data yet */
518 char *raw = NULL; /* String to be stored in raw_out */
519 char *prefix = NULL; /* String to be stored in prefix_out */
520 char *data = NULL; /* String to be stored in data_out */
521 int scrapnewline; /* Used for (*newline) if newline==NULL */
522 jb_err rval = JB_ERR_OK;
525 assert(raw_out || data_out);
526 assert(newline == NULL
527 || *newline == NEWLINE_UNKNOWN
528 || *newline == NEWLINE_UNIX
529 || *newline == NEWLINE_DOS
530 || *newline == NEWLINE_MAC);
534 scrapnewline = NEWLINE_UNKNOWN;
535 newline = &scrapnewline;
538 /* Set output parameters to NULL */
552 /* Set string variables to new, empty strings. */
556 raw = strdup_or_die("");
560 prefix = strdup_or_die("");
564 data = strdup_or_die("");
567 /* Main loop. Loop while we need more data & it's not EOF. */
569 while ((contflag || is_empty)
570 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
578 string_append(&raw,linebuf);
579 if (string_append(&raw,NEWLINE(*newline)))
584 return JB_ERR_MEMORY;
588 /* Line continuation? Trim escape and set flag. */
589 p = linebuf + strlen(linebuf) - 1;
590 contflag = ((*linebuf != '\0') && (*p == '\\'));
596 /* Trim leading spaces if we're at the start of the line */
598 assert(NULL != data);
601 /* Trim leading spaces */
602 while (*linestart && isspace((int)(unsigned char)*linestart))
608 /* Handle comment characters. */
610 while ((p = strchr(p, '#')) != NULL)
612 /* Found a comment char.. */
613 if ((p != linebuf) && (*(p-1) == '\\'))
615 /* ..and it's escaped, left-shift the line over the escape. */
617 while ((*q = *(q + 1)) != '\0')
621 /* Now scan from just after the "#". */
625 /* Real comment. Save it... */
628 /* Special case: Line only contains a comment, so all the
629 * previous whitespace is considered part of the comment.
630 * Undo the whitespace skipping, if any.
637 string_append(&prefix,p);
638 if (string_append(&prefix, NEWLINE(*newline)))
643 return JB_ERR_MEMORY;
647 /* ... and chop off the rest of the line */
650 } /* END while (there's a # character) */
652 /* Write to the buffer */
656 if (string_append(&data, linestart))
661 return JB_ERR_MEMORY;
666 } /* END while(we need more data) */
668 /* Handle simple_read_line() errors - ignore EOF */
669 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
677 if (raw ? (*raw == '\0') : is_empty)
679 /* EOF and no data there. (Definition of "data" depends on whether
680 * the caller cares about "raw" or just "data").
691 /* Got at least some data */
693 /* Remove trailing whitespace */
706 *prefix_out = prefix;
725 /*********************************************************************
727 * Function : read_config_line
729 * Description : Read a single non-empty line from a file and return
730 * it. Trims comments, leading and trailing whitespace
731 * and respects escaping of newline and comment char.
734 * 1 : fp = File to read from
735 * 2 : linenum = linenumber in file
736 * 3 : buf = Pointer to a pointer to set to the data buffer.
738 * Returns : NULL on EOF or error
739 * Otherwise, returns buf.
741 *********************************************************************/
742 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
745 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
748 if (err == JB_ERR_MEMORY)
750 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
759 /*********************************************************************
761 * Function : unload_trustfile
763 * Description : Unloads a trustfile.
766 * 1 : f = the data structure associated with the trustfile.
770 *********************************************************************/
771 static void unload_trustfile(void *f)
773 struct block_spec *cur = (struct block_spec *)f;
774 struct block_spec *next;
780 free_pattern_spec(cur->url);
789 #ifdef FEATURE_GRACEFUL_TERMINATION
790 /*********************************************************************
792 * Function : unload_current_trust_file
794 * Description : Unloads current trust file - reset to state at
795 * beginning of program.
801 *********************************************************************/
802 void unload_current_trust_file(void)
804 if (current_trustfile)
806 current_trustfile->unloader = unload_trustfile;
807 current_trustfile = NULL;
810 #endif /* FEATURE_GRACEFUL_TERMINATION */
813 /*********************************************************************
815 * Function : load_trustfile
817 * Description : Read and parse a trustfile and add to files list.
820 * 1 : csp = Current client state (buffers, headers, etc...)
822 * Returns : 0 => Ok, everything else is an error.
824 *********************************************************************/
825 int load_trustfile(struct client_state *csp)
829 struct block_spec *b, *bl;
830 struct pattern_spec **tl;
834 struct file_list *fs;
835 unsigned long linenum = 0;
836 int trusted_referrers = 0;
838 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
840 /* No need to load */
841 csp->tlist = current_trustfile;
846 goto load_trustfile_error;
849 fs->f = bl = zalloc_or_die(sizeof(*bl));
851 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
853 goto load_trustfile_error;
855 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
857 tl = csp->config->trust_list;
859 while (read_config_line(fp, &linenum, &buf) != NULL)
878 while ((*p++ = *q++) != '\0')
884 /* skip blank lines */
891 /* allocate a new node */
892 b = zalloc_or_die(sizeof(*b));
894 /* add it to the list */
900 /* Save the URL pattern */
901 if (create_pattern_spec(b->url, buf))
904 goto load_trustfile_error;
908 * save a pointer to URL's spec in the list of trusted URL's, too
912 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
920 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
923 * FIXME: ... after Privoxy 3.0.4 is out.
925 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
926 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
927 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
928 MAX_TRUSTED_REFERRERS, trusted_referrers);
935 /* the old one is now obsolete */
936 if (current_trustfile)
938 current_trustfile->unloader = unload_trustfile;
941 fs->next = files->next;
943 current_trustfile = fs;
948 load_trustfile_error:
949 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
950 csp->config->trustfile);
955 #endif /* def FEATURE_TRUST */
958 /*********************************************************************
960 * Function : unload_re_filterfile
962 * Description : Unload the re_filter list by freeing all chained
963 * re_filterfile specs and their data.
966 * 1 : f = the data structure associated with the filterfile.
970 *********************************************************************/
971 static void unload_re_filterfile(void *f)
973 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
979 destroy_list(b->patterns);
980 pcrs_free_joblist(b->joblist);
982 freez(b->description);
991 /*********************************************************************
993 * Function : unload_forward_spec
995 * Description : Unload the forward spec settings by freeing all
996 * memory referenced by members and the memory for
1000 * 1 : fwd = the forward spec.
1004 *********************************************************************/
1005 void unload_forward_spec(struct forward_spec *fwd)
1007 free_pattern_spec(fwd->url);
1008 freez(fwd->gateway_host);
1009 freez(fwd->forward_host);
1016 #ifdef FEATURE_GRACEFUL_TERMINATION
1017 /*********************************************************************
1019 * Function : unload_current_re_filterfile
1021 * Description : Unloads current re_filter file - reset to state at
1022 * beginning of program.
1028 *********************************************************************/
1029 void unload_current_re_filterfile(void)
1033 for (i = 0; i < MAX_AF_FILES; i++)
1035 if (current_re_filterfile[i])
1037 current_re_filterfile[i]->unloader = unload_re_filterfile;
1038 current_re_filterfile[i] = NULL;
1045 /*********************************************************************
1047 * Function : load_re_filterfiles
1049 * Description : Loads all the filterfiles.
1050 * Generate a chained list of re_filterfile_spec's from
1051 * the "FILTER: " blocks, compiling all their substitutions
1052 * into chained lists of pcrs_job structs.
1055 * 1 : csp = Current client state (buffers, headers, etc...)
1057 * Returns : 0 => Ok, everything else is an error.
1059 *********************************************************************/
1060 int load_re_filterfiles(struct client_state *csp)
1065 for (i = 0; i < MAX_AF_FILES; i++)
1067 if (csp->config->re_filterfile[i])
1069 result = load_one_re_filterfile(csp, i);
1075 else if (current_re_filterfile[i])
1077 current_re_filterfile[i]->unloader = unload_re_filterfile;
1078 current_re_filterfile[i] = NULL;
1086 /*********************************************************************
1088 * Function : load_one_re_filterfile
1090 * Description : Load a re_filterfile.
1091 * Generate a chained list of re_filterfile_spec's from
1092 * the "FILTER: " blocks, compiling all their substitutions
1093 * into chained lists of pcrs_job structs.
1096 * 1 : csp = Current client state (buffers, headers, etc...)
1098 * Returns : 0 => Ok, everything else is an error.
1100 *********************************************************************/
1101 int load_one_re_filterfile(struct client_state *csp, int fileid)
1105 struct re_filterfile_spec *new_bl, *bl = NULL;
1106 struct file_list *fs;
1110 unsigned long linenum = 0;
1111 pcrs_job *dummy, *lastjob = NULL;
1114 * No need to reload if unchanged
1116 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1118 csp->rlist[fileid] = current_re_filterfile[fileid];
1123 goto load_re_filterfile_error;
1127 * Open the file or fail
1129 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1131 goto load_re_filterfile_error;
1134 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1139 while (read_config_line(fp, &linenum, &buf) != NULL)
1141 enum filter_type new_filter = FT_INVALID_FILTER;
1143 if (strncmp(buf, "FILTER:", 7) == 0)
1145 new_filter = FT_CONTENT_FILTER;
1147 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1149 new_filter = FT_SERVER_HEADER_FILTER;
1151 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1153 new_filter = FT_CLIENT_HEADER_FILTER;
1155 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1157 new_filter = FT_CLIENT_HEADER_TAGGER;
1159 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1161 new_filter = FT_SERVER_HEADER_TAGGER;
1163 #ifdef FEATURE_EXTERNAL_FILTERS
1164 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1166 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1171 * If this is the head of a new filter block, make it a
1172 * re_filterfile spec of its own and chain it to the list:
1174 if (new_filter != FT_INVALID_FILTER)
1176 new_bl = zalloc_or_die(sizeof(*bl));
1177 if (new_filter == FT_CONTENT_FILTER)
1179 new_bl->name = chomp(buf + 7);
1181 #ifdef FEATURE_EXTERNAL_FILTERS
1182 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1184 new_bl->name = chomp(buf + 16);
1189 new_bl->name = chomp(buf + 21);
1191 new_bl->type = new_filter;
1194 * If a filter description is available,
1195 * encode it to HTML and save it.
1197 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1199 *new_bl->description++ = '\0';
1200 new_bl->description = html_encode(chomp(new_bl->description));
1201 if (NULL == new_bl->description)
1203 new_bl->description = strdup_or_die("Out of memory while "
1204 "encoding filter description to HTML");
1209 new_bl->description = strdup_or_die("No description available");
1212 new_bl->name = strdup_or_die(chomp(new_bl->name));
1215 * If this is the first filter block, chain it
1216 * to the file_list rather than its (nonexistant)
1230 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1236 #ifdef FEATURE_EXTERNAL_FILTERS
1237 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1239 /* Save the code as "pattern", but do not compile anything. */
1240 if (bl->patterns->first != NULL)
1242 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1243 "Did you forget to escape a line break?",
1246 error = enlist(bl->patterns, buf);
1247 if (JB_ERR_MEMORY == error)
1249 log_error(LOG_LEVEL_FATAL,
1250 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1260 * Save the expression, make it a pcrs_job
1261 * and chain it into the current filter's joblist
1263 error = enlist(bl->patterns, buf);
1264 if (JB_ERR_MEMORY == error)
1266 log_error(LOG_LEVEL_FATAL,
1267 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1269 assert(JB_ERR_OK == error);
1271 if (pcrs_job_is_dynamic(buf))
1274 * Dynamic pattern that might contain variables
1275 * and has to be recompiled for every request
1277 if (bl->joblist != NULL)
1279 pcrs_free_joblist(bl->joblist);
1283 log_error(LOG_LEVEL_RE_FILTER,
1284 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1288 else if (bl->dynamic)
1291 * A previous job was dynamic and as we
1292 * recompile the whole filter anyway, it
1293 * makes no sense to compile this job now.
1295 log_error(LOG_LEVEL_RE_FILTER,
1296 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1301 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1303 log_error(LOG_LEVEL_ERROR,
1304 "Adding re_filter job \'%s\' to filter %s failed: %s",
1305 buf, bl->name, pcrs_strerror(error));
1311 if (bl->joblist == NULL)
1313 bl->joblist = dummy;
1315 else if (NULL != lastjob)
1317 lastjob->next = dummy;
1320 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1325 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1326 buf, csp->config->re_filterfile[fileid], linenum);
1334 * Schedule the now-obsolete old data for unloading
1336 if (NULL != current_re_filterfile[fileid])
1338 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1342 * Chain this file into the global list of loaded files
1344 fs->next = files->next;
1346 current_re_filterfile[fileid] = fs;
1347 csp->rlist[fileid] = fs;
1351 load_re_filterfile_error:
1352 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1353 csp->config->re_filterfile[fileid]);
1359 /*********************************************************************
1361 * Function : add_loader
1363 * Description : Called from `load_config'. Called once for each input
1364 * file found in config.
1367 * 1 : loader = pointer to a function that can parse and load
1368 * the appropriate config file.
1369 * 2 : config = The configuration_spec to add the loader to.
1373 *********************************************************************/
1374 void add_loader(int (*loader)(struct client_state *),
1375 struct configuration_spec * config)
1379 for (i = 0; i < NLOADERS; i++)
1381 if (config->loaders[i] == NULL)
1383 config->loaders[i] = loader;
1391 /*********************************************************************
1393 * Function : run_loader
1395 * Description : Called from `load_config' and `listen_loop'. This
1396 * function keeps the "csp" current with any file mods
1397 * since the last loop. If a file is unchanged, the
1398 * loader functions do NOT reload the file.
1401 * 1 : csp = Current client state (buffers, headers, etc...)
1402 * Must be non-null. Reads: "csp->config"
1403 * Writes: various data members.
1405 * Returns : 0 => Ok, everything else is an error.
1407 *********************************************************************/
1408 int run_loader(struct client_state *csp)
1413 for (i = 0; i < NLOADERS; i++)
1415 if (csp->config->loaders[i] == NULL)
1419 ret |= (csp->config->loaders[i])(csp);
1425 /*********************************************************************
1427 * Function : file_has_been_modified
1429 * Description : Helper function to check if a file has been changed
1432 * 1 : filename = The name of the file to check
1433 * 2 : last_known_modification = The time of the last known
1436 * Returns : TRUE if the file has been changed,
1439 *********************************************************************/
1440 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1442 struct stat statbuf[1];
1444 if (stat(filename, statbuf) < 0)
1446 /* Error, probably file not found which counts as change. */
1450 return (last_know_modification != statbuf->st_mtime);
1454 /*********************************************************************
1456 * Function : any_loaded_file_changed
1458 * Description : Helper function to check if any loaded file has been
1459 * changed since the time it has been loaded.
1461 * XXX: Should we cache the return value for x seconds?
1464 * 1 : files_to_check = List of files to check
1466 * Returns : TRUE if any file has been changed,
1469 *********************************************************************/
1470 int any_loaded_file_changed(const struct client_state *csp)
1472 const struct file_list *file_to_check = csp->config->config_file_list;
1475 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1480 for (i = 0; i < MAX_AF_FILES; i++)
1482 if (csp->actions_list[i])
1484 file_to_check = csp->actions_list[i];
1485 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1492 for (i = 0; i < MAX_AF_FILES; i++)
1496 file_to_check = csp->rlist[i];
1497 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1504 #ifdef FEATURE_TRUST
1507 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1512 #endif /* def FEATURE_TRUST */