4 * Home page of code is: http://www.smartmontools.org
6 * Copyright (C) 2002-11 Bruce Allen
7 * Copyright (C) 2008-17 Christian Franke
8 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * You should have received a copy of the GNU General Public License
16 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
18 * This code was originally developed as a Senior Thesis by Michael Cornwell
19 * at the Concurrent Systems Laboratory (now part of the Storage Systems
20 * Research Center), Jack Baskin School of Engineering, University of
21 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
27 #include <sys/types.h>
40 #if defined(__FreeBSD__)
41 #include <sys/param.h>
46 #include "dev_interface.h"
48 #include "knowndrives.h"
50 #include "scsiprint.h"
51 #include "nvmeprint.h"
55 const char * smartctl_cpp_cvsid
= "$Id: smartctl.cpp 4585 2017-11-04 13:41:03Z chrfranke $"
56 CONFIG_H_CVSID SMARTCTL_H_CVSID
;
58 // Globals to control printing
59 bool printing_is_switchable
= false;
60 bool printing_is_off
= false;
62 static void printslogan()
64 pout("%s\n", format_version_info("smartctl").c_str());
67 static void UsageSummary()
69 pout("\nUse smartctl -h to get a usage summary\n\n");
73 static std::string
getvalidarglist(int opt
);
75 /* void prints help information for command syntax */
78 printf("Usage: smartctl [options] device\n\n");
80 "============================================ SHOW INFORMATION OPTIONS =====\n\n"
81 " -h, --help, --usage\n"
82 " Display this help and exit\n\n"
83 " -V, --version, --copyright, --license\n"
84 " Print license, copyright, and version information and exit\n\n"
86 " Show identity information for device\n\n"
87 " --identify[=[w][nvb]]\n"
88 " Show words and bits from IDENTIFY DEVICE data (ATA)\n\n"
89 " -g NAME, --get=NAME\n"
90 " Get device setting: all, aam, apm, dsn, lookahead, security,\n"
91 " wcache, rcache, wcreorder, wcache-sct\n\n"
93 " Show all SMART information for device\n\n"
95 " Show all information for device\n\n"
97 " Scan for devices\n\n"
99 " Scan for devices and try to open each device\n\n"
102 "================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n"
103 " -q TYPE, --quietmode=TYPE (ATA)\n"
104 " Set smartctl quiet mode to one of: errorsonly, silent, noserial\n\n"
105 " -d TYPE, --device=TYPE\n"
106 " Specify device type to one of:\n"
107 " %s\n\n" // TODO: fold this string
108 " -T TYPE, --tolerance=TYPE (ATA)\n"
109 " Tolerance: normal, conservative, permissive, verypermissive\n\n"
110 " -b TYPE, --badsum=TYPE (ATA)\n"
111 " Set action on bad checksum to one of: warn, exit, ignore\n\n"
112 " -r TYPE, --report=TYPE\n"
113 " Report transactions (see man page)\n\n"
114 " -n MODE[,STATUS], --nocheck=MODE[,STATUS] (ATA)\n"
115 " No check if: never, sleep, standby, idle (see man page)\n\n",
116 getvalidarglist('d').c_str()); // TODO: Use this function also for other options ?
118 "============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n"
119 " -s VALUE, --smart=VALUE\n"
120 " Enable/disable SMART on device (on/off)\n\n"
121 " -o VALUE, --offlineauto=VALUE (ATA)\n"
122 " Enable/disable automatic offline testing on device (on/off)\n\n"
123 " -S VALUE, --saveauto=VALUE (ATA)\n"
124 " Enable/disable Attribute autosave on device (on/off)\n\n"
125 " -s NAME[,VALUE], --set=NAME[,VALUE]\n"
126 " Enable/disable/change device setting: aam,[N|off], apm,[N|off],\n"
127 " dsn,[on|off], lookahead,[on|off], security-freeze,\n"
128 " standby,[N|off|now], wcache,[on|off], rcache,[on|off],\n"
129 " wcreorder,[on|off[,p]], wcache-sct,[ata|on|off[,p]]\n\n"
132 "======================================= READ AND DISPLAY DATA OPTIONS =====\n\n"
134 " Show device SMART health status\n\n"
135 " -c, --capabilities (ATA, NVMe)\n"
136 " Show device SMART capabilities\n\n"
137 " -A, --attributes\n"
138 " Show device SMART vendor-specific Attributes and values\n\n"
139 " -f FORMAT, --format=FORMAT (ATA)\n"
140 " Set output format for attributes: old, brief, hex[,id|val]\n\n"
141 " -l TYPE, --log=TYPE\n"
142 " Show device log. TYPE: error, selftest, selective, directory[,g|s],\n"
143 " xerror[,N][,error], xselftest[,N][,selftest], background,\n"
144 " sasphy[,reset], sataphy[,reset], scttemp[sts,hist],\n"
145 " scttempint,N[,p], scterc[,N,M], devstat[,N], defects[,N], ssd,\n"
146 " gplog,N[,RANGE], smartlog,N[,RANGE], nvmelog,N,SIZE\n\n"
147 " -v N,OPTION , --vendorattribute=N,OPTION (ATA)\n"
148 " Set display OPTION for vendor Attribute N (see man page)\n\n"
149 " -F TYPE, --firmwarebug=TYPE (ATA)\n"
150 " Use firmware bug workaround:\n"
152 " -P TYPE, --presets=TYPE (ATA)\n"
153 " Drive-specific presets: use, ignore, show, showall\n\n"
154 " -B [+]FILE, --drivedb=[+]FILE (ATA)\n"
155 " Read and replace [add] drive database from FILE\n"
157 get_valid_firmwarebug_args(),
158 get_drivedb_path_add()
160 #ifdef SMARTMONTOOLS_DRIVEDBDIR
164 get_drivedb_path_default()
169 "============================================ DEVICE SELF-TEST OPTIONS =====\n\n"
170 " -t TEST, --test=TEST\n"
171 " Run test. TEST: offline, short, long, conveyance, force, vendor,N,\n"
172 " select,M-N, pending,N, afterselect,[on|off]\n\n"
174 " Do test in captive mode (along with -t)\n\n"
176 " Abort any non-captive test on device\n\n"
178 std::string examples
= smi()->get_app_examples("smartctl");
179 if (!examples
.empty())
180 printf("%s\n", examples
.c_str());
183 // Values for --long only options, see parse_options()
184 enum { opt_identify
= 1000, opt_scan
, opt_scan_open
, opt_set
, opt_smart
};
186 /* Returns a string containing a formatted list of the valid arguments
187 to the option opt or empty on failure. Note 'v' case different */
188 static std::string
getvalidarglist(int opt
)
192 return "errorsonly, silent, noserial";
194 return smi()->get_valid_dev_types_str() + ", auto, test";
196 return "normal, conservative, permissive, verypermissive";
198 return "warn, exit, ignore";
200 return "[+]<FILE_NAME>";
202 return "ioctl[,N], ataioctl[,N], scsiioctl[,N], nvmeioctl[,N]";
208 return "error, selftest, selective, directory[,g|s], "
209 "xerror[,N][,error], xselftest[,N][,selftest], "
210 "background, sasphy[,reset], sataphy[,reset], "
211 "scttemp[sts,hist], scttempint,N[,p], "
212 "scterc[,N,M], devstat[,N], defects[,N], ssd, "
213 "gplog,N[,RANGE], smartlog,N[,RANGE], "
216 return "use, ignore, show, showall";
218 return "offline, short, long, conveyance, force, vendor,N, select,M-N, "
219 "pending,N, afterselect,[on|off]";
221 return std::string(get_valid_firmwarebug_args()) + ", swapid";
223 return "never, sleep[,STATUS], standby[,STATUS], idle[,STATUS]";
225 return "old, brief, hex[,id|val]";
227 return "aam, apm, dsn, lookahead, security, wcache, rcache, wcreorder, wcache-sct";
229 return "aam,[N|off], apm,[N|off], dsn,[on|off], lookahead,[on|off], security-freeze, "
230 "standby,[N|off|now], wcache,[on|off], rcache,[on|off], wcreorder,[on|off[,p]], "
231 "wcache-sct,[ata|on|off[,p]]";
233 return getvalidarglist(opt_smart
)+", "+getvalidarglist(opt_set
);
235 return "n, wn, w, v, wv, wb";
242 /* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where
243 <LIST> is the list of valid arguments for option opt. */
244 static void printvalidarglistmessage(int opt
)
247 pout("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n",
248 create_vendor_attribute_arg_list().c_str());
251 // getvalidarglist() might produce a multiline or single line string. We
252 // need to figure out which to get the formatting right.
253 std::string s
= getvalidarglist(opt
);
254 char separator
= strchr(s
.c_str(), '\n') ? '\n' : ' ';
255 pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator
, s
.c_str(), separator
);
261 // Checksum error mode
262 enum checksum_err_mode_t
{
263 CHECKSUM_ERR_WARN
, CHECKSUM_ERR_EXIT
, CHECKSUM_ERR_IGNORE
266 static checksum_err_mode_t checksum_err_mode
= CHECKSUM_ERR_WARN
;
268 static void scan_devices(const smart_devtype_list
& types
, bool with_open
, char ** argv
);
271 /* Takes command options and sets features to be run */
272 static const char * parse_options(int argc
, char** argv
,
273 ata_print_options
& ataopts
, scsi_print_options
& scsiopts
,
274 nvme_print_options
& nvmeopts
, bool & print_type_only
)
276 // Please update getvalidarglist() if you edit shortopts
277 const char *shortopts
= "h?Vq:d:T:b:r:s:o:S:HcAl:iaxv:P:t:CXF:n:B:f:g:";
278 // Please update getvalidarglist() if you edit longopts
279 struct option longopts
[] = {
280 { "help", no_argument
, 0, 'h' },
281 { "usage", no_argument
, 0, 'h' },
282 { "version", no_argument
, 0, 'V' },
283 { "copyright", no_argument
, 0, 'V' },
284 { "license", no_argument
, 0, 'V' },
285 { "quietmode", required_argument
, 0, 'q' },
286 { "device", required_argument
, 0, 'd' },
287 { "tolerance", required_argument
, 0, 'T' },
288 { "badsum", required_argument
, 0, 'b' },
289 { "report", required_argument
, 0, 'r' },
290 { "smart", required_argument
, 0, opt_smart
},
291 { "offlineauto", required_argument
, 0, 'o' },
292 { "saveauto", required_argument
, 0, 'S' },
293 { "health", no_argument
, 0, 'H' },
294 { "capabilities", no_argument
, 0, 'c' },
295 { "attributes", no_argument
, 0, 'A' },
296 { "log", required_argument
, 0, 'l' },
297 { "info", no_argument
, 0, 'i' },
298 { "all", no_argument
, 0, 'a' },
299 { "xall", no_argument
, 0, 'x' },
300 { "vendorattribute", required_argument
, 0, 'v' },
301 { "presets", required_argument
, 0, 'P' },
302 { "test", required_argument
, 0, 't' },
303 { "captive", no_argument
, 0, 'C' },
304 { "abort", no_argument
, 0, 'X' },
305 { "firmwarebug", required_argument
, 0, 'F' },
306 { "nocheck", required_argument
, 0, 'n' },
307 { "drivedb", required_argument
, 0, 'B' },
308 { "format", required_argument
, 0, 'f' },
309 { "get", required_argument
, 0, 'g' },
310 { "identify", optional_argument
, 0, opt_identify
},
311 { "set", required_argument
, 0, opt_set
},
312 { "scan", no_argument
, 0, opt_scan
},
313 { "scan-open", no_argument
, 0, opt_scan_open
},
317 char extraerror
[256];
318 memset(extraerror
, 0, sizeof(extraerror
));
321 const char * type
= 0; // set to -d optarg
322 smart_devtype_list scan_types
; // multiple -d TYPE options for --scan
323 bool use_default_db
= true; // set false on '-B FILE'
324 bool output_format_set
= false; // set true on '-f FORMAT'
325 int scan
= 0; // set by --scan, --scan-open
326 bool badarg
= false, captive
= false;
327 int testcnt
= 0; // number of self-tests requested
332 while ((optchar
= getopt_long(argc
, argv
, shortopts
, longopts
, 0)) != -1) {
335 printing_is_off
= false;
336 pout("%s", format_version_info("smartctl", true /*full*/).c_str());
340 if (!strcmp(optarg
,"errorsonly")) {
341 printing_is_switchable
= true;
342 printing_is_off
= false;
343 } else if (!strcmp(optarg
,"silent")) {
344 printing_is_switchable
= false;
345 printing_is_off
= true;
346 } else if (!strcmp(optarg
,"noserial")) {
347 dont_print_serial_number
= true;
353 if (!strcmp(optarg
, "test"))
354 print_type_only
= true;
355 else if (!strcmp(optarg
, "auto")) {
361 scan_types
.push_back(optarg
);
365 if (!strcmp(optarg
,"normal")) {
366 failuretest_conservative
= false;
367 failuretest_permissive
= 0;
368 } else if (!strcmp(optarg
,"conservative")) {
369 failuretest_conservative
= true;
370 } else if (!strcmp(optarg
,"permissive")) {
371 if (failuretest_permissive
< 0xff)
372 failuretest_permissive
++;
373 } else if (!strcmp(optarg
,"verypermissive")) {
374 failuretest_permissive
= 0xff;
380 if (!strcmp(optarg
,"warn")) {
381 checksum_err_mode
= CHECKSUM_ERR_WARN
;
382 } else if (!strcmp(optarg
,"exit")) {
383 checksum_err_mode
= CHECKSUM_ERR_EXIT
;
384 } else if (!strcmp(optarg
,"ignore")) {
385 checksum_err_mode
= CHECKSUM_ERR_IGNORE
;
392 int n1
= -1, n2
= -1, len
= strlen(optarg
);
393 char s
[9+1]; unsigned i
= 1;
394 sscanf(optarg
, "%9[a-z]%n,%u%n", s
, &n1
, &i
, &n2
);
395 if (!((n1
== len
|| n2
== len
) && 1 <= i
&& i
<= 4)) {
397 } else if (!strcmp(s
,"ioctl")) {
398 ata_debugmode
= scsi_debugmode
= nvme_debugmode
= i
;
399 } else if (!strcmp(s
,"ataioctl")) {
401 } else if (!strcmp(s
,"scsiioctl")) {
403 } else if (!strcmp(s
,"nvmeioctl")) {
412 case opt_smart
: // --smart
413 if (!strcmp(optarg
,"on")) {
414 ataopts
.smart_enable
= scsiopts
.smart_enable
= true;
415 ataopts
.smart_disable
= scsiopts
.smart_disable
= false;
416 } else if (!strcmp(optarg
,"off")) {
417 ataopts
.smart_disable
= scsiopts
.smart_disable
= true;
418 ataopts
.smart_enable
= scsiopts
.smart_enable
= false;
419 } else if (optchar
== 's') {
420 goto case_s_continued
; // --set, see below
427 if (!strcmp(optarg
,"on")) {
428 ataopts
.smart_auto_offl_enable
= true;
429 ataopts
.smart_auto_offl_disable
= false;
430 } else if (!strcmp(optarg
,"off")) {
431 ataopts
.smart_auto_offl_disable
= true;
432 ataopts
.smart_auto_offl_enable
= false;
438 if (!strcmp(optarg
,"on")) {
439 ataopts
.smart_auto_save_enable
= scsiopts
.smart_auto_save_enable
= true;
440 ataopts
.smart_auto_save_disable
= scsiopts
.smart_auto_save_disable
= false;
441 } else if (!strcmp(optarg
,"off")) {
442 ataopts
.smart_auto_save_disable
= scsiopts
.smart_auto_save_disable
= true;
443 ataopts
.smart_auto_save_enable
= scsiopts
.smart_auto_save_enable
= false;
449 ataopts
.smart_check_status
= scsiopts
.smart_check_status
= nvmeopts
.smart_check_status
= true;
450 scsiopts
.smart_ss_media_log
= true;
453 if (!strcmp(optarg
, "swapid"))
454 ataopts
.fix_swapped_id
= true;
455 else if (!parse_firmwarebug_def(optarg
, ataopts
.firmwarebugs
))
459 ataopts
.smart_general_values
= nvmeopts
.drive_capabilities
= true;
462 ataopts
.smart_vendor_attrib
= scsiopts
.smart_vendor_attrib
= nvmeopts
.smart_vendor_attrib
= true;
465 if (str_starts_with(optarg
, "error")) {
466 int n1
= -1, n2
= -1, len
= strlen(optarg
);
468 sscanf(optarg
, "error%n,%u%n", &n1
, &val
, &n2
);
469 ataopts
.smart_error_log
= scsiopts
.smart_error_log
= true;
471 nvmeopts
.error_log_entries
= 16;
472 else if (n2
== len
&& val
> 0)
473 nvmeopts
.error_log_entries
= val
;
476 } else if (!strcmp(optarg
,"selftest")) {
477 ataopts
.smart_selftest_log
= scsiopts
.smart_selftest_log
= true;
478 } else if (!strcmp(optarg
, "selective")) {
479 ataopts
.smart_selective_selftest_log
= true;
480 } else if (!strcmp(optarg
,"directory")) {
481 ataopts
.smart_logdir
= ataopts
.gp_logdir
= true; // SMART+GPL
482 } else if (!strcmp(optarg
,"directory,s")) {
483 ataopts
.smart_logdir
= true; // SMART
484 } else if (!strcmp(optarg
,"directory,g")) {
485 ataopts
.gp_logdir
= true; // GPL
486 } else if (!strcmp(optarg
,"sasphy")) {
487 scsiopts
.sasphy
= true;
488 } else if (!strcmp(optarg
,"sasphy,reset")) {
489 scsiopts
.sasphy
= scsiopts
.sasphy_reset
= true;
490 } else if (!strcmp(optarg
,"sataphy")) {
491 ataopts
.sataphy
= true;
492 } else if (!strcmp(optarg
,"sataphy,reset")) {
493 ataopts
.sataphy
= ataopts
.sataphy_reset
= true;
494 } else if (!strcmp(optarg
,"background")) {
495 scsiopts
.smart_background_log
= true;
496 } else if (!strcmp(optarg
,"ssd")) {
497 ataopts
.devstat_ssd_page
= true;
498 scsiopts
.smart_ss_media_log
= true;
499 } else if (!strcmp(optarg
,"scterc")) {
500 ataopts
.sct_erc_get
= true;
501 } else if (!strcmp(optarg
,"scttemp")) {
502 ataopts
.sct_temp_sts
= ataopts
.sct_temp_hist
= true;
503 } else if (!strcmp(optarg
,"scttempsts")) {
504 ataopts
.sct_temp_sts
= true;
505 } else if (!strcmp(optarg
,"scttemphist")) {
506 ataopts
.sct_temp_hist
= true;
508 } else if (!strncmp(optarg
, "scttempint,", sizeof("scstempint,")-1)) {
509 unsigned interval
= 0; int n1
= -1, n2
= -1, len
= strlen(optarg
);
510 if (!( sscanf(optarg
,"scttempint,%u%n,p%n", &interval
, &n1
, &n2
) == 1
511 && 0 < interval
&& interval
<= 0xffff && (n1
== len
|| n2
== len
))) {
512 snprintf(extraerror
, sizeof(extraerror
), "Option -l scttempint,N[,p] must have positive integer N\n");
515 ataopts
.sct_temp_int
= interval
;
516 ataopts
.sct_temp_int_pers
= (n2
== len
);
518 } else if (!strncmp(optarg
, "devstat", sizeof("devstat")-1)) {
519 int n1
= -1, n2
= -1, len
= strlen(optarg
);
521 sscanf(optarg
, "devstat%n,%u%n", &n1
, &val
, &n2
);
523 ataopts
.devstat_all_pages
= true;
525 if (n2
!= len
) // retry with hex
526 sscanf(optarg
, "devstat,0x%x%n", &val
, &n2
);
527 if (n2
== len
&& val
<= 0xff)
528 ataopts
.devstat_pages
.push_back(val
);
533 } else if (str_starts_with(optarg
, "defects")) {
534 int n1
= -1, n2
= -1, len
= strlen(optarg
);
536 sscanf(optarg
, "defects%n,%u%n", &n1
, &val
, &n2
);
538 ataopts
.pending_defects_log
= 31; // Entries of first page
539 else if (n2
== len
&& val
<= 0xffff * 32 - 1)
540 ataopts
.pending_defects_log
= val
;
544 } else if (!strncmp(optarg
, "xerror", sizeof("xerror")-1)) {
545 int n1
= -1, n2
= -1, len
= strlen(optarg
);
547 sscanf(optarg
, "xerror%n,error%n", &n1
, &n2
);
548 if (!(n1
== len
|| n2
== len
)) {
550 sscanf(optarg
, "xerror,%u%n,error%n", &val
, &n1
, &n2
);
552 if ((n1
== len
|| n2
== len
) && val
> 0) {
553 ataopts
.smart_ext_error_log
= val
;
554 ataopts
.retry_error_log
= (n2
== len
);
559 } else if (!strncmp(optarg
, "xselftest", sizeof("xselftest")-1)) {
560 int n1
= -1, n2
= -1, len
= strlen(optarg
);
562 sscanf(optarg
, "xselftest%n,selftest%n", &n1
, &n2
);
563 if (!(n1
== len
|| n2
== len
)) {
565 sscanf(optarg
, "xselftest,%u%n,selftest%n", &val
, &n1
, &n2
);
567 if ((n1
== len
|| n2
== len
) && val
> 0) {
568 ataopts
.smart_ext_selftest_log
= val
;
569 ataopts
.retry_selftest_log
= (n2
== len
);
574 } else if (!strncmp(optarg
, "scterc,", sizeof("scterc,")-1)) {
575 unsigned rt
= ~0, wt
= ~0; int n
= -1;
576 sscanf(optarg
,"scterc,%u,%u%n", &rt
, &wt
, &n
);
577 if (n
== (int)strlen(optarg
) && rt
<= 999 && wt
<= 999) {
578 ataopts
.sct_erc_set
= true;
579 ataopts
.sct_erc_readtime
= rt
;
580 ataopts
.sct_erc_writetime
= wt
;
583 snprintf(extraerror
, sizeof(extraerror
), "Option -l scterc,[READTIME,WRITETIME] syntax error\n");
586 } else if ( !strncmp(optarg
, "gplog," , sizeof("gplog," )-1)
587 || !strncmp(optarg
, "smartlog,", sizeof("smartlog,")-1)) {
588 unsigned logaddr
= ~0U; unsigned page
= 0, nsectors
= 1; char sign
= 0;
589 int n1
= -1, n2
= -1, n3
= -1, len
= strlen(optarg
);
590 sscanf(optarg
, "%*[a-z],0x%x%n,%u%n%c%u%n",
591 &logaddr
, &n1
, &page
, &n2
, &sign
, &nsectors
, &n3
);
592 if (len
> n2
&& n3
== -1 && !strcmp(optarg
+n2
, "-max")) {
593 nsectors
= ~0U; sign
= '+'; n3
= len
;
595 bool gpl
= (optarg
[0] == 'g');
596 const char * erropt
= (gpl
? "gplog" : "smartlog");
597 if (!( n1
== len
|| n2
== len
598 || (n3
== len
&& (sign
== '+' || sign
== '-')))) {
599 snprintf(extraerror
, sizeof(extraerror
), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] syntax error\n", erropt
);
602 else if (!( logaddr
<= 0xff && page
<= (gpl
? 0xffffU
: 0x00ffU
)
604 && (nsectors
<= (gpl
? 0xffffU
: 0xffU
) || nsectors
== ~0U)
605 && (sign
!= '-' || page
<= nsectors
) )) {
606 snprintf(extraerror
, sizeof(extraerror
), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] parameter out of range\n", erropt
);
611 req
.gpl
= gpl
; req
.logaddr
= logaddr
; req
.page
= page
;
612 req
.nsectors
= (sign
== '-' ? nsectors
-page
+1 : nsectors
);
613 ataopts
.log_requests
.push_back(req
);
617 else if (str_starts_with(optarg
, "nvmelog,")) {
618 int n
= -1, len
= strlen(optarg
);
619 unsigned page
= 0, size
= 0;
620 sscanf(optarg
, "nvmelog,0x%x,0x%x%n", &page
, &size
, &n
);
621 if (n
== len
&& page
<= 0xff && 0 < size
&& size
<= 0x4000) {
622 nvmeopts
.log_page
= page
; nvmeopts
.log_page_size
= size
;
633 ataopts
.drive_info
= scsiopts
.drive_info
= nvmeopts
.drive_info
= true;
637 ataopts
.identify_word_level
= ataopts
.identify_bit_level
= 0;
639 for (int i
= 0; optarg
[i
]; i
++) {
641 case 'w': ataopts
.identify_word_level
= 1; break;
642 case 'n': ataopts
.identify_bit_level
= -1; break;
643 case 'v': ataopts
.identify_bit_level
= 1; break;
644 case 'b': ataopts
.identify_bit_level
= 2; break;
645 default: badarg
= true;
652 ataopts
.drive_info
= scsiopts
.drive_info
= nvmeopts
.drive_info
= true;
653 ataopts
.smart_check_status
= scsiopts
.smart_check_status
= nvmeopts
.smart_check_status
= true;
654 ataopts
.smart_general_values
= nvmeopts
.drive_capabilities
= true;
655 ataopts
.smart_vendor_attrib
= scsiopts
.smart_vendor_attrib
= nvmeopts
.smart_vendor_attrib
= true;
656 ataopts
.smart_error_log
= scsiopts
.smart_error_log
= true;
657 nvmeopts
.error_log_entries
= 16;
658 ataopts
.smart_selftest_log
= scsiopts
.smart_selftest_log
= true;
659 ataopts
.smart_selective_selftest_log
= true;
660 /* scsiopts.smart_background_log = true; */
661 scsiopts
.smart_ss_media_log
= true;
664 ataopts
.drive_info
= scsiopts
.drive_info
= nvmeopts
.drive_info
= true;
665 ataopts
.smart_check_status
= scsiopts
.smart_check_status
= nvmeopts
.smart_check_status
= true;
666 ataopts
.smart_general_values
= nvmeopts
.drive_capabilities
= true;
667 ataopts
.smart_vendor_attrib
= scsiopts
.smart_vendor_attrib
= nvmeopts
.smart_vendor_attrib
= true;
668 ataopts
.smart_ext_error_log
= 8;
669 ataopts
.retry_error_log
= true;
670 nvmeopts
.error_log_entries
= 16;
671 ataopts
.smart_ext_selftest_log
= 25;
672 ataopts
.retry_selftest_log
= true;
673 scsiopts
.smart_error_log
= scsiopts
.smart_selftest_log
= true;
674 ataopts
.smart_selective_selftest_log
= true;
675 ataopts
.smart_logdir
= ataopts
.gp_logdir
= true;
676 ataopts
.sct_temp_sts
= ataopts
.sct_temp_hist
= true;
677 ataopts
.sct_erc_get
= true;
678 ataopts
.sct_wcache_reorder_get
= true;
679 ataopts
.devstat_all_pages
= true;
680 // ataopts.pending_defects_log = 31; // TODO: Add if no longer EXPERIMENTAL
681 ataopts
.pending_defects_info
= true; // TODO: Remove then
682 ataopts
.sataphy
= true;
683 ataopts
.get_set_used
= true;
684 ataopts
.get_aam
= ataopts
.get_apm
= true;
685 ataopts
.get_security
= true;
686 ataopts
.get_lookahead
= ataopts
.get_wcache
= true;
687 ataopts
.get_dsn
= true;
688 scsiopts
.get_rcd
= scsiopts
.get_wce
= true;
689 scsiopts
.smart_background_log
= true;
690 scsiopts
.smart_ss_media_log
= true;
691 scsiopts
.sasphy
= true;
692 if (!output_format_set
)
693 ataopts
.output_format
|= ata_print_options::FMT_BRIEF
;
696 // parse vendor-specific definitions of attributes
697 if (!strcmp(optarg
,"help")) {
698 printing_is_off
= false;
700 pout("The valid arguments to -v are:\n\thelp\n%s\n",
701 create_vendor_attribute_arg_list().c_str());
704 if (!parse_attribute_def(optarg
, ataopts
.attribute_defs
, PRIOR_USER
))
708 if (!strcmp(optarg
, "use")) {
709 ataopts
.ignore_presets
= false;
710 } else if (!strcmp(optarg
, "ignore")) {
711 ataopts
.ignore_presets
= true;
712 } else if (!strcmp(optarg
, "show")) {
713 ataopts
.show_presets
= true;
714 } else if (!strcmp(optarg
, "showall")) {
715 if (!init_drive_database(use_default_db
))
717 if (optind
< argc
) { // -P showall MODEL [FIRMWARE]
718 int cnt
= showmatchingpresets(argv
[optind
], (optind
+1<argc
? argv
[optind
+1] : NULL
));
719 EXIT(cnt
); // report #matches
721 if (showallpresets())
722 EXIT(FAILCMD
); // report regexp syntax error
729 if (!strcmp(optarg
,"offline")) {
731 ataopts
.smart_selftest_type
= OFFLINE_FULL_SCAN
;
732 scsiopts
.smart_default_selftest
= true;
733 } else if (!strcmp(optarg
,"short")) {
735 ataopts
.smart_selftest_type
= SHORT_SELF_TEST
;
736 scsiopts
.smart_short_selftest
= true;
737 } else if (!strcmp(optarg
,"long")) {
739 ataopts
.smart_selftest_type
= EXTEND_SELF_TEST
;
740 scsiopts
.smart_extend_selftest
= true;
741 } else if (!strcmp(optarg
,"conveyance")) {
743 ataopts
.smart_selftest_type
= CONVEYANCE_SELF_TEST
;
744 } else if (!strcmp(optarg
,"force")) {
745 ataopts
.smart_selftest_force
= true;
746 scsiopts
.smart_selftest_force
= true;
747 } else if (!strcmp(optarg
,"afterselect,on")) {
748 // scan remainder of disk after doing selected segment
749 ataopts
.smart_selective_args
.scan_after_select
= 2;
750 } else if (!strcmp(optarg
,"afterselect,off")) {
751 // don't scan remainder of disk after doing selected segments
752 ataopts
.smart_selective_args
.scan_after_select
= 1;
753 } else if (!strncmp(optarg
,"pending,",strlen("pending,"))) {
754 // parse number of minutes that test should be pending
758 i
=(int)strtol(optarg
+strlen("pending,"), &tailptr
, 10);
759 if (errno
|| *tailptr
!= '\0') {
760 snprintf(extraerror
, sizeof(extraerror
), "Option -t pending,N requires N to be a non-negative integer\n");
762 } else if (i
<0 || i
>65535) {
763 snprintf(extraerror
, sizeof(extraerror
), "Option -t pending,N (N=%d) must have 0 <= N <= 65535\n", i
);
766 ataopts
.smart_selective_args
.pending_time
= i
+1;
768 } else if (!strncmp(optarg
,"select",strlen("select"))) {
769 if (ataopts
.smart_selective_args
.num_spans
== 0)
771 // parse range of LBAs to test
772 uint64_t start
, stop
; int mode
;
773 if (split_selective_arg(optarg
, &start
, &stop
, &mode
)) {
774 snprintf(extraerror
, sizeof(extraerror
), "Option -t select,M-N must have non-negative integer M and N\n");
777 if (ataopts
.smart_selective_args
.num_spans
>= 5 || start
> stop
) {
779 snprintf(extraerror
, sizeof(extraerror
), "ERROR: Start LBA (%" PRIu64
") > ending LBA (%" PRId64
") in argument \"%s\"\n",
780 start
, stop
, optarg
);
782 snprintf(extraerror
, sizeof(extraerror
),"ERROR: No more than five selective self-test spans may be"
787 ataopts
.smart_selective_args
.span
[ataopts
.smart_selective_args
.num_spans
].start
= start
;
788 ataopts
.smart_selective_args
.span
[ataopts
.smart_selective_args
.num_spans
].end
= stop
;
789 ataopts
.smart_selective_args
.span
[ataopts
.smart_selective_args
.num_spans
].mode
= mode
;
790 ataopts
.smart_selective_args
.num_spans
++;
791 ataopts
.smart_selftest_type
= SELECTIVE_SELF_TEST
;
793 } else if (!strncmp(optarg
, "scttempint", sizeof("scstempint")-1)) {
794 snprintf(extraerror
, sizeof(extraerror
), "-t scttempint is no longer supported, use -l scttempint instead\n");
796 } else if (!strncmp(optarg
, "vendor,", sizeof("vendor,")-1)) {
797 unsigned subcmd
= ~0U; int n
= -1;
798 if (!( sscanf(optarg
, "%*[a-z],0x%x%n", &subcmd
, &n
) == 1
799 && subcmd
<= 0xff && n
== (int)strlen(optarg
))) {
800 snprintf(extraerror
, sizeof(extraerror
), "Option -t vendor,0xNN syntax error\n");
804 ataopts
.smart_selftest_type
= subcmd
;
814 scsiopts
.smart_selftest_abort
= true;
815 ataopts
.smart_selftest_type
= ABORT_SELF_TEST
;
818 // skip disk check if in low-power mode
819 if (!strcmp(optarg
, "never")) {
820 ataopts
.powermode
= 1; // do not skip, but print mode
823 int n1
= -1, n2
= -1, len
= strlen(optarg
);
824 char s
[7+1]; unsigned i
= FAILPOWER
;
825 sscanf(optarg
, "%9[a-z]%n,%u%n", s
, &n1
, &i
, &n2
);
826 if (!((n1
== len
|| n2
== len
) && i
<= 255))
828 else if (!strcmp(s
, "sleep"))
829 ataopts
.powermode
= 2;
830 else if (!strcmp(s
, "standby"))
831 ataopts
.powermode
= 3;
832 else if (!strcmp(s
, "idle"))
833 ataopts
.powermode
= 4;
836 ataopts
.powerexit
= i
;
840 if (!strcmp(optarg
, "old")) {
841 ataopts
.output_format
&= ~ata_print_options::FMT_BRIEF
;
842 output_format_set
= true;
844 else if (!strcmp(optarg
, "brief")) {
845 ataopts
.output_format
|= ata_print_options::FMT_BRIEF
;
846 output_format_set
= true;
848 else if (!strcmp(optarg
, "hex"))
849 ataopts
.output_format
|= ata_print_options::FMT_HEX_ID
850 | ata_print_options::FMT_HEX_VAL
;
851 else if (!strcmp(optarg
, "hex,id"))
852 ataopts
.output_format
|= ata_print_options::FMT_HEX_ID
;
853 else if (!strcmp(optarg
, "hex,val"))
854 ataopts
.output_format
|= ata_print_options::FMT_HEX_VAL
;
860 const char * path
= optarg
;
861 if (*path
== '+' && path
[1])
864 use_default_db
= false;
865 if (!read_drive_database(path
))
870 printing_is_off
= false;
877 case_s_continued
: // -s, see above
878 case opt_set
: // --set
880 ataopts
.get_set_used
= true;
881 bool get
= (optchar
== 'g');
882 char name
[16+1]; unsigned val
;
883 int n1
= -1, n2
= -1, n3
= -1, len
= strlen(optarg
);
884 if (sscanf(optarg
, "%16[^,=]%n%*[,=]%n%u%n", name
, &n1
, &n2
, &val
, &n3
) >= 1
885 && (n1
== len
|| (!get
&& n2
> 0))) {
889 bool persistent
= false;
892 int len2
= strlen(optarg
+ n2
);
893 char * tmp
= strstr(optarg
+n2
, ",p");
894 // handle ",p" in persistent options like: wcache-sct,[ata|on|off],p
895 if (tmp
&& (strlen(tmp
) == 2)) {
897 len2
= strlen(optarg
+n2
) - 2;
899 // the ,p option only works for set of SCT Feature Control command
900 if (strcmp(name
, "wcache-sct") != 0 &&
901 strcmp(name
, "wcreorder") != 0)
904 on
= !strncmp(optarg
+n2
, "on", len2
);
905 off
= !strncmp(optarg
+n2
, "off", len2
);
906 ata
= !strncmp(optarg
+n2
, "ata", len2
);
911 if (get
&& !strcmp(name
, "all")) {
912 ataopts
.get_aam
= ataopts
.get_apm
= true;
913 ataopts
.get_security
= true;
914 ataopts
.get_lookahead
= ataopts
.get_wcache
= true;
915 ataopts
.get_dsn
= true;
916 scsiopts
.get_rcd
= scsiopts
.get_wce
= true;
918 else if (!strcmp(name
, "aam")) {
920 ataopts
.get_aam
= true;
922 ataopts
.set_aam
= -1;
924 ataopts
.set_aam
= val
+ 1;
926 snprintf(extraerror
, sizeof(extraerror
), "Option -s aam,N must have 0 <= N <= 254\n");
930 else if (!strcmp(name
, "apm")) {
932 ataopts
.get_apm
= true;
934 ataopts
.set_apm
= -1;
935 else if (1 <= val
&& val
<= 254)
936 ataopts
.set_apm
= val
+ 1;
938 snprintf(extraerror
, sizeof(extraerror
), "Option -s apm,N must have 1 <= N <= 254\n");
942 else if (!strcmp(name
, "lookahead")) {
944 ataopts
.get_lookahead
= true;
947 ataopts
.set_lookahead
= -1;
949 ataopts
.set_lookahead
= 1;
953 else if (!strcmp(name
, "wcreorder")) {
954 ataopts
.sct_wcache_reorder_set_pers
= persistent
;
956 ataopts
.sct_wcache_reorder_get
= true;
959 ataopts
.sct_wcache_reorder_set
= -1;
961 ataopts
.sct_wcache_reorder_set
= 1;
965 else if (!strcmp(name
, "wcache-sct")) {
966 ataopts
.sct_wcache_sct_set_pers
= persistent
;
968 ataopts
.sct_wcache_sct_get
= true;
971 ataopts
.sct_wcache_sct_set
= 3;
973 ataopts
.sct_wcache_sct_set
= 2;
975 ataopts
.sct_wcache_sct_set
= 1;
979 else if (!strcmp(name
, "rcache")) {
981 scsiopts
.get_rcd
= true;
983 scsiopts
.set_rcd
= -1;
985 scsiopts
.set_rcd
= 1;
989 else if (get
&& !strcmp(name
, "security")) {
990 ataopts
.get_security
= true;
992 else if (!get
&& !strcmp(optarg
, "security-freeze")) {
993 ataopts
.set_security_freeze
= true;
995 else if (!get
&& !strcmp(optarg
, "standby,now")) {
996 ataopts
.set_standby_now
= true;
998 else if (!get
&& !strcmp(name
, "standby")) {
1000 ataopts
.set_standby
= 0 + 1;
1001 else if (val
<= 255)
1002 ataopts
.set_standby
= val
+ 1;
1004 snprintf(extraerror
, sizeof(extraerror
), "Option -s standby,N must have 0 <= N <= 255\n");
1008 else if (!strcmp(name
, "wcache")) {
1010 ataopts
.get_wcache
= true;
1011 scsiopts
.get_wce
= true;
1014 ataopts
.set_wcache
= -1;
1015 scsiopts
.set_wce
= -1;
1018 ataopts
.set_wcache
= 1;
1019 scsiopts
.set_wce
= 1;
1024 else if (!strcmp(name
, "dsn")) {
1026 ataopts
.get_dsn
= true;
1029 ataopts
.set_dsn
= -1;
1032 ataopts
.set_dsn
= 1;
1052 printing_is_off
= false;
1054 // Point arg to the argument in which this option was found.
1055 arg
= argv
[optind
-1];
1056 // Check whether the option is a long option that doesn't map to -h.
1057 if (arg
[1] == '-' && optchar
!= 'h') {
1058 // Iff optopt holds a valid option then argument must be missing.
1059 if (optopt
&& (optopt
>= opt_scan
|| strchr(shortopts
, optopt
))) {
1060 pout("=======> ARGUMENT REQUIRED FOR OPTION: %s\n", arg
+2);
1061 printvalidarglistmessage(optopt
);
1063 pout("=======> UNRECOGNIZED OPTION: %s\n",arg
+2);
1065 pout("=======> %s", extraerror
);
1069 if (0 < optopt
&& optopt
< '~') {
1070 // Iff optopt holds a valid option then argument must be
1071 // missing. Note (BA) this logic seems to fail using Solaris
1073 if (strchr(shortopts
, optopt
) != NULL
) {
1074 pout("=======> ARGUMENT REQUIRED FOR OPTION: %c\n", optopt
);
1075 printvalidarglistmessage(optopt
);
1077 pout("=======> UNRECOGNIZED OPTION: %c\n",optopt
);
1079 pout("=======> %s", extraerror
);
1085 } // closes switch statement to process command-line options
1087 // Check to see if option had an unrecognized or incorrect argument.
1090 // It would be nice to print the actual option name given by the user
1091 // here, but we just print the short form. Please fix this if you know
1092 // a clean way to do it.
1093 char optstr
[] = { (char)optchar
, 0 };
1094 pout("=======> INVALID ARGUMENT TO -%s: %s\n",
1095 (optchar
== opt_identify
? "-identify" :
1096 optchar
== opt_set
? "-set" :
1097 optchar
== opt_smart
? "-smart" : optstr
), optarg
);
1098 printvalidarglistmessage(optchar
);
1100 pout("=======> %s", extraerror
);
1106 // Special handling of --scan, --scanopen
1108 // Read or init drive database to allow USB ID check.
1109 if (!init_drive_database(use_default_db
))
1111 scan_devices(scan_types
, (scan
== opt_scan_open
), argv
+ optind
);
1115 // At this point we have processed all command-line options. If the
1116 // print output is switchable, then start with the print output
1118 if (printing_is_switchable
)
1119 printing_is_off
= true;
1121 // Check for multiple -d TYPE options
1122 if (scan_types
.size() > 1) {
1123 printing_is_off
= false;
1125 pout("ERROR: multiple -d TYPE options are only allowed with --scan\n");
1130 // error message if user has asked for more than one test
1132 printing_is_off
= false;
1134 pout("\nERROR: smartctl can only run a single test type (or abort) at a time.\n");
1139 // error message if user has set selective self-test options without
1140 // asking for a selective self-test
1141 if ( (ataopts
.smart_selective_args
.pending_time
|| ataopts
.smart_selective_args
.scan_after_select
)
1142 && !ataopts
.smart_selective_args
.num_spans
) {
1143 printing_is_off
= false;
1145 if (ataopts
.smart_selective_args
.pending_time
)
1146 pout("\nERROR: smartctl -t pending,N must be used with -t select,N-M.\n");
1148 pout("\nERROR: smartctl -t afterselect,(on|off) must be used with -t select,N-M.\n");
1153 // If captive option was used, change test type if appropriate.
1155 switch (ataopts
.smart_selftest_type
) {
1156 case SHORT_SELF_TEST
:
1157 ataopts
.smart_selftest_type
= SHORT_CAPTIVE_SELF_TEST
;
1158 scsiopts
.smart_short_selftest
= false;
1159 scsiopts
.smart_short_cap_selftest
= true;
1161 case EXTEND_SELF_TEST
:
1162 ataopts
.smart_selftest_type
= EXTEND_CAPTIVE_SELF_TEST
;
1163 scsiopts
.smart_extend_selftest
= false;
1164 scsiopts
.smart_extend_cap_selftest
= true;
1166 case CONVEYANCE_SELF_TEST
:
1167 ataopts
.smart_selftest_type
= CONVEYANCE_CAPTIVE_SELF_TEST
;
1169 case SELECTIVE_SELF_TEST
:
1170 ataopts
.smart_selftest_type
= SELECTIVE_CAPTIVE_SELF_TEST
;
1174 // From here on, normal operations...
1177 // Warn if the user has provided no device name
1179 pout("ERROR: smartctl requires a device name as the final command-line argument.\n\n");
1184 // Warn if the user has provided more than one device name
1187 pout("ERROR: smartctl takes ONE device name as the final command-line argument.\n");
1188 pout("You have provided %d device names:\n",argc
-optind
);
1189 for (i
=0; i
<argc
-optind
; i
++)
1190 pout("%s\n",argv
[optind
+i
]);
1195 // Read or init drive database
1196 if (!init_drive_database(use_default_db
))
1202 // Printing function (controlled by global printing_is_off)
1203 // [From GLIBC Manual: Since the prototype doesn't specify types for
1204 // optional arguments, in a call to a variadic function the default
1205 // argument promotions are performed on the optional argument
1206 // values. This means the objects of type char or short int (whether
1207 // signed or not) are promoted to either int or unsigned int, as
1209 void pout(const char *fmt
, ...){
1212 // initialize variable argument list
1214 if (printing_is_off
) {
1226 // Globals to set failuretest() policy
1227 bool failuretest_conservative
= false;
1228 unsigned char failuretest_permissive
= 0;
1230 // Compares failure type to policy in effect, and either exits or
1231 // simply returns to the calling routine.
1232 // Used in ataprint.cpp and scsiprint.cpp.
1233 void failuretest(failure_type type
, int returnvalue
)
1235 // If this is an error in an "optional" SMART command
1236 if (type
== OPTIONAL_CMD
) {
1237 if (!failuretest_conservative
)
1239 pout("An optional SMART command failed: exiting. Remove '-T conservative' option to continue.\n");
1243 // If this is an error in a "mandatory" SMART command
1244 if (type
== MANDATORY_CMD
) {
1245 if (failuretest_permissive
--)
1247 pout("A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n");
1251 throw std::logic_error("failuretest: Unknown type");
1254 // Used to warn users about invalid checksums. Called from atacmds.cpp.
1255 // Action to be taken may be altered by the user.
1256 void checksumwarning(const char * string
)
1258 // user has asked us to ignore checksum errors
1259 if (checksum_err_mode
== CHECKSUM_ERR_IGNORE
)
1262 pout("Warning! %s error: invalid SMART checksum.\n", string
);
1264 // user has asked us to fail on checksum errors
1265 if (checksum_err_mode
== CHECKSUM_ERR_EXIT
)
1269 // Return info string about device protocol
1270 static const char * get_protocol_info(const smart_device
* dev
)
1272 switch ( (int)dev
->is_ata()
1273 | ((int)dev
->is_scsi() << 1)
1274 | ((int)dev
->is_nvme() << 2)) {
1275 case 0x1: return "ATA";
1276 case 0x2: return "SCSI";
1277 case 0x3: return "ATA+SCSI";
1278 case 0x4: return "NVMe";
1279 default: return "Unknown";
1284 // smartctl [-d type] --scan[-open] -- [PATTERN] [smartd directive ...]
1285 void scan_devices(const smart_devtype_list
& types
, bool with_open
, char ** argv
)
1287 bool dont_print
= !(ata_debugmode
|| scsi_debugmode
|| nvme_debugmode
);
1289 const char * pattern
= 0;
1291 if (argv
[ai
] && argv
[ai
][0] != '-')
1292 pattern
= argv
[ai
++];
1294 smart_device_list devlist
;
1295 printing_is_off
= dont_print
;
1296 bool ok
= smi()->scan_smart_devices(devlist
, types
, pattern
);
1297 printing_is_off
= false;
1300 pout("# scan_smart_devices: %s\n", smi()->get_errmsg());
1304 for (unsigned i
= 0; i
< devlist
.size(); i
++) {
1305 smart_device_auto_ptr
dev( devlist
.release(i
) );
1308 printing_is_off
= dont_print
;
1309 dev
.replace ( dev
->autodetect_open() );
1310 printing_is_off
= false;
1312 if (!dev
->is_open()) {
1313 pout("# %s -d %s # %s, %s device open failed: %s\n", dev
->get_dev_name(),
1314 dev
->get_dev_type(), dev
->get_info_name(),
1315 get_protocol_info(dev
.get()), dev
->get_errmsg());
1320 pout("%s -d %s", dev
->get_dev_name(), dev
->get_dev_type());
1322 pout(" # %s, %s device\n", dev
->get_info_name(), get_protocol_info(dev
.get()));
1324 for (int j
= ai
; argv
[j
]; j
++)
1325 pout(" %s", argv
[j
]);
1334 // Main program without exception handling
1335 static int main_worker(int argc
, char **argv
)
1337 // Throw if runtime environment does not match compile time test.
1340 // Initialize interface
1341 smart_interface::init();
1345 // Parse input arguments
1346 ata_print_options ataopts
;
1347 scsi_print_options scsiopts
;
1348 nvme_print_options nvmeopts
;
1349 bool print_type_only
= false;
1350 const char * type
= parse_options(argc
, argv
, ataopts
, scsiopts
, nvmeopts
, print_type_only
);
1352 const char * name
= argv
[argc
-1];
1354 smart_device_auto_ptr dev
;
1355 if (!strcmp(name
,"-")) {
1356 // Parse "smartctl -r ataioctl,2 ..." output from stdin
1357 if (type
|| print_type_only
) {
1358 pout("-d option is not allowed in conjunction with device name \"-\".\n");
1362 dev
= get_parsed_ata_device(smi(), name
);
1365 // get device of appropriate type
1366 dev
= smi()->get_smart_device(name
, type
);
1369 pout("%s: %s\n", name
, smi()->get_errmsg());
1371 printvalidarglistmessage('d');
1373 pout("Please specify device type with the -d option.\n");
1378 if (print_type_only
)
1379 // Report result of first autodetection
1380 pout("%s: Device of type '%s' [%s] detected\n",
1381 dev
->get_info_name(), dev
->get_dev_type(), get_protocol_info(dev
.get()));
1383 if (dev
->is_ata() && ataopts
.powermode
>=2 && dev
->is_powered_down()) {
1384 pout("Device is in STANDBY (OS) mode, exit(%d)\n", ataopts
.powerexit
);
1385 return ataopts
.powerexit
;
1391 smart_device::device_info oldinfo
= dev
->get_info();
1393 // Open with autodetect support, may return 'better' device
1394 dev
.replace( dev
->autodetect_open() );
1396 // Report if type has changed
1397 if ( (ata_debugmode
|| scsi_debugmode
|| nvme_debugmode
|| print_type_only
)
1398 && oldinfo
.dev_type
!= dev
->get_dev_type() )
1399 pout("%s: Device open changed type from '%s' to '%s'\n",
1400 dev
->get_info_name(), oldinfo
.dev_type
.c_str(), dev
->get_dev_type());
1402 if (!dev
->is_open()) {
1403 pout("Smartctl open device: %s failed: %s\n", dev
->get_info_name(), dev
->get_errmsg());
1407 // now call appropriate ATA or SCSI routine
1409 if (print_type_only
)
1410 pout("%s: Device of type '%s' [%s] opened\n",
1411 dev
->get_info_name(), dev
->get_dev_type(), get_protocol_info(dev
.get()));
1412 else if (dev
->is_ata())
1413 retval
= ataPrintMain(dev
->to_ata(), ataopts
);
1414 else if (dev
->is_scsi())
1415 retval
= scsiPrintMain(dev
->to_scsi(), scsiopts
);
1416 else if (dev
->is_nvme())
1417 retval
= nvmePrintMain(dev
->to_nvme(), nvmeopts
);
1419 // we should never fall into this branch!
1420 pout("%s: Neither ATA, SCSI nor NVMe device\n", dev
->get_info_name());
1428 int main(int argc
, char **argv
)
1431 bool badcode
= false;
1434 // Do the real work ...
1435 status
= main_worker(argc
, argv
);
1438 // EXIT(status) arrives here
1441 catch (const std::bad_alloc
& /*ex*/) {
1442 // Memory allocation failed (also thrown by std::operator new)
1443 printf("Smartctl: Out of memory\n");
1446 catch (const std::exception
& ex
) {
1447 // Other fatal errors
1448 printf("Smartctl: Exception: %s\n", ex
.what());
1453 // Check for remaining device objects
1454 if (smart_device::get_num_objects() != 0) {
1455 printf("Smartctl: Internal Error: %d device object(s) left at exit.\n",
1456 smart_device::get_num_objects());
1462 printf("Please inform " PACKAGE_BUGREPORT
", including output of smartctl -V.\n");