Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 514 lines (417 sloc) 11.099 kb
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4 *
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 *
920e20b @alobbs Happy new year!
alobbs authored
8 * Copyright (C) 2001-2011 Alvaro Lopez Ortega
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
2c39092 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2660 5dc97367-97f1...
alobbs authored
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
23 */
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
24
25 #include "common-internal.h"
26 #include "logger_writer.h"
27 #include "util.h"
28
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
89220c0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@637 5dc97367-97f1-...
alobbs authored
32 #include <stdlib.h>
33 #include <errno.h>
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
34 #include <fcntl.h>
35
36 #ifdef HAVE_SYSLOG_H
37 # include <syslog.h>
38 #endif
39
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
40 typedef struct {
41 CHEROKEE_MUTEX_T (mutex);
42 int foo;
43 } priv_t;
44
45 #define PRIV(l) ((priv_t *)(l->priv))
46
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
47
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
48 ret_t
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
49 cherokee_logger_writer_new (cherokee_logger_writer_t **writer)
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
50 {
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
51 CHEROKEE_NEW_STRUCT(n,logger_writer);
bb452e0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@618 5dc97367-97f1-...
alobbs authored
52
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
53 INIT_LIST_HEAD (&n->listed);
bb452e0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@618 5dc97367-97f1-...
alobbs authored
54
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
55 n->type = cherokee_logger_writer_syslog;
56 n->fd = -1;
57 n->max_bufsize = DEFAULT_LOGGER_MAX_BUFSIZE;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
58
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
59 cherokee_buffer_init (&n->command);
60 cherokee_buffer_init (&n->filename);
61 cherokee_buffer_init (&n->buffer);
62
63 cherokee_buffer_ensure_size (&n->buffer, n->max_bufsize);
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
64
65 n->priv = malloc (sizeof(priv_t));
66 if (n->priv == NULL) {
1225410 @skinkie Possible memory leak. Dynamic memory stored in 'n->buffer.buf' allocated...
skinkie authored
67 cherokee_buffer_mrproper (&n->buffer);
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
68 free(n);
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
69 return ret_nomem;
70 }
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
71
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
72 CHEROKEE_MUTEX_INIT (&PRIV(n)->mutex, NULL);
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
73 n->initialized = false;
74
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
75 *writer = n;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
76 return ret_ok;
77 }
78
79
8dbf0dc @alobbs Error log management clean up. It should work better now.
alobbs authored
80 ret_t
81 cherokee_logger_writer_new_stderr (cherokee_logger_writer_t **writer)
82 {
83 ret_t ret;
84 cherokee_logger_writer_t *n;
85
86 ret = cherokee_logger_writer_new (&n);
87 if (ret != ret_ok) {
88 return ret_error;
89 }
90
91 n->type = cherokee_logger_writer_stderr;
92 n->max_bufsize = 0;
93
94 *writer = n;
95 return ret_ok;
96 }
97
98
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
99 static ret_t
100 logger_writer_close_file (cherokee_logger_writer_t *writer)
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
101 {
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
102 ret_t ret = ret_ok;
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
103
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
104 if (writer->fd != -1) {
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
105 if (writer->type != cherokee_logger_writer_stderr) {
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
106 if (cherokee_fd_close (writer->fd) != 0)
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
107 ret = ret_error;
108 }
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
109 writer->fd = -1;
110 }
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
111
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
112 return ret;
113 }
114
115
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
116 ret_t
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
117 cherokee_logger_writer_free (cherokee_logger_writer_t *writer)
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
118 {
911d6cb @alobbs Fixes the error logging subsystem. The server was segment-faulting on
alobbs authored
119 logger_writer_close_file (writer);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
120
121 cherokee_buffer_mrproper (&writer->buffer);
122 cherokee_buffer_mrproper (&writer->filename);
123 cherokee_buffer_mrproper (&writer->command);
124
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
125 CHEROKEE_MUTEX_DESTROY (&PRIV(writer)->mutex);
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
126
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
127 free (writer->priv);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
128 free (writer);
911d6cb @alobbs Fixes the error logging subsystem. The server was segment-faulting on
alobbs authored
129
130 return ret_ok;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
131 }
132
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
133 static ret_t
134 config_read_type (cherokee_config_node_t *config,
135 cherokee_logger_writer_types_t *type)
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
136 {
137 ret_t ret;
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
138 cherokee_buffer_t *tmp = NULL;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
139
140 ret = cherokee_config_node_read (config, "type", &tmp);
141 if (ret != ret_ok) {
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
142 LOG_ERROR_S (CHEROKEE_ERROR_LOGGER_NO_WRITER);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
143 return ret_error;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
144 }
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
145
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
146 if (equal_buf_str (tmp, "syslog")) {
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
147 *type = cherokee_logger_writer_syslog;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
148 } else if (equal_buf_str (tmp, "stderr")) {
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
149 *type = cherokee_logger_writer_stderr;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
150 } else if (equal_buf_str (tmp, "file")) {
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
151 *type = cherokee_logger_writer_file;
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
152 } else if (equal_buf_str (tmp, "exec")) {
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
153 *type = cherokee_logger_writer_pipe;
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
154 } else {
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
155 LOG_CRITICAL (CHEROKEE_ERROR_LOGGER_WRITER_UNKNOWN, tmp->buf);
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
156 return ret_error;
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
157 }
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
158
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
159 return ret_ok;
160 }
161
162
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
163 ret_t
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
164 cherokee_logger_writer_configure (cherokee_logger_writer_t *writer, cherokee_config_node_t *config)
165 {
166 ret_t ret;
167 cherokee_buffer_t *tmp = NULL;
168
169 /* Check the type
170 */
171 ret = config_read_type (config, &writer->type);
172 if (ret != ret_ok) {
173 return ret;
174 }
175
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
176 /* Extra properties
177 */
178 switch (writer->type) {
179 case cherokee_logger_writer_file:
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
180 ret = cherokee_config_node_read (config, "filename", &tmp);
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
181 if (ret != ret_ok) {
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
182 LOG_ERROR (CHEROKEE_ERROR_LOGGER_WRITER_READ, "file");
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
183 return ret_error;
184 }
185 cherokee_buffer_add_buffer (&writer->filename, tmp);
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
186 break;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
187
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
188 case cherokee_logger_writer_pipe:
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
189 ret = cherokee_config_node_read (config, "command", &tmp);
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
190 if (ret != ret_ok) {
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
191 LOG_ERROR (CHEROKEE_ERROR_LOGGER_WRITER_READ, "exec");
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
192 return ret_error;
193 }
194 cherokee_buffer_add_buffer (&writer->command, tmp);
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
195 break;
196 default:
197 break;
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
198 }
802276c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@638 5dc97367-97f1-...
alobbs authored
199
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
200 /* Reside the internal buffer if needed
802276c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@638 5dc97367-97f1-...
alobbs authored
201 */
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
202 ret = cherokee_config_node_read (config, "bufsize", &tmp);
203 if (ret == ret_ok) {
2459250 @alobbs Replace atoi() calls by either cherokee_atoi() or cherokee_atob().
alobbs authored
204 int buf_len;
205
206 ret = cherokee_atoi (tmp->buf, &buf_len);
207 if (ret != ret_ok) {
208 return ret_error;
209 }
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
210
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
211 if (buf_len < LOGGER_MIN_BUFSIZE)
212 buf_len = LOGGER_MIN_BUFSIZE;
213 else if (buf_len > LOGGER_MAX_BUFSIZE)
214 buf_len = LOGGER_MAX_BUFSIZE;
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
215
802276c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@638 5dc97367-97f1-...
alobbs authored
216 cherokee_buffer_mrproper (&writer->buffer);
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
217 cherokee_buffer_init (&writer->buffer);
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
218
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
219 ret = cherokee_buffer_ensure_size (&writer->buffer, buf_len);
802276c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@638 5dc97367-97f1-...
alobbs authored
220 if (ret != ret_ok) {
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
221 LOG_ERROR (CHEROKEE_ERROR_LOGGER_WRITER_ALLOC, writer->max_bufsize);
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
222 return ret_nomem;
802276c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@638 5dc97367-97f1-...
alobbs authored
223 }
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
224
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
225 writer->max_bufsize = (size_t)buf_len;
802276c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@638 5dc97367-97f1-...
alobbs authored
226 }
227
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
228 return ret_ok;
229 }
230
231
232 static ret_t
233 launch_logger_process (cherokee_logger_writer_t *writer)
234 {
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
235 #ifdef HAVE_FORK
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
236 int fd;
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
237 int to_log_fds[2];
238 pid_t pid;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
239
a68f41b @alobbs Merges the Front-Line Cache branch.
alobbs authored
240 if (cherokee_pipe (to_log_fds)) {
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
241 LOG_ERRNO (errno, cherokee_err_error, CHEROKEE_ERROR_LOGGER_WRITER_PIPE, errno);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
242 return ret_error;
243 }
244
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
245 switch (pid = fork()) {
246 case 0:
247 /* Child
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
248 */
9a6e161 @alobbs Safer fd closing
alobbs authored
249 cherokee_fd_close (STDIN_FILENO);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
250 dup2 (to_log_fds[0], STDIN_FILENO);
9a6e161 @alobbs Safer fd closing
alobbs authored
251 cherokee_fd_close (to_log_fds[0]);
252 cherokee_fd_close (to_log_fds[1]);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
253
9a6e161 @alobbs Safer fd closing
alobbs authored
254 for (fd = 3; fd < 256; fd++) {
255 cherokee_fd_close (fd);
256 }
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
257
5ced005 @alobbs Handles EINTR while invoking execv*()
alobbs authored
258 do {
259 execl("/bin/sh", "sh", "-c", writer->command.buf, NULL);
260 } while (errno == EINTR);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
261
262 SHOULDNT_HAPPEN;
263
264 case -1:
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
265 LOG_ERRNO (errno, cherokee_err_error, CHEROKEE_ERROR_LOGGER_WRITER_FORK, errno);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
266 break;
267
268 default:
9a6e161 @alobbs Safer fd closing
alobbs authored
269 cherokee_fd_close (to_log_fds[0]);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
270 writer->fd = to_log_fds[1];
271 }
272 #else
273 return ret_no_sys;
274 #endif
275 return ret_ok;
276 }
277
278
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
279 ret_t
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
280 cherokee_logger_writer_open (cherokee_logger_writer_t *writer)
281 {
4eec0fb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1754 5dc97367-97f1...
alobbs authored
282 ret_t ret;
283
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
284 switch (writer->type) {
285 case cherokee_logger_writer_syslog:
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
286 /* Nothing to do, syslog already opened at startup.
287 */
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
288 goto out;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
289
290 case cherokee_logger_writer_pipe:
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
291 ret = launch_logger_process (writer);
292 if (ret != ret_ok)
293 goto error;
294
295 goto out;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
296
297 case cherokee_logger_writer_stderr:
298 writer->fd = STDERR_FILENO;
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
299 goto out;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
300
301 case cherokee_logger_writer_file:
a68f41b @alobbs Merges the Front-Line Cache branch.
alobbs authored
302 writer->fd = cherokee_open (writer->filename.buf, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE | O_NOFOLLOW, 0640);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
303 if (writer->fd == -1) {
5c3818a @alobbs The error conversion continues: Replaces PRINT_* by LOG_*.
alobbs authored
304 LOG_ERROR (CHEROKEE_ERROR_LOGGER_WRITER_APPEND, writer->filename.buf);
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
305 ret = ret_error;
306 goto error;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
307 }
4eec0fb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1754 5dc97367-97f1...
alobbs authored
308
309 ret = cherokee_fd_set_closexec (writer->fd);
310 if (ret != ret_ok)
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
311 goto error;
4eec0fb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1754 5dc97367-97f1...
alobbs authored
312
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
313 goto out;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
314
315 default:
316 SHOULDNT_HAPPEN;
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
317 ret = ret_error;
318 goto error;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
319 }
320
0929809 @skarcha Warnings and Errors are written to "stderr" when loggers are not yet ini...
skarcha authored
321 out:
322 writer->initialized = true;
323 return ret_ok;
324 error:
325 return ret;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
326 }
327
328
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
329 ret_t
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
330 cherokee_logger_writer_reopen (cherokee_logger_writer_t *writer)
331 {
332 ret_t ret;
333
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
334 CHEROKEE_MUTEX_LOCK (&PRIV(writer)->mutex);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
335
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
336 switch (writer->type) {
337 case cherokee_logger_writer_syslog:
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
338 goto out;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
339
340 case cherokee_logger_writer_file:
341 case cherokee_logger_writer_pipe:
342 case cherokee_logger_writer_stderr:
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
343 ret = logger_writer_close_file (writer);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
344 break;
345
346 default:
347 SHOULDNT_HAPPEN;
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
348 goto error;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
349 }
350
351 ret = cherokee_logger_writer_open (writer);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
352 if (ret != ret_ok)
353 goto error;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
354
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
355 out:
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
356 CHEROKEE_MUTEX_UNLOCK (&PRIV(writer)->mutex);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
357 return ret_ok;
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
358 error:
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
359 CHEROKEE_MUTEX_UNLOCK (&PRIV(writer)->mutex);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
360 return ret_error;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
361 }
362
363
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
364 ret_t
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
365 cherokee_logger_writer_get_buf (cherokee_logger_writer_t *writer, cherokee_buffer_t **buf)
366 {
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
367 CHEROKEE_MUTEX_LOCK (&PRIV(writer)->mutex);
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
368 *buf = &writer->buffer;
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
369
370 return ret_ok;
371 }
372
373 ret_t
374 cherokee_logger_writer_release_buf (cherokee_logger_writer_t *writer)
375 {
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
376 CHEROKEE_MUTEX_UNLOCK (&PRIV(writer)->mutex);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
377 return ret_ok;
378 }
379
380
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
381 ret_t
382 cherokee_logger_writer_flush (cherokee_logger_writer_t *writer,
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
383 cherokee_boolean_t locked)
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
384 {
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
385 int re;
6be1e20 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1785 5dc97367-97f1...
alobbs authored
386 ret_t ret = ret_ok;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
387
388 /* The internal buffer might be empty
389 */
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
390 if (cherokee_buffer_is_empty (&writer->buffer)) {
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
391 return ret_ok;
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
392 }
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
393
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
394 if (!locked) {
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
395 CHEROKEE_MUTEX_LOCK (&PRIV(writer)->mutex);
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
396 }
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
397
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
398 /* If not, do the proper thing
399 */
400 switch (writer->type) {
401 case cherokee_logger_writer_stderr:
802276c @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@638 5dc97367-97f1-...
alobbs authored
402 /* In this case we ignore errors.
403 */
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
404 re = fwrite (writer->buffer.buf, 1, writer->buffer.len, stderr);
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
405 if (re != (size_t) writer->buffer.len) {
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
406 ret = ret_error;
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
407 }
408
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
409 /* Cleanup the log buffer even if there is an error,
410 * because it's safer to go on anyway.
411 */
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
412 cherokee_buffer_clean (&writer->buffer);
413 break;
414
415 case cherokee_logger_writer_pipe:
416 case cherokee_logger_writer_file:
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
417 {
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
418 ssize_t nwr = 0;
419 size_t buflen = writer->buffer.len;
420
b54b116 @alobbs Clean up: Reindents a few comments
alobbs authored
421 /* If there is at least 1 page to write then round
422 * down the length to speed up write(s).
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
423 */
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
424 if (buflen > LOGGER_BUF_PAGESIZE) {
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
425 buflen &= ~LOGGER_BUF_PAGESIZE;
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
426 }
427
89220c0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@637 5dc97367-97f1-...
alobbs authored
428 do {
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
429 nwr = write (writer->fd, writer->buffer.buf, buflen);
430 } while (nwr == -1 && errno == EINTR);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
431
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
432 if (nwr <= 0) {
b54b116 @alobbs Clean up: Reindents a few comments
alobbs authored
433 /* If an error occured in blocking write, then
434 * cleanup the log buffer now because we don't
435 * want to let it grow too much.
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
436 */
437 cherokee_buffer_clean (&writer->buffer);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
438 ret = ret_error;
439 goto out;
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
440 }
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
441
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
442 /* OK, something has been written.
443 */
444 cherokee_buffer_move_to_begin (&writer->buffer, nwr);
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
445 if (! cherokee_buffer_is_empty (&writer->buffer)) {
446 ret = ret_eagain;
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
447 }
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
448 break;
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
449 }
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
450 case cherokee_logger_writer_syslog:
b54b116 @alobbs Clean up: Reindents a few comments
alobbs authored
451 /* Write to syslog the whole log buffer, then cleanup
452 * it in any case.
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
453 */
454 ret = cherokee_syslog (LOG_INFO, &writer->buffer);
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
455 cherokee_buffer_clean (&writer->buffer);
456 break;
457
458 default:
459 SHOULDNT_HAPPEN;
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
460 ret = ret_error;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
461 }
462
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
463 out:
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
464 if (! locked) {
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
465 CHEROKEE_MUTEX_UNLOCK (&PRIV(writer)->mutex);
905addb @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3047 5dc97367-97f1...
alobbs authored
466 }
26bd721 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@640 5dc97367-97f1-...
alobbs authored
467 return ret;
acc9fd3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@610 5dc97367-97f1-...
alobbs authored
468 }
469
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
470
471 ret_t
472 cherokee_logger_writer_get_id (cherokee_config_node_t *config,
473 cherokee_buffer_t *id)
474 {
475 ret_t ret;
476 cherokee_buffer_t *tmp;
477 cherokee_logger_writer_types_t type;
478
479 ret = config_read_type (config, &type);
480 if (ret != ret_ok) {
481 return ret;
482 }
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
483
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
484 switch (type) {
485 case cherokee_logger_writer_syslog:
486 cherokee_buffer_add_str (id, "syslog");
487 break;
488 case cherokee_logger_writer_stderr:
489 cherokee_buffer_add_str (id, "stderr");
490 break;
491 case cherokee_logger_writer_file:
492 ret = cherokee_config_node_read (config, "filename", &tmp);
493 if (ret == ret_ok) {
494 cherokee_buffer_add_buffer (id, tmp);
495 } else {
496 cherokee_buffer_add_str (id, "unknown");
497 }
498 break;
499 case cherokee_logger_writer_pipe:
500 ret = cherokee_config_node_read (config, "command", &tmp);
501 if (ret == ret_ok) {
502 cherokee_buffer_add_buffer (id, tmp);
503 } else {
504 cherokee_buffer_add_str (id, "unknown");
505 }
506 break;
507 default:
508 SHOULDNT_HAPPEN;
509 return ret_error;
510 }
0f2dd5c @alobbs Fixes a really bizarre but heroically possible memory leak. It also
alobbs authored
511
6392f7f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3045 5dc97367-97f1...
alobbs authored
512 return ret_ok;
513 }
Something went wrong with that request. Please try again.