1 const char loaders_rcs[] = "$Id: loaders.c,v 1.76 2010/07/21 14:35:09 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-2010 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 * Pseudo filter type for load_one_re_filterfile
87 #define NO_NEW_FILTER -1
90 /*********************************************************************
94 * Description : Basically a mark and sweep garbage collector, it is run
95 * (by the parent thread) every once in a while to reclaim memory.
97 * It uses a mark and sweep strategy:
98 * 1) mark all files as inactive
100 * 2) check with each client:
101 * if it is active, mark its files as active
102 * if it is inactive, free its resources
104 * 3) free the resources of all of the files that
105 * are still marked as inactive (and are obsolete).
107 * N.B. files that are not obsolete don't have an unloader defined.
111 * Returns : The number of threads that are still active.
113 *********************************************************************/
114 unsigned int sweep(void)
116 struct file_list *fl, *nfl;
117 struct client_state *csp;
118 struct client_states *last_active, *client_list;
120 unsigned int active_threads = 0;
122 /* clear all of the file's active flags */
123 for ( fl = files->next; NULL != fl; fl = fl->next )
128 last_active = clients;
129 client_list = clients->next;
131 while (NULL != client_list)
133 csp = &client_list->csp;
134 if (csp->flags & CSP_FLAG_ACTIVE)
136 /* Mark this client's files as active */
139 * Always have a configuration file.
140 * (Also note the slightly non-standard extra
143 csp->config->config_file_list->active = 1;
148 for (i = 0; i < MAX_AF_FILES; i++)
150 if (csp->actions_list[i])
152 csp->actions_list[i]->active = 1;
159 for (i = 0; i < MAX_AF_FILES; i++)
163 csp->rlist[i]->active = 1;
173 csp->tlist->active = 1;
175 #endif /* def FEATURE_TRUST */
179 last_active = client_list;
180 client_list = client_list->next;
184 * This client is not active. Free its resources.
187 last_active->next = client_list->next;
189 freez(csp->ip_addr_str);
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 = (struct file_list *)zalloc(sizeof(struct file_list));
298 /* Out of memory error */
303 fs->filename = strdup(filename);
304 fs->lastmodified = statbuf->st_mtime;
306 if (fs->filename == NULL)
308 /* Out of memory error */
317 /*********************************************************************
319 * Function : simple_read_line
321 * Description : Read a single line from a file and return it.
322 * This is basically a version of fgets() that malloc()s
323 * it's own line buffer. Note that the buffer will
324 * always be a multiple of BUFFER_SIZE bytes long.
325 * Therefore if you are going to keep the string for
326 * an extended period of time, you should probably
327 * strdup() it and free() the original, to save memory.
331 * 1 : dest = destination for newly malloc'd pointer to
332 * line data. Will be set to NULL on error.
333 * 2 : fp = File to read from
334 * 3 : newline = Standard for newlines in the file.
335 * Will be unchanged if it's value on input is not
337 * On output, may be changed from NEWLINE_UNKNOWN to
338 * actual convention in file.
340 * Returns : JB_ERR_OK on success
341 * JB_ERR_MEMORY on out-of-memory
342 * JB_ERR_FILE on EOF.
344 *********************************************************************/
345 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
348 size_t buflen = BUFFER_SIZE;
352 int realnewline = NEWLINE_UNKNOWN;
354 if (NULL == (buf = malloc(buflen)))
356 return JB_ERR_MEMORY;
362 * Character codes. If you have a wierd compiler and the following are
363 * incorrect, you also need to fix NEWLINE() in loaders.h
365 #define CHAR_CR '\r' /* ASCII 13 */
366 #define CHAR_LF '\n' /* ASCII 10 */
386 else if (ch == CHAR_CR)
391 if (*newline == NEWLINE_UNKNOWN)
393 *newline = NEWLINE_DOS;
402 if (*newline == NEWLINE_UNKNOWN)
404 *newline = NEWLINE_MAC;
409 if (*newline == NEWLINE_UNKNOWN)
411 *newline = realnewline;
415 else if (ch == CHAR_LF)
419 if (*newline == NEWLINE_UNKNOWN)
421 *newline = NEWLINE_UNIX;
436 buflen += BUFFER_SIZE;
437 if (NULL == (p = realloc(buf, buflen)))
440 return JB_ERR_MEMORY;
449 /*********************************************************************
451 * Function : edit_read_line
453 * Description : Read a single non-empty line from a file and return
454 * it. Trims comments, leading and trailing whitespace
455 * and respects escaping of newline and comment char.
456 * Provides the line in 2 alternative forms: raw and
458 * - raw is the raw data read from the file. If the
459 * line is not modified, then this should be written
461 * - prefix is any comments and blank lines that were
462 * read from the file. If the line is modified, then
463 * this should be written out to the file followed
464 * by the modified data. (If this string is non-empty
465 * then it will have a newline at the end).
466 * - data is the actual data that will be parsed
467 * further by appropriate routines.
468 * On EOF, the 3 strings will all be set to NULL and
469 * 0 will be returned.
472 * 1 : fp = File to read from
473 * 2 : raw_out = destination for newly malloc'd pointer to
474 * raw line data. May be NULL if you don't want it.
475 * 3 : prefix_out = destination for newly malloc'd pointer to
476 * comments. May be NULL if you don't want it.
477 * 4 : data_out = destination for newly malloc'd pointer to
478 * line data with comments and leading/trailing spaces
479 * removed, and line continuation performed. May be
480 * NULL if you don't want it.
481 * 5 : newline = Standard for newlines in the file.
482 * On input, set to value to use or NEWLINE_UNKNOWN.
483 * On output, may be changed from NEWLINE_UNKNOWN to
484 * actual convention in file. May be NULL if you
486 * 6 : line_number = Line number in file. In "lines" as
487 * reported by a text editor, not lines containing data.
489 * Returns : JB_ERR_OK on success
490 * JB_ERR_MEMORY on out-of-memory
491 * JB_ERR_FILE on EOF.
493 *********************************************************************/
494 jb_err edit_read_line(FILE *fp,
499 unsigned long *line_number)
501 char *p; /* Temporary pointer */
502 char *linebuf; /* Line read from file */
503 char *linestart; /* Start of linebuf, usually first non-whitespace char */
504 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
505 int is_empty = 1; /* Flag if not got any data yet */
506 char *raw = NULL; /* String to be stored in raw_out */
507 char *prefix = NULL; /* String to be stored in prefix_out */
508 char *data = NULL; /* String to be stored in data_out */
509 int scrapnewline; /* Used for (*newline) if newline==NULL */
510 jb_err rval = JB_ERR_OK;
513 assert(raw_out || data_out);
514 assert(newline == NULL
515 || *newline == NEWLINE_UNKNOWN
516 || *newline == NEWLINE_UNIX
517 || *newline == NEWLINE_DOS
518 || *newline == NEWLINE_MAC);
522 scrapnewline = NEWLINE_UNKNOWN;
523 newline = &scrapnewline;
526 /* Set output parameters to NULL */
540 /* Set string variables to new, empty strings. */
547 return JB_ERR_MEMORY;
556 return JB_ERR_MEMORY;
566 return JB_ERR_MEMORY;
570 /* Main loop. Loop while we need more data & it's not EOF. */
572 while ( (contflag || is_empty)
573 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
581 string_append(&raw,linebuf);
582 if (string_append(&raw,NEWLINE(*newline)))
587 return JB_ERR_MEMORY;
591 /* Line continuation? Trim escape and set flag. */
592 p = linebuf + strlen(linebuf) - 1;
593 contflag = ((*linebuf != '\0') && (*p == '\\'));
599 /* Trim leading spaces if we're at the start of the line */
601 assert(NULL != data);
604 /* Trim leading spaces */
605 while (*linestart && isspace((int)(unsigned char)*linestart))
611 /* Handle comment characters. */
613 while ((p = strchr(p, '#')) != NULL)
615 /* Found a comment char.. */
616 if ((p != linebuf) && (*(p-1) == '\\'))
618 /* ..and it's escaped, left-shift the line over the escape. */
620 while ((*q = *(q + 1)) != '\0')
624 /* Now scan from just after the "#". */
628 /* Real comment. Save it... */
631 /* Special case: Line only contains a comment, so all the
632 * previous whitespace is considered part of the comment.
633 * Undo the whitespace skipping, if any.
640 string_append(&prefix,p);
641 if (string_append(&prefix, NEWLINE(*newline)))
646 return JB_ERR_MEMORY;
650 /* ... and chop off the rest of the line */
653 } /* END while (there's a # character) */
655 /* Write to the buffer */
661 if (string_append(&data, linestart))
666 return JB_ERR_MEMORY;
672 } /* END while(we need more data) */
674 /* Handle simple_read_line() errors - ignore EOF */
675 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
683 if (raw ? (*raw == '\0') : is_empty)
685 /* EOF and no data there. (Definition of "data" depends on whether
686 * the caller cares about "raw" or just "data").
697 /* Got at least some data */
699 /* Remove trailing whitespace */
712 *prefix_out = prefix;
731 /*********************************************************************
733 * Function : read_config_line
735 * Description : Read a single non-empty line from a file and return
736 * it. Trims comments, leading and trailing whitespace
737 * and respects escaping of newline and comment char.
740 * 1 : buf = Buffer to use.
741 * 2 : buflen = Size of buffer in bytes.
742 * 3 : fp = File to read from
743 * 4 : linenum = linenumber in file
745 * Returns : NULL on EOF or error
746 * Otherwise, returns buf.
748 *********************************************************************/
749 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
753 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
756 if (err == JB_ERR_MEMORY)
758 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
765 if (strlen(buf2) + 1U > buflen)
767 log_error(LOG_LEVEL_FATAL,
768 "Max line limit reached. Linenumber: %u. Lenght: %u. Max lenght: %u.",
769 *linenum, strlen(buf2), buflen-1);
771 strlcpy(buf, buf2, buflen);
779 /*********************************************************************
781 * Function : unload_trustfile
783 * Description : Unloads a trustfile.
786 * 1 : f = the data structure associated with the trustfile.
790 *********************************************************************/
791 static void unload_trustfile(void *f)
793 struct block_spec *cur = (struct block_spec *)f;
794 struct block_spec *next;
800 free_url_spec(cur->url);
809 #ifdef FEATURE_GRACEFUL_TERMINATION
810 /*********************************************************************
812 * Function : unload_current_trust_file
814 * Description : Unloads current trust file - reset to state at
815 * beginning of program.
821 *********************************************************************/
822 void unload_current_trust_file(void)
824 if (current_trustfile)
826 current_trustfile->unloader = unload_trustfile;
827 current_trustfile = NULL;
830 #endif /* FEATURE_GRACEFUL_TERMINATION */
833 /*********************************************************************
835 * Function : load_trustfile
837 * Description : Read and parse a trustfile and add to files list.
840 * 1 : csp = Current client state (buffers, headers, etc...)
842 * Returns : 0 => Ok, everything else is an error.
844 *********************************************************************/
845 int load_trustfile(struct client_state *csp)
849 struct block_spec *b, *bl;
850 struct url_spec **tl;
852 char buf[BUFFER_SIZE], *p, *q;
854 struct file_list *fs;
855 unsigned long linenum = 0;
856 int trusted_referrers = 0;
858 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
860 /* No need to load */
863 csp->tlist = current_trustfile;
869 goto load_trustfile_error;
872 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
875 goto load_trustfile_error;
878 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
880 goto load_trustfile_error;
882 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
884 tl = csp->config->trust_list;
886 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
902 while ((*p++ = *q++) != '\0')
908 /* skip blank lines */
914 /* allocate a new node */
915 if ((b = zalloc(sizeof(*b))) == NULL)
918 goto load_trustfile_error;
921 /* add it to the list */
927 /* Save the URL pattern */
928 if (create_url_spec(b->url, buf))
931 goto load_trustfile_error;
935 * save a pointer to URL's spec in the list of trusted URL's, too
939 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
946 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
949 * FIXME: ... after Privoxy 3.0.4 is out.
951 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
952 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
953 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
954 MAX_TRUSTED_REFERRERS, trusted_referrers);
961 /* the old one is now obsolete */
962 if (current_trustfile)
964 current_trustfile->unloader = unload_trustfile;
967 fs->next = files->next;
969 current_trustfile = fs;
978 load_trustfile_error:
979 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
980 csp->config->trustfile);
984 #endif /* def FEATURE_TRUST */
987 /*********************************************************************
989 * Function : unload_re_filterfile
991 * Description : Unload the re_filter list by freeing all chained
992 * re_filterfile specs and their data.
995 * 1 : f = the data structure associated with the filterfile.
999 *********************************************************************/
1000 static void unload_re_filterfile(void *f)
1002 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1008 destroy_list(b->patterns);
1009 pcrs_free_joblist(b->joblist);
1011 freez(b->description);
1020 /*********************************************************************
1022 * Function : unload_forward_spec
1024 * Description : Unload the forward spec settings by freeing all
1025 * memory referenced by members and the memory for
1029 * 1 : fwd = the forward spec.
1033 *********************************************************************/
1034 void unload_forward_spec(struct forward_spec *fwd)
1036 free_url_spec(fwd->url);
1037 freez(fwd->gateway_host);
1038 freez(fwd->forward_host);
1045 #ifdef FEATURE_GRACEFUL_TERMINATION
1046 /*********************************************************************
1048 * Function : unload_current_re_filterfile
1050 * Description : Unloads current re_filter file - reset to state at
1051 * beginning of program.
1057 *********************************************************************/
1058 void unload_current_re_filterfile(void)
1062 for (i = 0; i < MAX_AF_FILES; i++)
1064 if (current_re_filterfile[i])
1066 current_re_filterfile[i]->unloader = unload_re_filterfile;
1067 current_re_filterfile[i] = NULL;
1074 /*********************************************************************
1076 * Function : load_re_filterfiles
1078 * Description : Loads all the filterfiles.
1079 * Generate a chained list of re_filterfile_spec's from
1080 * the "FILTER: " blocks, compiling all their substitutions
1081 * into chained lists of pcrs_job structs.
1084 * 1 : csp = Current client state (buffers, headers, etc...)
1086 * Returns : 0 => Ok, everything else is an error.
1088 *********************************************************************/
1089 int load_re_filterfiles(struct client_state *csp)
1094 for (i = 0; i < MAX_AF_FILES; i++)
1096 if (csp->config->re_filterfile[i])
1098 result = load_one_re_filterfile(csp, i);
1104 else if (current_re_filterfile[i])
1106 current_re_filterfile[i]->unloader = unload_re_filterfile;
1107 current_re_filterfile[i] = NULL;
1115 /*********************************************************************
1117 * Function : load_one_re_filterfile
1119 * Description : Load a re_filterfile.
1120 * Generate a chained list of re_filterfile_spec's from
1121 * the "FILTER: " blocks, compiling all their substitutions
1122 * into chained lists of pcrs_job structs.
1125 * 1 : csp = Current client state (buffers, headers, etc...)
1127 * Returns : 0 => Ok, everything else is an error.
1129 *********************************************************************/
1130 int load_one_re_filterfile(struct client_state *csp, int fileid)
1134 struct re_filterfile_spec *new_bl, *bl = NULL;
1135 struct file_list *fs;
1137 char buf[BUFFER_SIZE];
1139 unsigned long linenum = 0;
1140 pcrs_job *dummy, *lastjob = NULL;
1143 * No need to reload if unchanged
1145 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1149 csp->rlist[fileid] = current_re_filterfile[fileid];
1155 goto load_re_filterfile_error;
1159 * Open the file or fail
1161 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1163 goto load_re_filterfile_error;
1166 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1171 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1173 int new_filter = NO_NEW_FILTER;
1175 if (strncmp(buf, "FILTER:", 7) == 0)
1177 new_filter = FT_CONTENT_FILTER;
1179 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1181 new_filter = FT_SERVER_HEADER_FILTER;
1183 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1185 new_filter = FT_CLIENT_HEADER_FILTER;
1187 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1189 new_filter = FT_CLIENT_HEADER_TAGGER;
1191 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1193 new_filter = FT_SERVER_HEADER_TAGGER;
1197 * If this is the head of a new filter block, make it a
1198 * re_filterfile spec of its own and chain it to the list:
1200 if (new_filter != NO_NEW_FILTER)
1202 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1205 goto load_re_filterfile_error;
1207 if (new_filter == FT_CONTENT_FILTER)
1209 new_bl->name = chomp(buf + 7);
1213 new_bl->name = chomp(buf + 21);
1215 new_bl->type = new_filter;
1218 * If a filter description is available,
1219 * encode it to HTML and save it.
1221 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1223 *new_bl->description++ = '\0';
1224 new_bl->description = html_encode(chomp(new_bl->description));
1225 if (NULL == new_bl->description)
1227 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1232 new_bl->description = strdup("No description available for this filter");
1235 new_bl->name = strdup(chomp(new_bl->name));
1238 * If this is the first filter block, chain it
1239 * to the file_list rather than its (nonexistant)
1253 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1259 * Else, save the expression, make it a pcrs_job
1260 * and chain it into the current filter's joblist
1264 error = enlist(bl->patterns, buf);
1265 if (JB_ERR_MEMORY == error)
1267 log_error(LOG_LEVEL_FATAL,
1268 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1270 assert(JB_ERR_OK == error);
1272 if (pcrs_job_is_dynamic(buf))
1275 * Dynamic pattern that might contain variables
1276 * and has to be recompiled for every request
1278 if (bl->joblist != NULL)
1280 pcrs_free_joblist(bl->joblist);
1284 log_error(LOG_LEVEL_RE_FILTER,
1285 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1288 else if (bl->dynamic)
1291 * A previous job was dynamic and as we
1292 * recompile the whole filter anyway, it
1293 * makes no sense to compile this job now.
1295 log_error(LOG_LEVEL_RE_FILTER,
1296 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1300 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1302 log_error(LOG_LEVEL_ERROR,
1303 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1308 if (bl->joblist == NULL)
1310 bl->joblist = dummy;
1312 else if (NULL != lastjob)
1314 lastjob->next = dummy;
1317 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1322 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1323 buf, csp->config->re_filterfile[fileid], linenum);
1330 * Schedule the now-obsolete old data for unloading
1332 if ( NULL != current_re_filterfile[fileid] )
1334 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1338 * Chain this file into the global list of loaded files
1340 fs->next = files->next;
1342 current_re_filterfile[fileid] = fs;
1346 csp->rlist[fileid] = fs;
1351 load_re_filterfile_error:
1352 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1353 csp->config->re_filterfile[fileid]);
1359 /*********************************************************************
1361 * Function : add_loader
1363 * Description : Called from `load_config'. Called once for each input
1364 * file found in config.
1367 * 1 : loader = pointer to a function that can parse and load
1368 * the appropriate config file.
1369 * 2 : config = The configuration_spec to add the loader to.
1373 *********************************************************************/
1374 void add_loader(int (*loader)(struct client_state *),
1375 struct configuration_spec * config)
1379 for (i=0; i < NLOADERS; i++)
1381 if (config->loaders[i] == NULL)
1383 config->loaders[i] = loader;
1391 /*********************************************************************
1393 * Function : run_loader
1395 * Description : Called from `load_config' and `listen_loop'. This
1396 * function keeps the "csp" current with any file mods
1397 * since the last loop. If a file is unchanged, the
1398 * loader functions do NOT reload the file.
1401 * 1 : csp = Current client state (buffers, headers, etc...)
1402 * Must be non-null. Reads: "csp->config"
1403 * Writes: various data members.
1405 * Returns : 0 => Ok, everything else is an error.
1407 *********************************************************************/
1408 int run_loader(struct client_state *csp)
1413 for (i=0; i < NLOADERS; i++)
1415 if (csp->config->loaders[i] == NULL)
1419 ret |= (csp->config->loaders[i])(csp);
1425 /*********************************************************************
1427 * Function : file_has_been_modified
1429 * Description : Helper function to check if a file has been changed
1432 * 1 : filename = The name of the file to check
1433 * 2 : last_known_modification = The time of the last known
1436 * Returns : TRUE if the file has been changed,
1439 *********************************************************************/
1440 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1442 struct stat statbuf[1];
1444 if (stat(filename, statbuf) < 0)
1446 /* Error, probably file not found which counts as change. */
1450 return (last_know_modification != statbuf->st_mtime);
1454 /*********************************************************************
1456 * Function : any_loaded_file_changed
1458 * Description : Helper function to check if any loaded file has been
1459 * changed since the time it has been loaded.
1461 * XXX: Should we cache the return value for x seconds?
1464 * 1 : files_to_check = List of files to check
1466 * Returns : TRUE if any file has been changed,
1469 *********************************************************************/
1470 int any_loaded_file_changed(const struct file_list *files_to_check)
1472 const struct file_list *file_to_check = files_to_check;
1474 while (file_to_check != NULL)
1476 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1478 log_error(LOG_LEVEL_INFO, "%s has been changed", file_to_check->filename);
1481 file_to_check = file_to_check->next;