]> git.pld-linux.org Git - packages/pound.git/blob - pound-logfile.patch
9603e7d73db96b0de618a2a94755c6849dfdb5c2
[packages/pound.git] / pound-logfile.patch
1 diff -urNp -x '*.orig' Pound-2.8.org/config.c Pound-2.8/config.c
2 --- Pound-2.8.org/config.c      2021-10-04 21:55:51.849501382 +0200
3 +++ Pound-2.8/config.c  2021-10-04 21:55:51.876168039 +0200
4 @@ -73,7 +73,7 @@ static CODE facilitynames[] = {
5  };
6  #endif
7  
8 -static regex_t  Empty, Comment, User, Group, RootJail, Daemon, LogFacility, LogLevel, Alive, SSLEngine, Control;
9 +static regex_t  Empty, Comment, User, Group, RootJail, Daemon, LogFacility, LogFile, LogLevel, ErrorLog, Alive, SSLEngine, Control;
10  static regex_t  ListenHTTP, ListenHTTPS, End, Address, Port, Cert, xHTTP, Client, CheckURL;
11  static regex_t  Err414, Err500, Err501, Err503, MaxRequest, HeadRemove, RewriteLocation, RewriteDestination;
12  static regex_t  Service, ServiceName, URL, HeadRequire, HeadDeny, BackEnd, Emergency, Priority, HAport, HAportAddr;
13 @@ -1330,6 +1330,18 @@ parse_file(void)
14                          def_facility = facilitynames[i].c_val;
15                          break;
16                      }
17 +        } else if(!regexec(&LogFile, lin, 4, matches, 0)) {
18 +            lin[matches[1].rm_eo] = '\0';
19 +            if((log_file = strdup(lin + matches[1].rm_so)) == NULL) {
20 +                logmsg(LOG_ERR, "line %d: LogFile config: out of memory - aborted", n_lin);
21 +                exit(1);
22 +            }
23 +        } else if(!regexec(&ErrorLog, lin, 4, matches, 0)) {
24 +            lin[matches[1].rm_eo] = '\0';
25 +            if((error_log = strdup(lin + matches[1].rm_so)) == NULL) {
26 +                logmsg(LOG_ERR, "line %d: ErrorLog config: out of memory - aborted", n_lin);
27 +                exit(1);
28 +            }
29          } else if(!regexec(&Grace, lin, 4, matches, 0)) {
30              grace = atoi(lin + matches[1].rm_so);
31          } else if(!regexec(&LogLevel, lin, 4, matches, 0)) {
32 @@ -1436,7 +1448,9 @@ config_parse(const int argc, char **cons
33      || regcomp(&Daemon, "^[ \t]*Daemon[ \t]+([01])[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
34      || regcomp(&Threads, "^[ \t]*Threads[ \t]+([1-9][0-9]*)[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
35      || regcomp(&LogFacility, "^[ \t]*LogFacility[ \t]+([a-z0-9-]+)[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
36 +    || regcomp(&LogFile, "^[ \t]*LogFile[ \t]+(.+)[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
37      || regcomp(&LogLevel, "^[ \t]*LogLevel[ \t]+([0-5])[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
38 +    || regcomp(&ErrorLog, "^[ \t]*ErrorLog[ \t]+(.+)[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
39      || regcomp(&Grace, "^[ \t]*Grace[ \t]+([0-9]+)[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
40      || regcomp(&Alive, "^[ \t]*Alive[ \t]+([1-9][0-9]*)[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
41      || regcomp(&SSLEngine, "^[ \t]*SSLEngine[ \t]+\"(.+)\"[ \t]*$", REG_ICASE | REG_NEWLINE | REG_EXTENDED)
42 @@ -1572,6 +1586,8 @@ config_parse(const int argc, char **cons
43      group = NULL;
44      root_jail = NULL;
45      ctrl_name = NULL;
46 +    log_file = NULL;
47 +    error_log = NULL;
48  
49      numthreads = 128;
50      alive_to = 30;
51 @@ -1601,7 +1617,9 @@ config_parse(const int argc, char **cons
52      regfree(&Daemon);
53      regfree(&Threads);
54      regfree(&LogFacility);
55 +    regfree(&LogFile);
56      regfree(&LogLevel);
57 +    regfree(&ErrorLog);
58      regfree(&Grace);
59      regfree(&Alive);
60      regfree(&SSLEngine);
61 diff -urNp -x '*.orig' Pound-2.8.org/pound.c Pound-2.8/pound.c
62 --- Pound-2.8.org/pound.c       2018-05-11 12:16:05.000000000 +0200
63 +++ Pound-2.8/pound.c   2021-10-04 21:55:51.876168039 +0200
64 @@ -32,6 +32,8 @@ char        *user,              /* user
65              *group,             /* group to run as */
66              *root_jail,         /* directory to chroot to */
67              *pid_name,          /* file to record pid in */
68 +            *log_file,          /* print log messages to this file, instead of syslog */
69 +            *error_log,         /* print error messages to this file, instead of syslog */
70              *ctrl_name;         /* control socket name */
71  
72  int         alive_to,           /* check interval for resurrection */
73 @@ -54,6 +56,7 @@ regex_t HEADER,             /* Allowed h
74          AUTHORIZATION;      /* the Authorisation header */
75  
76  static int  shut_down = 0;
77 +static int  flush_logs = 0;
78  
79  #ifndef  SOL_TCP
80  /* for systems without the definition */
81 @@ -183,6 +186,33 @@ get_thr_qlen(void)
82      return res;
83  }
84  
85 +static void
86 +reopen_logs()
87 +{
88 +
89 +    logmsg(LOG_NOTICE, "reopening logs...");
90 +    // reopen logs, but check access first, as it might fail in chroot
91 +    if (log_file) {
92 +        if (access(log_file, F_OK) == 0 && access(log_file, W_OK) == -1) {
93 +            logmsg(LOG_ERR, "%s: %s", log_file, strerror(errno));
94 +        } else {
95 +            stdout = freopen(log_file, "a", stdout);
96 +            logmsg(LOG_ERR, "reopen stdout: %p", stdout);
97 +            setlinebuf(stdout);
98 +        }
99 +    }
100 +    if (error_log) {
101 +        if (access(error_log, F_OK) == 0 && access(error_log, W_OK) == -1) {
102 +            logmsg(LOG_ERR, "%s: %s", error_log, strerror(errno));
103 +        } else {
104 +            stderr = freopen(error_log, "a", stderr);
105 +            logmsg(LOG_ERR, "reopen stderr: %p", stderr);
106 +            setlinebuf(stderr);
107 +        }
108 +    }
109 +    flush_logs = 0;
110 +}
111 +
112  /*
113   * handle SIGTERM/SIGQUIT - exit
114   */
115 @@ -220,6 +250,21 @@ h_shut(const int sig)
116  }
117  
118  /*
119 + * handle SIGUSR1 - reopen logs
120 + */
121 +static RETSIGTYPE
122 +h_reopen(const int sig)
123 +{
124 +    logmsg(LOG_NOTICE, "%d received signal %d - reopening logs...", getpid(), sig);
125 +    if (son > 0) {
126 +        reopen_logs();
127 +        kill(son, sig);
128 +    } else {
129 +        flush_logs = 1;
130 +    }
131 +}
132 +
133 +/*
134   * Pound: the reverse-proxy/load-balancer
135   *
136   * Arguments:
137 @@ -254,6 +299,7 @@ main(const int argc, char **argv)
138      signal(SIGINT, h_shut);
139      signal(SIGTERM, h_term);
140      signal(SIGQUIT, h_term);
141 +    signal(SIGUSR1, h_reopen);
142      signal(SIGPIPE, SIG_IGN);
143  
144      srandom(getpid());
145 @@ -386,11 +432,23 @@ main(const int argc, char **argv)
146          /* daemonize - make ourselves a subprocess. */
147          switch (fork()) {
148              case 0:
149 -                if(log_facility != -1) {
150 -                    close(0);
151 -                    close(1);
152 -                    close(2);
153 +                if (error_log) {
154 +                    stderr = freopen(error_log, "a", stderr);
155 +                    setlinebuf(stderr);
156 +                } else {
157 +                    if (log_facility != -1) {
158 +                        close(2);
159 +                    }
160                  }
161 +                if (log_file) {
162 +                    stdout = freopen(log_file, "a", stdout);
163 +                    setlinebuf(stdout);
164 +                } else {
165 +                    if (log_facility != -1) {
166 +                        close(1);
167 +                    }
168 +                }
169 +                close(0);
170                  break;
171              case -1:
172                  logmsg(LOG_ERR, "fork: %s - aborted", strerror(errno));
173 @@ -448,7 +506,6 @@ main(const int argc, char **argv)
174                  logmsg(LOG_ERR, "MONITOR: worker exited (stopped?) %d, restarting...", status);
175          } else if (son == 0) {
176  #endif
177 -
178              /* thread stuff */
179              pthread_attr_init(&attr);
180              pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
181 @@ -499,6 +556,11 @@ main(const int argc, char **argv)
182                          (void)unlink(ctrl_name);
183                      exit(0);
184                  }
185 +
186 +                               if (flush_logs) {
187 +                    reopen_logs();
188 +                               }
189 +
190                  for(lstn = listeners, i = 0; i < n_listeners; lstn = lstn->next, i++) {
191                      polls[i].events = POLLIN | POLLPRI;
192                      polls[i].revents = 0;
193 diff -urNp -x '*.orig' Pound-2.8.org/pound.h Pound-2.8/pound.h
194 --- Pound-2.8.org/pound.h       2018-05-11 12:16:05.000000000 +0200
195 +++ Pound-2.8/pound.h   2021-10-04 21:55:51.876168039 +0200
196 @@ -264,6 +264,8 @@ extern char *user,              /* user
197              *group,             /* group to run as */
198              *root_jail,         /* directory to chroot to */
199              *pid_name,          /* file to record pid in */
200 +            *log_file,          /* print log messages to this file, instead of syslog */
201 +            *error_log,         /* print error messages to this file, instead of syslog */
202              *ctrl_name;         /* control socket name */
203  
204  extern int  numthreads,         /* number of worker threads */
205 diff -urNp -x '*.orig' Pound-2.8.org/svc.c Pound-2.8/svc.c
206 --- Pound-2.8.org/svc.c 2021-10-04 21:55:51.849501382 +0200
207 +++ Pound-2.8/svc.c     2021-10-04 21:55:51.876168039 +0200
208 @@ -220,49 +220,47 @@ t_clean(LHASH_OF(TABNODE) *const tab, vo
209  #ifdef  HAVE_STDARG_H
210  void
211  logmsg(const int priority, const char *fmt, ...)
212 -{
213 -    char    buf[MAXBUF + 1];
214 -    va_list ap;
215 -    struct tm   *t_now, t_res;
216 -
217 -    buf[MAXBUF] = '\0';
218 -    va_start(ap, fmt);
219 -    vsnprintf(buf, MAXBUF, fmt, ap);
220 -    va_end(ap);
221 -    if(log_facility == -1) {
222 -        fprintf((priority == LOG_INFO || priority == LOG_DEBUG)? stdout: stderr, "%s\n", buf);
223 -    } else {
224 -        if(print_log)
225 -            printf("%s\n", buf);
226 -        else
227 -            syslog(log_facility | priority, "%s", buf);
228 -    }
229 -    return;
230 -}
231  #else
232  void
233  logmsg(const int priority, const char *fmt, va_alist)
234  va_dcl
235 +#endif
236  {
237      char    buf[MAXBUF + 1];
238      va_list ap;
239      struct tm   *t_now, t_res;
240 +    int log = (priority == LOG_INFO || priority == LOG_DEBUG);
241  
242      buf[MAXBUF] = '\0';
243 +#ifdef  HAVE_STDARG_H
244 +    va_start(ap, fmt);
245 +#else
246      va_start(ap);
247 +#endif
248      vsnprintf(buf, MAXBUF, fmt, ap);
249      va_end(ap);
250 -    if(log_facility == -1) {
251 -        fprintf((priority == LOG_INFO || priority == LOG_DEBUG)? stdout: stderr, "%s\n", buf);
252 +
253 +    // if access log or error log are set, use them.
254 +    if (log && log_file) {
255 +        fprintf(stdout, "%s\n", buf);
256 +        return;
257 +    }
258 +    if (!log && error_log) {
259 +        fprintf(stderr, "%s\n", buf);
260 +        return;
261 +    }
262 +
263 +    if (log_facility == -1) {
264 +        fprintf(log ? stdout : stderr, "%s\n", buf);
265      } else {
266 -        if(print_log)
267 +        if (print_log) {
268              printf("%s\n", buf);
269 -        else
270 +               } else {
271              syslog(log_facility | priority, "%s", buf);
272 +               }
273      }
274      return;
275  }
276 -#endif
277  
278  /*
279   * Translate inet/inet6 address/port into a string
This page took 0.183162 seconds and 3 git commands to generate.