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