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. http://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>
49 #if !defined(_WIN32) && !defined(__OS2__)
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 destroy_list(csp->tags);
117 free_current_action(csp->action);
120 /*********************************************************************
124 * Description : Basically a mark and sweep garbage collector, it is run
125 * (by the parent thread) every once in a while to reclaim memory.
127 * It uses a mark and sweep strategy:
128 * 1) mark all files as inactive
130 * 2) check with each client:
131 * if it is active, mark its files as active
132 * if it is inactive, free its resources
134 * 3) free the resources of all of the files that
135 * are still marked as inactive (and are obsolete).
137 * N.B. files that are not obsolete don't have an unloader defined.
141 * Returns : The number of threads that are still active.
143 *********************************************************************/
144 unsigned int sweep(void)
146 struct file_list *fl, *nfl;
147 struct client_state *csp;
148 struct client_states *last_active, *client_list;
150 unsigned int active_threads = 0;
152 /* clear all of the file's active flags */
153 for (fl = files->next; NULL != fl; fl = fl->next)
158 last_active = clients;
159 client_list = clients->next;
161 while (NULL != client_list)
163 csp = &client_list->csp;
164 if (csp->flags & CSP_FLAG_ACTIVE)
166 /* Mark this client's files as active */
169 * Always have a configuration file.
170 * (Also note the slightly non-standard extra
173 csp->config->config_file_list->active = 1;
178 for (i = 0; i < MAX_AF_FILES; i++)
180 if (csp->actions_list[i])
182 csp->actions_list[i]->active = 1;
189 for (i = 0; i < MAX_AF_FILES; i++)
193 csp->rlist[i]->active = 1;
203 csp->tlist->active = 1;
205 #endif /* def FEATURE_TRUST */
209 last_active = client_list;
210 client_list = client_list->next;
214 * This client is not active. Free its resources.
217 last_active->next = client_list->next;
219 #ifdef FEATURE_STATISTICS
221 if (csp->flags & CSP_FLAG_REJECTED)
225 #endif /* def FEATURE_STATISTICS */
229 client_list = last_active->next;
238 if ((0 == fl->active) && (NULL != fl->unloader))
240 nfl->next = fl->next;
242 (fl->unloader)(fl->f);
256 return active_threads;
261 /*********************************************************************
263 * Function : check_file_changed
265 * Description : Helper function to check if a file needs reloading.
266 * If "current" is still current, return it. Otherwise
267 * allocates a new (zeroed) "struct file_list", fills
268 * in the disk file name and timestamp, and returns it.
271 * 1 : current = The file_list currently being used - will
272 * be checked to see if it is out of date.
273 * May be NULL (which is treated as out of
275 * 2 : filename = Name of file to check.
276 * 3 : newfl = New file list. [Output only]
277 * This will be set to NULL, OR a struct
278 * file_list newly allocated on the
279 * heap, with the filename and lastmodified
280 * fields filled, and all others zeroed.
282 * Returns : If file unchanged: 0 (and sets newfl == NULL)
283 * If file changed: 1 and sets newfl != NULL
284 * On error: 1 and sets newfl == NULL
286 *********************************************************************/
287 int check_file_changed(const struct file_list * current,
288 const char * filename,
289 struct file_list ** newfl)
291 struct file_list *fs;
292 struct stat statbuf[1];
296 if (stat(filename, statbuf) < 0)
298 /* Error, probably file not found. */
303 && (current->lastmodified == statbuf->st_mtime)
304 && (0 == strcmp(current->filename, filename)))
309 fs = zalloc_or_die(sizeof(struct file_list));
310 fs->filename = strdup_or_die(filename);
311 fs->lastmodified = statbuf->st_mtime;
318 /*********************************************************************
320 * Function : simple_read_line
322 * Description : Read a single line from a file and return it.
323 * This is basically a version of fgets() that malloc()s
324 * it's own line buffer. Note that the buffer will
325 * always be a multiple of BUFFER_SIZE bytes long.
326 * Therefore if you are going to keep the string for
327 * an extended period of time, you should probably
328 * strdup() it and free() the original, to save memory.
332 * 1 : dest = destination for newly malloc'd pointer to
333 * line data. Will be set to NULL on error.
334 * 2 : fp = File to read from
335 * 3 : newline = Standard for newlines in the file.
336 * Will be unchanged if it's value on input is not
338 * On output, may be changed from NEWLINE_UNKNOWN to
339 * actual convention in file.
341 * Returns : JB_ERR_OK on success
342 * JB_ERR_MEMORY on out-of-memory
343 * JB_ERR_FILE on EOF.
345 *********************************************************************/
346 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
349 size_t buflen = BUFFER_SIZE;
353 int realnewline = NEWLINE_UNKNOWN;
355 if (NULL == (buf = malloc(buflen)))
357 return JB_ERR_MEMORY;
363 * Character codes. If you have a weird compiler and the following are
364 * incorrect, you also need to fix NEWLINE() in loaders.h
366 #define CHAR_CR '\r' /* ASCII 13 */
367 #define CHAR_LF '\n' /* ASCII 10 */
388 else if (ch == CHAR_CR)
393 if (*newline == NEWLINE_UNKNOWN)
395 *newline = NEWLINE_DOS;
404 if (*newline == NEWLINE_UNKNOWN)
406 *newline = NEWLINE_MAC;
411 if (*newline == NEWLINE_UNKNOWN)
413 *newline = realnewline;
417 else if (ch == CHAR_LF)
421 if (*newline == NEWLINE_UNKNOWN)
423 *newline = NEWLINE_UNIX;
429 /* XXX: Why do we allow this anyway? */
439 buflen += BUFFER_SIZE;
440 if (NULL == (p = realloc(buf, buflen)))
443 return JB_ERR_MEMORY;
452 /*********************************************************************
454 * Function : edit_read_line
456 * Description : Read a single non-empty line from a file and return
457 * it. Trims comments, leading and trailing whitespace
458 * and respects escaping of newline and comment char.
459 * Provides the line in 2 alternative forms: raw and
461 * - raw is the raw data read from the file. If the
462 * line is not modified, then this should be written
464 * - prefix is any comments and blank lines that were
465 * read from the file. If the line is modified, then
466 * this should be written out to the file followed
467 * by the modified data. (If this string is non-empty
468 * then it will have a newline at the end).
469 * - data is the actual data that will be parsed
470 * further by appropriate routines.
471 * On EOF, the 3 strings will all be set to NULL and
472 * 0 will be returned.
475 * 1 : fp = File to read from
476 * 2 : raw_out = destination for newly malloc'd pointer to
477 * raw line data. May be NULL if you don't want it.
478 * 3 : prefix_out = destination for newly malloc'd pointer to
479 * comments. May be NULL if you don't want it.
480 * 4 : data_out = destination for newly malloc'd pointer to
481 * line data with comments and leading/trailing spaces
482 * removed, and line continuation performed. May be
483 * NULL if you don't want it.
484 * 5 : newline = Standard for newlines in the file.
485 * On input, set to value to use or NEWLINE_UNKNOWN.
486 * On output, may be changed from NEWLINE_UNKNOWN to
487 * actual convention in file. May be NULL if you
489 * 6 : line_number = Line number in file. In "lines" as
490 * reported by a text editor, not lines containing data.
492 * Returns : JB_ERR_OK on success
493 * JB_ERR_MEMORY on out-of-memory
494 * JB_ERR_FILE on EOF.
496 *********************************************************************/
497 jb_err edit_read_line(FILE *fp,
502 unsigned long *line_number)
504 char *p; /* Temporary pointer */
505 char *linebuf; /* Line read from file */
506 char *linestart; /* Start of linebuf, usually first non-whitespace char */
507 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
508 int is_empty = 1; /* Flag if not got any data yet */
509 char *raw = NULL; /* String to be stored in raw_out */
510 char *prefix = NULL; /* String to be stored in prefix_out */
511 char *data = NULL; /* String to be stored in data_out */
512 int scrapnewline; /* Used for (*newline) if newline==NULL */
513 jb_err rval = JB_ERR_OK;
516 assert(raw_out || data_out);
517 assert(newline == NULL
518 || *newline == NEWLINE_UNKNOWN
519 || *newline == NEWLINE_UNIX
520 || *newline == NEWLINE_DOS
521 || *newline == NEWLINE_MAC);
525 scrapnewline = NEWLINE_UNKNOWN;
526 newline = &scrapnewline;
529 /* Set output parameters to NULL */
543 /* Set string variables to new, empty strings. */
547 raw = strdup_or_die("");
551 prefix = strdup_or_die("");
555 data = strdup_or_die("");
558 /* Main loop. Loop while we need more data & it's not EOF. */
560 while ((contflag || is_empty)
561 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
569 string_append(&raw,linebuf);
570 if (string_append(&raw,NEWLINE(*newline)))
575 return JB_ERR_MEMORY;
579 /* Line continuation? Trim escape and set flag. */
580 p = linebuf + strlen(linebuf) - 1;
581 contflag = ((*linebuf != '\0') && (*p == '\\'));
587 /* Trim leading spaces if we're at the start of the line */
589 assert(NULL != data);
592 /* Trim leading spaces */
593 while (*linestart && isspace((int)(unsigned char)*linestart))
599 /* Handle comment characters. */
601 while ((p = strchr(p, '#')) != NULL)
603 /* Found a comment char.. */
604 if ((p != linebuf) && (*(p-1) == '\\'))
606 /* ..and it's escaped, left-shift the line over the escape. */
608 while ((*q = *(q + 1)) != '\0')
612 /* Now scan from just after the "#". */
616 /* Real comment. Save it... */
619 /* Special case: Line only contains a comment, so all the
620 * previous whitespace is considered part of the comment.
621 * Undo the whitespace skipping, if any.
628 string_append(&prefix,p);
629 if (string_append(&prefix, NEWLINE(*newline)))
634 return JB_ERR_MEMORY;
638 /* ... and chop off the rest of the line */
641 } /* END while (there's a # character) */
643 /* Write to the buffer */
647 if (string_append(&data, linestart))
652 return JB_ERR_MEMORY;
657 } /* END while(we need more data) */
659 /* Handle simple_read_line() errors - ignore EOF */
660 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
668 if (raw ? (*raw == '\0') : is_empty)
670 /* EOF and no data there. (Definition of "data" depends on whether
671 * the caller cares about "raw" or just "data").
682 /* Got at least some data */
684 /* Remove trailing whitespace */
697 *prefix_out = prefix;
716 /*********************************************************************
718 * Function : read_config_line
720 * Description : Read a single non-empty line from a file and return
721 * it. Trims comments, leading and trailing whitespace
722 * and respects escaping of newline and comment char.
725 * 1 : fp = File to read from
726 * 2 : linenum = linenumber in file
727 * 3 : buf = Pointer to a pointer to set to the data buffer.
729 * Returns : NULL on EOF or error
730 * Otherwise, returns buf.
732 *********************************************************************/
733 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
736 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
739 if (err == JB_ERR_MEMORY)
741 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
750 /*********************************************************************
752 * Function : unload_trustfile
754 * Description : Unloads a trustfile.
757 * 1 : f = the data structure associated with the trustfile.
761 *********************************************************************/
762 static void unload_trustfile(void *f)
764 struct block_spec *cur = (struct block_spec *)f;
765 struct block_spec *next;
771 free_pattern_spec(cur->url);
780 #ifdef FEATURE_GRACEFUL_TERMINATION
781 /*********************************************************************
783 * Function : unload_current_trust_file
785 * Description : Unloads current trust file - reset to state at
786 * beginning of program.
792 *********************************************************************/
793 void unload_current_trust_file(void)
795 if (current_trustfile)
797 current_trustfile->unloader = unload_trustfile;
798 current_trustfile = NULL;
801 #endif /* FEATURE_GRACEFUL_TERMINATION */
804 /*********************************************************************
806 * Function : load_trustfile
808 * Description : Read and parse a trustfile and add to files list.
811 * 1 : csp = Current client state (buffers, headers, etc...)
813 * Returns : 0 => Ok, everything else is an error.
815 *********************************************************************/
816 int load_trustfile(struct client_state *csp)
820 struct block_spec *b, *bl;
821 struct pattern_spec **tl;
825 struct file_list *fs;
826 unsigned long linenum = 0;
827 int trusted_referrers = 0;
829 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
831 /* No need to load */
832 csp->tlist = current_trustfile;
837 goto load_trustfile_error;
840 fs->f = bl = zalloc_or_die(sizeof(*bl));
842 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
844 goto load_trustfile_error;
846 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
848 tl = csp->config->trust_list;
850 while (read_config_line(fp, &linenum, &buf) != NULL)
869 while ((*p++ = *q++) != '\0')
875 /* skip blank lines */
882 /* allocate a new node */
883 b = zalloc_or_die(sizeof(*b));
885 /* add it to the list */
891 /* Save the URL pattern */
892 if (create_pattern_spec(b->url, buf))
895 goto load_trustfile_error;
899 * save a pointer to URL's spec in the list of trusted URL's, too
903 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
911 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
914 * FIXME: ... after Privoxy 3.0.4 is out.
916 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
917 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
918 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
919 MAX_TRUSTED_REFERRERS, trusted_referrers);
926 /* the old one is now obsolete */
927 if (current_trustfile)
929 current_trustfile->unloader = unload_trustfile;
932 fs->next = files->next;
934 current_trustfile = fs;
939 load_trustfile_error:
940 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
941 csp->config->trustfile);
946 #endif /* def FEATURE_TRUST */
949 /*********************************************************************
951 * Function : unload_re_filterfile
953 * Description : Unload the re_filter list by freeing all chained
954 * re_filterfile specs and their data.
957 * 1 : f = the data structure associated with the filterfile.
961 *********************************************************************/
962 static void unload_re_filterfile(void *f)
964 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
970 destroy_list(b->patterns);
971 pcrs_free_joblist(b->joblist);
973 freez(b->description);
982 /*********************************************************************
984 * Function : unload_forward_spec
986 * Description : Unload the forward spec settings by freeing all
987 * memory referenced by members and the memory for
991 * 1 : fwd = the forward spec.
995 *********************************************************************/
996 void unload_forward_spec(struct forward_spec *fwd)
998 free_pattern_spec(fwd->url);
999 freez(fwd->gateway_host);
1000 freez(fwd->forward_host);
1007 #ifdef FEATURE_GRACEFUL_TERMINATION
1008 /*********************************************************************
1010 * Function : unload_current_re_filterfile
1012 * Description : Unloads current re_filter file - reset to state at
1013 * beginning of program.
1019 *********************************************************************/
1020 void unload_current_re_filterfile(void)
1024 for (i = 0; i < MAX_AF_FILES; i++)
1026 if (current_re_filterfile[i])
1028 current_re_filterfile[i]->unloader = unload_re_filterfile;
1029 current_re_filterfile[i] = NULL;
1036 /*********************************************************************
1038 * Function : load_re_filterfiles
1040 * Description : Loads all the filterfiles.
1041 * Generate a chained list of re_filterfile_spec's from
1042 * the "FILTER: " blocks, compiling all their substitutions
1043 * into chained lists of pcrs_job structs.
1046 * 1 : csp = Current client state (buffers, headers, etc...)
1048 * Returns : 0 => Ok, everything else is an error.
1050 *********************************************************************/
1051 int load_re_filterfiles(struct client_state *csp)
1056 for (i = 0; i < MAX_AF_FILES; i++)
1058 if (csp->config->re_filterfile[i])
1060 result = load_one_re_filterfile(csp, i);
1066 else if (current_re_filterfile[i])
1068 current_re_filterfile[i]->unloader = unload_re_filterfile;
1069 current_re_filterfile[i] = NULL;
1077 /*********************************************************************
1079 * Function : load_one_re_filterfile
1081 * Description : Load a re_filterfile.
1082 * Generate a chained list of re_filterfile_spec's from
1083 * the "FILTER: " blocks, compiling all their substitutions
1084 * into chained lists of pcrs_job structs.
1087 * 1 : csp = Current client state (buffers, headers, etc...)
1089 * Returns : 0 => Ok, everything else is an error.
1091 *********************************************************************/
1092 int load_one_re_filterfile(struct client_state *csp, int fileid)
1096 struct re_filterfile_spec *new_bl, *bl = NULL;
1097 struct file_list *fs;
1100 unsigned long linenum = 0;
1101 pcrs_job *dummy, *lastjob = NULL;
1104 * No need to reload if unchanged
1106 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1108 csp->rlist[fileid] = current_re_filterfile[fileid];
1113 goto load_re_filterfile_error;
1117 * Open the file or fail
1119 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1121 goto load_re_filterfile_error;
1124 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1129 while (read_config_line(fp, &linenum, &buf) != NULL)
1131 enum filter_type new_filter = FT_INVALID_FILTER;
1133 if (strncmp(buf, "FILTER:", 7) == 0)
1135 new_filter = FT_CONTENT_FILTER;
1137 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1139 new_filter = FT_SERVER_HEADER_FILTER;
1141 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1143 new_filter = FT_CLIENT_HEADER_FILTER;
1145 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1147 new_filter = FT_CLIENT_HEADER_TAGGER;
1149 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1151 new_filter = FT_SERVER_HEADER_TAGGER;
1153 #ifdef FEATURE_EXTERNAL_FILTERS
1154 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1156 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1161 * If this is the head of a new filter block, make it a
1162 * re_filterfile spec of its own and chain it to the list:
1164 if (new_filter != FT_INVALID_FILTER)
1166 new_bl = zalloc_or_die(sizeof(*bl));
1167 if (new_filter == FT_CONTENT_FILTER)
1169 new_bl->name = chomp(buf + 7);
1171 #ifdef FEATURE_EXTERNAL_FILTERS
1172 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1174 new_bl->name = chomp(buf + 16);
1179 new_bl->name = chomp(buf + 21);
1181 new_bl->type = new_filter;
1184 * If a filter description is available,
1185 * encode it to HTML and save it.
1187 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1189 *new_bl->description++ = '\0';
1190 new_bl->description = html_encode(chomp(new_bl->description));
1191 if (NULL == new_bl->description)
1193 new_bl->description = strdup_or_die("Out of memory while "
1194 "encoding filter description to HTML");
1199 new_bl->description = strdup_or_die("No description available");
1202 new_bl->name = strdup_or_die(chomp(new_bl->name));
1205 * If this is the first filter block, chain it
1206 * to the file_list rather than its (nonexistant)
1220 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1226 #ifdef FEATURE_EXTERNAL_FILTERS
1227 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1230 /* Save the code as "pattern", but do not compile anything. */
1231 if (bl->patterns->first != NULL)
1233 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1234 "Did you forget to escape a line break?",
1237 jb_error = enlist(bl->patterns, buf);
1238 if (JB_ERR_MEMORY == jb_error)
1240 log_error(LOG_LEVEL_FATAL,
1241 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1253 * Save the expression, make it a pcrs_job
1254 * and chain it into the current filter's joblist
1256 jb_error = enlist(bl->patterns, buf);
1257 if (JB_ERR_MEMORY == jb_error)
1259 log_error(LOG_LEVEL_FATAL,
1260 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1262 assert(JB_ERR_OK == jb_error);
1264 if (pcrs_job_is_dynamic(buf))
1267 * Dynamic pattern that might contain variables
1268 * and has to be recompiled for every request
1270 if (bl->joblist != NULL)
1272 pcrs_free_joblist(bl->joblist);
1276 log_error(LOG_LEVEL_RE_FILTER,
1277 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1281 else if (bl->dynamic)
1284 * A previous job was dynamic and as we
1285 * recompile the whole filter anyway, it
1286 * makes no sense to compile this job now.
1288 log_error(LOG_LEVEL_RE_FILTER,
1289 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1294 if ((dummy = pcrs_compile_command(buf, &pcrs_error)) == NULL)
1296 log_error(LOG_LEVEL_ERROR,
1297 "Adding re_filter job \'%s\' to filter %s failed: %s",
1298 buf, bl->name, pcrs_strerror(pcrs_error));
1304 if (bl->joblist == NULL)
1306 bl->joblist = dummy;
1308 else if (NULL != lastjob)
1310 lastjob->next = dummy;
1313 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1318 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1319 buf, csp->config->re_filterfile[fileid], linenum);
1327 * Schedule the now-obsolete old data for unloading
1329 if (NULL != current_re_filterfile[fileid])
1331 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1335 * Chain this file into the global list of loaded files
1337 fs->next = files->next;
1339 current_re_filterfile[fileid] = fs;
1340 csp->rlist[fileid] = fs;
1344 load_re_filterfile_error:
1345 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1346 csp->config->re_filterfile[fileid]);
1352 /*********************************************************************
1354 * Function : add_loader
1356 * Description : Called from `load_config'. Called once for each input
1357 * file found in config.
1360 * 1 : loader = pointer to a function that can parse and load
1361 * the appropriate config file.
1362 * 2 : config = The configuration_spec to add the loader to.
1366 *********************************************************************/
1367 void add_loader(int (*loader)(struct client_state *),
1368 struct configuration_spec * config)
1372 for (i = 0; i < NLOADERS; i++)
1374 if (config->loaders[i] == NULL)
1376 config->loaders[i] = loader;
1384 /*********************************************************************
1386 * Function : run_loader
1388 * Description : Called from `load_config' and `listen_loop'. This
1389 * function keeps the "csp" current with any file mods
1390 * since the last loop. If a file is unchanged, the
1391 * loader functions do NOT reload the file.
1394 * 1 : csp = Current client state (buffers, headers, etc...)
1395 * Must be non-null. Reads: "csp->config"
1396 * Writes: various data members.
1398 * Returns : 0 => Ok, everything else is an error.
1400 *********************************************************************/
1401 int run_loader(struct client_state *csp)
1406 for (i = 0; i < NLOADERS; i++)
1408 if (csp->config->loaders[i] == NULL)
1412 ret |= (csp->config->loaders[i])(csp);
1418 /*********************************************************************
1420 * Function : file_has_been_modified
1422 * Description : Helper function to check if a file has been changed
1425 * 1 : filename = The name of the file to check
1426 * 2 : last_known_modification = The time of the last known
1429 * Returns : TRUE if the file has been changed,
1432 *********************************************************************/
1433 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1435 struct stat statbuf[1];
1437 if (stat(filename, statbuf) < 0)
1439 /* Error, probably file not found which counts as change. */
1443 return (last_know_modification != statbuf->st_mtime);
1447 /*********************************************************************
1449 * Function : any_loaded_file_changed
1451 * Description : Helper function to check if any loaded file has been
1452 * changed since the time it has been loaded.
1454 * XXX: Should we cache the return value for x seconds?
1457 * 1 : files_to_check = List of files to check
1459 * Returns : TRUE if any file has been changed,
1462 *********************************************************************/
1463 int any_loaded_file_changed(const struct client_state *csp)
1465 const struct file_list *file_to_check = csp->config->config_file_list;
1468 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1473 for (i = 0; i < MAX_AF_FILES; i++)
1475 if (csp->actions_list[i])
1477 file_to_check = csp->actions_list[i];
1478 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1485 for (i = 0; i < MAX_AF_FILES; i++)
1489 file_to_check = csp->rlist[i];
1490 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1497 #ifdef FEATURE_TRUST
1500 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1505 #endif /* def FEATURE_TRUST */