"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/test_utils.c" (13 Jan 2022, 16542 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_utils.c": 2.4.0_vs_2.4.1.

    1 /*
    2  * cryptsetup library API test utilities
    3  *
    4  * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
    5  * Copyright (C) 2009-2021 Milan Broz
    6  *
    7  * This program is free software; you can redistribute it and/or
    8  * modify it under the terms of the GNU General Public License
    9  * as published by the Free Software Foundation; either version 2
   10  * of the License, or (at your option) any later version.
   11  *
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   20  */
   21 
   22 #include <errno.h>
   23 #include <fcntl.h>
   24 #include <inttypes.h>
   25 #include <stdlib.h>
   26 #include <libdevmapper.h>
   27 #include <linux/fs.h>
   28 #include <sys/ioctl.h>
   29 #include <sys/stat.h>
   30 #include <sys/types.h>
   31 #include <unistd.h>
   32 #ifdef KERNEL_KEYRING
   33 # include <linux/keyctl.h>
   34 # include <sys/syscall.h>
   35 #endif
   36 #ifdef HAVE_SYS_SYSMACROS_H
   37 # include <sys/sysmacros.h>
   38 #endif
   39 #include <linux/loop.h>
   40 
   41 #include "api_test.h"
   42 #include "libcryptsetup.h"
   43 
   44 #ifndef LOOP_CONFIGURE
   45 #define LOOP_CONFIGURE 0x4C0A
   46 struct loop_config {
   47   __u32 fd;
   48   __u32 block_size;
   49   struct loop_info64 info;
   50   __u64 __reserved[8];
   51 };
   52 #endif
   53 
   54 static char last_error[256];
   55 static char global_log[4096];
   56 static uint32_t t_dm_crypt_flags = 0;
   57 
   58 char *THE_LOOP_DEV = NULL;
   59 int _debug   = 0;
   60 int global_lines = 0;
   61 int _quit = 0;
   62 int _verbose = 0;
   63 uint64_t t_dev_offset = 0;
   64 
   65 static void (*_cleanup)(void);
   66 
   67 void register_cleanup(void (*cleanup)(void))
   68 {
   69     _cleanup = cleanup;
   70 }
   71 
   72 void check_ok(int status, int line, const char *func)
   73 {
   74     if (status) {
   75         printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
   76         _cleanup();
   77         exit(-1);
   78     }
   79 }
   80 
   81 void check_ok_return(int status, int line, const char *func)
   82 {
   83     if (status < 0) {
   84         printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
   85         _cleanup();
   86         exit(-1);
   87     }
   88 }
   89 
   90 void check_ko(int status, int line, const char *func)
   91 {
   92     if (status >= 0) {
   93         printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
   94         _cleanup();
   95         exit(-1);
   96     } else if (_verbose)
   97         printf("   => errno %d, errmsg: %s\n", status, last_error);
   98 }
   99 
  100 void check_equal(int line, const char *func, int64_t x, int64_t y)
  101 {
  102     printf("FAIL line %d [%s]: expected equal values differs: %"
  103         PRIi64 " != %" PRIi64 "\n", line, func, x, y);
  104     _cleanup();
  105     exit(-1);
  106 }
  107 
  108 void check_ge_equal(int line, const char *func, int64_t x, int64_t y)
  109 {
  110     printf("FAIL line %d [%s]: expected greater or equal values differs: %"
  111         PRIi64 " < %" PRIi64 "\n", line, func, x, y);
  112     _cleanup();
  113     exit(-1);
  114 }
  115 
  116 void check_null(int line, const char *func, const void *x)
  117 {
  118     if (x) {
  119         printf("FAIL line %d [%s]: expected NULL value: %p\n", line, func, x);
  120         _cleanup();
  121         exit(-1);
  122     }
  123 }
  124 
  125 void check_notnull(int line, const char *func, const void *x)
  126 {
  127     if (!x) {
  128         printf("FAIL line %d [%s]: expected not NULL value: %p\n", line, func, x);
  129         _cleanup();
  130         exit(-1);
  131     }
  132 }
  133 
  134 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
  135 {
  136     if (_verbose) {
  137         if (txt)
  138             printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
  139         else
  140             printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
  141     }
  142     if (_quit) {
  143         if (_verbose)
  144             printf("Interrupted by a signal.\n");
  145         _cleanup();
  146         exit(-1);
  147     }
  148 }
  149 
  150 int t_device_size(const char *device, uint64_t *size)
  151 {
  152     int devfd, r = 0;
  153 
  154     devfd = open(device, O_RDONLY);
  155     if(devfd == -1)
  156         return -EINVAL;
  157 
  158     if (ioctl(devfd, BLKGETSIZE64, size) < 0)
  159         r = -EINVAL;
  160     close(devfd);
  161     return r;
  162 }
  163 
  164 int fips_mode(void)
  165 {
  166     int fd;
  167     char buf = 0;
  168 
  169     fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);
  170 
  171     if (fd < 0)
  172         return 0;
  173 
  174     if (read(fd, &buf, 1) != 1)
  175         buf = '0';
  176 
  177     close(fd);
  178 
  179     return (buf == '1');
  180 }
  181 
  182 /*
  183  * Creates dm-linear target over the test loop device. Offset is held in
  184  * global variables so that size can be tested whether it fits into remaining
  185  * size of the loop device or not
  186  */
  187 int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
  188 {
  189     char cmd[128];
  190     int r;
  191     uint64_t r_size;
  192 
  193     if (t_device_size(THE_LOOP_DEV, &r_size) < 0 || r_size <= t_dev_offset || !size)
  194         return -1;
  195     if ((r_size - t_dev_offset) < size) {
  196         printf("No enough space on backing loop device\n.");
  197         return -2;
  198     }
  199     snprintf(cmd, sizeof(cmd),
  200          "dmsetup create %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
  201          dm_name, size, THE_LOOP_DEV, t_dev_offset);
  202     if (!(r = _system(cmd, 1)))
  203         t_dev_offset += size;
  204     return r;
  205 }
  206 
  207 // Get key from kernel dm mapping table using dm-ioctl
  208 int get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
  209 {
  210     struct dm_task *dmt;
  211     struct dm_info dmi;
  212     uint64_t start, length;
  213     char *target_type, *key, *params;
  214     void *next = NULL;
  215     int r = -EINVAL;
  216 
  217     if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
  218         goto out;
  219     if (!dm_task_set_name(dmt, name))
  220         goto out;
  221     if (!dm_task_run(dmt))
  222         goto out;
  223     if (!dm_task_get_info(dmt, &dmi))
  224         goto out;
  225     if (!dmi.exists)
  226         goto out;
  227 
  228     next = dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
  229     if (!target_type || strcmp(target_type, "crypt") != 0)
  230         goto out;
  231 
  232     (void)strsep(&params, " "); /* rcipher */
  233     key = strsep(&params, " ");
  234 
  235     if (buffer_size <= strlen(key))
  236         goto out;
  237 
  238     strncpy(buffer, key, buffer_size);
  239     r = 0;
  240 out:
  241     if (dmt)
  242         dm_task_destroy(dmt);
  243 
  244     return r;
  245 }
  246 
  247 int prepare_keyfile(const char *name, const char *passphrase, int size)
  248 {
  249     int fd, r;
  250 
  251     fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR);
  252     if (fd != -1) {
  253         r = write(fd, passphrase, size);
  254         close(fd);
  255     } else
  256         r = 0;
  257 
  258     return r == size ? 0 : 1;
  259 }
  260 
  261 // Decode key from its hex representation
  262 int crypt_decode_key(char *key, const char *hex, unsigned int size)
  263 {
  264     char buffer[3];
  265     char *endp;
  266     unsigned int i;
  267 
  268     buffer[2] = '\0';
  269 
  270     for (i = 0; i < size; i++) {
  271         buffer[0] = *hex++;
  272         buffer[1] = *hex++;
  273 
  274         key[i] = (unsigned char)strtoul(buffer, &endp, 16);
  275 
  276         if (endp != &buffer[2])
  277             return -1;
  278     }
  279 
  280     if (*hex != '\0')
  281         return -1;
  282 
  283     return 0;
  284 }
  285 
  286 void global_log_callback(int level, const char *msg, void *usrptr __attribute__((unused)))
  287 {
  288     size_t len;
  289 
  290     if (_debug) {
  291         if (level == CRYPT_LOG_DEBUG)
  292             fprintf(stdout, "# %s", msg);
  293         else
  294             fprintf(stdout, "%s", msg);
  295     }
  296 
  297     if (level <= CRYPT_LOG_DEBUG)
  298         return;
  299 
  300     len = strlen(global_log);
  301 
  302     if (len + strlen(msg) > sizeof(global_log)) {
  303             printf("Log buffer is too small, fix the test.\n");
  304             return;
  305     }
  306 
  307     strncat(global_log, msg, sizeof(global_log) - len);
  308     global_lines++;
  309     if (level == CRYPT_LOG_ERROR) {
  310         len = strlen(msg);
  311         if (len > sizeof(last_error))
  312             len = sizeof(last_error);
  313         strncpy(last_error, msg, sizeof(last_error));
  314         last_error[len-1] = '\0';
  315     }
  316 }
  317 
  318 void reset_log(void)
  319 {
  320     memset(global_log, 0, sizeof(global_log));
  321     memset(last_error, 0, sizeof(last_error));
  322     global_lines = 0;
  323 }
  324 
  325 int _system(const char *command, int warn)
  326 {
  327     int r;
  328     if (_debug)
  329         printf("Running system: %s\n", command);
  330     if ((r=system(command)) < 0 && warn)
  331         printf("System command failed: %s", command);
  332     return r;
  333 }
  334 
  335 static int _keyring_check(void)
  336 {
  337 #ifdef KERNEL_KEYRING
  338     return syscall(__NR_request_key, "logon", "dummy", NULL, 0) == -1l && errno != ENOSYS;
  339 #else
  340     return 0;
  341 #endif
  342 }
  343 
  344 static int t_dm_satisfies_version(unsigned target_maj, unsigned target_min, unsigned target_patch,
  345                  unsigned actual_maj, unsigned actual_min, unsigned actual_patch)
  346 {
  347     if (actual_maj > target_maj)
  348         return 1;
  349     if (actual_maj == target_maj && actual_min > target_min)
  350         return 1;
  351     if (actual_maj == target_maj && actual_min == target_min && actual_patch >= target_patch)
  352         return 1;
  353     return 0;
  354 }
  355 
  356 static void t_dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
  357                  unsigned crypt_min, unsigned crypt_patch)
  358 {
  359     unsigned dm_maj = 0, dm_min = 0, dm_patch = 0;
  360 
  361     if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3) {
  362         dm_maj = 0;
  363         dm_min = 0;
  364         dm_patch = 0;
  365     }
  366 
  367     if (t_dm_satisfies_version(1, 2, 0, crypt_maj, crypt_min, 0))
  368         t_dm_crypt_flags |= T_DM_KEY_WIPE_SUPPORTED;
  369 
  370     if (t_dm_satisfies_version(1, 10, 0, crypt_maj, crypt_min, 0))
  371         t_dm_crypt_flags |= T_DM_LMK_SUPPORTED;
  372 
  373     if (t_dm_satisfies_version(4, 20, 0, dm_maj, dm_min, 0))
  374         t_dm_crypt_flags |= T_DM_SECURE_SUPPORTED;
  375 
  376     if (t_dm_satisfies_version(1, 8, 0, crypt_maj, crypt_min, 0))
  377         t_dm_crypt_flags |= T_DM_PLAIN64_SUPPORTED;
  378 
  379     if (t_dm_satisfies_version(1, 11, 0, crypt_maj, crypt_min, 0))
  380         t_dm_crypt_flags |= T_DM_DISCARDS_SUPPORTED;
  381 
  382     if (t_dm_satisfies_version(1, 13, 0, crypt_maj, crypt_min, 0))
  383         t_dm_crypt_flags |= T_DM_TCW_SUPPORTED;
  384 
  385     if (t_dm_satisfies_version(1, 14, 0, crypt_maj, crypt_min, 0)) {
  386         t_dm_crypt_flags |= T_DM_SAME_CPU_CRYPT_SUPPORTED;
  387         t_dm_crypt_flags |= T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED;
  388     }
  389 
  390     if (t_dm_satisfies_version(1, 18, 1, crypt_maj, crypt_min, crypt_patch) && _keyring_check())
  391         t_dm_crypt_flags |= T_DM_KERNEL_KEYRING_SUPPORTED;
  392 }
  393 
  394 static void t_dm_set_verity_compat(const char *dm_version __attribute__((unused)),
  395     unsigned verity_maj,
  396     unsigned verity_min,
  397     unsigned verity_patch __attribute__((unused)))
  398 {
  399     if (verity_maj > 0)
  400         t_dm_crypt_flags |= T_DM_VERITY_SUPPORTED;
  401     else
  402         return;
  403     /*
  404      * ignore_corruption, restart_on corruption is available since 1.2 (kernel 4.1)
  405      * ignore_zero_blocks since 1.3 (kernel 4.5)
  406      * (but some dm-verity targets 1.2 don't support it)
  407      * FEC is added in 1.3 as well.
  408      */
  409     if (t_dm_satisfies_version(1, 3, 0, verity_maj, verity_min, 0)) {
  410         t_dm_crypt_flags |= T_DM_VERITY_ON_CORRUPTION_SUPPORTED;
  411         t_dm_crypt_flags |= T_DM_VERITY_FEC_SUPPORTED;
  412     }
  413 }
  414 
  415 static void t_dm_set_integrity_compat(const char *dm_version __attribute__((unused)),
  416     unsigned integrity_maj __attribute__((unused)),
  417     unsigned integrity_min __attribute__((unused)),
  418     unsigned integrity_patch __attribute__((unused)))
  419 {
  420     if (integrity_maj > 0)
  421         t_dm_crypt_flags |= T_DM_INTEGRITY_SUPPORTED;
  422 }
  423 
  424 int t_dm_check_versions(void)
  425 {
  426     struct dm_task *dmt;
  427     struct dm_versions *target, *last_target;
  428     char dm_version[16];
  429     int r = 1;
  430 
  431     if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
  432         goto out;
  433 
  434     if (!dm_task_run(dmt))
  435         goto out;
  436 
  437     if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version)))
  438         goto out;
  439 
  440     target = dm_task_get_versions(dmt);
  441     do {
  442         last_target = target;
  443         if (!strcmp("crypt", target->name)) {
  444             t_dm_set_crypt_compat(dm_version,
  445                          (unsigned)target->version[0],
  446                          (unsigned)target->version[1],
  447                          (unsigned)target->version[2]);
  448         } else if (!strcmp("verity", target->name)) {
  449             t_dm_set_verity_compat(dm_version,
  450                          (unsigned)target->version[0],
  451                          (unsigned)target->version[1],
  452                          (unsigned)target->version[2]);
  453         } else if (!strcmp("integrity", target->name)) {
  454             t_dm_set_integrity_compat(dm_version,
  455                          (unsigned)target->version[0],
  456                          (unsigned)target->version[1],
  457                          (unsigned)target->version[2]);
  458         }
  459         target = (struct dm_versions *)((char *) target + target->next);
  460     } while (last_target != target);
  461 
  462     r = 0;
  463 out:
  464     if (dmt)
  465         dm_task_destroy(dmt);
  466 
  467     return r;
  468 }
  469 
  470 int t_dm_crypt_keyring_support(void)
  471 {
  472     return t_dm_crypt_flags & T_DM_KERNEL_KEYRING_SUPPORTED;
  473 }
  474 
  475 int t_dm_crypt_cpu_switch_support(void)
  476 {
  477     return t_dm_crypt_flags & (T_DM_SAME_CPU_CRYPT_SUPPORTED |
  478                    T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED);
  479 }
  480 
  481 int t_dm_crypt_discard_support(void)
  482 {
  483     return t_dm_crypt_flags & T_DM_DISCARDS_SUPPORTED;
  484 }
  485 
  486 /* loop helpers */
  487 
  488 #define LOOP_DEV_MAJOR 7
  489 
  490 #ifndef LO_FLAGS_AUTOCLEAR
  491 #define LO_FLAGS_AUTOCLEAR 4
  492 #endif
  493 
  494 #ifndef LOOP_CTL_GET_FREE
  495 #define LOOP_CTL_GET_FREE 0x4C82
  496 #endif
  497 
  498 #ifndef LOOP_SET_CAPACITY
  499 #define LOOP_SET_CAPACITY 0x4C07
  500 #endif
  501 
  502 int loop_device(const char *loop)
  503 {
  504     struct stat st;
  505 
  506     if (!loop)
  507         return 0;
  508 
  509     if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
  510         major(st.st_rdev) != LOOP_DEV_MAJOR)
  511         return 0;
  512 
  513     return 1;
  514 }
  515 
  516 static char *crypt_loop_get_device_old(void)
  517 {
  518     char dev[20];
  519     int i, loop_fd;
  520     struct loop_info64 lo64 = {0};
  521 
  522     for (i = 0; i < 256; i++) {
  523         sprintf(dev, "/dev/loop%d", i);
  524 
  525         loop_fd = open(dev, O_RDONLY);
  526         if (loop_fd < 0)
  527             return NULL;
  528 
  529         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
  530             errno == ENXIO) {
  531             close(loop_fd);
  532             return strdup(dev);
  533         }
  534         close(loop_fd);
  535     }
  536 
  537     return NULL;
  538 }
  539 
  540 static char *crypt_loop_get_device(void)
  541 {
  542     char dev[64];
  543     int i, loop_fd;
  544     struct stat st;
  545 
  546     loop_fd = open("/dev/loop-control", O_RDONLY);
  547     if (loop_fd < 0)
  548         return crypt_loop_get_device_old();
  549 
  550     i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
  551     if (i < 0) {
  552         close(loop_fd);
  553         return NULL;
  554     }
  555     close(loop_fd);
  556 
  557     if (sprintf(dev, "/dev/loop%d", i) < 0)
  558         return NULL;
  559 
  560     if (stat(dev, &st) || !S_ISBLK(st.st_mode))
  561         return NULL;
  562 
  563     return strdup(dev);
  564 }
  565 
  566 int loop_attach(char **loop, const char *file, int offset,
  567               int autoclear, int *readonly)
  568 {
  569     struct loop_config config = {0};
  570     char *lo_file_name;
  571     int loop_fd = -1, file_fd = -1, r = 1;
  572     int fallback = 0;
  573 
  574     *loop = NULL;
  575 
  576     file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
  577     if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
  578         *readonly = 1;
  579         file_fd = open(file, O_RDONLY | O_EXCL);
  580     }
  581     if (file_fd < 0)
  582         goto out;
  583 
  584     config.fd = file_fd;
  585 
  586     lo_file_name = (char*)config.info.lo_file_name;
  587     lo_file_name[LO_NAME_SIZE-1] = '\0';
  588     strncpy(lo_file_name, file, LO_NAME_SIZE-1);
  589     config.info.lo_offset = offset;
  590     if (autoclear)
  591         config.info.lo_flags |= LO_FLAGS_AUTOCLEAR;
  592 
  593     while (loop_fd < 0)  {
  594         *loop = crypt_loop_get_device();
  595         if (!*loop)
  596             goto out;
  597 
  598         loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
  599         if (loop_fd < 0)
  600             goto out;
  601         if (ioctl(loop_fd, LOOP_CONFIGURE, &config) < 0) {
  602             if (errno == EINVAL || errno == ENOTTY) {
  603                 free(*loop);
  604                 *loop = NULL;
  605 
  606                 close(loop_fd);
  607                 loop_fd = -1;
  608 
  609                 /* kernel doesn't support LOOP_CONFIGURE */
  610                 fallback = 1;
  611                 break;
  612             }
  613             if (errno != EBUSY)
  614                 goto out;
  615             free(*loop);
  616             *loop = NULL;
  617 
  618             close(loop_fd);
  619             loop_fd = -1;
  620         }
  621     }
  622 
  623     if (fallback)
  624     {
  625         while (loop_fd < 0)  {
  626             *loop = crypt_loop_get_device();
  627             if (!*loop)
  628                 goto out;
  629 
  630             loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
  631             if (loop_fd < 0)
  632                 goto out;
  633             if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
  634                 if (errno != EBUSY)
  635                     goto out;
  636                 free(*loop);
  637                 *loop = NULL;
  638 
  639                 close(loop_fd);
  640                 loop_fd = -1;
  641             }
  642         }
  643 
  644         if (ioctl(loop_fd, LOOP_SET_STATUS64, &config.info) < 0) {
  645             (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
  646             goto out;
  647         }
  648     }
  649 
  650     /* Verify that autoclear is really set */
  651     if (autoclear) {
  652         memset(&config.info, 0, sizeof(config.info));
  653         if (ioctl(loop_fd, LOOP_GET_STATUS64, &config.info) < 0 ||
  654            !(config.info.lo_flags & LO_FLAGS_AUTOCLEAR)) {
  655         (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
  656             goto out;
  657         }
  658     }
  659 
  660     r = 0;
  661 out:
  662     if (r && loop_fd >= 0)
  663         close(loop_fd);
  664     if (file_fd >= 0)
  665         close(file_fd);
  666     if (r && *loop) {
  667         free(*loop);
  668         *loop = NULL;
  669     }
  670     return r ? -1 : loop_fd;
  671 }
  672 
  673 int loop_detach(const char *loop)
  674 {
  675     int loop_fd = -1, r = 1;
  676 
  677     loop_fd = open(loop, O_RDONLY);
  678     if (loop_fd < 0)
  679                 return 1;
  680 
  681     if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
  682         r = 0;
  683 
  684     close(loop_fd);
  685     return r;
  686 }
  687 
  688 int t_get_devno(const char *name, dev_t *devno)
  689 {
  690     char path[PATH_MAX];
  691     int r;
  692     struct stat st;
  693 
  694     r = snprintf(path, sizeof(path), DMDIR "%s", name);
  695     if (r < 0 || (size_t)r >= sizeof(path))
  696         return 1;
  697 
  698     if (stat(path, &st) || !S_ISBLK(st.st_mode))
  699         return 1;
  700 
  701     *devno = st.st_rdev;
  702 
  703     return 0;
  704 }
  705 
  706 static int _read_uint64(const char *sysfs_path, uint64_t *value)
  707 {
  708         char tmp[64] = {0};
  709         int fd, r;
  710 
  711         if ((fd = open(sysfs_path, O_RDONLY)) < 0)
  712                 return 0;
  713         r = read(fd, tmp, sizeof(tmp));
  714         close(fd);
  715 
  716         if (r <= 0)
  717                 return 0;
  718 
  719         if (sscanf(tmp, "%" PRIu64, value) != 1)
  720                 return 0;
  721 
  722         return 1;
  723 }
  724 
  725 static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *attr)
  726 {
  727         char path[PATH_MAX];
  728 
  729         if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/%s",
  730                      major, minor, attr) < 0)
  731                 return 0;
  732 
  733         return _read_uint64(path, value);
  734 }
  735 
  736 int t_device_size_by_devno(dev_t devno, uint64_t *retval)
  737 {
  738     if (!_sysfs_get_uint64(major(devno), minor(devno), retval, "size"))
  739         return 1;
  740 
  741     *retval *= 512;
  742     return 0;
  743 }