LCOV - code coverage report
Current view: top level - lib - syslog-direct.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 28 0.0 %
Date: 2016-09-14 01:02:56 Functions: 0 3 0.0 %
Branches: 0 14 0.0 %

           Branch data     Line data    Source code
       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 "openvswitch/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                 :          0 : syslog_direct_create(const char *method)
      51                 :            : {
      52                 :          0 :     struct syslog_direct *this = xmalloc(sizeof *this);
      53                 :            : 
      54                 :          0 :     this->parent.class = &syslog_direct_class;
      55                 :          0 :     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         [ #  # ]:          0 :     if (!strncmp(method, "udp:", 4)) {
      61                 :          0 :         inet_open_active(SOCK_DGRAM, &method[4], 514, NULL, &this->fd, 0);
      62         [ #  # ]:          0 :     } else if (!strncmp(method, "unix:", 5)) {
      63                 :          0 :         this->fd = make_unix_socket(SOCK_DGRAM, true, NULL, &method[5]);
      64                 :            :     } else {
      65                 :          0 :         this->fd = -1;
      66                 :            :     }
      67                 :            : 
      68                 :          0 :     return &this->parent;
      69                 :            : }
      70                 :            : 
      71                 :            : static void
      72                 :          0 : syslog_direct_open(struct syslogger *this, int facility)
      73                 :            : {
      74                 :          0 :     struct syslog_direct *this_ = (struct syslog_direct*) this;
      75                 :            : 
      76                 :          0 :     this_->facility = facility;
      77                 :          0 : }
      78                 :            : 
      79                 :            : static void
      80                 :          0 : 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                 :          0 :     struct syslog_direct *this_ = (struct syslog_direct*) this;
      87                 :          0 :     struct ds ds = DS_EMPTY_INITIALIZER;
      88                 :            :     const char *wire_msg;
      89                 :            :     size_t send_len;
      90                 :            : 
      91         [ #  # ]:          0 :     if (this_->fd < 0) {
      92                 :            :         /* Failed to open socket for logging. */
      93                 :          0 :         return;
      94                 :            :     }
      95                 :            : 
      96         [ #  # ]:          0 :     if (!(pri & FACILITY_MASK)) {
      97                 :          0 :         pri |= this_->facility;
      98                 :            :     }
      99                 :          0 :     ds_put_format(&ds, "<%u>%s", pri, msg);
     100                 :          0 :     wire_msg = ds_cstr(&ds);
     101         [ #  # ]:          0 :     send_len = MIN(strlen(wire_msg), max_len);
     102 [ #  # ][ #  # ]:          0 :     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                 :          0 :         send_len -= send_len / 20;
     107                 :          0 :         max_len = send_len;
     108                 :            :     }
     109                 :          0 :     ds_destroy(&ds);
     110                 :            : }

Generated by: LCOV version 1.12