smartmontools SVN Rev 5470
Utility to control and monitor storage systems with "S.M.A.R.T."
smartctl.cpp
Go to the documentation of this file.
1/*
2 * smartctl.cpp
3 *
4 * Home page of code is: https://www.smartmontools.org
5 *
6 * Copyright (C) 2002-11 Bruce Allen
7 * Copyright (C) 2008-22 Christian Franke
8 * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
13#include "config.h"
14#define __STDC_FORMAT_MACROS 1 // enable PRI* for C++
15
16#include <errno.h>
17#include <inttypes.h>
18#include <stdio.h>
19#include <sys/types.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdarg.h>
23#include <stdexcept>
24#include <getopt.h>
25
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29
30#if defined(__FreeBSD__)
31#include <sys/param.h>
32#endif
33
34#include "atacmds.h"
35#include "dev_interface.h"
36#include "ataprint.h"
37#include "knowndrives.h"
38#include "scsicmds.h"
39#include "scsiprint.h"
40#include "nvmeprint.h"
41#include "smartctl.h"
42#include "utility.h"
43#include "svnversion.h"
44
45const char * smartctl_cpp_cvsid = "$Id: smartctl.cpp 5462 2023-03-13 10:45:06Z chrfranke $"
46 CONFIG_H_CVSID SMARTCTL_H_CVSID;
47
48// Globals to control printing
50bool printing_is_off = false;
51
52// Control JSON output
54static bool print_as_json = false;
56static bool print_as_json_output = false;
57static bool print_as_json_impl = false;
58static bool print_as_json_unimpl = false;
59
60static void printslogan()
61{
62 jout("%s\n", format_version_info("smartctl").c_str());
63}
64
65static void UsageSummary()
66{
67 pout("\nUse smartctl -h to get a usage summary\n\n");
68 return;
69}
70
71static void js_initialize(int argc, char **argv, bool verbose)
72{
73 if (jglb.is_enabled())
74 return;
75 jglb.enable();
76 if (verbose)
78
79 // Major.minor version of JSON format
80 jglb["json_format_version"][0] = 1;
81 jglb["json_format_version"][1] = 0;
82
83 // Smartctl version info
84 json::ref jref = jglb["smartctl"];
85 int ver[3] = { 0, 0, 0 };
86 sscanf(PACKAGE_VERSION, "%d.%d.%d", ver, ver+1, ver+2);
87 jref["version"][0] = ver[0];
88 jref["version"][1] = ver[1];
89 if (ver[2] > 0)
90 jref["version"][2] = ver[2];
91
92#ifdef SMARTMONTOOLS_RELEASE_DATE
93 jref["pre_release"] = false;
94#else
95 jref["pre_release"] = true;
96#endif
97#ifdef SMARTMONTOOLS_SVN_REV
98 jref["svn_revision"] = SMARTMONTOOLS_SVN_REV;
99#endif
100 jref["platform_info"] = smi()->get_os_version_str();
101#ifdef BUILD_INFO
102 jref["build_info"] = BUILD_INFO;
103#endif
104
105 jref["argv"][0] = "smartctl";
106 for (int i = 1; i < argc; i++)
107 jref["argv"][i] = argv[i];
108}
109
110static std::string getvalidarglist(int opt);
111
112/* void prints help information for command syntax */
113static void Usage()
114{
115 pout("Usage: smartctl [options] device\n\n");
116 pout(
117"============================================ SHOW INFORMATION OPTIONS =====\n\n"
118" -h, --help, --usage\n"
119" Display this help and exit\n\n"
120" -V, --version, --copyright, --license\n"
121" Print license, copyright, and version information and exit\n\n"
122" -i, --info\n"
123" Show identity information for device\n\n"
124" --identify[=[w][nvb]]\n"
125" Show words and bits from IDENTIFY DEVICE data (ATA)\n\n"
126" -g NAME, --get=NAME\n"
127" Get device setting: all, aam, apm, dsn, lookahead, security,\n"
128" wcache, rcache, wcreorder, wcache-sct\n\n"
129" -a, --all\n"
130" Show all SMART information for device\n\n"
131" -x, --xall\n"
132" Show all information for device\n\n"
133" --scan\n"
134" Scan for devices\n\n"
135" --scan-open\n"
136" Scan for devices and try to open each device\n\n"
137 );
138 pout(
139"================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n"
140" -j, --json[=cgiosuvy]\n"
141" Print output in JSON or YAML format\n\n"
142" -q TYPE, --quietmode=TYPE (ATA)\n"
143" Set smartctl quiet mode to one of: errorsonly, silent, noserial\n\n"
144" -d TYPE, --device=TYPE\n"
145" Specify device type to one of:\n"
146" %s\n\n" // TODO: fold this string
147" -T TYPE, --tolerance=TYPE (ATA)\n"
148" Tolerance: normal, conservative, permissive, verypermissive\n\n"
149" -b TYPE, --badsum=TYPE (ATA)\n"
150" Set action on bad checksum to one of: warn, exit, ignore\n\n"
151" -r TYPE, --report=TYPE\n"
152" Report transactions (see man page)\n\n"
153" -n MODE[,STATUS[,STATUS2]], --nocheck=MODE[,STATUS[,STATUS2]] (ATA, SCSI)\n"
154" No check if: never, sleep, standby, idle (see man page)\n\n",
155 getvalidarglist('d').c_str()); // TODO: Use this function also for other options ?
156 pout(
157"============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n"
158" -s VALUE, --smart=VALUE\n"
159" Enable/disable SMART on device (on/off)\n\n"
160" -o VALUE, --offlineauto=VALUE (ATA)\n"
161" Enable/disable automatic offline testing on device (on/off)\n\n"
162" -S VALUE, --saveauto=VALUE (ATA)\n"
163" Enable/disable Attribute autosave on device (on/off)\n\n"
164" -s NAME[,VALUE], --set=NAME[,VALUE]\n"
165" Enable/disable/change device setting: aam,[N|off], apm,[N|off],\n"
166" dsn,[on|off], lookahead,[on|off], security-freeze,\n"
167" standby,[N|off|now], wcache,[on|off], rcache,[on|off],\n"
168" wcreorder,[on|off[,p]], wcache-sct,[ata|on|off[,p]]\n\n"
169 );
170 pout(
171"======================================= READ AND DISPLAY DATA OPTIONS =====\n\n"
172" -H, --health\n"
173" Show device SMART health status\n\n"
174" -c, --capabilities (ATA, NVMe)\n"
175" Show device SMART capabilities\n\n"
176" -A, --attributes\n"
177" Show device SMART vendor-specific Attributes and values\n\n"
178" -f FORMAT, --format=FORMAT (ATA)\n"
179" Set output format for attributes: old, brief, hex[,id|val]\n\n"
180" -l TYPE, --log=TYPE\n"
181" Show device log. TYPE: error, selftest, selective, directory[,g|s],\n"
182" xerror[,N][,error], xselftest[,N][,selftest], background,\n"
183" sasphy[,reset], sataphy[,reset], scttemp[sts,hist],\n"
184" scttempint,N[,p], scterc[,N,M][,p|reset], devstat[,N], defects[,N],\n"
185" ssd, gplog,N[,RANGE], smartlog,N[,RANGE], nvmelog,N,SIZE\n"
186" tapedevstat, zdevstat, envrep, farm\n\n"
187" -v N,OPTION , --vendorattribute=N,OPTION (ATA)\n"
188" Set display OPTION for vendor Attribute N (see man page)\n\n"
189" -F TYPE, --firmwarebug=TYPE (ATA)\n"
190" Use firmware bug workaround:\n"
191" %s, swapid\n\n"
192" -P TYPE, --presets=TYPE (ATA)\n"
193" Drive-specific presets: use, ignore, show, showall\n\n"
194" -B [+]FILE, --drivedb=[+]FILE (ATA)\n"
195" Read and replace [add] drive database from FILE\n"
196" [default is +%s",
199 );
200#ifdef SMARTMONTOOLS_DRIVEDBDIR
201 pout(
202 "\n"
203" and then %s",
204 get_drivedb_path_default()
205 );
206#endif
207 pout(
208 "]\n\n"
209"============================================ DEVICE SELF-TEST OPTIONS =====\n\n"
210" -t TEST, --test=TEST\n"
211" Run test. TEST: offline, short, long, conveyance, force, vendor,N,\n"
212" select,M-N, pending,N, afterselect,[on|off]\n\n"
213" -C, --captive\n"
214" Do test in captive mode (along with -t)\n\n"
215" -X, --abort\n"
216" Abort any non-captive test on device\n\n"
217);
218 std::string examples = smi()->get_app_examples("smartctl");
219 if (!examples.empty())
220 pout("%s\n", examples.c_str());
221}
222
223// Values for --long only options, see parse_options()
225
226/* Returns a string containing a formatted list of the valid arguments
227 to the option opt or empty on failure. Note 'v' case different */
228static std::string getvalidarglist(int opt)
229{
230 switch (opt) {
231 case 'q':
232 return "errorsonly, silent, noserial";
233 case 'd':
234 return smi()->get_valid_dev_types_str() + ", auto, test";
235 case 'T':
236 return "normal, conservative, permissive, verypermissive";
237 case 'b':
238 return "warn, exit, ignore";
239 case 'B':
240 return "[+]<FILE_NAME>";
241 case 'r':
242 return "ioctl[,N], ataioctl[,N], scsiioctl[,N], nvmeioctl[,N]";
243 case opt_smart:
244 case 'o':
245 case 'S':
246 return "on, off";
247 case 'l':
248 return "error, selftest, selective, directory[,g|s], "
249 "xerror[,N][,error], xselftest[,N][,selftest], "
250 "background, sasphy[,reset], sataphy[,reset], "
251 "scttemp[sts,hist], scttempint,N[,p], "
252 "scterc[,N,M][,p|reset], devstat[,N], defects[,N], "
253 "ssd, gplog,N[,RANGE], smartlog,N[,RANGE], "
254 "nvmelog,N,SIZE, tapedevstat, zdevstat, envrep, farm";
255 case 'P':
256 return "use, ignore, show, showall";
257 case 't':
258 return "offline, short, long, conveyance, force, vendor,N, select,M-N, "
259 "pending,N, afterselect,[on|off]";
260 case 'F':
261 return std::string(get_valid_firmwarebug_args()) + ", swapid";
262 case 'n':
263 return "never, sleep[,STATUS[,STATUS2]], standby[,STATUS[,STATUS2]], "
264 "idle[,STATUS[,STATUS2]]";
265 case 'f':
266 return "old, brief, hex[,id|val]";
267 case 'g':
268 return "aam, apm, dsn, lookahead, security, wcache, rcache, wcreorder, wcache-sct";
269 case opt_set:
270 return "aam,[N|off], apm,[N|off], dsn,[on|off], lookahead,[on|off], security-freeze, "
271 "standby,[N|off|now], wcache,[on|off], rcache,[on|off], wcreorder,[on|off[,p]], "
272 "wcache-sct,[ata|on|off[,p]]";
273 case 's':
275 case 'j':
276 return "c, g, i, o, s, u, v, y";
277 case opt_identify:
278 return "n, wn, w, v, wv, wb";
279 case 'v':
280 default:
281 return "";
282 }
283}
284
285/* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where
286 <LIST> is the list of valid arguments for option opt. */
287static void printvalidarglistmessage(int opt)
288{
289 if (opt=='v'){
290 jerr("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n",
292 }
293 else {
294 // getvalidarglist() might produce a multiline or single line string. We
295 // need to figure out which to get the formatting right.
296 std::string s = getvalidarglist(opt);
297 char separator = strchr(s.c_str(), '\n') ? '\n' : ' ';
298 jerr("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, s.c_str(), separator);
299 }
300
301 return;
302}
303
304// Checksum error mode
308
310
311static void scan_devices(const smart_devtype_list & types, bool with_open, char ** argv);
312
313
314/* Takes command options and sets features to be run */
315static int parse_options(int argc, char** argv, const char * & type,
316 ata_print_options & ataopts, scsi_print_options & scsiopts,
317 nvme_print_options & nvmeopts, bool & print_type_only)
318{
319 // Please update getvalidarglist() if you edit shortopts
320 const char *shortopts = "h?Vq:d:T:b:r:s:o:S:HcAl:iaxv:P:t:CXF:n:B:f:g:j";
321 // Please update getvalidarglist() if you edit longopts
322 struct option longopts[] = {
323 { "help", no_argument, 0, 'h' },
324 { "usage", no_argument, 0, 'h' },
325 { "version", no_argument, 0, 'V' },
326 { "copyright", no_argument, 0, 'V' },
327 { "license", no_argument, 0, 'V' },
328 { "quietmode", required_argument, 0, 'q' },
329 { "device", required_argument, 0, 'd' },
330 { "tolerance", required_argument, 0, 'T' },
331 { "badsum", required_argument, 0, 'b' },
332 { "report", required_argument, 0, 'r' },
333 { "smart", required_argument, 0, opt_smart },
334 { "offlineauto", required_argument, 0, 'o' },
335 { "saveauto", required_argument, 0, 'S' },
336 { "health", no_argument, 0, 'H' },
337 { "capabilities", no_argument, 0, 'c' },
338 { "attributes", no_argument, 0, 'A' },
339 { "log", required_argument, 0, 'l' },
340 { "info", no_argument, 0, 'i' },
341 { "all", no_argument, 0, 'a' },
342 { "xall", no_argument, 0, 'x' },
343 { "vendorattribute", required_argument, 0, 'v' },
344 { "presets", required_argument, 0, 'P' },
345 { "test", required_argument, 0, 't' },
346 { "captive", no_argument, 0, 'C' },
347 { "abort", no_argument, 0, 'X' },
348 { "firmwarebug", required_argument, 0, 'F' },
349 { "nocheck", required_argument, 0, 'n' },
350 { "drivedb", required_argument, 0, 'B' },
351 { "format", required_argument, 0, 'f' },
352 { "get", required_argument, 0, 'g' },
353 { "json", optional_argument, 0, 'j' },
354 { "identify", optional_argument, 0, opt_identify },
355 { "set", required_argument, 0, opt_set },
356 { "scan", no_argument, 0, opt_scan },
357 { "scan-open", no_argument, 0, opt_scan_open },
358 { 0, 0, 0, 0 }
359 };
360
361 char extraerror[256];
362 memset(extraerror, 0, sizeof(extraerror));
363 opterr=optopt=0;
364
365 smart_devtype_list scan_types; // multiple -d TYPE options for --scan
366 bool use_default_db = true; // set false on '-B FILE'
367 bool output_format_set = false; // set true on '-f FORMAT'
368 int scan = 0; // set by --scan, --scan-open
369 bool badarg = false, captive = false;
370 int testcnt = 0; // number of self-tests requested
371
372 int optchar;
373 char *arg;
374
375 while ((optchar = getopt_long(argc, argv, shortopts, longopts, 0)) != -1) {
376
377 // Clang analyzer: Workaround for false positive messages
378 // 'Dereference of null pointer' and 'Null pointer argument'
379 bool optarg_is_set = !!optarg;
380 #ifdef __clang_analyzer__
381 if (!optarg_is_set) optarg = (char *)"";
382 #endif
383
384 switch (optchar){
385 case 'V':
386 printing_is_off = false;
387 pout("%s", format_version_info("smartctl", true /*full*/).c_str());
388 return 0;
389 case 'q':
390 if (!strcmp(optarg,"errorsonly")) {
392 printing_is_off = false;
393 } else if (!strcmp(optarg,"silent")) {
395 printing_is_off = true;
396 } else if (!strcmp(optarg,"noserial")) {
398 } else {
399 badarg = true;
400 }
401 break;
402 case 'd':
403 if (!strcmp(optarg, "test"))
404 print_type_only = true;
405 else if (!strcmp(optarg, "auto")) {
406 type = 0;
407 scan_types.clear();
408 }
409 else {
410 type = optarg;
411 scan_types.push_back(optarg);
412 }
413 break;
414 case 'T':
415 if (!strcmp(optarg,"normal")) {
418 } else if (!strcmp(optarg,"conservative")) {
420 } else if (!strcmp(optarg,"permissive")) {
421 if (failuretest_permissive < 0xff)
423 } else if (!strcmp(optarg,"verypermissive")) {
425 } else {
426 badarg = true;
427 }
428 break;
429 case 'b':
430 if (!strcmp(optarg,"warn")) {
432 } else if (!strcmp(optarg,"exit")) {
434 } else if (!strcmp(optarg,"ignore")) {
436 } else {
437 badarg = true;
438 }
439 break;
440 case 'r':
441 {
442 int n1 = -1, n2 = -1, len = strlen(optarg);
443 char s[9+1]; unsigned i = 1;
444 sscanf(optarg, "%9[a-z]%n,%u%n", s, &n1, &i, &n2);
445 if (!((n1 == len || n2 == len) && 1 <= i && i <= 4)) {
446 badarg = true;
447 } else if (!strcmp(s,"ioctl")) {
449 } else if (!strcmp(s,"ataioctl")) {
450 ata_debugmode = i;
451 } else if (!strcmp(s,"scsiioctl")) {
452 scsi_debugmode = i;
453 } else if (!strcmp(s,"nvmeioctl")) {
454 nvme_debugmode = i;
455 } else {
456 badarg = true;
457 }
458 }
459 break;
460
461 case 's':
462 case opt_smart: // --smart
463 if (!strcmp(optarg,"on")) {
464 ataopts.smart_enable = scsiopts.smart_enable = true;
465 ataopts.smart_disable = scsiopts.smart_disable = false;
466 } else if (!strcmp(optarg,"off")) {
467 ataopts.smart_disable = scsiopts.smart_disable = true;
468 ataopts.smart_enable = scsiopts.smart_enable = false;
469 } else if (optchar == 's') {
470 goto case_s_continued; // --set, see below
471 } else {
472 badarg = true;
473 }
474 break;
475
476 case 'o':
477 if (!strcmp(optarg,"on")) {
478 ataopts.smart_auto_offl_enable = true;
479 ataopts.smart_auto_offl_disable = false;
480 } else if (!strcmp(optarg,"off")) {
481 ataopts.smart_auto_offl_disable = true;
482 ataopts.smart_auto_offl_enable = false;
483 } else {
484 badarg = true;
485 }
486 break;
487 case 'S':
488 if (!strcmp(optarg,"on")) {
489 ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = true;
490 ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = false;
491 } else if (!strcmp(optarg,"off")) {
492 ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = true;
493 ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = false;
494 } else {
495 badarg = true;
496 }
497 break;
498 case 'H':
499 ataopts.smart_check_status = scsiopts.smart_check_status = nvmeopts.smart_check_status = true;
500 scsiopts.smart_ss_media_log = true;
501 ++scsiopts.health_opt_count;
502 break;
503 case 'F':
504 if (!strcmp(optarg, "swapid"))
505 ataopts.fix_swapped_id = true;
506 else if (!parse_firmwarebug_def(optarg, ataopts.firmwarebugs))
507 badarg = true;
508 break;
509 case 'c':
510 ataopts.smart_general_values = nvmeopts.drive_capabilities = true;
511 break;
512 case 'A':
513 ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = nvmeopts.smart_vendor_attrib = true;
514 break;
515 case 'l':
516 if (str_starts_with(optarg, "error")) {
517 int n1 = -1, n2 = -1, len = strlen(optarg);
518 unsigned val = ~0;
519 sscanf(optarg, "error%n,%u%n", &n1, &val, &n2);
520 ataopts.smart_error_log = scsiopts.smart_error_log = true;
521 if (n1 == len)
522 nvmeopts.error_log_entries = 16;
523 else if (n2 == len && val > 0)
524 nvmeopts.error_log_entries = val;
525 else
526 badarg = true;
527 } else if (!strcmp(optarg,"selftest")) {
528 ataopts.smart_selftest_log = scsiopts.smart_selftest_log = nvmeopts.smart_selftest_log = true;
529 } else if (!strcmp(optarg, "selective")) {
530 ataopts.smart_selective_selftest_log = true;
531 } else if (!strcmp(optarg,"directory")) {
532 ataopts.smart_logdir = ataopts.gp_logdir = true; // SMART+GPL
533 } else if (!strcmp(optarg,"directory,s")) {
534 ataopts.smart_logdir = true; // SMART
535 } else if (!strcmp(optarg,"directory,g")) {
536 ataopts.gp_logdir = true; // GPL
537 } else if (!strcmp(optarg, "genstats")) {
538 scsiopts.general_stats_and_perf = true;
539 } else if (!strcmp(optarg,"sasphy")) {
540 scsiopts.sasphy = true;
541 } else if (!strcmp(optarg,"sasphy,reset")) {
542 scsiopts.sasphy = scsiopts.sasphy_reset = true;
543 } else if (!strcmp(optarg,"sataphy")) {
544 ataopts.sataphy = true;
545 } else if (!strcmp(optarg,"sataphy,reset")) {
546 ataopts.sataphy = ataopts.sataphy_reset = true;
547 } else if (!strcmp(optarg,"background")) {
548 scsiopts.smart_background_log = true;
549 } else if (!strcmp(optarg,"ssd")) {
550 ataopts.devstat_ssd_page = true;
551 scsiopts.smart_ss_media_log = true;
552 } else if (!strcmp(optarg,"scterc")) {
553 ataopts.sct_erc_get = 1;
554 } else if (!strcmp(optarg,"scttemp")) {
555 ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
556 } else if (!strcmp(optarg,"envrep")) {
557 scsiopts.smart_env_rep = true;
558 } else if (!strcmp(optarg,"scttempsts")) {
559 ataopts.sct_temp_sts = true;
560 } else if (!strcmp(optarg,"scttemphist")) {
561 ataopts.sct_temp_hist = true;
562 } else if (!strcmp(optarg,"farm")) {
563 ataopts.farm_log = scsiopts.farm_log = true; // Seagate Field Access Reliability Metrics (FARM) log
564 } else if (!strcmp(optarg,"tapealert")) {
565 scsiopts.tape_alert = true;
566 } else if (!strcmp(optarg,"tapedevstat")) {
567 scsiopts.tape_device_stats = true;
568 } else if (!strcmp(optarg,"zdevstat")) {
569 scsiopts.zoned_device_stats = true;
570 } else if (!strncmp(optarg, "scttempint,", sizeof("scstempint,")-1)) {
571 unsigned interval = 0; int n1 = -1, n2 = -1, len = strlen(optarg);
572 if (!( sscanf(optarg,"scttempint,%u%n,p%n", &interval, &n1, &n2) == 1
573 && 0 < interval && interval <= 0xffff && (n1 == len || n2 == len))) {
574 snprintf(extraerror, sizeof(extraerror), "Option -l scttempint,N[,p] must have positive integer N\n");
575 badarg = true;
576 }
577 ataopts.sct_temp_int = interval;
578 ataopts.sct_temp_int_pers = (n2 == len);
579
580 } else if (!strncmp(optarg, "devstat", sizeof("devstat")-1)) {
581 int n1 = -1, n2 = -1, len = strlen(optarg);
582 unsigned val = ~0;
583 sscanf(optarg, "devstat%n,%u%n", &n1, &val, &n2);
584 if (n1 == len)
585 ataopts.devstat_all_pages = true;
586 else {
587 if (n2 != len) // retry with hex
588 sscanf(optarg, "devstat,0x%x%n", &val, &n2);
589 if (n2 == len && val <= 0xff)
590 ataopts.devstat_pages.push_back(val);
591 else
592 badarg = true;
593 }
594
595 } else if (str_starts_with(optarg, "defects")) {
596 int n1 = -1, n2 = -1, len = strlen(optarg);
597 unsigned val = ~0;
598 sscanf(optarg, "defects%n,%u%n", &n1, &val, &n2);
599 scsiopts.scsi_pending_defects = true;
600 if (n1 == len)
601 ataopts.pending_defects_log = 31; // Entries of first page
602 else if (n2 == len && val <= 0xffff * 32 - 1)
603 ataopts.pending_defects_log = val;
604 else
605 badarg = true;
606
607 } else if (!strncmp(optarg, "xerror", sizeof("xerror")-1)) {
608 int n1 = -1, n2 = -1, len = strlen(optarg);
609 unsigned val = 8;
610 sscanf(optarg, "xerror%n,error%n", &n1, &n2);
611 if (!(n1 == len || n2 == len)) {
612 n1 = n2 = -1;
613 sscanf(optarg, "xerror,%u%n,error%n", &val, &n1, &n2);
614 }
615 if ((n1 == len || n2 == len) && val > 0) {
616 ataopts.smart_ext_error_log = val;
617 ataopts.retry_error_log = (n2 == len);
618 }
619 else
620 badarg = true;
621
622 } else if (!strncmp(optarg, "xselftest", sizeof("xselftest")-1)) {
623 int n1 = -1, n2 = -1, len = strlen(optarg);
624 unsigned val = 25;
625 sscanf(optarg, "xselftest%n,selftest%n", &n1, &n2);
626 if (!(n1 == len || n2 == len)) {
627 n1 = n2 = -1;
628 sscanf(optarg, "xselftest,%u%n,selftest%n", &val, &n1, &n2);
629 }
630 if ((n1 == len || n2 == len) && val > 0) {
631 ataopts.smart_ext_selftest_log = val;
632 ataopts.retry_selftest_log = (n2 == len);
633 }
634 else
635 badarg = true;
636
637 } else if (!strncmp(optarg, "scterc,", sizeof("scterc,")-1)) {
638 int n1 = -1, n2 = -1, len = strlen(optarg);
639 unsigned rt = ~0, wt = ~0;
640 sscanf(optarg, "scterc,%u,%u%n,p%n", &rt, &wt, &n1, &n2);
641 if ((n1 == len || n2 == len) && rt <= 999 && wt <= 999) {
642 ataopts.sct_erc_set = (n2 == len ? 2 : 1);
643 ataopts.sct_erc_readtime = rt;
644 ataopts.sct_erc_writetime = wt;
645 } else if (!strcmp(optarg, "scterc,p")) {
646 ataopts.sct_erc_get = 2;
647 } else if (!strcmp(optarg, "scterc,reset")) {
648 ataopts.sct_erc_set = 3;
649 ataopts.sct_erc_readtime = ataopts.sct_erc_writetime = 0;
650 } else {
651 snprintf(extraerror, sizeof(extraerror), "Option -l scterc[,READTIME,WRITETIME][,p|reset] syntax error\n");
652 badarg = true;
653 }
654 } else if ( !strncmp(optarg, "gplog," , sizeof("gplog," )-1)
655 || !strncmp(optarg, "smartlog,", sizeof("smartlog,")-1)) {
656 unsigned logaddr = ~0U; unsigned page = 0, nsectors = 1; char sign = 0;
657 int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
658 sscanf(optarg, "%*[a-z],0x%x%n,%u%n%c%u%n",
659 &logaddr, &n1, &page, &n2, &sign, &nsectors, &n3);
660 if (len > n2 && n3 == -1 && !strcmp(optarg+n2, "-max")) {
661 nsectors = ~0U; sign = '+'; n3 = len;
662 }
663 bool gpl = (optarg[0] == 'g');
664 const char * erropt = (gpl ? "gplog" : "smartlog");
665 if (!( n1 == len || n2 == len
666 || (n3 == len && (sign == '+' || sign == '-')))) {
667 snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] syntax error\n", erropt);
668 badarg = true;
669 }
670 else if (!( logaddr <= 0xff && page <= (gpl ? 0xffffU : 0x00ffU)
671 && 0 < nsectors
672 && (nsectors <= (gpl ? 0xffffU : 0xffU) || nsectors == ~0U)
673 && (sign != '-' || page <= nsectors) )) {
674 snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] parameter out of range\n", erropt);
675 badarg = true;
676 }
677 else {
678 ata_log_request req;
679 req.gpl = gpl; req.logaddr = logaddr; req.page = page;
680 req.nsectors = (sign == '-' ? nsectors-page+1 : nsectors);
681 ataopts.log_requests.push_back(req);
682 }
683 }
684
685 else if (str_starts_with(optarg, "nvmelog,")) {
686 int n = -1, len = strlen(optarg);
687 unsigned page = 0, size = 0;
688 sscanf(optarg, "nvmelog,0x%x,0x%x%n", &page, &size, &n);
689 if (n == len && page <= 0xff && 0 < size && size <= 0x4000) {
690 nvmeopts.log_page = page; nvmeopts.log_page_size = size;
691 }
692 else
693 badarg = true;
694 }
695
696 else {
697 badarg = true;
698 }
699 break;
700 case 'i':
701 ataopts.drive_info = scsiopts.drive_info = nvmeopts.drive_info = true;
702 break;
703
704 case opt_identify:
705 ataopts.identify_word_level = ataopts.identify_bit_level = 0;
706 if (optarg_is_set) {
707 for (int i = 0; optarg[i]; i++) {
708 switch (optarg[i]) {
709 case 'w': ataopts.identify_word_level = 1; break;
710 case 'n': ataopts.identify_bit_level = -1; break;
711 case 'v': ataopts.identify_bit_level = 1; break;
712 case 'b': ataopts.identify_bit_level = 2; break;
713 default: badarg = true;
714 }
715 }
716 }
717 break;
718
719 case 'a':
720 ataopts.drive_info = scsiopts.drive_info = nvmeopts.drive_info = true;
721 ataopts.smart_check_status = scsiopts.smart_check_status = nvmeopts.smart_check_status = true;
722 ataopts.smart_general_values = nvmeopts.drive_capabilities = true;
723 ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = nvmeopts.smart_vendor_attrib = true;
724 ataopts.smart_error_log = scsiopts.smart_error_log = true;
725 nvmeopts.error_log_entries = 16;
726 ataopts.smart_selftest_log = scsiopts.smart_selftest_log = nvmeopts.smart_selftest_log = true;
727 ataopts.smart_selective_selftest_log = true;
728 /* scsiopts.smart_background_log = true; */
729 scsiopts.smart_ss_media_log = true;
730 ataopts.farm_log_suggest = scsiopts.farm_log_suggest = true; // Suggests FARM log, if supported (does not pull log!)
731 break;
732 case 'x':
733 ataopts.drive_info = scsiopts.drive_info = nvmeopts.drive_info = true;
734 ataopts.smart_check_status = scsiopts.smart_check_status = nvmeopts.smart_check_status = true;
735 ataopts.smart_general_values = nvmeopts.drive_capabilities = true;
736 ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = nvmeopts.smart_vendor_attrib = true;
737 ataopts.smart_ext_error_log = 8;
738 ataopts.retry_error_log = true;
739 nvmeopts.error_log_entries = 16;
740 ataopts.smart_ext_selftest_log = 25;
741 ataopts.retry_selftest_log = true;
742 scsiopts.smart_error_log = scsiopts.smart_selftest_log = nvmeopts.smart_selftest_log = true;
743 ataopts.smart_selective_selftest_log = true;
744 ataopts.smart_logdir = ataopts.gp_logdir = true;
745 ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
746 ataopts.sct_erc_get = 1;
747 ataopts.sct_wcache_reorder_get = true;
748 ataopts.devstat_all_pages = true;
749 ataopts.pending_defects_log = 31;
750 ataopts.sataphy = true;
751 ataopts.get_set_used = true;
752 ataopts.get_aam = ataopts.get_apm = true;
753 ataopts.get_security = true;
754 ataopts.get_lookahead = ataopts.get_wcache = true;
755 ataopts.get_dsn = true;
756 scsiopts.get_rcd = scsiopts.get_wce = true;
757 scsiopts.smart_background_log = true;
758 scsiopts.smart_ss_media_log = true;
759 scsiopts.sasphy = true;
760 ataopts.farm_log = scsiopts.farm_log = true; // Helper for FARM debug messages
761 scsiopts.smart_env_rep = true;
762 scsiopts.scsi_pending_defects = true;
763 scsiopts.tape_device_stats = true;
764 scsiopts.zoned_device_stats = true;
765 scsiopts.general_stats_and_perf = true;
766 if (!output_format_set)
768 break;
769 case 'v':
770 // parse vendor-specific definitions of attributes
771 if (!strcmp(optarg,"help")) {
772 printing_is_off = false;
773 printslogan();
774 pout("The valid arguments to -v are:\n\thelp\n%s\n",
776 return 0;
777 }
778 if (!parse_attribute_def(optarg, ataopts.attribute_defs, PRIOR_USER))
779 badarg = true;
780 break;
781 case 'P':
782 if (!strcmp(optarg, "use")) {
783 ataopts.ignore_presets = false;
784 } else if (!strcmp(optarg, "ignore")) {
785 ataopts.ignore_presets = true;
786 } else if (!strcmp(optarg, "show")) {
787 ataopts.show_presets = true;
788 } else if (!strcmp(optarg, "showall")) {
789 if (!init_drive_database(use_default_db))
790 return FAILCMD;
791 if (optind < argc) { // -P showall MODEL [FIRMWARE]
792 int cnt = showmatchingpresets(argv[optind], (optind+1<argc ? argv[optind+1] : NULL));
793 return (cnt >= 0 ? cnt : 0);
794 }
795 if (showallpresets())
796 return FAILCMD; // report regexp syntax error
797 return 0;
798 } else {
799 badarg = true;
800 }
801 break;
802 case 't':
803 if (!strcmp(optarg,"offline")) {
804 testcnt++;
806 scsiopts.smart_default_selftest = true;
807 } else if (!strcmp(optarg,"short")) {
808 testcnt++;
810 scsiopts.smart_short_selftest = true;
811 nvmeopts.smart_selftest_type = 0x1; // TODO: enum
812 } else if (!strcmp(optarg,"long")) {
813 testcnt++;
815 scsiopts.smart_extend_selftest = true;
816 nvmeopts.smart_selftest_type = 0x2;
817 } else if (!strcmp(optarg,"conveyance")) {
818 testcnt++;
820 } else if (!strcmp(optarg,"force")) {
821 ataopts.smart_selftest_force = true;
822 scsiopts.smart_selftest_force = true;
823 } else if (!strcmp(optarg,"afterselect,on")) {
824 // scan remainder of disk after doing selected segment
826 } else if (!strcmp(optarg,"afterselect,off")) {
827 // don't scan remainder of disk after doing selected segments
829 } else if (!strncmp(optarg,"pending,",strlen("pending,"))) {
830 // parse number of minutes that test should be pending
831 int i;
832 char *tailptr=NULL;
833 errno=0;
834 i=(int)strtol(optarg+strlen("pending,"), &tailptr, 10);
835 if (errno || *tailptr != '\0') {
836 snprintf(extraerror, sizeof(extraerror), "Option -t pending,N requires N to be a non-negative integer\n");
837 badarg = true;
838 } else if (i<0 || i>65535) {
839 snprintf(extraerror, sizeof(extraerror), "Option -t pending,N (N=%d) must have 0 <= N <= 65535\n", i);
840 badarg = true;
841 } else {
843 }
844 } else if (!strncmp(optarg,"select",strlen("select"))) {
845 if (ataopts.smart_selective_args.num_spans == 0)
846 testcnt++;
847 // parse range of LBAs to test
848 uint64_t start, stop; int mode;
849 if (split_selective_arg(optarg, &start, &stop, &mode)) {
850 snprintf(extraerror, sizeof(extraerror), "Option -t select,M-N must have non-negative integer M and N\n");
851 badarg = true;
852 } else {
853 if (ataopts.smart_selective_args.num_spans >= 5 || start > stop) {
854 if (start > stop) {
855 snprintf(extraerror, sizeof(extraerror), "ERROR: Start LBA (%" PRIu64 ") > ending LBA (%" PRId64 ") in argument \"%s\"\n",
856 start, stop, optarg);
857 } else {
858 snprintf(extraerror, sizeof(extraerror),"ERROR: No more than five selective self-test spans may be"
859 " defined\n");
860 }
861 badarg = true;
862 }
868 }
869 } else if (!strncmp(optarg, "scttempint", sizeof("scstempint")-1)) {
870 snprintf(extraerror, sizeof(extraerror), "-t scttempint is no longer supported, use -l scttempint instead\n");
871 badarg = true;
872 } else if (!strncmp(optarg, "vendor,", sizeof("vendor,")-1)) {
873 unsigned subcmd = ~0U; int n = -1;
874 if (!( sscanf(optarg, "%*[a-z],0x%x%n", &subcmd, &n) == 1
875 && subcmd <= 0xff && n == (int)strlen(optarg))) {
876 snprintf(extraerror, sizeof(extraerror), "Option -t vendor,0xNN syntax error\n");
877 badarg = true;
878 }
879 else
880 ataopts.smart_selftest_type = subcmd;
881 } else {
882 badarg = true;
883 }
884 break;
885 case 'C':
886 captive = true;
887 break;
888 case 'X':
889 testcnt++;
891 scsiopts.smart_selftest_abort = true;
892 nvmeopts.smart_selftest_type = 0xf;
893 break;
894 case 'n':
895 // skip disk check if in low-power mode
896 if (!strcmp(optarg, "never")) {
897 ataopts.powermode = 1; // do not skip, but print mode
898 scsiopts.powermode = 1;
899 }
900 else {
901 int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
902 char s[7+1]; unsigned i = FAILPOWER, j = 0;
903 sscanf(optarg, "%7[a-z]%n,%u%n,%u%n", s, &n1, &i, &n2, &j, &n3);
904 if (!((n1 == len || n2 == len || n3 == len) && i <= 255 && j <= 255))
905 badarg = true;
906 else if (!strcmp(s, "sleep")) {
907 ataopts.powermode = 2;
908 scsiopts.powermode = 2;
909 } else if (!strcmp(s, "standby")) {
910 ataopts.powermode = 3;
911 scsiopts.powermode = 3;
912 } else if (!strcmp(s, "idle")) {
913 ataopts.powermode = 4;
914 scsiopts.powermode = 4;
915 } else
916 badarg = true;
917
918 ataopts.powerexit = i;
919 ataopts.powerexit_unsup = (n3 == len ? j : -1);
920 scsiopts.powerexit = i;
921 }
922 break;
923 case 'f':
924 if (!strcmp(optarg, "old")) {
925 ataopts.output_format &= ~ata_print_options::FMT_BRIEF;
926 output_format_set = true;
927 }
928 else if (!strcmp(optarg, "brief")) {
930 output_format_set = true;
931 }
932 else if (!strcmp(optarg, "hex"))
935 else if (!strcmp(optarg, "hex,id"))
937 else if (!strcmp(optarg, "hex,val"))
939 else
940 badarg = true;
941 break;
942 case 'B':
943 {
944 const char * path = optarg;
945 if (*path == '+' && path[1])
946 path++;
947 else
948 use_default_db = false;
949 if (!read_drive_database(path))
950 return FAILCMD;
951 }
952 break;
953 case 'h':
954 printing_is_off = false;
955 printslogan();
956 Usage();
957 return 0;
958
959 case 'g':
960 case_s_continued: // -s, see above
961 case opt_set: // --set
962 {
963 ataopts.get_set_used = true;
964 bool get = (optchar == 'g');
965 char name[16+1]; unsigned val;
966 int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
967 if (sscanf(optarg, "%16[^,=]%n%*[,=]%n%u%n", name, &n1, &n2, &val, &n3) >= 1
968 && (n1 == len || (!get && n2 > 0))) {
969 bool on = false;
970 bool off = false;
971 bool ata = false;
972 bool persistent = false;
973
974 if (n2 > 0) {
975 int len2 = strlen(optarg + n2);
976 char * tmp = strstr(optarg+n2, ",p");
977 // handle ",p" in persistent options like: wcache-sct,[ata|on|off],p
978 if (tmp && (strlen(tmp) == 2)) {
979 persistent = true;
980 len2 = strlen(optarg+n2) - 2;
981
982 // the ,p option only works for set of SCT Feature Control command
983 if (strcmp(name, "wcache-sct") != 0 &&
984 strcmp(name, "wcreorder") != 0)
985 badarg = true;
986 }
987 on = !strncmp(optarg+n2, "on", len2);
988 off = !strncmp(optarg+n2, "off", len2);
989 ata = !strncmp(optarg+n2, "ata", len2);
990 }
991 if (n3 != len)
992 val = ~0U;
993
994 if (get && !strcmp(name, "all")) {
995 ataopts.get_aam = ataopts.get_apm = true;
996 ataopts.get_security = true;
997 ataopts.get_lookahead = ataopts.get_wcache = true;
998 ataopts.get_dsn = true;
999 scsiopts.get_rcd = scsiopts.get_wce = true;
1000 }
1001 else if (!strcmp(name, "aam")) {
1002 if (get)
1003 ataopts.get_aam = true;
1004 else if (off)
1005 ataopts.set_aam = -1;
1006 else if (val <= 254)
1007 ataopts.set_aam = val + 1;
1008 else {
1009 snprintf(extraerror, sizeof(extraerror), "Option -s aam,N must have 0 <= N <= 254\n");
1010 badarg = true;
1011 }
1012 }
1013 else if (!strcmp(name, "apm")) {
1014 if (get)
1015 ataopts.get_apm = true;
1016 else if (off)
1017 ataopts.set_apm = -1;
1018 else if (1 <= val && val <= 254)
1019 ataopts.set_apm = val + 1;
1020 else {
1021 snprintf(extraerror, sizeof(extraerror), "Option -s apm,N must have 1 <= N <= 254\n");
1022 badarg = true;
1023 }
1024 }
1025 else if (!strcmp(name, "lookahead")) {
1026 if (get) {
1027 ataopts.get_lookahead = true;
1028 }
1029 else if (off)
1030 ataopts.set_lookahead = -1;
1031 else if (on)
1032 ataopts.set_lookahead = 1;
1033 else
1034 badarg = true;
1035 }
1036 else if (!strcmp(name, "wcreorder")) {
1037 ataopts.sct_wcache_reorder_set_pers = persistent;
1038 if (get) {
1039 ataopts.sct_wcache_reorder_get = true;
1040 }
1041 else if (off)
1042 ataopts.sct_wcache_reorder_set = -1;
1043 else if (on)
1044 ataopts.sct_wcache_reorder_set = 1;
1045 else
1046 badarg = true;
1047 }
1048 else if (!strcmp(name, "wcache-sct")) {
1049 ataopts.sct_wcache_sct_set_pers = persistent;
1050 if (get) {
1051 ataopts.sct_wcache_sct_get = true;
1052 }
1053 else if (off)
1054 ataopts.sct_wcache_sct_set = 3;
1055 else if (on)
1056 ataopts.sct_wcache_sct_set = 2;
1057 else if (ata)
1058 ataopts.sct_wcache_sct_set = 1;
1059 else
1060 badarg = true;
1061 }
1062 else if (!strcmp(name, "rcache")) {
1063 if (get)
1064 scsiopts.get_rcd = true;
1065 else if (off)
1066 scsiopts.set_rcd = -1;
1067 else if (on)
1068 scsiopts.set_rcd = 1;
1069 else
1070 badarg = true;
1071 }
1072 else if (get && !strcmp(name, "security")) {
1073 ataopts.get_security = true;
1074 }
1075 else if (!get && !strcmp(optarg, "security-freeze")) {
1076 ataopts.set_security_freeze = true;
1077 }
1078 else if (!get && !strcmp(optarg, "standby,now")) {
1079 ataopts.set_standby_now = true;
1080 scsiopts.set_standby_now = true;
1081 }
1082 else if (!get && !strcmp(name, "standby")) {
1083 if (off) {
1084 ataopts.set_standby = 0 + 1;
1085 scsiopts.set_standby = 0 + 1;
1086 } else if (val <= 255) {
1087 ataopts.set_standby = val + 1;
1088 scsiopts.set_standby = val + 1;
1089 } else {
1090 snprintf(extraerror, sizeof(extraerror), "Option -s standby,N must have 0 <= N <= 255\n");
1091 badarg = true;
1092 }
1093 }
1094 else if (!strcmp(name, "wcache")) {
1095 if (get) {
1096 ataopts.get_wcache = true;
1097 scsiopts.get_wce = true;
1098 }
1099 else if (off) {
1100 ataopts.set_wcache = -1;
1101 scsiopts.set_wce = -1;
1102 }
1103 else if (on) {
1104 ataopts.set_wcache = 1;
1105 scsiopts.set_wce = 1;
1106 }
1107 else
1108 badarg = true;
1109 }
1110 else if (!strcmp(name, "dsn")) {
1111 if (get) {
1112 ataopts.get_dsn = true;
1113 }
1114 else if (off) {
1115 ataopts.set_dsn = -1;
1116 }
1117 else if (on) {
1118 ataopts.set_dsn = 1;
1119 }
1120 else
1121 badarg = true;
1122 }
1123 else
1124 badarg = true;
1125 }
1126 else
1127 badarg = true;
1128 }
1129 break;
1130
1131 case opt_scan:
1132 case opt_scan_open:
1133 scan = optchar;
1134 break;
1135
1136 case 'j':
1137 {
1138 print_as_json = true;
1142 print_as_json_output = false;
1144 bool json_verbose = false;
1145 if (optarg_is_set) {
1146 for (int i = 0; optarg[i]; i++) {
1147 switch (optarg[i]) {
1148 case 'c': print_as_json_options.pretty = false; break;
1149 case 'g': print_as_json_options.format = 'g'; break;
1150 case 'i': print_as_json_impl = true; break;
1151 case 'o': print_as_json_output = true; break;
1152 case 's': print_as_json_options.sorted = true; break;
1153 case 'u': print_as_json_unimpl = true; break;
1154 case 'v': json_verbose = true; break;
1155 case 'y': print_as_json_options.format = 'y'; break;
1156 default: badarg = true;
1157 }
1158 }
1159 }
1160 js_initialize(argc, argv, json_verbose);
1161 }
1162 break;
1163
1164 case '?':
1165 default:
1166 printing_is_off = false;
1167 printslogan();
1168 // Point arg to the argument in which this option was found.
1169 arg = argv[optind-1];
1170 // Check whether the option is a long option that doesn't map to -h.
1171 if (arg[1] == '-' && optchar != 'h') {
1172 // Iff optopt holds a valid option then argument must be missing.
1173 if (optopt && (optopt > '~' || strchr(shortopts, optopt))) {
1174 jerr("=======> ARGUMENT REQUIRED FOR OPTION: %s\n", arg+2);
1176 } else
1177 jerr("=======> UNRECOGNIZED OPTION: %s\n",arg+2);
1178 if (extraerror[0])
1179 pout("=======> %s", extraerror);
1180 UsageSummary();
1181 return FAILCMD;
1182 }
1183 if (0 < optopt && optopt < '~') {
1184 // Iff optopt holds a valid option then argument must be
1185 // missing. Note (BA) this logic seems to fail using Solaris
1186 // getopt!
1187 if (strchr(shortopts, optopt) != NULL) {
1188 jerr("=======> ARGUMENT REQUIRED FOR OPTION: %c\n", optopt);
1190 } else
1191 jerr("=======> UNRECOGNIZED OPTION: %c\n",optopt);
1192 if (extraerror[0])
1193 pout("=======> %s", extraerror);
1194 UsageSummary();
1195 return FAILCMD;
1196 }
1197 Usage();
1198 return 0;
1199 } // closes switch statement to process command-line options
1200
1201 // Check to see if option had an unrecognized or incorrect argument.
1202 if (badarg) {
1203 printslogan();
1204 // It would be nice to print the actual option name given by the user
1205 // here, but we just print the short form. Please fix this if you know
1206 // a clean way to do it.
1207 char optstr[] = { (char)optchar, 0 };
1208 jerr("=======> INVALID ARGUMENT TO -%s: %s\n",
1209 (optchar == opt_identify ? "-identify" :
1210 optchar == opt_set ? "-set" :
1211 optchar == opt_smart ? "-smart" :
1212 optchar == 'j' ? "-json" : optstr), optarg);
1213 printvalidarglistmessage(optchar);
1214 if (extraerror[0])
1215 pout("=======> %s", extraerror);
1216 UsageSummary();
1217 return FAILCMD;
1218 }
1219 }
1220
1221 // Special handling of --scan, --scanopen
1222 if (scan) {
1223 // Read or init drive database to allow USB ID check.
1224 if (!init_drive_database(use_default_db))
1225 return FAILCMD;
1226 scan_devices(scan_types, (scan == opt_scan_open), argv + optind);
1227 return 0;
1228 }
1229
1230 // At this point we have processed all command-line options. If the
1231 // print output is switchable, then start with the print output
1232 // turned off
1234 printing_is_off = true;
1235
1236 // Check for multiple -d TYPE options
1237 if (scan_types.size() > 1) {
1238 printing_is_off = false;
1239 printslogan();
1240 jerr("ERROR: multiple -d TYPE options are only allowed with --scan\n");
1241 UsageSummary();
1242 return FAILCMD;
1243 }
1244
1245 // error message if user has asked for more than one test
1246 if (testcnt > 1) {
1247 printing_is_off = false;
1248 printslogan();
1249 jerr("\nERROR: smartctl can only run a single test type (or abort) at a time.\n");
1250 UsageSummary();
1251 return FAILCMD;
1252 }
1253
1254 // error message if user has set selective self-test options without
1255 // asking for a selective self-test
1256 if ( (ataopts.smart_selective_args.pending_time || ataopts.smart_selective_args.scan_after_select)
1257 && !ataopts.smart_selective_args.num_spans) {
1258 printing_is_off = false;
1259 printslogan();
1260 if (ataopts.smart_selective_args.pending_time)
1261 jerr("\nERROR: smartctl -t pending,N must be used with -t select,N-M.\n");
1262 else
1263 jerr("\nERROR: smartctl -t afterselect,(on|off) must be used with -t select,N-M.\n");
1264 UsageSummary();
1265 return FAILCMD;
1266 }
1267
1268 // If captive option was used, change test type if appropriate.
1269 if (captive)
1270 switch (ataopts.smart_selftest_type) {
1271 case SHORT_SELF_TEST:
1272 ataopts.smart_selftest_type = SHORT_CAPTIVE_SELF_TEST;
1273 scsiopts.smart_short_selftest = false;
1274 scsiopts.smart_short_cap_selftest = true;
1275 break;
1276 case EXTEND_SELF_TEST:
1277 ataopts.smart_selftest_type = EXTEND_CAPTIVE_SELF_TEST;
1278 scsiopts.smart_extend_selftest = false;
1279 scsiopts.smart_extend_cap_selftest = true;
1280 break;
1282 ataopts.smart_selftest_type = CONVEYANCE_CAPTIVE_SELF_TEST;
1283 break;
1285 ataopts.smart_selftest_type = SELECTIVE_CAPTIVE_SELF_TEST;
1286 break;
1287 }
1288
1289 // From here on, normal operations...
1290 printslogan();
1291
1292 // Warn if the user has provided no device name
1293 if (argc-optind<1){
1294 jerr("ERROR: smartctl requires a device name as the final command-line argument.\n\n");
1295 UsageSummary();
1296 return FAILCMD;
1297 }
1298
1299 // Warn if the user has provided more than one device name
1300 if (argc-optind>1){
1301 int i;
1302 jerr("ERROR: smartctl takes ONE device name as the final command-line argument.\n");
1303 pout("You have provided %d device names:\n",argc-optind);
1304 for (i=0; i<argc-optind; i++)
1305 pout("%s\n",argv[optind+i]);
1306 UsageSummary();
1307 return FAILCMD;
1308 }
1309
1310 // Read or init drive database
1311 if (!init_drive_database(use_default_db))
1312 return FAILCMD;
1313
1314 // No error, continue in main_worker()
1315 return -1;
1316}
1317
1318// Printing functions
1319
1321static void vjpout(bool is_js_impl, const char * msg_severity,
1322 const char *fmt, va_list ap)
1323{
1324 if (!print_as_json) {
1325 // Print out directly
1326 vprintf(fmt, ap);
1327 fflush(stdout);
1328 }
1329 else {
1330 // Add lines to JSON output
1331 static char buf[1024];
1332 static char * bufnext = buf;
1333 vsnprintf(bufnext, sizeof(buf) - (bufnext - buf), fmt, ap);
1334 for (char * p = buf, *q; ; p = q) {
1335 if (!(q = strchr(p, '\n'))) {
1336 // Keep remaining line for next call
1337 for (bufnext = buf; *p; bufnext++, p++)
1338 *bufnext = *p;
1339 break;
1340 }
1341 *q++ = 0; // '\n' -> '\0'
1342
1343 static int lineno = 0;
1344 lineno++;
1346 // Collect full output in array
1347 static int outindex = 0;
1348 jglb["smartctl"]["output"][outindex++] = p;
1349 }
1350 if (!*p)
1351 continue; // Skip empty line
1352
1353 if (msg_severity) {
1354 // Collect non-empty messages in array
1355 static int errindex = 0;
1356 json::ref jref = jglb["smartctl"]["messages"][errindex++];
1357 jref["string"] = p;
1358 jref["severity"] = msg_severity;
1359 }
1360
1361 if ( ( is_js_impl && print_as_json_impl )
1362 || (!is_js_impl && print_as_json_unimpl)) {
1363 // Add (un)implemented non-empty lines to global object
1364 jglb[strprintf("smartctl_%04d_%c", lineno,
1365 (is_js_impl ? 'i' : 'u')).c_str()] = p;
1366 }
1367 }
1368 }
1369}
1370
1371// Default: print to stdout
1372// --json: ignore
1373// --json=o: append to "output" array
1374// --json=u: add "smartctl_NNNN_u" element(s)
1375void pout(const char *fmt, ...)
1376{
1377 if (printing_is_off)
1378 return;
1381 return;
1382
1383 va_list ap;
1384 va_start(ap, fmt);
1385 vjpout(false, 0, fmt, ap);
1386 va_end(ap);
1387}
1388
1389// Default: Print to stdout
1390// --json: ignore
1391// --json=o: append to "output" array
1392// --json=i: add "smartctl_NNNN_i" element(s)
1393void jout(const char *fmt, ...)
1394{
1395 if (printing_is_off)
1396 return;
1399 return;
1400
1401 va_list ap;
1402 va_start(ap, fmt);
1403 vjpout(true, 0, fmt, ap);
1404 va_end(ap);
1405}
1406
1407// Default: print to stdout
1408// --json: append to "messages"
1409// --json=o: append to "output" array
1410// --json=i: add "smartctl_NNNN_i" element(s)
1411void jinf(const char *fmt, ...)
1412{
1413 if (printing_is_off)
1414 return;
1415
1416 va_list ap;
1417 va_start(ap, fmt);
1418 vjpout(true, "information", fmt, ap);
1419 va_end(ap);
1420}
1421
1422void jwrn(const char *fmt, ...)
1423{
1424 if (printing_is_off)
1425 return;
1426
1427 va_list ap;
1428 va_start(ap, fmt);
1429 vjpout(true, "warning", fmt, ap);
1430 va_end(ap);
1431}
1432
1433void jerr(const char *fmt, ...)
1434{
1435 if (printing_is_off)
1436 return;
1437
1438 va_list ap;
1439 va_start(ap, fmt);
1440 vjpout(true, "error", fmt, ap);
1441 va_end(ap);
1442}
1443
1444static char startup_datetime_buf[DATEANDEPOCHLEN];
1445
1446// Print smartctl start-up date and time and timezone
1447void jout_startup_datetime(const char *prefix)
1448{
1449 jout("%s%s\n", prefix, startup_datetime_buf);
1450}
1451
1452// Globals to set failuretest() policy
1454unsigned char failuretest_permissive = 0;
1455
1456// Compares failure type to policy in effect, and either exits or
1457// simply returns to the calling routine.
1458// Used in ataprint.cpp and scsiprint.cpp.
1459void failuretest(failure_type type, int returnvalue)
1460{
1461 // If this is an error in an "optional" SMART command
1462 if (type == OPTIONAL_CMD) {
1464 return;
1465 pout("An optional SMART command failed: exiting. Remove '-T conservative' option to continue.\n");
1466 throw int(returnvalue);
1467 }
1468
1469 // If this is an error in a "mandatory" SMART command
1470 if (type == MANDATORY_CMD) {
1472 return;
1473 pout("A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n");
1474 throw int(returnvalue);
1475 }
1476
1477 throw std::logic_error("failuretest: Unknown type");
1478}
1479
1480// Used to warn users about invalid checksums. Called from atacmds.cpp.
1481// Action to be taken may be altered by the user.
1482void checksumwarning(const char * string)
1483{
1484 // user has asked us to ignore checksum errors
1486 return;
1487
1488 pout("Warning! %s error: invalid SMART checksum.\n", string);
1489
1490 // user has asked us to fail on checksum errors
1492 throw int(FAILSMART);
1493}
1494
1495// Return info string about device protocol
1496static const char * get_protocol_info(const smart_device * dev)
1497{
1498 switch ( (int)dev->is_ata()
1499 | ((int)dev->is_scsi() << 1)
1500 | ((int)dev->is_nvme() << 2)) {
1501 case 0x1: return "ATA";
1502 case 0x2: return "SCSI";
1503 case 0x3: return "ATA+SCSI";
1504 case 0x4: return "NVMe";
1505 default: return "Unknown";
1506 }
1507}
1508
1509// Add JSON device info
1510static void js_device_info(const json::ref & jref, const smart_device * dev)
1511{
1512 jref["name"] = dev->get_dev_name();
1513 jref["info_name"] = dev->get_info_name();
1514 jref["type"] = dev->get_dev_type();
1515 jref["protocol"] = get_protocol_info(dev);
1516}
1517
1518// Device scan
1519// smartctl [-d type] --scan[-open] -- [PATTERN] [smartd directive ...]
1520void scan_devices(const smart_devtype_list & types, bool with_open, char ** argv)
1521{
1522 bool dont_print = !(ata_debugmode || scsi_debugmode || nvme_debugmode);
1523
1524 const char * pattern = 0;
1525 int ai = 0;
1526 if (argv[ai] && argv[ai][0] != '-')
1527 pattern = argv[ai++];
1528
1529 smart_device_list devlist;
1530 printing_is_off = dont_print;
1531 bool ok = smi()->scan_smart_devices(devlist, types, pattern);
1532 printing_is_off = false;
1533
1534 if (!ok) {
1535 pout("# scan_smart_devices: %s\n", smi()->get_errmsg());
1536 return;
1537 }
1538
1539 for (unsigned i = 0; i < devlist.size(); i++) {
1540 smart_device_auto_ptr dev( devlist.release(i) );
1541 json::ref jref = jglb["devices"][i];
1542
1543 if (with_open) {
1544 printing_is_off = dont_print;
1545 dev.replace ( dev->autodetect_open() );
1546 printing_is_off = false;
1547 }
1548
1549 js_device_info(jref, dev.get());
1550
1551 if (with_open && !dev->is_open()) {
1552 jout("# %s -d %s # %s, %s device open failed: %s\n", dev->get_dev_name(),
1553 dev->get_dev_type(), dev->get_info_name(),
1554 get_protocol_info(dev.get()), dev->get_errmsg());
1555 jref["open_error"] = dev->get_errmsg();
1556 continue;
1557 }
1558
1559 jout("%s -d %s", dev->get_dev_name(), dev->get_dev_type());
1560 if (!argv[ai])
1561 jout(" # %s, %s device\n", dev->get_info_name(), get_protocol_info(dev.get()));
1562 else {
1563 for (int j = ai; argv[j]; j++)
1564 jout(" %s", argv[j]);
1565 jout("\n");
1566 }
1567
1568 if (dev->is_open())
1569 dev->close();
1570 }
1571}
1572
1573// Main program without exception handling
1574static int main_worker(int argc, char **argv)
1575{
1576 // Throw if runtime environment does not match compile time test.
1577 check_config();
1578
1579 // Initialize interface
1581 if (!smi())
1582 return 1;
1583
1584 // Parse input arguments
1585 const char * type = 0;
1586 ata_print_options ataopts;
1587 scsi_print_options scsiopts;
1588 nvme_print_options nvmeopts;
1589 bool print_type_only = false;
1590 {
1591 int status = parse_options(argc, argv, type, ataopts, scsiopts, nvmeopts, print_type_only);
1592 if (status >= 0)
1593 return status;
1594 }
1595
1596 // Store formatted current time for jout_startup_datetime()
1597 // Output as JSON regardless of '-i' option
1598 {
1599 time_t now = time(nullptr);
1600 dateandtimezoneepoch(startup_datetime_buf, now);
1601 jglb["local_time"] += { {"time_t", now}, {"asctime", startup_datetime_buf} };
1602 }
1603
1604 const char * name = argv[argc-1];
1605
1607 if (!strcmp(name,"-")) {
1608 // Parse "smartctl -r ataioctl,2 ..." output from stdin
1609 if (type || print_type_only) {
1610 pout("-d option is not allowed in conjunction with device name \"-\".\n");
1611 UsageSummary();
1612 return FAILCMD;
1613 }
1614 dev = get_parsed_ata_device(smi(), name);
1615 }
1616 else
1617 // get device of appropriate type
1618 dev = smi()->get_smart_device(name, type);
1619
1620 if (!dev) {
1621 jerr("%s: %s\n", name, smi()->get_errmsg());
1622 if (type)
1624 else
1625 pout("Please specify device type with the -d option.\n");
1626 UsageSummary();
1627 return FAILCMD;
1628 }
1629
1630 if (print_type_only)
1631 // Report result of first autodetection
1632 pout("%s: Device of type '%s' [%s] detected\n",
1633 dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
1634
1635 if (dev->is_ata() && ataopts.powermode>=2 && dev->is_powered_down()) {
1636 jinf("Device is in STANDBY (OS) mode, exit(%d)\n", ataopts.powerexit);
1637 return ataopts.powerexit;
1638 }
1639
1640 // Open device
1641 {
1642 // Save old info
1643 smart_device::device_info oldinfo = dev->get_info();
1644
1645 // Open with autodetect support, may return 'better' device
1646 dev.replace( dev->autodetect_open() );
1647
1648 // Report if type has changed
1649 if ( (ata_debugmode || scsi_debugmode || nvme_debugmode || print_type_only)
1650 && oldinfo.dev_type != dev->get_dev_type() )
1651 pout("%s: Device open changed type from '%s' to '%s'\n",
1652 dev->get_info_name(), oldinfo.dev_type.c_str(), dev->get_dev_type());
1653 }
1654 if (!dev->is_open()) {
1655 jerr("Smartctl open device: %s failed: %s\n", dev->get_info_name(), dev->get_errmsg());
1656 return FAILDEV;
1657 }
1658
1659 // Add JSON info similar to --scan output
1660 js_device_info(jglb["device"], dev.get());
1661
1662 // now call appropriate ATA or SCSI routine
1663 int retval = 0;
1664 if (print_type_only)
1665 jout("%s: Device of type '%s' [%s] opened\n",
1666 dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
1667 else if (dev->is_ata())
1668 retval = ataPrintMain(dev->to_ata(), ataopts);
1669 else if (dev->is_scsi())
1670 retval = scsiPrintMain(dev->to_scsi(), scsiopts);
1671 else if (dev->is_nvme())
1672 retval = nvmePrintMain(dev->to_nvme(), nvmeopts);
1673 else
1674 // we should never fall into this branch!
1675 pout("%s: Neither ATA, SCSI nor NVMe device\n", dev->get_info_name());
1676
1677 dev->close();
1678 return retval;
1679}
1680
1681
1682// Main program
1683int main(int argc, char **argv)
1684{
1685 int status;
1686 bool badcode = false;
1687
1688 try {
1689 try {
1690 // Do the real work ...
1691 status = main_worker(argc, argv);
1692 }
1693 catch (int ex) {
1694 // Exit status from checksumwarning() and failuretest() arrives here
1695 status = ex;
1696 }
1697 // Print JSON if enabled
1699 jglb["smartctl"]["uint128_precision_bits"] = uint128_to_str_precision_bits();
1700 jglb["smartctl"]["exit_status"] = status;
1702 }
1703 catch (const std::bad_alloc & /*ex*/) {
1704 // Memory allocation failed (also thrown by std::operator new)
1705 printf("Smartctl: Out of memory\n");
1706 status = FAILCMD;
1707 }
1708 catch (const std::exception & ex) {
1709 // Other fatal errors
1710 printf("Smartctl: Exception: %s\n", ex.what());
1711 badcode = true;
1712 status = FAILCMD;
1713 }
1714
1715 // Check for remaining device objects
1716 if (smart_device::get_num_objects() != 0) {
1717 printf("Smartctl: Internal Error: %d device object(s) left at exit.\n",
1719 badcode = true;
1720 status = FAILCMD;
1721 }
1722
1723 if (badcode)
1724 printf("Please inform " PACKAGE_BUGREPORT ", including output of smartctl -V.\n");
1725
1726 return status;
1727}
1728
ata_device * get_parsed_ata_device(smart_interface *intf, const char *dev_name)
Definition: atacmds.cpp:2838
bool parse_attribute_def(const char *opt, ata_vendor_attr_defs &defs, ata_vendor_def_prior priority)
Definition: atacmds.cpp:149
std::string create_vendor_attribute_arg_list()
Definition: atacmds.cpp:262
bool parse_firmwarebug_def(const char *opt, firmwarebug_defs &firmwarebugs)
Definition: atacmds.cpp:277
unsigned char ata_debugmode
Definition: atacmds.cpp:33
const char * get_valid_firmwarebug_args()
Definition: atacmds.cpp:297
bool dont_print_serial_number
Definition: atacmds.cpp:37
#define SELECTIVE_SELF_TEST
Definition: atacmds.h:101
#define OFFLINE_FULL_SCAN
Definition: atacmds.h:97
#define SHORT_SELF_TEST
Definition: atacmds.h:98
#define EXTEND_SELF_TEST
Definition: atacmds.h:99
@ PRIOR_USER
Definition: atacmds.h:644
#define ABORT_SELF_TEST
Definition: atacmds.h:102
#define EXTEND_CAPTIVE_SELF_TEST
Definition: atacmds.h:104
#define CONVEYANCE_CAPTIVE_SELF_TEST
Definition: atacmds.h:105
#define CONVEYANCE_SELF_TEST
Definition: atacmds.h:100
#define SHORT_CAPTIVE_SELF_TEST
Definition: atacmds.h:103
#define SELECTIVE_CAPTIVE_SELF_TEST
Definition: atacmds.h:106
int ataPrintMain(ata_device *device, const ata_print_options &options)
Definition: ataprint.cpp:3341
Smart pointer class for device pointers.
device_type * get() const
Return the pointer.
void replace(device_type *dev)
Replace the pointer.
Reference to a JSON element.
Definition: json.h:105
Create and print JSON output.
Definition: json.h:24
bool has_uint128_output() const
Return true if any 128-bit value has been output.
Definition: json.h:195
bool is_enabled() const
Return true if enabled.
Definition: json.h:187
void set_verbose(bool yes=true)
Enable/disable extra string output for safe integers also.
Definition: json.h:191
void enable(bool yes=true)
Enable/disable JSON output.
Definition: json.h:183
void print(FILE *f, const print_options &options) const
Print JSON tree to a file.
Definition: json.cpp:710
List of devices for DEVICESCAN.
unsigned size() const
smart_device * release(unsigned i)
Base class for all devices.
Definition: dev_interface.h:33
bool is_scsi() const
Return true if SCSI device.
Definition: dev_interface.h:89
bool is_nvme() const
Return true if NVMe device.
Definition: dev_interface.h:92
const char * get_dev_type() const
Get device type.
const char * get_info_name() const
Get informal name.
static int get_num_objects()
Get current number of allocated 'smart_device' objects.
bool is_ata() const
Return true if ATA device.
Definition: dev_interface.h:86
const char * get_dev_name() const
Get device (path)name.
virtual std::string get_app_examples(const char *appname)
Return example string for program 'appname'.
virtual smart_device * get_smart_device(const char *name, const char *type)
Return device object for device 'name' with some 'type'.
static void init()
Initialize platform interface and register with smi().
Definition: dev_legacy.cpp:334
virtual std::string get_os_version_str()
Return info string about build host and/or OS version.
virtual bool scan_smart_devices(smart_device_list &devlist, const char *type, const char *pattern=0)
Fill 'devlist' with devices of some 'type' with device names specified by some optional 'pattern'.
virtual std::string get_valid_dev_types_str()
Return valid args for device type option/directive.
std::vector< std::string > smart_devtype_list
List of types for DEVICESCAN.
smart_interface * smi()
Global access to the (usually singleton) smart_interface.
bool init_drive_database(bool use_default_db)
const char * get_drivedb_path_add()
bool read_drive_database(const char *path)
int showmatchingpresets(const char *model, const char *firmware)
int showallpresets()
u16 s[6]
Definition: megaraid.h:18
u32 size
Definition: megaraid.h:0
unsigned char nvme_debugmode
Definition: nvmecmds.cpp:27
int nvmePrintMain(nvme_device *device, const nvme_print_options &options)
Definition: nvmeprint.cpp:568
unsigned char scsi_debugmode
Definition: scsicmds.cpp:45
int scsiPrintMain(scsi_device *device, const scsi_print_options &options)
Definition: scsiprint.cpp:3463
bool failuretest_conservative
Definition: smartctl.cpp:1453
static std::string getvalidarglist(int opt)
Definition: smartctl.cpp:228
void jout_startup_datetime(const char *prefix)
Definition: smartctl.cpp:1447
unsigned char failuretest_permissive
Definition: smartctl.cpp:1454
bool printing_is_off
Definition: smartctl.cpp:50
@ opt_scan_open
Definition: smartctl.cpp:224
@ opt_set
Definition: smartctl.cpp:224
@ opt_identify
Definition: smartctl.cpp:224
@ opt_scan
Definition: smartctl.cpp:224
@ opt_smart
Definition: smartctl.cpp:224
static int parse_options(int argc, char **argv, const char *&type, ata_print_options &ataopts, scsi_print_options &scsiopts, nvme_print_options &nvmeopts, bool &print_type_only)
Definition: smartctl.cpp:315
checksum_err_mode_t
Definition: smartctl.cpp:305
@ CHECKSUM_ERR_IGNORE
Definition: smartctl.cpp:306
@ CHECKSUM_ERR_EXIT
Definition: smartctl.cpp:306
@ CHECKSUM_ERR_WARN
Definition: smartctl.cpp:306
int main(int argc, char **argv)
Definition: smartctl.cpp:1683
static json::print_options print_as_json_options
Definition: smartctl.cpp:55
static bool print_as_json_output
Definition: smartctl.cpp:56
static void js_device_info(const json::ref &jref, const smart_device *dev)
Definition: smartctl.cpp:1510
void checksumwarning(const char *string)
Definition: smartctl.cpp:1482
static const char * get_protocol_info(const smart_device *dev)
Definition: smartctl.cpp:1496
json jglb
Definition: smartctl.cpp:53
static checksum_err_mode_t checksum_err_mode
Definition: smartctl.cpp:309
static void printvalidarglistmessage(int opt)
Definition: smartctl.cpp:287
static bool print_as_json_impl
Definition: smartctl.cpp:57
static bool print_as_json_unimpl
Definition: smartctl.cpp:58
static void js_initialize(int argc, char **argv, bool verbose)
Definition: smartctl.cpp:71
void failuretest(failure_type type, int returnvalue)
Definition: smartctl.cpp:1459
static int main_worker(int argc, char **argv)
Definition: smartctl.cpp:1574
static bool print_as_json
Definition: smartctl.cpp:54
static void UsageSummary()
Definition: smartctl.cpp:65
const char * smartctl_cpp_cvsid
Definition: smartctl.cpp:45
static void printslogan()
Definition: smartctl.cpp:60
const char const char * fmt
Definition: smartctl.cpp:1322
static void Usage()
Definition: smartctl.cpp:113
bool printing_is_switchable
Definition: smartctl.cpp:49
const char * msg_severity
Definition: smartctl.cpp:1321
static void scan_devices(const smart_devtype_list &types, bool with_open, char **argv)
Definition: smartctl.cpp:1520
const char const char va_list ap
Definition: smartctl.cpp:1323
void void jinf(const char *fmt,...) __attribute_format_printf(1
#define FAILPOWER
Definition: smartctl.h:27
#define SMARTCTL_H_CVSID
Definition: smartctl.h:16
#define FAILSMART
Definition: smartctl.h:33
void void void jwrn(const char *fmt,...) __attribute_format_printf(1
#define FAILDEV
Definition: smartctl.h:24
#define FAILCMD
Definition: smartctl.h:21
void void void void jerr(const char *fmt,...) __attribute_format_printf(1
void jout(const char *fmt,...) __attribute_format_printf(1
failure_type
Definition: smartctl.h:56
@ OPTIONAL_CMD
Definition: smartctl.h:57
@ MANDATORY_CMD
Definition: smartctl.h:58
vsnprintf(buf, sizeof(buf), fmt, ap)
void pout(const char *fmt,...)
Definition: smartd.cpp:1333
unsigned page
Definition: ataprint.h:25
unsigned char logaddr
Definition: ataprint.h:24
unsigned nsectors
Definition: ataprint.h:26
bool sct_wcache_sct_get
Definition: ataprint.h:100
bool sct_temp_hist
Definition: ataprint.h:53
bool smart_auto_offl_disable
Definition: ataprint.h:60
bool sct_wcache_reorder_get
Definition: ataprint.h:97
bool smart_auto_save_enable
Definition: ataprint.h:61
bool smart_general_values
Definition: ataprint.h:35
unsigned sct_erc_writetime
Definition: ataprint.h:56
bool devstat_all_pages
Definition: ataprint.h:48
bool set_security_freeze
Definition: ataprint.h:94
unsigned pending_defects_log
Definition: ataprint.h:51
bool set_standby_now
Definition: ataprint.h:92
int identify_word_level
Definition: ataprint.h:33
int smart_selftest_type
Definition: ataprint.h:63
bool smart_selftest_log
Definition: ataprint.h:38
int identify_bit_level
Definition: ataprint.h:33
bool smart_disable
Definition: ataprint.h:59
ata_selective_selftest_args smart_selective_args
Definition: ataprint.h:65
unsigned char powerexit
Definition: ataprint.h:81
std::vector< int > devstat_pages
Definition: ataprint.h:49
bool smart_check_status
Definition: ataprint.h:34
bool farm_log_suggest
Definition: ataprint.h:107
unsigned smart_ext_error_log
Definition: ataprint.h:42
bool fix_swapped_id
Definition: ataprint.h:74
unsigned smart_ext_selftest_log
Definition: ataprint.h:43
firmwarebug_defs firmwarebugs
Definition: ataprint.h:73
bool ignore_presets
Definition: ataprint.h:78
unsigned char powermode
Definition: ataprint.h:80
bool get_lookahead
Definition: ataprint.h:89
bool sataphy_reset
Definition: ataprint.h:57
unsigned char output_format
Definition: ataprint.h:71
int sct_wcache_reorder_set
Definition: ataprint.h:98
bool sct_temp_int_pers
Definition: ataprint.h:68
bool smart_selftest_force
Definition: ataprint.h:64
bool smart_selective_selftest_log
Definition: ataprint.h:39
bool devstat_ssd_page
Definition: ataprint.h:48
ata_vendor_attr_defs attribute_defs
Definition: ataprint.h:76
unsigned sct_temp_int
Definition: ataprint.h:67
unsigned sct_erc_readtime
Definition: ataprint.h:56
bool smart_vendor_attrib
Definition: ataprint.h:36
std::vector< ata_log_request > log_requests
Definition: ataprint.h:46
bool sct_wcache_sct_set_pers
Definition: ataprint.h:102
bool retry_error_log
Definition: ataprint.h:44
bool sct_wcache_reorder_set_pers
Definition: ataprint.h:99
bool smart_auto_offl_enable
Definition: ataprint.h:60
bool retry_selftest_log
Definition: ataprint.h:44
bool smart_error_log
Definition: ataprint.h:37
bool smart_auto_save_disable
Definition: ataprint.h:61
int sct_wcache_sct_set
Definition: ataprint.h:101
Options for print().
Definition: json.h:199
bool smart_selftest_log
Definition: nvmeprint.h:25
unsigned char smart_selftest_type
Definition: nvmeprint.h:26
unsigned char log_page
Definition: nvmeprint.h:28
unsigned log_page_size
Definition: nvmeprint.h:29
bool smart_vendor_attrib
Definition: nvmeprint.h:24
unsigned error_log_entries
Definition: nvmeprint.h:27
bool drive_capabilities
Definition: nvmeprint.h:22
bool smart_check_status
Definition: nvmeprint.h:23
short int set_rcd
Definition: scsiprint.h:53
bool smart_background_log
Definition: scsiprint.h:29
bool smart_selftest_abort
Definition: scsiprint.h:38
bool smart_auto_save_disable
Definition: scsiprint.h:33
bool smart_selftest_force
Definition: scsiprint.h:39
short int set_wce
Definition: scsiprint.h:53
unsigned char powerexit
Definition: scsiprint.h:56
bool smart_vendor_attrib
Definition: scsiprint.h:26
bool smart_default_selftest
Definition: scsiprint.h:35
bool smart_selftest_log
Definition: scsiprint.h:28
bool smart_check_status
Definition: scsiprint.h:25
bool smart_short_selftest
Definition: scsiprint.h:36
bool smart_extend_selftest
Definition: scsiprint.h:37
bool tape_device_stats
Definition: scsiprint.h:47
bool smart_auto_save_enable
Definition: scsiprint.h:33
bool smart_ss_media_log
Definition: scsiprint.h:30
unsigned char powermode
Definition: scsiprint.h:55
bool scsi_pending_defects
Definition: scsiprint.h:40
bool general_stats_and_perf
Definition: scsiprint.h:41
bool zoned_device_stats
Definition: scsiprint.h:50
Device info strings.
Definition: dev_interface.h:37
std::string dev_type
Actual device type.
Definition: dev_interface.h:47
void dateandtimezoneepoch(char(&buffer)[DATEANDEPOCHLEN], time_t tval)
Definition: utility.cpp:325
std::string strprintf(const char *fmt,...)
Definition: utility.cpp:775
int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode)
Definition: utility.cpp:606
void check_config()
Definition: utility.cpp:895
#define BUILD_INFO
Definition: utility.cpp:82
std::string format_version_info(const char *prog_name, bool full)
Definition: utility.cpp:86
#define DATEANDEPOCHLEN
Definition: utility.h:63
#define __attribute_format_printf(x, y)
Definition: utility.h:34
int uint128_to_str_precision_bits()
Definition: utility.h:295
bool str_starts_with(const char *str, const char *prefix)
Definition: utility.h:51