2 * DEBUG: section 50 Log file handling
4 * Developed based on ModUdp.* by Adrian Chadd
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "comm/Connection.h"
38 #include "log/ModTcp.h"
47 * This logfile TCP module is mostly inspired by a patch by Tim Starling
50 * It doesn't do any TCP buffering - it'd be quite a bit of work for
51 * something which the kernel could be doing for you!
62 logfile_mod_tcp_write(Logfile
* lf
, const char *buf
, size_t len
)
64 l_tcp_t
*ll
= (l_tcp_t
*) lf
->data
;
66 s
= write(ll
->fd
, (char const *) buf
, len
);
68 fd_bytes(ll
->fd
, s
, FD_WRITE
);
71 debugs(1, DBG_IMPORTANT
, "logfile (tcp): got errno (" << errno
<< "):" << xstrerror());
74 debugs(1, DBG_IMPORTANT
, "logfile (tcp): len=" << len
<< ", wrote=" << s
);
78 /* We don't worry about network errors for now */
82 logfile_mod_tcp_flush(Logfile
* lf
)
84 l_tcp_t
*ll
= (l_tcp_t
*) lf
->data
;
87 logfile_mod_tcp_write(lf
, ll
->buf
, (size_t) ll
->offset
);
92 logfile_mod_tcp_writeline(Logfile
* lf
, const char *buf
, size_t len
)
94 l_tcp_t
*ll
= (l_tcp_t
*) lf
->data
;
97 /* buffering disabled */
98 logfile_mod_tcp_write(lf
, buf
, len
);
101 if (ll
->offset
> 0 && (ll
->offset
+ len
+ 4) > ll
->bufsz
)
102 logfile_mod_tcp_flush(lf
);
104 if (len
> ll
->bufsz
) {
105 /* too big to fit in buffer */
106 logfile_mod_tcp_write(lf
, buf
, len
);
110 memcpy(ll
->buf
+ ll
->offset
, buf
, len
);
114 assert(ll
->offset
>= 0);
116 assert((size_t) ll
->offset
<= ll
->bufsz
);
120 logfile_mod_tcp_linestart(Logfile
* lf
)
125 logfile_mod_tcp_lineend(Logfile
* lf
)
127 logfile_mod_tcp_flush(lf
);
131 logfile_mod_tcp_rotate(Logfile
* lf
)
137 logfile_mod_tcp_close(Logfile
* lf
)
139 l_tcp_t
*ll
= (l_tcp_t
*) lf
->data
;
153 * This code expects the path to be //host:port
156 logfile_mod_tcp_open(Logfile
* lf
, const char *path
, size_t bufsz
, int fatal_flag
)
158 debugs(5, 3, "Tcp Open called");
163 lf
->f_close
= logfile_mod_tcp_close
;
164 lf
->f_linewrite
= logfile_mod_tcp_writeline
;
165 lf
->f_linestart
= logfile_mod_tcp_linestart
;
166 lf
->f_lineend
= logfile_mod_tcp_lineend
;
167 lf
->f_flush
= logfile_mod_tcp_flush
;
168 lf
->f_rotate
= logfile_mod_tcp_rotate
;
170 l_tcp_t
*ll
= static_cast<l_tcp_t
*>(xcalloc(1, sizeof(*ll
)));
173 if (strncmp(path
, "//", 2) == 0) {
176 strAddr
= xstrdup(path
);
178 if (!GetHostWithPort(strAddr
, &addr
)) {
179 if (lf
->flags
.fatal
) {
180 fatalf("Invalid TCP logging address '%s'\n", lf
->path
);
182 debugs(50, DBG_IMPORTANT
, "Invalid TCP logging address '" << lf
->path
<< "'");
190 Ip::Address any_addr
;
191 any_addr
.SetAnyAddr();
193 // require the sending TCP port to be of the right family for the destination address.
197 ll
->fd
= comm_open(SOCK_STREAM
, IPPROTO_TCP
, any_addr
, COMM_NONBLOCKING
, "TCP log socket");
199 if (lf
->flags
.fatal
) {
200 fatalf("Unable to open TCP socket for logging\n");
202 debugs(50, DBG_IMPORTANT
, "Unable to open TCP socket for logging");
205 } else if (!comm_connect_addr(ll
->fd
, &addr
)) {
206 if (lf
->flags
.fatal
) {
207 fatalf("Unable to connect to %s for TCP log: %s\n", lf
->path
, xstrerror());
209 debugs(50, DBG_IMPORTANT
, "Unable to connect to " << lf
->path
<< " for TCP log: " << xstrerror());
214 if (ENOENT
== errno
&& fatal_flag
) {
215 fatalf("Cannot open '%s' because\n"
216 "\tthe parent directory does not exist.\n"
217 "\tPlease create the directory.\n", path
);
218 } else if (EACCES
== errno
&& fatal_flag
) {
219 fatalf("Cannot open '%s' for writing.\n"
220 "\tThe parent directory must be writeable by the\n"
221 "\tuser '%s', which is the cache_effective_user\n"
222 "\tset in squid.conf.", path
, Config
.effectiveUser
);
224 debugs(50, DBG_IMPORTANT
, "logfileOpen (TCP): " << lf
->path
<< ": " << xstrerror());
231 ll
->buf
= static_cast<char*>(xmalloc(bufsz
));