LCOV - code coverage report
Current view: top level - control - tap-ctl-stats.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 64 0.0 %
Date: 2025-02-07 10:27:58 Functions: 0 3 0.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2016, Citrix Systems, Inc.
       3                 :            :  *
       4                 :            :  * All rights reserved.
       5                 :            :  *
       6                 :            :  * Redistribution and use in source and binary forms, with or without
       7                 :            :  * modification, are permitted provided that the following conditions are met:
       8                 :            :  * 
       9                 :            :  *  1. Redistributions of source code must retain the above copyright
      10                 :            :  *     notice, this list of conditions and the following disclaimer.
      11                 :            :  *  2. Redistributions in binary form must reproduce the above copyright
      12                 :            :  *     notice, this list of conditions and the following disclaimer in the
      13                 :            :  *     documentation and/or other materials provided with the distribution.
      14                 :            :  *  3. Neither the name of the copyright holder nor the names of its 
      15                 :            :  *     contributors may be used to endorse or promote products derived from 
      16                 :            :  *     this software without specific prior written permission.
      17                 :            :  *
      18                 :            :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      19                 :            :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      20                 :            :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      21                 :            :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
      22                 :            :  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      23                 :            :  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      24                 :            :  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
      25                 :            :  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
      26                 :            :  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
      27                 :            :  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      28                 :            :  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29                 :            :  */
      30                 :            : 
      31                 :            : #ifdef HAVE_CONFIG_H
      32                 :            : #include "config.h"
      33                 :            : #endif
      34                 :            : 
      35                 :            : #include <stdio.h>
      36                 :            : #include <string.h>
      37                 :            : #include <unistd.h>
      38                 :            : #include <sys/mman.h>
      39                 :            : 
      40                 :            : #include "tap-ctl.h"
      41                 :            : 
      42                 :            : int
      43                 :          0 : _tap_ctl_stats_connect_and_send(pid_t pid, int minor)
      44                 :            : {
      45                 :          0 :         struct timeval timeout = { .tv_sec = 10, .tv_usec = 0 };
      46                 :            :         tapdisk_message_t message;
      47                 :            :         int sfd, err;
      48                 :            : 
      49                 :          0 :         err = tap_ctl_connect_id(pid, &sfd);
      50                 :          0 :         if (err)
      51                 :          0 :                 return err;
      52                 :            : 
      53                 :            :         memset(&message, 0, sizeof(message));
      54                 :          0 :         message.type   = TAPDISK_MESSAGE_STATS;
      55                 :          0 :         message.cookie = minor;
      56                 :            : 
      57                 :          0 :         err = tap_ctl_write_message(sfd, &message, &timeout);
      58                 :          0 :         if (err) {
      59                 :          0 :                 close(sfd);
      60                 :            :                 return err;
      61                 :            :         }
      62                 :            : 
      63                 :          0 :         return sfd;
      64                 :            : }
      65                 :            : 
      66                 :            : ssize_t
      67                 :          0 : tap_ctl_stats(pid_t pid, int minor, char *buf, size_t size)
      68                 :            : {
      69                 :            :         tapdisk_message_t message;
      70                 :            :         int sfd, err;
      71                 :            :         size_t len;
      72                 :            : 
      73                 :          0 :         sfd = _tap_ctl_stats_connect_and_send(pid, minor);
      74                 :          0 :         if (sfd < 0)
      75                 :          0 :                 return sfd;
      76                 :            : 
      77                 :          0 :         err = tap_ctl_read_message(sfd, &message, NULL);
      78                 :          0 :         if (err)
      79                 :            :                 goto out;
      80                 :            : 
      81                 :          0 :         len = message.u.info.length;
      82                 :          0 :         if (size < len + 1)
      83                 :          0 :                 len = size - 1;
      84                 :            : 
      85                 :          0 :         err = tap_ctl_read_raw(sfd, buf, len, NULL);
      86                 :          0 :         if (err)
      87                 :            :                 goto out;
      88                 :            : 
      89                 :          0 :         buf[len] = 0;
      90                 :            : 
      91                 :            : out:
      92                 :          0 :         close(sfd);
      93                 :          0 :         return err;
      94                 :            : }
      95                 :            : 
      96                 :            : int
      97                 :          0 : tap_ctl_stats_fwrite(pid_t pid, int minor, FILE *stream)
      98                 :            : {
      99                 :            :         tapdisk_message_t message;
     100                 :          0 :         int sfd = -1, prot, flags, err;
     101                 :            :         size_t len, bufsz;
     102                 :          0 :         char *buf = MAP_FAILED;
     103                 :            : 
     104                 :          0 :         prot  = PROT_READ|PROT_WRITE;
     105                 :          0 :         flags = MAP_ANONYMOUS|MAP_PRIVATE;
     106                 :            : 
     107                 :          0 :         err = sysconf(_SC_PAGE_SIZE);
     108                 :          0 :         if (err == -1) {
     109                 :          0 :                 err = -errno;
     110                 :          0 :                 goto out;
     111                 :            :         }
     112                 :            : 
     113                 :          0 :         bufsz = err;
     114                 :            : 
     115                 :          0 :         buf = mmap(NULL, bufsz, prot, flags, -1, 0);
     116                 :          0 :         if (buf == MAP_FAILED) {
     117                 :            :                 err = -ENOMEM;
     118                 :            :                 goto out;
     119                 :            :         }
     120                 :            : 
     121                 :          0 :         sfd = _tap_ctl_stats_connect_and_send(pid, minor);
     122                 :          0 :         if (sfd < 0) {
     123                 :            :                 err = sfd;
     124                 :            :                 goto out;
     125                 :            :         }
     126                 :            : 
     127                 :          0 :         err = tap_ctl_read_message(sfd, &message, NULL);
     128                 :          0 :         if (err)
     129                 :            :                 goto out;
     130                 :            : 
     131                 :          0 :         len = message.u.info.length;
     132                 :          0 :         while (len) {
     133                 :            :                 fd_set rfds;
     134                 :            :                 size_t in, out;
     135                 :            :                 int n;
     136                 :            : 
     137                 :          0 :                 FD_ZERO(&rfds);
     138                 :          0 :                 FD_SET(sfd, &rfds);
     139                 :            : 
     140                 :          0 :                 n = select(sfd + 1, &rfds, NULL, NULL, NULL);
     141                 :          0 :                 if (n < 0) {
     142                 :            :                         err = n;
     143                 :          0 :                         goto out;
     144                 :            :                 }
     145                 :            : 
     146                 :          0 :                 in = read(sfd, buf, bufsz);
     147                 :          0 :                 if (in <= 0) {
     148                 :          0 :                         err = in;
     149                 :          0 :                         goto out;
     150                 :            :                 }
     151                 :            : 
     152                 :          0 :                 len -= in;
     153                 :            : 
     154                 :          0 :                 out = fwrite(buf, in, 1, stream);
     155                 :          0 :                 if (out != 1) {
     156                 :          0 :                         err = -errno;
     157                 :          0 :                         goto out;
     158                 :            :                 }
     159                 :            :         }
     160                 :            : 
     161                 :          0 :         if (fwrite("\n", 1, 1, stream) != 1) {
     162                 :          0 :                 err = -EIO;
     163                 :            :         }
     164                 :            : 
     165                 :            : out:
     166                 :          0 :         if (sfd >= 0)
     167                 :          0 :                 close(sfd);
     168                 :          0 :         if (buf != MAP_FAILED)
     169                 :          0 :                 munmap(buf, bufsz);
     170                 :            : 
     171                 :          0 :         return err;
     172                 :            : }

Generated by: LCOV version 1.13