LCOV - code coverage report
Current view: top level - cmdline - unix.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 314 447 70.2 %
Date: 2017-05-13 Functions: 27 31 87.1 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2013 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "portable.h"
      19             : 
      20             : #ifndef __MINGW32__ /* Only for Unix */
      21             : 
      22             : #include "support.h"
      23             : 
      24             : /**
      25             :  * Exit codes.
      26             :  */
      27             : int exit_success = 0;
      28             : int exit_failure = 1;
      29             : int exit_sync_needed = 2;
      30             : 
      31     1718110 : int open_noatime(const char* file, int flags)
      32             : {
      33             : #ifdef O_NOATIME
      34     1718110 :         int f = open(file, flags | O_NOATIME);
      35             : 
      36             :         /* only root is allowed to use O_NOATIME, in case retry without it */
      37     1718988 :         if (f == -1 && errno == EPERM)
      38           0 :                 f = open(file, flags);
      39     1717923 :         return f;
      40             : #else
      41             :         return open(file, flags);
      42             : #endif
      43             : }
      44             : 
      45           0 : int dirent_hidden(struct dirent* dd)
      46             : {
      47           0 :         return dd->d_name[0] == '.';
      48             : }
      49             : 
      50           0 : const char* stat_desc(struct stat* st)
      51             : {
      52           0 :         if (S_ISREG(st->st_mode))
      53           0 :                 return "regular";
      54           0 :         if (S_ISDIR(st->st_mode))
      55           0 :                 return "directory";
      56           0 :         if (S_ISCHR(st->st_mode))
      57           0 :                 return "character";
      58           0 :         if (S_ISBLK(st->st_mode))
      59           0 :                 return "block-device";
      60           0 :         if (S_ISFIFO(st->st_mode))
      61           0 :                 return "fifo";
      62           0 :         if (S_ISLNK(st->st_mode))
      63           0 :                 return "link";
      64           0 :         if (S_ISLNK(st->st_mode))
      65           0 :                 return "symbolic-link";
      66           0 :         if (S_ISSOCK(st->st_mode))
      67           0 :                 return "socket";
      68           0 :         return "unknown";
      69             : }
      70             : 
      71             : /**
      72             :  * Get the device file from the device number.
      73             :  *
      74             :  * It uses /proc/self/mountinfo.
      75             :  *
      76             :  * Null devices (major==0) are resolved to the device indicated in mountinfo.
      77             :  */
      78             : #if HAVE_LINUX_DEVICE
      79           4 : static int devresolve_proc(uint64_t device, char* path, size_t path_size)
      80             : {
      81             :         FILE* f;
      82             :         char match[32];
      83             : 
      84             :         /* generate the matching string */
      85           4 :         snprintf(match, sizeof(match), "%u:%u", major(device), minor(device));
      86             : 
      87           4 :         f = fopen("/proc/self/mountinfo", "r");
      88           4 :         if (!f) {
      89           0 :                 log_tag("resolve:proc:%u:%u: failed to open /proc/self/mountinfo\n", major(device), minor(device));
      90           0 :                 return -1;
      91             :         }
      92             : 
      93             :         /*
      94             :          * mountinfo format
      95             :          * 0 - mount ID
      96             :          * 1 - parent ID
      97             :          * 2 - major:minor
      98             :          * 3 - root
      99             :          * 4 - mount point
     100             :          * 5 - options
     101             :          * 6 - "-" (separator)
     102             :          * 7 - fs
     103             :          * 8 - mount source - /dev/device
     104             :          */
     105             : 
     106             :         while (1) {
     107             :                 char buf[256];
     108             :                 char* first_map[8];
     109             :                 unsigned first_mac;
     110             :                 char* second_map[8];
     111             :                 unsigned second_mac;
     112             :                 char* s;
     113             :                 struct stat st;
     114             :                 char* separator;
     115             :                 char* majorminor;
     116             :                 char* mountpoint;
     117             :                 char* fs;
     118             :                 char* mountsource;
     119             : 
     120         104 :                 s = fgets(buf, sizeof(buf), f);
     121         104 :                 if (s == 0)
     122           4 :                         break;
     123             : 
     124             :                 /* find the separator position */
     125         100 :                 separator = strstr(s, " - ");
     126         100 :                 if (!separator)
     127           0 :                         continue;
     128             : 
     129             :                 /* skip the separator */
     130         100 :                 *separator = 0;
     131         100 :                 separator += 3;
     132             : 
     133             :                 /* split the line */
     134         100 :                 first_mac = strsplit(first_map, 8, s, " \t\r\n");
     135         100 :                 second_mac = strsplit(second_map, 8, separator, " \t\r\n");
     136             : 
     137             :                 /* if too short, it's the wrong line */
     138         100 :                 if (first_mac < 5)
     139           0 :                         continue;
     140         100 :                 if (second_mac < 2)
     141           0 :                         continue;
     142             : 
     143         100 :                 majorminor = first_map[2];
     144         100 :                 mountpoint = first_map[4];
     145         100 :                 fs = second_map[0];
     146         100 :                 mountsource = second_map[1];
     147             : 
     148             :                 /* compare major:minor from mountinfo */
     149         100 :                 if (strcmp(majorminor, match) == 0) {
     150             :                         /*
     151             :                          * Accept only /dev/... mountsource
     152             :                          *
     153             :                          * This excludes ZFS that uses a bare label for mountsource, like "tank".
     154             :                          *
     155             :                          * 410 408 0:193 / /XXX rw,relatime shared:217 - zfs tank/system/data/var/lib/docker/XXX rw,xattr,noacl
     156             :                          *
     157             :                          * Also excludes AUTOFS unmounted devices that point to a fake filesystem
     158             :                          * used to remount them at the first use.
     159             :                          *
     160             :                          * 97 25 0:42 / /XXX rw,relatime shared:76 - autofs /etc/auto.seed rw,fd=6,pgrp=952,timeout=30,minproto=5,maxproto=5,indirect
     161             :                          */
     162           0 :                         if (strncmp(mountsource, "/dev/", 5) != 0) {
     163           0 :                                 log_tag("resolve:proc:%u:%u: match skipped for not /dev/ mountsource for %s %s\n", major(device), minor(device), fs, mountsource);
     164           0 :                                 continue;
     165             :                         }
     166             : 
     167           0 :                         pathcpy(path, path_size, mountsource);
     168             : 
     169           0 :                         log_tag("resolve:proc:%u:%u: found device %s matching device %s\n", major(device), minor(device), path, match);
     170             : 
     171           0 :                         fclose(f);
     172           0 :                         return 0;
     173             :                 }
     174             : 
     175             :                 /* get the device of the mount point */
     176             :                 /* in Btrfs it could be different than the one in mountinfo */
     177         100 :                 if (stat(mountpoint, &st) == 0 && st.st_dev == device) {
     178           0 :                         if (strncmp(mountsource, "/dev/", 5) != 0) {
     179           0 :                                 log_tag("resolve:proc:%u:%u: match skipped for not /dev/ mountsource for %s %s\n", major(device), minor(device), fs, mountsource);
     180           0 :                                 continue;
     181             :                         }
     182             : 
     183           0 :                         pathcpy(path, path_size, mountsource);
     184             : 
     185           0 :                         log_tag("resolve:proc:%u:%u: found device %s matching mountpoint %s\n", major(device), minor(device), path, mountpoint);
     186             : 
     187           0 :                         fclose(f);
     188           0 :                         return 0;
     189             :                 }
     190         100 :         }
     191             : 
     192           4 :         log_tag("resolve:proc:%u:%u: not found\n", major(device), minor(device));
     193             : 
     194           4 :         fclose(f);
     195           4 :         return -1;
     196             : }
     197             : #endif
     198             : 
     199             : /**
     200             :  * Get the device of a virtual superblock.
     201             :  *
     202             :  * This is intended to resolve the case of Btrfs filesystems that
     203             :  * create a virtual superblock (major==0) not backed by any low
     204             :  * level device.
     205             :  *
     206             :  * See:
     207             :  * Bug 711881 - too funny btrfs st_dev numbers
     208             :  * https://bugzilla.redhat.com/show_bug.cgi?id=711881
     209             :  */
     210             : #if HAVE_LINUX_DEVICE
     211           4 : static int devdereference(uint64_t device, uint64_t* new_device)
     212             : {
     213             :         char path[PATH_MAX];
     214             :         struct stat st;
     215             : 
     216             :         /* use the proc interface to get the device containing the filesystem */
     217           4 :         if (devresolve_proc(device, path, sizeof(path)) != 0) {
     218             :                 /* LCOV_EXCL_START */
     219             :                 return -1;
     220             :                 /* LCOV_EXCL_STOP */
     221             :         }
     222             : 
     223             :         /* check the device */
     224           0 :         if (stat(path, &st) != 0) {
     225             :                 /* LCOV_EXCL_START */
     226             :                 log_tag("dereference:%u:%u: failed to stat %s\n", major(device), minor(device), path);
     227             :                 return -1;
     228             :                 /* LCOV_EXCL_STOP */
     229             :         }
     230             : 
     231           0 :         if (major(st.st_rdev) == 0) {
     232             :                 /* LCOV_EXCL_START */
     233             :                 log_tag("dereference:%u:%u: still null device %s -> %u:%u\n", major(device), minor(device), path, major(st.st_rdev), minor(st.st_rdev));
     234             :                 return -1;
     235             :                 /* LCOV_EXCL_STOP */
     236             :         }
     237             : 
     238           0 :         *new_device = st.st_rdev;
     239           0 :         log_tag("dereference:%u:%u: found %u:%u\n", major(device), minor(device), major(st.st_rdev), minor(st.st_rdev));
     240           0 :         return 0;
     241             : }
     242             : #endif
     243             : 
     244             : /**
     245             :  * Read a file extracting the specified tag TAG=VALUE format.
     246             :  * Return !=0 on error.
     247             :  */
     248             : #if HAVE_LINUX_DEVICE
     249         191 : static int tagread(const char* path, const char* tag, char* value, size_t value_size)
     250             : {
     251             :         int f;
     252             :         int ret;
     253             :         int len;
     254             :         char buf[512];
     255             :         size_t tag_len;
     256             :         char* i;
     257             :         char* e;
     258             : 
     259         191 :         f = open(path, O_RDONLY);
     260         191 :         if (f == -1) {
     261             :                 /* LCOV_EXCL_START */
     262             :                 log_fatal("Failed to open '%s'.\n", path);
     263             :                 return 0;
     264             :                 /* LCOV_EXCL_STOP */
     265             :         }
     266             : 
     267         191 :         len = read(f, buf, sizeof(buf));
     268         190 :         if (len < 0) {
     269             :                 /* LCOV_EXCL_START */
     270             :                 close(f);
     271             :                 log_fatal("Failed to read '%s'.\n", path);
     272             :                 return -1;
     273             :                 /* LCOV_EXCL_STOP */
     274             :         }
     275         190 :         if (len == sizeof(buf)) {
     276             :                 /* LCOV_EXCL_START */
     277             :                 close(f);
     278             :                 log_fatal("Too long read '%s'.\n", path);
     279             :                 return -1;
     280             :                 /* LCOV_EXCL_STOP */
     281             :         }
     282             : 
     283         190 :         ret = close(f);
     284         195 :         if (ret != 0) {
     285             :                 /* LCOV_EXCL_START */
     286             :                 log_fatal("Failed to close '%s'.\n", path);
     287             :                 return -1;
     288             :                 /* LCOV_EXCL_STOP */
     289             :         }
     290             : 
     291         195 :         buf[len] = 0;
     292         195 :         tag_len = strlen(tag);
     293             : 
     294        3319 :         for (i = buf; *i; ++i) {
     295        3320 :                 char* p = i;
     296             : 
     297             :                 /* start with a space */
     298        3320 :                 if (p != buf) {
     299        3129 :                         if (!isspace(*p))
     300        2743 :                                 continue;
     301         381 :                         ++p;
     302             :                 }
     303             : 
     304         572 :                 if (strncmp(p, tag, tag_len) != 0)
     305         381 :                         continue;
     306         191 :                 p += tag_len;
     307             : 
     308             :                 /* end with a = */
     309         191 :                 if (*p != '=')
     310           0 :                         continue;
     311         191 :                 ++p;
     312             : 
     313             :                 /* found */
     314         191 :                 i = p;
     315         191 :                 break;
     316             :         }
     317         190 :         if (!*i) {
     318             :                 /* LCOV_EXCL_START */
     319             :                 log_fatal("Missing tag '%s' for '%s'.\n", tag, path);
     320             :                 return -1;
     321             :                 /* LCOV_EXCL_STOP */
     322             :         }
     323             : 
     324             :         /* terminate at the first space */
     325         190 :         e = i;
     326        1017 :         while (*e != 0 && !isspace(*e))
     327         637 :                 ++e;
     328         191 :         *e = 0;
     329             : 
     330         191 :         if (!*i) {
     331             :                 /* LCOV_EXCL_START */
     332             :                 log_fatal("Empty tag '%s' for '%s'.\n", tag, path);
     333             :                 return -1;
     334             :                 /* LCOV_EXCL_STOP */
     335             :         }
     336             : 
     337         191 :         pathprint(value, value_size, "%s", i);
     338             : 
     339         190 :         return 0;
     340             : }
     341             : #endif
     342             : 
     343             : /**
     344             :  * Get the device file from the device number.
     345             :  *
     346             :  * It uses /sys/dev/block/.../uevent.
     347             :  *
     348             :  * For null device (major==0) it fails.
     349             :  */
     350             : #if HAVE_LINUX_DEVICE
     351         191 : static int devresolve_sys(dev_t device, char* path, size_t path_size)
     352             : {
     353             :         struct stat st;
     354             :         char buf[PATH_MAX];
     355             : 
     356             :         /* default device path from device number */
     357         191 :         pathprint(path, path_size, "/sys/dev/block/%u:%u/uevent", major(device), minor(device));
     358             : 
     359         191 :         if (tagread(path, "DEVNAME", buf, sizeof(buf)) != 0) {
     360             :                 /* LCOV_EXCL_START */
     361             :                 log_tag("resolve:sys:%u:%u: failed to read DEVNAME tag '%s'\n", major(device), minor(device), path);
     362             :                 return -1;
     363             :                 /* LCOV_EXCL_STOP */
     364             :         }
     365             : 
     366             :         /* set the real device path */
     367         190 :         pathprint(path, path_size, "/dev/%s", buf);
     368             : 
     369             :         /* check the device */
     370         191 :         if (stat(path, &st) != 0) {
     371             :                 /* LCOV_EXCL_START */
     372             :                 log_tag("resolve:sys:%u:%u: failed to stat '%s'\n", major(device), minor(device), path);
     373             :                 return -1;
     374             :                 /* LCOV_EXCL_STOP */
     375             :         }
     376         190 :         if (st.st_rdev != device) {
     377             :                 /* LCOV_EXCL_START */
     378             :                 log_tag("resolve:sys:%u:%u: unexpected device '%u:%u' for '%s'.\n", major(device), minor(device), major(st.st_rdev), minor(st.st_rdev), path);
     379             :                 return -1;
     380             :                 /* LCOV_EXCL_STOP */
     381             :         }
     382             : 
     383         190 :         log_tag("resolve:sys:%u:%u:%s: found\n", major(device), minor(device), path);
     384             : 
     385         191 :         return 0;
     386             : }
     387             : #endif
     388             : 
     389             : /**
     390             :  * Get the device file from the device number.
     391             :  */
     392             : #if HAVE_LINUX_DEVICE
     393         190 : static int devresolve(uint64_t device, char* path, size_t path_size)
     394             : {
     395         190 :         if (devresolve_sys(device, path, path_size) == 0)
     396         191 :                 return 0;
     397             : 
     398           0 :         return -1;
     399             : }
     400             : #endif
     401             : 
     402             : /**
     403             :  * Cache used by blkid.
     404             :  */
     405             : #if HAVE_BLKID
     406             : static blkid_cache cache = 0;
     407             : #endif
     408             : 
     409             : /**
     410             :  * Get the UUID using the /dev/disk/by-uuid/ links.
     411             :  * It doesn't require root permission, and the uuid are always updated.
     412             :  * It doesn't work with Btrfs file-systems that don't export the main UUID
     413             :  * in /dev/disk/by-uuid/.
     414             :  */
     415             : #if HAVE_LINUX_DEVICE
     416        5626 : static int devuuid_dev(uint64_t device, char* uuid, size_t uuid_size)
     417             : {
     418             :         int ret;
     419             :         DIR* d;
     420             :         struct dirent* dd;
     421             :         struct stat st;
     422             : 
     423             :         /* scan the UUID directory searching for the device */
     424        5626 :         d = opendir("/dev/disk/by-uuid");
     425        5626 :         if (!d) {
     426           0 :                 log_tag("uuid:by-uuidd:%u:%u: opendir(/dev/disk/by-uuid) failed, %s\n", major(device), minor(device), strerror(errno));
     427             :                 /* directory missing?, likely we are not in Linux */
     428           0 :                 return -1;
     429             :         }
     430             : 
     431       95642 :         while ((dd = readdir(d)) != 0) {
     432             :                 /* skip "." and ".." files, UUIDs never start with '.' */
     433       90016 :                 if (dd->d_name[0] == '.')
     434       11252 :                         continue;
     435             : 
     436       78764 :                 ret = fstatat(dirfd(d), dd->d_name, &st, 0);
     437       78764 :                 if (ret != 0) {
     438           0 :                         log_tag("uuid:by-uuidd:%u:%u: fstatat(%s) failed, %s\n", major(device), minor(device), dd->d_name, strerror(errno));
     439             :                         /* generic error, ignore and continue the search */
     440           0 :                         continue;
     441             :                 }
     442             : 
     443             :                 /* if it matches, we have the uuid */
     444       78764 :                 if (S_ISBLK(st.st_mode) && st.st_rdev == (dev_t)device) {
     445             :                         char buf[PATH_MAX];
     446             :                         char path[PATH_MAX];
     447             : 
     448             :                         /* resolve the link */
     449        5626 :                         pathprint(path, sizeof(path), "/dev/disk/by-uuid/%s", dd->d_name);
     450        5626 :                         ret = readlink(path, buf, sizeof(buf));
     451        5626 :                         if (ret < 0 || ret == sizeof(buf)) {
     452           0 :                                 log_tag("uuid:by-uuidd:%u:%u: readlink(/dev/disk/by-uuid/%s) failed, %s\n", major(device), minor(device), dd->d_name, strerror(errno));
     453             :                                 /* generic error, ignore and continue the search */
     454           0 :                                 continue;
     455             :                         }
     456        5626 :                         buf[ret] = 0;
     457             : 
     458             :                         /* found */
     459        5626 :                         pathcpy(uuid, uuid_size, dd->d_name);
     460             : 
     461        5626 :                         log_tag("uuid:by-uuid:%u:%u:%s: found %s\n", major(device), minor(device), uuid, buf);
     462             : 
     463        5626 :                         closedir(d);
     464        5626 :                         return 0;
     465             :                 }
     466             :         }
     467             : 
     468           0 :         log_tag("uuid:by-uuidd:%u:%u: /dev/disk/by-uuid doesn't contain a matching block device\n", major(device), minor(device));
     469             : 
     470             :         /* not found */
     471           0 :         closedir(d);
     472           0 :         return -1;
     473             : }
     474             : #endif
     475             : 
     476             : /**
     477             :  * Get the UUID using liblkid.
     478             :  * It uses a cache to work without root permission, resulting in UUID
     479             :  * not necessarily recent.
     480             :  * We could call blkid_probe_all() to refresh the UUID, but it would
     481             :  * require root permission to read the superblocks, and resulting in
     482             :  * all the disks spinning.
     483             :  */
     484             : #if HAVE_BLKID
     485           0 : static int devuuid_blkid(uint64_t device, char* uuid, size_t uuid_size)
     486             : {
     487             :         char* devname;
     488             :         char* uuidname;
     489             : 
     490           0 :         devname = blkid_devno_to_devname(device);
     491           0 :         if (!devname) {
     492           0 :                 log_tag("uuid:blkid:%u:%u: blkid_devno_to_devname() failed, %s\n", major(device), minor(device), strerror(errno));
     493             :                 /* device mapping failed */
     494           0 :                 return -1;
     495             :         }
     496             : 
     497           0 :         uuidname = blkid_get_tag_value(cache, "UUID", devname);
     498           0 :         if (!uuidname) {
     499           0 :                 log_tag("uuid:blkid:%u:%u: blkid_get_tag_value(UUID,%s) failed, %s\n", major(device), minor(device), devname, strerror(errno));
     500             :                 /* uuid mapping failed */
     501           0 :                 free(devname);
     502           0 :                 return -1;
     503             :         }
     504             : 
     505           0 :         pathcpy(uuid, uuid_size, uuidname);
     506             : 
     507           0 :         log_tag("uuid:blkid:%u:%u:%s: found %s\n", major(device), minor(device), uuid, devname);
     508             : 
     509           0 :         free(devname);
     510           0 :         free(uuidname);
     511           0 :         return 0;
     512             : }
     513             : #endif
     514             : 
     515        5630 : int devuuid(uint64_t device, char* uuid, size_t uuid_size)
     516             : {
     517             : #if HAVE_LINUX_DEVICE
     518             :         /* if the major is the null device */
     519        5630 :         if (major(device) == 0) {
     520             :                 /* obtain the real device */
     521           4 :                 if (devdereference(device, &device) != 0) {
     522             :                         /* LCOV_EXCL_START */
     523             :                         return -1;
     524             :                         /* LCOV_EXCL_STOP */
     525             :                 }
     526             :         }
     527             : #endif
     528             : 
     529             :         /* first try with the /dev/disk/by-uuid version */
     530             : #if HAVE_LINUX_DEVICE
     531        5626 :         if (devuuid_dev(device, uuid, uuid_size) == 0)
     532        5626 :                 return 0;
     533             : #else
     534             :         log_tag("uuid:by-uuidd:%u:%u: by-uuid not supported\n", major(device), minor(device));
     535             : #endif
     536             : 
     537             :         /* fall back to blkid for other cases */
     538             : #if HAVE_BLKID
     539           0 :         if (devuuid_blkid(device, uuid, uuid_size) == 0)
     540           0 :                 return 0;
     541             : #else
     542             :         log_tag("uuid:blkid:%u:%u: blkid not supported\n", major(device), minor(device));
     543             : #endif
     544             : 
     545           0 :         log_tag("uuid:notfound:%u:%u:\n", major(device), minor(device));
     546             : 
     547             :         /* not supported */
     548             :         (void)uuid;
     549             :         (void)uuid_size;
     550           0 :         return -1;
     551             : }
     552             : 
     553       11432 : int filephy(const char* path, uint64_t size, uint64_t* physical)
     554             : {
     555             : #if HAVE_LINUX_FIEMAP_H
     556             :         /* In Linux get the real physical address of the file */
     557             :         /* Note that FIEMAP doesn't require root permission */
     558             :         int f;
     559             :         struct {
     560             :                 struct fiemap fiemap;
     561             :                 struct fiemap_extent extent;
     562             :         } fm;
     563             :         unsigned int blknum;
     564             : 
     565       11432 :         f = open(path, O_RDONLY);
     566       11432 :         if (f == -1) {
     567           0 :                 return -1;
     568             :         }
     569             : 
     570             :         /* first try with FIEMAP */
     571             :         /* if works for ext2, ext3, ext4, xfs, btrfs */
     572       11432 :         memset(&fm, 0, sizeof(fm));
     573       11432 :         fm.fiemap.fm_start = 0;
     574       11432 :         fm.fiemap.fm_length = ~0ULL;
     575       11432 :         fm.fiemap.fm_flags = FIEMAP_FLAG_SYNC; /* required to ensure that just created files report a valid address and not 0 */
     576       11432 :         fm.fiemap.fm_extent_count = 1; /* we are interested only at the first block */
     577             : 
     578       11432 :         if (ioctl(f, FS_IOC_FIEMAP, &fm) != -1) {
     579       11432 :                 uint32_t flags = fm.fiemap.fm_extents[0].fe_flags;
     580       11432 :                 uint64_t offset = fm.fiemap.fm_extents[0].fe_physical;
     581             : 
     582             :                 /* check some condition for validating the offset */
     583       11432 :                 if (flags & FIEMAP_EXTENT_DATA_INLINE) {
     584             :                         /* if the data is inline, we don't have an offset to report */
     585           0 :                         *physical = FILEPHY_WITHOUT_OFFSET;
     586       11432 :                 } else if (flags & FIEMAP_EXTENT_UNKNOWN) {
     587             :                         /* if the offset is unknown, we don't have an offset to report */
     588           0 :                         *physical = FILEPHY_WITHOUT_OFFSET;
     589       11432 :                 } else if (offset == 0) {
     590             :                         /* 0 is the general fallback for file-systems when */
     591             :                         /* they don't have an offset to report */
     592          14 :                         *physical = FILEPHY_WITHOUT_OFFSET;
     593             :                 } else {
     594             :                         /* finally report the real offset */
     595       11418 :                         *physical = offset + FILEPHY_REAL_OFFSET;
     596             :                 }
     597             : 
     598       11432 :                 if (close(f) == -1)
     599           0 :                         return -1;
     600       11432 :                 return 0;
     601             :         }
     602             : 
     603             :         /* if the file is empty, FIBMAP doesn't work, and we don't even try to use it */
     604           0 :         if (size == 0) {
     605           0 :                 *physical = FILEPHY_WITHOUT_OFFSET;
     606           0 :                 if (close(f) == -1)
     607           0 :                         return -1;
     608           0 :                 return 0;
     609             :         }
     610             : 
     611             :         /* then try with FIBMAP */
     612             :         /* it works for jfs, reiserfs, ntfs-3g */
     613             :         /* in exfat it always returns 0, that it's anyway better than the fake inodes */
     614           0 :         blknum = 0; /* first block */
     615           0 :         if (ioctl(f, FIBMAP, &blknum) != -1) {
     616           0 :                 *physical = blknum + FILEPHY_REAL_OFFSET;
     617           0 :                 if (close(f) == -1)
     618           0 :                         return -1;
     619           0 :                 return 0;
     620             :         }
     621             : 
     622             :         /* otherwise don't use anything, and keep the directory traversal order */
     623             :         /* at now this should happen only for vfat */
     624             :         /* and it's surely better than using fake inodes */
     625           0 :         *physical = FILEPHY_UNREPORTED_OFFSET;
     626           0 :         if (close(f) == -1)
     627           0 :                 return -1;
     628             : #else
     629             :         /* In a generic Unix use a dummy value for all the files */
     630             :         /* We don't want to risk to use the inode without knowing */
     631             :         /* if it really improves performance. */
     632             :         /* In this way we keep them in the directory traversal order */
     633             :         /* that at least keeps files in the same directory together. */
     634             :         /* Note also that in newer file-system with snapshot, like ZFS, */
     635             :         /* the inode doesn't represent evenmore the disk position, because files */
     636             :         /* are not overwritten in place, but rewritten in another location */
     637             :         /* of the disk. */
     638             :         *physical = FILEPHY_UNREPORTED_OFFSET;
     639             : 
     640             :         (void)path; /* not used here */
     641             :         (void)size;
     642             : #endif
     643             : 
     644           0 :         return 0;
     645             : }
     646             : 
     647        5483 : int fsinfo(const char* path, int* has_persistent_inode, uint64_t* total_space, uint64_t* free_space)
     648             : {
     649             :         char type[64];
     650             :         const char* ptype;
     651             : 
     652             : #if HAVE_STATFS
     653             :         struct statfs st;
     654             : 
     655        5483 :         if (statfs(path, &st) != 0) {
     656             :                 char dir[PATH_MAX];
     657             :                 char* slash;
     658             : 
     659          24 :                 if (errno != ENOENT) {
     660           0 :                         return -1;
     661             :                 }
     662             : 
     663             :                 /* if it doesn't exist, we assume a file */
     664             :                 /* and we check for the containing dir */
     665          24 :                 if (strlen(path) + 1 > sizeof(dir)) {
     666           0 :                         errno = ENAMETOOLONG;
     667           0 :                         return -1;
     668             :                 }
     669             : 
     670          24 :                 strcpy(dir, path);
     671             : 
     672          24 :                 slash = strrchr(dir, '/');
     673          24 :                 if (!slash)
     674           0 :                         return -1;
     675             : 
     676          24 :                 *slash = 0;
     677          24 :                 if (statfs(dir, &st) != 0)
     678           0 :                         return -1;
     679             :         }
     680             : #endif
     681             : 
     682             :         /* to get the fs type check "man stat" or "stat -f -t FILE" */
     683        5483 :         if (has_persistent_inode) {
     684             : #if HAVE_STATFS && HAVE_STRUCT_STATFS_F_TYPE
     685         575 :                 switch (st.f_type) {
     686             :                 case 0x65735546 : /* FUSE, "fuseblk" in the stat command */
     687             :                 case 0x4d44 : /* VFAT, "msdos" in the stat command */
     688           0 :                         *has_persistent_inode = 0;
     689           0 :                         break;
     690             :                 default :
     691             :                         /* by default assume yes */
     692         575 :                         *has_persistent_inode = 1;
     693         575 :                         break;
     694             :                 }
     695             : #else
     696             :                 /* by default assume yes */
     697             :                 *has_persistent_inode = 1;
     698             : #endif
     699             :         }
     700             : 
     701        5483 :         if (total_space) {
     702             : #if HAVE_STATFS
     703        4908 :                 *total_space = st.f_bsize * (uint64_t)st.f_blocks;
     704             : #else
     705             :                 *total_space = 0;
     706             : #endif
     707             :         }
     708             : 
     709        5483 :         if (free_space) {
     710             : #if HAVE_STATFS
     711        4908 :                 *free_space = st.f_bsize * (uint64_t)st.f_bfree;
     712             : #else
     713             :                 *free_space = 0;
     714             : #endif
     715             :         }
     716             : 
     717             : #if HAVE_STATFS && HAVE_STRUCT_STATFS_F_FSTYPENAME
     718             :         /* get the filesystem type directly from the struct (Mac OS X) */
     719             :         (void)type;
     720             :         ptype = st.f_fstypename;
     721             : #elif HAVE_STATFS && HAVE_STRUCT_STATFS_F_TYPE
     722             :         /* get the filesystem type from f_type (Linux) */
     723             :         /* from: https://github.com/influxdata/gopsutil/blob/master/disk/disk_linux.go */
     724        5483 :         switch (st.f_type) {
     725           0 :         case 0x65735546 : ptype = "fuseblk"; break;
     726           0 :         case 0x4D44 : ptype = "vfat/msdos"; break;
     727        5483 :         case 0xEF53 : ptype = "ext2/3/4"; break;
     728           0 :         case 0x6969 : ptype = "nfs"; break; /* remote */
     729           0 :         case 0x6E667364 : ptype = "nfsd"; break; /* remote */
     730           0 :         case 0x517B : ptype = "smb"; break; /* remote */
     731           0 :         case 0x5346544E : ptype = "ntfs"; break;
     732           0 :         case 0x52654973 : ptype = "reiserfs"; break;
     733           0 :         case 0x3153464A : ptype = "jfs"; break;
     734           0 :         case 0x58465342 : ptype = "xfs"; break;
     735           0 :         case 0x9123683E : ptype = "btrfs"; break;
     736           0 :         case 0x2FC12FC1 : ptype = "zfs"; break;
     737             :         default :
     738           0 :                 snprintf(type, sizeof(type), "0x%X", (unsigned)st.f_type);
     739           0 :                 ptype = type;
     740             :         }
     741             : #else
     742             :         (void)type;
     743             :         ptype = "unknown";
     744             : #endif
     745             : 
     746        5483 :         log_tag("statfs:%s: %s \n", ptype, path);
     747             : 
     748        5483 :         return 0;
     749             : }
     750             : 
     751     3819097 : uint64_t tick(void)
     752             : {
     753             : #if HAVE_MACH_ABSOLUTE_TIME
     754             :         /* for Mac OS X */
     755             :         return mach_absolute_time();
     756             : #elif HAVE_CLOCK_GETTIME && (defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW))
     757             :         /* for Linux */
     758             :         struct timespec tv;
     759             : 
     760             :         /* nanosecond precision with clock_gettime() */
     761             : #if defined(CLOCK_MONOTONIC_RAW)
     762     3819097 :         if (clock_gettime(CLOCK_MONOTONIC_RAW, &tv) != 0) {
     763             : #else
     764             :         if (clock_gettime(CLOCK_MONOTONIC, &tv) != 0) {
     765             : #endif
     766           0 :                 return 0;
     767             :         }
     768             : 
     769     3819097 :         return tv.tv_sec * 1000000000ULL + tv.tv_nsec;
     770             : #else
     771             :         /* other platforms */
     772             :         struct timeval tv;
     773             : 
     774             :         /* microsecond precision with gettimeofday() */
     775             :         if (gettimeofday(&tv, 0) != 0) {
     776             :                 return 0;
     777             :         }
     778             : 
     779             :         return tv.tv_sec * 1000000ULL + tv.tv_usec;
     780             : #endif
     781             : }
     782             : 
     783          65 : uint64_t tick_ms(void)
     784             : {
     785             :         struct timeval tv;
     786             : 
     787          65 :         if (gettimeofday(&tv, 0) != 0)
     788           0 :                 return 0;
     789             : 
     790          63 :         return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
     791             : }
     792             : 
     793         282 : int randomize(void* ptr, size_t size)
     794             : {
     795             :         int f;
     796             :         ssize_t ret;
     797             : 
     798         282 :         f = open("/dev/urandom", O_RDONLY);
     799         282 :         if (f == -1)
     800           0 :                 return -1;
     801             : 
     802         282 :         ret = read(f, ptr, size);
     803         282 :         if (ret < 0 || (size_t)ret != size) {
     804           0 :                 close(f);
     805           0 :                 return -1;
     806             :         }
     807             : 
     808         282 :         if (close(f) != 0)
     809           0 :                 return -1;
     810             : 
     811         282 :         return 0;
     812             : }
     813             : 
     814             : /**
     815             :  * Read a file extracting the contained device number in %u:%u format.
     816             :  * Return 0 on error.
     817             :  */
     818             : #if HAVE_LINUX_DEVICE
     819         144 : static dev_t devread(const char* path)
     820             : {
     821             :         int f;
     822             :         int ret;
     823             :         int len;
     824             :         char buf[64];
     825             :         char* e;
     826             :         unsigned ma;
     827             :         unsigned mi;
     828             : 
     829         144 :         f = open(path, O_RDONLY);
     830         144 :         if (f == -1) {
     831             :                 /* LCOV_EXCL_START */
     832             :                 log_fatal("Failed to open '%s'.\n", path);
     833             :                 return 0;
     834             :                 /* LCOV_EXCL_STOP */
     835             :         }
     836             : 
     837         144 :         len = read(f, buf, sizeof(buf));
     838         144 :         if (len < 0) {
     839             :                 /* LCOV_EXCL_START */
     840             :                 close(f);
     841             :                 log_fatal("Failed to read '%s'.\n", path);
     842             :                 return 0;
     843             :                 /* LCOV_EXCL_STOP */
     844             :         }
     845         144 :         if (len == sizeof(buf)) {
     846             :                 /* LCOV_EXCL_START */
     847             :                 close(f);
     848             :                 log_fatal("Too long read '%s'.\n", path);
     849             :                 return 0;
     850             :                 /* LCOV_EXCL_STOP */
     851             :         }
     852             : 
     853         144 :         ret = close(f);
     854         144 :         if (ret != 0) {
     855             :                 /* LCOV_EXCL_START */
     856             :                 log_fatal("Failed to close '%s'.\n", path);
     857             :                 return 0;
     858             :                 /* LCOV_EXCL_STOP */
     859             :         }
     860             : 
     861         144 :         buf[len] = 0;
     862             : 
     863         144 :         ma = strtoul(buf, &e, 10);
     864         144 :         if (*e != ':') {
     865             :                 /* LCOV_EXCL_START */
     866             :                 log_fatal("Invalid format in '%s' for '%s'.\n", path, buf);
     867             :                 return 0;
     868             :                 /* LCOV_EXCL_STOP */
     869             :         }
     870             : 
     871         144 :         mi = strtoul(e + 1, &e, 10);
     872         144 :         if (*e != 0 && !isspace(*e)) {
     873             :                 /* LCOV_EXCL_START */
     874             :                 log_fatal("Invalid format in '%s' for '%s'.\n", path, buf);
     875             :                 return 0;
     876             :                 /* LCOV_EXCL_STOP */
     877             :         }
     878             : 
     879         144 :         return makedev(ma, mi);
     880             : }
     881             : #endif
     882             : 
     883             : /**
     884             :  * Read a device tree filling the specified list of disk_t entries.
     885             :  */
     886             : #if HAVE_LINUX_DEVICE
     887         130 : static int devtree(const char* name, const char* custom, dev_t device, devinfo_t* parent, tommy_list* list)
     888             : {
     889             :         char path[PATH_MAX];
     890             :         DIR* d;
     891         130 :         int slaves = 0;
     892             : 
     893         130 :         pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/slaves", major(device), minor(device));
     894             : 
     895             :         /* check if there is a slaves list */
     896         130 :         d = opendir(path);
     897         130 :         if (d != 0) {
     898             :                 struct dirent* dd;
     899             : 
     900         325 :                 while ((dd = readdir(d)) != 0) {
     901         195 :                         if (dd->d_name[0] != '.') {
     902             :                                 /* for each slave, expand the full potential tree */
     903          65 :                                 pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/slaves/%s/dev", major(device), minor(device), dd->d_name);
     904             : 
     905          65 :                                 device = devread(path);
     906          65 :                                 if (!device) {
     907             :                                         /* LCOV_EXCL_START */
     908             :                                         closedir(d);
     909             :                                         return -1;
     910             :                                         /* LCOV_EXCL_STOP */
     911             :                                 }
     912             : 
     913          65 :                                 if (devtree(name, custom, device, parent, list) != 0) {
     914             :                                         /* LCOV_EXCL_START */
     915             :                                         closedir(d);
     916             :                                         return -1;
     917             :                                         /* LCOV_EXCL_STOP */
     918             :                                 }
     919             : 
     920          65 :                                 ++slaves;
     921             :                         }
     922             :                 }
     923             : 
     924          65 :                 closedir(d);
     925             :         }
     926             : 
     927             :         /* if no slaves found */
     928         130 :         if (!slaves) {
     929             :                 /* this is a raw device */
     930             :                 devinfo_t* devinfo;
     931             : 
     932             :                 /* check if it's a real device */
     933          65 :                 pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/device", major(device), minor(device));
     934          65 :                 if (access(path, F_OK) != 0) {
     935             :                         /* get the parent device */
     936          65 :                         pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/../dev", major(device), minor(device));
     937             : 
     938          65 :                         device = devread(path);
     939          65 :                         if (!device) {
     940             :                                 /* LCOV_EXCL_START */
     941             :                                 return -1;
     942             :                                 /* LCOV_EXCL_STOP */
     943             :                         }
     944             :                 }
     945             : 
     946             :                 /* get the device file */
     947          65 :                 if (devresolve(device, path, sizeof(path)) != 0) {
     948             :                         /* LCOV_EXCL_START */
     949             :                         log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
     950             :                         return -1;
     951             :                         /* LCOV_EXCL_STOP */
     952             :                 }
     953             : 
     954          65 :                 devinfo = calloc_nofail(1, sizeof(devinfo_t));
     955             : 
     956          65 :                 devinfo->device = device;
     957          65 :                 pathcpy(devinfo->name, sizeof(devinfo->name), name);
     958          65 :                 pathcpy(devinfo->smartctl, sizeof(devinfo->smartctl), custom);
     959          65 :                 pathcpy(devinfo->file, sizeof(devinfo->file), path);
     960          65 :                 devinfo->parent = parent;
     961             : 
     962             :                 /* insert in the list */
     963          65 :                 tommy_list_insert_tail(list, &devinfo->node, devinfo);
     964             :         }
     965             : 
     966         130 :         return 0;
     967             : }
     968             : #endif
     969             : 
     970             : /**
     971             :  * Scan all the devices.
     972             :  *
     973             :  * If a device is already in, it's not added again.
     974             :  */
     975             : #if HAVE_LINUX_DEVICE
     976           1 : static int devscan(tommy_list* list)
     977             : {
     978             :         char dir[PATH_MAX];
     979             :         DIR* d;
     980             :         struct dirent* dd;
     981             : 
     982           1 :         pathprint(dir, sizeof(dir), "/sys/dev/block/");
     983             : 
     984             :         /* check if there is a slaves list */
     985           1 :         d = opendir(dir);
     986           1 :         if (d == 0) {
     987             :                 /* LCOV_EXCL_START */
     988             :                 log_fatal("Failed to open dir '%s'.\n", dir);
     989             :                 return -1;
     990             :                 /* LCOV_EXCL_STOP */
     991             :         }
     992             : 
     993          74 :         while ((dd = readdir(d)) != 0) {
     994             :                 char path[PATH_MAX];
     995             :                 tommy_node* i;
     996             :                 dev_t device;
     997             :                 devinfo_t* devinfo;
     998             : 
     999          72 :                 if (dd->d_name[0] == '.')
    1000          61 :                         continue;
    1001             : 
    1002          70 :                 pathprint(path, sizeof(path), "/sys/dev/block/%s/device", dd->d_name);
    1003             : 
    1004             :                 /* check if it's a real device */
    1005          70 :                 if (access(path, F_OK) != 0)
    1006          56 :                         continue;
    1007             : 
    1008          14 :                 pathprint(path, sizeof(path), "/sys/dev/block/%s/dev", dd->d_name);
    1009             : 
    1010          14 :                 device = devread(path);
    1011          14 :                 if (!device) {
    1012             :                         /* LCOV_EXCL_START */
    1013             :                         log_tag("scan:skip: Skipping device %s because failed to read its device number.\n", dd->d_name);
    1014             :                         continue;
    1015             :                         /* LCOV_EXCL_STOP */
    1016             :                 }
    1017             : 
    1018             :                 /* check if already present */
    1019         482 :                 for (i = tommy_list_head(list); i != 0; i = i->next) {
    1020         469 :                         devinfo = i->data;
    1021         469 :                         if (devinfo->device == device)
    1022           1 :                                 break;
    1023             :                 }
    1024             : 
    1025             :                 /* if already present */
    1026          14 :                 if (i != 0)
    1027           1 :                         continue;
    1028             : 
    1029             :                 /* get the device file */
    1030          13 :                 if (devresolve(device, path, sizeof(path)) != 0) {
    1031             :                         /* LCOV_EXCL_START */
    1032             :                         log_tag("scan:skip: Skipping device %u:%u because failed to resolve.\n", major(device), minor(device));
    1033             :                         continue;
    1034             :                         /* LCOV_EXCL_STOP */
    1035             :                 }
    1036             : 
    1037          13 :                 devinfo = calloc_nofail(1, sizeof(devinfo_t));
    1038             : 
    1039          13 :                 devinfo->device = device;
    1040          13 :                 pathcpy(devinfo->file, sizeof(devinfo->file), path);
    1041             : 
    1042             :                 /* insert in the list */
    1043          13 :                 tommy_list_insert_tail(list, &devinfo->node, devinfo);
    1044             :         }
    1045             : 
    1046           1 :         closedir(d);
    1047           1 :         return 0;
    1048             : }
    1049             : #endif
    1050             : 
    1051             : /**
    1052             :  * Get SMART attributes.
    1053             :  */
    1054             : #if HAVE_LINUX_DEVICE
    1055          42 : static int devsmart(dev_t device, const char* name, const char* custom, uint64_t* smart, char* serial, char* vendor, char* model)
    1056             : {
    1057             :         char cmd[128];
    1058             :         char file[128];
    1059             :         FILE* f;
    1060             :         int ret;
    1061             : 
    1062          42 :         if (devresolve(device, file, sizeof(file)) != 0) {
    1063             :                 /* LCOV_EXCL_START */
    1064             :                 log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
    1065             :                 return -1;
    1066             :                 /* LCOV_EXCL_STOP */
    1067             :         }
    1068             : 
    1069             :         /* if there is a custom command */
    1070          43 :         if (custom[0]) {
    1071             :                 char option[128];
    1072           5 :                 snprintf(option, sizeof(option), custom, file);
    1073           5 :                 snprintf(cmd, sizeof(cmd), "smartctl -a %s", option);
    1074             :         } else {
    1075          38 :                 snprintf(cmd, sizeof(cmd), "smartctl -a %s", file);
    1076             :         }
    1077             : 
    1078          43 :         log_tag("smartctl:%s:%s:run: %s\n", file, name, cmd);
    1079             : 
    1080          43 :         f = popen(cmd, "r");
    1081          42 :         if (!f) {
    1082             :                 /* LCOV_EXCL_START */
    1083             :                 log_fatal("Failed to run '%s' (from popen).\n", cmd);
    1084             :                 return -1;
    1085             :                 /* LCOV_EXCL_STOP */
    1086             :         }
    1087             : 
    1088          42 :         if (smartctl_attribute(f, file, name, smart, serial, vendor, model) != 0) {
    1089             :                 /* LCOV_EXCL_START */
    1090             :                 pclose(f);
    1091             :                 return -1;
    1092             :                 /* LCOV_EXCL_STOP */
    1093             :         }
    1094             : 
    1095          42 :         ret = pclose(f);
    1096             : 
    1097          43 :         log_tag("smartctl:%s:%s:ret: %x\n", file, name, ret);
    1098             : 
    1099          43 :         if (!WIFEXITED(ret)) {
    1100             :                 /* LCOV_EXCL_START */
    1101             :                 log_fatal("Failed to run '%s' (not exited).\n", cmd);
    1102             :                 return -1;
    1103             :                 /* LCOV_EXCL_STOP */
    1104             :         }
    1105          43 :         if (WEXITSTATUS(ret) == 127) {
    1106             :                 /* LCOV_EXCL_START */
    1107             :                 log_fatal("Failed to run '%s' (from sh).\n", cmd);
    1108             :                 return -1;
    1109             :                 /* LCOV_EXCL_STOP */
    1110             :         }
    1111             : 
    1112             :         /* store the return smartctl return value */
    1113           0 :         smart[SMART_FLAGS] = WEXITSTATUS(ret);
    1114             : 
    1115           0 :         return 0;
    1116             : }
    1117             : #endif
    1118             : 
    1119             : /**
    1120             :  * Spin down a specific device.
    1121             :  */
    1122             : #if HAVE_LINUX_DEVICE
    1123           5 : static int devdown(dev_t device, const char* name, const char* custom)
    1124             : {
    1125             :         char cmd[128];
    1126             :         char file[128];
    1127             :         FILE* f;
    1128             :         int ret;
    1129             : 
    1130           5 :         if (devresolve(device, file, sizeof(file)) != 0) {
    1131             :                 /* LCOV_EXCL_START */
    1132             :                 log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
    1133             :                 return -1;
    1134             :                 /* LCOV_EXCL_STOP */
    1135             :         }
    1136             : 
    1137             :         /* if there is a custom command */
    1138           5 :         if (custom[0]) {
    1139             :                 char option[128];
    1140           5 :                 snprintf(option, sizeof(option), custom, file);
    1141           5 :                 snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", option);
    1142             :         } else {
    1143           0 :                 snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", file);
    1144             :         }
    1145             : 
    1146           5 :         log_tag("smartctl:%s:%s:run: %s\n", file, name, cmd);
    1147             : 
    1148           5 :         f = popen(cmd, "r");
    1149           5 :         if (!f) {
    1150             :                 /* LCOV_EXCL_START */
    1151             :                 log_fatal("Failed to run '%s' (from popen).\n", cmd);
    1152             :                 return -1;
    1153             :                 /* LCOV_EXCL_STOP */
    1154             :         }
    1155             : 
    1156           5 :         if (smartctl_flush(f, file, name) != 0) {
    1157             :                 /* LCOV_EXCL_START */
    1158             :                 pclose(f);
    1159             :                 return -1;
    1160             :                 /* LCOV_EXCL_STOP */
    1161             :         }
    1162             : 
    1163           5 :         ret = pclose(f);
    1164             : 
    1165           5 :         log_tag("smartctl:%s:%s:ret: %x\n", file, name, ret);
    1166             : 
    1167           5 :         if (!WIFEXITED(ret)) {
    1168             :                 /* LCOV_EXCL_START */
    1169             :                 log_fatal("Failed to run '%s' (not exited).\n", cmd);
    1170             :                 return -1;
    1171             :                 /* LCOV_EXCL_STOP */
    1172             :         }
    1173           5 :         if (WEXITSTATUS(ret) == 127) {
    1174             :                 /* LCOV_EXCL_START */
    1175             :                 log_fatal("Failed to run '%s' (from sh).\n", cmd);
    1176             :                 return -1;
    1177             :                 /* LCOV_EXCL_STOP */
    1178             :         }
    1179           0 :         if (WEXITSTATUS(ret) != 0) {
    1180             :                 /* LCOV_EXCL_START */
    1181             :                 log_fatal("Failed to run '%s' with return code %xh.\n", cmd, WEXITSTATUS(ret));
    1182             :                 return -1;
    1183             :                 /* LCOV_EXCL_STOP */
    1184             :         }
    1185             : 
    1186           0 :         return 0;
    1187             : }
    1188             : #endif
    1189             : 
    1190             : /**
    1191             :  * Spin up a device.
    1192             :  *
    1193             :  * There isn't a defined way to spin up a device,
    1194             :  * so we just do a generic write.
    1195             :  */
    1196          30 : static int devup(const char* mountpoint)
    1197             : {
    1198             :         int ret;
    1199             :         char path[PATH_MAX];
    1200             : 
    1201             :         /* add a temporary name used for writing */
    1202          30 :         pathprint(path, sizeof(path), "%s.snapraid-spinup", mountpoint);
    1203             : 
    1204             :         /* do a generic write, and immediately undo it */
    1205          30 :         ret = mkdir(path, 0);
    1206          30 :         if (ret != 0 && errno != EEXIST) {
    1207             :                 /* LCOV_EXCL_START */
    1208             :                 log_fatal("Failed to create dir '%s'.\n", path);
    1209             :                 return -1;
    1210             :                 /* LCOV_EXCL_STOP */
    1211             :         }
    1212             : 
    1213             :         /* remove the just created dir */
    1214          30 :         rmdir(path);
    1215             : 
    1216          30 :         return 0;
    1217             : }
    1218             : 
    1219             : /**
    1220             :  * Thread for spinning up.
    1221             :  *
    1222             :  * Note that filling up the devinfo object is done inside this thread,
    1223             :  * to avoid to block the main thread if the device need to be spin up
    1224             :  * to handle stat/resolve requests.
    1225             :  */
    1226          30 : static void* thread_spinup(void* arg)
    1227             : {
    1228          30 :         devinfo_t* devinfo = arg;
    1229             :         struct stat st;
    1230             :         uint64_t start;
    1231             : 
    1232          30 :         start = tick_ms();
    1233             : 
    1234             :         /* first get the device number, this usually doesn't trigger a thread_spinup */
    1235          30 :         if (stat(devinfo->mount, &st) != 0) {
    1236             :                 /* LCOV_EXCL_START */
    1237             :                 log_fatal("Failed to stat device '%s'.\n", devinfo->mount);
    1238             :                 return (void*)-1;
    1239             :                 /* LCOV_EXCL_STOP */
    1240             :         }
    1241             : 
    1242             :         /* set the device number for printing */
    1243          30 :         devinfo->device = st.st_dev;
    1244             : 
    1245          30 :         if (devup(devinfo->mount) != 0) {
    1246             :                 /* LCOV_EXCL_START */
    1247             :                 return (void*)-1;
    1248             :                 /* LCOV_EXCL_STOP */
    1249             :         }
    1250             : 
    1251          30 :         msg_status("Spunup device '%u:%u' for disk '%s' in %" PRIu64 " ms.\n", major(devinfo->device), minor(devinfo->device), devinfo->name, tick_ms() - start);
    1252             : 
    1253          30 :         return 0;
    1254             : }
    1255             : 
    1256             : /**
    1257             :  * Thread for spinning down.
    1258             :  */
    1259           5 : static void* thread_spindown(void* arg)
    1260             : {
    1261             : #if HAVE_LINUX_DEVICE
    1262           5 :         devinfo_t* devinfo = arg;
    1263             :         uint64_t start;
    1264             : 
    1265           5 :         start = tick_ms();
    1266             : 
    1267           5 :         if (devdown(devinfo->device, devinfo->name, devinfo->smartctl) != 0) {
    1268             :                 /* LCOV_EXCL_START */
    1269             :                 return (void*)-1;
    1270             :                 /* LCOV_EXCL_STOP */
    1271             :         }
    1272             : 
    1273           0 :         msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
    1274             : 
    1275           0 :         return 0;
    1276             : #else
    1277             :         (void)arg;
    1278             :         return (void*)-1;
    1279             : #endif
    1280             : }
    1281             : 
    1282             : /**
    1283             :  * Thread for getting smart info.
    1284             :  */
    1285          42 : static void* thread_smart(void* arg)
    1286             : {
    1287             : #if HAVE_LINUX_DEVICE
    1288          42 :         devinfo_t* devinfo = arg;
    1289             : 
    1290          42 :         if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->smart_serial, devinfo->smart_vendor, devinfo->smart_model) != 0) {
    1291             :                 /* LCOV_EXCL_START */
    1292             :                 return (void*)-1;
    1293             :                 /* LCOV_EXCL_STOP */
    1294             :         }
    1295             : 
    1296           0 :         return 0;
    1297             : #else
    1298             :         (void)arg;
    1299             :         return (void*)-1;
    1300             : #endif
    1301             : }
    1302             : 
    1303           3 : static int device_thread(tommy_list* list, void* (*func)(void* arg))
    1304             : {
    1305           3 :         int fail = 0;
    1306             :         tommy_node* i;
    1307             : 
    1308             : #if HAVE_PTHREAD
    1309             :         /* start all threads */
    1310          81 :         for (i = tommy_list_head(list); i != 0; i = i->next) {
    1311          78 :                 devinfo_t* devinfo = i->data;
    1312             : 
    1313          78 :                 thread_create(&devinfo->thread, 0, func, devinfo);
    1314             :         }
    1315             : 
    1316             :         /* join all threads */
    1317          81 :         for (i = tommy_list_head(list); i != 0; i = i->next) {
    1318          78 :                 devinfo_t* devinfo = i->data;
    1319             :                 void* retval;
    1320             : 
    1321          78 :                 thread_join(devinfo->thread, &retval);
    1322             : 
    1323          78 :                 if (retval != 0)
    1324          48 :                         ++fail;
    1325             :         }
    1326             : #else
    1327             :         for (i = tommy_list_head(list); i != 0; i = i->next) {
    1328             :                 devinfo_t* devinfo = i->data;
    1329             : 
    1330             :                 if (func(devinfo) != 0)
    1331             :                         ++fail;
    1332             :         }
    1333             : #endif
    1334           3 :         if (fail != 0) {
    1335             :                 /* LCOV_EXCL_START */
    1336             :                 return -1;
    1337             :                 /* LCOV_EXCL_STOP */
    1338             :         }
    1339             : 
    1340           1 :         return 0;
    1341             : }
    1342             : 
    1343           4 : int devquery(tommy_list* high, tommy_list* low, int operation, int others)
    1344             : {
    1345             :         tommy_node* i;
    1346           4 :         void* (*func)(void* arg) = 0;
    1347             : 
    1348             : #if HAVE_LINUX_DEVICE
    1349           4 :         if (operation != DEVICE_UP) {
    1350             :                 struct stat st;
    1351             :                 /* sysfs interface is required */
    1352           3 :                 if (stat("/sys/dev/block", &st) != 0) {
    1353             :                         /* LCOV_EXCL_START */
    1354             :                         log_fatal("Missing interface /sys/dev/block.\n");
    1355             :                         return -1;
    1356             :                         /* LCOV_EXCL_STOP */
    1357             :                 }
    1358             : 
    1359             :                 /* for each device */
    1360         136 :                 for (i = tommy_list_head(high); i != 0; i = i->next) {
    1361          65 :                         devinfo_t* devinfo = i->data;
    1362          65 :                         uint64_t device = devinfo->device;
    1363             : 
    1364             :                         /* if the major is the null device, find the real one */
    1365          65 :                         if (major(device) == 0) {
    1366             :                                 /* obtain the real device */
    1367           0 :                                 if (devdereference(device, &device) != 0) {
    1368             :                                         /* LCOV_EXCL_START */
    1369             :                                         log_fatal("Failed to dereference device '%u:%u'.\n", major(device), minor(device));
    1370             :                                         return -1;
    1371             :                                         /* LCOV_EXCL_STOP */
    1372             :                                 }
    1373             :                         }
    1374             : 
    1375             :                         /* get the device file */
    1376          65 :                         if (devresolve(device, devinfo->file, sizeof(devinfo->file)) != 0) {
    1377             :                                 /* LCOV_EXCL_START */
    1378             :                                 log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
    1379             :                                 return -1;
    1380             :                                 /* LCOV_EXCL_STOP */
    1381             :                         }
    1382             : 
    1383             :                         /* expand the tree of devices */
    1384          65 :                         if (devtree(devinfo->name, devinfo->smartctl, device, devinfo, low) != 0) {
    1385             :                                 /* LCOV_EXCL_START */
    1386             :                                 log_fatal("Failed to expand device '%u:%u'.\n", major(device), minor(device));
    1387             :                                 return -1;
    1388             :                                 /* LCOV_EXCL_STOP */
    1389             :                         }
    1390             :                 }
    1391             :         }
    1392             : #endif
    1393             : 
    1394           4 :         if (operation == DEVICE_UP) {
    1395             :                 /* duplicate the high */
    1396          31 :                 for (i = tommy_list_head(high); i != 0; i = i->next) {
    1397          30 :                         devinfo_t* devinfo = i->data;
    1398             :                         devinfo_t* entry;
    1399             : 
    1400          30 :                         entry = calloc_nofail(1, sizeof(devinfo_t));
    1401             : 
    1402          30 :                         entry->device = devinfo->device;
    1403          30 :                         pathcpy(entry->name, sizeof(entry->name), devinfo->name);
    1404          30 :                         pathcpy(entry->mount, sizeof(entry->mount), devinfo->mount);
    1405             : 
    1406             :                         /* insert in the high */
    1407          30 :                         tommy_list_insert_tail(low, &entry->node, entry);
    1408             :                 }
    1409             :         }
    1410             : 
    1411             : #if HAVE_LINUX_DEVICE
    1412             :         /* add other devices */
    1413           4 :         if (others) {
    1414           1 :                 if (devscan(low) != 0) {
    1415             :                         /* LCOV_EXCL_START */
    1416             :                         log_fatal("Failed to list other devices.\n");
    1417             :                         return -1;
    1418             :                         /* LCOV_EXCL_STOP */
    1419             :                 }
    1420             :         }
    1421             : #else
    1422             :         (void)others;
    1423             : #endif
    1424             : 
    1425           4 :         switch (operation) {
    1426           1 :         case DEVICE_UP : func = thread_spinup; break;
    1427           1 :         case DEVICE_DOWN : func = thread_spindown; break;
    1428           1 :         case DEVICE_SMART : func = thread_smart; break;
    1429             :         }
    1430             : 
    1431           4 :         if (!func)
    1432           1 :                 return 0;
    1433             : 
    1434           3 :         return device_thread(low, func);
    1435             : }
    1436             : 
    1437         271 : void os_init(int opt)
    1438             : {
    1439             : #if HAVE_BLKID
    1440             :         int ret;
    1441         271 :         ret = blkid_get_cache(&cache, NULL);
    1442         271 :         if (ret != 0) {
    1443             :                 /* LCOV_EXCL_START */
    1444             :                 log_fatal("WARNING Failed to get blkid cache\n");
    1445             :                 /* LCOV_EXCL_STOP */
    1446             :         }
    1447             : #endif
    1448             : 
    1449             :         (void)opt;
    1450         271 : }
    1451             : 
    1452         248 : void os_done(void)
    1453             : {
    1454             : #if HAVE_BLKID
    1455         248 :         if (cache != 0)
    1456         248 :                 blkid_put_cache(cache);
    1457             : #endif
    1458         248 : }
    1459             : 
    1460             : /* LCOV_EXCL_START */
    1461             : void os_abort(void)
    1462             : {
    1463             : #if HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS
    1464             :         void* stack[32];
    1465             :         char** messages;
    1466             :         size_t size;
    1467             :         unsigned i;
    1468             : #endif
    1469             : 
    1470             :         printf("Stacktrace of " PACKAGE " v" VERSION);
    1471             : #ifdef _linux
    1472             :         printf(", linux");
    1473             : #endif
    1474             : #ifdef __GNUC__
    1475             :         printf(", gcc " __VERSION__);
    1476             : #endif
    1477             :         printf(", %d-bit", (int)sizeof(void *) * 8);
    1478             :         printf(", PATH_MAX=%d", PATH_MAX);
    1479             :         printf("\n");
    1480             : 
    1481             : #if HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS
    1482             :         size = backtrace(stack, 32);
    1483             : 
    1484             :         messages = backtrace_symbols(stack, size);
    1485             : 
    1486             :         for (i = 1; i < size; ++i) {
    1487             :                 const char* msg;
    1488             : 
    1489             :                 if (messages)
    1490             :                         msg = messages[i];
    1491             :                 else
    1492             :                         msg = "<unknown>";
    1493             : 
    1494             :                 printf("[bt] %02u: %s\n", i, msg);
    1495             : 
    1496             :                 if (messages) {
    1497             :                         int ret;
    1498             :                         char addr2line[1024];
    1499             :                         size_t j = 0;
    1500             :                         while (msg[j] != '(' && msg[j] != ' ' && msg[j] != 0)
    1501             :                                 ++j;
    1502             : 
    1503             :                         snprintf(addr2line, sizeof(addr2line), "addr2line %p -e %.*s", stack[i], (unsigned)j, msg);
    1504             : 
    1505             :                         ret = system(addr2line);
    1506             :                         if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0)
    1507             :                                 printf("exit:%d\n", WEXITSTATUS(ret));
    1508             :                         if (WIFSIGNALED(ret))
    1509             :                                 printf("signal:%d\n", WTERMSIG(ret));
    1510             :                 }
    1511             :         }
    1512             : #endif
    1513             : 
    1514             :         printf("Please report this error to the SnapRAID Forum:\n");
    1515             :         printf("https://sourceforge.net/p/snapraid/discussion/1677233/\n");
    1516             : 
    1517             :         abort();
    1518             : }
    1519             : /* LCOV_EXCL_STOP */
    1520             : 
    1521           0 : void os_clear(void)
    1522             : {
    1523             :         /* ANSI codes */
    1524           0 :         printf("\033[H"); /* cursor at topleft */
    1525           0 :         printf("\033[2J"); /* clear screen */
    1526           0 : }
    1527             : 
    1528           7 : size_t direct_size(void)
    1529             : {
    1530             :         long size;
    1531             : 
    1532           7 :         size = sysconf(_SC_PAGESIZE);
    1533             : 
    1534           7 :         if (size == -1) {
    1535             :                 /* LCOV_EXCL_START */
    1536             :                 log_fatal("No page size\n");
    1537             :                 exit(EXIT_FAILURE);
    1538             :                 /* LCOV_EXCL_STOP */
    1539             :         }
    1540             : 
    1541           7 :         return size;
    1542             : }
    1543             : 
    1544             : #endif
    1545             : 

Generated by: LCOV version 1.10