LogSyslogFast.c

filter: Statementstmt 83.0% 166 / 200 Branchbran 67.3% 62 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.0% 228 / 292 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« const-xs.inc ↑ Summary Fast.xs »
1 #include "LogSyslogFast.h"
2  
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <netdb.h>
6 #include <netinet/in.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include <sys/un.h>
13 #include <unistd.h>
14  
15 #define INITIAL_BUFSIZE 2048
16  
17 static
18 void
19 204 update_prefix(LogSyslogFast* logger, time_t t)
20 {
21 204 ▶ if (!logger->sender || !logger->name)
Branch: 2 branches
BranchTrueFalseTotal
if (!logger->sender || !logger->name) 166 38 204
if (!logger->sender || !logger->name) 38 128 166
22 76 return; /* still initializing */
23  
24 128 logger->last_time = t;
25  
26 /* LOG_RFC3164 time string tops out at 15 chars, LOG_RFC5424 at 25 */
27 char timestr[26];
28 128 strftime(timestr, 26, logger->time_format, localtime(&t));
29  
30 /* %z in strftime returns 4DIGIT, but we need 2DIGIT ":" 2DIGIT */
31 128 ▶ if (logger->format == LOG_RFC5424) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->format == LOG_RFC5424) { 4 124 128
32 4 timestr[25] = 0;
33 4 timestr[24] = timestr[23];
34 4 timestr[23] = timestr[22];
35 4 timestr[22] = ':';
36 }
37  
38 128 ▶ if (logger->format == LOG_RFC3164 || logger->format == LOG_RFC5424) {
Branch: 2 branches
BranchTrueFalseTotal
if (logger->format == LOG_RFC3164 || logger->format == LOG_RFC5424) { 12 116 128
if (logger->format == LOG_RFC3164 || logger->format == LOG_RFC5424) { 4 8 12
39 120 logger->prefix_len = snprintf(
40 120 logger->linebuf, logger->bufsize, logger->msg_format,
41 logger->priority, timestr, logger->sender, logger->name, logger->pid
42 );
43 8 ▶ } else if (logger->format == LOG_RFC3164_LOCAL) { /* without sender */
Branch: 1 branch
BranchTrueFalseTotal
} else if (logger->format == LOG_RFC3164_LOCAL) { /* without sender */ 8 0 8
44 8 logger->prefix_len = snprintf(
45 8 logger->linebuf, logger->bufsize, logger->msg_format,
46 logger->priority, timestr, logger->name, logger->pid
47 );
48 }
49  
50 128 ▶ if (logger->prefix_len > logger->bufsize - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (logger->prefix_len > logger->bufsize - 1) 0 128 128
51 0 logger->prefix_len = logger->bufsize - 1;
52  
53 /* cache the location in linebuf where msg should be pasted in */
54 128 logger->msg_start = logger->linebuf + logger->prefix_len;
55 }
56  
57 LogSyslogFast*
58 38 LSF_alloc()
59 {
60 38 return malloc(sizeof(LogSyslogFast));
61 }
62  
63 int
64 38 LSF_init(
65 LogSyslogFast* logger, int proto, const char* hostname, int port,
66 int facility, int severity, const char* sender, const char* name)
67 {
68 38 ▶ if (!logger)
Branch: 1 branch
BranchTrueFalseTotal
if (!logger) 0 38 38
69 0 return -1;
70  
71 38 logger->sock = -1;
72  
73 38 logger->pid = getpid();
74  
75 38 logger->linebuf = malloc(logger->bufsize = INITIAL_BUFSIZE);
76 38 ▶ if (!logger->linebuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!logger->linebuf) { 0 38 38
77 0 logger->err = strerror(errno);
78 0 return -1;
79 }
80  
81 38 logger->sender = NULL;
82 38 logger->name = NULL;
83 38 LSF_set_format(logger, LOG_RFC3164);
84 38 LSF_set_sender(logger, sender);
85 38 LSF_set_name(logger, name);
86  
87 38 logger->priority = (facility << 3) | severity;
88 38 update_prefix(logger, time(0));
89  
90 38 return LSF_set_receiver(logger, proto, hostname, port);
91 }
92  
93 int
94 28 LSF_destroy(LogSyslogFast* logger)
95 {
96 28 int ret = 0;
97 28 ▶ if (logger->sock >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->sock >= 0) { 28 0 28
98 28 ret = close(logger->sock);
99 28 ▶ if (ret)
Branch: 1 branch
BranchTrueFalseTotal
if (ret) 0 28 28
100 0 logger->err = strerror(errno);
101 }
102 28 free(logger->sender);
103 28 free(logger->name);
104 28 free(logger->linebuf);
105 28 free(logger);
106 28 return ret;
107 }
108  
109 void
110 11 LSF_set_priority(LogSyslogFast* logger, int facility, int severity)
111 {
112 11 logger->priority = (facility << 3) | severity;
113 11 update_prefix(logger, time(0));
114 11 }
115  
116 void
117 1 LSF_set_facility(LogSyslogFast* logger, int facility)
118 {
119 1 LSF_set_priority(logger, facility, LSF_get_severity(logger));
120 1 }
121  
122 void
123 1 LSF_set_severity(LogSyslogFast* logger, int severity)
124 {
125 1 LSF_set_priority(logger, LSF_get_facility(logger), severity);
126 1 }
127  
128 int
129 47 LSF_set_sender(LogSyslogFast* logger, const char* sender)
130 {
131 47 free(logger->sender);
132 47 logger->sender = strdup(sender);
133 47 ▶ if (!logger->sender) {
Branch: 1 branch
BranchTrueFalseTotal
if (!logger->sender) { 0 47 47
134 0 logger->err = "strdup failure in set_sender";
135 0 return -1;
136 }
137 47 update_prefix(logger, time(0));
138 47 return 0;
139 }
140  
141 int
142 47 LSF_set_name(LogSyslogFast* logger, const char* name)
143 {
144 47 free(logger->name);
145 47 logger->name = strdup(name);
146 47 ▶ if (!logger->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (!logger->name) { 0 47 47
147 0 logger->err = "strdup failure in set_name";
148 0 return -1;
149 }
150 47 update_prefix(logger, time(0));
151 47 return 0;
152 }
153  
154 void
155 9 LSF_set_pid(LogSyslogFast* logger, int pid)
156 {
157 9 logger->pid = pid;
158 9 update_prefix(logger, time(0));
159 9 }
160  
161 int
162 52 LSF_set_format(LogSyslogFast* logger, int format)
163 {
164 52 logger->format = format;
165  
166 /* msg_format must contain format strings for:
167 1) priority (int)
168 2) timestamp (string)
169 3) hostname (string)
170 4) app name (string)
171 5) pid (int)
172 */
173 52 ▶ if (logger->format == LOG_RFC3164) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->format == LOG_RFC3164) { 40 12 52
174 /*
175 'The TIMESTAMP field is the local time and is in the format of
176 "Mmm dd hh:mm:ss"'
177  
178 Example: "Jan 4 11:22:33"
179 */
180 40 logger->time_format = "%h %e %H:%M:%S";
181 40 logger->msg_format = "<%d>%s %s %s[%d]: ";
182 }
183 12 ▶ else if (logger->format == LOG_RFC5424) {
Branch: 1 branch
BranchTrueFalseTotal
else if (logger->format == LOG_RFC5424) { 4 8 12
184 /*
185 TIMESTAMP = NILVALUE / FULL-DATE "T" FULL-TIME
186 FULL-DATE = DATE-FULLYEAR "-" DATE-MONTH "-" DATE-MDAY
187 DATE-FULLYEAR = 4DIGIT
188 DATE-MONTH = 2DIGIT ; 01-12
189 DATE-MDAY = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on
190 ; month/year
191 FULL-TIME = PARTIAL-TIME TIME-OFFSET
192 PARTIAL-TIME = TIME-HOUR ":" TIME-MINUTE ":" TIME-SECOND
193 [TIME-SECFRAC]
194 TIME-HOUR = 2DIGIT ; 00-23
195 TIME-MINUTE = 2DIGIT ; 00-59
196 TIME-SECOND = 2DIGIT ; 00-59
197 TIME-SECFRAC = "." 1*6DIGIT
198 TIME-OFFSET = "Z" / TIME-NUMOFFSET
199 TIME-NUMOFFSET = ("+" / "-") TIME-HOUR ":" TIME-MINUTE
200  
201 Example: "2012-01-04T11:22:33-0800"
202 */
203 4 logger->time_format = "%Y-%m-%dT%H:%M:%S%z";
204  
205 /* STRUCTURED-DATA and MSGID fields are omitted */
206 4 logger->msg_format = "<%d>1 %s %s %s %d - - ";
207 }
208 8 ▶ else if (logger->format == LOG_RFC3164_LOCAL) {
Branch: 1 branch
BranchTrueFalseTotal
else if (logger->format == LOG_RFC3164_LOCAL) { 8 0 8
209 /* Same as LOG_RFC3164 but without HOSTNAME */
210 8 logger->time_format = "%h %e %H:%M:%S";
211 8 logger->msg_format = "<%d>%s %s[%d]: ";
212 }
213 else {
214 0 logger->err = "invalid format constant";
215 0 return -1;
216 }
217 52 update_prefix(logger, time(0));
218 52 return 0;
219 }
220  
221 #ifdef AF_INET6
222 #define clean_return(x) if (results) freeaddrinfo(results); return x;
223 #else
224 #define clean_return(x) return x;
225 #endif
226  
227 /* must match constants in LogSyslogFast.pm */
228 #define LOG_UDP 0
229 #define LOG_TCP 1
230 #define LOG_UNIX 2
231  
232 int
233 146 LSF_set_receiver(LogSyslogFast* logger, int proto, const char* hostname, int port)
234 {
235 const struct sockaddr* p_address;
236 int address_len;
237 #ifdef AF_INET6
238 146 struct addrinfo* results = NULL;
239 #endif
240  
241 146 ▶ if (logger->sock >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->sock >= 0) { 108 38 146
242 108 int ret = close(logger->sock);
243 108 ▶ if (ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret) { 0 108 108
244 0 logger->err = strerror(errno);
245 0 return -1;
246 }
247 108 logger->sock = -1;
248 }
249  
250 /* set up a socket, letting kernel assign local port */
251 269 ▶ if (proto == LOG_UDP || proto == LOG_TCP) {
Branch: 2 branches
BranchTrueFalseTotal
if (proto == LOG_UDP || proto == LOG_TCP) { 130 16 146
if (proto == LOG_UDP || proto == LOG_TCP) { 110 20 130
252  
253 #ifdef AF_INET6
254  
255 /* For NetBSD: http://www.mail-archive.com/bug-gnulib@gnu.org/msg17067.html */
256 #ifndef AI_ADDRCONFIG
257 #define AI_ADDRCONFIG 0
258 #endif
259  
260 /* For MacOS: http://mailman.videolan.org/pipermail/vlc-devel/2008-May/044005.html */
261 #ifndef AI_NUMERICSERV
262 #define AI_NUMERICSERV 0
263 #endif
264  
265 struct addrinfo *rp;
266 struct addrinfo hints;
267 char portstr[32];
268 int r;
269  
270 126 snprintf(portstr, sizeof(portstr), "%d", port);
271 126 memset(&hints, 0, sizeof(hints));
272 126 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
273 126 hints.ai_family = AF_UNSPEC;
274 126 ▶ if (proto == LOG_TCP) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto == LOG_TCP) { 110 16 126
275 110 hints.ai_socktype = SOCK_STREAM;
276 } else {
277 16 hints.ai_socktype = SOCK_DGRAM;
278 }
279 126 hints.ai_protocol = 0;
280 126 hints.ai_addrlen = 0;
281 126 hints.ai_addr = NULL;
282 126 hints.ai_canonname = NULL;
283 126 hints.ai_next = NULL;
284  
285 126 r = getaddrinfo(hostname, portstr, &hints, &results);
286 126 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 3 123 126
287 3 logger->err = gai_strerror(r);
288 3 return -1;
289 }
290 123 ▶ else if (!results) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!results) { 0 123 123
291 0 logger->err = "no results from getaddrinfo";
292 0 return -1;
293 }
294 123 ▶ for (rp = results; rp != NULL; rp = rp->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (rp = results; rp != NULL; rp = rp->ai_next) { 123 0 123
295 123 logger->sock = socket(rp->ai_family, rp->ai_socktype, 0);
296 123 ▶ if (logger->sock == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->sock == -1) { 0 123 123
297 0 r = errno;
298 0 continue;
299 }
300 123 p_address = rp->ai_addr;
301 123 address_len = rp->ai_addrlen;
302 123 break;
303 }
304 123 ▶ if (logger->sock == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->sock == -1) { 0 123 123
305 0 logger->err = "socket failure";
306 0 ▶ clean_return(-1);
Branch: 1 branch
BranchTrueFalseTotal
clean_return(-1); 0 0 0
307 }
308  
309 #else /* !AF_INET6 */
310  
311 /* resolve the remote host */
312 struct hostent* host = gethostbyname(hostname);
313 if (!host || !host->h_addr_list || !host->h_addr_list[0]) {
314 logger->err = "resolve failure";
315 return -1;
316 }
317  
318 /* create the remote host's address */
319 struct sockaddr_in raddress;
320 raddress.sin_family = AF_INET;
321 memcpy(&raddress.sin_addr, host->h_addr_list[0], sizeof(raddress.sin_addr));
322 raddress.sin_port = htons(port);
323 p_address = (const struct sockaddr*) &raddress;
324 address_len = sizeof(raddress);
325  
326 /* construct socket */
327 if (proto == LOG_UDP) {
328 logger->sock = socket(AF_INET, SOCK_DGRAM, 0);
329  
330 /* make the socket non-blocking */
331 int flags = fcntl(logger->sock, F_GETFL, 0);
332 fcntl(logger->sock, F_SETFL, flags | O_NONBLOCK);
333 flags = fcntl(logger->sock, F_GETFL, 0);
334 if (!(flags & O_NONBLOCK)) {
335 logger->err = "nonblock failure";
336 return -1;
337 }
338 }
339 else if (proto == LOG_TCP) {
340 logger->sock = socket(AF_INET, SOCK_STREAM, 0);
341 }
342  
343 #endif /* AF_INET6 */
344 }
345 20 ▶ else if (proto == LOG_UNIX) {
Branch: 1 branch
BranchTrueFalseTotal
else if (proto == LOG_UNIX) { 20 0 20
346  
347 /* create the log device's address */
348 struct sockaddr_un raddress;
349 20 raddress.sun_family = AF_UNIX;
350 20 strncpy(raddress.sun_path, hostname, sizeof(raddress.sun_path) - 1);
351 20 p_address = (const struct sockaddr*) &raddress;
352 20 address_len = sizeof(raddress);
353  
354 /* construct socket */
355 20 logger->sock = socket(AF_UNIX, SOCK_STREAM, 0);
356 }
357 else {
358 0 logger->err = "bad protocol";
359 0 return -1;
360 }
361  
362 143 ▶ if (logger->sock < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->sock < 0) { 0 143 143
363 0 logger->err = strerror(errno);
364 0 ▶ clean_return(-1);
Branch: 1 branch
BranchTrueFalseTotal
clean_return(-1); 0 0 0
365 }
366  
367 /* close the socket after exec to match normal Perl behavior for sockets */
368 143 fcntl(logger->sock, F_SETFD, FD_CLOEXEC);
369  
370 /* connect the socket */
371 143 ▶ if (connect(logger->sock, p_address, address_len) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect(logger->sock, p_address, address_len) != 0) { 14 129 143
372 /* some servers (rsyslog) may use SOCK_DGRAM for unix domain sockets */
373 14 ▶ if (proto == LOG_UNIX && errno == EPROTOTYPE) {
Branch: 2 branches
BranchTrueFalseTotal
if (proto == LOG_UNIX && errno == EPROTOTYPE) { 13 1 14
if (proto == LOG_UNIX && errno == EPROTOTYPE) { 7 6 13
374 /* clean up existing bad socket */
375 7 close(logger->sock);
376 7 ▶ if (logger->sock < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->sock < 0) { 0 7 7
377 0 logger->err = strerror(errno);
378 0 ▶ clean_return(-1);
Branch: 1 branch
BranchTrueFalseTotal
clean_return(-1); 0 0 0
379 }
380  
381 7 logger->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
382 7 ▶ if (connect(logger->sock, p_address, address_len) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect(logger->sock, p_address, address_len) != 0) { 0 7 7
383 0 logger->err = strerror(errno);
384 0 ▶ clean_return(-1);
Branch: 1 branch
BranchTrueFalseTotal
clean_return(-1); 0 0 0
385 }
386 }
387 else {
388 7 ▶ if (logger->sock >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->sock >= 0) { 7 0 7
389 7 close(logger->sock);
390 7 logger->sock = -1;
391 }
392 7 logger->err = strerror(errno);
393 7 ▶ clean_return(-1);
Branch: 1 branch
BranchTrueFalseTotal
clean_return(-1); 1 6 7
394 }
395 }
396  
397 136 ▶ clean_return(0);
Branch: 1 branch
BranchTrueFalseTotal
clean_return(0); 122 14 136
398 }
399  
400 int
401 35 LSF_send(LogSyslogFast* logger, const char* msg_str, int msg_len, time_t t)
402 {
403 /* update the prefix if seconds have rolled over */
404 35 ▶ if (t != logger->last_time)
Branch: 1 branch
BranchTrueFalseTotal
if (t != logger->last_time) 0 35 35
405 0 update_prefix(logger, t);
406  
407 35 int line_len = logger->prefix_len + msg_len;
408 /* ensure there's space in the buffer for total length including a trailing NULL */
409 35 ▶ if (logger->bufsize < line_len + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->bufsize < line_len + 1) { 1 34 35
410 /* try to increase buffer */
411 1 int new_bufsize = 2 * logger->bufsize;
412 2 ▶ while (new_bufsize < line_len + 1)
Branch: 1 branch
BranchTrueFalseTotal
while (new_bufsize < line_len + 1) 1 1 2
413 1 new_bufsize *= 2;
414 1 ▶ if (new_bufsize < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_bufsize < 0) { 0 1 1
415 /* overflow */
416 0 logger->err = "message too large";
417 0 return -1;
418 }
419  
420 1 char* new_buf = realloc(logger->linebuf, new_bufsize);
421 1 ▶ if (!new_buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_buf) { 0 1 1
422 0 logger->err = strerror(errno);
423 0 return -1;
424 }
425  
426 1 logger->linebuf = new_buf;
427 1 logger->bufsize = new_bufsize;
428 1 logger->msg_start = logger->linebuf + logger->prefix_len;
429 }
430  
431 /* paste the message into linebuf just past where the prefix was placed */
432 35 memcpy(logger->msg_start, msg_str, msg_len + 1); /* include perl-added null */
433  
434 35 int ret = send(logger->sock, logger->linebuf, line_len, 0);
435  
436 35 ▶ if (ret < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret < 0) 5 30 35
437 5 logger->err = strerror(errno);
438 35 return ret;
439 }
440  
441 int
442 3 LSF_get_priority(LogSyslogFast* logger)
443 {
444 3 return logger->priority;
445 }
446  
447 int
448 3 LSF_get_facility(LogSyslogFast* logger)
449 {
450 3 return logger->priority >> 3;
451 }
452  
453 int
454 3 LSF_get_severity(LogSyslogFast* logger)
455 {
456 3 return logger->priority & 7;
457 }
458  
459 const char*
460 2 LSF_get_sender(LogSyslogFast* logger)
461 {
462 2 return logger->sender;
463 }
464  
465 const char*
466 2 LSF_get_name(LogSyslogFast* logger)
467 {
468 2 return logger->name;
469 }
470  
471 int
472 2 LSF_get_pid(LogSyslogFast* logger)
473 {
474 2 return logger->pid;
475 }
476  
477 int
478 2 LSF_get_sock(LogSyslogFast* logger)
479 {
480 2 return logger->sock;
481 }
482  
483 int
484 0 LSF_get_format(LogSyslogFast* logger)
485 {
486 0 return logger->format;
487 }
« const-xs.inc ↑ Summary Fast.xs »