LCOV - code coverage report
Current view: top level - control - tap-ctl-allocate.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 73 0.0 %
Date: 2025-02-07 10:09:38 Functions: 0 4 0.0 %
Branches: 0 34 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 <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                 :          0 : tap_ctl_prepare_directory(const char *dir)
      49                 :            : {
      50                 :            :         int err;
      51                 :            :         char *ptr, *name, *start;
      52                 :            : 
      53                 :          0 :         err = access(dir, W_OK | R_OK);
      54         [ #  # ]:          0 :         if (!err)
      55                 :            :                 return 0;
      56                 :            : 
      57                 :          0 :         name = strdup(dir);
      58         [ #  # ]:          0 :         if (!name)
      59                 :          0 :                 return -errno;
      60                 :            : 
      61                 :            :         start = name;
      62                 :            : 
      63                 :            :         for (;;) {
      64                 :          0 :                 ptr = strchr(start + 1, '/');
      65         [ #  # ]:          0 :                 if (ptr)
      66                 :          0 :                         *ptr = '\0';
      67                 :            : 
      68                 :          0 :                 err = mkdir(name, 0700);
      69 [ #  # ][ #  # ]:          0 :                 if (err && errno != EEXIST) {
      70                 :          0 :                         err = -errno;
      71                 :          0 :                         PERROR("mkdir %s", name);
      72                 :            :                         EPRINTF("mkdir failed with %d\n", err);
      73                 :            :                         break;
      74                 :            :                 }
      75                 :            : 
      76                 :          0 :                 err = 0;
      77                 :            : 
      78         [ #  # ]:          0 :                 if (!ptr)
      79                 :            :                         break;
      80                 :            :                 else {
      81                 :          0 :                         *ptr = '/';
      82                 :          0 :                         start = ptr + 1;
      83                 :            :                 }
      84                 :          0 :         }
      85                 :            : 
      86                 :          0 :         free(name);
      87                 :          0 :         return err;
      88                 :            : }
      89                 :            : 
      90                 :            : static int
      91                 :          0 : tap_ctl_check_environment(void)
      92                 :            : {
      93                 :            :         int err;
      94                 :            : 
      95                 :          0 :         err = tap_ctl_prepare_directory(BLKTAP2_CONTROL_DIR);
      96         [ #  # ]:          0 :         if (err) {
      97                 :            :                 EPRINTF("Prepare %s directory failed %d",
      98                 :            :                                 BLKTAP2_CONTROL_DIR, err);
      99                 :          0 :                 return err;
     100                 :            :         }
     101                 :            : 
     102                 :          0 :         err = tap_ctl_prepare_directory(BLKTAP2_NP_RUN_DIR);
     103         [ #  # ]:          0 :         if (err) {
     104                 :            :                 EPRINTF("Prepare %s directory failed %d",
     105                 :            :                                 BLKTAP2_NP_RUN_DIR, err);
     106                 :          0 :                 return err;
     107                 :            :         }
     108                 :            : 
     109                 :            :         return err;
     110                 :            : }
     111                 :            : 
     112                 :            : static int
     113                 :          0 : tap_ctl_allocate_minor(int *minor, char **minor_name)
     114                 :            : {
     115                 :          0 :         char *path = NULL;
     116                 :            :         struct stat st_buf;
     117                 :            :         int err, id, st, f, fid;
     118                 :            : 
     119                 :          0 :         *minor = -1;
     120                 :            : 
     121                 :          0 :         f = open(BLKTAP2_NP_RUN_DIR, O_RDONLY);
     122         [ #  # ]:          0 :         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                 :          0 :         err = flock(f, LOCK_EX);
     130         [ #  # ]:          0 :         if (err == -1) {
     131                 :          0 :                 err = -errno;
     132                 :          0 :                 EPRINTF("Failed to lock runtime directory %d\n", errno);
     133                 :            :                 return err;
     134                 :            :         }
     135                 :            : 
     136         [ #  # ]:          0 :         for (id=0; id<MAX_ID; id++) {
     137                 :          0 :                 err = asprintf(&path, "%s/tapdisk-%d", BLKTAP2_NP_RUN_DIR, id);
     138         [ #  # ]:          0 :                 if (err == -1) {
     139                 :          0 :                         err = -errno;
     140                 :          0 :                         goto out;
     141                 :            :                 }
     142                 :            : 
     143                 :          0 :                 st = stat(path, &st_buf);
     144         [ #  # ]:          0 :                 if (st == 0) {
     145                 :            :                         /* Already exists */
     146                 :          0 :                         free(path);
     147                 :          0 :                         path = NULL;
     148                 :          0 :                         continue;
     149                 :            :                 }
     150         [ #  # ]:          0 :                 if (errno != ENOENT) {
     151                 :          0 :                         err = -errno;
     152                 :          0 :                         free(path);
     153                 :          0 :                         goto out;
     154                 :            :                 }
     155                 :            : 
     156                 :          0 :                 fid = open(path, O_CREAT | O_WRONLY, 0600);
     157         [ #  # ]:          0 :                 if (fid == -1) {
     158                 :          0 :                         err = -errno;
     159                 :          0 :                         EPRINTF("Failed to create ID file %s, %d\n", path, errno);
     160                 :          0 :                         free(path);
     161                 :          0 :                         goto out;
     162                 :            :                 }
     163                 :          0 :                 close(fid);
     164                 :            : 
     165                 :          0 :                 *minor = id;
     166                 :          0 :                 *minor_name = path;
     167                 :          0 :                 break;
     168                 :            :         }
     169                 :            : 
     170                 :            :         err = 0;
     171                 :            : out:
     172                 :          0 :         flock(f, LOCK_UN);
     173                 :          0 :         close(f);
     174                 :            :         return err;
     175                 :            : }
     176                 :            : 
     177                 :            : int
     178                 :          0 : tap_ctl_allocate(int *minor, char **minor_name)
     179                 :            : {
     180                 :            :         int err;
     181                 :            : 
     182                 :          0 :         *minor = -1;
     183                 :            : 
     184                 :          0 :         err = tap_ctl_check_environment();
     185         [ #  # ]:          0 :         if (err) {
     186                 :            :                 EPRINTF("tap-ctl allocate failed check environment");
     187                 :          0 :                 return err;
     188                 :            :         }
     189                 :            : 
     190                 :          0 :         err = tap_ctl_allocate_minor(minor, minor_name);
     191         [ #  # ]:          0 :         if (err) {
     192                 :            :                 EPRINTF("tap-ctl allocate failed to allocate device");
     193                 :          0 :                 return err;
     194                 :            :         }
     195                 :            : 
     196                 :            :         return 0;
     197                 :            : }

Generated by: LCOV version 1.13