LCOV - code coverage report
Current view: top level - cmdline - unix.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 227 289 78.5 %
Date: 2015-05-12 Functions: 21 23 91.3 %

          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     1721022 : int open_noatime(const char* file, int flags)
      32             : {
      33             : #ifdef O_NOATIME
      34     1721022 :         int f = open(file, flags | O_NOATIME);
      35             : 
      36             :         /* only root is allowed to use O_NOATIME, in case retry without it */
      37     1721022 :         if (f == -1 && errno == EPERM)
      38           0 :                 f = open(file, flags);
      39     1721022 :         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             : #if HAVE_FSTATAT
      72             : /**
      73             :  * This function get the UUID using the /dev/disk/by-uuid/ links.
      74             :  * Comparing with the libblkid library, it works also from not root processes.
      75             :  */
      76        2129 : int devuuid(uint64_t device, char* uuid, size_t uuid_size)
      77             : {
      78             :         int ret;
      79             :         DIR* d;
      80             :         struct dirent* dd;
      81             :         struct stat st;
      82             : 
      83             :         /* scan the UUID directory searching for the device */
      84        2129 :         d = opendir("/dev/disk/by-uuid");
      85        2129 :         if (!d) {
      86             :                 /* directory missing?, likely we are not in Linux */
      87           0 :                 return -1;
      88             :         }
      89             : 
      90       36193 :         while ((dd = readdir(d)) != 0) {
      91             :                 /* skip "." and ".." files, UUIDs never start with '.' */
      92       34064 :                 if (dd->d_name[0] == '.')
      93        4258 :                         continue;
      94             : 
      95       29806 :                 ret = fstatat(dirfd(d), dd->d_name, &st, 0);
      96       29806 :                 if (ret != 0) {
      97             :                         /* generic error, ignore and continue the search */
      98           0 :                         continue;
      99             :                 }
     100             : 
     101             :                 /* if it matches, we have the uuid */
     102       29806 :                 if (S_ISBLK(st.st_mode) && st.st_rdev == (dev_t)device) {
     103             :                         /* found */
     104        2129 :                         pathcpy(uuid, uuid_size, dd->d_name);
     105        2129 :                         closedir(d);
     106        2129 :                         return 0;
     107             :                 }
     108             :         }
     109             : 
     110             :         /* not found */
     111           0 :         closedir(d);
     112           0 :         return -1;
     113             : }
     114             : #else
     115             : int devuuid(uint64_t device, char* uuid, size_t uuid_size)
     116             : {
     117             :         (void)device;
     118             :         (void)uuid;
     119             :         (void)uuid_size;
     120             : 
     121             :         /* not supported */
     122             :         return -1;
     123             : }
     124             : #endif
     125             : 
     126       11430 : int filephy(const char* path, uint64_t size, uint64_t* physical)
     127             : {
     128             : #if HAVE_LINUX_FIEMAP_H
     129             :         /* In Linux gets the real physical address of the file */
     130             :         /* Note that FIEMAP doesn't require root permission */
     131             :         int f;
     132             :         struct {
     133             :                 struct fiemap fiemap;
     134             :                 struct fiemap_extent extent;
     135             :         } fm;
     136             :         unsigned int blknum;
     137             : 
     138       11430 :         f = open(path, O_RDONLY);
     139       11430 :         if (f == -1) {
     140           0 :                 return -1;
     141             :         }
     142             : 
     143             :         /* first try with FIEMAP */
     144             :         /* if works for ext2, ext3, ext4, xfs, btrfs */
     145       11430 :         memset(&fm, 0, sizeof(fm));
     146       11430 :         fm.fiemap.fm_start = 0;
     147       11430 :         fm.fiemap.fm_length = ~0ULL;
     148       11430 :         fm.fiemap.fm_flags = FIEMAP_FLAG_SYNC; /* required to ensure that just created files report a valid address and not 0 */
     149       11430 :         fm.fiemap.fm_extent_count = 1; /* we are interested only at the first block */
     150             : 
     151       11430 :         if (ioctl(f, FS_IOC_FIEMAP, &fm) != -1) {
     152       11430 :                 *physical = fm.fiemap.fm_extents[0].fe_physical + FILEPHY_REAL_OFFSET;
     153       11430 :                 if (close(f) == -1)
     154           0 :                         return -1;
     155       11430 :                 return 0;
     156             :         }
     157             : 
     158             :         /* if the file is empty, FIBMAP doesn't work, and we don't even try to use it */
     159           0 :         if (size == 0) {
     160           0 :                 *physical = FILEPHY_WITHOUT_OFFSET;
     161           0 :                 if (close(f) == -1)
     162           0 :                         return -1;
     163           0 :                 return 0;
     164             :         }
     165             : 
     166             :         /* then try with FIBMAP */
     167             :         /* it works for jfs, reiserfs, ntfs-3g */
     168             :         /* in exfat it always returns 0, that it's anyway better than the fake inodes */
     169           0 :         blknum = 0; /* first block */
     170           0 :         if (ioctl(f, FIBMAP, &blknum) != -1) {
     171           0 :                 *physical = blknum + FILEPHY_REAL_OFFSET;
     172           0 :                 if (close(f) == -1)
     173           0 :                         return -1;
     174           0 :                 return 0;
     175             :         }
     176             : 
     177             :         /* otherwise don't use anything, and keep the directory traversal order */
     178             :         /* at now this should happen only for vfat */
     179             :         /* and it's surely better than using fake inodes */
     180           0 :         *physical = FILEPHY_UNREPORTED_OFFSET;
     181           0 :         if (close(f) == -1)
     182           0 :                 return -1;
     183             : #else
     184             :         /* In a generic Unix use a dummy value for all the files */
     185             :         /* We don't want to risk to use the inode without knowing */
     186             :         /* if it really improves performance. */
     187             :         /* In this way we keep them in the directory traversal order */
     188             :         /* that at least keeps files in the same directory together. */
     189             :         /* Note also that in newer filesystem with snapshot, like ZFS, */
     190             :         /* the inode doesn't represent even less the disk position, because files */
     191             :         /* are not overwritten in place, but rewritten in another location */
     192             :         /* of the disk. */
     193             :         *physical = FILEPHY_UNREPORTED_OFFSET;
     194             : 
     195             :         (void)path; /* not used here */
     196             :         (void)size;
     197             : #endif
     198             : 
     199           0 :         return 0;
     200             : }
     201             : 
     202        1903 : int fsinfo(const char* path, int* has_persistent_inode, uint64_t* total_space, uint64_t* free_space)
     203             : {
     204             : #if HAVE_STATFS && HAVE_STRUCT_STATFS_F_TYPE
     205             :         struct statfs st;
     206             : 
     207        1903 :         if (statfs(path, &st) != 0) {
     208             :                 char dir[PATH_MAX];
     209             :                 char* slash;
     210             : 
     211           6 :                 if (errno != ENOENT) {
     212           0 :                         return -1;
     213             :                 }
     214             : 
     215             :                 /* if it doesn't exist, we assume a file */
     216             :                 /* and we check for the containing dir */
     217           6 :                 if (strlen(path) + 1 > sizeof(dir)) {
     218           0 :                         errno = ENAMETOOLONG;
     219           0 :                         return -1;
     220             :                 }
     221             : 
     222           6 :                 strcpy(dir, path);
     223             : 
     224           6 :                 slash = strrchr(dir, '/');
     225           6 :                 if (!slash)
     226           0 :                         return -1;
     227             : 
     228           6 :                 *slash = 0;
     229           6 :                 if (statfs(dir, &st) != 0)
     230           0 :                         return -1;
     231             :         }
     232             : 
     233             :         /* to get the fs type check "man stat" or "stat -f -t FILE" */
     234        1903 :         if (has_persistent_inode)
     235         413 :                 switch (st.f_type) {
     236             :                 case 0x65735546 : /* FUSE, "fuseblk" in the stat command */
     237             :                 case 0x4d44 : /* VFAT, "msdos" in the stat command */
     238           0 :                         *has_persistent_inode = 0;
     239           0 :                         break;
     240             :                 default :
     241             :                         /* by default assume yes */
     242         413 :                         *has_persistent_inode = 1;
     243         413 :                         break;
     244             :                 }
     245             : 
     246        1903 :         if (total_space)
     247        1490 :                 *total_space = st.f_bsize * (uint64_t)st.f_blocks;
     248        1903 :         if (free_space)
     249        1490 :                 *free_space = st.f_bsize * (uint64_t)st.f_bfree;
     250             : #else
     251             :         (void)path;
     252             : 
     253             :         /* by default assume yes */
     254             :         if (has_persistent_inode)
     255             :                 *has_persistent_inode = 1;
     256             :         if (total_space)
     257             :                 *total_space = 0;
     258             :         if (free_space)
     259             :                 *free_space = 0;
     260             : #endif
     261             : 
     262        1903 :         return 0;
     263             : }
     264             : 
     265     1810494 : uint64_t tick(void)
     266             : {
     267             : #if HAVE_MACH_ABSOLUTE_TIME
     268             :         /* for Mac OS X */
     269             :         return mach_absolute_time();
     270             : #elif HAVE_CLOCK_GETTIME && (defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW))
     271             :         /* for Linux */
     272             :         struct timespec tv;
     273             : 
     274             :         /* nanosecond precision with clock_gettime() */
     275             : #if defined(CLOCK_MONOTONIC_RAW)
     276     1810494 :         if (clock_gettime(CLOCK_MONOTONIC_RAW, &tv) != 0) {
     277             : #else
     278             :         if (clock_gettime(CLOCK_MONOTONIC, &tv) != 0) {
     279             : #endif
     280           0 :                 return 0;
     281             :         }
     282             : 
     283     1810494 :         return tv.tv_sec * 1000000000ULL + tv.tv_nsec;
     284             : #else
     285             :         /* other platforms */
     286             :         struct timeval tv;
     287             : 
     288             :         /* microsecond precision with gettimeofday() */
     289             :         if (gettimeofday(&tv, 0) != 0) {
     290             :                 return 0;
     291             :         }
     292             : 
     293             :         return tv.tv_sec * 1000000ULL + tv.tv_usec;
     294             : #endif
     295             : }
     296             : 
     297          36 : uint64_t tick_ms(void)
     298             : {
     299             :         struct timeval tv;
     300             : 
     301          36 :         if (gettimeofday(&tv, 0) != 0)
     302           0 :                 return 0;
     303             : 
     304          36 :         return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
     305             : }
     306             : 
     307         252 : int randomize(void* ptr, size_t size)
     308             : {
     309             :         int f;
     310             :         ssize_t ret;
     311             : 
     312         252 :         f = open("/dev/urandom", O_RDONLY);
     313         252 :         if (f == -1)
     314           0 :                 return -1;
     315             : 
     316         252 :         ret = read(f, ptr, size);
     317         252 :         if (ret < 0 || (size_t)ret != size) {
     318           0 :                 close(f);
     319           0 :                 return -1;
     320             :         }
     321             : 
     322         252 :         if (close(f) != 0)
     323           0 :                 return -1;
     324             : 
     325         252 :         return 0;
     326             : }
     327             : 
     328             : /**
     329             :  * Read a file extracting the contained device number in %u:%u format.
     330             :  * Returns 0 on error.
     331             :  */
     332             : #if HAVE_LINUX_DEVICE
     333          86 : static dev_t devread(const char* path)
     334             : {
     335             :         int f;
     336             :         int ret;
     337             :         char buf[64];
     338             :         char* e;
     339             :         unsigned ma;
     340             :         unsigned mi;
     341             : 
     342          86 :         f = open(path, O_RDONLY);
     343          86 :         if (f == -1) {
     344             :                 /* LCOV_EXCL_START */
     345             :                 log_fatal("Failed to open '%s'.\n", path);
     346             :                 return 0;
     347             :                 /* LCOV_EXCL_STOP */
     348             :         }
     349             : 
     350          86 :         ret = read(f, buf, sizeof(buf));
     351          86 :         if (ret < 0) {
     352             :                 /* LCOV_EXCL_START */
     353             :                 close(f);
     354             :                 log_fatal("Failed to read '%s'.\n", path);
     355             :                 return 0;
     356             :                 /* LCOV_EXCL_STOP */
     357             :         }
     358          86 :         if (ret == sizeof(buf)) {
     359             :                 /* LCOV_EXCL_START */
     360             :                 close(f);
     361             :                 log_fatal("Too long read '%s'.\n", path);
     362             :                 return 0;
     363             :                 /* LCOV_EXCL_STOP */
     364             :         }
     365             : 
     366          86 :         buf[ret] = 0;
     367             : 
     368          86 :         ma = strtoul(buf, &e, 10);
     369          86 :         if (*e != ':') {
     370             :                 /* LCOV_EXCL_START */
     371             :                 close(f);
     372             :                 log_fatal("Invalid format in '%s' for '%s'.\n", path, buf);
     373             :                 return 0;
     374             :                 /* LCOV_EXCL_STOP */
     375             :         }
     376             : 
     377          86 :         mi = strtoul(e+1, &e, 10);
     378          86 :         if (*e != 0 && !isspace(*e)) {
     379             :                 /* LCOV_EXCL_START */
     380             :                 close(f);
     381             :                 log_fatal("Invalid format in '%s' for '%s'.\n", path, buf);
     382             :                 return 0;
     383             :                 /* LCOV_EXCL_STOP */
     384             :         }
     385             : 
     386          86 :         ret = close(f);
     387          86 :         if (ret != 0) {
     388             :                 /* LCOV_EXCL_START */
     389             :                 log_fatal("Failed to close '%s'.\n", path);
     390             :                 return 0;
     391             :                 /* LCOV_EXCL_STOP */
     392             :         }
     393             : 
     394          86 :         return makedev(ma, mi);
     395             : }
     396             : #endif
     397             : 
     398             : #if HAVE_LINUX_DEVICE
     399             : /**
     400             :  * Get the device file from the device number.
     401             :  */
     402          97 : static int devresolve(dev_t device, char* path, size_t path_size)
     403             : {
     404             :         struct stat st;
     405             :         char buf[PATH_MAX];
     406             :         int ret;
     407             : 
     408             :         /* default device path from device number */
     409          97 :         pathprint(path, path_size, "/dev/block/%u:%u", major(device), minor(device));
     410             : 
     411             :         /* resolve the link from /dev/block */
     412          97 :         ret = readlink(path, buf, sizeof(buf));
     413          96 :         if (ret < 0) {
     414             :                 /* LCOV_EXCL_START */
     415             :                 log_fatal("Failed to readlink '%s'.\n", path);
     416             :                 return -1;
     417             :                 /* LCOV_EXCL_STOP */
     418             :         }
     419          96 :         if (ret == sizeof(buf)) {
     420             :                 /* LCOV_EXCL_START */
     421             :                 log_fatal("Too long readlink '%s'.\n", path);
     422             :                 return -1;
     423             :                 /* LCOV_EXCL_STOP */
     424             :         }
     425             : 
     426          96 :         buf[ret] = 0;
     427             : 
     428          96 :         if (buf[0] != '.' || buf[1] != '.' || buf[2] != '/') {
     429             :                 /* LCOV_EXCL_START */
     430             :                 log_fatal("Unexpected link '%s' at '%s'.\n", buf, path);
     431             :                 return -1;
     432             :                 /* LCOV_EXCL_STOP */
     433             :         }
     434             : 
     435             :         /* set the real device path */
     436          97 :         pathprint(path, path_size, "/dev/%s", buf + 3);
     437             : 
     438             :         /* check the device */
     439          97 :         if (stat(path, &st) != 0) {
     440             :                 /* LCOV_EXCL_START */
     441             :                 log_fatal("Failed to stat '%s'.\n", path);
     442             :                 return -1;
     443             :                 /* LCOV_EXCL_STOP */
     444             :         }
     445          96 :         if (st.st_rdev != device) {
     446             :                 /* LCOV_EXCL_START */
     447             :                 log_fatal("Unexpected device '%u:%u' instead of '%u:%u' for '%s'.\n", major(st.st_rdev), minor(st.st_rdev), major(device), minor(device), path);
     448             :                 return -1;
     449             :                 /* LCOV_EXCL_STOP */
     450             :         }
     451             : 
     452          96 :         return 0;
     453             : }
     454             : #endif
     455             : 
     456             : /**
     457             :  * Read a device tree filling the specified list of disk_t entries.
     458             :  */
     459             : #if HAVE_LINUX_DEVICE
     460          72 : static int devtree(const char* name, const char* custom, dev_t device, devinfo_t* parent, tommy_list* list)
     461             : {
     462             :         char path[PATH_MAX];
     463             :         DIR* d;
     464          72 :         int slaves = 0;
     465             : 
     466          72 :         pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/slaves", major(device), minor(device));
     467             : 
     468             :         /* check if there is a slaves list */
     469          72 :         d = opendir(path);
     470          72 :         if (d != 0) {
     471             :                 struct dirent* dd;
     472             : 
     473         180 :                 while ((dd = readdir(d)) != 0) {
     474         108 :                         if (dd->d_name[0] != '.') {
     475             :                                 /* for each slave, expand the full potential tree */
     476          36 :                                 pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/slaves/%s/dev", major(device), minor(device), dd->d_name);
     477             : 
     478          36 :                                 device = devread(path);
     479          36 :                                 if (!device) {
     480             :                                         /* LCOV_EXCL_START */
     481             :                                         closedir(d);
     482             :                                         return -1;
     483             :                                         /* LCOV_EXCL_STOP */
     484             :                                 }
     485             : 
     486          36 :                                 if (devtree(name, custom, device, parent, list) != 0) {
     487             :                                         /* LCOV_EXCL_START */
     488             :                                         closedir(d);
     489             :                                         return -1;
     490             :                                         /* LCOV_EXCL_STOP */
     491             :                                 }
     492             : 
     493          36 :                                 ++slaves;
     494             :                         }
     495             :                 }
     496             : 
     497          36 :                 closedir(d);
     498             :         }
     499             : 
     500             :         /* if no slaves found */
     501          72 :         if (!slaves) {
     502             :                 /* this is a raw device */
     503             :                 devinfo_t* devinfo;
     504             : 
     505             :                 /* check if it's a real device */
     506          36 :                 pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/device", major(device), minor(device));
     507          36 :                 if (access(path, F_OK) != 0) {
     508             :                         /* get the parent device */
     509          36 :                         pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/../dev", major(device), minor(device));
     510             : 
     511          36 :                         device = devread(path);
     512          36 :                         if (!device) {
     513             :                                 /* LCOV_EXCL_START */
     514             :                                 return -1;
     515             :                                 /* LCOV_EXCL_STOP */
     516             :                         }
     517             :                 }
     518             : 
     519             :                 /* get the device file */
     520          36 :                 if (devresolve(device, path, sizeof(path)) != 0) {
     521             :                         /* LCOV_EXCL_START */
     522             :                         return -1;
     523             :                         /* LCOV_EXCL_STOP */
     524             :                 }
     525             : 
     526          36 :                 devinfo = calloc_nofail(1, sizeof(devinfo_t));
     527             : 
     528          36 :                 devinfo->device = device;
     529          36 :                 pathcpy(devinfo->name, sizeof(devinfo->name), name);
     530          36 :                 pathcpy(devinfo->smartctl, sizeof(devinfo->smartctl), custom);
     531          36 :                 pathcpy(devinfo->file, sizeof(devinfo->file), path);
     532          36 :                 devinfo->parent = parent;
     533             : 
     534             :                 /* insert in the list */
     535          36 :                 tommy_list_insert_tail(list, &devinfo->node, devinfo);
     536             :         }
     537             : 
     538          72 :         return 0;
     539             : }
     540             : #endif
     541             : 
     542             : /**
     543             :  * Scan all the devices.
     544             :  *
     545             :  * If a device is already in, it's not added again.
     546             :  */
     547             : #if HAVE_LINUX_DEVICE
     548           1 : static int devscan(tommy_list* list)
     549             : {
     550             :         char dir[PATH_MAX];
     551             :         DIR* d;
     552             :         struct dirent* dd;
     553             : 
     554           1 :         pathprint(dir, sizeof(dir), "/sys/dev/block/");
     555             : 
     556             :         /* check if there is a slaves list */
     557           1 :         d = opendir(dir);
     558           1 :         if (d == 0) {
     559             :                 /* LCOV_EXCL_START */
     560             :                 log_fatal("Failed to open dir '%s'.\n", dir);
     561             :                 return -1;
     562             :                 /* LCOV_EXCL_STOP */
     563             :         }
     564             : 
     565          75 :         while ((dd = readdir(d)) != 0) {
     566          73 :                 if (dd->d_name[0] != '.') {
     567             :                         char path[PATH_MAX];
     568             : 
     569          71 :                         pathprint(path, sizeof(path), "/sys/dev/block/%s/device", dd->d_name);
     570             : 
     571             :                         /* check if it's a real device */
     572          71 :                         if (access(path, F_OK) == 0) {
     573             :                                 tommy_node* i;
     574             :                                 dev_t device;
     575             : 
     576          14 :                                 pathprint(path, sizeof(path), "/sys/dev/block/%s/dev", dd->d_name);
     577             : 
     578          14 :                                 device = devread(path);
     579          14 :                                 if (!device) {
     580             :                                         /* LCOV_EXCL_START */
     581             :                                         closedir(d);
     582             :                                         return -1;
     583             :                                         /* LCOV_EXCL_STOP */
     584             :                                 }
     585             : 
     586             :                                 /* check if already present */
     587         248 :                                 for (i = tommy_list_head(list); i != 0; i = i->next) {
     588         235 :                                         devinfo_t* devinfo = i->data;
     589         235 :                                         if (devinfo->device == device)
     590           1 :                                                 break;
     591             :                                 }
     592             : 
     593             :                                 /* if not found */
     594          14 :                                 if (i == 0) {
     595             :                                         devinfo_t* devinfo;
     596             : 
     597             :                                         /* get the device file */
     598          13 :                                         if (devresolve(device, path, sizeof(path)) != 0) {
     599             :                                                 /* LCOV_EXCL_START */
     600             :                                                 closedir(d);
     601             :                                                 return -1;
     602             :                                                 /* LCOV_EXCL_STOP */
     603             :                                         }
     604             : 
     605          13 :                                         devinfo = calloc_nofail(1, sizeof(devinfo_t));
     606             : 
     607          13 :                                         devinfo->device = device;
     608          13 :                                         pathcpy(devinfo->file, sizeof(devinfo->file), path);
     609             : 
     610             :                                         /* insert in the list */
     611          13 :                                         tommy_list_insert_tail(list, &devinfo->node, devinfo);
     612             :                                 }
     613             :                         }
     614             :                 }
     615             :         }
     616             : 
     617           1 :         closedir(d);
     618           1 :         return 0;
     619             : }
     620             : #endif
     621             : 
     622             : /**
     623             :  * Gets SMART attributes.
     624             :  */
     625          25 : static int devsmart(dev_t device, const char* name, const char* custom, uint64_t* smart, char* serial)
     626             : {
     627             :         char cmd[128];
     628             :         char file[128];
     629             :         FILE* f;
     630             :         int ret;
     631             : 
     632          25 :         snprintf(file, sizeof(file), "/dev/block/%u:%u", major(device), minor(device));
     633             : 
     634             :         /* if there is a custom command */
     635          25 :         if (custom[0]) {
     636             :                 char option[128];
     637           2 :                 snprintf(option, sizeof(option), custom, file);
     638           2 :                 snprintf(cmd, sizeof(cmd), "smartctl -a %s", option);
     639             :         } else {
     640          23 :                 snprintf(cmd, sizeof(cmd), "smartctl -a %s", file);
     641             :         }
     642             : 
     643          25 :         log_tag("smartctl:%s:%s:run: %s\n", file, name, cmd);
     644             : 
     645          25 :         f = popen(cmd, "r");
     646          25 :         if (!f) {
     647             :                 /* LCOV_EXCL_START */
     648             :                 log_fatal("Failed to run '%s' (from popen).\n", cmd);
     649             :                 return -1;
     650             :                 /* LCOV_EXCL_STOP */
     651             :         }
     652             : 
     653          25 :         if (smartctl_attribute(f, file, name, smart, serial) != 0) {
     654             :                 /* LCOV_EXCL_START */
     655             :                 pclose(f);
     656             :                 return -1;
     657             :                 /* LCOV_EXCL_STOP */
     658             :         }
     659             : 
     660          25 :         ret = pclose(f);
     661             : 
     662          25 :         log_tag("smartctl:%s:%s:ret: %x\n", file, name, ret);
     663             : 
     664          25 :         if (!WIFEXITED(ret)) {
     665             :                 /* LCOV_EXCL_START */
     666             :                 log_fatal("Failed to run '%s' (not exited).\n", cmd);
     667             :                 return -1;
     668             :                 /* LCOV_EXCL_STOP */
     669             :         }
     670          25 :         if (WEXITSTATUS(ret) == 127) {
     671             :                 /* LCOV_EXCL_START */
     672             :                 log_fatal("Failed to run '%s' (from sh).\n", cmd);
     673             :                 return -1;
     674             :                 /* LCOV_EXCL_STOP */
     675             :         }
     676             : 
     677             :         /* store the return smartctl return value */
     678           0 :         smart[SMART_FLAGS] = WEXITSTATUS(ret);
     679             : 
     680           0 :         return 0;
     681             : }
     682             : 
     683             : /**
     684             :  * Spin down a specific device.
     685             :  */
     686             : #if HAVE_LINUX_DEVICE
     687          12 : static int devdown(dev_t device, const char* name, const char* custom)
     688             : {
     689             :         char cmd[128];
     690             :         char file[128];
     691             :         FILE* f;
     692             :         int ret;
     693             : 
     694          12 :         snprintf(file, sizeof(file), "/dev/block/%u:%u", major(device), minor(device));
     695             : 
     696             :         /* if there is a custom command */
     697          12 :         if (custom[0]) {
     698             :                 char option[128];
     699           2 :                 snprintf(option, sizeof(option), custom, file);
     700           2 :                 snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", option);
     701             :         } else {
     702          10 :                 snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", file);
     703             :         }
     704             : 
     705          12 :         log_tag("smartctl:%s:%s:run: %s\n", file, name, cmd);
     706             : 
     707          12 :         f = popen(cmd, "r");
     708          12 :         if (!f) {
     709             :                 /* LCOV_EXCL_START */
     710             :                 log_fatal("Failed to run '%s' (from popen).\n", cmd);
     711             :                 return -1;
     712             :                 /* LCOV_EXCL_STOP */
     713             :         }
     714             : 
     715          12 :         if (smartctl_flush(f, file, name) != 0) {
     716             :                 /* LCOV_EXCL_START */
     717             :                 pclose(f);
     718             :                 return -1;
     719             :                 /* LCOV_EXCL_STOP */
     720             :         }
     721             : 
     722          12 :         ret = pclose(f);
     723             : 
     724          12 :         log_tag("smartctl:%s:%s:ret: %x\n", file, name, ret);
     725             : 
     726          12 :         if (!WIFEXITED(ret)) {
     727             :                 /* LCOV_EXCL_START */
     728             :                 log_fatal("Failed to run '%s' (not exited).\n", cmd);
     729             :                 return -1;
     730             :                 /* LCOV_EXCL_STOP */
     731             :         }
     732          12 :         if (WEXITSTATUS(ret) == 127) {
     733             :                 /* LCOV_EXCL_START */
     734             :                 log_fatal("Failed to run '%s' (from sh).\n", cmd);
     735             :                 return -1;
     736             :                 /* LCOV_EXCL_STOP */
     737             :         }
     738           0 :         if (WEXITSTATUS(ret) != 0) {
     739             :                 /* LCOV_EXCL_START */
     740             :                 log_fatal("Failed to run '%s' with return code %xh.\n", cmd, WEXITSTATUS(ret));
     741             :                 return -1;
     742             :                 /* LCOV_EXCL_STOP */
     743             :         }
     744             : 
     745           0 :         return 0;
     746             : }
     747             : #endif
     748             : 
     749             : /**
     750             :  * Spin up a device.
     751             :  *
     752             :  * There isn't a defined way to spin up a device,
     753             :  * so we just do a generic write.
     754             :  */
     755          12 : static int devup(const char* mount)
     756             : {
     757             :         int ret;
     758             :         char path[PATH_MAX];
     759             : 
     760             :         /* add a temporary name used for writing */
     761          12 :         pathprint(path, sizeof(path), "%s.snapraid-spinup", mount);
     762             : 
     763             :         /* do a generic write, and immediately undo it */
     764          12 :         ret = mkdir(path, 0);
     765          12 :         if (ret != 0 && errno != EEXIST) {
     766             :                 /* LCOV_EXCL_START */
     767             :                 log_fatal("Failed to create dir '%s'.\n", path);
     768             :                 return -1;
     769             :                 /* LCOV_EXCL_STOP */
     770             :         }
     771             : 
     772             :         /* removes the just created dir */
     773          12 :         rmdir(path);
     774             : 
     775          12 :         return 0;
     776             : }
     777             : 
     778             : /**
     779             :  * Thread for spinning up.
     780             :  *
     781             :  * Note that filling up the devinfo object is done inside this thread,
     782             :  * to avoid to block the main thread if the device need to be spin up
     783             :  * to handle stat/resolve requests.
     784             :  */
     785          12 : static void* thread_spinup(void* arg)
     786             : {
     787          12 :         devinfo_t* devinfo = arg;
     788             :         struct stat st;
     789             :         uint64_t start;
     790             : 
     791          12 :         start = tick_ms();
     792             : 
     793             :         /* first get the device number, this usually doesn't trigger a thread_spinup */
     794          12 :         if (stat(devinfo->mount, &st) != 0) {
     795             :                 /* LCOV_EXCL_START */
     796             :                 log_fatal("Failed to stat device '%s'.\n", devinfo->mount);
     797             :                 return (void*)-1;
     798             :                 /* LCOV_EXCL_STOP */
     799             :         }
     800             : 
     801             :         /* set the device number for printing */
     802          12 :         devinfo->device = st.st_dev;
     803             : 
     804             : #if HAVE_LINUX_DEVICE
     805             :         /* get the device file, but only in Linux and only for the message */
     806          12 :         if (devresolve(devinfo->device, devinfo->file, sizeof(devinfo->file)) != 0) {
     807             :                 /* LCOV_EXCL_START */
     808             :                 return (void*)-1;
     809             :                 /* LCOV_EXCL_STOP */
     810             :         }
     811             : #endif
     812             : 
     813          12 :         if (devup(devinfo->mount) != 0) {
     814             :                 /* LCOV_EXCL_START */
     815             :                 return (void*)-1;
     816             :                 /* LCOV_EXCL_STOP */
     817             :         }
     818             : 
     819             : #if HAVE_LINUX_DEVICE
     820          12 :         msg_status("Spunup device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
     821             : #else
     822             :         msg_status("Spunup device '%u:%u' for disk '%s' in %" PRIu64 " ms.\n", major(devinfo->device), minor(devinfo->device), devinfo->name, tick_ms() - start);
     823             : #endif
     824             : 
     825          12 :         return 0;
     826             : }
     827             : 
     828             : /**
     829             :  * Thread for spinning down.
     830             :  */
     831          12 : static void* thread_spindown(void* arg)
     832             : {
     833             : #if HAVE_LINUX_DEVICE
     834          12 :         devinfo_t* devinfo = arg;
     835             :         uint64_t start;
     836             : 
     837          12 :         start = tick_ms();
     838             : 
     839          12 :         if (devdown(devinfo->device, devinfo->name, devinfo->smartctl) != 0) {
     840             :                 /* LCOV_EXCL_START */
     841             :                 return (void*)-1;
     842             :                 /* LCOV_EXCL_STOP */
     843             :         }
     844             : 
     845           0 :         msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
     846             : 
     847           0 :         return 0;
     848             : #else
     849             :         (void)arg;
     850             :         return (void*)-1;
     851             : #endif
     852             : }
     853             : 
     854             : /**
     855             :  * Thread for getting smart info.
     856             :  */
     857          25 : static void* thread_smart(void* arg)
     858             : {
     859          25 :         devinfo_t* devinfo = arg;
     860             : 
     861          25 :         if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->smart_serial) != 0) {
     862             :                 /* LCOV_EXCL_START */
     863             :                 return (void*)-1;
     864             :                 /* LCOV_EXCL_STOP */
     865             :         }
     866             : 
     867           0 :         return 0;
     868             : }
     869             : 
     870           3 : static int device_thread(tommy_list* list, void* (*func)(void* arg))
     871             : {
     872           3 :         int fail = 0;
     873             :         tommy_node* i;
     874             : 
     875             : #if HAVE_PTHREAD_CREATE
     876             :         /* starts all threads */
     877          52 :         for (i = tommy_list_head(list); i != 0; i = i->next) {
     878          49 :                 devinfo_t* devinfo = i->data;
     879             : 
     880          49 :                 if (pthread_create(&devinfo->thread, 0, func, devinfo) != 0) {
     881             :                         /* LCOV_EXCL_START */
     882             :                         log_fatal("Failed to create thread.\n");
     883             :                         exit(EXIT_FAILURE);
     884             :                         /* LCOV_EXCL_STOP */
     885             :                 }
     886             :         }
     887             : 
     888             :         /* joins all threads */
     889          52 :         for (i = tommy_list_head(list); i != 0; i = i->next) {
     890          49 :                 devinfo_t* devinfo = i->data;
     891             :                 void* retval;
     892             : 
     893          49 :                 if (pthread_join(devinfo->thread, &retval) != 0) {
     894             :                         /* LCOV_EXCL_START */
     895             :                         log_fatal("Failed to join thread.\n");
     896             :                         exit(EXIT_FAILURE);
     897             :                         /* LCOV_EXCL_STOP */
     898             :                 }
     899             : 
     900          49 :                 if (retval != 0)
     901          37 :                         ++fail;
     902             :         }
     903             : #else
     904             :         for (i = tommy_list_head(list); i != 0; i = i->next) {
     905             :                 devinfo_t* devinfo = i->data;
     906             : 
     907             :                 if (func(devinfo) != 0)
     908             :                         ++fail;
     909             :         }
     910             : #endif
     911           3 :         if (fail != 0) {
     912             :                 /* LCOV_EXCL_START */
     913             :                 return -1;
     914             :                 /* LCOV_EXCL_STOP */
     915             :         }
     916             : 
     917           1 :         return 0;
     918             : }
     919             : 
     920           4 : int devquery(tommy_list* high, tommy_list* low, int operation)
     921             : {
     922             :         tommy_node* i;
     923           4 :         void* (*func)(void* arg) = 0;
     924             : 
     925             : #if HAVE_LINUX_DEVICE
     926           4 :         if (operation != DEVICE_UP) {
     927             :                 struct stat st;
     928             :                 /* sysfs and devfs interfaces are required */
     929           3 :                 if (stat("/sys/dev/block", &st) != 0) {
     930             :                         /* LCOV_EXCL_START */
     931             :                         log_fatal("Missing interface /sys/dev/block.\n");
     932             :                         return -1;
     933             :                         /* LCOV_EXCL_STOP */
     934             :                 }
     935           3 :                 if (stat("/dev/block", &st) != 0) {
     936             :                         /* LCOV_EXCL_START */
     937             :                         log_fatal("Missing interface /dev/block.\n");
     938             :                         return -1;
     939             :                         /* LCOV_EXCL_STOP */
     940             :                 }
     941             : 
     942             :                 /* for each device */
     943          39 :                 for (i = tommy_list_head(high); i != 0; i = i->next) {
     944          36 :                         devinfo_t* devinfo = i->data;
     945             : 
     946             :                         /* get the device file */
     947          36 :                         if (devresolve(devinfo->device, devinfo->file, sizeof(devinfo->file)) != 0) {
     948             :                                 /* LCOV_EXCL_START */
     949             :                                 log_fatal("Failed to resolve device '%u:%u'.\n", major(devinfo->device), minor(devinfo->device));
     950             :                                 return -1;
     951             :                                 /* LCOV_EXCL_STOP */
     952             :                         }
     953             : 
     954             :                         /* expand the tree of devices */
     955          36 :                         if (devtree(devinfo->name, devinfo->smartctl, devinfo->device, devinfo, low) != 0) {
     956             :                                 /* LCOV_EXCL_START */
     957             :                                 log_fatal("Failed to expand device '%u:%u'.\n", major(devinfo->device), minor(devinfo->device));
     958             :                                 return -1;
     959             :                                 /* LCOV_EXCL_STOP */
     960             :                         }
     961             :                 }
     962             :         }
     963             : #endif
     964             : 
     965           4 :         if (operation == DEVICE_UP) {
     966             :                 /* duplicate the high */
     967          13 :                 for (i = tommy_list_head(high); i != 0; i = i->next) {
     968          12 :                         devinfo_t* devinfo = i->data;
     969             :                         devinfo_t* entry;
     970             : 
     971          12 :                         entry = calloc_nofail(1, sizeof(devinfo_t));
     972             : 
     973          12 :                         entry->device = devinfo->device;
     974          12 :                         pathcpy(entry->name, sizeof(entry->name), devinfo->name);
     975          12 :                         pathcpy(entry->mount, sizeof(entry->mount), devinfo->mount);
     976             : 
     977             :                         /* insert in the high */
     978          12 :                         tommy_list_insert_tail(low, &entry->node, entry);
     979             :                 }
     980             :         }
     981             : 
     982             : #if HAVE_LINUX_DEVICE
     983             :         /* add other devices */
     984           4 :         if (operation == DEVICE_SMART) {
     985           1 :                 if (devscan(low) != 0) {
     986             :                         /* LCOV_EXCL_START */
     987             :                         log_fatal("Failed to list other devices.\n");
     988             :                         return -1;
     989             :                         /* LCOV_EXCL_STOP */
     990             :                 }
     991             :         }
     992             : #endif
     993             : 
     994           4 :         switch (operation) {
     995           1 :         case DEVICE_UP : func = thread_spinup; break;
     996           1 :         case DEVICE_DOWN : func = thread_spindown; break;
     997           1 :         case DEVICE_SMART : func = thread_smart; break;
     998             :         }
     999             : 
    1000           4 :         if (!func)
    1001           1 :                 return 0;
    1002             : 
    1003           3 :         return device_thread(low, func);
    1004             : }
    1005             : 
    1006         240 : void os_init(int opt)
    1007             : {
    1008             :         (void)opt;
    1009         240 : }
    1010             : 
    1011         222 : void os_done(void)
    1012             : {
    1013         222 : }
    1014             : 
    1015             : #endif
    1016             : 

Generated by: LCOV version 1.10