1 const char loaders_rcs[] = "$Id: loaders.c,v 1.104 2016/05/22 12:43:07 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 #ifdef FEATURE_CLIENT_TAGS
186 freez(csp->client_address);
188 freez(csp->listen_addr_str);
189 freez(csp->client_iob->buf);
190 freez(csp->iob->buf);
191 freez(csp->error_message);
193 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
196 unload_forward_spec(csp->fwd);
198 free_http_request(csp->http);
200 destroy_list(csp->headers);
201 destroy_list(csp->tags);
203 free_current_action(csp->action);
205 #ifdef FEATURE_STATISTICS
207 if (csp->flags & CSP_FLAG_REJECTED)
211 #endif /* def FEATURE_STATISTICS */
215 client_list = last_active->next;
224 if ((0 == fl->active) && (NULL != fl->unloader))
226 nfl->next = fl->next;
228 (fl->unloader)(fl->f);
242 return active_threads;
247 /*********************************************************************
249 * Function : check_file_changed
251 * Description : Helper function to check if a file needs reloading.
252 * If "current" is still current, return it. Otherwise
253 * allocates a new (zeroed) "struct file_list", fills
254 * in the disk file name and timestamp, and returns it.
257 * 1 : current = The file_list currently being used - will
258 * be checked to see if it is out of date.
259 * May be NULL (which is treated as out of
261 * 2 : filename = Name of file to check.
262 * 3 : newfl = New file list. [Output only]
263 * This will be set to NULL, OR a struct
264 * file_list newly allocated on the
265 * heap, with the filename and lastmodified
266 * fields filled, and all others zeroed.
268 * Returns : If file unchanged: 0 (and sets newfl == NULL)
269 * If file changed: 1 and sets newfl != NULL
270 * On error: 1 and sets newfl == NULL
272 *********************************************************************/
273 int check_file_changed(const struct file_list * current,
274 const char * filename,
275 struct file_list ** newfl)
277 struct file_list *fs;
278 struct stat statbuf[1];
282 if (stat(filename, statbuf) < 0)
284 /* Error, probably file not found. */
289 && (current->lastmodified == statbuf->st_mtime)
290 && (0 == strcmp(current->filename, filename)))
295 fs = zalloc_or_die(sizeof(struct file_list));
296 fs->filename = strdup_or_die(filename);
297 fs->lastmodified = statbuf->st_mtime;
299 if (fs->filename == NULL)
301 /* Out of memory error */
310 /*********************************************************************
312 * Function : simple_read_line
314 * Description : Read a single line from a file and return it.
315 * This is basically a version of fgets() that malloc()s
316 * it's own line buffer. Note that the buffer will
317 * always be a multiple of BUFFER_SIZE bytes long.
318 * Therefore if you are going to keep the string for
319 * an extended period of time, you should probably
320 * strdup() it and free() the original, to save memory.
324 * 1 : dest = destination for newly malloc'd pointer to
325 * line data. Will be set to NULL on error.
326 * 2 : fp = File to read from
327 * 3 : newline = Standard for newlines in the file.
328 * Will be unchanged if it's value on input is not
330 * On output, may be changed from NEWLINE_UNKNOWN to
331 * actual convention in file.
333 * Returns : JB_ERR_OK on success
334 * JB_ERR_MEMORY on out-of-memory
335 * JB_ERR_FILE on EOF.
337 *********************************************************************/
338 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
341 size_t buflen = BUFFER_SIZE;
345 int realnewline = NEWLINE_UNKNOWN;
347 if (NULL == (buf = malloc(buflen)))
349 return JB_ERR_MEMORY;
355 * Character codes. If you have a weird compiler and the following are
356 * incorrect, you also need to fix NEWLINE() in loaders.h
358 #define CHAR_CR '\r' /* ASCII 13 */
359 #define CHAR_LF '\n' /* ASCII 10 */
379 else if (ch == CHAR_CR)
384 if (*newline == NEWLINE_UNKNOWN)
386 *newline = NEWLINE_DOS;
395 if (*newline == NEWLINE_UNKNOWN)
397 *newline = NEWLINE_MAC;
402 if (*newline == NEWLINE_UNKNOWN)
404 *newline = realnewline;
408 else if (ch == CHAR_LF)
412 if (*newline == NEWLINE_UNKNOWN)
414 *newline = NEWLINE_UNIX;
429 buflen += BUFFER_SIZE;
430 if (NULL == (p = realloc(buf, buflen)))
433 return JB_ERR_MEMORY;
442 /*********************************************************************
444 * Function : edit_read_line
446 * Description : Read a single non-empty line from a file and return
447 * it. Trims comments, leading and trailing whitespace
448 * and respects escaping of newline and comment char.
449 * Provides the line in 2 alternative forms: raw and
451 * - raw is the raw data read from the file. If the
452 * line is not modified, then this should be written
454 * - prefix is any comments and blank lines that were
455 * read from the file. If the line is modified, then
456 * this should be written out to the file followed
457 * by the modified data. (If this string is non-empty
458 * then it will have a newline at the end).
459 * - data is the actual data that will be parsed
460 * further by appropriate routines.
461 * On EOF, the 3 strings will all be set to NULL and
462 * 0 will be returned.
465 * 1 : fp = File to read from
466 * 2 : raw_out = destination for newly malloc'd pointer to
467 * raw line data. May be NULL if you don't want it.
468 * 3 : prefix_out = destination for newly malloc'd pointer to
469 * comments. May be NULL if you don't want it.
470 * 4 : data_out = destination for newly malloc'd pointer to
471 * line data with comments and leading/trailing spaces
472 * removed, and line continuation performed. May be
473 * NULL if you don't want it.
474 * 5 : newline = Standard for newlines in the file.
475 * On input, set to value to use or NEWLINE_UNKNOWN.
476 * On output, may be changed from NEWLINE_UNKNOWN to
477 * actual convention in file. May be NULL if you
479 * 6 : line_number = Line number in file. In "lines" as
480 * reported by a text editor, not lines containing data.
482 * Returns : JB_ERR_OK on success
483 * JB_ERR_MEMORY on out-of-memory
484 * JB_ERR_FILE on EOF.
486 *********************************************************************/
487 jb_err edit_read_line(FILE *fp,
492 unsigned long *line_number)
494 char *p; /* Temporary pointer */
495 char *linebuf; /* Line read from file */
496 char *linestart; /* Start of linebuf, usually first non-whitespace char */
497 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
498 int is_empty = 1; /* Flag if not got any data yet */
499 char *raw = NULL; /* String to be stored in raw_out */
500 char *prefix = NULL; /* String to be stored in prefix_out */
501 char *data = NULL; /* String to be stored in data_out */
502 int scrapnewline; /* Used for (*newline) if newline==NULL */
503 jb_err rval = JB_ERR_OK;
506 assert(raw_out || data_out);
507 assert(newline == NULL
508 || *newline == NEWLINE_UNKNOWN
509 || *newline == NEWLINE_UNIX
510 || *newline == NEWLINE_DOS
511 || *newline == NEWLINE_MAC);
515 scrapnewline = NEWLINE_UNKNOWN;
516 newline = &scrapnewline;
519 /* Set output parameters to NULL */
533 /* Set string variables to new, empty strings. */
537 raw = strdup_or_die("");
541 prefix = strdup_or_die("");
545 data = strdup_or_die("");
548 /* Main loop. Loop while we need more data & it's not EOF. */
550 while ((contflag || is_empty)
551 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
559 string_append(&raw,linebuf);
560 if (string_append(&raw,NEWLINE(*newline)))
565 return JB_ERR_MEMORY;
569 /* Line continuation? Trim escape and set flag. */
570 p = linebuf + strlen(linebuf) - 1;
571 contflag = ((*linebuf != '\0') && (*p == '\\'));
577 /* Trim leading spaces if we're at the start of the line */
579 assert(NULL != data);
582 /* Trim leading spaces */
583 while (*linestart && isspace((int)(unsigned char)*linestart))
589 /* Handle comment characters. */
591 while ((p = strchr(p, '#')) != NULL)
593 /* Found a comment char.. */
594 if ((p != linebuf) && (*(p-1) == '\\'))
596 /* ..and it's escaped, left-shift the line over the escape. */
598 while ((*q = *(q + 1)) != '\0')
602 /* Now scan from just after the "#". */
606 /* Real comment. Save it... */
609 /* Special case: Line only contains a comment, so all the
610 * previous whitespace is considered part of the comment.
611 * Undo the whitespace skipping, if any.
618 string_append(&prefix,p);
619 if (string_append(&prefix, NEWLINE(*newline)))
624 return JB_ERR_MEMORY;
628 /* ... and chop off the rest of the line */
631 } /* END while (there's a # character) */
633 /* Write to the buffer */
637 if (string_append(&data, linestart))
642 return JB_ERR_MEMORY;
647 } /* END while(we need more data) */
649 /* Handle simple_read_line() errors - ignore EOF */
650 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
658 if (raw ? (*raw == '\0') : is_empty)
660 /* EOF and no data there. (Definition of "data" depends on whether
661 * the caller cares about "raw" or just "data").
672 /* Got at least some data */
674 /* Remove trailing whitespace */
687 *prefix_out = prefix;
706 /*********************************************************************
708 * Function : read_config_line
710 * Description : Read a single non-empty line from a file and return
711 * it. Trims comments, leading and trailing whitespace
712 * and respects escaping of newline and comment char.
715 * 1 : fp = File to read from
716 * 2 : linenum = linenumber in file
717 * 3 : buf = Pointer to a pointer to set to the data buffer.
719 * Returns : NULL on EOF or error
720 * Otherwise, returns buf.
722 *********************************************************************/
723 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
726 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
729 if (err == JB_ERR_MEMORY)
731 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
740 /*********************************************************************
742 * Function : unload_trustfile
744 * Description : Unloads a trustfile.
747 * 1 : f = the data structure associated with the trustfile.
751 *********************************************************************/
752 static void unload_trustfile(void *f)
754 struct block_spec *cur = (struct block_spec *)f;
755 struct block_spec *next;
761 free_pattern_spec(cur->url);
770 #ifdef FEATURE_GRACEFUL_TERMINATION
771 /*********************************************************************
773 * Function : unload_current_trust_file
775 * Description : Unloads current trust file - reset to state at
776 * beginning of program.
782 *********************************************************************/
783 void unload_current_trust_file(void)
785 if (current_trustfile)
787 current_trustfile->unloader = unload_trustfile;
788 current_trustfile = NULL;
791 #endif /* FEATURE_GRACEFUL_TERMINATION */
794 /*********************************************************************
796 * Function : load_trustfile
798 * Description : Read and parse a trustfile and add to files list.
801 * 1 : csp = Current client state (buffers, headers, etc...)
803 * Returns : 0 => Ok, everything else is an error.
805 *********************************************************************/
806 int load_trustfile(struct client_state *csp)
810 struct block_spec *b, *bl;
811 struct pattern_spec **tl;
815 struct file_list *fs;
816 unsigned long linenum = 0;
817 int trusted_referrers = 0;
819 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
821 /* No need to load */
822 csp->tlist = current_trustfile;
827 goto load_trustfile_error;
830 fs->f = bl = zalloc_or_die(sizeof(*bl));
832 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
834 goto load_trustfile_error;
836 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
838 tl = csp->config->trust_list;
840 while (read_config_line(fp, &linenum, &buf) != NULL)
859 while ((*p++ = *q++) != '\0')
865 /* skip blank lines */
872 /* allocate a new node */
873 b = zalloc_or_die(sizeof(*b));
875 /* add it to the list */
881 /* Save the URL pattern */
882 if (create_pattern_spec(b->url, buf))
885 goto load_trustfile_error;
889 * save a pointer to URL's spec in the list of trusted URL's, too
893 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
901 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
904 * FIXME: ... after Privoxy 3.0.4 is out.
906 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
907 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
908 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
909 MAX_TRUSTED_REFERRERS, trusted_referrers);
916 /* the old one is now obsolete */
917 if (current_trustfile)
919 current_trustfile->unloader = unload_trustfile;
922 fs->next = files->next;
924 current_trustfile = fs;
929 load_trustfile_error:
930 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
931 csp->config->trustfile);
936 #endif /* def FEATURE_TRUST */
939 /*********************************************************************
941 * Function : unload_re_filterfile
943 * Description : Unload the re_filter list by freeing all chained
944 * re_filterfile specs and their data.
947 * 1 : f = the data structure associated with the filterfile.
951 *********************************************************************/
952 static void unload_re_filterfile(void *f)
954 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
960 destroy_list(b->patterns);
961 pcrs_free_joblist(b->joblist);
963 freez(b->description);
972 /*********************************************************************
974 * Function : unload_forward_spec
976 * Description : Unload the forward spec settings by freeing all
977 * memory referenced by members and the memory for
981 * 1 : fwd = the forward spec.
985 *********************************************************************/
986 void unload_forward_spec(struct forward_spec *fwd)
988 free_pattern_spec(fwd->url);
989 freez(fwd->gateway_host);
990 freez(fwd->forward_host);
997 #ifdef FEATURE_GRACEFUL_TERMINATION
998 /*********************************************************************
1000 * Function : unload_current_re_filterfile
1002 * Description : Unloads current re_filter file - reset to state at
1003 * beginning of program.
1009 *********************************************************************/
1010 void unload_current_re_filterfile(void)
1014 for (i = 0; i < MAX_AF_FILES; i++)
1016 if (current_re_filterfile[i])
1018 current_re_filterfile[i]->unloader = unload_re_filterfile;
1019 current_re_filterfile[i] = NULL;
1026 /*********************************************************************
1028 * Function : load_re_filterfiles
1030 * Description : Loads all the filterfiles.
1031 * Generate a chained list of re_filterfile_spec's from
1032 * the "FILTER: " blocks, compiling all their substitutions
1033 * into chained lists of pcrs_job structs.
1036 * 1 : csp = Current client state (buffers, headers, etc...)
1038 * Returns : 0 => Ok, everything else is an error.
1040 *********************************************************************/
1041 int load_re_filterfiles(struct client_state *csp)
1046 for (i = 0; i < MAX_AF_FILES; i++)
1048 if (csp->config->re_filterfile[i])
1050 result = load_one_re_filterfile(csp, i);
1056 else if (current_re_filterfile[i])
1058 current_re_filterfile[i]->unloader = unload_re_filterfile;
1059 current_re_filterfile[i] = NULL;
1067 /*********************************************************************
1069 * Function : load_one_re_filterfile
1071 * Description : Load a re_filterfile.
1072 * Generate a chained list of re_filterfile_spec's from
1073 * the "FILTER: " blocks, compiling all their substitutions
1074 * into chained lists of pcrs_job structs.
1077 * 1 : csp = Current client state (buffers, headers, etc...)
1079 * Returns : 0 => Ok, everything else is an error.
1081 *********************************************************************/
1082 int load_one_re_filterfile(struct client_state *csp, int fileid)
1086 struct re_filterfile_spec *new_bl, *bl = NULL;
1087 struct file_list *fs;
1091 unsigned long linenum = 0;
1092 pcrs_job *dummy, *lastjob = NULL;
1095 * No need to reload if unchanged
1097 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1099 csp->rlist[fileid] = current_re_filterfile[fileid];
1104 goto load_re_filterfile_error;
1108 * Open the file or fail
1110 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1112 goto load_re_filterfile_error;
1115 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1120 while (read_config_line(fp, &linenum, &buf) != NULL)
1122 enum filter_type new_filter = FT_INVALID_FILTER;
1124 if (strncmp(buf, "FILTER:", 7) == 0)
1126 new_filter = FT_CONTENT_FILTER;
1128 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1130 new_filter = FT_SERVER_HEADER_FILTER;
1132 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1134 new_filter = FT_CLIENT_HEADER_FILTER;
1136 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1138 new_filter = FT_CLIENT_HEADER_TAGGER;
1140 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1142 new_filter = FT_SERVER_HEADER_TAGGER;
1144 #ifdef FEATURE_EXTERNAL_FILTERS
1145 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1147 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1152 * If this is the head of a new filter block, make it a
1153 * re_filterfile spec of its own and chain it to the list:
1155 if (new_filter != FT_INVALID_FILTER)
1157 new_bl = zalloc_or_die(sizeof(*bl));
1158 if (new_filter == FT_CONTENT_FILTER)
1160 new_bl->name = chomp(buf + 7);
1162 #ifdef FEATURE_EXTERNAL_FILTERS
1163 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1165 new_bl->name = chomp(buf + 16);
1170 new_bl->name = chomp(buf + 21);
1172 new_bl->type = new_filter;
1175 * If a filter description is available,
1176 * encode it to HTML and save it.
1178 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1180 *new_bl->description++ = '\0';
1181 new_bl->description = html_encode(chomp(new_bl->description));
1182 if (NULL == new_bl->description)
1184 new_bl->description = strdup_or_die("Out of memory while "
1185 "encoding filter description to HTML");
1190 new_bl->description = strdup_or_die("No description available");
1193 new_bl->name = strdup_or_die(chomp(new_bl->name));
1196 * If this is the first filter block, chain it
1197 * to the file_list rather than its (nonexistant)
1211 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1217 #ifdef FEATURE_EXTERNAL_FILTERS
1218 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1220 /* Save the code as "pattern", but do not compile anything. */
1221 if (bl->patterns->first != NULL)
1223 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1224 "Did you forget to escape a line break?",
1227 error = enlist(bl->patterns, buf);
1228 if (JB_ERR_MEMORY == error)
1230 log_error(LOG_LEVEL_FATAL,
1231 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1241 * Save the expression, make it a pcrs_job
1242 * and chain it into the current filter's joblist
1244 error = enlist(bl->patterns, buf);
1245 if (JB_ERR_MEMORY == error)
1247 log_error(LOG_LEVEL_FATAL,
1248 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1250 assert(JB_ERR_OK == error);
1252 if (pcrs_job_is_dynamic(buf))
1255 * Dynamic pattern that might contain variables
1256 * and has to be recompiled for every request
1258 if (bl->joblist != NULL)
1260 pcrs_free_joblist(bl->joblist);
1264 log_error(LOG_LEVEL_RE_FILTER,
1265 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1269 else if (bl->dynamic)
1272 * A previous job was dynamic and as we
1273 * recompile the whole filter anyway, it
1274 * makes no sense to compile this job now.
1276 log_error(LOG_LEVEL_RE_FILTER,
1277 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1282 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1284 log_error(LOG_LEVEL_ERROR,
1285 "Adding re_filter job \'%s\' to filter %s failed: %s",
1286 buf, bl->name, pcrs_strerror(error));
1292 if (bl->joblist == NULL)
1294 bl->joblist = dummy;
1296 else if (NULL != lastjob)
1298 lastjob->next = dummy;
1301 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1306 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1307 buf, csp->config->re_filterfile[fileid], linenum);
1315 * Schedule the now-obsolete old data for unloading
1317 if (NULL != current_re_filterfile[fileid])
1319 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1323 * Chain this file into the global list of loaded files
1325 fs->next = files->next;
1327 current_re_filterfile[fileid] = fs;
1328 csp->rlist[fileid] = fs;
1332 load_re_filterfile_error:
1333 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1334 csp->config->re_filterfile[fileid]);
1340 /*********************************************************************
1342 * Function : add_loader
1344 * Description : Called from `load_config'. Called once for each input
1345 * file found in config.
1348 * 1 : loader = pointer to a function that can parse and load
1349 * the appropriate config file.
1350 * 2 : config = The configuration_spec to add the loader to.
1354 *********************************************************************/
1355 void add_loader(int (*loader)(struct client_state *),
1356 struct configuration_spec * config)
1360 for (i = 0; i < NLOADERS; i++)
1362 if (config->loaders[i] == NULL)
1364 config->loaders[i] = loader;
1372 /*********************************************************************
1374 * Function : run_loader
1376 * Description : Called from `load_config' and `listen_loop'. This
1377 * function keeps the "csp" current with any file mods
1378 * since the last loop. If a file is unchanged, the
1379 * loader functions do NOT reload the file.
1382 * 1 : csp = Current client state (buffers, headers, etc...)
1383 * Must be non-null. Reads: "csp->config"
1384 * Writes: various data members.
1386 * Returns : 0 => Ok, everything else is an error.
1388 *********************************************************************/
1389 int run_loader(struct client_state *csp)
1394 for (i = 0; i < NLOADERS; i++)
1396 if (csp->config->loaders[i] == NULL)
1400 ret |= (csp->config->loaders[i])(csp);
1406 /*********************************************************************
1408 * Function : file_has_been_modified
1410 * Description : Helper function to check if a file has been changed
1413 * 1 : filename = The name of the file to check
1414 * 2 : last_known_modification = The time of the last known
1417 * Returns : TRUE if the file has been changed,
1420 *********************************************************************/
1421 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1423 struct stat statbuf[1];
1425 if (stat(filename, statbuf) < 0)
1427 /* Error, probably file not found which counts as change. */
1431 return (last_know_modification != statbuf->st_mtime);
1435 /*********************************************************************
1437 * Function : any_loaded_file_changed
1439 * Description : Helper function to check if any loaded file has been
1440 * changed since the time it has been loaded.
1442 * XXX: Should we cache the return value for x seconds?
1445 * 1 : files_to_check = List of files to check
1447 * Returns : TRUE if any file has been changed,
1450 *********************************************************************/
1451 int any_loaded_file_changed(const struct client_state *csp)
1453 const struct file_list *file_to_check = csp->config->config_file_list;
1456 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1461 for (i = 0; i < MAX_AF_FILES; i++)
1463 if (csp->actions_list[i])
1465 file_to_check = csp->actions_list[i];
1466 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1473 for (i = 0; i < MAX_AF_FILES; i++)
1477 file_to_check = csp->rlist[i];
1478 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1485 #ifdef FEATURE_TRUST
1488 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1493 #endif /* def FEATURE_TRUST */