netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / syslog-direct.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "syslog-direct.h"
17
18 #include <config.h>
19
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "compiler.h"
24 #include "dynamic-string.h"
25 #include "socket-util.h"
26 #include "syslog-provider.h"
27 #include "util.h"
28
29 #define FACILITY_MASK 0x03f8
30
31 static void syslog_direct_open(struct syslogger *this, int facility);
32 static void syslog_direct_log(struct syslogger *this, int pri,
33                               const char *msg);
34
35 static struct syslog_class syslog_direct_class = {
36     syslog_direct_open,
37     syslog_direct_log,
38 };
39
40 struct syslog_direct {
41     struct syslogger parent;
42     int fd;  /* Negative number in error case.  Otherwise, socket. */
43     int facility;
44 };
45
46
47 /* This function creates object that directly interacts with syslog over
48  * UDP or Unix domain socket specified in 'method'. */
49 struct syslogger *
50 syslog_direct_create(const char *method)
51 {
52     struct syslog_direct *this = xmalloc(sizeof *this);
53
54     this->parent.class = &syslog_direct_class;
55     this->parent.prefix = "<%B>";
56
57     /* socket is created from here (opposed to syslog_direct_open())
58      * so that deadlocks would be avoided.  The problem is that these
59      * functions that create socket might call VLOG() */
60     if (!strncmp(method, "udp:", 4)) {
61         inet_open_active(SOCK_DGRAM, &method[4], 514, NULL, &this->fd, 0);
62     } else if (!strncmp(method, "unix:", 5)) {
63         this->fd = make_unix_socket(SOCK_DGRAM, true, NULL, &method[5]);
64     } else {
65         this->fd = -1;
66     }
67
68     return &this->parent;
69 }
70
71 static void
72 syslog_direct_open(struct syslogger *this, int facility)
73 {
74     struct syslog_direct *this_ = (struct syslog_direct*) this;
75
76     this_->facility = facility;
77 }
78
79 static void
80 syslog_direct_log(struct syslogger *this, int pri, const char *msg)
81 {
82     static size_t max_len = SIZE_MAX; /* max message size we have discovered
83                                        * to be able to send() without failing
84                                        * with EMSGSIZE. */
85
86     struct syslog_direct *this_ = (struct syslog_direct*) this;
87     struct ds ds = DS_EMPTY_INITIALIZER;
88     const char *wire_msg;
89     size_t send_len;
90
91     if (this_->fd < 0) {
92         /* Failed to open socket for logging. */
93         return;
94     }
95
96     if (!(pri & FACILITY_MASK)) {
97         pri |= this_->facility;
98     }
99     ds_put_format(&ds, "<%u>%s", pri, msg);
100     wire_msg = ds_cstr(&ds);
101     send_len = MIN(strlen(wire_msg), max_len);
102     while (send(this_->fd, wire_msg, send_len, 0) < 0 && errno == EMSGSIZE) {
103         /* If message was too large for send() function then try to discover
104          * max_len supported for this particular socket and retry sending a
105          * truncated version of the same message. */
106         send_len -= send_len / 20;
107         max_len = send_len;
108     }
109     ds_destroy(&ds);
110 }