smartmontools SVN Rev 5640
Utility to control and monitor storage systems with "S.M.A.R.T."
smartd.cpp
Go to the documentation of this file.
1/*
2 * Home page of code is: https://www.smartmontools.org
3 *
4 * Copyright (C) 2002-11 Bruce Allen
5 * Copyright (C) 2008-24 Christian Franke
6 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
7 * Copyright (C) 2008 Oliver Bock <brevilo@users.sourceforge.net>
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12#include "config.h"
13#define __STDC_FORMAT_MACROS 1 // enable PRI* for C++
14
15// unconditionally included files
16#include <inttypes.h>
17#include <stdio.h>
18#include <sys/types.h>
19#include <sys/stat.h> // umask
20#include <signal.h>
21#include <fcntl.h>
22#include <string.h>
23#include <syslog.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <time.h>
28#include <limits.h>
29#include <getopt.h>
30
31#include <algorithm> // std::replace()
32#include <map>
33#include <stdexcept>
34#include <string>
35#include <vector>
36
37// conditionally included files
38#ifndef _WIN32
39#include <sys/wait.h>
40#endif
41#ifdef HAVE_UNISTD_H
42#include <unistd.h>
43#endif
44
45#ifdef _WIN32
46#include "os_win32/popen.h" // popen_as_rstr_user(), pclose()
47#ifdef _MSC_VER
48#pragma warning(disable:4761) // "conversion supplied"
49typedef unsigned short mode_t;
50typedef int pid_t;
51#endif
52#include <io.h> // umask()
53#include <process.h> // getpid()
54#endif // _WIN32
55
56#ifdef __CYGWIN__
57#include <io.h> // setmode()
58#endif // __CYGWIN__
59
60#ifdef HAVE_LIBCAP_NG
61#include <cap-ng.h>
62#endif // LIBCAP_NG
63
64#ifdef HAVE_LIBSYSTEMD
65#include <systemd/sd-daemon.h>
66#endif // HAVE_LIBSYSTEMD
67
68// locally included files
69#include "atacmds.h"
70#include "dev_interface.h"
71#include "knowndrives.h"
72#include "scsicmds.h"
73#include "nvmecmds.h"
74#include "utility.h"
75#include "sg_unaligned.h"
76
77#ifdef HAVE_POSIX_API
78#include "popen_as_ugid.h"
79#endif
80
81#ifdef _WIN32
82// fork()/signal()/initd simulation for native Windows
83#include "os_win32/daemon_win32.h" // daemon_main/detach/signal()
84#define strsignal daemon_strsignal
85#define sleep daemon_sleep
86// SIGQUIT does not exist, CONTROL-Break signals SIGBREAK.
87#define SIGQUIT SIGBREAK
88#define SIGQUIT_KEYNAME "CONTROL-Break"
89#else // _WIN32
90#define SIGQUIT_KEYNAME "CONTROL-\\"
91#endif // _WIN32
92
93const char * smartd_cpp_cvsid = "$Id: smartd.cpp 5636 2024-11-17 15:11:37Z chrfranke $"
94 CONFIG_H_CVSID;
95
96extern "C" {
97 typedef void (*signal_handler_type)(int);
98}
99
101{
102#if defined(_WIN32)
103 // signal() emulation
104 daemon_signal(sig, handler);
105
106#elif defined(HAVE_SIGACTION)
107 // SVr4, POSIX.1-2001, POSIX.1-2008
108 struct sigaction sa;
109 sa.sa_handler = SIG_DFL;
110 sigaction(sig, (struct sigaction *)0, &sa);
111 if (sa.sa_handler == SIG_IGN)
112 return;
113
114 memset(&sa, 0, sizeof(sa));
115 sa.sa_handler = handler;
116 sa.sa_flags = SA_RESTART; // BSD signal() semantics
117 sigaction(sig, &sa, (struct sigaction *)0);
118
119#elif defined(HAVE_SIGSET)
120 // SVr4, POSIX.1-2001, obsoleted in POSIX.1-2008
121 if (sigset(sig, handler) == SIG_IGN)
122 sigset(sig, SIG_IGN);
123
124#else
125 // POSIX.1-2001, POSIX.1-2008, C89, C99, undefined semantics.
126 // Important: BSD semantics is required. Traditional signal()
127 // resets the handler to SIG_DFL after the first signal is caught.
128 if (signal(sig, handler) == SIG_IGN)
129 signal(sig, SIG_IGN);
130#endif
131}
132
133using namespace smartmontools;
134
135static const int scsiLogRespLen = 252;
136
137// smartd exit codes
138#define EXIT_BADCMD 1 // command line did not parse
139#define EXIT_BADCONF 2 // syntax error in config file
140#define EXIT_STARTUP 3 // problem forking daemon
141#define EXIT_PID 4 // problem creating pid file
142#define EXIT_NOCONF 5 // config file does not exist
143#define EXIT_READCONF 6 // config file exists but cannot be read
144
145#define EXIT_NOMEM 8 // out of memory
146#define EXIT_BADCODE 10 // internal error - should NEVER happen
147
148#define EXIT_BADDEV 16 // we can't monitor this device
149#define EXIT_NODEV 17 // no devices to monitor
150
151#define EXIT_SIGNAL 254 // abort on signal
152
153
154// command-line: 1=debug mode, 2=print presets
155static unsigned char debugmode = 0;
156
157// command-line: how long to sleep between checks
158static constexpr int default_checktime = 1800;
160static int checktime_min = 0; // Minimum individual check time, 0 if none
161
162// command-line: name of PID file (empty for no pid file)
163static std::string pid_file;
164
165// command-line: path prefix of persistent state file, empty if no persistence.
166static std::string state_path_prefix
167#ifdef SMARTMONTOOLS_SAVESTATES
168 = SMARTMONTOOLS_SAVESTATES
169#endif
170 ;
171
172// command-line: path prefix of attribute log file, empty if no logs.
173static std::string attrlog_path_prefix
174#ifdef SMARTMONTOOLS_ATTRIBUTELOG
175 = SMARTMONTOOLS_ATTRIBUTELOG
176#endif
177 ;
178
179// configuration file name
180static const char * configfile;
181// configuration file "name" if read from stdin
182static const char * const configfile_stdin = "<stdin>";
183// path of alternate configuration file
184static std::string configfile_alt;
185
186// warning script file
187static std::string warning_script;
188
189#ifdef HAVE_POSIX_API
190// run warning script as non-privileged user
191static bool warn_as_user;
192static uid_t warn_uid;
193static gid_t warn_gid;
194static std::string warn_uname, warn_gname;
195#elif defined(_WIN32)
196// run warning script as restricted user
197static bool warn_as_restr_user;
198#endif
199
200// command-line: when should we exit?
201enum quit_t {
206static bool quit_nodev0 = false;
207
208// command-line; this is the default syslog(3) log facility to use.
209static int facility=LOG_DAEMON;
210
211#ifndef _WIN32
212// command-line: fork into background?
213static bool do_fork=true;
214#endif
215
216// TODO: This smartctl only variable is also used in some os_*.cpp
217unsigned char failuretest_permissive = 0;
218
219// set to one if we catch a USR1 (check devices now)
220static volatile int caughtsigUSR1=0;
221
222#ifdef _WIN32
223// set to one if we catch a USR2 (toggle debug mode)
224static volatile int caughtsigUSR2=0;
225#endif
226
227// set to one if we catch a HUP (reload config file). In debug mode,
228// set to two, if we catch INT (also reload config file).
229static volatile int caughtsigHUP=0;
230
231// set to signal value if we catch INT, QUIT, or TERM
232static volatile int caughtsigEXIT=0;
233
234// This function prints either to stdout or to the syslog as needed.
235static void PrintOut(int priority, const char *fmt, ...)
237
238#ifdef HAVE_LIBSYSTEMD
239// systemd notify support
240
241static bool notify_enabled = false;
242static bool notify_ready = false;
243
244static inline void notify_init()
245{
246 if (!getenv("NOTIFY_SOCKET"))
247 return;
248 notify_enabled = true;
249}
250
251static inline bool notify_post_init()
252{
253 if (!notify_enabled)
254 return true;
255 if (do_fork) {
256 PrintOut(LOG_CRIT, "Option -n (--no-fork) is required if 'Type=notify' is set.\n");
257 return false;
258 }
259 return true;
260}
261
262static inline void notify_extend_timeout()
263{
264 if (!notify_enabled)
265 return;
266 if (notify_ready)
267 return;
268 const char * notify = "EXTEND_TIMEOUT_USEC=20000000"; // typical drive spinup time is 20s tops
269 if (debugmode) {
270 pout("sd_notify(0, \"%s\")\n", notify);
271 return;
272 }
273 sd_notify(0, notify);
274}
275
276static void notify_msg(const char * msg, bool ready = false)
277{
278 if (!notify_enabled)
279 return;
280 if (debugmode) {
281 pout("sd_notify(0, \"%sSTATUS=%s\")\n", (ready ? "READY=1\\n" : ""), msg);
282 return;
283 }
284 sd_notifyf(0, "%sSTATUS=%s", (ready ? "READY=1\n" : ""), msg);
285}
286
287static void notify_check(int numdev)
288{
289 if (!notify_enabled)
290 return;
291 char msg[32];
292 snprintf(msg, sizeof(msg), "Checking %d device%s ...",
293 numdev, (numdev != 1 ? "s" : ""));
294 notify_msg(msg);
295}
296
297static void notify_wait(time_t wakeuptime, int numdev)
298{
299 if (!notify_enabled)
300 return;
301 char ts[16] = ""; struct tm tmbuf;
302 strftime(ts, sizeof(ts), "%H:%M:%S", time_to_tm_local(&tmbuf, wakeuptime));
303 char msg[64];
304 snprintf(msg, sizeof(msg), "Next check of %d device%s will start at %s",
305 numdev, (numdev != 1 ? "s" : ""), ts);
306 notify_msg(msg, !notify_ready); // first call notifies READY=1
307 notify_ready = true;
308}
309
310static void notify_exit(int status)
311{
312 if (!notify_enabled)
313 return;
314 const char * msg;
315 switch (status) {
316 case 0: msg = "Exiting ..."; break;
317 case EXIT_BADCMD: msg = "Error in command line (see SYSLOG)"; break;
318 case EXIT_BADCONF: case EXIT_NOCONF:
319 case EXIT_READCONF: msg = "Error in config file (see SYSLOG)"; break;
320 case EXIT_BADDEV: msg = "Unable to register a device (see SYSLOG)"; break;
321 case EXIT_NODEV: msg = "No devices to monitor"; break;
322 default: msg = "Error (see SYSLOG)"; break;
323 }
324 // Ensure that READY=1 is notified before 'exit(0)' because otherwise
325 // systemd will report a service (protocol) failure
326 notify_msg(msg, (!status && !notify_ready));
327}
328
329#else // HAVE_LIBSYSTEMD
330// No systemd notify support
331
332static inline bool notify_post_init()
333{
334#ifdef __linux__
335 if (getenv("NOTIFY_SOCKET")) {
336 PrintOut(LOG_CRIT, "This version of smartd was build without 'Type=notify' support.\n");
337 return false;
338 }
339#endif
340 return true;
341}
342
343static inline void notify_init() { }
344static inline void notify_extend_timeout() { }
345static inline void notify_msg(const char *) { }
346static inline void notify_check(int) { }
347static inline void notify_wait(time_t, int) { }
348static inline void notify_exit(int) { }
349
350#endif // HAVE_LIBSYSTEMD
351
352// Email frequencies
353enum class emailfreqs : unsigned char {
355};
356
357// Attribute monitoring flags.
358// See monitor_attr_flags below.
359enum {
366};
367
368// Array of flags for each attribute.
370{
371public:
372 bool is_set(int id, unsigned char flag) const
373 { return (0 < id && id < (int)sizeof(m_flags) && (m_flags[id] & flag)); }
374
375 void set(int id, unsigned char flags)
376 {
377 if (0 < id && id < (int)sizeof(m_flags))
378 m_flags[id] |= flags;
379 }
380
381private:
382 unsigned char m_flags[256]{};
383};
384
385
386/// Configuration data for a device. Read from smartd.conf.
387/// Supports copy & assignment and is compatible with STL containers.
389{
390 int lineno{}; // Line number of entry in file
391 std::string name; // Device name (with optional extra info)
392 std::string dev_name; // Device name (plain, for SMARTD_DEVICE variable)
393 std::string dev_type; // Device type argument from -d directive, empty if none
394 std::string dev_idinfo; // Device identify info for warning emails and duplicate check
395 std::string dev_idinfo_bc; // Same without namespace id for duplicate check
396 std::string state_file; // Path of the persistent state file, empty if none
397 std::string attrlog_file; // Path of the persistent attrlog file, empty if none
398 int checktime{}; // Individual check interval, 0 if none
399 bool ignore{}; // Ignore this entry
400 bool id_is_unique{}; // True if dev_idinfo is unique (includes S/N or WWN)
401 bool smartcheck{}; // Check SMART status
402 bool usagefailed{}; // Check for failed Usage Attributes
403 bool prefail{}; // Track changes in Prefail Attributes
404 bool usage{}; // Track changes in Usage Attributes
405 bool selftest{}; // Monitor number of selftest errors
406 bool errorlog{}; // Monitor number of ATA errors
407 bool xerrorlog{}; // Monitor number of ATA errors (Extended Comprehensive error log)
408 bool offlinests{}; // Monitor changes in offline data collection status
409 bool offlinests_ns{}; // Disable auto standby if in progress
410 bool selfteststs{}; // Monitor changes in self-test execution status
411 bool selfteststs_ns{}; // Disable auto standby if in progress
412 bool permissive{}; // Ignore failed SMART commands
413 char autosave{}; // 1=disable, 2=enable Autosave Attributes
414 char autoofflinetest{}; // 1=disable, 2=enable Auto Offline Test
415 firmwarebug_defs firmwarebugs; // -F directives from drivedb or smartd.conf
416 bool ignorepresets{}; // Ignore database of -v options
417 bool showpresets{}; // Show database entry for this device
418 bool removable{}; // Device may disappear (not be present)
419 char powermode{}; // skip check, if disk in idle or standby mode
420 bool powerquiet{}; // skip powermode 'skipping checks' message
421 int powerskipmax{}; // how many times can be check skipped
422 unsigned char tempdiff{}; // Track Temperature changes >= this limit
423 unsigned char tempinfo{}, tempcrit{}; // Track Temperatures >= these limits as LOG_INFO, LOG_CRIT+mail
424 regular_expression test_regex; // Regex for scheduled testing
425 unsigned test_offset_factor{}; // Factor for staggering of scheduled tests
426
427 // Configuration of email warning messages
428 std::string emailcmdline; // script to execute, empty if no messages
429 std::string emailaddress; // email address, or empty
430 emailfreqs emailfreq{}; // Send emails once, daily, diminishing
431 bool emailtest{}; // Send test email?
432
433 // ATA ONLY
434 int dev_rpm{}; // rotation rate, 0 = unknown, 1 = SSD, >1 = HDD
435 int set_aam{}; // disable(-1), enable(1..255->0..254) Automatic Acoustic Management
436 int set_apm{}; // disable(-1), enable(2..255->1..254) Advanced Power Management
437 int set_lookahead{}; // disable(-1), enable(1) read look-ahead
438 int set_standby{}; // set(1..255->0..254) standby timer
439 bool set_security_freeze{}; // Freeze ATA security
440 int set_wcache{}; // disable(-1), enable(1) write cache
441 int set_dsn{}; // disable(0x2), enable(0x1) DSN
442
443 bool sct_erc_set{}; // set SCT ERC to:
444 unsigned short sct_erc_readtime{}; // ERC read time (deciseconds)
445 unsigned short sct_erc_writetime{}; // ERC write time (deciseconds)
446
447 unsigned char curr_pending_id{}; // ID of current pending sector count, 0 if none
448 unsigned char offl_pending_id{}; // ID of offline uncorrectable sector count, 0 if none
449 bool curr_pending_incr{}, offl_pending_incr{}; // True if current/offline pending values increase
450 bool curr_pending_set{}, offl_pending_set{}; // True if '-C', '-U' set in smartd.conf
451
452 attribute_flags monitor_attr_flags; // MONITOR_* flags for each attribute
453
455
456 // NVMe only
457 unsigned nvme_err_log_max_entries{}; // size of error log
458};
459
460// Number of allowed mail message types
461static const int SMARTD_NMAIL = 13;
462// Type for '-M test' mails (state not persistent)
463static const int MAILTYPE_TEST = 0;
464// TODO: Add const or enum for all mail types.
465
466struct mailinfo {
467 int logged{}; // number of times an email has been sent
468 time_t firstsent{}; // time first email was sent, as defined by time(2)
469 time_t lastsent{}; // time last email was sent, as defined by time(2)
470};
471
472/// Persistent state data for a device.
474{
475 unsigned char tempmin{}, tempmax{}; // Min/Max Temperatures
476
477 unsigned char selflogcount{}; // total number of self-test errors
478 uint64_t selfloghour{}; // lifetime hours of last self-test error
479 // (NVMe self-test log uses a 64 bit value)
480
481 time_t scheduled_test_next_check{}; // Time of next check for scheduled self-tests
482
483 uint64_t selective_test_last_start{}; // Start LBA of last scheduled selective self-test
484 uint64_t selective_test_last_end{}; // End LBA of last scheduled selective self-test
485
486 mailinfo maillog[SMARTD_NMAIL]; // log info on when mail sent
487
488 // ATA ONLY
489 int ataerrorcount{}; // Total number of ATA errors
490
491 // Persistent part of ata_smart_values:
493 unsigned char id{};
494 unsigned char val{};
495 unsigned char worst{}; // Byte needed for 'raw64' attribute only.
496 uint64_t raw{};
497 unsigned char resvd{};
498 };
500
501 // SCSI ONLY
502
505 unsigned char found{};
506 };
508
511 unsigned char found{};
512 };
514
515 // NVMe only
517};
518
519/// Non-persistent state data for a device.
521{
522 bool must_write{}; // true if persistent part should be written
523
524 bool skip{}; // skip during next check cycle
525 time_t wakeuptime{}; // next wakeup time, 0 if unknown or global
526
527 bool not_cap_offline{}; // true == not capable of offline testing
532
533 unsigned char temperature{}; // last recorded Temperature (in Celsius)
534 time_t tempmin_delay{}; // time where Min Temperature tracking will start
535
536 bool removed{}; // true if open() failed for removable device
537
538 bool powermodefail{}; // true if power mode check failed
539 int powerskipcnt{}; // Number of checks skipped due to idle or standby mode
540 int lastpowermodeskipped{}; // the last power mode that was skipped
541
542 bool attrlog_dirty{}; // true if persistent part has new attr values that
543 // need to be written to attrlog
544
545 // SCSI ONLY
546 // TODO: change to bool
547 unsigned char SmartPageSupported{}; // has log sense IE page (0x2f)
548 unsigned char TempPageSupported{}; // has log sense temperature page (0xd)
553 unsigned char SuppressReport{}; // minimize nuisance reports
554 unsigned char modese_len{}; // mode sense/select cmd len: 0 (don't
555 // know yet) 6 or 10
556 // ATA ONLY
557 uint64_t num_sectors{}; // Number of sectors
558 ata_smart_values smartval{}; // SMART data
560 bool offline_started{}; // true if offline data collection was started
561
562 // ATA and NVMe
563 bool selftest_started{}; // true if self-test was started
564
565 // NVMe only
566 uint8_t selftest_op{}; // last self-test operation
567 uint8_t selftest_compl{}; // last self-test completion
568};
569
570/// Runtime state data for a device.
572: public persistent_dev_state,
573 public temp_dev_state
574{
576 void update_temp_state();
577};
578
579/// Container for configuration info for each device.
580typedef std::vector<dev_config> dev_config_vector;
581
582/// Container for state info for each device.
583typedef std::vector<dev_state> dev_state_vector;
584
585// Copy ATA attributes to persistent state.
587{
588 for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
591 pa.id = ta.id;
592 if (ta.id == 0) {
593 pa.val = pa.worst = 0; pa.raw = 0;
594 continue;
595 }
596 pa.val = ta.current;
597 pa.worst = ta.worst;
598 pa.raw = ta.raw[0]
599 | ( ta.raw[1] << 8)
600 | ( ta.raw[2] << 16)
601 | ((uint64_t)ta.raw[3] << 24)
602 | ((uint64_t)ta.raw[4] << 32)
603 | ((uint64_t)ta.raw[5] << 40);
604 pa.resvd = ta.reserv;
605 }
606}
607
608// Copy ATA from persistent to temp state.
610{
611 for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
612 const ata_attribute & pa = ata_attributes[i];
614 ta.id = pa.id;
615 if (pa.id == 0) {
616 ta.current = ta.worst = 0;
617 memset(ta.raw, 0, sizeof(ta.raw));
618 continue;
619 }
620 ta.current = pa.val;
621 ta.worst = pa.worst;
622 ta.raw[0] = (unsigned char) pa.raw;
623 ta.raw[1] = (unsigned char)(pa.raw >> 8);
624 ta.raw[2] = (unsigned char)(pa.raw >> 16);
625 ta.raw[3] = (unsigned char)(pa.raw >> 24);
626 ta.raw[4] = (unsigned char)(pa.raw >> 32);
627 ta.raw[5] = (unsigned char)(pa.raw >> 40);
628 ta.reserv = pa.resvd;
629 }
630}
631
632// Parse a line from a state file.
633static bool parse_dev_state_line(const char * line, persistent_dev_state & state)
634{
635 static const regular_expression regex(
636 "^ *"
637 "((temperature-min)" // (1 (2)
638 "|(temperature-max)" // (3)
639 "|(self-test-errors)" // (4)
640 "|(self-test-last-err-hour)" // (5)
641 "|(scheduled-test-next-check)" // (6)
642 "|(selective-test-last-start)" // (7)
643 "|(selective-test-last-end)" // (8)
644 "|(ata-error-count)" // (9)
645 "|(mail\\.([0-9]+)\\." // (10 (11)
646 "((count)" // (12 (13)
647 "|(first-sent-time)" // (14)
648 "|(last-sent-time)" // (15)
649 ")" // 12)
650 ")" // 10)
651 "|(ata-smart-attribute\\.([0-9]+)\\." // (16 (17)
652 "((id)" // (18 (19)
653 "|(val)" // (20)
654 "|(worst)" // (21)
655 "|(raw)" // (22)
656 "|(resvd)" // (23)
657 ")" // 18)
658 ")" // 16)
659 "|(nvme-err-log-entries)" // (24)
660 ")" // 1)
661 " *= *([0-9]+)[ \n]*$" // (25)
662 );
663
664 const int nmatch = 1+25;
666 if (!regex.execute(line, nmatch, match))
667 return false;
668 if (match[nmatch-1].rm_so < 0)
669 return false;
670
671 uint64_t val = strtoull(line + match[nmatch-1].rm_so, (char **)0, 10);
672
673 int m = 1;
674 if (match[++m].rm_so >= 0)
675 state.tempmin = (unsigned char)val;
676 else if (match[++m].rm_so >= 0)
677 state.tempmax = (unsigned char)val;
678 else if (match[++m].rm_so >= 0)
679 state.selflogcount = (unsigned char)val;
680 else if (match[++m].rm_so >= 0)
681 state.selfloghour = val;
682 else if (match[++m].rm_so >= 0)
683 state.scheduled_test_next_check = (time_t)val;
684 else if (match[++m].rm_so >= 0)
685 state.selective_test_last_start = val;
686 else if (match[++m].rm_so >= 0)
687 state.selective_test_last_end = val;
688 else if (match[++m].rm_so >= 0)
689 state.ataerrorcount = (int)val;
690 else if (match[m+=2].rm_so >= 0) {
691 int i = atoi(line+match[m].rm_so);
692 if (!(0 <= i && i < SMARTD_NMAIL))
693 return false;
694 if (i == MAILTYPE_TEST) // Don't suppress test mails
695 return true;
696 if (match[m+=2].rm_so >= 0)
697 state.maillog[i].logged = (int)val;
698 else if (match[++m].rm_so >= 0)
699 state.maillog[i].firstsent = (time_t)val;
700 else if (match[++m].rm_so >= 0)
701 state.maillog[i].lastsent = (time_t)val;
702 else
703 return false;
704 }
705 else if (match[m+=5+1].rm_so >= 0) {
706 int i = atoi(line+match[m].rm_so);
707 if (!(0 <= i && i < NUMBER_ATA_SMART_ATTRIBUTES))
708 return false;
709 if (match[m+=2].rm_so >= 0)
710 state.ata_attributes[i].id = (unsigned char)val;
711 else if (match[++m].rm_so >= 0)
712 state.ata_attributes[i].val = (unsigned char)val;
713 else if (match[++m].rm_so >= 0)
714 state.ata_attributes[i].worst = (unsigned char)val;
715 else if (match[++m].rm_so >= 0)
716 state.ata_attributes[i].raw = val;
717 else if (match[++m].rm_so >= 0)
718 state.ata_attributes[i].resvd = (unsigned char)val;
719 else
720 return false;
721 }
722 else if (match[m+7].rm_so >= 0)
723 state.nvme_err_log_entries = val;
724 else
725 return false;
726 return true;
727}
728
729// Read a state file.
730static bool read_dev_state(const char * path, persistent_dev_state & state)
731{
732 stdio_file f(path, "r");
733 if (!f) {
734 if (errno != ENOENT)
735 pout("Cannot read state file \"%s\"\n", path);
736 return false;
737 }
738#ifdef __CYGWIN__
739 setmode(fileno(f), O_TEXT); // Allow files with \r\n
740#endif
741
742 persistent_dev_state new_state;
743 int good = 0, bad = 0;
744 char line[256];
745 while (fgets(line, sizeof(line), f)) {
746 const char * s = line + strspn(line, " \t");
747 if (!*s || *s == '#')
748 continue;
749 if (!parse_dev_state_line(line, new_state))
750 bad++;
751 else
752 good++;
753 }
754
755 if (bad) {
756 if (!good) {
757 pout("%s: format error\n", path);
758 return false;
759 }
760 pout("%s: %d invalid line(s) ignored\n", path, bad);
761 }
762
763 // This sets the values missing in the file to 0.
764 state = new_state;
765 return true;
766}
767
768static void write_dev_state_line(FILE * f, const char * name, uint64_t val)
769{
770 if (val)
771 fprintf(f, "%s = %" PRIu64 "\n", name, val);
772}
773
774static void write_dev_state_line(FILE * f, const char * name1, int id, const char * name2, uint64_t val)
775{
776 if (val)
777 fprintf(f, "%s.%d.%s = %" PRIu64 "\n", name1, id, name2, val);
778}
779
780// Write a state file
781static bool write_dev_state(const char * path, const persistent_dev_state & state)
782{
783 // Rename old "file" to "file~"
784 std::string pathbak = path; pathbak += '~';
785 unlink(pathbak.c_str());
786 rename(path, pathbak.c_str());
787
788 stdio_file f(path, "w");
789 if (!f) {
790 pout("Cannot create state file \"%s\"\n", path);
791 return false;
792 }
793
794 fprintf(f, "# smartd state file\n");
795 write_dev_state_line(f, "temperature-min", state.tempmin);
796 write_dev_state_line(f, "temperature-max", state.tempmax);
797 write_dev_state_line(f, "self-test-errors", state.selflogcount);
798 write_dev_state_line(f, "self-test-last-err-hour", state.selfloghour);
799 write_dev_state_line(f, "scheduled-test-next-check", state.scheduled_test_next_check);
800 write_dev_state_line(f, "selective-test-last-start", state.selective_test_last_start);
801 write_dev_state_line(f, "selective-test-last-end", state.selective_test_last_end);
802
803 for (int i = 0; i < SMARTD_NMAIL; i++) {
804 if (i == MAILTYPE_TEST) // Don't suppress test mails
805 continue;
806 const mailinfo & mi = state.maillog[i];
807 if (!mi.logged)
808 continue;
809 write_dev_state_line(f, "mail", i, "count", mi.logged);
810 write_dev_state_line(f, "mail", i, "first-sent-time", mi.firstsent);
811 write_dev_state_line(f, "mail", i, "last-sent-time", mi.lastsent);
812 }
813
814 // ATA ONLY
815 write_dev_state_line(f, "ata-error-count", state.ataerrorcount);
816
817 for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
818 const auto & pa = state.ata_attributes[i];
819 if (!pa.id)
820 continue;
821 write_dev_state_line(f, "ata-smart-attribute", i, "id", pa.id);
822 write_dev_state_line(f, "ata-smart-attribute", i, "val", pa.val);
823 write_dev_state_line(f, "ata-smart-attribute", i, "worst", pa.worst);
824 write_dev_state_line(f, "ata-smart-attribute", i, "raw", pa.raw);
825 write_dev_state_line(f, "ata-smart-attribute", i, "resvd", pa.resvd);
826 }
827
828 // NVMe only
829 write_dev_state_line(f, "nvme-err-log-entries", state.nvme_err_log_entries);
830
831 return true;
832}
833
834// Write to the attrlog file
835static bool write_dev_attrlog(const char * path, const dev_state & state)
836{
837 stdio_file f(path, "a");
838 if (!f) {
839 pout("Cannot create attribute log file \"%s\"\n", path);
840 return false;
841 }
842
843
844 time_t now = time(nullptr);
845 struct tm tmbuf, * tms = time_to_tm_local(&tmbuf, now);
846 fprintf(f, "%d-%02d-%02d %02d:%02d:%02d;",
847 1900+tms->tm_year, 1+tms->tm_mon, tms->tm_mday,
848 tms->tm_hour, tms->tm_min, tms->tm_sec);
849 // ATA ONLY
850 for (const auto & pa : state.ata_attributes) {
851 if (!pa.id)
852 continue;
853 fprintf(f, "\t%d;%d;%" PRIu64 ";", pa.id, pa.val, pa.raw);
854 }
855 // SCSI ONLY
856 const struct scsiErrorCounter * ecp;
857 const char * pageNames[3] = {"read", "write", "verify"};
858 for (int k = 0; k < 3; ++k) {
859 if ( !state.scsi_error_counters[k].found ) continue;
860 ecp = &state.scsi_error_counters[k].errCounter;
861 fprintf(f, "\t%s-corr-by-ecc-fast;%" PRIu64 ";"
862 "\t%s-corr-by-ecc-delayed;%" PRIu64 ";"
863 "\t%s-corr-by-retry;%" PRIu64 ";"
864 "\t%s-total-err-corrected;%" PRIu64 ";"
865 "\t%s-corr-algorithm-invocations;%" PRIu64 ";"
866 "\t%s-gb-processed;%.3f;"
867 "\t%s-total-unc-errors;%" PRIu64 ";",
868 pageNames[k], ecp->counter[0],
869 pageNames[k], ecp->counter[1],
870 pageNames[k], ecp->counter[2],
871 pageNames[k], ecp->counter[3],
872 pageNames[k], ecp->counter[4],
873 pageNames[k], (ecp->counter[5] / 1000000000.0),
874 pageNames[k], ecp->counter[6]);
875 }
876 if(state.scsi_nonmedium_error.found && state.scsi_nonmedium_error.nme.gotPC0) {
877 fprintf(f, "\tnon-medium-errors;%" PRIu64 ";", state.scsi_nonmedium_error.nme.counterPC0);
878 }
879 // write SCSI current temperature if it is monitored
880 if (state.temperature)
881 fprintf(f, "\ttemperature;%d;", state.temperature);
882 // end of line
883 fprintf(f, "\n");
884 return true;
885}
886
887// Write all state files. If write_always is false, don't write
888// unless must_write is set.
889static void write_all_dev_states(const dev_config_vector & configs,
890 dev_state_vector & states,
891 bool write_always = true)
892{
893 for (unsigned i = 0; i < states.size(); i++) {
894 const dev_config & cfg = configs.at(i);
895 if (cfg.state_file.empty())
896 continue;
897 dev_state & state = states[i];
898 if (!write_always && !state.must_write)
899 continue;
900 if (!write_dev_state(cfg.state_file.c_str(), state))
901 continue;
902 state.must_write = false;
903 if (write_always || debugmode)
904 PrintOut(LOG_INFO, "Device: %s, state written to %s\n",
905 cfg.name.c_str(), cfg.state_file.c_str());
906 }
907}
908
909// Write to all attrlog files
910static void write_all_dev_attrlogs(const dev_config_vector & configs,
911 dev_state_vector & states)
912{
913 for (unsigned i = 0; i < states.size(); i++) {
914 const dev_config & cfg = configs.at(i);
915 if (cfg.attrlog_file.empty())
916 continue;
917 dev_state & state = states[i];
918 if (state.attrlog_dirty) {
919 write_dev_attrlog(cfg.attrlog_file.c_str(), state);
920 state.attrlog_dirty = false;
921 }
922 }
923}
924
925extern "C" { // signal handlers require C-linkage
926
927// Note if we catch a SIGUSR1
928static void USR1handler(int sig)
929{
930 if (SIGUSR1==sig)
932 return;
933}
934
935#ifdef _WIN32
936// Note if we catch a SIGUSR2
937static void USR2handler(int sig)
938{
939 if (SIGUSR2==sig)
940 caughtsigUSR2=1;
941 return;
942}
943#endif
944
945// Note if we catch a HUP (or INT in debug mode)
946static void HUPhandler(int sig)
947{
948 if (sig==SIGHUP)
949 caughtsigHUP=1;
950 else
951 caughtsigHUP=2;
952 return;
953}
954
955// signal handler for TERM, QUIT, and INT (if not in debug mode)
956static void sighandler(int sig)
957{
958 if (!caughtsigEXIT)
959 caughtsigEXIT=sig;
960 return;
961}
962
963} // extern "C"
964
965#ifdef HAVE_LIBCAP_NG
966// capabilities(7) support
967
968static int capabilities_mode /* = 0 */; // 1=enabled, 2=mail
969
970static void capabilities_drop_now()
971{
972 if (!capabilities_mode)
973 return;
974 capng_clear(CAPNG_SELECT_BOTH);
975 capng_updatev(CAPNG_ADD, (capng_type_t)(CAPNG_EFFECTIVE|CAPNG_PERMITTED),
976 CAP_SYS_ADMIN, CAP_MKNOD, CAP_SYS_RAWIO, -1);
977 if (warn_as_user && (warn_uid || warn_gid)) {
978 // For popen_as_ugid()
979 capng_updatev(CAPNG_ADD, (capng_type_t)(CAPNG_EFFECTIVE|CAPNG_PERMITTED),
980 CAP_SETGID, CAP_SETUID, -1);
981 }
982 if (capabilities_mode > 1) {
983 // For exim MTA
984 capng_updatev(CAPNG_ADD, CAPNG_BOUNDING_SET,
985 CAP_SETGID, CAP_SETUID, CAP_CHOWN, CAP_FOWNER, CAP_DAC_OVERRIDE, -1);
986 }
987 capng_apply(CAPNG_SELECT_BOTH);
988}
989
990static void capabilities_log_error_hint()
991{
992 if (!capabilities_mode)
993 return;
994 PrintOut(LOG_INFO, "If mail notification does not work with '--capabilities%s\n",
995 (capabilities_mode == 1 ? "', try '--capabilities=mail'"
996 : "=mail', please inform " PACKAGE_BUGREPORT));
997}
998
999#else // HAVE_LIBCAP_NG
1000// No capabilities(7) support
1001
1002static inline void capabilities_drop_now() { }
1003static inline void capabilities_log_error_hint() { }
1004
1005#endif // HAVE_LIBCAP_NG
1006
1007// a replacement for setenv() which is not available on all platforms.
1008// Note that the string passed to putenv must not be freed or made
1009// invalid, since a pointer to it is kept by putenv(). This means that
1010// it must either be a static buffer or allocated off the heap. The
1011// string can be freed if the environment variable is redefined via
1012// another call to putenv(). There is no portable way to unset a variable
1013// with putenv(). So we manage the buffer in a static object.
1014// Using setenv() if available is not considered because some
1015// implementations may produce memory leaks.
1016
1018{
1019public:
1020 env_buffer() = default;
1021 env_buffer(const env_buffer &) = delete;
1022 void operator=(const env_buffer &) = delete;
1023
1024 void set(const char * name, const char * value);
1025private:
1026 char * m_buf = nullptr;
1027};
1028
1029void env_buffer::set(const char * name, const char * value)
1030{
1031 int size = strlen(name) + 1 + strlen(value) + 1;
1032 char * newbuf = new char[size];
1033 snprintf(newbuf, size, "%s=%s", name, value);
1034
1035 if (putenv(newbuf))
1036 throw std::runtime_error("putenv() failed");
1037
1038 // This assumes that the same NAME is passed on each call
1039 delete [] m_buf;
1040 m_buf = newbuf;
1041}
1042
1043#define EBUFLEN 1024
1044
1045static void MailWarning(const dev_config & cfg, dev_state & state, int which, const char *fmt, ...)
1047
1048// If either address or executable path is non-null then send and log
1049// a warning email, or execute executable
1050static void MailWarning(const dev_config & cfg, dev_state & state, int which, const char *fmt, ...)
1051{
1052 // See if user wants us to send mail
1053 if (cfg.emailaddress.empty() && cfg.emailcmdline.empty())
1054 return;
1055
1056 // Which type of mail are we sending?
1057 static const char * const whichfail[] = {
1058 "EmailTest", // 0
1059 "Health", // 1
1060 "Usage", // 2
1061 "SelfTest", // 3
1062 "ErrorCount", // 4
1063 "FailedHealthCheck", // 5
1064 "FailedReadSmartData", // 6
1065 "FailedReadSmartErrorLog", // 7
1066 "FailedReadSmartSelfTestLog", // 8
1067 "FailedOpenDevice", // 9
1068 "CurrentPendingSector", // 10
1069 "OfflineUncorrectableSector", // 11
1070 "Temperature" // 12
1071 };
1072 STATIC_ASSERT(sizeof(whichfail) == SMARTD_NMAIL * sizeof(whichfail[0]));
1073
1074 if (!(0 <= which && which < SMARTD_NMAIL)) {
1075 PrintOut(LOG_CRIT, "Internal error in MailWarning(): which=%d\n", which);
1076 return;
1077 }
1078 mailinfo * mail = state.maillog + which;
1079
1080 // Calc current and next interval for warning reminder emails
1081 int days, nextdays;
1082 if (which == 0)
1083 days = nextdays = -1; // EmailTest
1084 else switch (cfg.emailfreq) {
1085 case emailfreqs::once:
1086 days = nextdays = -1; break;
1087 case emailfreqs::always:
1088 days = nextdays = 0; break;
1089 case emailfreqs::daily:
1090 days = nextdays = 1; break;
1092 // 0, 1, 2, 3, 4, 5, 6, 7, ... => 1, 2, 4, 8, 16, 32, 32, 32, ...
1093 nextdays = 1 << ((unsigned)mail->logged <= 5 ? mail->logged : 5);
1094 // 0, 1, 2, 3, 4, 5, 6, 7, ... => 0, 1, 2, 4, 8, 16, 32, 32, ... (0 not used below)
1095 days = ((unsigned)mail->logged <= 5 ? nextdays >> 1 : nextdays);
1096 break;
1097 default:
1098 PrintOut(LOG_CRIT, "Internal error in MailWarning(): cfg.emailfreq=%d\n", (int)cfg.emailfreq);
1099 return;
1100 }
1101
1102 time_t now = time(nullptr);
1103 if (mail->logged) {
1104 // Return if no warning reminder email needs to be sent (now)
1105 if (days < 0)
1106 return; // '-M once' or EmailTest
1107 if (days > 0 && now < mail->lastsent + days * 24 * 3600)
1108 return; // '-M daily/diminishing' and too early
1109 }
1110 else {
1111 // Record the time of this first email message
1112 mail->firstsent = now;
1113 }
1114
1115 // Record the time of this email message
1116 mail->lastsent = now;
1117
1118 // print warning string into message
1119 // Note: Message length may reach ~300 characters as device names may be
1120 // very long on certain platforms (macOS ~230 characters).
1121 // Message length must not exceed email line length limit, see RFC 5322:
1122 // "... MUST be no more than 998 characters, ... excluding the CRLF."
1123 char message[512];
1124 va_list ap;
1125 va_start(ap, fmt);
1126 vsnprintf(message, sizeof(message), fmt, ap);
1127 va_end(ap);
1128
1129 // replace commas by spaces to separate recipients
1130 std::string address = cfg.emailaddress;
1131 std::replace(address.begin(), address.end(), ',', ' ');
1132
1133 // Export information in environment variables that will be useful
1134 // for user scripts
1135 const char * executable = cfg.emailcmdline.c_str();
1136 static env_buffer env[13];
1137 env[0].set("SMARTD_MAILER", executable);
1138 env[1].set("SMARTD_MESSAGE", message);
1139 char dates[DATEANDEPOCHLEN];
1140 snprintf(dates, sizeof(dates), "%d", mail->logged);
1141 env[2].set("SMARTD_PREVCNT", dates);
1142 dateandtimezoneepoch(dates, mail->firstsent);
1143 env[3].set("SMARTD_TFIRST", dates);
1144 snprintf(dates, DATEANDEPOCHLEN,"%d", (int)mail->firstsent);
1145 env[4].set("SMARTD_TFIRSTEPOCH", dates);
1146 env[5].set("SMARTD_FAILTYPE", whichfail[which]);
1147 env[6].set("SMARTD_ADDRESS", address.c_str());
1148 env[7].set("SMARTD_DEVICESTRING", cfg.name.c_str());
1149
1150 // Allow 'smartctl ... -d $SMARTD_DEVICETYPE $SMARTD_DEVICE'
1151 env[8].set("SMARTD_DEVICETYPE",
1152 (!cfg.dev_type.empty() ? cfg.dev_type.c_str() : "auto"));
1153 env[9].set("SMARTD_DEVICE", cfg.dev_name.c_str());
1154
1155 env[10].set("SMARTD_DEVICEINFO", cfg.dev_idinfo.c_str());
1156 dates[0] = 0;
1157 if (nextdays >= 0)
1158 snprintf(dates, sizeof(dates), "%d", nextdays);
1159 env[11].set("SMARTD_NEXTDAYS", dates);
1160 // Avoid false positive recursion detection by smartd_warning.{sh,cmd}
1161 env[12].set("SMARTD_SUBJECT", "");
1162
1163 // now construct a command to send this as EMAIL
1164 if (!*executable)
1165 executable = "<mail>";
1166 const char * newadd = (!address.empty()? address.c_str() : "<nomailer>");
1167 const char * newwarn = (which? "Warning via" : "Test of");
1168
1169 char command[256];
1170#ifdef _WIN32
1171 // Path may contain spaces
1172 snprintf(command, sizeof(command), "\"%s\" 2>&1", warning_script.c_str());
1173#else
1174 snprintf(command, sizeof(command), "%s 2>&1", warning_script.c_str());
1175#endif
1176
1177 // tell SYSLOG what we are about to do...
1178 PrintOut(LOG_INFO,"%s %s to %s%s ...\n",
1179 (which ? "Sending warning via" : "Executing test of"), executable, newadd,
1180 (
1181#ifdef HAVE_POSIX_API
1182 warn_as_user ?
1183 strprintf(" (uid=%u(%s) gid=%u(%s))",
1184 (unsigned)warn_uid, warn_uname.c_str(),
1185 (unsigned)warn_gid, warn_gname.c_str() ).c_str() :
1186#elif defined(_WIN32)
1187 warn_as_restr_user ? " (restricted user)" :
1188#endif
1189 ""
1190 )
1191 );
1192
1193 // issue the command to send mail or to run the user's executable
1194 errno=0;
1195 FILE * pfp;
1196
1197#ifdef HAVE_POSIX_API
1198 if (warn_as_user) {
1199 pfp = popen_as_ugid(command, "r", warn_uid, warn_gid);
1200 } else
1201#endif
1202 {
1203#ifdef _WIN32
1204 pfp = popen_as_restr_user(command, "r", warn_as_restr_user);
1205#else
1206 pfp = popen(command, "r");
1207#endif
1208 }
1209
1210 if (!pfp)
1211 // failed to popen() mail process
1212 PrintOut(LOG_CRIT,"%s %s to %s: failed (fork or pipe failed, or no memory) %s\n",
1213 newwarn, executable, newadd, errno?strerror(errno):"");
1214 else {
1215 // pipe succeeded!
1216 int len;
1217 char buffer[EBUFLEN];
1218
1219 // if unexpected output on stdout/stderr, null terminate, print, and flush
1220 if ((len=fread(buffer, 1, EBUFLEN, pfp))) {
1221 int count=0;
1222 int newlen = len<EBUFLEN ? len : EBUFLEN-1;
1223 buffer[newlen]='\0';
1224 PrintOut(LOG_CRIT,"%s %s to %s produced unexpected output (%s%d bytes) to STDOUT/STDERR: \n%s\n",
1225 newwarn, executable, newadd, len!=newlen?"here truncated to ":"", newlen, buffer);
1226
1227 // flush pipe if needed
1228 while (fread(buffer, 1, EBUFLEN, pfp) && count<EBUFLEN)
1229 count++;
1230
1231 // tell user that pipe was flushed, or that something is really wrong
1232 if (count && count<EBUFLEN)
1233 PrintOut(LOG_CRIT,"%s %s to %s: flushed remaining STDOUT/STDERR\n",
1234 newwarn, executable, newadd);
1235 else if (count)
1236 PrintOut(LOG_CRIT,"%s %s to %s: more than 1 MB STDOUT/STDERR flushed, breaking pipe\n",
1237 newwarn, executable, newadd);
1238 }
1239
1240 // if something went wrong with mail process, print warning
1241 errno=0;
1242 int status;
1243
1244#ifdef HAVE_POSIX_API
1245 if (warn_as_user) {
1246 status = pclose_as_ugid(pfp);
1247 } else
1248#endif
1249 {
1250 status = pclose(pfp);
1251 }
1252
1253 if (status == -1)
1254 PrintOut(LOG_CRIT,"%s %s to %s: pclose(3) failed %s\n", newwarn, executable, newadd,
1255 errno?strerror(errno):"");
1256 else {
1257 // mail process apparently succeeded. Check and report exit status
1258 if (WIFEXITED(status)) {
1259 // exited 'normally' (but perhaps with nonzero status)
1260 int status8 = WEXITSTATUS(status);
1261 if (status8>128)
1262 PrintOut(LOG_CRIT,"%s %s to %s: failed (32-bit/8-bit exit status: %d/%d) perhaps caught signal %d [%s]\n",
1263 newwarn, executable, newadd, status, status8, status8-128, strsignal(status8-128));
1264 else if (status8) {
1265 PrintOut(LOG_CRIT,"%s %s to %s: failed (32-bit/8-bit exit status: %d/%d)\n",
1266 newwarn, executable, newadd, status, status8);
1268 }
1269 else
1270 PrintOut(LOG_INFO,"%s %s to %s: successful\n", newwarn, executable, newadd);
1271 }
1272
1273 if (WIFSIGNALED(status))
1274 PrintOut(LOG_INFO,"%s %s to %s: exited because of uncaught signal %d [%s]\n",
1275 newwarn, executable, newadd, WTERMSIG(status), strsignal(WTERMSIG(status)));
1276
1277 // this branch is probably not possible. If subprocess is
1278 // stopped then pclose() should not return.
1279 if (WIFSTOPPED(status))
1280 PrintOut(LOG_CRIT,"%s %s to %s: process STOPPED because it caught signal %d [%s]\n",
1281 newwarn, executable, newadd, WSTOPSIG(status), strsignal(WSTOPSIG(status)));
1282
1283 }
1284 }
1285
1286 // increment mail sent counter
1287 mail->logged++;
1288}
1289
1290static void reset_warning_mail(const dev_config & cfg, dev_state & state, int which, const char *fmt, ...)
1292
1293static void reset_warning_mail(const dev_config & cfg, dev_state & state, int which, const char *fmt, ...)
1294{
1295 if (!(0 <= which && which < SMARTD_NMAIL))
1296 return;
1297
1298 // Return if no mail sent yet
1299 mailinfo & mi = state.maillog[which];
1300 if (!mi.logged)
1301 return;
1302
1303 // Format & print message
1304 char msg[256];
1305 va_list ap;
1306 va_start(ap, fmt);
1307 vsnprintf(msg, sizeof(msg), fmt, ap);
1308 va_end(ap);
1309
1310 PrintOut(LOG_INFO, "Device: %s, %s, warning condition reset after %d email%s\n", cfg.name.c_str(),
1311 msg, mi.logged, (mi.logged==1 ? "" : "s"));
1312
1313 // Clear mail counter and timestamps
1314 mi = mailinfo();
1315 state.must_write = true;
1316}
1317
1318#ifndef _WIN32
1319
1320// Output multiple lines via separate syslog(3) calls.
1322static void vsyslog_lines(int priority, const char * fmt, va_list ap)
1323{
1324 char buf[512+EBUFLEN]; // enough space for exec cmd output in MailWarning()
1325 vsnprintf(buf, sizeof(buf), fmt, ap);
1326
1327 for (char * p = buf, * q; p && *p; p = q) {
1328 if ((q = strchr(p, '\n')))
1329 *q++ = 0;
1330 if (*p)
1331 syslog(priority, "%s\n", p);
1332 }
1333}
1334
1335#else // _WIN32
1336// os_win32/syslog_win32.cpp supports multiple lines.
1337#define vsyslog_lines vsyslog
1338#endif // _WIN32
1339
1340// Printing function for watching ataprint commands, or losing them
1341// [From GLIBC Manual: Since the prototype doesn't specify types for
1342// optional arguments, in a call to a variadic function the default
1343// argument promotions are performed on the optional argument
1344// values. This means the objects of type char or short int (whether
1345// signed or not) are promoted to either int or unsigned int, as
1346// appropriate.]
1347void pout(const char *fmt, ...){
1348 va_list ap;
1349
1350 // get the correct time in syslog()
1352 // initialize variable argument list
1353 va_start(ap,fmt);
1354 // in debugmode==1 mode we will print the output from the ataprint.o functions!
1355 if (debugmode && debugmode != 2) {
1356 FILE * f = stdout;
1357#ifdef _WIN32
1358 if (facility == LOG_LOCAL1) // logging to stdout
1359 f = stderr;
1360#endif
1361 vfprintf(f, fmt, ap);
1362 fflush(f);
1363 }
1364 // in debugmode==2 mode we print output from knowndrives.o functions
1365 else if (debugmode==2 || ata_debugmode || scsi_debugmode) {
1366 openlog("smartd", LOG_PID, facility);
1367 vsyslog_lines(LOG_INFO, fmt, ap);
1368 closelog();
1369 }
1370 va_end(ap);
1371 return;
1372}
1373
1374// This function prints either to stdout or to the syslog as needed.
1375static void PrintOut(int priority, const char *fmt, ...){
1376 va_list ap;
1377
1378 // get the correct time in syslog()
1380 // initialize variable argument list
1381 va_start(ap,fmt);
1382 if (debugmode) {
1383 FILE * f = stdout;
1384#ifdef _WIN32
1385 if (facility == LOG_LOCAL1) // logging to stdout
1386 f = stderr;
1387#endif
1388 vfprintf(f, fmt, ap);
1389 fflush(f);
1390 }
1391 else {
1392 openlog("smartd", LOG_PID, facility);
1393 vsyslog_lines(priority, fmt, ap);
1394 closelog();
1395 }
1396 va_end(ap);
1397 return;
1398}
1399
1400// Used to warn users about invalid checksums. Called from atacmds.cpp.
1401void checksumwarning(const char * string)
1402{
1403 pout("Warning! %s error: invalid SMART checksum.\n", string);
1404}
1405
1406#ifndef _WIN32
1407
1408// Wait for the pid file to show up, this makes sure a calling program knows
1409// that the daemon is really up and running and has a pid to kill it
1410static bool WaitForPidFile()
1411{
1412 int waited, max_wait = 10;
1413 struct stat stat_buf;
1414
1415 if (pid_file.empty() || debugmode)
1416 return true;
1417
1418 for(waited = 0; waited < max_wait; ++waited) {
1419 if (!stat(pid_file.c_str(), &stat_buf)) {
1420 return true;
1421 } else
1422 sleep(1);
1423 }
1424 return false;
1425}
1426
1427#endif // _WIN32
1428
1429// Forks new process if needed, closes ALL file descriptors,
1430// redirects stdin, stdout, and stderr. Not quite daemon().
1431// See https://www.linuxjournal.com/article/2335
1432// for a good description of why we do things this way.
1433static int daemon_init()
1434{
1435#ifndef _WIN32
1436
1437 // flush all buffered streams. Else we might get two copies of open
1438 // streams since both parent and child get copies of the buffers.
1439 fflush(nullptr);
1440
1441 if (do_fork) {
1442 pid_t pid;
1443 if ((pid=fork()) < 0) {
1444 // unable to fork!
1445 PrintOut(LOG_CRIT,"smartd unable to fork daemon process!\n");
1446 return EXIT_STARTUP;
1447 }
1448 if (pid) {
1449 // we are the parent process, wait for pid file, then exit cleanly
1450 if(!WaitForPidFile()) {
1451 PrintOut(LOG_CRIT,"PID file %s didn't show up!\n", pid_file.c_str());
1452 return EXIT_STARTUP;
1453 }
1454 return 0;
1455 }
1456
1457 // from here on, we are the child process.
1458 setsid();
1459
1460 // Fork one more time to avoid any possibility of having terminals
1461 if ((pid=fork()) < 0) {
1462 // unable to fork!
1463 PrintOut(LOG_CRIT,"smartd unable to fork daemon process!\n");
1464 return EXIT_STARTUP;
1465 }
1466 if (pid)
1467 // we are the parent process -- exit cleanly
1468 return 0;
1469
1470 // Now we are the child's child...
1471 }
1472
1473 // close any open file descriptors
1474 int open_max = sysconf(_SC_OPEN_MAX);
1475#ifdef HAVE_CLOSE_RANGE
1476 if (close_range(0, open_max - 1, 0))
1477#endif
1478 {
1479 // Limit number of unneeded close() calls under the assumption that
1480 // there are no large gaps between open FDs
1481 for (int i = 0, failed = 0; i < open_max && failed < 1024; i++)
1482 failed = (!close(i) ? 0 : failed + 1);
1483 }
1484
1485 // redirect any IO attempts to /dev/null and change to root directory
1486 int fd = open("/dev/null", O_RDWR);
1487 if (!(fd == 0 && dup(fd) == 1 && dup(fd) == 2 && !chdir("/"))) {
1488 PrintOut(LOG_CRIT, "smartd unable to redirect to /dev/null or to chdir to root!\n");
1489 return EXIT_STARTUP;
1490 }
1491 umask(0022);
1492
1493 if (do_fork)
1494 PrintOut(LOG_INFO, "smartd has fork()ed into background mode. New PID=%d.\n", (int)getpid());
1495
1496#else // _WIN32
1497
1498 // No fork() on native Win32
1499 // Detach this process from console
1500 fflush(nullptr);
1501 if (daemon_detach("smartd")) {
1502 PrintOut(LOG_CRIT,"smartd unable to detach from console!\n");
1503 return EXIT_STARTUP;
1504 }
1505 // stdin/out/err now closed if not redirected
1506
1507#endif // _WIN32
1508
1509 // No error, continue in main_worker()
1510 return -1;
1511}
1512
1513// create a PID file containing the current process id
1514static bool write_pid_file()
1515{
1516 if (!pid_file.empty()) {
1517 pid_t pid = getpid();
1518 mode_t old_umask;
1519#ifndef __CYGWIN__
1520 old_umask = umask(0077); // rwx------
1521#else
1522 // Cygwin: smartd service runs on system account, ensure PID file can be read by admins
1523 old_umask = umask(0033); // rwxr--r--
1524#endif
1525
1526 stdio_file f(pid_file.c_str(), "w");
1527 umask(old_umask);
1528 if (!(f && fprintf(f, "%d\n", (int)pid) > 0 && f.close())) {
1529 PrintOut(LOG_CRIT, "unable to write PID file %s - exiting.\n", pid_file.c_str());
1530 return false;
1531 }
1532 PrintOut(LOG_INFO, "file %s written containing PID %d\n", pid_file.c_str(), (int)pid);
1533 }
1534 return true;
1535}
1536
1537// Prints header identifying version of code and home
1538static void PrintHead()
1539{
1540 PrintOut(LOG_INFO, "%s\n", format_version_info("smartd").c_str());
1541}
1542
1543// prints help info for configuration file Directives
1544static void Directives()
1545{
1546 PrintOut(LOG_INFO,
1547 "Configuration file (%s) Directives (after device name):\n"
1548 " -d TYPE Set the device type: auto, ignore, removable,\n"
1549 " %s\n"
1550 " -T TYPE Set the tolerance to one of: normal, permissive\n"
1551 " -o VAL Enable/disable automatic offline tests (on/off)\n"
1552 " -S VAL Enable/disable attribute autosave (on/off)\n"
1553 " -n MODE No check if: never, sleep[,N][,q], standby[,N][,q], idle[,N][,q]\n"
1554 " -H Monitor SMART Health Status, report if failed\n"
1555 " -s REG Do Self-Test at time(s) given by regular expression REG\n"
1556 " -l TYPE Monitor SMART log or self-test status:\n"
1557 " error, selftest, xerror, offlinests[,ns], selfteststs[,ns]\n"
1558 " -l scterc,R,W Set SCT Error Recovery Control\n"
1559 " -e Change device setting: aam,[N|off], apm,[N|off], dsn,[on|off],\n"
1560 " lookahead,[on|off], security-freeze, standby,[N|off], wcache,[on|off]\n"
1561 " -f Monitor 'Usage' Attributes, report failures\n"
1562 " -m ADD Send email warning to address ADD\n"
1563 " -M TYPE Modify email warning behavior (see man page)\n"
1564 " -p Report changes in 'Prefailure' Attributes\n"
1565 " -u Report changes in 'Usage' Attributes\n"
1566 " -t Equivalent to -p and -u Directives\n"
1567 " -r ID Also report Raw values of Attribute ID with -p, -u or -t\n"
1568 " -R ID Track changes in Attribute ID Raw value with -p, -u or -t\n"
1569 " -i ID Ignore Attribute ID for -f Directive\n"
1570 " -I ID Ignore Attribute ID for -p, -u or -t Directive\n"
1571 " -C ID[+] Monitor [increases of] Current Pending Sectors in Attribute ID\n"
1572 " -U ID[+] Monitor [increases of] Offline Uncorrectable Sectors in Attribute ID\n"
1573 " -W D,I,C Monitor Temperature D)ifference, I)nformal limit, C)ritical limit\n"
1574 " -v N,ST Modifies labeling of Attribute N (see man page) \n"
1575 " -P TYPE Drive-specific presets: use, ignore, show, showall\n"
1576 " -a Default: -H -f -t -l error -l selftest -l selfteststs -C 197 -U 198\n"
1577 " -F TYPE Use firmware bug workaround:\n"
1578 " %s\n"
1579 " -c i=N Set interval between disk checks to N seconds\n"
1580 " # Comment: text after a hash sign is ignored\n"
1581 " \\ Line continuation character\n"
1582 "Attribute ID is a decimal integer 1 <= ID <= 255\n"
1583 "Use ID = 0 to turn off -C and/or -U Directives\n"
1584 "Example: /dev/sda -a\n",
1585 configfile,
1586 smi()->get_valid_dev_types_str().c_str(),
1588}
1589
1590/* Returns a pointer to a static string containing a formatted list of the valid
1591 arguments to the option opt or nullptr on failure. */
1592static const char *GetValidArgList(char opt)
1593{
1594 switch (opt) {
1595 case 'A':
1596 case 's':
1597 return "<PATH_PREFIX>, -";
1598 case 'B':
1599 return "[+]<FILE_NAME>";
1600 case 'c':
1601 return "<FILE_NAME>, -";
1602 case 'l':
1603 return "daemon, local0, local1, local2, local3, local4, local5, local6, local7";
1604 case 'q':
1605 return "nodev[0], errors[,nodev0], nodev[0]startup, never, onecheck, showtests";
1606 case 'r':
1607 return "ioctl[,N], ataioctl[,N], scsiioctl[,N], nvmeioctl[,N]";
1608 case 'p':
1609 case 'w':
1610 return "<FILE_NAME>";
1611 case 'i':
1612 return "<INTEGER_SECONDS>";
1613#ifdef HAVE_POSIX_API
1614 case 'u':
1615 return "<USER>[:<GROUP>], -";
1616#elif defined(_WIN32)
1617 case 'u':
1618 return "restricted, unchanged";
1619#endif
1620#ifdef HAVE_LIBCAP_NG
1621 case 'C':
1622 return "mail, <no_argument>";
1623#endif
1624 default:
1625 return nullptr;
1626 }
1627}
1628
1629/* prints help information for command syntax */
1630static void Usage()
1631{
1632 PrintOut(LOG_INFO,"Usage: smartd [options]\n\n");
1633#ifdef SMARTMONTOOLS_ATTRIBUTELOG
1634 PrintOut(LOG_INFO," -A PREFIX|-, --attributelog=PREFIX|-\n");
1635#else
1636 PrintOut(LOG_INFO," -A PREFIX, --attributelog=PREFIX\n");
1637#endif
1638 PrintOut(LOG_INFO," Log attribute information to {PREFIX}MODEL-SERIAL.TYPE.csv\n");
1639#ifdef SMARTMONTOOLS_ATTRIBUTELOG
1640 PrintOut(LOG_INFO," [default is " SMARTMONTOOLS_ATTRIBUTELOG "MODEL-SERIAL.TYPE.csv]\n");
1641#endif
1642 PrintOut(LOG_INFO,"\n");
1643 PrintOut(LOG_INFO," -B [+]FILE, --drivedb=[+]FILE\n");
1644 PrintOut(LOG_INFO," Read and replace [add] drive database from FILE\n");
1645 PrintOut(LOG_INFO," [default is +%s", get_drivedb_path_add());
1646#ifdef SMARTMONTOOLS_DRIVEDBDIR
1647 PrintOut(LOG_INFO,"\n");
1648 PrintOut(LOG_INFO," and then %s", get_drivedb_path_default());
1649#endif
1650 PrintOut(LOG_INFO,"]\n\n");
1651 PrintOut(LOG_INFO," -c NAME|-, --configfile=NAME|-\n");
1652 PrintOut(LOG_INFO," Read configuration file NAME or stdin\n");
1653 PrintOut(LOG_INFO," [default is %s]\n\n", configfile);
1654#ifdef HAVE_LIBCAP_NG
1655 PrintOut(LOG_INFO," -C, --capabilities[=mail]\n");
1656 PrintOut(LOG_INFO," Drop unneeded Linux process capabilities.\n"
1657 " Warning: Mail notification may not work when used.\n\n");
1658#endif
1659 PrintOut(LOG_INFO," -d, --debug\n");
1660 PrintOut(LOG_INFO," Start smartd in debug mode\n\n");
1661 PrintOut(LOG_INFO," -D, --showdirectives\n");
1662 PrintOut(LOG_INFO," Print the configuration file Directives and exit\n\n");
1663 PrintOut(LOG_INFO," -h, --help, --usage\n");
1664 PrintOut(LOG_INFO," Display this help and exit\n\n");
1665 PrintOut(LOG_INFO," -i N, --interval=N\n");
1666 PrintOut(LOG_INFO," Set interval between disk checks to N seconds, where N >= 10\n\n");
1667 PrintOut(LOG_INFO," -l local[0-7], --logfacility=local[0-7]\n");
1668#ifndef _WIN32
1669 PrintOut(LOG_INFO," Use syslog facility local0 - local7 or daemon [default]\n\n");
1670#else
1671 PrintOut(LOG_INFO," Log to \"./smartd.log\", stdout, stderr [default is event log]\n\n");
1672#endif
1673#ifndef _WIN32
1674 PrintOut(LOG_INFO," -n, --no-fork\n");
1675 PrintOut(LOG_INFO," Do not fork into background\n");
1676#ifdef HAVE_LIBSYSTEMD
1677 PrintOut(LOG_INFO," (systemd 'Type=notify' is assumed if $NOTIFY_SOCKET is set)\n");
1678#endif // HAVE_LIBSYSTEMD
1679 PrintOut(LOG_INFO,"\n");
1680#endif // WIN32
1681 PrintOut(LOG_INFO," -p NAME, --pidfile=NAME\n");
1682 PrintOut(LOG_INFO," Write PID file NAME\n\n");
1683 PrintOut(LOG_INFO," -q WHEN, --quit=WHEN\n");
1684 PrintOut(LOG_INFO," Quit on one of: %s\n\n", GetValidArgList('q'));
1685 PrintOut(LOG_INFO," -r, --report=TYPE\n");
1686 PrintOut(LOG_INFO," Report transactions for one of: %s\n\n", GetValidArgList('r'));
1687#ifdef SMARTMONTOOLS_SAVESTATES
1688 PrintOut(LOG_INFO," -s PREFIX|-, --savestates=PREFIX|-\n");
1689#else
1690 PrintOut(LOG_INFO," -s PREFIX, --savestates=PREFIX\n");
1691#endif
1692 PrintOut(LOG_INFO," Save disk states to {PREFIX}MODEL-SERIAL.TYPE.state\n");
1693#ifdef SMARTMONTOOLS_SAVESTATES
1694 PrintOut(LOG_INFO," [default is " SMARTMONTOOLS_SAVESTATES "MODEL-SERIAL.TYPE.state]\n");
1695#endif
1696 PrintOut(LOG_INFO,"\n");
1697 PrintOut(LOG_INFO," -w NAME, --warnexec=NAME\n");
1698 PrintOut(LOG_INFO," Run executable NAME on warnings\n");
1699#ifndef _WIN32
1700 PrintOut(LOG_INFO," [default is " SMARTMONTOOLS_SMARTDSCRIPTDIR "/smartd_warning.sh]\n\n");
1701#else
1702 PrintOut(LOG_INFO," [default is %s/smartd_warning.cmd]\n\n", get_exe_dir().c_str());
1703#endif
1704#ifdef HAVE_POSIX_API
1705 PrintOut(LOG_INFO," -u USER[:GROUP], --warn-as-user=USER[:GROUP]\n");
1706 PrintOut(LOG_INFO," Run warning script as non-privileged USER\n\n");
1707#elif defined(_WIN32)
1708 PrintOut(LOG_INFO," -u MODE, --warn-as-user=MODE\n");
1709 PrintOut(LOG_INFO," Run warning script with modified access token: %s\n\n", GetValidArgList('u'));
1710#endif
1711#ifdef _WIN32
1712 PrintOut(LOG_INFO," --service\n");
1713 PrintOut(LOG_INFO," Running as windows service (see man page), install with:\n");
1714 PrintOut(LOG_INFO," smartd install [options]\n");
1715 PrintOut(LOG_INFO," Remove service with:\n");
1716 PrintOut(LOG_INFO," smartd remove\n\n");
1717#endif // _WIN32
1718 PrintOut(LOG_INFO," -V, --version, --license, --copyright\n");
1719 PrintOut(LOG_INFO," Print License, Copyright, and version information\n");
1720}
1721
1722static int CloseDevice(smart_device * device, const char * name)
1723{
1724 if (!device->close()){
1725 PrintOut(LOG_INFO,"Device: %s, %s, close() failed\n", name, device->get_errmsg());
1726 return 1;
1727 }
1728 // device successfully closed
1729 return 0;
1730}
1731
1732// Replace invalid characters in cfg.dev_idinfo
1733static bool sanitize_dev_idinfo(std::string & s)
1734{
1735 bool changed = false;
1736 for (unsigned i = 0; i < s.size(); i++) {
1737 char c = s[i];
1738 STATIC_ASSERT(' ' == 0x20 && '~' == 0x07e); // Assume ASCII
1739 // Don't pass possible command escapes ('~! COMMAND') to the 'mail' command.
1740 if ((' ' <= c && c <= '~') && !(i == 0 && c == '~'))
1741 continue;
1742 s[i] = '?';
1743 changed = true;
1744 }
1745 return changed;
1746}
1747
1748// return true if a char is not allowed in a state file name
1749static bool not_allowed_in_filename(char c)
1750{
1751 return !( ('0' <= c && c <= '9')
1752 || ('A' <= c && c <= 'Z')
1753 || ('a' <= c && c <= 'z'));
1754}
1755
1756// Read error count from Summary or Extended Comprehensive SMART error log
1757// Return -1 on error
1758static int read_ata_error_count(ata_device * device, const char * name,
1759 firmwarebug_defs firmwarebugs, bool extended)
1760{
1761 if (!extended) {
1763 if (ataReadErrorLog(device, &log, firmwarebugs)){
1764 PrintOut(LOG_INFO,"Device: %s, Read Summary SMART Error Log failed\n",name);
1765 return -1;
1766 }
1767 return (log.error_log_pointer ? log.ata_error_count : 0);
1768 }
1769 else {
1771 if (!ataReadExtErrorLog(device, &logx, 0, 1 /*first sector only*/, firmwarebugs)) {
1772 PrintOut(LOG_INFO,"Device: %s, Read Extended Comprehensive SMART Error Log failed\n",name);
1773 return -1;
1774 }
1775 // Some disks use the reserved byte as index, see ataprint.cpp.
1776 return (logx.error_log_index || logx.reserved1 ? logx.device_error_count : 0);
1777 }
1778}
1779
1780// Count error entries in ATA self-test log, set HOUR to power on hours of most
1781// recent error. Return error count or -1 on failure.
1782static int check_ata_self_test_log(ata_device * device, const char * name,
1783 firmwarebug_defs firmwarebugs,
1784 unsigned & hour)
1785{
1786 struct ata_smart_selftestlog log;
1787
1788 hour = 0;
1789 if (ataReadSelfTestLog(device, &log, firmwarebugs)){
1790 PrintOut(LOG_INFO,"Device: %s, Read SMART Self Test Log Failed\n",name);
1791 return -1;
1792 }
1793
1794 if (!log.mostrecenttest)
1795 // No tests logged
1796 return 0;
1797
1798 // Count failed self-tests
1799 int errcnt = 0;
1800 for (int i = 20; i >= 0; i--) {
1801 int j = (i + log.mostrecenttest) % 21;
1803 if (!nonempty(&entry, sizeof(entry)))
1804 continue;
1805
1806 int status = entry.selfteststatus >> 4;
1807 if (status == 0x0 && (entry.selftestnumber & 0x7f) == 0x02)
1808 // First successful extended self-test, stop count
1809 break;
1810
1811 if (0x3 <= status && status <= 0x8) {
1812 // Self-test showed an error
1813 errcnt++;
1814 // Keep track of time of most recent error
1815 if (!hour)
1816 hour = entry.timestamp;
1817 }
1818 }
1819
1820 return errcnt;
1821}
1822
1823// Check offline data collection status
1824static inline bool is_offl_coll_in_progress(unsigned char status)
1825{
1826 return ((status & 0x7f) == 0x03);
1827}
1828
1829// Check self-test execution status
1830static inline bool is_self_test_in_progress(unsigned char status)
1831{
1832 return ((status >> 4) == 0xf);
1833}
1834
1835// Log offline data collection status
1836static void log_offline_data_coll_status(const char * name, unsigned char status)
1837{
1838 const char * msg;
1839 switch (status & 0x7f) {
1840 case 0x00: msg = "was never started"; break;
1841 case 0x02: msg = "was completed without error"; break;
1842 case 0x03: msg = "is in progress"; break;
1843 case 0x04: msg = "was suspended by an interrupting command from host"; break;
1844 case 0x05: msg = "was aborted by an interrupting command from host"; break;
1845 case 0x06: msg = "was aborted by the device with a fatal error"; break;
1846 default: msg = nullptr;
1847 }
1848
1849 if (msg)
1850 PrintOut(((status & 0x7f) == 0x06 ? LOG_CRIT : LOG_INFO),
1851 "Device: %s, offline data collection %s%s\n", name, msg,
1852 ((status & 0x80) ? " (auto:on)" : ""));
1853 else
1854 PrintOut(LOG_INFO, "Device: %s, unknown offline data collection status 0x%02x\n",
1855 name, status);
1856}
1857
1858// Log self-test execution status
1859static void log_self_test_exec_status(const char * name, unsigned char status)
1860{
1861 const char * msg;
1862 switch (status >> 4) {
1863 case 0x0: msg = "completed without error"; break;
1864 case 0x1: msg = "was aborted by the host"; break;
1865 case 0x2: msg = "was interrupted by the host with a reset"; break;
1866 case 0x3: msg = "could not complete due to a fatal or unknown error"; break;
1867 case 0x4: msg = "completed with error (unknown test element)"; break;
1868 case 0x5: msg = "completed with error (electrical test element)"; break;
1869 case 0x6: msg = "completed with error (servo/seek test element)"; break;
1870 case 0x7: msg = "completed with error (read test element)"; break;
1871 case 0x8: msg = "completed with error (handling damage?)"; break;
1872 default: msg = nullptr;
1873 }
1874
1875 if (msg)
1876 PrintOut(((status >> 4) >= 0x4 ? LOG_CRIT : LOG_INFO),
1877 "Device: %s, previous self-test %s\n", name, msg);
1878 else if ((status >> 4) == 0xf)
1879 PrintOut(LOG_INFO, "Device: %s, self-test in progress, %u0%% remaining\n",
1880 name, status & 0x0f);
1881 else
1882 PrintOut(LOG_INFO, "Device: %s, unknown self-test status 0x%02x\n",
1883 name, status);
1884}
1885
1886// Check pending sector count id (-C, -U directives).
1887static bool check_pending_id(const dev_config & cfg, const dev_state & state,
1888 unsigned char id, const char * msg)
1889{
1890 // Check attribute index
1891 int i = ata_find_attr_index(id, state.smartval);
1892 if (i < 0) {
1893 PrintOut(LOG_INFO, "Device: %s, can't monitor %s count - no Attribute %d\n",
1894 cfg.name.c_str(), msg, id);
1895 return false;
1896 }
1897
1898 // Check value
1899 uint64_t rawval = ata_get_attr_raw_value(state.smartval.vendor_attributes[i],
1900 cfg.attribute_defs);
1901 if (rawval >= (state.num_sectors ? state.num_sectors : 0xffffffffULL)) {
1902 PrintOut(LOG_INFO, "Device: %s, ignoring %s count - bogus Attribute %d value %" PRIu64 " (0x%" PRIx64 ")\n",
1903 cfg.name.c_str(), msg, id, rawval, rawval);
1904 return false;
1905 }
1906
1907 return true;
1908}
1909
1910// Called by ATA/SCSI/NVMeDeviceScan() after successful device check
1911static void finish_device_scan(dev_config & cfg, dev_state & state)
1912{
1913 // Set cfg.emailfreq if user hasn't set it
1914 if ((!cfg.emailaddress.empty() || !cfg.emailcmdline.empty()) && cfg.emailfreq == emailfreqs::unknown) {
1915 // Avoid that emails are suppressed forever due to state persistence
1916 if (cfg.state_file.empty())
1918 else
1920 }
1921
1922 // Start self-test regex check now if time was not read from state file
1923 if (!cfg.test_regex.empty() && !state.scheduled_test_next_check)
1924 state.scheduled_test_next_check = time(nullptr);
1925}
1926
1927// Common function to format result message for ATA setting
1928static void format_set_result_msg(std::string & msg, const char * name, bool ok,
1929 int set_option = 0, bool has_value = false)
1930{
1931 if (!msg.empty())
1932 msg += ", ";
1933 msg += name;
1934 if (!ok)
1935 msg += ":--";
1936 else if (set_option < 0)
1937 msg += ":off";
1938 else if (has_value)
1939 msg += strprintf(":%d", set_option-1);
1940 else if (set_option > 0)
1941 msg += ":on";
1942}
1943
1944// Return true and print message if CFG.dev_idinfo is already in PREV_CFGS
1945static bool is_duplicate_dev_idinfo(const dev_config & cfg, const dev_config_vector & prev_cfgs)
1946{
1947 if (!cfg.id_is_unique)
1948 return false;
1949
1950 for (const auto & prev_cfg : prev_cfgs) {
1951 if (!prev_cfg.id_is_unique)
1952 continue;
1953 if (!( cfg.dev_idinfo == prev_cfg.dev_idinfo
1954 // Also check identity without NSID if device does not support multiple namespaces
1955 || (!cfg.dev_idinfo_bc.empty() && cfg.dev_idinfo_bc == prev_cfg.dev_idinfo)
1956 || (!prev_cfg.dev_idinfo_bc.empty() && cfg.dev_idinfo == prev_cfg.dev_idinfo_bc)))
1957 continue;
1958
1959 PrintOut(LOG_INFO, "Device: %s, same identity as %s, ignored\n",
1960 cfg.dev_name.c_str(), prev_cfg.dev_name.c_str());
1961 return true;
1962 }
1963
1964 return false;
1965}
1966
1967// TODO: Add '-F swapid' directive
1968const bool fix_swapped_id = false;
1969
1970// scan to see what ata devices there are, and if they support SMART
1971static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atadev,
1972 const dev_config_vector * prev_cfgs)
1973{
1974 int supported=0;
1975 struct ata_identify_device drive;
1976 const char *name = cfg.name.c_str();
1977 int retid;
1978
1979 // Device must be open
1980
1981 // Get drive identity structure
1982 if ((retid = ata_read_identity(atadev, &drive, fix_swapped_id))) {
1983 if (retid<0)
1984 // Unable to read Identity structure
1985 PrintOut(LOG_INFO,"Device: %s, not ATA, no IDENTIFY DEVICE Structure\n",name);
1986 else
1987 PrintOut(LOG_INFO,"Device: %s, packet devices [this device %s] not SMART capable\n",
1988 name, packetdevicetype(retid-1));
1989 CloseDevice(atadev, name);
1990 return 2;
1991 }
1992
1993 // Get drive identity, size and rotation rate (HDD/SSD)
1994 char model[40+1], serial[20+1], firmware[8+1];
1995 ata_format_id_string(model, drive.model, sizeof(model)-1);
1996 ata_format_id_string(serial, drive.serial_no, sizeof(serial)-1);
1997 ata_format_id_string(firmware, drive.fw_rev, sizeof(firmware)-1);
1998
1999 ata_size_info sizes;
2000 ata_get_size_info(&drive, sizes);
2001 state.num_sectors = sizes.sectors;
2002 cfg.dev_rpm = ata_get_rotation_rate(&drive);
2003
2004 char wwn[64]; wwn[0] = 0;
2005 unsigned oui = 0; uint64_t unique_id = 0;
2006 int naa = ata_get_wwn(&drive, oui, unique_id);
2007 if (naa >= 0)
2008 snprintf(wwn, sizeof(wwn), "WWN:%x-%06x-%09" PRIx64 ", ", naa, oui, unique_id);
2009
2010 // Format device id string for warning emails
2011 char cap[32];
2012 cfg.dev_idinfo = strprintf("%s, S/N:%s, %sFW:%s, %s", model, serial, wwn, firmware,
2013 format_capacity(cap, sizeof(cap), sizes.capacity, "."));
2014 cfg.id_is_unique = true; // TODO: Check serial?
2016 cfg.id_is_unique = false;
2017
2018 PrintOut(LOG_INFO, "Device: %s, %s\n", name, cfg.dev_idinfo.c_str());
2019
2020 // Check for duplicates
2021 if (prev_cfgs && is_duplicate_dev_idinfo(cfg, *prev_cfgs)) {
2022 CloseDevice(atadev, name);
2023 return 1;
2024 }
2025
2026 // Show if device in database, and use preset vendor attribute
2027 // options unless user has requested otherwise.
2028 if (cfg.ignorepresets)
2029 PrintOut(LOG_INFO, "Device: %s, smartd database not searched (Directive: -P ignore).\n", name);
2030 else {
2031 // Apply vendor specific presets, print warning if present
2032 std::string dbversion;
2034 &drive, cfg.attribute_defs, cfg.firmwarebugs, dbversion);
2035 if (!dbentry)
2036 PrintOut(LOG_INFO, "Device: %s, not found in smartd database%s%s.\n", name,
2037 (!dbversion.empty() ? " " : ""), (!dbversion.empty() ? dbversion.c_str() : ""));
2038 else {
2039 PrintOut(LOG_INFO, "Device: %s, found in smartd database%s%s%s%s\n",
2040 name, (!dbversion.empty() ? " " : ""), (!dbversion.empty() ? dbversion.c_str() : ""),
2041 (*dbentry->modelfamily ? ": " : "."), (*dbentry->modelfamily ? dbentry->modelfamily : ""));
2042 if (*dbentry->warningmsg)
2043 PrintOut(LOG_CRIT, "Device: %s, WARNING: %s\n", name, dbentry->warningmsg);
2044 }
2045 }
2046
2047 // Check for ATA Security LOCK
2048 unsigned short word128 = drive.words088_255[128-88];
2049 bool locked = ((word128 & 0x0007) == 0x0007); // LOCKED|ENABLED|SUPPORTED
2050 if (locked)
2051 PrintOut(LOG_INFO, "Device: %s, ATA Security is **LOCKED**\n", name);
2052
2053 // Set default '-C 197[+]' if no '-C ID' is specified.
2054 if (!cfg.curr_pending_set)
2056 // Set default '-U 198[+]' if no '-U ID' is specified.
2057 if (!cfg.offl_pending_set)
2059
2060 // If requested, show which presets would be used for this drive
2061 if (cfg.showpresets) {
2062 int savedebugmode=debugmode;
2063 PrintOut(LOG_INFO, "Device %s: presets are:\n", name);
2064 if (!debugmode)
2065 debugmode=2;
2066 show_presets(&drive);
2067 debugmode=savedebugmode;
2068 }
2069
2070 // see if drive supports SMART
2071 supported=ataSmartSupport(&drive);
2072 if (supported!=1) {
2073 if (supported==0)
2074 // drive does NOT support SMART
2075 PrintOut(LOG_INFO,"Device: %s, lacks SMART capability\n",name);
2076 else
2077 // can't tell if drive supports SMART
2078 PrintOut(LOG_INFO,"Device: %s, ATA IDENTIFY DEVICE words 82-83 don't specify if SMART capable.\n",name);
2079
2080 // should we proceed anyway?
2081 if (cfg.permissive) {
2082 PrintOut(LOG_INFO,"Device: %s, proceeding since '-T permissive' Directive given.\n",name);
2083 }
2084 else {
2085 PrintOut(LOG_INFO,"Device: %s, to proceed anyway, use '-T permissive' Directive.\n",name);
2086 CloseDevice(atadev, name);
2087 return 2;
2088 }
2089 }
2090
2091 if (ataEnableSmart(atadev)) {
2092 // Enable SMART command has failed
2093 PrintOut(LOG_INFO,"Device: %s, could not enable SMART capability\n",name);
2094
2095 if (ataIsSmartEnabled(&drive) <= 0) {
2096 if (!cfg.permissive) {
2097 PrintOut(LOG_INFO, "Device: %s, to proceed anyway, use '-T permissive' Directive.\n", name);
2098 CloseDevice(atadev, name);
2099 return 2;
2100 }
2101 PrintOut(LOG_INFO, "Device: %s, proceeding since '-T permissive' Directive given.\n", name);
2102 }
2103 else {
2104 PrintOut(LOG_INFO, "Device: %s, proceeding since SMART is already enabled\n", name);
2105 }
2106 }
2107
2108 // disable device attribute autosave...
2109 if (cfg.autosave==1) {
2110 if (ataDisableAutoSave(atadev))
2111 PrintOut(LOG_INFO,"Device: %s, could not disable SMART Attribute Autosave.\n",name);
2112 else
2113 PrintOut(LOG_INFO,"Device: %s, disabled SMART Attribute Autosave.\n",name);
2114 }
2115
2116 // or enable device attribute autosave
2117 if (cfg.autosave==2) {
2118 if (ataEnableAutoSave(atadev))
2119 PrintOut(LOG_INFO,"Device: %s, could not enable SMART Attribute Autosave.\n",name);
2120 else
2121 PrintOut(LOG_INFO,"Device: %s, enabled SMART Attribute Autosave.\n",name);
2122 }
2123
2124 // capability check: SMART status
2125 if (cfg.smartcheck && ataSmartStatus2(atadev) == -1) {
2126 PrintOut(LOG_INFO,"Device: %s, not capable of SMART Health Status check\n",name);
2127 cfg.smartcheck = false;
2128 }
2129
2130 // capability check: Read smart values and thresholds. Note that
2131 // smart values are ALSO needed even if we ONLY want to know if the
2132 // device is self-test log or error-log capable! After ATA-5, this
2133 // information was ALSO reproduced in the IDENTIFY DEVICE response,
2134 // but sadly not for ATA-5. Sigh.
2135
2136 // do we need to get SMART data?
2137 bool smart_val_ok = false;
2138 if ( cfg.autoofflinetest || 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 || cfg.curr_pending_id || cfg.offl_pending_id ) {
2144
2145 if (ataReadSmartValues(atadev, &state.smartval)) {
2146 PrintOut(LOG_INFO, "Device: %s, Read SMART Values failed\n", name);
2147 cfg.usagefailed = cfg.prefail = cfg.usage = false;
2148 cfg.tempdiff = cfg.tempinfo = cfg.tempcrit = 0;
2149 cfg.curr_pending_id = cfg.offl_pending_id = 0;
2150 }
2151 else {
2152 smart_val_ok = true;
2153 if (ataReadSmartThresholds(atadev, &state.smartthres)) {
2154 PrintOut(LOG_INFO, "Device: %s, Read SMART Thresholds failed%s\n",
2155 name, (cfg.usagefailed ? ", ignoring -f Directive" : ""));
2156 cfg.usagefailed = false;
2157 // Let ata_get_attr_state() return ATTRSTATE_NO_THRESHOLD:
2158 memset(&state.smartthres, 0, sizeof(state.smartthres));
2159 }
2160 }
2161
2162 // see if the necessary Attribute is there to monitor offline or
2163 // current pending sectors or temperature
2164 if ( cfg.curr_pending_id
2165 && !check_pending_id(cfg, state, cfg.curr_pending_id,
2166 "Current_Pending_Sector"))
2167 cfg.curr_pending_id = 0;
2168
2169 if ( cfg.offl_pending_id
2170 && !check_pending_id(cfg, state, cfg.offl_pending_id,
2171 "Offline_Uncorrectable"))
2172 cfg.offl_pending_id = 0;
2173
2174 if ( (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)
2176 PrintOut(LOG_INFO, "Device: %s, can't monitor Temperature, ignoring -W %d,%d,%d\n",
2177 name, cfg.tempdiff, cfg.tempinfo, cfg.tempcrit);
2178 cfg.tempdiff = cfg.tempinfo = cfg.tempcrit = 0;
2179 }
2180
2181 // Report ignored '-r' or '-R' directives
2182 for (int id = 1; id <= 255; id++) {
2184 char opt = (!cfg.monitor_attr_flags.is_set(id, MONITOR_RAW) ? 'r' : 'R');
2185 const char * excl = (cfg.monitor_attr_flags.is_set(id,
2186 (opt == 'r' ? MONITOR_AS_CRIT : MONITOR_RAW_AS_CRIT)) ? "!" : "");
2187
2188 int idx = ata_find_attr_index(id, state.smartval);
2189 if (idx < 0)
2190 PrintOut(LOG_INFO,"Device: %s, no Attribute %d, ignoring -%c %d%s\n", name, id, opt, id, excl);
2191 else {
2192 bool prefail = !!ATTRIBUTE_FLAGS_PREFAILURE(state.smartval.vendor_attributes[idx].flags);
2193 if (!((prefail && cfg.prefail) || (!prefail && cfg.usage)))
2194 PrintOut(LOG_INFO,"Device: %s, not monitoring %s Attributes, ignoring -%c %d%s\n", name,
2195 (prefail ? "Prefailure" : "Usage"), opt, id, excl);
2196 }
2197 }
2198 }
2199 }
2200
2201 // enable/disable automatic on-line testing
2202 if (cfg.autoofflinetest) {
2203 // is this an enable or disable request?
2204 const char *what=(cfg.autoofflinetest==1)?"disable":"enable";
2205 if (!smart_val_ok)
2206 PrintOut(LOG_INFO,"Device: %s, could not %s SMART Automatic Offline Testing.\n",name, what);
2207 else {
2208 // if command appears unsupported, issue a warning...
2209 if (!isSupportAutomaticTimer(&state.smartval))
2210 PrintOut(LOG_INFO,"Device: %s, SMART Automatic Offline Testing unsupported...\n",name);
2211 // ... but then try anyway
2212 if ((cfg.autoofflinetest==1)?ataDisableAutoOffline(atadev):ataEnableAutoOffline(atadev))
2213 PrintOut(LOG_INFO,"Device: %s, %s SMART Automatic Offline Testing failed.\n", name, what);
2214 else
2215 PrintOut(LOG_INFO,"Device: %s, %sd SMART Automatic Offline Testing.\n", name, what);
2216 }
2217 }
2218
2219 // Read log directories if required for capability check
2220 ata_smart_log_directory smart_logdir, gp_logdir;
2221 bool smart_logdir_ok = false, gp_logdir_ok = false;
2222
2224 && (cfg.errorlog || cfg.selftest)
2225 && !cfg.firmwarebugs.is_set(BUG_NOLOGDIR)) {
2226 if (!ataReadLogDirectory(atadev, &smart_logdir, false))
2227 smart_logdir_ok = true;
2228 }
2229
2230 if (cfg.xerrorlog && !cfg.firmwarebugs.is_set(BUG_NOLOGDIR)) {
2231 if (!ataReadLogDirectory(atadev, &gp_logdir, true))
2232 gp_logdir_ok = true;
2233 }
2234
2235 // capability check: self-test-log
2236 state.selflogcount = 0; state.selfloghour = 0;
2237 if (cfg.selftest) {
2238 int errcnt = 0; unsigned hour = 0;
2239 if (!( cfg.permissive
2240 || ( smart_logdir_ok && smart_logdir.entry[0x06-1].numsectors)
2241 || (!smart_logdir_ok && smart_val_ok && isSmartTestLogCapable(&state.smartval, &drive)))) {
2242 PrintOut(LOG_INFO, "Device: %s, no SMART Self-test Log, ignoring -l selftest (override with -T permissive)\n", name);
2243 cfg.selftest = false;
2244 }
2245 else if ((errcnt = check_ata_self_test_log(atadev, name, cfg.firmwarebugs, hour)) < 0) {
2246 PrintOut(LOG_INFO, "Device: %s, no SMART Self-test Log, ignoring -l selftest\n", name);
2247 cfg.selftest = false;
2248 }
2249 else {
2250 state.selflogcount = (unsigned char)errcnt;
2251 state.selfloghour = hour;
2252 }
2253 }
2254
2255 // capability check: ATA error log
2256 state.ataerrorcount = 0;
2257 if (cfg.errorlog) {
2258 int errcnt1;
2259 if (!( cfg.permissive
2260 || ( smart_logdir_ok && smart_logdir.entry[0x01-1].numsectors)
2261 || (!smart_logdir_ok && smart_val_ok && isSmartErrorLogCapable(&state.smartval, &drive)))) {
2262 PrintOut(LOG_INFO, "Device: %s, no SMART Error Log, ignoring -l error (override with -T permissive)\n", name);
2263 cfg.errorlog = false;
2264 }
2265 else if ((errcnt1 = read_ata_error_count(atadev, name, cfg.firmwarebugs, false)) < 0) {
2266 PrintOut(LOG_INFO, "Device: %s, no SMART Error Log, ignoring -l error\n", name);
2267 cfg.errorlog = false;
2268 }
2269 else
2270 state.ataerrorcount = errcnt1;
2271 }
2272
2273 if (cfg.xerrorlog) {
2274 int errcnt2;
2275 if (!( cfg.permissive || cfg.firmwarebugs.is_set(BUG_NOLOGDIR)
2276 || (gp_logdir_ok && gp_logdir.entry[0x03-1].numsectors) )) {
2277 PrintOut(LOG_INFO, "Device: %s, no Extended Comprehensive SMART Error Log, ignoring -l xerror (override with -T permissive)\n",
2278 name);
2279 cfg.xerrorlog = false;
2280 }
2281 else if ((errcnt2 = read_ata_error_count(atadev, name, cfg.firmwarebugs, true)) < 0) {
2282 PrintOut(LOG_INFO, "Device: %s, no Extended Comprehensive SMART Error Log, ignoring -l xerror\n", name);
2283 cfg.xerrorlog = false;
2284 }
2285 else if (cfg.errorlog && state.ataerrorcount != errcnt2) {
2286 PrintOut(LOG_INFO, "Device: %s, SMART Error Logs report different error counts: %d != %d\n",
2287 name, state.ataerrorcount, errcnt2);
2288 // Record max error count
2289 if (errcnt2 > state.ataerrorcount)
2290 state.ataerrorcount = errcnt2;
2291 }
2292 else
2293 state.ataerrorcount = errcnt2;
2294 }
2295
2296 // capability check: self-test and offline data collection status
2297 if (cfg.offlinests || cfg.selfteststs) {
2298 if (!(cfg.permissive || (smart_val_ok && state.smartval.offline_data_collection_capability))) {
2299 if (cfg.offlinests)
2300 PrintOut(LOG_INFO, "Device: %s, no SMART Offline Data Collection capability, ignoring -l offlinests (override with -T permissive)\n", name);
2301 if (cfg.selfteststs)
2302 PrintOut(LOG_INFO, "Device: %s, no SMART Self-test capability, ignoring -l selfteststs (override with -T permissive)\n", name);
2303 cfg.offlinests = cfg.selfteststs = false;
2304 }
2305 }
2306
2307 // capabilities check -- does it support powermode?
2308 if (cfg.powermode) {
2309 int powermode = ataCheckPowerMode(atadev);
2310
2311 if (-1 == powermode) {
2312 PrintOut(LOG_CRIT, "Device: %s, no ATA CHECK POWER STATUS support, ignoring -n Directive\n", name);
2313 cfg.powermode=0;
2314 }
2315 else if (powermode!=0x00 && powermode!=0x01
2316 && powermode!=0x40 && powermode!=0x41
2317 && powermode!=0x80 && powermode!=0x81 && powermode!=0x82 && powermode!=0x83
2318 && powermode!=0xff) {
2319 PrintOut(LOG_CRIT, "Device: %s, CHECK POWER STATUS returned %d, not ATA compliant, ignoring -n Directive\n",
2320 name, powermode);
2321 cfg.powermode=0;
2322 }
2323 }
2324
2325 // Apply ATA settings
2326 std::string msg;
2327
2328 if (cfg.set_aam)
2329 format_set_result_msg(msg, "AAM", (cfg.set_aam > 0 ?
2330 ata_set_features(atadev, ATA_ENABLE_AAM, cfg.set_aam-1) :
2331 ata_set_features(atadev, ATA_DISABLE_AAM)), cfg.set_aam, true);
2332
2333 if (cfg.set_apm)
2334 format_set_result_msg(msg, "APM", (cfg.set_apm > 0 ?
2335 ata_set_features(atadev, ATA_ENABLE_APM, cfg.set_apm-1) :
2336 ata_set_features(atadev, ATA_DISABLE_APM)), cfg.set_apm, true);
2337
2338 if (cfg.set_lookahead)
2339 format_set_result_msg(msg, "Rd-ahead", ata_set_features(atadev,
2341 cfg.set_lookahead);
2342
2343 if (cfg.set_wcache)
2344 format_set_result_msg(msg, "Wr-cache", ata_set_features(atadev,
2346
2347 if (cfg.set_dsn)
2348 format_set_result_msg(msg, "DSN", ata_set_features(atadev,
2349 ATA_ENABLE_DISABLE_DSN, (cfg.set_dsn > 0 ? 0x1 : 0x2)));
2350
2351 if (cfg.set_security_freeze)
2352 format_set_result_msg(msg, "Security freeze",
2354
2355 if (cfg.set_standby)
2356 format_set_result_msg(msg, "Standby",
2357 ata_nodata_command(atadev, ATA_IDLE, cfg.set_standby-1), cfg.set_standby, true);
2358
2359 // Report as one log entry
2360 if (!msg.empty())
2361 PrintOut(LOG_INFO, "Device: %s, ATA settings applied: %s\n", name, msg.c_str());
2362
2363 // set SCT Error Recovery Control if requested
2364 if (cfg.sct_erc_set) {
2366 PrintOut(LOG_INFO, "Device: %s, no SCT Error Recovery Control support, ignoring -l scterc\n",
2367 name);
2368 else if (locked)
2369 PrintOut(LOG_INFO, "Device: %s, no SCT support if ATA Security is LOCKED, ignoring -l scterc\n",
2370 name);
2371 else if ( ataSetSCTErrorRecoveryControltime(atadev, 1, cfg.sct_erc_readtime, false, false )
2372 || ataSetSCTErrorRecoveryControltime(atadev, 2, cfg.sct_erc_writetime, false, false))
2373 PrintOut(LOG_INFO, "Device: %s, set of SCT Error Recovery Control failed\n", name);
2374 else
2375 PrintOut(LOG_INFO, "Device: %s, SCT Error Recovery Control set to: Read: %u, Write: %u\n",
2376 name, cfg.sct_erc_readtime, cfg.sct_erc_writetime);
2377 }
2378
2379 // If no tests available or selected, return
2380 if (!( cfg.smartcheck || cfg.selftest
2381 || cfg.errorlog || cfg.xerrorlog
2382 || cfg.offlinests || cfg.selfteststs
2383 || cfg.usagefailed || cfg.prefail || cfg.usage
2384 || cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)) {
2385 CloseDevice(atadev, name);
2386 return 3;
2387 }
2388
2389 // tell user we are registering device
2390 PrintOut(LOG_INFO,"Device: %s, is SMART capable. Adding to \"monitor\" list.\n",name);
2391
2392 // close file descriptor
2393 CloseDevice(atadev, name);
2394
2395 if (!state_path_prefix.empty() || !attrlog_path_prefix.empty()) {
2396 // Build file name for state file
2397 std::replace_if(model, model+strlen(model), not_allowed_in_filename, '_');
2398 std::replace_if(serial, serial+strlen(serial), not_allowed_in_filename, '_');
2399 if (!state_path_prefix.empty()) {
2400 cfg.state_file = strprintf("%s%s-%s.ata.state", state_path_prefix.c_str(), model, serial);
2401 // Read previous state
2402 if (read_dev_state(cfg.state_file.c_str(), state)) {
2403 PrintOut(LOG_INFO, "Device: %s, state read from %s\n", name, cfg.state_file.c_str());
2404 // Copy ATA attribute values to temp state
2405 state.update_temp_state();
2406 }
2407 }
2408 if (!attrlog_path_prefix.empty())
2409 cfg.attrlog_file = strprintf("%s%s-%s.ata.csv", attrlog_path_prefix.c_str(), model, serial);
2410 }
2411
2412 finish_device_scan(cfg, state);
2413
2414 return 0;
2415}
2416
2417// on success, return 0. On failure, return >0. Never return <0,
2418// please.
2419static int SCSIDeviceScan(dev_config & cfg, dev_state & state, scsi_device * scsidev,
2420 const dev_config_vector * prev_cfgs)
2421{
2422 int err, req_len, avail_len, version, len;
2423 const char *device = cfg.name.c_str();
2424 struct scsi_iec_mode_page iec;
2425 uint8_t tBuf[64];
2426 uint8_t inqBuf[96];
2427 uint8_t vpdBuf[252];
2428 char lu_id[64], serial[256], vendor[40], model[40];
2429
2430 // Device must be open
2431 memset(inqBuf, 0, 96);
2432 req_len = 36;
2433 if ((err = scsiStdInquiry(scsidev, inqBuf, req_len))) {
2434 /* Marvell controllers fail on a 36 bytes StdInquiry, but 64 suffices */
2435 req_len = 64;
2436 int err64;
2437 if ((err64 = scsiStdInquiry(scsidev, inqBuf, req_len))) {
2438 PrintOut(LOG_INFO, "Device: %s, Both 36 and 64 byte INQUIRY failed; "
2439 "skip device [err=%d, %d]\n", device, err, err64);
2440 return 2;
2441 }
2442 }
2443 version = (inqBuf[2] & 0x7f); /* Accept old ISO/IEC 9316:1995 variants */
2444
2445 avail_len = inqBuf[4] + 5;
2446 len = (avail_len < req_len) ? avail_len : req_len;
2447 if (len < 36) {
2448 PrintOut(LOG_INFO, "Device: %s, INQUIRY response less than 36 bytes; "
2449 "skip device\n", device);
2450 return 2;
2451 }
2452
2453 int pdt = inqBuf[0] & 0x1f;
2454
2455 switch (pdt) {
2457 case SCSI_PT_WO:
2458 case SCSI_PT_CDROM:
2459 case SCSI_PT_OPTICAL:
2460 case SCSI_PT_RBC: /* Reduced Block commands */
2461 case SCSI_PT_HOST_MANAGED: /* Zoned disk */
2462 break;
2463 default:
2464 PrintOut(LOG_INFO, "Device: %s, not a disk like device [PDT=0x%x], "
2465 "skip\n", device, pdt);
2466 return 2;
2467 }
2468
2470 delete supported_vpd_pages_p;
2471 supported_vpd_pages_p = nullptr;
2472 }
2474
2475 lu_id[0] = '\0';
2476 if (version >= 0x3) {
2477 /* SPC to SPC-5, assume SPC-6 is version==8 or higher */
2479 vpdBuf, sizeof(vpdBuf))) {
2480 len = vpdBuf[3];
2481 scsi_decode_lu_dev_id(vpdBuf + 4, len, lu_id, sizeof(lu_id), nullptr);
2482 }
2483 }
2484 serial[0] = '\0';
2486 vpdBuf, sizeof(vpdBuf))) {
2487 len = vpdBuf[3];
2488 vpdBuf[4 + len] = '\0';
2489 scsi_format_id_string(serial, &vpdBuf[4], len);
2490 }
2491
2492 char si_str[64];
2493 struct scsi_readcap_resp srr;
2494 uint64_t capacity = scsiGetSize(scsidev, scsidev->use_rcap16(), &srr);
2495
2496 if (capacity)
2497 format_capacity(si_str, sizeof(si_str), capacity, ".");
2498 else
2499 si_str[0] = '\0';
2500
2501 // Format device id string for warning emails
2502 cfg.dev_idinfo = strprintf("[%.8s %.16s %.4s]%s%s%s%s%s%s",
2503 (char *)&inqBuf[8], (char *)&inqBuf[16], (char *)&inqBuf[32],
2504 (lu_id[0] ? ", lu id: " : ""), (lu_id[0] ? lu_id : ""),
2505 (serial[0] ? ", S/N: " : ""), (serial[0] ? serial : ""),
2506 (si_str[0] ? ", " : ""), (si_str[0] ? si_str : ""));
2507 cfg.id_is_unique = (lu_id[0] || serial[0]);
2509 cfg.id_is_unique = false;
2510
2511 // format "model" string
2512 scsi_format_id_string(vendor, &inqBuf[8], 8);
2513 scsi_format_id_string(model, &inqBuf[16], 16);
2514 PrintOut(LOG_INFO, "Device: %s, %s\n", device, cfg.dev_idinfo.c_str());
2515
2516 // Check for duplicates
2517 if (prev_cfgs && is_duplicate_dev_idinfo(cfg, *prev_cfgs)) {
2518 CloseDevice(scsidev, device);
2519 return 1;
2520 }
2521
2522 // check that device is ready for commands. IE stores its stuff on
2523 // the media.
2524 if ((err = scsiTestUnitReady(scsidev))) {
2525 if (SIMPLE_ERR_NOT_READY == err)
2526 PrintOut(LOG_INFO, "Device: %s, NOT READY (e.g. spun down); skip device\n", device);
2527 else if (SIMPLE_ERR_NO_MEDIUM == err)
2528 PrintOut(LOG_INFO, "Device: %s, NO MEDIUM present; skip device\n", device);
2529 else if (SIMPLE_ERR_BECOMING_READY == err)
2530 PrintOut(LOG_INFO, "Device: %s, BECOMING (but not yet) READY; skip device\n", device);
2531 else
2532 PrintOut(LOG_CRIT, "Device: %s, failed Test Unit Ready [err=%d]\n", device, err);
2533 CloseDevice(scsidev, device);
2534 return 2;
2535 }
2536
2537 // Badly-conforming USB storage devices may fail this check.
2538 // The response to the following IE mode page fetch (current and
2539 // changeable values) is carefully examined. It has been found
2540 // that various USB devices that malform the response will lock up
2541 // if asked for a log page (e.g. temperature) so it is best to
2542 // bail out now.
2543 if (!(err = scsiFetchIECmpage(scsidev, &iec, state.modese_len)))
2544 state.modese_len = iec.modese_len;
2545 else if (SIMPLE_ERR_BAD_FIELD == err)
2546 ; /* continue since it is reasonable not to support IE mpage */
2547 else { /* any other error (including malformed response) unreasonable */
2548 PrintOut(LOG_INFO,
2549 "Device: %s, Bad IEC (SMART) mode page, err=%d, skip device\n",
2550 device, err);
2551 CloseDevice(scsidev, device);
2552 return 3;
2553 }
2554
2555 // N.B. The following is passive (i.e. it doesn't attempt to turn on
2556 // smart if it is off). This may change to be the same as the ATA side.
2557 if (!scsi_IsExceptionControlEnabled(&iec)) {
2558 PrintOut(LOG_INFO, "Device: %s, IE (SMART) not enabled, skip device\n"
2559 "Try 'smartctl -s on %s' to turn on SMART features\n",
2560 device, device);
2561 CloseDevice(scsidev, device);
2562 return 3;
2563 }
2564
2565 // Flag that certain log pages are supported (information may be
2566 // available from other sources).
2567 if (0 == scsiLogSense(scsidev, SUPPORTED_LPAGES, 0, tBuf, sizeof(tBuf), 0) ||
2568 0 == scsiLogSense(scsidev, SUPPORTED_LPAGES, 0, tBuf, sizeof(tBuf), 68))
2569 /* workaround for the bug #678 on ST8000NM0075/E001. Up to 64 pages + 4b header */
2570 {
2571 for (int k = 4; k < tBuf[3] + LOGPAGEHDRSIZE; ++k) {
2572 switch (tBuf[k]) {
2573 case TEMPERATURE_LPAGE:
2574 state.TempPageSupported = 1;
2575 break;
2576 case IE_LPAGE:
2577 state.SmartPageSupported = 1;
2578 break;
2580 state.ReadECounterPageSupported = 1;
2581 break;
2584 break;
2587 break;
2590 break;
2591 default:
2592 break;
2593 }
2594 }
2595 }
2596
2597 // Check if scsiCheckIE() is going to work
2598 {
2599 uint8_t asc = 0;
2600 uint8_t ascq = 0;
2601 uint8_t currenttemp = 0;
2602 uint8_t triptemp = 0;
2603
2604 if (scsiCheckIE(scsidev, state.SmartPageSupported, state.TempPageSupported,
2605 &asc, &ascq, &currenttemp, &triptemp)) {
2606 PrintOut(LOG_INFO, "Device: %s, unexpectedly failed to read SMART values\n", device);
2607 state.SuppressReport = 1;
2608 }
2609 if ( (state.SuppressReport || !currenttemp)
2610 && (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)) {
2611 PrintOut(LOG_INFO, "Device: %s, can't monitor Temperature, ignoring -W %d,%d,%d\n",
2612 device, cfg.tempdiff, cfg.tempinfo, cfg.tempcrit);
2613 cfg.tempdiff = cfg.tempinfo = cfg.tempcrit = 0;
2614 }
2615 }
2616
2617 // capability check: self-test-log
2618 if (cfg.selftest){
2619 int retval = scsiCountFailedSelfTests(scsidev, 0);
2620 if (retval<0) {
2621 // no self-test log, turn off monitoring
2622 PrintOut(LOG_INFO, "Device: %s, does not support SMART Self-Test Log.\n", device);
2623 cfg.selftest = false;
2624 state.selflogcount = 0;
2625 state.selfloghour = 0;
2626 }
2627 else {
2628 // register starting values to watch for changes
2629 state.selflogcount = retval & 0xff;
2630 state.selfloghour = (retval >> 8) & 0xffff;
2631 }
2632 }
2633
2634 // disable autosave (set GLTSD bit)
2635 if (cfg.autosave==1){
2636 if (scsiSetControlGLTSD(scsidev, 1, state.modese_len))
2637 PrintOut(LOG_INFO,"Device: %s, could not disable autosave (set GLTSD bit).\n",device);
2638 else
2639 PrintOut(LOG_INFO,"Device: %s, disabled autosave (set GLTSD bit).\n",device);
2640 }
2641
2642 // or enable autosave (clear GLTSD bit)
2643 if (cfg.autosave==2){
2644 if (scsiSetControlGLTSD(scsidev, 0, state.modese_len))
2645 PrintOut(LOG_INFO,"Device: %s, could not enable autosave (clear GLTSD bit).\n",device);
2646 else
2647 PrintOut(LOG_INFO,"Device: %s, enabled autosave (cleared GLTSD bit).\n",device);
2648 }
2649
2650 // tell user we are registering device
2651 PrintOut(LOG_INFO, "Device: %s, is SMART capable. Adding to \"monitor\" list.\n", device);
2652
2653 // Disable ATA specific self-tests
2654 state.not_cap_conveyance = state.not_cap_offline = state.not_cap_selective = true;
2655
2656 // Make sure that init_standby_check() ignores SCSI devices
2657 cfg.offlinests_ns = cfg.selfteststs_ns = false;
2658
2659 // close file descriptor
2660 CloseDevice(scsidev, device);
2661
2662 if (!state_path_prefix.empty() || !attrlog_path_prefix.empty()) {
2663 // Build file name for state file
2664 std::replace_if(model, model+strlen(model), not_allowed_in_filename, '_');
2665 std::replace_if(serial, serial+strlen(serial), not_allowed_in_filename, '_');
2666 if (!state_path_prefix.empty()) {
2667 cfg.state_file = strprintf("%s%s-%s-%s.scsi.state", state_path_prefix.c_str(), vendor, model, serial);
2668 // Read previous state
2669 if (read_dev_state(cfg.state_file.c_str(), state)) {
2670 PrintOut(LOG_INFO, "Device: %s, state read from %s\n", device, cfg.state_file.c_str());
2671 // Copy ATA attribute values to temp state
2672 state.update_temp_state();
2673 }
2674 }
2675 if (!attrlog_path_prefix.empty())
2676 cfg.attrlog_file = strprintf("%s%s-%s-%s.scsi.csv", attrlog_path_prefix.c_str(), vendor, model, serial);
2677 }
2678
2679 finish_device_scan(cfg, state);
2680
2681 return 0;
2682}
2683
2684// Convert 128 bit LE integer to uint64_t or its max value on overflow.
2685static uint64_t le128_to_uint64(const unsigned char (& val)[16])
2686{
2687 for (int i = 8; i < 16; i++) {
2688 if (val[i])
2689 return ~(uint64_t)0;
2690 }
2691 uint64_t lo = val[7];
2692 for (int i = 7-1; i >= 0; i--) {
2693 lo <<= 8; lo += val[i];
2694 }
2695 return lo;
2696}
2697
2698// Check the NVMe Error Information log for device related errors.
2699static bool check_nvme_error_log(const dev_config & cfg, dev_state & state, nvme_device * nvmedev,
2700 uint64_t newcnt = 0)
2701{
2702 // Limit transfer size to one page (64 entries) to avoid problems with
2703 // limits of NVMe pass-through layer or too low MDTS values.
2704 unsigned want_entries = 64;
2705 if (want_entries > cfg.nvme_err_log_max_entries)
2706 want_entries = cfg.nvme_err_log_max_entries;
2707 raw_buffer error_log_buf(want_entries * sizeof(nvme_error_log_page));
2708 nvme_error_log_page * error_log =
2709 reinterpret_cast<nvme_error_log_page *>(error_log_buf.data());
2710 unsigned read_entries = nvme_read_error_log(nvmedev, error_log, want_entries, false /*!lpo_sup*/);
2711 if (!read_entries) {
2712 PrintOut(LOG_INFO, "Device: %s, Read %u entries from Error Information Log failed\n",
2713 cfg.name.c_str(), want_entries);
2714 return false;
2715 }
2716
2717 if (!newcnt)
2718 return true; // Support check only
2719
2720 // Scan log, find device related errors
2721 uint64_t oldcnt = state.nvme_err_log_entries, mincnt = newcnt;
2722 int err = 0, ign = 0;
2723 for (unsigned i = 0; i < read_entries; i++) {
2724 const nvme_error_log_page & e = error_log[i];
2725 if (!e.error_count)
2726 continue; // unused
2727 if (e.error_count <= oldcnt)
2728 break; // stop on first old entry
2729 if (e.error_count < mincnt)
2730 mincnt = e.error_count; // min known error
2731 if (e.error_count > newcnt)
2732 newcnt = e.error_count; // adjust maximum
2733 uint16_t status = e.status_field >> 1;
2734 if (!nvme_status_is_error(status) || nvme_status_to_errno(status) == EINVAL) {
2735 ign++; // Not a device related error
2736 continue;
2737 }
2738
2739 // Log the most recent 8 errors
2740 if (++err > 8)
2741 continue;
2742 char buf[64];
2743 PrintOut(LOG_INFO, "Device: %s, NVMe error [%u], count %" PRIu64 ", status 0x%04x: %s\n",
2744 cfg.name.c_str(), i, e.error_count, e.status_field,
2746 }
2747
2748 std::string msg = strprintf("Device: %s, NVMe error count increased from %" PRIu64 " to %" PRIu64
2749 " (%d new, %d ignored, %" PRIu64 " unknown)",
2750 cfg.name.c_str(), oldcnt, newcnt, err, ign,
2751 (mincnt > oldcnt + 1 ? mincnt - oldcnt - 1 : 0));
2752 // LOG_CRIT only if device related errors are found
2753 if (!err) {
2754 PrintOut(LOG_INFO, "%s\n", msg.c_str());
2755 }
2756 else {
2757 PrintOut(LOG_CRIT, "%s\n", msg.c_str());
2758 MailWarning(cfg, state, 4, "%s", msg.c_str());
2759 }
2760
2761 state.nvme_err_log_entries = newcnt;
2762 state.must_write = true;
2763 return true;
2764}
2765
2766static int NVMeDeviceScan(dev_config & cfg, dev_state & state, nvme_device * nvmedev,
2767 const dev_config_vector * prev_cfgs)
2768{
2769 const char *name = cfg.name.c_str();
2770
2771 // Device must be open
2772
2773 // Get ID Controller
2774 nvme_id_ctrl id_ctrl;
2775 if (!nvme_read_id_ctrl(nvmedev, id_ctrl)) {
2776 PrintOut(LOG_INFO, "Device: %s, NVMe Identify Controller failed\n", name);
2777 CloseDevice(nvmedev, name);
2778 return 2;
2779 }
2780
2781 // Get drive identity
2782 char model[40+1], serial[20+1], firmware[8+1];
2783 format_char_array(model, id_ctrl.mn);
2784 format_char_array(serial, id_ctrl.sn);
2785 format_char_array(firmware, id_ctrl.fr);
2786
2787 // Format device id string for warning emails
2788 char nsstr[32] = "", capstr[32] = "";
2789 unsigned nsid = nvmedev->get_nsid();
2791 snprintf(nsstr, sizeof(nsstr), ", NSID:%u", nsid);
2792 uint64_t capacity = le128_to_uint64(id_ctrl.tnvmcap);
2793 if (capacity)
2794 format_capacity(capstr, sizeof(capstr), capacity, ".");
2795
2796 auto idinfo = &dev_config::dev_idinfo;
2797 for (;;) {
2798 cfg.*idinfo = strprintf("%s, S/N:%s, FW:%s%s%s%s", model, serial, firmware,
2799 nsstr, (capstr[0] ? ", " : ""), capstr);
2800 if (!(nsstr[0] && id_ctrl.nn == 1))
2801 break; // No namespace id or device supports multiple namespaces
2802 // Keep version without namespace id for 'is_duplicate_dev_idinfo()'
2803 nsstr[0] = 0;
2804 idinfo = &dev_config::dev_idinfo_bc;
2805 }
2806
2807 cfg.id_is_unique = true; // TODO: Check serial?
2809 cfg.id_is_unique = false;
2810
2811 PrintOut(LOG_INFO, "Device: %s, %s\n", name, cfg.dev_idinfo.c_str());
2812
2813 // Check for duplicates
2814 if (prev_cfgs && is_duplicate_dev_idinfo(cfg, *prev_cfgs)) {
2815 CloseDevice(nvmedev, name);
2816 return 1;
2817 }
2818
2819 // Read SMART/Health log
2820 // TODO: Support per namespace SMART/Health log
2821 nvme_smart_log smart_log;
2822 if (!nvme_read_smart_log(nvmedev, nvme_broadcast_nsid, smart_log)) {
2823 PrintOut(LOG_INFO, "Device: %s, failed to read NVMe SMART/Health Information\n", name);
2824 CloseDevice(nvmedev, name);
2825 return 2;
2826 }
2827
2828 // Check temperature sensor support
2829 if (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit) {
2830 if (!sg_get_unaligned_le16(smart_log.temperature)) {
2831 PrintOut(LOG_INFO, "Device: %s, no Temperature sensors, ignoring -W %d,%d,%d\n",
2832 name, cfg.tempdiff, cfg.tempinfo, cfg.tempcrit);
2833 cfg.tempdiff = cfg.tempinfo = cfg.tempcrit = 0;
2834 }
2835 }
2836
2837 // Init total error count
2838 cfg.nvme_err_log_max_entries = id_ctrl.elpe + 1; // 0's based value
2839 if (cfg.errorlog || cfg.xerrorlog) {
2840 if (!check_nvme_error_log(cfg, state, nvmedev)) {
2841 PrintOut(LOG_INFO, "Device: %s, Error Information unavailable, ignoring -l [x]error\n", name);
2842 cfg.errorlog = cfg.xerrorlog = false;
2843 }
2844 else
2846 }
2847
2848 // Check for self-test support
2849 state.not_cap_short = state.not_cap_long = !(id_ctrl.oacs & 0x0010);
2850 state.selflogcount = 0; state.selfloghour = 0;
2851 if (cfg.selftest || cfg.selfteststs || !cfg.test_regex.empty()) {
2852 nvme_self_test_log self_test_log;
2853 if ( !state.not_cap_short
2854 && !nvme_read_self_test_log(nvmedev, nvme_broadcast_nsid, self_test_log)) {
2855 PrintOut(LOG_INFO, "Device: %s, Read NVMe Self-test Log failed: %s\n", name,
2856 nvmedev->get_errmsg());
2857 state.not_cap_short = state.not_cap_long = true;
2858 }
2859 if (state.not_cap_short) {
2860 PrintOut(LOG_INFO, "Device: %s, does not support NVMe Self-tests, ignoring%s%s%s%s\n", name,
2861 (cfg.selftest ? " -l selftest" : ""),
2862 (cfg.selfteststs ? " -l selfteststs" : ""),
2863 (!cfg.test_regex.empty() ? " -s " : ""), cfg.test_regex.get_pattern());
2864 cfg.selftest = cfg.selfteststs = false; cfg.test_regex = {};
2865 }
2866 }
2867
2868 // If no supported tests selected, return
2869 if (!( cfg.smartcheck || cfg.errorlog || cfg.xerrorlog
2870 || cfg.selftest || cfg.selfteststs || !cfg.test_regex.empty()
2871 || cfg.tempdiff || cfg.tempinfo || cfg.tempcrit ) ) {
2872 CloseDevice(nvmedev, name);
2873 return 3;
2874 }
2875
2876 // Tell user we are registering device
2877 PrintOut(LOG_INFO,"Device: %s, is SMART capable. Adding to \"monitor\" list.\n", name);
2878
2879 // Disable ATA specific self-tests
2880 state.not_cap_conveyance = state.not_cap_offline = state.not_cap_selective = true;
2881
2882 // Make sure that init_standby_check() ignores NVMe devices
2883 // TODO: Implement '-l selfteststs,ns' for NVMe
2884 cfg.offlinests_ns = cfg.selfteststs_ns = false;
2885
2886 CloseDevice(nvmedev, name);
2887
2888 if (!state_path_prefix.empty()) {
2889 // Build file name for state file
2890 std::replace_if(model, model+strlen(model), not_allowed_in_filename, '_');
2891 std::replace_if(serial, serial+strlen(serial), not_allowed_in_filename, '_');
2892 nsstr[0] = 0;
2894 snprintf(nsstr, sizeof(nsstr), "-n%u", nsid);
2895 cfg.state_file = strprintf("%s%s-%s%s.nvme.state", state_path_prefix.c_str(), model, serial, nsstr);
2896 // Read previous state
2897 if (read_dev_state(cfg.state_file.c_str(), state))
2898 PrintOut(LOG_INFO, "Device: %s, state read from %s\n", name, cfg.state_file.c_str());
2899 }
2900
2901 finish_device_scan(cfg, state);
2902
2903 return 0;
2904}
2905
2906// Open device for next check, return false on error
2907static bool open_device(const dev_config & cfg, dev_state & state, smart_device * device,
2908 const char * type)
2909{
2910 const char * name = cfg.name.c_str();
2911
2912 // If user has asked, test the email warning system
2913 if (cfg.emailtest)
2914 MailWarning(cfg, state, 0, "TEST EMAIL from smartd for device: %s", name);
2915
2916 // User may have requested (with the -n Directive) to leave the disk
2917 // alone if it is in idle or standby mode. In this case check the
2918 // power mode first before opening the device for full access,
2919 // and exit without check if disk is reported in standby.
2920 if (device->is_ata() && cfg.powermode && !state.powermodefail && !state.removed) {
2921 // Note that 'is_powered_down()' handles opening the device itself, and
2922 // can be used before calling 'open()' (that's the whole point of 'is_powered_down()'!).
2923 if (device->is_powered_down())
2924 {
2925 // skip at most powerskipmax checks
2926 if (!cfg.powerskipmax || state.powerskipcnt<cfg.powerskipmax) {
2927 // report first only except if state has changed, avoid waking up system disk
2928 if ((!state.powerskipcnt || state.lastpowermodeskipped != -1) && !cfg.powerquiet) {
2929 PrintOut(LOG_INFO, "Device: %s, is in %s mode, suspending checks\n", name, "STANDBY (OS)");
2930 state.lastpowermodeskipped = -1;
2931 }
2932 state.powerskipcnt++;
2933 return false;
2934 }
2935 }
2936 }
2937
2938 // if we can't open device, fail gracefully rather than hard --
2939 // perhaps the next time around we'll be able to open it
2940 if (!device->open()) {
2941 // For removable devices, print error message only once and suppress email
2942 if (!cfg.removable) {
2943 PrintOut(LOG_INFO, "Device: %s, open() of %s device failed: %s\n", name, type, device->get_errmsg());
2944 MailWarning(cfg, state, 9, "Device: %s, unable to open %s device", name, type);
2945 }
2946 else if (!state.removed) {
2947 PrintOut(LOG_INFO, "Device: %s, removed %s device: %s\n", name, type, device->get_errmsg());
2948 state.removed = true;
2949 }
2950 else if (debugmode)
2951 PrintOut(LOG_INFO, "Device: %s, %s device still removed: %s\n", name, type, device->get_errmsg());
2952 return false;
2953 }
2954
2955 if (debugmode)
2956 PrintOut(LOG_INFO,"Device: %s, opened %s device\n", name, type);
2957
2958 if (!cfg.removable)
2959 reset_warning_mail(cfg, state, 9, "open of %s device worked again", type);
2960 else if (state.removed) {
2961 PrintOut(LOG_INFO, "Device: %s, reconnected %s device\n", name, type);
2962 state.removed = false;
2963 }
2964
2965 return true;
2966}
2967
2968// If the self-test log has got more self-test errors (or more recent
2969// self-test errors) recorded, then notify user.
2970static void report_self_test_log_changes(const dev_config & cfg, dev_state & state,
2971 int errcnt, uint64_t hour)
2972{
2973 const char * name = cfg.name.c_str();
2974
2975 if (errcnt < 0)
2976 // command failed
2977 // TODO: Move this to ATA/SCSICheckDevice()
2978 MailWarning(cfg, state, 8, "Device: %s, Read SMART Self-Test Log Failed", name);
2979 else {
2980 reset_warning_mail(cfg, state, 8, "Read SMART Self-Test Log worked again");
2981
2982 if (state.selflogcount < errcnt) {
2983 // increase in error count
2984 PrintOut(LOG_CRIT, "Device: %s, Self-Test Log error count increased from %d to %d\n",
2985 name, state.selflogcount, errcnt);
2986 MailWarning(cfg, state, 3, "Device: %s, Self-Test Log error count increased from %d to %d",
2987 name, state.selflogcount, errcnt);
2988 state.must_write = true;
2989 }
2990 else if (errcnt > 0 && state.selfloghour != hour) {
2991 // more recent error
2992 // ATA: a 'more recent' error might actually be a smaller hour number,
2993 // if the hour number has wrapped.
2994 // There's still a bug here. You might just happen to run a new test
2995 // exactly 32768 hours after the previous failure, and have run exactly
2996 // 20 tests between the two, in which case smartd will miss the
2997 // new failure.
2998 PrintOut(LOG_CRIT, "Device: %s, new Self-Test Log error at hour timestamp %" PRIu64 "\n",
2999 name, hour);
3000 MailWarning(cfg, state, 3, "Device: %s, new Self-Test Log error at hour timestamp %" PRIu64 "\n",
3001 name, hour);
3002 state.must_write = true;
3003 }
3004
3005 // Print info if error entries have disappeared
3006 // or newer successful extended self-test exists
3007 if (state.selflogcount > errcnt) {
3008 PrintOut(LOG_INFO, "Device: %s, Self-Test Log error count decreased from %d to %d\n",
3009 name, state.selflogcount, errcnt);
3010 if (errcnt == 0)
3011 reset_warning_mail(cfg, state, 3, "Self-Test Log does no longer report errors");
3012 }
3013
3014 state.selflogcount = errcnt;
3015 state.selfloghour = hour;
3016 }
3017 return;
3018}
3019
3020// Test types, ordered by priority.
3021static const char test_type_chars[] = "LncrSCO";
3022static const unsigned num_test_types = sizeof(test_type_chars)-1;
3023
3024// returns test type if time to do test of type testtype,
3025// 0 if not time to do test.
3026static char next_scheduled_test(const dev_config & cfg, dev_state & state, time_t usetime = 0)
3027{
3028 // check that self-testing has been requested
3029 if (cfg.test_regex.empty())
3030 return 0;
3031
3032 // Exit if drive not capable of any test
3033 if ( state.not_cap_long && state.not_cap_short
3034 && state.not_cap_conveyance && state.not_cap_offline && state.not_cap_selective)
3035 return 0;
3036
3037 // since we are about to call localtime(), be sure glibc is informed
3038 // of any timezone changes we make.
3039 if (!usetime)
3041
3042 // Is it time for next check?
3043 time_t now = (!usetime ? time(nullptr) : usetime);
3044 if (now < state.scheduled_test_next_check) {
3045 if (state.scheduled_test_next_check <= now + 3600)
3046 return 0; // Next check within one hour
3047 // More than one hour, assume system clock time adjusted to the past
3048 state.scheduled_test_next_check = now;
3049 }
3050 else if (state.scheduled_test_next_check + (3600L*24*90) < now) {
3051 // Limit time check interval to 90 days
3052 state.scheduled_test_next_check = now - (3600L*24*90);
3053 }
3054
3055 // Find ':NNN[-LLL]' in regex for possible offsets and limits
3056 const unsigned max_offsets = 1 + num_test_types;
3057 unsigned offsets[max_offsets] = {0, }, limits[max_offsets] = {0, };
3058 unsigned num_offsets = 1; // offsets/limits[0] == 0 always
3059 for (const char * p = cfg.test_regex.get_pattern(); num_offsets < max_offsets; ) {
3060 const char * q = strchr(p, ':');
3061 if (!q)
3062 break;
3063 p = q + 1;
3064 unsigned offset = 0, limit = 0; int n1 = -1, n2 = -1, n3 = -1;
3065 sscanf(p, "%u%n-%n%u%n", &offset, &n1, &n2, &limit, &n3);
3066 if (!(n1 == 3 && (n2 < 0 || (n3 == 3+1+3 && limit > 0))))
3067 continue;
3068 offsets[num_offsets] = offset; limits[num_offsets] = limit;
3069 num_offsets++;
3070 p += (n3 > 0 ? n3 : n1);
3071 }
3072
3073 // Check interval [state.scheduled_test_next_check, now] for scheduled tests
3074 char testtype = 0;
3075 time_t testtime = 0; int testhour = 0;
3076 int maxtest = num_test_types-1;
3077
3078 for (time_t t = state.scheduled_test_next_check; ; ) {
3079 // Check offset 0 and then all offsets for ':NNN' found above
3080 for (unsigned i = 0; i < num_offsets; i++) {
3081 unsigned offset = offsets[i], limit = limits[i];
3082 unsigned delay = cfg.test_offset_factor * offset;
3083 if (0 < limit && limit < delay)
3084 delay %= limit + 1;
3085 struct tm tmbuf, * tms = time_to_tm_local(&tmbuf, t - (delay * 3600));
3086
3087 // tm_wday is 0 (Sunday) to 6 (Saturday). We use 1 (Monday) to 7 (Sunday).
3088 int weekday = (tms->tm_wday ? tms->tm_wday : 7);
3089 for (int j = 0; j <= maxtest; j++) {
3090 // Skip if drive not capable of this test
3091 switch (test_type_chars[j]) {
3092 case 'L': if (state.not_cap_long) continue; break;
3093 case 'S': if (state.not_cap_short) continue; break;
3094 case 'C': if (state.not_cap_conveyance) continue; break;
3095 case 'O': if (state.not_cap_offline) continue; break;
3096 case 'c': case 'n':
3097 case 'r': if (state.not_cap_selective) continue; break;
3098 default: continue;
3099 }
3100 // Try match of "T/MM/DD/d/HH[:NNN]"
3101 char pattern[64];
3102 snprintf(pattern, sizeof(pattern), "%c/%02d/%02d/%1d/%02d",
3103 test_type_chars[j], tms->tm_mon+1, tms->tm_mday, weekday, tms->tm_hour);
3104 if (i > 0) {
3105 const unsigned len = sizeof("S/01/01/1/01") - 1;
3106 snprintf(pattern + len, sizeof(pattern) - len, ":%03u", offset);
3107 if (limit > 0)
3108 snprintf(pattern + len + 4, sizeof(pattern) - len - 4, "-%03u", limit);
3109 }
3110 if (cfg.test_regex.full_match(pattern)) {
3111 // Test found
3112 testtype = pattern[0];
3113 testtime = t; testhour = tms->tm_hour;
3114 // Limit further matches to higher priority self-tests
3115 maxtest = j-1;
3116 break;
3117 }
3118 }
3119 }
3120
3121 // Exit if no tests left or current time reached
3122 if (maxtest < 0)
3123 break;
3124 if (t >= now)
3125 break;
3126 // Check next hour
3127 if ((t += 3600) > now)
3128 t = now;
3129 }
3130
3131 // Do next check not before next hour.
3132 struct tm tmbuf, * tmnow = time_to_tm_local(&tmbuf, now);
3133 state.scheduled_test_next_check = now + (3600 - tmnow->tm_min*60 - tmnow->tm_sec);
3134
3135 if (testtype) {
3136 state.must_write = true;
3137 // Tell user if an old test was found.
3138 if (!usetime && !(testhour == tmnow->tm_hour && testtime + 3600 > now)) {
3139 char datebuf[DATEANDEPOCHLEN]; dateandtimezoneepoch(datebuf, testtime);
3140 PrintOut(LOG_INFO, "Device: %s, old test of type %c not run at %s, starting now.\n",
3141 cfg.name.c_str(), testtype, datebuf);
3142 }
3143 }
3144
3145 return testtype;
3146}
3147
3148// Print a list of future tests.
3150{
3151 unsigned numdev = configs.size();
3152 if (!numdev)
3153 return;
3154 std::vector<int> testcnts(numdev * num_test_types, 0);
3155
3156 PrintOut(LOG_INFO, "\nNext scheduled self tests (at most 5 of each type per device):\n");
3157
3158 // FixGlibcTimeZoneBug(); // done in PrintOut()
3159 time_t now = time(nullptr);
3160 char datenow[DATEANDEPOCHLEN], date[DATEANDEPOCHLEN];
3161 dateandtimezoneepoch(datenow, now);
3162
3163 long seconds;
3164 for (seconds=checktime; seconds<3600L*24*90; seconds+=checktime) {
3165 // Check for each device whether a test will be run
3166 time_t testtime = now + seconds;
3167 for (unsigned i = 0; i < numdev; i++) {
3168 const dev_config & cfg = configs.at(i);
3169 dev_state & state = states.at(i);
3170 const char * p;
3171 char testtype = next_scheduled_test(cfg, state, testtime);
3172 if (testtype && (p = strchr(test_type_chars, testtype))) {
3173 unsigned t = (p - test_type_chars);
3174 // Report at most 5 tests of each type
3175 if (++testcnts[i*num_test_types + t] <= 5) {
3176 dateandtimezoneepoch(date, testtime);
3177 PrintOut(LOG_INFO, "Device: %s, will do test %d of type %c at %s\n", cfg.name.c_str(),
3178 testcnts[i*num_test_types + t], testtype, date);
3179 }
3180 }
3181 }
3182 }
3183
3184 // Report totals
3185 dateandtimezoneepoch(date, now+seconds);
3186 PrintOut(LOG_INFO, "\nTotals [%s - %s]:\n", datenow, date);
3187 for (unsigned i = 0; i < numdev; i++) {
3188 const dev_config & cfg = configs.at(i);
3189 bool ata = devices.at(i)->is_ata();
3190 for (unsigned t = 0; t < num_test_types; t++) {
3191 int cnt = testcnts[i*num_test_types + t];
3192 if (cnt == 0 && !strchr((ata ? "LSCO" : "LS"), test_type_chars[t]))
3193 continue;
3194 PrintOut(LOG_INFO, "Device: %s, will do %3d test%s of type %c\n", cfg.name.c_str(),
3195 cnt, (cnt==1?"":"s"), test_type_chars[t]);
3196 }
3197 }
3198
3199}
3200
3201// Return zero on success, nonzero on failure. Perform offline (background)
3202// short or long (extended) self test on given scsi device.
3203static int DoSCSISelfTest(const dev_config & cfg, dev_state & state, scsi_device * device, char testtype)
3204{
3205 int retval = 0;
3206 const char *testname = nullptr;
3207 const char *name = cfg.name.c_str();
3208 int inProgress;
3209
3210 if (scsiSelfTestInProgress(device, &inProgress)) {
3211 PrintOut(LOG_CRIT, "Device: %s, does not support Self-Tests\n", name);
3212 state.not_cap_short = state.not_cap_long = true;
3213 return 1;
3214 }
3215
3216 if (1 == inProgress) {
3217 PrintOut(LOG_INFO, "Device: %s, skip since Self-Test already in "
3218 "progress.\n", name);
3219 return 1;
3220 }
3221
3222 switch (testtype) {
3223 case 'S':
3224 testname = "Short Self";
3225 retval = scsiSmartShortSelfTest(device);
3226 break;
3227 case 'L':
3228 testname = "Long Self";
3229 retval = scsiSmartExtendSelfTest(device);
3230 break;
3231 }
3232 // If we can't do the test, exit
3233 if (!testname) {
3234 PrintOut(LOG_CRIT, "Device: %s, not capable of %c Self-Test\n", name,
3235 testtype);
3236 return 1;
3237 }
3238 if (retval) {
3239 if ((SIMPLE_ERR_BAD_OPCODE == retval) ||
3240 (SIMPLE_ERR_BAD_FIELD == retval)) {
3241 PrintOut(LOG_CRIT, "Device: %s, not capable of %s-Test\n", name,
3242 testname);
3243 if ('L'==testtype)
3244 state.not_cap_long = true;
3245 else
3246 state.not_cap_short = true;
3247
3248 return 1;
3249 }
3250 PrintOut(LOG_CRIT, "Device: %s, execute %s-Test failed (err: %d)\n", name,
3251 testname, retval);
3252 return 1;
3253 }
3254
3255 PrintOut(LOG_INFO, "Device: %s, starting scheduled %s-Test.\n", name, testname);
3256
3257 return 0;
3258}
3259
3260// Do an offline immediate or self-test. Return zero on success,
3261// nonzero on failure.
3262static int DoATASelfTest(const dev_config & cfg, dev_state & state, ata_device * device, char testtype)
3263{
3264 const char *name = cfg.name.c_str();
3265
3266 // Read current smart data and check status/capability
3267 // TODO: Reuse smart data already read in ATACheckDevice()
3268 struct ata_smart_values data;
3269 if (ataReadSmartValues(device, &data) || !(data.offline_data_collection_capability)) {
3270 PrintOut(LOG_CRIT, "Device: %s, not capable of Offline or Self-Testing.\n", name);
3271 return 1;
3272 }
3273
3274 // Check for capability to do the test
3275 int dotest = -1, mode = 0;
3276 const char *testname = nullptr;
3277 switch (testtype) {
3278 case 'O':
3279 testname="Offline Immediate ";
3281 dotest=OFFLINE_FULL_SCAN;
3282 else
3283 state.not_cap_offline = true;
3284 break;
3285 case 'C':
3286 testname="Conveyance Self-";
3288 dotest=CONVEYANCE_SELF_TEST;
3289 else
3290 state.not_cap_conveyance = true;
3291 break;
3292 case 'S':
3293 testname="Short Self-";
3294 if (isSupportSelfTest(&data))
3295 dotest=SHORT_SELF_TEST;
3296 else
3297 state.not_cap_short = true;
3298 break;
3299 case 'L':
3300 testname="Long Self-";
3301 if (isSupportSelfTest(&data))
3302 dotest=EXTEND_SELF_TEST;
3303 else
3304 state.not_cap_long = true;
3305 break;
3306
3307 case 'c': case 'n': case 'r':
3308 testname = "Selective Self-";
3310 dotest = SELECTIVE_SELF_TEST;
3311 switch (testtype) {
3312 case 'c': mode = SEL_CONT; break;
3313 case 'n': mode = SEL_NEXT; break;
3314 case 'r': mode = SEL_REDO; break;
3315 }
3316 }
3317 else
3318 state.not_cap_selective = true;
3319 break;
3320 }
3321
3322 // If we can't do the test, exit
3323 if (dotest<0) {
3324 PrintOut(LOG_CRIT, "Device: %s, not capable of %sTest\n", name, testname);
3325 return 1;
3326 }
3327
3328 // If currently running a self-test, do not interrupt it to start another.
3329 if (15==(data.self_test_exec_status >> 4)) {
3330 if (cfg.firmwarebugs.is_set(BUG_SAMSUNG3) && data.self_test_exec_status == 0xf0) {
3331 PrintOut(LOG_INFO, "Device: %s, will not skip scheduled %sTest "
3332 "despite unclear Self-Test byte (SAMSUNG Firmware bug).\n", name, testname);
3333 } else {
3334 PrintOut(LOG_INFO, "Device: %s, skip scheduled %sTest; %1d0%% remaining of current Self-Test.\n",
3335 name, testname, (int)(data.self_test_exec_status & 0x0f));
3336 return 1;
3337 }
3338 }
3339
3340 if (dotest == SELECTIVE_SELF_TEST) {
3341 // Set test span
3342 ata_selective_selftest_args selargs, prev_args;
3343 selargs.num_spans = 1;
3344 selargs.span[0].mode = mode;
3345 prev_args.num_spans = 1;
3346 prev_args.span[0].start = state.selective_test_last_start;
3347 prev_args.span[0].end = state.selective_test_last_end;
3348 if (ataWriteSelectiveSelfTestLog(device, selargs, &data, state.num_sectors, &prev_args)) {
3349 PrintOut(LOG_CRIT, "Device: %s, prepare %sTest failed\n", name, testname);
3350 return 1;
3351 }
3352 uint64_t start = selargs.span[0].start, end = selargs.span[0].end;
3353 PrintOut(LOG_INFO, "Device: %s, %s test span at LBA %" PRIu64 " - %" PRIu64 " (%" PRIu64 " sectors, %u%% - %u%% of disk).\n",
3354 name, (selargs.span[0].mode == SEL_NEXT ? "next" : "redo"),
3355 start, end, end - start + 1,
3356 (unsigned)((100 * start + state.num_sectors/2) / state.num_sectors),
3357 (unsigned)((100 * end + state.num_sectors/2) / state.num_sectors));
3358 state.selective_test_last_start = start;
3359 state.selective_test_last_end = end;
3360 }
3361
3362 // execute the test, and return status
3363 int retval = smartcommandhandler(device, IMMEDIATE_OFFLINE, dotest, nullptr);
3364 if (retval) {
3365 PrintOut(LOG_CRIT, "Device: %s, execute %sTest failed.\n", name, testname);
3366 return retval;
3367 }
3368
3369 // Report recent test start to do_disable_standby_check()
3370 // and force log of next test status
3371 if (testtype == 'O')
3372 state.offline_started = true;
3373 else
3374 state.selftest_started = true;
3375
3376 PrintOut(LOG_INFO, "Device: %s, starting scheduled %sTest.\n", name, testname);
3377 return 0;
3378}
3379
3380// Check pending sector count attribute values (-C, -U directives).
3381static void check_pending(const dev_config & cfg, dev_state & state,
3382 unsigned char id, bool increase_only,
3383 const ata_smart_values & smartval,
3384 int mailtype, const char * msg)
3385{
3386 // Find attribute index
3387 int i = ata_find_attr_index(id, smartval);
3388 if (!(i >= 0 && ata_find_attr_index(id, state.smartval) == i))
3389 return;
3390
3391 // No report if no sectors pending.
3392 uint64_t rawval = ata_get_attr_raw_value(smartval.vendor_attributes[i], cfg.attribute_defs);
3393 if (rawval == 0) {
3394 reset_warning_mail(cfg, state, mailtype, "No more %s", msg);
3395 return;
3396 }
3397
3398 // If attribute is not reset, report only sector count increases.
3399 uint64_t prev_rawval = ata_get_attr_raw_value(state.smartval.vendor_attributes[i], cfg.attribute_defs);
3400 if (!(!increase_only || prev_rawval < rawval))
3401 return;
3402
3403 // Format message.
3404 std::string s = strprintf("Device: %s, %" PRId64 " %s", cfg.name.c_str(), rawval, msg);
3405 if (prev_rawval > 0 && rawval != prev_rawval)
3406 s += strprintf(" (changed %+" PRId64 ")", rawval - prev_rawval);
3407
3408 PrintOut(LOG_CRIT, "%s\n", s.c_str());
3409 MailWarning(cfg, state, mailtype, "%s", s.c_str());
3410 state.must_write = true;
3411}
3412
3413// Format Temperature value
3414static const char * fmt_temp(unsigned char x, char (& buf)[20])
3415{
3416 if (!x) // unset
3417 return "??";
3418 snprintf(buf, sizeof(buf), "%u", x);
3419 return buf;
3420}
3421
3422// Check Temperature limits
3423static void CheckTemperature(const dev_config & cfg, dev_state & state, unsigned char currtemp, unsigned char triptemp)
3424{
3425 if (!(0 < currtemp && currtemp < 255)) {
3426 PrintOut(LOG_INFO, "Device: %s, failed to read Temperature\n", cfg.name.c_str());
3427 return;
3428 }
3429
3430 // Update Max Temperature
3431 const char * minchg = "", * maxchg = "";
3432 if (currtemp > state.tempmax) {
3433 if (state.tempmax)
3434 maxchg = "!";
3435 state.tempmax = currtemp;
3436 state.must_write = true;
3437 }
3438
3439 char buf[20];
3440 if (!state.temperature) {
3441 // First check
3442 if (!state.tempmin || currtemp < state.tempmin)
3443 // Delay Min Temperature update by ~ 30 minutes.
3444 state.tempmin_delay = time(nullptr) + default_checktime - 60;
3445 PrintOut(LOG_INFO, "Device: %s, initial Temperature is %d Celsius (Min/Max %s/%u%s)\n",
3446 cfg.name.c_str(), (int)currtemp, fmt_temp(state.tempmin, buf), state.tempmax, maxchg);
3447 if (triptemp)
3448 PrintOut(LOG_INFO, " [trip Temperature is %d Celsius]\n", (int)triptemp);
3449 state.temperature = currtemp;
3450 }
3451 else {
3452 if (state.tempmin_delay) {
3453 // End Min Temperature update delay if ...
3454 if ( (state.tempmin && currtemp > state.tempmin) // current temp exceeds recorded min,
3455 || (state.tempmin_delay <= time(nullptr))) { // or delay time is over.
3456 state.tempmin_delay = 0;
3457 if (!state.tempmin)
3458 state.tempmin = 255;
3459 }
3460 }
3461
3462 // Update Min Temperature
3463 if (!state.tempmin_delay && currtemp < state.tempmin) {
3464 state.tempmin = currtemp;
3465 state.must_write = true;
3466 if (currtemp != state.temperature)
3467 minchg = "!";
3468 }
3469
3470 // Track changes
3471 if (cfg.tempdiff && (*minchg || *maxchg || abs((int)currtemp - (int)state.temperature) >= cfg.tempdiff)) {
3472 PrintOut(LOG_INFO, "Device: %s, Temperature changed %+d Celsius to %u Celsius (Min/Max %s%s/%u%s)\n",
3473 cfg.name.c_str(), (int)currtemp-(int)state.temperature, currtemp, fmt_temp(state.tempmin, buf), minchg, state.tempmax, maxchg);
3474 state.temperature = currtemp;
3475 }
3476 }
3477
3478 // Check limits
3479 if (cfg.tempcrit && currtemp >= cfg.tempcrit) {
3480 PrintOut(LOG_CRIT, "Device: %s, Temperature %u Celsius reached critical limit of %u Celsius (Min/Max %s%s/%u%s)\n",
3481 cfg.name.c_str(), currtemp, cfg.tempcrit, fmt_temp(state.tempmin, buf), minchg, state.tempmax, maxchg);
3482 MailWarning(cfg, state, 12, "Device: %s, Temperature %d Celsius reached critical limit of %u Celsius (Min/Max %s%s/%u%s)",
3483 cfg.name.c_str(), currtemp, cfg.tempcrit, fmt_temp(state.tempmin, buf), minchg, state.tempmax, maxchg);
3484 }
3485 else if (cfg.tempinfo && currtemp >= cfg.tempinfo) {
3486 PrintOut(LOG_INFO, "Device: %s, Temperature %u Celsius reached limit of %u Celsius (Min/Max %s%s/%u%s)\n",
3487 cfg.name.c_str(), currtemp, cfg.tempinfo, fmt_temp(state.tempmin, buf), minchg, state.tempmax, maxchg);
3488 }
3489 else if (cfg.tempcrit) {
3490 unsigned char limit = (cfg.tempinfo ? cfg.tempinfo : cfg.tempcrit-5);
3491 if (currtemp < limit)
3492 reset_warning_mail(cfg, state, 12, "Temperature %u Celsius dropped below %u Celsius", currtemp, limit);
3493 }
3494}
3495
3496// Check normalized and raw attribute values.
3497static void check_attribute(const dev_config & cfg, dev_state & state,
3498 const ata_smart_attribute & attr,
3499 const ata_smart_attribute & prev,
3500 int attridx,
3501 const ata_smart_threshold_entry * thresholds)
3502{
3503 // Check attribute and threshold
3504 ata_attr_state attrstate = ata_get_attr_state(attr, attridx, thresholds, cfg.attribute_defs);
3505 if (attrstate == ATTRSTATE_NON_EXISTING)
3506 return;
3507
3508 // If requested, check for usage attributes that have failed.
3509 if ( cfg.usagefailed && attrstate == ATTRSTATE_FAILED_NOW
3511 std::string attrname = ata_get_smart_attr_name(attr.id, cfg.attribute_defs, cfg.dev_rpm);
3512 PrintOut(LOG_CRIT, "Device: %s, Failed SMART usage Attribute: %d %s.\n", cfg.name.c_str(), attr.id, attrname.c_str());
3513 MailWarning(cfg, state, 2, "Device: %s, Failed SMART usage Attribute: %d %s.", cfg.name.c_str(), attr.id, attrname.c_str());
3514 state.must_write = true;
3515 }
3516
3517 // Return if we're not tracking this type of attribute
3518 bool prefail = !!ATTRIBUTE_FLAGS_PREFAILURE(attr.flags);
3519 if (!( ( prefail && cfg.prefail)
3520 || (!prefail && cfg.usage )))
3521 return;
3522
3523 // Return if '-I ID' was specified
3525 return;
3526
3527 // Issue warning if they don't have the same ID in all structures.
3528 if (attr.id != prev.id) {
3529 PrintOut(LOG_INFO,"Device: %s, same Attribute has different ID numbers: %d = %d\n",
3530 cfg.name.c_str(), attr.id, prev.id);
3531 return;
3532 }
3533
3534 // Compare normalized values if valid.
3535 bool valchanged = false;
3536 if (attrstate > ATTRSTATE_NO_NORMVAL) {
3537 if (attr.current != prev.current)
3538 valchanged = true;
3539 }
3540
3541 // Compare raw values if requested.
3542 bool rawchanged = false;
3543 if (cfg.monitor_attr_flags.is_set(attr.id, MONITOR_RAW)) {
3546 rawchanged = true;
3547 }
3548
3549 // Return if no change
3550 if (!(valchanged || rawchanged))
3551 return;
3552
3553 // Format value strings
3554 std::string currstr, prevstr;
3555 if (attrstate == ATTRSTATE_NO_NORMVAL) {
3556 // Print raw values only
3557 currstr = strprintf("%s (Raw)",
3558 ata_format_attr_raw_value(attr, cfg.attribute_defs).c_str());
3559 prevstr = strprintf("%s (Raw)",
3560 ata_format_attr_raw_value(prev, cfg.attribute_defs).c_str());
3561 }
3562 else if (cfg.monitor_attr_flags.is_set(attr.id, MONITOR_RAW_PRINT)) {
3563 // Print normalized and raw values
3564 currstr = strprintf("%d [Raw %s]", attr.current,
3565 ata_format_attr_raw_value(attr, cfg.attribute_defs).c_str());
3566 prevstr = strprintf("%d [Raw %s]", prev.current,
3567 ata_format_attr_raw_value(prev, cfg.attribute_defs).c_str());
3568 }
3569 else {
3570 // Print normalized values only
3571 currstr = strprintf("%d", attr.current);
3572 prevstr = strprintf("%d", prev.current);
3573 }
3574
3575 // Format message
3576 std::string msg = strprintf("Device: %s, SMART %s Attribute: %d %s changed from %s to %s",
3577 cfg.name.c_str(), (prefail ? "Prefailure" : "Usage"), attr.id,
3578 ata_get_smart_attr_name(attr.id, cfg.attribute_defs, cfg.dev_rpm).c_str(),
3579 prevstr.c_str(), currstr.c_str());
3580
3581 // Report this change as critical ?
3582 if ( (valchanged && cfg.monitor_attr_flags.is_set(attr.id, MONITOR_AS_CRIT))
3583 || (rawchanged && cfg.monitor_attr_flags.is_set(attr.id, MONITOR_RAW_AS_CRIT))) {
3584 PrintOut(LOG_CRIT, "%s\n", msg.c_str());
3585 MailWarning(cfg, state, 2, "%s", msg.c_str());
3586 }
3587 else {
3588 PrintOut(LOG_INFO, "%s\n", msg.c_str());
3589 }
3590 state.must_write = true;
3591}
3592
3593
3594static int ATACheckDevice(const dev_config & cfg, dev_state & state, ata_device * atadev,
3595 bool firstpass, bool allow_selftests)
3596{
3597 if (!open_device(cfg, state, atadev, "ATA"))
3598 return 1;
3599
3600 const char * name = cfg.name.c_str();
3601
3602 // user may have requested (with the -n Directive) to leave the disk
3603 // alone if it is in idle or sleeping mode. In this case check the
3604 // power mode and exit without check if needed
3605 if (cfg.powermode && !state.powermodefail) {
3606 int dontcheck=0, powermode=ataCheckPowerMode(atadev);
3607 const char * mode = 0;
3608 if (0 <= powermode && powermode < 0xff) {
3609 // wait for possible spin up and check again
3610 int powermode2;
3611 sleep(5);
3612 powermode2 = ataCheckPowerMode(atadev);
3613 if (powermode2 > powermode)
3614 PrintOut(LOG_INFO, "Device: %s, CHECK POWER STATUS spins up disk (0x%02x -> 0x%02x)\n", name, powermode, powermode2);
3615 powermode = powermode2;
3616 }
3617
3618 switch (powermode){
3619 case -1:
3620 // SLEEP
3621 mode="SLEEP";
3622 if (cfg.powermode>=1)
3623 dontcheck=1;
3624 break;
3625 case 0x00:
3626 // STANDBY
3627 mode="STANDBY";
3628 if (cfg.powermode>=2)
3629 dontcheck=1;
3630 break;
3631 case 0x01:
3632 // STANDBY_Y
3633 mode="STANDBY_Y";
3634 if (cfg.powermode>=2)
3635 dontcheck=1;
3636 break;
3637 case 0x80:
3638 // IDLE
3639 mode="IDLE";
3640 if (cfg.powermode>=3)
3641 dontcheck=1;
3642 break;
3643 case 0x81:
3644 // IDLE_A
3645 mode="IDLE_A";
3646 if (cfg.powermode>=3)
3647 dontcheck=1;
3648 break;
3649 case 0x82:
3650 // IDLE_B
3651 mode="IDLE_B";
3652 if (cfg.powermode>=3)
3653 dontcheck=1;
3654 break;
3655 case 0x83:
3656 // IDLE_C
3657 mode="IDLE_C";
3658 if (cfg.powermode>=3)
3659 dontcheck=1;
3660 break;
3661 case 0xff:
3662 // ACTIVE/IDLE
3663 case 0x40:
3664 // ACTIVE
3665 case 0x41:
3666 // ACTIVE
3667 mode="ACTIVE or IDLE";
3668 break;
3669 default:
3670 // UNKNOWN
3671 PrintOut(LOG_CRIT, "Device: %s, CHECK POWER STATUS returned %d, not ATA compliant, ignoring -n Directive\n",
3672 name, powermode);
3673 state.powermodefail = true;
3674 break;
3675 }
3676
3677 // if we are going to skip a check, return now
3678 if (dontcheck){
3679 // skip at most powerskipmax checks
3680 if (!cfg.powerskipmax || state.powerskipcnt<cfg.powerskipmax) {
3681 CloseDevice(atadev, name);
3682 // report first only except if state has changed, avoid waking up system disk
3683 if ((!state.powerskipcnt || state.lastpowermodeskipped != powermode) && !cfg.powerquiet) {
3684 PrintOut(LOG_INFO, "Device: %s, is in %s mode, suspending checks\n", name, mode);
3685 state.lastpowermodeskipped = powermode;
3686 }
3687 state.powerskipcnt++;
3688 return 0;
3689 }
3690 else {
3691 PrintOut(LOG_INFO, "Device: %s, %s mode ignored due to reached limit of skipped checks (%d check%s skipped)\n",
3692 name, mode, state.powerskipcnt, (state.powerskipcnt==1?"":"s"));
3693 }
3694 state.powerskipcnt = 0;
3695 state.tempmin_delay = time(nullptr) + default_checktime - 60; // Delay Min Temperature update
3696 }
3697 else if (state.powerskipcnt) {
3698 PrintOut(LOG_INFO, "Device: %s, is back in %s mode, resuming checks (%d check%s skipped)\n",
3699 name, mode, state.powerskipcnt, (state.powerskipcnt==1?"":"s"));
3700 state.powerskipcnt = 0;
3701 state.tempmin_delay = time(nullptr) + default_checktime - 60; // Delay Min Temperature update
3702 }
3703 }
3704
3705 // check smart status
3706 if (cfg.smartcheck) {
3707 int status=ataSmartStatus2(atadev);
3708 if (status==-1){
3709 PrintOut(LOG_INFO,"Device: %s, not capable of SMART self-check\n",name);
3710 MailWarning(cfg, state, 5, "Device: %s, not capable of SMART self-check", name);
3711 state.must_write = true;
3712 }
3713 else if (status==1){
3714 PrintOut(LOG_CRIT, "Device: %s, FAILED SMART self-check. BACK UP DATA NOW!\n", name);
3715 MailWarning(cfg, state, 1, "Device: %s, FAILED SMART self-check. BACK UP DATA NOW!", name);
3716 state.must_write = true;
3717 }
3718 }
3719
3720 // Check everything that depends upon SMART Data (eg, Attribute values)
3721 if ( cfg.usagefailed || cfg.prefail || cfg.usage
3722 || cfg.curr_pending_id || cfg.offl_pending_id
3723 || cfg.tempdiff || cfg.tempinfo || cfg.tempcrit
3724 || cfg.selftest || cfg.offlinests || cfg.selfteststs) {
3725
3726 // Read current attribute values.
3727 ata_smart_values curval;
3728 if (ataReadSmartValues(atadev, &curval)){
3729 PrintOut(LOG_CRIT, "Device: %s, failed to read SMART Attribute Data\n", name);
3730 MailWarning(cfg, state, 6, "Device: %s, failed to read SMART Attribute Data", name);
3731 state.must_write = true;
3732 }
3733 else {
3734 reset_warning_mail(cfg, state, 6, "read SMART Attribute Data worked again");
3735
3736 // look for current or offline pending sectors
3737 if (cfg.curr_pending_id)
3738 check_pending(cfg, state, cfg.curr_pending_id, cfg.curr_pending_incr, curval, 10,
3739 (!cfg.curr_pending_incr ? "Currently unreadable (pending) sectors"
3740 : "Total unreadable (pending) sectors" ));
3741
3742 if (cfg.offl_pending_id)
3743 check_pending(cfg, state, cfg.offl_pending_id, cfg.offl_pending_incr, curval, 11,
3744 (!cfg.offl_pending_incr ? "Offline uncorrectable sectors"
3745 : "Total offline uncorrectable sectors"));
3746
3747 // check temperature limits
3748 if (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)
3749 CheckTemperature(cfg, state, ata_return_temperature_value(&curval, cfg.attribute_defs), 0);
3750
3751 // look for failed usage attributes, or track usage or prefail attributes
3752 if (cfg.usagefailed || cfg.prefail || cfg.usage) {
3753 for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
3754 check_attribute(cfg, state,
3755 curval.vendor_attributes[i],
3756 state.smartval.vendor_attributes[i],
3757 i, state.smartthres.thres_entries);
3758 }
3759 }
3760
3761 // Log changes of offline data collection status
3762 if (cfg.offlinests) {
3765 || state.offline_started // test was started in previous call
3766 || (firstpass && (debugmode || (curval.offline_data_collection_status & 0x7d))))
3768 }
3769
3770 // Log changes of self-test execution status
3771 if (cfg.selfteststs) {
3773 || state.selftest_started // test was started in previous call
3774 || (firstpass && (debugmode || (curval.self_test_exec_status & 0xf0))))
3776 }
3777
3778 // Save the new values for the next time around
3779 state.smartval = curval;
3781 state.attrlog_dirty = true;
3782 }
3783 }
3784 state.offline_started = state.selftest_started = false;
3785
3786 // check if number of selftest errors has increased (note: may also DECREASE)
3787 if (cfg.selftest) {
3788 unsigned hour = 0;
3789 int errcnt = check_ata_self_test_log(atadev, name, cfg.firmwarebugs, hour);
3790 report_self_test_log_changes(cfg, state, errcnt, hour);
3791 }
3792
3793 // check if number of ATA errors has increased
3794 if (cfg.errorlog || cfg.xerrorlog) {
3795
3796 int errcnt1 = -1, errcnt2 = -1;
3797 if (cfg.errorlog)
3798 errcnt1 = read_ata_error_count(atadev, name, cfg.firmwarebugs, false);
3799 if (cfg.xerrorlog)
3800 errcnt2 = read_ata_error_count(atadev, name, cfg.firmwarebugs, true);
3801
3802 // new number of errors is max of both logs
3803 int newc = (errcnt1 >= errcnt2 ? errcnt1 : errcnt2);
3804
3805 // did command fail?
3806 if (newc<0)
3807 // lack of PrintOut here is INTENTIONAL
3808 MailWarning(cfg, state, 7, "Device: %s, Read SMART Error Log Failed", name);
3809
3810 // has error count increased?
3811 int oldc = state.ataerrorcount;
3812 if (newc>oldc){
3813 PrintOut(LOG_CRIT, "Device: %s, ATA error count increased from %d to %d\n",
3814 name, oldc, newc);
3815 MailWarning(cfg, state, 4, "Device: %s, ATA error count increased from %d to %d",
3816 name, oldc, newc);
3817 state.must_write = true;
3818 }
3819
3820 if (newc>=0)
3821 state.ataerrorcount=newc;
3822 }
3823
3824 // if the user has asked, and device is capable (or we're not yet
3825 // sure) check whether a self test should be done now.
3826 if (allow_selftests && !cfg.test_regex.empty()) {
3827 char testtype = next_scheduled_test(cfg, state, false/*!scsi*/);
3828 if (testtype)
3829 DoATASelfTest(cfg, state, atadev, testtype);
3830 }
3831
3832 // Don't leave device open -- the OS/user may want to access it
3833 // before the next smartd cycle!
3834 CloseDevice(atadev, name);
3835 return 0;
3836}
3837
3838static int SCSICheckDevice(const dev_config & cfg, dev_state & state, scsi_device * scsidev, bool allow_selftests)
3839{
3840 if (!open_device(cfg, state, scsidev, "SCSI"))
3841 return 1;
3842
3843 const char * name = cfg.name.c_str();
3844
3845 uint8_t asc = 0, ascq = 0;
3846 uint8_t currenttemp = 0, triptemp = 0;
3847 if (!state.SuppressReport) {
3848 if (scsiCheckIE(scsidev, state.SmartPageSupported, state.TempPageSupported,
3849 &asc, &ascq, &currenttemp, &triptemp)) {
3850 PrintOut(LOG_INFO, "Device: %s, failed to read SMART values\n",
3851 name);
3852 MailWarning(cfg, state, 6, "Device: %s, failed to read SMART values", name);
3853 state.SuppressReport = 1;
3854 }
3855 }
3856 if (asc > 0) {
3857 char b[128];
3858 const char * cp = scsiGetIEString(asc, ascq, b, sizeof(b));
3859
3860 if (cp) {
3861 PrintOut(LOG_CRIT, "Device: %s, SMART Failure: %s\n", name, cp);
3862 MailWarning(cfg, state, 1,"Device: %s, SMART Failure: %s", name, cp);
3863 } else if (asc == 4 && ascq == 9) {
3864 PrintOut(LOG_INFO,"Device: %s, self-test in progress\n", name);
3865 } else if (debugmode)
3866 PrintOut(LOG_INFO,"Device: %s, non-SMART asc,ascq: %d,%d\n",
3867 name, (int)asc, (int)ascq);
3868 } else if (debugmode)
3869 PrintOut(LOG_INFO,"Device: %s, SMART health: passed\n", name);
3870
3871 // check temperature limits
3872 if (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)
3873 CheckTemperature(cfg, state, currenttemp, triptemp);
3874
3875 // check if number of selftest errors has increased (note: may also DECREASE)
3876 if (cfg.selftest) {
3877 int retval = scsiCountFailedSelfTests(scsidev, 0);
3878 report_self_test_log_changes(cfg, state, (retval >= 0 ? (retval & 0xff) : -1), retval >> 8);
3879 }
3880
3881 if (allow_selftests && !cfg.test_regex.empty()) {
3882 char testtype = next_scheduled_test(cfg, state);
3883 if (testtype)
3884 DoSCSISelfTest(cfg, state, scsidev, testtype);
3885 }
3886 if (!cfg.attrlog_file.empty()){
3887 // saving error counters to state
3888 uint8_t tBuf[252];
3889 if (state.ReadECounterPageSupported && (0 == scsiLogSense(scsidev,
3890 READ_ERROR_COUNTER_LPAGE, 0, tBuf, sizeof(tBuf), 0))) {
3893 state.scsi_error_counters[0].found=1;
3894 }
3895 if (state.WriteECounterPageSupported && (0 == scsiLogSense(scsidev,
3896 WRITE_ERROR_COUNTER_LPAGE, 0, tBuf, sizeof(tBuf), 0))) {
3899 state.scsi_error_counters[1].found=1;
3900 }
3901 if (state.VerifyECounterPageSupported && (0 == scsiLogSense(scsidev,
3902 VERIFY_ERROR_COUNTER_LPAGE, 0, tBuf, sizeof(tBuf), 0))) {
3905 state.scsi_error_counters[2].found=1;
3906 }
3907 if (state.NonMediumErrorPageSupported && (0 == scsiLogSense(scsidev,
3908 NON_MEDIUM_ERROR_LPAGE, 0, tBuf, sizeof(tBuf), 0))) {
3912 }
3913 // store temperature if not done by CheckTemperature() above
3914 if (!(cfg.tempdiff || cfg.tempinfo || cfg.tempcrit))
3915 state.temperature = currenttemp;
3916 }
3917 CloseDevice(scsidev, name);
3918 state.attrlog_dirty = true;
3919 return 0;
3920}
3921
3922// Log NVMe self-test execution status changes
3923static void log_nvme_self_test_exec_status(const char * name, dev_state & state, bool firstpass,
3924 const nvme_self_test_log & self_test_log)
3925{
3926 uint8_t curr_op = self_test_log.current_operation & 0xf;
3927 uint8_t curr_compl = self_test_log.current_completion & 0x7f;
3928
3929 // Return if no changes and log not forced
3930 if (!( curr_op != state.selftest_op
3931 || curr_compl != state.selftest_compl
3932 || state.selftest_started // test was started in previous call
3933 || (firstpass && (debugmode || curr_op))))
3934 return;
3935
3936 state.selftest_op = curr_op;
3937 state.selftest_compl = curr_compl;
3938
3939 const nvme_self_test_result & r = self_test_log.results[0];
3940 uint8_t op0 = r.self_test_status >> 4, res0 = r.self_test_status & 0xf;
3941
3942 uint8_t op = (curr_op ? curr_op : op0);
3943 const char * t; char tb[32];
3944 switch (op) {
3945 case 0x0: t = ""; break;
3946 case 0x1: t = "short"; break;
3947 case 0x2: t = "extended"; break;
3948 case 0xe: t = "vendor specific"; break;
3949 default: snprintf(tb, sizeof(tb), "unknown (0x%x)", op);
3950 t = tb; break;
3951 }
3952
3953 if (curr_op) {
3954 PrintOut(LOG_INFO, "Device %s, %s self-test in progress, %d%% remaining\n",
3955 name, t, 100 - curr_compl);
3956 }
3957 else if (!op0 || res0 == 0xf) { // First entry unused
3958 PrintOut(LOG_INFO, "Device %s, no self-test has ever been run\n", name);
3959 }
3960 else {
3961 // Report last test result from first log entry
3962 const char * m; char mb[48];
3963 switch (res0) {
3964 case 0x0: m = "completed without error"; break;
3965 case 0x1: m = "was aborted by a self-test command"; break;
3966 case 0x2: m = "was aborted by a controller reset"; break;
3967 case 0x3: m = "was aborted due to a namespace removal"; break;
3968 case 0x4: m = "was aborted by a format NVM command"; break;
3969 case 0x5: m = "completed with error (fatal or unknown error)"; break;
3970 case 0x6: m = "completed with error (unknown failed segment)"; break;
3971 case 0x7: m = "completed with error (failed segments)"; break;
3972 case 0x8: m = "was aborted (unknown reason)"; break;
3973 case 0x9: m = "was aborted due to a sanitize operation"; break;
3974 default: snprintf(mb, sizeof(mb), "returned an unknown result (0x%x)", res0);
3975 m = mb; break;
3976 }
3977
3978 char ns[32] = "";
3979 if (r.valid & 0x01)
3980 snprintf(ns, sizeof(ns), " of NSID 0x%x", r.nsid);
3981
3982 PrintOut((0x5 <= res0 && res0 <= 0x7 ? LOG_CRIT : LOG_INFO),
3983 "Device %s, previous %s self-test%s %s\n", name, t, ns, m);
3984 }
3985}
3986
3987// Count error entries in NVMe self-test log, set HOUR to power on hours of most
3988// recent error. Return the error count.
3989static int check_nvme_self_test_log(uint32_t nsid, const nvme_self_test_log & self_test_log,
3990 uint64_t & hour)
3991{
3992 hour = 0;
3993 int errcnt = 0;
3994
3995 for (unsigned i = 0; i < 20; i++) {
3996 const nvme_self_test_result & r = self_test_log.results[i];
3997 uint8_t op = r.self_test_status >> 4;
3998 uint8_t res = r.self_test_status & 0xf;
3999 if (!op || res == 0xf)
4000 continue; // Unused entry
4001
4002 if (!( nsid == nvme_broadcast_nsid
4003 || !(r.valid & 0x01) /* No NSID */
4004 || r.nsid == nvme_broadcast_nsid || r.nsid == nsid))
4005 continue; // Different individual namespace
4006
4007 if (op == 0x2 /* Extended */ && !res /* Completed without error */)
4008 break; // Stop count at first successful extended test
4009
4010 if (!(0x5 <= res && res <= 0x7))
4011 continue; // No error or aborted
4012
4013 // Error found
4014 if (++errcnt != 1)
4015 continue; // Not most recent error
4016
4017 // Keep track of time of most recent error
4019 }
4020
4021 return errcnt;
4022}
4023
4024static int start_nvme_self_test(const dev_config & cfg, dev_state & state, nvme_device * device,
4025 char testtype, const nvme_self_test_log & self_test_log)
4026{
4027 const char *name = cfg.name.c_str();
4028 unsigned nsid = device->get_nsid();
4029
4030 const char *testname; uint8_t stc;
4031 switch (testtype) {
4032 case 'S': testname = "Short"; stc = 1; break;
4033 case 'L': testname = "Extended"; stc = 2; break;
4034 default: // Should not happen
4035 PrintOut(LOG_INFO, "Device: %s, not capable of %c Self-Test\n", name, testtype);
4036 return 1;
4037 }
4038
4039 // If currently running a self-test, do not try to start another.
4040 if (self_test_log.current_operation & 0xf) {
4041 PrintOut(LOG_INFO, "Device: %s, skip scheduled %s Self-Test (NSID 0x%x); %d%% remaining of current Self-Test.\n",
4042 name, testname, nsid, 100 - (self_test_log.current_completion & 0x7f));
4043 return 1;
4044 }
4045
4046 if (!nvme_self_test(device, stc, nsid)) {
4047 PrintOut(LOG_CRIT, "Device: %s, execute %s Self-Test failed (NSID 0x%x): %s.\n",
4048 name, testname, nsid, device->get_errmsg());
4049 return 1;
4050 }
4051
4052 // Report recent test start to do_disable_standby_check()
4053 // and force log of next test status
4054 // TODO: Add NVMe support to do_disable_standby_check()
4055 state.selftest_started = true;
4056
4057 PrintOut(LOG_INFO, "Device: %s, starting scheduled %s Self-Test (NSID 0x%x).\n",
4058 name, testname, nsid);
4059 return 0;
4060}
4061
4062static int NVMeCheckDevice(const dev_config & cfg, dev_state & state, nvme_device * nvmedev, bool firstpass, bool allow_selftests)
4063{
4064 if (!open_device(cfg, state, nvmedev, "NVMe"))
4065 return 1;
4066
4067 const char * name = cfg.name.c_str();
4068
4069 // Read SMART/Health log
4070 // TODO: Support per namespace SMART/Health log
4071 nvme_smart_log smart_log;
4072 if (!nvme_read_smart_log(nvmedev, nvme_broadcast_nsid, smart_log)) {
4073 CloseDevice(nvmedev, name);
4074 PrintOut(LOG_INFO, "Device: %s, failed to read NVMe SMART/Health Information\n", name);
4075 MailWarning(cfg, state, 6, "Device: %s, failed to read NVMe SMART/Health Information", name);
4076 state.must_write = true;
4077 return 0;
4078 }
4079
4080 // Check Critical Warning bits
4081 if (cfg.smartcheck && smart_log.critical_warning) {
4082 unsigned char w = smart_log.critical_warning;
4083 std::string msg;
4084 static const char * const wnames[] =
4085 {"LowSpare", "Temperature", "Reliability", "R/O", "VolMemBackup"};
4086
4087 for (unsigned b = 0, cnt = 0; b < 8 ; b++) {
4088 if (!(w & (1 << b)))
4089 continue;
4090 if (cnt)
4091 msg += ", ";
4092 if (++cnt > 3) {
4093 msg += "..."; break;
4094 }
4095 if (b >= sizeof(wnames)/sizeof(wnames[0])) {
4096 msg += "*Unknown*"; break;
4097 }
4098 msg += wnames[b];
4099 }
4100
4101 PrintOut(LOG_CRIT, "Device: %s, Critical Warning (0x%02x): %s\n", name, w, msg.c_str());
4102 MailWarning(cfg, state, 1, "Device: %s, Critical Warning (0x%02x): %s", name, w, msg.c_str());
4103 state.must_write = true;
4104 }
4105
4106 // Check temperature limits
4107 if (cfg.tempdiff || cfg.tempinfo || cfg.tempcrit) {
4108 uint16_t k = sg_get_unaligned_le16(smart_log.temperature);
4109 // Convert Kelvin to positive Celsius (TODO: Allow negative temperatures)
4110 int c = (int)k - 273;
4111 if (c < 1)
4112 c = 1;
4113 else if (c > 0xff)
4114 c = 0xff;
4115 CheckTemperature(cfg, state, c, 0);
4116 }
4117
4118 // Check for test schedule
4119 char testtype = (allow_selftests && !cfg.test_regex.empty()
4120 ? next_scheduled_test(cfg, state) : 0);
4121
4122 // Read the self-test log if required
4123 nvme_self_test_log self_test_log{};
4124 if (testtype || cfg.selftest || cfg.selfteststs) {
4125 if (!nvme_read_self_test_log(nvmedev, nvme_broadcast_nsid, self_test_log)) {
4126 PrintOut(LOG_CRIT, "Device: %s, Read Self-test Log failed: %s\n",
4127 name, nvmedev->get_errmsg());
4128 MailWarning(cfg, state, 8, "Device: %s, Read Self-test Log failed: %s\n",
4129 name, nvmedev->get_errmsg());
4130 testtype = 0;
4131 }
4132 else {
4133 reset_warning_mail(cfg, state, 8, "Read Self-Test Log worked again");
4134
4135 // Log changes of self-test execution status
4136 if (cfg.selfteststs)
4137 log_nvme_self_test_exec_status(name, state, firstpass, self_test_log);
4138
4139 // Check if number of selftest errors has increased (note: may also DECREASE)
4140 if (cfg.selftest) {
4141 uint64_t hour = 0;
4142 int errcnt = check_nvme_self_test_log(nvmedev->get_nsid(), self_test_log, hour);
4143 report_self_test_log_changes(cfg, state, errcnt, hour);
4144 }
4145 }
4146 }
4147 state.selftest_started = false;
4148
4149 // Check if number of errors has increased
4150 if (cfg.errorlog || cfg.xerrorlog) {
4151 uint64_t newcnt = le128_to_uint64(smart_log.num_err_log_entries);
4152 if (newcnt > state.nvme_err_log_entries) {
4153 // Warn only if device related errors are found
4154 check_nvme_error_log(cfg, state, nvmedev, newcnt);
4155 }
4156 // else // TODO: Handle decrease of count?
4157 }
4158
4159 // Start self-test if scheduled
4160 if (testtype)
4161 start_nvme_self_test(cfg, state, nvmedev, testtype, self_test_log);
4162
4163 CloseDevice(nvmedev, name);
4164 state.attrlog_dirty = true;
4165 return 0;
4166}
4167
4168// 0=not used, 1=not disabled, 2=disable rejected by OS, 3=disabled
4170
4172{
4173 // Check for '-l offlinests,ns' or '-l selfteststs,ns' directives
4174 bool sts1 = false, sts2 = false;
4175 for (const auto & cfg : configs) {
4176 if (cfg.offlinests_ns)
4177 sts1 = true;
4178 if (cfg.selfteststs_ns)
4179 sts2 = true;
4180 }
4181
4182 // Check for support of disable auto standby
4183 // Reenable standby if smartd.conf was reread
4184 if (sts1 || sts2 || standby_disable_state == 3) {
4185 if (!smi()->disable_system_auto_standby(false)) {
4186 if (standby_disable_state == 3)
4187 PrintOut(LOG_CRIT, "System auto standby enable failed: %s\n", smi()->get_errmsg());
4188 if (sts1 || sts2) {
4189 PrintOut(LOG_INFO, "Disable auto standby not supported, ignoring ',ns' from %s%s%s\n",
4190 (sts1 ? "-l offlinests,ns" : ""), (sts1 && sts2 ? " and " : ""), (sts2 ? "-l selfteststs,ns" : ""));
4191 sts1 = sts2 = false;
4192 }
4193 }
4194 }
4195
4196 standby_disable_state = (sts1 || sts2 ? 1 : 0);
4197}
4198
4199static void do_disable_standby_check(const dev_config_vector & configs, const dev_state_vector & states)
4200{
4202 return;
4203
4204 // Check for just started or still running self-tests
4205 bool running = false;
4206 for (unsigned i = 0; i < configs.size() && !running; i++) {
4207 const dev_config & cfg = configs.at(i); const dev_state & state = states.at(i);
4208
4209 if ( ( cfg.offlinests_ns
4210 && (state.offline_started ||
4212 || ( cfg.selfteststs_ns
4213 && (state.selftest_started ||
4215 running = true;
4216 // state.offline/selftest_started will be reset after next logging of test status
4217 }
4218
4219 // Disable/enable auto standby and log state changes
4220 if (!running) {
4221 if (standby_disable_state != 1) {
4222 if (!smi()->disable_system_auto_standby(false))
4223 PrintOut(LOG_CRIT, "Self-test(s) completed, system auto standby enable failed: %s\n",
4224 smi()->get_errmsg());
4225 else
4226 PrintOut(LOG_INFO, "Self-test(s) completed, system auto standby enabled\n");
4228 }
4229 }
4230 else if (!smi()->disable_system_auto_standby(true)) {
4231 if (standby_disable_state != 2) {
4232 PrintOut(LOG_INFO, "Self-test(s) in progress, system auto standby disable rejected: %s\n",
4233 smi()->get_errmsg());
4235 }
4236 }
4237 else {
4238 if (standby_disable_state != 3) {
4239 PrintOut(LOG_INFO, "Self-test(s) in progress, system auto standby disabled\n");
4241 }
4242 }
4243}
4244
4245// Checks the SMART status of all ATA and SCSI devices
4246static void CheckDevicesOnce(const dev_config_vector & configs, dev_state_vector & states,
4247 smart_device_list & devices, bool firstpass, bool allow_selftests)
4248{
4249 for (unsigned i = 0; i < configs.size(); i++) {
4250 const dev_config & cfg = configs.at(i);
4251 dev_state & state = states.at(i);
4252 if (state.skip) {
4253 if (debugmode)
4254 PrintOut(LOG_INFO, "Device: %s, skipped (interval=%d)\n", cfg.name.c_str(),
4255 (cfg.checktime ? cfg.checktime : checktime));
4256 continue;
4257 }
4258
4259 smart_device * dev = devices.at(i);
4260 if (dev->is_ata())
4261 ATACheckDevice(cfg, state, dev->to_ata(), firstpass, allow_selftests);
4262 else if (dev->is_scsi())
4263 SCSICheckDevice(cfg, state, dev->to_scsi(), allow_selftests);
4264 else if (dev->is_nvme())
4265 NVMeCheckDevice(cfg, state, dev->to_nvme(), firstpass, allow_selftests);
4266
4267 // Prevent systemd unit startup timeout when checking many devices on startup
4269 }
4270
4271 do_disable_standby_check(configs, states);
4272}
4273
4274// Install all signal handlers
4276{
4277 // normal and abnormal exit
4280
4281 // in debug mode, <CONTROL-C> ==> HUP
4283
4284 // Catch HUP and USR1
4287#ifdef _WIN32
4288 set_signal_if_not_ignored(SIGUSR2, USR2handler);
4289#endif
4290}
4291
4292#ifdef _WIN32
4293// Toggle debug mode implemented for native windows only
4294// (there is no easy way to reopen tty on *nix)
4295static void ToggleDebugMode()
4296{
4297 if (!debugmode) {
4298 PrintOut(LOG_INFO,"Signal USR2 - enabling debug mode\n");
4299 if (!daemon_enable_console("smartd [Debug]")) {
4300 debugmode = 1;
4301 daemon_signal(SIGINT, HUPhandler);
4302 PrintOut(LOG_INFO,"smartd debug mode enabled, PID=%d\n", getpid());
4303 }
4304 else
4305 PrintOut(LOG_INFO,"enable console failed\n");
4306 }
4307 else if (debugmode == 1) {
4308 daemon_disable_console();
4309 debugmode = 0;
4310 daemon_signal(SIGINT, sighandler);
4311 PrintOut(LOG_INFO,"Signal USR2 - debug mode disabled\n");
4312 }
4313 else
4314 PrintOut(LOG_INFO,"Signal USR2 - debug mode %d not changed\n", debugmode);
4315}
4316#endif
4317
4318time_t calc_next_wakeuptime(time_t wakeuptime, time_t timenow, int ct)
4319{
4320 if (timenow < wakeuptime)
4321 return wakeuptime;
4322 return timenow + ct - (timenow - wakeuptime) % ct;
4323}
4324
4325static time_t dosleep(time_t wakeuptime, const dev_config_vector & configs,
4326 dev_state_vector & states, bool & sigwakeup)
4327{
4328 // If past wake-up-time, compute next wake-up-time
4329 time_t timenow = time(nullptr);
4330 unsigned n = configs.size();
4331 int ct;
4332 if (!checktime_min) {
4333 // Same for all devices
4334 wakeuptime = calc_next_wakeuptime(wakeuptime, timenow, checktime);
4335 ct = checktime;
4336 }
4337 else {
4338 // Determine wakeuptime of next device(s)
4339 wakeuptime = 0;
4340 for (unsigned i = 0; i < n; i++) {
4341 const dev_config & cfg = configs.at(i);
4342 dev_state & state = states.at(i);
4343 if (!state.skip)
4344 state.wakeuptime = calc_next_wakeuptime((state.wakeuptime ? state.wakeuptime : timenow),
4345 timenow, (cfg.checktime ? cfg.checktime : checktime));
4346 if (!wakeuptime || state.wakeuptime < wakeuptime)
4347 wakeuptime = state.wakeuptime;
4348 }
4349 ct = checktime_min;
4350 }
4351
4352 notify_wait(wakeuptime, n);
4353
4354 // Sleep until we catch a signal or have completed sleeping
4355 bool no_skip = false;
4356 int addtime = 0;
4357 while (timenow < wakeuptime+addtime && !caughtsigUSR1 && !caughtsigHUP && !caughtsigEXIT) {
4358 // Restart if system clock has been adjusted to the past
4359 if (wakeuptime > timenow + ct) {
4360 PrintOut(LOG_INFO, "System clock time adjusted to the past. Resetting next wakeup time.\n");
4361 wakeuptime = timenow + ct;
4362 for (auto & state : states)
4363 state.wakeuptime = 0;
4364 no_skip = true;
4365 }
4366
4367 // Exit sleep when time interval has expired or a signal is received
4368 sleep(wakeuptime+addtime-timenow);
4369
4370#ifdef _WIN32
4371 // toggle debug mode?
4372 if (caughtsigUSR2) {
4373 ToggleDebugMode();
4374 caughtsigUSR2 = 0;
4375 }
4376#endif
4377
4378 timenow = time(nullptr);
4379
4380 // Actual sleep time too long?
4381 if (!addtime && timenow > wakeuptime+60) {
4382 if (debugmode)
4383 PrintOut(LOG_INFO, "Sleep time was %d seconds too long, assuming wakeup from standby mode.\n",
4384 (int)(timenow-wakeuptime));
4385 // Wait another 20 seconds to avoid I/O errors during disk spin-up
4386 addtime = timenow-wakeuptime+20;
4387 // Use next wake-up-time if close
4388 int nextcheck = ct - addtime % ct;
4389 if (nextcheck <= 20)
4390 addtime += nextcheck;
4391 }
4392 }
4393
4394 // if we caught a SIGUSR1 then print message and clear signal
4395 if (caughtsigUSR1){
4396 PrintOut(LOG_INFO,"Signal USR1 - checking devices now rather than in %d seconds.\n",
4397 wakeuptime-timenow>0?(int)(wakeuptime-timenow):0);
4398 caughtsigUSR1=0;
4399 sigwakeup = no_skip = true;
4400 }
4401
4402 // Check which devices must be skipped in this cycle
4403 if (checktime_min) {
4404 for (auto & state : states)
4405 state.skip = (!no_skip && timenow < state.wakeuptime);
4406 }
4407
4408 // return adjusted wakeuptime
4409 return wakeuptime;
4410}
4411
4412// Print out a list of valid arguments for the Directive d
4413static void printoutvaliddirectiveargs(int priority, char d)
4414{
4415 switch (d) {
4416 case 'n':
4417 PrintOut(priority, "never[,N][,q], sleep[,N][,q], standby[,N][,q], idle[,N][,q]");
4418 break;
4419 case 's':
4420 PrintOut(priority, "valid_regular_expression");
4421 break;
4422 case 'd':
4423 PrintOut(priority, "%s", smi()->get_valid_dev_types_str().c_str());
4424 break;
4425 case 'T':
4426 PrintOut(priority, "normal, permissive");
4427 break;
4428 case 'o':
4429 case 'S':
4430 PrintOut(priority, "on, off");
4431 break;
4432 case 'l':
4433 PrintOut(priority, "error, selftest");
4434 break;
4435 case 'M':
4436 PrintOut(priority, "\"once\", \"always\", \"daily\", \"diminishing\", \"test\", \"exec\"");
4437 break;
4438 case 'v':
4439 PrintOut(priority, "\n%s\n", create_vendor_attribute_arg_list().c_str());
4440 break;
4441 case 'P':
4442 PrintOut(priority, "use, ignore, show, showall");
4443 break;
4444 case 'F':
4445 PrintOut(priority, "%s", get_valid_firmwarebug_args());
4446 break;
4447 case 'e':
4448 PrintOut(priority, "aam,[N|off], apm,[N|off], lookahead,[on|off], dsn,[on|off] "
4449 "security-freeze, standby,[N|off], wcache,[on|off]");
4450 break;
4451 case 'c':
4452 PrintOut(priority, "i=N, interval=N");
4453 break;
4454 }
4455}
4456
4457// exits with an error message, or returns integer value of token
4458static int GetInteger(const char *arg, const char *name, const char *token, int lineno, const char *cfgfile,
4459 int min, int max, char * suffix = 0)
4460{
4461 // make sure argument is there
4462 if (!arg) {
4463 PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s takes integer argument from %d to %d.\n",
4464 cfgfile, lineno, name, token, min, max);
4465 return -1;
4466 }
4467
4468 // get argument value (base 10), check that it's integer, and in-range
4469 char *endptr;
4470 int val = strtol(arg,&endptr,10);
4471
4472 // optional suffix present?
4473 if (suffix) {
4474 if (!strcmp(endptr, suffix))
4475 endptr += strlen(suffix);
4476 else
4477 *suffix = 0;
4478 }
4479
4480 if (!(!*endptr && min <= val && val <= max)) {
4481 PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s has argument: %s; needs integer from %d to %d.\n",
4482 cfgfile, lineno, name, token, arg, min, max);
4483 return -1;
4484 }
4485
4486 // all is well; return value
4487 return val;
4488}
4489
4490
4491// Get 1-3 small integer(s) for '-W' directive
4492static int Get3Integers(const char *arg, const char *name, const char *token, int lineno, const char *cfgfile,
4493 unsigned char *val1, unsigned char *val2, unsigned char *val3)
4494{
4495 unsigned v1 = 0, v2 = 0, v3 = 0;
4496 int n1 = -1, n2 = -1, n3 = -1, len;
4497 if (!arg) {
4498 PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s takes 1-3 integer argument(s) from 0 to 255.\n",
4499 cfgfile, lineno, name, token);
4500 return -1;
4501 }
4502
4503 len = strlen(arg);
4504 if (!( sscanf(arg, "%u%n,%u%n,%u%n", &v1, &n1, &v2, &n2, &v3, &n3) >= 1
4505 && (n1 == len || n2 == len || n3 == len) && v1 <= 255 && v2 <= 255 && v3 <= 255)) {
4506 PrintOut(LOG_CRIT,"File %s line %d (drive %s): Directive: %s has argument: %s; needs 1-3 integer(s) from 0 to 255.\n",
4507 cfgfile, lineno, name, token, arg);
4508 return -1;
4509 }
4510 *val1 = (unsigned char)v1; *val2 = (unsigned char)v2; *val3 = (unsigned char)v3;
4511 return 0;
4512}
4513
4514
4515#ifdef _WIN32
4516
4517// Concatenate strtok() results if quoted with "..."
4518static const char * strtok_dequote(const char * delimiters)
4519{
4520 const char * t = strtok(nullptr, delimiters);
4521 if (!t || t[0] != '"')
4522 return t;
4523
4524 static std::string token;
4525 token = t+1;
4526 for (;;) {
4527 t = strtok(nullptr, delimiters);
4528 if (!t || !*t)
4529 return "\"";
4530 token += ' ';
4531 int len = strlen(t);
4532 if (t[len-1] == '"') {
4533 token += std::string(t, len-1);
4534 break;
4535 }
4536 token += t;
4537 }
4538 return token.c_str();
4539}
4540
4541#endif // _WIN32
4542
4543
4544// This function returns 1 if it has correctly parsed one token (and
4545// any arguments), else zero if no tokens remain. It returns -1 if an
4546// error was encountered.
4547static int ParseToken(char * token, dev_config & cfg, smart_devtype_list & scan_types)
4548{
4549 char sym;
4550 const char * name = cfg.name.c_str();
4551 int lineno=cfg.lineno;
4552 const char *delim = " \n\t";
4553 int badarg = 0;
4554 int missingarg = 0;
4555 const char *arg = 0;
4556
4557 // is the rest of the line a comment
4558 if (*token=='#')
4559 return 1;
4560
4561 // is the token not recognized?
4562 if (*token!='-' || strlen(token)!=2) {
4563 PrintOut(LOG_CRIT,"File %s line %d (drive %s): unknown Directive: %s\n",
4564 configfile, lineno, name, token);
4565 PrintOut(LOG_CRIT, "Run smartd -D to print a list of valid Directives.\n");
4566 return -1;
4567 }
4568
4569 // token we will be parsing:
4570 sym=token[1];
4571
4572 // parse the token and swallow its argument
4573 int val;
4574 char plus[] = "+", excl[] = "!";
4575
4576 switch (sym) {
4577 case 'C':
4578 // monitor current pending sector count (default 197)
4579 if ((val = GetInteger((arg = strtok(nullptr, delim)), name, token, lineno, configfile, 0, 255, plus)) < 0)
4580 return -1;
4581 cfg.curr_pending_id = (unsigned char)val;
4582 cfg.curr_pending_incr = (*plus == '+');
4583 cfg.curr_pending_set = true;
4584 break;
4585 case 'U':
4586 // monitor offline uncorrectable sectors (default 198)
4587 if ((val = GetInteger((arg = strtok(nullptr, delim)), name, token, lineno, configfile, 0, 255, plus)) < 0)
4588 return -1;
4589 cfg.offl_pending_id = (unsigned char)val;
4590 cfg.offl_pending_incr = (*plus == '+');
4591 cfg.offl_pending_set = true;
4592 break;
4593 case 'T':
4594 // Set tolerance level for SMART command failures
4595 if (!(arg = strtok(nullptr, delim))) {
4596 missingarg = 1;
4597 } else if (!strcmp(arg, "normal")) {
4598 // Normal mode: exit on failure of a mandatory S.M.A.R.T. command, but
4599 // not on failure of an optional S.M.A.R.T. command.
4600 // This is the default so we don't need to actually do anything here.
4601 cfg.permissive = false;
4602 } else if (!strcmp(arg, "permissive")) {
4603 // Permissive mode; ignore errors from Mandatory SMART commands
4604 cfg.permissive = true;
4605 } else {
4606 badarg = 1;
4607 }
4608 break;
4609 case 'd':
4610 // specify the device type
4611 if (!(arg = strtok(nullptr, delim))) {
4612 missingarg = 1;
4613 } else if (!strcmp(arg, "ignore")) {
4614 cfg.ignore = true;
4615 } else if (!strcmp(arg, "removable")) {
4616 cfg.removable = true;
4617 } else if (!strcmp(arg, "auto")) {
4618 cfg.dev_type = "";
4619 scan_types.clear();
4620 } else {
4621 cfg.dev_type = arg;
4622 scan_types.push_back(arg);
4623 }
4624 break;
4625 case 'F':
4626 // fix firmware bug
4627 if (!(arg = strtok(nullptr, delim)))
4628 missingarg = 1;
4629 else if (!parse_firmwarebug_def(arg, cfg.firmwarebugs))
4630 badarg = 1;
4631 break;
4632 case 'H':
4633 // check SMART status
4634 cfg.smartcheck = true;
4635 break;
4636 case 'f':
4637 // check for failure of usage attributes
4638 cfg.usagefailed = true;
4639 break;
4640 case 't':
4641 // track changes in all vendor attributes
4642 cfg.prefail = true;
4643 cfg.usage = true;
4644 break;
4645 case 'p':
4646 // track changes in prefail vendor attributes
4647 cfg.prefail = true;
4648 break;
4649 case 'u':
4650 // track changes in usage vendor attributes
4651 cfg.usage = true;
4652 break;
4653 case 'l':
4654 // track changes in SMART logs
4655 if (!(arg = strtok(nullptr, delim))) {
4656 missingarg = 1;
4657 } else if (!strcmp(arg, "selftest")) {
4658 // track changes in self-test log
4659 cfg.selftest = true;
4660 } else if (!strcmp(arg, "error")) {
4661 // track changes in ATA error log
4662 cfg.errorlog = true;
4663 } else if (!strcmp(arg, "xerror")) {
4664 // track changes in Extended Comprehensive SMART error log
4665 cfg.xerrorlog = true;
4666 } else if (!strcmp(arg, "offlinests")) {
4667 // track changes in offline data collection status
4668 cfg.offlinests = true;
4669 } else if (!strcmp(arg, "offlinests,ns")) {
4670 // track changes in offline data collection status, disable auto standby
4671 cfg.offlinests = cfg.offlinests_ns = true;
4672 } else if (!strcmp(arg, "selfteststs")) {
4673 // track changes in self-test execution status
4674 cfg.selfteststs = true;
4675 } else if (!strcmp(arg, "selfteststs,ns")) {
4676 // track changes in self-test execution status, disable auto standby
4677 cfg.selfteststs = cfg.selfteststs_ns = true;
4678 } else if (!strncmp(arg, "scterc,", sizeof("scterc,")-1)) {
4679 // set SCT Error Recovery Control
4680 unsigned rt = ~0, wt = ~0; int nc = -1;
4681 sscanf(arg,"scterc,%u,%u%n", &rt, &wt, &nc);
4682 if (nc == (int)strlen(arg) && rt <= 999 && wt <= 999) {
4683 cfg.sct_erc_set = true;
4684 cfg.sct_erc_readtime = rt;
4685 cfg.sct_erc_writetime = wt;
4686 }
4687 else
4688 badarg = 1;
4689 } else {
4690 badarg = 1;
4691 }
4692 break;
4693 case 'a':
4694 // monitor everything
4695 cfg.smartcheck = true;
4696 cfg.prefail = true;
4697 cfg.usagefailed = true;
4698 cfg.usage = true;
4699 cfg.selftest = true;
4700 cfg.errorlog = true;
4701 cfg.selfteststs = true;
4702 break;
4703 case 'o':
4704 // automatic offline testing enable/disable
4705 if (!(arg = strtok(nullptr, delim))) {
4706 missingarg = 1;
4707 } else if (!strcmp(arg, "on")) {
4708 cfg.autoofflinetest = 2;
4709 } else if (!strcmp(arg, "off")) {
4710 cfg.autoofflinetest = 1;
4711 } else {
4712 badarg = 1;
4713 }
4714 break;
4715 case 'n':
4716 // skip disk check if in idle or standby mode
4717 if (!(arg = strtok(nullptr, delim)))
4718 missingarg = 1;
4719 else {
4720 char *endptr = nullptr;
4721 char *next = strchr(const_cast<char*>(arg), ',');
4722
4723 cfg.powerquiet = false;
4724 cfg.powerskipmax = 0;
4725
4726 if (next)
4727 *next = '\0';
4728 if (!strcmp(arg, "never"))
4729 cfg.powermode = 0;
4730 else if (!strcmp(arg, "sleep"))
4731 cfg.powermode = 1;
4732 else if (!strcmp(arg, "standby"))
4733 cfg.powermode = 2;
4734 else if (!strcmp(arg, "idle"))
4735 cfg.powermode = 3;
4736 else
4737 badarg = 1;
4738
4739 // if optional arguments are present
4740 if (!badarg && next) {
4741 next++;
4742 cfg.powerskipmax = strtol(next, &endptr, 10);
4743 if (endptr == next)
4744 cfg.powerskipmax = 0;
4745 else {
4746 next = endptr + (*endptr != '\0');
4747 if (cfg.powerskipmax <= 0)
4748 badarg = 1;
4749 }
4750 if (*next != '\0') {
4751 if (!strcmp("q", next))
4752 cfg.powerquiet = true;
4753 else {
4754 badarg = 1;
4755 }
4756 }
4757 }
4758 }
4759 break;
4760 case 'S':
4761 // automatic attribute autosave enable/disable
4762 if (!(arg = strtok(nullptr, delim))) {
4763 missingarg = 1;
4764 } else if (!strcmp(arg, "on")) {
4765 cfg.autosave = 2;
4766 } else if (!strcmp(arg, "off")) {
4767 cfg.autosave = 1;
4768 } else {
4769 badarg = 1;
4770 }
4771 break;
4772 case 's':
4773 // warn user, and delete any previously given -s REGEXP Directives
4774 if (!cfg.test_regex.empty()){
4775 PrintOut(LOG_INFO, "File %s line %d (drive %s): ignoring previous Test Directive -s %s\n",
4776 configfile, lineno, name, cfg.test_regex.get_pattern());
4778 }
4779 // check for missing argument
4780 if (!(arg = strtok(nullptr, delim))) {
4781 missingarg = 1;
4782 }
4783 // Compile regex
4784 else {
4785 if (!cfg.test_regex.compile(arg)) {
4786 // not a valid regular expression!
4787 PrintOut(LOG_CRIT, "File %s line %d (drive %s): -s argument \"%s\" is INVALID extended regular expression. %s.\n",
4788 configfile, lineno, name, arg, cfg.test_regex.get_errmsg());
4789 return -1;
4790 }
4791 // Do a bit of sanity checking and warn user if we think that
4792 // their regexp is "strange". User probably confused about shell
4793 // glob(3) syntax versus regular expression syntax regexp(7).
4794 // Check also for possible invalid number of digits in ':NNN[-LLL]' suffix.
4795 static const regular_expression syntax_check(
4796 "[^]$()*+./:?^[|0-9LSCOncr-]+|"
4797 ":[0-9]{0,2}($|[^0-9])|:[0-9]{4,}|"
4798 ":[0-9]{3}-(000|[0-9]{0,2}($|[^0-9])|[0-9]{4,})"
4799 );
4801 if (syntax_check.execute(arg, 1, &range) && 0 <= range.rm_so && range.rm_so < range.rm_eo)
4802 PrintOut(LOG_INFO, "File %s line %d (drive %s): warning, \"%.*s\" looks odd in "
4803 "extended regular expression \"%s\"\n",
4804 configfile, lineno, name, (int)(range.rm_eo - range.rm_so), arg + range.rm_so, arg);
4805 }
4806 break;
4807 case 'm':
4808 // send email to address that follows
4809 if (!(arg = strtok(nullptr, delim)))
4810 missingarg = 1;
4811 else {
4812 if (!cfg.emailaddress.empty())
4813 PrintOut(LOG_INFO, "File %s line %d (drive %s): ignoring previous Address Directive -m %s\n",
4814 configfile, lineno, name, cfg.emailaddress.c_str());
4815 cfg.emailaddress = arg;
4816 }
4817 break;
4818 case 'M':
4819 // email warning options
4820 if (!(arg = strtok(nullptr, delim)))
4821 missingarg = 1;
4822 else if (!strcmp(arg, "once"))
4824 else if (!strcmp(arg, "always"))
4826 else if (!strcmp(arg, "daily"))
4828 else if (!strcmp(arg, "diminishing"))
4830 else if (!strcmp(arg, "test"))
4831 cfg.emailtest = true;
4832 else if (!strcmp(arg, "exec")) {
4833 // Get the next argument (the command line)
4834#ifdef _WIN32
4835 // Allow "/path name/with spaces/..." on Windows
4836 arg = strtok_dequote(delim);
4837 if (arg && arg[0] == '"') {
4838 PrintOut(LOG_CRIT, "File %s line %d (drive %s): Directive %s 'exec' argument: missing closing quote\n",
4839 configfile, lineno, name, token);
4840 return -1;
4841 }
4842#else
4843 arg = strtok(nullptr, delim);
4844#endif
4845 if (!arg) {
4846 PrintOut(LOG_CRIT, "File %s line %d (drive %s): Directive %s 'exec' argument must be followed by executable path.\n",
4847 configfile, lineno, name, token);
4848 return -1;
4849 }
4850 // Free the last cmd line given if any, and copy new one
4851 if (!cfg.emailcmdline.empty())
4852 PrintOut(LOG_INFO, "File %s line %d (drive %s): ignoring previous mail Directive -M exec %s\n",
4853 configfile, lineno, name, cfg.emailcmdline.c_str());
4854 cfg.emailcmdline = arg;
4855 }
4856 else
4857 badarg = 1;
4858 break;
4859 case 'i':
4860 // ignore failure of usage attribute
4861 if ((val = GetInteger((arg = strtok(nullptr, delim)), name, token, lineno, configfile, 1, 255)) < 0)
4862 return -1;
4864 break;
4865 case 'I':
4866 // ignore attribute for tracking purposes
4867 if ((val = GetInteger((arg = strtok(nullptr, delim)), name, token, lineno, configfile, 1, 255)) < 0)
4868 return -1;
4870 break;
4871 case 'r':
4872 // print raw value when tracking
4873 if ((val = GetInteger((arg = strtok(nullptr, delim)), name, token, lineno, configfile, 1, 255, excl)) < 0)
4874 return -1;
4876 if (*excl == '!') // attribute change is critical
4878 break;
4879 case 'R':
4880 // track changes in raw value (forces printing of raw value)
4881 if ((val = GetInteger((arg = strtok(nullptr, delim)), name, token, lineno, configfile, 1, 255, excl)) < 0)
4882 return -1;
4884 if (*excl == '!') // raw value change is critical
4886 break;
4887 case 'W':
4888 // track Temperature
4889 if (Get3Integers((arg = strtok(nullptr, delim)), name, token, lineno, configfile,
4890 &cfg.tempdiff, &cfg.tempinfo, &cfg.tempcrit) < 0)
4891 return -1;
4892 break;
4893 case 'v':
4894 // non-default vendor-specific attribute meaning
4895 if (!(arg = strtok(nullptr, delim))) {
4896 missingarg = 1;
4897 } else if (!parse_attribute_def(arg, cfg.attribute_defs, PRIOR_USER)) {
4898 badarg = 1;
4899 }
4900 break;
4901 case 'P':
4902 // Define use of drive-specific presets.
4903 if (!(arg = strtok(nullptr, delim))) {
4904 missingarg = 1;
4905 } else if (!strcmp(arg, "use")) {
4906 cfg.ignorepresets = false;
4907 } else if (!strcmp(arg, "ignore")) {
4908 cfg.ignorepresets = true;
4909 } else if (!strcmp(arg, "show")) {
4910 cfg.showpresets = true;
4911 } else if (!strcmp(arg, "showall")) {
4913 } else {
4914 badarg = 1;
4915 }
4916 break;
4917
4918 case 'e':
4919 // Various ATA settings
4920 if (!(arg = strtok(nullptr, delim))) {
4921 missingarg = true;
4922 }
4923 else {
4924 char arg2[16+1]; unsigned uval;
4925 int n1 = -1, n2 = -1, n3 = -1, len = strlen(arg);
4926 if (sscanf(arg, "%16[^,=]%n%*[,=]%n%u%n", arg2, &n1, &n2, &uval, &n3) >= 1
4927 && (n1 == len || n2 > 0)) {
4928 bool on = (n2 > 0 && !strcmp(arg+n2, "on"));
4929 bool off = (n2 > 0 && !strcmp(arg+n2, "off"));
4930 if (n3 != len)
4931 uval = ~0U;
4932
4933 if (!strcmp(arg2, "aam")) {
4934 if (off)
4935 cfg.set_aam = -1;
4936 else if (uval <= 254)
4937 cfg.set_aam = uval + 1;
4938 else
4939 badarg = true;
4940 }
4941 else if (!strcmp(arg2, "apm")) {
4942 if (off)
4943 cfg.set_apm = -1;
4944 else if (1 <= uval && uval <= 254)
4945 cfg.set_apm = uval + 1;
4946 else
4947 badarg = true;
4948 }
4949 else if (!strcmp(arg2, "lookahead")) {
4950 if (off)
4951 cfg.set_lookahead = -1;
4952 else if (on)
4953 cfg.set_lookahead = 1;
4954 else
4955 badarg = true;
4956 }
4957 else if (!strcmp(arg, "security-freeze")) {
4958 cfg.set_security_freeze = true;
4959 }
4960 else if (!strcmp(arg2, "standby")) {
4961 if (off)
4962 cfg.set_standby = 0 + 1;
4963 else if (uval <= 255)
4964 cfg.set_standby = uval + 1;
4965 else
4966 badarg = true;
4967 }
4968 else if (!strcmp(arg2, "wcache")) {
4969 if (off)
4970 cfg.set_wcache = -1;
4971 else if (on)
4972 cfg.set_wcache = 1;
4973 else
4974 badarg = true;
4975 }
4976 else if (!strcmp(arg2, "dsn")) {
4977 if (off)
4978 cfg.set_dsn = -1;
4979 else if (on)
4980 cfg.set_dsn = 1;
4981 else
4982 badarg = true;
4983 }
4984 else
4985 badarg = true;
4986 }
4987 else
4988 badarg = true;
4989 }
4990 break;
4991
4992 case 'c':
4993 // Override command line options
4994 {
4995 if (!(arg = strtok(nullptr, delim))) {
4996 missingarg = true;
4997 break;
4998 }
4999 int n = 0, nc = -1, len = strlen(arg);
5000 if ( ( sscanf(arg, "i=%d%n", &n, &nc) == 1
5001 || sscanf(arg, "interval=%d%n", &n, &nc) == 1)
5002 && nc == len && n >= 10)
5003 cfg.checktime = n;
5004 else
5005 badarg = true;
5006 }
5007 break;
5008
5009 default:
5010 // Directive not recognized
5011 PrintOut(LOG_CRIT,"File %s line %d (drive %s): unknown Directive: %s\n",
5012 configfile, lineno, name, token);
5013 PrintOut(LOG_CRIT, "Run smartd -D to print a list of valid Directives.\n");
5014 return -1;
5015 }
5016 if (missingarg) {
5017 PrintOut(LOG_CRIT, "File %s line %d (drive %s): Missing argument to %s Directive\n",
5018 configfile, lineno, name, token);
5019 }
5020 if (badarg) {
5021 PrintOut(LOG_CRIT, "File %s line %d (drive %s): Invalid argument to %s Directive: %s\n",
5022 configfile, lineno, name, token, arg);
5023 }
5024 if (missingarg || badarg) {
5025 PrintOut(LOG_CRIT, "Valid arguments to %s Directive are: ", token);
5026 printoutvaliddirectiveargs(LOG_CRIT, sym);
5027 PrintOut(LOG_CRIT, "\n");
5028 return -1;
5029 }
5030
5031 return 1;
5032}
5033
5034// Scan directive for configuration file
5035#define SCANDIRECTIVE "DEVICESCAN"
5036
5037// This is the routine that adds things to the conf_entries list.
5038//
5039// Return values are:
5040// 1: parsed a normal line
5041// 0: found DEFAULT setting or comment or blank line
5042// -1: found SCANDIRECTIVE line
5043// -2: found an error
5044//
5045// Note: this routine modifies *line from the caller!
5046static int ParseConfigLine(dev_config_vector & conf_entries, dev_config & default_conf,
5047 smart_devtype_list & scan_types, int lineno, /*const*/ char * line)
5048{
5049 const char *delim = " \n\t";
5050
5051 // get first token: device name. If a comment, skip line
5052 const char * name = strtok(line, delim);
5053 if (!name || *name == '#')
5054 return 0;
5055
5056 // Check device name for DEFAULT or DEVICESCAN
5057 int retval;
5058 if (!strcmp("DEFAULT", name)) {
5059 retval = 0;
5060 // Restart with empty defaults
5061 default_conf = dev_config();
5062 }
5063 else {
5064 retval = (!strcmp(SCANDIRECTIVE, name) ? -1 : 1);
5065 // Init new entry with current defaults
5066 conf_entries.push_back(default_conf);
5067 }
5068 dev_config & cfg = (retval ? conf_entries.back() : default_conf);
5069
5070 cfg.name = name; // Later replaced by dev->get_info().info_name
5071 cfg.dev_name = name; // If DEVICESCAN later replaced by get->dev_info().dev_name
5072 cfg.lineno = lineno;
5073
5074 // parse tokens one at a time from the file.
5075 while (char * token = strtok(nullptr, delim)) {
5076 int rc = ParseToken(token, cfg, scan_types);
5077 if (rc < 0)
5078 // error found on the line
5079 return -2;
5080
5081 if (rc == 0)
5082 // No tokens left
5083 break;
5084
5085 // PrintOut(LOG_INFO,"Parsed token %s\n",token);
5086 }
5087
5088 // Check for multiple -d TYPE directives
5089 if (retval != -1 && scan_types.size() > 1) {
5090 PrintOut(LOG_CRIT, "Drive: %s, invalid multiple -d TYPE Directives on line %d of file %s\n",
5091 cfg.name.c_str(), cfg.lineno, configfile);
5092 return -2;
5093 }
5094
5095 // Don't perform checks below for DEFAULT entries
5096 if (retval == 0)
5097 return retval;
5098
5099 // If NO monitoring directives are set, then set all of them.
5100 if (!( cfg.smartcheck || cfg.selftest
5101 || cfg.errorlog || cfg.xerrorlog
5102 || cfg.offlinests || cfg.selfteststs
5103 || cfg.usagefailed || cfg.prefail || cfg.usage
5104 || cfg.tempdiff || cfg.tempinfo || cfg.tempcrit)) {
5105
5106 PrintOut(LOG_INFO,"Drive: %s, implied '-a' Directive on line %d of file %s\n",
5107 cfg.name.c_str(), cfg.lineno, configfile);
5108
5109 cfg.smartcheck = true;
5110 cfg.usagefailed = true;
5111 cfg.prefail = true;
5112 cfg.usage = true;
5113 cfg.selftest = true;
5114 cfg.errorlog = true;
5115 cfg.selfteststs = true;
5116 }
5117
5118 // additional sanity check. Has user set -M options without -m?
5119 if ( cfg.emailaddress.empty()
5120 && (!cfg.emailcmdline.empty() || cfg.emailfreq != emailfreqs::unknown || cfg.emailtest)) {
5121 PrintOut(LOG_CRIT,"Drive: %s, -M Directive(s) on line %d of file %s need -m ADDRESS Directive\n",
5122 cfg.name.c_str(), cfg.lineno, configfile);
5123 return -2;
5124 }
5125
5126 // has the user has set <nomailer>?
5127 if (cfg.emailaddress == "<nomailer>") {
5128 // check that -M exec is also set
5129 if (cfg.emailcmdline.empty()){
5130 PrintOut(LOG_CRIT,"Drive: %s, -m <nomailer> Directive on line %d of file %s needs -M exec Directive\n",
5131 cfg.name.c_str(), cfg.lineno, configfile);
5132 return -2;
5133 }
5134 // From here on the sign of <nomailer> is cfg.emailaddress.empty() and !cfg.emailcmdline.empty()
5135 cfg.emailaddress.clear();
5136 }
5137
5138 return retval;
5139}
5140
5141// Parses a configuration file. Return values are:
5142// N=>0: found N entries
5143// -1: syntax error in config file
5144// -2: config file does not exist
5145// -3: config file exists but cannot be read
5146//
5147// In the case where the return value is 0, there are three
5148// possibilities:
5149// Empty configuration file ==> conf_entries.empty()
5150// No configuration file ==> conf_entries[0].lineno == 0
5151// SCANDIRECTIVE found ==> conf_entries.back().lineno != 0 (size >= 1)
5152static int ParseConfigFile(dev_config_vector & conf_entries, smart_devtype_list & scan_types)
5153{
5154 // maximum line length in configuration file
5155 const int MAXLINELEN = 256;
5156 // maximum length of a continued line in configuration file
5157 const int MAXCONTLINE = 1023;
5158
5159 stdio_file f;
5160 // Open config file, if it exists and is not <stdin>
5161 if (!(configfile == configfile_stdin)) { // pointer comparison ok here
5162 if (!f.open(configfile,"r") && (errno!=ENOENT || !configfile_alt.empty())) {
5163 // file exists but we can't read it or it should exist due to '-c' option
5164 int ret = (errno!=ENOENT ? -3 : -2);
5165 PrintOut(LOG_CRIT,"%s: Unable to open configuration file %s\n",
5166 strerror(errno),configfile);
5167 return ret;
5168 }
5169 }
5170 else // read from stdin ('-c -' option)
5171 f.open(stdin);
5172
5173 // Start with empty defaults
5174 dev_config default_conf;
5175
5176 // No configuration file found -- use fake one
5177 int entry = 0;
5178 if (!f) {
5179 char fakeconfig[] = SCANDIRECTIVE " -a"; // TODO: Remove this hack, build cfg_entry.
5180
5181 if (ParseConfigLine(conf_entries, default_conf, scan_types, 0, fakeconfig) != -1)
5182 throw std::logic_error("Internal error parsing " SCANDIRECTIVE);
5183 return 0;
5184 }
5185
5186#ifdef __CYGWIN__
5187 setmode(fileno(f), O_TEXT); // Allow files with \r\n
5188#endif
5189
5190 // configuration file exists
5191 PrintOut(LOG_INFO,"Opened configuration file %s\n",configfile);
5192
5193 // parse config file line by line
5194 int lineno = 1, cont = 0, contlineno = 0;
5195 char line[MAXLINELEN+2];
5196 char fullline[MAXCONTLINE+1];
5197
5198 for (;;) {
5199 int len=0,scandevice;
5200 char *lastslash;
5201 char *comment;
5202 char *code;
5203
5204 // make debugging simpler
5205 memset(line,0,sizeof(line));
5206
5207 // get a line
5208 code=fgets(line, MAXLINELEN+2, f);
5209
5210 // are we at the end of the file?
5211 if (!code){
5212 if (cont) {
5213 scandevice = ParseConfigLine(conf_entries, default_conf, scan_types, contlineno, fullline);
5214 // See if we found a SCANDIRECTIVE directive
5215 if (scandevice==-1)
5216 return 0;
5217 // did we find a syntax error
5218 if (scandevice==-2)
5219 return -1;
5220 // the final line is part of a continuation line
5221 entry+=scandevice;
5222 }
5223 break;
5224 }
5225
5226 // input file line number
5227 contlineno++;
5228
5229 // See if line is too long
5230 len=strlen(line);
5231 if (len>MAXLINELEN){
5232 const char *warn;
5233 if (line[len-1]=='\n')
5234 warn="(including newline!) ";
5235 else
5236 warn="";
5237 PrintOut(LOG_CRIT,"Error: line %d of file %s %sis more than MAXLINELEN=%d characters.\n",
5238 (int)contlineno,configfile,warn,(int)MAXLINELEN);
5239 return -1;
5240 }
5241
5242 // Ignore anything after comment symbol
5243 if ((comment=strchr(line,'#'))){
5244 *comment='\0';
5245 len=strlen(line);
5246 }
5247
5248 // is the total line (made of all continuation lines) too long?
5249 if (cont+len>MAXCONTLINE){
5250 PrintOut(LOG_CRIT,"Error: continued line %d (actual line %d) of file %s is more than MAXCONTLINE=%d characters.\n",
5251 lineno, (int)contlineno, configfile, (int)MAXCONTLINE);
5252 return -1;
5253 }
5254
5255 // copy string so far into fullline, and increment length
5256 snprintf(fullline+cont, sizeof(fullline)-cont, "%s" ,line);
5257 cont+=len;
5258
5259 // is this a continuation line. If so, replace \ by space and look at next line
5260 if ( (lastslash=strrchr(line,'\\')) && !strtok(lastslash+1," \n\t")){
5261 *(fullline+(cont-len)+(lastslash-line))=' ';
5262 continue;
5263 }
5264
5265 // Not a continuation line. Parse it
5266 scan_types.clear();
5267 scandevice = ParseConfigLine(conf_entries, default_conf, scan_types, contlineno, fullline);
5268
5269 // did we find a scandevice directive?
5270 if (scandevice==-1)
5271 return 0;
5272 // did we find a syntax error
5273 if (scandevice==-2)
5274 return -1;
5275
5276 entry+=scandevice;
5277 lineno++;
5278 cont=0;
5279 }
5280
5281 // note -- may be zero if syntax of file OK, but no valid entries!
5282 return entry;
5283}
5284
5285/* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> <=======\n", where
5286 <LIST> is the list of valid arguments for option opt. */
5287static void PrintValidArgs(char opt)
5288{
5289 const char *s;
5290
5291 PrintOut(LOG_CRIT, "=======> VALID ARGUMENTS ARE: ");
5292 if (!(s = GetValidArgList(opt)))
5293 PrintOut(LOG_CRIT, "Error constructing argument list for option %c", opt);
5294 else
5295 PrintOut(LOG_CRIT, "%s", (char *)s);
5296 PrintOut(LOG_CRIT, " <=======\n");
5297}
5298
5299#ifndef _WIN32
5300// Report error and return false if specified path is not absolute.
5301static bool check_abs_path(char option, const std::string & path)
5302{
5303 if (path.empty() || path[0] == '/')
5304 return true;
5305
5306 debugmode = 1;
5307 PrintHead();
5308 PrintOut(LOG_CRIT, "=======> INVALID ARGUMENT TO -%c: %s <=======\n\n", option, path.c_str());
5309 PrintOut(LOG_CRIT, "Error: relative path names are not allowed\n\n");
5310 return false;
5311}
5312#endif // !_WIN32
5313
5314// Parses input line, prints usage message and
5315// version/license/copyright messages
5316static int parse_options(int argc, char **argv)
5317{
5318 // Init default path names
5319#ifndef _WIN32
5320 configfile = SMARTMONTOOLS_SYSCONFDIR "/smartd.conf";
5321 warning_script = SMARTMONTOOLS_SMARTDSCRIPTDIR "/smartd_warning.sh";
5322#else
5323 std::string exedir = get_exe_dir();
5324 static std::string configfile_str = exedir + "/smartd.conf";
5325 configfile = configfile_str.c_str();
5326 warning_script = exedir + "/smartd_warning.cmd";
5327#endif
5328
5329 // Please update GetValidArgList() if you edit shortopts
5330 static const char shortopts[] = "c:l:q:dDni:p:r:s:A:B:w:Vh?"
5331#if defined(HAVE_POSIX_API) || defined(_WIN32)
5332 "u:"
5333#endif
5334#ifdef HAVE_LIBCAP_NG
5335 "C"
5336#endif
5337 ;
5338 // Please update GetValidArgList() if you edit longopts
5339 struct option longopts[] = {
5340 { "configfile", required_argument, 0, 'c' },
5341 { "logfacility", required_argument, 0, 'l' },
5342 { "quit", required_argument, 0, 'q' },
5343 { "debug", no_argument, 0, 'd' },
5344 { "showdirectives", no_argument, 0, 'D' },
5345 { "interval", required_argument, 0, 'i' },
5346#ifndef _WIN32
5347 { "no-fork", no_argument, 0, 'n' },
5348#else
5349 { "service", no_argument, 0, 'n' },
5350#endif
5351 { "pidfile", required_argument, 0, 'p' },
5352 { "report", required_argument, 0, 'r' },
5353 { "savestates", required_argument, 0, 's' },
5354 { "attributelog", required_argument, 0, 'A' },
5355 { "drivedb", required_argument, 0, 'B' },
5356 { "warnexec", required_argument, 0, 'w' },
5357 { "version", no_argument, 0, 'V' },
5358 { "license", no_argument, 0, 'V' },
5359 { "copyright", no_argument, 0, 'V' },
5360 { "help", no_argument, 0, 'h' },
5361 { "usage", no_argument, 0, 'h' },
5362#if defined(HAVE_POSIX_API) || defined(_WIN32)
5363 { "warn-as-user", required_argument, 0, 'u' },
5364#endif
5365#ifdef HAVE_LIBCAP_NG
5366 { "capabilities", optional_argument, 0, 'C' },
5367#endif
5368 { 0, 0, 0, 0 }
5369 };
5370
5371 opterr=optopt=0;
5372 bool badarg = false;
5373 const char * badarg_msg = nullptr;
5374 bool use_default_db = true; // set false on '-B FILE'
5375
5376 // Parse input options.
5377 int optchar;
5378 while ((optchar = getopt_long(argc, argv, shortopts, longopts, nullptr)) != -1) {
5379 char *arg;
5380 char *tailptr;
5381 long lchecktime;
5382
5383 switch(optchar) {
5384 case 'q':
5385 // when to quit
5386 quit_nodev0 = false;
5387 if (!strcmp(optarg, "nodev"))
5388 quit = QUIT_NODEV;
5389 else if (!strcmp(optarg, "nodev0")) {
5390 quit = QUIT_NODEV;
5391 quit_nodev0 = true;
5392 }
5393 else if (!strcmp(optarg, "nodevstartup"))
5395 else if (!strcmp(optarg, "nodev0startup")) {
5397 quit_nodev0 = true;
5398 }
5399 else if (!strcmp(optarg, "errors"))
5400 quit = QUIT_ERRORS;
5401 else if (!strcmp(optarg, "errors,nodev0")) {
5402 quit = QUIT_ERRORS;
5403 quit_nodev0 = true;
5404 }
5405 else if (!strcmp(optarg, "never"))
5406 quit = QUIT_NEVER;
5407 else if (!strcmp(optarg, "onecheck")) {
5409 debugmode = 1;
5410 }
5411 else if (!strcmp(optarg, "showtests")) {
5413 debugmode = 1;
5414 }
5415 else
5416 badarg = true;
5417 break;
5418 case 'l':
5419 // set the log facility level
5420 if (!strcmp(optarg, "daemon"))
5421 facility=LOG_DAEMON;
5422 else if (!strcmp(optarg, "local0"))
5423 facility=LOG_LOCAL0;
5424 else if (!strcmp(optarg, "local1"))
5425 facility=LOG_LOCAL1;
5426 else if (!strcmp(optarg, "local2"))
5427 facility=LOG_LOCAL2;
5428 else if (!strcmp(optarg, "local3"))
5429 facility=LOG_LOCAL3;
5430 else if (!strcmp(optarg, "local4"))
5431 facility=LOG_LOCAL4;
5432 else if (!strcmp(optarg, "local5"))
5433 facility=LOG_LOCAL5;
5434 else if (!strcmp(optarg, "local6"))
5435 facility=LOG_LOCAL6;
5436 else if (!strcmp(optarg, "local7"))
5437 facility=LOG_LOCAL7;
5438 else
5439 badarg = true;
5440 break;
5441 case 'd':
5442 // enable debug mode
5443 debugmode = 1;
5444 break;
5445 case 'n':
5446 // don't fork()
5447#ifndef _WIN32 // On Windows, --service is already handled by daemon_main()
5448 do_fork = false;
5449#endif
5450 break;
5451 case 'D':
5452 // print summary of all valid directives
5453 debugmode = 1;
5454 Directives();
5455 return 0;
5456 case 'i':
5457 // Period (time interval) for checking
5458 // strtol will set errno in the event of overflow, so we'll check it.
5459 errno = 0;
5460 lchecktime = strtol(optarg, &tailptr, 10);
5461 if (*tailptr != '\0' || lchecktime < 10 || lchecktime > INT_MAX || errno) {
5462 debugmode=1;
5463 PrintHead();
5464 PrintOut(LOG_CRIT, "======> INVALID INTERVAL: %s <=======\n", optarg);
5465 PrintOut(LOG_CRIT, "======> INTERVAL MUST BE INTEGER BETWEEN %d AND %d <=======\n", 10, INT_MAX);
5466 PrintOut(LOG_CRIT, "\nUse smartd -h to get a usage summary\n\n");
5467 return EXIT_BADCMD;
5468 }
5469 checktime = (int)lchecktime;
5470 break;
5471 case 'r':
5472 // report IOCTL transactions
5473 {
5474 int n1 = -1, n2 = -1, len = strlen(optarg);
5475 char s[9+1]; unsigned i = 1;
5476 sscanf(optarg, "%9[a-z]%n,%u%n", s, &n1, &i, &n2);
5477 if (!((n1 == len || n2 == len) && 1 <= i && i <= 4)) {
5478 badarg = true;
5479 } else if (!strcmp(s,"ioctl")) {
5481 } else if (!strcmp(s,"ataioctl")) {
5482 ata_debugmode = i;
5483 } else if (!strcmp(s,"scsiioctl")) {
5484 scsi_debugmode = i;
5485 } else if (!strcmp(s,"nvmeioctl")) {
5486 nvme_debugmode = i;
5487 } else {
5488 badarg = true;
5489 }
5490 }
5491 break;
5492 case 'c':
5493 // alternate configuration file
5494 if (strcmp(optarg,"-"))
5495 configfile = (configfile_alt = optarg).c_str();
5496 else // read from stdin
5498 break;
5499 case 'p':
5500 // output file with PID number
5501 pid_file = optarg;
5502 break;
5503 case 's':
5504 // path prefix of persistent state file
5505 state_path_prefix = (strcmp(optarg, "-") ? optarg : "");
5506 break;
5507 case 'A':
5508 // path prefix of attribute log file
5509 attrlog_path_prefix = (strcmp(optarg, "-") ? optarg : "");
5510 break;
5511 case 'B':
5512 {
5513 const char * path = optarg;
5514 if (*path == '+' && path[1])
5515 path++;
5516 else
5517 use_default_db = false;
5518 unsigned char savedebug = debugmode; debugmode = 1;
5519 if (!read_drive_database(path))
5520 return EXIT_BADCMD;
5521 debugmode = savedebug;
5522 }
5523 break;
5524 case 'w':
5525 warning_script = optarg;
5526 break;
5527#ifdef HAVE_POSIX_API
5528 case 'u':
5529 warn_as_user = false;
5530 if (strcmp(optarg, "-")) {
5531 warn_uname = warn_gname = "unknown";
5532 badarg_msg = parse_ugid(optarg, warn_uid, warn_gid,
5533 warn_uname, warn_gname );
5534 if (badarg_msg)
5535 break;
5536 warn_as_user = true;
5537 }
5538 break;
5539#elif defined(_WIN32)
5540 case 'u':
5541 if (!strcmp(optarg, "restricted"))
5542 warn_as_restr_user = true;
5543 else if (!strcmp(optarg, "unchanged"))
5544 warn_as_restr_user = false;
5545 else
5546 badarg = true;
5547 break;
5548#endif // HAVE_POSIX_API ||_WIN32
5549 case 'V':
5550 // print version and CVS info
5551 debugmode = 1;
5552 PrintOut(LOG_INFO, "%s", format_version_info("smartd", 3 /*full*/).c_str());
5553 return 0;
5554#ifdef HAVE_LIBCAP_NG
5555 case 'C':
5556 // enable capabilities
5557 if (!optarg)
5558 capabilities_mode = 1;
5559 else if (!strcmp(optarg, "mail"))
5560 capabilities_mode = 2;
5561 else
5562 badarg = true;
5563 break;
5564#endif
5565 case 'h':
5566 // help: print summary of command-line options
5567 debugmode=1;
5568 PrintHead();
5569 Usage();
5570 return 0;
5571 case '?':
5572 default:
5573 // unrecognized option
5574 debugmode=1;
5575 PrintHead();
5576 // Point arg to the argument in which this option was found.
5577 // Note: getopt_long() may set optind > argc (e.g. musl libc)
5578 arg = argv[optind <= argc ? optind - 1 : argc - 1];
5579 // Check whether the option is a long option that doesn't map to -h.
5580 if (arg[1] == '-' && optchar != 'h') {
5581 // Iff optopt holds a valid option then argument must be missing.
5582 if (optopt && strchr(shortopts, optopt)) {
5583 PrintOut(LOG_CRIT, "=======> ARGUMENT REQUIRED FOR OPTION: %s <=======\n",arg+2);
5584 PrintValidArgs(optopt);
5585 } else {
5586 PrintOut(LOG_CRIT, "=======> UNRECOGNIZED OPTION: %s <=======\n\n",arg+2);
5587 }
5588 PrintOut(LOG_CRIT, "\nUse smartd --help to get a usage summary\n\n");
5589 return EXIT_BADCMD;
5590 }
5591 if (optopt) {
5592 // Iff optopt holds a valid option then argument must be missing.
5593 if (strchr(shortopts, optopt)){
5594 PrintOut(LOG_CRIT, "=======> ARGUMENT REQUIRED FOR OPTION: %c <=======\n",optopt);
5595 PrintValidArgs(optopt);
5596 } else {
5597 PrintOut(LOG_CRIT, "=======> UNRECOGNIZED OPTION: %c <=======\n\n",optopt);
5598 }
5599 PrintOut(LOG_CRIT, "\nUse smartd -h to get a usage summary\n\n");
5600 return EXIT_BADCMD;
5601 }
5602 Usage();
5603 return 0;
5604 }
5605
5606 // Check to see if option had an unrecognized or incorrect argument.
5607 if (badarg || badarg_msg) {
5608 debugmode=1;
5609 PrintHead();
5610 // It would be nice to print the actual option name given by the user
5611 // here, but we just print the short form. Please fix this if you know
5612 // a clean way to do it.
5613 PrintOut(LOG_CRIT, "=======> INVALID ARGUMENT TO -%c: %s <======= \n", optchar, optarg);
5614 if (badarg_msg)
5615 PrintOut(LOG_CRIT, "%s\n", badarg_msg);
5616 else
5617 PrintValidArgs(optchar);
5618 PrintOut(LOG_CRIT, "\nUse smartd -h to get a usage summary\n\n");
5619 return EXIT_BADCMD;
5620 }
5621 }
5622
5623 // non-option arguments are not allowed
5624 if (argc > optind) {
5625 debugmode=1;
5626 PrintHead();
5627 PrintOut(LOG_CRIT, "=======> UNRECOGNIZED ARGUMENT: %s <=======\n\n", argv[optind]);
5628 PrintOut(LOG_CRIT, "\nUse smartd -h to get a usage summary\n\n");
5629 return EXIT_BADCMD;
5630 }
5631
5632 // no pidfile in debug mode
5633 if (debugmode && !pid_file.empty()) {
5634 debugmode=1;
5635 PrintHead();
5636 PrintOut(LOG_CRIT, "=======> INVALID CHOICE OF OPTIONS: -d and -p <======= \n\n");
5637 PrintOut(LOG_CRIT, "Error: pid file %s not written in debug (-d) mode\n\n", pid_file.c_str());
5638 return EXIT_BADCMD;
5639 }
5640
5641#ifndef _WIN32
5642 if (!debugmode) {
5643 // absolute path names are required due to chdir('/') in daemon_init()
5644 if (!( check_abs_path('p', pid_file)
5647 return EXIT_BADCMD;
5648 }
5649#endif
5650
5651#ifdef _WIN32
5652 if (warn_as_restr_user && !popen_as_restr_check()) {
5653 // debugmode=1 // would suppress messages to eventlog or log file
5654 PrintHead();
5655 PrintOut(LOG_CRIT, "Option '--warn-as-user=restricted' is not effective if the current user\n");
5656 PrintOut(LOG_CRIT, "is the local 'SYSTEM' or 'Administrator' account\n\n");
5657 return EXIT_BADCMD;
5658 }
5659#endif
5660
5661 // Read or init drive database
5662 {
5663 unsigned char savedebug = debugmode; debugmode = 1;
5664 if (!init_drive_database(use_default_db))
5665 return EXIT_BADCMD;
5666 debugmode = savedebug;
5667 }
5668
5669 // Check option compatibility of notify support
5670 // cppcheck-suppress knownConditionTrueFalse
5671 if (!notify_post_init())
5672 return EXIT_BADCMD;
5673
5674 // print header, don't write Copyright line to syslog
5675 PrintOut(LOG_INFO, "%s\n", format_version_info("smartd", (debugmode ? 2 : 1)).c_str());
5676
5677 // No error, continue in main_worker()
5678 return -1;
5679}
5680
5681// Function we call if no configuration file was found or if the
5682// SCANDIRECTIVE Directive was found. It makes entries for device
5683// names returned by scan_smart_devices() in os_OSNAME.cpp
5684static int MakeConfigEntries(const dev_config & base_cfg,
5685 dev_config_vector & conf_entries, smart_device_list & scanned_devs,
5686 const smart_devtype_list & types)
5687{
5688 // make list of devices
5689 smart_device_list devlist;
5690 if (!smi()->scan_smart_devices(devlist, types)) {
5691 PrintOut(LOG_CRIT, "DEVICESCAN failed: %s\n", smi()->get_errmsg());
5692 return 0;
5693 }
5694
5695 // if no devices, return
5696 if (devlist.size() == 0)
5697 return 0;
5698
5699 // add empty device slots for existing config entries
5700 while (scanned_devs.size() < conf_entries.size())
5701 scanned_devs.push_back((smart_device *)0);
5702
5703 // loop over entries to create
5704 for (unsigned i = 0; i < devlist.size(); i++) {
5705 // Move device pointer
5706 smart_device * dev = devlist.release(i);
5707 scanned_devs.push_back(dev);
5708
5709 // Append configuration and update names
5710 conf_entries.push_back(base_cfg);
5711 dev_config & cfg = conf_entries.back();
5712 cfg.name = dev->get_info().info_name;
5713 cfg.dev_name = dev->get_info().dev_name;
5714
5715 // Set type only if scanning is limited to specific types
5716 // This is later used to set SMARTD_DEVICETYPE environment variable
5717 if (!types.empty())
5718 cfg.dev_type = dev->get_info().dev_type;
5719 else // SMARTD_DEVICETYPE=auto
5720 cfg.dev_type.clear();
5721 }
5722
5723 return devlist.size();
5724}
5725
5726// Returns negative value (see ParseConfigFile()) if config file
5727// had errors, else number of entries which may be zero or positive.
5728static int ReadOrMakeConfigEntries(dev_config_vector & conf_entries, smart_device_list & scanned_devs)
5729{
5730 // parse configuration file configfile (normally /etc/smartd.conf)
5731 smart_devtype_list scan_types;
5732 int entries = ParseConfigFile(conf_entries, scan_types);
5733
5734 if (entries < 0) {
5735 // There was an error reading the configuration file.
5736 conf_entries.clear();
5737 if (entries == -1)
5738 PrintOut(LOG_CRIT, "Configuration file %s has fatal syntax errors.\n", configfile);
5739 return entries;
5740 }
5741
5742 // no error parsing config file.
5743 if (entries) {
5744 // we did not find a SCANDIRECTIVE and did find valid entries
5745 PrintOut(LOG_INFO, "Configuration file %s parsed.\n", configfile);
5746 }
5747 else if (!conf_entries.empty()) {
5748 // we found a SCANDIRECTIVE or there was no configuration file so
5749 // scan. Configuration file's last entry contains all options
5750 // that were set
5751 dev_config first = conf_entries.back();
5752 conf_entries.pop_back();
5753
5754 if (first.lineno)
5755 PrintOut(LOG_INFO,"Configuration file %s was parsed, found %s, scanning devices\n", configfile, SCANDIRECTIVE);
5756 else
5757 PrintOut(LOG_INFO,"No configuration file %s found, scanning devices\n", configfile);
5758
5759 // make config list of devices to search for
5760 MakeConfigEntries(first, conf_entries, scanned_devs, scan_types);
5761
5762 // warn user if scan table found no devices
5763 if (conf_entries.empty())
5764 PrintOut(LOG_CRIT,"In the system's table of devices NO devices found to scan\n");
5765 }
5766 else
5767 PrintOut(LOG_CRIT, "Configuration file %s parsed but has no entries\n", configfile);
5768
5769 return conf_entries.size();
5770}
5771
5772// Register one device, return false on error
5774 const dev_config_vector * prev_cfgs)
5775{
5776 bool scanning;
5777 if (!dev) {
5778 // Get device of appropriate type
5779 dev = smi()->get_smart_device(cfg.name.c_str(), cfg.dev_type.c_str());
5780 if (!dev) {
5781 if (cfg.dev_type.empty())
5782 PrintOut(LOG_INFO, "Device: %s, unable to autodetect device type\n", cfg.name.c_str());
5783 else
5784 PrintOut(LOG_INFO, "Device: %s, unsupported device type '%s'\n", cfg.name.c_str(), cfg.dev_type.c_str());
5785 return false;
5786 }
5787 scanning = false;
5788 }
5789 else {
5790 // Use device from device scan
5791 scanning = true;
5792 }
5793
5794 // Save old info
5795 smart_device::device_info oldinfo = dev->get_info();
5796
5797 // Open with autodetect support, may return 'better' device
5798 dev.replace( dev->autodetect_open() );
5799
5800 // Report if type has changed
5801 if (oldinfo.dev_type != dev->get_dev_type())
5802 PrintOut(LOG_INFO, "Device: %s, type changed from '%s' to '%s'\n",
5803 cfg.name.c_str(), oldinfo.dev_type.c_str(), dev->get_dev_type());
5804
5805 // Return if autodetect_open() failed
5806 if (!dev->is_open()) {
5807 if (debugmode || !scanning)
5808 PrintOut(LOG_INFO, "Device: %s, open() failed: %s\n", dev->get_info_name(), dev->get_errmsg());
5809 return false;
5810 }
5811
5812 // Update informal name
5813 cfg.name = dev->get_info().info_name;
5814 PrintOut(LOG_INFO, "Device: %s, opened\n", cfg.name.c_str());
5815
5816 int status;
5817 const char * typemsg;
5818 // register ATA device
5819 if (dev->is_ata()){
5820 typemsg = "ATA";
5821 status = ATADeviceScan(cfg, state, dev->to_ata(), prev_cfgs);
5822 }
5823 // or register SCSI device
5824 else if (dev->is_scsi()){
5825 typemsg = "SCSI";
5826 status = SCSIDeviceScan(cfg, state, dev->to_scsi(), prev_cfgs);
5827 }
5828 // or register NVMe device
5829 else if (dev->is_nvme()) {
5830 typemsg = "NVMe";
5831 status = NVMeDeviceScan(cfg, state, dev->to_nvme(), prev_cfgs);
5832 }
5833 else {
5834 PrintOut(LOG_INFO, "Device: %s, neither ATA, SCSI nor NVMe device\n", cfg.name.c_str());
5835 return false;
5836 }
5837
5838 if (status) {
5839 if (!scanning || debugmode) {
5840 if (cfg.lineno)
5841 PrintOut(scanning ? LOG_INFO : LOG_CRIT,
5842 "Unable to register %s device %s at line %d of file %s\n",
5843 typemsg, cfg.name.c_str(), cfg.lineno, configfile);
5844 else
5845 PrintOut(LOG_INFO, "Unable to register %s device %s\n",
5846 typemsg, cfg.name.c_str());
5847 }
5848
5849 return false;
5850 }
5851
5852 return true;
5853}
5854
5855// This function tries devices from conf_entries. Each one that can be
5856// registered is moved onto the [ata|scsi]devices lists and removed
5857// from the conf_entries list.
5858static bool register_devices(const dev_config_vector & conf_entries, smart_device_list & scanned_devs,
5860{
5861 // start by clearing lists/memory of ALL existing devices
5862 configs.clear();
5863 devices.clear();
5864 states.clear();
5865
5866 // Map of already seen non-DEVICESCAN devices (unique_name -> cfg.name)
5867 typedef std::map<std::string, std::string> prev_unique_names_map;
5868 prev_unique_names_map prev_unique_names;
5869
5870 // Register entries
5871 for (unsigned i = 0; i < conf_entries.size(); i++) {
5872 dev_config cfg = conf_entries[i];
5873
5874 // Get unique device "name [type]" (with symlinks resolved) for duplicate detection
5875 std::string unique_name = smi()->get_unique_dev_name(cfg.dev_name.c_str(), cfg.dev_type.c_str());
5876 if (debugmode && unique_name != cfg.dev_name) {
5877 pout("Device: %s%s%s%s, unique name: %s\n", cfg.name.c_str(),
5878 (!cfg.dev_type.empty() ? " [" : ""), cfg.dev_type.c_str(),
5879 (!cfg.dev_type.empty() ? "]" : ""), unique_name.c_str());
5880 }
5881
5882 if (cfg.ignore) {
5883 // Store for duplicate detection and ignore
5884 PrintOut(LOG_INFO, "Device: %s%s%s%s, ignored\n", cfg.name.c_str(),
5885 (!cfg.dev_type.empty() ? " [" : ""), cfg.dev_type.c_str(),
5886 (!cfg.dev_type.empty() ? "]" : ""));
5887 prev_unique_names[unique_name] = cfg.name;
5888 continue;
5889 }
5890
5892
5893 // Device may already be detected during devicescan
5894 bool scanning = false;
5895 if (i < scanned_devs.size()) {
5896 dev = scanned_devs.release(i);
5897 if (dev) {
5898 // Check for a preceding non-DEVICESCAN entry for the same device
5899 prev_unique_names_map::iterator ui = prev_unique_names.find(unique_name);
5900 if (ui != prev_unique_names.end()) {
5901 bool ne = (ui->second != cfg.name);
5902 PrintOut(LOG_INFO, "Device: %s, %s%s, ignored\n", dev->get_info_name(),
5903 (ne ? "same as " : "duplicate"), (ne ? ui->second.c_str() : ""));
5904 continue;
5905 }
5906 scanning = true;
5907 }
5908 }
5909
5910 // Prevent systemd unit startup timeout when registering many devices
5912
5913 // Register device
5914 // If scanning, pass dev_idinfo of previous devices for duplicate check
5915 dev_state state;
5916 if (!register_device(cfg, state, dev, (scanning ? &configs : 0))) {
5917 // if device is explicitly listed and we can't register it, then
5918 // exit unless the user has specified that the device is removable
5919 if (!scanning) {
5920 if (!(cfg.removable || quit == QUIT_NEVER)) {
5921 PrintOut(LOG_CRIT, "Unable to register device %s (no Directive -d removable). Exiting.\n",
5922 cfg.name.c_str());
5923 return false;
5924 }
5925 PrintOut(LOG_INFO, "Device: %s, not available\n", cfg.name.c_str());
5926 // Prevent retry of registration
5927 prev_unique_names[unique_name] = cfg.name;
5928 }
5929 continue;
5930 }
5931
5932 // move onto the list of devices
5933 configs.push_back(cfg);
5934 states.push_back(state);
5935 devices.push_back(dev);
5936 if (!scanning)
5937 // Store for duplicate detection
5938 prev_unique_names[unique_name] = cfg.name;
5939 }
5940
5941 // Set minimum check time and factors for staggered tests
5942 checktime_min = 0;
5943 unsigned factor = 0;
5944 for (auto & cfg : configs) {
5945 if (cfg.checktime && (!checktime_min || checktime_min > cfg.checktime))
5946 checktime_min = cfg.checktime;
5947 if (!cfg.test_regex.empty())
5948 cfg.test_offset_factor = factor++;
5949 }
5952
5954 return true;
5955}
5956
5957
5958// Main program without exception handling
5959static int main_worker(int argc, char **argv)
5960{
5961 // Initialize interface
5963 if (!smi())
5964 return 1;
5965
5966 // Check whether systemd notify is supported and enabled
5967 notify_init();
5968
5969 // parse input and print header and usage info if needed
5970 int status = parse_options(argc,argv);
5971 if (status >= 0)
5972 return status;
5973
5974 // Configuration for each device
5975 dev_config_vector configs;
5976 // Device states
5977 dev_state_vector states;
5978 // Devices to monitor
5980
5981 // Drop capabilities if supported and enabled
5983
5984 notify_msg("Initializing ...");
5985
5986 // the main loop of the code
5987 bool firstpass = true, write_states_always = true;
5988 time_t wakeuptime = 0;
5989 // assert(status < 0);
5990 do {
5991 // Should we (re)read the config file?
5992 if (firstpass || caughtsigHUP){
5993 if (!firstpass) {
5994 // Write state files
5995 if (!state_path_prefix.empty())
5996 write_all_dev_states(configs, states);
5997
5998 PrintOut(LOG_INFO,
5999 caughtsigHUP==1?
6000 "Signal HUP - rereading configuration file %s\n":
6001 "\a\nSignal INT - rereading configuration file %s (" SIGQUIT_KEYNAME " quits)\n\n",
6002 configfile);
6003 notify_msg("Reloading ...");
6004 }
6005
6006 {
6007 dev_config_vector conf_entries; // Entries read from smartd.conf
6008 smart_device_list scanned_devs; // Devices found during scan
6009 // (re)reads config file, makes >=0 entries
6010 int entries = ReadOrMakeConfigEntries(conf_entries, scanned_devs);
6011
6012 if (entries>=0) {
6013 // checks devices, then moves onto ata/scsi list or deallocates.
6014 if (!register_devices(conf_entries, scanned_devs, configs, states, devices)) {
6015 status = EXIT_BADDEV;
6016 break;
6017 }
6018 if (!(configs.size() == devices.size() && configs.size() == states.size()))
6019 throw std::logic_error("Invalid result from RegisterDevices");
6020 }
6021 else if ( quit == QUIT_NEVER
6022 || ((quit == QUIT_NODEV || quit == QUIT_NODEVSTARTUP) && !firstpass)) {
6023 // user has asked to continue on error in configuration file
6024 if (!firstpass)
6025 PrintOut(LOG_INFO,"Reusing previous configuration\n");
6026 }
6027 else {
6028 // exit with configuration file error status
6029 status = (entries == -3 ? EXIT_READCONF : entries == -2 ? EXIT_NOCONF : EXIT_BADCONF);
6030 break;
6031 }
6032 }
6033
6034 if (!( devices.size() > 0 || quit == QUIT_NEVER
6035 || (quit == QUIT_NODEVSTARTUP && !firstpass))) {
6036 status = (!quit_nodev0 ? EXIT_NODEV : 0);
6037 PrintOut((status ? LOG_CRIT : LOG_INFO),
6038 "Unable to monitor any SMART enabled devices. Exiting.\n");
6039 break;
6040 }
6041
6042 // Log number of devices we are monitoring...
6043 int numata = 0, numscsi = 0;
6044 for (unsigned i = 0; i < devices.size(); i++) {
6045 const smart_device * dev = devices.at(i);
6046 if (dev->is_ata())
6047 numata++;
6048 else if (dev->is_scsi())
6049 numscsi++;
6050 }
6051 PrintOut(LOG_INFO, "Monitoring %d ATA/SATA, %d SCSI/SAS and %d NVMe devices\n",
6052 numata, numscsi, (int)devices.size() - numata - numscsi);
6053
6054 if (quit == QUIT_SHOWTESTS) {
6055 // user has asked to print test schedule
6056 PrintTestSchedule(configs, states, devices);
6057 // assert(firstpass);
6058 return 0;
6059 }
6060
6061 // reset signal
6062 caughtsigHUP=0;
6063
6064 // Always write state files after (re)configuration
6065 write_states_always = true;
6066 }
6067
6068 // check all devices once,
6069 // self tests are not started in first pass unless '-q onecheck' is specified
6070 notify_check((int)devices.size());
6071 CheckDevicesOnce(configs, states, devices, firstpass, (!firstpass || quit == QUIT_ONECHECK));
6072
6073 // Write state files
6074 if (!state_path_prefix.empty())
6075 write_all_dev_states(configs, states, write_states_always);
6076 write_states_always = false;
6077
6078 // Write attribute logs
6079 if (!attrlog_path_prefix.empty())
6080 write_all_dev_attrlogs(configs, states);
6081
6082 // user has asked us to exit after first check
6083 if (quit == QUIT_ONECHECK) {
6084 PrintOut(LOG_INFO,"Started with '-q onecheck' option. All devices successfully checked once.\n"
6085 "smartd is exiting (exit status 0)\n");
6086 // assert(firstpass);
6087 return 0;
6088 }
6089
6090 if (firstpass) {
6091 if (!debugmode) {
6092 // fork() into background if needed, close ALL file descriptors,
6093 // redirect stdin, stdout, and stderr, chdir to "/".
6094 status = daemon_init();
6095 if (status >= 0)
6096 return status;
6097
6098 // Write PID file if configured
6099 if (!write_pid_file())
6100 return EXIT_PID;
6101 }
6102
6103 // Set exit and signal handlers
6105
6106 // Initialize wakeup time to CURRENT time
6107 wakeuptime = time(nullptr);
6108
6109 firstpass = false;
6110 }
6111
6112 // sleep until next check time, or a signal arrives
6113 wakeuptime = dosleep(wakeuptime, configs, states, write_states_always);
6114
6115 } while (!caughtsigEXIT);
6116
6117 if (caughtsigEXIT && status < 0) {
6118 // Loop exited on signal
6119 if (caughtsigEXIT == SIGTERM || (debugmode && caughtsigEXIT == SIGQUIT)) {
6120 PrintOut(LOG_INFO, "smartd received signal %d: %s\n",
6121 caughtsigEXIT, strsignal(caughtsigEXIT));
6122 }
6123 else {
6124 // Unexpected SIGINT or SIGQUIT
6125 PrintOut(LOG_CRIT, "smartd received unexpected signal %d: %s\n",
6126 caughtsigEXIT, strsignal(caughtsigEXIT));
6127 status = EXIT_SIGNAL;
6128 }
6129 }
6130
6131 // Status unset above implies success
6132 if (status < 0)
6133 status = 0;
6134
6135 if (!firstpass) {
6136 // Loop exited after daemon_init() and write_pid_file()
6137
6138 // Write state files only on normal exit
6139 if (!status && !state_path_prefix.empty())
6140 write_all_dev_states(configs, states);
6141
6142 // Delete PID file, if one was created
6143 if (!pid_file.empty() && unlink(pid_file.c_str()))
6144 PrintOut(LOG_CRIT,"Can't unlink PID file %s (%s).\n",
6145 pid_file.c_str(), strerror(errno));
6146 }
6147
6148 PrintOut((status ? LOG_CRIT : LOG_INFO), "smartd is exiting (exit status %d)\n", status);
6149 return status;
6150}
6151
6152
6153#ifndef _WIN32
6154// Main program
6155int main(int argc, char **argv)
6156#else
6157// Windows: internal main function started direct or by service control manager
6158static int smartd_main(int argc, char **argv)
6159#endif
6160{
6161 int status;
6162 try {
6163 // Do the real work ...
6164 status = main_worker(argc, argv);
6165 }
6166 catch (const std::bad_alloc & /*ex*/) {
6167 // Memory allocation failed (also thrown by std::operator new)
6168 PrintOut(LOG_CRIT, "Smartd: Out of memory\n");
6169 status = EXIT_NOMEM;
6170 }
6171 catch (const std::exception & ex) {
6172 // Other fatal errors
6173 PrintOut(LOG_CRIT, "Smartd: Exception: %s\n", ex.what());
6174 status = EXIT_BADCODE;
6175 }
6176
6177 // Check for remaining device objects
6178 if (smart_device::get_num_objects() != 0) {
6179 PrintOut(LOG_CRIT, "Smartd: Internal Error: %d device object(s) left at exit.\n",
6181 status = EXIT_BADCODE;
6182 }
6183
6184 if (status == EXIT_BADCODE)
6185 PrintOut(LOG_CRIT, "Please inform " PACKAGE_BUGREPORT ", including output of smartd -V.\n");
6186
6187 notify_exit(status);
6188#ifdef _WIN32
6189 daemon_winsvc_exitcode = status;
6190#endif
6191 return status;
6192}
6193
6194
6195#ifdef _WIN32
6196// Main function for Windows
6197int main(int argc, char **argv){
6198 // Options for smartd windows service
6199 static const daemon_winsvc_options svc_opts = {
6200 "--service", // cmd_opt
6201 "smartd", "SmartD Service", // servicename, displayname
6202 // description
6203 "Controls and monitors storage devices using the Self-Monitoring, "
6204 "Analysis and Reporting Technology System (SMART) built into "
6205 "ATA/SATA and SCSI/SAS hard drives and solid-state drives. "
6206 "www.smartmontools.org"
6207 };
6208 // daemon_main() handles daemon and service specific commands
6209 // and starts smartd_main() direct, from a new process,
6210 // or via service control manager
6211 return daemon_main("smartd", &svc_opts , smartd_main, argc, argv);
6212}
6213#endif
bool ata_nodata_command(ata_device *device, unsigned char command, int sector_count)
Definition: atacmds.cpp:787
int ataDisableAutoOffline(ata_device *device)
Definition: atacmds.cpp:1581
unsigned char ata_return_temperature_value(const ata_smart_values *data, const ata_vendor_attr_defs &defs)
Definition: atacmds.cpp:2159
bool isSmartErrorLogCapable(const ata_smart_values *data, const ata_identify_device *identity)
Definition: atacmds.cpp:1731
int ata_get_wwn(const ata_identify_device *id, unsigned &oui, uint64_t &unique_id)
Definition: atacmds.cpp:900
int ataEnableAutoOffline(ata_device *device)
Definition: atacmds.cpp:1570
int ataSmartStatus2(ata_device *device)
Definition: atacmds.cpp:1604
int ataSmartSupport(const ata_identify_device *drive)
Definition: atacmds.cpp:936
int ataDisableAutoSave(ata_device *device)
Definition: atacmds.cpp:1558
int ata_get_rotation_rate(const ata_identify_device *id)
Definition: atacmds.cpp:920
unsigned char get_unc_attr_id(bool offline, const ata_vendor_attr_defs &defs, bool &increase)
Definition: atacmds.cpp:53
bool parse_attribute_def(const char *opt, ata_vendor_attr_defs &defs, ata_vendor_def_prior priority)
Definition: atacmds.cpp:149
int ataEnableAutoSave(ata_device *device)
Definition: atacmds.cpp:1551
int ataReadSelfTestLog(ata_device *device, ata_smart_selftestlog *data, firmwarebug_defs firmwarebugs)
Definition: atacmds.cpp:1013
std::string ata_format_attr_raw_value(const ata_smart_attribute &attr, const ata_vendor_attr_defs &defs)
Definition: atacmds.cpp:1920
int ata_find_attr_index(unsigned char id, const ata_smart_values &smartval)
Definition: atacmds.cpp:2146
int ataReadErrorLog(ata_device *device, ata_smart_errorlog *data, firmwarebug_defs firmwarebugs)
Definition: atacmds.cpp:1424
void ata_get_size_info(const ata_identify_device *id, ata_size_info &sizes)
Definition: atacmds.cpp:658
int ata_read_identity(ata_device *device, ata_identify_device *buf, bool fix_swapped_id, unsigned char *raw_buf)
Definition: atacmds.cpp:817
int ataEnableSmart(ata_device *device)
Definition: atacmds.cpp:1536
int smartcommandhandler(ata_device *device, smart_command_set command, int select, char *data)
Definition: atacmds.cpp:431
bool isGeneralPurposeLoggingCapable(const ata_identify_device *identity)
Definition: atacmds.cpp:1769
int ataCheckPowerMode(ata_device *device)
Definition: atacmds.cpp:777
std::string create_vendor_attribute_arg_list()
Definition: atacmds.cpp:262
bool isSmartTestLogCapable(const ata_smart_values *data, const ata_identify_device *identity)
Definition: atacmds.cpp:1750
int ataReadSmartValues(ata_device *device, struct ata_smart_values *data)
Definition: atacmds.cpp:967
bool parse_firmwarebug_def(const char *opt, firmwarebug_defs &firmwarebugs)
Definition: atacmds.cpp:277
int ataSetSCTErrorRecoveryControltime(ata_device *device, unsigned type, unsigned short time_limit, bool power_on, bool mfg_default)
Definition: atacmds.cpp:2516
unsigned char ata_debugmode
Definition: atacmds.cpp:33
const char * get_valid_firmwarebug_args()
Definition: atacmds.cpp:297
uint64_t ata_get_attr_raw_value(const ata_smart_attribute &attr, const ata_vendor_attr_defs &defs)
Definition: atacmds.cpp:1846
bool ataReadExtErrorLog(ata_device *device, ata_smart_exterrlog *log, unsigned page, unsigned nsectors, firmwarebug_defs firmwarebugs)
Definition: atacmds.cpp:1491
int ataWriteSelectiveSelfTestLog(ata_device *device, ata_selective_selftest_args &args, const ata_smart_values *sv, uint64_t num_sectors, const ata_selective_selftest_args *prev_args)
Definition: atacmds.cpp:1213
std::string ata_get_smart_attr_name(unsigned char id, const ata_vendor_attr_defs &defs, int rpm)
Definition: atacmds.cpp:2127
int ataReadSmartThresholds(ata_device *device, struct ata_smart_thresholds_pvt *data)
Definition: atacmds.cpp:1518
int ataIsSmartEnabled(const ata_identify_device *drive)
Definition: atacmds.cpp:951
void ata_format_id_string(char *out, const unsigned char *in, int n)
Definition: atacmds.cpp:762
int ataReadLogDirectory(ata_device *device, ata_smart_log_directory *data, bool gpl)
Definition: atacmds.cpp:1164
bool ata_set_features(ata_device *device, unsigned char features, int sector_count)
Definition: atacmds.cpp:799
ata_attr_state ata_get_attr_state(const ata_smart_attribute &attr, int attridx, const ata_smart_threshold_entry *thresholds, const ata_vendor_attr_defs &defs, unsigned char *threshval)
Definition: atacmds.cpp:1796
#define ATA_ENABLE_READ_LOOK_AHEAD
Definition: atacmds.h:72
#define ATA_DISABLE_WRITE_CACHE
Definition: atacmds.h:67
bool isSupportSelfTest(const ata_smart_values *data)
Definition: atacmds.h:877
#define SELECTIVE_SELF_TEST
Definition: atacmds.h:101
bool isSCTErrorRecoveryControlCapable(const ata_identify_device *drive)
Definition: atacmds.h:889
#define ATA_ENABLE_APM
Definition: atacmds.h:70
#define ATA_DISABLE_AAM
Definition: atacmds.h:65
#define OFFLINE_FULL_SCAN
Definition: atacmds.h:97
#define SHORT_SELF_TEST
Definition: atacmds.h:98
bool isSupportConveyanceSelfTest(const ata_smart_values *data)
Definition: atacmds.h:880
#define ATA_ENABLE_DISABLE_DSN
Definition: atacmds.h:73
#define ATA_IDLE
Definition: atacmds.h:55
bool isSupportAutomaticTimer(const ata_smart_values *data)
Definition: atacmds.h:868
#define EXTEND_SELF_TEST
Definition: atacmds.h:99
#define ATA_ENABLE_WRITE_CACHE
Definition: atacmds.h:71
bool isSupportSelectiveSelfTest(const ata_smart_values *data)
Definition: atacmds.h:883
#define ATA_DISABLE_READ_LOOK_AHEAD
Definition: atacmds.h:68
#define ATTRIBUTE_FLAGS_PREFAILURE(x)
Definition: atacmds.h:164
@ PRIOR_USER
Definition: atacmds.h:644
#define ATA_ENABLE_AAM
Definition: atacmds.h:69
bool isSupportExecuteOfflineImmediate(const ata_smart_values *data)
Definition: atacmds.h:862
@ IMMEDIATE_OFFLINE
Definition: atacmds.h:34
#define CONVEYANCE_SELF_TEST
Definition: atacmds.h:100
@ BUG_SAMSUNG3
Definition: atacmds.h:717
@ BUG_NOLOGDIR
Definition: atacmds.h:714
#define ATA_SECURITY_FREEZE_LOCK
Definition: atacmds.h:57
@ SEL_REDO
Definition: atacmds.h:607
@ SEL_NEXT
Definition: atacmds.h:608
@ SEL_CONT
Definition: atacmds.h:609
#define ATA_DISABLE_APM
Definition: atacmds.h:66
#define NUMBER_ATA_SMART_ATTRIBUTES
Definition: atacmds.h:110
ata_attr_state
Definition: atacmds.h:902
@ ATTRSTATE_FAILED_NOW
Definition: atacmds.h:908
@ ATTRSTATE_NON_EXISTING
Definition: atacmds.h:903
@ ATTRSTATE_NO_NORMVAL
Definition: atacmds.h:904
Smart pointer class for device pointers.
void replace(device_type *dev)
Replace the pointer.
ATA device access.
unsigned char m_flags[256]
Definition: smartd.cpp:382
bool is_set(int id, unsigned char flag) const
Definition: smartd.cpp:372
void set(int id, unsigned char flags)
Definition: smartd.cpp:375
env_buffer()=default
env_buffer(const env_buffer &)=delete
char * m_buf
Definition: smartd.cpp:1026
void set(const char *name, const char *value)
Definition: smartd.cpp:1029
void operator=(const env_buffer &)=delete
bool is_set(firmwarebug_t bug) const
Definition: atacmds.h:728
NVMe device access.
unsigned get_nsid() const
Get namespace id.
unsigned char * data()
Definition: utility.h:148
Wrapper class for POSIX regex(3) or std::regex Supports copy & assignment and is compatible with STL ...
Definition: utility.h:222
bool full_match(const char *str) const
Return true if full string matches pattern.
Definition: utility.cpp:593
regmatch_t match_range
Definition: utility.h:262
const char * get_errmsg() const
Get error message from last compile().
Definition: utility.h:249
bool empty() const
Definition: utility.h:253
const char * get_pattern() const
Definition: utility.h:245
bool compile(const char *pattern)
Set and compile new pattern, return false on error.
Definition: utility.cpp:547
bool execute(const char *str, unsigned nmatch, match_range *pmatch) const
Return true if substring matches pattern, fill match_range array.
Definition: utility.cpp:604
SCSI device access.
bool use_rcap16() const
List of devices for DEVICESCAN.
unsigned size() const
void push_back(smart_device *dev)
smart_device * release(unsigned i)
Base class for all devices.
Definition: dev_interface.h:33
bool is_scsi() const
Return true if SCSI device.
Definition: dev_interface.h:89
virtual bool is_powered_down()
Early test if device is powered up or down.
bool is_nvme() const
Return true if NVMe device.
Definition: dev_interface.h:92
const device_info & get_info() const
Get device info struct.
const char * get_errmsg() const
Get last error message.
virtual bool close()=0
Close device, return false on error.
nvme_device * to_nvme()
Downcast to NVMe device.
ata_device * to_ata()
Downcast to ATA device.
Definition: dev_interface.h:96
scsi_device * to_scsi()
Downcast to SCSI device.
static int get_num_objects()
Get current number of allocated 'smart_device' objects.
bool is_ata() const
Return true if ATA device.
Definition: dev_interface.h:86
virtual bool open()=0
Open device, return false on error.
virtual std::string get_unique_dev_name(const char *name, const char *type) const
Return unique device name which is (only) suitable for duplicate detection.
virtual smart_device * get_smart_device(const char *name, const char *type)
Return device object for device 'name' with some 'type'.
static void init()
Initialize platform interface and register with smi().
Definition: dev_legacy.cpp:334
Wrapper class for FILE *.
Definition: utility.h:163
bool close()
Definition: utility.h:194
bool open(const char *name, const char *mode)
Definition: utility.h:177
std::vector< std::string > smart_devtype_list
List of types for DEVICESCAN.
smart_interface * smi()
Global access to the (usually singleton) smart_interface.
const drive_settings * lookup_drive_apply_presets(const ata_identify_device *drive, ata_vendor_attr_defs &defs, firmwarebug_defs &firmwarebugs, std::string &dbversion)
bool init_drive_database(bool use_default_db)
const char * get_drivedb_path_add()
static bool match(const char *pattern, const char *str)
void show_presets(const ata_identify_device *drive)
bool read_drive_database(const char *path)
int showallpresets()
u16 flags
Definition: megaraid.h:14
u32 count
Definition: megaraid.h:1
u32 w[3]
Definition: megaraid.h:19
u8 b[12]
Definition: megaraid.h:17
ptr_t buffer
Definition: megaraid.h:3
u16 s[6]
Definition: megaraid.h:18
ptr_t data
Definition: megaraid.h:15
u32 size
Definition: megaraid.h:0
uint8_t id
uint32_t nsid
union @43 entry
bool nvme_read_self_test_log(nvme_device *device, uint32_t nsid, smartmontools::nvme_self_test_log &self_test_log)
Definition: nvmecmds.cpp:270
int nvme_status_to_errno(uint16_t status)
Definition: nvmecmds.cpp:472
bool nvme_read_id_ctrl(nvme_device *device, nvme_id_ctrl &id_ctrl)
Definition: nvmecmds.cpp:132
unsigned char nvme_debugmode
Definition: nvmecmds.cpp:27
bool nvme_self_test(nvme_device *device, uint8_t stc, uint32_t nsid)
Definition: nvmecmds.cpp:285
unsigned nvme_read_error_log(nvme_device *device, nvme_error_log_page *error_log, unsigned num_entries, bool lpo_sup)
Definition: nvmecmds.cpp:231
bool nvme_read_smart_log(nvme_device *device, uint32_t nsid, nvme_smart_log &smart_log)
Definition: nvmecmds.cpp:254
const char * nvme_status_to_info_str(char *buf, size_t bufsize, uint16_t status)
Definition: nvmecmds.cpp:490
constexpr bool nvme_status_is_error(uint16_t status)
Definition: nvmecmds.h:288
constexpr uint32_t nvme_broadcast_nsid
Definition: nvmecmds.h:257
static struct @44 devices[20]
std::string get_exe_dir()
Definition: os_win32.cpp:4841
#define _WIN32
Definition: os_win32.cpp:54
FILE * popen_as_ugid(const char *cmd, const char *mode, uid_t uid, gid_t gid)
int pclose_as_ugid(FILE *f)
const char * parse_ugid(const char *s, uid_t &uid, gid_t &gid, std::string &uname, std::string &gname)
void scsiDecodeErrCounterPage(unsigned char *resp, struct scsiErrorCounter *ecp, int allocLen)
Definition: scsicmds.cpp:2609
int scsiFetchIECmpage(scsi_device *device, struct scsi_iec_mode_page *iecp, int modese_len)
Definition: scsicmds.cpp:1838
int scsi_decode_lu_dev_id(const unsigned char *b, int blen, char *s, int slen, int *transport)
Definition: scsicmds.cpp:747
int scsiTestUnitReady(scsi_device *device)
Definition: scsicmds.cpp:1456
int scsiInquiryVpd(scsi_device *device, int vpd_page, uint8_t *pBuf, int bufLen)
Definition: scsicmds.cpp:1210
int scsiSmartExtendSelfTest(scsi_device *device)
Definition: scsicmds.cpp:2509
void scsiDecodeNonMediumErrPage(unsigned char *resp, struct scsiNonMediumError *nmep, int allocLen)
Definition: scsicmds.cpp:2654
int scsiCheckIE(scsi_device *device, int hasIELogPage, int hasTempLogPage, uint8_t *asc, uint8_t *ascq, uint8_t *currenttemp, uint8_t *triptemp)
Definition: scsicmds.cpp:2028
uint64_t scsiGetSize(scsi_device *device, bool avoid_rcap16, struct scsi_readcap_resp *srrp)
Definition: scsicmds.cpp:1713
int scsiSelfTestInProgress(scsi_device *fd, int *inProgress)
Definition: scsicmds.cpp:2773
char * scsiGetIEString(uint8_t asc, uint8_t ascq, char *b, int blen)
Definition: scsicmds.cpp:3198
supported_vpd_pages * supported_vpd_pages_p
Definition: scsicmds.cpp:47
void scsi_format_id_string(char *out, const uint8_t *in, int n)
Definition: scsicmds.cpp:3117
int scsiStdInquiry(scsi_device *device, uint8_t *pBuf, int bufLen)
Definition: scsicmds.cpp:1163
int scsiCountFailedSelfTests(scsi_device *fd, int noisy)
Definition: scsicmds.cpp:2722
int scsiSetControlGLTSD(scsi_device *device, int enabled, int modese_len)
Definition: scsicmds.cpp:2987
int scsi_IsExceptionControlEnabled(const struct scsi_iec_mode_page *iecp)
Definition: scsicmds.cpp:1885
int scsiSmartShortSelfTest(scsi_device *device)
Definition: scsicmds.cpp:2498
int scsiLogSense(scsi_device *device, int pagenum, int subpagenum, uint8_t *pBuf, int bufLen, int known_resp_len)
Definition: scsicmds.cpp:873
unsigned char scsi_debugmode
Definition: scsicmds.cpp:45
#define SIMPLE_ERR_BECOMING_READY
Definition: scsicmds.h:354
#define SIMPLE_ERR_BAD_FIELD
Definition: scsicmds.h:350
#define LOGPAGEHDRSIZE
Definition: scsicmds.h:385
#define SIMPLE_ERR_NOT_READY
Definition: scsicmds.h:348
#define SCSI_VPD_DEVICE_IDENTIFICATION
Definition: scsicmds.h:309
#define SIMPLE_ERR_NO_MEDIUM
Definition: scsicmds.h:353
#define SCSI_PT_CDROM
Definition: scsicmds.h:194
#define VERIFY_ERROR_COUNTER_LPAGE
Definition: scsicmds.h:223
#define SUPPORTED_LPAGES
Definition: scsicmds.h:218
#define SIMPLE_ERR_BAD_OPCODE
Definition: scsicmds.h:349
#define NON_MEDIUM_ERROR_LPAGE
Definition: scsicmds.h:224
#define SCSI_VPD_UNIT_SERIAL_NUMBER
Definition: scsicmds.h:308
#define SCSI_PT_HOST_MANAGED
Definition: scsicmds.h:199
#define SCSI_PT_WO
Definition: scsicmds.h:193
#define SCSI_PT_RBC
Definition: scsicmds.h:198
#define TEMPERATURE_LPAGE
Definition: scsicmds.h:229
#define WRITE_ERROR_COUNTER_LPAGE
Definition: scsicmds.h:220
#define SCSI_PT_DIRECT_ACCESS
Definition: scsicmds.h:191
#define READ_ERROR_COUNTER_LPAGE
Definition: scsicmds.h:221
#define IE_LPAGE
Definition: scsicmds.h:241
#define SCSI_PT_OPTICAL
Definition: scsicmds.h:195
static uint64_t sg_get_unaligned_le64(const void *p)
Definition: sg_unaligned.h:303
static uint16_t sg_get_unaligned_le16(const void *p)
Definition: sg_unaligned.h:292
#define EXIT_BADCONF
Definition: smartd.cpp:139
static int CloseDevice(smart_device *device, const char *name)
Definition: smartd.cpp:1722
#define EXIT_SIGNAL
Definition: smartd.cpp:151
static bool is_duplicate_dev_idinfo(const dev_config &cfg, const dev_config_vector &prev_cfgs)
Definition: smartd.cpp:1945
static void reset_warning_mail(const dev_config &cfg, dev_state &state, int which, const char *fmt,...) __attribute_format_printf(4
Definition: smartd.cpp:1293
unsigned char failuretest_permissive
Definition: smartd.cpp:217
#define SIGQUIT_KEYNAME
Definition: smartd.cpp:90
const bool fix_swapped_id
Definition: smartd.cpp:1968
static std::string state_path_prefix
Definition: smartd.cpp:170
static bool write_dev_state(const char *path, const persistent_dev_state &state)
Definition: smartd.cpp:781
static int NVMeDeviceScan(dev_config &cfg, dev_state &state, nvme_device *nvmedev, const dev_config_vector *prev_cfgs)
Definition: smartd.cpp:2766
static int ParseToken(char *token, dev_config &cfg, smart_devtype_list &scan_types)
Definition: smartd.cpp:4547
static std::string configfile_alt
Definition: smartd.cpp:184
static int Get3Integers(const char *arg, const char *name, const char *token, int lineno, const char *cfgfile, unsigned char *val1, unsigned char *val2, unsigned char *val3)
Definition: smartd.cpp:4492
static int facility
Definition: smartd.cpp:209
#define EXIT_BADDEV
Definition: smartd.cpp:148
#define EXIT_NODEV
Definition: smartd.cpp:149
static void sighandler(int sig)
Definition: smartd.cpp:956
static int check_ata_self_test_log(ata_device *device, const char *name, firmwarebug_defs firmwarebugs, unsigned &hour)
Definition: smartd.cpp:1782
static const int MAILTYPE_TEST
Definition: smartd.cpp:463
static void do_disable_standby_check(const dev_config_vector &configs, const dev_state_vector &states)
Definition: smartd.cpp:4199
static void log_self_test_exec_status(const char *name, unsigned char status)
Definition: smartd.cpp:1859
const char * fmt
Definition: smartd.cpp:1322
static void check_attribute(const dev_config &cfg, dev_state &state, const ata_smart_attribute &attr, const ata_smart_attribute &prev, int attridx, const ata_smart_threshold_entry *thresholds)
Definition: smartd.cpp:3497
static void log_offline_data_coll_status(const char *name, unsigned char status)
Definition: smartd.cpp:1836
static void notify_exit(int)
Definition: smartd.cpp:348
static void write_all_dev_attrlogs(const dev_config_vector &configs, dev_state_vector &states)
Definition: smartd.cpp:910
int main(int argc, char **argv)
Definition: smartd.cpp:6155
static int ReadOrMakeConfigEntries(dev_config_vector &conf_entries, smart_device_list &scanned_devs)
Definition: smartd.cpp:5728
static void report_self_test_log_changes(const dev_config &cfg, dev_state &state, int errcnt, uint64_t hour)
Definition: smartd.cpp:2970
static quit_t quit
Definition: smartd.cpp:205
static void write_dev_state_line(FILE *f, const char *name, uint64_t val)
Definition: smartd.cpp:768
static time_t dosleep(time_t wakeuptime, const dev_config_vector &configs, dev_state_vector &states, bool &sigwakeup)
Definition: smartd.cpp:4325
static void notify_extend_timeout()
Definition: smartd.cpp:344
static void check_pending(const dev_config &cfg, dev_state &state, unsigned char id, bool increase_only, const ata_smart_values &smartval, int mailtype, const char *msg)
Definition: smartd.cpp:3381
static void PrintTestSchedule(const dev_config_vector &configs, dev_state_vector &states, const smart_device_list &devices)
Definition: smartd.cpp:3149
static bool check_abs_path(char option, const std::string &path)
Definition: smartd.cpp:5301
static bool register_device(dev_config &cfg, dev_state &state, smart_device_auto_ptr &dev, const dev_config_vector *prev_cfgs)
Definition: smartd.cpp:5773
static void MailWarning(const dev_config &cfg, dev_state &state, int which, const char *fmt,...) __attribute_format_printf(4
Definition: smartd.cpp:1050
static void init_disable_standby_check(const dev_config_vector &configs)
Definition: smartd.cpp:4171
static void notify_init()
Definition: smartd.cpp:343
static void install_signal_handlers()
Definition: smartd.cpp:4275
static void capabilities_drop_now()
Definition: smartd.cpp:1002
static void PrintValidArgs(char opt)
Definition: smartd.cpp:5287
#define EXIT_BADCMD
Definition: smartd.cpp:138
#define EXIT_NOCONF
Definition: smartd.cpp:142
const char * smartd_cpp_cvsid
Definition: smartd.cpp:93
static int NVMeCheckDevice(const dev_config &cfg, dev_state &state, nvme_device *nvmedev, bool firstpass, bool allow_selftests)
Definition: smartd.cpp:4062
static void notify_check(int)
Definition: smartd.cpp:346
#define SCANDIRECTIVE
Definition: smartd.cpp:5035
static std::string attrlog_path_prefix
Definition: smartd.cpp:177
static bool WaitForPidFile()
Definition: smartd.cpp:1410
static void notify_wait(time_t, int)
Definition: smartd.cpp:347
void checksumwarning(const char *string)
Definition: smartd.cpp:1401
static volatile int caughtsigEXIT
Definition: smartd.cpp:232
static int ParseConfigFile(dev_config_vector &conf_entries, smart_devtype_list &scan_types)
Definition: smartd.cpp:5152
static uint64_t le128_to_uint64(const unsigned char(&val)[16])
Definition: smartd.cpp:2685
void(* signal_handler_type)(int)
Definition: smartd.cpp:97
#define EXIT_PID
Definition: smartd.cpp:141
vsnprintf(buf, sizeof(buf), fmt, ap)
static int standby_disable_state
Definition: smartd.cpp:4169
static void notify_msg(const char *)
Definition: smartd.cpp:345
static int checktime
Definition: smartd.cpp:159
static int SCSICheckDevice(const dev_config &cfg, dev_state &state, scsi_device *scsidev, bool allow_selftests)
Definition: smartd.cpp:3838
static int ParseConfigLine(dev_config_vector &conf_entries, dev_config &default_conf, smart_devtype_list &scan_types, int lineno, char *line)
Definition: smartd.cpp:5046
static const char * GetValidArgList(char opt)
Definition: smartd.cpp:1592
static const char test_type_chars[]
Definition: smartd.cpp:3021
static bool write_dev_attrlog(const char *path, const dev_state &state)
Definition: smartd.cpp:835
static void CheckDevicesOnce(const dev_config_vector &configs, dev_state_vector &states, smart_device_list &devices, bool firstpass, bool allow_selftests)
Definition: smartd.cpp:4246
static const char * configfile
Definition: smartd.cpp:180
static const char *const configfile_stdin
Definition: smartd.cpp:182
static const char * fmt_temp(unsigned char x, char(&buf)[20])
Definition: smartd.cpp:3414
static void write_all_dev_states(const dev_config_vector &configs, dev_state_vector &states, bool write_always=true)
Definition: smartd.cpp:889
static void static bool notify_post_init()
Definition: smartd.cpp:332
static bool check_pending_id(const dev_config &cfg, const dev_state &state, unsigned char id, const char *msg)
Definition: smartd.cpp:1887
quit_t
Definition: smartd.cpp:201
@ QUIT_NODEVSTARTUP
Definition: smartd.cpp:202
@ QUIT_NEVER
Definition: smartd.cpp:202
@ QUIT_ERRORS
Definition: smartd.cpp:203
@ QUIT_ONECHECK
Definition: smartd.cpp:202
@ QUIT_SHOWTESTS
Definition: smartd.cpp:203
@ QUIT_NODEV
Definition: smartd.cpp:202
static bool not_allowed_in_filename(char c)
Definition: smartd.cpp:1749
static int SCSIDeviceScan(dev_config &cfg, dev_state &state, scsi_device *scsidev, const dev_config_vector *prev_cfgs)
Definition: smartd.cpp:2419
static bool do_fork
Definition: smartd.cpp:213
static void capabilities_log_error_hint()
Definition: smartd.cpp:1003
static void PrintHead()
Definition: smartd.cpp:1538
static char next_scheduled_test(const dev_config &cfg, dev_state &state, time_t usetime=0)
Definition: smartd.cpp:3026
static void CheckTemperature(const dev_config &cfg, dev_state &state, unsigned char currtemp, unsigned char triptemp)
Definition: smartd.cpp:3423
static int parse_options(int argc, char **argv)
Definition: smartd.cpp:5316
static unsigned char debugmode
Definition: smartd.cpp:155
static bool read_dev_state(const char *path, persistent_dev_state &state)
Definition: smartd.cpp:730
static int DoATASelfTest(const dev_config &cfg, dev_state &state, ata_device *device, char testtype)
Definition: smartd.cpp:3262
static void printoutvaliddirectiveargs(int priority, char d)
Definition: smartd.cpp:4413
static bool write_pid_file()
Definition: smartd.cpp:1514
#define EXIT_BADCODE
Definition: smartd.cpp:146
static std::string pid_file
Definition: smartd.cpp:163
static int daemon_init()
Definition: smartd.cpp:1433
static bool is_offl_coll_in_progress(unsigned char status)
Definition: smartd.cpp:1824
static void USR1handler(int sig)
Definition: smartd.cpp:928
static bool open_device(const dev_config &cfg, dev_state &state, smart_device *device, const char *type)
Definition: smartd.cpp:2907
static int ATACheckDevice(const dev_config &cfg, dev_state &state, ata_device *atadev, bool firstpass, bool allow_selftests)
Definition: smartd.cpp:3594
const char va_list ap
Definition: smartd.cpp:1323
static int check_nvme_self_test_log(uint32_t nsid, const nvme_self_test_log &self_test_log, uint64_t &hour)
Definition: smartd.cpp:3989
static int main_worker(int argc, char **argv)
Definition: smartd.cpp:5959
static bool quit_nodev0
Definition: smartd.cpp:206
static void Directives()
Definition: smartd.cpp:1544
static bool register_devices(const dev_config_vector &conf_entries, smart_device_list &scanned_devs, dev_config_vector &configs, dev_state_vector &states, smart_device_list &devices)
Definition: smartd.cpp:5858
std::vector< dev_config > dev_config_vector
Container for configuration info for each device.
Definition: smartd.cpp:580
static void HUPhandler(int sig)
Definition: smartd.cpp:946
static int read_ata_error_count(ata_device *device, const char *name, firmwarebug_defs firmwarebugs, bool extended)
Definition: smartd.cpp:1758
static constexpr int default_checktime
Definition: smartd.cpp:158
static int GetInteger(const char *arg, const char *name, const char *token, int lineno, const char *cfgfile, int min, int max, char *suffix=0)
Definition: smartd.cpp:4458
static const int SMARTD_NMAIL
Definition: smartd.cpp:461
#define EXIT_READCONF
Definition: smartd.cpp:143
static void PrintOut(int priority, const char *fmt,...) __attribute_format_printf(2
Definition: smartd.cpp:1375
static void log_nvme_self_test_exec_status(const char *name, dev_state &state, bool firstpass, const nvme_self_test_log &self_test_log)
Definition: smartd.cpp:3923
emailfreqs
Definition: smartd.cpp:353
static const unsigned num_test_types
Definition: smartd.cpp:3022
static void Usage()
Definition: smartd.cpp:1630
static void format_set_result_msg(std::string &msg, const char *name, bool ok, int set_option=0, bool has_value=false)
Definition: smartd.cpp:1928
static int checktime_min
Definition: smartd.cpp:160
static int DoSCSISelfTest(const dev_config &cfg, dev_state &state, scsi_device *device, char testtype)
Definition: smartd.cpp:3203
static void set_signal_if_not_ignored(int sig, signal_handler_type handler)
Definition: smartd.cpp:100
@ MONITOR_RAW
Definition: smartd.cpp:363
@ MONITOR_RAW_PRINT
Definition: smartd.cpp:362
@ MONITOR_RAW_AS_CRIT
Definition: smartd.cpp:365
@ MONITOR_IGN_FAILUSE
Definition: smartd.cpp:360
@ MONITOR_IGNORE
Definition: smartd.cpp:361
@ MONITOR_AS_CRIT
Definition: smartd.cpp:364
time_t calc_next_wakeuptime(time_t wakeuptime, time_t timenow, int ct)
Definition: smartd.cpp:4318
static int ATADeviceScan(dev_config &cfg, dev_state &state, ata_device *atadev, const dev_config_vector *prev_cfgs)
Definition: smartd.cpp:1971
std::vector< dev_state > dev_state_vector
Container for state info for each device.
Definition: smartd.cpp:583
void pout(const char *fmt,...)
Definition: smartd.cpp:1347
static bool is_self_test_in_progress(unsigned char status)
Definition: smartd.cpp:1830
#define EXIT_NOMEM
Definition: smartd.cpp:145
static void finish_device_scan(dev_config &cfg, dev_state &state)
Definition: smartd.cpp:1911
#define EBUFLEN
Definition: smartd.cpp:1043
static bool check_nvme_error_log(const dev_config &cfg, dev_state &state, nvme_device *nvmedev, uint64_t newcnt=0)
Definition: smartd.cpp:2699
static bool parse_dev_state_line(const char *line, persistent_dev_state &state)
Definition: smartd.cpp:633
static const int scsiLogRespLen
Definition: smartd.cpp:135
static std::string warning_script
Definition: smartd.cpp:187
#define EXIT_STARTUP
Definition: smartd.cpp:140
static volatile int caughtsigUSR1
Definition: smartd.cpp:220
static int MakeConfigEntries(const dev_config &base_cfg, dev_config_vector &conf_entries, smart_device_list &scanned_devs, const smart_devtype_list &types)
Definition: smartd.cpp:5684
static int start_nvme_self_test(const dev_config &cfg, dev_state &state, nvme_device *device, char testtype, const nvme_self_test_log &self_test_log)
Definition: smartd.cpp:4024
static bool sanitize_dev_idinfo(std::string &s)
Definition: smartd.cpp:1733
static volatile int caughtsigHUP
Definition: smartd.cpp:229
#define STATIC_ASSERT(x)
Definition: static_assert.h:24
unsigned char model[40]
Definition: atacmds.h:120
unsigned short words088_255[168]
Definition: atacmds.h:130
unsigned char fw_rev[8]
Definition: atacmds.h:119
unsigned char serial_no[20]
Definition: atacmds.h:117
uint64_t capacity
Definition: atacmds.h:986
uint64_t sectors
Definition: atacmds.h:985
unsigned char id
Definition: atacmds.h:138
unsigned char current
Definition: atacmds.h:142
unsigned short flags
Definition: atacmds.h:141
unsigned char reserv
Definition: atacmds.h:145
unsigned char worst
Definition: atacmds.h:143
unsigned char raw[6]
Definition: atacmds.h:144
unsigned short int ata_error_count
Definition: atacmds.h:302
unsigned char error_log_pointer
Definition: atacmds.h:300
unsigned short error_log_index
Definition: atacmds.h:383
unsigned char reserved1
Definition: atacmds.h:382
unsigned short device_error_count
Definition: atacmds.h:385
struct ata_smart_log_entry entry[255]
Definition: atacmds.h:467
unsigned char numsectors
Definition: atacmds.h:458
unsigned char mostrecenttest
Definition: atacmds.h:412
struct ata_smart_selftestlog_struct selftest_struct[21]
Definition: atacmds.h:410
Definition: atacmds.h:231
struct ata_smart_threshold_entry thres_entries[NUMBER_ATA_SMART_ATTRIBUTES]
Definition: atacmds.h:244
unsigned char self_test_exec_status
Definition: atacmds.h:202
unsigned char offline_data_collection_capability
Definition: atacmds.h:205
unsigned char offline_data_collection_status
Definition: atacmds.h:201
struct ata_smart_attribute vendor_attributes[NUMBER_ATA_SMART_ATTRIBUTES]
Definition: atacmds.h:200
Configuration data for a device.
Definition: smartd.cpp:389
bool ignorepresets
Definition: smartd.cpp:416
std::string emailcmdline
Definition: smartd.cpp:428
bool offlinests
Definition: smartd.cpp:408
char powermode
Definition: smartd.cpp:419
bool ignore
Definition: smartd.cpp:399
ata_vendor_attr_defs attribute_defs
Definition: smartd.cpp:454
bool smartcheck
Definition: smartd.cpp:401
int set_wcache
Definition: smartd.cpp:440
int powerskipmax
Definition: smartd.cpp:421
char autoofflinetest
Definition: smartd.cpp:414
unsigned char tempdiff
Definition: smartd.cpp:422
int set_standby
Definition: smartd.cpp:438
int dev_rpm
Definition: smartd.cpp:434
unsigned short sct_erc_readtime
Definition: smartd.cpp:444
std::string state_file
Definition: smartd.cpp:396
int set_dsn
Definition: smartd.cpp:441
bool errorlog
Definition: smartd.cpp:406
std::string dev_idinfo_bc
Definition: smartd.cpp:395
std::string dev_idinfo
Definition: smartd.cpp:394
bool powerquiet
Definition: smartd.cpp:420
attribute_flags monitor_attr_flags
Definition: smartd.cpp:452
unsigned nvme_err_log_max_entries
Definition: smartd.cpp:457
unsigned test_offset_factor
Definition: smartd.cpp:425
firmwarebug_defs firmwarebugs
Definition: smartd.cpp:415
bool showpresets
Definition: smartd.cpp:417
bool removable
Definition: smartd.cpp:418
unsigned char offl_pending_id
Definition: smartd.cpp:448
unsigned char tempcrit
Definition: smartd.cpp:423
bool offlinests_ns
Definition: smartd.cpp:409
bool curr_pending_set
Definition: smartd.cpp:450
unsigned short sct_erc_writetime
Definition: smartd.cpp:445
bool sct_erc_set
Definition: smartd.cpp:443
int set_aam
Definition: smartd.cpp:435
bool selfteststs
Definition: smartd.cpp:410
unsigned char curr_pending_id
Definition: smartd.cpp:447
bool selftest
Definition: smartd.cpp:405
bool id_is_unique
Definition: smartd.cpp:400
std::string name
Definition: smartd.cpp:391
std::string emailaddress
Definition: smartd.cpp:429
bool offl_pending_incr
Definition: smartd.cpp:449
int lineno
Definition: smartd.cpp:390
int checktime
Definition: smartd.cpp:398
bool prefail
Definition: smartd.cpp:403
bool xerrorlog
Definition: smartd.cpp:407
int set_lookahead
Definition: smartd.cpp:437
bool usage
Definition: smartd.cpp:404
std::string attrlog_file
Definition: smartd.cpp:397
regular_expression test_regex
Definition: smartd.cpp:424
bool emailtest
Definition: smartd.cpp:431
bool set_security_freeze
Definition: smartd.cpp:439
bool usagefailed
Definition: smartd.cpp:402
std::string dev_name
Definition: smartd.cpp:392
emailfreqs emailfreq
Definition: smartd.cpp:430
bool selfteststs_ns
Definition: smartd.cpp:411
unsigned char tempinfo
Definition: smartd.cpp:423
bool permissive
Definition: smartd.cpp:412
char autosave
Definition: smartd.cpp:413
int set_apm
Definition: smartd.cpp:436
bool offl_pending_set
Definition: smartd.cpp:450
std::string dev_type
Definition: smartd.cpp:393
bool curr_pending_incr
Definition: smartd.cpp:449
Runtime state data for a device.
Definition: smartd.cpp:574
void update_temp_state()
Definition: smartd.cpp:609
void update_persistent_state()
Definition: smartd.cpp:586
const char * modelfamily
Definition: knowndrives.h:19
const char * warningmsg
Definition: knowndrives.h:22
time_t lastsent
Definition: smartd.cpp:469
int logged
Definition: smartd.cpp:467
time_t firstsent
Definition: smartd.cpp:468
Persistent state data for a device.
Definition: smartd.cpp:474
scsi_nonmedium_error_t scsi_nonmedium_error
Definition: smartd.cpp:513
unsigned char selflogcount
Definition: smartd.cpp:477
uint64_t selfloghour
Definition: smartd.cpp:478
unsigned char tempmax
Definition: smartd.cpp:475
uint64_t nvme_err_log_entries
Definition: smartd.cpp:516
uint64_t selective_test_last_end
Definition: smartd.cpp:484
ata_attribute ata_attributes[NUMBER_ATA_SMART_ATTRIBUTES]
Definition: smartd.cpp:499
uint64_t selective_test_last_start
Definition: smartd.cpp:483
unsigned char tempmin
Definition: smartd.cpp:475
mailinfo maillog[SMARTD_NMAIL]
Definition: smartd.cpp:486
time_t scheduled_test_next_check
Definition: smartd.cpp:481
scsi_error_counter_t scsi_error_counters[3]
Definition: smartd.cpp:507
uint64_t counter[8]
Definition: scsicmds.h:158
uint8_t modese_len
Definition: scsicmds.h:149
Device info strings.
Definition: dev_interface.h:37
std::string info_name
Informal name.
Definition: dev_interface.h:46
std::string dev_type
Actual device type.
Definition: dev_interface.h:47
std::string dev_name
Device (path)name.
Definition: dev_interface.h:45
unsigned char tnvmcap[16]
Definition: nvmecmds.h:97
unsigned short oacs
Definition: nvmecmds.h:83
nvme_self_test_result results[20]
Definition: nvmecmds.h:248
unsigned char critical_warning
Definition: nvmecmds.h:177
unsigned char temperature[2]
Definition: nvmecmds.h:178
unsigned char num_err_log_entries[16]
Definition: nvmecmds.h:192
Non-persistent state data for a device.
Definition: smartd.cpp:521
bool attrlog_dirty
Definition: smartd.cpp:542
bool not_cap_selective
Definition: smartd.cpp:531
bool not_cap_long
Definition: smartd.cpp:530
ata_smart_values smartval
Definition: smartd.cpp:558
bool not_cap_conveyance
Definition: smartd.cpp:528
unsigned char NonMediumErrorPageSupported
Definition: smartd.cpp:552
bool offline_started
Definition: smartd.cpp:560
unsigned char WriteECounterPageSupported
Definition: smartd.cpp:550
bool powermodefail
Definition: smartd.cpp:538
unsigned char temperature
Definition: smartd.cpp:533
ata_smart_thresholds_pvt smartthres
Definition: smartd.cpp:559
bool not_cap_short
Definition: smartd.cpp:529
bool not_cap_offline
Definition: smartd.cpp:527
uint64_t num_sectors
Definition: smartd.cpp:557
time_t tempmin_delay
Definition: smartd.cpp:534
time_t wakeuptime
Definition: smartd.cpp:525
unsigned char SuppressReport
Definition: smartd.cpp:553
unsigned char VerifyECounterPageSupported
Definition: smartd.cpp:551
bool selftest_started
Definition: smartd.cpp:563
unsigned char modese_len
Definition: smartd.cpp:554
bool must_write
Definition: smartd.cpp:522
unsigned char ReadECounterPageSupported
Definition: smartd.cpp:549
uint8_t selftest_op
Definition: smartd.cpp:566
unsigned char TempPageSupported
Definition: smartd.cpp:548
int lastpowermodeskipped
Definition: smartd.cpp:540
uint8_t selftest_compl
Definition: smartd.cpp:567
unsigned char SmartPageSupported
Definition: smartd.cpp:547
int powerskipcnt
Definition: smartd.cpp:539
void FixGlibcTimeZoneBug()
Definition: utility.cpp:214
const char * format_char_array(char *str, int strsize, const char *chr, int chrsize)
Definition: utility.cpp:692
std::string format_version_info(const char *prog_name, int lines)
Definition: utility.cpp:87
void dateandtimezoneepoch(char(&buffer)[DATEANDEPOCHLEN], time_t tval)
Definition: utility.cpp:349
const char * format_capacity(char *str, int strsize, uint64_t val, const char *decimal_point)
Definition: utility.cpp:748
std::string strprintf(const char *fmt,...)
Definition: utility.cpp:799
bool nonempty(const void *data, int size)
Definition: utility.cpp:682
const char * packetdevicetype(int type)
Definition: utility.cpp:315
struct tm * time_to_tm_local(struct tm *tp, time_t t)
Definition: utility.cpp:326
#define DATEANDEPOCHLEN
Definition: utility.h:64
#define __attribute_format_printf(x, y)
Definition: utility.h:34