1 const char loaders_rcs[] = "$Id: loaders.c,v 1.90 2012/03/09 17:55:50 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 /*********************************************************************
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->iob->buf);
186 freez(csp->error_message);
188 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
191 unload_forward_spec(csp->fwd);
193 free_http_request(csp->http);
195 destroy_list(csp->headers);
196 destroy_list(csp->tags);
198 free_current_action(csp->action);
200 #ifdef FEATURE_STATISTICS
202 if (csp->flags & CSP_FLAG_REJECTED)
206 #endif /* def FEATURE_STATISTICS */
210 client_list = last_active->next;
219 if ((0 == fl->active) && (NULL != fl->unloader))
221 nfl->next = fl->next;
223 (fl->unloader)(fl->f);
237 return active_threads;
242 /*********************************************************************
244 * Function : check_file_changed
246 * Description : Helper function to check if a file needs reloading.
247 * If "current" is still current, return it. Otherwise
248 * allocates a new (zeroed) "struct file_list", fills
249 * in the disk file name and timestamp, and returns it.
252 * 1 : current = The file_list currently being used - will
253 * be checked to see if it is out of date.
254 * May be NULL (which is treated as out of
256 * 2 : filename = Name of file to check.
257 * 3 : newfl = New file list. [Output only]
258 * This will be set to NULL, OR a struct
259 * file_list newly allocated on the
260 * heap, with the filename and lastmodified
261 * fields filled, and all others zeroed.
263 * Returns : If file unchanged: 0 (and sets newfl == NULL)
264 * If file changed: 1 and sets newfl != NULL
265 * On error: 1 and sets newfl == NULL
267 *********************************************************************/
268 int check_file_changed(const struct file_list * current,
269 const char * filename,
270 struct file_list ** newfl)
272 struct file_list *fs;
273 struct stat statbuf[1];
277 if (stat(filename, statbuf) < 0)
279 /* Error, probably file not found. */
284 && (current->lastmodified == statbuf->st_mtime)
285 && (0 == strcmp(current->filename, filename)))
290 fs = (struct file_list *)zalloc(sizeof(struct file_list));
293 /* Out of memory error */
298 fs->filename = strdup(filename);
299 fs->lastmodified = statbuf->st_mtime;
301 if (fs->filename == NULL)
303 /* Out of memory error */
312 /*********************************************************************
314 * Function : simple_read_line
316 * Description : Read a single line from a file and return it.
317 * This is basically a version of fgets() that malloc()s
318 * it's own line buffer. Note that the buffer will
319 * always be a multiple of BUFFER_SIZE bytes long.
320 * Therefore if you are going to keep the string for
321 * an extended period of time, you should probably
322 * strdup() it and free() the original, to save memory.
326 * 1 : dest = destination for newly malloc'd pointer to
327 * line data. Will be set to NULL on error.
328 * 2 : fp = File to read from
329 * 3 : newline = Standard for newlines in the file.
330 * Will be unchanged if it's value on input is not
332 * On output, may be changed from NEWLINE_UNKNOWN to
333 * actual convention in file.
335 * Returns : JB_ERR_OK on success
336 * JB_ERR_MEMORY on out-of-memory
337 * JB_ERR_FILE on EOF.
339 *********************************************************************/
340 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
343 size_t buflen = BUFFER_SIZE;
347 int realnewline = NEWLINE_UNKNOWN;
349 if (NULL == (buf = malloc(buflen)))
351 return JB_ERR_MEMORY;
357 * Character codes. If you have a weird compiler and the following are
358 * incorrect, you also need to fix NEWLINE() in loaders.h
360 #define CHAR_CR '\r' /* ASCII 13 */
361 #define CHAR_LF '\n' /* ASCII 10 */
381 else if (ch == CHAR_CR)
386 if (*newline == NEWLINE_UNKNOWN)
388 *newline = NEWLINE_DOS;
397 if (*newline == NEWLINE_UNKNOWN)
399 *newline = NEWLINE_MAC;
404 if (*newline == NEWLINE_UNKNOWN)
406 *newline = realnewline;
410 else if (ch == CHAR_LF)
414 if (*newline == NEWLINE_UNKNOWN)
416 *newline = NEWLINE_UNIX;
431 buflen += BUFFER_SIZE;
432 if (NULL == (p = realloc(buf, buflen)))
435 return JB_ERR_MEMORY;
444 /*********************************************************************
446 * Function : edit_read_line
448 * Description : Read a single non-empty line from a file and return
449 * it. Trims comments, leading and trailing whitespace
450 * and respects escaping of newline and comment char.
451 * Provides the line in 2 alternative forms: raw and
453 * - raw is the raw data read from the file. If the
454 * line is not modified, then this should be written
456 * - prefix is any comments and blank lines that were
457 * read from the file. If the line is modified, then
458 * this should be written out to the file followed
459 * by the modified data. (If this string is non-empty
460 * then it will have a newline at the end).
461 * - data is the actual data that will be parsed
462 * further by appropriate routines.
463 * On EOF, the 3 strings will all be set to NULL and
464 * 0 will be returned.
467 * 1 : fp = File to read from
468 * 2 : raw_out = destination for newly malloc'd pointer to
469 * raw line data. May be NULL if you don't want it.
470 * 3 : prefix_out = destination for newly malloc'd pointer to
471 * comments. May be NULL if you don't want it.
472 * 4 : data_out = destination for newly malloc'd pointer to
473 * line data with comments and leading/trailing spaces
474 * removed, and line continuation performed. May be
475 * NULL if you don't want it.
476 * 5 : newline = Standard for newlines in the file.
477 * On input, set to value to use or NEWLINE_UNKNOWN.
478 * On output, may be changed from NEWLINE_UNKNOWN to
479 * actual convention in file. May be NULL if you
481 * 6 : line_number = Line number in file. In "lines" as
482 * reported by a text editor, not lines containing data.
484 * Returns : JB_ERR_OK on success
485 * JB_ERR_MEMORY on out-of-memory
486 * JB_ERR_FILE on EOF.
488 *********************************************************************/
489 jb_err edit_read_line(FILE *fp,
494 unsigned long *line_number)
496 char *p; /* Temporary pointer */
497 char *linebuf; /* Line read from file */
498 char *linestart; /* Start of linebuf, usually first non-whitespace char */
499 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
500 int is_empty = 1; /* Flag if not got any data yet */
501 char *raw = NULL; /* String to be stored in raw_out */
502 char *prefix = NULL; /* String to be stored in prefix_out */
503 char *data = NULL; /* String to be stored in data_out */
504 int scrapnewline; /* Used for (*newline) if newline==NULL */
505 jb_err rval = JB_ERR_OK;
508 assert(raw_out || data_out);
509 assert(newline == NULL
510 || *newline == NEWLINE_UNKNOWN
511 || *newline == NEWLINE_UNIX
512 || *newline == NEWLINE_DOS
513 || *newline == NEWLINE_MAC);
517 scrapnewline = NEWLINE_UNKNOWN;
518 newline = &scrapnewline;
521 /* Set output parameters to NULL */
535 /* Set string variables to new, empty strings. */
542 return JB_ERR_MEMORY;
551 return JB_ERR_MEMORY;
561 return JB_ERR_MEMORY;
565 /* Main loop. Loop while we need more data & it's not EOF. */
567 while ((contflag || is_empty)
568 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
576 string_append(&raw,linebuf);
577 if (string_append(&raw,NEWLINE(*newline)))
582 return JB_ERR_MEMORY;
586 /* Line continuation? Trim escape and set flag. */
587 p = linebuf + strlen(linebuf) - 1;
588 contflag = ((*linebuf != '\0') && (*p == '\\'));
594 /* Trim leading spaces if we're at the start of the line */
596 assert(NULL != data);
599 /* Trim leading spaces */
600 while (*linestart && isspace((int)(unsigned char)*linestart))
606 /* Handle comment characters. */
608 while ((p = strchr(p, '#')) != NULL)
610 /* Found a comment char.. */
611 if ((p != linebuf) && (*(p-1) == '\\'))
613 /* ..and it's escaped, left-shift the line over the escape. */
615 while ((*q = *(q + 1)) != '\0')
619 /* Now scan from just after the "#". */
623 /* Real comment. Save it... */
626 /* Special case: Line only contains a comment, so all the
627 * previous whitespace is considered part of the comment.
628 * Undo the whitespace skipping, if any.
635 string_append(&prefix,p);
636 if (string_append(&prefix, NEWLINE(*newline)))
641 return JB_ERR_MEMORY;
645 /* ... and chop off the rest of the line */
648 } /* END while (there's a # character) */
650 /* Write to the buffer */
656 if (string_append(&data, linestart))
661 return JB_ERR_MEMORY;
667 } /* END while(we need more data) */
669 /* Handle simple_read_line() errors - ignore EOF */
670 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
678 if (raw ? (*raw == '\0') : is_empty)
680 /* EOF and no data there. (Definition of "data" depends on whether
681 * the caller cares about "raw" or just "data").
692 /* Got at least some data */
694 /* Remove trailing whitespace */
707 *prefix_out = prefix;
726 /*********************************************************************
728 * Function : read_config_line
730 * Description : Read a single non-empty line from a file and return
731 * it. Trims comments, leading and trailing whitespace
732 * and respects escaping of newline and comment char.
735 * 1 : fp = File to read from
736 * 2 : linenum = linenumber in file
737 * 3 : buf = Pointer to a pointer to set to the data buffer.
739 * Returns : NULL on EOF or error
740 * Otherwise, returns buf.
742 *********************************************************************/
743 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
746 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
749 if (err == JB_ERR_MEMORY)
751 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
760 /*********************************************************************
762 * Function : unload_trustfile
764 * Description : Unloads a trustfile.
767 * 1 : f = the data structure associated with the trustfile.
771 *********************************************************************/
772 static void unload_trustfile(void *f)
774 struct block_spec *cur = (struct block_spec *)f;
775 struct block_spec *next;
781 free_url_spec(cur->url);
790 #ifdef FEATURE_GRACEFUL_TERMINATION
791 /*********************************************************************
793 * Function : unload_current_trust_file
795 * Description : Unloads current trust file - reset to state at
796 * beginning of program.
802 *********************************************************************/
803 void unload_current_trust_file(void)
805 if (current_trustfile)
807 current_trustfile->unloader = unload_trustfile;
808 current_trustfile = NULL;
811 #endif /* FEATURE_GRACEFUL_TERMINATION */
814 /*********************************************************************
816 * Function : load_trustfile
818 * Description : Read and parse a trustfile and add to files list.
821 * 1 : csp = Current client state (buffers, headers, etc...)
823 * Returns : 0 => Ok, everything else is an error.
825 *********************************************************************/
826 int load_trustfile(struct client_state *csp)
830 struct block_spec *b, *bl;
831 struct url_spec **tl;
835 struct file_list *fs;
836 unsigned long linenum = 0;
837 int trusted_referrers = 0;
839 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
841 /* No need to load */
842 csp->tlist = current_trustfile;
847 goto load_trustfile_error;
850 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
853 goto load_trustfile_error;
856 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
858 goto load_trustfile_error;
860 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
862 tl = csp->config->trust_list;
864 while (read_config_line(fp, &linenum, &buf) != NULL)
883 while ((*p++ = *q++) != '\0')
889 /* skip blank lines */
896 /* allocate a new node */
897 if ((b = zalloc(sizeof(*b))) == NULL)
900 goto load_trustfile_error;
903 /* add it to the list */
909 /* Save the URL pattern */
910 if (create_url_spec(b->url, buf))
913 goto load_trustfile_error;
917 * save a pointer to URL's spec in the list of trusted URL's, too
921 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
929 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
932 * FIXME: ... after Privoxy 3.0.4 is out.
934 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
935 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
936 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
937 MAX_TRUSTED_REFERRERS, trusted_referrers);
944 /* the old one is now obsolete */
945 if (current_trustfile)
947 current_trustfile->unloader = unload_trustfile;
950 fs->next = files->next;
952 current_trustfile = fs;
957 load_trustfile_error:
958 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
959 csp->config->trustfile);
964 #endif /* def FEATURE_TRUST */
967 /*********************************************************************
969 * Function : unload_re_filterfile
971 * Description : Unload the re_filter list by freeing all chained
972 * re_filterfile specs and their data.
975 * 1 : f = the data structure associated with the filterfile.
979 *********************************************************************/
980 static void unload_re_filterfile(void *f)
982 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
988 destroy_list(b->patterns);
989 pcrs_free_joblist(b->joblist);
991 freez(b->description);
1000 /*********************************************************************
1002 * Function : unload_forward_spec
1004 * Description : Unload the forward spec settings by freeing all
1005 * memory referenced by members and the memory for
1009 * 1 : fwd = the forward spec.
1013 *********************************************************************/
1014 void unload_forward_spec(struct forward_spec *fwd)
1016 free_url_spec(fwd->url);
1017 freez(fwd->gateway_host);
1018 freez(fwd->forward_host);
1025 #ifdef FEATURE_GRACEFUL_TERMINATION
1026 /*********************************************************************
1028 * Function : unload_current_re_filterfile
1030 * Description : Unloads current re_filter file - reset to state at
1031 * beginning of program.
1037 *********************************************************************/
1038 void unload_current_re_filterfile(void)
1042 for (i = 0; i < MAX_AF_FILES; i++)
1044 if (current_re_filterfile[i])
1046 current_re_filterfile[i]->unloader = unload_re_filterfile;
1047 current_re_filterfile[i] = NULL;
1054 /*********************************************************************
1056 * Function : load_re_filterfiles
1058 * Description : Loads all the filterfiles.
1059 * Generate a chained list of re_filterfile_spec's from
1060 * the "FILTER: " blocks, compiling all their substitutions
1061 * into chained lists of pcrs_job structs.
1064 * 1 : csp = Current client state (buffers, headers, etc...)
1066 * Returns : 0 => Ok, everything else is an error.
1068 *********************************************************************/
1069 int load_re_filterfiles(struct client_state *csp)
1074 for (i = 0; i < MAX_AF_FILES; i++)
1076 if (csp->config->re_filterfile[i])
1078 result = load_one_re_filterfile(csp, i);
1084 else if (current_re_filterfile[i])
1086 current_re_filterfile[i]->unloader = unload_re_filterfile;
1087 current_re_filterfile[i] = NULL;
1095 /*********************************************************************
1097 * Function : load_one_re_filterfile
1099 * Description : Load a re_filterfile.
1100 * Generate a chained list of re_filterfile_spec's from
1101 * the "FILTER: " blocks, compiling all their substitutions
1102 * into chained lists of pcrs_job structs.
1105 * 1 : csp = Current client state (buffers, headers, etc...)
1107 * Returns : 0 => Ok, everything else is an error.
1109 *********************************************************************/
1110 int load_one_re_filterfile(struct client_state *csp, int fileid)
1114 struct re_filterfile_spec *new_bl, *bl = NULL;
1115 struct file_list *fs;
1119 unsigned long linenum = 0;
1120 pcrs_job *dummy, *lastjob = NULL;
1123 * No need to reload if unchanged
1125 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1127 csp->rlist[fileid] = current_re_filterfile[fileid];
1132 goto load_re_filterfile_error;
1136 * Open the file or fail
1138 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1140 goto load_re_filterfile_error;
1143 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1148 while (read_config_line(fp, &linenum, &buf) != NULL)
1150 enum filter_type new_filter = FT_INVALID_FILTER;
1152 if (strncmp(buf, "FILTER:", 7) == 0)
1154 new_filter = FT_CONTENT_FILTER;
1156 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1158 new_filter = FT_SERVER_HEADER_FILTER;
1160 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1162 new_filter = FT_CLIENT_HEADER_FILTER;
1164 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1166 new_filter = FT_CLIENT_HEADER_TAGGER;
1168 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1170 new_filter = FT_SERVER_HEADER_TAGGER;
1174 * If this is the head of a new filter block, make it a
1175 * re_filterfile spec of its own and chain it to the list:
1177 if (new_filter != FT_INVALID_FILTER)
1179 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1182 goto load_re_filterfile_error;
1184 if (new_filter == FT_CONTENT_FILTER)
1186 new_bl->name = chomp(buf + 7);
1190 new_bl->name = chomp(buf + 21);
1192 new_bl->type = new_filter;
1195 * If a filter description is available,
1196 * encode it to HTML and save it.
1198 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1200 *new_bl->description++ = '\0';
1201 new_bl->description = html_encode(chomp(new_bl->description));
1202 if (NULL == new_bl->description)
1204 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1209 new_bl->description = strdup("No description available for this filter");
1212 new_bl->name = strdup(chomp(new_bl->name));
1215 * If this is the first filter block, chain it
1216 * to the file_list rather than its (nonexistant)
1230 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1237 * Else, save the expression, make it a pcrs_job
1238 * and chain it into the current filter's joblist
1242 error = enlist(bl->patterns, buf);
1243 if (JB_ERR_MEMORY == error)
1245 log_error(LOG_LEVEL_FATAL,
1246 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1248 assert(JB_ERR_OK == error);
1250 if (pcrs_job_is_dynamic(buf))
1253 * Dynamic pattern that might contain variables
1254 * and has to be recompiled for every request
1256 if (bl->joblist != NULL)
1258 pcrs_free_joblist(bl->joblist);
1262 log_error(LOG_LEVEL_RE_FILTER,
1263 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1267 else if (bl->dynamic)
1270 * A previous job was dynamic and as we
1271 * recompile the whole filter anyway, it
1272 * makes no sense to compile this job now.
1274 log_error(LOG_LEVEL_RE_FILTER,
1275 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1280 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1282 log_error(LOG_LEVEL_ERROR,
1283 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1289 if (bl->joblist == NULL)
1291 bl->joblist = dummy;
1293 else if (NULL != lastjob)
1295 lastjob->next = dummy;
1298 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1303 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1304 buf, csp->config->re_filterfile[fileid], linenum);
1312 * Schedule the now-obsolete old data for unloading
1314 if (NULL != current_re_filterfile[fileid])
1316 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1320 * Chain this file into the global list of loaded files
1322 fs->next = files->next;
1324 current_re_filterfile[fileid] = fs;
1325 csp->rlist[fileid] = fs;
1329 load_re_filterfile_error:
1330 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1331 csp->config->re_filterfile[fileid]);
1337 /*********************************************************************
1339 * Function : add_loader
1341 * Description : Called from `load_config'. Called once for each input
1342 * file found in config.
1345 * 1 : loader = pointer to a function that can parse and load
1346 * the appropriate config file.
1347 * 2 : config = The configuration_spec to add the loader to.
1351 *********************************************************************/
1352 void add_loader(int (*loader)(struct client_state *),
1353 struct configuration_spec * config)
1357 for (i = 0; i < NLOADERS; i++)
1359 if (config->loaders[i] == NULL)
1361 config->loaders[i] = loader;
1369 /*********************************************************************
1371 * Function : run_loader
1373 * Description : Called from `load_config' and `listen_loop'. This
1374 * function keeps the "csp" current with any file mods
1375 * since the last loop. If a file is unchanged, the
1376 * loader functions do NOT reload the file.
1379 * 1 : csp = Current client state (buffers, headers, etc...)
1380 * Must be non-null. Reads: "csp->config"
1381 * Writes: various data members.
1383 * Returns : 0 => Ok, everything else is an error.
1385 *********************************************************************/
1386 int run_loader(struct client_state *csp)
1391 for (i = 0; i < NLOADERS; i++)
1393 if (csp->config->loaders[i] == NULL)
1397 ret |= (csp->config->loaders[i])(csp);
1403 /*********************************************************************
1405 * Function : file_has_been_modified
1407 * Description : Helper function to check if a file has been changed
1410 * 1 : filename = The name of the file to check
1411 * 2 : last_known_modification = The time of the last known
1414 * Returns : TRUE if the file has been changed,
1417 *********************************************************************/
1418 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1420 struct stat statbuf[1];
1422 if (stat(filename, statbuf) < 0)
1424 /* Error, probably file not found which counts as change. */
1428 return (last_know_modification != statbuf->st_mtime);
1432 /*********************************************************************
1434 * Function : any_loaded_file_changed
1436 * Description : Helper function to check if any loaded file has been
1437 * changed since the time it has been loaded.
1439 * XXX: Should we cache the return value for x seconds?
1442 * 1 : files_to_check = List of files to check
1444 * Returns : TRUE if any file has been changed,
1447 *********************************************************************/
1448 int any_loaded_file_changed(const struct file_list *files_to_check)
1450 const struct file_list *file_to_check = files_to_check;
1452 while (file_to_check != NULL)
1454 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1458 file_to_check = file_to_check->next;