1 const char loaders_rcs[] = "$Id: loaders.c,v 1.100 2015/01/24 16:40:21 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 */
77 static int load_one_re_filterfile(struct client_state *csp, int fileid);
79 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
80 NULL, NULL, NULL, NULL, NULL,
81 NULL, NULL, NULL, NULL, NULL
85 /*********************************************************************
89 * Description : Basically a mark and sweep garbage collector, it is run
90 * (by the parent thread) every once in a while to reclaim memory.
92 * It uses a mark and sweep strategy:
93 * 1) mark all files as inactive
95 * 2) check with each client:
96 * if it is active, mark its files as active
97 * if it is inactive, free its resources
99 * 3) free the resources of all of the files that
100 * are still marked as inactive (and are obsolete).
102 * N.B. files that are not obsolete don't have an unloader defined.
106 * Returns : The number of threads that are still active.
108 *********************************************************************/
109 unsigned int sweep(void)
111 struct file_list *fl, *nfl;
112 struct client_state *csp;
113 struct client_states *last_active, *client_list;
115 unsigned int active_threads = 0;
117 /* clear all of the file's active flags */
118 for (fl = files->next; NULL != fl; fl = fl->next)
123 last_active = clients;
124 client_list = clients->next;
126 while (NULL != client_list)
128 csp = &client_list->csp;
129 if (csp->flags & CSP_FLAG_ACTIVE)
131 /* Mark this client's files as active */
134 * Always have a configuration file.
135 * (Also note the slightly non-standard extra
138 csp->config->config_file_list->active = 1;
143 for (i = 0; i < MAX_AF_FILES; i++)
145 if (csp->actions_list[i])
147 csp->actions_list[i]->active = 1;
154 for (i = 0; i < MAX_AF_FILES; i++)
158 csp->rlist[i]->active = 1;
168 csp->tlist->active = 1;
170 #endif /* def FEATURE_TRUST */
174 last_active = client_list;
175 client_list = client_list->next;
179 * This client is not active. Free its resources.
182 last_active->next = client_list->next;
184 freez(csp->ip_addr_str);
185 freez(csp->client_iob->buf);
186 freez(csp->iob->buf);
187 freez(csp->error_message);
189 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
192 unload_forward_spec(csp->fwd);
194 free_http_request(csp->http);
196 destroy_list(csp->headers);
197 destroy_list(csp->tags);
199 free_current_action(csp->action);
201 #ifdef FEATURE_STATISTICS
203 if (csp->flags & CSP_FLAG_REJECTED)
207 #endif /* def FEATURE_STATISTICS */
211 client_list = last_active->next;
220 if ((0 == fl->active) && (NULL != fl->unloader))
222 nfl->next = fl->next;
224 (fl->unloader)(fl->f);
238 return active_threads;
243 /*********************************************************************
245 * Function : check_file_changed
247 * Description : Helper function to check if a file needs reloading.
248 * If "current" is still current, return it. Otherwise
249 * allocates a new (zeroed) "struct file_list", fills
250 * in the disk file name and timestamp, and returns it.
253 * 1 : current = The file_list currently being used - will
254 * be checked to see if it is out of date.
255 * May be NULL (which is treated as out of
257 * 2 : filename = Name of file to check.
258 * 3 : newfl = New file list. [Output only]
259 * This will be set to NULL, OR a struct
260 * file_list newly allocated on the
261 * heap, with the filename and lastmodified
262 * fields filled, and all others zeroed.
264 * Returns : If file unchanged: 0 (and sets newfl == NULL)
265 * If file changed: 1 and sets newfl != NULL
266 * On error: 1 and sets newfl == NULL
268 *********************************************************************/
269 int check_file_changed(const struct file_list * current,
270 const char * filename,
271 struct file_list ** newfl)
273 struct file_list *fs;
274 struct stat statbuf[1];
278 if (stat(filename, statbuf) < 0)
280 /* Error, probably file not found. */
285 && (current->lastmodified == statbuf->st_mtime)
286 && (0 == strcmp(current->filename, filename)))
291 fs = zalloc_or_die(sizeof(struct file_list));
292 fs->filename = strdup(filename);
293 fs->lastmodified = statbuf->st_mtime;
295 if (fs->filename == NULL)
297 /* Out of memory error */
306 /*********************************************************************
308 * Function : simple_read_line
310 * Description : Read a single line from a file and return it.
311 * This is basically a version of fgets() that malloc()s
312 * it's own line buffer. Note that the buffer will
313 * always be a multiple of BUFFER_SIZE bytes long.
314 * Therefore if you are going to keep the string for
315 * an extended period of time, you should probably
316 * strdup() it and free() the original, to save memory.
320 * 1 : dest = destination for newly malloc'd pointer to
321 * line data. Will be set to NULL on error.
322 * 2 : fp = File to read from
323 * 3 : newline = Standard for newlines in the file.
324 * Will be unchanged if it's value on input is not
326 * On output, may be changed from NEWLINE_UNKNOWN to
327 * actual convention in file.
329 * Returns : JB_ERR_OK on success
330 * JB_ERR_MEMORY on out-of-memory
331 * JB_ERR_FILE on EOF.
333 *********************************************************************/
334 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
337 size_t buflen = BUFFER_SIZE;
341 int realnewline = NEWLINE_UNKNOWN;
343 if (NULL == (buf = malloc(buflen)))
345 return JB_ERR_MEMORY;
351 * Character codes. If you have a weird compiler and the following are
352 * incorrect, you also need to fix NEWLINE() in loaders.h
354 #define CHAR_CR '\r' /* ASCII 13 */
355 #define CHAR_LF '\n' /* ASCII 10 */
375 else if (ch == CHAR_CR)
380 if (*newline == NEWLINE_UNKNOWN)
382 *newline = NEWLINE_DOS;
391 if (*newline == NEWLINE_UNKNOWN)
393 *newline = NEWLINE_MAC;
398 if (*newline == NEWLINE_UNKNOWN)
400 *newline = realnewline;
404 else if (ch == CHAR_LF)
408 if (*newline == NEWLINE_UNKNOWN)
410 *newline = NEWLINE_UNIX;
425 buflen += BUFFER_SIZE;
426 if (NULL == (p = realloc(buf, buflen)))
429 return JB_ERR_MEMORY;
438 /*********************************************************************
440 * Function : edit_read_line
442 * Description : Read a single non-empty line from a file and return
443 * it. Trims comments, leading and trailing whitespace
444 * and respects escaping of newline and comment char.
445 * Provides the line in 2 alternative forms: raw and
447 * - raw is the raw data read from the file. If the
448 * line is not modified, then this should be written
450 * - prefix is any comments and blank lines that were
451 * read from the file. If the line is modified, then
452 * this should be written out to the file followed
453 * by the modified data. (If this string is non-empty
454 * then it will have a newline at the end).
455 * - data is the actual data that will be parsed
456 * further by appropriate routines.
457 * On EOF, the 3 strings will all be set to NULL and
458 * 0 will be returned.
461 * 1 : fp = File to read from
462 * 2 : raw_out = destination for newly malloc'd pointer to
463 * raw line data. May be NULL if you don't want it.
464 * 3 : prefix_out = destination for newly malloc'd pointer to
465 * comments. May be NULL if you don't want it.
466 * 4 : data_out = destination for newly malloc'd pointer to
467 * line data with comments and leading/trailing spaces
468 * removed, and line continuation performed. May be
469 * NULL if you don't want it.
470 * 5 : newline = Standard for newlines in the file.
471 * On input, set to value to use or NEWLINE_UNKNOWN.
472 * On output, may be changed from NEWLINE_UNKNOWN to
473 * actual convention in file. May be NULL if you
475 * 6 : line_number = Line number in file. In "lines" as
476 * reported by a text editor, not lines containing data.
478 * Returns : JB_ERR_OK on success
479 * JB_ERR_MEMORY on out-of-memory
480 * JB_ERR_FILE on EOF.
482 *********************************************************************/
483 jb_err edit_read_line(FILE *fp,
488 unsigned long *line_number)
490 char *p; /* Temporary pointer */
491 char *linebuf; /* Line read from file */
492 char *linestart; /* Start of linebuf, usually first non-whitespace char */
493 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
494 int is_empty = 1; /* Flag if not got any data yet */
495 char *raw = NULL; /* String to be stored in raw_out */
496 char *prefix = NULL; /* String to be stored in prefix_out */
497 char *data = NULL; /* String to be stored in data_out */
498 int scrapnewline; /* Used for (*newline) if newline==NULL */
499 jb_err rval = JB_ERR_OK;
502 assert(raw_out || data_out);
503 assert(newline == NULL
504 || *newline == NEWLINE_UNKNOWN
505 || *newline == NEWLINE_UNIX
506 || *newline == NEWLINE_DOS
507 || *newline == NEWLINE_MAC);
511 scrapnewline = NEWLINE_UNKNOWN;
512 newline = &scrapnewline;
515 /* Set output parameters to NULL */
529 /* Set string variables to new, empty strings. */
536 return JB_ERR_MEMORY;
545 return JB_ERR_MEMORY;
555 return JB_ERR_MEMORY;
559 /* Main loop. Loop while we need more data & it's not EOF. */
561 while ((contflag || is_empty)
562 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
570 string_append(&raw,linebuf);
571 if (string_append(&raw,NEWLINE(*newline)))
576 return JB_ERR_MEMORY;
580 /* Line continuation? Trim escape and set flag. */
581 p = linebuf + strlen(linebuf) - 1;
582 contflag = ((*linebuf != '\0') && (*p == '\\'));
588 /* Trim leading spaces if we're at the start of the line */
590 assert(NULL != data);
593 /* Trim leading spaces */
594 while (*linestart && isspace((int)(unsigned char)*linestart))
600 /* Handle comment characters. */
602 while ((p = strchr(p, '#')) != NULL)
604 /* Found a comment char.. */
605 if ((p != linebuf) && (*(p-1) == '\\'))
607 /* ..and it's escaped, left-shift the line over the escape. */
609 while ((*q = *(q + 1)) != '\0')
613 /* Now scan from just after the "#". */
617 /* Real comment. Save it... */
620 /* Special case: Line only contains a comment, so all the
621 * previous whitespace is considered part of the comment.
622 * Undo the whitespace skipping, if any.
629 string_append(&prefix,p);
630 if (string_append(&prefix, NEWLINE(*newline)))
635 return JB_ERR_MEMORY;
639 /* ... and chop off the rest of the line */
642 } /* END while (there's a # character) */
644 /* Write to the buffer */
650 if (string_append(&data, linestart))
655 return JB_ERR_MEMORY;
661 } /* END while(we need more data) */
663 /* Handle simple_read_line() errors - ignore EOF */
664 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
672 if (raw ? (*raw == '\0') : is_empty)
674 /* EOF and no data there. (Definition of "data" depends on whether
675 * the caller cares about "raw" or just "data").
686 /* Got at least some data */
688 /* Remove trailing whitespace */
701 *prefix_out = prefix;
720 /*********************************************************************
722 * Function : read_config_line
724 * Description : Read a single non-empty line from a file and return
725 * it. Trims comments, leading and trailing whitespace
726 * and respects escaping of newline and comment char.
729 * 1 : fp = File to read from
730 * 2 : linenum = linenumber in file
731 * 3 : buf = Pointer to a pointer to set to the data buffer.
733 * Returns : NULL on EOF or error
734 * Otherwise, returns buf.
736 *********************************************************************/
737 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
740 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
743 if (err == JB_ERR_MEMORY)
745 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
754 /*********************************************************************
756 * Function : unload_trustfile
758 * Description : Unloads a trustfile.
761 * 1 : f = the data structure associated with the trustfile.
765 *********************************************************************/
766 static void unload_trustfile(void *f)
768 struct block_spec *cur = (struct block_spec *)f;
769 struct block_spec *next;
775 free_pattern_spec(cur->url);
784 #ifdef FEATURE_GRACEFUL_TERMINATION
785 /*********************************************************************
787 * Function : unload_current_trust_file
789 * Description : Unloads current trust file - reset to state at
790 * beginning of program.
796 *********************************************************************/
797 void unload_current_trust_file(void)
799 if (current_trustfile)
801 current_trustfile->unloader = unload_trustfile;
802 current_trustfile = NULL;
805 #endif /* FEATURE_GRACEFUL_TERMINATION */
808 /*********************************************************************
810 * Function : load_trustfile
812 * Description : Read and parse a trustfile and add to files list.
815 * 1 : csp = Current client state (buffers, headers, etc...)
817 * Returns : 0 => Ok, everything else is an error.
819 *********************************************************************/
820 int load_trustfile(struct client_state *csp)
824 struct block_spec *b, *bl;
825 struct pattern_spec **tl;
829 struct file_list *fs;
830 unsigned long linenum = 0;
831 int trusted_referrers = 0;
833 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
835 /* No need to load */
836 csp->tlist = current_trustfile;
841 goto load_trustfile_error;
844 fs->f = bl = zalloc_or_die(sizeof(*bl));
846 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
848 goto load_trustfile_error;
850 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
852 tl = csp->config->trust_list;
854 while (read_config_line(fp, &linenum, &buf) != NULL)
873 while ((*p++ = *q++) != '\0')
879 /* skip blank lines */
886 /* allocate a new node */
887 b = zalloc_or_die(sizeof(*b));
889 /* add it to the list */
895 /* Save the URL pattern */
896 if (create_pattern_spec(b->url, buf))
899 goto load_trustfile_error;
903 * save a pointer to URL's spec in the list of trusted URL's, too
907 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
915 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
918 * FIXME: ... after Privoxy 3.0.4 is out.
920 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
921 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
922 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
923 MAX_TRUSTED_REFERRERS, trusted_referrers);
930 /* the old one is now obsolete */
931 if (current_trustfile)
933 current_trustfile->unloader = unload_trustfile;
936 fs->next = files->next;
938 current_trustfile = fs;
943 load_trustfile_error:
944 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
945 csp->config->trustfile);
950 #endif /* def FEATURE_TRUST */
953 /*********************************************************************
955 * Function : unload_re_filterfile
957 * Description : Unload the re_filter list by freeing all chained
958 * re_filterfile specs and their data.
961 * 1 : f = the data structure associated with the filterfile.
965 *********************************************************************/
966 static void unload_re_filterfile(void *f)
968 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
974 destroy_list(b->patterns);
975 pcrs_free_joblist(b->joblist);
977 freez(b->description);
986 /*********************************************************************
988 * Function : unload_forward_spec
990 * Description : Unload the forward spec settings by freeing all
991 * memory referenced by members and the memory for
995 * 1 : fwd = the forward spec.
999 *********************************************************************/
1000 void unload_forward_spec(struct forward_spec *fwd)
1002 free_pattern_spec(fwd->url);
1003 freez(fwd->gateway_host);
1004 freez(fwd->forward_host);
1011 #ifdef FEATURE_GRACEFUL_TERMINATION
1012 /*********************************************************************
1014 * Function : unload_current_re_filterfile
1016 * Description : Unloads current re_filter file - reset to state at
1017 * beginning of program.
1023 *********************************************************************/
1024 void unload_current_re_filterfile(void)
1028 for (i = 0; i < MAX_AF_FILES; i++)
1030 if (current_re_filterfile[i])
1032 current_re_filterfile[i]->unloader = unload_re_filterfile;
1033 current_re_filterfile[i] = NULL;
1040 /*********************************************************************
1042 * Function : load_re_filterfiles
1044 * Description : Loads all the filterfiles.
1045 * Generate a chained list of re_filterfile_spec's from
1046 * the "FILTER: " blocks, compiling all their substitutions
1047 * into chained lists of pcrs_job structs.
1050 * 1 : csp = Current client state (buffers, headers, etc...)
1052 * Returns : 0 => Ok, everything else is an error.
1054 *********************************************************************/
1055 int load_re_filterfiles(struct client_state *csp)
1060 for (i = 0; i < MAX_AF_FILES; i++)
1062 if (csp->config->re_filterfile[i])
1064 result = load_one_re_filterfile(csp, i);
1070 else if (current_re_filterfile[i])
1072 current_re_filterfile[i]->unloader = unload_re_filterfile;
1073 current_re_filterfile[i] = NULL;
1081 /*********************************************************************
1083 * Function : load_one_re_filterfile
1085 * Description : Load a re_filterfile.
1086 * Generate a chained list of re_filterfile_spec's from
1087 * the "FILTER: " blocks, compiling all their substitutions
1088 * into chained lists of pcrs_job structs.
1091 * 1 : csp = Current client state (buffers, headers, etc...)
1093 * Returns : 0 => Ok, everything else is an error.
1095 *********************************************************************/
1096 int load_one_re_filterfile(struct client_state *csp, int fileid)
1100 struct re_filterfile_spec *new_bl, *bl = NULL;
1101 struct file_list *fs;
1105 unsigned long linenum = 0;
1106 pcrs_job *dummy, *lastjob = NULL;
1109 * No need to reload if unchanged
1111 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1113 csp->rlist[fileid] = current_re_filterfile[fileid];
1118 goto load_re_filterfile_error;
1122 * Open the file or fail
1124 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1126 goto load_re_filterfile_error;
1129 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1134 while (read_config_line(fp, &linenum, &buf) != NULL)
1136 enum filter_type new_filter = FT_INVALID_FILTER;
1138 if (strncmp(buf, "FILTER:", 7) == 0)
1140 new_filter = FT_CONTENT_FILTER;
1142 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1144 new_filter = FT_SERVER_HEADER_FILTER;
1146 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1148 new_filter = FT_CLIENT_HEADER_FILTER;
1150 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1152 new_filter = FT_CLIENT_HEADER_TAGGER;
1154 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1156 new_filter = FT_SERVER_HEADER_TAGGER;
1158 #ifdef FEATURE_EXTERNAL_FILTERS
1159 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1161 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1166 * If this is the head of a new filter block, make it a
1167 * re_filterfile spec of its own and chain it to the list:
1169 if (new_filter != FT_INVALID_FILTER)
1171 new_bl = zalloc_or_die(sizeof(*bl));
1172 if (new_filter == FT_CONTENT_FILTER)
1174 new_bl->name = chomp(buf + 7);
1176 #ifdef FEATURE_EXTERNAL_FILTERS
1177 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1179 new_bl->name = chomp(buf + 16);
1184 new_bl->name = chomp(buf + 21);
1186 new_bl->type = new_filter;
1189 * If a filter description is available,
1190 * encode it to HTML and save it.
1192 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1194 *new_bl->description++ = '\0';
1195 new_bl->description = html_encode(chomp(new_bl->description));
1196 if (NULL == new_bl->description)
1198 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1203 new_bl->description = strdup("No description available for this filter");
1206 new_bl->name = strdup(chomp(new_bl->name));
1209 * If this is the first filter block, chain it
1210 * to the file_list rather than its (nonexistant)
1224 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1230 #ifdef FEATURE_EXTERNAL_FILTERS
1231 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1233 /* Save the code as "pattern", but do not compile anything. */
1234 if (bl->patterns->first != NULL)
1236 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1237 "Did you forget to escape a line break?",
1240 error = enlist(bl->patterns, buf);
1241 if (JB_ERR_MEMORY == error)
1243 log_error(LOG_LEVEL_FATAL,
1244 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1254 * Save the expression, make it a pcrs_job
1255 * and chain it into the current filter's joblist
1257 error = enlist(bl->patterns, buf);
1258 if (JB_ERR_MEMORY == error)
1260 log_error(LOG_LEVEL_FATAL,
1261 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1263 assert(JB_ERR_OK == error);
1265 if (pcrs_job_is_dynamic(buf))
1268 * Dynamic pattern that might contain variables
1269 * and has to be recompiled for every request
1271 if (bl->joblist != NULL)
1273 pcrs_free_joblist(bl->joblist);
1277 log_error(LOG_LEVEL_RE_FILTER,
1278 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1282 else if (bl->dynamic)
1285 * A previous job was dynamic and as we
1286 * recompile the whole filter anyway, it
1287 * makes no sense to compile this job now.
1289 log_error(LOG_LEVEL_RE_FILTER,
1290 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1295 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1297 log_error(LOG_LEVEL_ERROR,
1298 "Adding re_filter job \'%s\' to filter %s failed: %s",
1299 buf, bl->name, pcrs_strerror(error));
1305 if (bl->joblist == NULL)
1307 bl->joblist = dummy;
1309 else if (NULL != lastjob)
1311 lastjob->next = dummy;
1314 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1319 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1320 buf, csp->config->re_filterfile[fileid], linenum);
1328 * Schedule the now-obsolete old data for unloading
1330 if (NULL != current_re_filterfile[fileid])
1332 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1336 * Chain this file into the global list of loaded files
1338 fs->next = files->next;
1340 current_re_filterfile[fileid] = fs;
1341 csp->rlist[fileid] = fs;
1345 load_re_filterfile_error:
1346 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1347 csp->config->re_filterfile[fileid]);
1353 /*********************************************************************
1355 * Function : add_loader
1357 * Description : Called from `load_config'. Called once for each input
1358 * file found in config.
1361 * 1 : loader = pointer to a function that can parse and load
1362 * the appropriate config file.
1363 * 2 : config = The configuration_spec to add the loader to.
1367 *********************************************************************/
1368 void add_loader(int (*loader)(struct client_state *),
1369 struct configuration_spec * config)
1373 for (i = 0; i < NLOADERS; i++)
1375 if (config->loaders[i] == NULL)
1377 config->loaders[i] = loader;
1385 /*********************************************************************
1387 * Function : run_loader
1389 * Description : Called from `load_config' and `listen_loop'. This
1390 * function keeps the "csp" current with any file mods
1391 * since the last loop. If a file is unchanged, the
1392 * loader functions do NOT reload the file.
1395 * 1 : csp = Current client state (buffers, headers, etc...)
1396 * Must be non-null. Reads: "csp->config"
1397 * Writes: various data members.
1399 * Returns : 0 => Ok, everything else is an error.
1401 *********************************************************************/
1402 int run_loader(struct client_state *csp)
1407 for (i = 0; i < NLOADERS; i++)
1409 if (csp->config->loaders[i] == NULL)
1413 ret |= (csp->config->loaders[i])(csp);
1419 /*********************************************************************
1421 * Function : file_has_been_modified
1423 * Description : Helper function to check if a file has been changed
1426 * 1 : filename = The name of the file to check
1427 * 2 : last_known_modification = The time of the last known
1430 * Returns : TRUE if the file has been changed,
1433 *********************************************************************/
1434 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1436 struct stat statbuf[1];
1438 if (stat(filename, statbuf) < 0)
1440 /* Error, probably file not found which counts as change. */
1444 return (last_know_modification != statbuf->st_mtime);
1448 /*********************************************************************
1450 * Function : any_loaded_file_changed
1452 * Description : Helper function to check if any loaded file has been
1453 * changed since the time it has been loaded.
1455 * XXX: Should we cache the return value for x seconds?
1458 * 1 : files_to_check = List of files to check
1460 * Returns : TRUE if any file has been changed,
1463 *********************************************************************/
1464 int any_loaded_file_changed(const struct client_state *csp)
1466 const struct file_list *file_to_check = csp->config->config_file_list;
1469 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1474 for (i = 0; i < MAX_AF_FILES; i++)
1476 if (csp->actions_list[i])
1478 file_to_check = csp->actions_list[i];
1479 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1486 for (i = 0; i < MAX_AF_FILES; i++)
1490 file_to_check = csp->rlist[i];
1491 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1498 #ifdef FEATURE_TRUST
1501 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1506 #endif /* def FEATURE_TRUST */