smartmontools SVN Rev 3317
Utility to control and monitor storage systems with "S.M.A.R.T."
smartctl.cpp
Go to the documentation of this file.
00001 /*
00002  * smartctl.cpp
00003  *
00004  * Home page of code is: http://smartmontools.sourceforge.net
00005  *
00006  * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
00007  * Copyright (C) 2008-13 Christian Franke <smartmontools-support@lists.sourceforge.net>
00008  * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * This code was originally developed as a Senior Thesis by Michael Cornwell
00019  * at the Concurrent Systems Laboratory (now part of the Storage Systems
00020  * Research Center), Jack Baskin School of Engineering, University of
00021  * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
00022  *
00023  */
00024 
00025 #include <errno.h>
00026 #include <stdio.h>
00027 #include <sys/types.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <stdarg.h>
00031 #include <stdexcept>
00032 #include <getopt.h>
00033 
00034 #include "config.h"
00035 
00036 #ifdef HAVE_UNISTD_H
00037 #include <unistd.h>
00038 #endif
00039 
00040 #if defined(__FreeBSD__)
00041 #include <sys/param.h>
00042 #endif
00043 
00044 #include "int64.h"
00045 #include "atacmds.h"
00046 #include "dev_interface.h"
00047 #include "ataprint.h"
00048 #include "knowndrives.h"
00049 #include "scsicmds.h"
00050 #include "scsiprint.h"
00051 #include "smartctl.h"
00052 #include "utility.h"
00053 
00054 const char * smartctl_cpp_cvsid = "$Id: smartctl.cpp 3857 2013-11-07 20:41:51Z chrfranke $"
00055   CONFIG_H_CVSID SMARTCTL_H_CVSID;
00056 
00057 // Globals to control printing
00058 bool printing_is_switchable = false;
00059 bool printing_is_off = false;
00060 
00061 static void printslogan()
00062 {
00063   pout("%s\n", format_version_info("smartctl").c_str());
00064 }
00065 
00066 static void UsageSummary()
00067 {
00068   pout("\nUse smartctl -h to get a usage summary\n\n");
00069   return;
00070 }
00071 
00072 static std::string getvalidarglist(int opt);
00073 
00074 /*  void prints help information for command syntax */
00075 static void Usage()
00076 {
00077   printf("Usage: smartctl [options] device\n\n");
00078   printf(
00079 "============================================ SHOW INFORMATION OPTIONS =====\n\n"
00080 "  -h, --help, --usage\n"
00081 "         Display this help and exit\n\n"
00082 "  -V, --version, --copyright, --license\n"
00083 "         Print license, copyright, and version information and exit\n\n"
00084 "  -i, --info\n"
00085 "         Show identity information for device\n\n"
00086 "  --identify[=[w][nvb]]\n"
00087 "         Show words and bits from IDENTIFY DEVICE data                (ATA)\n\n"
00088 "  -g NAME, --get=NAME\n"
00089 "        Get device setting: all, aam, apm, lookahead, security, wcache, rcache, wcreorder\n\n"
00090 "  -a, --all\n"
00091 "         Show all SMART information for device\n\n"
00092 "  -x, --xall\n"
00093 "         Show all information for device\n\n"
00094 "  --scan\n"
00095 "         Scan for devices\n\n"
00096 "  --scan-open\n"
00097 "         Scan for devices and try to open each device\n\n"
00098   );
00099   printf(
00100 "================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n"
00101 "  -q TYPE, --quietmode=TYPE                                           (ATA)\n"
00102 "         Set smartctl quiet mode to one of: errorsonly, silent, noserial\n\n"
00103 "  -d TYPE, --device=TYPE\n"
00104 "         Specify device type to one of: %s\n\n"
00105 "  -T TYPE, --tolerance=TYPE                                           (ATA)\n"
00106 "         Tolerance: normal, conservative, permissive, verypermissive\n\n"
00107 "  -b TYPE, --badsum=TYPE                                              (ATA)\n"
00108 "         Set action on bad checksum to one of: warn, exit, ignore\n\n"
00109 "  -r TYPE, --report=TYPE\n"
00110 "         Report transactions (see man page)\n\n"
00111 "  -n MODE, --nocheck=MODE                                             (ATA)\n"
00112 "         No check if: never, sleep, standby, idle (see man page)\n\n",
00113   getvalidarglist('d').c_str()); // TODO: Use this function also for other options ?
00114   printf(
00115 "============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n"
00116 "  -s VALUE, --smart=VALUE\n"
00117 "        Enable/disable SMART on device (on/off)\n\n"
00118 "  -o VALUE, --offlineauto=VALUE                                       (ATA)\n"
00119 "        Enable/disable automatic offline testing on device (on/off)\n\n"
00120 "  -S VALUE, --saveauto=VALUE                                          (ATA)\n"
00121 "        Enable/disable Attribute autosave on device (on/off)\n\n"
00122 "  -s NAME[,VALUE], --set=NAME[,VALUE]\n"
00123 "        Enable/disable/change device setting: aam,[N|off], apm,[N|off],\n"
00124 "        lookahead,[on|off], security-freeze, standby,[N|off|now],\n"
00125 "        wcache,[on|off], rcache,[on|off], wcreorder,[on|off]\n\n"
00126   );
00127   printf(
00128 "======================================= READ AND DISPLAY DATA OPTIONS =====\n\n"
00129 "  -H, --health\n"
00130 "        Show device SMART health status\n\n"
00131 "  -c, --capabilities                                                  (ATA)\n"
00132 "        Show device SMART capabilities\n\n"
00133 "  -A, --attributes\n"
00134 "        Show device SMART vendor-specific Attributes and values\n\n"
00135 "  -f FORMAT, --format=FORMAT                                          (ATA)\n"
00136 "        Set output format for attributes: old, brief, hex[,id|val]\n\n"
00137 "  -l TYPE, --log=TYPE\n"
00138 "        Show device log. TYPE: error, selftest, selective, directory[,g|s],\n"
00139 "                               xerror[,N][,error], xselftest[,N][,selftest],\n"
00140 "                               background, sasphy[,reset], sataphy[,reset],\n"
00141 "                               scttemp[sts,hist], scttempint,N[,p],\n"
00142 "                               scterc[,N,M], devstat[,N], ssd,\n"
00143 "                               gplog,N[,RANGE], smartlog,N[,RANGE]\n\n"
00144 "  -v N,OPTION , --vendorattribute=N,OPTION                            (ATA)\n"
00145 "        Set display OPTION for vendor Attribute N (see man page)\n\n"
00146 "  -F TYPE, --firmwarebug=TYPE                                         (ATA)\n"
00147 "        Use firmware bug workaround:\n"
00148 "        %s, swapid\n\n"
00149 "  -P TYPE, --presets=TYPE                                             (ATA)\n"
00150 "        Drive-specific presets: use, ignore, show, showall\n\n"
00151 "  -B [+]FILE, --drivedb=[+]FILE                                       (ATA)\n"
00152 "        Read and replace [add] drive database from FILE\n"
00153 "        [default is +%s",
00154     get_valid_firmwarebug_args(),
00155     get_drivedb_path_add()
00156   );
00157 #ifdef SMARTMONTOOLS_DRIVEDBDIR
00158   printf(
00159                       "\n"
00160 "         and then    %s",
00161     get_drivedb_path_default()
00162   );
00163 #endif
00164   printf(
00165          "]\n\n"
00166 "============================================ DEVICE SELF-TEST OPTIONS =====\n\n"
00167 "  -t TEST, --test=TEST\n"
00168 "        Run test. TEST: offline, short, long, conveyance, force, vendor,N,\n"
00169 "                        select,M-N, pending,N, afterselect,[on|off]\n\n"
00170 "  -C, --captive\n"
00171 "        Do test in captive mode (along with -t)\n\n"
00172 "  -X, --abort\n"
00173 "        Abort any non-captive test on device\n\n"
00174 );
00175   std::string examples = smi()->get_app_examples("smartctl");
00176   if (!examples.empty())
00177     printf("%s\n", examples.c_str());
00178 }
00179 
00180 // Values for  --long only options, see parse_options()
00181 enum { opt_identify = 1000, opt_scan, opt_scan_open, opt_set, opt_smart };
00182 
00183 /* Returns a string containing a formatted list of the valid arguments
00184    to the option opt or empty on failure. Note 'v' case different */
00185 static std::string getvalidarglist(int opt)
00186 {
00187   switch (opt) {
00188   case 'q':
00189     return "errorsonly, silent, noserial";
00190   case 'd':
00191     return smi()->get_valid_dev_types_str() + ", auto, test";
00192   case 'T':
00193     return "normal, conservative, permissive, verypermissive";
00194   case 'b':
00195     return "warn, exit, ignore";
00196   case 'r':
00197     return "ioctl[,N], ataioctl[,N], scsiioctl[,N]";
00198   case opt_smart:
00199   case 'o':
00200   case 'S':
00201     return "on, off";
00202   case 'l':
00203     return "error, selftest, selective, directory[,g|s], "
00204            "xerror[,N][,error], xselftest[,N][,selftest], "
00205            "background, sasphy[,reset], sataphy[,reset], "
00206            "scttemp[sts,hist], scttempint,N[,p], "
00207            "scterc[,N,M], devstat[,N], ssd, "
00208            "gplog,N[,RANGE], smartlog,N[,RANGE]";
00209 
00210   case 'P':
00211     return "use, ignore, show, showall";
00212   case 't':
00213     return "offline, short, long, conveyance, force, vendor,N, select,M-N, "
00214            "pending,N, afterselect,[on|off]";
00215   case 'F':
00216     return std::string(get_valid_firmwarebug_args()) + ", swapid";
00217   case 'n':
00218     return "never, sleep, standby, idle";
00219   case 'f':
00220     return "old, brief, hex[,id|val]";
00221   case 'g':
00222     return "aam, apm, lookahead, security, wcache, rcache, wcreorder";
00223   case opt_set:
00224     return "aam,[N|off], apm,[N|off], lookahead,[on|off], security-freeze, "
00225            "standby,[N|off|now], wcache,[on|off], rcache,[on|off], wcreorder,[on|off]";
00226   case 's':
00227     return getvalidarglist(opt_smart)+", "+getvalidarglist(opt_set);
00228   case opt_identify:
00229     return "n, wn, w, v, wv, wb";
00230   case 'v':
00231   default:
00232     return "";
00233   }
00234 }
00235 
00236 /* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where
00237    <LIST> is the list of valid arguments for option opt. */
00238 static void printvalidarglistmessage(int opt)
00239 {
00240   if (opt=='v'){
00241     pout("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n",
00242          create_vendor_attribute_arg_list().c_str());
00243   }
00244   else {
00245   // getvalidarglist() might produce a multiline or single line string.  We
00246   // need to figure out which to get the formatting right.
00247     std::string s = getvalidarglist(opt);
00248     char separator = strchr(s.c_str(), '\n') ? '\n' : ' ';
00249     pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, s.c_str(), separator);
00250   }
00251 
00252   return;
00253 }
00254 
00255 // Checksum error mode
00256 enum checksum_err_mode_t {
00257   CHECKSUM_ERR_WARN, CHECKSUM_ERR_EXIT, CHECKSUM_ERR_IGNORE
00258 };
00259 
00260 static checksum_err_mode_t checksum_err_mode = CHECKSUM_ERR_WARN;
00261 
00262 static void scan_devices(const char * type, bool with_open, char ** argv);
00263 
00264 
00265 /*      Takes command options and sets features to be run */    
00266 static const char * parse_options(int argc, char** argv,
00267   ata_print_options & ataopts, scsi_print_options & scsiopts,
00268   bool & print_type_only)
00269 {
00270   // Please update getvalidarglist() if you edit shortopts
00271   const char *shortopts = "h?Vq:d:T:b:r:s:o:S:HcAl:iaxv:P:t:CXF:n:B:f:g:";
00272   // Please update getvalidarglist() if you edit longopts
00273   struct option longopts[] = {
00274     { "help",            no_argument,       0, 'h' },
00275     { "usage",           no_argument,       0, 'h' },
00276     { "version",         no_argument,       0, 'V' },
00277     { "copyright",       no_argument,       0, 'V' },
00278     { "license",         no_argument,       0, 'V' },
00279     { "quietmode",       required_argument, 0, 'q' },
00280     { "device",          required_argument, 0, 'd' },
00281     { "tolerance",       required_argument, 0, 'T' },
00282     { "badsum",          required_argument, 0, 'b' },
00283     { "report",          required_argument, 0, 'r' },
00284     { "smart",           required_argument, 0, opt_smart },
00285     { "offlineauto",     required_argument, 0, 'o' },
00286     { "saveauto",        required_argument, 0, 'S' },
00287     { "health",          no_argument,       0, 'H' },
00288     { "capabilities",    no_argument,       0, 'c' },
00289     { "attributes",      no_argument,       0, 'A' },
00290     { "log",             required_argument, 0, 'l' },
00291     { "info",            no_argument,       0, 'i' },
00292     { "all",             no_argument,       0, 'a' },
00293     { "xall",            no_argument,       0, 'x' },
00294     { "vendorattribute", required_argument, 0, 'v' },
00295     { "presets",         required_argument, 0, 'P' },
00296     { "test",            required_argument, 0, 't' },
00297     { "captive",         no_argument,       0, 'C' },
00298     { "abort",           no_argument,       0, 'X' },
00299     { "firmwarebug",     required_argument, 0, 'F' },
00300     { "nocheck",         required_argument, 0, 'n' },
00301     { "drivedb",         required_argument, 0, 'B' },
00302     { "format",          required_argument, 0, 'f' },
00303     { "get",             required_argument, 0, 'g' },
00304     { "identify",        optional_argument, 0, opt_identify },
00305     { "set",             required_argument, 0, opt_set },
00306     { "scan",            no_argument,       0, opt_scan      },
00307     { "scan-open",       no_argument,       0, opt_scan_open },
00308     { 0,                 0,                 0, 0   }
00309   };
00310 
00311   char extraerror[256];
00312   memset(extraerror, 0, sizeof(extraerror));
00313   opterr=optopt=0;
00314 
00315   const char * type = 0; // set to -d optarg
00316   bool no_defaultdb = false; // set true on '-B FILE'
00317   bool output_format_set = false; // set true on '-f FORMAT'
00318   int scan = 0; // set by --scan, --scan-open
00319   bool badarg = false, captive = false;
00320   int testcnt = 0; // number of self-tests requested
00321 
00322   int optchar;
00323   char *arg;
00324 
00325   while ((optchar = getopt_long(argc, argv, shortopts, longopts, 0)) != -1) {
00326     switch (optchar){
00327     case 'V':
00328       printing_is_off = false;
00329       pout("%s", format_version_info("smartctl", true /*full*/).c_str());
00330       EXIT(0);
00331       break;
00332     case 'q':
00333       if (!strcmp(optarg,"errorsonly")) {
00334         printing_is_switchable = true;
00335         printing_is_off = false;
00336       } else if (!strcmp(optarg,"silent")) {
00337         printing_is_switchable = false;
00338         printing_is_off = true;
00339       } else if (!strcmp(optarg,"noserial")) {
00340         dont_print_serial_number = true;
00341       } else {
00342         badarg = true;
00343       }
00344       break;
00345     case 'd':
00346       if (!strcmp(optarg, "test"))
00347         print_type_only = true;
00348       else
00349         type = (strcmp(optarg, "auto") ? optarg : (char *)0);
00350       break;
00351     case 'T':
00352       if (!strcmp(optarg,"normal")) {
00353         failuretest_conservative = false;
00354         failuretest_permissive   = 0;
00355       } else if (!strcmp(optarg,"conservative")) {
00356         failuretest_conservative = true;
00357       } else if (!strcmp(optarg,"permissive")) {
00358         if (failuretest_permissive < 0xff)
00359           failuretest_permissive++;
00360       } else if (!strcmp(optarg,"verypermissive")) {
00361         failuretest_permissive = 0xff;
00362       } else {
00363         badarg = true;
00364       }
00365       break;
00366     case 'b':
00367       if (!strcmp(optarg,"warn")) {
00368         checksum_err_mode = CHECKSUM_ERR_WARN;
00369       } else if (!strcmp(optarg,"exit")) {
00370         checksum_err_mode = CHECKSUM_ERR_EXIT;
00371       } else if (!strcmp(optarg,"ignore")) {
00372         checksum_err_mode = CHECKSUM_ERR_IGNORE;
00373       } else {
00374         badarg = true;
00375       }
00376       break;
00377     case 'r':
00378       {
00379         int i;
00380         char *s;
00381 
00382         // split_report_arg() may modify its first argument string, so use a
00383         // copy of optarg in case we want optarg for an error message.
00384         if (!(s = strdup(optarg))) {
00385           throw std::bad_alloc();
00386         }
00387         if (split_report_arg(s, &i)) {
00388           badarg = true;
00389         } else if (!strcmp(s,"ioctl")) {
00390           ata_debugmode  = scsi_debugmode = i;
00391         } else if (!strcmp(s,"ataioctl")) {
00392           ata_debugmode = i;
00393         } else if (!strcmp(s,"scsiioctl")) {
00394           scsi_debugmode = i;
00395         } else {
00396           badarg = true;
00397         }
00398         free(s);
00399       }
00400       break;
00401 
00402     case 's':
00403     case opt_smart: // --smart
00404       if (!strcmp(optarg,"on")) {
00405         ataopts.smart_enable  = scsiopts.smart_enable  = true;
00406         ataopts.smart_disable = scsiopts.smart_disable = false;
00407       } else if (!strcmp(optarg,"off")) {
00408         ataopts.smart_disable = scsiopts.smart_disable = true;
00409         ataopts.smart_enable  = scsiopts.smart_enable  = false;
00410       } else if (optchar == 's') {
00411         goto case_s_continued; // --set, see below
00412       } else {
00413         badarg = true;
00414       }
00415       break;
00416 
00417     case 'o':
00418       if (!strcmp(optarg,"on")) {
00419         ataopts.smart_auto_offl_enable  = true;
00420         ataopts.smart_auto_offl_disable = false;
00421       } else if (!strcmp(optarg,"off")) {
00422         ataopts.smart_auto_offl_disable = true;
00423         ataopts.smart_auto_offl_enable  = false;
00424       } else {
00425         badarg = true;
00426       }
00427       break;
00428     case 'S':
00429       if (!strcmp(optarg,"on")) {
00430         ataopts.smart_auto_save_enable  = scsiopts.smart_auto_save_enable  = true;
00431         ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = false;
00432       } else if (!strcmp(optarg,"off")) {
00433         ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = true;
00434         ataopts.smart_auto_save_enable  = scsiopts.smart_auto_save_enable  = false;
00435       } else {
00436         badarg = true;
00437       }
00438       break;
00439     case 'H':
00440       ataopts.smart_check_status = scsiopts.smart_check_status = true;
00441       scsiopts.smart_ss_media_log = true;
00442       break;
00443     case 'F':
00444       if (!strcmp(optarg, "swapid"))
00445         ataopts.fix_swapped_id = true;
00446       else if (!parse_firmwarebug_def(optarg, ataopts.firmwarebugs))
00447         badarg = true;
00448       break;
00449     case 'c':
00450       ataopts.smart_general_values = true;
00451       break;
00452     case 'A':
00453       ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true;
00454       break;
00455     case 'l':
00456       if (!strcmp(optarg,"error")) {
00457         ataopts.smart_error_log = scsiopts.smart_error_log = true;
00458       } else if (!strcmp(optarg,"selftest")) {
00459         ataopts.smart_selftest_log = scsiopts.smart_selftest_log = true;
00460       } else if (!strcmp(optarg, "selective")) {
00461         ataopts.smart_selective_selftest_log = true;
00462       } else if (!strcmp(optarg,"directory")) {
00463         ataopts.smart_logdir = ataopts.gp_logdir = true; // SMART+GPL
00464       } else if (!strcmp(optarg,"directory,s")) {
00465         ataopts.smart_logdir = true; // SMART
00466       } else if (!strcmp(optarg,"directory,g")) {
00467         ataopts.gp_logdir = true; // GPL
00468       } else if (!strcmp(optarg,"sasphy")) {
00469         scsiopts.sasphy = true;
00470       } else if (!strcmp(optarg,"sasphy,reset")) {
00471         scsiopts.sasphy = scsiopts.sasphy_reset = true;
00472       } else if (!strcmp(optarg,"sataphy")) {
00473         ataopts.sataphy = true;
00474       } else if (!strcmp(optarg,"sataphy,reset")) {
00475         ataopts.sataphy = ataopts.sataphy_reset = true;
00476       } else if (!strcmp(optarg,"background")) {
00477         scsiopts.smart_background_log = true;
00478       } else if (!strcmp(optarg,"ssd")) {
00479         ataopts.devstat_ssd_page = true;
00480         scsiopts.smart_ss_media_log = true;
00481       } else if (!strcmp(optarg,"scterc")) {
00482         ataopts.sct_erc_get = true;
00483       } else if (!strcmp(optarg,"scttemp")) {
00484         ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
00485       } else if (!strcmp(optarg,"scttempsts")) {
00486         ataopts.sct_temp_sts = true;
00487       } else if (!strcmp(optarg,"scttemphist")) {
00488         ataopts.sct_temp_hist = true;
00489 
00490       } else if (!strncmp(optarg, "scttempint,", sizeof("scstempint,")-1)) {
00491         unsigned interval = 0; int n1 = -1, n2 = -1, len = strlen(optarg);
00492         if (!(   sscanf(optarg,"scttempint,%u%n,p%n", &interval, &n1, &n2) == 1
00493               && 0 < interval && interval <= 0xffff && (n1 == len || n2 == len))) {
00494             snprintf(extraerror, sizeof(extraerror), "Option -l scttempint,N[,p] must have positive integer N\n");
00495             badarg = true;
00496         }
00497         ataopts.sct_temp_int = interval;
00498         ataopts.sct_temp_int_pers = (n2 == len);
00499 
00500       } else if (!strncmp(optarg, "devstat", sizeof("devstat")-1)) {
00501         int n1 = -1, n2 = -1, len = strlen(optarg);
00502         unsigned val = ~0;
00503         sscanf(optarg, "devstat%n,%u%n", &n1, &val, &n2);
00504         if (n1 == len)
00505           ataopts.devstat_all_pages = true;
00506         else if (n2 == len && val <= 255)
00507           ataopts.devstat_pages.push_back(val);
00508         else
00509           badarg = true;
00510 
00511       } else if (!strncmp(optarg, "xerror", sizeof("xerror")-1)) {
00512         int n1 = -1, n2 = -1, len = strlen(optarg);
00513         unsigned val = 8;
00514         sscanf(optarg, "xerror%n,error%n", &n1, &n2);
00515         if (!(n1 == len || n2 == len)) {
00516           n1 = n2 = -1;
00517           sscanf(optarg, "xerror,%u%n,error%n", &val, &n1, &n2);
00518         }
00519         if ((n1 == len || n2 == len) && val > 0) {
00520           ataopts.smart_ext_error_log = val;
00521           ataopts.retry_error_log = (n2 == len);
00522         }
00523         else
00524           badarg = true;
00525 
00526       } else if (!strncmp(optarg, "xselftest", sizeof("xselftest")-1)) {
00527         int n1 = -1, n2 = -1, len = strlen(optarg);
00528         unsigned val = 25;
00529         sscanf(optarg, "xselftest%n,selftest%n", &n1, &n2);
00530         if (!(n1 == len || n2 == len)) {
00531           n1 = n2 = -1;
00532           sscanf(optarg, "xselftest,%u%n,selftest%n", &val, &n1, &n2);
00533         }
00534         if ((n1 == len || n2 == len) && val > 0) {
00535           ataopts.smart_ext_selftest_log = val;
00536           ataopts.retry_selftest_log = (n2 == len);
00537         }
00538         else
00539           badarg = true;
00540 
00541       } else if (!strncmp(optarg, "scterc,", sizeof("scterc,")-1)) {
00542         unsigned rt = ~0, wt = ~0; int n = -1;
00543         sscanf(optarg,"scterc,%u,%u%n", &rt, &wt, &n);
00544         if (n == (int)strlen(optarg) && rt <= 999 && wt <= 999) {
00545           ataopts.sct_erc_set = true;
00546           ataopts.sct_erc_readtime = rt;
00547           ataopts.sct_erc_writetime = wt;
00548         }
00549         else {
00550           snprintf(extraerror, sizeof(extraerror), "Option -l scterc,[READTIME,WRITETIME] syntax error\n");
00551           badarg = true;
00552         }
00553       } else if (   !strncmp(optarg, "gplog,"   , sizeof("gplog,"   )-1)
00554                  || !strncmp(optarg, "smartlog,", sizeof("smartlog,")-1)) {
00555         unsigned logaddr = ~0U; unsigned page = 0, nsectors = 1; char sign = 0;
00556         int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
00557         sscanf(optarg, "%*[a-z],0x%x%n,%u%n%c%u%n",
00558                &logaddr, &n1, &page, &n2, &sign, &nsectors, &n3);
00559         if (len > n2 && n3 == -1 && !strcmp(optarg+n2, "-max")) {
00560           nsectors = ~0U; sign = '+'; n3 = len;
00561         }
00562         bool gpl = (optarg[0] == 'g');
00563         const char * erropt = (gpl ? "gplog" : "smartlog");
00564         if (!(   n1 == len || n2 == len
00565               || (n3 == len && (sign == '+' || sign == '-')))) {
00566           snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] syntax error\n", erropt);
00567           badarg = true;
00568         }
00569         else if (!(    logaddr <= 0xff && page <= (gpl ? 0xffffU : 0x00ffU)
00570                    && 0 < nsectors
00571                    && (nsectors <= (gpl ? 0xffffU : 0xffU) || nsectors == ~0U)
00572                    && (sign != '-' || page <= nsectors)                       )) {
00573           snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] parameter out of range\n", erropt);
00574           badarg = true;
00575         }
00576         else {
00577           ata_log_request req;
00578           req.gpl = gpl; req.logaddr = logaddr; req.page = page;
00579           req.nsectors = (sign == '-' ? nsectors-page+1 : nsectors);
00580           ataopts.log_requests.push_back(req);
00581         }
00582       } else {
00583         badarg = true;
00584       }
00585       break;
00586     case 'i':
00587       ataopts.drive_info = scsiopts.drive_info = true;
00588       break;
00589 
00590     case opt_identify:
00591       ataopts.identify_word_level = ataopts.identify_bit_level = 0;
00592       if (optarg) {
00593         for (int i = 0; optarg[i]; i++) {
00594           switch (optarg[i]) {
00595             case 'w': ataopts.identify_word_level = 1; break;
00596             case 'n': ataopts.identify_bit_level = -1; break;
00597             case 'v': ataopts.identify_bit_level = 1; break;
00598             case 'b': ataopts.identify_bit_level = 2; break;
00599             default: badarg = true;
00600           }
00601         }
00602       }
00603       break;
00604 
00605     case 'a':
00606       ataopts.drive_info           = scsiopts.drive_info          = true;
00607       ataopts.smart_check_status   = scsiopts.smart_check_status  = true;
00608       ataopts.smart_general_values = true;
00609       ataopts.smart_vendor_attrib  = scsiopts.smart_vendor_attrib = true;
00610       ataopts.smart_error_log      = scsiopts.smart_error_log     = true;
00611       ataopts.smart_selftest_log   = scsiopts.smart_selftest_log  = true;
00612       ataopts.smart_selective_selftest_log = true;
00613       /* scsiopts.smart_background_log = true; */
00614       scsiopts.smart_ss_media_log = true;
00615       break;
00616     case 'x':
00617       ataopts.drive_info           = scsiopts.drive_info          = true;
00618       ataopts.smart_check_status   = scsiopts.smart_check_status  = true;
00619       ataopts.smart_general_values = true;
00620       ataopts.smart_vendor_attrib  = scsiopts.smart_vendor_attrib = true;
00621       ataopts.smart_ext_error_log  = 8;
00622       ataopts.retry_error_log      = true;
00623       ataopts.smart_ext_selftest_log = 25;
00624       ataopts.retry_selftest_log   = true;
00625       scsiopts.smart_error_log     = scsiopts.smart_selftest_log    = true;
00626       ataopts.smart_selective_selftest_log = true;
00627       ataopts.smart_logdir = ataopts.gp_logdir = true;
00628       ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
00629       ataopts.sct_erc_get = true;
00630       ataopts.sct_wcache_reorder_get = true;
00631       ataopts.devstat_all_pages = true;
00632       ataopts.sataphy = true;
00633       ataopts.get_set_used = true;
00634       ataopts.get_aam = ataopts.get_apm = true;
00635       ataopts.get_security = true;
00636       ataopts.get_lookahead = ataopts.get_wcache = true;
00637       scsiopts.get_rcd = scsiopts.get_wce = true;
00638       scsiopts.smart_background_log = true;
00639       scsiopts.smart_ss_media_log = true;
00640       scsiopts.sasphy = true;
00641       if (!output_format_set)
00642         ataopts.output_format |= ata_print_options::FMT_BRIEF;
00643       break;
00644     case 'v':
00645       // parse vendor-specific definitions of attributes
00646       if (!strcmp(optarg,"help")) {
00647         printing_is_off = false;
00648         printslogan();
00649         pout("The valid arguments to -v are:\n\thelp\n%s\n",
00650              create_vendor_attribute_arg_list().c_str());
00651         EXIT(0);
00652       }
00653       if (!parse_attribute_def(optarg, ataopts.attribute_defs, PRIOR_USER))
00654         badarg = true;
00655       break;    
00656     case 'P':
00657       if (!strcmp(optarg, "use")) {
00658         ataopts.ignore_presets = false;
00659       } else if (!strcmp(optarg, "ignore")) {
00660         ataopts.ignore_presets = true;
00661       } else if (!strcmp(optarg, "show")) {
00662         ataopts.show_presets = true;
00663       } else if (!strcmp(optarg, "showall")) {
00664         if (!no_defaultdb && !read_default_drive_databases())
00665           EXIT(FAILCMD);
00666         if (optind < argc) { // -P showall MODEL [FIRMWARE]
00667           int cnt = showmatchingpresets(argv[optind], (optind+1<argc ? argv[optind+1] : NULL));
00668           EXIT(cnt); // report #matches
00669         }
00670         if (showallpresets())
00671           EXIT(FAILCMD); // report regexp syntax error
00672         EXIT(0);
00673       } else {
00674         badarg = true;
00675       }
00676       break;
00677     case 't':
00678       if (!strcmp(optarg,"offline")) {
00679         testcnt++;
00680         ataopts.smart_selftest_type = OFFLINE_FULL_SCAN;
00681         scsiopts.smart_default_selftest = true;
00682       } else if (!strcmp(optarg,"short")) {
00683         testcnt++;
00684         ataopts.smart_selftest_type = SHORT_SELF_TEST;
00685         scsiopts.smart_short_selftest = true;
00686       } else if (!strcmp(optarg,"long")) {
00687         testcnt++;
00688         ataopts.smart_selftest_type = EXTEND_SELF_TEST;
00689         scsiopts.smart_extend_selftest = true;
00690       } else if (!strcmp(optarg,"conveyance")) {
00691         testcnt++;
00692         ataopts.smart_selftest_type = CONVEYANCE_SELF_TEST;
00693       } else if (!strcmp(optarg,"force")) {
00694         ataopts.smart_selftest_force = true;
00695         scsiopts.smart_selftest_force = true;
00696       } else if (!strcmp(optarg,"afterselect,on")) {
00697         // scan remainder of disk after doing selected segment
00698         ataopts.smart_selective_args.scan_after_select = 2;
00699       } else if (!strcmp(optarg,"afterselect,off")) {
00700         // don't scan remainder of disk after doing selected segments
00701         ataopts.smart_selective_args.scan_after_select = 1;
00702       } else if (!strncmp(optarg,"pending,",strlen("pending,"))) {
00703         // parse number of minutes that test should be pending
00704         int i;
00705         char *tailptr=NULL;
00706         errno=0;
00707         i=(int)strtol(optarg+strlen("pending,"), &tailptr, 10);
00708         if (errno || *tailptr != '\0') {
00709           snprintf(extraerror, sizeof(extraerror), "Option -t pending,N requires N to be a non-negative integer\n");
00710           badarg = true;
00711         } else if (i<0 || i>65535) {
00712           snprintf(extraerror, sizeof(extraerror), "Option -t pending,N (N=%d) must have 0 <= N <= 65535\n", i);
00713           badarg = true;
00714         } else {
00715           ataopts.smart_selective_args.pending_time = i+1;
00716         }
00717       } else if (!strncmp(optarg,"select",strlen("select"))) {
00718         if (ataopts.smart_selective_args.num_spans == 0)
00719           testcnt++;
00720         // parse range of LBAs to test
00721         uint64_t start, stop; int mode;
00722         if (split_selective_arg(optarg, &start, &stop, &mode)) {
00723           snprintf(extraerror, sizeof(extraerror), "Option -t select,M-N must have non-negative integer M and N\n");
00724           badarg = true;
00725         } else {
00726           if (ataopts.smart_selective_args.num_spans >= 5 || start > stop) {
00727             if (start > stop) {
00728               snprintf(extraerror, sizeof(extraerror), "ERROR: Start LBA (%" PRIu64 ") > ending LBA (%" PRId64 ") in argument \"%s\"\n",
00729                 start, stop, optarg);
00730             } else {
00731               snprintf(extraerror, sizeof(extraerror),"ERROR: No more than five selective self-test spans may be"
00732                 " defined\n");
00733             }
00734             badarg = true;
00735           }
00736           ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].start = start;
00737           ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].end   = stop;
00738           ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].mode  = mode;
00739           ataopts.smart_selective_args.num_spans++;
00740           ataopts.smart_selftest_type = SELECTIVE_SELF_TEST;
00741         }
00742       } else if (!strncmp(optarg, "scttempint", sizeof("scstempint")-1)) {
00743         snprintf(extraerror, sizeof(extraerror), "-t scttempint is no longer supported, use -l scttempint instead\n");
00744         badarg = true;
00745       } else if (!strncmp(optarg, "vendor,", sizeof("vendor,")-1)) {
00746         unsigned subcmd = ~0U; int n = -1;
00747         if (!(   sscanf(optarg, "%*[a-z],0x%x%n", &subcmd, &n) == 1
00748               && subcmd <= 0xff && n == (int)strlen(optarg))) {
00749           snprintf(extraerror, sizeof(extraerror), "Option -t vendor,0xNN syntax error\n");
00750           badarg = true;
00751         }
00752         else
00753           ataopts.smart_selftest_type = subcmd;
00754       } else {
00755         badarg = true;
00756       }
00757       break;
00758     case 'C':
00759       captive = true;
00760       break;
00761     case 'X':
00762       testcnt++;
00763       scsiopts.smart_selftest_abort = true;
00764       ataopts.smart_selftest_type = ABORT_SELF_TEST;
00765       break;
00766     case 'n':
00767       // skip disk check if in low-power mode
00768       if (!strcmp(optarg, "never"))
00769         ataopts.powermode = 1; // do not skip, but print mode
00770       else if (!strcmp(optarg, "sleep"))
00771         ataopts.powermode = 2;
00772       else if (!strcmp(optarg, "standby"))
00773         ataopts.powermode = 3;
00774       else if (!strcmp(optarg, "idle"))
00775         ataopts.powermode = 4;
00776       else
00777         badarg = true;
00778       break;
00779     case 'f':
00780       if (!strcmp(optarg, "old")) {
00781         ataopts.output_format &= ~ata_print_options::FMT_BRIEF;
00782         output_format_set = true;
00783       }
00784       else if (!strcmp(optarg, "brief")) {
00785         ataopts.output_format |= ata_print_options::FMT_BRIEF;
00786         output_format_set = true;
00787       }
00788       else if (!strcmp(optarg, "hex"))
00789         ataopts.output_format |= ata_print_options::FMT_HEX_ID
00790                               |  ata_print_options::FMT_HEX_VAL;
00791       else if (!strcmp(optarg, "hex,id"))
00792         ataopts.output_format |= ata_print_options::FMT_HEX_ID;
00793       else if (!strcmp(optarg, "hex,val"))
00794         ataopts.output_format |= ata_print_options::FMT_HEX_VAL;
00795       else
00796         badarg = true;
00797       break;
00798     case 'B':
00799       {
00800         const char * path = optarg;
00801         if (*path == '+' && path[1])
00802           path++;
00803         else
00804           no_defaultdb = true;
00805         if (!read_drive_database(path))
00806           EXIT(FAILCMD);
00807       }
00808       break;
00809     case 'h':
00810       printing_is_off = false;
00811       printslogan();
00812       Usage();
00813       EXIT(0);  
00814       break;
00815 
00816     case 'g':
00817     case_s_continued: // -s, see above
00818     case opt_set: // --set
00819       {
00820         ataopts.get_set_used = true;
00821         bool get = (optchar == 'g');
00822         char name[16+1]; unsigned val;
00823         int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
00824         if (sscanf(optarg, "%16[^,=]%n%*[,=]%n%u%n", name, &n1, &n2, &val, &n3) >= 1
00825             && (n1 == len || (!get && n2 > 0))) {
00826           bool on  = (n2 > 0 && !strcmp(optarg+n2, "on"));
00827           bool off = (n2 > 0 && !strcmp(optarg+n2, "off"));
00828           if (n3 != len)
00829             val = ~0U;
00830 
00831           if (get && !strcmp(name, "all")) {
00832             ataopts.get_aam = ataopts.get_apm = true;
00833             ataopts.get_security = true;
00834             ataopts.get_lookahead = ataopts.get_wcache = true;
00835             scsiopts.get_rcd = scsiopts.get_wce = true;
00836           }
00837           else if (!strcmp(name, "aam")) {
00838             if (get)
00839               ataopts.get_aam = true;
00840             else if (off)
00841               ataopts.set_aam = -1;
00842             else if (val <= 254)
00843               ataopts.set_aam = val + 1;
00844             else {
00845               snprintf(extraerror, sizeof(extraerror), "Option -s aam,N must have 0 <= N <= 254\n");
00846               badarg = true;
00847             }
00848           }
00849           else if (!strcmp(name, "apm")) {
00850             if (get)
00851               ataopts.get_apm = true;
00852             else if (off)
00853               ataopts.set_apm = -1;
00854             else if (1 <= val && val <= 254)
00855               ataopts.set_apm = val + 1;
00856             else {
00857               snprintf(extraerror, sizeof(extraerror), "Option -s apm,N must have 1 <= N <= 254\n");
00858               badarg = true;
00859             }
00860           }
00861           else if (!strcmp(name, "lookahead")) {
00862             if (get) {
00863               ataopts.get_lookahead = true;
00864             }
00865             else if (off)
00866               ataopts.set_lookahead = -1;
00867             else if (on)
00868               ataopts.set_lookahead = 1;
00869             else
00870               badarg = true;
00871           }
00872           else if (!strcmp(name, "wcreorder")) {
00873             if (get) {
00874               ataopts.sct_wcache_reorder_get = true;
00875             }
00876             else if (off)
00877               ataopts.sct_wcache_reorder_set = -1;
00878             else if (on)
00879               ataopts.sct_wcache_reorder_set = 1;
00880             else
00881               badarg = true;
00882           }
00883           else if (!strcmp(name, "rcache")) {
00884             if (get)
00885               scsiopts.get_rcd = true;
00886             else if (off)
00887               scsiopts.set_rcd = -1;
00888             else if (on)
00889               scsiopts.set_rcd = 1;
00890             else
00891               badarg = true;
00892           }
00893           else if (get && !strcmp(name, "security")) {
00894             ataopts.get_security = true;
00895           }
00896           else if (!get && !strcmp(optarg, "security-freeze")) {
00897             ataopts.set_security_freeze = true;
00898           }
00899           else if (!get && !strcmp(optarg, "standby,now")) {
00900               ataopts.set_standby_now = true;
00901           }
00902           else if (!get && !strcmp(name, "standby")) {
00903             if (off)
00904               ataopts.set_standby = 0 + 1;
00905             else if (val <= 255)
00906               ataopts.set_standby = val + 1;
00907             else {
00908               snprintf(extraerror, sizeof(extraerror), "Option -s standby,N must have 0 <= N <= 255\n");
00909               badarg = true;
00910             }
00911           }
00912           else if (!strcmp(name, "wcache")) {
00913             if (get) {
00914               ataopts.get_wcache = true;
00915               scsiopts.get_wce = true;
00916             }
00917             else if (off) {
00918               ataopts.set_wcache = -1;
00919               scsiopts.set_wce = -1;
00920             }
00921             else if (on) {
00922               ataopts.set_wcache = 1;
00923               scsiopts.set_wce = 1;
00924             }
00925             else
00926               badarg = true;
00927           }
00928           else
00929             badarg = true;
00930         }
00931         else
00932           badarg = true;
00933       }
00934       break;
00935 
00936     case opt_scan:
00937     case opt_scan_open:
00938       scan = optchar;
00939       break;
00940 
00941     case '?':
00942     default:
00943       printing_is_off = false;
00944       printslogan();
00945       // Point arg to the argument in which this option was found.
00946       arg = argv[optind-1];
00947       // Check whether the option is a long option that doesn't map to -h.
00948       if (arg[1] == '-' && optchar != 'h') {
00949         // Iff optopt holds a valid option then argument must be missing.
00950         if (optopt && (optopt >= opt_scan || strchr(shortopts, optopt))) {
00951           pout("=======> ARGUMENT REQUIRED FOR OPTION: %s\n", arg+2);
00952           printvalidarglistmessage(optopt);
00953         } else
00954           pout("=======> UNRECOGNIZED OPTION: %s\n",arg+2);
00955         if (extraerror[0])
00956           pout("=======> %s", extraerror);
00957         UsageSummary();
00958         EXIT(FAILCMD);
00959       }
00960       if (0 < optopt && optopt < '~') {
00961         // Iff optopt holds a valid option then argument must be
00962         // missing.  Note (BA) this logic seems to fail using Solaris
00963         // getopt!
00964         if (strchr(shortopts, optopt) != NULL) {
00965           pout("=======> ARGUMENT REQUIRED FOR OPTION: %c\n", optopt);
00966           printvalidarglistmessage(optopt);
00967         } else
00968           pout("=======> UNRECOGNIZED OPTION: %c\n",optopt);
00969         if (extraerror[0])
00970           pout("=======> %s", extraerror);
00971         UsageSummary();
00972         EXIT(FAILCMD);
00973       }
00974       Usage();
00975       EXIT(0);  
00976     } // closes switch statement to process command-line options
00977     
00978     // Check to see if option had an unrecognized or incorrect argument.
00979     if (badarg) {
00980       printslogan();
00981       // It would be nice to print the actual option name given by the user
00982       // here, but we just print the short form.  Please fix this if you know
00983       // a clean way to do it.
00984       char optstr[] = { (char)optchar, 0 };
00985       pout("=======> INVALID ARGUMENT TO -%s: %s\n",
00986         (optchar == opt_identify ? "-identify" :
00987          optchar == opt_set ? "-set" :
00988          optchar == opt_smart ? "-smart" : optstr), optarg);
00989       printvalidarglistmessage(optchar);
00990       if (extraerror[0])
00991         pout("=======> %s", extraerror);
00992       UsageSummary();
00993       EXIT(FAILCMD);
00994     }
00995   }
00996 
00997   // Special handling of --scan, --scanopen
00998   if (scan) {
00999     // Read or init drive database to allow USB ID check.
01000     if (!no_defaultdb && !read_default_drive_databases())
01001       EXIT(FAILCMD);
01002     scan_devices(type, (scan == opt_scan_open), argv + optind);
01003     EXIT(0);
01004   }
01005 
01006   // At this point we have processed all command-line options.  If the
01007   // print output is switchable, then start with the print output
01008   // turned off
01009   if (printing_is_switchable)
01010     printing_is_off = true;
01011 
01012   // error message if user has asked for more than one test
01013   if (testcnt > 1) {
01014     printing_is_off = false;
01015     printslogan();
01016     pout("\nERROR: smartctl can only run a single test type (or abort) at a time.\n");
01017     UsageSummary();
01018     EXIT(FAILCMD);
01019   }
01020 
01021   // error message if user has set selective self-test options without
01022   // asking for a selective self-test
01023   if (   (ataopts.smart_selective_args.pending_time || ataopts.smart_selective_args.scan_after_select)
01024       && !ataopts.smart_selective_args.num_spans) {
01025     printing_is_off = false;
01026     printslogan();
01027     if (ataopts.smart_selective_args.pending_time)
01028       pout("\nERROR: smartctl -t pending,N must be used with -t select,N-M.\n");
01029     else
01030       pout("\nERROR: smartctl -t afterselect,(on|off) must be used with -t select,N-M.\n");
01031     UsageSummary();
01032     EXIT(FAILCMD);
01033   }
01034 
01035   // If captive option was used, change test type if appropriate.
01036   if (captive)
01037     switch (ataopts.smart_selftest_type) {
01038       case SHORT_SELF_TEST:
01039         ataopts.smart_selftest_type = SHORT_CAPTIVE_SELF_TEST;
01040         scsiopts.smart_short_selftest     = false;
01041         scsiopts.smart_short_cap_selftest = true;
01042         break;
01043       case EXTEND_SELF_TEST:
01044         ataopts.smart_selftest_type = EXTEND_CAPTIVE_SELF_TEST;
01045         scsiopts.smart_extend_selftest     = false;
01046         scsiopts.smart_extend_cap_selftest = true;
01047         break;
01048       case CONVEYANCE_SELF_TEST:
01049         ataopts.smart_selftest_type = CONVEYANCE_CAPTIVE_SELF_TEST;
01050         break;
01051       case SELECTIVE_SELF_TEST:
01052         ataopts.smart_selftest_type = SELECTIVE_CAPTIVE_SELF_TEST;
01053         break;
01054     }
01055 
01056   // From here on, normal operations...
01057   printslogan();
01058   
01059   // Warn if the user has provided no device name
01060   if (argc-optind<1){
01061     pout("ERROR: smartctl requires a device name as the final command-line argument.\n\n");
01062     UsageSummary();
01063     EXIT(FAILCMD);
01064   }
01065   
01066   // Warn if the user has provided more than one device name
01067   if (argc-optind>1){
01068     int i;
01069     pout("ERROR: smartctl takes ONE device name as the final command-line argument.\n");
01070     pout("You have provided %d device names:\n",argc-optind);
01071     for (i=0; i<argc-optind; i++)
01072       pout("%s\n",argv[optind+i]);
01073     UsageSummary();
01074     EXIT(FAILCMD);
01075   }
01076 
01077   // Read or init drive database
01078   if (!no_defaultdb && !read_default_drive_databases())
01079     EXIT(FAILCMD);
01080 
01081   return type;
01082 }
01083 
01084 // Printing function (controlled by global printing_is_off)
01085 // [From GLIBC Manual: Since the prototype doesn't specify types for
01086 // optional arguments, in a call to a variadic function the default
01087 // argument promotions are performed on the optional argument
01088 // values. This means the objects of type char or short int (whether
01089 // signed or not) are promoted to either int or unsigned int, as
01090 // appropriate.]
01091 void pout(const char *fmt, ...){
01092   va_list ap;
01093   
01094   // initialize variable argument list 
01095   va_start(ap,fmt);
01096   if (printing_is_off) {
01097     va_end(ap);
01098     return;
01099   }
01100 
01101   // print out
01102   vprintf(fmt,ap);
01103   va_end(ap);
01104   fflush(stdout);
01105   return;
01106 }
01107 
01108 // Globals to set failuretest() policy
01109 bool failuretest_conservative = false;
01110 unsigned char failuretest_permissive = 0;
01111 
01112 // Compares failure type to policy in effect, and either exits or
01113 // simply returns to the calling routine.
01114 // Used in ataprint.cpp and scsiprint.cpp.
01115 void failuretest(failure_type type, int returnvalue)
01116 {
01117   // If this is an error in an "optional" SMART command
01118   if (type == OPTIONAL_CMD) {
01119     if (!failuretest_conservative)
01120       return;
01121     pout("An optional SMART command failed: exiting. Remove '-T conservative' option to continue.\n");
01122     EXIT(returnvalue);
01123   }
01124 
01125   // If this is an error in a "mandatory" SMART command
01126   if (type == MANDATORY_CMD) {
01127     if (failuretest_permissive--)
01128       return;
01129     pout("A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n");
01130     EXIT(returnvalue);
01131   }
01132 
01133   throw std::logic_error("failuretest: Unknown type");
01134 }
01135 
01136 // Used to warn users about invalid checksums. Called from atacmds.cpp.
01137 // Action to be taken may be altered by the user.
01138 void checksumwarning(const char * string)
01139 {
01140   // user has asked us to ignore checksum errors
01141   if (checksum_err_mode == CHECKSUM_ERR_IGNORE)
01142     return;
01143 
01144   pout("Warning! %s error: invalid SMART checksum.\n", string);
01145 
01146   // user has asked us to fail on checksum errors
01147   if (checksum_err_mode == CHECKSUM_ERR_EXIT)
01148     EXIT(FAILSMART);
01149 }
01150 
01151 // Return info string about device protocol
01152 static const char * get_protocol_info(const smart_device * dev)
01153 {
01154   switch ((int)dev->is_ata() | ((int)dev->is_scsi() << 1)) {
01155     case 0x1: return "ATA";
01156     case 0x2: return "SCSI";
01157     case 0x3: return "ATA+SCSI";
01158     default:  return "Unknown";
01159   }
01160 }
01161 
01162 // Device scan
01163 // smartctl [-d type] --scan[-open] -- [PATTERN] [smartd directive ...]
01164 void scan_devices(const char * type, bool with_open, char ** argv)
01165 {
01166   bool dont_print = !(ata_debugmode || scsi_debugmode);
01167 
01168   const char * pattern = 0;
01169   int ai = 0;
01170   if (argv[ai] && argv[ai][0] != '-')
01171     pattern = argv[ai++];
01172 
01173   smart_device_list devlist;
01174   printing_is_off = dont_print;
01175   bool ok = smi()->scan_smart_devices(devlist, type , pattern);
01176   printing_is_off = false;
01177 
01178   if (!ok) {
01179     pout("# scan_smart_devices: %s\n", smi()->get_errmsg());
01180     return;
01181   }
01182 
01183   for (unsigned i = 0; i < devlist.size(); i++) {
01184     smart_device_auto_ptr dev( devlist.release(i) );
01185 
01186     if (with_open) {
01187       printing_is_off = dont_print;
01188       dev.replace ( dev->autodetect_open() );
01189       printing_is_off = false;
01190 
01191       if (!dev->is_open()) {
01192         pout("# %s -d %s # %s, %s device open failed: %s\n", dev->get_dev_name(),
01193           dev->get_dev_type(), dev->get_info_name(),
01194           get_protocol_info(dev.get()), dev->get_errmsg());
01195         continue;
01196       }
01197     }
01198 
01199     pout("%s -d %s", dev->get_dev_name(), dev->get_dev_type());
01200     if (!argv[ai])
01201       pout(" # %s, %s device\n", dev->get_info_name(), get_protocol_info(dev.get()));
01202     else {
01203       for (int j = ai; argv[j]; j++)
01204         pout(" %s", argv[j]);
01205       pout("\n");
01206     }
01207 
01208     if (dev->is_open())
01209       dev->close();
01210   }
01211 }
01212 
01213 // Main program without exception handling
01214 static int main_worker(int argc, char **argv)
01215 {
01216   // Throw if CPU endianess does not match compile time test.
01217   check_endianness();
01218 
01219   // Initialize interface
01220   smart_interface::init();
01221   if (!smi())
01222     return 1;
01223 
01224   // Parse input arguments
01225   ata_print_options ataopts;
01226   scsi_print_options scsiopts;
01227   bool print_type_only = false;
01228   const char * type = parse_options(argc, argv, ataopts, scsiopts, print_type_only);
01229 
01230   const char * name = argv[argc-1];
01231 
01232   smart_device_auto_ptr dev;
01233   if (!strcmp(name,"-")) {
01234     // Parse "smartctl -r ataioctl,2 ..." output from stdin
01235     if (type || print_type_only) {
01236       pout("-d option is not allowed in conjunction with device name \"-\".\n");
01237       UsageSummary();
01238       return FAILCMD;
01239     }
01240     dev = get_parsed_ata_device(smi(), name);
01241   }
01242   else
01243     // get device of appropriate type
01244     dev = smi()->get_smart_device(name, type);
01245 
01246   if (!dev) {
01247     pout("%s: %s\n", name, smi()->get_errmsg());
01248     if (type)
01249       printvalidarglistmessage('d');
01250     else
01251       pout("Please specify device type with the -d option.\n");
01252     UsageSummary();
01253     return FAILCMD;
01254   }
01255 
01256   if (print_type_only)
01257     // Report result of first autodetection
01258     pout("%s: Device of type '%s' [%s] detected\n",
01259          dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
01260 
01261   // Open device
01262   {
01263     // Save old info
01264     smart_device::device_info oldinfo = dev->get_info();
01265 
01266     // Open with autodetect support, may return 'better' device
01267     dev.replace( dev->autodetect_open() );
01268 
01269     // Report if type has changed
01270     if ((type || print_type_only) && oldinfo.dev_type != dev->get_dev_type())
01271       pout("%s: Device open changed type from '%s' to '%s'\n",
01272         dev->get_info_name(), oldinfo.dev_type.c_str(), dev->get_dev_type());
01273   }
01274   if (!dev->is_open()) {
01275     pout("Smartctl open device: %s failed: %s\n", dev->get_info_name(), dev->get_errmsg());
01276     return FAILDEV;
01277   }
01278 
01279   // now call appropriate ATA or SCSI routine
01280   int retval = 0;
01281   if (print_type_only)
01282     pout("%s: Device of type '%s' [%s] opened\n",
01283          dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
01284   else if (dev->is_ata())
01285     retval = ataPrintMain(dev->to_ata(), ataopts);
01286   else if (dev->is_scsi())
01287     retval = scsiPrintMain(dev->to_scsi(), scsiopts);
01288   else
01289     // we should never fall into this branch!
01290     pout("%s: Neither ATA nor SCSI device\n", dev->get_info_name());
01291 
01292   dev->close();
01293   return retval;
01294 }
01295 
01296 
01297 // Main program
01298 int main(int argc, char **argv)
01299 {
01300   int status;
01301   try {
01302     // Do the real work ...
01303     status = main_worker(argc, argv);
01304   }
01305   catch (int ex) {
01306     // EXIT(status) arrives here
01307     status = ex;
01308   }
01309   catch (const std::bad_alloc & /*ex*/) {
01310     // Memory allocation failed (also thrown by std::operator new)
01311     printf("Smartctl: Out of memory\n");
01312     status = FAILCMD;
01313   }
01314   catch (const std::exception & ex) {
01315     // Other fatal errors
01316     printf("Smartctl: Exception: %s\n", ex.what());
01317     status = FAILCMD;
01318   }
01319   return status;
01320 }
01321