Annotation of embedaddon/smartmontools/smartctl.cpp, revision 1.1.1.4
1.1 misho 1: /*
2: * smartctl.cpp
3: *
4: * Home page of code is: http://smartmontools.sourceforge.net
5: *
6: * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
1.1.1.2 misho 7: * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
1.1 misho 8: * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
9: *
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)
13: * any later version.
14: *
15: * You should have received a copy of the GNU General Public License
1.1.1.3 misho 16: * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
1.1 misho 17: *
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/
22: *
23: */
24:
25: #include <errno.h>
26: #include <stdio.h>
27: #include <sys/types.h>
28: #include <string.h>
29: #include <stdarg.h>
30: #include <stdexcept>
31: #include <getopt.h>
32:
33: #include "config.h"
34:
35: #ifdef HAVE_UNISTD_H
36: #include <unistd.h>
37: #endif
38:
39: #if defined(__FreeBSD__)
40: #include <sys/param.h>
41: #endif
42:
43: #include "int64.h"
44: #include "atacmds.h"
45: #include "dev_interface.h"
46: #include "ataprint.h"
47: #include "knowndrives.h"
48: #include "scsicmds.h"
49: #include "scsiprint.h"
50: #include "smartctl.h"
51: #include "utility.h"
52:
1.1.1.4 ! misho 53: const char * smartctl_cpp_cvsid = "$Id: smartctl.cpp 3826 2013-07-06 21:57:29Z samm2 $"
1.1 misho 54: CONFIG_H_CVSID SMARTCTL_H_CVSID;
55:
56: // Globals to control printing
57: bool printing_is_switchable = false;
58: bool printing_is_off = false;
59:
60: static void printslogan()
61: {
62: pout("%s\n", format_version_info("smartctl").c_str());
63: }
64:
65: static void UsageSummary()
66: {
67: pout("\nUse smartctl -h to get a usage summary\n\n");
68: return;
69: }
70:
1.1.1.2 misho 71: static std::string getvalidarglist(int opt);
1.1 misho 72:
73: /* void prints help information for command syntax */
74: static void Usage()
75: {
76: printf("Usage: smartctl [options] device\n\n");
77: printf(
78: "============================================ SHOW INFORMATION OPTIONS =====\n\n"
79: " -h, --help, --usage\n"
80: " Display this help and exit\n\n"
81: " -V, --version, --copyright, --license\n"
82: " Print license, copyright, and version information and exit\n\n"
1.1.1.2 misho 83: " -i, --info\n"
1.1 misho 84: " Show identity information for device\n\n"
1.1.1.3 misho 85: " --identify[=[w][nvb]]\n"
86: " Show words and bits from IDENTIFY DEVICE data (ATA)\n\n"
1.1.1.2 misho 87: " -g NAME, --get=NAME\n"
1.1.1.4 ! misho 88: " Get device setting: all, aam, apm, lookahead, security, wcache, rcache, wcreorder\n\n"
1.1.1.2 misho 89: " -a, --all\n"
1.1 misho 90: " Show all SMART information for device\n\n"
91: " -x, --xall\n"
92: " Show all information for device\n\n"
93: " --scan\n"
94: " Scan for devices\n\n"
95: " --scan-open\n"
96: " Scan for devices and try to open each device\n\n"
97: );
98: printf(
99: "================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n"
100: " -q TYPE, --quietmode=TYPE (ATA)\n"
101: " Set smartctl quiet mode to one of: errorsonly, silent, noserial\n\n"
102: " -d TYPE, --device=TYPE\n"
103: " Specify device type to one of: %s\n\n"
104: " -T TYPE, --tolerance=TYPE (ATA)\n"
105: " Tolerance: normal, conservative, permissive, verypermissive\n\n"
106: " -b TYPE, --badsum=TYPE (ATA)\n"
107: " Set action on bad checksum to one of: warn, exit, ignore\n\n"
108: " -r TYPE, --report=TYPE\n"
109: " Report transactions (see man page)\n\n"
110: " -n MODE, --nocheck=MODE (ATA)\n"
111: " No check if: never, sleep, standby, idle (see man page)\n\n",
112: getvalidarglist('d').c_str()); // TODO: Use this function also for other options ?
113: printf(
114: "============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n"
115: " -s VALUE, --smart=VALUE\n"
116: " Enable/disable SMART on device (on/off)\n\n"
117: " -o VALUE, --offlineauto=VALUE (ATA)\n"
118: " Enable/disable automatic offline testing on device (on/off)\n\n"
119: " -S VALUE, --saveauto=VALUE (ATA)\n"
120: " Enable/disable Attribute autosave on device (on/off)\n\n"
1.1.1.2 misho 121: " -s NAME[,VALUE], --set=NAME[,VALUE]\n"
122: " Enable/disable/change device setting: aam,[N|off], apm,[N|off],\n"
123: " lookahead,[on|off], security-freeze, standby,[N|off|now],\n"
1.1.1.4 ! misho 124: " wcache,[on|off], rcache,[on|off], wcreorder,[on|off]\n\n"
1.1 misho 125: );
126: printf(
127: "======================================= READ AND DISPLAY DATA OPTIONS =====\n\n"
128: " -H, --health\n"
129: " Show device SMART health status\n\n"
130: " -c, --capabilities (ATA)\n"
131: " Show device SMART capabilities\n\n"
132: " -A, --attributes\n"
133: " Show device SMART vendor-specific Attributes and values\n\n"
134: " -f FORMAT, --format=FORMAT (ATA)\n"
1.1.1.2 misho 135: " Set output format for attributes: old, brief, hex[,id|val]\n\n"
1.1 misho 136: " -l TYPE, --log=TYPE\n"
137: " Show device log. TYPE: error, selftest, selective, directory[,g|s],\n"
138: " xerror[,N][,error], xselftest[,N][,selftest],\n"
139: " background, sasphy[,reset], sataphy[,reset],\n"
140: " scttemp[sts,hist], scttempint,N[,p],\n"
141: " scterc[,N,M], devstat[,N], ssd,\n"
142: " gplog,N[,RANGE], smartlog,N[,RANGE]\n\n"
143: " -v N,OPTION , --vendorattribute=N,OPTION (ATA)\n"
144: " Set display OPTION for vendor Attribute N (see man page)\n\n"
145: " -F TYPE, --firmwarebug=TYPE (ATA)\n"
1.1.1.3 misho 146: " Use firmware bug workaround:\n"
147: " %s, swapid\n\n"
1.1 misho 148: " -P TYPE, --presets=TYPE (ATA)\n"
149: " Drive-specific presets: use, ignore, show, showall\n\n"
150: " -B [+]FILE, --drivedb=[+]FILE (ATA)\n"
151: " Read and replace [add] drive database from FILE\n"
152: " [default is +%s",
1.1.1.3 misho 153: get_valid_firmwarebug_args(),
1.1 misho 154: get_drivedb_path_add()
155: );
156: #ifdef SMARTMONTOOLS_DRIVEDBDIR
157: printf(
158: "\n"
159: " and then %s",
160: get_drivedb_path_default()
161: );
162: #endif
163: printf(
164: "]\n\n"
165: "============================================ DEVICE SELF-TEST OPTIONS =====\n\n"
166: " -t TEST, --test=TEST\n"
1.1.1.2 misho 167: " Run test. TEST: offline, short, long, conveyance, force, vendor,N,\n"
1.1 misho 168: " select,M-N, pending,N, afterselect,[on|off]\n\n"
169: " -C, --captive\n"
170: " Do test in captive mode (along with -t)\n\n"
171: " -X, --abort\n"
172: " Abort any non-captive test on device\n\n"
173: );
174: std::string examples = smi()->get_app_examples("smartctl");
175: if (!examples.empty())
176: printf("%s\n", examples.c_str());
177: }
178:
1.1.1.2 misho 179: // Values for --long only options, see parse_options()
1.1.1.3 misho 180: enum { opt_identify = 1000, opt_scan, opt_scan_open, opt_set, opt_smart };
1.1.1.2 misho 181:
1.1 misho 182: /* Returns a string containing a formatted list of the valid arguments
183: to the option opt or empty on failure. Note 'v' case different */
1.1.1.2 misho 184: static std::string getvalidarglist(int opt)
1.1 misho 185: {
186: switch (opt) {
187: case 'q':
188: return "errorsonly, silent, noserial";
189: case 'd':
190: return smi()->get_valid_dev_types_str() + ", auto, test";
191: case 'T':
192: return "normal, conservative, permissive, verypermissive";
193: case 'b':
194: return "warn, exit, ignore";
195: case 'r':
196: return "ioctl[,N], ataioctl[,N], scsiioctl[,N]";
1.1.1.2 misho 197: case opt_smart:
1.1 misho 198: case 'o':
199: case 'S':
200: return "on, off";
201: case 'l':
202: return "error, selftest, selective, directory[,g|s], "
203: "xerror[,N][,error], xselftest[,N][,selftest], "
204: "background, sasphy[,reset], sataphy[,reset], "
205: "scttemp[sts,hist], scttempint,N[,p], "
206: "scterc[,N,M], devstat[,N], ssd, "
207: "gplog,N[,RANGE], smartlog,N[,RANGE]";
208:
209: case 'P':
210: return "use, ignore, show, showall";
211: case 't':
1.1.1.2 misho 212: return "offline, short, long, conveyance, force, vendor,N, select,M-N, "
1.1 misho 213: "pending,N, afterselect,[on|off]";
214: case 'F':
1.1.1.3 misho 215: return std::string(get_valid_firmwarebug_args()) + ", swapid";
1.1 misho 216: case 'n':
217: return "never, sleep, standby, idle";
218: case 'f':
1.1.1.2 misho 219: return "old, brief, hex[,id|val]";
220: case 'g':
1.1.1.4 ! misho 221: return "aam, apm, lookahead, security, wcache, rcache, wcreorder";
1.1.1.2 misho 222: case opt_set:
223: return "aam,[N|off], apm,[N|off], lookahead,[on|off], security-freeze, "
1.1.1.4 ! misho 224: "standby,[N|off|now], wcache,[on|off], rcache,[on|off], wcreorder,[on|off]";
1.1.1.2 misho 225: case 's':
226: return getvalidarglist(opt_smart)+", "+getvalidarglist(opt_set);
1.1.1.3 misho 227: case opt_identify:
228: return "n, wn, w, v, wv, wb";
1.1 misho 229: case 'v':
230: default:
231: return "";
232: }
233: }
234:
235: /* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where
236: <LIST> is the list of valid arguments for option opt. */
1.1.1.2 misho 237: static void printvalidarglistmessage(int opt)
1.1 misho 238: {
239: if (opt=='v'){
240: pout("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n",
241: create_vendor_attribute_arg_list().c_str());
242: }
243: else {
244: // getvalidarglist() might produce a multiline or single line string. We
245: // need to figure out which to get the formatting right.
246: std::string s = getvalidarglist(opt);
247: char separator = strchr(s.c_str(), '\n') ? '\n' : ' ';
248: pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, s.c_str(), separator);
249: }
250:
251: return;
252: }
253:
254: // Checksum error mode
255: enum checksum_err_mode_t {
256: CHECKSUM_ERR_WARN, CHECKSUM_ERR_EXIT, CHECKSUM_ERR_IGNORE
257: };
258:
259: static checksum_err_mode_t checksum_err_mode = CHECKSUM_ERR_WARN;
260:
261: static void scan_devices(const char * type, bool with_open, char ** argv);
262:
1.1.1.2 misho 263:
1.1 misho 264: /* Takes command options and sets features to be run */
265: static const char * parse_options(int argc, char** argv,
1.1.1.2 misho 266: ata_print_options & ataopts, scsi_print_options & scsiopts,
267: bool & print_type_only)
1.1 misho 268: {
269: // Please update getvalidarglist() if you edit shortopts
1.1.1.2 misho 270: const char *shortopts = "h?Vq:d:T:b:r:s:o:S:HcAl:iaxv:P:t:CXF:n:B:f:g:";
1.1 misho 271: // Please update getvalidarglist() if you edit longopts
272: struct option longopts[] = {
273: { "help", no_argument, 0, 'h' },
274: { "usage", no_argument, 0, 'h' },
275: { "version", no_argument, 0, 'V' },
276: { "copyright", no_argument, 0, 'V' },
277: { "license", no_argument, 0, 'V' },
278: { "quietmode", required_argument, 0, 'q' },
279: { "device", required_argument, 0, 'd' },
280: { "tolerance", required_argument, 0, 'T' },
281: { "badsum", required_argument, 0, 'b' },
282: { "report", required_argument, 0, 'r' },
1.1.1.2 misho 283: { "smart", required_argument, 0, opt_smart },
1.1 misho 284: { "offlineauto", required_argument, 0, 'o' },
285: { "saveauto", required_argument, 0, 'S' },
286: { "health", no_argument, 0, 'H' },
287: { "capabilities", no_argument, 0, 'c' },
288: { "attributes", no_argument, 0, 'A' },
289: { "log", required_argument, 0, 'l' },
290: { "info", no_argument, 0, 'i' },
291: { "all", no_argument, 0, 'a' },
292: { "xall", no_argument, 0, 'x' },
293: { "vendorattribute", required_argument, 0, 'v' },
294: { "presets", required_argument, 0, 'P' },
295: { "test", required_argument, 0, 't' },
296: { "captive", no_argument, 0, 'C' },
297: { "abort", no_argument, 0, 'X' },
298: { "firmwarebug", required_argument, 0, 'F' },
299: { "nocheck", required_argument, 0, 'n' },
300: { "drivedb", required_argument, 0, 'B' },
301: { "format", required_argument, 0, 'f' },
1.1.1.2 misho 302: { "get", required_argument, 0, 'g' },
1.1.1.3 misho 303: { "identify", optional_argument, 0, opt_identify },
1.1.1.2 misho 304: { "set", required_argument, 0, opt_set },
1.1 misho 305: { "scan", no_argument, 0, opt_scan },
306: { "scan-open", no_argument, 0, opt_scan_open },
307: { 0, 0, 0, 0 }
308: };
309:
310: char extraerror[256];
311: memset(extraerror, 0, sizeof(extraerror));
312: opterr=optopt=0;
313:
314: const char * type = 0; // set to -d optarg
315: bool no_defaultdb = false; // set true on '-B FILE'
316: bool output_format_set = false; // set true on '-f FORMAT'
317: int scan = 0; // set by --scan, --scan-open
318: bool badarg = false, captive = false;
319: int testcnt = 0; // number of self-tests requested
320:
321: int optchar;
322: char *arg;
323:
324: while ((optchar = getopt_long(argc, argv, shortopts, longopts, 0)) != -1) {
325: switch (optchar){
326: case 'V':
327: printing_is_off = false;
328: pout("%s", format_version_info("smartctl", true /*full*/).c_str());
329: EXIT(0);
330: break;
331: case 'q':
332: if (!strcmp(optarg,"errorsonly")) {
333: printing_is_switchable = true;
334: printing_is_off = false;
335: } else if (!strcmp(optarg,"silent")) {
336: printing_is_switchable = false;
337: printing_is_off = true;
338: } else if (!strcmp(optarg,"noserial")) {
339: dont_print_serial_number = true;
340: } else {
341: badarg = true;
342: }
343: break;
344: case 'd':
1.1.1.2 misho 345: if (!strcmp(optarg, "test"))
346: print_type_only = true;
347: else
348: type = (strcmp(optarg, "auto") ? optarg : (char *)0);
1.1 misho 349: break;
350: case 'T':
351: if (!strcmp(optarg,"normal")) {
352: failuretest_conservative = false;
353: failuretest_permissive = 0;
354: } else if (!strcmp(optarg,"conservative")) {
355: failuretest_conservative = true;
356: } else if (!strcmp(optarg,"permissive")) {
357: if (failuretest_permissive < 0xff)
358: failuretest_permissive++;
359: } else if (!strcmp(optarg,"verypermissive")) {
360: failuretest_permissive = 0xff;
361: } else {
362: badarg = true;
363: }
364: break;
365: case 'b':
366: if (!strcmp(optarg,"warn")) {
367: checksum_err_mode = CHECKSUM_ERR_WARN;
368: } else if (!strcmp(optarg,"exit")) {
369: checksum_err_mode = CHECKSUM_ERR_EXIT;
370: } else if (!strcmp(optarg,"ignore")) {
371: checksum_err_mode = CHECKSUM_ERR_IGNORE;
372: } else {
373: badarg = true;
374: }
375: break;
376: case 'r':
377: {
378: int i;
379: char *s;
380:
381: // split_report_arg() may modify its first argument string, so use a
382: // copy of optarg in case we want optarg for an error message.
383: if (!(s = strdup(optarg))) {
384: throw std::bad_alloc();
385: }
386: if (split_report_arg(s, &i)) {
387: badarg = true;
388: } else if (!strcmp(s,"ioctl")) {
389: ata_debugmode = scsi_debugmode = i;
390: } else if (!strcmp(s,"ataioctl")) {
391: ata_debugmode = i;
392: } else if (!strcmp(s,"scsiioctl")) {
393: scsi_debugmode = i;
394: } else {
395: badarg = true;
396: }
397: free(s);
398: }
399: break;
1.1.1.2 misho 400:
1.1 misho 401: case 's':
1.1.1.2 misho 402: case opt_smart: // --smart
1.1 misho 403: if (!strcmp(optarg,"on")) {
404: ataopts.smart_enable = scsiopts.smart_enable = true;
405: ataopts.smart_disable = scsiopts.smart_disable = false;
406: } else if (!strcmp(optarg,"off")) {
407: ataopts.smart_disable = scsiopts.smart_disable = true;
408: ataopts.smart_enable = scsiopts.smart_enable = false;
1.1.1.2 misho 409: } else if (optchar == 's') {
410: goto case_s_continued; // --set, see below
1.1 misho 411: } else {
412: badarg = true;
413: }
414: break;
1.1.1.2 misho 415:
1.1 misho 416: case 'o':
417: if (!strcmp(optarg,"on")) {
418: ataopts.smart_auto_offl_enable = true;
419: ataopts.smart_auto_offl_disable = false;
420: } else if (!strcmp(optarg,"off")) {
421: ataopts.smart_auto_offl_disable = true;
422: ataopts.smart_auto_offl_enable = false;
423: } else {
424: badarg = true;
425: }
426: break;
427: case 'S':
428: if (!strcmp(optarg,"on")) {
429: ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = true;
430: ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = false;
431: } else if (!strcmp(optarg,"off")) {
432: ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = true;
433: ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = false;
434: } else {
435: badarg = true;
436: }
437: break;
438: case 'H':
439: ataopts.smart_check_status = scsiopts.smart_check_status = true;
440: scsiopts.smart_ss_media_log = true;
441: break;
442: case 'F':
1.1.1.3 misho 443: if (!strcmp(optarg, "swapid"))
1.1 misho 444: ataopts.fix_swapped_id = true;
1.1.1.3 misho 445: else if (!parse_firmwarebug_def(optarg, ataopts.firmwarebugs))
1.1 misho 446: badarg = true;
447: break;
448: case 'c':
449: ataopts.smart_general_values = true;
450: break;
451: case 'A':
452: ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true;
453: break;
454: case 'l':
455: if (!strcmp(optarg,"error")) {
456: ataopts.smart_error_log = scsiopts.smart_error_log = true;
457: } else if (!strcmp(optarg,"selftest")) {
458: ataopts.smart_selftest_log = scsiopts.smart_selftest_log = true;
459: } else if (!strcmp(optarg, "selective")) {
460: ataopts.smart_selective_selftest_log = true;
461: } else if (!strcmp(optarg,"directory")) {
462: ataopts.smart_logdir = ataopts.gp_logdir = true; // SMART+GPL
463: } else if (!strcmp(optarg,"directory,s")) {
464: ataopts.smart_logdir = true; // SMART
465: } else if (!strcmp(optarg,"directory,g")) {
466: ataopts.gp_logdir = true; // GPL
467: } else if (!strcmp(optarg,"sasphy")) {
468: scsiopts.sasphy = true;
469: } else if (!strcmp(optarg,"sasphy,reset")) {
470: scsiopts.sasphy = scsiopts.sasphy_reset = true;
471: } else if (!strcmp(optarg,"sataphy")) {
472: ataopts.sataphy = true;
473: } else if (!strcmp(optarg,"sataphy,reset")) {
474: ataopts.sataphy = ataopts.sataphy_reset = true;
475: } else if (!strcmp(optarg,"background")) {
476: scsiopts.smart_background_log = true;
477: } else if (!strcmp(optarg,"ssd")) {
478: ataopts.devstat_ssd_page = true;
479: scsiopts.smart_ss_media_log = true;
480: } else if (!strcmp(optarg,"scterc")) {
481: ataopts.sct_erc_get = true;
482: } else if (!strcmp(optarg,"scttemp")) {
483: ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
484: } else if (!strcmp(optarg,"scttempsts")) {
485: ataopts.sct_temp_sts = true;
486: } else if (!strcmp(optarg,"scttemphist")) {
487: ataopts.sct_temp_hist = true;
488:
489: } else if (!strncmp(optarg, "scttempint,", sizeof("scstempint,")-1)) {
490: unsigned interval = 0; int n1 = -1, n2 = -1, len = strlen(optarg);
491: if (!( sscanf(optarg,"scttempint,%u%n,p%n", &interval, &n1, &n2) == 1
492: && 0 < interval && interval <= 0xffff && (n1 == len || n2 == len))) {
1.1.1.3 misho 493: snprintf(extraerror, sizeof(extraerror), "Option -l scttempint,N[,p] must have positive integer N\n");
1.1 misho 494: badarg = true;
495: }
496: ataopts.sct_temp_int = interval;
497: ataopts.sct_temp_int_pers = (n2 == len);
498:
499: } else if (!strncmp(optarg, "devstat", sizeof("devstat")-1)) {
500: int n1 = -1, n2 = -1, len = strlen(optarg);
501: unsigned val = ~0;
502: sscanf(optarg, "devstat%n,%u%n", &n1, &val, &n2);
503: if (n1 == len)
504: ataopts.devstat_all_pages = true;
505: else if (n2 == len && val <= 255)
506: ataopts.devstat_pages.push_back(val);
507: else
508: badarg = true;
509:
510: } else if (!strncmp(optarg, "xerror", sizeof("xerror")-1)) {
511: int n1 = -1, n2 = -1, len = strlen(optarg);
512: unsigned val = 8;
513: sscanf(optarg, "xerror%n,error%n", &n1, &n2);
514: if (!(n1 == len || n2 == len)) {
515: n1 = n2 = -1;
516: sscanf(optarg, "xerror,%u%n,error%n", &val, &n1, &n2);
517: }
518: if ((n1 == len || n2 == len) && val > 0) {
519: ataopts.smart_ext_error_log = val;
520: ataopts.retry_error_log = (n2 == len);
521: }
522: else
523: badarg = true;
524:
525: } else if (!strncmp(optarg, "xselftest", sizeof("xselftest")-1)) {
526: int n1 = -1, n2 = -1, len = strlen(optarg);
527: unsigned val = 25;
528: sscanf(optarg, "xselftest%n,selftest%n", &n1, &n2);
529: if (!(n1 == len || n2 == len)) {
530: n1 = n2 = -1;
531: sscanf(optarg, "xselftest,%u%n,selftest%n", &val, &n1, &n2);
532: }
533: if ((n1 == len || n2 == len) && val > 0) {
534: ataopts.smart_ext_selftest_log = val;
535: ataopts.retry_selftest_log = (n2 == len);
536: }
537: else
538: badarg = true;
539:
540: } else if (!strncmp(optarg, "scterc,", sizeof("scterc,")-1)) {
541: unsigned rt = ~0, wt = ~0; int n = -1;
542: sscanf(optarg,"scterc,%u,%u%n", &rt, &wt, &n);
543: if (n == (int)strlen(optarg) && rt <= 999 && wt <= 999) {
544: ataopts.sct_erc_set = true;
545: ataopts.sct_erc_readtime = rt;
546: ataopts.sct_erc_writetime = wt;
547: }
548: else {
1.1.1.3 misho 549: snprintf(extraerror, sizeof(extraerror), "Option -l scterc,[READTIME,WRITETIME] syntax error\n");
1.1 misho 550: badarg = true;
551: }
552: } else if ( !strncmp(optarg, "gplog," , sizeof("gplog," )-1)
553: || !strncmp(optarg, "smartlog,", sizeof("smartlog,")-1)) {
554: unsigned logaddr = ~0U; unsigned page = 0, nsectors = 1; char sign = 0;
555: int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
556: sscanf(optarg, "%*[a-z],0x%x%n,%u%n%c%u%n",
557: &logaddr, &n1, &page, &n2, &sign, &nsectors, &n3);
558: if (len > n2 && n3 == -1 && !strcmp(optarg+n2, "-max")) {
559: nsectors = ~0U; sign = '+'; n3 = len;
560: }
561: bool gpl = (optarg[0] == 'g');
562: const char * erropt = (gpl ? "gplog" : "smartlog");
563: if (!( n1 == len || n2 == len
564: || (n3 == len && (sign == '+' || sign == '-')))) {
1.1.1.3 misho 565: snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] syntax error\n", erropt);
1.1 misho 566: badarg = true;
567: }
568: else if (!( logaddr <= 0xff && page <= (gpl ? 0xffffU : 0x00ffU)
569: && 0 < nsectors
570: && (nsectors <= (gpl ? 0xffffU : 0xffU) || nsectors == ~0U)
571: && (sign != '-' || page <= nsectors) )) {
1.1.1.3 misho 572: snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] parameter out of range\n", erropt);
1.1 misho 573: badarg = true;
574: }
575: else {
576: ata_log_request req;
577: req.gpl = gpl; req.logaddr = logaddr; req.page = page;
578: req.nsectors = (sign == '-' ? nsectors-page+1 : nsectors);
579: ataopts.log_requests.push_back(req);
580: }
581: } else {
582: badarg = true;
583: }
584: break;
585: case 'i':
586: ataopts.drive_info = scsiopts.drive_info = true;
587: break;
1.1.1.3 misho 588:
589: case opt_identify:
590: ataopts.identify_word_level = ataopts.identify_bit_level = 0;
591: if (optarg) {
592: for (int i = 0; optarg[i]; i++) {
593: switch (optarg[i]) {
594: case 'w': ataopts.identify_word_level = 1; break;
595: case 'n': ataopts.identify_bit_level = -1; break;
596: case 'v': ataopts.identify_bit_level = 1; break;
597: case 'b': ataopts.identify_bit_level = 2; break;
598: default: badarg = true;
599: }
600: }
601: }
602: break;
603:
1.1 misho 604: case 'a':
605: ataopts.drive_info = scsiopts.drive_info = true;
606: ataopts.smart_check_status = scsiopts.smart_check_status = true;
607: ataopts.smart_general_values = true;
608: ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true;
609: ataopts.smart_error_log = scsiopts.smart_error_log = true;
610: ataopts.smart_selftest_log = scsiopts.smart_selftest_log = true;
611: ataopts.smart_selective_selftest_log = true;
612: /* scsiopts.smart_background_log = true; */
613: scsiopts.smart_ss_media_log = true;
614: break;
615: case 'x':
616: ataopts.drive_info = scsiopts.drive_info = true;
617: ataopts.smart_check_status = scsiopts.smart_check_status = true;
618: ataopts.smart_general_values = true;
619: ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true;
620: ataopts.smart_ext_error_log = 8;
621: ataopts.retry_error_log = true;
622: ataopts.smart_ext_selftest_log = 25;
623: ataopts.retry_selftest_log = true;
624: scsiopts.smart_error_log = scsiopts.smart_selftest_log = true;
625: ataopts.smart_selective_selftest_log = true;
626: ataopts.smart_logdir = ataopts.gp_logdir = true;
627: ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
628: ataopts.sct_erc_get = true;
1.1.1.4 ! misho 629: ataopts.sct_wcache_reorder_get = true;
1.1.1.3 misho 630: ataopts.devstat_all_pages = true;
1.1 misho 631: ataopts.sataphy = true;
1.1.1.2 misho 632: ataopts.get_set_used = true;
633: ataopts.get_aam = ataopts.get_apm = true;
634: ataopts.get_security = true;
635: ataopts.get_lookahead = ataopts.get_wcache = true;
1.1.1.3 misho 636: scsiopts.get_rcd = scsiopts.get_wce = true;
1.1 misho 637: scsiopts.smart_background_log = true;
638: scsiopts.smart_ss_media_log = true;
639: scsiopts.sasphy = true;
640: if (!output_format_set)
1.1.1.2 misho 641: ataopts.output_format |= ata_print_options::FMT_BRIEF;
1.1 misho 642: break;
643: case 'v':
644: // parse vendor-specific definitions of attributes
645: if (!strcmp(optarg,"help")) {
646: printing_is_off = false;
647: printslogan();
648: pout("The valid arguments to -v are:\n\thelp\n%s\n",
649: create_vendor_attribute_arg_list().c_str());
650: EXIT(0);
651: }
652: if (!parse_attribute_def(optarg, ataopts.attribute_defs, PRIOR_USER))
653: badarg = true;
654: break;
655: case 'P':
656: if (!strcmp(optarg, "use")) {
657: ataopts.ignore_presets = false;
658: } else if (!strcmp(optarg, "ignore")) {
659: ataopts.ignore_presets = true;
660: } else if (!strcmp(optarg, "show")) {
661: ataopts.show_presets = true;
662: } else if (!strcmp(optarg, "showall")) {
663: if (!no_defaultdb && !read_default_drive_databases())
664: EXIT(FAILCMD);
665: if (optind < argc) { // -P showall MODEL [FIRMWARE]
666: int cnt = showmatchingpresets(argv[optind], (optind+1<argc ? argv[optind+1] : NULL));
667: EXIT(cnt); // report #matches
668: }
669: if (showallpresets())
670: EXIT(FAILCMD); // report regexp syntax error
671: EXIT(0);
672: } else {
673: badarg = true;
674: }
675: break;
676: case 't':
677: if (!strcmp(optarg,"offline")) {
678: testcnt++;
679: ataopts.smart_selftest_type = OFFLINE_FULL_SCAN;
680: scsiopts.smart_default_selftest = true;
681: } else if (!strcmp(optarg,"short")) {
682: testcnt++;
683: ataopts.smart_selftest_type = SHORT_SELF_TEST;
684: scsiopts.smart_short_selftest = true;
685: } else if (!strcmp(optarg,"long")) {
686: testcnt++;
687: ataopts.smart_selftest_type = EXTEND_SELF_TEST;
688: scsiopts.smart_extend_selftest = true;
689: } else if (!strcmp(optarg,"conveyance")) {
690: testcnt++;
691: ataopts.smart_selftest_type = CONVEYANCE_SELF_TEST;
1.1.1.2 misho 692: } else if (!strcmp(optarg,"force")) {
693: ataopts.smart_selftest_force = true;
1.1.1.3 misho 694: scsiopts.smart_selftest_force = true;
1.1 misho 695: } else if (!strcmp(optarg,"afterselect,on")) {
696: // scan remainder of disk after doing selected segment
697: ataopts.smart_selective_args.scan_after_select = 2;
698: } else if (!strcmp(optarg,"afterselect,off")) {
699: // don't scan remainder of disk after doing selected segments
700: ataopts.smart_selective_args.scan_after_select = 1;
701: } else if (!strncmp(optarg,"pending,",strlen("pending,"))) {
702: // parse number of minutes that test should be pending
703: int i;
704: char *tailptr=NULL;
705: errno=0;
706: i=(int)strtol(optarg+strlen("pending,"), &tailptr, 10);
707: if (errno || *tailptr != '\0') {
1.1.1.3 misho 708: snprintf(extraerror, sizeof(extraerror), "Option -t pending,N requires N to be a non-negative integer\n");
1.1 misho 709: badarg = true;
710: } else if (i<0 || i>65535) {
1.1.1.3 misho 711: snprintf(extraerror, sizeof(extraerror), "Option -t pending,N (N=%d) must have 0 <= N <= 65535\n", i);
1.1 misho 712: badarg = true;
713: } else {
714: ataopts.smart_selective_args.pending_time = i+1;
715: }
716: } else if (!strncmp(optarg,"select",strlen("select"))) {
717: if (ataopts.smart_selective_args.num_spans == 0)
718: testcnt++;
719: // parse range of LBAs to test
720: uint64_t start, stop; int mode;
721: if (split_selective_arg(optarg, &start, &stop, &mode)) {
1.1.1.3 misho 722: snprintf(extraerror, sizeof(extraerror), "Option -t select,M-N must have non-negative integer M and N\n");
1.1 misho 723: badarg = true;
724: } else {
725: if (ataopts.smart_selective_args.num_spans >= 5 || start > stop) {
726: if (start > stop) {
1.1.1.3 misho 727: snprintf(extraerror, sizeof(extraerror), "ERROR: Start LBA (%"PRIu64") > ending LBA (%"PRId64") in argument \"%s\"\n",
1.1 misho 728: start, stop, optarg);
729: } else {
1.1.1.3 misho 730: snprintf(extraerror, sizeof(extraerror),"ERROR: No more than five selective self-test spans may be"
1.1 misho 731: " defined\n");
732: }
733: badarg = true;
734: }
735: ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].start = start;
736: ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].end = stop;
737: ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].mode = mode;
738: ataopts.smart_selective_args.num_spans++;
739: ataopts.smart_selftest_type = SELECTIVE_SELF_TEST;
740: }
741: } else if (!strncmp(optarg, "scttempint", sizeof("scstempint")-1)) {
1.1.1.3 misho 742: snprintf(extraerror, sizeof(extraerror), "-t scttempint is no longer supported, use -l scttempint instead\n");
1.1 misho 743: badarg = true;
744: } else if (!strncmp(optarg, "vendor,", sizeof("vendor,")-1)) {
745: unsigned subcmd = ~0U; int n = -1;
746: if (!( sscanf(optarg, "%*[a-z],0x%x%n", &subcmd, &n) == 1
747: && subcmd <= 0xff && n == (int)strlen(optarg))) {
1.1.1.3 misho 748: snprintf(extraerror, sizeof(extraerror), "Option -t vendor,0xNN syntax error\n");
1.1 misho 749: badarg = true;
750: }
751: else
752: ataopts.smart_selftest_type = subcmd;
753: } else {
754: badarg = true;
755: }
756: break;
757: case 'C':
758: captive = true;
759: break;
760: case 'X':
761: testcnt++;
762: scsiopts.smart_selftest_abort = true;
763: ataopts.smart_selftest_type = ABORT_SELF_TEST;
764: break;
765: case 'n':
766: // skip disk check if in low-power mode
767: if (!strcmp(optarg, "never"))
768: ataopts.powermode = 1; // do not skip, but print mode
769: else if (!strcmp(optarg, "sleep"))
770: ataopts.powermode = 2;
771: else if (!strcmp(optarg, "standby"))
772: ataopts.powermode = 3;
773: else if (!strcmp(optarg, "idle"))
774: ataopts.powermode = 4;
775: else
776: badarg = true;
777: break;
778: case 'f':
1.1.1.2 misho 779: if (!strcmp(optarg, "old")) {
780: ataopts.output_format &= ~ata_print_options::FMT_BRIEF;
781: output_format_set = true;
1.1 misho 782: }
1.1.1.2 misho 783: else if (!strcmp(optarg, "brief")) {
784: ataopts.output_format |= ata_print_options::FMT_BRIEF;
785: output_format_set = true;
786: }
787: else if (!strcmp(optarg, "hex"))
788: ataopts.output_format |= ata_print_options::FMT_HEX_ID
789: | ata_print_options::FMT_HEX_VAL;
790: else if (!strcmp(optarg, "hex,id"))
791: ataopts.output_format |= ata_print_options::FMT_HEX_ID;
792: else if (!strcmp(optarg, "hex,val"))
793: ataopts.output_format |= ata_print_options::FMT_HEX_VAL;
794: else
795: badarg = true;
1.1 misho 796: break;
797: case 'B':
798: {
799: const char * path = optarg;
800: if (*path == '+' && path[1])
801: path++;
802: else
803: no_defaultdb = true;
804: if (!read_drive_database(path))
805: EXIT(FAILCMD);
806: }
807: break;
808: case 'h':
809: printing_is_off = false;
810: printslogan();
811: Usage();
812: EXIT(0);
813: break;
814:
1.1.1.2 misho 815: case 'g':
816: case_s_continued: // -s, see above
817: case opt_set: // --set
818: {
819: ataopts.get_set_used = true;
820: bool get = (optchar == 'g');
821: char name[16+1]; unsigned val;
822: int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
823: if (sscanf(optarg, "%16[^,=]%n%*[,=]%n%u%n", name, &n1, &n2, &val, &n3) >= 1
824: && (n1 == len || (!get && n2 > 0))) {
825: bool on = (n2 > 0 && !strcmp(optarg+n2, "on"));
826: bool off = (n2 > 0 && !strcmp(optarg+n2, "off"));
827: if (n3 != len)
828: val = ~0U;
829:
830: if (get && !strcmp(name, "all")) {
831: ataopts.get_aam = ataopts.get_apm = true;
832: ataopts.get_security = true;
833: ataopts.get_lookahead = ataopts.get_wcache = true;
1.1.1.3 misho 834: scsiopts.get_rcd = scsiopts.get_wce = true;
1.1.1.2 misho 835: }
836: else if (!strcmp(name, "aam")) {
837: if (get)
838: ataopts.get_aam = true;
839: else if (off)
840: ataopts.set_aam = -1;
841: else if (val <= 254)
842: ataopts.set_aam = val + 1;
843: else {
1.1.1.3 misho 844: snprintf(extraerror, sizeof(extraerror), "Option -s aam,N must have 0 <= N <= 254\n");
1.1.1.2 misho 845: badarg = true;
846: }
847: }
848: else if (!strcmp(name, "apm")) {
849: if (get)
850: ataopts.get_apm = true;
851: else if (off)
852: ataopts.set_apm = -1;
853: else if (1 <= val && val <= 254)
854: ataopts.set_apm = val + 1;
855: else {
1.1.1.3 misho 856: snprintf(extraerror, sizeof(extraerror), "Option -s apm,N must have 1 <= N <= 254\n");
1.1.1.2 misho 857: badarg = true;
858: }
859: }
860: else if (!strcmp(name, "lookahead")) {
1.1.1.3 misho 861: if (get) {
1.1.1.2 misho 862: ataopts.get_lookahead = true;
1.1.1.3 misho 863: }
1.1.1.2 misho 864: else if (off)
865: ataopts.set_lookahead = -1;
866: else if (on)
867: ataopts.set_lookahead = 1;
868: else
869: badarg = true;
870: }
1.1.1.4 ! misho 871: else if (!strcmp(name, "wcreorder")) {
! 872: if (get) {
! 873: ataopts.sct_wcache_reorder_get = true;
! 874: }
! 875: else if (off)
! 876: ataopts.sct_wcache_reorder_set = -1;
! 877: else if (on)
! 878: ataopts.sct_wcache_reorder_set = 1;
! 879: else
! 880: badarg = true;
! 881: }
1.1.1.3 misho 882: else if (!strcmp(name, "rcache")) {
883: if (get)
884: scsiopts.get_rcd = true;
885: else if (off)
886: scsiopts.set_rcd = -1;
887: else if (on)
888: scsiopts.set_rcd = 1;
889: else
890: badarg = true;
891: }
1.1.1.2 misho 892: else if (get && !strcmp(name, "security")) {
893: ataopts.get_security = true;
894: }
895: else if (!get && !strcmp(optarg, "security-freeze")) {
896: ataopts.set_security_freeze = true;
897: }
898: else if (!get && !strcmp(optarg, "standby,now")) {
899: ataopts.set_standby_now = true;
900: }
901: else if (!get && !strcmp(name, "standby")) {
902: if (off)
903: ataopts.set_standby = 0 + 1;
904: else if (val <= 255)
905: ataopts.set_standby = val + 1;
906: else {
1.1.1.3 misho 907: snprintf(extraerror, sizeof(extraerror), "Option -s standby,N must have 0 <= N <= 255\n");
1.1.1.2 misho 908: badarg = true;
909: }
910: }
911: else if (!strcmp(name, "wcache")) {
1.1.1.3 misho 912: if (get) {
1.1.1.2 misho 913: ataopts.get_wcache = true;
1.1.1.3 misho 914: scsiopts.get_wce = true;
915: }
916: else if (off) {
1.1.1.2 misho 917: ataopts.set_wcache = -1;
1.1.1.3 misho 918: scsiopts.set_wce = -1;
919: }
920: else if (on) {
1.1.1.2 misho 921: ataopts.set_wcache = 1;
1.1.1.3 misho 922: scsiopts.set_wce = 1;
923: }
1.1.1.2 misho 924: else
925: badarg = true;
926: }
927: else
928: badarg = true;
929: }
930: else
931: badarg = true;
932: }
933: break;
934:
1.1 misho 935: case opt_scan:
936: case opt_scan_open:
937: scan = optchar;
938: break;
939:
940: case '?':
941: default:
942: printing_is_off = false;
943: printslogan();
944: // Point arg to the argument in which this option was found.
945: arg = argv[optind-1];
946: // Check whether the option is a long option that doesn't map to -h.
947: if (arg[1] == '-' && optchar != 'h') {
948: // Iff optopt holds a valid option then argument must be missing.
1.1.1.2 misho 949: if (optopt && (optopt >= opt_scan || strchr(shortopts, optopt))) {
1.1 misho 950: pout("=======> ARGUMENT REQUIRED FOR OPTION: %s\n", arg+2);
951: printvalidarglistmessage(optopt);
952: } else
953: pout("=======> UNRECOGNIZED OPTION: %s\n",arg+2);
954: if (extraerror[0])
955: pout("=======> %s", extraerror);
956: UsageSummary();
957: EXIT(FAILCMD);
958: }
1.1.1.2 misho 959: if (0 < optopt && optopt < '~') {
1.1 misho 960: // Iff optopt holds a valid option then argument must be
961: // missing. Note (BA) this logic seems to fail using Solaris
962: // getopt!
963: if (strchr(shortopts, optopt) != NULL) {
964: pout("=======> ARGUMENT REQUIRED FOR OPTION: %c\n", optopt);
965: printvalidarglistmessage(optopt);
966: } else
967: pout("=======> UNRECOGNIZED OPTION: %c\n",optopt);
968: if (extraerror[0])
969: pout("=======> %s", extraerror);
970: UsageSummary();
971: EXIT(FAILCMD);
972: }
973: Usage();
974: EXIT(0);
975: } // closes switch statement to process command-line options
976:
977: // Check to see if option had an unrecognized or incorrect argument.
978: if (badarg) {
979: printslogan();
980: // It would be nice to print the actual option name given by the user
981: // here, but we just print the short form. Please fix this if you know
982: // a clean way to do it.
1.1.1.2 misho 983: char optstr[] = { (char)optchar, 0 };
984: pout("=======> INVALID ARGUMENT TO -%s: %s\n",
1.1.1.3 misho 985: (optchar == opt_identify ? "-identify" :
986: optchar == opt_set ? "-set" :
1.1.1.2 misho 987: optchar == opt_smart ? "-smart" : optstr), optarg);
1.1 misho 988: printvalidarglistmessage(optchar);
989: if (extraerror[0])
990: pout("=======> %s", extraerror);
991: UsageSummary();
992: EXIT(FAILCMD);
993: }
994: }
995:
996: // Special handling of --scan, --scanopen
997: if (scan) {
998: // Read or init drive database to allow USB ID check.
999: if (!no_defaultdb && !read_default_drive_databases())
1000: EXIT(FAILCMD);
1001: scan_devices(type, (scan == opt_scan_open), argv + optind);
1002: EXIT(0);
1003: }
1004:
1005: // At this point we have processed all command-line options. If the
1006: // print output is switchable, then start with the print output
1007: // turned off
1008: if (printing_is_switchable)
1009: printing_is_off = true;
1010:
1011: // error message if user has asked for more than one test
1012: if (testcnt > 1) {
1013: printing_is_off = false;
1014: printslogan();
1015: pout("\nERROR: smartctl can only run a single test type (or abort) at a time.\n");
1016: UsageSummary();
1017: EXIT(FAILCMD);
1018: }
1019:
1020: // error message if user has set selective self-test options without
1021: // asking for a selective self-test
1022: if ( (ataopts.smart_selective_args.pending_time || ataopts.smart_selective_args.scan_after_select)
1023: && !ataopts.smart_selective_args.num_spans) {
1024: printing_is_off = false;
1025: printslogan();
1026: if (ataopts.smart_selective_args.pending_time)
1027: pout("\nERROR: smartctl -t pending,N must be used with -t select,N-M.\n");
1028: else
1029: pout("\nERROR: smartctl -t afterselect,(on|off) must be used with -t select,N-M.\n");
1030: UsageSummary();
1031: EXIT(FAILCMD);
1032: }
1033:
1034: // If captive option was used, change test type if appropriate.
1035: if (captive)
1036: switch (ataopts.smart_selftest_type) {
1037: case SHORT_SELF_TEST:
1038: ataopts.smart_selftest_type = SHORT_CAPTIVE_SELF_TEST;
1039: scsiopts.smart_short_selftest = false;
1040: scsiopts.smart_short_cap_selftest = true;
1041: break;
1042: case EXTEND_SELF_TEST:
1043: ataopts.smart_selftest_type = EXTEND_CAPTIVE_SELF_TEST;
1044: scsiopts.smart_extend_selftest = false;
1045: scsiopts.smart_extend_cap_selftest = true;
1046: break;
1047: case CONVEYANCE_SELF_TEST:
1048: ataopts.smart_selftest_type = CONVEYANCE_CAPTIVE_SELF_TEST;
1049: break;
1050: case SELECTIVE_SELF_TEST:
1051: ataopts.smart_selftest_type = SELECTIVE_CAPTIVE_SELF_TEST;
1052: break;
1053: }
1054:
1055: // From here on, normal operations...
1056: printslogan();
1057:
1058: // Warn if the user has provided no device name
1059: if (argc-optind<1){
1060: pout("ERROR: smartctl requires a device name as the final command-line argument.\n\n");
1061: UsageSummary();
1062: EXIT(FAILCMD);
1063: }
1064:
1065: // Warn if the user has provided more than one device name
1066: if (argc-optind>1){
1067: int i;
1068: pout("ERROR: smartctl takes ONE device name as the final command-line argument.\n");
1069: pout("You have provided %d device names:\n",argc-optind);
1070: for (i=0; i<argc-optind; i++)
1071: pout("%s\n",argv[optind+i]);
1072: UsageSummary();
1073: EXIT(FAILCMD);
1074: }
1075:
1076: // Read or init drive database
1077: if (!no_defaultdb && !read_default_drive_databases())
1078: EXIT(FAILCMD);
1079:
1080: return type;
1081: }
1082:
1083: // Printing function (controlled by global printing_is_off)
1084: // [From GLIBC Manual: Since the prototype doesn't specify types for
1085: // optional arguments, in a call to a variadic function the default
1086: // argument promotions are performed on the optional argument
1087: // values. This means the objects of type char or short int (whether
1088: // signed or not) are promoted to either int or unsigned int, as
1089: // appropriate.]
1090: void pout(const char *fmt, ...){
1091: va_list ap;
1092:
1093: // initialize variable argument list
1094: va_start(ap,fmt);
1095: if (printing_is_off) {
1096: va_end(ap);
1097: return;
1098: }
1099:
1100: // print out
1101: vprintf(fmt,ap);
1102: va_end(ap);
1103: fflush(stdout);
1104: return;
1105: }
1106:
1107: // Globals to set failuretest() policy
1108: bool failuretest_conservative = false;
1109: unsigned char failuretest_permissive = 0;
1110:
1111: // Compares failure type to policy in effect, and either exits or
1112: // simply returns to the calling routine.
1113: // Used in ataprint.cpp and scsiprint.cpp.
1114: void failuretest(failure_type type, int returnvalue)
1115: {
1116: // If this is an error in an "optional" SMART command
1117: if (type == OPTIONAL_CMD) {
1118: if (!failuretest_conservative)
1119: return;
1120: pout("An optional SMART command failed: exiting. Remove '-T conservative' option to continue.\n");
1121: EXIT(returnvalue);
1122: }
1123:
1124: // If this is an error in a "mandatory" SMART command
1125: if (type == MANDATORY_CMD) {
1126: if (failuretest_permissive--)
1127: return;
1128: pout("A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n");
1129: EXIT(returnvalue);
1130: }
1131:
1132: throw std::logic_error("failuretest: Unknown type");
1133: }
1134:
1135: // Used to warn users about invalid checksums. Called from atacmds.cpp.
1136: // Action to be taken may be altered by the user.
1137: void checksumwarning(const char * string)
1138: {
1139: // user has asked us to ignore checksum errors
1140: if (checksum_err_mode == CHECKSUM_ERR_IGNORE)
1141: return;
1142:
1143: pout("Warning! %s error: invalid SMART checksum.\n", string);
1144:
1145: // user has asked us to fail on checksum errors
1146: if (checksum_err_mode == CHECKSUM_ERR_EXIT)
1147: EXIT(FAILSMART);
1148: }
1149:
1150: // Return info string about device protocol
1151: static const char * get_protocol_info(const smart_device * dev)
1152: {
1153: switch ((int)dev->is_ata() | ((int)dev->is_scsi() << 1)) {
1154: case 0x1: return "ATA";
1155: case 0x2: return "SCSI";
1156: case 0x3: return "ATA+SCSI";
1157: default: return "Unknown";
1158: }
1159: }
1160:
1161: // Device scan
1162: // smartctl [-d type] --scan[-open] -- [PATTERN] [smartd directive ...]
1163: void scan_devices(const char * type, bool with_open, char ** argv)
1164: {
1165: bool dont_print = !(ata_debugmode || scsi_debugmode);
1166:
1167: const char * pattern = 0;
1168: int ai = 0;
1169: if (argv[ai] && argv[ai][0] != '-')
1170: pattern = argv[ai++];
1171:
1172: smart_device_list devlist;
1173: printing_is_off = dont_print;
1174: bool ok = smi()->scan_smart_devices(devlist, type , pattern);
1175: printing_is_off = false;
1176:
1177: if (!ok) {
1178: pout("# scan_smart_devices: %s\n", smi()->get_errmsg());
1179: return;
1180: }
1181:
1182: for (unsigned i = 0; i < devlist.size(); i++) {
1183: smart_device_auto_ptr dev( devlist.release(i) );
1184:
1185: if (with_open) {
1186: printing_is_off = dont_print;
1187: dev.replace ( dev->autodetect_open() );
1188: printing_is_off = false;
1189:
1190: if (!dev->is_open()) {
1191: pout("# %s -d %s # %s, %s device open failed: %s\n", dev->get_dev_name(),
1192: dev->get_dev_type(), dev->get_info_name(),
1193: get_protocol_info(dev.get()), dev->get_errmsg());
1194: continue;
1195: }
1196: }
1197:
1198: pout("%s -d %s", dev->get_dev_name(), dev->get_dev_type());
1199: if (!argv[ai])
1200: pout(" # %s, %s device\n", dev->get_info_name(), get_protocol_info(dev.get()));
1201: else {
1202: for (int j = ai; argv[j]; j++)
1203: pout(" %s", argv[j]);
1204: pout("\n");
1205: }
1206:
1207: if (dev->is_open())
1208: dev->close();
1209: }
1210: }
1211:
1212: // Main program without exception handling
1213: static int main_worker(int argc, char **argv)
1214: {
1215: // Throw if CPU endianess does not match compile time test.
1216: check_endianness();
1217:
1218: // Initialize interface
1219: smart_interface::init();
1220: if (!smi())
1221: return 1;
1222:
1223: // Parse input arguments
1224: ata_print_options ataopts;
1225: scsi_print_options scsiopts;
1.1.1.2 misho 1226: bool print_type_only = false;
1227: const char * type = parse_options(argc, argv, ataopts, scsiopts, print_type_only);
1.1 misho 1228:
1229: const char * name = argv[argc-1];
1230:
1231: smart_device_auto_ptr dev;
1232: if (!strcmp(name,"-")) {
1233: // Parse "smartctl -r ataioctl,2 ..." output from stdin
1234: if (type || print_type_only) {
1.1.1.3 misho 1235: pout("-d option is not allowed in conjunction with device name \"-\".\n");
1.1 misho 1236: UsageSummary();
1237: return FAILCMD;
1238: }
1239: dev = get_parsed_ata_device(smi(), name);
1240: }
1241: else
1242: // get device of appropriate type
1243: dev = smi()->get_smart_device(name, type);
1244:
1245: if (!dev) {
1246: pout("%s: %s\n", name, smi()->get_errmsg());
1247: if (type)
1248: printvalidarglistmessage('d');
1249: else
1.1.1.3 misho 1250: pout("Please specify device type with the -d option.\n");
1.1 misho 1251: UsageSummary();
1252: return FAILCMD;
1253: }
1254:
1255: if (print_type_only)
1256: // Report result of first autodetection
1257: pout("%s: Device of type '%s' [%s] detected\n",
1258: dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
1259:
1260: // Open device
1261: {
1262: // Save old info
1263: smart_device::device_info oldinfo = dev->get_info();
1264:
1265: // Open with autodetect support, may return 'better' device
1266: dev.replace( dev->autodetect_open() );
1267:
1268: // Report if type has changed
1269: if ((type || print_type_only) && oldinfo.dev_type != dev->get_dev_type())
1270: pout("%s: Device open changed type from '%s' to '%s'\n",
1271: dev->get_info_name(), oldinfo.dev_type.c_str(), dev->get_dev_type());
1272: }
1273: if (!dev->is_open()) {
1274: pout("Smartctl open device: %s failed: %s\n", dev->get_info_name(), dev->get_errmsg());
1275: return FAILDEV;
1276: }
1277:
1278: // now call appropriate ATA or SCSI routine
1279: int retval = 0;
1280: if (print_type_only)
1281: pout("%s: Device of type '%s' [%s] opened\n",
1282: dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
1283: else if (dev->is_ata())
1284: retval = ataPrintMain(dev->to_ata(), ataopts);
1285: else if (dev->is_scsi())
1286: retval = scsiPrintMain(dev->to_scsi(), scsiopts);
1287: else
1288: // we should never fall into this branch!
1289: pout("%s: Neither ATA nor SCSI device\n", dev->get_info_name());
1290:
1291: dev->close();
1292: return retval;
1293: }
1294:
1295:
1296: // Main program
1297: int main(int argc, char **argv)
1298: {
1299: int status;
1300: try {
1301: // Do the real work ...
1302: status = main_worker(argc, argv);
1303: }
1304: catch (int ex) {
1305: // EXIT(status) arrives here
1306: status = ex;
1307: }
1308: catch (const std::bad_alloc & /*ex*/) {
1309: // Memory allocation failed (also thrown by std::operator new)
1310: printf("Smartctl: Out of memory\n");
1311: status = FAILCMD;
1312: }
1313: catch (const std::exception & ex) {
1314: // Other fatal errors
1315: printf("Smartctl: Exception: %s\n", ex.what());
1316: status = FAILCMD;
1317: }
1318: return status;
1319: }
1320:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>