LCOV - code coverage report
Current view: top level - cmdline - portable.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 2 2 100.0 %
Date: 2016-11-21 Functions: 1 1 100.0 %

          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 targetting 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             : /**
      50             :  * Enable the rand_s() function.l
      51             :  */
      52             : #define _CRT_RAND_S
      53             : 
      54             : #include <windows.h>
      55             : #endif
      56             : 
      57             : /**
      58             :  * Specify the format attribute for printf.
      59             :  */
      60             : #ifdef __MINGW32__
      61             : #if defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 1
      62             : #define attribute_printf gnu_printf /* GNU format */
      63             : #else
      64             : #define attribute_printf ms_printf /* MSVCRT format */
      65             : #endif
      66             : #else
      67             : #define attribute_printf printf /* GNU format is the default one */
      68             : #endif
      69             : 
      70             : /**
      71             :  * Compiler extension
      72             :  */
      73             : #ifndef __always_inline
      74             : #define __always_inline inline __attribute__((always_inline))
      75             : #endif
      76             : 
      77             : #ifndef __noreturn
      78             : #define __noreturn __attribute__((noreturn))
      79             : #endif
      80             : 
      81             : 
      82             : /**
      83             :  * Architecture for inline assembly.
      84             :  */
      85             : #if HAVE_ASSEMBLY
      86             : #if defined(__i386__)
      87             : #define CONFIG_X86 1
      88             : #define CONFIG_X86_32 1
      89             : #endif
      90             : 
      91             : #if defined(__x86_64__)
      92             : #define CONFIG_X86 1
      93             : #define CONFIG_X86_64 1
      94             : #endif
      95             : #endif
      96             : 
      97             : /**
      98             :  * Includes some platform specific headers.
      99             :  */
     100             : #if HAVE_SYS_PARAM_H
     101             : #include <sys/param.h>
     102             : #endif
     103             : 
     104             : #if HAVE_SYS_MOUNT_H
     105             : #include <sys/mount.h>
     106             : #endif
     107             : 
     108             : #if HAVE_SYS_VFS_H
     109             : #include <sys/vfs.h>
     110             : #endif
     111             : 
     112             : #if HAVE_SYS_STATFS_H
     113             : #include <sys/statfs.h>
     114             : #endif
     115             : 
     116             : #if HAVE_SYS_FILE_H
     117             : #include <sys/file.h>
     118             : #endif
     119             : 
     120             : #if HAVE_SYS_IOCTL_H
     121             : #include <sys/ioctl.h>
     122             : #endif
     123             : 
     124             : #if HAVE_LINUX_FS_H
     125             : #include <linux/fs.h>
     126             : #endif
     127             : 
     128             : #if HAVE_LINUX_FIEMAP_H
     129             : #include <linux/fiemap.h>
     130             : #endif
     131             : 
     132             : #if HAVE_BLKID_BLKID_H
     133             : #include <blkid/blkid.h>
     134             : #if HAVE_BLKID_DEVNO_TO_DEVNAME && HAVE_BLKID_GET_TAG_VALUE
     135             : #define HAVE_BLKID 1
     136             : #endif
     137             : #endif
     138             : 
     139             : /**
     140             :  * Includes some standard headers.
     141             :  */
     142             : #include <stdio.h>
     143             : #include <stdlib.h> /* On many systems (e.g., Darwin), `stdio.h' is a prerequisite. */
     144             : #include <stdarg.h>
     145             : #include <string.h>
     146             : #include <ctype.h>
     147             : #include <fcntl.h>
     148             : #include <assert.h>
     149             : #include <errno.h>
     150             : #include <signal.h>
     151             : #include <limits.h>
     152             : 
     153             : #if HAVE_STDINT_H
     154             : #include <stdint.h>
     155             : #endif
     156             : 
     157             : #if HAVE_INTTYPES_H
     158             : #include <inttypes.h>
     159             : #endif
     160             : 
     161             : #if HAVE_UNISTD_H
     162             : #include <unistd.h>
     163             : #endif
     164             : 
     165             : #if TIME_WITH_SYS_TIME
     166             : #include <sys/time.h>
     167             : #include <time.h>
     168             : #else
     169             : #if HAVE_SYS_TIME_H
     170             : #include <sys/time.h>
     171             : #else
     172             : #include <time.h>
     173             : #endif
     174             : #endif
     175             : 
     176             : #if HAVE_MACH_MACH_TIME_H
     177             : #include <mach/mach_time.h>
     178             : #endif
     179             : 
     180             : #if HAVE_DIRENT_H
     181             : #include <dirent.h>
     182             : #define NAMLEN(dirent) strlen((dirent)->d_name)
     183             : #else
     184             : #define dirent direct
     185             : #define NAMLEN(dirent) (dirent)->d_namlen
     186             : #if HAVE_SYS_NDIR_H
     187             : #include <sys/ndir.h>
     188             : #endif
     189             : #if HAVE_SYS_DIR_H
     190             : #include <sys/dir.h>
     191             : #endif
     192             : #if HAVE_NDIR_H
     193             : #include <ndir.h>
     194             : #endif
     195             : #endif
     196             : 
     197             : #if HAVE_SYS_TYPES_H
     198             : #include <sys/types.h>
     199             : #endif
     200             : 
     201             : #if HAVE_SYS_STAT_H
     202             : #include <sys/stat.h>
     203             : #endif
     204             : 
     205             : #if HAVE_SYS_WAIT_H
     206             : #include <sys/wait.h>
     207             : #endif
     208             : #ifndef WEXITSTATUS
     209             : #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
     210             : #endif
     211             : #ifndef WIFEXITED
     212             : #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
     213             : #endif
     214             : 
     215             : #if HAVE_GETOPT_H
     216             : #include <getopt.h>
     217             : #endif
     218             : 
     219             : #if HAVE_FNMATCH_H
     220             : #include <fnmatch.h>
     221             : #else
     222             : #include "fnmatch.h"
     223             : #endif
     224             : 
     225             : #if HAVE_PTHREAD_H
     226             : #include <pthread.h>
     227             : #endif
     228             : 
     229             : #if HAVE_MATH_H
     230             : #include <math.h>
     231             : #endif
     232             : 
     233             : #if HAVE_EXECINFO_H
     234             : #include <execinfo.h>
     235             : #endif
     236             : 
     237             : /**
     238             :  * Enable thread use.
     239             :  */
     240             : #if HAVE_PTHREAD_CREATE
     241             : #define HAVE_PTHREAD 1
     242             : #endif
     243             : 
     244             : /**
     245             :  * Disable case check in Windows.
     246             :  */
     247             : #ifdef _WIN32
     248             : #define FNM_CASEINSENSITIVE_FOR_WIN FNM_CASEFOLD
     249             : #else
     250             : #define FNM_CASEINSENSITIVE_FOR_WIN 0
     251             : #endif
     252             : 
     253             : #if HAVE_IO_H
     254             : #include <io.h>
     255             : #endif
     256             : 
     257             : #if HAVE_GETOPT_LONG
     258             : #define SWITCH_GETOPT_LONG(a, b) a
     259             : #else
     260             : #define SWITCH_GETOPT_LONG(a, b) b
     261             : #endif
     262             : 
     263             : /**
     264             :  * Enables lock file support.
     265             :  */
     266             : #if HAVE_FLOCK && HAVE_FTRUNCATE
     267             : #define HAVE_LOCKFILE 1
     268             : #endif
     269             : 
     270             : /**
     271             :  * Basic block position type.
     272             :  * With 32 bits and 128k blocks you can address 256 TB.
     273             :  */
     274             : typedef uint32_t block_off_t;
     275             : 
     276             : /**
     277             :  * Basic data position type.
     278             :  * It's signed as file size and offset are usually signed.
     279             :  */
     280             : typedef int64_t data_off_t;
     281             : 
     282             : /**
     283             :  * Includes specific support for Windows or Linux.
     284             :  */
     285             : #ifdef __MINGW32__
     286             : #include "mingw.h"
     287             : #else
     288             : #include "unix.h"
     289             : #endif
     290             : 
     291             : /**
     292             :  * Include list support to have tommy_node.
     293             :  */
     294             : #include "tommyds/tommylist.h"
     295             : 
     296             : /**
     297             :  * Another name for link() to avoid confusion with local variables called "link".
     298             :  */
     299          12 : static inline int hardlink(const char* a, const char* b)
     300             : {
     301          12 :         return link(a, b);
     302             : }
     303             : 
     304             : /**
     305             :  * Get the device UUID.
     306             :  * Return 0 on success.
     307             :  */
     308             : int devuuid(uint64_t device, char* uuid, size_t size);
     309             : 
     310             : /**
     311             :  * Physical offset not yet read.
     312             :  */
     313             : #define FILEPHY_UNREAD_OFFSET 0
     314             : 
     315             : /**
     316             :  * Special value returned when the file-system doesn't report any offset for unknown reason.
     317             :  */
     318             : #define FILEPHY_UNREPORTED_OFFSET 1
     319             : 
     320             : /**
     321             :  * Special value returned when the file doesn't have a real offset.
     322             :  * For example, because it's stored in the NTFS MFT.
     323             :  */
     324             : #define FILEPHY_WITHOUT_OFFSET 2
     325             : 
     326             : /**
     327             :  * Value indicating real offsets. All offsets greater or equal at this one are real.
     328             :  */
     329             : #define FILEPHY_REAL_OFFSET 3
     330             : 
     331             : /**
     332             :  * Get the physcal address of the specified file.
     333             :  * This is expected to be just a hint and not necessarily correct or unique.
     334             :  * Return 0 on success.
     335             :  */
     336             : int filephy(const char* path, uint64_t size, uint64_t* physical);
     337             : 
     338             : /**
     339             :  * Check if the underline file-system support persistent inodes.
     340             :  * Return -1 on error, 0 on success.
     341             :  */
     342             : int fsinfo(const char* path, int* has_persistent_inode, uint64_t* total_space, uint64_t* free_space);
     343             : 
     344             : /**
     345             :  * Get the tick counter value.
     346             :  *
     347             :  * Note that the frequency is unspecified, because the time measure
     348             :  * is meant to be used to compare the ratio between usage times.
     349             :  */
     350             : uint64_t tick(void);
     351             : 
     352             : /**
     353             :  * Get the tick counter value in millisecond.
     354             :  */
     355             : uint64_t tick_ms(void);
     356             : 
     357             : /**
     358             :  * Initializes the system.
     359             :  */
     360             : void os_init(int opt);
     361             : 
     362             : /**
     363             :  * Deinitialize the system.
     364             :  */
     365             : void os_done(void);
     366             : 
     367             : /**
     368             :  * Abort the process with a stacktrace.
     369             :  */
     370             : void os_abort(void) __noreturn;
     371             : 
     372             : /**
     373             :  * Clear the screen.
     374             :  */
     375             : void os_clear(void);
     376             : 
     377             : /**
     378             :  * Log file.
     379             :  *
     380             :  * This stream if fully buffered.
     381             :  *
     382             :  * If no log file is selected, it's 0.
     383             :  */
     384             : FILE* stdlog;
     385             : 
     386             : /**
     387             :  * Exit codes for testing.
     388             :  */
     389             : int exit_success;
     390             : int exit_failure;
     391             : int exit_sync_needed;
     392             : #undef EXIT_SUCCESS
     393             : #undef EXIT_FAILURE
     394             : #define EXIT_SUCCESS exit_success
     395             : #define EXIT_FAILURE exit_failure
     396             : #define EXIT_SYNC_NEEDED exit_sync_needed
     397             : 
     398             : /**
     399             :  * Fill memory with pseudo-random values.
     400             :  */
     401             : int randomize(void* ptr, size_t size);
     402             : 
     403             : /**
     404             :  * Standard SMART attributes.
     405             :  */
     406             : #define SMART_START_STOP_COUNT 4
     407             : #define SMART_REALLOCATED_SECTOR_COUNT 5
     408             : #define SMART_POWER_ON_HOURS 9
     409             : #define SMART_AIRFLOW_TEMPERATURE_CELSIUS 190
     410             : #define SMART_LOAD_CYCLE_COUNT 193
     411             : #define SMART_TEMPERATURE_CELSIUS 194
     412             : 
     413             : /**
     414             :  * Additional SMART attributes.
     415             :  */
     416             : #define SMART_ERROR 256 /**< ATA Error count. */
     417             : #define SMART_SIZE 257 /**< Size in bytes. */
     418             : #define SMART_ROTATION_RATE 258 /**< Rotation speed. 0 for SSD. */
     419             : #define SMART_FLAGS 259 /**< Flags returned by smartctl. */
     420             : 
     421             : /**
     422             :  * SMART attributes count.
     423             :  */
     424             : #define SMART_COUNT 260
     425             : 
     426             : /**
     427             :  * Flags returned by smartctl.
     428             :  */
     429             : #define SMARTCTL_FLAG_UNSUPPORTED (1 << 0) /**< Device not recognized, requiring the -d option. */
     430             : #define SMARTCTL_FLAG_OPEN (1 << 1) /**< Device open or identification failed. */
     431             : #define SMARTCTL_FLAG_COMMAND (1 << 2) /**< Some SMART or ATA commands failed. This is a common error, also happening with full info gathering. */
     432             : #define SMARTCTL_FLAG_FAIL (1 << 3) /**< SMART status check returned "DISK FAILING". */
     433             : #define SMARTCTL_FLAG_PREFAIL (1 << 4) /**< We found prefail Attributes <= threshold. */
     434             : #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. */
     435             : #define SMARTCTL_FLAG_ERROR (1 << 6) /**< The device error log contains records of errors. */
     436             : #define SMARTCTL_FLAG_ERROR_LOGGED (1 << 7) /**< The device self-test log contains records of errors. */
     437             : 
     438             : /**
     439             :  * SMART max attribute length.
     440             :  */
     441             : #define SMART_MAX 64
     442             : 
     443             : /**
     444             :  * Value for unassigned SMART attribute.
     445             :  */
     446             : #define SMART_UNASSIGNED 0xFFFFFFFFFFFFFFFFULL
     447             : 
     448             : /**
     449             :  * Device info entry.
     450             :  */
     451             : struct devinfo_struct {
     452             :         uint64_t device; /**< Device ID. */
     453             :         char name[PATH_MAX]; /**< Name of the disk. */
     454             :         char mount[PATH_MAX]; /**< Mount point or other contained directory. */
     455             :         char smartctl[PATH_MAX]; /**< Options for smartctl. */
     456             :         char file[PATH_MAX]; /**< File device. */
     457             : #ifdef _WIN32
     458             :         char wfile[PATH_MAX]; /**< File device in Windows format. Like \\.\PhysicalDriveX, or \\?\Volume{X}. */
     459             : #endif
     460             :         struct devinfo_struct* parent; /**< Pointer at the parent if any. */
     461             :         uint64_t smart[SMART_COUNT]; /**< SMART raw attributes. */
     462             :         char smart_serial[SMART_MAX]; /**< SMART serial number. */
     463             :         char smart_vendor[SMART_MAX]; /**< SMART vendor. */
     464             :         char smart_model[SMART_MAX]; /**< SMART model. */
     465             : #if HAVE_PTHREAD
     466             :         pthread_t thread;
     467             : #endif
     468             :         tommy_node node;
     469             : };
     470             : typedef struct devinfo_struct devinfo_t;
     471             : 
     472             : #define DEVICE_LIST 0
     473             : #define DEVICE_DOWN 1
     474             : #define DEVICE_UP 2
     475             : #define DEVICE_SMART 3
     476             : 
     477             : /**
     478             :  * Query all the "high" level devices with the specified operation,
     479             :  * and produces a list of "low" level devices to operate on.
     480             :  *
     481             :  * The passed "low" device list must be already initialized.
     482             :  */
     483             : int devquery(tommy_list* high, tommy_list* low, int operation, int others);
     484             : 
     485             : #endif
     486             : 

Generated by: LCOV version 1.10