LCOV - code coverage report
Current view: top level - control - tap-ctl-allocate.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 86.2 % 87 75
Test Date: 2026-06-26 00:00:00 Functions: 100.0 % 4 4
Branches: 79.4 % 34 27

             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 <errno.h>
      37                 :             : #include <stdlib.h>
      38                 :             : #include <unistd.h>
      39                 :             : #include <string.h>
      40                 :             : #include <sys/file.h>
      41                 :             : #include <sys/stat.h>
      42                 :             : #include <sys/types.h>
      43                 :             : 
      44                 :             : #include "tap-ctl.h"
      45                 :             : #include "blktap.h"
      46                 :             : 
      47                 :             : static int
      48                 :           9 : tap_ctl_prepare_directory(const char *dir)
      49                 :             : {
      50                 :           9 :         int err;
      51                 :           9 :         char *ptr, *name, *start;
      52                 :             : 
      53                 :           9 :         err = access(dir, W_OK | R_OK);
      54         [ +  - ]:           9 :         if (!err)
      55                 :             :                 return 0;
      56                 :             : 
      57                 :           9 :         name = strdup(dir);
      58         [ -  + ]:           9 :         if (!name)
      59                 :           0 :                 return -errno;
      60                 :             : 
      61                 :             :         start = name;
      62                 :             : 
      63                 :          35 :         for (;;) {
      64                 :          35 :                 ptr = strchr(start + 1, '/');
      65         [ +  + ]:          22 :                 if (ptr)
      66                 :          13 :                         *ptr = '\0';
      67                 :             : 
      68                 :          22 :                 err = mkdir(name, 0700);
      69   [ +  +  +  + ]:          22 :                 if (err && errno != EEXIST) {
      70                 :           2 :                         err = -errno;
      71                 :           2 :                         PERROR("mkdir %s", name);
      72                 :           2 :                         EPRINTF("mkdir failed with %d\n", err);
      73                 :           2 :                         break;
      74                 :             :                 }
      75                 :             : 
      76                 :          20 :                 err = 0;
      77                 :             : 
      78         [ +  + ]:          20 :                 if (!ptr)
      79                 :             :                         break;
      80                 :             :                 else {
      81                 :          13 :                         *ptr = '/';
      82                 :          13 :                         start = ptr + 1;
      83                 :             :                 }
      84                 :             :         }
      85                 :             : 
      86                 :           9 :         free(name);
      87                 :           9 :         return err;
      88                 :             : }
      89                 :             : 
      90                 :             : static int
      91                 :           5 : tap_ctl_check_environment(void)
      92                 :             : {
      93                 :           5 :         int err;
      94                 :             : 
      95                 :           5 :         err = tap_ctl_prepare_directory(BLKTAP2_CONTROL_DIR);
      96         [ +  + ]:           5 :         if (err) {
      97                 :           1 :                 EPRINTF("Prepare %s directory failed %d",
      98                 :             :                                 BLKTAP2_CONTROL_DIR, err);
      99                 :           1 :                 return err;
     100                 :             :         }
     101                 :             : 
     102                 :           4 :         err = tap_ctl_prepare_directory(BLKTAP2_NP_RUN_DIR);
     103         [ +  + ]:           4 :         if (err) {
     104                 :           1 :                 EPRINTF("Prepare %s directory failed %d",
     105                 :             :                                 BLKTAP2_NP_RUN_DIR, err);
     106                 :           1 :                 return err;
     107                 :             :         }
     108                 :             : 
     109                 :             :         return err;
     110                 :             : }
     111                 :             : 
     112                 :             : static int
     113                 :           3 : tap_ctl_allocate_minor(int *minor, char **minor_name)
     114                 :             : {
     115                 :           3 :         char *path = NULL;
     116                 :           3 :         struct stat st_buf;
     117                 :           3 :         int err, id, st, f, fid;
     118                 :             : 
     119                 :           3 :         *minor = -1;
     120                 :             : 
     121                 :           3 :         f = open(BLKTAP2_NP_RUN_DIR, O_RDONLY);
     122         [ -  + ]:           3 :         if (f == -1) {
     123                 :           0 :                 err = -errno;
     124                 :           0 :                 EPRINTF("Failed to open runtime directory %d\n", errno);
     125                 :           0 :                 return err;
     126                 :             :         }
     127                 :             : 
     128                 :             :         /* The only way this can fail is with an EINTR or ENOLCK*/
     129                 :           3 :         err = flock(f, LOCK_EX);
     130         [ -  + ]:           3 :         if (err == -1) {
     131                 :           0 :                 err = -errno;
     132                 :           0 :                 EPRINTF("Failed to lock runtime directory %d\n", errno);
     133                 :           0 :                 goto out;
     134                 :             :         }
     135                 :             : 
     136         [ +  - ]:           4 :         for (id=0; id<MAX_ID; id++) {
     137                 :           4 :                 err = asprintf(&path, "%s/tapdisk-%d", BLKTAP2_NP_RUN_DIR, id);
     138         [ -  + ]:           4 :                 if (err == -1) {
     139                 :           0 :                         err = -errno;
     140                 :           0 :                         goto out;
     141                 :             :                 }
     142                 :             : 
     143                 :           4 :                 st = stat(path, &st_buf);
     144         [ +  + ]:           4 :                 if (st == 0) {
     145                 :             :                         /* Already exists */
     146                 :           1 :                         free(path);
     147                 :           1 :                         path = NULL;
     148                 :           1 :                         continue;
     149                 :             :                 }
     150         [ -  + ]:           3 :                 if (errno != ENOENT) {
     151                 :           0 :                         err = -errno;
     152                 :           0 :                         free(path);
     153                 :           0 :                         goto out;
     154                 :             :                 }
     155                 :             : 
     156                 :           3 :                 fid = open(path, O_CREAT | O_WRONLY, 0600);
     157         [ +  + ]:           3 :                 if (fid == -1) {
     158                 :           1 :                         err = -errno;
     159                 :           1 :                         EPRINTF("Failed to create ID file %s, %d\n", path, errno);
     160                 :           1 :                         free(path);
     161                 :           1 :                         goto out;
     162                 :             :                 }
     163                 :           2 :                 close(fid);
     164                 :             : 
     165                 :           2 :                 *minor = id;
     166                 :           2 :                 *minor_name = path;
     167                 :           2 :                 break;
     168                 :             :         }
     169                 :             : 
     170                 :             :         err = 0;
     171                 :           3 : out:
     172                 :           3 :         flock(f, LOCK_UN);
     173                 :           3 :         close(f);
     174                 :             :         return err;
     175                 :             : }
     176                 :             : 
     177                 :             : int
     178                 :           5 : tap_ctl_allocate(int *minor, char **minor_name)
     179                 :             : {
     180                 :           5 :         int err;
     181                 :             : 
     182                 :           5 :         *minor = -1;
     183                 :             : 
     184                 :           5 :         err = tap_ctl_check_environment();
     185         [ +  + ]:           5 :         if (err) {
     186                 :           2 :                 EPRINTF("tap-ctl allocate failed check environment");
     187                 :           2 :                 return err;
     188                 :             :         }
     189                 :             : 
     190                 :           3 :         err = tap_ctl_allocate_minor(minor, minor_name);
     191         [ +  + ]:           3 :         if (err) {
     192                 :           1 :                 EPRINTF("tap-ctl allocate failed to allocate device");
     193                 :           1 :                 return err;
     194                 :             :         }
     195                 :             : 
     196                 :             :         return 0;
     197                 :             : }
        

Generated by: LCOV version 2.0-1