4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2002-10 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2008-10 Christian Franke <smartmontools-support@lists.sourceforge.net>
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, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * This code was originally developed as a Senior Thesis by Michael Cornwell
20 * at the Concurrent Systems Laboratory (now part of the Storage Systems
21 * Research Center), Jack Baskin School of Engineering, University of
22 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
28 #include <sys/types.h>
40 #if defined(__FreeBSD__)
41 #include <sys/param.h>
44 #if defined(__QNXNTO__)
45 #include <new> // TODO: Why is this include necessary on QNX ?
50 #include "dev_interface.h"
53 #include "knowndrives.h"
55 #include "scsiprint.h"
59 const char * smartctl_cpp_cvsid
= "$Id: smartctl.cpp 3065 2010-02-10 22:16:50Z chrfranke $"
60 CONFIG_H_CVSID EXTERN_H_CVSID SMARTCTL_H_CVSID
;
62 // This is a block containing all the "control variables". We declare
63 // this globally in this file, and externally in other files.
64 smartmonctrl
*con
=NULL
;
66 static void printslogan()
68 pout("%s\n", format_version_info("smartctl").c_str());
72 pout("\nUse smartctl -h to get a usage summary\n\n");
76 static std::string
getvalidarglist(char opt
);
78 /* void prints help information for command syntax */
80 printf("Usage: smartctl [options] device\n\n");
82 "============================================ SHOW INFORMATION OPTIONS =====\n\n"
83 " -h, --help, --usage\n"
84 " Display this help and exit\n\n"
85 " -V, --version, --copyright, --license\n"
86 " Print license, copyright, and version information and exit\n\n"
88 " Show identity information for device\n\n"
90 " Show all SMART information for device\n\n"
92 " Show all information for device\n\n"
95 "================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n"
96 " -q TYPE, --quietmode=TYPE (ATA)\n"
97 " Set smartctl quiet mode to one of: errorsonly, silent, noserial\n\n"
98 " -d TYPE, --device=TYPE\n"
99 " Specify device type to one of: %s\n\n"
100 " -T TYPE, --tolerance=TYPE (ATA)\n"
101 " Tolerance: normal, conservative, permissive, verypermissive\n\n"
102 " -b TYPE, --badsum=TYPE (ATA)\n"
103 " Set action on bad checksum to one of: warn, exit, ignore\n\n"
104 " -r TYPE, --report=TYPE\n"
105 " Report transactions (see man page)\n\n"
106 " -n MODE, --nocheck=MODE (ATA)\n"
107 " No check if: never, sleep, standby, idle (see man page)\n\n",
108 getvalidarglist('d').c_str()); // TODO: Use this function also for other options ?
110 "============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n"
111 " -s VALUE, --smart=VALUE\n"
112 " Enable/disable SMART on device (on/off)\n\n"
113 " -o VALUE, --offlineauto=VALUE (ATA)\n"
114 " Enable/disable automatic offline testing on device (on/off)\n\n"
115 " -S VALUE, --saveauto=VALUE (ATA)\n"
116 " Enable/disable Attribute autosave on device (on/off)\n\n"
119 "======================================= READ AND DISPLAY DATA OPTIONS =====\n\n"
121 " Show device SMART health status\n\n"
122 " -c, --capabilities (ATA)\n"
123 " Show device SMART capabilities\n\n"
124 " -A, --attributes \n"
125 " Show device SMART vendor-specific Attributes and values\n\n"
126 " -l TYPE, --log=TYPE\n"
127 " Show device log. TYPE: error, selftest, selective, directory[,g|s],\n"
128 " background, sasphy[,reset], sataphy[,reset],\n"
129 " scttemp[sts,hist], scterc[,N,M],\n"
130 " gplog,N[,RANGE], smartlog,N[,RANGE],\n"
131 " xerror[,N][,error], xselftest[,N][,selftest]\n\n"
132 " -v N,OPTION , --vendorattribute=N,OPTION (ATA)\n"
133 " Set display OPTION for vendor Attribute N (see man page)\n\n"
134 " -F TYPE, --firmwarebug=TYPE (ATA)\n"
135 " Use firmware bug workaround: none, samsung, samsung2,\n"
136 " samsung3, swapid\n\n"
137 " -P TYPE, --presets=TYPE (ATA)\n"
138 " Drive-specific presets: use, ignore, show, showall\n\n"
139 " -B [+]FILE, --drivedb=[+]FILE (ATA)\n"
140 " Read and replace [add] drive database from FILE\n"
141 #ifdef SMARTMONTOOLS_DRIVEDBDIR
142 " [default is "SMARTMONTOOLS_DRIVEDBDIR
"/drivedb.h]\n"
147 "============================================ DEVICE SELF-TEST OPTIONS =====\n\n"
148 " -t TEST, --test=TEST\n"
149 " Run test. TEST: offline short long conveyance select,M-N\n"
150 " pending,N afterselect,[on|off] scttempint,N[,p]\n\n"
152 " Do test in captive mode (along with -t)\n\n"
154 " Abort any non-captive test on device\n\n"
156 std::string examples
= smi()->get_app_examples("smartctl");
157 if (!examples
.empty())
158 printf("%s\n", examples
.c_str());
161 /* Returns a string containing a formatted list of the valid arguments
162 to the option opt or empty on failure. Note 'v' case different */
163 static std::string
getvalidarglist(char opt
)
167 return "errorsonly, silent, noserial";
169 return smi()->get_valid_dev_types_str() + ", test";
171 return "normal, conservative, permissive, verypermissive";
173 return "warn, exit, ignore";
175 return "ioctl[,N], ataioctl[,N], scsiioctl[,N]";
181 return "error, selftest, selective, directory[,g|s], background, scttemp[sts|hist], scterc[,N,M], "
182 "sasphy[,reset], sataphy[,reset], gplog,N[,RANGE], smartlog,N[,RANGE], "
183 "xerror[,N][,error], xselftest[,N][,selftest]";
185 return "use, ignore, show, showall";
187 return "offline, short, long, conveyance, select,M-N, pending,N, afterselect,[on|off], scttempint,N[,p]";
189 return "none, samsung, samsung2, samsung3, swapid";
191 return "never, sleep, standby, idle";
198 /* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where
199 <LIST> is the list of valid arguments for option opt. */
200 void printvalidarglistmessage(char opt
) {
203 pout("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n",
204 create_vendor_attribute_arg_list().c_str());
207 // getvalidarglist() might produce a multiline or single line string. We
208 // need to figure out which to get the formatting right.
209 std::string s
= getvalidarglist(opt
);
210 char separator
= strchr(s
.c_str(), '\n') ? '\n' : ' ';
211 pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator
, s
.c_str(), separator
);
217 // Checksum error mode
218 enum checksum_err_mode_t
{
219 CHECKSUM_ERR_WARN
, CHECKSUM_ERR_EXIT
, CHECKSUM_ERR_IGNORE
222 static checksum_err_mode_t checksum_err_mode
= CHECKSUM_ERR_WARN
;
224 /* Takes command options and sets features to be run */
225 const char * parse_options(int argc
, char** argv
,
226 ata_print_options
& ataopts
,
227 scsi_print_options
& scsiopts
)
229 // Please update getvalidarglist() if you edit shortopts
230 const char *shortopts
= "h?Vq:d:T:b:r:s:o:S:HcAl:iaxv:P:t:CXF:n:B:";
231 // Please update getvalidarglist() if you edit longopts
232 struct option longopts
[] = {
233 { "help", no_argument
, 0, 'h' },
234 { "usage", no_argument
, 0, 'h' },
235 { "version", no_argument
, 0, 'V' },
236 { "copyright", no_argument
, 0, 'V' },
237 { "license", no_argument
, 0, 'V' },
238 { "quietmode", required_argument
, 0, 'q' },
239 { "device", required_argument
, 0, 'd' },
240 { "tolerance", required_argument
, 0, 'T' },
241 { "badsum", required_argument
, 0, 'b' },
242 { "report", required_argument
, 0, 'r' },
243 { "smart", required_argument
, 0, 's' },
244 { "offlineauto", required_argument
, 0, 'o' },
245 { "saveauto", required_argument
, 0, 'S' },
246 { "health", no_argument
, 0, 'H' },
247 { "capabilities", no_argument
, 0, 'c' },
248 { "attributes", no_argument
, 0, 'A' },
249 { "log", required_argument
, 0, 'l' },
250 { "info", no_argument
, 0, 'i' },
251 { "all", no_argument
, 0, 'a' },
252 { "xall", no_argument
, 0, 'x' },
253 { "vendorattribute", required_argument
, 0, 'v' },
254 { "presets", required_argument
, 0, 'P' },
255 { "test", required_argument
, 0, 't' },
256 { "captive", no_argument
, 0, 'C' },
257 { "abort", no_argument
, 0, 'X' },
258 { "firmwarebug", required_argument
, 0, 'F' },
259 { "nocheck", required_argument
, 0, 'n' },
260 { "drivedb", required_argument
, 0, 'B' },
264 char extraerror
[256];
265 memset(extraerror
, 0, sizeof(extraerror
));
266 memset(con
,0,sizeof(*con
));
269 const char * type
= 0; // set to -d optarg
270 bool no_defaultdb
= false; // set true on '-B FILE'
271 bool badarg
= false, captive
= false;
272 int testcnt
= 0; // number of self-tests requested
277 // This miserable construction is needed to get emacs to do proper indenting. Sorry!
278 while (-1 != (optchar
=
279 getopt_long(argc
, argv
, shortopts
, longopts
, NULL
)
283 con
->dont_print
= false;
284 pout("%s", format_version_info("smartctl", true /*full*/).c_str());
288 if (!strcmp(optarg
,"errorsonly")) {
289 con
->printing_switchable
= true;
290 con
->dont_print
= false;
291 } else if (!strcmp(optarg
,"silent")) {
292 con
->printing_switchable
= false;
293 con
->dont_print
= true;
294 } else if (!strcmp(optarg
,"noserial")) {
295 con
->dont_print_serial
= true;
304 if (!strcmp(optarg
,"normal")) {
305 con
->conservative
= false;
307 } else if (!strcmp(optarg
,"conservative")) {
308 con
->conservative
= true;
309 } else if (!strcmp(optarg
,"permissive")) {
310 if (con
->permissive
<0xff)
312 } else if (!strcmp(optarg
,"verypermissive")) {
313 con
->permissive
= 0xff;
319 if (!strcmp(optarg
,"warn")) {
320 checksum_err_mode
= CHECKSUM_ERR_WARN
;
321 } else if (!strcmp(optarg
,"exit")) {
322 checksum_err_mode
= CHECKSUM_ERR_EXIT
;
323 } else if (!strcmp(optarg
,"ignore")) {
324 checksum_err_mode
= CHECKSUM_ERR_IGNORE
;
334 // split_report_arg() may modify its first argument string, so use a
335 // copy of optarg in case we want optarg for an error message.
336 if (!(s
= strdup(optarg
))) {
337 throw std::bad_alloc();
339 if (split_report_arg(s
, &i
)) {
341 } else if (!strcmp(s
,"ioctl")) {
342 con
->reportataioctl
= con
->reportscsiioctl
= i
;
343 } else if (!strcmp(s
,"ataioctl")) {
344 con
->reportataioctl
= i
;
345 } else if (!strcmp(s
,"scsiioctl")) {
346 con
->reportscsiioctl
= i
;
354 if (!strcmp(optarg
,"on")) {
355 ataopts
.smart_enable
= scsiopts
.smart_enable
= true;
356 ataopts
.smart_disable
= scsiopts
.smart_disable
= false;
357 } else if (!strcmp(optarg
,"off")) {
358 ataopts
.smart_disable
= scsiopts
.smart_disable
= true;
359 ataopts
.smart_enable
= scsiopts
.smart_enable
= false;
365 if (!strcmp(optarg
,"on")) {
366 ataopts
.smart_auto_offl_enable
= true;
367 ataopts
.smart_auto_offl_disable
= false;
368 } else if (!strcmp(optarg
,"off")) {
369 ataopts
.smart_auto_offl_disable
= true;
370 ataopts
.smart_auto_offl_enable
= false;
376 if (!strcmp(optarg
,"on")) {
377 ataopts
.smart_auto_save_enable
= scsiopts
.smart_auto_save_enable
= true;
378 ataopts
.smart_auto_save_disable
= scsiopts
.smart_auto_save_disable
= false;
379 } else if (!strcmp(optarg
,"off")) {
380 ataopts
.smart_auto_save_disable
= scsiopts
.smart_auto_save_disable
= true;
381 ataopts
.smart_auto_save_enable
= scsiopts
.smart_auto_save_enable
= false;
387 ataopts
.smart_check_status
= scsiopts
.smart_check_status
= true;
390 if (!strcmp(optarg
,"none")) {
391 ataopts
.fix_firmwarebug
= FIX_NONE
;
392 } else if (!strcmp(optarg
,"samsung")) {
393 ataopts
.fix_firmwarebug
= FIX_SAMSUNG
;
394 } else if (!strcmp(optarg
,"samsung2")) {
395 ataopts
.fix_firmwarebug
= FIX_SAMSUNG2
;
396 } else if (!strcmp(optarg
,"samsung3")) {
397 ataopts
.fix_firmwarebug
= FIX_SAMSUNG3
;
398 } else if (!strcmp(optarg
,"swapid")) {
399 ataopts
.fix_swapped_id
= true;
405 ataopts
.smart_general_values
= true;
408 ataopts
.smart_vendor_attrib
= scsiopts
.smart_vendor_attrib
= true;
411 if (!strcmp(optarg
,"error")) {
412 ataopts
.smart_error_log
= scsiopts
.smart_error_log
= true;
413 } else if (!strcmp(optarg
,"selftest")) {
414 ataopts
.smart_selftest_log
= scsiopts
.smart_selftest_log
= true;
415 } else if (!strcmp(optarg
, "selective")) {
416 ataopts
.smart_selective_selftest_log
= true;
417 } else if (!strcmp(optarg
,"directory")) {
418 ataopts
.smart_logdir
= ataopts
.gp_logdir
= true; // SMART+GPL
419 } else if (!strcmp(optarg
,"directory,s")) {
420 ataopts
.smart_logdir
= true; // SMART
421 } else if (!strcmp(optarg
,"directory,g")) {
422 ataopts
.gp_logdir
= true; // GPL
423 } else if (!strcmp(optarg
,"sasphy")) {
424 scsiopts
.sasphy
= true;
425 } else if (!strcmp(optarg
,"sasphy,reset")) {
426 scsiopts
.sasphy
= scsiopts
.sasphy_reset
= true;
427 } else if (!strcmp(optarg
,"sataphy")) {
428 ataopts
.sataphy
= true;
429 } else if (!strcmp(optarg
,"sataphy,reset")) {
430 ataopts
.sataphy
= ataopts
.sataphy_reset
= true;
431 } else if (!strcmp(optarg
,"background")) {
432 scsiopts
.smart_background_log
= true;
433 } else if (!strcmp(optarg
,"scterc")) {
434 ataopts
.sct_erc_get
= true;
435 } else if (!strcmp(optarg
,"scttemp")) {
436 ataopts
.sct_temp_sts
= ataopts
.sct_temp_hist
= true;
437 } else if (!strcmp(optarg
,"scttempsts")) {
438 ataopts
.sct_temp_sts
= true;
439 } else if (!strcmp(optarg
,"scttemphist")) {
440 ataopts
.sct_temp_hist
= true;
442 } else if (!strncmp(optarg
, "xerror", sizeof("xerror")-1)) {
443 int n1
= -1, n2
= -1, len
= strlen(optarg
);
445 sscanf(optarg
, "xerror%n,error%n", &n1
, &n2
);
446 if (!(n1
== len
|| n2
== len
)) {
448 sscanf(optarg
, "xerror,%u%n,error%n", &val
, &n1
, &n2
);
450 if ((n1
== len
|| n2
== len
) && val
> 0) {
451 ataopts
.smart_ext_error_log
= val
;
452 ataopts
.retry_error_log
= (n2
== len
);
457 } else if (!strncmp(optarg
, "xselftest", sizeof("xselftest")-1)) {
458 int n1
= -1, n2
= -1, len
= strlen(optarg
);
460 sscanf(optarg
, "xselftest%n,selftest%n", &n1
, &n2
);
461 if (!(n1
== len
|| n2
== len
)) {
463 sscanf(optarg
, "xselftest,%u%n,selftest%n", &val
, &n1
, &n2
);
465 if ((n1
== len
|| n2
== len
) && val
> 0) {
466 ataopts
.smart_ext_selftest_log
= val
;
467 ataopts
.retry_selftest_log
= (n2
== len
);
472 } else if (!strncmp(optarg
, "scterc,", sizeof("scterc,")-1)) {
473 unsigned rt
= ~0, wt
= ~0; int n
= -1;
474 sscanf(optarg
,"scterc,%u,%u%n", &rt
, &wt
, &n
);
475 if (n
== (int)strlen(optarg
) && rt
<= 999 && wt
<= 999) {
476 ataopts
.sct_erc_set
= true;
477 ataopts
.sct_erc_readtime
= rt
;
478 ataopts
.sct_erc_writetime
= wt
;
481 sprintf(extraerror
, "Option -l scterc,[READTIME,WRITETIME] syntax error\n");
484 } else if ( !strncmp(optarg
, "gplog," , sizeof("gplog," )-1)
485 || !strncmp(optarg
, "smartlog,", sizeof("smartlog,")-1)) {
486 unsigned logaddr
= ~0U; unsigned page
= 0, nsectors
= 1; char sign
= 0;
487 int n1
= -1, n2
= -1, n3
= -1, len
= strlen(optarg
);
488 sscanf(optarg
, "%*[a-z],0x%x%n,%u%n%c%u%n",
489 &logaddr
, &n1
, &page
, &n2
, &sign
, &nsectors
, &n3
);
490 if (len
> n2
&& n3
== -1 && !strcmp(optarg
+n2
, "-max")) {
491 nsectors
= ~0U; sign
= '+'; n3
= len
;
493 bool gpl
= (optarg
[0] == 'g');
494 const char * erropt
= (gpl
? "gplog" : "smartlog");
495 if (!( n1
== len
|| n2
== len
496 || (n3
== len
&& (sign
== '+' || sign
== '-')))) {
497 sprintf(extraerror
, "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] syntax error\n", erropt
);
500 else if (!( logaddr
<= 0xff && page
<= (gpl
? 0xffffU
: 0x00ffU
)
502 && (nsectors
<= (gpl
? 0xffffU
: 0xffU
) || nsectors
== ~0U)
503 && (sign
!= '-' || page
<= nsectors
) )) {
504 sprintf(extraerror
, "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] parameter out of range\n", erropt
);
509 req
.gpl
= gpl
; req
.logaddr
= logaddr
; req
.page
= page
;
510 req
.nsectors
= (sign
== '-' ? nsectors
-page
+1 : nsectors
);
511 ataopts
.log_requests
.push_back(req
);
518 ataopts
.drive_info
= scsiopts
.drive_info
= true;
521 ataopts
.drive_info
= scsiopts
.drive_info
= true;
522 ataopts
.smart_check_status
= scsiopts
.smart_check_status
= true;
523 ataopts
.smart_general_values
= true;
524 ataopts
.smart_vendor_attrib
= scsiopts
.smart_vendor_attrib
= true;
525 ataopts
.smart_error_log
= scsiopts
.smart_error_log
= true;
526 ataopts
.smart_selftest_log
= scsiopts
.smart_selftest_log
= true;
527 ataopts
.smart_selective_selftest_log
= true;
528 /* scsiopts.smart_background_log = true; */
531 ataopts
.drive_info
= scsiopts
.drive_info
= true;
532 ataopts
.smart_check_status
= scsiopts
.smart_check_status
= true;
533 ataopts
.smart_general_values
= true;
534 ataopts
.smart_vendor_attrib
= scsiopts
.smart_vendor_attrib
= true;
535 ataopts
.smart_ext_error_log
= 8;
536 ataopts
.retry_error_log
= true;
537 ataopts
.smart_ext_selftest_log
= 25;
538 ataopts
.retry_selftest_log
= true;
539 scsiopts
.smart_error_log
= scsiopts
.smart_selftest_log
= true;
540 ataopts
.smart_selective_selftest_log
= true;
541 ataopts
.smart_logdir
= ataopts
.gp_logdir
= true;
542 ataopts
.sct_temp_sts
= ataopts
.sct_temp_hist
= true;
543 ataopts
.sct_erc_get
= true;
544 ataopts
.sataphy
= true;
545 scsiopts
.smart_background_log
= true;
546 scsiopts
.sasphy
= true;
549 // parse vendor-specific definitions of attributes
550 if (!strcmp(optarg
,"help")) {
551 con
->dont_print
= false;
553 pout("The valid arguments to -v are:\n\thelp\n%s\n",
554 create_vendor_attribute_arg_list().c_str());
557 if (!parse_attribute_def(optarg
, ataopts
.attribute_defs
, PRIOR_USER
))
561 if (!strcmp(optarg
, "use")) {
562 ataopts
.ignore_presets
= false;
563 } else if (!strcmp(optarg
, "ignore")) {
564 ataopts
.ignore_presets
= true;
565 } else if (!strcmp(optarg
, "show")) {
566 ataopts
.show_presets
= true;
567 } else if (!strcmp(optarg
, "showall")) {
568 if (!no_defaultdb
&& !read_default_drive_databases())
570 if (optind
< argc
) { // -P showall MODEL [FIRMWARE]
571 int cnt
= showmatchingpresets(argv
[optind
], (optind
+1<argc
? argv
[optind
+1] : NULL
));
572 EXIT(cnt
); // report #matches
574 if (showallpresets())
575 EXIT(FAILCMD
); // report regexp syntax error
582 if (!strcmp(optarg
,"offline")) {
584 ataopts
.smart_selftest_type
= OFFLINE_FULL_SCAN
;
585 scsiopts
.smart_default_selftest
= true;
586 } else if (!strcmp(optarg
,"short")) {
588 ataopts
.smart_selftest_type
= SHORT_SELF_TEST
;
589 scsiopts
.smart_short_selftest
= true;
590 } else if (!strcmp(optarg
,"long")) {
592 ataopts
.smart_selftest_type
= EXTEND_SELF_TEST
;
593 scsiopts
.smart_extend_selftest
= true;
594 } else if (!strcmp(optarg
,"conveyance")) {
596 ataopts
.smart_selftest_type
= CONVEYANCE_SELF_TEST
;
597 } else if (!strcmp(optarg
,"afterselect,on")) {
598 // scan remainder of disk after doing selected segment
599 ataopts
.smart_selective_args
.scan_after_select
= 2;
600 } else if (!strcmp(optarg
,"afterselect,off")) {
601 // don't scan remainder of disk after doing selected segments
602 ataopts
.smart_selective_args
.scan_after_select
= 1;
603 } else if (!strncmp(optarg
,"pending,",strlen("pending,"))) {
604 // parse number of minutes that test should be pending
608 i
=(int)strtol(optarg
+strlen("pending,"), &tailptr
, 10);
609 if (errno
|| *tailptr
!= '\0') {
610 sprintf(extraerror
, "Option -t pending,N requires N to be a non-negative integer\n");
612 } else if (i
<0 || i
>65535) {
613 sprintf(extraerror
, "Option -t pending,N (N=%d) must have 0 <= N <= 65535\n", i
);
616 ataopts
.smart_selective_args
.pending_time
= i
+1;
618 } else if (!strncmp(optarg
,"select",strlen("select"))) {
620 // parse range of LBAs to test
621 uint64_t start
, stop
; int mode
;
622 if (split_selective_arg(optarg
, &start
, &stop
, &mode
)) {
623 sprintf(extraerror
, "Option -t select,M-N must have non-negative integer M and N\n");
626 if (ataopts
.smart_selective_args
.num_spans
>= 5 || start
> stop
) {
628 sprintf(extraerror
, "ERROR: Start LBA (%"PRIu64
") > ending LBA (%"PRId64
") in argument \"%s\"\n",
629 start
, stop
, optarg
);
631 sprintf(extraerror
,"ERROR: No more than five selective self-test spans may be"
636 ataopts
.smart_selective_args
.span
[ataopts
.smart_selective_args
.num_spans
].start
= start
;
637 ataopts
.smart_selective_args
.span
[ataopts
.smart_selective_args
.num_spans
].end
= stop
;
638 ataopts
.smart_selective_args
.span
[ataopts
.smart_selective_args
.num_spans
].mode
= mode
;
639 ataopts
.smart_selective_args
.num_spans
++;
640 ataopts
.smart_selftest_type
= SELECTIVE_SELF_TEST
;
642 } else if (!strncmp(optarg
, "scttempint,", sizeof("scstempint,")-1)) {
643 unsigned interval
= 0; int n1
= -1, n2
= -1, len
= strlen(optarg
);
644 if (!( sscanf(optarg
,"scttempint,%u%n,p%n", &interval
, &n1
, &n2
) == 1
645 && 0 < interval
&& interval
<= 0xffff && (n1
== len
|| n2
== len
))) {
646 strcpy(extraerror
, "Option -t scttempint,N[,p] must have positive integer N\n");
649 ataopts
.sct_temp_int
= interval
;
650 ataopts
.sct_temp_int_pers
= (n2
== len
);
660 scsiopts
.smart_selftest_abort
= true;
661 ataopts
.smart_selftest_type
= ABORT_SELF_TEST
;
664 // skip disk check if in low-power mode
665 if (!strcmp(optarg
, "never"))
666 ataopts
.powermode
= 1; // do not skip, but print mode
667 else if (!strcmp(optarg
, "sleep"))
668 ataopts
.powermode
= 2;
669 else if (!strcmp(optarg
, "standby"))
670 ataopts
.powermode
= 3;
671 else if (!strcmp(optarg
, "idle"))
672 ataopts
.powermode
= 4;
678 const char * path
= optarg
;
679 if (*path
== '+' && path
[1])
683 if (!read_drive_database(path
))
688 con
->dont_print
= false;
695 con
->dont_print
= false;
697 // Point arg to the argument in which this option was found.
698 arg
= argv
[optind
-1];
699 // Check whether the option is a long option that doesn't map to -h.
700 if (arg
[1] == '-' && optchar
!= 'h') {
701 // Iff optopt holds a valid option then argument must be missing.
702 if (optopt
&& (strchr(shortopts
, optopt
) != NULL
)) {
703 pout("=======> ARGUMENT REQUIRED FOR OPTION: %s\n", arg
+2);
704 printvalidarglistmessage(optopt
);
706 pout("=======> UNRECOGNIZED OPTION: %s\n",arg
+2);
708 pout("=======> %s", extraerror
);
713 // Iff optopt holds a valid option then argument must be
714 // missing. Note (BA) this logic seems to fail using Solaris
716 if (strchr(shortopts
, optopt
) != NULL
) {
717 pout("=======> ARGUMENT REQUIRED FOR OPTION: %c\n", optopt
);
718 printvalidarglistmessage(optopt
);
720 pout("=======> UNRECOGNIZED OPTION: %c\n",optopt
);
722 pout("=======> %s", extraerror
);
728 } // closes switch statement to process command-line options
730 // Check to see if option had an unrecognized or incorrect argument.
733 // It would be nice to print the actual option name given by the user
734 // here, but we just print the short form. Please fix this if you know
735 // a clean way to do it.
736 pout("=======> INVALID ARGUMENT TO -%c: %s\n", optchar
, optarg
);
737 printvalidarglistmessage(optchar
);
739 pout("=======> %s", extraerror
);
744 // At this point we have processed all command-line options. If the
745 // print output is switchable, then start with the print output
747 if (con
->printing_switchable
)
748 con
->dont_print
= true;
750 // error message if user has asked for more than one test
752 con
->dont_print
= false;
754 pout("\nERROR: smartctl can only run a single test type (or abort) at a time.\n");
759 // error message if user has set selective self-test options without
760 // asking for a selective self-test
761 if ( (ataopts
.smart_selective_args
.pending_time
|| ataopts
.smart_selective_args
.scan_after_select
)
762 && !ataopts
.smart_selective_args
.num_spans
) {
763 con
->dont_print
= false;
765 if (ataopts
.smart_selective_args
.pending_time
)
766 pout("\nERROR: smartctl -t pending,N must be used with -t select,N-M.\n");
768 pout("\nERROR: smartctl -t afterselect,(on|off) must be used with -t select,N-M.\n");
773 // If captive option was used, change test type if appropriate.
775 switch (ataopts
.smart_selftest_type
) {
776 case SHORT_SELF_TEST
:
777 ataopts
.smart_selftest_type
= SHORT_CAPTIVE_SELF_TEST
;
778 scsiopts
.smart_short_selftest
= false;
779 scsiopts
.smart_short_cap_selftest
= true;
781 case EXTEND_SELF_TEST
:
782 ataopts
.smart_selftest_type
= EXTEND_CAPTIVE_SELF_TEST
;
783 scsiopts
.smart_extend_selftest
= false;
784 scsiopts
.smart_extend_cap_selftest
= true;
786 case CONVEYANCE_SELF_TEST
:
787 ataopts
.smart_selftest_type
= CONVEYANCE_CAPTIVE_SELF_TEST
;
789 case SELECTIVE_SELF_TEST
:
790 ataopts
.smart_selftest_type
= SELECTIVE_CAPTIVE_SELF_TEST
;
794 // From here on, normal operations...
797 // Warn if the user has provided no device name
799 pout("ERROR: smartctl requires a device name as the final command-line argument.\n\n");
804 // Warn if the user has provided more than one device name
807 pout("ERROR: smartctl takes ONE device name as the final command-line argument.\n");
808 pout("You have provided %d device names:\n",argc
-optind
);
809 for (i
=0; i
<argc
-optind
; i
++)
810 pout("%s\n",argv
[optind
+i
]);
815 // Read or init drive database
816 if (!no_defaultdb
&& !read_default_drive_databases())
822 // Printing function (controlled by global con->dont_print)
823 // [From GLIBC Manual: Since the prototype doesn't specify types for
824 // optional arguments, in a call to a variadic function the default
825 // argument promotions are performed on the optional argument
826 // values. This means the objects of type char or short int (whether
827 // signed or not) are promoted to either int or unsigned int, as
829 void pout(const char *fmt
, ...){
832 // initialize variable argument list
834 if (con
->dont_print
){
846 // This function is used by utility.cpp to report LOG_CRIT errors.
847 // The smartctl version prints to stdout instead of syslog().
848 void PrintOut(int priority
, const char *fmt
, ...) {
851 // avoid warning message about unused variable from gcc -W: just
852 // change value of local copy.
861 // Used to warn users about invalid checksums. Called from atacmds.cpp.
862 // Action to be taken may be altered by the user.
863 void checksumwarning(const char * string
)
865 // user has asked us to ignore checksum errors
866 if (checksum_err_mode
== CHECKSUM_ERR_IGNORE
)
869 pout("Warning! %s error: invalid SMART checksum.\n", string
);
871 // user has asked us to fail on checksum errors
872 if (checksum_err_mode
== CHECKSUM_ERR_EXIT
)
876 // Return info string about device protocol
877 static const char * get_protocol_info(const smart_device
* dev
)
879 switch ((int)dev
->is_ata() | ((int)dev
->is_scsi() << 1)) {
880 case 0x1: return "ATA";
881 case 0x2: return "SCSI";
882 case 0x3: return "ATA+SCSI";
883 default: return "Unknown";
887 // Main program without exception handling
888 int main_worker(int argc
, char **argv
)
890 // Initialize interface
891 smart_interface::init();
895 // define control block for external functions
896 smartmonctrl control
;
899 // Parse input arguments
900 ata_print_options ataopts
;
901 scsi_print_options scsiopts
;
902 const char * type
= parse_options(argc
, argv
, ataopts
, scsiopts
);
904 // '-d test' -> Report result of autodetection
905 bool print_type_only
= (type
&& !strcmp(type
, "test"));
909 const char * name
= argv
[argc
-1];
911 smart_device_auto_ptr dev
;
912 if (!strcmp(name
,"-")) {
913 // Parse "smartctl -r ataioctl,2 ..." output from stdin
914 if (type
|| print_type_only
) {
915 pout("Smartctl: -d option is not allowed in conjunction with device name \"-\".\n");
919 dev
= get_parsed_ata_device(smi(), name
);
922 // get device of appropriate type
923 dev
= smi()->get_smart_device(name
, type
);
926 pout("%s: %s\n", name
, smi()->get_errmsg());
928 printvalidarglistmessage('d');
930 pout("Smartctl: please specify device type with the -d option.\n");
936 // Report result of first autodetection
937 pout("%s: Device of type '%s' [%s] detected\n",
938 dev
->get_info_name(), dev
->get_dev_type(), get_protocol_info(dev
.get()));
943 smart_device::device_info oldinfo
= dev
->get_info();
945 // Open with autodetect support, may return 'better' device
946 dev
.replace( dev
->autodetect_open() );
948 // Report if type has changed
949 if ((type
|| print_type_only
) && oldinfo
.dev_type
!= dev
->get_dev_type())
950 pout("%s: Device open changed type from '%s' to '%s'\n",
951 dev
->get_info_name(), oldinfo
.dev_type
.c_str(), dev
->get_dev_type());
953 if (!dev
->is_open()) {
954 pout("Smartctl open device: %s failed: %s\n", dev
->get_info_name(), dev
->get_errmsg());
958 // now call appropriate ATA or SCSI routine
961 pout("%s: Device of type '%s' [%s] opened\n",
962 dev
->get_info_name(), dev
->get_dev_type(), get_protocol_info(dev
.get()));
963 else if (dev
->is_ata())
964 retval
= ataPrintMain(dev
->to_ata(), ataopts
);
965 else if (dev
->is_scsi())
966 retval
= scsiPrintMain(dev
->to_scsi(), scsiopts
);
968 // we should never fall into this branch!
969 pout("%s: Neither ATA nor SCSI device\n", dev
->get_info_name());
977 int main(int argc
, char **argv
)
981 // Do the real work ...
982 status
= main_worker(argc
, argv
);
985 // EXIT(status) arrives here
988 catch (const std::bad_alloc
& /*ex*/) {
989 // Memory allocation failed (also thrown by std::operator new)
990 printf("Smartctl: Out of memory\n");
993 catch (const std::exception
& ex
) {
994 // Other fatal errors
995 printf("Smartctl: Exception: %s\n", ex
.what());