2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 50 Log file handling */
13 #include "comm/Connection.h"
18 #include "log/ModUdp.h"
20 #include "SquidConfig.h"
25 * This logfile UDP module is mostly inspired by a patch by Tim Starling
28 * It doesn't do any UDP buffering - it'd be quite a bit of work for
29 * something which the kernel could be doing for you!
40 logfile_mod_udp_write(Logfile
* lf
, const char *buf
, size_t len
)
42 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
44 s
= write(ll
->fd
, (char const *) buf
, len
);
45 fd_bytes(ll
->fd
, s
, FD_WRITE
);
47 // TODO: Enable after polishing to properly log these errors.
50 debugs(1, DBG_IMPORTANT
, "logfile (udp): got errno (" << errno
<< "):" << xstrerr(xerrno
));
53 debugs(1, DBG_IMPORTANT
, "logfile (udp): len=" << len
<< ", wrote=" << s
);
57 /* We don't worry about network errors for now */
61 logfile_mod_udp_flush(Logfile
* lf
)
63 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
66 logfile_mod_udp_write(lf
, ll
->buf
, (size_t) ll
->offset
);
71 logfile_mod_udp_writeline(Logfile
* lf
, const char *buf
, size_t len
)
73 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
76 /* buffering disabled */
77 logfile_mod_udp_write(lf
, buf
, len
);
80 if (ll
->offset
> 0 && (ll
->offset
+ len
+ 4) > ll
->bufsz
)
81 logfile_mod_udp_flush(lf
);
83 if (len
> ll
->bufsz
) {
84 /* too big to fit in buffer */
85 logfile_mod_udp_write(lf
, buf
, len
);
89 memcpy(ll
->buf
+ ll
->offset
, buf
, len
);
93 assert(ll
->offset
>= 0);
95 assert((size_t) ll
->offset
<= ll
->bufsz
);
99 logfile_mod_udp_linestart(Logfile
*)
104 logfile_mod_udp_lineend(Logfile
*)
109 logfile_mod_udp_rotate(Logfile
*, const int16_t)
114 logfile_mod_udp_close(Logfile
* lf
)
116 l_udp_t
*ll
= (l_udp_t
*) lf
->data
;
130 * This code expects the path to be //host:port
133 logfile_mod_udp_open(Logfile
* lf
, const char *path
, size_t bufsz
, int fatal_flag
)
138 lf
->f_close
= logfile_mod_udp_close
;
139 lf
->f_linewrite
= logfile_mod_udp_writeline
;
140 lf
->f_linestart
= logfile_mod_udp_linestart
;
141 lf
->f_lineend
= logfile_mod_udp_lineend
;
142 lf
->f_flush
= logfile_mod_udp_flush
;
143 lf
->f_rotate
= logfile_mod_udp_rotate
;
145 l_udp_t
*ll
= static_cast<l_udp_t
*>(xcalloc(1, sizeof(*ll
)));
148 if (strncmp(path
, "//", 2) == 0) {
151 strAddr
= xstrdup(path
);
152 if (!GetHostWithPort(strAddr
, &addr
)) {
153 if (lf
->flags
.fatal
) {
154 fatalf("Invalid UDP logging address '%s'\n", lf
->path
);
156 debugs(50, DBG_IMPORTANT
, "ERROR: Invalid UDP logging address '" << lf
->path
<< "'");
163 Ip::Address any_addr
;
164 any_addr
.setAnyAddr();
166 // require the sending UDP port to be of the right family for the destination address.
170 ll
->fd
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, any_addr
, COMM_NONBLOCKING
, "UDP log socket");
173 if (lf
->flags
.fatal
) {
174 fatalf("Unable to open UDP socket for logging\n");
176 debugs(50, DBG_IMPORTANT
, "ERROR: Unable to open UDP socket for logging");
179 } else if (!comm_connect_addr(ll
->fd
, addr
)) {
181 if (lf
->flags
.fatal
) {
182 fatalf("Unable to connect to %s for UDP log: %s\n", lf
->path
, xstrerr(xerrno
));
184 debugs(50, DBG_IMPORTANT
, "ERROR: Unable to connect to " << lf
->path
<< " for UDP log: " << xstrerr(xerrno
));
189 if (ENOENT
== xerrno
&& fatal_flag
) {
190 fatalf("Cannot open '%s' because\n"
191 "\tthe parent directory does not exist.\n"
192 "\tPlease create the directory.\n", path
);
193 } else if (EACCES
== xerrno
&& fatal_flag
) {
194 fatalf("Cannot open '%s' for writing.\n"
195 "\tThe parent directory must be writeable by the\n"
196 "\tuser '%s', which is the cache_effective_user\n"
197 "\tset in squid.conf.", path
, Config
.effectiveUser
);
199 debugs(50, DBG_IMPORTANT
, "logfileOpen (UDP): " << lf
->path
<< ": " << xstrerr(xerrno
));
203 /* Force buffer size to something roughly fitting inside an MTU */
205 * XXX note the receive side needs to receive the whole packet at once;
206 * applications like netcat have a small default receive buffer and will
212 ll
->buf
= static_cast<char*>(xmalloc(bufsz
));