1 const char loaders_rcs[] = "$Id: loaders.c,v 1.95 2013/01/13 15:38:14 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-2012 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 = (struct file_list *)zalloc(sizeof(struct file_list));
294 /* Out of memory error */
299 fs->filename = strdup(filename);
300 fs->lastmodified = statbuf->st_mtime;
302 if (fs->filename == NULL)
304 /* Out of memory error */
313 /*********************************************************************
315 * Function : simple_read_line
317 * Description : Read a single line from a file and return it.
318 * This is basically a version of fgets() that malloc()s
319 * it's own line buffer. Note that the buffer will
320 * always be a multiple of BUFFER_SIZE bytes long.
321 * Therefore if you are going to keep the string for
322 * an extended period of time, you should probably
323 * strdup() it and free() the original, to save memory.
327 * 1 : dest = destination for newly malloc'd pointer to
328 * line data. Will be set to NULL on error.
329 * 2 : fp = File to read from
330 * 3 : newline = Standard for newlines in the file.
331 * Will be unchanged if it's value on input is not
333 * On output, may be changed from NEWLINE_UNKNOWN to
334 * actual convention in file.
336 * Returns : JB_ERR_OK on success
337 * JB_ERR_MEMORY on out-of-memory
338 * JB_ERR_FILE on EOF.
340 *********************************************************************/
341 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
344 size_t buflen = BUFFER_SIZE;
348 int realnewline = NEWLINE_UNKNOWN;
350 if (NULL == (buf = malloc(buflen)))
352 return JB_ERR_MEMORY;
358 * Character codes. If you have a weird compiler and the following are
359 * incorrect, you also need to fix NEWLINE() in loaders.h
361 #define CHAR_CR '\r' /* ASCII 13 */
362 #define CHAR_LF '\n' /* ASCII 10 */
382 else if (ch == CHAR_CR)
387 if (*newline == NEWLINE_UNKNOWN)
389 *newline = NEWLINE_DOS;
398 if (*newline == NEWLINE_UNKNOWN)
400 *newline = NEWLINE_MAC;
405 if (*newline == NEWLINE_UNKNOWN)
407 *newline = realnewline;
411 else if (ch == CHAR_LF)
415 if (*newline == NEWLINE_UNKNOWN)
417 *newline = NEWLINE_UNIX;
432 buflen += BUFFER_SIZE;
433 if (NULL == (p = realloc(buf, buflen)))
436 return JB_ERR_MEMORY;
445 /*********************************************************************
447 * Function : edit_read_line
449 * Description : Read a single non-empty line from a file and return
450 * it. Trims comments, leading and trailing whitespace
451 * and respects escaping of newline and comment char.
452 * Provides the line in 2 alternative forms: raw and
454 * - raw is the raw data read from the file. If the
455 * line is not modified, then this should be written
457 * - prefix is any comments and blank lines that were
458 * read from the file. If the line is modified, then
459 * this should be written out to the file followed
460 * by the modified data. (If this string is non-empty
461 * then it will have a newline at the end).
462 * - data is the actual data that will be parsed
463 * further by appropriate routines.
464 * On EOF, the 3 strings will all be set to NULL and
465 * 0 will be returned.
468 * 1 : fp = File to read from
469 * 2 : raw_out = destination for newly malloc'd pointer to
470 * raw line data. May be NULL if you don't want it.
471 * 3 : prefix_out = destination for newly malloc'd pointer to
472 * comments. May be NULL if you don't want it.
473 * 4 : data_out = destination for newly malloc'd pointer to
474 * line data with comments and leading/trailing spaces
475 * removed, and line continuation performed. May be
476 * NULL if you don't want it.
477 * 5 : newline = Standard for newlines in the file.
478 * On input, set to value to use or NEWLINE_UNKNOWN.
479 * On output, may be changed from NEWLINE_UNKNOWN to
480 * actual convention in file. May be NULL if you
482 * 6 : line_number = Line number in file. In "lines" as
483 * reported by a text editor, not lines containing data.
485 * Returns : JB_ERR_OK on success
486 * JB_ERR_MEMORY on out-of-memory
487 * JB_ERR_FILE on EOF.
489 *********************************************************************/
490 jb_err edit_read_line(FILE *fp,
495 unsigned long *line_number)
497 char *p; /* Temporary pointer */
498 char *linebuf; /* Line read from file */
499 char *linestart; /* Start of linebuf, usually first non-whitespace char */
500 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
501 int is_empty = 1; /* Flag if not got any data yet */
502 char *raw = NULL; /* String to be stored in raw_out */
503 char *prefix = NULL; /* String to be stored in prefix_out */
504 char *data = NULL; /* String to be stored in data_out */
505 int scrapnewline; /* Used for (*newline) if newline==NULL */
506 jb_err rval = JB_ERR_OK;
509 assert(raw_out || data_out);
510 assert(newline == NULL
511 || *newline == NEWLINE_UNKNOWN
512 || *newline == NEWLINE_UNIX
513 || *newline == NEWLINE_DOS
514 || *newline == NEWLINE_MAC);
518 scrapnewline = NEWLINE_UNKNOWN;
519 newline = &scrapnewline;
522 /* Set output parameters to NULL */
536 /* Set string variables to new, empty strings. */
543 return JB_ERR_MEMORY;
552 return JB_ERR_MEMORY;
562 return JB_ERR_MEMORY;
566 /* Main loop. Loop while we need more data & it's not EOF. */
568 while ((contflag || is_empty)
569 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
577 string_append(&raw,linebuf);
578 if (string_append(&raw,NEWLINE(*newline)))
583 return JB_ERR_MEMORY;
587 /* Line continuation? Trim escape and set flag. */
588 p = linebuf + strlen(linebuf) - 1;
589 contflag = ((*linebuf != '\0') && (*p == '\\'));
595 /* Trim leading spaces if we're at the start of the line */
597 assert(NULL != data);
600 /* Trim leading spaces */
601 while (*linestart && isspace((int)(unsigned char)*linestart))
607 /* Handle comment characters. */
609 while ((p = strchr(p, '#')) != NULL)
611 /* Found a comment char.. */
612 if ((p != linebuf) && (*(p-1) == '\\'))
614 /* ..and it's escaped, left-shift the line over the escape. */
616 while ((*q = *(q + 1)) != '\0')
620 /* Now scan from just after the "#". */
624 /* Real comment. Save it... */
627 /* Special case: Line only contains a comment, so all the
628 * previous whitespace is considered part of the comment.
629 * Undo the whitespace skipping, if any.
636 string_append(&prefix,p);
637 if (string_append(&prefix, NEWLINE(*newline)))
642 return JB_ERR_MEMORY;
646 /* ... and chop off the rest of the line */
649 } /* END while (there's a # character) */
651 /* Write to the buffer */
657 if (string_append(&data, linestart))
662 return JB_ERR_MEMORY;
668 } /* END while(we need more data) */
670 /* Handle simple_read_line() errors - ignore EOF */
671 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
679 if (raw ? (*raw == '\0') : is_empty)
681 /* EOF and no data there. (Definition of "data" depends on whether
682 * the caller cares about "raw" or just "data").
693 /* Got at least some data */
695 /* Remove trailing whitespace */
708 *prefix_out = prefix;
727 /*********************************************************************
729 * Function : read_config_line
731 * Description : Read a single non-empty line from a file and return
732 * it. Trims comments, leading and trailing whitespace
733 * and respects escaping of newline and comment char.
736 * 1 : fp = File to read from
737 * 2 : linenum = linenumber in file
738 * 3 : buf = Pointer to a pointer to set to the data buffer.
740 * Returns : NULL on EOF or error
741 * Otherwise, returns buf.
743 *********************************************************************/
744 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
747 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
750 if (err == JB_ERR_MEMORY)
752 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
761 /*********************************************************************
763 * Function : unload_trustfile
765 * Description : Unloads a trustfile.
768 * 1 : f = the data structure associated with the trustfile.
772 *********************************************************************/
773 static void unload_trustfile(void *f)
775 struct block_spec *cur = (struct block_spec *)f;
776 struct block_spec *next;
782 free_url_spec(cur->url);
791 #ifdef FEATURE_GRACEFUL_TERMINATION
792 /*********************************************************************
794 * Function : unload_current_trust_file
796 * Description : Unloads current trust file - reset to state at
797 * beginning of program.
803 *********************************************************************/
804 void unload_current_trust_file(void)
806 if (current_trustfile)
808 current_trustfile->unloader = unload_trustfile;
809 current_trustfile = NULL;
812 #endif /* FEATURE_GRACEFUL_TERMINATION */
815 /*********************************************************************
817 * Function : load_trustfile
819 * Description : Read and parse a trustfile and add to files list.
822 * 1 : csp = Current client state (buffers, headers, etc...)
824 * Returns : 0 => Ok, everything else is an error.
826 *********************************************************************/
827 int load_trustfile(struct client_state *csp)
831 struct block_spec *b, *bl;
832 struct pattern_spec **tl;
836 struct file_list *fs;
837 unsigned long linenum = 0;
838 int trusted_referrers = 0;
840 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
842 /* No need to load */
843 csp->tlist = current_trustfile;
848 goto load_trustfile_error;
851 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
854 goto load_trustfile_error;
857 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
859 goto load_trustfile_error;
861 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
863 tl = csp->config->trust_list;
865 while (read_config_line(fp, &linenum, &buf) != NULL)
884 while ((*p++ = *q++) != '\0')
890 /* skip blank lines */
897 /* allocate a new node */
898 if ((b = zalloc(sizeof(*b))) == NULL)
901 goto load_trustfile_error;
904 /* add it to the list */
910 /* Save the URL pattern */
911 if (create_url_spec(b->url, buf))
914 goto load_trustfile_error;
918 * save a pointer to URL's spec in the list of trusted URL's, too
922 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
930 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
933 * FIXME: ... after Privoxy 3.0.4 is out.
935 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
936 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
937 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
938 MAX_TRUSTED_REFERRERS, trusted_referrers);
945 /* the old one is now obsolete */
946 if (current_trustfile)
948 current_trustfile->unloader = unload_trustfile;
951 fs->next = files->next;
953 current_trustfile = fs;
958 load_trustfile_error:
959 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
960 csp->config->trustfile);
965 #endif /* def FEATURE_TRUST */
968 /*********************************************************************
970 * Function : unload_re_filterfile
972 * Description : Unload the re_filter list by freeing all chained
973 * re_filterfile specs and their data.
976 * 1 : f = the data structure associated with the filterfile.
980 *********************************************************************/
981 static void unload_re_filterfile(void *f)
983 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
989 destroy_list(b->patterns);
990 pcrs_free_joblist(b->joblist);
992 freez(b->description);
1001 /*********************************************************************
1003 * Function : unload_forward_spec
1005 * Description : Unload the forward spec settings by freeing all
1006 * memory referenced by members and the memory for
1010 * 1 : fwd = the forward spec.
1014 *********************************************************************/
1015 void unload_forward_spec(struct forward_spec *fwd)
1017 free_url_spec(fwd->url);
1018 freez(fwd->gateway_host);
1019 freez(fwd->forward_host);
1026 #ifdef FEATURE_GRACEFUL_TERMINATION
1027 /*********************************************************************
1029 * Function : unload_current_re_filterfile
1031 * Description : Unloads current re_filter file - reset to state at
1032 * beginning of program.
1038 *********************************************************************/
1039 void unload_current_re_filterfile(void)
1043 for (i = 0; i < MAX_AF_FILES; i++)
1045 if (current_re_filterfile[i])
1047 current_re_filterfile[i]->unloader = unload_re_filterfile;
1048 current_re_filterfile[i] = NULL;
1055 /*********************************************************************
1057 * Function : load_re_filterfiles
1059 * Description : Loads all the filterfiles.
1060 * Generate a chained list of re_filterfile_spec's from
1061 * the "FILTER: " blocks, compiling all their substitutions
1062 * into chained lists of pcrs_job structs.
1065 * 1 : csp = Current client state (buffers, headers, etc...)
1067 * Returns : 0 => Ok, everything else is an error.
1069 *********************************************************************/
1070 int load_re_filterfiles(struct client_state *csp)
1075 for (i = 0; i < MAX_AF_FILES; i++)
1077 if (csp->config->re_filterfile[i])
1079 result = load_one_re_filterfile(csp, i);
1085 else if (current_re_filterfile[i])
1087 current_re_filterfile[i]->unloader = unload_re_filterfile;
1088 current_re_filterfile[i] = NULL;
1096 /*********************************************************************
1098 * Function : load_one_re_filterfile
1100 * Description : Load a re_filterfile.
1101 * Generate a chained list of re_filterfile_spec's from
1102 * the "FILTER: " blocks, compiling all their substitutions
1103 * into chained lists of pcrs_job structs.
1106 * 1 : csp = Current client state (buffers, headers, etc...)
1108 * Returns : 0 => Ok, everything else is an error.
1110 *********************************************************************/
1111 int load_one_re_filterfile(struct client_state *csp, int fileid)
1115 struct re_filterfile_spec *new_bl, *bl = NULL;
1116 struct file_list *fs;
1120 unsigned long linenum = 0;
1121 pcrs_job *dummy, *lastjob = NULL;
1124 * No need to reload if unchanged
1126 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1128 csp->rlist[fileid] = current_re_filterfile[fileid];
1133 goto load_re_filterfile_error;
1137 * Open the file or fail
1139 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1141 goto load_re_filterfile_error;
1144 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1149 while (read_config_line(fp, &linenum, &buf) != NULL)
1151 enum filter_type new_filter = FT_INVALID_FILTER;
1153 if (strncmp(buf, "FILTER:", 7) == 0)
1155 new_filter = FT_CONTENT_FILTER;
1157 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1159 new_filter = FT_SERVER_HEADER_FILTER;
1161 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1163 new_filter = FT_CLIENT_HEADER_FILTER;
1165 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1167 new_filter = FT_CLIENT_HEADER_TAGGER;
1169 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1171 new_filter = FT_SERVER_HEADER_TAGGER;
1175 * If this is the head of a new filter block, make it a
1176 * re_filterfile spec of its own and chain it to the list:
1178 if (new_filter != FT_INVALID_FILTER)
1180 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1183 goto load_re_filterfile_error;
1185 if (new_filter == FT_CONTENT_FILTER)
1187 new_bl->name = chomp(buf + 7);
1191 new_bl->name = chomp(buf + 21);
1193 new_bl->type = new_filter;
1196 * If a filter description is available,
1197 * encode it to HTML and save it.
1199 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1201 *new_bl->description++ = '\0';
1202 new_bl->description = html_encode(chomp(new_bl->description));
1203 if (NULL == new_bl->description)
1205 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1210 new_bl->description = strdup("No description available for this filter");
1213 new_bl->name = strdup(chomp(new_bl->name));
1216 * If this is the first filter block, chain it
1217 * to the file_list rather than its (nonexistant)
1231 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1238 * Else, save the expression, make it a pcrs_job
1239 * and chain it into the current filter's joblist
1243 error = enlist(bl->patterns, buf);
1244 if (JB_ERR_MEMORY == error)
1246 log_error(LOG_LEVEL_FATAL,
1247 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1249 assert(JB_ERR_OK == error);
1251 if (pcrs_job_is_dynamic(buf))
1254 * Dynamic pattern that might contain variables
1255 * and has to be recompiled for every request
1257 if (bl->joblist != NULL)
1259 pcrs_free_joblist(bl->joblist);
1263 log_error(LOG_LEVEL_RE_FILTER,
1264 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1268 else if (bl->dynamic)
1271 * A previous job was dynamic and as we
1272 * recompile the whole filter anyway, it
1273 * makes no sense to compile this job now.
1275 log_error(LOG_LEVEL_RE_FILTER,
1276 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1281 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1283 log_error(LOG_LEVEL_ERROR,
1284 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1290 if (bl->joblist == NULL)
1292 bl->joblist = dummy;
1294 else if (NULL != lastjob)
1296 lastjob->next = dummy;
1299 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1304 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1305 buf, csp->config->re_filterfile[fileid], linenum);
1313 * Schedule the now-obsolete old data for unloading
1315 if (NULL != current_re_filterfile[fileid])
1317 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1321 * Chain this file into the global list of loaded files
1323 fs->next = files->next;
1325 current_re_filterfile[fileid] = fs;
1326 csp->rlist[fileid] = fs;
1330 load_re_filterfile_error:
1331 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1332 csp->config->re_filterfile[fileid]);
1338 /*********************************************************************
1340 * Function : add_loader
1342 * Description : Called from `load_config'. Called once for each input
1343 * file found in config.
1346 * 1 : loader = pointer to a function that can parse and load
1347 * the appropriate config file.
1348 * 2 : config = The configuration_spec to add the loader to.
1352 *********************************************************************/
1353 void add_loader(int (*loader)(struct client_state *),
1354 struct configuration_spec * config)
1358 for (i = 0; i < NLOADERS; i++)
1360 if (config->loaders[i] == NULL)
1362 config->loaders[i] = loader;
1370 /*********************************************************************
1372 * Function : run_loader
1374 * Description : Called from `load_config' and `listen_loop'. This
1375 * function keeps the "csp" current with any file mods
1376 * since the last loop. If a file is unchanged, the
1377 * loader functions do NOT reload the file.
1380 * 1 : csp = Current client state (buffers, headers, etc...)
1381 * Must be non-null. Reads: "csp->config"
1382 * Writes: various data members.
1384 * Returns : 0 => Ok, everything else is an error.
1386 *********************************************************************/
1387 int run_loader(struct client_state *csp)
1392 for (i = 0; i < NLOADERS; i++)
1394 if (csp->config->loaders[i] == NULL)
1398 ret |= (csp->config->loaders[i])(csp);
1404 /*********************************************************************
1406 * Function : file_has_been_modified
1408 * Description : Helper function to check if a file has been changed
1411 * 1 : filename = The name of the file to check
1412 * 2 : last_known_modification = The time of the last known
1415 * Returns : TRUE if the file has been changed,
1418 *********************************************************************/
1419 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1421 struct stat statbuf[1];
1423 if (stat(filename, statbuf) < 0)
1425 /* Error, probably file not found which counts as change. */
1429 return (last_know_modification != statbuf->st_mtime);
1433 /*********************************************************************
1435 * Function : any_loaded_file_changed
1437 * Description : Helper function to check if any loaded file has been
1438 * changed since the time it has been loaded.
1440 * XXX: Should we cache the return value for x seconds?
1443 * 1 : files_to_check = List of files to check
1445 * Returns : TRUE if any file has been changed,
1448 *********************************************************************/
1449 int any_loaded_file_changed(const struct client_state *csp)
1451 const struct file_list *file_to_check = csp->config->config_file_list;
1454 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1459 for (i = 0; i < MAX_AF_FILES; i++)
1461 if (csp->actions_list[i])
1463 file_to_check = csp->actions_list[i];
1464 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1471 for (i = 0; i < MAX_AF_FILES; i++)
1475 file_to_check = csp->rlist[i];
1476 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1483 #ifdef FEATURE_TRUST
1486 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1491 #endif /* def FEATURE_TRUST */