]> git.proxmox.com Git - mirror_smartmontools-debian.git/blame - utility.h
import upstream version 5.36
[mirror_smartmontools-debian.git] / utility.h
CommitLineData
832b75ed
GG
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.43 2006/04/12 14:54:28 ballen4705 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>
37int safe_snprintf(char *buf, int size, const char *fmt, ...);
38int 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
47void dateandtimezone(char *buffer);
48// Same, but for time defined by epoch tval
49void dateandtimezoneepoch(char *buffer, time_t tval);
50
51// utility function for printing out CVS strings
52#define CVSMAXLEN 1024
53void 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
62void pout(char *fmt, ...)
63 __attribute__ ((format (printf, 1, 2)));
64
65// replacement for perror() with redirected output.
66void syserror(const char *message);
67
68// Prints a warning message for a failed regular expression compilation from
69// regcomp().
70void printregexwarning(int errcode, regex_t *compiled);
71
72// A wrapper for regcomp(). Returns zero for success, non-zero otherwise.
73int compileregex(regex_t *compiled, const char *pattern, int cflags);
74
75// Function for processing -r option in smartctl and smartd
76int split_report_arg(char *s, int *i);
77// Function for processing -c option in smartctl and smartd
78int split_report_arg2(char *s, int *i);
79// Function for processing -t selective... option in smartctl
80int 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
86int 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
90int 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
96void *Calloc(size_t nmemb, size_t size);
97
98// Utility function to free memory
99void *FreeNonZero(void* address, int size, int whatline, const char* file);
100
101// A custom version of strdup() that keeps track of how much memory is
102// being allocated. If mustexist is set, it also throws an error if we
103// try to duplicate a NULL string.
104char *CustomStrDup(char *ptr, int mustexist, int whatline, const char* file);
105
106// To help with memory checking. Use when it is known that address is
107// NOT null.
108void *CheckFree(void *address, int whatline, const char* file);
109
110// This function prints either to stdout or to the syslog as needed
111
112// [From GLIBC Manual: Since the prototype doesn't specify types for
113// optional arguments, in a call to a variadic function the default
114// argument promotions are performed on the optional argument
115// values. This means the objects of type char or short int (whether
116// signed or not) are promoted to either int or unsigned int, as
117// appropriate.]
118void PrintOut(int priority,char *fmt, ...);
119
120// run time, determine byte ordering
121int isbigendian();
122
123// This value follows the peripheral device type value as defined in
124// SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in
125// the ATA standard for packet devices to define the device type.
126const char *packetdevicetype(int type);
127
128int deviceopen(const char *pathname, char *type);
129
130int deviceclose(int fd);
131
132// Optional functions of os_*.c
133#ifdef HAVE_GET_OS_VERSION_STR
134// Return build host and OS version as static string
135const char * get_os_version_str(void);
136#endif
137
138// returns 1 if any of the n bytes are nonzero, else zero.
139int nonempty(unsigned char *testarea,int n);
140
141// needed to fix glibc bug
142void FixGlibcTimeZoneBug();
143
144// convert time in msec to a text string
145void MsecToText(unsigned int msec, char *txt);
146
147// Exit codes
148#define EXIT_BADCMD 1 // command line did not parse
149#define EXIT_BADCONF 2 // syntax error in config file
150#define EXIT_STARTUP 3 // problem forking daemon
151#define EXIT_PID 4 // problem creating pid file
152#define EXIT_NOCONF 5 // config file does not exist
153#define EXIT_READCONF 6 // config file exists but cannot be read
154
155#define EXIT_NOMEM 8 // out of memory
156#define EXIT_BADCODE 10 // internal error - should NEVER happen
157
158#define EXIT_BADDEV 16 // we can't monitor this device
159#define EXIT_NODEV 17 // no devices to monitor
160
161#define EXIT_SIGNAL 254 // abort on signal
162
163
164// macros to control printing
165#define PRINT_ON(control) {if (control->printing_switchable) control->dont_print=0;}
166#define PRINT_OFF(control) {if (control->printing_switchable) control->dont_print=1;}
167
168// possible values for controller_type in extern.h
169#define CONTROLLER_UNKNOWN 0x00
170#define CONTROLLER_ATA 0x01
171#define CONTROLLER_SCSI 0x02
172#define CONTROLLER_3WARE 0x03 // set by -d option, but converted to one of three types below
173#define CONTROLLER_3WARE_678K 0x04 // NOT set by guess_device_type()
174#define CONTROLLER_3WARE_9000_CHAR 0x05 // set by guess_device_type()
175#define CONTROLLER_3WARE_678K_CHAR 0x06 // set by guess_device_type()
176#define CONTROLLER_MARVELL_SATA 0x07 // SATA drives behind Marvell controllers
177
178
179#endif