2 * Home page of code is: http://www.smartmontools.org
4 * Copyright (C) 2002-11 Bruce Allen
5 * Copyright (C) 2008-16 Christian Franke
6 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
7 * Copyright (C) 2008 Oliver Bock <brevilo@users.sourceforge.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * You should have received a copy of the GNU General Public License
15 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
17 * This code was originally developed as a Senior Thesis by Michael Cornwell
18 * at the Concurrent Systems Laboratory (now part of the Storage Systems
19 * Research Center), Jack Baskin School of Engineering, University of
20 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
27 // unconditionally included files
29 #include <sys/types.h>
30 #include <sys/stat.h> // umask
45 #include <algorithm> // std::replace()
47 // conditionally included files
57 #pragma warning(disable:4761) // "conversion supplied"
58 typedef unsigned short mode_t
;
61 #include <io.h> // umask()
62 #include <process.h> // getpid()
66 #include <io.h> // setmode()
73 // locally included files
75 #include "dev_interface.h"
76 #include "knowndrives.h"
81 // This is for solaris, where signal() resets the handler to SIG_DFL
82 // after the first signal is caught.
84 #define SIGNALFN sigset
86 #define SIGNALFN signal
90 // fork()/signal()/initd simulation for native Windows
91 #include "daemon_win32.h" // daemon_main/detach/signal()
93 #define SIGNALFN daemon_signal
94 #define strsignal daemon_strsignal
95 #define sleep daemon_sleep
96 // SIGQUIT does not exist, CONTROL-Break signals SIGBREAK.
97 #define SIGQUIT SIGBREAK
98 #define SIGQUIT_KEYNAME "CONTROL-Break"
100 #define SIGQUIT_KEYNAME "CONTROL-\\"
103 const char * smartd_cpp_cvsid
= "$Id: smartd.cpp 4308 2016-04-24 13:36:10Z chrfranke $"
106 using namespace smartmontools
;
109 #define EXIT_BADCMD 1 // command line did not parse
110 #define EXIT_BADCONF 2 // syntax error in config file
111 #define EXIT_STARTUP 3 // problem forking daemon
112 #define EXIT_PID 4 // problem creating pid file
113 #define EXIT_NOCONF 5 // config file does not exist
114 #define EXIT_READCONF 6 // config file exists but cannot be read
116 #define EXIT_NOMEM 8 // out of memory
117 #define EXIT_BADCODE 10 // internal error - should NEVER happen
119 #define EXIT_BADDEV 16 // we can't monitor this device
120 #define EXIT_NODEV 17 // no devices to monitor
122 #define EXIT_SIGNAL 254 // abort on signal
125 // command-line: 1=debug mode, 2=print presets
126 static unsigned char debugmode
= 0;
128 // command-line: how long to sleep between checks
129 #define CHECKTIME 1800
130 static int checktime
=CHECKTIME
;
132 // command-line: name of PID file (empty for no pid file)
133 static std::string pid_file
;
135 // command-line: path prefix of persistent state file, empty if no persistence.
136 static std::string state_path_prefix
137 #ifdef SMARTMONTOOLS_SAVESTATES
138 = SMARTMONTOOLS_SAVESTATES
142 // command-line: path prefix of attribute log file, empty if no logs.
143 static std::string attrlog_path_prefix
144 #ifdef SMARTMONTOOLS_ATTRIBUTELOG
145 = SMARTMONTOOLS_ATTRIBUTELOG
149 // configuration file name
150 static const char * configfile
;
151 // configuration file "name" if read from stdin
152 static const char * const configfile_stdin
= "<stdin>";
153 // path of alternate configuration file
154 static std::string configfile_alt
;
156 // warning script file
157 static std::string warning_script
;
159 // command-line: when should we exit?
162 // command-line; this is the default syslog(3) log facility to use.
163 static int facility
=LOG_DAEMON
;
166 // command-line: fork into background?
167 static bool do_fork
=true;
170 #ifdef HAVE_LIBCAP_NG
171 // command-line: enable capabilities?
172 static bool enable_capabilities
= false;
175 // TODO: This smartctl only variable is also used in os_win32.cpp
176 unsigned char failuretest_permissive
= 0;
178 // set to one if we catch a USR1 (check devices now)
179 static volatile int caughtsigUSR1
=0;
182 // set to one if we catch a USR2 (toggle debug mode)
183 static volatile int caughtsigUSR2
=0;
186 // set to one if we catch a HUP (reload config file). In debug mode,
187 // set to two, if we catch INT (also reload config file).
188 static volatile int caughtsigHUP
=0;
190 // set to signal value if we catch INT, QUIT, or TERM
191 static volatile int caughtsigEXIT
=0;
193 // This function prints either to stdout or to the syslog as needed.
194 static void PrintOut(int priority
, const char *fmt
, ...)
195 __attribute_format_printf(2, 3);
197 // Attribute monitoring flags.
198 // See monitor_attr_flags below.
200 MONITOR_IGN_FAILUSE
= 0x01,
201 MONITOR_IGNORE
= 0x02,
202 MONITOR_RAW_PRINT
= 0x04,
204 MONITOR_AS_CRIT
= 0x10,
205 MONITOR_RAW_AS_CRIT
= 0x20,
208 // Array of flags for each attribute.
209 class attribute_flags
213 { memset(m_flags
, 0, sizeof(m_flags
)); }
215 bool is_set(int id
, unsigned char flag
) const
216 { return (0 < id
&& id
< (int)sizeof(m_flags
) && (m_flags
[id
] & flag
)); }
218 void set(int id
, unsigned char flags
)
220 if (0 < id
&& id
< (int)sizeof(m_flags
))
221 m_flags
[id
] |= flags
;
225 unsigned char m_flags
[256];
229 /// Configuration data for a device. Read from smartd.conf.
230 /// Supports copy & assignment and is compatible with STL containers.
233 int lineno
; // Line number of entry in file
234 std::string name
; // Device name (with optional extra info)
235 std::string dev_name
; // Device name (plain, for SMARTD_DEVICE variable)
236 std::string dev_type
; // Device type argument from -d directive, empty if none
237 std::string dev_idinfo
; // Device identify info for warning emails
238 std::string state_file
; // Path of the persistent state file, empty if none
239 std::string attrlog_file
; // Path of the persistent attrlog file, empty if none
240 bool ignore
; // Ignore this entry
241 bool smartcheck
; // Check SMART status
242 bool usagefailed
; // Check for failed Usage Attributes
243 bool prefail
; // Track changes in Prefail Attributes
244 bool usage
; // Track changes in Usage Attributes
245 bool selftest
; // Monitor number of selftest errors
246 bool errorlog
; // Monitor number of ATA errors
247 bool xerrorlog
; // Monitor number of ATA errors (Extended Comprehensive error log)
248 bool offlinests
; // Monitor changes in offline data collection status
249 bool offlinests_ns
; // Disable auto standby if in progress
250 bool selfteststs
; // Monitor changes in self-test execution status
251 bool selfteststs_ns
; // Disable auto standby if in progress
252 bool permissive
; // Ignore failed SMART commands
253 char autosave
; // 1=disable, 2=enable Autosave Attributes
254 char autoofflinetest
; // 1=disable, 2=enable Auto Offline Test
255 firmwarebug_defs firmwarebugs
; // -F directives from drivedb or smartd.conf
256 bool ignorepresets
; // Ignore database of -v options
257 bool showpresets
; // Show database entry for this device
258 bool removable
; // Device may disappear (not be present)
259 char powermode
; // skip check, if disk in idle or standby mode
260 bool powerquiet
; // skip powermode 'skipping checks' message
261 int powerskipmax
; // how many times can be check skipped
262 unsigned char tempdiff
; // Track Temperature changes >= this limit
263 unsigned char tempinfo
, tempcrit
; // Track Temperatures >= these limits as LOG_INFO, LOG_CRIT+mail
264 regular_expression test_regex
; // Regex for scheduled testing
266 // Configuration of email warning messages
267 std::string emailcmdline
; // script to execute, empty if no messages
268 std::string emailaddress
; // email address, or empty
269 unsigned char emailfreq
; // Emails once (1) daily (2) diminishing (3)
270 bool emailtest
; // Send test email?
273 int dev_rpm
; // rotation rate, 0 = unknown, 1 = SSD, >1 = HDD
274 int set_aam
; // disable(-1), enable(1..255->0..254) Automatic Acoustic Management
275 int set_apm
; // disable(-1), enable(2..255->1..254) Advanced Power Management
276 int set_lookahead
; // disable(-1), enable(1) read look-ahead
277 int set_standby
; // set(1..255->0..254) standby timer
278 bool set_security_freeze
; // Freeze ATA security
279 int set_wcache
; // disable(-1), enable(1) write cache
281 bool sct_erc_set
; // set SCT ERC to:
282 unsigned short sct_erc_readtime
; // ERC read time (deciseconds)
283 unsigned short sct_erc_writetime
; // ERC write time (deciseconds)
285 unsigned char curr_pending_id
; // ID of current pending sector count, 0 if none
286 unsigned char offl_pending_id
; // ID of offline uncorrectable sector count, 0 if none
287 bool curr_pending_incr
, offl_pending_incr
; // True if current/offline pending values increase
288 bool curr_pending_set
, offl_pending_set
; // True if '-C', '-U' set in smartd.conf
290 attribute_flags monitor_attr_flags
; // MONITOR_* flags for each attribute
292 ata_vendor_attr_defs attribute_defs
; // -v options
297 dev_config::dev_config()
307 offlinests(false), offlinests_ns(false),
308 selfteststs(false), selfteststs_ns(false),
312 ignorepresets(false),
319 tempinfo(0), tempcrit(0),
323 set_aam(0), set_apm(0),
326 set_security_freeze(false),
329 sct_erc_readtime(0), sct_erc_writetime(0),
330 curr_pending_id(0), offl_pending_id(0),
331 curr_pending_incr(false), offl_pending_incr(false),
332 curr_pending_set(false), offl_pending_set(false)
337 // Number of allowed mail message types
338 static const int SMARTD_NMAIL
= 13;
339 // Type for '-M test' mails (state not persistent)
340 static const int MAILTYPE_TEST
= 0;
341 // TODO: Add const or enum for all mail types.
344 int logged
;// number of times an email has been sent
345 time_t firstsent
;// time first email was sent, as defined by time(2)
346 time_t lastsent
; // time last email was sent, as defined by time(2)
349 : logged(0), firstsent(0), lastsent(0) { }
352 /// Persistent state data for a device.
353 struct persistent_dev_state
355 unsigned char tempmin
, tempmax
; // Min/Max Temperatures
357 unsigned char selflogcount
; // total number of self-test errors
358 unsigned short selfloghour
; // lifetime hours of last self-test error
360 time_t scheduled_test_next_check
; // Time of next check for scheduled self-tests
362 uint64_t selective_test_last_start
; // Start LBA of last scheduled selective self-test
363 uint64_t selective_test_last_end
; // End LBA of last scheduled selective self-test
365 mailinfo maillog
[SMARTD_NMAIL
]; // log info on when mail sent
368 int ataerrorcount
; // Total number of ATA errors
370 // Persistent part of ata_smart_values:
371 struct ata_attribute
{
374 unsigned char worst
; // Byte needed for 'raw64' attribute only.
378 ata_attribute() : id(0), val(0), worst(0), raw(0), resvd(0) { }
380 ata_attribute ata_attributes
[NUMBER_ATA_SMART_ATTRIBUTES
];
384 struct scsi_error_counter_t
{
385 struct scsiErrorCounter errCounter
;
387 scsi_error_counter_t() : found(0)
388 { memset(&errCounter
, 0, sizeof(errCounter
)); }
390 scsi_error_counter_t scsi_error_counters
[3];
392 struct scsi_nonmedium_error_t
{
393 struct scsiNonMediumError nme
;
395 scsi_nonmedium_error_t() : found(0)
396 { memset(&nme
, 0, sizeof(nme
)); }
398 scsi_nonmedium_error_t scsi_nonmedium_error
;
401 uint64_t nvme_err_log_entries
;
403 persistent_dev_state();
406 persistent_dev_state::persistent_dev_state()
407 : tempmin(0), tempmax(0),
410 scheduled_test_next_check(0),
411 selective_test_last_start(0),
412 selective_test_last_end(0),
414 nvme_err_log_entries(0)
418 /// Non-persistent state data for a device.
419 struct temp_dev_state
421 bool must_write
; // true if persistent part should be written
423 bool not_cap_offline
; // true == not capable of offline testing
424 bool not_cap_conveyance
;
427 bool not_cap_selective
;
429 unsigned char temperature
; // last recorded Temperature (in Celsius)
430 time_t tempmin_delay
; // time where Min Temperature tracking will start
432 bool powermodefail
; // true if power mode check failed
433 int powerskipcnt
; // Number of checks skipped due to idle or standby mode
434 int lastpowermodeskipped
; // the last power mode that was skipped
437 unsigned char SmartPageSupported
; // has log sense IE page (0x2f)
438 unsigned char TempPageSupported
; // has log sense temperature page (0xd)
439 unsigned char ReadECounterPageSupported
;
440 unsigned char WriteECounterPageSupported
;
441 unsigned char VerifyECounterPageSupported
;
442 unsigned char NonMediumErrorPageSupported
;
443 unsigned char SuppressReport
; // minimize nuisance reports
444 unsigned char modese_len
; // mode sense/select cmd len: 0 (don't
447 uint64_t num_sectors
; // Number of sectors
448 ata_smart_values smartval
; // SMART data
449 ata_smart_thresholds_pvt smartthres
; // SMART thresholds
450 bool offline_started
; // true if offline data collection was started
451 bool selftest_started
; // true if self-test was started
456 temp_dev_state::temp_dev_state()
458 not_cap_offline(false),
459 not_cap_conveyance(false),
460 not_cap_short(false),
462 not_cap_selective(false),
465 powermodefail(false),
467 lastpowermodeskipped(0),
468 SmartPageSupported(false),
469 TempPageSupported(false),
470 ReadECounterPageSupported(false),
471 WriteECounterPageSupported(false),
472 VerifyECounterPageSupported(false),
473 NonMediumErrorPageSupported(false),
474 SuppressReport(false),
477 offline_started(false),
478 selftest_started(false)
480 memset(&smartval
, 0, sizeof(smartval
));
481 memset(&smartthres
, 0, sizeof(smartthres
));
484 /// Runtime state data for a device.
486 : public persistent_dev_state
,
487 public temp_dev_state
489 void update_persistent_state();
490 void update_temp_state();
493 /// Container for configuration info for each device.
494 typedef std::vector
<dev_config
> dev_config_vector
;
496 /// Container for state info for each device.
497 typedef std::vector
<dev_state
> dev_state_vector
;
499 // Copy ATA attributes to persistent state.
500 void dev_state::update_persistent_state()
502 for (int i
= 0; i
< NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
503 const ata_smart_attribute
& ta
= smartval
.vendor_attributes
[i
];
504 ata_attribute
& pa
= ata_attributes
[i
];
507 pa
.val
= pa
.worst
= 0; pa
.raw
= 0;
515 | ((uint64_t)ta
.raw
[3] << 24)
516 | ((uint64_t)ta
.raw
[4] << 32)
517 | ((uint64_t)ta
.raw
[5] << 40);
518 pa
.resvd
= ta
.reserv
;
522 // Copy ATA from persistent to temp state.
523 void dev_state::update_temp_state()
525 for (int i
= 0; i
< NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
526 const ata_attribute
& pa
= ata_attributes
[i
];
527 ata_smart_attribute
& ta
= smartval
.vendor_attributes
[i
];
530 ta
.current
= ta
.worst
= 0;
531 memset(ta
.raw
, 0, sizeof(ta
.raw
));
536 ta
.raw
[0] = (unsigned char) pa
.raw
;
537 ta
.raw
[1] = (unsigned char)(pa
.raw
>> 8);
538 ta
.raw
[2] = (unsigned char)(pa
.raw
>> 16);
539 ta
.raw
[3] = (unsigned char)(pa
.raw
>> 24);
540 ta
.raw
[4] = (unsigned char)(pa
.raw
>> 32);
541 ta
.raw
[5] = (unsigned char)(pa
.raw
>> 40);
542 ta
.reserv
= pa
.resvd
;
546 // Parse a line from a state file.
547 static bool parse_dev_state_line(const char * line
, persistent_dev_state
& state
)
549 static const regular_expression
regex(
551 "((temperature-min)" // (1 (2)
552 "|(temperature-max)" // (3)
553 "|(self-test-errors)" // (4)
554 "|(self-test-last-err-hour)" // (5)
555 "|(scheduled-test-next-check)" // (6)
556 "|(selective-test-last-start)" // (7)
557 "|(selective-test-last-end)" // (8)
558 "|(ata-error-count)" // (9)
559 "|(mail\\.([0-9]+)\\." // (10 (11)
560 "((count)" // (12 (13)
561 "|(first-sent-time)" // (14)
562 "|(last-sent-time)" // (15)
565 "|(ata-smart-attribute\\.([0-9]+)\\." // (16 (17)
573 "|(nvme-err-log-entries)" // (24)
575 " *= *([0-9]+)[ \n]*$", // (25)
579 const int nmatch
= 1+25;
580 regmatch_t match
[nmatch
];
581 if (!regex
.execute(line
, nmatch
, match
))
583 if (match
[nmatch
-1].rm_so
< 0)
586 uint64_t val
= strtoull(line
+ match
[nmatch
-1].rm_so
, (char **)0, 10);
589 if (match
[++m
].rm_so
>= 0)
590 state
.tempmin
= (unsigned char)val
;
591 else if (match
[++m
].rm_so
>= 0)
592 state
.tempmax
= (unsigned char)val
;
593 else if (match
[++m
].rm_so
>= 0)
594 state
.selflogcount
= (unsigned char)val
;
595 else if (match
[++m
].rm_so
>= 0)
596 state
.selfloghour
= (unsigned short)val
;
597 else if (match
[++m
].rm_so
>= 0)
598 state
.scheduled_test_next_check
= (time_t)val
;
599 else if (match
[++m
].rm_so
>= 0)
600 state
.selective_test_last_start
= val
;
601 else if (match
[++m
].rm_so
>= 0)
602 state
.selective_test_last_end
= val
;
603 else if (match
[++m
].rm_so
>= 0)
604 state
.ataerrorcount
= (int)val
;
605 else if (match
[m
+=2].rm_so
>= 0) {
606 int i
= atoi(line
+match
[m
].rm_so
);
607 if (!(0 <= i
&& i
< SMARTD_NMAIL
))
609 if (i
== MAILTYPE_TEST
) // Don't suppress test mails
611 if (match
[m
+=2].rm_so
>= 0)
612 state
.maillog
[i
].logged
= (int)val
;
613 else if (match
[++m
].rm_so
>= 0)
614 state
.maillog
[i
].firstsent
= (time_t)val
;
615 else if (match
[++m
].rm_so
>= 0)
616 state
.maillog
[i
].lastsent
= (time_t)val
;
620 else if (match
[m
+=5+1].rm_so
>= 0) {
621 int i
= atoi(line
+match
[m
].rm_so
);
622 if (!(0 <= i
&& i
< NUMBER_ATA_SMART_ATTRIBUTES
))
624 if (match
[m
+=2].rm_so
>= 0)
625 state
.ata_attributes
[i
].id
= (unsigned char)val
;
626 else if (match
[++m
].rm_so
>= 0)
627 state
.ata_attributes
[i
].val
= (unsigned char)val
;
628 else if (match
[++m
].rm_so
>= 0)
629 state
.ata_attributes
[i
].worst
= (unsigned char)val
;
630 else if (match
[++m
].rm_so
>= 0)
631 state
.ata_attributes
[i
].raw
= val
;
632 else if (match
[++m
].rm_so
>= 0)
633 state
.ata_attributes
[i
].resvd
= (unsigned char)val
;
637 else if (match
[m
+7].rm_so
>= 0)
638 state
.nvme_err_log_entries
= val
;
644 // Read a state file.
645 static bool read_dev_state(const char * path
, persistent_dev_state
& state
)
647 stdio_file
f(path
, "r");
650 pout("Cannot read state file \"%s\"\n", path
);
654 setmode(fileno(f
), O_TEXT
); // Allow files with \r\n
657 persistent_dev_state new_state
;
658 int good
= 0, bad
= 0;
660 while (fgets(line
, sizeof(line
), f
)) {
661 const char * s
= line
+ strspn(line
, " \t");
662 if (!*s
|| *s
== '#')
664 if (!parse_dev_state_line(line
, new_state
))
672 pout("%s: format error\n", path
);
675 pout("%s: %d invalid line(s) ignored\n", path
, bad
);
678 // This sets the values missing in the file to 0.
683 static void write_dev_state_line(FILE * f
, const char * name
, uint64_t val
)
686 fprintf(f
, "%s = %" PRIu64
"\n", name
, val
);
689 static void write_dev_state_line(FILE * f
, const char * name1
, int id
, const char * name2
, uint64_t val
)
692 fprintf(f
, "%s.%d.%s = %" PRIu64
"\n", name1
, id
, name2
, val
);
695 // Write a state file
696 static bool write_dev_state(const char * path
, const persistent_dev_state
& state
)
698 // Rename old "file" to "file~"
699 std::string pathbak
= path
; pathbak
+= '~';
700 unlink(pathbak
.c_str());
701 rename(path
, pathbak
.c_str());
703 stdio_file
f(path
, "w");
705 pout("Cannot create state file \"%s\"\n", path
);
709 fprintf(f
, "# smartd state file\n");
710 write_dev_state_line(f
, "temperature-min", state
.tempmin
);
711 write_dev_state_line(f
, "temperature-max", state
.tempmax
);
712 write_dev_state_line(f
, "self-test-errors", state
.selflogcount
);
713 write_dev_state_line(f
, "self-test-last-err-hour", state
.selfloghour
);
714 write_dev_state_line(f
, "scheduled-test-next-check", state
.scheduled_test_next_check
);
715 write_dev_state_line(f
, "selective-test-last-start", state
.selective_test_last_start
);
716 write_dev_state_line(f
, "selective-test-last-end", state
.selective_test_last_end
);
719 for (i
= 0; i
< SMARTD_NMAIL
; i
++) {
720 if (i
== MAILTYPE_TEST
) // Don't suppress test mails
722 const mailinfo
& mi
= state
.maillog
[i
];
725 write_dev_state_line(f
, "mail", i
, "count", mi
.logged
);
726 write_dev_state_line(f
, "mail", i
, "first-sent-time", mi
.firstsent
);
727 write_dev_state_line(f
, "mail", i
, "last-sent-time", mi
.lastsent
);
731 write_dev_state_line(f
, "ata-error-count", state
.ataerrorcount
);
733 for (i
= 0; i
< NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
734 const persistent_dev_state::ata_attribute
& pa
= state
.ata_attributes
[i
];
737 write_dev_state_line(f
, "ata-smart-attribute", i
, "id", pa
.id
);
738 write_dev_state_line(f
, "ata-smart-attribute", i
, "val", pa
.val
);
739 write_dev_state_line(f
, "ata-smart-attribute", i
, "worst", pa
.worst
);
740 write_dev_state_line(f
, "ata-smart-attribute", i
, "raw", pa
.raw
);
741 write_dev_state_line(f
, "ata-smart-attribute", i
, "resvd", pa
.resvd
);
745 write_dev_state_line(f
, "nvme-err-log-entries", state
.nvme_err_log_entries
);
750 // Write to the attrlog file
751 static bool write_dev_attrlog(const char * path
, const dev_state
& state
)
753 stdio_file
f(path
, "a");
755 pout("Cannot create attribute log file \"%s\"\n", path
);
760 time_t now
= time(0);
761 struct tm
* tms
= gmtime(&now
);
762 fprintf(f
, "%d-%02d-%02d %02d:%02d:%02d;",
763 1900+tms
->tm_year
, 1+tms
->tm_mon
, tms
->tm_mday
,
764 tms
->tm_hour
, tms
->tm_min
, tms
->tm_sec
);
766 for (int i
= 0; i
< NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
767 const persistent_dev_state::ata_attribute
& pa
= state
.ata_attributes
[i
];
770 fprintf(f
, "\t%d;%d;%" PRIu64
";", pa
.id
, pa
.val
, pa
.raw
);
773 const struct scsiErrorCounter
* ecp
;
774 const char * pageNames
[3] = {"read", "write", "verify"};
775 for (int k
= 0; k
< 3; ++k
) {
776 if ( !state
.scsi_error_counters
[k
].found
) continue;
777 ecp
= &state
.scsi_error_counters
[k
].errCounter
;
778 fprintf(f
, "\t%s-corr-by-ecc-fast;%" PRIu64
";"
779 "\t%s-corr-by-ecc-delayed;%" PRIu64
";"
780 "\t%s-corr-by-retry;%" PRIu64
";"
781 "\t%s-total-err-corrected;%" PRIu64
";"
782 "\t%s-corr-algorithm-invocations;%" PRIu64
";"
783 "\t%s-gb-processed;%.3f;"
784 "\t%s-total-unc-errors;%" PRIu64
";",
785 pageNames
[k
], ecp
->counter
[0],
786 pageNames
[k
], ecp
->counter
[1],
787 pageNames
[k
], ecp
->counter
[2],
788 pageNames
[k
], ecp
->counter
[3],
789 pageNames
[k
], ecp
->counter
[4],
790 pageNames
[k
], (ecp
->counter
[5] / 1000000000.0),
791 pageNames
[k
], ecp
->counter
[6]);
793 if(state
.scsi_nonmedium_error
.found
&& state
.scsi_nonmedium_error
.nme
.gotPC0
) {
794 fprintf(f
, "\tnon-medium-errors;%" PRIu64
";", state
.scsi_nonmedium_error
.nme
.counterPC0
);
796 // write SCSI current temperature if it is monitored
797 if(state
.TempPageSupported
&& state
.temperature
)
798 fprintf(f
, "\ttemperature;%d;", state
.temperature
);
804 // Write all state files. If write_always is false, don't write
805 // unless must_write is set.
806 static void write_all_dev_states(const dev_config_vector
& configs
,
807 dev_state_vector
& states
,
808 bool write_always
= true)
810 for (unsigned i
= 0; i
< states
.size(); i
++) {
811 const dev_config
& cfg
= configs
.at(i
);
812 if (cfg
.state_file
.empty())
814 dev_state
& state
= states
[i
];
815 if (!write_always
&& !state
.must_write
)
817 if (!write_dev_state(cfg
.state_file
.c_str(), state
))
819 state
.must_write
= false;
820 if (write_always
|| debugmode
)
821 PrintOut(LOG_INFO
, "Device: %s, state written to %s\n",
822 cfg
.name
.c_str(), cfg
.state_file
.c_str());
826 // Write to all attrlog files
827 static void write_all_dev_attrlogs(const dev_config_vector
& configs
,
828 dev_state_vector
& states
)
830 for (unsigned i
= 0; i
< states
.size(); i
++) {
831 const dev_config
& cfg
= configs
.at(i
);
832 if (cfg
.attrlog_file
.empty())
834 dev_state
& state
= states
[i
];
835 write_dev_attrlog(cfg
.attrlog_file
.c_str(), state
);
839 // remove the PID file
840 static void RemovePidFile()
842 if (!pid_file
.empty()) {
843 if (unlink(pid_file
.c_str()))
844 PrintOut(LOG_CRIT
,"Can't unlink PID file %s (%s).\n",
845 pid_file
.c_str(), strerror(errno
));
851 extern "C" { // signal handlers require C-linkage
853 // Note if we catch a SIGUSR1
854 static void USR1handler(int sig
)
862 // Note if we catch a SIGUSR2
863 static void USR2handler(int sig
)
871 // Note if we catch a HUP (or INT in debug mode)
872 static void HUPhandler(int sig
)
881 // signal handler for TERM, QUIT, and INT (if not in debug mode)
882 static void sighandler(int sig
)
891 // Cleanup, print Goodbye message and remove pidfile
892 static int Goodbye(int status
)
894 // delete PID file, if one was created
897 // and this should be the final output from smartd before it exits
898 PrintOut(status
?LOG_CRIT
:LOG_INFO
, "smartd is exiting (exit status %d)\n", status
);
903 // a replacement for setenv() which is not available on all platforms.
904 // Note that the string passed to putenv must not be freed or made
905 // invalid, since a pointer to it is kept by putenv(). This means that
906 // it must either be a static buffer or allocated off the heap. The
907 // string can be freed if the environment variable is redefined via
908 // another call to putenv(). There is no portable way to unset a variable
909 // with putenv(). So we manage the buffer in a static object.
910 // Using setenv() if available is not considered because some
911 // implementations may produce memory leaks.
917 : m_buf((char *)0) { }
919 void set(const char * name
, const char * value
);
924 env_buffer(const env_buffer
&);
925 void operator=(const env_buffer
&);
928 void env_buffer::set(const char * name
, const char * value
)
930 int size
= strlen(name
) + 1 + strlen(value
) + 1;
931 char * newbuf
= new char[size
];
932 snprintf(newbuf
, size
, "%s=%s", name
, value
);
935 throw std::runtime_error("putenv() failed");
937 // This assumes that the same NAME is passed on each call
944 static void MailWarning(const dev_config
& cfg
, dev_state
& state
, int which
, const char *fmt
, ...)
945 __attribute_format_printf(4, 5);
947 // If either address or executable path is non-null then send and log
948 // a warning email, or execute executable
949 static void MailWarning(const dev_config
& cfg
, dev_state
& state
, int which
, const char *fmt
, ...)
951 static const char * const whichfail
[] = {
957 "FailedHealthCheck", // 5
958 "FailedReadSmartData", // 6
959 "FailedReadSmartErrorLog", // 7
960 "FailedReadSmartSelfTestLog", // 8
961 "FailedOpenDevice", // 9
962 "CurrentPendingSector", // 10
963 "OfflineUncorrectableSector", // 11
967 // See if user wants us to send mail
968 if (cfg
.emailaddress
.empty() && cfg
.emailcmdline
.empty())
971 std::string address
= cfg
.emailaddress
;
972 const char * executable
= cfg
.emailcmdline
.c_str();
974 // which type of mail are we sending?
975 mailinfo
* mail
=(state
.maillog
)+which
;
978 if (cfg
.emailfreq
<1 || cfg
.emailfreq
>3) {
979 PrintOut(LOG_CRIT
,"internal error in MailWarning(): cfg.mailwarn->emailfreq=%d\n",cfg
.emailfreq
);
982 if (which
<0 || which
>=SMARTD_NMAIL
|| sizeof(whichfail
)!=SMARTD_NMAIL
*sizeof(char *)) {
983 PrintOut(LOG_CRIT
,"Contact " PACKAGE_BUGREPORT
"; internal error in MailWarning(): which=%d, size=%d\n",
984 which
, (int)sizeof(whichfail
));
988 // Return if a single warning mail has been sent.
989 if ((cfg
.emailfreq
==1) && mail
->logged
)
992 // Return if this is an email test and one has already been sent.
993 if (which
== 0 && mail
->logged
)
996 // To decide if to send mail, we need to know what time it is.
997 time_t epoch
= time(0);
999 // Return if less than one day has gone by
1000 const int day
= 24*3600;
1001 if (cfg
.emailfreq
==2 && mail
->logged
&& epoch
<(mail
->lastsent
+day
))
1004 // Return if less than 2^(logged-1) days have gone by
1005 if (cfg
.emailfreq
==3 && mail
->logged
) {
1006 int days
= 0x01 << (mail
->logged
- 1);
1008 if (epoch
<(mail
->lastsent
+days
))
1012 #ifdef HAVE_LIBCAP_NG
1013 if (enable_capabilities
) {
1014 PrintOut(LOG_ERR
, "Sending a mail was supressed. "
1015 "Mails can't be send when capabilites are enabled\n");
1020 // record the time of this mail message, and the first mail message
1022 mail
->firstsent
=epoch
;
1023 mail
->lastsent
=epoch
;
1025 // print warning string into message
1029 vsnprintf(message
, sizeof(message
), fmt
, ap
);
1032 // replace commas by spaces to separate recipients
1033 std::replace(address
.begin(), address
.end(), ',', ' ');
1035 // Export information in environment variables that will be useful
1037 static env_buffer env
[12];
1038 env
[0].set("SMARTD_MAILER", executable
);
1039 env
[1].set("SMARTD_MESSAGE", message
);
1040 char dates
[DATEANDEPOCHLEN
];
1041 snprintf(dates
, sizeof(dates
), "%d", mail
->logged
);
1042 env
[2].set("SMARTD_PREVCNT", dates
);
1043 dateandtimezoneepoch(dates
, mail
->firstsent
);
1044 env
[3].set("SMARTD_TFIRST", dates
);
1045 snprintf(dates
, DATEANDEPOCHLEN
,"%d", (int)mail
->firstsent
);
1046 env
[4].set("SMARTD_TFIRSTEPOCH", dates
);
1047 env
[5].set("SMARTD_FAILTYPE", whichfail
[which
]);
1048 env
[6].set("SMARTD_ADDRESS", address
.c_str());
1049 env
[7].set("SMARTD_DEVICESTRING", cfg
.name
.c_str());
1051 // Allow 'smartctl ... -d $SMARTD_DEVICETYPE $SMARTD_DEVICE'
1052 env
[8].set("SMARTD_DEVICETYPE",
1053 (!cfg
.dev_type
.empty() ? cfg
.dev_type
.c_str() : "auto"));
1054 env
[9].set("SMARTD_DEVICE", cfg
.dev_name
.c_str());
1056 env
[10].set("SMARTD_DEVICEINFO", cfg
.dev_idinfo
.c_str());
1058 if (which
) switch (cfg
.emailfreq
) {
1059 case 2: dates
[0] = '1'; dates
[1] = 0; break;
1060 case 3: snprintf(dates
, sizeof(dates
), "%d", (0x01)<<mail
->logged
);
1062 env
[11].set("SMARTD_NEXTDAYS", dates
);
1064 // now construct a command to send this as EMAIL
1066 executable
= "<mail>";
1067 const char * newadd
= (!address
.empty()? address
.c_str() : "<nomailer>");
1068 const char * newwarn
= (which
? "Warning via" : "Test of");
1072 snprintf(command
, sizeof(command
), "%s 2>&1", warning_script
.c_str());
1074 // tell SYSLOG what we are about to do...
1075 PrintOut(LOG_INFO
,"%s %s to %s ...\n",
1076 which
?"Sending warning via":"Executing test of", executable
, newadd
);
1078 // issue the command to send mail or to run the user's executable
1081 if (!(pfp
=popen(command
, "r")))
1082 // failed to popen() mail process
1083 PrintOut(LOG_CRIT
,"%s %s to %s: failed (fork or pipe failed, or no memory) %s\n",
1084 newwarn
, executable
, newadd
, errno
?strerror(errno
):"");
1088 char buffer
[EBUFLEN
];
1090 // if unexpected output on stdout/stderr, null terminate, print, and flush
1091 if ((len
=fread(buffer
, 1, EBUFLEN
, pfp
))) {
1093 int newlen
= len
<EBUFLEN
? len
: EBUFLEN
-1;
1094 buffer
[newlen
]='\0';
1095 PrintOut(LOG_CRIT
,"%s %s to %s produced unexpected output (%s%d bytes) to STDOUT/STDERR: \n%s\n",
1096 newwarn
, executable
, newadd
, len
!=newlen
?"here truncated to ":"", newlen
, buffer
);
1098 // flush pipe if needed
1099 while (fread(buffer
, 1, EBUFLEN
, pfp
) && count
<EBUFLEN
)
1102 // tell user that pipe was flushed, or that something is really wrong
1103 if (count
&& count
<EBUFLEN
)
1104 PrintOut(LOG_CRIT
,"%s %s to %s: flushed remaining STDOUT/STDERR\n",
1105 newwarn
, executable
, newadd
);
1107 PrintOut(LOG_CRIT
,"%s %s to %s: more than 1 MB STDOUT/STDERR flushed, breaking pipe\n",
1108 newwarn
, executable
, newadd
);
1111 // if something went wrong with mail process, print warning
1113 if (-1==(status
=pclose(pfp
)))
1114 PrintOut(LOG_CRIT
,"%s %s to %s: pclose(3) failed %s\n", newwarn
, executable
, newadd
,
1115 errno
?strerror(errno
):"");
1117 // mail process apparently succeeded. Check and report exit status
1118 if (WIFEXITED(status
)) {
1119 // exited 'normally' (but perhaps with nonzero status)
1120 int status8
= WEXITSTATUS(status
);
1122 PrintOut(LOG_CRIT
,"%s %s to %s: failed (32-bit/8-bit exit status: %d/%d) perhaps caught signal %d [%s]\n",
1123 newwarn
, executable
, newadd
, status
, status8
, status8
-128, strsignal(status8
-128));
1125 PrintOut(LOG_CRIT
,"%s %s to %s: failed (32-bit/8-bit exit status: %d/%d)\n",
1126 newwarn
, executable
, newadd
, status
, status8
);
1128 PrintOut(LOG_INFO
,"%s %s to %s: successful\n", newwarn
, executable
, newadd
);
1131 if (WIFSIGNALED(status
))
1132 PrintOut(LOG_INFO
,"%s %s to %s: exited because of uncaught signal %d [%s]\n",
1133 newwarn
, executable
, newadd
, WTERMSIG(status
), strsignal(WTERMSIG(status
)));
1135 // this branch is probably not possible. If subprocess is
1136 // stopped then pclose() should not return.
1137 if (WIFSTOPPED(status
))
1138 PrintOut(LOG_CRIT
,"%s %s to %s: process STOPPED because it caught signal %d [%s]\n",
1139 newwarn
, executable
, newadd
, WSTOPSIG(status
), strsignal(WSTOPSIG(status
)));
1147 snprintf(command
, sizeof(command
), "cmd /c \"%s\"", warning_script
.c_str());
1149 char stdoutbuf
[800]; // < buffer in syslog_win32::vsyslog()
1152 PrintOut(LOG_INFO
,"%s %s to %s ...\n",
1153 (which
?"Sending warning via":"Executing test of"), executable
, newadd
);
1154 rc
= daemon_spawn(command
, "", 0, stdoutbuf
, sizeof(stdoutbuf
));
1155 if (rc
>= 0 && stdoutbuf
[0])
1156 PrintOut(LOG_CRIT
,"%s %s to %s produced unexpected output (%d bytes) to STDOUT/STDERR:\n%s\n",
1157 newwarn
, executable
, newadd
, (int)strlen(stdoutbuf
), stdoutbuf
);
1159 PrintOut(LOG_CRIT
,"%s %s to %s: failed, exit status %d\n",
1160 newwarn
, executable
, newadd
, rc
);
1162 PrintOut(LOG_INFO
,"%s %s to %s: successful\n", newwarn
, executable
, newadd
);
1167 // increment mail sent counter
1171 static void reset_warning_mail(const dev_config
& cfg
, dev_state
& state
, int which
, const char *fmt
, ...)
1172 __attribute_format_printf(4, 5);
1174 static void reset_warning_mail(const dev_config
& cfg
, dev_state
& state
, int which
, const char *fmt
, ...)
1176 if (!(0 <= which
&& which
< SMARTD_NMAIL
))
1179 // Return if no mail sent yet
1180 mailinfo
& mi
= state
.maillog
[which
];
1184 // Format & print message
1188 vsnprintf(msg
, sizeof(msg
), fmt
, ap
);
1191 PrintOut(LOG_INFO
, "Device: %s, %s, warning condition reset after %d email%s\n", cfg
.name
.c_str(),
1192 msg
, mi
.logged
, (mi
.logged
==1 ? "" : "s"));
1194 // Clear mail counter and timestamps
1196 state
.must_write
= true;
1201 // Output multiple lines via separate syslog(3) calls.
1202 static void vsyslog_lines(int priority
, const char * fmt
, va_list ap
)
1204 char buf
[512+EBUFLEN
]; // enough space for exec cmd output in MailWarning()
1205 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1207 for (char * p
= buf
, * q
; p
&& *p
; p
= q
) {
1208 if ((q
= strchr(p
, '\n')))
1211 syslog(priority
, "%s\n", p
);
1216 // os_win32/syslog_win32.cpp supports multiple lines.
1217 #define vsyslog_lines vsyslog
1220 // Printing function for watching ataprint commands, or losing them
1221 // [From GLIBC Manual: Since the prototype doesn't specify types for
1222 // optional arguments, in a call to a variadic function the default
1223 // argument promotions are performed on the optional argument
1224 // values. This means the objects of type char or short int (whether
1225 // signed or not) are promoted to either int or unsigned int, as
1227 void pout(const char *fmt
, ...){
1230 // get the correct time in syslog()
1231 FixGlibcTimeZoneBug();
1232 // initialize variable argument list
1234 // in debugmode==1 mode we will print the output from the ataprint.o functions!
1235 if (debugmode
&& debugmode
!= 2) {
1238 if (facility
== LOG_LOCAL1
) // logging to stdout
1241 vfprintf(f
, fmt
, ap
);
1244 // in debugmode==2 mode we print output from knowndrives.o functions
1245 else if (debugmode
==2 || ata_debugmode
|| scsi_debugmode
) {
1246 openlog("smartd", LOG_PID
, facility
);
1247 vsyslog_lines(LOG_INFO
, fmt
, ap
);
1254 // This function prints either to stdout or to the syslog as needed.
1255 static void PrintOut(int priority
, const char *fmt
, ...){
1258 // get the correct time in syslog()
1259 FixGlibcTimeZoneBug();
1260 // initialize variable argument list
1265 if (facility
== LOG_LOCAL1
) // logging to stdout
1268 vfprintf(f
, fmt
, ap
);
1272 openlog("smartd", LOG_PID
, facility
);
1273 vsyslog_lines(priority
, fmt
, ap
);
1280 // Used to warn users about invalid checksums. Called from atacmds.cpp.
1281 void checksumwarning(const char * string
)
1283 pout("Warning! %s error: invalid SMART checksum.\n", string
);
1288 // Wait for the pid file to show up, this makes sure a calling program knows
1289 // that the daemon is really up and running and has a pid to kill it
1290 static bool WaitForPidFile()
1292 int waited
, max_wait
= 10;
1293 struct stat stat_buf
;
1295 if (pid_file
.empty() || debugmode
)
1298 for(waited
= 0; waited
< max_wait
; ++waited
) {
1299 if (!stat(pid_file
.c_str(), &stat_buf
)) {
1309 // Forks new process, closes ALL file descriptors, redirects stdin,
1310 // stdout, and stderr. Not quite daemon(). See
1311 // http://www.linuxjournal.com/article/2335
1312 // for a good description of why we do things this way.
1313 static void DaemonInit()
1319 // flush all buffered streams. Else we might get two copies of open
1320 // streams since both parent and child get copies of the buffers.
1324 if ((pid
=fork()) < 0) {
1326 PrintOut(LOG_CRIT
,"smartd unable to fork daemon process!\n");
1330 // we are the parent process, wait for pid file, then exit cleanly
1331 if(!WaitForPidFile()) {
1332 PrintOut(LOG_CRIT
,"PID file %s didn't show up!\n", pid_file
.c_str());
1338 // from here on, we are the child process.
1341 // Fork one more time to avoid any possibility of having terminals
1342 if ((pid
=fork()) < 0) {
1344 PrintOut(LOG_CRIT
,"smartd unable to fork daemon process!\n");
1348 // we are the parent process -- exit cleanly
1351 // Now we are the child's child...
1354 // close any open file descriptors
1355 for (i
=getdtablesize();i
>=0;--i
)
1358 #define NO_warn_unused_result(cmd) { if (cmd) {} ; }
1360 // redirect any IO attempts to /dev/null for stdin
1361 i
=open("/dev/null",O_RDWR
);
1364 NO_warn_unused_result(dup(i
));
1366 NO_warn_unused_result(dup(i
));
1369 NO_warn_unused_result(chdir("/"));
1372 PrintOut(LOG_INFO
, "smartd has fork()ed into background mode. New PID=%d.\n", (int)getpid());
1376 // No fork() on native Win32
1377 // Detach this process from console
1379 if (daemon_detach("smartd")) {
1380 PrintOut(LOG_CRIT
,"smartd unable to detach from console!\n");
1383 // stdin/out/err now closed if not redirected
1389 // create a PID file containing the current process id
1390 static void WritePidFile()
1392 if (!pid_file
.empty()) {
1393 pid_t pid
= getpid();
1396 old_umask
= umask(0077); // rwx------
1398 // Cygwin: smartd service runs on system account, ensure PID file can be read by admins
1399 old_umask
= umask(0033); // rwxr--r--
1402 stdio_file
f(pid_file
.c_str(), "w");
1404 if (!(f
&& fprintf(f
, "%d\n", (int)pid
) > 0 && f
.close())) {
1405 PrintOut(LOG_CRIT
, "unable to write PID file %s - exiting.\n", pid_file
.c_str());
1408 PrintOut(LOG_INFO
, "file %s written containing PID %d\n", pid_file
.c_str(), (int)pid
);
1412 // Prints header identifying version of code and home
1413 static void PrintHead()
1415 PrintOut(LOG_INFO
, "%s\n", format_version_info("smartd").c_str());
1418 // prints help info for configuration file Directives
1419 static void Directives()
1422 "Configuration file (%s) Directives (after device name):\n"
1423 " -d TYPE Set the device type: auto, ignore, removable,\n"
1425 " -T TYPE Set the tolerance to one of: normal, permissive\n"
1426 " -o VAL Enable/disable automatic offline tests (on/off)\n"
1427 " -S VAL Enable/disable attribute autosave (on/off)\n"
1428 " -n MODE No check if: never, sleep[,N][,q], standby[,N][,q], idle[,N][,q]\n"
1429 " -H Monitor SMART Health Status, report if failed\n"
1430 " -s REG Do Self-Test at time(s) given by regular expression REG\n"
1431 " -l TYPE Monitor SMART log or self-test status:\n"
1432 " error, selftest, xerror, offlinests[,ns], selfteststs[,ns]\n"
1433 " -l scterc,R,W Set SCT Error Recovery Control\n"
1434 " -e Change device setting: aam,[N|off], apm,[N|off], lookahead,[on|off],\n"
1435 " security-freeze, standby,[N|off], wcache,[on|off]\n"
1436 " -f Monitor 'Usage' Attributes, report failures\n"
1437 " -m ADD Send email warning to address ADD\n"
1438 " -M TYPE Modify email warning behavior (see man page)\n"
1439 " -p Report changes in 'Prefailure' Attributes\n"
1440 " -u Report changes in 'Usage' Attributes\n"
1441 " -t Equivalent to -p and -u Directives\n"
1442 " -r ID Also report Raw values of Attribute ID with -p, -u or -t\n"
1443 " -R ID Track changes in Attribute ID Raw value with -p, -u or -t\n"
1444 " -i ID Ignore Attribute ID for -f Directive\n"
1445 " -I ID Ignore Attribute ID for -p, -u or -t Directive\n"
1446 " -C ID[+] Monitor [increases of] Current Pending Sectors in Attribute ID\n"
1447 " -U ID[+] Monitor [increases of] Offline Uncorrectable Sectors in Attribute ID\n"
1448 " -W D,I,C Monitor Temperature D)ifference, I)nformal limit, C)ritical limit\n"
1449 " -v N,ST Modifies labeling of Attribute N (see man page) \n"
1450 " -P TYPE Drive-specific presets: use, ignore, show, showall\n"
1451 " -a Default: -H -f -t -l error -l selftest -l selfteststs -C 197 -U 198\n"
1452 " -F TYPE Use firmware bug workaround:\n"
1454 " # Comment: text after a hash sign is ignored\n"
1455 " \\ Line continuation character\n"
1456 "Attribute ID is a decimal integer 1 <= ID <= 255\n"
1457 "Use ID = 0 to turn off -C and/or -U Directives\n"
1458 "Example: /dev/sda -a\n",
1460 smi()->get_valid_dev_types_str().c_str(),
1461 get_valid_firmwarebug_args());
1464 /* Returns a pointer to a static string containing a formatted list of the valid
1465 arguments to the option opt or NULL on failure. */
1466 static const char *GetValidArgList(char opt
)
1471 return "<PATH_PREFIX>";
1473 return "<FILE_NAME>, -";
1475 return "daemon, local0, local1, local2, local3, local4, local5, local6, local7";
1477 return "nodev, errors, nodevstartup, never, onecheck, showtests";
1479 return "ioctl[,N], ataioctl[,N], scsiioctl[,N], nvmeioctl[,N]";
1483 return "<FILE_NAME>";
1485 return "<INTEGER_SECONDS>";
1491 /* prints help information for command syntax */
1494 PrintOut(LOG_INFO
,"Usage: smartd [options]\n\n");
1495 PrintOut(LOG_INFO
," -A PREFIX, --attributelog=PREFIX\n");
1496 PrintOut(LOG_INFO
," Log ATA attribute information to {PREFIX}MODEL-SERIAL.ata.csv\n");
1497 #ifdef SMARTMONTOOLS_ATTRIBUTELOG
1498 PrintOut(LOG_INFO
," [default is " SMARTMONTOOLS_ATTRIBUTELOG
"MODEL-SERIAL.ata.csv]\n");
1500 PrintOut(LOG_INFO
,"\n");
1501 PrintOut(LOG_INFO
," -B [+]FILE, --drivedb=[+]FILE\n");
1502 PrintOut(LOG_INFO
," Read and replace [add] drive database from FILE\n");
1503 PrintOut(LOG_INFO
," [default is +%s", get_drivedb_path_add());
1504 #ifdef SMARTMONTOOLS_DRIVEDBDIR
1505 PrintOut(LOG_INFO
,"\n");
1506 PrintOut(LOG_INFO
," and then %s", get_drivedb_path_default());
1508 PrintOut(LOG_INFO
,"]\n\n");
1509 PrintOut(LOG_INFO
," -c NAME|-, --configfile=NAME|-\n");
1510 PrintOut(LOG_INFO
," Read configuration file NAME or stdin\n");
1511 PrintOut(LOG_INFO
," [default is %s]\n\n", configfile
);
1512 #ifdef HAVE_LIBCAP_NG
1513 PrintOut(LOG_INFO
," -C, --capabilities\n");
1514 PrintOut(LOG_INFO
," Drop unneeded Linux process capabilities.\n"
1515 " Warning: Mail notification does not work when used.\n\n");
1517 PrintOut(LOG_INFO
," -d, --debug\n");
1518 PrintOut(LOG_INFO
," Start smartd in debug mode\n\n");
1519 PrintOut(LOG_INFO
," -D, --showdirectives\n");
1520 PrintOut(LOG_INFO
," Print the configuration file Directives and exit\n\n");
1521 PrintOut(LOG_INFO
," -h, --help, --usage\n");
1522 PrintOut(LOG_INFO
," Display this help and exit\n\n");
1523 PrintOut(LOG_INFO
," -i N, --interval=N\n");
1524 PrintOut(LOG_INFO
," Set interval between disk checks to N seconds, where N >= 10\n\n");
1525 PrintOut(LOG_INFO
," -l local[0-7], --logfacility=local[0-7]\n");
1527 PrintOut(LOG_INFO
," Use syslog facility local0 - local7 or daemon [default]\n\n");
1529 PrintOut(LOG_INFO
," Log to \"./smartd.log\", stdout, stderr [default is event log]\n\n");
1532 PrintOut(LOG_INFO
," -n, --no-fork\n");
1533 PrintOut(LOG_INFO
," Do not fork into background\n\n");
1535 PrintOut(LOG_INFO
," -p NAME, --pidfile=NAME\n");
1536 PrintOut(LOG_INFO
," Write PID file NAME\n\n");
1537 PrintOut(LOG_INFO
," -q WHEN, --quit=WHEN\n");
1538 PrintOut(LOG_INFO
," Quit on one of: %s\n\n", GetValidArgList('q'));
1539 PrintOut(LOG_INFO
," -r, --report=TYPE\n");
1540 PrintOut(LOG_INFO
," Report transactions for one of: %s\n\n", GetValidArgList('r'));
1541 PrintOut(LOG_INFO
," -s PREFIX, --savestates=PREFIX\n");
1542 PrintOut(LOG_INFO
," Save disk states to {PREFIX}MODEL-SERIAL.TYPE.state\n");
1543 #ifdef SMARTMONTOOLS_SAVESTATES
1544 PrintOut(LOG_INFO
," [default is " SMARTMONTOOLS_SAVESTATES
"MODEL-SERIAL.TYPE.state]\n");
1546 PrintOut(LOG_INFO
,"\n");
1547 PrintOut(LOG_INFO
," -w NAME, --warnexec=NAME\n");
1548 PrintOut(LOG_INFO
," Run executable NAME on warnings\n");
1550 PrintOut(LOG_INFO
," [default is " SMARTMONTOOLS_SMARTDSCRIPTDIR
"/smartd_warning.sh]\n\n");
1552 PrintOut(LOG_INFO
," [default is %s/smartd_warning.cmd]\n\n", get_exe_dir().c_str());
1555 PrintOut(LOG_INFO
," --service\n");
1556 PrintOut(LOG_INFO
," Running as windows service (see man page), install with:\n");
1557 PrintOut(LOG_INFO
," smartd install [options]\n");
1558 PrintOut(LOG_INFO
," Remove service with:\n");
1559 PrintOut(LOG_INFO
," smartd remove\n\n");
1561 PrintOut(LOG_INFO
," -V, --version, --license, --copyright\n");
1562 PrintOut(LOG_INFO
," Print License, Copyright, and version information\n");
1565 static int CloseDevice(smart_device
* device
, const char * name
)
1567 if (!device
->close()){
1568 PrintOut(LOG_INFO
,"Device: %s, %s, close() failed\n", name
, device
->get_errmsg());
1571 // device sucessfully closed
1575 // return true if a char is not allowed in a state file name
1576 static bool not_allowed_in_filename(char c
)
1578 return !( ('0' <= c
&& c
<= '9')
1579 || ('A' <= c
&& c
<= 'Z')
1580 || ('a' <= c
&& c
<= 'z'));
1583 // Read error count from Summary or Extended Comprehensive SMART error log
1584 // Return -1 on error
1585 static int read_ata_error_count(ata_device
* device
, const char * name
,
1586 firmwarebug_defs firmwarebugs
, bool extended
)
1589 ata_smart_errorlog log
;
1590 if (ataReadErrorLog(device
, &log
, firmwarebugs
)){
1591 PrintOut(LOG_INFO
,"Device: %s, Read Summary SMART Error Log failed\n",name
);
1594 return (log
.error_log_pointer
? log
.ata_error_count
: 0);
1597 ata_smart_exterrlog logx
;
1598 if (!ataReadExtErrorLog(device
, &logx
, 0, 1 /*first sector only*/, firmwarebugs
)) {
1599 PrintOut(LOG_INFO
,"Device: %s, Read Extended Comprehensive SMART Error Log failed\n",name
);
1602 // Some disks use the reserved byte as index, see ataprint.cpp.
1603 return (logx
.error_log_index
|| logx
.reserved1
? logx
.device_error_count
: 0);
1607 // returns <0 if problem. Otherwise, bottom 8 bits are the self test
1608 // error count, and top bits are the power-on hours of the last error.
1609 static int SelfTestErrorCount(ata_device
* device
, const char * name
,
1610 firmwarebug_defs firmwarebugs
)
1612 struct ata_smart_selftestlog log
;
1614 if (ataReadSelfTestLog(device
, &log
, firmwarebugs
)){
1615 PrintOut(LOG_INFO
,"Device: %s, Read SMART Self Test Log Failed\n",name
);
1619 // return current number of self-test errors
1620 return ataPrintSmartSelfTestlog(&log
, false, firmwarebugs
);
1623 #define SELFTEST_ERRORCOUNT(x) (x & 0xff)
1624 #define SELFTEST_ERRORHOURS(x) ((x >> 8) & 0xffff)
1626 // Check offline data collection status
1627 static inline bool is_offl_coll_in_progress(unsigned char status
)
1629 return ((status
& 0x7f) == 0x03);
1632 // Check self-test execution status
1633 static inline bool is_self_test_in_progress(unsigned char status
)
1635 return ((status
>> 4) == 0xf);
1638 // Log offline data collection status
1639 static void log_offline_data_coll_status(const char * name
, unsigned char status
)
1642 switch (status
& 0x7f) {
1643 case 0x00: msg
= "was never started"; break;
1644 case 0x02: msg
= "was completed without error"; break;
1645 case 0x03: msg
= "is in progress"; break;
1646 case 0x04: msg
= "was suspended by an interrupting command from host"; break;
1647 case 0x05: msg
= "was aborted by an interrupting command from host"; break;
1648 case 0x06: msg
= "was aborted by the device with a fatal error"; break;
1653 PrintOut(((status
& 0x7f) == 0x06 ? LOG_CRIT
: LOG_INFO
),
1654 "Device: %s, offline data collection %s%s\n", name
, msg
,
1655 ((status
& 0x80) ? " (auto:on)" : ""));
1657 PrintOut(LOG_INFO
, "Device: %s, unknown offline data collection status 0x%02x\n",
1661 // Log self-test execution status
1662 static void log_self_test_exec_status(const char * name
, unsigned char status
)
1665 switch (status
>> 4) {
1666 case 0x0: msg
= "completed without error"; break;
1667 case 0x1: msg
= "was aborted by the host"; break;
1668 case 0x2: msg
= "was interrupted by the host with a reset"; break;
1669 case 0x3: msg
= "could not complete due to a fatal or unknown error"; break;
1670 case 0x4: msg
= "completed with error (unknown test element)"; break;
1671 case 0x5: msg
= "completed with error (electrical test element)"; break;
1672 case 0x6: msg
= "completed with error (servo/seek test element)"; break;
1673 case 0x7: msg
= "completed with error (read test element)"; break;
1674 case 0x8: msg
= "completed with error (handling damage?)"; break;
1679 PrintOut(((status
>> 4) >= 0x4 ? LOG_CRIT
: LOG_INFO
),
1680 "Device: %s, previous self-test %s\n", name
, msg
);
1681 else if ((status
>> 4) == 0xf)
1682 PrintOut(LOG_INFO
, "Device: %s, self-test in progress, %u0%% remaining\n",
1683 name
, status
& 0x0f);
1685 PrintOut(LOG_INFO
, "Device: %s, unknown self-test status 0x%02x\n",
1689 // Check pending sector count id (-C, -U directives).
1690 static bool check_pending_id(const dev_config
& cfg
, const dev_state
& state
,
1691 unsigned char id
, const char * msg
)
1693 // Check attribute index
1694 int i
= ata_find_attr_index(id
, state
.smartval
);
1696 PrintOut(LOG_INFO
, "Device: %s, can't monitor %s count - no Attribute %d\n",
1697 cfg
.name
.c_str(), msg
, id
);
1702 uint64_t rawval
= ata_get_attr_raw_value(state
.smartval
.vendor_attributes
[i
],
1703 cfg
.attribute_defs
);
1704 if (rawval
>= (state
.num_sectors
? state
.num_sectors
: 0xffffffffULL
)) {
1705 PrintOut(LOG_INFO
, "Device: %s, ignoring %s count - bogus Attribute %d value %" PRIu64
" (0x%" PRIx64
")\n",
1706 cfg
.name
.c_str(), msg
, id
, rawval
, rawval
);
1713 // Called by ATA/SCSI/NVMeDeviceScan() after successful device check
1714 static void finish_device_scan(dev_config
& cfg
, dev_state
& state
)
1716 // Set cfg.emailfreq if user hasn't set it
1717 if ((!cfg
.emailaddress
.empty() || !cfg
.emailcmdline
.empty()) && !cfg
.emailfreq
) {
1718 // Avoid that emails are suppressed forever due to state persistence
1719 if (cfg
.state_file
.empty())
1720 cfg
.emailfreq
= 1; // '-M once'
1722 cfg
.emailfreq
= 2; // '-M daily'
1725 // Start self-test regex check now if time was not read from state file
1726 if (!cfg
.test_regex
.empty() && !state
.scheduled_test_next_check
)
1727 state
.scheduled_test_next_check
= time(0);
1730 // Common function to format result message for ATA setting
1731 static void format_set_result_msg(std::string
& msg
, const char * name
, bool ok
,
1732 int set_option
= 0, bool has_value
= false)
1739 else if (set_option
< 0)
1742 msg
+= strprintf(":%d", set_option
-1);
1743 else if (set_option
> 0)
1748 // TODO: Add '-F swapid' directive
1749 const bool fix_swapped_id
= false;
1751 // scan to see what ata devices there are, and if they support SMART
1752 static int ATADeviceScan(dev_config
& cfg
, dev_state
& state
, ata_device
* atadev
)
1755 struct ata_identify_device drive
;
1756 const char *name
= cfg
.name
.c_str();
1759 // Device must be open
1761 // Get drive identity structure
1762 if ((retid
= ata_read_identity(atadev
, &drive
, fix_swapped_id
))) {
1764 // Unable to read Identity structure
1765 PrintOut(LOG_INFO
,"Device: %s, not ATA, no IDENTIFY DEVICE Structure\n",name
);
1767 PrintOut(LOG_INFO
,"Device: %s, packet devices [this device %s] not SMART capable\n",
1768 name
, packetdevicetype(retid
-1));
1769 CloseDevice(atadev
, name
);
1773 // Get drive identity, size and rotation rate (HDD/SSD)
1774 char model
[40+1], serial
[20+1], firmware
[8+1];
1775 ata_format_id_string(model
, drive
.model
, sizeof(model
)-1);
1776 ata_format_id_string(serial
, drive
.serial_no
, sizeof(serial
)-1);
1777 ata_format_id_string(firmware
, drive
.fw_rev
, sizeof(firmware
)-1);
1779 ata_size_info sizes
;
1780 ata_get_size_info(&drive
, sizes
);
1781 state
.num_sectors
= sizes
.sectors
;
1782 cfg
.dev_rpm
= ata_get_rotation_rate(&drive
);
1784 char wwn
[30]; wwn
[0] = 0;
1785 unsigned oui
= 0; uint64_t unique_id
= 0;
1786 int naa
= ata_get_wwn(&drive
, oui
, unique_id
);
1788 snprintf(wwn
, sizeof(wwn
), "WWN:%x-%06x-%09" PRIx64
", ", naa
, oui
, unique_id
);
1790 // Format device id string for warning emails
1792 cfg
.dev_idinfo
= strprintf("%s, S/N:%s, %sFW:%s, %s", model
, serial
, wwn
, firmware
,
1793 format_capacity(cap
, sizeof(cap
), sizes
.capacity
, "."));
1795 PrintOut(LOG_INFO
, "Device: %s, %s\n", name
, cfg
.dev_idinfo
.c_str());
1797 // Show if device in database, and use preset vendor attribute
1798 // options unless user has requested otherwise.
1799 if (cfg
.ignorepresets
)
1800 PrintOut(LOG_INFO
, "Device: %s, smartd database not searched (Directive: -P ignore).\n", name
);
1802 // Apply vendor specific presets, print warning if present
1803 const drive_settings
* dbentry
= lookup_drive_apply_presets(
1804 &drive
, cfg
.attribute_defs
, cfg
.firmwarebugs
);
1806 PrintOut(LOG_INFO
, "Device: %s, not found in smartd database.\n", name
);
1808 PrintOut(LOG_INFO
, "Device: %s, found in smartd database%s%s\n",
1809 name
, (*dbentry
->modelfamily
? ": " : "."), (*dbentry
->modelfamily
? dbentry
->modelfamily
: ""));
1810 if (*dbentry
->warningmsg
)
1811 PrintOut(LOG_CRIT
, "Device: %s, WARNING: %s\n", name
, dbentry
->warningmsg
);
1815 // Check for ATA Security LOCK
1816 unsigned short word128
= drive
.words088_255
[128-88];
1817 bool locked
= ((word128
& 0x0007) == 0x0007); // LOCKED|ENABLED|SUPPORTED
1819 PrintOut(LOG_INFO
, "Device: %s, ATA Security is **LOCKED**\n", name
);
1821 // Set default '-C 197[+]' if no '-C ID' is specified.
1822 if (!cfg
.curr_pending_set
)
1823 cfg
.curr_pending_id
= get_unc_attr_id(false, cfg
.attribute_defs
, cfg
.curr_pending_incr
);
1824 // Set default '-U 198[+]' if no '-U ID' is specified.
1825 if (!cfg
.offl_pending_set
)
1826 cfg
.offl_pending_id
= get_unc_attr_id(true, cfg
.attribute_defs
, cfg
.offl_pending_incr
);
1828 // If requested, show which presets would be used for this drive
1829 if (cfg
.showpresets
) {
1830 int savedebugmode
=debugmode
;
1831 PrintOut(LOG_INFO
, "Device %s: presets are:\n", name
);
1834 show_presets(&drive
);
1835 debugmode
=savedebugmode
;
1838 // see if drive supports SMART
1839 supported
=ataSmartSupport(&drive
);
1842 // drive does NOT support SMART
1843 PrintOut(LOG_INFO
,"Device: %s, lacks SMART capability\n",name
);
1845 // can't tell if drive supports SMART
1846 PrintOut(LOG_INFO
,"Device: %s, ATA IDENTIFY DEVICE words 82-83 don't specify if SMART capable.\n",name
);
1848 // should we proceed anyway?
1849 if (cfg
.permissive
) {
1850 PrintOut(LOG_INFO
,"Device: %s, proceeding since '-T permissive' Directive given.\n",name
);
1853 PrintOut(LOG_INFO
,"Device: %s, to proceed anyway, use '-T permissive' Directive.\n",name
);
1854 CloseDevice(atadev
, name
);
1859 if (ataEnableSmart(atadev
)) {
1860 // Enable SMART command has failed
1861 PrintOut(LOG_INFO
,"Device: %s, could not enable SMART capability\n",name
);
1863 if (ataIsSmartEnabled(&drive
) <= 0) {
1864 CloseDevice(atadev
, name
);
1867 PrintOut(LOG_INFO
, "Device: %s, proceeding since SMART is already enabled\n", name
);
1870 // disable device attribute autosave...
1871 if (cfg
.autosave
==1) {
1872 if (ataDisableAutoSave(atadev
))
1873 PrintOut(LOG_INFO
,"Device: %s, could not disable SMART Attribute Autosave.\n",name
);
1875 PrintOut(LOG_INFO
,"Device: %s, disabled SMART Attribute Autosave.\n",name
);
1878 // or enable device attribute autosave
1879 if (cfg
.autosave
==2) {
1880 if (ataEnableAutoSave(atadev
))
1881 PrintOut(LOG_INFO
,"Device: %s, could not enable SMART Attribute Autosave.\n",name
);
1883 PrintOut(LOG_INFO
,"Device: %s, enabled SMART Attribute Autosave.\n",name
);
1886 // capability check: SMART status
1887 if (cfg
.smartcheck
&& ataSmartStatus2(atadev
) == -1) {
1888 PrintOut(LOG_INFO
,"Device: %s, not capable of SMART Health Status check\n",name
);
1889 cfg
.smartcheck
= false;
1892 // capability check: Read smart values and thresholds. Note that
1893 // smart values are ALSO needed even if we ONLY want to know if the
1894 // device is self-test log or error-log capable! After ATA-5, this
1895 // information was ALSO reproduced in the IDENTIFY DEVICE response,
1896 // but sadly not for ATA-5. Sigh.
1898 // do we need to get SMART data?
1899 bool smart_val_ok
= false;
1900 if ( cfg
.autoofflinetest
|| cfg
.selftest
1901 || cfg
.errorlog
|| cfg
.xerrorlog
1902 || cfg
.offlinests
|| cfg
.selfteststs
1903 || cfg
.usagefailed
|| cfg
.prefail
|| cfg
.usage
1904 || cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
1905 || cfg
.curr_pending_id
|| cfg
.offl_pending_id
) {
1907 if (ataReadSmartValues(atadev
, &state
.smartval
)) {
1908 PrintOut(LOG_INFO
, "Device: %s, Read SMART Values failed\n", name
);
1909 cfg
.usagefailed
= cfg
.prefail
= cfg
.usage
= false;
1910 cfg
.tempdiff
= cfg
.tempinfo
= cfg
.tempcrit
= 0;
1911 cfg
.curr_pending_id
= cfg
.offl_pending_id
= 0;
1914 smart_val_ok
= true;
1915 if (ataReadSmartThresholds(atadev
, &state
.smartthres
)) {
1916 PrintOut(LOG_INFO
, "Device: %s, Read SMART Thresholds failed%s\n",
1917 name
, (cfg
.usagefailed
? ", ignoring -f Directive" : ""));
1918 cfg
.usagefailed
= false;
1919 // Let ata_get_attr_state() return ATTRSTATE_NO_THRESHOLD:
1920 memset(&state
.smartthres
, 0, sizeof(state
.smartthres
));
1924 // see if the necessary Attribute is there to monitor offline or
1925 // current pending sectors or temperature
1926 if ( cfg
.curr_pending_id
1927 && !check_pending_id(cfg
, state
, cfg
.curr_pending_id
,
1928 "Current_Pending_Sector"))
1929 cfg
.curr_pending_id
= 0;
1931 if ( cfg
.offl_pending_id
1932 && !check_pending_id(cfg
, state
, cfg
.offl_pending_id
,
1933 "Offline_Uncorrectable"))
1934 cfg
.offl_pending_id
= 0;
1936 if ( (cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
)
1937 && !ata_return_temperature_value(&state
.smartval
, cfg
.attribute_defs
)) {
1938 PrintOut(LOG_INFO
, "Device: %s, can't monitor Temperature, ignoring -W %d,%d,%d\n",
1939 name
, cfg
.tempdiff
, cfg
.tempinfo
, cfg
.tempcrit
);
1940 cfg
.tempdiff
= cfg
.tempinfo
= cfg
.tempcrit
= 0;
1943 // Report ignored '-r' or '-R' directives
1944 for (int id
= 1; id
<= 255; id
++) {
1945 if (cfg
.monitor_attr_flags
.is_set(id
, MONITOR_RAW_PRINT
)) {
1946 char opt
= (!cfg
.monitor_attr_flags
.is_set(id
, MONITOR_RAW
) ? 'r' : 'R');
1947 const char * excl
= (cfg
.monitor_attr_flags
.is_set(id
,
1948 (opt
== 'r' ? MONITOR_AS_CRIT
: MONITOR_RAW_AS_CRIT
)) ? "!" : "");
1950 int idx
= ata_find_attr_index(id
, state
.smartval
);
1952 PrintOut(LOG_INFO
,"Device: %s, no Attribute %d, ignoring -%c %d%s\n", name
, id
, opt
, id
, excl
);
1954 bool prefail
= !!ATTRIBUTE_FLAGS_PREFAILURE(state
.smartval
.vendor_attributes
[idx
].flags
);
1955 if (!((prefail
&& cfg
.prefail
) || (!prefail
&& cfg
.usage
)))
1956 PrintOut(LOG_INFO
,"Device: %s, not monitoring %s Attributes, ignoring -%c %d%s\n", name
,
1957 (prefail
? "Prefailure" : "Usage"), opt
, id
, excl
);
1963 // enable/disable automatic on-line testing
1964 if (cfg
.autoofflinetest
) {
1965 // is this an enable or disable request?
1966 const char *what
=(cfg
.autoofflinetest
==1)?"disable":"enable";
1968 PrintOut(LOG_INFO
,"Device: %s, could not %s SMART Automatic Offline Testing.\n",name
, what
);
1970 // if command appears unsupported, issue a warning...
1971 if (!isSupportAutomaticTimer(&state
.smartval
))
1972 PrintOut(LOG_INFO
,"Device: %s, SMART Automatic Offline Testing unsupported...\n",name
);
1973 // ... but then try anyway
1974 if ((cfg
.autoofflinetest
==1)?ataDisableAutoOffline(atadev
):ataEnableAutoOffline(atadev
))
1975 PrintOut(LOG_INFO
,"Device: %s, %s SMART Automatic Offline Testing failed.\n", name
, what
);
1977 PrintOut(LOG_INFO
,"Device: %s, %sd SMART Automatic Offline Testing.\n", name
, what
);
1981 // Read log directories if required for capability check
1982 ata_smart_log_directory smart_logdir
, gp_logdir
;
1983 bool smart_logdir_ok
= false, gp_logdir_ok
= false;
1985 if ( isGeneralPurposeLoggingCapable(&drive
)
1986 && (cfg
.errorlog
|| cfg
.selftest
)
1987 && !cfg
.firmwarebugs
.is_set(BUG_NOLOGDIR
)) {
1988 if (!ataReadLogDirectory(atadev
, &smart_logdir
, false))
1989 smart_logdir_ok
= true;
1992 if (cfg
.xerrorlog
&& !cfg
.firmwarebugs
.is_set(BUG_NOLOGDIR
)) {
1993 if (!ataReadLogDirectory(atadev
, &gp_logdir
, true))
1994 gp_logdir_ok
= true;
1997 // capability check: self-test-log
1998 state
.selflogcount
= 0; state
.selfloghour
= 0;
2001 if (!( cfg
.permissive
2002 || ( smart_logdir_ok
&& smart_logdir
.entry
[0x06-1].numsectors
)
2003 || (!smart_logdir_ok
&& smart_val_ok
&& isSmartTestLogCapable(&state
.smartval
, &drive
)))) {
2004 PrintOut(LOG_INFO
, "Device: %s, no SMART Self-test Log, ignoring -l selftest (override with -T permissive)\n", name
);
2005 cfg
.selftest
= false;
2007 else if ((retval
= SelfTestErrorCount(atadev
, name
, cfg
.firmwarebugs
)) < 0) {
2008 PrintOut(LOG_INFO
, "Device: %s, no SMART Self-test Log, ignoring -l selftest\n", name
);
2009 cfg
.selftest
= false;
2012 state
.selflogcount
=SELFTEST_ERRORCOUNT(retval
);
2013 state
.selfloghour
=SELFTEST_ERRORHOURS(retval
);
2017 // capability check: ATA error log
2018 state
.ataerrorcount
= 0;
2021 if (!( cfg
.permissive
2022 || ( smart_logdir_ok
&& smart_logdir
.entry
[0x01-1].numsectors
)
2023 || (!smart_logdir_ok
&& smart_val_ok
&& isSmartErrorLogCapable(&state
.smartval
, &drive
)))) {
2024 PrintOut(LOG_INFO
, "Device: %s, no SMART Error Log, ignoring -l error (override with -T permissive)\n", name
);
2025 cfg
.errorlog
= false;
2027 else if ((errcnt1
= read_ata_error_count(atadev
, name
, cfg
.firmwarebugs
, false)) < 0) {
2028 PrintOut(LOG_INFO
, "Device: %s, no SMART Error Log, ignoring -l error\n", name
);
2029 cfg
.errorlog
= false;
2032 state
.ataerrorcount
= errcnt1
;
2035 if (cfg
.xerrorlog
) {
2037 if (!( cfg
.permissive
|| cfg
.firmwarebugs
.is_set(BUG_NOLOGDIR
)
2038 || (gp_logdir_ok
&& gp_logdir
.entry
[0x03-1].numsectors
) )) {
2039 PrintOut(LOG_INFO
, "Device: %s, no Extended Comprehensive SMART Error Log, ignoring -l xerror (override with -T permissive)\n",
2041 cfg
.xerrorlog
= false;
2043 else if ((errcnt2
= read_ata_error_count(atadev
, name
, cfg
.firmwarebugs
, true)) < 0) {
2044 PrintOut(LOG_INFO
, "Device: %s, no Extended Comprehensive SMART Error Log, ignoring -l xerror\n", name
);
2045 cfg
.xerrorlog
= false;
2047 else if (cfg
.errorlog
&& state
.ataerrorcount
!= errcnt2
) {
2048 PrintOut(LOG_INFO
, "Device: %s, SMART Error Logs report different error counts: %d != %d\n",
2049 name
, state
.ataerrorcount
, errcnt2
);
2050 // Record max error count
2051 if (errcnt2
> state
.ataerrorcount
)
2052 state
.ataerrorcount
= errcnt2
;
2055 state
.ataerrorcount
= errcnt2
;
2058 // capability check: self-test and offline data collection status
2059 if (cfg
.offlinests
|| cfg
.selfteststs
) {
2060 if (!(cfg
.permissive
|| (smart_val_ok
&& state
.smartval
.offline_data_collection_capability
))) {
2062 PrintOut(LOG_INFO
, "Device: %s, no SMART Offline Data Collection capability, ignoring -l offlinests (override with -T permissive)\n", name
);
2063 if (cfg
.selfteststs
)
2064 PrintOut(LOG_INFO
, "Device: %s, no SMART Self-test capability, ignoring -l selfteststs (override with -T permissive)\n", name
);
2065 cfg
.offlinests
= cfg
.selfteststs
= false;
2069 // capabilities check -- does it support powermode?
2070 if (cfg
.powermode
) {
2071 int powermode
= ataCheckPowerMode(atadev
);
2073 if (-1 == powermode
) {
2074 PrintOut(LOG_CRIT
, "Device: %s, no ATA CHECK POWER STATUS support, ignoring -n Directive\n", name
);
2077 else if (powermode
!=0x00 && powermode
!=0x01
2078 && powermode
!=0x40 && powermode
!=0x41
2079 && powermode
!=0x80 && powermode
!=0x81 && powermode
!=0x82 && powermode
!=0x83
2080 && powermode
!=0xff) {
2081 PrintOut(LOG_CRIT
, "Device: %s, CHECK POWER STATUS returned %d, not ATA compliant, ignoring -n Directive\n",
2087 // Apply ATA settings
2091 format_set_result_msg(msg
, "AAM", (cfg
.set_aam
> 0 ?
2092 ata_set_features(atadev
, ATA_ENABLE_AAM
, cfg
.set_aam
-1) :
2093 ata_set_features(atadev
, ATA_DISABLE_AAM
)), cfg
.set_aam
, true);
2096 format_set_result_msg(msg
, "APM", (cfg
.set_apm
> 0 ?
2097 ata_set_features(atadev
, ATA_ENABLE_APM
, cfg
.set_apm
-1) :
2098 ata_set_features(atadev
, ATA_DISABLE_APM
)), cfg
.set_apm
, true);
2100 if (cfg
.set_lookahead
)
2101 format_set_result_msg(msg
, "Rd-ahead", ata_set_features(atadev
,
2102 (cfg
.set_lookahead
> 0 ? ATA_ENABLE_READ_LOOK_AHEAD
: ATA_DISABLE_READ_LOOK_AHEAD
)),
2106 format_set_result_msg(msg
, "Wr-cache", ata_set_features(atadev
,
2107 (cfg
.set_wcache
> 0? ATA_ENABLE_WRITE_CACHE
: ATA_DISABLE_WRITE_CACHE
)), cfg
.set_wcache
);
2109 if (cfg
.set_security_freeze
)
2110 format_set_result_msg(msg
, "Security freeze",
2111 ata_nodata_command(atadev
, ATA_SECURITY_FREEZE_LOCK
));
2113 if (cfg
.set_standby
)
2114 format_set_result_msg(msg
, "Standby",
2115 ata_nodata_command(atadev
, ATA_IDLE
, cfg
.set_standby
-1), cfg
.set_standby
, true);
2117 // Report as one log entry
2119 PrintOut(LOG_INFO
, "Device: %s, ATA settings applied: %s\n", name
, msg
.c_str());
2121 // set SCT Error Recovery Control if requested
2122 if (cfg
.sct_erc_set
) {
2123 if (!isSCTErrorRecoveryControlCapable(&drive
))
2124 PrintOut(LOG_INFO
, "Device: %s, no SCT Error Recovery Control support, ignoring -l scterc\n",
2127 PrintOut(LOG_INFO
, "Device: %s, no SCT support if ATA Security is LOCKED, ignoring -l scterc\n",
2129 else if ( ataSetSCTErrorRecoveryControltime(atadev
, 1, cfg
.sct_erc_readtime
)
2130 || ataSetSCTErrorRecoveryControltime(atadev
, 2, cfg
.sct_erc_writetime
))
2131 PrintOut(LOG_INFO
, "Device: %s, set of SCT Error Recovery Control failed\n", name
);
2133 PrintOut(LOG_INFO
, "Device: %s, SCT Error Recovery Control set to: Read: %u, Write: %u\n",
2134 name
, cfg
.sct_erc_readtime
, cfg
.sct_erc_writetime
);
2137 // If no tests available or selected, return
2138 if (!( cfg
.smartcheck
|| cfg
.selftest
2139 || cfg
.errorlog
|| cfg
.xerrorlog
2140 || cfg
.offlinests
|| cfg
.selfteststs
2141 || cfg
.usagefailed
|| cfg
.prefail
|| cfg
.usage
2142 || cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
)) {
2143 CloseDevice(atadev
, name
);
2147 // tell user we are registering device
2148 PrintOut(LOG_INFO
,"Device: %s, is SMART capable. Adding to \"monitor\" list.\n",name
);
2150 // close file descriptor
2151 CloseDevice(atadev
, name
);
2153 if (!state_path_prefix
.empty() || !attrlog_path_prefix
.empty()) {
2154 // Build file name for state file
2155 std::replace_if(model
, model
+strlen(model
), not_allowed_in_filename
, '_');
2156 std::replace_if(serial
, serial
+strlen(serial
), not_allowed_in_filename
, '_');
2157 if (!state_path_prefix
.empty()) {
2158 cfg
.state_file
= strprintf("%s%s-%s.ata.state", state_path_prefix
.c_str(), model
, serial
);
2159 // Read previous state
2160 if (read_dev_state(cfg
.state_file
.c_str(), state
)) {
2161 PrintOut(LOG_INFO
, "Device: %s, state read from %s\n", name
, cfg
.state_file
.c_str());
2162 // Copy ATA attribute values to temp state
2163 state
.update_temp_state();
2166 if (!attrlog_path_prefix
.empty())
2167 cfg
.attrlog_file
= strprintf("%s%s-%s.ata.csv", attrlog_path_prefix
.c_str(), model
, serial
);
2170 finish_device_scan(cfg
, state
);
2175 // on success, return 0. On failure, return >0. Never return <0,
2177 static int SCSIDeviceScan(dev_config
& cfg
, dev_state
& state
, scsi_device
* scsidev
)
2179 int err
, req_len
, avail_len
, version
, len
;
2180 const char *device
= cfg
.name
.c_str();
2181 struct scsi_iec_mode_page iec
;
2185 char lu_id
[64], serial
[256], vendor
[40], model
[40];
2187 // Device must be open
2188 memset(inqBuf
, 0, 96);
2190 if ((err
= scsiStdInquiry(scsidev
, inqBuf
, req_len
))) {
2191 /* Marvell controllers fail on a 36 bytes StdInquiry, but 64 suffices */
2193 if ((err
= scsiStdInquiry(scsidev
, inqBuf
, req_len
))) {
2194 PrintOut(LOG_INFO
, "Device: %s, Both 36 and 64 byte INQUIRY failed; "
2195 "skip device\n", device
);
2199 version
= (inqBuf
[2] & 0x7f); /* Accept old ISO/IEC 9316:1995 variants */
2201 avail_len
= inqBuf
[4] + 5;
2202 len
= (avail_len
< req_len
) ? avail_len
: req_len
;
2204 PrintOut(LOG_INFO
, "Device: %s, INQUIRY response less than 36 bytes; "
2205 "skip device\n", device
);
2209 int pdt
= inqBuf
[0] & 0x1f;
2211 if (! ((0 == pdt
) || (4 == pdt
) || (5 == pdt
) || (7 == pdt
) ||
2213 PrintOut(LOG_INFO
, "Device: %s, not a disk like device [PDT=0x%x], "
2214 "skip\n", device
, pdt
);
2218 if (supported_vpd_pages_p
) {
2219 delete supported_vpd_pages_p
;
2220 supported_vpd_pages_p
= NULL
;
2222 supported_vpd_pages_p
= new supported_vpd_pages(scsidev
);
2225 if ((version
>= 0x3) && (version
< 0x8)) {
2227 if (0 == scsiInquiryVpd(scsidev
, SCSI_VPD_DEVICE_IDENTIFICATION
,
2228 vpdBuf
, sizeof(vpdBuf
))) {
2230 scsi_decode_lu_dev_id(vpdBuf
+ 4, len
, lu_id
, sizeof(lu_id
), NULL
);
2234 if (0 == scsiInquiryVpd(scsidev
, SCSI_VPD_UNIT_SERIAL_NUMBER
,
2235 vpdBuf
, sizeof(vpdBuf
))) {
2237 vpdBuf
[4 + len
] = '\0';
2238 scsi_format_id_string(serial
, (const unsigned char *)&vpdBuf
[4], len
);
2241 unsigned int lb_size
;
2243 uint64_t capacity
= scsiGetSize(scsidev
, &lb_size
, NULL
);
2246 format_capacity(si_str
, sizeof(si_str
), capacity
, ".");
2250 // Format device id string for warning emails
2251 cfg
.dev_idinfo
= strprintf("[%.8s %.16s %.4s]%s%s%s%s%s%s",
2252 (char *)&inqBuf
[8], (char *)&inqBuf
[16], (char *)&inqBuf
[32],
2253 (lu_id
[0] ? ", lu id: " : ""), (lu_id
[0] ? lu_id
: ""),
2254 (serial
[0] ? ", S/N: " : ""), (serial
[0] ? serial
: ""),
2255 (si_str
[0] ? ", " : ""), (si_str
[0] ? si_str
: ""));
2257 // format "model" string
2258 scsi_format_id_string(vendor
, (const unsigned char *)&inqBuf
[8], 8);
2259 scsi_format_id_string(model
, (const unsigned char *)&inqBuf
[16], 16);
2260 PrintOut(LOG_INFO
, "Device: %s, %s\n", device
, cfg
.dev_idinfo
.c_str());
2262 // check that device is ready for commands. IE stores its stuff on
2264 if ((err
= scsiTestUnitReady(scsidev
))) {
2265 if (SIMPLE_ERR_NOT_READY
== err
)
2266 PrintOut(LOG_INFO
, "Device: %s, NOT READY (e.g. spun down); skip device\n", device
);
2267 else if (SIMPLE_ERR_NO_MEDIUM
== err
)
2268 PrintOut(LOG_INFO
, "Device: %s, NO MEDIUM present; skip device\n", device
);
2269 else if (SIMPLE_ERR_BECOMING_READY
== err
)
2270 PrintOut(LOG_INFO
, "Device: %s, BECOMING (but not yet) READY; skip device\n", device
);
2272 PrintOut(LOG_CRIT
, "Device: %s, failed Test Unit Ready [err=%d]\n", device
, err
);
2273 CloseDevice(scsidev
, device
);
2277 // Badly-conforming USB storage devices may fail this check.
2278 // The response to the following IE mode page fetch (current and
2279 // changeable values) is carefully examined. It has been found
2280 // that various USB devices that malform the response will lock up
2281 // if asked for a log page (e.g. temperature) so it is best to
2283 if (!(err
= scsiFetchIECmpage(scsidev
, &iec
, state
.modese_len
)))
2284 state
.modese_len
= iec
.modese_len
;
2285 else if (SIMPLE_ERR_BAD_FIELD
== err
)
2286 ; /* continue since it is reasonable not to support IE mpage */
2287 else { /* any other error (including malformed response) unreasonable */
2289 "Device: %s, Bad IEC (SMART) mode page, err=%d, skip device\n",
2291 CloseDevice(scsidev
, device
);
2295 // N.B. The following is passive (i.e. it doesn't attempt to turn on
2296 // smart if it is off). This may change to be the same as the ATA side.
2297 if (!scsi_IsExceptionControlEnabled(&iec
)) {
2298 PrintOut(LOG_INFO
, "Device: %s, IE (SMART) not enabled, skip device\n"
2299 "Try 'smartctl -s on %s' to turn on SMART features\n",
2301 CloseDevice(scsidev
, device
);
2305 // Flag that certain log pages are supported (information may be
2306 // available from other sources).
2307 if (0 == scsiLogSense(scsidev
, SUPPORTED_LPAGES
, 0, tBuf
, sizeof(tBuf
), 0) ||
2308 0 == scsiLogSense(scsidev
, SUPPORTED_LPAGES
, 0, tBuf
, sizeof(tBuf
), 68))
2309 /* workaround for the bug #678 on ST8000NM0075/E001. Up to 64 pages + 4b header */
2311 for (int k
= 4; k
< tBuf
[3] + LOGPAGEHDRSIZE
; ++k
) {
2313 case TEMPERATURE_LPAGE
:
2314 state
.TempPageSupported
= 1;
2317 state
.SmartPageSupported
= 1;
2319 case READ_ERROR_COUNTER_LPAGE
:
2320 state
.ReadECounterPageSupported
= 1;
2322 case WRITE_ERROR_COUNTER_LPAGE
:
2323 state
.WriteECounterPageSupported
= 1;
2325 case VERIFY_ERROR_COUNTER_LPAGE
:
2326 state
.VerifyECounterPageSupported
= 1;
2328 case NON_MEDIUM_ERROR_LPAGE
:
2329 state
.NonMediumErrorPageSupported
= 1;
2337 // Check if scsiCheckIE() is going to work
2341 UINT8 currenttemp
= 0;
2344 if (scsiCheckIE(scsidev
, state
.SmartPageSupported
, state
.TempPageSupported
,
2345 &asc
, &ascq
, ¤ttemp
, &triptemp
)) {
2346 PrintOut(LOG_INFO
, "Device: %s, unexpectedly failed to read SMART values\n", device
);
2347 state
.SuppressReport
= 1;
2348 if (cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
) {
2349 PrintOut(LOG_INFO
, "Device: %s, can't monitor Temperature, ignoring -W %d,%d,%d\n",
2350 device
, cfg
.tempdiff
, cfg
.tempinfo
, cfg
.tempcrit
);
2351 cfg
.tempdiff
= cfg
.tempinfo
= cfg
.tempcrit
= 0;
2356 // capability check: self-test-log
2358 int retval
= scsiCountFailedSelfTests(scsidev
, 0);
2360 // no self-test log, turn off monitoring
2361 PrintOut(LOG_INFO
, "Device: %s, does not support SMART Self-Test Log.\n", device
);
2362 cfg
.selftest
= false;
2363 state
.selflogcount
= 0;
2364 state
.selfloghour
= 0;
2367 // register starting values to watch for changes
2368 state
.selflogcount
=SELFTEST_ERRORCOUNT(retval
);
2369 state
.selfloghour
=SELFTEST_ERRORHOURS(retval
);
2373 // disable autosave (set GLTSD bit)
2374 if (cfg
.autosave
==1){
2375 if (scsiSetControlGLTSD(scsidev
, 1, state
.modese_len
))
2376 PrintOut(LOG_INFO
,"Device: %s, could not disable autosave (set GLTSD bit).\n",device
);
2378 PrintOut(LOG_INFO
,"Device: %s, disabled autosave (set GLTSD bit).\n",device
);
2381 // or enable autosave (clear GLTSD bit)
2382 if (cfg
.autosave
==2){
2383 if (scsiSetControlGLTSD(scsidev
, 0, state
.modese_len
))
2384 PrintOut(LOG_INFO
,"Device: %s, could not enable autosave (clear GLTSD bit).\n",device
);
2386 PrintOut(LOG_INFO
,"Device: %s, enabled autosave (cleared GLTSD bit).\n",device
);
2389 // tell user we are registering device
2390 PrintOut(LOG_INFO
, "Device: %s, is SMART capable. Adding to \"monitor\" list.\n", device
);
2392 // Make sure that init_standby_check() ignores SCSI devices
2393 cfg
.offlinests_ns
= cfg
.selfteststs_ns
= false;
2395 // close file descriptor
2396 CloseDevice(scsidev
, device
);
2398 if (!state_path_prefix
.empty() || !attrlog_path_prefix
.empty()) {
2399 // Build file name for state file
2400 std::replace_if(model
, model
+strlen(model
), not_allowed_in_filename
, '_');
2401 std::replace_if(serial
, serial
+strlen(serial
), not_allowed_in_filename
, '_');
2402 if (!state_path_prefix
.empty()) {
2403 cfg
.state_file
= strprintf("%s%s-%s-%s.scsi.state", state_path_prefix
.c_str(), vendor
, model
, serial
);
2404 // Read previous state
2405 if (read_dev_state(cfg
.state_file
.c_str(), state
)) {
2406 PrintOut(LOG_INFO
, "Device: %s, state read from %s\n", device
, cfg
.state_file
.c_str());
2407 // Copy ATA attribute values to temp state
2408 state
.update_temp_state();
2411 if (!attrlog_path_prefix
.empty())
2412 cfg
.attrlog_file
= strprintf("%s%s-%s-%s.scsi.csv", attrlog_path_prefix
.c_str(), vendor
, model
, serial
);
2415 finish_device_scan(cfg
, state
);
2420 // Convert 128 bit LE integer to uint64_t or its max value on overflow.
2421 static uint64_t le128_to_uint64(const unsigned char (& val
)[16])
2423 for (int i
= 8; i
< 16; i
++) {
2425 return ~(uint64_t)0;
2427 uint64_t lo
= val
[7];
2428 for (int i
= 7-1; i
>= 0; i
--) {
2429 lo
<<= 8; lo
+= val
[i
];
2434 // Get max temperature in Kelvin reported in NVMe SMART/Health log.
2435 static int nvme_get_max_temp_kelvin(const nvme_smart_log
& smart_log
)
2437 int k
= (smart_log
.temperature
[1] << 8) | smart_log
.temperature
[0];
2438 for (int i
= 0; i
< 8; i
++) {
2439 if (smart_log
.temp_sensor
[i
] > k
)
2440 k
= smart_log
.temp_sensor
[i
];
2445 static int NVMeDeviceScan(dev_config
& cfg
, dev_state
& state
, nvme_device
* nvmedev
)
2447 const char *name
= cfg
.name
.c_str();
2449 // Device must be open
2451 // Get ID Controller
2452 nvme_id_ctrl id_ctrl
;
2453 if (!nvme_read_id_ctrl(nvmedev
, id_ctrl
)) {
2454 PrintOut(LOG_INFO
, "Device: %s, NVMe Identify Controller failed\n", name
);
2455 CloseDevice(nvmedev
, name
);
2459 // Get drive identity
2460 char model
[40+1], serial
[20+1], firmware
[8+1];
2461 format_char_array(model
, id_ctrl
.mn
);
2462 format_char_array(serial
, id_ctrl
.sn
);
2463 format_char_array(firmware
, id_ctrl
.fr
);
2465 // Format device id string for warning emails
2466 char nsstr
[32] = "", capstr
[32] = "";
2467 unsigned nsid
= nvmedev
->get_nsid();
2468 if (nsid
!= 0xffffffff)
2469 snprintf(nsstr
, sizeof(nsstr
), ", NSID:%u", nsid
);
2470 uint64_t capacity
= le128_to_uint64(id_ctrl
.tnvmcap
);
2472 format_capacity(capstr
, sizeof(capstr
), capacity
, ".");
2473 cfg
.dev_idinfo
= strprintf("%s, S/N:%s, FW:%s%s%s%s", model
, serial
, firmware
,
2474 nsstr
, (capstr
[0] ? ", " : ""), capstr
);
2476 PrintOut(LOG_INFO
, "Device: %s, %s\n", name
, cfg
.dev_idinfo
.c_str());
2478 // Read SMART/Health log
2479 nvme_smart_log smart_log
;
2480 if (!nvme_read_smart_log(nvmedev
, smart_log
)) {
2481 PrintOut(LOG_INFO
, "Device: %s, failed to read NVMe SMART/Health Information\n", name
);
2482 CloseDevice(nvmedev
, name
);
2486 // Check temperature sensor support
2487 if (cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
) {
2488 if (!nvme_get_max_temp_kelvin(smart_log
)) {
2489 PrintOut(LOG_INFO
, "Device: %s, no Temperature sensors, ignoring -W %d,%d,%d\n",
2490 name
, cfg
.tempdiff
, cfg
.tempinfo
, cfg
.tempcrit
);
2491 cfg
.tempdiff
= cfg
.tempinfo
= cfg
.tempcrit
= 0;
2495 // Init total error count
2496 if (cfg
.errorlog
|| cfg
.xerrorlog
) {
2497 state
.nvme_err_log_entries
= le128_to_uint64(smart_log
.num_err_log_entries
);
2500 // If no supported tests selected, return
2501 if (!( cfg
.smartcheck
|| cfg
.errorlog
|| cfg
.xerrorlog
2502 || cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
)) {
2503 CloseDevice(nvmedev
, name
);
2507 // Tell user we are registering device
2508 PrintOut(LOG_INFO
,"Device: %s, is SMART capable. Adding to \"monitor\" list.\n", name
);
2510 // Make sure that init_standby_check() ignores NVMe devices
2511 cfg
.offlinests_ns
= cfg
.selfteststs_ns
= false;
2513 CloseDevice(nvmedev
, name
);
2515 if (!state_path_prefix
.empty()) {
2516 // Build file name for state file
2517 std::replace_if(model
, model
+strlen(model
), not_allowed_in_filename
, '_');
2518 std::replace_if(serial
, serial
+strlen(serial
), not_allowed_in_filename
, '_');
2520 if (nsid
!= 0xffffffff)
2521 snprintf(nsstr
, sizeof(nsstr
), "-n%u", nsid
);
2522 cfg
.state_file
= strprintf("%s%s-%s%s.nvme.state", state_path_prefix
.c_str(), model
, serial
, nsstr
);
2523 // Read previous state
2524 if (read_dev_state(cfg
.state_file
.c_str(), state
))
2525 PrintOut(LOG_INFO
, "Device: %s, state read from %s\n", name
, cfg
.state_file
.c_str());
2528 finish_device_scan(cfg
, state
);
2533 // If the self-test log has got more self-test errors (or more recent
2534 // self-test errors) recorded, then notify user.
2535 static void CheckSelfTestLogs(const dev_config
& cfg
, dev_state
& state
, int newi
)
2537 const char * name
= cfg
.name
.c_str();
2541 MailWarning(cfg
, state
, 8, "Device: %s, Read SMART Self-Test Log Failed", name
);
2543 reset_warning_mail(cfg
, state
, 8, "Read SMART Self-Test Log worked again");
2545 // old and new error counts
2546 int oldc
=state
.selflogcount
;
2547 int newc
=SELFTEST_ERRORCOUNT(newi
);
2549 // old and new error timestamps in hours
2550 int oldh
=state
.selfloghour
;
2551 int newh
=SELFTEST_ERRORHOURS(newi
);
2554 // increase in error count
2555 PrintOut(LOG_CRIT
, "Device: %s, Self-Test Log error count increased from %d to %d\n",
2557 MailWarning(cfg
, state
, 3, "Device: %s, Self-Test Log error count increased from %d to %d",
2559 state
.must_write
= true;
2561 else if (newc
> 0 && oldh
!= newh
) {
2562 // more recent error
2563 // a 'more recent' error might actually be a smaller hour number,
2564 // if the hour number has wrapped.
2565 // There's still a bug here. You might just happen to run a new test
2566 // exactly 32768 hours after the previous failure, and have run exactly
2567 // 20 tests between the two, in which case smartd will miss the
2569 PrintOut(LOG_CRIT
, "Device: %s, new Self-Test Log error at hour timestamp %d\n",
2571 MailWarning(cfg
, state
, 3, "Device: %s, new Self-Test Log error at hour timestamp %d",
2573 state
.must_write
= true;
2576 // Print info if error entries have disappeared
2577 // or newer successful successful extended self-test exits
2579 PrintOut(LOG_INFO
, "Device: %s, Self-Test Log error count decreased from %d to %d\n",
2582 reset_warning_mail(cfg
, state
, 3, "Self-Test Log does no longer report errors");
2585 // Needed since self-test error count may DECREASE. Hour might
2586 // also have changed.
2587 state
.selflogcount
= newc
;
2588 state
.selfloghour
= newh
;
2593 // Test types, ordered by priority.
2594 static const char test_type_chars
[] = "LncrSCO";
2595 static const unsigned num_test_types
= sizeof(test_type_chars
)-1;
2597 // returns test type if time to do test of type testtype,
2598 // 0 if not time to do test.
2599 static char next_scheduled_test(const dev_config
& cfg
, dev_state
& state
, bool scsi
, time_t usetime
= 0)
2601 // check that self-testing has been requested
2602 if (cfg
.test_regex
.empty())
2605 // Exit if drive not capable of any test
2606 if ( state
.not_cap_long
&& state
.not_cap_short
&&
2607 (scsi
|| (state
.not_cap_conveyance
&& state
.not_cap_offline
)))
2610 // since we are about to call localtime(), be sure glibc is informed
2611 // of any timezone changes we make.
2613 FixGlibcTimeZoneBug();
2615 // Is it time for next check?
2616 time_t now
= (!usetime
? time(0) : usetime
);
2617 if (now
< state
.scheduled_test_next_check
)
2620 // Limit time check interval to 90 days
2621 if (state
.scheduled_test_next_check
+ (3600L*24*90) < now
)
2622 state
.scheduled_test_next_check
= now
- (3600L*24*90);
2624 // Check interval [state.scheduled_test_next_check, now] for scheduled tests
2626 time_t testtime
= 0; int testhour
= 0;
2627 int maxtest
= num_test_types
-1;
2629 for (time_t t
= state
.scheduled_test_next_check
; ; ) {
2630 struct tm
* tms
= localtime(&t
);
2631 // tm_wday is 0 (Sunday) to 6 (Saturday). We use 1 (Monday) to 7 (Sunday).
2632 int weekday
= (tms
->tm_wday
? tms
->tm_wday
: 7);
2633 for (int i
= 0; i
<= maxtest
; i
++) {
2634 // Skip if drive not capable of this test
2635 switch (test_type_chars
[i
]) {
2636 case 'L': if (state
.not_cap_long
) continue; break;
2637 case 'S': if (state
.not_cap_short
) continue; break;
2638 case 'C': if (scsi
|| state
.not_cap_conveyance
) continue; break;
2639 case 'O': if (scsi
|| state
.not_cap_offline
) continue; break;
2641 case 'r': if (scsi
|| state
.not_cap_selective
) continue; break;
2644 // Try match of "T/MM/DD/d/HH"
2646 snprintf(pattern
, sizeof(pattern
), "%c/%02d/%02d/%1d/%02d",
2647 test_type_chars
[i
], tms
->tm_mon
+1, tms
->tm_mday
, weekday
, tms
->tm_hour
);
2648 if (cfg
.test_regex
.full_match(pattern
)) {
2650 testtype
= pattern
[0];
2651 testtime
= t
; testhour
= tms
->tm_hour
;
2652 // Limit further matches to higher priority self-tests
2657 // Exit if no tests left or current time reached
2663 if ((t
+= 3600) > now
)
2667 // Do next check not before next hour.
2668 struct tm
* tmnow
= localtime(&now
);
2669 state
.scheduled_test_next_check
= now
+ (3600 - tmnow
->tm_min
*60 - tmnow
->tm_sec
);
2672 state
.must_write
= true;
2673 // Tell user if an old test was found.
2674 if (!usetime
&& !(testhour
== tmnow
->tm_hour
&& testtime
+ 3600 > now
)) {
2675 char datebuf
[DATEANDEPOCHLEN
]; dateandtimezoneepoch(datebuf
, testtime
);
2676 PrintOut(LOG_INFO
, "Device: %s, old test of type %c not run at %s, starting now.\n",
2677 cfg
.name
.c_str(), testtype
, datebuf
);
2684 // Print a list of future tests.
2685 static void PrintTestSchedule(const dev_config_vector
& configs
, dev_state_vector
& states
, const smart_device_list
& devices
)
2687 unsigned numdev
= configs
.size();
2690 std::vector
<int> testcnts(numdev
* num_test_types
, 0);
2692 PrintOut(LOG_INFO
, "\nNext scheduled self tests (at most 5 of each type per device):\n");
2694 // FixGlibcTimeZoneBug(); // done in PrintOut()
2695 time_t now
= time(0);
2696 char datenow
[DATEANDEPOCHLEN
], date
[DATEANDEPOCHLEN
];
2697 dateandtimezoneepoch(datenow
, now
);
2700 for (seconds
=checktime
; seconds
<3600L*24*90; seconds
+=checktime
) {
2701 // Check for each device whether a test will be run
2702 time_t testtime
= now
+ seconds
;
2703 for (unsigned i
= 0; i
< numdev
; i
++) {
2704 const dev_config
& cfg
= configs
.at(i
);
2705 dev_state
& state
= states
.at(i
);
2707 char testtype
= next_scheduled_test(cfg
, state
, devices
.at(i
)->is_scsi(), testtime
);
2708 if (testtype
&& (p
= strchr(test_type_chars
, testtype
))) {
2709 unsigned t
= (p
- test_type_chars
);
2710 // Report at most 5 tests of each type
2711 if (++testcnts
[i
*num_test_types
+ t
] <= 5) {
2712 dateandtimezoneepoch(date
, testtime
);
2713 PrintOut(LOG_INFO
, "Device: %s, will do test %d of type %c at %s\n", cfg
.name
.c_str(),
2714 testcnts
[i
*num_test_types
+ t
], testtype
, date
);
2721 dateandtimezoneepoch(date
, now
+seconds
);
2722 PrintOut(LOG_INFO
, "\nTotals [%s - %s]:\n", datenow
, date
);
2723 for (unsigned i
= 0; i
< numdev
; i
++) {
2724 const dev_config
& cfg
= configs
.at(i
);
2725 bool scsi
= devices
.at(i
)->is_scsi();
2726 for (unsigned t
= 0; t
< num_test_types
; t
++) {
2727 int cnt
= testcnts
[i
*num_test_types
+ t
];
2728 if (cnt
== 0 && !strchr((scsi
? "LS" : "LSCO"), test_type_chars
[t
]))
2730 PrintOut(LOG_INFO
, "Device: %s, will do %3d test%s of type %c\n", cfg
.name
.c_str(),
2731 cnt
, (cnt
==1?"":"s"), test_type_chars
[t
]);
2737 // Return zero on success, nonzero on failure. Perform offline (background)
2738 // short or long (extended) self test on given scsi device.
2739 static int DoSCSISelfTest(const dev_config
& cfg
, dev_state
& state
, scsi_device
* device
, char testtype
)
2742 const char *testname
= 0;
2743 const char *name
= cfg
.name
.c_str();
2746 if (scsiSelfTestInProgress(device
, &inProgress
)) {
2747 PrintOut(LOG_CRIT
, "Device: %s, does not support Self-Tests\n", name
);
2748 state
.not_cap_short
= state
.not_cap_long
= true;
2752 if (1 == inProgress
) {
2753 PrintOut(LOG_INFO
, "Device: %s, skip since Self-Test already in "
2754 "progress.\n", name
);
2760 testname
= "Short Self";
2761 retval
= scsiSmartShortSelfTest(device
);
2764 testname
= "Long Self";
2765 retval
= scsiSmartExtendSelfTest(device
);
2768 // If we can't do the test, exit
2769 if (NULL
== testname
) {
2770 PrintOut(LOG_CRIT
, "Device: %s, not capable of %c Self-Test\n", name
,
2775 if ((SIMPLE_ERR_BAD_OPCODE
== retval
) ||
2776 (SIMPLE_ERR_BAD_FIELD
== retval
)) {
2777 PrintOut(LOG_CRIT
, "Device: %s, not capable of %s-Test\n", name
,
2780 state
.not_cap_long
= true;
2782 state
.not_cap_short
= true;
2786 PrintOut(LOG_CRIT
, "Device: %s, execute %s-Test failed (err: %d)\n", name
,
2791 PrintOut(LOG_INFO
, "Device: %s, starting scheduled %s-Test.\n", name
, testname
);
2796 // Do an offline immediate or self-test. Return zero on success,
2797 // nonzero on failure.
2798 static int DoATASelfTest(const dev_config
& cfg
, dev_state
& state
, ata_device
* device
, char testtype
)
2800 const char *name
= cfg
.name
.c_str();
2802 // Read current smart data and check status/capability
2803 struct ata_smart_values data
;
2804 if (ataReadSmartValues(device
, &data
) || !(data
.offline_data_collection_capability
)) {
2805 PrintOut(LOG_CRIT
, "Device: %s, not capable of Offline or Self-Testing.\n", name
);
2809 // Check for capability to do the test
2810 int dotest
= -1, mode
= 0;
2811 const char *testname
= 0;
2814 testname
="Offline Immediate ";
2815 if (isSupportExecuteOfflineImmediate(&data
))
2816 dotest
=OFFLINE_FULL_SCAN
;
2818 state
.not_cap_offline
= true;
2821 testname
="Conveyance Self-";
2822 if (isSupportConveyanceSelfTest(&data
))
2823 dotest
=CONVEYANCE_SELF_TEST
;
2825 state
.not_cap_conveyance
= true;
2828 testname
="Short Self-";
2829 if (isSupportSelfTest(&data
))
2830 dotest
=SHORT_SELF_TEST
;
2832 state
.not_cap_short
= true;
2835 testname
="Long Self-";
2836 if (isSupportSelfTest(&data
))
2837 dotest
=EXTEND_SELF_TEST
;
2839 state
.not_cap_long
= true;
2842 case 'c': case 'n': case 'r':
2843 testname
= "Selective Self-";
2844 if (isSupportSelectiveSelfTest(&data
)) {
2845 dotest
= SELECTIVE_SELF_TEST
;
2847 case 'c': mode
= SEL_CONT
; break;
2848 case 'n': mode
= SEL_NEXT
; break;
2849 case 'r': mode
= SEL_REDO
; break;
2853 state
.not_cap_selective
= true;
2857 // If we can't do the test, exit
2859 PrintOut(LOG_CRIT
, "Device: %s, not capable of %sTest\n", name
, testname
);
2863 // If currently running a self-test, do not interrupt it to start another.
2864 if (15==(data
.self_test_exec_status
>> 4)) {
2865 if (cfg
.firmwarebugs
.is_set(BUG_SAMSUNG3
) && data
.self_test_exec_status
== 0xf0) {
2866 PrintOut(LOG_INFO
, "Device: %s, will not skip scheduled %sTest "
2867 "despite unclear Self-Test byte (SAMSUNG Firmware bug).\n", name
, testname
);
2869 PrintOut(LOG_INFO
, "Device: %s, skip scheduled %sTest; %1d0%% remaining of current Self-Test.\n",
2870 name
, testname
, (int)(data
.self_test_exec_status
& 0x0f));
2875 if (dotest
== SELECTIVE_SELF_TEST
) {
2877 ata_selective_selftest_args selargs
, prev_args
;
2878 selargs
.num_spans
= 1;
2879 selargs
.span
[0].mode
= mode
;
2880 prev_args
.num_spans
= 1;
2881 prev_args
.span
[0].start
= state
.selective_test_last_start
;
2882 prev_args
.span
[0].end
= state
.selective_test_last_end
;
2883 if (ataWriteSelectiveSelfTestLog(device
, selargs
, &data
, state
.num_sectors
, &prev_args
)) {
2884 PrintOut(LOG_CRIT
, "Device: %s, prepare %sTest failed\n", name
, testname
);
2887 uint64_t start
= selargs
.span
[0].start
, end
= selargs
.span
[0].end
;
2888 PrintOut(LOG_INFO
, "Device: %s, %s test span at LBA %" PRIu64
" - %" PRIu64
" (%" PRIu64
" sectors, %u%% - %u%% of disk).\n",
2889 name
, (selargs
.span
[0].mode
== SEL_NEXT
? "next" : "redo"),
2890 start
, end
, end
- start
+ 1,
2891 (unsigned)((100 * start
+ state
.num_sectors
/2) / state
.num_sectors
),
2892 (unsigned)((100 * end
+ state
.num_sectors
/2) / state
.num_sectors
));
2893 state
.selective_test_last_start
= start
;
2894 state
.selective_test_last_end
= end
;
2897 // execute the test, and return status
2898 int retval
= smartcommandhandler(device
, IMMEDIATE_OFFLINE
, dotest
, NULL
);
2900 PrintOut(LOG_CRIT
, "Device: %s, execute %sTest failed.\n", name
, testname
);
2904 // Report recent test start to do_disable_standby_check()
2905 // and force log of next test status
2906 if (testtype
== 'O')
2907 state
.offline_started
= true;
2909 state
.selftest_started
= true;
2911 PrintOut(LOG_INFO
, "Device: %s, starting scheduled %sTest.\n", name
, testname
);
2915 // Check pending sector count attribute values (-C, -U directives).
2916 static void check_pending(const dev_config
& cfg
, dev_state
& state
,
2917 unsigned char id
, bool increase_only
,
2918 const ata_smart_values
& smartval
,
2919 int mailtype
, const char * msg
)
2921 // Find attribute index
2922 int i
= ata_find_attr_index(id
, smartval
);
2923 if (!(i
>= 0 && ata_find_attr_index(id
, state
.smartval
) == i
))
2926 // No report if no sectors pending.
2927 uint64_t rawval
= ata_get_attr_raw_value(smartval
.vendor_attributes
[i
], cfg
.attribute_defs
);
2929 reset_warning_mail(cfg
, state
, mailtype
, "No more %s", msg
);
2933 // If attribute is not reset, report only sector count increases.
2934 uint64_t prev_rawval
= ata_get_attr_raw_value(state
.smartval
.vendor_attributes
[i
], cfg
.attribute_defs
);
2935 if (!(!increase_only
|| prev_rawval
< rawval
))
2939 std::string s
= strprintf("Device: %s, %" PRId64
" %s", cfg
.name
.c_str(), rawval
, msg
);
2940 if (prev_rawval
> 0 && rawval
!= prev_rawval
)
2941 s
+= strprintf(" (changed %+" PRId64
")", rawval
- prev_rawval
);
2943 PrintOut(LOG_CRIT
, "%s\n", s
.c_str());
2944 MailWarning(cfg
, state
, mailtype
, "%s", s
.c_str());
2945 state
.must_write
= true;
2948 // Format Temperature value
2949 static const char * fmt_temp(unsigned char x
, char (& buf
)[20])
2953 snprintf(buf
, sizeof(buf
), "%u", x
);
2957 // Check Temperature limits
2958 static void CheckTemperature(const dev_config
& cfg
, dev_state
& state
, unsigned char currtemp
, unsigned char triptemp
)
2960 if (!(0 < currtemp
&& currtemp
< 255)) {
2961 PrintOut(LOG_INFO
, "Device: %s, failed to read Temperature\n", cfg
.name
.c_str());
2965 // Update Max Temperature
2966 const char * minchg
= "", * maxchg
= "";
2967 if (currtemp
> state
.tempmax
) {
2970 state
.tempmax
= currtemp
;
2971 state
.must_write
= true;
2975 if (!state
.temperature
) {
2977 if (!state
.tempmin
|| currtemp
< state
.tempmin
)
2978 // Delay Min Temperature update by ~ 30 minutes.
2979 state
.tempmin_delay
= time(0) + CHECKTIME
- 60;
2980 PrintOut(LOG_INFO
, "Device: %s, initial Temperature is %d Celsius (Min/Max %s/%u%s)\n",
2981 cfg
.name
.c_str(), (int)currtemp
, fmt_temp(state
.tempmin
, buf
), state
.tempmax
, maxchg
);
2983 PrintOut(LOG_INFO
, " [trip Temperature is %d Celsius]\n", (int)triptemp
);
2984 state
.temperature
= currtemp
;
2987 if (state
.tempmin_delay
) {
2988 // End Min Temperature update delay if ...
2989 if ( (state
.tempmin
&& currtemp
> state
.tempmin
) // current temp exceeds recorded min,
2990 || (state
.tempmin_delay
<= time(0))) { // or delay time is over.
2991 state
.tempmin_delay
= 0;
2993 state
.tempmin
= 255;
2997 // Update Min Temperature
2998 if (!state
.tempmin_delay
&& currtemp
< state
.tempmin
) {
2999 state
.tempmin
= currtemp
;
3000 state
.must_write
= true;
3001 if (currtemp
!= state
.temperature
)
3006 if (cfg
.tempdiff
&& (*minchg
|| *maxchg
|| abs((int)currtemp
- (int)state
.temperature
) >= cfg
.tempdiff
)) {
3007 PrintOut(LOG_INFO
, "Device: %s, Temperature changed %+d Celsius to %u Celsius (Min/Max %s%s/%u%s)\n",
3008 cfg
.name
.c_str(), (int)currtemp
-(int)state
.temperature
, currtemp
, fmt_temp(state
.tempmin
, buf
), minchg
, state
.tempmax
, maxchg
);
3009 state
.temperature
= currtemp
;
3014 if (cfg
.tempcrit
&& currtemp
>= cfg
.tempcrit
) {
3015 PrintOut(LOG_CRIT
, "Device: %s, Temperature %u Celsius reached critical limit of %u Celsius (Min/Max %s%s/%u%s)\n",
3016 cfg
.name
.c_str(), currtemp
, cfg
.tempcrit
, fmt_temp(state
.tempmin
, buf
), minchg
, state
.tempmax
, maxchg
);
3017 MailWarning(cfg
, state
, 12, "Device: %s, Temperature %d Celsius reached critical limit of %u Celsius (Min/Max %s%s/%u%s)",
3018 cfg
.name
.c_str(), currtemp
, cfg
.tempcrit
, fmt_temp(state
.tempmin
, buf
), minchg
, state
.tempmax
, maxchg
);
3020 else if (cfg
.tempinfo
&& currtemp
>= cfg
.tempinfo
) {
3021 PrintOut(LOG_INFO
, "Device: %s, Temperature %u Celsius reached limit of %u Celsius (Min/Max %s%s/%u%s)\n",
3022 cfg
.name
.c_str(), currtemp
, cfg
.tempinfo
, fmt_temp(state
.tempmin
, buf
), minchg
, state
.tempmax
, maxchg
);
3024 else if (cfg
.tempcrit
) {
3025 unsigned char limit
= (cfg
.tempinfo
? cfg
.tempinfo
: cfg
.tempcrit
-5);
3026 if (currtemp
< limit
)
3027 reset_warning_mail(cfg
, state
, 12, "Temperature %u Celsius dropped below %u Celsius", currtemp
, limit
);
3031 // Check normalized and raw attribute values.
3032 static void check_attribute(const dev_config
& cfg
, dev_state
& state
,
3033 const ata_smart_attribute
& attr
,
3034 const ata_smart_attribute
& prev
,
3036 const ata_smart_threshold_entry
* thresholds
)
3038 // Check attribute and threshold
3039 ata_attr_state attrstate
= ata_get_attr_state(attr
, attridx
, thresholds
, cfg
.attribute_defs
);
3040 if (attrstate
== ATTRSTATE_NON_EXISTING
)
3043 // If requested, check for usage attributes that have failed.
3044 if ( cfg
.usagefailed
&& attrstate
== ATTRSTATE_FAILED_NOW
3045 && !cfg
.monitor_attr_flags
.is_set(attr
.id
, MONITOR_IGN_FAILUSE
)) {
3046 std::string attrname
= ata_get_smart_attr_name(attr
.id
, cfg
.attribute_defs
, cfg
.dev_rpm
);
3047 PrintOut(LOG_CRIT
, "Device: %s, Failed SMART usage Attribute: %d %s.\n", cfg
.name
.c_str(), attr
.id
, attrname
.c_str());
3048 MailWarning(cfg
, state
, 2, "Device: %s, Failed SMART usage Attribute: %d %s.", cfg
.name
.c_str(), attr
.id
, attrname
.c_str());
3049 state
.must_write
= true;
3052 // Return if we're not tracking this type of attribute
3053 bool prefail
= !!ATTRIBUTE_FLAGS_PREFAILURE(attr
.flags
);
3054 if (!( ( prefail
&& cfg
.prefail
)
3055 || (!prefail
&& cfg
.usage
)))
3058 // Return if '-I ID' was specified
3059 if (cfg
.monitor_attr_flags
.is_set(attr
.id
, MONITOR_IGNORE
))
3062 // Issue warning if they don't have the same ID in all structures.
3063 if (attr
.id
!= prev
.id
) {
3064 PrintOut(LOG_INFO
,"Device: %s, same Attribute has different ID numbers: %d = %d\n",
3065 cfg
.name
.c_str(), attr
.id
, prev
.id
);
3069 // Compare normalized values if valid.
3070 bool valchanged
= false;
3071 if (attrstate
> ATTRSTATE_NO_NORMVAL
) {
3072 if (attr
.current
!= prev
.current
)
3076 // Compare raw values if requested.
3077 bool rawchanged
= false;
3078 if (cfg
.monitor_attr_flags
.is_set(attr
.id
, MONITOR_RAW
)) {
3079 if ( ata_get_attr_raw_value(attr
, cfg
.attribute_defs
)
3080 != ata_get_attr_raw_value(prev
, cfg
.attribute_defs
))
3084 // Return if no change
3085 if (!(valchanged
|| rawchanged
))
3088 // Format value strings
3089 std::string currstr
, prevstr
;
3090 if (attrstate
== ATTRSTATE_NO_NORMVAL
) {
3091 // Print raw values only
3092 currstr
= strprintf("%s (Raw)",
3093 ata_format_attr_raw_value(attr
, cfg
.attribute_defs
).c_str());
3094 prevstr
= strprintf("%s (Raw)",
3095 ata_format_attr_raw_value(prev
, cfg
.attribute_defs
).c_str());
3097 else if (cfg
.monitor_attr_flags
.is_set(attr
.id
, MONITOR_RAW_PRINT
)) {
3098 // Print normalized and raw values
3099 currstr
= strprintf("%d [Raw %s]", attr
.current
,
3100 ata_format_attr_raw_value(attr
, cfg
.attribute_defs
).c_str());
3101 prevstr
= strprintf("%d [Raw %s]", prev
.current
,
3102 ata_format_attr_raw_value(prev
, cfg
.attribute_defs
).c_str());
3105 // Print normalized values only
3106 currstr
= strprintf("%d", attr
.current
);
3107 prevstr
= strprintf("%d", prev
.current
);
3111 std::string msg
= strprintf("Device: %s, SMART %s Attribute: %d %s changed from %s to %s",
3112 cfg
.name
.c_str(), (prefail
? "Prefailure" : "Usage"), attr
.id
,
3113 ata_get_smart_attr_name(attr
.id
, cfg
.attribute_defs
, cfg
.dev_rpm
).c_str(),
3114 prevstr
.c_str(), currstr
.c_str());
3116 // Report this change as critical ?
3117 if ( (valchanged
&& cfg
.monitor_attr_flags
.is_set(attr
.id
, MONITOR_AS_CRIT
))
3118 || (rawchanged
&& cfg
.monitor_attr_flags
.is_set(attr
.id
, MONITOR_RAW_AS_CRIT
))) {
3119 PrintOut(LOG_CRIT
, "%s\n", msg
.c_str());
3120 MailWarning(cfg
, state
, 2, "%s", msg
.c_str());
3123 PrintOut(LOG_INFO
, "%s\n", msg
.c_str());
3125 state
.must_write
= true;
3129 static int ATACheckDevice(const dev_config
& cfg
, dev_state
& state
, ata_device
* atadev
,
3130 bool firstpass
, bool allow_selftests
)
3132 const char * name
= cfg
.name
.c_str();
3134 // If user has asked, test the email warning system
3136 MailWarning(cfg
, state
, 0, "TEST EMAIL from smartd for device: %s", name
);
3138 // User may have requested (with the -n Directive) to leave the disk
3139 // alone if it is in idle or standby mode. In this case check the
3140 // power mode first before opening the device for full access,
3141 // and exit without check if disk is reported in standby.
3142 if (cfg
.powermode
&& !state
.powermodefail
) {
3143 // Note that 'is_powered_down()' handles opening the device itself, and
3144 // can be used before calling 'open()' (that's the whole point of 'is_powered_down()'!).
3145 if (atadev
->is_powered_down())
3147 // skip at most powerskipmax checks
3148 if (!cfg
.powerskipmax
|| state
.powerskipcnt
<cfg
.powerskipmax
) {
3149 // report first only except if state has changed, avoid waking up system disk
3150 if ((!state
.powerskipcnt
|| state
.lastpowermodeskipped
!= -1) && !cfg
.powerquiet
) {
3151 PrintOut(LOG_INFO
, "Device: %s, is in %s mode, suspending checks\n", name
, "STANDBY (OS)");
3152 state
.lastpowermodeskipped
= -1;
3154 state
.powerskipcnt
++;
3160 // if we can't open device, fail gracefully rather than hard --
3161 // perhaps the next time around we'll be able to open it. ATAPI
3162 // cd/dvd devices will hang awaiting media if O_NONBLOCK is not
3163 // given (see linux cdrom driver).
3164 if (!atadev
->open()) {
3165 PrintOut(LOG_INFO
, "Device: %s, open() failed: %s\n", name
, atadev
->get_errmsg());
3166 MailWarning(cfg
, state
, 9, "Device: %s, unable to open device", name
);
3170 PrintOut(LOG_INFO
,"Device: %s, opened ATA device\n", name
);
3171 reset_warning_mail(cfg
, state
, 9, "open device worked again");
3173 // user may have requested (with the -n Directive) to leave the disk
3174 // alone if it is in idle or sleeping mode. In this case check the
3175 // power mode and exit without check if needed
3176 if (cfg
.powermode
&& !state
.powermodefail
) {
3177 int dontcheck
=0, powermode
=ataCheckPowerMode(atadev
);
3178 const char * mode
= 0;
3179 if (0 <= powermode
&& powermode
< 0xff) {
3180 // wait for possible spin up and check again
3183 powermode2
= ataCheckPowerMode(atadev
);
3184 if (powermode2
> powermode
)
3185 PrintOut(LOG_INFO
, "Device: %s, CHECK POWER STATUS spins up disk (0x%02x -> 0x%02x)\n", name
, powermode
, powermode2
);
3186 powermode
= powermode2
;
3193 if (cfg
.powermode
>=1)
3199 if (cfg
.powermode
>=2)
3205 if (cfg
.powermode
>=2)
3211 if (cfg
.powermode
>=3)
3217 if (cfg
.powermode
>=3)
3223 if (cfg
.powermode
>=3)
3229 if (cfg
.powermode
>=3)
3238 mode
="ACTIVE or IDLE";
3242 PrintOut(LOG_CRIT
, "Device: %s, CHECK POWER STATUS returned %d, not ATA compliant, ignoring -n Directive\n",
3244 state
.powermodefail
= true;
3248 // if we are going to skip a check, return now
3250 // skip at most powerskipmax checks
3251 if (!cfg
.powerskipmax
|| state
.powerskipcnt
<cfg
.powerskipmax
) {
3252 CloseDevice(atadev
, name
);
3253 // report first only except if state has changed, avoid waking up system disk
3254 if ((!state
.powerskipcnt
|| state
.lastpowermodeskipped
!= powermode
) && !cfg
.powerquiet
) {
3255 PrintOut(LOG_INFO
, "Device: %s, is in %s mode, suspending checks\n", name
, mode
);
3256 state
.lastpowermodeskipped
= powermode
;
3258 state
.powerskipcnt
++;
3262 PrintOut(LOG_INFO
, "Device: %s, %s mode ignored due to reached limit of skipped checks (%d check%s skipped)\n",
3263 name
, mode
, state
.powerskipcnt
, (state
.powerskipcnt
==1?"":"s"));
3265 state
.powerskipcnt
= 0;
3266 state
.tempmin_delay
= time(0) + CHECKTIME
- 60; // Delay Min Temperature update
3268 else if (state
.powerskipcnt
) {
3269 PrintOut(LOG_INFO
, "Device: %s, is back in %s mode, resuming checks (%d check%s skipped)\n",
3270 name
, mode
, state
.powerskipcnt
, (state
.powerskipcnt
==1?"":"s"));
3271 state
.powerskipcnt
= 0;
3272 state
.tempmin_delay
= time(0) + CHECKTIME
- 60; // Delay Min Temperature update
3276 // check smart status
3277 if (cfg
.smartcheck
) {
3278 int status
=ataSmartStatus2(atadev
);
3280 PrintOut(LOG_INFO
,"Device: %s, not capable of SMART self-check\n",name
);
3281 MailWarning(cfg
, state
, 5, "Device: %s, not capable of SMART self-check", name
);
3282 state
.must_write
= true;
3284 else if (status
==1){
3285 PrintOut(LOG_CRIT
, "Device: %s, FAILED SMART self-check. BACK UP DATA NOW!\n", name
);
3286 MailWarning(cfg
, state
, 1, "Device: %s, FAILED SMART self-check. BACK UP DATA NOW!", name
);
3287 state
.must_write
= true;
3291 // Check everything that depends upon SMART Data (eg, Attribute values)
3292 if ( cfg
.usagefailed
|| cfg
.prefail
|| cfg
.usage
3293 || cfg
.curr_pending_id
|| cfg
.offl_pending_id
3294 || cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
3295 || cfg
.selftest
|| cfg
.offlinests
|| cfg
.selfteststs
) {
3297 // Read current attribute values.
3298 ata_smart_values curval
;
3299 if (ataReadSmartValues(atadev
, &curval
)){
3300 PrintOut(LOG_CRIT
, "Device: %s, failed to read SMART Attribute Data\n", name
);
3301 MailWarning(cfg
, state
, 6, "Device: %s, failed to read SMART Attribute Data", name
);
3302 state
.must_write
= true;
3305 reset_warning_mail(cfg
, state
, 6, "read SMART Attribute Data worked again");
3307 // look for current or offline pending sectors
3308 if (cfg
.curr_pending_id
)
3309 check_pending(cfg
, state
, cfg
.curr_pending_id
, cfg
.curr_pending_incr
, curval
, 10,
3310 (!cfg
.curr_pending_incr
? "Currently unreadable (pending) sectors"
3311 : "Total unreadable (pending) sectors" ));
3313 if (cfg
.offl_pending_id
)
3314 check_pending(cfg
, state
, cfg
.offl_pending_id
, cfg
.offl_pending_incr
, curval
, 11,
3315 (!cfg
.offl_pending_incr
? "Offline uncorrectable sectors"
3316 : "Total offline uncorrectable sectors"));
3318 // check temperature limits
3319 if (cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
)
3320 CheckTemperature(cfg
, state
, ata_return_temperature_value(&curval
, cfg
.attribute_defs
), 0);
3322 // look for failed usage attributes, or track usage or prefail attributes
3323 if (cfg
.usagefailed
|| cfg
.prefail
|| cfg
.usage
) {
3324 for (int i
= 0; i
< NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
3325 check_attribute(cfg
, state
,
3326 curval
.vendor_attributes
[i
],
3327 state
.smartval
.vendor_attributes
[i
],
3328 i
, state
.smartthres
.thres_entries
);
3332 // Log changes of offline data collection status
3333 if (cfg
.offlinests
) {
3334 if ( curval
.offline_data_collection_status
3335 != state
.smartval
.offline_data_collection_status
3336 || state
.offline_started
// test was started in previous call
3337 || (firstpass
&& (debugmode
|| (curval
.offline_data_collection_status
& 0x7d))))
3338 log_offline_data_coll_status(name
, curval
.offline_data_collection_status
);
3341 // Log changes of self-test execution status
3342 if (cfg
.selfteststs
) {
3343 if ( curval
.self_test_exec_status
!= state
.smartval
.self_test_exec_status
3344 || state
.selftest_started
// test was started in previous call
3345 || (firstpass
&& (debugmode
|| curval
.self_test_exec_status
!= 0x00)))
3346 log_self_test_exec_status(name
, curval
.self_test_exec_status
);
3349 // Save the new values for the next time around
3350 state
.smartval
= curval
;
3353 state
.offline_started
= state
.selftest_started
= false;
3355 // check if number of selftest errors has increased (note: may also DECREASE)
3357 CheckSelfTestLogs(cfg
, state
, SelfTestErrorCount(atadev
, name
, cfg
.firmwarebugs
));
3359 // check if number of ATA errors has increased
3360 if (cfg
.errorlog
|| cfg
.xerrorlog
) {
3362 int errcnt1
= -1, errcnt2
= -1;
3364 errcnt1
= read_ata_error_count(atadev
, name
, cfg
.firmwarebugs
, false);
3366 errcnt2
= read_ata_error_count(atadev
, name
, cfg
.firmwarebugs
, true);
3368 // new number of errors is max of both logs
3369 int newc
= (errcnt1
>= errcnt2
? errcnt1
: errcnt2
);
3371 // did command fail?
3373 // lack of PrintOut here is INTENTIONAL
3374 MailWarning(cfg
, state
, 7, "Device: %s, Read SMART Error Log Failed", name
);
3376 // has error count increased?
3377 int oldc
= state
.ataerrorcount
;
3379 PrintOut(LOG_CRIT
, "Device: %s, ATA error count increased from %d to %d\n",
3381 MailWarning(cfg
, state
, 4, "Device: %s, ATA error count increased from %d to %d",
3383 state
.must_write
= true;
3387 state
.ataerrorcount
=newc
;
3390 // if the user has asked, and device is capable (or we're not yet
3391 // sure) check whether a self test should be done now.
3392 if (allow_selftests
&& !cfg
.test_regex
.empty()) {
3393 char testtype
= next_scheduled_test(cfg
, state
, false/*!scsi*/);
3395 DoATASelfTest(cfg
, state
, atadev
, testtype
);
3398 // Don't leave device open -- the OS/user may want to access it
3399 // before the next smartd cycle!
3400 CloseDevice(atadev
, name
);
3402 // Copy ATA attribute values to persistent state
3403 state
.update_persistent_state();
3408 static int SCSICheckDevice(const dev_config
& cfg
, dev_state
& state
, scsi_device
* scsidev
, bool allow_selftests
)
3410 const char * name
= cfg
.name
.c_str();
3412 // If the user has asked for it, test the email warning system
3414 MailWarning(cfg
, state
, 0, "TEST EMAIL from smartd for device: %s", name
);
3416 // if we can't open device, fail gracefully rather than hard --
3417 // perhaps the next time around we'll be able to open it
3418 if (!scsidev
->open()) {
3419 PrintOut(LOG_INFO
, "Device: %s, open() failed: %s\n", name
, scsidev
->get_errmsg());
3420 MailWarning(cfg
, state
, 9, "Device: %s, unable to open device", name
);
3422 } else if (debugmode
)
3423 PrintOut(LOG_INFO
,"Device: %s, opened SCSI device\n", name
);
3424 reset_warning_mail(cfg
, state
, 9, "open device worked again");
3426 UINT8 asc
= 0, ascq
= 0;
3427 UINT8 currenttemp
= 0, triptemp
= 0;
3428 if (!state
.SuppressReport
) {
3429 if (scsiCheckIE(scsidev
, state
.SmartPageSupported
, state
.TempPageSupported
,
3430 &asc
, &ascq
, ¤ttemp
, &triptemp
)) {
3431 PrintOut(LOG_INFO
, "Device: %s, failed to read SMART values\n",
3433 MailWarning(cfg
, state
, 6, "Device: %s, failed to read SMART values", name
);
3434 state
.SuppressReport
= 1;
3438 const char * cp
= scsiGetIEString(asc
, ascq
);
3440 PrintOut(LOG_CRIT
, "Device: %s, SMART Failure: %s\n", name
, cp
);
3441 MailWarning(cfg
, state
, 1,"Device: %s, SMART Failure: %s", name
, cp
);
3442 } else if (asc
== 4 && ascq
== 9) {
3443 PrintOut(LOG_INFO
,"Device: %s, self-test in progress\n", name
);
3444 } else if (debugmode
)
3445 PrintOut(LOG_INFO
,"Device: %s, non-SMART asc,ascq: %d,%d\n",
3446 name
, (int)asc
, (int)ascq
);
3447 } else if (debugmode
)
3448 PrintOut(LOG_INFO
,"Device: %s, SMART health: passed\n", name
);
3450 // check temperature limits
3451 if (cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
|| !cfg
.attrlog_file
.empty())
3452 CheckTemperature(cfg
, state
, currenttemp
, triptemp
);
3454 // check if number of selftest errors has increased (note: may also DECREASE)
3456 CheckSelfTestLogs(cfg
, state
, scsiCountFailedSelfTests(scsidev
, 0));
3458 if (allow_selftests
&& !cfg
.test_regex
.empty()) {
3459 char testtype
= next_scheduled_test(cfg
, state
, true/*scsi*/);
3461 DoSCSISelfTest(cfg
, state
, scsidev
, testtype
);
3463 if (!cfg
.attrlog_file
.empty()){
3464 // saving error counters to state
3466 if (state
.ReadECounterPageSupported
&& (0 == scsiLogSense(scsidev
,
3467 READ_ERROR_COUNTER_LPAGE
, 0, tBuf
, sizeof(tBuf
), 0))) {
3468 scsiDecodeErrCounterPage(tBuf
, &state
.scsi_error_counters
[0].errCounter
);
3469 state
.scsi_error_counters
[0].found
=1;
3471 if (state
.WriteECounterPageSupported
&& (0 == scsiLogSense(scsidev
,
3472 WRITE_ERROR_COUNTER_LPAGE
, 0, tBuf
, sizeof(tBuf
), 0))) {
3473 scsiDecodeErrCounterPage(tBuf
, &state
.scsi_error_counters
[1].errCounter
);
3474 state
.scsi_error_counters
[1].found
=1;
3476 if (state
.VerifyECounterPageSupported
&& (0 == scsiLogSense(scsidev
,
3477 VERIFY_ERROR_COUNTER_LPAGE
, 0, tBuf
, sizeof(tBuf
), 0))) {
3478 scsiDecodeErrCounterPage(tBuf
, &state
.scsi_error_counters
[2].errCounter
);
3479 state
.scsi_error_counters
[2].found
=1;
3481 if (state
.NonMediumErrorPageSupported
&& (0 == scsiLogSense(scsidev
,
3482 NON_MEDIUM_ERROR_LPAGE
, 0, tBuf
, sizeof(tBuf
), 0))) {
3483 scsiDecodeNonMediumErrPage(tBuf
, &state
.scsi_nonmedium_error
.nme
);
3484 state
.scsi_nonmedium_error
.found
=1;
3487 CloseDevice(scsidev
, name
);
3491 static int NVMeCheckDevice(const dev_config
& cfg
, dev_state
& state
, nvme_device
* nvmedev
)
3493 const char * name
= cfg
.name
.c_str();
3495 // TODO: Use common open function for ATA/SCSI/NVMe
3496 // If user has asked, test the email warning system
3498 MailWarning(cfg
, state
, 0, "TEST EMAIL from smartd for device: %s", name
);
3500 if (!nvmedev
->open()) {
3501 PrintOut(LOG_INFO
, "Device: %s, open() failed: %s\n", name
, nvmedev
->get_errmsg());
3502 MailWarning(cfg
, state
, 9, "Device: %s, unable to open device", name
);
3506 PrintOut(LOG_INFO
,"Device: %s, opened NVMe device\n", name
);
3507 reset_warning_mail(cfg
, state
, 9, "open device worked again");
3509 // Read SMART/Health log
3510 nvme_smart_log smart_log
;
3511 if (!nvme_read_smart_log(nvmedev
, smart_log
)) {
3512 PrintOut(LOG_INFO
, "Device: %s, failed to read NVMe SMART/Health Information\n", name
);
3513 MailWarning(cfg
, state
, 6, "Device: %s, failed to read NVMe SMART/Health Information", name
);
3514 state
.must_write
= true;
3518 // Check Critical Warning bits
3519 if (cfg
.smartcheck
&& smart_log
.critical_warning
) {
3520 unsigned char w
= smart_log
.critical_warning
;
3522 static const char * const wnames
[] =
3523 {"LowSpare", "Temperature", "Reliability", "R/O", "VolMemBackup"};
3525 for (unsigned b
= 0, cnt
= 0; b
< 8 ; b
++) {
3526 if (!(w
& (1 << b
)))
3531 msg
+= "..."; break;
3533 if (b
>= sizeof(wnames
)/sizeof(wnames
[0])) {
3534 msg
+= "*Unknown*"; break;
3539 PrintOut(LOG_CRIT
, "Device: %s, Critical Warning (0x%02x): %s\n", name
, w
, msg
.c_str());
3540 MailWarning(cfg
, state
, 1, "Device: %s, Critical Warning (0x%02x): %s", name
, w
, msg
.c_str());
3541 state
.must_write
= true;
3544 // Check temperature limits
3545 if (cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
) {
3546 int k
= nvme_get_max_temp_kelvin(smart_log
);
3547 // Convert Kelvin to positive Celsius (TODO: Allow negative temperatures)
3553 CheckTemperature(cfg
, state
, c
, 0);
3556 // Check if number of errors has increased
3557 if (cfg
.errorlog
|| cfg
.xerrorlog
) {
3558 uint64_t oldcnt
= state
.nvme_err_log_entries
;
3559 uint64_t newcnt
= le128_to_uint64(smart_log
.num_err_log_entries
);
3560 if (newcnt
> oldcnt
) {
3561 PrintOut(LOG_CRIT
, "Device: %s, number of Error Log entries increased from %" PRIu64
" to %" PRIu64
"\n",
3562 name
, oldcnt
, newcnt
);
3563 MailWarning(cfg
, state
, 4, "Device: %s, number of Error Log entries increased from %" PRIu64
" to %" PRIu64
,
3564 name
, oldcnt
, newcnt
);
3565 state
.must_write
= true;
3567 state
.nvme_err_log_entries
= newcnt
;
3570 CloseDevice(nvmedev
, name
);
3574 // 0=not used, 1=not disabled, 2=disable rejected by OS, 3=disabled
3575 static int standby_disable_state
= 0;
3577 static void init_disable_standby_check(dev_config_vector
& configs
)
3579 // Check for '-l offlinests,ns' or '-l selfteststs,ns' directives
3580 bool sts1
= false, sts2
= false;
3581 for (unsigned i
= 0; i
< configs
.size() && !(sts1
|| sts2
); i
++) {
3582 const dev_config
& cfg
= configs
.at(i
);
3583 if (cfg
.offlinests_ns
)
3585 if (cfg
.selfteststs_ns
)
3589 // Check for support of disable auto standby
3590 // Reenable standby if smartd.conf was reread
3591 if (sts1
|| sts2
|| standby_disable_state
== 3) {
3592 if (!smi()->disable_system_auto_standby(false)) {
3593 if (standby_disable_state
== 3)
3594 PrintOut(LOG_CRIT
, "System auto standby enable failed: %s\n", smi()->get_errmsg());
3596 PrintOut(LOG_INFO
, "Disable auto standby not supported, ignoring ',ns' from %s%s%s\n",
3597 (sts1
? "-l offlinests,ns" : ""), (sts1
&& sts2
? " and " : ""), (sts2
? "-l selfteststs,ns" : ""));
3598 sts1
= sts2
= false;
3603 standby_disable_state
= (sts1
|| sts2
? 1 : 0);
3606 static void do_disable_standby_check(const dev_config_vector
& configs
, const dev_state_vector
& states
)
3608 if (!standby_disable_state
)
3611 // Check for just started or still running self-tests
3612 bool running
= false;
3613 for (unsigned i
= 0; i
< configs
.size() && !running
; i
++) {
3614 const dev_config
& cfg
= configs
.at(i
); const dev_state
& state
= states
.at(i
);
3616 if ( ( cfg
.offlinests_ns
3617 && (state
.offline_started
||
3618 is_offl_coll_in_progress(state
.smartval
.offline_data_collection_status
)))
3619 || ( cfg
.selfteststs_ns
3620 && (state
.selftest_started
||
3621 is_self_test_in_progress(state
.smartval
.self_test_exec_status
))) )
3623 // state.offline/selftest_started will be reset after next logging of test status
3626 // Disable/enable auto standby and log state changes
3628 if (standby_disable_state
!= 1) {
3629 if (!smi()->disable_system_auto_standby(false))
3630 PrintOut(LOG_CRIT
, "Self-test(s) completed, system auto standby enable failed: %s\n",
3631 smi()->get_errmsg());
3633 PrintOut(LOG_INFO
, "Self-test(s) completed, system auto standby enabled\n");
3634 standby_disable_state
= 1;
3637 else if (!smi()->disable_system_auto_standby(true)) {
3638 if (standby_disable_state
!= 2) {
3639 PrintOut(LOG_INFO
, "Self-test(s) in progress, system auto standby disable rejected: %s\n",
3640 smi()->get_errmsg());
3641 standby_disable_state
= 2;
3645 if (standby_disable_state
!= 3) {
3646 PrintOut(LOG_INFO
, "Self-test(s) in progress, system auto standby disabled\n");
3647 standby_disable_state
= 3;
3652 // Checks the SMART status of all ATA and SCSI devices
3653 static void CheckDevicesOnce(const dev_config_vector
& configs
, dev_state_vector
& states
,
3654 smart_device_list
& devices
, bool firstpass
, bool allow_selftests
)
3656 for (unsigned i
= 0; i
< configs
.size(); i
++) {
3657 const dev_config
& cfg
= configs
.at(i
);
3658 dev_state
& state
= states
.at(i
);
3659 smart_device
* dev
= devices
.at(i
);
3661 ATACheckDevice(cfg
, state
, dev
->to_ata(), firstpass
, allow_selftests
);
3662 else if (dev
->is_scsi())
3663 SCSICheckDevice(cfg
, state
, dev
->to_scsi(), allow_selftests
);
3664 else if (dev
->is_nvme())
3665 NVMeCheckDevice(cfg
, state
, dev
->to_nvme());
3668 do_disable_standby_check(configs
, states
);
3671 // Set if Initialize() was called
3672 static bool is_initialized
= false;
3674 // Does initialization right after fork to daemon mode
3675 static void Initialize(time_t *wakeuptime
)
3677 // Call Goodbye() on exit
3678 is_initialized
= true;
3684 // install signal handlers. On Solaris, can't use signal() because
3685 // it resets the handler to SIG_DFL after each call. So use sigset()
3686 // instead. So SIGNALFN()==signal() or SIGNALFN()==sigset().
3688 // normal and abnormal exit
3689 if (SIGNALFN(SIGTERM
, sighandler
)==SIG_IGN
)
3690 SIGNALFN(SIGTERM
, SIG_IGN
);
3691 if (SIGNALFN(SIGQUIT
, sighandler
)==SIG_IGN
)
3692 SIGNALFN(SIGQUIT
, SIG_IGN
);
3694 // in debug mode, <CONTROL-C> ==> HUP
3695 if (SIGNALFN(SIGINT
, debugmode
?HUPhandler
:sighandler
)==SIG_IGN
)
3696 SIGNALFN(SIGINT
, SIG_IGN
);
3698 // Catch HUP and USR1
3699 if (SIGNALFN(SIGHUP
, HUPhandler
)==SIG_IGN
)
3700 SIGNALFN(SIGHUP
, SIG_IGN
);
3701 if (SIGNALFN(SIGUSR1
, USR1handler
)==SIG_IGN
)
3702 SIGNALFN(SIGUSR1
, SIG_IGN
);
3704 if (SIGNALFN(SIGUSR2
, USR2handler
)==SIG_IGN
)
3705 SIGNALFN(SIGUSR2
, SIG_IGN
);
3708 // initialize wakeup time to CURRENT time
3709 *wakeuptime
=time(NULL
);
3715 // Toggle debug mode implemented for native windows only
3716 // (there is no easy way to reopen tty on *nix)
3717 static void ToggleDebugMode()
3720 PrintOut(LOG_INFO
,"Signal USR2 - enabling debug mode\n");
3721 if (!daemon_enable_console("smartd [Debug]")) {
3723 daemon_signal(SIGINT
, HUPhandler
);
3724 PrintOut(LOG_INFO
,"smartd debug mode enabled, PID=%d\n", getpid());
3727 PrintOut(LOG_INFO
,"enable console failed\n");
3729 else if (debugmode
== 1) {
3730 daemon_disable_console();
3732 daemon_signal(SIGINT
, sighandler
);
3733 PrintOut(LOG_INFO
,"Signal USR2 - debug mode disabled\n");
3736 PrintOut(LOG_INFO
,"Signal USR2 - debug mode %d not changed\n", debugmode
);
3740 static time_t dosleep(time_t wakeuptime
, bool & sigwakeup
)
3742 // If past wake-up-time, compute next wake-up-time
3743 time_t timenow
=time(NULL
);
3744 while (wakeuptime
<=timenow
){
3745 int intervals
=1+(timenow
-wakeuptime
)/checktime
;
3746 wakeuptime
+=intervals
*checktime
;
3749 // sleep until we catch SIGUSR1 or have completed sleeping
3751 while (timenow
< wakeuptime
+addtime
&& !caughtsigUSR1
&& !caughtsigHUP
&& !caughtsigEXIT
) {
3753 // protect user again system clock being adjusted backwards
3754 if (wakeuptime
>timenow
+checktime
){
3755 PrintOut(LOG_CRIT
, "System clock time adjusted to the past. Resetting next wakeup time.\n");
3756 wakeuptime
=timenow
+checktime
;
3759 // Exit sleep when time interval has expired or a signal is received
3760 sleep(wakeuptime
+addtime
-timenow
);
3763 // toggle debug mode?
3764 if (caughtsigUSR2
) {
3772 // Actual sleep time too long?
3773 if (!addtime
&& timenow
> wakeuptime
+60) {
3775 PrintOut(LOG_INFO
, "Sleep time was %d seconds too long, assuming wakeup from standby mode.\n",
3776 (int)(timenow
-wakeuptime
));
3777 // Wait another 20 seconds to avoid I/O errors during disk spin-up
3778 addtime
= timenow
-wakeuptime
+20;
3779 // Use next wake-up-time if close
3780 int nextcheck
= checktime
- addtime
% checktime
;
3781 if (nextcheck
<= 20)
3782 addtime
+= nextcheck
;
3786 // if we caught a SIGUSR1 then print message and clear signal
3788 PrintOut(LOG_INFO
,"Signal USR1 - checking devices now rather than in %d seconds.\n",
3789 wakeuptime
-timenow
>0?(int)(wakeuptime
-timenow
):0);
3794 // return adjusted wakeuptime
3798 // Print out a list of valid arguments for the Directive d
3799 static void printoutvaliddirectiveargs(int priority
, char d
)
3803 PrintOut(priority
, "never[,N][,q], sleep[,N][,q], standby[,N][,q], idle[,N][,q]");
3806 PrintOut(priority
, "valid_regular_expression");
3809 PrintOut(priority
, "%s", smi()->get_valid_dev_types_str().c_str());
3812 PrintOut(priority
, "normal, permissive");
3816 PrintOut(priority
, "on, off");
3819 PrintOut(priority
, "error, selftest");
3822 PrintOut(priority
, "\"once\", \"daily\", \"diminishing\", \"test\", \"exec\"");
3825 PrintOut(priority
, "\n%s\n", create_vendor_attribute_arg_list().c_str());
3828 PrintOut(priority
, "use, ignore, show, showall");
3831 PrintOut(priority
, "%s", get_valid_firmwarebug_args());
3834 PrintOut(priority
, "aam,[N|off], apm,[N|off], lookahead,[on|off], "
3835 "security-freeze, standby,[N|off], wcache,[on|off]");
3840 // exits with an error message, or returns integer value of token
3841 static int GetInteger(const char *arg
, const char *name
, const char *token
, int lineno
, const char *cfgfile
,
3842 int min
, int max
, char * suffix
= 0)
3844 // make sure argument is there
3846 PrintOut(LOG_CRIT
,"File %s line %d (drive %s): Directive: %s takes integer argument from %d to %d.\n",
3847 cfgfile
, lineno
, name
, token
, min
, max
);
3851 // get argument value (base 10), check that it's integer, and in-range
3853 int val
= strtol(arg
,&endptr
,10);
3855 // optional suffix present?
3857 if (!strcmp(endptr
, suffix
))
3858 endptr
+= strlen(suffix
);
3863 if (!(!*endptr
&& min
<= val
&& val
<= max
)) {
3864 PrintOut(LOG_CRIT
,"File %s line %d (drive %s): Directive: %s has argument: %s; needs integer from %d to %d.\n",
3865 cfgfile
, lineno
, name
, token
, arg
, min
, max
);
3869 // all is well; return value
3874 // Get 1-3 small integer(s) for '-W' directive
3875 static int Get3Integers(const char *arg
, const char *name
, const char *token
, int lineno
, const char *cfgfile
,
3876 unsigned char *val1
, unsigned char *val2
, unsigned char *val3
)
3878 unsigned v1
= 0, v2
= 0, v3
= 0;
3879 int n1
= -1, n2
= -1, n3
= -1, len
;
3881 PrintOut(LOG_CRIT
,"File %s line %d (drive %s): Directive: %s takes 1-3 integer argument(s) from 0 to 255.\n",
3882 cfgfile
, lineno
, name
, token
);
3887 if (!( sscanf(arg
, "%u%n,%u%n,%u%n", &v1
, &n1
, &v2
, &n2
, &v3
, &n3
) >= 1
3888 && (n1
== len
|| n2
== len
|| n3
== len
) && v1
<= 255 && v2
<= 255 && v3
<= 255)) {
3889 PrintOut(LOG_CRIT
,"File %s line %d (drive %s): Directive: %s has argument: %s; needs 1-3 integer(s) from 0 to 255.\n",
3890 cfgfile
, lineno
, name
, token
, arg
);
3893 *val1
= (unsigned char)v1
; *val2
= (unsigned char)v2
; *val3
= (unsigned char)v3
;
3900 // Concatenate strtok() results if quoted with "..."
3901 static const char * strtok_dequote(const char * delimiters
)
3903 const char * t
= strtok(0, delimiters
);
3904 if (!t
|| t
[0] != '"')
3907 static std::string token
;
3910 t
= strtok(0, delimiters
);
3914 int len
= strlen(t
);
3915 if (t
[len
-1] == '"') {
3916 token
+= std::string(t
, len
-1);
3921 return token
.c_str();
3927 // This function returns 1 if it has correctly parsed one token (and
3928 // any arguments), else zero if no tokens remain. It returns -1 if an
3929 // error was encountered.
3930 static int ParseToken(char * token
, dev_config
& cfg
, smart_devtype_list
& scan_types
)
3933 const char * name
= cfg
.name
.c_str();
3934 int lineno
=cfg
.lineno
;
3935 const char *delim
= " \n\t";
3938 const char *arg
= 0;
3940 // is the rest of the line a comment
3944 // is the token not recognized?
3945 if (*token
!='-' || strlen(token
)!=2) {
3946 PrintOut(LOG_CRIT
,"File %s line %d (drive %s): unknown Directive: %s\n",
3947 configfile
, lineno
, name
, token
);
3948 PrintOut(LOG_CRIT
, "Run smartd -D to print a list of valid Directives.\n");
3952 // token we will be parsing:
3955 // parse the token and swallow its argument
3957 char plus
[] = "+", excl
[] = "!";
3961 // monitor current pending sector count (default 197)
3962 if ((val
= GetInteger(arg
=strtok(NULL
,delim
), name
, token
, lineno
, configfile
, 0, 255, plus
)) < 0)
3964 cfg
.curr_pending_id
= (unsigned char)val
;
3965 cfg
.curr_pending_incr
= (*plus
== '+');
3966 cfg
.curr_pending_set
= true;
3969 // monitor offline uncorrectable sectors (default 198)
3970 if ((val
= GetInteger(arg
=strtok(NULL
,delim
), name
, token
, lineno
, configfile
, 0, 255, plus
)) < 0)
3972 cfg
.offl_pending_id
= (unsigned char)val
;
3973 cfg
.offl_pending_incr
= (*plus
== '+');
3974 cfg
.offl_pending_set
= true;
3977 // Set tolerance level for SMART command failures
3978 if ((arg
= strtok(NULL
, delim
)) == NULL
) {
3980 } else if (!strcmp(arg
, "normal")) {
3981 // Normal mode: exit on failure of a mandatory S.M.A.R.T. command, but
3982 // not on failure of an optional S.M.A.R.T. command.
3983 // This is the default so we don't need to actually do anything here.
3984 cfg
.permissive
= false;
3985 } else if (!strcmp(arg
, "permissive")) {
3986 // Permissive mode; ignore errors from Mandatory SMART commands
3987 cfg
.permissive
= true;
3993 // specify the device type
3994 if ((arg
= strtok(NULL
, delim
)) == NULL
) {
3996 } else if (!strcmp(arg
, "ignore")) {
3998 } else if (!strcmp(arg
, "removable")) {
3999 cfg
.removable
= true;
4000 } else if (!strcmp(arg
, "auto")) {
4005 scan_types
.push_back(arg
);
4010 if (!(arg
= strtok(0, delim
)))
4012 else if (!parse_firmwarebug_def(arg
, cfg
.firmwarebugs
))
4016 // check SMART status
4017 cfg
.smartcheck
= true;
4020 // check for failure of usage attributes
4021 cfg
.usagefailed
= true;
4024 // track changes in all vendor attributes
4029 // track changes in prefail vendor attributes
4033 // track changes in usage vendor attributes
4037 // track changes in SMART logs
4038 if ((arg
= strtok(NULL
, delim
)) == NULL
) {
4040 } else if (!strcmp(arg
, "selftest")) {
4041 // track changes in self-test log
4042 cfg
.selftest
= true;
4043 } else if (!strcmp(arg
, "error")) {
4044 // track changes in ATA error log
4045 cfg
.errorlog
= true;
4046 } else if (!strcmp(arg
, "xerror")) {
4047 // track changes in Extended Comprehensive SMART error log
4048 cfg
.xerrorlog
= true;
4049 } else if (!strcmp(arg
, "offlinests")) {
4050 // track changes in offline data collection status
4051 cfg
.offlinests
= true;
4052 } else if (!strcmp(arg
, "offlinests,ns")) {
4053 // track changes in offline data collection status, disable auto standby
4054 cfg
.offlinests
= cfg
.offlinests_ns
= true;
4055 } else if (!strcmp(arg
, "selfteststs")) {
4056 // track changes in self-test execution status
4057 cfg
.selfteststs
= true;
4058 } else if (!strcmp(arg
, "selfteststs,ns")) {
4059 // track changes in self-test execution status, disable auto standby
4060 cfg
.selfteststs
= cfg
.selfteststs_ns
= true;
4061 } else if (!strncmp(arg
, "scterc,", sizeof("scterc,")-1)) {
4062 // set SCT Error Recovery Control
4063 unsigned rt
= ~0, wt
= ~0; int nc
= -1;
4064 sscanf(arg
,"scterc,%u,%u%n", &rt
, &wt
, &nc
);
4065 if (nc
== (int)strlen(arg
) && rt
<= 999 && wt
<= 999) {
4066 cfg
.sct_erc_set
= true;
4067 cfg
.sct_erc_readtime
= rt
;
4068 cfg
.sct_erc_writetime
= wt
;
4077 // monitor everything
4078 cfg
.smartcheck
= true;
4080 cfg
.usagefailed
= true;
4082 cfg
.selftest
= true;
4083 cfg
.errorlog
= true;
4084 cfg
.selfteststs
= true;
4087 // automatic offline testing enable/disable
4088 if ((arg
= strtok(NULL
, delim
)) == NULL
) {
4090 } else if (!strcmp(arg
, "on")) {
4091 cfg
.autoofflinetest
= 2;
4092 } else if (!strcmp(arg
, "off")) {
4093 cfg
.autoofflinetest
= 1;
4099 // skip disk check if in idle or standby mode
4100 if (!(arg
= strtok(NULL
, delim
)))
4103 char *endptr
= NULL
;
4104 char *next
= strchr(const_cast<char*>(arg
), ',');
4106 cfg
.powerquiet
= false;
4107 cfg
.powerskipmax
= 0;
4109 if (next
!=NULL
) *next
='\0';
4110 if (!strcmp(arg
, "never"))
4112 else if (!strcmp(arg
, "sleep"))
4114 else if (!strcmp(arg
, "standby"))
4116 else if (!strcmp(arg
, "idle"))
4121 // if optional arguments are present
4122 if (!badarg
&& next
!=NULL
) {
4124 cfg
.powerskipmax
= strtol(next
, &endptr
, 10);
4126 cfg
.powerskipmax
= 0;
4128 next
= endptr
+ (*endptr
!= '\0');
4129 if (cfg
.powerskipmax
<= 0)
4132 if (*next
!= '\0') {
4133 if (!strcmp("q", next
))
4134 cfg
.powerquiet
= true;
4143 // automatic attribute autosave enable/disable
4144 if ((arg
= strtok(NULL
, delim
)) == NULL
) {
4146 } else if (!strcmp(arg
, "on")) {
4148 } else if (!strcmp(arg
, "off")) {
4155 // warn user, and delete any previously given -s REGEXP Directives
4156 if (!cfg
.test_regex
.empty()){
4157 PrintOut(LOG_INFO
, "File %s line %d (drive %s): ignoring previous Test Directive -s %s\n",
4158 configfile
, lineno
, name
, cfg
.test_regex
.get_pattern());
4159 cfg
.test_regex
= regular_expression();
4161 // check for missing argument
4162 if (!(arg
= strtok(NULL
, delim
))) {
4167 if (!cfg
.test_regex
.compile(arg
, REG_EXTENDED
)) {
4168 // not a valid regular expression!
4169 PrintOut(LOG_CRIT
, "File %s line %d (drive %s): -s argument \"%s\" is INVALID extended regular expression. %s.\n",
4170 configfile
, lineno
, name
, arg
, cfg
.test_regex
.get_errmsg());
4173 // Do a bit of sanity checking and warn user if we think that
4174 // their regexp is "strange". User probably confused about shell
4175 // glob(3) syntax versus regular expression syntax regexp(7).
4176 if (arg
[(val
= strspn(arg
, "0123456789/.-+*|()?^$[]SLCOcnr"))])
4177 PrintOut(LOG_INFO
, "File %s line %d (drive %s): warning, character %d (%c) looks odd in extended regular expression %s\n",
4178 configfile
, lineno
, name
, val
+1, arg
[val
], arg
);
4182 // send email to address that follows
4183 if (!(arg
= strtok(NULL
,delim
)))
4186 if (!cfg
.emailaddress
.empty())
4187 PrintOut(LOG_INFO
, "File %s line %d (drive %s): ignoring previous Address Directive -m %s\n",
4188 configfile
, lineno
, name
, cfg
.emailaddress
.c_str());
4189 #ifdef _WIN32 // TODO: Remove after smartmontools 6.5
4190 if ( !strcmp(arg
, "msgbox") || !strcmp(arg
, "sysmsgbox")
4191 || str_starts_with(arg
, "msgbox,") || str_starts_with(arg
, "sysmsgbox,")) {
4192 PrintOut(LOG_CRIT
, "File %s line %d (drive %s): -m %s is no longer supported, use -m console[,...] instead\n",
4193 configfile
, lineno
, name
, arg
);
4197 cfg
.emailaddress
= arg
;
4201 // email warning options
4202 if (!(arg
= strtok(NULL
, delim
)))
4204 else if (!strcmp(arg
, "once"))
4206 else if (!strcmp(arg
, "daily"))
4208 else if (!strcmp(arg
, "diminishing"))
4210 else if (!strcmp(arg
, "test"))
4212 else if (!strcmp(arg
, "exec")) {
4213 // Get the next argument (the command line)
4215 // Allow "/path name/with spaces/..." on Windows
4216 arg
= strtok_dequote(delim
);
4217 if (arg
&& arg
[0] == '"') {
4218 PrintOut(LOG_CRIT
, "File %s line %d (drive %s): Directive %s 'exec' argument: missing closing quote\n",
4219 configfile
, lineno
, name
, token
);
4223 arg
= strtok(0, delim
);
4226 PrintOut(LOG_CRIT
, "File %s line %d (drive %s): Directive %s 'exec' argument must be followed by executable path.\n",
4227 configfile
, lineno
, name
, token
);
4230 // Free the last cmd line given if any, and copy new one
4231 if (!cfg
.emailcmdline
.empty())
4232 PrintOut(LOG_INFO
, "File %s line %d (drive %s): ignoring previous mail Directive -M exec %s\n",
4233 configfile
, lineno
, name
, cfg
.emailcmdline
.c_str());
4234 cfg
.emailcmdline
= arg
;
4240 // ignore failure of usage attribute
4241 if ((val
=GetInteger(arg
=strtok(NULL
,delim
), name
, token
, lineno
, configfile
, 1, 255))<0)
4243 cfg
.monitor_attr_flags
.set(val
, MONITOR_IGN_FAILUSE
);
4246 // ignore attribute for tracking purposes
4247 if ((val
=GetInteger(arg
=strtok(NULL
,delim
), name
, token
, lineno
, configfile
, 1, 255))<0)
4249 cfg
.monitor_attr_flags
.set(val
, MONITOR_IGNORE
);
4252 // print raw value when tracking
4253 if ((val
= GetInteger(arg
=strtok(NULL
,delim
), name
, token
, lineno
, configfile
, 1, 255, excl
)) < 0)
4255 cfg
.monitor_attr_flags
.set(val
, MONITOR_RAW_PRINT
);
4256 if (*excl
== '!') // attribute change is critical
4257 cfg
.monitor_attr_flags
.set(val
, MONITOR_AS_CRIT
);
4260 // track changes in raw value (forces printing of raw value)
4261 if ((val
= GetInteger(arg
=strtok(NULL
,delim
), name
, token
, lineno
, configfile
, 1, 255, excl
)) < 0)
4263 cfg
.monitor_attr_flags
.set(val
, MONITOR_RAW_PRINT
|MONITOR_RAW
);
4264 if (*excl
== '!') // raw value change is critical
4265 cfg
.monitor_attr_flags
.set(val
, MONITOR_RAW_AS_CRIT
);
4268 // track Temperature
4269 if (Get3Integers(arg
=strtok(NULL
, delim
), name
, token
, lineno
, configfile
,
4270 &cfg
.tempdiff
, &cfg
.tempinfo
, &cfg
.tempcrit
) < 0)
4274 // non-default vendor-specific attribute meaning
4275 if (!(arg
=strtok(NULL
,delim
))) {
4277 } else if (!parse_attribute_def(arg
, cfg
.attribute_defs
, PRIOR_USER
)) {
4282 // Define use of drive-specific presets.
4283 if (!(arg
= strtok(NULL
, delim
))) {
4285 } else if (!strcmp(arg
, "use")) {
4286 cfg
.ignorepresets
= false;
4287 } else if (!strcmp(arg
, "ignore")) {
4288 cfg
.ignorepresets
= true;
4289 } else if (!strcmp(arg
, "show")) {
4290 cfg
.showpresets
= true;
4291 } else if (!strcmp(arg
, "showall")) {
4299 // Various ATA settings
4300 if (!(arg
= strtok(NULL
, delim
))) {
4304 char arg2
[16+1]; unsigned val
;
4305 int n1
= -1, n2
= -1, n3
= -1, len
= strlen(arg
);
4306 if (sscanf(arg
, "%16[^,=]%n%*[,=]%n%u%n", arg2
, &n1
, &n2
, &val
, &n3
) >= 1
4307 && (n1
== len
|| n2
> 0)) {
4308 bool on
= (n2
> 0 && !strcmp(arg
+n2
, "on"));
4309 bool off
= (n2
> 0 && !strcmp(arg
+n2
, "off"));
4313 if (!strcmp(arg2
, "aam")) {
4316 else if (val
<= 254)
4317 cfg
.set_aam
= val
+ 1;
4321 else if (!strcmp(arg2
, "apm")) {
4324 else if (1 <= val
&& val
<= 254)
4325 cfg
.set_apm
= val
+ 1;
4329 else if (!strcmp(arg2
, "lookahead")) {
4331 cfg
.set_lookahead
= -1;
4333 cfg
.set_lookahead
= 1;
4337 else if (!strcmp(arg
, "security-freeze")) {
4338 cfg
.set_security_freeze
= true;
4340 else if (!strcmp(arg2
, "standby")) {
4342 cfg
.set_standby
= 0 + 1;
4343 else if (val
<= 255)
4344 cfg
.set_standby
= val
+ 1;
4348 else if (!strcmp(arg2
, "wcache")) {
4350 cfg
.set_wcache
= -1;
4365 // Directive not recognized
4366 PrintOut(LOG_CRIT
,"File %s line %d (drive %s): unknown Directive: %s\n",
4367 configfile
, lineno
, name
, token
);
4372 PrintOut(LOG_CRIT
, "File %s line %d (drive %s): Missing argument to %s Directive\n",
4373 configfile
, lineno
, name
, token
);
4376 PrintOut(LOG_CRIT
, "File %s line %d (drive %s): Invalid argument to %s Directive: %s\n",
4377 configfile
, lineno
, name
, token
, arg
);
4379 if (missingarg
|| badarg
) {
4380 PrintOut(LOG_CRIT
, "Valid arguments to %s Directive are: ", token
);
4381 printoutvaliddirectiveargs(LOG_CRIT
, sym
);
4382 PrintOut(LOG_CRIT
, "\n");
4389 // Scan directive for configuration file
4390 #define SCANDIRECTIVE "DEVICESCAN"
4392 // This is the routine that adds things to the conf_entries list.
4394 // Return values are:
4395 // 1: parsed a normal line
4396 // 0: found DEFAULT setting or comment or blank line
4397 // -1: found SCANDIRECTIVE line
4398 // -2: found an error
4400 // Note: this routine modifies *line from the caller!
4401 static int ParseConfigLine(dev_config_vector
& conf_entries
, dev_config
& default_conf
,
4402 smart_devtype_list
& scan_types
, int lineno
, /*const*/ char * line
)
4404 const char *delim
= " \n\t";
4406 // get first token: device name. If a comment, skip line
4407 const char * name
= strtok(line
, delim
);
4408 if (!name
|| *name
== '#')
4411 // Check device name for DEFAULT or DEVICESCAN
4413 if (!strcmp("DEFAULT", name
)) {
4415 // Restart with empty defaults
4416 default_conf
= dev_config();
4419 retval
= (!strcmp(SCANDIRECTIVE
, name
) ? -1 : 1);
4420 // Init new entry with current defaults
4421 conf_entries
.push_back(default_conf
);
4423 dev_config
& cfg
= (retval
? conf_entries
.back() : default_conf
);
4425 cfg
.name
= name
; // Later replaced by dev->get_info().info_name
4426 cfg
.dev_name
= name
; // If DEVICESCAN later replaced by get->dev_info().dev_name
4427 cfg
.lineno
= lineno
;
4429 // parse tokens one at a time from the file.
4430 while (char * token
= strtok(0, delim
)) {
4431 int rc
= ParseToken(token
, cfg
, scan_types
);
4433 // error found on the line
4440 // PrintOut(LOG_INFO,"Parsed token %s\n",token);
4443 // Check for multiple -d TYPE directives
4444 if (retval
!= -1 && scan_types
.size() > 1) {
4445 PrintOut(LOG_CRIT
, "Drive: %s, invalid multiple -d TYPE Directives on line %d of file %s\n",
4446 cfg
.name
.c_str(), cfg
.lineno
, configfile
);
4450 // Don't perform checks below for DEFAULT entries
4454 // If NO monitoring directives are set, then set all of them.
4455 if (!( cfg
.smartcheck
|| cfg
.selftest
4456 || cfg
.errorlog
|| cfg
.xerrorlog
4457 || cfg
.offlinests
|| cfg
.selfteststs
4458 || cfg
.usagefailed
|| cfg
.prefail
|| cfg
.usage
4459 || cfg
.tempdiff
|| cfg
.tempinfo
|| cfg
.tempcrit
)) {
4461 PrintOut(LOG_INFO
,"Drive: %s, implied '-a' Directive on line %d of file %s\n",
4462 cfg
.name
.c_str(), cfg
.lineno
, configfile
);
4464 cfg
.smartcheck
= true;
4465 cfg
.usagefailed
= true;
4468 cfg
.selftest
= true;
4469 cfg
.errorlog
= true;
4470 cfg
.selfteststs
= true;
4473 // additional sanity check. Has user set -M options without -m?
4474 if (cfg
.emailaddress
.empty() && (!cfg
.emailcmdline
.empty() || cfg
.emailfreq
|| cfg
.emailtest
)){
4475 PrintOut(LOG_CRIT
,"Drive: %s, -M Directive(s) on line %d of file %s need -m ADDRESS Directive\n",
4476 cfg
.name
.c_str(), cfg
.lineno
, configfile
);
4480 // has the user has set <nomailer>?
4481 if (cfg
.emailaddress
== "<nomailer>") {
4482 // check that -M exec is also set
4483 if (cfg
.emailcmdline
.empty()){
4484 PrintOut(LOG_CRIT
,"Drive: %s, -m <nomailer> Directive on line %d of file %s needs -M exec Directive\n",
4485 cfg
.name
.c_str(), cfg
.lineno
, configfile
);
4488 // From here on the sign of <nomailer> is cfg.emailaddress.empty() and !cfg.emailcmdline.empty()
4489 cfg
.emailaddress
.clear();
4495 // Parses a configuration file. Return values are:
4496 // N=>0: found N entries
4497 // -1: syntax error in config file
4498 // -2: config file does not exist
4499 // -3: config file exists but cannot be read
4501 // In the case where the return value is 0, there are three
4503 // Empty configuration file ==> conf_entries.empty()
4504 // No configuration file ==> conf_entries[0].lineno == 0
4505 // SCANDIRECTIVE found ==> conf_entries.back().lineno != 0 (size >= 1)
4506 static int ParseConfigFile(dev_config_vector
& conf_entries
, smart_devtype_list
& scan_types
)
4508 // maximum line length in configuration file
4509 const int MAXLINELEN
= 256;
4510 // maximum length of a continued line in configuration file
4511 const int MAXCONTLINE
= 1023;
4514 // Open config file, if it exists and is not <stdin>
4515 if (!(configfile
== configfile_stdin
)) { // pointer comparison ok here
4516 if (!f
.open(configfile
,"r") && (errno
!=ENOENT
|| !configfile_alt
.empty())) {
4517 // file exists but we can't read it or it should exist due to '-c' option
4518 int ret
= (errno
!=ENOENT
? -3 : -2);
4519 PrintOut(LOG_CRIT
,"%s: Unable to open configuration file %s\n",
4520 strerror(errno
),configfile
);
4524 else // read from stdin ('-c -' option)
4527 // Start with empty defaults
4528 dev_config default_conf
;
4530 // No configuration file found -- use fake one
4533 char fakeconfig
[] = SCANDIRECTIVE
" -a"; // TODO: Remove this hack, build cfg_entry.
4535 if (ParseConfigLine(conf_entries
, default_conf
, scan_types
, 0, fakeconfig
) != -1)
4536 throw std::logic_error("Internal error parsing " SCANDIRECTIVE
);
4541 setmode(fileno(f
), O_TEXT
); // Allow files with \r\n
4544 // configuration file exists
4545 PrintOut(LOG_INFO
,"Opened configuration file %s\n",configfile
);
4547 // parse config file line by line
4548 int lineno
= 1, cont
= 0, contlineno
= 0;
4549 char line
[MAXLINELEN
+2];
4550 char fullline
[MAXCONTLINE
+1];
4553 int len
=0,scandevice
;
4558 // make debugging simpler
4559 memset(line
,0,sizeof(line
));
4562 code
=fgets(line
, MAXLINELEN
+2, f
);
4564 // are we at the end of the file?
4567 scandevice
= ParseConfigLine(conf_entries
, default_conf
, scan_types
, contlineno
, fullline
);
4568 // See if we found a SCANDIRECTIVE directive
4571 // did we find a syntax error
4574 // the final line is part of a continuation line
4580 // input file line number
4583 // See if line is too long
4585 if (len
>MAXLINELEN
){
4587 if (line
[len
-1]=='\n')
4588 warn
="(including newline!) ";
4591 PrintOut(LOG_CRIT
,"Error: line %d of file %s %sis more than MAXLINELEN=%d characters.\n",
4592 (int)contlineno
,configfile
,warn
,(int)MAXLINELEN
);
4596 // Ignore anything after comment symbol
4597 if ((comment
=strchr(line
,'#'))){
4602 // is the total line (made of all continuation lines) too long?
4603 if (cont
+len
>MAXCONTLINE
){
4604 PrintOut(LOG_CRIT
,"Error: continued line %d (actual line %d) of file %s is more than MAXCONTLINE=%d characters.\n",
4605 lineno
, (int)contlineno
, configfile
, (int)MAXCONTLINE
);
4609 // copy string so far into fullline, and increment length
4610 snprintf(fullline
+cont
, sizeof(fullline
)-cont
, "%s" ,line
);
4613 // is this a continuation line. If so, replace \ by space and look at next line
4614 if ( (lastslash
=strrchr(line
,'\\')) && !strtok(lastslash
+1," \n\t")){
4615 *(fullline
+(cont
-len
)+(lastslash
-line
))=' ';
4619 // Not a continuation line. Parse it
4621 scandevice
= ParseConfigLine(conf_entries
, default_conf
, scan_types
, contlineno
, fullline
);
4623 // did we find a scandevice directive?
4626 // did we find a syntax error
4635 // note -- may be zero if syntax of file OK, but no valid entries!
4639 /* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> <=======\n", where
4640 <LIST> is the list of valid arguments for option opt. */
4641 static void PrintValidArgs(char opt
)
4645 PrintOut(LOG_CRIT
, "=======> VALID ARGUMENTS ARE: ");
4646 if (!(s
= GetValidArgList(opt
)))
4647 PrintOut(LOG_CRIT
, "Error constructing argument list for option %c", opt
);
4649 PrintOut(LOG_CRIT
, "%s", (char *)s
);
4650 PrintOut(LOG_CRIT
, " <=======\n");
4654 // Report error and exit if specified path is not absolute.
4655 static void check_abs_path(char option
, const std::string
& path
)
4657 if (path
.empty() || path
[0] == '/')
4662 PrintOut(LOG_CRIT
, "=======> INVALID ARGUMENT TO -%c: %s <=======\n\n", option
, path
.c_str());
4663 PrintOut(LOG_CRIT
, "Error: relative path names are not allowed\n\n");
4668 // Parses input line, prints usage message and
4669 // version/license/copyright messages
4670 static void ParseOpts(int argc
, char **argv
)
4672 // Init default path names
4674 configfile
= SMARTMONTOOLS_SYSCONFDIR
"/smartd.conf";
4675 warning_script
= SMARTMONTOOLS_SMARTDSCRIPTDIR
"/smartd_warning.sh";
4677 std::string exedir
= get_exe_dir();
4678 static std::string configfile_str
= exedir
+ "/smartd.conf";
4679 configfile
= configfile_str
.c_str();
4680 warning_script
= exedir
+ "/smartd_warning.cmd";
4683 // Please update GetValidArgList() if you edit shortopts
4684 static const char shortopts
[] = "c:l:q:dDni:p:r:s:A:B:w:Vh?"
4685 #ifdef HAVE_LIBCAP_NG
4689 // Please update GetValidArgList() if you edit longopts
4690 struct option longopts
[] = {
4691 { "configfile", required_argument
, 0, 'c' },
4692 { "logfacility", required_argument
, 0, 'l' },
4693 { "quit", required_argument
, 0, 'q' },
4694 { "debug", no_argument
, 0, 'd' },
4695 { "showdirectives", no_argument
, 0, 'D' },
4696 { "interval", required_argument
, 0, 'i' },
4698 { "no-fork", no_argument
, 0, 'n' },
4700 { "service", no_argument
, 0, 'n' },
4702 { "pidfile", required_argument
, 0, 'p' },
4703 { "report", required_argument
, 0, 'r' },
4704 { "savestates", required_argument
, 0, 's' },
4705 { "attributelog", required_argument
, 0, 'A' },
4706 { "drivedb", required_argument
, 0, 'B' },
4707 { "warnexec", required_argument
, 0, 'w' },
4708 { "version", no_argument
, 0, 'V' },
4709 { "license", no_argument
, 0, 'V' },
4710 { "copyright", no_argument
, 0, 'V' },
4711 { "help", no_argument
, 0, 'h' },
4712 { "usage", no_argument
, 0, 'h' },
4713 #ifdef HAVE_LIBCAP_NG
4714 { "capabilities", no_argument
, 0, 'C' },
4720 bool badarg
= false;
4721 bool use_default_db
= true; // set false on '-B FILE'
4723 // Parse input options.
4725 while ((optchar
= getopt_long(argc
, argv
, shortopts
, longopts
, NULL
)) != -1) {
4733 if (!(strcmp(optarg
,"nodev"))) {
4735 } else if (!(strcmp(optarg
,"nodevstartup"))) {
4737 } else if (!(strcmp(optarg
,"never"))) {
4739 } else if (!(strcmp(optarg
,"onecheck"))) {
4742 } else if (!(strcmp(optarg
,"showtests"))) {
4745 } else if (!(strcmp(optarg
,"errors"))) {
4752 // set the log facility level
4753 if (!strcmp(optarg
, "daemon"))
4754 facility
=LOG_DAEMON
;
4755 else if (!strcmp(optarg
, "local0"))
4756 facility
=LOG_LOCAL0
;
4757 else if (!strcmp(optarg
, "local1"))
4758 facility
=LOG_LOCAL1
;
4759 else if (!strcmp(optarg
, "local2"))
4760 facility
=LOG_LOCAL2
;
4761 else if (!strcmp(optarg
, "local3"))
4762 facility
=LOG_LOCAL3
;
4763 else if (!strcmp(optarg
, "local4"))
4764 facility
=LOG_LOCAL4
;
4765 else if (!strcmp(optarg
, "local5"))
4766 facility
=LOG_LOCAL5
;
4767 else if (!strcmp(optarg
, "local6"))
4768 facility
=LOG_LOCAL6
;
4769 else if (!strcmp(optarg
, "local7"))
4770 facility
=LOG_LOCAL7
;
4775 // enable debug mode
4780 #ifndef _WIN32 // On Windows, --service is already handled by daemon_main()
4785 // print summary of all valid directives
4791 // Period (time interval) for checking
4792 // strtol will set errno in the event of overflow, so we'll check it.
4794 lchecktime
= strtol(optarg
, &tailptr
, 10);
4795 if (*tailptr
!= '\0' || lchecktime
< 10 || lchecktime
> INT_MAX
|| errno
) {
4798 PrintOut(LOG_CRIT
, "======> INVALID INTERVAL: %s <=======\n", optarg
);
4799 PrintOut(LOG_CRIT
, "======> INTERVAL MUST BE INTEGER BETWEEN %d AND %d <=======\n", 10, INT_MAX
);
4800 PrintOut(LOG_CRIT
, "\nUse smartd -h to get a usage summary\n\n");
4803 checktime
= (int)lchecktime
;
4806 // report IOCTL transactions
4808 int n1
= -1, n2
= -1, len
= strlen(optarg
);
4809 char s
[9+1]; unsigned i
= 1;
4810 sscanf(optarg
, "%9[a-z]%n,%u%n", s
, &n1
, &i
, &n2
);
4811 if (!((n1
== len
|| n2
== len
) && 1 <= i
&& i
<= 4)) {
4813 } else if (!strcmp(s
,"ioctl")) {
4814 ata_debugmode
= scsi_debugmode
= nvme_debugmode
= i
;
4815 } else if (!strcmp(s
,"ataioctl")) {
4817 } else if (!strcmp(s
,"scsiioctl")) {
4819 } else if (!strcmp(s
,"nvmeioctl")) {
4827 // alternate configuration file
4828 if (strcmp(optarg
,"-"))
4829 configfile
= (configfile_alt
= optarg
).c_str();
4830 else // read from stdin
4831 configfile
=configfile_stdin
;
4834 // output file with PID number
4838 // path prefix of persistent state file
4839 state_path_prefix
= optarg
;
4842 // path prefix of attribute log file
4843 attrlog_path_prefix
= optarg
;
4847 const char * path
= optarg
;
4848 if (*path
== '+' && path
[1])
4851 use_default_db
= false;
4852 unsigned char savedebug
= debugmode
; debugmode
= 1;
4853 if (!read_drive_database(path
))
4855 debugmode
= savedebug
;
4859 warning_script
= optarg
;
4862 // print version and CVS info
4864 PrintOut(LOG_INFO
, "%s", format_version_info("smartd", true /*full*/).c_str());
4867 #ifdef HAVE_LIBCAP_NG
4869 // enable capabilities
4870 enable_capabilities
= true;
4874 // help: print summary of command-line options
4882 // unrecognized option
4885 // Point arg to the argument in which this option was found.
4886 arg
= argv
[optind
-1];
4887 // Check whether the option is a long option that doesn't map to -h.
4888 if (arg
[1] == '-' && optchar
!= 'h') {
4889 // Iff optopt holds a valid option then argument must be missing.
4890 if (optopt
&& (strchr(shortopts
, optopt
) != NULL
)) {
4891 PrintOut(LOG_CRIT
, "=======> ARGUMENT REQUIRED FOR OPTION: %s <=======\n",arg
+2);
4892 PrintValidArgs(optopt
);
4894 PrintOut(LOG_CRIT
, "=======> UNRECOGNIZED OPTION: %s <=======\n\n",arg
+2);
4896 PrintOut(LOG_CRIT
, "\nUse smartd --help to get a usage summary\n\n");
4900 // Iff optopt holds a valid option then argument must be missing.
4901 if (strchr(shortopts
, optopt
) != NULL
){
4902 PrintOut(LOG_CRIT
, "=======> ARGUMENT REQUIRED FOR OPTION: %c <=======\n",optopt
);
4903 PrintValidArgs(optopt
);
4905 PrintOut(LOG_CRIT
, "=======> UNRECOGNIZED OPTION: %c <=======\n\n",optopt
);
4907 PrintOut(LOG_CRIT
, "\nUse smartd -h to get a usage summary\n\n");
4914 // Check to see if option had an unrecognized or incorrect argument.
4918 // It would be nice to print the actual option name given by the user
4919 // here, but we just print the short form. Please fix this if you know
4920 // a clean way to do it.
4921 PrintOut(LOG_CRIT
, "=======> INVALID ARGUMENT TO -%c: %s <======= \n", optchar
, optarg
);
4922 PrintValidArgs(optchar
);
4923 PrintOut(LOG_CRIT
, "\nUse smartd -h to get a usage summary\n\n");
4928 // non-option arguments are not allowed
4929 if (argc
> optind
) {
4932 PrintOut(LOG_CRIT
, "=======> UNRECOGNIZED ARGUMENT: %s <=======\n\n", argv
[optind
]);
4933 PrintOut(LOG_CRIT
, "\nUse smartd -h to get a usage summary\n\n");
4937 // no pidfile in debug mode
4938 if (debugmode
&& !pid_file
.empty()) {
4941 PrintOut(LOG_CRIT
, "=======> INVALID CHOICE OF OPTIONS: -d and -p <======= \n\n");
4942 PrintOut(LOG_CRIT
, "Error: pid file %s not written in debug (-d) mode\n\n", pid_file
.c_str());
4948 // absolute path names are required due to chdir('/') after fork().
4949 check_abs_path('p', pid_file
);
4950 check_abs_path('s', state_path_prefix
);
4951 check_abs_path('A', attrlog_path_prefix
);
4955 // Read or init drive database
4957 unsigned char savedebug
= debugmode
; debugmode
= 1;
4958 if (!init_drive_database(use_default_db
))
4960 debugmode
= savedebug
;
4967 // Function we call if no configuration file was found or if the
4968 // SCANDIRECTIVE Directive was found. It makes entries for device
4969 // names returned by scan_smart_devices() in os_OSNAME.cpp
4970 static int MakeConfigEntries(const dev_config
& base_cfg
,
4971 dev_config_vector
& conf_entries
, smart_device_list
& scanned_devs
,
4972 const smart_devtype_list
& types
)
4974 // make list of devices
4975 smart_device_list devlist
;
4976 if (!smi()->scan_smart_devices(devlist
, types
)) {
4977 PrintOut(LOG_CRIT
, "DEVICESCAN failed: %s\n", smi()->get_errmsg());
4981 // if no devices, return
4982 if (devlist
.size() <= 0)
4985 // add empty device slots for existing config entries
4986 while (scanned_devs
.size() < conf_entries
.size())
4987 scanned_devs
.push_back((smart_device
*)0);
4989 // loop over entries to create
4990 for (unsigned i
= 0; i
< devlist
.size(); i
++) {
4991 // Move device pointer
4992 smart_device
* dev
= devlist
.release(i
);
4993 scanned_devs
.push_back(dev
);
4995 // Copy configuration, update device and type name
4996 conf_entries
.push_back(base_cfg
);
4997 dev_config
& cfg
= conf_entries
.back();
4998 cfg
.name
= dev
->get_info().info_name
;
4999 cfg
.dev_name
= dev
->get_info().dev_name
;
5000 cfg
.dev_type
= dev
->get_info().dev_type
;
5003 return devlist
.size();
5006 static void CanNotRegister(const char *name
, const char *type
, int line
, bool scandirective
)
5008 if (!debugmode
&& scandirective
)
5011 PrintOut(scandirective
?LOG_INFO
:LOG_CRIT
,
5012 "Unable to register %s device %s at line %d of file %s\n",
5013 type
, name
, line
, configfile
);
5015 PrintOut(LOG_INFO
,"Unable to register %s device %s\n",
5020 // Returns negative value (see ParseConfigFile()) if config file
5021 // had errors, else number of entries which may be zero or positive.
5022 static int ReadOrMakeConfigEntries(dev_config_vector
& conf_entries
, smart_device_list
& scanned_devs
)
5024 // parse configuration file configfile (normally /etc/smartd.conf)
5025 smart_devtype_list scan_types
;
5026 int entries
= ParseConfigFile(conf_entries
, scan_types
);
5029 // There was an error reading the configuration file.
5030 conf_entries
.clear();
5032 PrintOut(LOG_CRIT
, "Configuration file %s has fatal syntax errors.\n", configfile
);
5036 // no error parsing config file.
5038 // we did not find a SCANDIRECTIVE and did find valid entries
5039 PrintOut(LOG_INFO
, "Configuration file %s parsed.\n", configfile
);
5041 else if (!conf_entries
.empty()) {
5042 // we found a SCANDIRECTIVE or there was no configuration file so
5043 // scan. Configuration file's last entry contains all options
5045 dev_config first
= conf_entries
.back();
5046 conf_entries
.pop_back();
5049 PrintOut(LOG_INFO
,"Configuration file %s was parsed, found %s, scanning devices\n", configfile
, SCANDIRECTIVE
);
5051 PrintOut(LOG_INFO
,"No configuration file %s found, scanning devices\n", configfile
);
5053 // make config list of devices to search for
5054 MakeConfigEntries(first
, conf_entries
, scanned_devs
, scan_types
);
5056 // warn user if scan table found no devices
5057 if (conf_entries
.empty())
5058 PrintOut(LOG_CRIT
,"In the system's table of devices NO devices found to scan\n");
5061 PrintOut(LOG_CRIT
, "Configuration file %s parsed but has no entries\n", configfile
);
5063 return conf_entries
.size();
5066 // Return true if TYPE contains a RAID drive number
5067 static bool is_raid_type(const char * type
)
5069 if (str_starts_with(type
, "sat,"))
5072 if (sscanf(type
, "%*[^,],%d", &i
) != 1)
5077 // Return true if DEV is already in DEVICES[0..NUMDEVS) or IGNORED[*]
5078 static bool is_duplicate_device(const smart_device
* dev
,
5079 const smart_device_list
& devices
, unsigned numdevs
,
5080 const dev_config_vector
& ignored
)
5082 const smart_device::device_info
& info1
= dev
->get_info();
5083 bool is_raid1
= is_raid_type(info1
.dev_type
.c_str());
5085 for (unsigned i
= 0; i
< numdevs
; i
++) {
5086 const smart_device::device_info
& info2
= devices
.at(i
)->get_info();
5087 // -d TYPE options must match if RAID drive number is specified
5088 if ( info1
.dev_name
== info2
.dev_name
5089 && ( info1
.dev_type
== info2
.dev_type
5090 || !is_raid1
|| !is_raid_type(info2
.dev_type
.c_str())))
5094 for (unsigned i
= 0; i
< ignored
.size(); i
++) {
5095 const dev_config
& cfg2
= ignored
.at(i
);
5096 if ( info1
.dev_name
== cfg2
.dev_name
5097 && ( info1
.dev_type
== cfg2
.dev_type
5098 || !is_raid1
|| !is_raid_type(cfg2
.dev_type
.c_str())))
5104 // This function tries devices from conf_entries. Each one that can be
5105 // registered is moved onto the [ata|scsi]devices lists and removed
5106 // from the conf_entries list.
5107 static void RegisterDevices(const dev_config_vector
& conf_entries
, smart_device_list
& scanned_devs
,
5108 dev_config_vector
& configs
, dev_state_vector
& states
, smart_device_list
& devices
)
5110 // start by clearing lists/memory of ALL existing devices
5116 dev_config_vector ignored_entries
;
5117 unsigned numnoscan
= 0;
5118 for (unsigned i
= 0; i
< conf_entries
.size(); i
++){
5120 dev_config cfg
= conf_entries
[i
];
5123 // Store for is_duplicate_device() check and ignore
5124 PrintOut(LOG_INFO
, "Device: %s%s%s%s, ignored\n", cfg
.name
.c_str(),
5125 (!cfg
.dev_type
.empty() ? " [" : ""),
5126 cfg
.dev_type
.c_str(),
5127 (!cfg
.dev_type
.empty() ? "]" : ""));
5128 ignored_entries
.push_back(cfg
);
5132 // get device of appropriate type
5133 smart_device_auto_ptr dev
;
5134 bool scanning
= false;
5136 // Device may already be detected during devicescan
5137 if (i
< scanned_devs
.size()) {
5138 dev
= scanned_devs
.release(i
);
5140 // Check for a preceding non-DEVICESCAN entry for the same device
5141 if ( (numnoscan
|| !ignored_entries
.empty())
5142 && is_duplicate_device(dev
.get(), devices
, numnoscan
, ignored_entries
)) {
5143 PrintOut(LOG_INFO
, "Device: %s, duplicate, ignored\n", dev
->get_info_name());
5151 dev
= smi()->get_smart_device(cfg
.name
.c_str(), cfg
.dev_type
.c_str());
5153 if (cfg
.dev_type
.empty())
5154 PrintOut(LOG_INFO
,"Device: %s, unable to autodetect device type\n", cfg
.name
.c_str());
5156 PrintOut(LOG_INFO
,"Device: %s, unsupported device type '%s'\n", cfg
.name
.c_str(), cfg
.dev_type
.c_str());
5162 smart_device::device_info oldinfo
= dev
->get_info();
5164 // Open with autodetect support, may return 'better' device
5165 dev
.replace( dev
->autodetect_open() );
5167 // Report if type has changed
5168 if (oldinfo
.dev_type
!= dev
->get_dev_type())
5169 PrintOut(LOG_INFO
,"Device: %s, type changed from '%s' to '%s'\n",
5170 cfg
.name
.c_str(), oldinfo
.dev_type
.c_str(), dev
->get_dev_type());
5172 if (!dev
->is_open()) {
5173 // For linux+devfs, a nonexistent device gives a strange error
5174 // message. This makes the error message a bit more sensible.
5175 // If no debug and scanning - don't print errors
5176 if (debugmode
|| !scanning
)
5177 PrintOut(LOG_INFO
, "Device: %s, open() failed: %s\n", dev
->get_info_name(), dev
->get_errmsg());
5181 // Update informal name
5182 cfg
.name
= dev
->get_info().info_name
;
5183 PrintOut(LOG_INFO
, "Device: %s, opened\n", cfg
.name
.c_str());
5185 // Prepare initial state
5188 // register ATA devices
5190 if (ATADeviceScan(cfg
, state
, dev
->to_ata())) {
5191 CanNotRegister(cfg
.name
.c_str(), "ATA", cfg
.lineno
, scanning
);
5195 // or register SCSI devices
5196 else if (dev
->is_scsi()){
5197 if (SCSIDeviceScan(cfg
, state
, dev
->to_scsi())) {
5198 CanNotRegister(cfg
.name
.c_str(), "SCSI", cfg
.lineno
, scanning
);
5202 // or register NVMe devices
5203 else if (dev
->is_nvme()) {
5204 if (NVMeDeviceScan(cfg
, state
, dev
->to_nvme())) {
5205 CanNotRegister(cfg
.name
.c_str(), "NVMe", cfg
.lineno
, scanning
);
5210 PrintOut(LOG_INFO
, "Device: %s, neither ATA, SCSI nor NVMe device\n", cfg
.name
.c_str());
5215 // move onto the list of devices
5216 configs
.push_back(cfg
);
5217 states
.push_back(state
);
5218 devices
.push_back(dev
);
5220 numnoscan
= devices
.size();
5222 // if device is explictly listed and we can't register it, then
5223 // exit unless the user has specified that the device is removable
5224 else if (!scanning
) {
5225 if (cfg
.removable
|| quit
==2)
5226 PrintOut(LOG_INFO
, "Device %s not available\n", cfg
.name
.c_str());
5228 PrintOut(LOG_CRIT
, "Unable to register device %s (no Directive -d removable). Exiting.\n", cfg
.name
.c_str());
5234 init_disable_standby_check(configs
);
5238 // Main program without exception handling
5239 static int main_worker(int argc
, char **argv
)
5241 // Initialize interface
5242 smart_interface::init();
5246 // is it our first pass through?
5247 bool firstpass
= true;
5249 // next time to wake up
5250 time_t wakeuptime
= 0;
5252 // parse input and print header and usage info if needed
5253 ParseOpts(argc
,argv
);
5255 // Configuration for each device
5256 dev_config_vector configs
;
5258 dev_state_vector states
;
5259 // Devices to monitor
5260 smart_device_list devices
;
5262 bool write_states_always
= true;
5264 #ifdef HAVE_LIBCAP_NG
5265 // Drop capabilities
5266 if (enable_capabilities
) {
5267 capng_clear(CAPNG_SELECT_BOTH
);
5268 capng_updatev(CAPNG_ADD
, (capng_type_t
)(CAPNG_EFFECTIVE
|CAPNG_PERMITTED
),
5269 CAP_SYS_ADMIN
, CAP_MKNOD
, CAP_SYS_RAWIO
, -1);
5270 capng_apply(CAPNG_SELECT_BOTH
);
5274 // the main loop of the code
5277 // are we exiting from a signal?
5278 if (caughtsigEXIT
) {
5279 // are we exiting with SIGTERM?
5280 int isterm
=(caughtsigEXIT
==SIGTERM
);
5281 int isquit
=(caughtsigEXIT
==SIGQUIT
);
5282 int isok
=debugmode
?isterm
|| isquit
:isterm
;
5284 PrintOut(isok
?LOG_INFO
:LOG_CRIT
, "smartd received signal %d: %s\n",
5285 caughtsigEXIT
, strsignal(caughtsigEXIT
));
5290 // Write state files
5291 if (!state_path_prefix
.empty())
5292 write_all_dev_states(configs
, states
);
5297 // Should we (re)read the config file?
5298 if (firstpass
|| caughtsigHUP
){
5300 // Write state files
5301 if (!state_path_prefix
.empty())
5302 write_all_dev_states(configs
, states
);
5306 "Signal HUP - rereading configuration file %s\n":
5307 "\a\nSignal INT - rereading configuration file %s (" SIGQUIT_KEYNAME
" quits)\n\n",
5312 dev_config_vector conf_entries
; // Entries read from smartd.conf
5313 smart_device_list scanned_devs
; // Devices found during scan
5314 // (re)reads config file, makes >=0 entries
5315 int entries
= ReadOrMakeConfigEntries(conf_entries
, scanned_devs
);
5318 // checks devices, then moves onto ata/scsi list or deallocates.
5319 RegisterDevices(conf_entries
, scanned_devs
, configs
, states
, devices
);
5320 if (!(configs
.size() == devices
.size() && configs
.size() == states
.size()))
5321 throw std::logic_error("Invalid result from RegisterDevices");
5323 else if (quit
==2 || ((quit
==0 || quit
==1) && !firstpass
)) {
5324 // user has asked to continue on error in configuration file
5326 PrintOut(LOG_INFO
,"Reusing previous configuration\n");
5329 // exit with configuration file error status
5330 return (entries
==-3 ? EXIT_READCONF
: entries
==-2 ? EXIT_NOCONF
: EXIT_BADCONF
);
5334 // Log number of devices we are monitoring...
5335 if (devices
.size() > 0 || quit
==2 || (quit
==1 && !firstpass
)) {
5336 int numata
= 0, numscsi
= 0;
5337 for (unsigned i
= 0; i
< devices
.size(); i
++) {
5338 const smart_device
* dev
= devices
.at(i
);
5341 else if (dev
->is_scsi())
5344 PrintOut(LOG_INFO
,"Monitoring %d ATA/SATA, %d SCSI/SAS and %d NVMe devices\n",
5345 numata
, numscsi
, (int)devices
.size() - numata
- numscsi
);
5348 PrintOut(LOG_INFO
,"Unable to monitor any SMART enabled devices. Try debug (-d) option. Exiting...\n");
5353 // user has asked to print test schedule
5354 PrintTestSchedule(configs
, states
, devices
);
5358 #ifdef HAVE_LIBCAP_NG
5359 if (enable_capabilities
) {
5360 for (unsigned i
= 0; i
< configs
.size(); i
++) {
5361 if (!configs
[i
].emailaddress
.empty() || !configs
[i
].emailcmdline
.empty()) {
5362 PrintOut(LOG_WARNING
, "Mail can't be enabled together with --capabilities. All mail will be suppressed.\n");
5372 // Always write state files after (re)configuration
5373 write_states_always
= true;
5376 // check all devices once,
5377 // self tests are not started in first pass unless '-q onecheck' is specified
5378 CheckDevicesOnce(configs
, states
, devices
, firstpass
, (!firstpass
|| quit
==3));
5380 // Write state files
5381 if (!state_path_prefix
.empty())
5382 write_all_dev_states(configs
, states
, write_states_always
);
5383 write_states_always
= false;
5385 // Write attribute logs
5386 if (!attrlog_path_prefix
.empty())
5387 write_all_dev_attrlogs(configs
, states
);
5389 // user has asked us to exit after first check
5391 PrintOut(LOG_INFO
,"Started with '-q onecheck' option. All devices sucessfully checked once.\n"
5392 "smartd is exiting (exit status 0)\n");
5396 // fork into background if needed
5397 if (firstpass
&& !debugmode
) {
5401 // set exit and signal handlers, write PID file, set wake-up time
5403 Initialize(&wakeuptime
);
5407 // sleep until next check time, or a signal arrives
5408 wakeuptime
= dosleep(wakeuptime
, write_states_always
);
5415 int main(int argc
, char **argv
)
5417 // Windows: internal main function started direct or by service control manager
5418 static int smartd_main(int argc
, char **argv
)
5423 // Do the real work ...
5424 status
= main_worker(argc
, argv
);
5427 // EXIT(status) arrives here
5430 catch (const std::bad_alloc
& /*ex*/) {
5431 // Memory allocation failed (also thrown by std::operator new)
5432 PrintOut(LOG_CRIT
, "Smartd: Out of memory\n");
5433 status
= EXIT_NOMEM
;
5435 catch (const std::exception
& ex
) {
5436 // Other fatal errors
5437 PrintOut(LOG_CRIT
, "Smartd: Exception: %s\n", ex
.what());
5438 status
= EXIT_BADCODE
;
5441 // Check for remaining device objects
5442 if (smart_device::get_num_objects() != 0) {
5443 PrintOut(LOG_CRIT
, "Smartd: Internal Error: %d device object(s) left at exit.\n",
5444 smart_device::get_num_objects());
5445 status
= EXIT_BADCODE
;
5448 if (status
== EXIT_BADCODE
)
5449 PrintOut(LOG_CRIT
, "Please inform " PACKAGE_BUGREPORT
", including output of smartd -V.\n");
5452 status
= Goodbye(status
);
5455 daemon_winsvc_exitcode
= status
;
5462 // Main function for Windows
5463 int main(int argc
, char **argv
){
5464 // Options for smartd windows service
5465 static const daemon_winsvc_options svc_opts
= {
5466 "--service", // cmd_opt
5467 "smartd", "SmartD Service", // servicename, displayname
5469 "Controls and monitors storage devices using the Self-Monitoring, "
5470 "Analysis and Reporting Technology System (SMART) built into "
5471 "ATA/SATA and SCSI/SAS hard drives and solid-state drives. "
5472 "www.smartmontools.org"
5474 // daemon_main() handles daemon and service specific commands
5475 // and starts smartd_main() direct, from a new process,
5476 // or via service control manager
5477 return daemon_main("smartd", &svc_opts
, smartd_main
, argc
, argv
);