Line data Source code
1 : /*
2 : * Copyright (C) 2011 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 : #ifndef __PORTABLE_H
19 : #define __PORTABLE_H
20 :
21 : #if HAVE_CONFIG_H
22 : #include "config.h" /* Use " to include first in the same directory of this file */
23 : #endif
24 :
25 : /***************************************************************************/
26 : /* Config */
27 :
28 : #ifdef __MINGW32__
29 : /**
30 : * Enable the GNU printf functions instead of using the MSVCRT ones.
31 : *
32 : * Note that this is the default if _POSIX is also defined.
33 : * To disable it you have to set it to 0.
34 : */
35 : #define __USE_MINGW_ANSI_STDIO 1
36 :
37 : /**
38 : * Define the MSVCRT version targeting Windows Vista.
39 : */
40 : #define __MSVCRT_VERSION__ 0x0600
41 :
42 : /**
43 : * Include Windows Vista headers.
44 : *
45 : * Like for InitializeCriticalSection().
46 : */
47 : #define _WIN32_WINNT 0x600
48 :
49 : #include <windows.h>
50 : #endif
51 :
52 : /**
53 : * Specify the format attribute for printf.
54 : */
55 : #ifdef __MINGW32__
56 : #if defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 1
57 : #define attribute_printf gnu_printf /* GNU format */
58 : #else
59 : #define attribute_printf ms_printf /* MSVCRT format */
60 : #endif
61 : #else
62 : #define attribute_printf printf /* GNU format is the default one */
63 : #endif
64 :
65 : /**
66 : * Compiler extension
67 : */
68 : #ifndef __always_inline
69 : #define __always_inline inline __attribute__((always_inline))
70 : #endif
71 :
72 : #ifndef __noreturn
73 : #define __noreturn __attribute__((noreturn))
74 : #endif
75 :
76 :
77 : /**
78 : * Architecture for inline assembly.
79 : */
80 : #if HAVE_ASSEMBLY
81 : #if defined(__i386__)
82 : #define CONFIG_X86 1
83 : #define CONFIG_X86_32 1
84 : #endif
85 :
86 : #if defined(__x86_64__)
87 : #define CONFIG_X86 1
88 : #define CONFIG_X86_64 1
89 : #endif
90 : #endif
91 :
92 : /**
93 : * Includes some platform specific headers.
94 : */
95 : #if HAVE_SYS_PARAM_H
96 : #include <sys/param.h>
97 : #endif
98 :
99 : #if HAVE_SYS_MOUNT_H
100 : #include <sys/mount.h>
101 : #endif
102 :
103 : #if HAVE_SYS_VFS_H
104 : #include <sys/vfs.h>
105 : #endif
106 :
107 : #if HAVE_SYS_STATFS_H
108 : #include <sys/statfs.h>
109 : #endif
110 :
111 : #if HAVE_SYS_FILE_H
112 : #include <sys/file.h>
113 : #endif
114 :
115 : #if HAVE_SYS_IOCTL_H
116 : #include <sys/ioctl.h>
117 : #endif
118 :
119 : #if HAVE_LINUX_FS_H
120 : #include <linux/fs.h>
121 : #endif
122 :
123 : #if HAVE_LINUX_FIEMAP_H
124 : #include <linux/fiemap.h>
125 : #endif
126 :
127 : #if HAVE_BLKID_BLKID_H
128 : #include <blkid/blkid.h>
129 : #if HAVE_BLKID_DEVNO_TO_DEVNAME && HAVE_BLKID_GET_TAG_VALUE
130 : #define HAVE_BLKID 1
131 : #endif
132 : #endif
133 :
134 : /**
135 : * Includes some standard headers.
136 : */
137 : #include <stdio.h>
138 : #include <stdlib.h> /* On many systems (e.g., Darwin), `stdio.h' is a prerequisite. */
139 : #include <stdarg.h>
140 : #include <string.h>
141 : #include <ctype.h>
142 : #include <fcntl.h>
143 : #include <assert.h>
144 : #include <errno.h>
145 : #include <signal.h>
146 : #include <limits.h>
147 :
148 : #if HAVE_STDINT_H
149 : #include <stdint.h>
150 : #endif
151 :
152 : #if HAVE_INTTYPES_H
153 : #include <inttypes.h>
154 : #endif
155 :
156 : #if HAVE_UNISTD_H
157 : #include <unistd.h>
158 : #endif
159 :
160 : #if TIME_WITH_SYS_TIME
161 : #include <sys/time.h>
162 : #include <time.h>
163 : #else
164 : #if HAVE_SYS_TIME_H
165 : #include <sys/time.h>
166 : #else
167 : #include <time.h>
168 : #endif
169 : #endif
170 :
171 : #if HAVE_MACH_MACH_TIME_H
172 : #include <mach/mach_time.h>
173 : #endif
174 :
175 : #if HAVE_DIRENT_H
176 : #include <dirent.h>
177 : #define NAMLEN(dirent) strlen((dirent)->d_name)
178 : #else
179 : #define dirent direct
180 : #define NAMLEN(dirent) (dirent)->d_namlen
181 : #if HAVE_SYS_NDIR_H
182 : #include <sys/ndir.h>
183 : #endif
184 : #if HAVE_SYS_DIR_H
185 : #include <sys/dir.h>
186 : #endif
187 : #if HAVE_NDIR_H
188 : #include <ndir.h>
189 : #endif
190 : #endif
191 :
192 : #if HAVE_SYS_TYPES_H
193 : #include <sys/types.h>
194 : #endif
195 :
196 : #if HAVE_SYS_MKDEV
197 : #include <sys/mkdev.h>
198 : #endif
199 :
200 : #if HAVE_SYS_SYSMACROS_H
201 : #include <sys/sysmacros.h>
202 : #endif
203 :
204 : #if HAVE_SYS_STAT_H
205 : #include <sys/stat.h>
206 : #endif
207 :
208 : #if HAVE_SYS_WAIT_H
209 : #include <sys/wait.h>
210 : #endif
211 : #ifndef WEXITSTATUS
212 : #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
213 : #endif
214 : #ifndef WIFEXITED
215 : #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
216 : #endif
217 :
218 : #if HAVE_GETOPT_H
219 : #include <getopt.h>
220 : #endif
221 :
222 : #if HAVE_MATH_H
223 : #include <math.h>
224 : #endif
225 :
226 : #if HAVE_EXECINFO_H
227 : #include <execinfo.h>
228 : #endif
229 :
230 : #ifdef __APPLE__
231 : #include <CoreFoundation/CoreFoundation.h>
232 : #include <DiskArbitration/DiskArbitration.h>
233 : #endif
234 :
235 : /**
236 : * Enable thread use.
237 : */
238 : #ifdef _WIN32
239 : #define HAVE_THREAD 1
240 : typedef void* windows_thread_t;
241 : typedef CRITICAL_SECTION windows_mutex_t;
242 : typedef CONDITION_VARIABLE windows_cond_t;
243 : typedef void* windows_key_t;
244 : /* remap to pthread */
245 : #define thread_id_t windows_thread_t
246 : #define thread_mutex_t windows_mutex_t
247 : #define thread_cond_t windows_cond_t
248 : #define pthread_mutex_init windows_mutex_init
249 : #define pthread_mutex_destroy windows_mutex_destroy
250 : #define pthread_mutex_lock windows_mutex_lock
251 : #define pthread_mutex_unlock windows_mutex_unlock
252 : #define pthread_cond_init windows_cond_init
253 : #define pthread_cond_destroy windows_cond_destroy
254 : #define pthread_cond_signal windows_cond_signal
255 : #define pthread_cond_broadcast windows_cond_broadcast
256 : #define pthread_cond_wait windows_cond_wait
257 : #define pthread_create windows_create
258 : #define pthread_join windows_join
259 : #else
260 : #if HAVE_PTHREAD_H
261 : #include <pthread.h>
262 : #endif
263 : #if HAVE_PTHREAD_CREATE
264 : #define HAVE_THREAD 1
265 : typedef pthread_t thread_id_t;
266 : typedef pthread_mutex_t thread_mutex_t;
267 : typedef pthread_cond_t thread_cond_t;
268 : #endif
269 : #endif
270 :
271 : #if HAVE_IO_H
272 : #include <io.h>
273 : #endif
274 :
275 : #if HAVE_GETOPT_LONG
276 : #define SWITCH_GETOPT_LONG(a, b) a
277 : #else
278 : #define SWITCH_GETOPT_LONG(a, b) b
279 : #endif
280 :
281 : /**
282 : * Enables lock file support.
283 : */
284 : #if HAVE_FLOCK && HAVE_FTRUNCATE
285 : #define HAVE_LOCKFILE 1
286 : #endif
287 :
288 : /**
289 : * Basic block position type.
290 : * With 32 bits and 128k blocks you can address 256 TB.
291 : */
292 : typedef uint32_t block_off_t;
293 :
294 : /**
295 : * Basic data position type.
296 : * It's signed as file size and offset are usually signed.
297 : */
298 : typedef int64_t data_off_t;
299 :
300 : /**
301 : * Includes specific support for Windows or Linux.
302 : */
303 : #ifdef __MINGW32__
304 : #include "mingw.h"
305 : #else
306 : #include "unix.h"
307 : #endif
308 :
309 : /**
310 : * Include list support to have tommy_node.
311 : */
312 : #include "tommyds/tommylist.h"
313 :
314 : /**
315 : * Another name for link() to avoid confusion with local variables called "link".
316 : */
317 12 : static inline int hardlink(const char* a, const char* b)
318 : {
319 12 : return link(a, b);
320 : }
321 :
322 : /**
323 : * Get the device UUID.
324 : * Return 0 on success.
325 : */
326 : int devuuid(uint64_t device, const char* path, char* uuid, size_t size);
327 :
328 : /**
329 : * Physical offset not yet read.
330 : */
331 : #define FILEPHY_UNREAD_OFFSET 0
332 :
333 : /**
334 : * Special value returned when the file-system doesn't report any offset for unknown reason.
335 : */
336 : #define FILEPHY_UNREPORTED_OFFSET 1
337 :
338 : /**
339 : * Special value returned when the file doesn't have a real offset.
340 : * For example, because it's stored in the NTFS MFT.
341 : */
342 : #define FILEPHY_WITHOUT_OFFSET 2
343 :
344 : /**
345 : * Value indicating real offsets. All offsets greater or equal at this one are real.
346 : */
347 : #define FILEPHY_REAL_OFFSET 3
348 :
349 : /**
350 : * Get the physical address of the specified file.
351 : * This is expected to be just a hint and not necessarily correct or unique.
352 : * Return 0 on success.
353 : */
354 : int filephy(const char* path, uint64_t size, uint64_t* physical);
355 :
356 : /**
357 : * Check if the underline file-system support persistent inodes.
358 : * Return -1 on error, 0 on success.
359 : */
360 : int fsinfo(const char* path, int* has_persistent_inode, int* has_syncronized_hardlinks, uint64_t* total_space, uint64_t* free_space, char* fstype, size_t fstype_size, char* fslabel, size_t fslabel_size);
361 :
362 : /**
363 : * Get the tick counter value.
364 : *
365 : * Note that the frequency is unspecified, because the time measure
366 : * is meant to be used to compare the ratio between usage times.
367 : */
368 : uint64_t tick(void);
369 :
370 : /**
371 : * Get the tick counter value in millisecond.
372 : */
373 : uint64_t tick_ms(void);
374 :
375 : /**
376 : * Initializes the system.
377 : */
378 : void os_init(int opt);
379 :
380 : /**
381 : * Deinitialize the system.
382 : */
383 : void os_done(void);
384 :
385 : /**
386 : * Abort the process with a stacktrace.
387 : */
388 : void os_abort(void) __noreturn;
389 :
390 : /**
391 : * Clear the screen.
392 : */
393 : void os_clear(void);
394 :
395 : /**
396 : * Log file.
397 : *
398 : * This stream if fully buffered.
399 : *
400 : * If no log file is selected, it's 0.
401 : */
402 : extern FILE* stdlog;
403 :
404 : /**
405 : * Exit codes for testing.
406 : */
407 : extern int exit_success;
408 : extern int exit_failure;
409 : extern int exit_sync_needed;
410 : #undef EXIT_SUCCESS
411 : #undef EXIT_FAILURE
412 : #define EXIT_SUCCESS exit_success
413 : #define EXIT_FAILURE exit_failure
414 : #define EXIT_SYNC_NEEDED exit_sync_needed
415 :
416 : /**
417 : * Fill memory with pseudo-random values.
418 : */
419 : int randomize(void* ptr, size_t size);
420 :
421 : /**
422 : * Standard SMART attributes.
423 : */
424 : #define SMART_REALLOCATED_SECTOR_COUNT 5
425 : #define SMART_POWER_ON_HOURS 9
426 : #define SMART_POWER_CYCLE_COUNT 12
427 : #define SMART_UNCORRECTABLE_ERROR_CNT 187
428 : #define SMART_COMMAND_TIMEOUT 188
429 : #define SMART_CURRENT_PENDING_SECTOR 197
430 : #define SMART_OFFLINE_UNCORRECTABLE 198
431 : #define SMART_START_STOP_COUNT 4
432 : #define SMART_POWER_ON_HOURS 9
433 : #define SMART_AIRFLOW_TEMPERATURE_CELSIUS 190
434 : #define SMART_LOAD_CYCLE_COUNT 193
435 : #define SMART_TEMPERATURE_CELSIUS 194
436 :
437 : /**
438 : * Flags returned by smartctl via exit code
439 : */
440 : #define SMART_FLAGS 256
441 :
442 : /*
443 : * Counts command, transport, or controller-level errors reported by the device.
444 : * These reflect failed I/O operations not directly caused by media defects
445 : * (for example interface, firmware, or power-related errors).
446 : * This counter is cumulative and never resets to zero, even if the underlying
447 : * error condition is resolved.
448 : */
449 : #define SMART_ERROR_PROTOCOL 257
450 :
451 : /*
452 : * Counts media-level errors where data could not be reliably read or written.
453 : * These indicate actual storage surface or flash failures and may imply data loss.
454 : * This counter is cumulative and never resets to zero, even if the underlying
455 : * error condition is resolved.
456 : */
457 : #define SMART_ERROR_MEDIUM 258
458 :
459 : /**
460 : * Unified Wear Level Metric (0-100)
461 : *
462 : * Represents the remaining life of an SSD.
463 : * - 0: Brand new drive (0% wear).
464 : * - 100: Drive has reached or exceeded its manufacturer-rated design life.
465 : */
466 : #define SMART_WEAR_LEVEL 259
467 :
468 : /**
469 : * SMART attributes count.
470 : */
471 : #define SMART_COUNT 260
472 :
473 : /**
474 : * NVME custom SMART attributes
475 : *
476 : * The numbers are just arbitrary to put them in unused space
477 : */
478 : #define SMART_NVME_CRITICAL_WARNING 100
479 : #define SMART_NVME_AVAILABLE_SPARE 101
480 : #define SMART_NVME_DATA_UNITS_READ 102
481 : #define SMART_NVME_DATA_UNITS_WRITTEN 103
482 : #define SMART_NVME_HOST_READ_COMMANDS 104
483 : #define SMART_NVME_HOST_WRITE_COMMANDS 105
484 : #define SMART_NVME_CONTROLLER_BUSY_TIME 106
485 : #define SMART_NVME_UNSAFE_SHUTDOWNS 107
486 : #define SMART_NVME_WARNING_COMP_TEMPERATURE_TIME 108
487 : #define SMART_NVME_CRITICAL_COMP_TEMPERATURE_TIME 109
488 :
489 : /**
490 : * Info attributes.
491 : */
492 : #define INFO_SIZE 0 /**< Size in bytes. */
493 : #define INFO_ROTATION_RATE 1 /**< Rotation speed. 0 for SSD. */
494 :
495 : /**
496 : * Info attributes count.
497 : */
498 : #define INFO_COUNT 2
499 :
500 : /**
501 : * Max number of ignored smart attributes
502 : */
503 : #define SMART_IGNORE_MAX 4
504 :
505 : /**
506 : * Flags returned by smartctl.
507 : */
508 : #define SMARTCTL_FLAG_UNSUPPORTED (1 << 0) /**< Device not recognized, requiring the -d option. */
509 : #define SMARTCTL_FLAG_OPEN (1 << 1) /**< Device open or identification failed. */
510 : #define SMARTCTL_FLAG_COMMAND (1 << 2) /**< Some SMART or ATA commands failed. This is a common error, also happening with full info gathering. */
511 : #define SMARTCTL_FLAG_FAIL (1 << 3) /**< SMART status check returned "DISK FAILING". */
512 : #define SMARTCTL_FLAG_PREFAIL (1 << 4) /**< We found prefail Attributes <= threshold. */
513 : #define SMARTCTL_FLAG_PREFAIL_LOGGED (1 << 5) /**< SMART status check returned "DISK OK" but we found that some (usage or prefail) Attributes have been <= threshold at some time in the past. */
514 : #define SMARTCTL_FLAG_ERROR_LOGGED (1 << 6) /**< The device error log contains records of errors. */
515 : #define SMARTCTL_FLAG_SELFERROR_LOGGED (1 << 7) /**< The device self-test log contains records of errors. */
516 :
517 : /**
518 : * SMART max attribute length.
519 : */
520 : #define SMART_MAX 64
521 :
522 : /**
523 : * Value for unassigned SMART attribute.
524 : */
525 : #define SMART_UNASSIGNED 0xFFFFFFFFFFFFFFFFULL
526 :
527 : /**
528 : * SMART Attribute flags
529 : */
530 : #define SMART_ATTR_TYPE_PREFAIL 1
531 : #define SMART_ATTR_TYPE_OLDAGE 2
532 : #define SMART_ATTR_UPDATE_ALWAYS 4
533 : #define SMART_ATTR_UPDATE_OFFLINE 8
534 : #define SMART_ATTR_WHEN_FAILED_NOW 16
535 : #define SMART_ATTR_WHEN_FAILED_PAST 32
536 : #define SMART_ATTR_WHEN_FAILED_NEVER 64
537 :
538 : struct smart_attr {
539 : char name[128]; /**< SMART attribute name. */
540 : uint64_t raw; /**< SMART attributes raw. */
541 : uint64_t norm; /**< SMART attributes normalized. */
542 : uint64_t worst; /**< SMART attributes worst. */
543 : uint64_t thresh; /**< SMART attributes threshold. */
544 : int flags; /**< SMART_ATTR_* flags */
545 : };
546 :
547 : /**
548 : * Power mode
549 : */
550 : #define POWER_STANDBY 0
551 : #define POWER_ACTIVE 1
552 : #define POWER_UNKNOWN -1
553 :
554 : /**
555 : * Device info entry.
556 : */
557 : struct devinfo_struct {
558 : uint64_t device; /**< Device ID. */
559 : char name[PATH_MAX]; /**< Name of the disk combined with the split index if any */
560 : char mount[PATH_MAX]; /**< Mount point or other contained directory. */
561 : char smartctl[PATH_MAX]; /**< Options for smartctl. */
562 : int smartignore[SMART_IGNORE_MAX]; /**< Attribues to ignore */
563 : char file[PATH_MAX]; /**< File device. */
564 : #ifdef _WIN32
565 : char wfile[PATH_MAX]; /**< File device in Windows format. Like \\.\PhysicalDriveX, or \\?\Volume{X}. */
566 : #endif
567 : struct devinfo_struct* parent; /**< Pointer at the parent if any. */
568 : struct devinfo_struct* split; /**< Pointer at first split if any. */
569 : struct smart_attr smart[SMART_COUNT]; /**< All smart values. */
570 : uint64_t info[INFO_COUNT]; /**< Informational attributes not related to SMART telemetry. */
571 : uint64_t access_stat; /**< Access stat info. */
572 : char serial[SMART_MAX]; /**< Serial number. */
573 : char family[SMART_MAX]; /**< Family. */
574 : char model[SMART_MAX]; /**< Model. */
575 : char interf[SMART_MAX]; /**< Interface of the device: ata, sata, pata, nvme, usb */
576 : int power; /**< POWER mode. */
577 : #if HAVE_THREAD
578 : thread_id_t thread;
579 : #endif
580 : tommy_node node;
581 : };
582 : typedef struct devinfo_struct devinfo_t;
583 :
584 : void device_name_set(devinfo_t* dev, const char* name, int index);
585 :
586 : #define DEVICE_LIST 0
587 : #define DEVICE_DOWN 1
588 : #define DEVICE_UP 2
589 : #define DEVICE_SMART 3
590 : #define DEVICE_PROBE 4
591 : #define DEVICE_DOWNIFUP 5
592 :
593 : /**
594 : * Query all the "high" level devices with the specified operation,
595 : * and produces a list of "low" level devices to operate on.
596 : *
597 : * The passed "low" device list must be already initialized.
598 : */
599 : int devquery(tommy_list* high, tommy_list* low, int operation, int others);
600 :
601 : /**
602 : * Fill with fake data the device list.
603 : */
604 : int devtest(tommy_list* high, tommy_list* low, int operation);
605 :
606 : /**
607 : * Get the ambient temperature in degree
608 : *
609 : * Return 0 if not available.
610 : */
611 : int ambient_temperature(void);
612 :
613 : /**
614 : * Global variable to identify if Ctrl+C is pressed.
615 : */
616 : extern volatile int global_interrupt;
617 :
618 : /**
619 : * Size of the spaceholder file for Windows to avoid the message of low disk space
620 : */
621 : #define WINDOWS_SPACEHOLDER_SIZE (256 * 1024 * 1024)
622 :
623 : /**
624 : * Generic errors
625 : */
626 : #define EINTERNAL EFAULT /**< Internal assertion failed. */
627 : #define EDATA EIO /**< Silent data corruption. */
628 : #define ESOFT EINVAL /**< Software error, like permission denied. */
629 : #define EUSER EINVAL /**< Invalid value specified by the user. */
630 : #define EEXTERNAL EINVAL /**< Invalid external interface behaviour. */
631 : #define ECONTENT EINVAL /**< Invalid content file. */
632 : #define EENVIRONMENT EINVAL /**< Invalid physical environment, like temperature too high. */
633 :
634 : #endif
635 :
|