2 * DEBUG: section 50 Log file handling
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "comm/Connection.h"
37 #include "log/ModUdp.h"
47 * This logfile UDP module is mostly inspired by a patch by Tim Starling
50 * It doesn't do any UDP buffering - it'd be quite a bit of work for
51 * something which the kernel could be doing for you!
62 logfile_mod_udp_write(Logfile
* lf
, const char *buf
, size_t len
)
64 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
66 s
= write(ll
->fd
, (char const *) buf
, len
);
67 fd_bytes(ll
->fd
, s
, FD_WRITE
);
70 debugs(1, DBG_IMPORTANT
, "logfile (udp): got errno (" << errno
<< "):" << xstrerror());
73 debugs(1, DBG_IMPORTANT
, "logfile (udp): len=" << len
<< ", wrote=" << s
);
77 /* We don't worry about network errors for now */
81 logfile_mod_udp_flush(Logfile
* lf
)
83 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
86 logfile_mod_udp_write(lf
, ll
->buf
, (size_t) ll
->offset
);
91 logfile_mod_udp_writeline(Logfile
* lf
, const char *buf
, size_t len
)
93 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
96 /* buffering disabled */
97 logfile_mod_udp_write(lf
, buf
, len
);
100 if (ll
->offset
> 0 && (ll
->offset
+ len
+ 4) > ll
->bufsz
)
101 logfile_mod_udp_flush(lf
);
103 if (len
> ll
->bufsz
) {
104 /* too big to fit in buffer */
105 logfile_mod_udp_write(lf
, buf
, len
);
109 memcpy(ll
->buf
+ ll
->offset
, buf
, len
);
113 assert(ll
->offset
>= 0);
115 assert((size_t) ll
->offset
<= ll
->bufsz
);
119 logfile_mod_udp_linestart(Logfile
* lf
)
124 logfile_mod_udp_lineend(Logfile
* lf
)
129 logfile_mod_udp_rotate(Logfile
* lf
)
135 logfile_mod_udp_close(Logfile
* lf
)
137 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
151 * This code expects the path to be //host:port
154 logfile_mod_udp_open(Logfile
* lf
, const char *path
, size_t bufsz
, int fatal_flag
)
159 lf
->f_close
= logfile_mod_udp_close
;
160 lf
->f_linewrite
= logfile_mod_udp_writeline
;
161 lf
->f_linestart
= logfile_mod_udp_linestart
;
162 lf
->f_lineend
= logfile_mod_udp_lineend
;
163 lf
->f_flush
= logfile_mod_udp_flush
;
164 lf
->f_rotate
= logfile_mod_udp_rotate
;
166 l_udp_t
*ll
= static_cast<l_udp_t
*>(xcalloc(1, sizeof(*ll
)));
169 if (strncmp(path
, "//", 2) == 0) {
172 strAddr
= xstrdup(path
);
173 if (!GetHostWithPort(strAddr
, &addr
)) {
174 if (lf
->flags
.fatal
) {
175 fatalf("Invalid UDP logging address '%s'\n", lf
->path
);
177 debugs(50, DBG_IMPORTANT
, "Invalid UDP logging address '" << lf
->path
<< "'");
184 Ip::Address any_addr
;
185 any_addr
.SetAnyAddr();
187 // require the sending UDP port to be of the right family for the destination address.
191 ll
->fd
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, any_addr
, COMM_NONBLOCKING
, "UDP log socket");
193 if (lf
->flags
.fatal
) {
194 fatalf("Unable to open UDP socket for logging\n");
196 debugs(50, DBG_IMPORTANT
, "Unable to open UDP socket for logging");
199 } else if (!comm_connect_addr(ll
->fd
, &addr
)) {
200 if (lf
->flags
.fatal
) {
201 fatalf("Unable to connect to %s for UDP log: %s\n", lf
->path
, xstrerror());
203 debugs(50, DBG_IMPORTANT
, "Unable to connect to " << lf
->path
<< " for UDP log: " << xstrerror());
208 if (ENOENT
== errno
&& fatal_flag
) {
209 fatalf("Cannot open '%s' because\n"
210 "\tthe parent directory does not exist.\n"
211 "\tPlease create the directory.\n", path
);
212 } else if (EACCES
== errno
&& fatal_flag
) {
213 fatalf("Cannot open '%s' for writing.\n"
214 "\tThe parent directory must be writeable by the\n"
215 "\tuser '%s', which is the cache_effective_user\n"
216 "\tset in squid.conf.", path
, Config
.effectiveUser
);
218 debugs(50, DBG_IMPORTANT
, "logfileOpen (UDP): " << lf
->path
<< ": " << xstrerror());
222 /* Force buffer size to something roughly fitting inside an MTU */
224 * XXX note the receive side needs to receive the whole packet at once;
225 * applications like netcat have a small default receive buffer and will
230 ll
->buf
= static_cast<char*>(xmalloc(bufsz
));