]>
Commit | Line | Data |
---|---|---|
832b75ed GG |
1 | /* |
2 | * utility.h | |
3 | * | |
4 | * Home page of code is: http://smartmontools.sourceforge.net | |
5 | * | |
34ad0c5f | 6 | * Copyright (C) 2002-8 Bruce Allen <smartmontools-support@lists.sourceforge.net> |
832b75ed GG |
7 | * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org> |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2, or (at your option) | |
12 | * any later version. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * (for example COPYING); if not, write to the Free | |
16 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | * | |
18 | * This code was originally developed as a Senior Thesis by Michael Cornwell | |
19 | * at the Concurrent Systems Laboratory (now part of the Storage Systems | |
20 | * Research Center), Jack Baskin School of Engineering, University of | |
21 | * California, Santa Cruz. http://ssrc.soe.ucsc.edu/ | |
22 | * | |
23 | */ | |
24 | ||
25 | #ifndef UTILITY_H_ | |
26 | #define UTILITY_H_ | |
27 | ||
34ad0c5f | 28 | #define UTILITY_H_CVSID "$Id: utility.h,v 1.51 2008/03/04 22:09:47 ballen4705 Exp $\n" |
832b75ed GG |
29 | |
30 | #include <time.h> | |
31 | #include <sys/types.h> // for regex.h (according to POSIX) | |
32 | #include <regex.h> | |
33 | ||
34 | #ifndef HAVE_WORKING_SNPRINTF | |
35 | // Substitute by safe replacement functions | |
36 | #include <stdarg.h> | |
37 | int safe_snprintf(char *buf, int size, const char *fmt, ...); | |
38 | int safe_vsnprintf(char *buf, int size, const char *fmt, va_list ap); | |
39 | #define snprintf safe_snprintf | |
40 | #define vsnprintf safe_vsnprintf | |
41 | #endif | |
42 | ||
43 | // Utility function prints current date and time and timezone into a | |
44 | // character buffer of length>=64. All the fuss is needed to get the | |
45 | // right timezone info (sigh). | |
46 | #define DATEANDEPOCHLEN 64 | |
47 | void dateandtimezone(char *buffer); | |
48 | // Same, but for time defined by epoch tval | |
49 | void dateandtimezoneepoch(char *buffer, time_t tval); | |
50 | ||
51 | // utility function for printing out CVS strings | |
52 | #define CVSMAXLEN 1024 | |
53 | void printone(char *block, const char *cvsid); | |
54 | ||
55 | // like printf() except that we can control it better. Note -- | |
56 | // although the prototype is given here in utility.h, the function | |
57 | // itself is defined differently in smartctl and smartd. So the | |
58 | // function definition(s) are in smartd.c and in smartctl.c. | |
59 | #ifndef __GNUC__ | |
60 | #define __attribute__(x) /* nothing */ | |
61 | #endif | |
4d59bff9 | 62 | void pout(const char *fmt, ...) |
832b75ed GG |
63 | __attribute__ ((format (printf, 1, 2))); |
64 | ||
65 | // replacement for perror() with redirected output. | |
66 | void syserror(const char *message); | |
67 | ||
68 | // Prints a warning message for a failed regular expression compilation from | |
69 | // regcomp(). | |
70 | void printregexwarning(int errcode, regex_t *compiled); | |
71 | ||
72 | // A wrapper for regcomp(). Returns zero for success, non-zero otherwise. | |
73 | int compileregex(regex_t *compiled, const char *pattern, int cflags); | |
74 | ||
75 | // Function for processing -r option in smartctl and smartd | |
76 | int split_report_arg(char *s, int *i); | |
77 | // Function for processing -c option in smartctl and smartd | |
78 | int split_report_arg2(char *s, int *i); | |
a37e7145 GG |
79 | |
80 | // Possible values for smartselectivemode | |
81 | #define SEL_RANGE 0 // MIN-MAX | |
82 | #define SEL_REDO 1 // redo this | |
83 | #define SEL_NEXT 2 // do next range | |
84 | #define SEL_CONT 3 // redo or next depending of last test status | |
832b75ed | 85 | // Function for processing -t selective... option in smartctl |
a37e7145 | 86 | int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode); |
832b75ed GG |
87 | |
88 | ||
89 | // Guess device type (ata or scsi) based on device name | |
90 | // Guessing will now use Controller Type defines below | |
91 | ||
92 | int guess_device_type(const char * dev_name); | |
93 | ||
94 | // Create and return the list of devices to probe automatically | |
95 | // if the DEVICESCAN option is in the smartd config file | |
96 | int make_device_names (char ***devlist, const char* name); | |
97 | ||
98 | ||
99 | #define EXIT(x) { exitstatus = (x); exit((x)); } | |
100 | ||
101 | // replacement for calloc() that tracks memory usage | |
102 | void *Calloc(size_t nmemb, size_t size); | |
103 | ||
104 | // Utility function to free memory | |
4d59bff9 GG |
105 | void *FreeNonZero1(void* address, int size, int whatline, const char* file); |
106 | ||
107 | // Typesafe version of above | |
108 | template <class T> | |
109 | inline T * FreeNonZero(T * address, int size, int whatline, const char* file) | |
110 | { return (T *)FreeNonZero1((void *)address, size, whatline, file); } | |
832b75ed GG |
111 | |
112 | // A custom version of strdup() that keeps track of how much memory is | |
113 | // being allocated. If mustexist is set, it also throws an error if we | |
114 | // try to duplicate a NULL string. | |
a37e7145 | 115 | char *CustomStrDup(const char *ptr, int mustexist, int whatline, const char* file); |
832b75ed GG |
116 | |
117 | // To help with memory checking. Use when it is known that address is | |
118 | // NOT null. | |
4d59bff9 GG |
119 | void *CheckFree1(void *address, int whatline, const char* file); |
120 | ||
121 | // Typesafe version of above | |
122 | template <class T> | |
123 | inline T * CheckFree(T * address, int whatline, const char* file) | |
124 | { return (T *)CheckFree1((void *)address, whatline, file); } | |
832b75ed GG |
125 | |
126 | // This function prints either to stdout or to the syslog as needed | |
127 | ||
128 | // [From GLIBC Manual: Since the prototype doesn't specify types for | |
129 | // optional arguments, in a call to a variadic function the default | |
130 | // argument promotions are performed on the optional argument | |
131 | // values. This means the objects of type char or short int (whether | |
132 | // signed or not) are promoted to either int or unsigned int, as | |
133 | // appropriate.] | |
4d59bff9 | 134 | void PrintOut(int priority, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); |
832b75ed GG |
135 | |
136 | // run time, determine byte ordering | |
137 | int isbigendian(); | |
138 | ||
139 | // This value follows the peripheral device type value as defined in | |
140 | // SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in | |
141 | // the ATA standard for packet devices to define the device type. | |
142 | const char *packetdevicetype(int type); | |
143 | ||
144 | int deviceopen(const char *pathname, char *type); | |
145 | ||
146 | int deviceclose(int fd); | |
147 | ||
148 | // Optional functions of os_*.c | |
149 | #ifdef HAVE_GET_OS_VERSION_STR | |
150 | // Return build host and OS version as static string | |
151 | const char * get_os_version_str(void); | |
152 | #endif | |
153 | ||
154 | // returns 1 if any of the n bytes are nonzero, else zero. | |
155 | int nonempty(unsigned char *testarea,int n); | |
156 | ||
157 | // needed to fix glibc bug | |
158 | void FixGlibcTimeZoneBug(); | |
159 | ||
160 | // convert time in msec to a text string | |
161 | void MsecToText(unsigned int msec, char *txt); | |
162 | ||
163 | // Exit codes | |
164 | #define EXIT_BADCMD 1 // command line did not parse | |
165 | #define EXIT_BADCONF 2 // syntax error in config file | |
166 | #define EXIT_STARTUP 3 // problem forking daemon | |
167 | #define EXIT_PID 4 // problem creating pid file | |
168 | #define EXIT_NOCONF 5 // config file does not exist | |
169 | #define EXIT_READCONF 6 // config file exists but cannot be read | |
170 | ||
171 | #define EXIT_NOMEM 8 // out of memory | |
172 | #define EXIT_BADCODE 10 // internal error - should NEVER happen | |
173 | ||
174 | #define EXIT_BADDEV 16 // we can't monitor this device | |
175 | #define EXIT_NODEV 17 // no devices to monitor | |
176 | ||
177 | #define EXIT_SIGNAL 254 // abort on signal | |
178 | ||
179 | ||
180 | // macros to control printing | |
181 | #define PRINT_ON(control) {if (control->printing_switchable) control->dont_print=0;} | |
182 | #define PRINT_OFF(control) {if (control->printing_switchable) control->dont_print=1;} | |
183 | ||
184 | // possible values for controller_type in extern.h | |
185 | #define CONTROLLER_UNKNOWN 0x00 | |
186 | #define CONTROLLER_ATA 0x01 | |
187 | #define CONTROLLER_SCSI 0x02 | |
188 | #define CONTROLLER_3WARE 0x03 // set by -d option, but converted to one of three types below | |
189 | #define CONTROLLER_3WARE_678K 0x04 // NOT set by guess_device_type() | |
190 | #define CONTROLLER_3WARE_9000_CHAR 0x05 // set by guess_device_type() | |
191 | #define CONTROLLER_3WARE_678K_CHAR 0x06 // set by guess_device_type() | |
192 | #define CONTROLLER_MARVELL_SATA 0x07 // SATA drives behind Marvell controllers | |
4d59bff9 GG |
193 | #define CONTROLLER_SAT 0x08 // SATA device behind a SCSI ATA Translation (SAT) layer |
194 | #define CONTROLLER_HPT 0x09 // SATA drives behind HighPoint Raid controllers | |
ba59cff1 | 195 | #define CONTROLLER_CCISS 0x10 // CCISS controller |
a37e7145 | 196 | #define CONTROLLER_PARSEDEV 0x11 // "smartctl -r ataioctl,2 ..." output parser pseudo-device |
832b75ed GG |
197 | |
198 | #endif |