]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - smartd.cpp
Updated changelog
[mirror_smartmontools-debian.git] / smartd.cpp
index efaa226d31c17d942daa79c02f31e3da1d10bcf7..0e124ecc3ad6ae91cea1454201094adccbbeee69 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Home page of code is: http://smartmontools.sourceforge.net
  *
- * Copyright (C) 2002-9 Bruce Allen <smartmontools-support@lists.sourceforge.net>
- * Copyright (C) 2000   Michael Cornwell <cornwell@acm.org>
- * Copyright (C) 2008   Oliver Bock <brevilo@users.sourceforge.net>
- * Copyright (C) 2008-9 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2000    Michael Cornwell <cornwell@acm.org>
+ * Copyright (C) 2008    Oliver Bock <brevilo@users.sourceforge.net>
+ * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -71,17 +71,17 @@ typedef int pid_t;
 #endif // _WIN32
 
 #ifdef __CYGWIN__
-// From <windows.h>:
-// BOOL WINAPI FreeConsole(void);
-extern "C" int __stdcall FreeConsole(void);
 #include <io.h> // setmode()
 #endif // __CYGWIN__
 
+#ifdef HAVE_LIBCAP_NG
+#include <cap-ng.h>
+#endif // LIBCAP_NG
+
 // locally included files
 #include "int64.h"
 #include "atacmds.h"
 #include "dev_interface.h"
-#include "extern.h"
 #include "knowndrives.h"
 #include "scsicmds.h"
 #include "utility.h"
@@ -108,12 +108,7 @@ extern "C" int __stdcall FreeConsole(void);
 #define SIGQUIT SIGBREAK
 #define SIGQUIT_KEYNAME "CONTROL-Break"
 #else  // _WIN32
-#ifdef __CYGWIN__
-// 2x CONTROL-C simulates missing SIGQUIT via keyboard
-#define SIGQUIT_KEYNAME "2x CONTROL-C"
-#else // __CYGWIN__
 #define SIGQUIT_KEYNAME "CONTROL-\\"
-#endif // __CYGWIN__
 #endif // _WIN32
 
 #if defined (__SVR4) && defined (__sun)
@@ -122,12 +117,8 @@ extern "C" int getdomainname(char *, int); // no declaration in header files!
 
 #define ARGUSED(x) ((void)(x))
 
-const char * smartd_cpp_cvsid = "$Id: smartd.cpp 2915 2009-09-18 21:17:37Z chrfranke $"
-                                CONFIG_H_CVSID EXTERN_H_CVSID;
-
-extern const char *reportbug;
-
-extern unsigned char debugmode;
+const char * smartd_cpp_cvsid = "$Id: smartd.cpp 3513 2012-02-15 21:57:21Z chrfranke $"
+  CONFIG_H_CVSID;
 
 // smartd exit codes
 #define EXIT_BADCMD    1   // command line did not parse
@@ -145,6 +136,10 @@ extern unsigned char debugmode;
 
 #define EXIT_SIGNAL    254 // abort on signal
 
+
+// command-line: 1=debug mode, 2=print presets
+static unsigned char debugmode = 0;
+
 // command-line: how long to sleep between checks
 #define CHECKTIME 1800
 static int checktime=CHECKTIME;
@@ -167,13 +162,7 @@ static std::string attrlog_path_prefix
                                     ;
 
 // configuration file name
-#define CONFIGFILENAME "smartd.conf"
-
-#ifndef _WIN32
-static const char *configfile = SMARTMONTOOLS_SYSCONFDIR "/" CONFIGFILENAME ;
-#else
-static const char *configfile = "./" CONFIGFILENAME ;
-#endif
+static const char * configfile;
 // configuration file "name" if read from stdin
 static const char * const configfile_stdin = "<stdin>";
 // path of alternate configuration file
@@ -190,23 +179,34 @@ static int facility=LOG_DAEMON;
 static bool do_fork=true;
 #endif
 
-// used for control of printing, passing arguments to atacmds.c
-smartmonctrl *con=NULL;
+#ifdef HAVE_LIBCAP_NG
+// command-line: enable capabilities?
+static bool enable_capabilities = false;
+#endif
+
+#if defined(_WIN32) || defined(__CYGWIN__)
+// TODO: This smartctl only variable is also used in os_win32.cpp
+unsigned char failuretest_permissive = 0;
+#endif
 
 // set to one if we catch a USR1 (check devices now)
-volatile int caughtsigUSR1=0;
+static volatile int caughtsigUSR1=0;
 
 #ifdef _WIN32
 // set to one if we catch a USR2 (toggle debug mode)
-volatile int caughtsigUSR2=0;
+static volatile int caughtsigUSR2=0;
 #endif
 
 // set to one if we catch a HUP (reload config file). In debug mode,
 // set to two, if we catch INT (also reload config file).
-volatile int caughtsigHUP=0;
+static volatile int caughtsigHUP=0;
 
 // set to signal value if we catch INT, QUIT, or TERM
-volatile int caughtsigEXIT=0;
+static volatile int caughtsigEXIT=0;
+
+// This function prints either to stdout or to the syslog as needed.
+static void PrintOut(int priority, const char *fmt, ...)
+                     __attribute_format_printf(2, 3);
 
 // Attribute monitoring flags.
 // See monitor_attr_flags below.
@@ -245,7 +245,8 @@ private:
 struct dev_config
 {
   int lineno;                             // Line number of entry in file
-  std::string name;                       // Device name
+  std::string name;                       // Device name (with optional extra info)
+  std::string dev_name;                   // Device name (plain, for SMARTD_DEVICE variable)
   std::string dev_type;                   // Device type argument from -d directive, empty if none
   std::string state_file;                 // Path of the persistent state file, empty if none
   std::string attrlog_file;               // Path of the persistent attrlog file, empty if none
@@ -255,6 +256,11 @@ struct dev_config
   bool usage;                             // Track changes in Usage Attributes
   bool selftest;                          // Monitor number of selftest errors
   bool errorlog;                          // Monitor number of ATA errors
+  bool xerrorlog;                         // Monitor number of ATA errors (Extended Comprehensive error log)
+  bool offlinests;                        // Monitor changes in offline data collection status
+  bool offlinests_ns;                     // Disable auto standby if in progress
+  bool selfteststs;                       // Monitor changes in self-test execution status
+  bool selfteststs_ns;                    // Disable auto standby if in progress
   bool permissive;                        // Ignore failed SMART commands
   char autosave;                          // 1=disable, 2=enable Autosave Attributes
   char autoofflinetest;                   // 1=disable, 2=enable Auto Offline Test
@@ -276,6 +282,17 @@ struct dev_config
   bool emailtest;                         // Send test email?
 
   // ATA ONLY
+  int set_aam; // disable(-1), enable(1..255->0..254) Automatic Acoustic Management
+  int set_apm; // disable(-1), enable(2..255->1..254) Advanced Power Management
+  int set_lookahead; // disable(-1), enable(1) read look-ahead
+  int set_standby; // set(1..255->0..254) standby timer
+  bool set_security_freeze; // Freeze ATA security
+  int set_wcache; // disable(-1), enable(1) write cache
+
+  bool sct_erc_set;                       // set SCT ERC to:
+  unsigned short sct_erc_readtime;        // ERC read time (deciseconds)
+  unsigned short sct_erc_writetime;       // ERC write time (deciseconds)
+
   unsigned char curr_pending_id;          // ID of current pending sector count, 0 if none
   unsigned char offl_pending_id;          // ID of offline uncorrectable sector count, 0 if none
   bool curr_pending_incr, offl_pending_incr; // True if current/offline pending values increase
@@ -283,8 +300,7 @@ struct dev_config
 
   attribute_flags monitor_attr_flags;     // MONITOR_* flags for each attribute
 
-  // TODO: Encapsulate, add get/set functions
-  unsigned char attributedefs[256];       // -v options, see end of extern.h for def
+  ata_vendor_attr_defs attribute_defs;    // -v options
 
   dev_config();
 };
@@ -297,6 +313,9 @@ dev_config::dev_config()
   usage(false),
   selftest(false),
   errorlog(false),
+  xerrorlog(false),
+  offlinests(false),  offlinests_ns(false),
+  selfteststs(false), selfteststs_ns(false),
   permissive(false),
   autosave(0),
   autoofflinetest(0),
@@ -311,18 +330,24 @@ dev_config::dev_config()
   tempinfo(0), tempcrit(0),
   emailfreq(0),
   emailtest(false),
+  set_aam(0), set_apm(0),
+  set_lookahead(0),
+  set_standby(0),
+  set_security_freeze(false),
+  set_wcache(0),
+  sct_erc_set(false),
+  sct_erc_readtime(0), sct_erc_writetime(0),
   curr_pending_id(0), offl_pending_id(0),
   curr_pending_incr(false), offl_pending_incr(false),
   curr_pending_set(false),  offl_pending_set(false)
 {
-  memset(attributedefs, 0, sizeof(attributedefs));
 }
 
 
 // Number of allowed mail message types
-const int SMARTD_NMAIL = 13;
+static const int SMARTD_NMAIL = 13;
 // Type for '-M test' mails (state not persistent)
-const int MAILTYPE_TEST = 0;
+static const int MAILTYPE_TEST = 0;
 // TODO: Add const or enum for all mail types.
 
 struct mailinfo {
@@ -344,6 +369,9 @@ struct persistent_dev_state
 
   time_t scheduled_test_next_check;       // Time of next check for scheduled self-tests
 
+  uint64_t selective_test_last_start;     // Start LBA of last scheduled selective self-test
+  uint64_t selective_test_last_end;       // End LBA of last scheduled selective self-test
+
   mailinfo maillog[SMARTD_NMAIL];         // log info on when mail sent
 
   // ATA ONLY
@@ -353,9 +381,11 @@ struct persistent_dev_state
   struct ata_attribute {
     unsigned char id;
     unsigned char val;
+    unsigned char worst; // Byte needed for 'raw64' attribute only.
     uint64_t raw;
+    unsigned char resvd;
 
-    ata_attribute() : id(0), val(0), raw(0) { }
+    ata_attribute() : id(0), val(0), worst(0), raw(0), resvd(0) { }
   };
   ata_attribute ata_attributes[NUMBER_ATA_SMART_ATTRIBUTES];
 
@@ -367,6 +397,8 @@ persistent_dev_state::persistent_dev_state()
   selflogcount(0),
   selfloghour(0),
   scheduled_test_next_check(0),
+  selective_test_last_start(0),
+  selective_test_last_end(0),
   ataerrorcount(0)
 {
 }
@@ -396,9 +428,11 @@ struct temp_dev_state
                                           // know yet) 6 or 10
 
   // ATA ONLY
-  uint64_t num_sectors;                   // Number of sectors (for selective self-test only)
+  uint64_t num_sectors;                   // Number of sectors
   ata_smart_values smartval;              // SMART data
   ata_smart_thresholds_pvt smartthres;    // SMART thresholds
+  bool offline_started;                   // true if offline data collection was started
+  bool selftest_started;                  // true if self-test was started
 
   temp_dev_state();
 };
@@ -418,7 +452,9 @@ temp_dev_state::temp_dev_state()
   TempPageSupported(false),
   SuppressReport(false),
   modese_len(0),
-  num_sectors(0)
+  num_sectors(0),
+  offline_started(false),
+  selftest_started(false)
 {
   memset(&smartval, 0, sizeof(smartval));
   memset(&smartthres, 0, sizeof(smartthres));
@@ -447,16 +483,18 @@ void dev_state::update_persistent_state()
     ata_attribute & pa = ata_attributes[i];
     pa.id = ta.id;
     if (ta.id == 0) {
-      pa.val = 0; pa.raw = 0;
+      pa.val = pa.worst = 0; pa.raw = 0;
       continue;
     }
     pa.val = ta.current;
+    pa.worst = ta.worst;
     pa.raw =            ta.raw[0]
            | (          ta.raw[1] <<  8)
            | (          ta.raw[2] << 16)
            | ((uint64_t)ta.raw[3] << 24)
            | ((uint64_t)ta.raw[4] << 32)
            | ((uint64_t)ta.raw[5] << 40);
+    pa.resvd = ta.reserv;
   }
 }
 
@@ -468,50 +506,55 @@ void dev_state::update_temp_state()
     ata_smart_attribute & ta = smartval.vendor_attributes[i];
     ta.id = pa.id;
     if (pa.id == 0) {
-      ta.current = 0; memset(ta.raw, 0, sizeof(ta.raw));
+      ta.current = ta.worst = 0;
+      memset(ta.raw, 0, sizeof(ta.raw));
       continue;
     }
     ta.current = pa.val;
+    ta.worst = pa.worst;
     ta.raw[0] = (unsigned char) pa.raw;
     ta.raw[1] = (unsigned char)(pa.raw >>  8);
     ta.raw[2] = (unsigned char)(pa.raw >> 16);
     ta.raw[3] = (unsigned char)(pa.raw >> 24);
     ta.raw[4] = (unsigned char)(pa.raw >> 32);
     ta.raw[5] = (unsigned char)(pa.raw >> 40);
+    ta.reserv = pa.resvd;
   }
 }
 
 // Parse a line from a state file.
 static bool parse_dev_state_line(const char * line, persistent_dev_state & state)
 {
-  static regular_expression regex(
+  static const regular_expression regex(
     "^ *"
      "((temperature-min)" // (1 (2)
      "|(temperature-max)" // (3)
      "|(self-test-errors)" // (4)
      "|(self-test-last-err-hour)" // (5)
      "|(scheduled-test-next-check)" // (6)
-     "|(ata-error-count)"  // (7)
-     "|(mail\\.([0-9]+)\\." // (8 (9)
-       "((count)" // (10 (11)
-       "|(first-sent-time)" // (12)
-       "|(last-sent-time)" // (13)
-       ")" // 14)
-      ")" // 8)
-     "|(ata-smart-attribute\\.([0-9]+)\\." // (14 (15)
-       "((id)" // (16)
-       "|(val)" // (17)
-       "|(raw)" // (18)
-       ")" // 19)
-      ")" // 14)
+     "|(selective-test-last-start)" // (7)
+     "|(selective-test-last-end)" // (8)
+     "|(ata-error-count)"  // (9)
+     "|(mail\\.([0-9]+)\\." // (10 (11)
+       "((count)" // (12 (13)
+       "|(first-sent-time)" // (14)
+       "|(last-sent-time)" // (15)
+       ")" // 12)
+      ")" // 10)
+     "|(ata-smart-attribute\\.([0-9]+)\\." // (16 (17)
+       "((id)" // (18 (19)
+       "|(val)" // (20)
+       "|(worst)" // (21)
+       "|(raw)" // (22)
+       "|(resvd)" // (23)
+       ")" // 18)
+      ")" // 16)
      ")" // 1)
-     " *= *([0-9]+)[ \n]*$", // (20)
+     " *= *([0-9]+)[ \n]*$", // (24)
     REG_EXTENDED
   );
-  if (regex.empty())
-    throw std::logic_error("parse_dev_state_line: invalid regex");
 
-  const int nmatch = 1+20;
+  const int nmatch = 1+24;
   regmatch_t match[nmatch];
   if (!regex.execute(line, nmatch, match))
     return false;
@@ -531,6 +574,10 @@ static bool parse_dev_state_line(const char * line, persistent_dev_state & state
     state.selfloghour = (unsigned short)val;
   else if (match[++m].rm_so >= 0)
     state.scheduled_test_next_check = (time_t)val;
+  else if (match[++m].rm_so >= 0)
+    state.selective_test_last_start = val;
+  else if (match[++m].rm_so >= 0)
+    state.selective_test_last_end = val;
   else if (match[++m].rm_so >= 0)
     state.ataerrorcount = (int)val;
   else if (match[m+=2].rm_so >= 0) {
@@ -556,8 +603,12 @@ static bool parse_dev_state_line(const char * line, persistent_dev_state & state
       state.ata_attributes[i].id = (unsigned char)val;
     else if (match[++m].rm_so >= 0)
       state.ata_attributes[i].val = (unsigned char)val;
+    else if (match[++m].rm_so >= 0)
+      state.ata_attributes[i].worst = (unsigned char)val;
     else if (match[++m].rm_so >= 0)
       state.ata_attributes[i].raw = val;
+    else if (match[++m].rm_so >= 0)
+      state.ata_attributes[i].resvd = (unsigned char)val;
     else
       return false;
   }
@@ -579,13 +630,14 @@ static bool read_dev_state(const char * path, persistent_dev_state & state)
   setmode(fileno(f), O_TEXT); // Allow files with \r\n
 #endif
 
+  persistent_dev_state new_state;
   int good = 0, bad = 0;
   char line[256];
   while (fgets(line, sizeof(line), f)) {
     const char * s = line + strspn(line, " \t");
     if (!*s || *s == '#')
       continue;
-    if (!parse_dev_state_line(line, state))
+    if (!parse_dev_state_line(line, new_state))
       bad++;
     else
       good++;
@@ -598,6 +650,9 @@ static bool read_dev_state(const char * path, persistent_dev_state & state)
     }
     pout("%s: %d invalid line(s) ignored\n", path, bad);
   }
+
+  // This sets the values missing in the file to 0.
+  state = new_state;
   return true;
 }
 
@@ -633,6 +688,8 @@ static bool write_dev_state(const char * path, const persistent_dev_state & stat
   write_dev_state_line(f, "self-test-errors", state.selflogcount);
   write_dev_state_line(f, "self-test-last-err-hour", state.selfloghour);
   write_dev_state_line(f, "scheduled-test-next-check", state.scheduled_test_next_check);
+  write_dev_state_line(f, "selective-test-last-start", state.selective_test_last_start);
+  write_dev_state_line(f, "selective-test-last-end", state.selective_test_last_end);
 
   int i;
   for (i = 0; i < SMARTD_NMAIL; i++) {
@@ -655,7 +712,9 @@ static bool write_dev_state(const char * path, const persistent_dev_state & stat
       continue;
     write_dev_state_line(f, "ata-smart-attribute", i, "id", pa.id);
     write_dev_state_line(f, "ata-smart-attribute", i, "val", pa.val);
+    write_dev_state_line(f, "ata-smart-attribute", i, "worst", pa.worst);
     write_dev_state_line(f, "ata-smart-attribute", i, "raw", pa.raw);
+    write_dev_state_line(f, "ata-smart-attribute", i, "resvd", pa.resvd);
   }
 
   return true;
@@ -723,7 +782,8 @@ static void write_all_dev_attrlogs(const dev_config_vector & configs,
 }
 
 // remove the PID file
-void RemovePidFile(){
+static void RemovePidFile()
+{
   if (!pid_file.empty()) {
     if (unlink(pid_file.c_str()))
       PrintOut(LOG_CRIT,"Can't unlink PID file %s (%s).\n", 
@@ -736,7 +796,8 @@ void RemovePidFile(){
 extern "C" { // signal handlers require C-linkage
 
 //  Note if we catch a SIGUSR1
-void USR1handler(int sig){
+static void USR1handler(int sig)
+{
   if (SIGUSR1==sig)
     caughtsigUSR1=1;
   return;
@@ -744,7 +805,8 @@ void USR1handler(int sig){
 
 #ifdef _WIN32
 //  Note if we catch a SIGUSR2
-void USR2handler(int sig){
+static void USR2handler(int sig)
+{
   if (SIGUSR2==sig)
     caughtsigUSR2=1;
   return;
@@ -752,7 +814,8 @@ void USR2handler(int sig){
 #endif
 
 // Note if we catch a HUP (or INT in debug mode)
-void HUPhandler(int sig){
+static void HUPhandler(int sig)
+{
   if (sig==SIGHUP)
     caughtsigHUP=1;
   else
@@ -761,7 +824,8 @@ void HUPhandler(int sig){
 }
 
 // signal handler for TERM, QUIT, and INT (if not in debug mode)
-void sighandler(int sig){
+static void sighandler(int sig)
+{
   if (!caughtsigEXIT)
     caughtsigEXIT=sig;
   return;
@@ -794,12 +858,14 @@ static int Goodbye(int status)
 // string can be freed if the environment variable is redefined or
 // deleted via another call to putenv(). So we keep these on the stack
 // as long as the popen() call is underway.
-int exportenv(char* stackspace, const char *name, const char *value){
+static int exportenv(char *stackspace, const char *name, const char *value)
+{
   snprintf(stackspace,ENVLENGTH, "%s=%s", name, value);
   return putenv(stackspace);
 }
 
-char* dnsdomain(const char* hostname) {
+static char *dnsdomain(const char *hostname)
+{
   char *p = NULL;
 #ifdef HAVE_GETADDRINFO
   static char canon_name[NI_MAXHOST];
@@ -838,7 +904,7 @@ char* dnsdomain(const char* hostname) {
 #define EBUFLEN 1024
 
 static void MailWarning(const dev_config & cfg, dev_state & state, int which, const char *fmt, ...)
-                        __attribute__ ((format (printf, 4, 5)));
+                        __attribute_format_printf(4, 5);
 
 // If either address or executable path is non-null then send and log
 // a warning email, or execute executable
@@ -888,7 +954,7 @@ static void MailWarning(const dev_config & cfg, dev_state & state, int which, co
              which, (int)sizeof(whichfail));
     return;
   }
-  
+
   // Return if a single warning mail has been sent.
   if ((cfg.emailfreq==1) && mail->logged)
     return;
@@ -912,6 +978,14 @@ static void MailWarning(const dev_config & cfg, dev_state & state, int which, co
       return;
   }
 
+#ifdef HAVE_LIBCAP_NG
+  if (enable_capabilities) {
+    PrintOut(LOG_ERR, "Sending a mail was supressed. "
+             "Mails can't be send when capabilites are enabled\n");
+    return;
+  }
+#endif
+
   // record the time of this mail message, and the first mail message
   if (!mail->logged)
     mail->firstsent=epoch;
@@ -1005,8 +1079,10 @@ static void MailWarning(const dev_config & cfg, dev_state & state, int which, co
     exportenv(environ_strings[6], "SMARTD_ADDRESS", address.c_str());
   exportenv(environ_strings[7], "SMARTD_DEVICESTRING", cfg.name.c_str());
 
-  exportenv(environ_strings[8], "SMARTD_DEVICETYPE", cfg.dev_type.c_str());
-  exportenv(environ_strings[9], "SMARTD_DEVICE", cfg.name.c_str());
+  // Allow 'smartctl ... -d $SMARTD_DEVICETYPE $SMARTD_DEVICE'
+  exportenv(environ_strings[8], "SMARTD_DEVICETYPE",
+            (!cfg.dev_type.empty() ? cfg.dev_type.c_str() : "auto"));
+  exportenv(environ_strings[9], "SMARTD_DEVICE", cfg.dev_name.c_str());
 
   snprintf(fullmessage, 1024,
              "This email was generated by the smartd daemon running on:\n\n"
@@ -1015,7 +1091,7 @@ static void MailWarning(const dev_config & cfg, dev_state & state, int which, co
              "  NIS domain: %s\n\n"
              "The following warning/error was logged by the smartd daemon:\n\n"
              "%s\n\n"
-             "For details see host's SYSLOG (default: /var/log/messages).\n\n"
+             "For details see host's SYSLOG.\n\n"
              "%s%s%s",
             hostname, domainname, nisdomain, message, further, original, additional);
   exportenv(environ_strings[10], "SMARTD_FULLMESSAGE", fullmessage);
@@ -1132,22 +1208,22 @@ static void MailWarning(const dev_config & cfg, dev_state & state, int which, co
       newadd = address.c_str();
     }
 
-#ifdef _MSC_VER
-    _set_printf_count_output(1); // "%n" disabled by default
-#endif
     // Message for mail [0...] and messagebox [boxmsgoffs...]
     snprintf(stdinbuf, sizeof(stdinbuf),
              "This email was generated by the smartd daemon running on:\n\n"
              "   host name: %s\n"
              "  DNS domain: %s\n"
 //           "  NIS domain: %s\n"
-             "\n%n"
+             "\n",
+             hostname, /*domainname, */ nisdomain);
+    boxmsgoffs = strlen(stdinbuf);
+    snprintf(stdinbuf+boxmsgoffs, sizeof(stdinbuf)-boxmsgoffs,
              "The following warning/error was logged by the smartd daemon:\n\n"
              "%s\n\n"
              "For details see the event log or log file of smartd.\n\n"
              "%s%s%s"
              "\n",
-             hostname, /*domainname, */ nisdomain, &boxmsgoffs, message, further, original, additional);
+             message, further, original, additional);
   }
   else
     snprintf(command, sizeof(command), "%s", executable);
@@ -1167,7 +1243,7 @@ static void MailWarning(const dev_config & cfg, dev_state & state, int which, co
     rc = daemon_spawn(command, stdinbuf, strlen(stdinbuf), stdoutbuf, sizeof(stdoutbuf));
     if (rc >= 0 && stdoutbuf[0])
       PrintOut(LOG_CRIT,"%s %s to %s produced unexpected output (%d bytes) to STDOUT/STDERR:\n%s\n",
-        newwarn, executable, newadd, strlen(stdoutbuf), stdoutbuf);
+        newwarn, executable, newadd, (int)strlen(stdoutbuf), stdoutbuf);
     if (rc != 0)
       PrintOut(LOG_CRIT,"%s %s to %s: failed, exit status %d\n",
         newwarn, executable, newadd, rc);
@@ -1181,6 +1257,55 @@ static void MailWarning(const dev_config & cfg, dev_state & state, int which, co
   mail->logged++;
 }
 
+static void reset_warning_mail(const dev_config & cfg, dev_state & state, int which, const char *fmt, ...)
+                               __attribute_format_printf(4, 5);
+
+static void reset_warning_mail(const dev_config & cfg, dev_state & state, int which, const char *fmt, ...)
+{
+  if (!(0 <= which && which < SMARTD_NMAIL))
+    return;
+
+  // Return if no mail sent yet
+  mailinfo & mi = state.maillog[which];
+  if (!mi.logged)
+    return;
+
+  // Format & print message
+  char msg[256];
+  va_list ap;
+  va_start(ap, fmt);
+  vsnprintf(msg, sizeof(msg), fmt, ap);
+  va_end(ap);
+
+  PrintOut(LOG_INFO, "Device: %s, %s, warning condition reset after %d email%s\n", cfg.name.c_str(),
+           msg, mi.logged, (mi.logged==1 ? "" : "s"));
+
+  // Clear mail counter and timestamps
+  mi = mailinfo();
+  state.must_write = true;
+}
+
+#ifndef _WIN32
+
+// Output multiple lines via separate syslog(3) calls.
+static void vsyslog_lines(int priority, const char * fmt, va_list ap)
+{
+  char buf[512+EBUFLEN]; // enough space for exec cmd output in MailWarning()
+  vsnprintf(buf, sizeof(buf), fmt, ap);
+
+  for (char * p = buf, * q; p && *p; p = q) {
+    if ((q = strchr(p, '\n')))
+      *q++ = 0;
+    if (*p)
+      syslog(priority, "%s\n", p);
+  }
+}
+
+#else  // _WIN32
+// os_win32/syslog_win32.cpp supports multiple lines.
+#define vsyslog_lines vsyslog
+#endif // _WIN32
+
 // Printing function for watching ataprint commands, or losing them
 // [From GLIBC Manual: Since the prototype doesn't specify types for
 // optional arguments, in a call to a variadic function the default
@@ -1195,7 +1320,7 @@ void pout(const char *fmt, ...){
   FixGlibcTimeZoneBug();
   // initialize variable argument list 
   va_start(ap,fmt);
-  // in debug==1 mode we will print the output from the ataprint.o functions!
+  // in debugmode==1 mode we will print the output from the ataprint.o functions!
   if (debugmode && debugmode!=2)
 #ifdef _WIN32
    if (facility == LOG_LOCAL1) // logging to stdout
@@ -1203,10 +1328,10 @@ void pout(const char *fmt, ...){
    else   
 #endif
     vprintf(fmt,ap);
-  // in debug==2 mode we print output from knowndrives.o functions
-  else if (debugmode==2 || con->reportataioctl || con->reportscsiioctl /*|| con->controller_port???*/) {
+  // in debugmode==2 mode we print output from knowndrives.o functions
+  else if (debugmode==2 || ata_debugmode || scsi_debugmode) {
     openlog("smartd", LOG_PID, facility);
-    vsyslog(LOG_INFO, fmt, ap);
+    vsyslog_lines(LOG_INFO, fmt, ap);
     closelog();
   }
   va_end(ap);
@@ -1215,8 +1340,7 @@ void pout(const char *fmt, ...){
 }
 
 // This function prints either to stdout or to the syslog as needed.
-// This function is also used by utility.cpp to report LOG_CRIT errors.
-void PrintOut(int priority, const char *fmt, ...){
+static void PrintOut(int priority, const char *fmt, ...){
   va_list ap;
   
   // get the correct time in syslog()
@@ -1232,7 +1356,7 @@ void PrintOut(int priority, const char *fmt, ...){
     vprintf(fmt,ap);
   else {
     openlog("smartd", LOG_PID, facility);
-    vsyslog(priority,fmt,ap);
+    vsyslog_lines(priority, fmt, ap);
     closelog();
   }
   va_end(ap);
@@ -1245,9 +1369,11 @@ void checksumwarning(const char * string)
   pout("Warning! %s error: invalid SMART checksum.\n", string);
 }
 
+#ifndef _WIN32
+
 // Wait for the pid file to show up, this makes sure a calling program knows
 // that the daemon is really up and running and has a pid to kill it
-bool WaitForPidFile()
+static bool WaitForPidFile()
 {
     int waited, max_wait = 10;
     struct stat stat_buf;
@@ -1264,12 +1390,14 @@ bool WaitForPidFile()
     return false;
 }
 
+#endif // _WIN32
 
 // Forks new process, closes ALL file descriptors, redirects stdin,
 // stdout, and stderr.  Not quite daemon().  See
 // http://www.linuxjournal.com/article/2335
 // for a good description of why we do things this way.
-void DaemonInit(){
+static void DaemonInit()
+{
 #ifndef _WIN32
   pid_t pid;
   int i;  
@@ -1313,11 +1441,6 @@ void DaemonInit(){
   for (i=getdtablesize();i>=0;--i)
     close(i);
   
-#ifdef __CYGWIN__
-  // Cygwin's setsid() does not detach the process from Windows console
-  FreeConsole();
-#endif // __CYGWIN__
-
 #define NO_warn_unused_result(cmd) { if (cmd) {} ; }
 
   // redirect any IO attempts to /dev/null for stdin
@@ -1379,17 +1502,22 @@ static void PrintHead()
 }
 
 // prints help info for configuration file Directives
-void Directives() {
+static void Directives()
+{
   PrintOut(LOG_INFO,
            "Configuration file (%s) Directives (after device name):\n"
-           "  -d TYPE Set the device type: %s\n"
+           "  -d TYPE Set the device type: %s, auto, removable\n"
            "  -T TYPE Set the tolerance to one of: normal, permissive\n"
            "  -o VAL  Enable/disable automatic offline tests (on/off)\n"
            "  -S VAL  Enable/disable attribute autosave (on/off)\n"
            "  -n MODE No check if: never, sleep[,N][,q], standby[,N][,q], idle[,N][,q]\n"
            "  -H      Monitor SMART Health Status, report if failed\n"
            "  -s REG  Do Self-Test at time(s) given by regular expression REG\n"
-           "  -l TYPE Monitor SMART log.  Type is one of: error, selftest\n"
+           "  -l TYPE Monitor SMART log or self-test status:\n"
+           "          error, selftest, xerror, offlinests[,ns], selfteststs[,ns]\n"
+           "  -l scterc,R,W  Set SCT Error Recovery Control\n"
+           "  -e      Change device setting: aam,[N|off], apm,[N|off], lookahead,[on|off],\n"
+           "          security-freeze, standby,[N|off], wcache,[on|off]\n"
            "  -f      Monitor 'Usage' Attributes, report failures\n"
            "  -m ADD  Send email warning to address ADD\n"
            "  -M TYPE Modify email warning behavior (see man page)\n"
@@ -1405,7 +1533,7 @@ void Directives() {
            "  -W D,I,C Monitor Temperature D)ifference, I)nformal limit, C)ritical limit\n"
            "  -v N,ST Modifies labeling of Attribute N (see man page)  \n"
            "  -P TYPE Drive-specific presets: use, ignore, show, showall\n"
-           "  -a      Default: -H -f -t -l error -l selftest -C 197 -U 198\n"
+           "  -a      Default: -H -f -t -l error -l selftest -l selfteststs -C 197 -U 198\n"
            "  -F TYPE Firmware bug workaround: none, samsung, samsung2, samsung3\n"
            "   #      Comment: text after a hash sign is ignored\n"
            "   \\      Line continuation character\n"
@@ -1418,18 +1546,21 @@ void Directives() {
 
 /* Returns a pointer to a static string containing a formatted list of the valid
    arguments to the option opt or NULL on failure. */
-const char *GetValidArgList(char opt) {
+static const char *GetValidArgList(char opt)
+{
   switch (opt) {
+  case 'A':
+  case 's':
+    return "<PATH_PREFIX>";
   case 'c':
     return "<FILE_NAME>, -";
-  case 's':
-    return "valid_regular_expression";
   case 'l':
     return "daemon, local0, local1, local2, local3, local4, local5, local6, local7";
   case 'q':
     return "nodev, errors, nodevstartup, never, onecheck, showtests";
   case 'r':
     return "ioctl[,N], ataioctl[,N], scsiioctl[,N]";
+  case 'B':
   case 'p':
     return "<FILE_NAME>";
   case 'i':
@@ -1440,10 +1571,31 @@ const char *GetValidArgList(char opt) {
 }
 
 /* prints help information for command syntax */
-void Usage (void){
+static void Usage()
+{
   PrintOut(LOG_INFO,"Usage: smartd [options]\n\n");
+  PrintOut(LOG_INFO,"  -A PREFIX, --attributelog=PREFIX\n");
+  PrintOut(LOG_INFO,"        Log ATA attribute information to {PREFIX}MODEL-SERIAL.ata.csv\n");
+#ifdef SMARTMONTOOLS_ATTRIBUTELOG
+  PrintOut(LOG_INFO,"        [default is "SMARTMONTOOLS_ATTRIBUTELOG"MODEL-SERIAL.ata.csv]\n");
+#endif
+  PrintOut(LOG_INFO,"\n");
+  PrintOut(LOG_INFO,"  -B [+]FILE, --drivedb=[+]FILE\n");
+  PrintOut(LOG_INFO,"        Read and replace [add] drive database from FILE\n");
+  PrintOut(LOG_INFO,"        [default is +%s", get_drivedb_path_add());
+#ifdef SMARTMONTOOLS_DRIVEDBDIR
+  PrintOut(LOG_INFO,"\n");
+  PrintOut(LOG_INFO,"         and then    %s", get_drivedb_path_default());
+#endif
+  PrintOut(LOG_INFO,"]\n\n");
   PrintOut(LOG_INFO,"  -c NAME|-, --configfile=NAME|-\n");
-  PrintOut(LOG_INFO,"        Read configuration file NAME or stdin [default is %s]\n\n", configfile);
+  PrintOut(LOG_INFO,"        Read configuration file NAME or stdin\n");
+  PrintOut(LOG_INFO,"        [default is %s]\n\n", configfile);
+#ifdef HAVE_LIBCAP_NG
+  PrintOut(LOG_INFO,"  -C, --capabilities\n");
+  PrintOut(LOG_INFO,"        Use capabilities.\n"
+                    "        Warning: Mail notification does not work when used.\n\n");
+#endif
   PrintOut(LOG_INFO,"  -d, --debug\n");
   PrintOut(LOG_INFO,"        Start smartd in debug mode\n\n");
   PrintOut(LOG_INFO,"  -D, --showdirectives\n");
@@ -1472,12 +1624,6 @@ void Usage (void){
   PrintOut(LOG_INFO,"        Save disk states to {PREFIX}MODEL-SERIAL.TYPE.state\n");
 #ifdef SMARTMONTOOLS_SAVESTATES
   PrintOut(LOG_INFO,"        [default is "SMARTMONTOOLS_SAVESTATES"MODEL-SERIAL.TYPE.state]\n");
-#endif
-  PrintOut(LOG_INFO,"\n");
-  PrintOut(LOG_INFO,"  -B [+]FILE, --drivedb=[+]FILE\n");
-  PrintOut(LOG_INFO,"        Read and replace [add] drive database from FILE\n");
-#ifdef SMARTMONTOOLS_DRIVEDBDIR
-  PrintOut(LOG_INFO,"        [default is "SMARTMONTOOLS_DRIVEDBDIR"/drivedb.h]\n");
 #endif
   PrintOut(LOG_INFO,"\n");
 #ifdef _WIN32
@@ -1509,19 +1655,28 @@ static bool not_allowed_in_filename(char c)
            || ('a' <= c && c <= 'z'));
 }
 
-// returns <0 on failure
-static int ATAErrorCount(ata_device * device, const char * name,
-                         unsigned char fix_firmwarebug)
+// Read error count from Summary or Extended Comprehensive SMART error log
+// Return -1 on error
+static int read_ata_error_count(ata_device * device, const char * name,
+                                unsigned char fix_firmwarebug, bool extended)
 {
-  struct ata_smart_errorlog log;
-  
-  if (ataReadErrorLog(device, &log, fix_firmwarebug)){
-    PrintOut(LOG_INFO,"Device: %s, Read SMART Error Log Failed\n",name);
-    return -1;
+  if (!extended) {
+    ata_smart_errorlog log;
+    if (ataReadErrorLog(device, &log, fix_firmwarebug)){
+      PrintOut(LOG_INFO,"Device: %s, Read Summary SMART Error Log failed\n",name);
+      return -1;
+    }
+    return (log.error_log_pointer ? log.ata_error_count : 0);
+  }
+  else {
+    ata_smart_exterrlog logx;
+    if (!ataReadExtErrorLog(device, &logx, 1 /*first sector only*/)) {
+      PrintOut(LOG_INFO,"Device: %s, Read Extended Comprehensive SMART Error Log failed\n",name);
+      return -1;
+    }
+    // Some disks use the reserved byte as index, see ataprint.cpp.
+    return (logx.error_log_index || logx.reserved1 ? logx.device_error_count : 0);
   }
-  
-  // return current number of ATA errors
-  return log.error_log_pointer?log.ata_error_count:0;
 }
 
 // returns <0 if problem.  Otherwise, bottom 8 bits are the self test
@@ -1543,6 +1698,41 @@ static int SelfTestErrorCount(ata_device * device, const char * name,
 #define SELFTEST_ERRORCOUNT(x) (x & 0xff)
 #define SELFTEST_ERRORHOURS(x) ((x >> 8) & 0xffff)
 
+// Check offline data collection status
+static inline bool is_offl_coll_in_progress(unsigned char status)
+{
+  return ((status & 0x7f) == 0x03);
+}
+
+// Check self-test execution status
+static inline bool is_self_test_in_progress(unsigned char status)
+{
+  return ((status >> 4) == 0xf);
+}
+
+// Log offline data collection status
+static void log_offline_data_coll_status(const char * name, unsigned char status)
+{
+  const char * msg;
+  switch (status & 0x7f) {
+    case 0x00: msg = "was never started"; break;
+    case 0x02: msg = "was completed without error"; break;
+    case 0x03: msg = "is in progress"; break;
+    case 0x04: msg = "was suspended by an interrupting command from host"; break;
+    case 0x05: msg = "was aborted by an interrupting command from host"; break;
+    case 0x06: msg = "was aborted by the device with a fatal error"; break;
+    default:   msg = 0;
+  }
+
+  if (msg)
+    PrintOut(((status & 0x7f) == 0x06 ? LOG_CRIT : LOG_INFO),
+             "Device: %s, offline data collection %s%s\n", name, msg,
+             ((status & 0x80) ? " (auto:on)" : ""));
+  else
+    PrintOut(LOG_INFO, "Device: %s, unknown offline data collection status 0x%02x\n",
+             name, status);
+}
+
 // Log self-test execution status
 static void log_self_test_exec_status(const char * name, unsigned char status)
 {
@@ -1571,6 +1761,64 @@ static void log_self_test_exec_status(const char * name, unsigned char status)
              name, status);
 }
 
+// Check pending sector count id (-C, -U directives).
+static bool check_pending_id(const dev_config & cfg, const dev_state & state,
+                             unsigned char id, const char * msg)
+{
+  // Check attribute index
+  int i = ata_find_attr_index(id, state.smartval);
+  if (i < 0) {
+    PrintOut(LOG_INFO, "Device: %s, can't monitor %s count - no Attribute %d\n",
+             cfg.name.c_str(), msg, id);
+    return false;
+  }
+
+  // Check value
+  uint64_t rawval = ata_get_attr_raw_value(state.smartval.vendor_attributes[i],
+    cfg.attribute_defs);
+  if (rawval >= (state.num_sectors ? state.num_sectors : 0xffffffffULL)) {
+    PrintOut(LOG_INFO, "Device: %s, ignoring %s count - bogus Attribute %d value %"PRIu64" (0x%"PRIx64")\n",
+             cfg.name.c_str(), msg, id, rawval, rawval);
+    return false;
+  }
+
+  return true;
+}
+
+// Called by ATA/SCSIDeviceScan() after successful device check
+static void finish_device_scan(dev_config & cfg, dev_state & state)
+{
+  // Set cfg.emailfreq if user hasn't set it
+  if ((!cfg.emailaddress.empty() || !cfg.emailcmdline.empty()) && !cfg.emailfreq) {
+    // Avoid that emails are suppressed forever due to state persistence
+    if (cfg.state_file.empty())
+      cfg.emailfreq = 1; // '-M once'
+    else
+      cfg.emailfreq = 2; // '-M daily'
+  }
+
+  // Start self-test regex check now if time was not read from state file
+  if (!cfg.test_regex.empty() && !state.scheduled_test_next_check)
+    state.scheduled_test_next_check = time(0);
+}
+
+// Common function to format result message for ATA setting
+static void format_set_result_msg(std::string & msg, const char * name, bool ok,
+                                  int set_option = 0, bool has_value = false)
+{
+  if (!msg.empty())
+    msg += ", ";
+  msg += name;
+  if (!ok)
+    msg += ":--";
+  else if (set_option < 0)
+    msg += ":off";
+  else if (has_value)
+    msg += strprintf(":%d", set_option-1);
+  else if (set_option > 0)
+    msg += ":on";
+}
+
 
 // TODO: Add '-F swapid' directive
 const bool fix_swapped_id = false;
@@ -1586,7 +1834,7 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
   // Device must be open
 
   // Get drive identity structure
-  if ((retid=ataReadHDIdentity (atadev, &drive))){
+  if ((retid = ata_read_identity(atadev, &drive, fix_swapped_id))) {
     if (retid<0)
       // Unable to read Identity structure
       PrintOut(LOG_INFO,"Device: %s, not ATA, no IDENTIFY DEVICE Structure\n",name);
@@ -1596,27 +1844,52 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
     CloseDevice(atadev, name);
     return 2; 
   }
-  // Store drive size (for selective self-test only)
-  state.num_sectors = get_num_sectors(&drive);
+
+  // Log drive identity and size
+  char model[40+1], serial[20+1], firmware[8+1];
+  ata_format_id_string(model, drive.model, sizeof(model)-1);
+  ata_format_id_string(serial, drive.serial_no, sizeof(serial)-1);
+  ata_format_id_string(firmware, drive.fw_rev, sizeof(firmware)-1);
+
+  ata_size_info sizes;
+  ata_get_size_info(&drive, sizes);
+  state.num_sectors = sizes.sectors;
+
+  char wwn[30]; wwn[0] = 0;
+  unsigned oui = 0; uint64_t unique_id = 0;
+  int naa = ata_get_wwn(&drive, oui, unique_id);
+  if (naa >= 0)
+    snprintf(wwn, sizeof(wwn), "WWN:%x-%06x-%09"PRIx64", ", naa, oui, unique_id);
+
+  char cap[32];
+  PrintOut(LOG_INFO, "Device: %s, %s, S/N:%s, %sFW:%s, %s\n", name,
+           model, serial, wwn, firmware,
+           format_capacity(cap, sizeof(cap), sizes.capacity, "."));
 
   // Show if device in database, and use preset vendor attribute
   // options unless user has requested otherwise.
   if (cfg.ignorepresets)
     PrintOut(LOG_INFO, "Device: %s, smartd database not searched (Directive: -P ignore).\n", name);
   else {
-    // do whatever applypresets decides to do.
-    if (!apply_presets(&drive, cfg.attributedefs, cfg.fix_firmwarebug, fix_swapped_id))
+    // Apply vendor specific presets, print warning if present
+    const drive_settings * dbentry = lookup_drive_apply_presets(
+      &drive, cfg.attribute_defs, cfg.fix_firmwarebug);
+    if (!dbentry)
       PrintOut(LOG_INFO, "Device: %s, not found in smartd database.\n", name);
-    else
-      PrintOut(LOG_INFO, "Device: %s, found in smartd database.\n", name);
+    else {
+      PrintOut(LOG_INFO, "Device: %s, found in smartd database%s%s\n",
+        name, (*dbentry->modelfamily ? ": " : "."), (*dbentry->modelfamily ? dbentry->modelfamily : ""));
+      if (*dbentry->warningmsg)
+        PrintOut(LOG_CRIT, "Device: %s, WARNING: %s\n", name, dbentry->warningmsg);
+    }
   }
 
   // Set default '-C 197[+]' if no '-C ID' is specified.
   if (!cfg.curr_pending_set)
-    cfg.curr_pending_id = get_unc_attr_id(false, cfg.attributedefs, cfg.curr_pending_incr);
+    cfg.curr_pending_id = get_unc_attr_id(false, cfg.attribute_defs, cfg.curr_pending_incr);
   // Set default '-U 198[+]' if no '-U ID' is specified.
   if (!cfg.offl_pending_set)
-    cfg.offl_pending_id = get_unc_attr_id(true, cfg.attributedefs, cfg.offl_pending_incr);
+    cfg.offl_pending_id = get_unc_attr_id(true, cfg.attribute_defs, cfg.offl_pending_incr);
 
   // If requested, show which presets would be used for this drive
   if (cfg.showpresets) {
@@ -1624,7 +1897,7 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
     PrintOut(LOG_INFO, "Device %s: presets are:\n", name);
     if (!debugmode)
       debugmode=2;
-    show_presets(&drive, false);
+    show_presets(&drive);
     debugmode=savedebugmode;
   }
 
@@ -1684,43 +1957,68 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
   // information was ALSO reproduced in the IDENTIFY DEVICE response,
   // but sadly not for ATA-5.  Sigh.
 
-  // do we need to retain SMART data after returning from this routine?
-  bool retainsmartdata = (cfg.usagefailed || cfg.prefail || cfg.usage || cfg.tempdiff || cfg.tempinfo || cfg.tempcrit);
-  
   // do we need to get SMART data?
   bool smart_val_ok = false;
-  if (   retainsmartdata || cfg.autoofflinetest || cfg.selftest || cfg.errorlog
-      || cfg.curr_pending_id || cfg.offl_pending_id                            ) {
-
-    if (ataReadSmartValues(atadev, &state.smartval) ||
-        ataReadSmartThresholds (atadev, &state.smartthres)) {
-      PrintOut(LOG_INFO,"Device: %s, Read SMART Values and/or Thresholds Failed\n",name);
-      retainsmartdata = cfg.usagefailed = cfg.prefail = cfg.usage = false;
+  if (   cfg.autoofflinetest || cfg.selftest
+      || cfg.errorlog        || cfg.xerrorlog
+      || cfg.offlinests      || cfg.selfteststs
+      || cfg.usagefailed     || cfg.prefail  || cfg.usage
+      || cfg.tempdiff        || cfg.tempinfo || cfg.tempcrit
+      || cfg.curr_pending_id || cfg.offl_pending_id         ) {
+
+    if (ataReadSmartValues(atadev, &state.smartval)) {
+      PrintOut(LOG_INFO, "Device: %s, Read SMART Values failed\n", name);
+      cfg.usagefailed = cfg.prefail = cfg.usage = false;
       cfg.tempdiff = cfg.tempinfo = cfg.tempcrit = 0;
       cfg.curr_pending_id = cfg.offl_pending_id = 0;
     }
-    else
+    else {
       smart_val_ok = true;
+      if (ataReadSmartThresholds(atadev, &state.smartthres)) {
+        PrintOut(LOG_INFO, "Device: %s, Read SMART Thresholds failed%s\n",
+                 name, (cfg.usagefailed ? ", ignoring -f Directive" : ""));
+        cfg.usagefailed = false;
+        // Let ata_get_attr_state() return ATTRSTATE_NO_THRESHOLD:
+        memset(&state.smartthres, 0, sizeof(state.smartthres));
+      }
+    }
 
     // see if the necessary Attribute is there to monitor offline or
     // current pending sectors or temperature
-    if (cfg.curr_pending_id && ATAReturnAttributeRawValue(cfg.curr_pending_id, &state.smartval) < 0) {
-      PrintOut(LOG_INFO,"Device: %s, can't monitor Current Pending Sector count - no Attribute %d\n",
-               name, cfg.curr_pending_id);
+    if (   cfg.curr_pending_id
+        && !check_pending_id(cfg, state, cfg.curr_pending_id,
+              "Current_Pending_Sector"))
       cfg.curr_pending_id = 0;
-    }
-    
-    if (cfg.offl_pending_id && ATAReturnAttributeRawValue(cfg.offl_pending_id, &state.smartval) < 0) {
-      PrintOut(LOG_INFO,"Device: %s, can't monitor Offline Uncorrectable Sector count - no Attribute %d\n",
-               name, cfg.offl_pending_id);
+
+    if (   cfg.offl_pending_id
+        && !check_pending_id(cfg, state, cfg.offl_pending_id,
+              "Offline_Uncorrectable"))
       cfg.offl_pending_id = 0;
-    }
 
     if (   (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)
-        && !ATAReturnTemperatureValue(&state.smartval, cfg.attributedefs)) {
+        && !ata_return_temperature_value(&state.smartval, cfg.attribute_defs)) {
       PrintOut(LOG_CRIT, "Device: %s, can't monitor Temperature, ignoring -W Directive\n", name);
       cfg.tempdiff = cfg.tempinfo = cfg.tempcrit = 0;
     }
+
+    // Report ignored '-r' or '-R' directives
+    for (int id = 1; id <= 255; id++) {
+      if (cfg.monitor_attr_flags.is_set(id, MONITOR_RAW_PRINT)) {
+        char opt = (!cfg.monitor_attr_flags.is_set(id, MONITOR_RAW) ? 'r' : 'R');
+        const char * excl = (cfg.monitor_attr_flags.is_set(id,
+          (opt == 'r' ? MONITOR_AS_CRIT : MONITOR_RAW_AS_CRIT)) ? "!" : "");
+
+        int idx = ata_find_attr_index(id, state.smartval);
+        if (idx < 0)
+          PrintOut(LOG_INFO,"Device: %s, no Attribute %d, ignoring -%c %d%s\n", name, id, opt, id, excl);
+        else {
+          bool prefail = !!ATTRIBUTE_FLAGS_PREFAILURE(state.smartval.vendor_attributes[idx].flags);
+          if (!((prefail && cfg.prefail) || (!prefail && cfg.usage)))
+            PrintOut(LOG_INFO,"Device: %s, not monitoring %s Attributes, ignoring -%c %d%s\n", name,
+                     (prefail ? "Prefailure" : "Usage"), opt, id, excl);
+        }
+      }
+    }
   }
   
   // enable/disable automatic on-line testing
@@ -1740,49 +2038,93 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
         PrintOut(LOG_INFO,"Device: %s, %sd SMART Automatic Offline Testing.\n", name, what);
     }
   }
-  
+
+  // Read log directories if required for capability check
+  ata_smart_log_directory smart_logdir, gp_logdir;
+  bool smart_logdir_ok = false, gp_logdir_ok = false;
+
+  if (   isGeneralPurposeLoggingCapable(&drive)
+      && (cfg.errorlog || cfg.selftest)        ) {
+      if (!ataReadLogDirectory(atadev, &smart_logdir, false))
+        smart_logdir_ok = true;
+  }
+
+  if (cfg.xerrorlog) {
+    if (!ataReadLogDirectory(atadev, &gp_logdir, true))
+      gp_logdir_ok = true;
+  }
+
   // capability check: self-test-log
+  state.selflogcount = 0; state.selfloghour = 0;
   if (cfg.selftest) {
     int retval;
-    
-    // start with service disabled, and re-enable it if all works OK
-    cfg.selftest = false;
-    state.selflogcount = 0;
-    state.selfloghour = 0;
-
-    if (!smart_val_ok)
-      PrintOut(LOG_INFO, "Device: %s, no SMART Self-Test log (SMART READ DATA failed); disabling -l selftest\n", name);
-    else if (!cfg.permissive && !isSmartTestLogCapable(&state.smartval, &drive))
-      PrintOut(LOG_INFO, "Device: %s, appears to lack SMART Self-Test log; disabling -l selftest (override with -T permissive Directive)\n", name);
-    else if ((retval = SelfTestErrorCount(atadev, name, cfg.fix_firmwarebug)) < 0)
-      PrintOut(LOG_INFO, "Device: %s, no SMART Self-Test log; remove -l selftest Directive from smartd.conf\n", name);
+    if (!(   cfg.permissive
+          || ( smart_logdir_ok && smart_logdir.entry[0x06-1].numsectors)
+          || (!smart_logdir_ok && smart_val_ok && isSmartTestLogCapable(&state.smartval, &drive)))) {
+      PrintOut(LOG_INFO, "Device: %s, no SMART Self-test Log, ignoring -l selftest (override with -T permissive)\n", name);
+      cfg.selftest = false;
+    }
+    else if ((retval = SelfTestErrorCount(atadev, name, cfg.fix_firmwarebug)) < 0) {
+      PrintOut(LOG_INFO, "Device: %s, no SMART Self-test Log, ignoring -l selftest\n", name);
+      cfg.selftest = false;
+    }
     else {
-      cfg.selftest = true;
       state.selflogcount=SELFTEST_ERRORCOUNT(retval);
       state.selfloghour =SELFTEST_ERRORHOURS(retval);
     }
   }
   
   // capability check: ATA error log
+  state.ataerrorcount = 0;
   if (cfg.errorlog) {
-    int val;
+    int errcnt1;
+    if (!(   cfg.permissive
+          || ( smart_logdir_ok && smart_logdir.entry[0x01-1].numsectors)
+          || (!smart_logdir_ok && smart_val_ok && isSmartErrorLogCapable(&state.smartval, &drive)))) {
+      PrintOut(LOG_INFO, "Device: %s, no SMART Error Log, ignoring -l error (override with -T permissive)\n", name);
+      cfg.errorlog = false;
+    }
+    else if ((errcnt1 = read_ata_error_count(atadev, name, cfg.fix_firmwarebug, false)) < 0) {
+      PrintOut(LOG_INFO, "Device: %s, no SMART Error Log, ignoring -l error\n", name);
+      cfg.errorlog = false;
+    }
+    else
+      state.ataerrorcount = errcnt1;
+  }
 
-    // start with service disabled, and re-enable it if all works OK
-    cfg.errorlog = false;
-    state.ataerrorcount=0;
+  if (cfg.xerrorlog) {
+    int errcnt2;
+    if (!(cfg.permissive || (gp_logdir_ok && gp_logdir.entry[0x03-1].numsectors))) {
+      PrintOut(LOG_INFO, "Device: %s, no Extended Comprehensive SMART Error Log, ignoring -l xerror (override with -T permissive)\n",
+               name);
+      cfg.xerrorlog = false;
+    }
+    else if ((errcnt2 = read_ata_error_count(atadev, name, cfg.fix_firmwarebug, true)) < 0) {
+      PrintOut(LOG_INFO, "Device: %s, no Extended Comprehensive SMART Error Log, ignoring -l xerror\n", name);
+      cfg.xerrorlog = false;
+    }
+    else if (cfg.errorlog && state.ataerrorcount != errcnt2) {
+      PrintOut(LOG_INFO, "Device: %s, SMART Error Logs report different error counts: %d != %d\n",
+               name, state.ataerrorcount, errcnt2);
+      // Record max error count
+      if (errcnt2 > state.ataerrorcount)
+        state.ataerrorcount = errcnt2;
+    }
+    else
+      state.ataerrorcount = errcnt2;
+  }
 
-    if (!smart_val_ok)
-      PrintOut(LOG_INFO, "Device: %s, no SMART Error log (SMART READ DATA failed); disabling -l error\n", name);
-    else if (!cfg.permissive && !isSmartErrorLogCapable(&state.smartval, &drive))
-      PrintOut(LOG_INFO, "Device: %s, appears to lack SMART Error log; disabling -l error (override with -T permissive Directive)\n", name);
-    else if ((val = ATAErrorCount(atadev, name, cfg.fix_firmwarebug)) < 0)
-      PrintOut(LOG_INFO, "Device: %s, no SMART Error log; remove -l error Directive from smartd.conf\n", name);
-    else {
-        cfg.errorlog = true;
-        state.ataerrorcount=val;
+  // capability check: self-test and offline data collection status
+  if (cfg.offlinests || cfg.selfteststs) {
+    if (!(cfg.permissive || (smart_val_ok && state.smartval.offline_data_collection_capability))) {
+      if (cfg.offlinests)
+        PrintOut(LOG_INFO, "Device: %s, no SMART Offline Data Collection capability, ignoring -l offlinests (override with -T permissive)\n", name);
+      if (cfg.selfteststs)
+        PrintOut(LOG_INFO, "Device: %s, no SMART Self-test capability, ignoring -l selfteststs (override with -T permissive)\n", name);
+      cfg.offlinests = cfg.selfteststs = false;
     }
   }
-  
+
   // capabilities check -- does it support powermode?
   if (cfg.powermode) {
     int powermode = ataCheckPowerMode(atadev);
@@ -1798,10 +2140,59 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
     }
   }
 
+  // Apply ATA settings
+  std::string msg;
+
+  if (cfg.set_aam)
+    format_set_result_msg(msg, "AAM", (cfg.set_aam > 0 ?
+      ata_set_features(atadev, ATA_ENABLE_AAM, cfg.set_aam-1) :
+      ata_set_features(atadev, ATA_DISABLE_AAM)), cfg.set_aam, true);
+
+  if (cfg.set_apm)
+    format_set_result_msg(msg, "APM", (cfg.set_apm > 0 ?
+      ata_set_features(atadev, ATA_ENABLE_APM, cfg.set_apm-1) :
+      ata_set_features(atadev, ATA_DISABLE_APM)), cfg.set_apm, true);
+
+  if (cfg.set_lookahead)
+    format_set_result_msg(msg, "Rd-ahead", ata_set_features(atadev,
+      (cfg.set_lookahead > 0 ? ATA_ENABLE_READ_LOOK_AHEAD : ATA_DISABLE_READ_LOOK_AHEAD)),
+      cfg.set_lookahead);
+
+  if (cfg.set_wcache)
+    format_set_result_msg(msg, "Wr-cache", ata_set_features(atadev,
+      (cfg.set_wcache > 0? ATA_ENABLE_WRITE_CACHE : ATA_DISABLE_WRITE_CACHE)), cfg.set_wcache);
+
+  if (cfg.set_security_freeze)
+    format_set_result_msg(msg, "Security freeze",
+      ata_nodata_command(atadev, ATA_SECURITY_FREEZE_LOCK));
+
+  if (cfg.set_standby)
+    format_set_result_msg(msg, "Standby",
+      ata_nodata_command(atadev, ATA_IDLE, cfg.set_standby-1), cfg.set_standby, true);
+
+  // Report as one log entry
+  if (!msg.empty())
+    PrintOut(LOG_INFO, "Device: %s, ATA settings applied: %s\n", name, msg.c_str());
+
+  // set SCT Error Recovery Control if requested
+  if (cfg.sct_erc_set) {
+    if (!isSCTErrorRecoveryControlCapable(&drive))
+      PrintOut(LOG_INFO, "Device: %s, no SCT Error Recovery Control support, ignoring -l scterc\n",
+               name);
+    else if (   ataSetSCTErrorRecoveryControltime(atadev, 1, cfg.sct_erc_readtime )
+             || ataSetSCTErrorRecoveryControltime(atadev, 2, cfg.sct_erc_writetime))
+      PrintOut(LOG_INFO, "Device: %s, set of SCT Error Recovery Control failed\n", name);
+    else
+      PrintOut(LOG_INFO, "Device: %s, SCT Error Recovery Control set to: Read: %u, Write: %u\n",
+               name, cfg.sct_erc_readtime, cfg.sct_erc_writetime);
+  }
+
   // If no tests available or selected, return
-  if (!(cfg.errorlog    || cfg.selftest || cfg.smartcheck ||
-        cfg.usagefailed || cfg.prefail  || cfg.usage      ||
-        cfg.tempdiff    || cfg.tempinfo || cfg.tempcrit     )) {
+  if (!(   cfg.smartcheck  || cfg.selftest
+        || cfg.errorlog    || cfg.xerrorlog
+        || cfg.offlinests  || cfg.selfteststs
+        || cfg.usagefailed || cfg.prefail  || cfg.usage
+        || cfg.tempdiff    || cfg.tempinfo || cfg.tempcrit)) {
     CloseDevice(atadev, name);
     return 3;
   }
@@ -1809,18 +2200,11 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
   // tell user we are registering device
   PrintOut(LOG_INFO,"Device: %s, is SMART capable. Adding to \"monitor\" list.\n",name);
   
-  // record number of device, type of device, increment device count
-  if (cfg.dev_type.empty())
-    cfg.dev_type = "ata";
-  
   // close file descriptor
   CloseDevice(atadev, name);
 
   if (!state_path_prefix.empty() || !attrlog_path_prefix.empty()) {
     // Build file name for state file
-    char model[40+1], serial[20+1];
-    format_ata_string(model, drive.model, sizeof(model)-1, fix_swapped_id);
-    format_ata_string(serial, drive.serial_no, sizeof(serial)-1, fix_swapped_id);
     std::replace_if(model, model+strlen(model), not_allowed_in_filename, '_');
     std::replace_if(serial, serial+strlen(serial), not_allowed_in_filename, '_');
     if (!state_path_prefix.empty()) {
@@ -1836,9 +2220,7 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
       cfg.attrlog_file = strprintf("%s%s-%s.ata.csv", attrlog_path_prefix.c_str(), model, serial);
   }
 
-  // Start self-test regex check now if time was not read from state file
-  if (!cfg.test_regex.empty() && !state.scheduled_test_next_check)
-    state.scheduled_test_next_check = time(0);
+  finish_device_scan(cfg, state);
 
   return 0;
 }
@@ -1847,12 +2229,65 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade
 // please.
 static int SCSIDeviceScan(dev_config & cfg, dev_state & state, scsi_device * scsidev)
 {
-  int k, err;
+  int k, err, req_len, avail_len, version, len;
   const char *device = cfg.name.c_str();
   struct scsi_iec_mode_page iec;
   UINT8  tBuf[64];
+  UINT8  inqBuf[96];
+  UINT8  vpdBuf[252];
+  char lu_id[64];
 
   // Device must be open
+  memset(inqBuf, 0, 96);
+  req_len = 36;
+  if ((err = scsiStdInquiry(scsidev, inqBuf, req_len))) {
+    /* Marvell controllers fail on a 36 bytes StdInquiry, but 64 suffices */
+    req_len = 64;
+    if ((err = scsiStdInquiry(scsidev, inqBuf, req_len))) {
+      PrintOut(LOG_INFO, "Device: %s, Both 36 and 64 byte INQUIRY failed; "
+              "skip device\n", device);
+      return 2;
+    }
+  }
+  version = inqBuf[2];
+  avail_len = inqBuf[4] + 5;
+  len = (avail_len < req_len) ? avail_len : req_len;
+  if (len < 36) {
+    PrintOut(LOG_INFO, "Device: %s, INQUIRY response less than 36 bytes; "
+            "skip device\n", device);
+    return 2;
+  }
+
+  int pdt = inqBuf[0] & 0x1f;
+
+  if (! ((0 == pdt) || (4 == pdt) || (5 == pdt) || (7 == pdt) ||
+         (0xe == pdt))) {
+    PrintOut(LOG_INFO, "Device: %s, not a disk like device [PDT=0x%x], "
+             "skip\n", device, pdt);
+    return 2;
+  }
+  lu_id[0] = '\0';
+  if ((version >= 0x4) && (version < 0x8)) {
+    /* SPC-2 to SPC-5 */
+    if (0 == (err = scsiInquiryVpd(scsidev, 0x83, vpdBuf, sizeof(vpdBuf)))) {
+      len = vpdBuf[3];
+      scsi_decode_lu_dev_id(vpdBuf + 4, len, lu_id, sizeof(lu_id), NULL);
+    }
+  } 
+
+  unsigned int lb_size;
+  char si_str[64];
+  uint64_t capacity = scsiGetSize(scsidev, &lb_size);
+
+  if (capacity)
+    format_capacity(si_str, sizeof(si_str), capacity);
+  else
+    si_str[0] = '\0';
+  PrintOut(LOG_INFO, "Device: %s, [%.8s %.16s %.4s]%s%s%s%s\n",
+          device, (char *)&inqBuf[8], (char *)&inqBuf[16],
+          (char *)&inqBuf[32],
+          (lu_id[0] ? ", lu id: " : ""), (lu_id[0] ? lu_id : ""),
+          (si_str[0] ? ", " : ""), (si_str[0] ? si_str : ""));
 
   // check that device is ready for commands. IE stores its stuff on
   // the media.
@@ -1914,10 +2349,6 @@ static int SCSIDeviceScan(dev_config & cfg, dev_state & state, scsi_device * scs
     }   
   }
   
-  // record type of device
-  if (cfg.dev_type.empty())
-    cfg.dev_type = "scsi";
-  
   // Check if scsiCheckIE() is going to work
   {
     UINT8 asc = 0;
@@ -1981,85 +2412,17 @@ static int SCSIDeviceScan(dev_config & cfg, dev_state & state, scsi_device * scs
     PrintOut(LOG_INFO, "Device: %s, attribute log not yet supported for SCSI; ignoring -A option.\n", device);
   }
 
+  // Make sure that init_standby_check() ignores SCSI devices
+  cfg.offlinests_ns = cfg.selfteststs_ns = false;
+
   // close file descriptor
   CloseDevice(scsidev, device);
 
-  // Start self-test regex check now if time was not read from state file
-  if (!cfg.test_regex.empty() && !state.scheduled_test_next_check)
-    state.scheduled_test_next_check = time(0);
+  finish_device_scan(cfg, state);
 
   return 0;
 }
 
-
-struct changedattribute_t {
-  unsigned char newval;
-  unsigned char oldval;
-  unsigned char id;
-  unsigned char prefail;
-  unsigned char sameraw;
-};
-
-// We compare old and new values of the n'th attribute.  Note that n
-// is NOT the attribute ID number.. If (Normalized & Raw) equal,
-// then return 0, else nonzero.
-static int ATACompareValues(changedattribute_t *delta,
-                            struct ata_smart_values *newv,
-                            struct ata_smart_values *oldv,
-                            struct ata_smart_thresholds_pvt *thresholds,
-                            int n, const char * name)
-{
-  struct ata_smart_attribute *now,*was;
-  struct ata_smart_threshold_entry *thre;
-  unsigned char oldval,newval;
-  int sameraw;
-
-  // check that attribute number in range, and no null pointers
-  if (n<0 || n>=NUMBER_ATA_SMART_ATTRIBUTES || !newv || !oldv || !thresholds)
-    return 0;
-  
-  // pointers to disk's values and vendor's thresholds
-  now=newv->vendor_attributes+n;
-  was=oldv->vendor_attributes+n;
-  thre=thresholds->thres_entries+n;
-
-  // consider only valid attributes
-  if (!now->id || !was->id || !thre->id)
-    return 0;
-  
-  
-  // issue warning if they don't have the same ID in all structures:
-  if ( (now->id != was->id) || (now->id != thre->id) ){
-    PrintOut(LOG_INFO,"Device: %s, same Attribute has different ID numbers: %d = %d = %d\n",
-             name, (int)now->id, (int)was->id, (int)thre->id);
-    return 0;
-  }
-
-  // new and old values of Normalized Attributes
-  newval=now->current;
-  oldval=was->current;
-
-  // See if the RAW values are unchanged (ie, the same)
-  if (memcmp(now->raw, was->raw, 6))
-    sameraw=0;
-  else
-    sameraw=1;
-  
-  // if any values out of the allowed range, or if the values haven't
-  // changed, return 0
-  if (!newval || !oldval || newval>0xfe || oldval>0xfe || (oldval==newval && sameraw))
-    return 0;
-  
-  // values have changed.  Construct output and return
-  delta->newval=newval;
-  delta->oldval=oldval;
-  delta->id=now->id;
-  delta->prefail=ATTRIBUTE_FLAGS_PREFAILURE(now->flags);
-  delta->sameraw=sameraw;
-
-  return 1;
-}
-
 // If the self-test log has got more self-test errors (or more recent
 // self-test errors) recorded, then notify user.
 static void CheckSelfTestLogs(const dev_config & cfg, dev_state & state, int newi)
@@ -2069,7 +2432,9 @@ static void CheckSelfTestLogs(const dev_config & cfg, dev_state & state, int new
   if (newi<0)
     // command failed
     MailWarning(cfg, state, 8, "Device: %s, Read SMART Self-Test Log Failed", name);
-  else {      
+  else {
+    reset_warning_mail(cfg, state, 8, "Read SMART Self-Test Log worked again");
+
     // old and new error counts
     int oldc=state.selflogcount;
     int newc=SELFTEST_ERRORCOUNT(newi);
@@ -2085,7 +2450,8 @@ static void CheckSelfTestLogs(const dev_config & cfg, dev_state & state, int new
       MailWarning(cfg, state, 3, "Device: %s, Self-Test Log error count increased from %d to %d",
                    name, oldc, newc);
       state.must_write = true;
-    } else if (oldh!=newh) {
+    }
+    else if (newc > 0 && oldh != newh) {
       // more recent error
       // a 'more recent' error might actually be a smaller hour number,
       // if the hour number has wrapped.
@@ -2099,7 +2465,16 @@ static void CheckSelfTestLogs(const dev_config & cfg, dev_state & state, int new
                    name, newh);
       state.must_write = true;
     }
-    
+
+    // Print info if error entries have disappeared
+    // or newer successful successful extended self-test exits
+    if (oldc > newc) {
+      PrintOut(LOG_INFO, "Device: %s, Self-Test Log error count decreased from %d to %d\n",
+               name, oldc, newc);
+      if (newc == 0)
+        reset_warning_mail(cfg, state, 3, "Self-Test Log does no longer report errors");
+    }
+
     // Needed since self-test error count may DECREASE.  Hour might
     // also have changed.
     state.selflogcount= newc;
@@ -2110,7 +2485,7 @@ static void CheckSelfTestLogs(const dev_config & cfg, dev_state & state, int new
 
 // Test types, ordered by priority.
 static const char test_type_chars[] = "LncrSCO";
-const unsigned num_test_types = sizeof(test_type_chars)-1;
+static const unsigned num_test_types = sizeof(test_type_chars)-1;
 
 // returns test type if time to do test of type testtype,
 // 0 if not time to do test.
@@ -2392,10 +2767,13 @@ static int DoATASelfTest(const dev_config & cfg, dev_state & state, ata_device *
 
   if (dotest == SELECTIVE_SELF_TEST) {
     // Set test span
-    ata_selective_selftest_args selargs;
+    ata_selective_selftest_args selargs, prev_args;
     selargs.num_spans = 1;
     selargs.span[0].mode = mode;
-    if (ataWriteSelectiveSelfTestLog(device, selargs, &data, state.num_sectors)) {
+    prev_args.num_spans = 1;
+    prev_args.span[0].start = state.selective_test_last_start;
+    prev_args.span[0].end   = state.selective_test_last_end;
+    if (ataWriteSelectiveSelfTestLog(device, selargs, &data, state.num_sectors, &prev_args)) {
       PrintOut(LOG_CRIT, "Device: %s, prepare %sTest failed\n", name, testname);
       return 1;
     }
@@ -2405,6 +2783,8 @@ static int DoATASelfTest(const dev_config & cfg, dev_state & state, ata_device *
       start, end, end - start + 1,
       (unsigned)((100 * start + state.num_sectors/2) / state.num_sectors),
       (unsigned)((100 * end   + state.num_sectors/2) / state.num_sectors));
+    state.selective_test_last_start = start;
+    state.selective_test_last_end = end;
   }
 
   // execute the test, and return status
@@ -2414,9 +2794,12 @@ static int DoATASelfTest(const dev_config & cfg, dev_state & state, ata_device *
     return retval;
   }
 
-  if (testtype != 'O')
-    // Log next self-test execution status
-    state.smartval.self_test_exec_status = 0xff;
+  // Report recent test start to do_disable_standby_check()
+  // and force log of next test status
+  if (testtype == 'O')
+    state.offline_started = true;
+  else
+    state.selftest_started = true;
 
   PrintOut(LOG_INFO, "Device: %s, starting scheduled %sTest.\n", name, testname);
   return 0;
@@ -2428,13 +2811,20 @@ static void check_pending(const dev_config & cfg, dev_state & state,
                           const ata_smart_values & smartval,
                           int mailtype, const char * msg)
 {
+  // Find attribute index
+  int i = ata_find_attr_index(id, smartval);
+  if (!(i >= 0 && ata_find_attr_index(id, state.smartval) == i))
+    return;
+
   // No report if no sectors pending.
-  int64_t rawval = ATAReturnAttributeRawValue(id, &smartval);
-  if (rawval <= 0)
+  uint64_t rawval = ata_get_attr_raw_value(smartval.vendor_attributes[i], cfg.attribute_defs);
+  if (rawval == 0) {
+    reset_warning_mail(cfg, state, mailtype, "No more %s", msg);
     return;
+  }
 
   // If attribute is not reset, report only sector count increases.
-  int64_t prev_rawval = ATAReturnAttributeRawValue(id, &state.smartval);
+  uint64_t prev_rawval = ata_get_attr_raw_value(state.smartval.vendor_attributes[i], cfg.attribute_defs);
   if (!(!increase_only || prev_rawval < rawval))
     return;
 
@@ -2525,9 +2915,113 @@ static void CheckTemperature(const dev_config & cfg, dev_state & state, unsigned
     PrintOut(LOG_INFO, "Device: %s, Temperature %u Celsius reached limit of %u Celsius (Min/Max %s%s/%u%s)\n",
       cfg.name.c_str(), currtemp, cfg.tempinfo, fmt_temp(state.tempmin, buf), minchg, state.tempmax, maxchg);
   }
+  else if (cfg.tempcrit) {
+    unsigned char limit = (cfg.tempinfo ? cfg.tempinfo : cfg.tempcrit-5);
+    if (currtemp < limit)
+      reset_warning_mail(cfg, state, 12, "Temperature %u Celsius dropped below %u Celsius", currtemp, limit);
+  }
+}
+
+// Check normalized and raw attribute values.
+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)
+{
+  // Check attribute and threshold
+  ata_attr_state attrstate = ata_get_attr_state(attr, attridx, thresholds, cfg.attribute_defs);
+  if (attrstate == ATTRSTATE_NON_EXISTING)
+    return;
+
+  // If requested, check for usage attributes that have failed.
+  if (   cfg.usagefailed && attrstate == ATTRSTATE_FAILED_NOW
+      && !cfg.monitor_attr_flags.is_set(attr.id, MONITOR_IGN_FAILUSE)) {
+    std::string attrname = ata_get_smart_attr_name(attr.id, cfg.attribute_defs);
+    PrintOut(LOG_CRIT, "Device: %s, Failed SMART usage Attribute: %d %s.\n", cfg.name.c_str(), attr.id, attrname.c_str());
+    MailWarning(cfg, state, 2, "Device: %s, Failed SMART usage Attribute: %d %s.", cfg.name.c_str(), attr.id, attrname.c_str());
+    state.must_write = true;
+  }
+
+  // Return if we're not tracking this type of attribute
+  bool prefail = !!ATTRIBUTE_FLAGS_PREFAILURE(attr.flags);
+  if (!(   ( prefail && cfg.prefail)
+        || (!prefail && cfg.usage  )))
+    return;
+
+  // Return if '-I ID' was specified
+  if (cfg.monitor_attr_flags.is_set(attr.id, MONITOR_IGNORE))
+    return;
+
+  // Issue warning if they don't have the same ID in all structures.
+  if (attr.id != prev.id) {
+    PrintOut(LOG_INFO,"Device: %s, same Attribute has different ID numbers: %d = %d\n",
+             cfg.name.c_str(), attr.id, prev.id);
+    return;
+  }
+
+  // Compare normalized values if valid.
+  bool valchanged = false;
+  if (attrstate > ATTRSTATE_NO_NORMVAL) {
+    if (attr.current != prev.current)
+      valchanged = true;
+  }
+
+  // Compare raw values if requested.
+  bool rawchanged = false;
+  if (cfg.monitor_attr_flags.is_set(attr.id, MONITOR_RAW)) {
+    if (   ata_get_attr_raw_value(attr, cfg.attribute_defs)
+        != ata_get_attr_raw_value(prev, cfg.attribute_defs))
+      rawchanged = true;
+  }
+
+  // Return if no change
+  if (!(valchanged || rawchanged))
+    return;
+
+  // Format value strings
+  std::string currstr, prevstr;
+  if (attrstate == ATTRSTATE_NO_NORMVAL) {
+    // Print raw values only
+    currstr = strprintf("%s (Raw)",
+      ata_format_attr_raw_value(attr, cfg.attribute_defs).c_str());
+    prevstr = strprintf("%s (Raw)",
+      ata_format_attr_raw_value(prev, cfg.attribute_defs).c_str());
+  }
+  else if (cfg.monitor_attr_flags.is_set(attr.id, MONITOR_RAW_PRINT)) {
+    // Print normalized and raw values
+    currstr = strprintf("%d [Raw %s]", attr.current,
+      ata_format_attr_raw_value(attr, cfg.attribute_defs).c_str());
+    prevstr = strprintf("%d [Raw %s]", prev.current,
+      ata_format_attr_raw_value(prev, cfg.attribute_defs).c_str());
+  }
+  else {
+    // Print normalized values only
+    currstr = strprintf("%d", attr.current);
+    prevstr = strprintf("%d", prev.current);
+  }
+
+  // Format message
+  std::string msg = strprintf("Device: %s, SMART %s Attribute: %d %s changed from %s to %s",
+                              cfg.name.c_str(), (prefail ? "Prefailure" : "Usage"), attr.id,
+                              ata_get_smart_attr_name(attr.id, cfg.attribute_defs).c_str(),
+                              prevstr.c_str(), currstr.c_str());
+
+  // Report this change as critical ?
+  if (   (valchanged && cfg.monitor_attr_flags.is_set(attr.id, MONITOR_AS_CRIT))
+      || (rawchanged && cfg.monitor_attr_flags.is_set(attr.id, MONITOR_RAW_AS_CRIT))) {
+    PrintOut(LOG_CRIT, "%s\n", msg.c_str());
+    MailWarning(cfg, state, 2, "%s", msg.c_str());
+  }
+  else {
+    PrintOut(LOG_INFO, "%s\n", msg.c_str());
+  }
+  state.must_write = true;
 }
 
-static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device * atadev, bool allow_selftests)
+
+static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device * atadev,
+                          bool firstpass, bool allow_selftests)
 {
   const char * name = cfg.name.c_str();
 
@@ -2543,8 +3037,10 @@ static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device
     PrintOut(LOG_INFO, "Device: %s, open() failed: %s\n", name, atadev->get_errmsg());
     MailWarning(cfg, state, 9, "Device: %s, unable to open device", name);
     return 1;
-  } else if (debugmode)
+  }
+  if (debugmode)
     PrintOut(LOG_INFO,"Device: %s, opened ATA device\n", name);
+  reset_warning_mail(cfg, state, 9, "open device worked again");
 
   // user may have requested (with the -n Directive) to leave the disk
   // alone if it is in idle or sleeping mode.  In this case check the
@@ -2636,17 +3132,19 @@ static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device
   // Check everything that depends upon SMART Data (eg, Attribute values)
   if (   cfg.usagefailed || cfg.prefail || cfg.usage
       || cfg.curr_pending_id || cfg.offl_pending_id
-      || cfg.tempdiff || cfg.tempinfo || cfg.tempcrit || cfg.selftest) {
-    struct ata_smart_values     curval;
-    struct ata_smart_thresholds_pvt * thresh = &state.smartthres;
+      || cfg.tempdiff || cfg.tempinfo || cfg.tempcrit
+      || cfg.selftest ||  cfg.offlinests || cfg.selfteststs) {
 
-    // Read current attribute values. *drive contains old values and thresholds
+    // Read current attribute values.
+    ata_smart_values curval;
     if (ataReadSmartValues(atadev, &curval)){
       PrintOut(LOG_CRIT, "Device: %s, failed to read SMART Attribute Data\n", name);
       MailWarning(cfg, state, 6, "Device: %s, failed to read SMART Attribute Data", name);
       state.must_write = true;
     }
     else {
+      reset_warning_mail(cfg, state, 6, "read SMART Attribute Data worked again");
+
       // look for current or offline pending sectors
       if (cfg.curr_pending_id)
         check_pending(cfg, state, cfg.curr_pending_id, cfg.curr_pending_incr, curval, 10,
@@ -2660,118 +3158,56 @@ static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device
 
       // check temperature limits
       if (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)
-        CheckTemperature(cfg, state, ATAReturnTemperatureValue(&curval, cfg.attributedefs), 0);
+        CheckTemperature(cfg, state, ata_return_temperature_value(&curval, cfg.attribute_defs), 0);
 
+      // look for failed usage attributes, or track usage or prefail attributes
       if (cfg.usagefailed || cfg.prefail || cfg.usage) {
-
-       // look for failed usage attributes, or track usage or prefail attributes
         for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
-
-         // This block looks for usage attributes that have failed.
-         // Prefail attributes that have failed are returned with a
-         // positive sign. No failure returns 0. Usage attributes<0.
-          int att;
-         if (cfg.usagefailed && ((att=ataCheckAttribute(&curval, thresh, i))<0)){
-           
-           // are we ignoring failures of this attribute?
-           att *= -1;
-            if (!cfg.monitor_attr_flags.is_set(att, MONITOR_IGN_FAILUSE)) {
-             char attname[64], *loc=attname;
-             
-             // get attribute name & skip white space
-             ataPrintSmartAttribName(loc, att, cfg.attributedefs);
-             while (*loc && *loc==' ') loc++;
-             
-             // warning message
-             PrintOut(LOG_CRIT, "Device: %s, Failed SMART usage Attribute: %s.\n", name, loc);
-             MailWarning(cfg, state, 2, "Device: %s, Failed SMART usage Attribute: %s.", name, loc);
-              state.must_write = true;
-           }
-         }
-         
-         // This block tracks usage or prefailure attributes to see if
-         // they are changing.  It also looks for changes in RAW values
-         // if this has been requested by user.
-          changedattribute_t delta;
-         if ((cfg.usage || cfg.prefail) && ATACompareValues(&delta, &curval, &state.smartval, thresh, i, name)){
-
-            // Continue if we're not tracking this type of attribute
-            if (!(   ( delta.prefail && cfg.prefail)
-                  || (!delta.prefail && cfg.usage   )))
-              continue;
-
-            // Continue if '-I ID' was specified
-            unsigned char id = delta.id;
-            if (cfg.monitor_attr_flags.is_set(id, MONITOR_IGNORE))
-              continue;
-
-           // if the only change is the raw value, and we're not
-           // tracking raw value, then continue loop over attributes
-            if (   !delta.sameraw && delta.newval == delta.oldval
-                && !cfg.monitor_attr_flags.is_set(id, MONITOR_RAW))
-             continue;
-
-            // get attribute name, skip spaces
-            char attname[64], *loc = attname;
-            ataPrintSmartAttribName(loc, id, cfg.attributedefs);
-            while (*loc && *loc==' ')
-              loc++;
-
-            // has the user asked for us to print raw values?
-            char newrawstring[64], oldrawstring[64];
-            if (cfg.monitor_attr_flags.is_set(id, MONITOR_RAW_PRINT)) {
-              // get raw values (as a string) and add to printout
-              char rawstring[64];
-              ataPrintSmartAttribRawValue(rawstring, curval.vendor_attributes+i, cfg.attributedefs);
-              sprintf(newrawstring, " [Raw %s]", rawstring);
-              ataPrintSmartAttribRawValue(rawstring, state.smartval.vendor_attributes+i, cfg.attributedefs);
-              sprintf(oldrawstring, " [Raw %s]", rawstring);
-            }
-            else
-              newrawstring[0]=oldrawstring[0]='\0';
-
-            // Format message
-            std::string msg = strprintf("Device: %s, SMART %s Attribute: %s changed from %d%s to %d%s",
-                                        name, (delta.prefail ? "Prefailure" : "Usage"), loc,
-                                        delta.oldval, oldrawstring, delta.newval, newrawstring);
-
-            // Report this change as critical ?
-            if (   (delta.newval != delta.oldval && cfg.monitor_attr_flags.is_set(id, MONITOR_AS_CRIT))
-                || (!delta.sameraw               && cfg.monitor_attr_flags.is_set(id, MONITOR_RAW_AS_CRIT))) {
-              PrintOut(LOG_CRIT, "%s\n", msg.c_str());
-              MailWarning(cfg, state, 2, "%s", msg.c_str());
-            }
-            else {
-              PrintOut(LOG_INFO, "%s\n", msg.c_str());
-            }
-            state.must_write = true;
-         } // endof block tracking usage or prefailure
-       } // end of loop over attributes
-       
-        if (cfg.selftest) {
-          // Log changes of self-test executions status
-          if (   curval.self_test_exec_status != state.smartval.self_test_exec_status
-              || (!allow_selftests && curval.self_test_exec_status != 0x00)          )
-            log_self_test_exec_status(name, curval.self_test_exec_status);
+          check_attribute(cfg, state,
+                          curval.vendor_attributes[i],
+                          state.smartval.vendor_attributes[i],
+                          i, state.smartthres.thres_entries);
         }
+      }
+
+      // Log changes of offline data collection status
+      if (cfg.offlinests) {
+        if (   curval.offline_data_collection_status
+                != state.smartval.offline_data_collection_status
+            || state.offline_started // test was started in previous call
+            || (firstpass && (debugmode || (curval.offline_data_collection_status & 0x7d))))
+          log_offline_data_coll_status(name, curval.offline_data_collection_status);
+      }
 
-       // Save the new values into *drive for the next time around
-       state.smartval = curval;
+      // Log changes of self-test execution status
+      if (cfg.selfteststs) {
+        if (   curval.self_test_exec_status != state.smartval.self_test_exec_status
+            || state.selftest_started // test was started in previous call
+            || (firstpass && (debugmode || curval.self_test_exec_status != 0x00)))
+          log_self_test_exec_status(name, curval.self_test_exec_status);
       }
+
+      // Save the new values for the next time around
+      state.smartval = curval;
     }
   }
+  state.offline_started = state.selftest_started = false;
   
   // check if number of selftest errors has increased (note: may also DECREASE)
   if (cfg.selftest)
     CheckSelfTestLogs(cfg, state, SelfTestErrorCount(atadev, name, cfg.fix_firmwarebug));
 
   // check if number of ATA errors has increased
-  if (cfg.errorlog) {
+  if (cfg.errorlog || cfg.xerrorlog) {
 
-    int newc, oldc= state.ataerrorcount;
+    int errcnt1 = -1, errcnt2 = -1;
+    if (cfg.errorlog)
+      errcnt1 = read_ata_error_count(atadev, name, cfg.fix_firmwarebug, false);
+    if (cfg.xerrorlog)
+      errcnt2 = read_ata_error_count(atadev, name, cfg.fix_firmwarebug, true);
 
-    // new number of errors
-    newc = ATAErrorCount(atadev, name, cfg.fix_firmwarebug);
+    // new number of errors is max of both logs
+    int newc = (errcnt1 >= errcnt2 ? errcnt1 : errcnt2);
 
     // did command fail?
     if (newc<0)
@@ -2779,6 +3215,7 @@ static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device
       MailWarning(cfg, state, 7, "Device: %s, Read SMART Error Log Failed", name);
 
     // has error count increased?
+    int oldc = state.ataerrorcount;
     if (newc>oldc){
       PrintOut(LOG_CRIT, "Device: %s, ATA error count increased from %d to %d\n",
                name, oldc, newc);
@@ -2786,8 +3223,7 @@ static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device
                    name, oldc, newc);
       state.must_write = true;
     }
-    
-    // this last line is probably not needed, count always increases
+
     if (newc>=0)
       state.ataerrorcount=newc;
   }
@@ -2870,27 +3306,107 @@ static int SCSICheckDevice(const dev_config & cfg, dev_state & state, scsi_devic
     return 0;
 }
 
+// 0=not used, 1=not disabled, 2=disable rejected by OS, 3=disabled
+static int standby_disable_state = 0;
+
+static void init_disable_standby_check(dev_config_vector & configs)
+{
+  // Check for '-l offlinests,ns' or '-l selfteststs,ns' directives
+  bool sts1 = false, sts2 = false;
+  for (unsigned i = 0; i < configs.size() && !(sts1 || sts2); i++) {
+    const dev_config & cfg = configs.at(i);
+    if (cfg.offlinests_ns)
+      sts1 = true;
+    if (cfg.selfteststs_ns)
+      sts2 = true;
+  }
+
+  // Check for support of disable auto standby
+  // Reenable standby if smartd.conf was reread
+  if (sts1 || sts2 || standby_disable_state == 3) {
+   if (!smi()->disable_system_auto_standby(false)) {
+      if (standby_disable_state == 3)
+        PrintOut(LOG_CRIT, "System auto standby enable failed: %s\n", smi()->get_errmsg());
+      if (sts1 || sts2) {
+        PrintOut(LOG_INFO, "Disable auto standby not supported, ignoring ',ns' from %s%s%s\n",
+          (sts1 ? "-l offlinests,ns" : ""), (sts1 && sts2 ? " and " : ""), (sts2 ? "-l selfteststs,ns" : ""));
+        sts1 = sts2 = false;
+      }
+    }
+  }
+
+  standby_disable_state = (sts1 || sts2 ? 1 : 0);
+}
+
+static void do_disable_standby_check(const dev_config_vector & configs, const dev_state_vector & states)
+{
+  if (!standby_disable_state)
+    return;
+
+  // Check for just started or still running self-tests
+  bool running = false;
+  for (unsigned i = 0; i < configs.size() && !running; i++) {
+    const dev_config & cfg = configs.at(i); const dev_state & state = states.at(i);
+
+    if (   (   cfg.offlinests_ns
+            && (state.offline_started ||
+                is_offl_coll_in_progress(state.smartval.offline_data_collection_status)))
+        || (   cfg.selfteststs_ns
+            && (state.selftest_started ||
+                is_self_test_in_progress(state.smartval.self_test_exec_status)))         )
+      running = true;
+    // state.offline/selftest_started will be reset after next logging of test status
+  }
+
+  // Disable/enable auto standby and log state changes
+  if (!running) {
+    if (standby_disable_state != 1) {
+      if (!smi()->disable_system_auto_standby(false))
+        PrintOut(LOG_CRIT, "Self-test(s) completed, system auto standby enable failed: %s\n",
+                 smi()->get_errmsg());
+      else
+        PrintOut(LOG_INFO, "Self-test(s) completed, system auto standby enabled\n");
+      standby_disable_state = 1;
+    }
+  }
+  else if (!smi()->disable_system_auto_standby(true)) {
+    if (standby_disable_state != 2) {
+      PrintOut(LOG_INFO, "Self-test(s) in progress, system auto standby disable rejected: %s\n",
+               smi()->get_errmsg());
+      standby_disable_state = 2;
+    }
+  }
+  else {
+    if (standby_disable_state != 3) {
+      PrintOut(LOG_INFO, "Self-test(s) in progress, system auto standby disabled\n");
+      standby_disable_state = 3;
+    }
+  }
+}
+
 // Checks the SMART status of all ATA and SCSI devices
 static void CheckDevicesOnce(const dev_config_vector & configs, dev_state_vector & states,
-                             smart_device_list & devices, bool allow_selftests)
+                             smart_device_list & devices, bool firstpass, bool allow_selftests)
 {
   for (unsigned i = 0; i < configs.size(); i++) {
     const dev_config & cfg = configs.at(i);
     dev_state & state = states.at(i);
     smart_device * dev = devices.at(i);
     if (dev->is_ata())
-      ATACheckDevice(cfg, state, dev->to_ata(), allow_selftests);
+      ATACheckDevice(cfg, state, dev->to_ata(), firstpass, allow_selftests);
     else if (dev->is_scsi())
       SCSICheckDevice(cfg, state, dev->to_scsi(), allow_selftests);
   }
+
+  do_disable_standby_check(configs, states);
 }
 
 // Set if Initialize() was called
 static bool is_initialized = false;
 
 // Does initialization right after fork to daemon mode
-void Initialize(time_t *wakeuptime){
-
+static void Initialize(time_t *wakeuptime)
+{
   // Call Goodbye() on exit
   is_initialized = true;
   
@@ -2964,7 +3480,8 @@ static time_t dosleep(time_t wakeuptime, bool & sigwakeup)
   }
   
   // sleep until we catch SIGUSR1 or have completed sleeping
-  while (timenow<wakeuptime && !caughtsigUSR1 && !caughtsigHUP && !caughtsigEXIT){
+  int addtime = 0;
+  while (timenow < wakeuptime+addtime && !caughtsigUSR1 && !caughtsigHUP && !caughtsigEXIT) {
     
     // protect user again system clock being adjusted backwards
     if (wakeuptime>timenow+checktime){
@@ -2973,7 +3490,7 @@ static time_t dosleep(time_t wakeuptime, bool & sigwakeup)
     }
     
     // Exit sleep when time interval has expired or a signal is received
-    sleep(wakeuptime-timenow);
+    sleep(wakeuptime+addtime-timenow);
 
 #ifdef _WIN32
     // toggle debug mode?
@@ -2984,6 +3501,19 @@ static time_t dosleep(time_t wakeuptime, bool & sigwakeup)
 #endif
 
     timenow=time(NULL);
+
+    // Actual sleep time too long?
+    if (!addtime && timenow > wakeuptime+60) {
+      if (debugmode)
+        PrintOut(LOG_INFO, "Sleep time was %d seconds too long, assuming wakeup from standby mode.\n",
+          (int)(timenow-wakeuptime));
+      // Wait another 20 seconds to avoid I/O errors during disk spin-up
+      addtime = timenow-wakeuptime+20;
+      // Use next wake-up-time if close
+      int nextcheck = checktime - addtime % checktime;
+      if (nextcheck <= 20)
+        addtime += nextcheck;
+    }
   }
  
   // if we caught a SIGUSR1 then print message and clear signal
@@ -2999,8 +3529,8 @@ static time_t dosleep(time_t wakeuptime, bool & sigwakeup)
 }
 
 // Print out a list of valid arguments for the Directive d
-void printoutvaliddirectiveargs(int priority, char d) {
-
+static void printoutvaliddirectiveargs(int priority, char d)
+{
   switch (d) {
   case 'n':
     PrintOut(priority, "never[,N][,q], sleep[,N][,q], standby[,N][,q], idle[,N][,q]");
@@ -3032,18 +3562,21 @@ void printoutvaliddirectiveargs(int priority, char d) {
     break;
   case 'F':
     PrintOut(priority, "none, samsung, samsung2, samsung3");
+  case 'e':
+    PrintOut(priority, "aam,[N|off], apm,[N|off], lookahead,[on|off], "
+                       "security-freeze, standby,[N|off], wcache,[on|off]");
     break;
   }
 }
 
 // exits with an error message, or returns integer value of token
-int GetInteger(const char *arg, const char *name, const char *token, int lineno, const char *configfile,
+static int GetInteger(const char *arg, const char *name, const char *token, int lineno, const char *cfgfile,
                int min, int max, char * suffix = 0)
 {
   // make sure argument is there
   if (!arg) {
     PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s takes integer argument from %d to %d.\n",
-             configfile, lineno, name, token, min, max);
+             cfgfile, lineno, name, token, min, max);
     return -1;
   }
   
@@ -3061,7 +3594,7 @@ int GetInteger(const char *arg, const char *name, const char *token, int lineno,
 
   if (!(!*endptr && min <= val && val <= max)) {
     PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s has argument: %s; needs integer from %d to %d.\n",
-             configfile, lineno, name, token, arg, min, max);
+             cfgfile, lineno, name, token, arg, min, max);
     return -1;
   }
 
@@ -3071,13 +3604,14 @@ int GetInteger(const char *arg, const char *name, const char *token, int lineno,
 
 
 // Get 1-3 small integer(s) for '-W' directive
-int Get3Integers(const char *arg, const char *name, const char *token, int lineno, const char *configfile,
-                 unsigned char * val1, unsigned char * val2, unsigned char * val3){
+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)
+{
   unsigned v1 = 0, v2 = 0, v3 = 0;
   int n1 = -1, n2 = -1, n3 = -1, len;
   if (!arg) {
     PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s takes 1-3 integer argument(s) from 0 to 255.\n",
-             configfile, lineno, name, token);
+             cfgfile, lineno, name, token);
     return -1;
   }
 
@@ -3085,7 +3619,7 @@ int Get3Integers(const char *arg, const char *name, const char *token, int linen
   if (!(   sscanf(arg, "%u%n,%u%n,%u%n", &v1, &n1, &v2, &n2, &v3, &n3) >= 1
         && (n1 == len || n2 == len || n3 == len) && v1 <= 255 && v2 <= 255 && v3 <= 255)) {
     PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s has argument: %s; needs 1-3 integer(s) from 0 to 255.\n",
-             configfile, lineno, name, token, arg);
+             cfgfile, lineno, name, token, arg);
     return -1;
   }
   *val1 = (unsigned char)v1; *val2 = (unsigned char)v2; *val3 = (unsigned char)v3;
@@ -3164,6 +3698,8 @@ static int ParseToken(char * token, dev_config & cfg)
       missingarg = 1;
     } else if (!strcmp(arg, "removable")) {
       cfg.removable = true;
+    } else if (!strcmp(arg, "auto")) {
+      cfg.dev_type = "";
     } else {
       cfg.dev_type = arg;
     }
@@ -3215,6 +3751,32 @@ static int ParseToken(char * token, dev_config & cfg)
     } else if (!strcmp(arg, "error")) {
       // track changes in ATA error log
       cfg.errorlog = true;
+    } else if (!strcmp(arg, "xerror")) {
+      // track changes in Extended Comprehensive SMART error log
+      cfg.xerrorlog = true;
+    } else if (!strcmp(arg, "offlinests")) {
+      // track changes in offline data collection status
+      cfg.offlinests = true;
+    } else if (!strcmp(arg, "offlinests,ns")) {
+      // track changes in offline data collection status, disable auto standby
+      cfg.offlinests = cfg.offlinests_ns = true;
+    } else if (!strcmp(arg, "selfteststs")) {
+      // track changes in self-test execution status
+      cfg.selfteststs = true;
+    } else if (!strcmp(arg, "selfteststs,ns")) {
+      // track changes in self-test execution status, disable auto standby
+      cfg.selfteststs = cfg.selfteststs_ns = true;
+    } else if (!strncmp(arg, "scterc,", sizeof("scterc,")-1)) {
+        // set SCT Error Recovery Control
+        unsigned rt = ~0, wt = ~0; int nc = -1;
+        sscanf(arg,"scterc,%u,%u%n", &rt, &wt, &nc);
+        if (nc == (int)strlen(arg) && rt <= 999 && wt <= 999) {
+          cfg.sct_erc_set = true;
+          cfg.sct_erc_readtime = rt;
+          cfg.sct_erc_writetime = wt;
+        }
+        else
+          badarg = 1;
     } else {
       badarg = 1;
     }
@@ -3227,6 +3789,7 @@ static int ParseToken(char * token, dev_config & cfg)
     cfg.usage = true;
     cfg.selftest = true;
     cfg.errorlog = true;
+    cfg.selfteststs = true;
     break;
   case 'o':
     // automatic offline testing enable/disable
@@ -3400,7 +3963,7 @@ static int ParseToken(char * token, dev_config & cfg)
     // non-default vendor-specific attribute meaning
     if (!(arg=strtok(NULL,delim))) {
       missingarg = 1;
-    } else if (parse_attribute_def(arg, cfg.attributedefs)) {
+    } else if (!parse_attribute_def(arg, cfg.attribute_defs, PRIOR_USER)) {
       badarg = 1;
     }
     break;
@@ -3420,6 +3983,73 @@ static int ParseToken(char * token, dev_config & cfg)
       badarg = 1;
     }
     break;
+
+  case 'e':
+    // Various ATA settings
+    if (!(arg = strtok(NULL, delim))) {
+      missingarg = true;
+    }
+    else {
+      char arg2[16+1]; unsigned val;
+      int n1 = -1, n2 = -1, n3 = -1, len = strlen(arg);
+      if (sscanf(arg, "%16[^,=]%n%*[,=]%n%u%n", arg2, &n1, &n2, &val, &n3) >= 1
+          && (n1 == len || n2 > 0)) {
+        bool on  = (n2 > 0 && !strcmp(arg+n2, "on"));
+        bool off = (n2 > 0 && !strcmp(arg+n2, "off"));
+        if (n3 != len)
+          val = ~0U;
+
+        if (!strcmp(arg2, "aam")) {
+          if (off)
+            cfg.set_aam = -1;
+          else if (val <= 254)
+            cfg.set_aam = val + 1;
+          else
+            badarg = true;
+        }
+        else if (!strcmp(arg2, "apm")) {
+          if (off)
+            cfg.set_apm = -1;
+          else if (1 <= val && val <= 254)
+            cfg.set_apm = val + 1;
+          else
+            badarg = true;
+        }
+        else if (!strcmp(arg2, "lookahead")) {
+          if (off)
+            cfg.set_lookahead = -1;
+          else if (on)
+            cfg.set_lookahead = 1;
+          else
+            badarg = true;
+        }
+        else if (!strcmp(arg, "security-freeze")) {
+          cfg.set_security_freeze = true;
+        }
+        else if (!strcmp(arg2, "standby")) {
+          if (off)
+            cfg.set_standby = 0 + 1;
+          else if (val <= 255)
+            cfg.set_standby = val + 1;
+          else
+            badarg = true;
+        }
+        else if (!strcmp(arg2, "wcache")) {
+          if (off)
+            cfg.set_wcache = -1;
+          else if (on)
+            cfg.set_wcache = 1;
+          else
+            badarg = true;
+        }
+        else
+          badarg = true;
+      }
+      else
+        badarg = true;
+    }
+    break;
+
   default:
     // Directive not recognized
     PrintOut(LOG_CRIT,"File %s line %d (drive %s): unknown Directive: %s\n",
@@ -3452,67 +4082,62 @@ static int ParseToken(char * token, dev_config & cfg)
 //
 // Return values are:
 //  1: parsed a normal line
-//  0: found comment or blank line
+//  0: found DEFAULT setting or comment or blank line
 // -1: found SCANDIRECTIVE line
 // -2: found an error
 //
 // Note: this routine modifies *line from the caller!
-static int ParseConfigLine(dev_config_vector & conf_entries, int entry, int lineno, /*const*/ char * line)
+static int ParseConfigLine(dev_config_vector & conf_entries, dev_config & default_conf, int lineno, /*const*/ char * line)
 {
-  char *token=NULL;
-  char *name=NULL;
   const char *delim = " \n\t";
-  int devscan=0;
 
   // get first token: device name. If a comment, skip line
-  if (!(name=strtok(line,delim)) || *name=='#') {
+  const char * name = strtok(line, delim);
+  if (!name || *name == '#')
     return 0;
-  }
 
-  // Have we detected the SCANDIRECTIVE directive?
-  if (!strcmp(SCANDIRECTIVE,name)){
-    devscan=1;
-    if (entry) {
-      PrintOut(LOG_INFO,"Scan Directive %s (line %d) must be the first entry in %s\n",name, lineno, configfile);
-      return -2;
-    }
+  // Check device name for DEFAULT or DEVICESCAN
+  int retval;
+  if (!strcmp("DEFAULT", name)) {
+    retval = 0;
+    // Restart with empty defaults
+    default_conf = dev_config();
   }
-  
-  // We've got a legit entry, make space to store it
-  conf_entries.push_back( dev_config() );
-  dev_config & cfg = conf_entries.back();
-
-  cfg.name = name;
+  else {
+    retval = (!strcmp(SCANDIRECTIVE, name) ? -1 : 1);
+    // Init new entry with current defaults
+    conf_entries.push_back(default_conf);
+  }
+  dev_config & cfg = (retval ? conf_entries.back() : default_conf);
 
-  // Store line number, and by default check for both device types.
-  cfg.lineno=lineno;
+  cfg.name = name; // Later replaced by dev->get_info().info_name
+  cfg.dev_name = name; // If DEVICESCAN later replaced by get->dev_info().dev_name
+  cfg.lineno = lineno;
 
   // parse tokens one at a time from the file.
-  while ((token=strtok(NULL,delim))){
-    int retval=ParseToken(token,cfg);
-    
-    if (retval==0)
-      // No tokens left:
-      break;
-    
-    if (retval>0) {
-      // Parsed token  
-#if (0)
-      PrintOut(LOG_INFO,"Parsed token %s\n",token);
-#endif
-      continue;
-    }
-    
-    if (retval<0) {
+  while (char * token = strtok(0, delim)) {
+    int rc = ParseToken(token, cfg);
+    if (rc < 0)
       // error found on the line
       return -2;
-    }
+
+    if (rc == 0)
+      // No tokens left
+      break;
+
+    // PrintOut(LOG_INFO,"Parsed token %s\n",token);
   }
-  
+
+  // Don't perform checks below for DEFAULT entries
+  if (retval == 0)
+    return retval;
+
   // If NO monitoring directives are set, then set all of them.
-  if (!(cfg.smartcheck || cfg.usagefailed || cfg.prefail  ||
-        cfg.usage      || cfg.selftest    || cfg.errorlog ||
-       cfg.tempdiff   || cfg.tempinfo    || cfg.tempcrit   )) {
+  if (!(   cfg.smartcheck  || cfg.selftest
+        || cfg.errorlog    || cfg.xerrorlog
+        || cfg.offlinests  || cfg.selfteststs
+        || cfg.usagefailed || cfg.prefail  || cfg.usage
+        || cfg.tempdiff    || cfg.tempinfo || cfg.tempcrit)) {
     
     PrintOut(LOG_INFO,"Drive: %s, implied '-a' Directive on line %d of file %s\n",
              cfg.name.c_str(), cfg.lineno, configfile);
@@ -3523,6 +4148,7 @@ static int ParseConfigLine(dev_config_vector & conf_entries, int entry, int line
     cfg.usage = true;
     cfg.selftest = true;
     cfg.errorlog = true;
+    cfg.selfteststs = true;
   }
   
   // additional sanity check. Has user set -M options without -m?
@@ -3544,14 +4170,7 @@ static int ParseConfigLine(dev_config_vector & conf_entries, int entry, int line
     cfg.emailaddress.clear();
   }
 
-  // set cfg.emailfreq to 1 (once) if user hasn't set it
-  if ((!cfg.emailaddress.empty() || !cfg.emailcmdline.empty()) && !cfg.emailfreq)
-    cfg.emailfreq = 1;
-
-  if (devscan)
-    return -1;
-  else
-    return 1;
+  return retval;
 }
 
 // Parses a configuration file.  Return values are:
@@ -3564,7 +4183,7 @@ static int ParseConfigLine(dev_config_vector & conf_entries, int entry, int line
 // possiblities:
 // Empty configuration file ==> conf_entries.empty()
 // No configuration file    ==> conf_entries[0].lineno == 0
-// SCANDIRECTIVE found      ==> conf_entries[0].lineno != 0
+// SCANDIRECTIVE found      ==> conf_entries.back().lineno != 0 (size >= 1)
 static int ParseConfigFile(dev_config_vector & conf_entries)
 {
   // maximum line length in configuration file
@@ -3586,12 +4205,15 @@ static int ParseConfigFile(dev_config_vector & conf_entries)
   else // read from stdin ('-c -' option)
     f.open(stdin);
 
+  // Start with empty defaults
+  dev_config default_conf;
+
   // No configuration file found -- use fake one
   int entry = 0;
   if (!f) {
     char fakeconfig[] = SCANDIRECTIVE" -a"; // TODO: Remove this hack, build cfg_entry.
 
-    if (ParseConfigLine(conf_entries, entry, 0, fakeconfig) != -1)
+    if (ParseConfigLine(conf_entries, default_conf, 0, fakeconfig) != -1)
       throw std::logic_error("Internal error parsing "SCANDIRECTIVE);
     return 0;
   }
@@ -3623,7 +4245,7 @@ static int ParseConfigFile(dev_config_vector & conf_entries)
     // are we at the end of the file?
     if (!code){
       if (cont) {
-        scandevice = ParseConfigLine(conf_entries, entry, contlineno, fullline);
+        scandevice = ParseConfigLine(conf_entries, default_conf, contlineno, fullline);
         // See if we found a SCANDIRECTIVE directive
         if (scandevice==-1)
           return 0;
@@ -3677,7 +4299,7 @@ static int ParseConfigFile(dev_config_vector & conf_entries)
     }
 
     // Not a continuation line. Parse it
-    scandevice = ParseConfigLine(conf_entries, entry, contlineno, fullline);
+    scandevice = ParseConfigLine(conf_entries, default_conf, contlineno, fullline);
 
     // did we find a scandevice directive?
     if (scandevice==-1)
@@ -3697,7 +4319,8 @@ static int ParseConfigFile(dev_config_vector & conf_entries)
 
 /* Prints the message "=======> VALID ARGUMENTS ARE: <LIST>  <=======\n", where
    <LIST> is the list of valid arguments for option opt. */
-void PrintValidArgs(char opt) {
+static void PrintValidArgs(char opt)
+{
   const char *s;
 
   PrintOut(LOG_CRIT, "=======> VALID ARGUMENTS ARE: ");
@@ -3708,31 +4331,39 @@ void PrintValidArgs(char opt) {
   PrintOut(LOG_CRIT, " <=======\n");
 }
 
-// Return true if absolute path name
-static bool is_abs_path(const char * path)
+#ifndef _WIN32
+// Report error and exit if specified path is not absolute.
+static void check_abs_path(char option, const std::string & path)
 {
-  if (*path == '/')
-    return true;
-#if defined(_WIN32) || defined(__CYGWIN__)
-  if (*path == '\\')
-    return true;
-  int n = -1;
-  sscanf(path, "%*1[A-Za-z]:%*1[/\\]%n", &n);
-  if (n > 0)
-    return true;
-#endif
-  return false;
+  if (path.empty() || path[0] == '/')
+    return;
+
+  debugmode = 1;
+  PrintHead();
+  PrintOut(LOG_CRIT, "=======> INVALID ARGUMENT TO -%c: %s <=======\n\n", option, path.c_str());
+  PrintOut(LOG_CRIT, "Error: relative path names are not allowed\n\n");
+  EXIT(EXIT_BADCMD);
 }
+#endif // !_WIN32
 
 // Parses input line, prints usage message and
 // version/license/copyright messages
-void ParseOpts(int argc, char **argv){
-  int optchar;
-  char *tailptr;
-  long lchecktime;
+static void ParseOpts(int argc, char **argv)
+{
+  // Init default configfile path
+#ifndef _WIN32
+  configfile = SMARTMONTOOLS_SYSCONFDIR"/smartd.conf";
+#else
+  static std::string configfile_str = get_exe_dir() + "/smartd.conf";
+  configfile = configfile_str.c_str();
+#endif
+
   // Please update GetValidArgList() if you edit shortopts
-  const char *shortopts = "c:l:q:dDni:p:r:s:A:B:Vh?";
-  char *arg;
+  static const char shortopts[] = "c:l:q:dDni:p:r:s:A:B:Vh?"
+#ifdef HAVE_LIBCAP_NG
+                                                          "C"
+#endif
+                                                             ;
   // Please update GetValidArgList() if you edit longopts
   struct option longopts[] = {
     { "configfile",     required_argument, 0, 'c' },
@@ -3743,20 +4374,22 @@ void ParseOpts(int argc, char **argv){
     { "interval",       required_argument, 0, 'i' },
 #ifndef _WIN32
     { "no-fork",        no_argument,       0, 'n' },
+#else
+    { "service",        no_argument,       0, 'n' },
 #endif
     { "pidfile",        required_argument, 0, 'p' },
     { "report",         required_argument, 0, 'r' },
     { "savestates",     required_argument, 0, 's' },
     { "attributelog",   required_argument, 0, 'A' },
     { "drivedb",        required_argument, 0, 'B' },
-#if defined(_WIN32) || defined(__CYGWIN__)
-    { "service",        no_argument,       0, 'n' },
-#endif
     { "version",        no_argument,       0, 'V' },
     { "license",        no_argument,       0, 'V' },
     { "copyright",      no_argument,       0, 'V' },
     { "help",           no_argument,       0, 'h' },
     { "usage",          no_argument,       0, 'h' },
+#ifdef HAVE_LIBCAP_NG
+    { "capabilities",   no_argument,       0, 'C' },
+#endif
     { 0,                0,                 0, 0   }
   };
 
@@ -3764,12 +4397,13 @@ void ParseOpts(int argc, char **argv){
   bool badarg = false;
   bool no_defaultdb = false; // set true on '-B FILE'
 
-  // Parse input options.  This horrible construction is so that emacs
-  // indents properly.  Sorry.
-  while (-1 != (optchar = 
-                getopt_long(argc, argv, shortopts, longopts, NULL)
-                )) {
-    
+  // Parse input options.
+  int optchar;
+  while ((optchar = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
+    char *arg;
+    char *tailptr;
+    long lchecktime;
+
     switch(optchar) {
     case 'q':
       // when to quit
@@ -3866,11 +4500,11 @@ void ParseOpts(int argc, char **argv){
           PrintOut(LOG_CRIT, "======> LEVEL MUST BE INTEGER BETWEEN 1 AND 3<=======\n");
           EXIT(EXIT_BADCMD);
         } else if (!strcmp(s,"ioctl")) {
-          con->reportataioctl  = con->reportscsiioctl = i;
+          ata_debugmode = scsi_debugmode = i;
         } else if (!strcmp(s,"ataioctl")) {
-          con->reportataioctl = i;
+          ata_debugmode = i;
         } else if (!strcmp(s,"scsiioctl")) {
-          con->reportscsiioctl = i;
+          scsi_debugmode = i;
         } else {
           badarg = true;
         }
@@ -3915,6 +4549,12 @@ void ParseOpts(int argc, char **argv){
       PrintOut(LOG_INFO, "%s", format_version_info("smartd", true /*full*/).c_str());
       EXIT(0);
       break;
+#ifdef HAVE_LIBCAP_NG
+    case 'C':
+      // enable capabilities
+      enable_capabilities = true;
+      break;
+#endif
     case 'h':
       // help: print summary of command-line options
       debugmode=1;
@@ -3988,25 +4628,14 @@ void ParseOpts(int argc, char **argv){
     EXIT(EXIT_BADCMD);
   }
 
-  // absolute path is required due to chdir('/') after fork().
-  if (!state_path_prefix.empty() && !debugmode && !is_abs_path(state_path_prefix.c_str())) {
-    debugmode=1;
-    PrintHead();
-    PrintOut(LOG_CRIT, "=======> INVALID CHOICE OF OPTIONS: -s <======= \n\n");
-    PrintOut(LOG_CRIT, "Error: relative path %s is only allowed in debug (-d) mode\n\n",
-      state_path_prefix.c_str());
-    EXIT(EXIT_BADCMD);
-  }
-
-  // absolute path is required due to chdir('/') after fork().
-  if (!attrlog_path_prefix.empty() && !debugmode && !is_abs_path(attrlog_path_prefix.c_str())) {
-    debugmode=1;
-    PrintHead();
-    PrintOut(LOG_CRIT, "=======> INVALID CHOICE OF OPTIONS: -s <======= \n\n");
-    PrintOut(LOG_CRIT, "Error: relative path %s is only allowed in debug (-d) mode\n\n",
-      attrlog_path_prefix.c_str());
-    EXIT(EXIT_BADCMD);
+#ifndef _WIN32
+  if (!debugmode) {
+    // absolute path names are required due to chdir('/') after fork().
+    check_abs_path('p', pid_file);
+    check_abs_path('s', state_path_prefix);
+    check_abs_path('A', attrlog_path_prefix);
   }
+#endif
 
   // Read or init drive database
   if (!no_defaultdb) {
@@ -4035,6 +4664,10 @@ static int MakeConfigEntries(const dev_config & base_cfg,
   if (devlist.size() <= 0)
     return 0;
 
+  // add empty device slots for existing config entries
+  while (scanned_devs.size() < conf_entries.size())
+    scanned_devs.push_back((smart_device *)0);
+
   // loop over entries to create
   for (unsigned i = 0; i < devlist.size(); i++) {
     // Move device pointer
@@ -4045,6 +4678,7 @@ static int MakeConfigEntries(const dev_config & base_cfg,
     conf_entries.push_back(base_cfg);
     dev_config & cfg = conf_entries.back();
     cfg.name = dev->get_info().info_name;
+    cfg.dev_name = dev->get_info().dev_name;
     cfg.dev_type = type;
   }
   
@@ -4067,8 +4701,6 @@ static void CanNotRegister(const char *name, const char *type, int line, bool sc
 
 // Returns negative value (see ParseConfigFile()) if config file
 // had errors, else number of entries which may be zero or positive. 
-// If we found no configuration file, or it contained SCANDIRECTIVE,
-// then *scanning is set to 1, else 0.
 static int ReadOrMakeConfigEntries(dev_config_vector & conf_entries, smart_device_list & scanned_devs)
 {
   // parse configuration file configfile (normally /etc/smartd.conf)  
@@ -4087,12 +4719,12 @@ static int ReadOrMakeConfigEntries(dev_config_vector & conf_entries, smart_devic
     // we did not find a SCANDIRECTIVE and did find valid entries
     PrintOut(LOG_INFO, "Configuration file %s parsed.\n", configfile);
   }
-  else if (conf_entries.size() == 1) {
+  else if (!conf_entries.empty()) {
     // we found a SCANDIRECTIVE or there was no configuration file so
-    // scan.  Configuration file's first entry contains all options
+    // scan.  Configuration file's last entry contains all options
     // that were set
-    dev_config first = conf_entries.front();
-    conf_entries.clear();
+    dev_config first = conf_entries.back();
+    conf_entries.pop_back();
 
     if (first.lineno)
       PrintOut(LOG_INFO,"Configuration file %s was parsed, found %s, scanning devices\n", configfile, SCANDIRECTIVE);
@@ -4130,8 +4762,7 @@ static void RegisterDevices(const dev_config_vector & conf_entries, smart_device
     dev_config cfg = conf_entries[i];
 
     // get device of appropriate type
-    // TODO: exception handling
-    smart_device * dev = 0;
+    smart_device_auto_ptr dev;
     bool scanning = false;
 
     // Device may already be detected during devicescan
@@ -4156,10 +4787,10 @@ static void RegisterDevices(const dev_config_vector & conf_entries, smart_device
     smart_device::device_info oldinfo = dev->get_info();
 
     // Open with autodetect support, may return 'better' device
-    dev = dev->autodetect_open();
+    dev.replace( dev->autodetect_open() );
 
     // Report if type has changed
-    if (/* ent->dev_type && */ oldinfo.dev_type != dev->get_dev_type())
+    if (oldinfo.dev_type != dev->get_dev_type())
       PrintOut(LOG_INFO,"Device: %s, type changed from '%s' to '%s'\n",
         cfg.name.c_str(), oldinfo.dev_type.c_str(), dev->get_dev_type());
 
@@ -4169,7 +4800,6 @@ static void RegisterDevices(const dev_config_vector & conf_entries, smart_device
       // If no debug and scanning - don't print errors
       if (debugmode || !scanning)
         PrintOut(LOG_INFO, "Device: %s, open() failed: %s\n", dev->get_info_name(), dev->get_errmsg());
-      delete dev;
       continue;
     }
 
@@ -4184,33 +4814,30 @@ static void RegisterDevices(const dev_config_vector & conf_entries, smart_device
     if (dev->is_ata()){
       if (ATADeviceScan(cfg, state, dev->to_ata())) {
         CanNotRegister(cfg.name.c_str(), "ATA", cfg.lineno, scanning);
-        delete dev; dev = 0;
-      }
-      else {
-        // move onto the list of ata devices
-        configs.push_back(cfg);
-        states.push_back(state);
-        devices.push_back(dev);
+        dev.reset();
       }
     }
-    
     // or register SCSI devices
     else if (dev->is_scsi()){
       if (SCSIDeviceScan(cfg, state, dev->to_scsi())) {
         CanNotRegister(cfg.name.c_str(), "SCSI", cfg.lineno, scanning);
-        delete dev; dev = 0;
-      }
-      else {
-        // move onto the list of scsi devices
-        configs.push_back(cfg);
-        states.push_back(state);
-        devices.push_back(dev);
+        dev.reset();
       }
     }
-    
+    else {
+      PrintOut(LOG_INFO, "Device: %s, neither ATA nor SCSI device\n", cfg.name.c_str());
+      dev.reset();
+    }
+
+    if (dev) {
+      // move onto the list of devices
+      configs.push_back(cfg);
+      states.push_back(state);
+      devices.push_back(dev);
+    }
     // if device is explictly listed and we can't register it, then
     // exit unless the user has specified that the device is removable
-    if (!dev && !scanning) {
+    else if (!scanning) {
       if (cfg.removable || quit==2)
         PrintOut(LOG_INFO, "Device %s not available\n", cfg.name.c_str());
       else {
@@ -4219,37 +4846,28 @@ static void RegisterDevices(const dev_config_vector & conf_entries, smart_device
       }
     }
   }
+
+  init_disable_standby_check(configs);
 }
 
 
 // Main program without exception handling
-int main_worker(int argc, char **argv)
+static int main_worker(int argc, char **argv)
 {
   // Initialize interface
   smart_interface::init();
   if (!smi())
     return 1;
 
-  // external control variables for ATA disks
-  smartmonctrl control;
-
   // is it our first pass through?
   bool firstpass = true;
 
   // next time to wake up
-  time_t wakeuptime;
-
-  // for simplicity, null all global communications variables/lists
-  con=&control;
-  memset(con,        0,sizeof(control));
+  time_t wakeuptime = 0;
 
   // parse input and print header and usage info if needed
   ParseOpts(argc,argv);
   
-  // do we mute printing from ataprint commands?
-  con->printing_switchable = false;
-  con->dont_print = !debugmode;
-  
   // Configuration for each device
   dev_config_vector configs;
   // Device states
@@ -4259,6 +4877,16 @@ int main_worker(int argc, char **argv)
 
   bool write_states_always = true;
 
+#ifdef HAVE_LIBCAP_NG
+  // Drop capabilities
+  if (enable_capabilities) {
+    capng_clear(CAPNG_SELECT_BOTH);
+    capng_updatev(CAPNG_ADD, (capng_type_t)(CAPNG_EFFECTIVE|CAPNG_PERMITTED),
+                  CAP_SYS_ADMIN, CAP_MKNOD, CAP_SYS_RAWIO, -1);
+    capng_apply(CAPNG_SELECT_BOTH);
+  }
+#endif
+
   // the main loop of the code
   for (;;) {
 
@@ -4285,19 +4913,6 @@ int main_worker(int argc, char **argv)
     // Should we (re)read the config file?
     if (firstpass || caughtsigHUP){
       if (!firstpass) {
-#ifdef __CYGWIN__
-        // Workaround for missing SIGQUIT via keyboard on Cygwin
-        if (caughtsigHUP==2) {
-          // Simulate SIGQUIT if another SIGINT arrives soon
-          caughtsigHUP=0;
-          sleep(1);
-          if (caughtsigHUP==2) {
-            caughtsigEXIT=SIGQUIT;
-            continue;
-          }
-          caughtsigHUP=2;
-        }
-#endif
         // Write state files
         if (!state_path_prefix.empty())
           write_all_dev_states(configs, states);
@@ -4328,8 +4943,7 @@ int main_worker(int argc, char **argv)
         }
         else {
           // exit with configuration file error status
-          int status = (entries==-3 ? EXIT_READCONF : entries==-2 ? EXIT_NOCONF : EXIT_BADCONF);
-          EXIT(status);
+          return (entries==-3 ? EXIT_READCONF : entries==-2 ? EXIT_NOCONF : EXIT_BADCONF);
         }
       }
 
@@ -4345,15 +4959,26 @@ int main_worker(int argc, char **argv)
       }
       else {
         PrintOut(LOG_INFO,"Unable to monitor any SMART enabled devices. Try debug (-d) option. Exiting...\n");
-        EXIT(EXIT_NODEV);
+        return EXIT_NODEV;
       }
 
       if (quit==4) {
         // user has asked to print test schedule
         PrintTestSchedule(configs, states, devices);
-        EXIT(0);
+        return 0;
       }
-      
+
+#ifdef HAVE_LIBCAP_NG
+      if (enable_capabilities) {
+        for (unsigned i = 0; i < configs.size(); i++) {
+          if (!configs[i].emailaddress.empty() || !configs[i].emailcmdline.empty()) {
+            PrintOut(LOG_WARNING, "Mail can't be enabled together with --capabilities. All mail will be suppressed.\n");
+            break;
+          }
+        }
+      }
+#endif
+
       // reset signal
       caughtsigHUP=0;
 
@@ -4363,7 +4988,7 @@ int main_worker(int argc, char **argv)
 
     // check all devices once,
     // self tests are not started in first pass unless '-q onecheck' is specified
-    CheckDevicesOnce(configs, states, devices, (!firstpass || quit==3));
+    CheckDevicesOnce(configs, states, devices, firstpass, (!firstpass || quit==3));
 
      // Write state files
     if (!state_path_prefix.empty())
@@ -4378,7 +5003,7 @@ int main_worker(int argc, char **argv)
     if (quit==3) {
       PrintOut(LOG_INFO,"Started with '-q onecheck' option. All devices sucessfully checked once.\n"
                "smartd is exiting (exit status 0)\n");
-      EXIT(0);
+      return 0;
     }
     
     // fork into background if needed