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