2 * Copyright (c) 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "syslog-direct.h"
24 #include "openvswitch/dynamic-string.h"
25 #include "socket-util.h"
26 #include "syslog-provider.h"
29 #define FACILITY_MASK 0x03f8
31 static void syslog_direct_open(struct syslogger
*this, int facility
);
32 static void syslog_direct_log(struct syslogger
*this, int pri
,
35 static struct syslog_class syslog_direct_class
= {
40 struct syslog_direct
{
41 struct syslogger parent
;
42 int fd
; /* Negative number in error case. Otherwise, socket. */
47 /* This function creates object that directly interacts with syslog over
48 * UDP or Unix domain socket specified in 'method'. */
50 syslog_direct_create(const char *method
)
52 struct syslog_direct
*this = xmalloc(sizeof *this);
54 this->parent
.class = &syslog_direct_class
;
55 this->parent
.prefix
= "<%B>";
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]);
72 syslog_direct_open(struct syslogger
*this, int facility
)
74 struct syslog_direct
*this_
= (struct syslog_direct
*) this;
76 this_
->facility
= facility
;
80 syslog_direct_log(struct syslogger
*this, int pri
, const char *msg
)
82 static size_t max_len
= SIZE_MAX
; /* max message size we have discovered
83 * to be able to send() without failing
86 struct syslog_direct
*this_
= (struct syslog_direct
*) this;
87 struct ds ds
= DS_EMPTY_INITIALIZER
;
92 /* Failed to open socket for logging. */
96 if (!(pri
& FACILITY_MASK
)) {
97 pri
|= this_
->facility
;
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;