]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
mdadm: numbered names verification
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 64dd409c4d5576a97c83ad2fa7d4fc90f5ed6cd4..fa378ebac6ef3b7e8b15cbc2509105e26b4c1bd0 100644 (file)
--- a/util.c
+++ b/util.c
@@ -35,7 +35,6 @@
 #include       <poll.h>
 #include       <ctype.h>
 #include       <dirent.h>
-#include       <signal.h>
 #include       <dlfcn.h>
 
 
@@ -167,7 +166,7 @@ retry:
                pr_err("error %d when get PW mode on lock %s\n", errno, str);
                /* let's try several times if EAGAIN happened */
                if (dlm_lock_res->lksb.sb_status == EAGAIN && retry_count < 10) {
-                       sleep(10);
+                       sleep_for(10, 0, true);
                        retry_count++;
                        goto retry;
                }
@@ -268,7 +267,7 @@ int md_array_active(int fd)
                 * GET_ARRAY_INFO doesn't provide access to the proper state
                 * information, so fallback to a basic check for raid_disks != 0
                 */
-               ret = ioctl(fd, GET_ARRAY_INFO, &array);
+               ret = md_get_array_info(fd, &array);
        }
 
        return !ret;
@@ -306,43 +305,6 @@ int md_get_disk_info(int fd, struct mdu_disk_info_s *disk)
        return ioctl(fd, GET_DISK_INFO, disk);
 }
 
-/*
- * Parse a 128 bit uuid in 4 integers
- * format is 32 hexx nibbles with options :.<space> separator
- * If not exactly 32 hex digits are found, return 0
- * else return 1
- */
-int parse_uuid(char *str, int uuid[4])
-{
-       int hit = 0; /* number of Hex digIT */
-       int i;
-       char c;
-       for (i = 0; i < 4; i++)
-               uuid[i] = 0;
-
-       while ((c = *str++) != 0) {
-               int n;
-               if (c >= '0' && c <= '9')
-                       n = c-'0';
-               else if (c >= 'a' && c <= 'f')
-                       n = 10 + c - 'a';
-               else if (c >= 'A' && c <= 'F')
-                       n = 10 + c - 'A';
-               else if (strchr(":. -", c))
-                       continue;
-               else return 0;
-
-               if (hit<32) {
-                       uuid[hit/8] <<= 4;
-                       uuid[hit/8] += n;
-               }
-               hit++;
-       }
-       if (hit == 32)
-               return 1;
-       return 0;
-}
-
 int get_linux_version()
 {
        struct utsname name;
@@ -389,7 +351,7 @@ int mdadm_version(char *version)
 unsigned long long parse_size(char *size)
 {
        /* parse 'size' which should be a number optionally
-        * followed by 'K', 'M', or 'G'.
+        * followed by 'K', 'M'. 'G' or 'T'.
         * Without a suffix, K is assumed.
         * Number returned is in sectors (half-K)
         * INVALID_SECTORS returned on error.
@@ -411,6 +373,10 @@ unsigned long long parse_size(char *size)
                        c++;
                        s *= 1024 * 1024 * 2;
                        break;
+               case 'T':
+                       c++;
+                       s *= 1024 * 1024 * 1024 * 2LL;
+                       break;
                case 's': /* sectors */
                        c++;
                        break;
@@ -455,29 +421,25 @@ int parse_layout_10(char *layout)
 
 int parse_layout_faulty(char *layout)
 {
+       int ln, mode;
+       char *m;
+
+       if (!layout)
+               return -1;
+
        /* Parse the layout string for 'faulty' */
-       int ln = strcspn(layout, "0123456789");
-       char *m = xstrdup(layout);
-       int mode;
+       ln = strcspn(layout, "0123456789");
+       m = xstrdup(layout);
        m[ln] = 0;
        mode = map_name(faultylayout, m);
+       free(m);
+
        if (mode == UnSet)
                return -1;
 
        return mode | (atoi(layout+ln)<< ModeShift);
 }
 
-long parse_num(char *num)
-{
-       /* Either return a valid number, or -1 */
-       char *c;
-       long rv = strtol(num, &c, 10);
-       if (rv < 0 || *c || !num[0])
-               return -1;
-       else
-               return rv;
-}
-
 int parse_cluster_confirm_arg(char *input, char **devname, int *slot)
 {
        char *dev;
@@ -607,56 +569,6 @@ int enough(int level, int raid_disks, int layout, int clean, char *avail)
        }
 }
 
-const int uuid_zero[4] = { 0, 0, 0, 0 };
-
-int same_uuid(int a[4], int b[4], int swapuuid)
-{
-       if (swapuuid) {
-               /* parse uuids are hostendian.
-                * uuid's from some superblocks are big-ending
-                * if there is a difference, we need to swap..
-                */
-               unsigned char *ac = (unsigned char *)a;
-               unsigned char *bc = (unsigned char *)b;
-               int i;
-               for (i = 0; i < 16; i += 4) {
-                       if (ac[i+0] != bc[i+3] ||
-                           ac[i+1] != bc[i+2] ||
-                           ac[i+2] != bc[i+1] ||
-                           ac[i+3] != bc[i+0])
-                               return 0;
-               }
-               return 1;
-       } else {
-               if (a[0]==b[0] &&
-                   a[1]==b[1] &&
-                   a[2]==b[2] &&
-                   a[3]==b[3])
-                       return 1;
-               return 0;
-       }
-}
-
-void copy_uuid(void *a, int b[4], int swapuuid)
-{
-       if (swapuuid) {
-               /* parse uuids are hostendian.
-                * uuid's from some superblocks are big-ending
-                * if there is a difference, we need to swap..
-                */
-               unsigned char *ac = (unsigned char *)a;
-               unsigned char *bc = (unsigned char *)b;
-               int i;
-               for (i = 0; i < 16; i += 4) {
-                       ac[i+0] = bc[i+3];
-                       ac[i+1] = bc[i+2];
-                       ac[i+2] = bc[i+1];
-                       ac[i+3] = bc[i+0];
-               }
-       } else
-               memcpy(a, b, 16);
-}
-
 char *__fname_from_uuid(int id[4], int swap, char *buf, char sep)
 {
        int i, j;
@@ -893,13 +805,14 @@ char *human_size(long long bytes)
 {
        static char buf[47];
 
-       /* We convert bytes to either centi-M{ega,ibi}bytes or
-        * centi-G{igi,ibi}bytes, with appropriate rounding,
-        * and then print 1/100th of those as a decimal.
+       /* We convert bytes to either centi-M{ega,ibi}bytes,
+        * centi-G{igi,ibi}bytes or centi-T{era,ebi}bytes
+        * with appropriate rounding, and then print
+        * 1/100th of those as a decimal.
         * We allow upto 2048Megabytes before converting to
-        * gigabytes, as that shows more precision and isn't
+        * gigabytes and 2048Gigabytes before converting to
+        * terabytes, as that shows more precision and isn't
         * too large a number.
-        * Terabytes are not yet handled.
         */
 
        if (bytes < 5000*1024)
@@ -909,11 +822,16 @@ char *human_size(long long bytes)
                long cMB  = (bytes / ( 1000000LL / 200LL ) +1) /2;
                snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
                        cMiB/100, cMiB % 100, cMB/100, cMB % 100);
-       } else {
+       } else if (bytes < 2*1024LL*1024LL*1024LL*1024LL) {
                long cGiB = (bytes * 200LL / (1LL<<30) +1) / 2;
                long cGB  = (bytes / (1000000000LL/200LL ) +1) /2;
                snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
                        cGiB/100, cGiB % 100, cGB/100, cGB % 100);
+       } else {
+               long cTiB = (bytes * 200LL / (1LL<<40) + 1) / 2;
+               long cTB  = (bytes / (1000000000000LL / 200LL) + 1) / 2;
+               snprintf(buf, sizeof(buf), " (%ld.%02ld TiB %ld.%02ld TB)",
+                       cTiB/100, cTiB % 100, cTB/100, cTB % 100);
        }
        return buf;
 }
@@ -922,13 +840,14 @@ char *human_size_brief(long long bytes, int prefix)
 {
        static char buf[30];
 
-       /* We convert bytes to either centi-M{ega,ibi}bytes or
-        * centi-G{igi,ibi}bytes, with appropriate rounding,
-        * and then print 1/100th of those as a decimal.
+       /* We convert bytes to either centi-M{ega,ibi}bytes,
+        * centi-G{igi,ibi}bytes or centi-T{era,ebi}bytes
+        * with appropriate rounding, and then print
+        * 1/100th of those as a decimal.
         * We allow upto 2048Megabytes before converting to
-        * gigabytes, as that shows more precision and isn't
+        * gigabytes and 2048Gigabytes before converting to
+        * terabytes, as that shows more precision and isn't
         * too large a number.
-        * Terabytes are not yet handled.
         *
         * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
         * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
@@ -941,10 +860,14 @@ char *human_size_brief(long long bytes, int prefix)
                        long cMiB = (bytes * 200LL / (1LL<<20) +1) /2;
                        snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
                                 cMiB/100, cMiB % 100);
-               } else {
+               } else if (bytes < 2*1024LL*1024LL*1024LL*1024LL) {
                        long cGiB = (bytes * 200LL / (1LL<<30) +1) /2;
                        snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
                                 cGiB/100, cGiB % 100);
+               } else {
+                       long cTiB = (bytes * 200LL / (1LL<<40) + 1) / 2;
+                       snprintf(buf, sizeof(buf), "%ld.%02ldTiB",
+                                cTiB/100, cTiB % 100);
                }
        }
        else if (prefix == JEDEC) {
@@ -952,10 +875,14 @@ char *human_size_brief(long long bytes, int prefix)
                        long cMB  = (bytes / ( 1000000LL / 200LL ) +1) /2;
                        snprintf(buf, sizeof(buf), "%ld.%02ldMB",
                                 cMB/100, cMB % 100);
-               } else {
+               } else if (bytes < 2*1024LL*1024LL*1024LL*1024LL) {
                        long cGB  = (bytes / (1000000000LL/200LL ) +1) /2;
                        snprintf(buf, sizeof(buf), "%ld.%02ldGB",
                                 cGB/100, cGB % 100);
+               } else {
+                       long cTB  = (bytes / (1000000000000LL / 200LL) + 1) / 2;
+                       snprintf(buf, sizeof(buf), "%ld.%02ldTB",
+                                cTB/100, cTB % 100);
                }
        }
        else
@@ -1015,12 +942,12 @@ dev_t devnm2devid(char *devnm)
        /* First look in /sys/block/$DEVNM/dev for %d:%d
         * If that fails, try parsing out a number
         */
-       char path[100];
+       char path[PATH_MAX];
        char *ep;
        int fd;
        int mjr,mnr;
 
-       sprintf(path, "/sys/block/%s/dev", devnm);
+       snprintf(path, sizeof(path), "/sys/block/%s/dev", devnm);
        fd = open(path, O_RDONLY);
        if (fd >= 0) {
                char buf[20];
@@ -1046,47 +973,74 @@ dev_t devnm2devid(char *devnm)
        return 0;
 }
 
-char *get_md_name(char *devnm)
+/**
+ * is_devname_numbered() - helper for numbered devname verification.
+ * @devname: path or name to check.
+ * @pref: expected devname prefix.
+ * @pref_len: prefix len.
+ */
+static bool is_devname_numbered(const char *devname, const char *pref, const int pref_len)
 {
-       /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
-       /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
+       int val;
 
-       static char devname[50];
-       struct stat stb;
-       dev_t rdev = devnm2devid(devnm);
-       char *dn;
+       assert(devname && pref);
 
-       if (rdev == 0)
-               return 0;
-       if (strncmp(devnm, "md_", 3) == 0) {
-               snprintf(devname, sizeof(devname), "/dev/md/%s",
-                       devnm + 3);
-               if (stat(devname, &stb) == 0 &&
-                   (S_IFMT&stb.st_mode) == S_IFBLK && (stb.st_rdev == rdev))
-                       return devname;
-       }
-       snprintf(devname, sizeof(devname), "/dev/%s", devnm);
-       if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK &&
-           (stb.st_rdev == rdev))
-               return devname;
+       if (strncmp(devname, pref, pref_len) != 0)
+               return false;
 
-       snprintf(devname, sizeof(devname), "/dev/md/%s", devnm+2);
-       if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK &&
-           (stb.st_rdev == rdev))
-               return devname;
+       if (parse_num(&val, devname + pref_len) != 0)
+               return false;
+
+       if (val > 127)
+               return false;
+
+       return true;
+}
+
+/**
+ * is_devname_md_numbered() - check if &devname is numbered MD device (md).
+ * @devname: path or name to check.
+ */
+bool is_devname_md_numbered(const char *devname)
+{
+       return is_devname_numbered(devname, DEV_NUM_PREF, DEV_NUM_PREF_LEN);
+}
+
+/**
+ * is_devname_md_d_numbered() - check if &devname is secondary numbered MD device (md_d).
+ * @devname: path or name to check.
+ */
+bool is_devname_md_d_numbered(const char *devname)
+{
+       static const char d_dev[] = DEV_NUM_PREF "_d";
+
+       return is_devname_numbered(devname, d_dev, sizeof(d_dev) - 1);
+}
 
-       dn = map_dev(major(rdev), minor(rdev), 0);
-       if (dn)
-               return dn;
-       snprintf(devname, sizeof(devname), "/dev/.tmp.%s", devnm);
-       if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
-               if (errno != EEXIST)
-                       return NULL;
+/**
+ * get_md_name() - Get main dev node of the md device.
+ * @devnm: Md device name or path.
+ *
+ * Function checks if the full name was passed and returns md name
+ * if it is the MD device.
+ *
+ * Return: Main dev node of the md device or NULL if not found.
+ */
+char *get_md_name(char *devnm)
+{
+       static char devname[NAME_MAX];
+       struct stat stb;
+
+       if (strncmp(devnm, "/dev/", 5) == 0)
+               snprintf(devname, sizeof(devname), "%s", devnm);
+       else
+               snprintf(devname, sizeof(devname), "/dev/%s", devnm);
 
-       if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK &&
-           (stb.st_rdev == rdev))
+       if (!is_mddev(devname))
+               return NULL;
+       if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK)
                return devname;
-       unlink(devname);
+
        return NULL;
 }
 
@@ -1105,6 +1059,20 @@ int get_maj_min(char *dev, int *major, int *minor)
                *e == 0);
 }
 
+/**
+ * is_bit_set() - get bit value by index.
+ * @val: value.
+ * @index: index of the bit (LSB numbering).
+ *
+ * Return: bit value.
+ */
+bool is_bit_set(int *val, unsigned char index)
+{
+       if ((*val) & (1 << index))
+               return true;
+       return false;
+}
+
 int dev_open(char *dev, int flags)
 {
        /* like 'open', but if 'dev' matches %d:%d, create a temp
@@ -1163,7 +1131,7 @@ int open_dev_excl(char *devnm)
        int i;
        int flags = O_RDWR;
        dev_t devid = devnm2devid(devnm);
-       long delay = 1000;
+       unsigned int delay = 1; // miliseconds
 
        sprintf(buf, "%d:%d", major(devid), minor(devid));
        for (i = 0; i < 25; i++) {
@@ -1176,8 +1144,8 @@ int open_dev_excl(char *devnm)
                }
                if (errno != EBUSY)
                        return fd;
-               usleep(delay);
-               if (delay < 200000)
+               sleep_for(0, MSEC_TO_NSEC(delay), true);
+               if (delay < 200)
                        delay *= 2;
        }
        return -1;
@@ -1201,7 +1169,7 @@ void wait_for(char *dev, int fd)
 {
        int i;
        struct stat stb_want;
-       long delay = 1000;
+       unsigned int delay = 1; // miliseconds
 
        if (fstat(fd, &stb_want) != 0 ||
            (stb_want.st_mode & S_IFMT) != S_IFBLK)
@@ -1213,8 +1181,8 @@ void wait_for(char *dev, int fd)
                    (stb.st_mode & S_IFMT) == S_IFBLK &&
                    (stb.st_rdev == stb_want.st_rdev))
                        return;
-               usleep(delay);
-               if (delay < 200000)
+               sleep_for(0, MSEC_TO_NSEC(delay), true);
+               if (delay < 200)
                        delay *= 2;
        }
        if (i == 25)
@@ -1241,6 +1209,11 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
        int i;
        char *subarray = NULL;
        char container[32] = "";
+       char *devnm = NULL;
+
+       devnm = fd2devnm(fd);
+       if (!devnm)
+               return NULL;
 
        sra = sysfs_read(fd, NULL, GET_VERSION);
 
@@ -1286,7 +1259,7 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
                if (subarrayp)
                        *subarrayp = subarray;
                strcpy(st->container_devnm, container);
-               strcpy(st->devnm, fd2devnm(fd));
+               strncpy(st->devnm, devnm, MD_NAME_MAX - 1);
        } else
                free(subarray);
 
@@ -1611,7 +1584,7 @@ int open_container(int fd)
        /* 'fd' is a block device.  Find out if it is in use
         * by a container, and return an open fd on that container.
         */
-       char path[256];
+       char path[288];
        char *e;
        DIR *dir;
        struct dirent *de;
@@ -1899,7 +1872,7 @@ int hot_remove_disk(int mdfd, unsigned long dev, int force)
        while ((ret = ioctl(mdfd, HOT_REMOVE_DISK, dev)) == -1 &&
               errno == EBUSY &&
               cnt-- > 0)
-               usleep(10000);
+               sleep_for(0, MSEC_TO_NSEC(10), true);
 
        return ret;
 }
@@ -1912,7 +1885,7 @@ int sys_hot_remove_disk(int statefd, int force)
        while ((ret = write(statefd, "remove", 6)) == -1 &&
               errno == EBUSY &&
               cnt-- > 0)
-               usleep(10000);
+               sleep_for(0, MSEC_TO_NSEC(10), true);
        return ret == 6 ? 0 : -1;
 }
 
@@ -1983,10 +1956,11 @@ int mdmon_running(char *devnm)
 
 int start_mdmon(char *devnm)
 {
-       int i, skipped;
+       int i;
        int len;
        pid_t pid;
        int status;
+       char *prefix = in_initrd() ? "initrd-" : "";
        char pathbuf[1024];
        char *paths[4] = {
                pathbuf,
@@ -1997,7 +1971,10 @@ int start_mdmon(char *devnm)
 
        if (check_env("MDADM_NO_MDMON"))
                return 0;
+       if (continue_via_systemd(devnm, MDMON_SERVICE, prefix))
+               return 0;
 
+       /* That failed, try running mdmon directly */
        len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf)-1);
        if (len > 0) {
                char *sl;
@@ -2011,51 +1988,9 @@ int start_mdmon(char *devnm)
        } else
                pathbuf[0] = '\0';
 
-       /* First try to run systemctl */
-       if (!check_env("MDADM_NO_SYSTEMCTL"))
-               switch(fork()) {
-               case 0:
-                       /* FIXME yuk. CLOSE_EXEC?? */
-                       skipped = 0;
-                       for (i = 3; skipped < 20; i++)
-                               if (close(i) < 0)
-                                       skipped++;
-                               else
-                                       skipped = 0;
-
-                       /* Don't want to see error messages from
-                        * systemctl.  If the service doesn't exist,
-                        * we start mdmon ourselves.
-                        */
-                       close(2);
-                       open("/dev/null", O_WRONLY);
-                       snprintf(pathbuf, sizeof(pathbuf), "mdmon@%s.service",
-                                devnm);
-                       status = execl("/usr/bin/systemctl", "systemctl",
-                                      "start",
-                                      pathbuf, NULL);
-                       status = execl("/bin/systemctl", "systemctl", "start",
-                                      pathbuf, NULL);
-                       exit(1);
-               case -1: pr_err("cannot run mdmon. Array remains readonly\n");
-                       return -1;
-               default: /* parent - good */
-                       pid = wait(&status);
-                       if (pid >= 0 && status == 0)
-                               return 0;
-               }
-
-       /* That failed, try running mdmon directly */
        switch(fork()) {
        case 0:
-               /* FIXME yuk. CLOSE_EXEC?? */
-               skipped = 0;
-               for (i = 3; skipped < 20; i++)
-                       if (close(i) < 0)
-                               skipped++;
-                       else
-                               skipped = 0;
-
+               manage_fork_fds(1);
                for (i = 0; paths[i]; i++)
                        if (paths[i][0]) {
                                execl(paths[i], paths[i],
@@ -2260,17 +2195,84 @@ void enable_fds(int devices)
        setrlimit(RLIMIT_NOFILE, &lim);
 }
 
+/* Close all opened descriptors if needed and redirect
+ * streams to /dev/null.
+ * For debug purposed, leave STDOUT and STDERR untouched
+ * Returns:
+ *     1- if any error occurred
+ *     0- otherwise
+ */
+void manage_fork_fds(int close_all)
+{
+       DIR *dir;
+       struct dirent *dirent;
+
+       close(0);
+       open("/dev/null", O_RDWR);
+
+#ifndef DEBUG
+       dup2(0, 1);
+       dup2(0, 2);
+#endif
+
+       if (close_all == 0)
+               return;
+
+       dir = opendir("/proc/self/fd");
+       if (!dir) {
+               pr_err("Cannot open /proc/self/fd directory.\n");
+               return;
+       }
+       for (dirent = readdir(dir); dirent; dirent = readdir(dir)) {
+               int fd = -1;
+
+               if ((strcmp(dirent->d_name, ".") == 0) ||
+                   (strcmp(dirent->d_name, "..")) == 0)
+                       continue;
+
+               fd = strtol(dirent->d_name, NULL, 10);
+               if (fd > 2)
+                       close(fd);
+       }
+}
+
+/* In a systemd/udev world, it is best to get systemd to
+ * run daemon rather than running in the background.
+ * Returns:
+ *     1- if systemd service has been started
+ *     0- otherwise
+ */
+int continue_via_systemd(char *devnm, char *service_name, char *prefix)
+{
+       int pid, status;
+       char pathbuf[1024];
+
+       /* Simply return that service cannot be started */
+       if (check_env("MDADM_NO_SYSTEMCTL"))
+               return 0;
+       switch (fork()) {
+       case  0:
+               manage_fork_fds(1);
+               snprintf(pathbuf, sizeof(pathbuf),
+                        "%s@%s%s.service", service_name, prefix ?: "", devnm);
+               status = execl("/usr/bin/systemctl", "systemctl", "restart",
+                              pathbuf, NULL);
+               status = execl("/bin/systemctl", "systemctl", "restart",
+                              pathbuf, NULL);
+               exit(1);
+       case -1: /* Just do it ourselves. */
+               break;
+       default: /* parent - good */
+               pid = wait(&status);
+               if (pid >= 0 && status == 0)
+                       return 1;
+       }
+       return 0;
+}
+
 int in_initrd(void)
 {
-       /* This is based on similar function in systemd. */
-       struct statfs s;
-       /* statfs.f_type is signed long on s390x and MIPS, causing all
-          sorts of sign extension problems with RAMFS_MAGIC being
-          defined as 0x858458f6 */
-       return  statfs("/", &s) >= 0 &&
-               ((unsigned long)s.f_type == TMPFS_MAGIC ||
-                ((unsigned long)s.f_type & 0xFFFFFFFFUL) ==
-                ((unsigned long)RAMFS_MAGIC & 0xFFFFFFFFUL));
+       return access("/etc/initrd-release", F_OK) >= 0;
 }
 
 void reopen_mddev(int mdfd)
@@ -2417,3 +2419,72 @@ out:
        close(fd_zero);
        return ret;
 }
+
+/**
+ * sleep_for() - Sleeps for specified time.
+ * @sec: Seconds to sleep for.
+ * @nsec: Nanoseconds to sleep for, has to be less than one second.
+ * @wake_after_interrupt: If set, wake up if interrupted.
+ *
+ * Function immediately returns if error different than EINTR occurs.
+ */
+void sleep_for(unsigned int sec, long nsec, bool wake_after_interrupt)
+{
+       struct timespec delay = {.tv_sec = sec, .tv_nsec = nsec};
+
+       assert(nsec < MSEC_TO_NSEC(1000));
+
+       do {
+               errno = 0;
+               nanosleep(&delay, &delay);
+               if (errno != 0 && errno != EINTR) {
+                       pr_err("Error sleeping for %us %ldns: %s\n", sec, nsec, strerror(errno));
+                       return;
+               }
+       } while (!wake_after_interrupt && errno == EINTR);
+}
+
+/* is_directory() - Checks if directory provided by path is indeed a regular directory.
+ * @path: directory path to be checked
+ *
+ * Doesn't accept symlinks.
+ *
+ * Return: true if is a directory, false if not
+ */
+bool is_directory(const char *path)
+{
+       struct stat st;
+
+       if (lstat(path, &st) != 0) {
+               pr_err("%s: %s\n", strerror(errno), path);
+               return false;
+       }
+
+       if (!S_ISDIR(st.st_mode))
+               return false;
+
+       return true;
+}
+
+/*
+ * is_file() - Checks if file provided by path is indeed a regular file.
+ * @path: file path to be checked
+ *
+ * Doesn't accept symlinks.
+ *
+ * Return: true if is  a file, false if not
+ */
+bool is_file(const char *path)
+{
+       struct stat st;
+
+       if (lstat(path, &st) != 0) {
+               pr_err("%s: %s\n", strerror(errno), path);
+               return false;
+       }
+
+       if (!S_ISREG(st.st_mode))
+               return false;
+
+       return true;
+}