1 const char loaders_rcs[] = "$Id: loaders.c,v 1.106 2016/12/24 16:00:49 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 free_csp_resources(csp);
224 #ifdef FEATURE_STATISTICS
226 if (csp->flags & CSP_FLAG_REJECTED)
230 #endif /* def FEATURE_STATISTICS */
234 client_list = last_active->next;
243 if ((0 == fl->active) && (NULL != fl->unloader))
245 nfl->next = fl->next;
247 (fl->unloader)(fl->f);
261 return active_threads;
266 /*********************************************************************
268 * Function : check_file_changed
270 * Description : Helper function to check if a file needs reloading.
271 * If "current" is still current, return it. Otherwise
272 * allocates a new (zeroed) "struct file_list", fills
273 * in the disk file name and timestamp, and returns it.
276 * 1 : current = The file_list currently being used - will
277 * be checked to see if it is out of date.
278 * May be NULL (which is treated as out of
280 * 2 : filename = Name of file to check.
281 * 3 : newfl = New file list. [Output only]
282 * This will be set to NULL, OR a struct
283 * file_list newly allocated on the
284 * heap, with the filename and lastmodified
285 * fields filled, and all others zeroed.
287 * Returns : If file unchanged: 0 (and sets newfl == NULL)
288 * If file changed: 1 and sets newfl != NULL
289 * On error: 1 and sets newfl == NULL
291 *********************************************************************/
292 int check_file_changed(const struct file_list * current,
293 const char * filename,
294 struct file_list ** newfl)
296 struct file_list *fs;
297 struct stat statbuf[1];
301 if (stat(filename, statbuf) < 0)
303 /* Error, probably file not found. */
308 && (current->lastmodified == statbuf->st_mtime)
309 && (0 == strcmp(current->filename, filename)))
314 fs = zalloc_or_die(sizeof(struct file_list));
315 fs->filename = strdup_or_die(filename);
316 fs->lastmodified = statbuf->st_mtime;
318 if (fs->filename == NULL)
320 /* Out of memory error */
329 /*********************************************************************
331 * Function : simple_read_line
333 * Description : Read a single line from a file and return it.
334 * This is basically a version of fgets() that malloc()s
335 * it's own line buffer. Note that the buffer will
336 * always be a multiple of BUFFER_SIZE bytes long.
337 * Therefore if you are going to keep the string for
338 * an extended period of time, you should probably
339 * strdup() it and free() the original, to save memory.
343 * 1 : dest = destination for newly malloc'd pointer to
344 * line data. Will be set to NULL on error.
345 * 2 : fp = File to read from
346 * 3 : newline = Standard for newlines in the file.
347 * Will be unchanged if it's value on input is not
349 * On output, may be changed from NEWLINE_UNKNOWN to
350 * actual convention in file.
352 * Returns : JB_ERR_OK on success
353 * JB_ERR_MEMORY on out-of-memory
354 * JB_ERR_FILE on EOF.
356 *********************************************************************/
357 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
360 size_t buflen = BUFFER_SIZE;
364 int realnewline = NEWLINE_UNKNOWN;
366 if (NULL == (buf = malloc(buflen)))
368 return JB_ERR_MEMORY;
374 * Character codes. If you have a weird compiler and the following are
375 * incorrect, you also need to fix NEWLINE() in loaders.h
377 #define CHAR_CR '\r' /* ASCII 13 */
378 #define CHAR_LF '\n' /* ASCII 10 */
399 else if (ch == CHAR_CR)
404 if (*newline == NEWLINE_UNKNOWN)
406 *newline = NEWLINE_DOS;
415 if (*newline == NEWLINE_UNKNOWN)
417 *newline = NEWLINE_MAC;
422 if (*newline == NEWLINE_UNKNOWN)
424 *newline = realnewline;
428 else if (ch == CHAR_LF)
432 if (*newline == NEWLINE_UNKNOWN)
434 *newline = NEWLINE_UNIX;
440 /* XXX: Why do we allow this anyway? */
450 buflen += BUFFER_SIZE;
451 if (NULL == (p = realloc(buf, buflen)))
454 return JB_ERR_MEMORY;
463 /*********************************************************************
465 * Function : edit_read_line
467 * Description : Read a single non-empty line from a file and return
468 * it. Trims comments, leading and trailing whitespace
469 * and respects escaping of newline and comment char.
470 * Provides the line in 2 alternative forms: raw and
472 * - raw is the raw data read from the file. If the
473 * line is not modified, then this should be written
475 * - prefix is any comments and blank lines that were
476 * read from the file. If the line is modified, then
477 * this should be written out to the file followed
478 * by the modified data. (If this string is non-empty
479 * then it will have a newline at the end).
480 * - data is the actual data that will be parsed
481 * further by appropriate routines.
482 * On EOF, the 3 strings will all be set to NULL and
483 * 0 will be returned.
486 * 1 : fp = File to read from
487 * 2 : raw_out = destination for newly malloc'd pointer to
488 * raw line data. May be NULL if you don't want it.
489 * 3 : prefix_out = destination for newly malloc'd pointer to
490 * comments. May be NULL if you don't want it.
491 * 4 : data_out = destination for newly malloc'd pointer to
492 * line data with comments and leading/trailing spaces
493 * removed, and line continuation performed. May be
494 * NULL if you don't want it.
495 * 5 : newline = Standard for newlines in the file.
496 * On input, set to value to use or NEWLINE_UNKNOWN.
497 * On output, may be changed from NEWLINE_UNKNOWN to
498 * actual convention in file. May be NULL if you
500 * 6 : line_number = Line number in file. In "lines" as
501 * reported by a text editor, not lines containing data.
503 * Returns : JB_ERR_OK on success
504 * JB_ERR_MEMORY on out-of-memory
505 * JB_ERR_FILE on EOF.
507 *********************************************************************/
508 jb_err edit_read_line(FILE *fp,
513 unsigned long *line_number)
515 char *p; /* Temporary pointer */
516 char *linebuf; /* Line read from file */
517 char *linestart; /* Start of linebuf, usually first non-whitespace char */
518 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
519 int is_empty = 1; /* Flag if not got any data yet */
520 char *raw = NULL; /* String to be stored in raw_out */
521 char *prefix = NULL; /* String to be stored in prefix_out */
522 char *data = NULL; /* String to be stored in data_out */
523 int scrapnewline; /* Used for (*newline) if newline==NULL */
524 jb_err rval = JB_ERR_OK;
527 assert(raw_out || data_out);
528 assert(newline == NULL
529 || *newline == NEWLINE_UNKNOWN
530 || *newline == NEWLINE_UNIX
531 || *newline == NEWLINE_DOS
532 || *newline == NEWLINE_MAC);
536 scrapnewline = NEWLINE_UNKNOWN;
537 newline = &scrapnewline;
540 /* Set output parameters to NULL */
554 /* Set string variables to new, empty strings. */
558 raw = strdup_or_die("");
562 prefix = strdup_or_die("");
566 data = strdup_or_die("");
569 /* Main loop. Loop while we need more data & it's not EOF. */
571 while ((contflag || is_empty)
572 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
580 string_append(&raw,linebuf);
581 if (string_append(&raw,NEWLINE(*newline)))
586 return JB_ERR_MEMORY;
590 /* Line continuation? Trim escape and set flag. */
591 p = linebuf + strlen(linebuf) - 1;
592 contflag = ((*linebuf != '\0') && (*p == '\\'));
598 /* Trim leading spaces if we're at the start of the line */
600 assert(NULL != data);
603 /* Trim leading spaces */
604 while (*linestart && isspace((int)(unsigned char)*linestart))
610 /* Handle comment characters. */
612 while ((p = strchr(p, '#')) != NULL)
614 /* Found a comment char.. */
615 if ((p != linebuf) && (*(p-1) == '\\'))
617 /* ..and it's escaped, left-shift the line over the escape. */
619 while ((*q = *(q + 1)) != '\0')
623 /* Now scan from just after the "#". */
627 /* Real comment. Save it... */
630 /* Special case: Line only contains a comment, so all the
631 * previous whitespace is considered part of the comment.
632 * Undo the whitespace skipping, if any.
639 string_append(&prefix,p);
640 if (string_append(&prefix, NEWLINE(*newline)))
645 return JB_ERR_MEMORY;
649 /* ... and chop off the rest of the line */
652 } /* END while (there's a # character) */
654 /* Write to the buffer */
658 if (string_append(&data, linestart))
663 return JB_ERR_MEMORY;
668 } /* END while(we need more data) */
670 /* Handle simple_read_line() errors - ignore EOF */
671 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
679 if (raw ? (*raw == '\0') : is_empty)
681 /* EOF and no data there. (Definition of "data" depends on whether
682 * the caller cares about "raw" or just "data").
693 /* Got at least some data */
695 /* Remove trailing whitespace */
708 *prefix_out = prefix;
727 /*********************************************************************
729 * Function : read_config_line
731 * Description : Read a single non-empty line from a file and return
732 * it. Trims comments, leading and trailing whitespace
733 * and respects escaping of newline and comment char.
736 * 1 : fp = File to read from
737 * 2 : linenum = linenumber in file
738 * 3 : buf = Pointer to a pointer to set to the data buffer.
740 * Returns : NULL on EOF or error
741 * Otherwise, returns buf.
743 *********************************************************************/
744 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
747 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
750 if (err == JB_ERR_MEMORY)
752 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
761 /*********************************************************************
763 * Function : unload_trustfile
765 * Description : Unloads a trustfile.
768 * 1 : f = the data structure associated with the trustfile.
772 *********************************************************************/
773 static void unload_trustfile(void *f)
775 struct block_spec *cur = (struct block_spec *)f;
776 struct block_spec *next;
782 free_pattern_spec(cur->url);
791 #ifdef FEATURE_GRACEFUL_TERMINATION
792 /*********************************************************************
794 * Function : unload_current_trust_file
796 * Description : Unloads current trust file - reset to state at
797 * beginning of program.
803 *********************************************************************/
804 void unload_current_trust_file(void)
806 if (current_trustfile)
808 current_trustfile->unloader = unload_trustfile;
809 current_trustfile = NULL;
812 #endif /* FEATURE_GRACEFUL_TERMINATION */
815 /*********************************************************************
817 * Function : load_trustfile
819 * Description : Read and parse a trustfile and add to files list.
822 * 1 : csp = Current client state (buffers, headers, etc...)
824 * Returns : 0 => Ok, everything else is an error.
826 *********************************************************************/
827 int load_trustfile(struct client_state *csp)
831 struct block_spec *b, *bl;
832 struct pattern_spec **tl;
836 struct file_list *fs;
837 unsigned long linenum = 0;
838 int trusted_referrers = 0;
840 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
842 /* No need to load */
843 csp->tlist = current_trustfile;
848 goto load_trustfile_error;
851 fs->f = bl = zalloc_or_die(sizeof(*bl));
853 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
855 goto load_trustfile_error;
857 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
859 tl = csp->config->trust_list;
861 while (read_config_line(fp, &linenum, &buf) != NULL)
880 while ((*p++ = *q++) != '\0')
886 /* skip blank lines */
893 /* allocate a new node */
894 b = zalloc_or_die(sizeof(*b));
896 /* add it to the list */
902 /* Save the URL pattern */
903 if (create_pattern_spec(b->url, buf))
906 goto load_trustfile_error;
910 * save a pointer to URL's spec in the list of trusted URL's, too
914 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
922 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
925 * FIXME: ... after Privoxy 3.0.4 is out.
927 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
928 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
929 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
930 MAX_TRUSTED_REFERRERS, trusted_referrers);
937 /* the old one is now obsolete */
938 if (current_trustfile)
940 current_trustfile->unloader = unload_trustfile;
943 fs->next = files->next;
945 current_trustfile = fs;
950 load_trustfile_error:
951 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
952 csp->config->trustfile);
957 #endif /* def FEATURE_TRUST */
960 /*********************************************************************
962 * Function : unload_re_filterfile
964 * Description : Unload the re_filter list by freeing all chained
965 * re_filterfile specs and their data.
968 * 1 : f = the data structure associated with the filterfile.
972 *********************************************************************/
973 static void unload_re_filterfile(void *f)
975 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
981 destroy_list(b->patterns);
982 pcrs_free_joblist(b->joblist);
984 freez(b->description);
993 /*********************************************************************
995 * Function : unload_forward_spec
997 * Description : Unload the forward spec settings by freeing all
998 * memory referenced by members and the memory for
1002 * 1 : fwd = the forward spec.
1006 *********************************************************************/
1007 void unload_forward_spec(struct forward_spec *fwd)
1009 free_pattern_spec(fwd->url);
1010 freez(fwd->gateway_host);
1011 freez(fwd->forward_host);
1018 #ifdef FEATURE_GRACEFUL_TERMINATION
1019 /*********************************************************************
1021 * Function : unload_current_re_filterfile
1023 * Description : Unloads current re_filter file - reset to state at
1024 * beginning of program.
1030 *********************************************************************/
1031 void unload_current_re_filterfile(void)
1035 for (i = 0; i < MAX_AF_FILES; i++)
1037 if (current_re_filterfile[i])
1039 current_re_filterfile[i]->unloader = unload_re_filterfile;
1040 current_re_filterfile[i] = NULL;
1047 /*********************************************************************
1049 * Function : load_re_filterfiles
1051 * Description : Loads all the filterfiles.
1052 * Generate a chained list of re_filterfile_spec's from
1053 * the "FILTER: " blocks, compiling all their substitutions
1054 * into chained lists of pcrs_job structs.
1057 * 1 : csp = Current client state (buffers, headers, etc...)
1059 * Returns : 0 => Ok, everything else is an error.
1061 *********************************************************************/
1062 int load_re_filterfiles(struct client_state *csp)
1067 for (i = 0; i < MAX_AF_FILES; i++)
1069 if (csp->config->re_filterfile[i])
1071 result = load_one_re_filterfile(csp, i);
1077 else if (current_re_filterfile[i])
1079 current_re_filterfile[i]->unloader = unload_re_filterfile;
1080 current_re_filterfile[i] = NULL;
1088 /*********************************************************************
1090 * Function : load_one_re_filterfile
1092 * Description : Load a re_filterfile.
1093 * Generate a chained list of re_filterfile_spec's from
1094 * the "FILTER: " blocks, compiling all their substitutions
1095 * into chained lists of pcrs_job structs.
1098 * 1 : csp = Current client state (buffers, headers, etc...)
1100 * Returns : 0 => Ok, everything else is an error.
1102 *********************************************************************/
1103 int load_one_re_filterfile(struct client_state *csp, int fileid)
1107 struct re_filterfile_spec *new_bl, *bl = NULL;
1108 struct file_list *fs;
1112 unsigned long linenum = 0;
1113 pcrs_job *dummy, *lastjob = NULL;
1116 * No need to reload if unchanged
1118 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1120 csp->rlist[fileid] = current_re_filterfile[fileid];
1125 goto load_re_filterfile_error;
1129 * Open the file or fail
1131 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1133 goto load_re_filterfile_error;
1136 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1141 while (read_config_line(fp, &linenum, &buf) != NULL)
1143 enum filter_type new_filter = FT_INVALID_FILTER;
1145 if (strncmp(buf, "FILTER:", 7) == 0)
1147 new_filter = FT_CONTENT_FILTER;
1149 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1151 new_filter = FT_SERVER_HEADER_FILTER;
1153 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1155 new_filter = FT_CLIENT_HEADER_FILTER;
1157 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1159 new_filter = FT_CLIENT_HEADER_TAGGER;
1161 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1163 new_filter = FT_SERVER_HEADER_TAGGER;
1165 #ifdef FEATURE_EXTERNAL_FILTERS
1166 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1168 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1173 * If this is the head of a new filter block, make it a
1174 * re_filterfile spec of its own and chain it to the list:
1176 if (new_filter != FT_INVALID_FILTER)
1178 new_bl = zalloc_or_die(sizeof(*bl));
1179 if (new_filter == FT_CONTENT_FILTER)
1181 new_bl->name = chomp(buf + 7);
1183 #ifdef FEATURE_EXTERNAL_FILTERS
1184 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1186 new_bl->name = chomp(buf + 16);
1191 new_bl->name = chomp(buf + 21);
1193 new_bl->type = new_filter;
1196 * If a filter description is available,
1197 * encode it to HTML and save it.
1199 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1201 *new_bl->description++ = '\0';
1202 new_bl->description = html_encode(chomp(new_bl->description));
1203 if (NULL == new_bl->description)
1205 new_bl->description = strdup_or_die("Out of memory while "
1206 "encoding filter description to HTML");
1211 new_bl->description = strdup_or_die("No description available");
1214 new_bl->name = strdup_or_die(chomp(new_bl->name));
1217 * If this is the first filter block, chain it
1218 * to the file_list rather than its (nonexistant)
1232 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1238 #ifdef FEATURE_EXTERNAL_FILTERS
1239 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1241 /* Save the code as "pattern", but do not compile anything. */
1242 if (bl->patterns->first != NULL)
1244 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1245 "Did you forget to escape a line break?",
1248 error = enlist(bl->patterns, buf);
1249 if (JB_ERR_MEMORY == error)
1251 log_error(LOG_LEVEL_FATAL,
1252 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1262 * Save the expression, make it a pcrs_job
1263 * and chain it into the current filter's joblist
1265 error = enlist(bl->patterns, buf);
1266 if (JB_ERR_MEMORY == error)
1268 log_error(LOG_LEVEL_FATAL,
1269 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1271 assert(JB_ERR_OK == error);
1273 if (pcrs_job_is_dynamic(buf))
1276 * Dynamic pattern that might contain variables
1277 * and has to be recompiled for every request
1279 if (bl->joblist != NULL)
1281 pcrs_free_joblist(bl->joblist);
1285 log_error(LOG_LEVEL_RE_FILTER,
1286 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1290 else if (bl->dynamic)
1293 * A previous job was dynamic and as we
1294 * recompile the whole filter anyway, it
1295 * makes no sense to compile this job now.
1297 log_error(LOG_LEVEL_RE_FILTER,
1298 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1303 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1305 log_error(LOG_LEVEL_ERROR,
1306 "Adding re_filter job \'%s\' to filter %s failed: %s",
1307 buf, bl->name, pcrs_strerror(error));
1313 if (bl->joblist == NULL)
1315 bl->joblist = dummy;
1317 else if (NULL != lastjob)
1319 lastjob->next = dummy;
1322 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1327 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1328 buf, csp->config->re_filterfile[fileid], linenum);
1336 * Schedule the now-obsolete old data for unloading
1338 if (NULL != current_re_filterfile[fileid])
1340 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1344 * Chain this file into the global list of loaded files
1346 fs->next = files->next;
1348 current_re_filterfile[fileid] = fs;
1349 csp->rlist[fileid] = fs;
1353 load_re_filterfile_error:
1354 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1355 csp->config->re_filterfile[fileid]);
1361 /*********************************************************************
1363 * Function : add_loader
1365 * Description : Called from `load_config'. Called once for each input
1366 * file found in config.
1369 * 1 : loader = pointer to a function that can parse and load
1370 * the appropriate config file.
1371 * 2 : config = The configuration_spec to add the loader to.
1375 *********************************************************************/
1376 void add_loader(int (*loader)(struct client_state *),
1377 struct configuration_spec * config)
1381 for (i = 0; i < NLOADERS; i++)
1383 if (config->loaders[i] == NULL)
1385 config->loaders[i] = loader;
1393 /*********************************************************************
1395 * Function : run_loader
1397 * Description : Called from `load_config' and `listen_loop'. This
1398 * function keeps the "csp" current with any file mods
1399 * since the last loop. If a file is unchanged, the
1400 * loader functions do NOT reload the file.
1403 * 1 : csp = Current client state (buffers, headers, etc...)
1404 * Must be non-null. Reads: "csp->config"
1405 * Writes: various data members.
1407 * Returns : 0 => Ok, everything else is an error.
1409 *********************************************************************/
1410 int run_loader(struct client_state *csp)
1415 for (i = 0; i < NLOADERS; i++)
1417 if (csp->config->loaders[i] == NULL)
1421 ret |= (csp->config->loaders[i])(csp);
1427 /*********************************************************************
1429 * Function : file_has_been_modified
1431 * Description : Helper function to check if a file has been changed
1434 * 1 : filename = The name of the file to check
1435 * 2 : last_known_modification = The time of the last known
1438 * Returns : TRUE if the file has been changed,
1441 *********************************************************************/
1442 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1444 struct stat statbuf[1];
1446 if (stat(filename, statbuf) < 0)
1448 /* Error, probably file not found which counts as change. */
1452 return (last_know_modification != statbuf->st_mtime);
1456 /*********************************************************************
1458 * Function : any_loaded_file_changed
1460 * Description : Helper function to check if any loaded file has been
1461 * changed since the time it has been loaded.
1463 * XXX: Should we cache the return value for x seconds?
1466 * 1 : files_to_check = List of files to check
1468 * Returns : TRUE if any file has been changed,
1471 *********************************************************************/
1472 int any_loaded_file_changed(const struct client_state *csp)
1474 const struct file_list *file_to_check = csp->config->config_file_list;
1477 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1482 for (i = 0; i < MAX_AF_FILES; i++)
1484 if (csp->actions_list[i])
1486 file_to_check = csp->actions_list[i];
1487 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1494 for (i = 0; i < MAX_AF_FILES; i++)
1498 file_to_check = csp->rlist[i];
1499 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1506 #ifdef FEATURE_TRUST
1509 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1514 #endif /* def FEATURE_TRUST */