]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - utility.h
2b23d51e04d1c00505c13d752e1296b0f01e3927
[mirror_smartmontools-debian.git] / utility.h
1 /*
2 * utility.h
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2002-6 Bruce Allen <smartmontools-support@lists.sourceforge.net>
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
28 #define UTILITY_H_CVSID "$Id: utility.h,v 1.46 2006/08/25 06:06:25 sxzzsf Exp $\n"
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
62 void pout(const char *fmt, ...)
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);
79 // Function for processing -t selective... option in smartctl
80 int split_selective_arg(char *s, uint64_t *start, uint64_t *stop);
81
82
83 // Guess device type (ata or scsi) based on device name
84 // Guessing will now use Controller Type defines below
85
86 int guess_device_type(const char * dev_name);
87
88 // Create and return the list of devices to probe automatically
89 // if the DEVICESCAN option is in the smartd config file
90 int make_device_names (char ***devlist, const char* name);
91
92
93 #define EXIT(x) { exitstatus = (x); exit((x)); }
94
95 // replacement for calloc() that tracks memory usage
96 void *Calloc(size_t nmemb, size_t size);
97
98 // Utility function to free memory
99 void *FreeNonZero1(void* address, int size, int whatline, const char* file);
100
101 // Typesafe version of above
102 template <class T>
103 inline T * FreeNonZero(T * address, int size, int whatline, const char* file)
104 { return (T *)FreeNonZero1((void *)address, size, whatline, file); }
105
106 // A custom version of strdup() that keeps track of how much memory is
107 // being allocated. If mustexist is set, it also throws an error if we
108 // try to duplicate a NULL string.
109 char *CustomStrDup(char *ptr, int mustexist, int whatline, const char* file);
110
111 // To help with memory checking. Use when it is known that address is
112 // NOT null.
113 void *CheckFree1(void *address, int whatline, const char* file);
114
115 // Typesafe version of above
116 template <class T>
117 inline T * CheckFree(T * address, int whatline, const char* file)
118 { return (T *)CheckFree1((void *)address, whatline, file); }
119
120 // This function prints either to stdout or to the syslog as needed
121
122 // [From GLIBC Manual: Since the prototype doesn't specify types for
123 // optional arguments, in a call to a variadic function the default
124 // argument promotions are performed on the optional argument
125 // values. This means the objects of type char or short int (whether
126 // signed or not) are promoted to either int or unsigned int, as
127 // appropriate.]
128 void PrintOut(int priority, const char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
129
130 // run time, determine byte ordering
131 int isbigendian();
132
133 // This value follows the peripheral device type value as defined in
134 // SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
135 // the ATA standard for packet devices to define the device type.
136 const char *packetdevicetype(int type);
137
138 int deviceopen(const char *pathname, char *type);
139
140 int deviceclose(int fd);
141
142 // Optional functions of os_*.c
143 #ifdef HAVE_GET_OS_VERSION_STR
144 // Return build host and OS version as static string
145 const char * get_os_version_str(void);
146 #endif
147
148 // returns 1 if any of the n bytes are nonzero, else zero.
149 int nonempty(unsigned char *testarea,int n);
150
151 // needed to fix glibc bug
152 void FixGlibcTimeZoneBug();
153
154 // convert time in msec to a text string
155 void MsecToText(unsigned int msec, char *txt);
156
157 // Exit codes
158 #define EXIT_BADCMD 1 // command line did not parse
159 #define EXIT_BADCONF 2 // syntax error in config file
160 #define EXIT_STARTUP 3 // problem forking daemon
161 #define EXIT_PID 4 // problem creating pid file
162 #define EXIT_NOCONF 5 // config file does not exist
163 #define EXIT_READCONF 6 // config file exists but cannot be read
164
165 #define EXIT_NOMEM 8 // out of memory
166 #define EXIT_BADCODE 10 // internal error - should NEVER happen
167
168 #define EXIT_BADDEV 16 // we can't monitor this device
169 #define EXIT_NODEV 17 // no devices to monitor
170
171 #define EXIT_SIGNAL 254 // abort on signal
172
173
174 // macros to control printing
175 #define PRINT_ON(control) {if (control->printing_switchable) control->dont_print=0;}
176 #define PRINT_OFF(control) {if (control->printing_switchable) control->dont_print=1;}
177
178 // possible values for controller_type in extern.h
179 #define CONTROLLER_UNKNOWN 0x00
180 #define CONTROLLER_ATA 0x01
181 #define CONTROLLER_SCSI 0x02
182 #define CONTROLLER_3WARE 0x03 // set by -d option, but converted to one of three types below
183 #define CONTROLLER_3WARE_678K 0x04 // NOT set by guess_device_type()
184 #define CONTROLLER_3WARE_9000_CHAR 0x05 // set by guess_device_type()
185 #define CONTROLLER_3WARE_678K_CHAR 0x06 // set by guess_device_type()
186 #define CONTROLLER_MARVELL_SATA 0x07 // SATA drives behind Marvell controllers
187 #define CONTROLLER_SAT 0x08 // SATA device behind a SCSI ATA Translation (SAT) layer
188 #define CONTROLLER_HPT 0x09 // SATA drives behind HighPoint Raid controllers
189
190 #endif