smartmontools  SVN Rev 4092
Utility to control and monitor storage systems with "S.M.A.R.T."
Classes | Macros | Typedefs | Enumerations | Functions | Variables
smartd.cpp File Reference
#include "config.h"
#include "int64.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <limits.h>
#include <getopt.h>
#include <stdexcept>
#include <string>
#include <vector>
#include <algorithm>
#include <sys/wait.h>
#include "atacmds.h"
#include "dev_interface.h"
#include "knowndrives.h"
#include "scsicmds.h"
#include "utility.h"
Include dependency graph for smartd.cpp:

Go to the source code of this file.

Classes

class  attribute_flags
 
struct  dev_config
 Configuration data for a device. More...
 
struct  mailinfo
 
struct  persistent_dev_state
 Persistent state data for a device. More...
 
struct  persistent_dev_state::ata_attribute
 
struct  persistent_dev_state::scsi_error_counter
 
struct  persistent_dev_state::scsi_nonmedium_error
 
struct  temp_dev_state
 Non-persistent state data for a device. More...
 
struct  dev_state
 Runtime state data for a device. More...
 
class  env_buffer
 

Macros

#define SIGNALFN   signal
 
#define SIGQUIT_KEYNAME   "CONTROL-\\"
 
#define EXIT_BADCMD   1
 
#define EXIT_BADCONF   2
 
#define EXIT_STARTUP   3
 
#define EXIT_PID   4
 
#define EXIT_NOCONF   5
 
#define EXIT_READCONF   6
 
#define EXIT_NOMEM   8
 
#define EXIT_BADCODE   10
 
#define EXIT_BADDEV   16
 
#define EXIT_NODEV   17
 
#define EXIT_SIGNAL   254
 
#define CHECKTIME   1800
 
#define EBUFLEN   1024
 
#define NO_warn_unused_result(cmd)   { if (cmd) {} ; }
 
#define SELFTEST_ERRORCOUNT(x)   (x & 0xff)
 
#define SELFTEST_ERRORHOURS(x)   ((x >> 8) & 0xffff)
 
#define SCANDIRECTIVE   "DEVICESCAN"
 

Typedefs

typedef std::vector< dev_configdev_config_vector
 Container for configuration info for each device. More...
 
typedef std::vector< dev_statedev_state_vector
 Container for state info for each device. More...
 

Enumerations

enum  {
  MONITOR_IGN_FAILUSE = 0x01, MONITOR_IGNORE = 0x02, MONITOR_RAW_PRINT = 0x04, MONITOR_RAW = 0x08,
  MONITOR_AS_CRIT = 0x10, MONITOR_RAW_AS_CRIT = 0x20
}
 

Functions

static void PrintOut (int priority, const char *fmt,...) __attribute_format_printf(2
 
static bool parse_dev_state_line (const char *line, persistent_dev_state &state)
 
static bool read_dev_state (const char *path, persistent_dev_state &state)
 
static void write_dev_state_line (FILE *f, const char *name, uint64_t val)
 
static void write_dev_state_line (FILE *f, const char *name1, int id, const char *name2, uint64_t val)
 
static bool write_dev_state (const char *path, const persistent_dev_state &state)
 
static bool write_dev_attrlog (const char *path, const dev_state &state)
 
static void write_all_dev_states (const dev_config_vector &configs, dev_state_vector &states, bool write_always=true)
 
static void write_all_dev_attrlogs (const dev_config_vector &configs, dev_state_vector &states)
 
static void RemovePidFile ()
 
static void USR1handler (int sig)
 
static void HUPhandler (int sig)
 
static void sighandler (int sig)
 
static int Goodbye (int status)
 
static void MailWarning (const dev_config &cfg, dev_state &state, int which, const char *fmt,...) __attribute_format_printf(4
 
static void reset_warning_mail (const dev_config &cfg, dev_state &state, int which, const char *fmt,...) __attribute_format_printf(4
 
static void vsyslog_lines (int priority, const char *fmt, va_list ap)
 
void pout (const char *fmt,...)
 
void checksumwarning (const char *string)
 
static bool WaitForPidFile ()
 
static void DaemonInit ()
 
static void WritePidFile ()
 
static void PrintHead ()
 
static void Directives ()
 
static const char * GetValidArgList (char opt)
 
static void Usage ()
 
static int CloseDevice (smart_device *device, const char *name)
 
static bool not_allowed_in_filename (char c)
 
static int read_ata_error_count (ata_device *device, const char *name, firmwarebug_defs firmwarebugs, bool extended)
 
static int SelfTestErrorCount (ata_device *device, const char *name, firmwarebug_defs firmwarebugs)
 
static bool is_offl_coll_in_progress (unsigned char status)
 
static bool is_self_test_in_progress (unsigned char status)
 
static void log_offline_data_coll_status (const char *name, unsigned char status)
 
static void log_self_test_exec_status (const char *name, unsigned char status)
 
static bool check_pending_id (const dev_config &cfg, const dev_state &state, unsigned char id, const char *msg)
 
static void finish_device_scan (dev_config &cfg, dev_state &state)
 
static void format_set_result_msg (std::string &msg, const char *name, bool ok, int set_option=0, bool has_value=false)
 
static int ATADeviceScan (dev_config &cfg, dev_state &state, ata_device *atadev)
 
static int SCSIDeviceScan (dev_config &cfg, dev_state &state, scsi_device *scsidev)
 
static void CheckSelfTestLogs (const dev_config &cfg, dev_state &state, int newi)
 
static char next_scheduled_test (const dev_config &cfg, dev_state &state, bool scsi, time_t usetime=0)
 
static void PrintTestSchedule (const dev_config_vector &configs, dev_state_vector &states, const smart_device_list &devices)
 
static int DoSCSISelfTest (const dev_config &cfg, dev_state &state, scsi_device *device, char testtype)
 
static int DoATASelfTest (const dev_config &cfg, dev_state &state, ata_device *device, char testtype)
 
static void check_pending (const dev_config &cfg, dev_state &state, unsigned char id, bool increase_only, const ata_smart_values &smartval, int mailtype, const char *msg)
 
static const char * fmt_temp (unsigned char x, char(&buf)[20])
 
static void CheckTemperature (const dev_config &cfg, dev_state &state, unsigned char currtemp, unsigned char triptemp)
 
static void check_attribute (const dev_config &cfg, dev_state &state, const ata_smart_attribute &attr, const ata_smart_attribute &prev, int attridx, const ata_smart_threshold_entry *thresholds)
 
static int ATACheckDevice (const dev_config &cfg, dev_state &state, ata_device *atadev, bool firstpass, bool allow_selftests)
 
static int SCSICheckDevice (const dev_config &cfg, dev_state &state, scsi_device *scsidev, bool allow_selftests)
 
static void init_disable_standby_check (dev_config_vector &configs)
 
static void do_disable_standby_check (const dev_config_vector &configs, const dev_state_vector &states)
 
static void CheckDevicesOnce (const dev_config_vector &configs, dev_state_vector &states, smart_device_list &devices, bool firstpass, bool allow_selftests)
 
static void Initialize (time_t *wakeuptime)
 
static time_t dosleep (time_t wakeuptime, bool &sigwakeup)
 
static void printoutvaliddirectiveargs (int priority, char d)
 
static int GetInteger (const char *arg, const char *name, const char *token, int lineno, const char *cfgfile, int min, int max, char *suffix=0)
 
static int Get3Integers (const char *arg, const char *name, const char *token, int lineno, const char *cfgfile, unsigned char *val1, unsigned char *val2, unsigned char *val3)
 
static int ParseToken (char *token, dev_config &cfg)
 
static int ParseConfigLine (dev_config_vector &conf_entries, dev_config &default_conf, int lineno, char *line)
 
static int ParseConfigFile (dev_config_vector &conf_entries)
 
static void PrintValidArgs (char opt)
 
static void check_abs_path (char option, const std::string &path)
 
static void ParseOpts (int argc, char **argv)
 
static int MakeConfigEntries (const dev_config &base_cfg, dev_config_vector &conf_entries, smart_device_list &scanned_devs, const char *type)
 
static void CanNotRegister (const char *name, const char *type, int line, bool scandirective)
 
static int ReadOrMakeConfigEntries (dev_config_vector &conf_entries, smart_device_list &scanned_devs)
 
static bool is_raid_type (const char *type)
 
static bool is_duplicate_device (const smart_device *dev, const smart_device_list &devices, unsigned numdevs, const dev_config_vector &ignored)
 
static void RegisterDevices (const dev_config_vector &conf_entries, smart_device_list &scanned_devs, dev_config_vector &configs, dev_state_vector &states, smart_device_list &devices)
 
static int main_worker (int argc, char **argv)
 
int main (int argc, char **argv)
 

Variables

const char * smartd_cpp_cvsid
 
static unsigned char debugmode = 0
 
static int checktime =CHECKTIME
 
static std::string pid_file
 
static std::string state_path_prefix
 
static std::string attrlog_path_prefix
 
static const char * configfile
 
static const char *const configfile_stdin = "<stdin>"
 
static std::string configfile_alt
 
static std::string warning_script
 
static int quit =0
 
static int facility =LOG_DAEMON
 
static bool do_fork =true
 
unsigned char failuretest_permissive = 0
 
static volatile int caughtsigUSR1 =0
 
static volatile int caughtsigHUP =0
 
static volatile int caughtsigEXIT =0
 
static const int SMARTD_NMAIL = 13
 
static const int MAILTYPE_TEST = 0
 
const bool fix_swapped_id = false
 
static const char test_type_chars [] = "LncrSCO"
 
static const unsigned num_test_types = sizeof(test_type_chars)-1
 
static int standby_disable_state = 0
 
static bool is_initialized = false
 

Macro Definition Documentation

#define CHECKTIME   1800

Definition at line 133 of file smartd.cpp.

#define EBUFLEN   1024

Definition at line 936 of file smartd.cpp.

#define EXIT_BADCMD   1

Definition at line 113 of file smartd.cpp.

#define EXIT_BADCODE   10

Definition at line 121 of file smartd.cpp.

#define EXIT_BADCONF   2

Definition at line 114 of file smartd.cpp.

#define EXIT_BADDEV   16

Definition at line 123 of file smartd.cpp.

#define EXIT_NOCONF   5

Definition at line 117 of file smartd.cpp.

#define EXIT_NODEV   17

Definition at line 124 of file smartd.cpp.

#define EXIT_NOMEM   8

Definition at line 120 of file smartd.cpp.

#define EXIT_PID   4

Definition at line 116 of file smartd.cpp.

#define EXIT_READCONF   6

Definition at line 118 of file smartd.cpp.

#define EXIT_SIGNAL   254

Definition at line 126 of file smartd.cpp.

#define EXIT_STARTUP   3

Definition at line 115 of file smartd.cpp.

#define NO_warn_unused_result (   cmd)    { if (cmd) {} ; }
#define SCANDIRECTIVE   "DEVICESCAN"

Definition at line 4126 of file smartd.cpp.

#define SELFTEST_ERRORCOUNT (   x)    (x & 0xff)

Definition at line 1619 of file smartd.cpp.

#define SELFTEST_ERRORHOURS (   x)    ((x >> 8) & 0xffff)

Definition at line 1620 of file smartd.cpp.

#define SIGNALFN   signal

Definition at line 88 of file smartd.cpp.

#define SIGQUIT_KEYNAME   "CONTROL-\\"

Definition at line 102 of file smartd.cpp.

Typedef Documentation

typedef std::vector<dev_config> dev_config_vector

Container for configuration info for each device.

Definition at line 490 of file smartd.cpp.

typedef std::vector<dev_state> dev_state_vector

Container for state info for each device.

Definition at line 493 of file smartd.cpp.

Enumeration Type Documentation

anonymous enum
Enumerator
MONITOR_IGN_FAILUSE 
MONITOR_IGNORE 
MONITOR_RAW_PRINT 
MONITOR_RAW 
MONITOR_AS_CRIT 
MONITOR_RAW_AS_CRIT 

Definition at line 203 of file smartd.cpp.

Function Documentation

static int ATACheckDevice ( const dev_config cfg,
dev_state state,
ata_device atadev,
bool  firstpass,
bool  allow_selftests 
)
static

scsi

Definition at line 2997 of file smartd.cpp.

static int ATADeviceScan ( dev_config cfg,
dev_state state,
ata_device atadev 
)
static

Definition at line 1748 of file smartd.cpp.

static void CanNotRegister ( const char *  name,
const char *  type,
int  line,
bool  scandirective 
)
static

Definition at line 4742 of file smartd.cpp.

static void check_abs_path ( char  option,
const std::string &  path 
)
static

Definition at line 4383 of file smartd.cpp.

static void check_attribute ( const dev_config cfg,
dev_state state,
const ata_smart_attribute attr,
const ata_smart_attribute prev,
int  attridx,
const ata_smart_threshold_entry thresholds 
)
static

Definition at line 2900 of file smartd.cpp.

static void check_pending ( const dev_config cfg,
dev_state state,
unsigned char  id,
bool  increase_only,
const ata_smart_values smartval,
int  mailtype,
const char *  msg 
)
static

Definition at line 2784 of file smartd.cpp.

static bool check_pending_id ( const dev_config cfg,
const dev_state state,
unsigned char  id,
const char *  msg 
)
static

Definition at line 1686 of file smartd.cpp.

static void CheckDevicesOnce ( const dev_config_vector configs,
dev_state_vector states,
smart_device_list devices,
bool  firstpass,
bool  allow_selftests 
)
static

Definition at line 3389 of file smartd.cpp.

static void CheckSelfTestLogs ( const dev_config cfg,
dev_state state,
int  newi 
)
static

Definition at line 2403 of file smartd.cpp.

void checksumwarning ( const char *  string)

Definition at line 1277 of file smartd.cpp.

static void CheckTemperature ( const dev_config cfg,
dev_state state,
unsigned char  currtemp,
unsigned char  triptemp 
)
static

Definition at line 2826 of file smartd.cpp.

static int CloseDevice ( smart_device device,
const char *  name 
)
static

Definition at line 1561 of file smartd.cpp.

static void DaemonInit ( )
static

Definition at line 1309 of file smartd.cpp.

static void Directives ( )
static

Definition at line 1415 of file smartd.cpp.

static void do_disable_standby_check ( const dev_config_vector configs,
const dev_state_vector states 
)
static

Definition at line 3342 of file smartd.cpp.

static int DoATASelfTest ( const dev_config cfg,
dev_state state,
ata_device device,
char  testtype 
)
static

Definition at line 2666 of file smartd.cpp.

static int DoSCSISelfTest ( const dev_config cfg,
dev_state state,
scsi_device device,
char  testtype 
)
static

Definition at line 2607 of file smartd.cpp.

static time_t dosleep ( time_t  wakeuptime,
bool &  sigwakeup 
)
static

Definition at line 3474 of file smartd.cpp.

static void finish_device_scan ( dev_config cfg,
dev_state state 
)
static

Definition at line 1710 of file smartd.cpp.

static const char* fmt_temp ( unsigned char  x,
char(&)  buf[20] 
)
static

Definition at line 2817 of file smartd.cpp.

static void format_set_result_msg ( std::string &  msg,
const char *  name,
bool  ok,
int  set_option = 0,
bool  has_value = false 
)
static

Definition at line 1727 of file smartd.cpp.

static int Get3Integers ( const char *  arg,
const char *  name,
const char *  token,
int  lineno,
const char *  cfgfile,
unsigned char *  val1,
unsigned char *  val2,
unsigned char *  val3 
)
static

Definition at line 3609 of file smartd.cpp.

static int GetInteger ( const char *  arg,
const char *  name,
const char *  token,
int  lineno,
const char *  cfgfile,
int  min,
int  max,
char *  suffix = 0 
)
static

Definition at line 3575 of file smartd.cpp.

static const char* GetValidArgList ( char  opt)
static

Definition at line 1462 of file smartd.cpp.

static int Goodbye ( int  status)
static

Definition at line 882 of file smartd.cpp.

static void HUPhandler ( int  sig)
static

Definition at line 862 of file smartd.cpp.

static void init_disable_standby_check ( dev_config_vector configs)
static

Definition at line 3313 of file smartd.cpp.

static void Initialize ( time_t *  wakeuptime)
static

Definition at line 3409 of file smartd.cpp.

static bool is_duplicate_device ( const smart_device dev,
const smart_device_list devices,
unsigned  numdevs,
const dev_config_vector ignored 
)
static

Definition at line 4813 of file smartd.cpp.

static bool is_offl_coll_in_progress ( unsigned char  status)
inlinestatic

Definition at line 1623 of file smartd.cpp.

static bool is_raid_type ( const char *  type)
static

Definition at line 4802 of file smartd.cpp.

static bool is_self_test_in_progress ( unsigned char  status)
inlinestatic

Definition at line 1629 of file smartd.cpp.

static void log_offline_data_coll_status ( const char *  name,
unsigned char  status 
)
static

Definition at line 1635 of file smartd.cpp.

static void log_self_test_exec_status ( const char *  name,
unsigned char  status 
)
static

Definition at line 1658 of file smartd.cpp.

static void static void MailWarning ( const dev_config cfg,
dev_state state,
int  which,
const char *  fmt,
  ... 
)
static

Definition at line 943 of file smartd.cpp.

int main ( int  argc,
char **  argv 
)

Definition at line 5140 of file smartd.cpp.

static int main_worker ( int  argc,
char **  argv 
)
static

Definition at line 4967 of file smartd.cpp.

static int MakeConfigEntries ( const dev_config base_cfg,
dev_config_vector conf_entries,
smart_device_list scanned_devs,
const char *  type 
)
static

Definition at line 4709 of file smartd.cpp.

static char next_scheduled_test ( const dev_config cfg,
dev_state state,
bool  scsi,
time_t  usetime = 0 
)
static

Definition at line 2467 of file smartd.cpp.

static bool not_allowed_in_filename ( char  c)
static

Definition at line 1572 of file smartd.cpp.

static bool parse_dev_state_line ( const char *  line,
persistent_dev_state state 
)
static

Definition at line 543 of file smartd.cpp.

static int ParseConfigFile ( dev_config_vector conf_entries)
static

Definition at line 4234 of file smartd.cpp.

static int ParseConfigLine ( dev_config_vector conf_entries,
dev_config default_conf,
int  lineno,
char *  line 
)
static

Definition at line 4137 of file smartd.cpp.

static void ParseOpts ( int  argc,
char **  argv 
)
static

Definition at line 4398 of file smartd.cpp.

static int ParseToken ( char *  token,
dev_config cfg 
)
static

Definition at line 3664 of file smartd.cpp.

void pout ( const char *  fmt,
  ... 
)

Definition at line 1223 of file smartd.cpp.

static void PrintHead ( )
static

Definition at line 1409 of file smartd.cpp.

static void PrintOut ( int  priority,
const char *  fmt,
  ... 
)
static

Definition at line 1251 of file smartd.cpp.

static void printoutvaliddirectiveargs ( int  priority,
char  d 
)
static

Definition at line 3533 of file smartd.cpp.

static void PrintTestSchedule ( const dev_config_vector configs,
dev_state_vector states,
const smart_device_list devices 
)
static

Definition at line 2553 of file smartd.cpp.

static void PrintValidArgs ( char  opt)
static

Definition at line 4369 of file smartd.cpp.

static int read_ata_error_count ( ata_device device,
const char *  name,
firmwarebug_defs  firmwarebugs,
bool  extended 
)
static

Definition at line 1581 of file smartd.cpp.

static bool read_dev_state ( const char *  path,
persistent_dev_state state 
)
static

Definition at line 638 of file smartd.cpp.

static int ReadOrMakeConfigEntries ( dev_config_vector conf_entries,
smart_device_list scanned_devs 
)
static

Definition at line 4758 of file smartd.cpp.

static void RegisterDevices ( const dev_config_vector conf_entries,
smart_device_list scanned_devs,
dev_config_vector configs,
dev_state_vector states,
smart_device_list devices 
)
static

Definition at line 4842 of file smartd.cpp.

static void RemovePidFile ( )
static

Definition at line 830 of file smartd.cpp.

static void static void reset_warning_mail ( const dev_config cfg,
dev_state state,
int  which,
const char *  fmt,
  ... 
)
static

Definition at line 1170 of file smartd.cpp.

static int SCSICheckDevice ( const dev_config cfg,
dev_state state,
scsi_device scsidev,
bool  allow_selftests 
)
static

Definition at line 3223 of file smartd.cpp.

static int SCSIDeviceScan ( dev_config cfg,
dev_state state,
scsi_device scsidev 
)
static

Definition at line 2161 of file smartd.cpp.

static int SelfTestErrorCount ( ata_device device,
const char *  name,
firmwarebug_defs  firmwarebugs 
)
static

Definition at line 1605 of file smartd.cpp.

static void sighandler ( int  sig)
static

Definition at line 872 of file smartd.cpp.

static void Usage ( )
static

Definition at line 1488 of file smartd.cpp.

static void USR1handler ( int  sig)
static

Definition at line 844 of file smartd.cpp.

static void vsyslog_lines ( int  priority,
const char *  fmt,
va_list  ap 
)
static

Definition at line 1198 of file smartd.cpp.

static bool WaitForPidFile ( )
static

Definition at line 1286 of file smartd.cpp.

static void write_all_dev_attrlogs ( const dev_config_vector configs,
dev_state_vector states 
)
static

Definition at line 817 of file smartd.cpp.

static void write_all_dev_states ( const dev_config_vector configs,
dev_state_vector states,
bool  write_always = true 
)
static

Definition at line 796 of file smartd.cpp.

static bool write_dev_attrlog ( const char *  path,
const dev_state state 
)
static

Definition at line 741 of file smartd.cpp.

static bool write_dev_state ( const char *  path,
const persistent_dev_state state 
)
static

Definition at line 689 of file smartd.cpp.

static void write_dev_state_line ( FILE *  f,
const char *  name,
uint64_t  val 
)
static

Definition at line 676 of file smartd.cpp.

static void write_dev_state_line ( FILE *  f,
const char *  name1,
int  id,
const char *  name2,
uint64_t  val 
)
static

Definition at line 682 of file smartd.cpp.

static void WritePidFile ( )
static

Definition at line 1386 of file smartd.cpp.

Variable Documentation

std::string attrlog_path_prefix
static

Definition at line 151 of file smartd.cpp.

volatile int caughtsigEXIT =0
static

Definition at line 195 of file smartd.cpp.

volatile int caughtsigHUP =0
static

Definition at line 192 of file smartd.cpp.

volatile int caughtsigUSR1 =0
static

Definition at line 183 of file smartd.cpp.

int checktime =CHECKTIME
static

Definition at line 134 of file smartd.cpp.

const char* configfile
static

Definition at line 154 of file smartd.cpp.

std::string configfile_alt
static

Definition at line 158 of file smartd.cpp.

const char* const configfile_stdin = "<stdin>"
static

Definition at line 156 of file smartd.cpp.

unsigned char debugmode = 0
static

Definition at line 130 of file smartd.cpp.

bool do_fork =true
static

Definition at line 171 of file smartd.cpp.

int facility =LOG_DAEMON
static

Definition at line 167 of file smartd.cpp.

unsigned char failuretest_permissive = 0

Definition at line 180 of file smartd.cpp.

const bool fix_swapped_id = false

Definition at line 1745 of file smartd.cpp.

bool is_initialized = false
static

Definition at line 3406 of file smartd.cpp.

const int MAILTYPE_TEST = 0
static

Definition at line 344 of file smartd.cpp.

const unsigned num_test_types = sizeof(test_type_chars)-1
static

Definition at line 2463 of file smartd.cpp.

std::string pid_file
static

Definition at line 137 of file smartd.cpp.

int quit =0
static

Definition at line 164 of file smartd.cpp.

const char* smartd_cpp_cvsid
Initial value:
= "$Id: smartd.cpp 4059 2015-04-18 17:01:31Z chrfranke $"
CONFIG_H_CVSID

Definition at line 109 of file smartd.cpp.

const int SMARTD_NMAIL = 13
static

Definition at line 342 of file smartd.cpp.

int standby_disable_state = 0
static

Definition at line 3311 of file smartd.cpp.

std::string state_path_prefix
static

Definition at line 144 of file smartd.cpp.

const char test_type_chars[] = "LncrSCO"
static

Definition at line 2462 of file smartd.cpp.

std::string warning_script
static

Definition at line 161 of file smartd.cpp.