1 const char loaders_rcs[] = "$Id: loaders.c,v 1.103 2016/05/08 10:45:32 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->client_iob->buf);
189 freez(csp->iob->buf);
190 freez(csp->error_message);
192 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
195 unload_forward_spec(csp->fwd);
197 free_http_request(csp->http);
199 destroy_list(csp->headers);
200 destroy_list(csp->tags);
202 free_current_action(csp->action);
204 #ifdef FEATURE_STATISTICS
206 if (csp->flags & CSP_FLAG_REJECTED)
210 #endif /* def FEATURE_STATISTICS */
214 client_list = last_active->next;
223 if ((0 == fl->active) && (NULL != fl->unloader))
225 nfl->next = fl->next;
227 (fl->unloader)(fl->f);
241 return active_threads;
246 /*********************************************************************
248 * Function : check_file_changed
250 * Description : Helper function to check if a file needs reloading.
251 * If "current" is still current, return it. Otherwise
252 * allocates a new (zeroed) "struct file_list", fills
253 * in the disk file name and timestamp, and returns it.
256 * 1 : current = The file_list currently being used - will
257 * be checked to see if it is out of date.
258 * May be NULL (which is treated as out of
260 * 2 : filename = Name of file to check.
261 * 3 : newfl = New file list. [Output only]
262 * This will be set to NULL, OR a struct
263 * file_list newly allocated on the
264 * heap, with the filename and lastmodified
265 * fields filled, and all others zeroed.
267 * Returns : If file unchanged: 0 (and sets newfl == NULL)
268 * If file changed: 1 and sets newfl != NULL
269 * On error: 1 and sets newfl == NULL
271 *********************************************************************/
272 int check_file_changed(const struct file_list * current,
273 const char * filename,
274 struct file_list ** newfl)
276 struct file_list *fs;
277 struct stat statbuf[1];
281 if (stat(filename, statbuf) < 0)
283 /* Error, probably file not found. */
288 && (current->lastmodified == statbuf->st_mtime)
289 && (0 == strcmp(current->filename, filename)))
294 fs = zalloc_or_die(sizeof(struct file_list));
295 fs->filename = strdup_or_die(filename);
296 fs->lastmodified = statbuf->st_mtime;
298 if (fs->filename == NULL)
300 /* Out of memory error */
309 /*********************************************************************
311 * Function : simple_read_line
313 * Description : Read a single line from a file and return it.
314 * This is basically a version of fgets() that malloc()s
315 * it's own line buffer. Note that the buffer will
316 * always be a multiple of BUFFER_SIZE bytes long.
317 * Therefore if you are going to keep the string for
318 * an extended period of time, you should probably
319 * strdup() it and free() the original, to save memory.
323 * 1 : dest = destination for newly malloc'd pointer to
324 * line data. Will be set to NULL on error.
325 * 2 : fp = File to read from
326 * 3 : newline = Standard for newlines in the file.
327 * Will be unchanged if it's value on input is not
329 * On output, may be changed from NEWLINE_UNKNOWN to
330 * actual convention in file.
332 * Returns : JB_ERR_OK on success
333 * JB_ERR_MEMORY on out-of-memory
334 * JB_ERR_FILE on EOF.
336 *********************************************************************/
337 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
340 size_t buflen = BUFFER_SIZE;
344 int realnewline = NEWLINE_UNKNOWN;
346 if (NULL == (buf = malloc(buflen)))
348 return JB_ERR_MEMORY;
354 * Character codes. If you have a weird compiler and the following are
355 * incorrect, you also need to fix NEWLINE() in loaders.h
357 #define CHAR_CR '\r' /* ASCII 13 */
358 #define CHAR_LF '\n' /* ASCII 10 */
378 else if (ch == CHAR_CR)
383 if (*newline == NEWLINE_UNKNOWN)
385 *newline = NEWLINE_DOS;
394 if (*newline == NEWLINE_UNKNOWN)
396 *newline = NEWLINE_MAC;
401 if (*newline == NEWLINE_UNKNOWN)
403 *newline = realnewline;
407 else if (ch == CHAR_LF)
411 if (*newline == NEWLINE_UNKNOWN)
413 *newline = NEWLINE_UNIX;
428 buflen += BUFFER_SIZE;
429 if (NULL == (p = realloc(buf, buflen)))
432 return JB_ERR_MEMORY;
441 /*********************************************************************
443 * Function : edit_read_line
445 * Description : Read a single non-empty line from a file and return
446 * it. Trims comments, leading and trailing whitespace
447 * and respects escaping of newline and comment char.
448 * Provides the line in 2 alternative forms: raw and
450 * - raw is the raw data read from the file. If the
451 * line is not modified, then this should be written
453 * - prefix is any comments and blank lines that were
454 * read from the file. If the line is modified, then
455 * this should be written out to the file followed
456 * by the modified data. (If this string is non-empty
457 * then it will have a newline at the end).
458 * - data is the actual data that will be parsed
459 * further by appropriate routines.
460 * On EOF, the 3 strings will all be set to NULL and
461 * 0 will be returned.
464 * 1 : fp = File to read from
465 * 2 : raw_out = destination for newly malloc'd pointer to
466 * raw line data. May be NULL if you don't want it.
467 * 3 : prefix_out = destination for newly malloc'd pointer to
468 * comments. May be NULL if you don't want it.
469 * 4 : data_out = destination for newly malloc'd pointer to
470 * line data with comments and leading/trailing spaces
471 * removed, and line continuation performed. May be
472 * NULL if you don't want it.
473 * 5 : newline = Standard for newlines in the file.
474 * On input, set to value to use or NEWLINE_UNKNOWN.
475 * On output, may be changed from NEWLINE_UNKNOWN to
476 * actual convention in file. May be NULL if you
478 * 6 : line_number = Line number in file. In "lines" as
479 * reported by a text editor, not lines containing data.
481 * Returns : JB_ERR_OK on success
482 * JB_ERR_MEMORY on out-of-memory
483 * JB_ERR_FILE on EOF.
485 *********************************************************************/
486 jb_err edit_read_line(FILE *fp,
491 unsigned long *line_number)
493 char *p; /* Temporary pointer */
494 char *linebuf; /* Line read from file */
495 char *linestart; /* Start of linebuf, usually first non-whitespace char */
496 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
497 int is_empty = 1; /* Flag if not got any data yet */
498 char *raw = NULL; /* String to be stored in raw_out */
499 char *prefix = NULL; /* String to be stored in prefix_out */
500 char *data = NULL; /* String to be stored in data_out */
501 int scrapnewline; /* Used for (*newline) if newline==NULL */
502 jb_err rval = JB_ERR_OK;
505 assert(raw_out || data_out);
506 assert(newline == NULL
507 || *newline == NEWLINE_UNKNOWN
508 || *newline == NEWLINE_UNIX
509 || *newline == NEWLINE_DOS
510 || *newline == NEWLINE_MAC);
514 scrapnewline = NEWLINE_UNKNOWN;
515 newline = &scrapnewline;
518 /* Set output parameters to NULL */
532 /* Set string variables to new, empty strings. */
536 raw = strdup_or_die("");
540 prefix = strdup_or_die("");
544 data = strdup_or_die("");
547 /* Main loop. Loop while we need more data & it's not EOF. */
549 while ((contflag || is_empty)
550 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
558 string_append(&raw,linebuf);
559 if (string_append(&raw,NEWLINE(*newline)))
564 return JB_ERR_MEMORY;
568 /* Line continuation? Trim escape and set flag. */
569 p = linebuf + strlen(linebuf) - 1;
570 contflag = ((*linebuf != '\0') && (*p == '\\'));
576 /* Trim leading spaces if we're at the start of the line */
578 assert(NULL != data);
581 /* Trim leading spaces */
582 while (*linestart && isspace((int)(unsigned char)*linestart))
588 /* Handle comment characters. */
590 while ((p = strchr(p, '#')) != NULL)
592 /* Found a comment char.. */
593 if ((p != linebuf) && (*(p-1) == '\\'))
595 /* ..and it's escaped, left-shift the line over the escape. */
597 while ((*q = *(q + 1)) != '\0')
601 /* Now scan from just after the "#". */
605 /* Real comment. Save it... */
608 /* Special case: Line only contains a comment, so all the
609 * previous whitespace is considered part of the comment.
610 * Undo the whitespace skipping, if any.
617 string_append(&prefix,p);
618 if (string_append(&prefix, NEWLINE(*newline)))
623 return JB_ERR_MEMORY;
627 /* ... and chop off the rest of the line */
630 } /* END while (there's a # character) */
632 /* Write to the buffer */
636 if (string_append(&data, linestart))
641 return JB_ERR_MEMORY;
646 } /* END while(we need more data) */
648 /* Handle simple_read_line() errors - ignore EOF */
649 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
657 if (raw ? (*raw == '\0') : is_empty)
659 /* EOF and no data there. (Definition of "data" depends on whether
660 * the caller cares about "raw" or just "data").
671 /* Got at least some data */
673 /* Remove trailing whitespace */
686 *prefix_out = prefix;
705 /*********************************************************************
707 * Function : read_config_line
709 * Description : Read a single non-empty line from a file and return
710 * it. Trims comments, leading and trailing whitespace
711 * and respects escaping of newline and comment char.
714 * 1 : fp = File to read from
715 * 2 : linenum = linenumber in file
716 * 3 : buf = Pointer to a pointer to set to the data buffer.
718 * Returns : NULL on EOF or error
719 * Otherwise, returns buf.
721 *********************************************************************/
722 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
725 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
728 if (err == JB_ERR_MEMORY)
730 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
739 /*********************************************************************
741 * Function : unload_trustfile
743 * Description : Unloads a trustfile.
746 * 1 : f = the data structure associated with the trustfile.
750 *********************************************************************/
751 static void unload_trustfile(void *f)
753 struct block_spec *cur = (struct block_spec *)f;
754 struct block_spec *next;
760 free_pattern_spec(cur->url);
769 #ifdef FEATURE_GRACEFUL_TERMINATION
770 /*********************************************************************
772 * Function : unload_current_trust_file
774 * Description : Unloads current trust file - reset to state at
775 * beginning of program.
781 *********************************************************************/
782 void unload_current_trust_file(void)
784 if (current_trustfile)
786 current_trustfile->unloader = unload_trustfile;
787 current_trustfile = NULL;
790 #endif /* FEATURE_GRACEFUL_TERMINATION */
793 /*********************************************************************
795 * Function : load_trustfile
797 * Description : Read and parse a trustfile and add to files list.
800 * 1 : csp = Current client state (buffers, headers, etc...)
802 * Returns : 0 => Ok, everything else is an error.
804 *********************************************************************/
805 int load_trustfile(struct client_state *csp)
809 struct block_spec *b, *bl;
810 struct pattern_spec **tl;
814 struct file_list *fs;
815 unsigned long linenum = 0;
816 int trusted_referrers = 0;
818 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
820 /* No need to load */
821 csp->tlist = current_trustfile;
826 goto load_trustfile_error;
829 fs->f = bl = zalloc_or_die(sizeof(*bl));
831 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
833 goto load_trustfile_error;
835 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
837 tl = csp->config->trust_list;
839 while (read_config_line(fp, &linenum, &buf) != NULL)
858 while ((*p++ = *q++) != '\0')
864 /* skip blank lines */
871 /* allocate a new node */
872 b = zalloc_or_die(sizeof(*b));
874 /* add it to the list */
880 /* Save the URL pattern */
881 if (create_pattern_spec(b->url, buf))
884 goto load_trustfile_error;
888 * save a pointer to URL's spec in the list of trusted URL's, too
892 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
900 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
903 * FIXME: ... after Privoxy 3.0.4 is out.
905 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
906 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
907 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
908 MAX_TRUSTED_REFERRERS, trusted_referrers);
915 /* the old one is now obsolete */
916 if (current_trustfile)
918 current_trustfile->unloader = unload_trustfile;
921 fs->next = files->next;
923 current_trustfile = fs;
928 load_trustfile_error:
929 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
930 csp->config->trustfile);
935 #endif /* def FEATURE_TRUST */
938 /*********************************************************************
940 * Function : unload_re_filterfile
942 * Description : Unload the re_filter list by freeing all chained
943 * re_filterfile specs and their data.
946 * 1 : f = the data structure associated with the filterfile.
950 *********************************************************************/
951 static void unload_re_filterfile(void *f)
953 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
959 destroy_list(b->patterns);
960 pcrs_free_joblist(b->joblist);
962 freez(b->description);
971 /*********************************************************************
973 * Function : unload_forward_spec
975 * Description : Unload the forward spec settings by freeing all
976 * memory referenced by members and the memory for
980 * 1 : fwd = the forward spec.
984 *********************************************************************/
985 void unload_forward_spec(struct forward_spec *fwd)
987 free_pattern_spec(fwd->url);
988 freez(fwd->gateway_host);
989 freez(fwd->forward_host);
996 #ifdef FEATURE_GRACEFUL_TERMINATION
997 /*********************************************************************
999 * Function : unload_current_re_filterfile
1001 * Description : Unloads current re_filter file - reset to state at
1002 * beginning of program.
1008 *********************************************************************/
1009 void unload_current_re_filterfile(void)
1013 for (i = 0; i < MAX_AF_FILES; i++)
1015 if (current_re_filterfile[i])
1017 current_re_filterfile[i]->unloader = unload_re_filterfile;
1018 current_re_filterfile[i] = NULL;
1025 /*********************************************************************
1027 * Function : load_re_filterfiles
1029 * Description : Loads all the filterfiles.
1030 * Generate a chained list of re_filterfile_spec's from
1031 * the "FILTER: " blocks, compiling all their substitutions
1032 * into chained lists of pcrs_job structs.
1035 * 1 : csp = Current client state (buffers, headers, etc...)
1037 * Returns : 0 => Ok, everything else is an error.
1039 *********************************************************************/
1040 int load_re_filterfiles(struct client_state *csp)
1045 for (i = 0; i < MAX_AF_FILES; i++)
1047 if (csp->config->re_filterfile[i])
1049 result = load_one_re_filterfile(csp, i);
1055 else if (current_re_filterfile[i])
1057 current_re_filterfile[i]->unloader = unload_re_filterfile;
1058 current_re_filterfile[i] = NULL;
1066 /*********************************************************************
1068 * Function : load_one_re_filterfile
1070 * Description : Load a re_filterfile.
1071 * Generate a chained list of re_filterfile_spec's from
1072 * the "FILTER: " blocks, compiling all their substitutions
1073 * into chained lists of pcrs_job structs.
1076 * 1 : csp = Current client state (buffers, headers, etc...)
1078 * Returns : 0 => Ok, everything else is an error.
1080 *********************************************************************/
1081 int load_one_re_filterfile(struct client_state *csp, int fileid)
1085 struct re_filterfile_spec *new_bl, *bl = NULL;
1086 struct file_list *fs;
1090 unsigned long linenum = 0;
1091 pcrs_job *dummy, *lastjob = NULL;
1094 * No need to reload if unchanged
1096 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1098 csp->rlist[fileid] = current_re_filterfile[fileid];
1103 goto load_re_filterfile_error;
1107 * Open the file or fail
1109 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1111 goto load_re_filterfile_error;
1114 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1119 while (read_config_line(fp, &linenum, &buf) != NULL)
1121 enum filter_type new_filter = FT_INVALID_FILTER;
1123 if (strncmp(buf, "FILTER:", 7) == 0)
1125 new_filter = FT_CONTENT_FILTER;
1127 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1129 new_filter = FT_SERVER_HEADER_FILTER;
1131 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1133 new_filter = FT_CLIENT_HEADER_FILTER;
1135 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1137 new_filter = FT_CLIENT_HEADER_TAGGER;
1139 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1141 new_filter = FT_SERVER_HEADER_TAGGER;
1143 #ifdef FEATURE_EXTERNAL_FILTERS
1144 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1146 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1151 * If this is the head of a new filter block, make it a
1152 * re_filterfile spec of its own and chain it to the list:
1154 if (new_filter != FT_INVALID_FILTER)
1156 new_bl = zalloc_or_die(sizeof(*bl));
1157 if (new_filter == FT_CONTENT_FILTER)
1159 new_bl->name = chomp(buf + 7);
1161 #ifdef FEATURE_EXTERNAL_FILTERS
1162 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1164 new_bl->name = chomp(buf + 16);
1169 new_bl->name = chomp(buf + 21);
1171 new_bl->type = new_filter;
1174 * If a filter description is available,
1175 * encode it to HTML and save it.
1177 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1179 *new_bl->description++ = '\0';
1180 new_bl->description = html_encode(chomp(new_bl->description));
1181 if (NULL == new_bl->description)
1183 new_bl->description = strdup_or_die("Out of memory while "
1184 "encoding filter description to HTML");
1189 new_bl->description = strdup_or_die("No description available");
1192 new_bl->name = strdup_or_die(chomp(new_bl->name));
1195 * If this is the first filter block, chain it
1196 * to the file_list rather than its (nonexistant)
1210 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1216 #ifdef FEATURE_EXTERNAL_FILTERS
1217 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1219 /* Save the code as "pattern", but do not compile anything. */
1220 if (bl->patterns->first != NULL)
1222 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1223 "Did you forget to escape a line break?",
1226 error = enlist(bl->patterns, buf);
1227 if (JB_ERR_MEMORY == error)
1229 log_error(LOG_LEVEL_FATAL,
1230 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1240 * Save the expression, make it a pcrs_job
1241 * 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: %s",
1285 buf, bl->name, pcrs_strerror(error));
1291 if (bl->joblist == NULL)
1293 bl->joblist = dummy;
1295 else if (NULL != lastjob)
1297 lastjob->next = dummy;
1300 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1305 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1306 buf, csp->config->re_filterfile[fileid], linenum);
1314 * Schedule the now-obsolete old data for unloading
1316 if (NULL != current_re_filterfile[fileid])
1318 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1322 * Chain this file into the global list of loaded files
1324 fs->next = files->next;
1326 current_re_filterfile[fileid] = fs;
1327 csp->rlist[fileid] = fs;
1331 load_re_filterfile_error:
1332 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1333 csp->config->re_filterfile[fileid]);
1339 /*********************************************************************
1341 * Function : add_loader
1343 * Description : Called from `load_config'. Called once for each input
1344 * file found in config.
1347 * 1 : loader = pointer to a function that can parse and load
1348 * the appropriate config file.
1349 * 2 : config = The configuration_spec to add the loader to.
1353 *********************************************************************/
1354 void add_loader(int (*loader)(struct client_state *),
1355 struct configuration_spec * config)
1359 for (i = 0; i < NLOADERS; i++)
1361 if (config->loaders[i] == NULL)
1363 config->loaders[i] = loader;
1371 /*********************************************************************
1373 * Function : run_loader
1375 * Description : Called from `load_config' and `listen_loop'. This
1376 * function keeps the "csp" current with any file mods
1377 * since the last loop. If a file is unchanged, the
1378 * loader functions do NOT reload the file.
1381 * 1 : csp = Current client state (buffers, headers, etc...)
1382 * Must be non-null. Reads: "csp->config"
1383 * Writes: various data members.
1385 * Returns : 0 => Ok, everything else is an error.
1387 *********************************************************************/
1388 int run_loader(struct client_state *csp)
1393 for (i = 0; i < NLOADERS; i++)
1395 if (csp->config->loaders[i] == NULL)
1399 ret |= (csp->config->loaders[i])(csp);
1405 /*********************************************************************
1407 * Function : file_has_been_modified
1409 * Description : Helper function to check if a file has been changed
1412 * 1 : filename = The name of the file to check
1413 * 2 : last_known_modification = The time of the last known
1416 * Returns : TRUE if the file has been changed,
1419 *********************************************************************/
1420 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1422 struct stat statbuf[1];
1424 if (stat(filename, statbuf) < 0)
1426 /* Error, probably file not found which counts as change. */
1430 return (last_know_modification != statbuf->st_mtime);
1434 /*********************************************************************
1436 * Function : any_loaded_file_changed
1438 * Description : Helper function to check if any loaded file has been
1439 * changed since the time it has been loaded.
1441 * XXX: Should we cache the return value for x seconds?
1444 * 1 : files_to_check = List of files to check
1446 * Returns : TRUE if any file has been changed,
1449 *********************************************************************/
1450 int any_loaded_file_changed(const struct client_state *csp)
1452 const struct file_list *file_to_check = csp->config->config_file_list;
1455 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1460 for (i = 0; i < MAX_AF_FILES; i++)
1462 if (csp->actions_list[i])
1464 file_to_check = csp->actions_list[i];
1465 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1472 for (i = 0; i < MAX_AF_FILES; i++)
1476 file_to_check = csp->rlist[i];
1477 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1484 #ifdef FEATURE_TRUST
1487 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1492 #endif /* def FEATURE_TRUST */