1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
|
/*
* smartctl.cpp
*
* Home page of code is: http://smartmontools.sourceforge.net
*
* Copyright (C) 2002-10 Bruce Allen <smartmontools-support@lists.sourceforge.net>
* Copyright (C) 2008-10 Christian Franke <smartmontools-support@lists.sourceforge.net>
* Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* You should have received a copy of the GNU General Public License
* (for example COPYING); if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* This code was originally developed as a Senior Thesis by Michael Cornwell
* at the Concurrent Systems Laboratory (now part of the Storage Systems
* Research Center), Jack Baskin School of Engineering, University of
* California, Santa Cruz. http://ssrc.soe.ucsc.edu/
*
*/
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdarg.h>
#include <stdexcept>
#include <getopt.h>
#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if defined(__FreeBSD__)
#include <sys/param.h>
#endif
#if defined(__QNXNTO__)
#include <new> // TODO: Why is this include necessary on QNX ?
#endif
#include "int64.h"
#include "atacmds.h"
#include "dev_interface.h"
#include "ataprint.h"
#include "extern.h"
#include "knowndrives.h"
#include "scsicmds.h"
#include "scsiprint.h"
#include "smartctl.h"
#include "utility.h"
const char * smartctl_cpp_cvsid = "$Id: smartctl.cpp 3119 2010-06-11 16:21:25Z chrfranke $"
CONFIG_H_CVSID EXTERN_H_CVSID SMARTCTL_H_CVSID;
// This is a block containing all the "control variables". We declare
// this globally in this file, and externally in other files.
smartmonctrl *con=NULL;
static void printslogan()
{
pout("%s\n", format_version_info("smartctl").c_str());
}
void UsageSummary(){
pout("\nUse smartctl -h to get a usage summary\n\n");
return;
}
static std::string getvalidarglist(char opt);
/* void prints help information for command syntax */
void Usage (void){
printf("Usage: smartctl [options] device\n\n");
printf(
"============================================ SHOW INFORMATION OPTIONS =====\n\n"
" -h, --help, --usage\n"
" Display this help and exit\n\n"
" -V, --version, --copyright, --license\n"
" Print license, copyright, and version information and exit\n\n"
" -i, --info \n"
" Show identity information for device\n\n"
" -a, --all \n"
" Show all SMART information for device\n\n"
" -x, --xall\n"
" Show all information for device\n\n"
" --scan\n"
" Scan for devices\n\n"
" --scan-open\n"
" Scan for devices and try to open each device\n\n"
);
printf(
"================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n"
" -q TYPE, --quietmode=TYPE (ATA)\n"
" Set smartctl quiet mode to one of: errorsonly, silent, noserial\n\n"
" -d TYPE, --device=TYPE\n"
" Specify device type to one of: %s\n\n"
" -T TYPE, --tolerance=TYPE (ATA)\n"
" Tolerance: normal, conservative, permissive, verypermissive\n\n"
" -b TYPE, --badsum=TYPE (ATA)\n"
" Set action on bad checksum to one of: warn, exit, ignore\n\n"
" -r TYPE, --report=TYPE\n"
" Report transactions (see man page)\n\n"
" -n MODE, --nocheck=MODE (ATA)\n"
" No check if: never, sleep, standby, idle (see man page)\n\n",
getvalidarglist('d').c_str()); // TODO: Use this function also for other options ?
printf(
"============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n"
" -s VALUE, --smart=VALUE\n"
" Enable/disable SMART on device (on/off)\n\n"
" -o VALUE, --offlineauto=VALUE (ATA)\n"
" Enable/disable automatic offline testing on device (on/off)\n\n"
" -S VALUE, --saveauto=VALUE (ATA)\n"
" Enable/disable Attribute autosave on device (on/off)\n\n"
);
printf(
"======================================= READ AND DISPLAY DATA OPTIONS =====\n\n"
" -H, --health\n"
" Show device SMART health status\n\n"
" -c, --capabilities (ATA)\n"
" Show device SMART capabilities\n\n"
" -A, --attributes \n"
" Show device SMART vendor-specific Attributes and values\n\n"
" -l TYPE, --log=TYPE\n"
" Show device log. TYPE: error, selftest, selective, directory[,g|s],\n"
" background, sasphy[,reset], sataphy[,reset],\n"
" scttemp[sts,hist], scterc[,N,M],\n"
" gplog,N[,RANGE], smartlog,N[,RANGE],\n"
" xerror[,N][,error], xselftest[,N][,selftest]\n\n"
" -v N,OPTION , --vendorattribute=N,OPTION (ATA)\n"
" Set display OPTION for vendor Attribute N (see man page)\n\n"
" -F TYPE, --firmwarebug=TYPE (ATA)\n"
" Use firmware bug workaround: none, samsung, samsung2,\n"
" samsung3, swapid\n\n"
" -P TYPE, --presets=TYPE (ATA)\n"
" Drive-specific presets: use, ignore, show, showall\n\n"
" -B [+]FILE, --drivedb=[+]FILE (ATA)\n"
" Read and replace [add] drive database from FILE\n"
" [default is +%s",
get_drivedb_path_add()
);
#ifdef SMARTMONTOOLS_DRIVEDBDIR
printf(
"\n"
" and then %s",
get_drivedb_path_default()
);
#endif
printf(
"]\n\n"
"============================================ DEVICE SELF-TEST OPTIONS =====\n\n"
" -t TEST, --test=TEST\n"
" Run test. TEST: offline short long conveyance select,M-N\n"
" pending,N afterselect,[on|off] scttempint,N[,p]\n\n"
" -C, --captive\n"
" Do test in captive mode (along with -t)\n\n"
" -X, --abort\n"
" Abort any non-captive test on device\n\n"
);
std::string examples = smi()->get_app_examples("smartctl");
if (!examples.empty())
printf("%s\n", examples.c_str());
}
/* Returns a string containing a formatted list of the valid arguments
to the option opt or empty on failure. Note 'v' case different */
static std::string getvalidarglist(char opt)
{
switch (opt) {
case 'q':
return "errorsonly, silent, noserial";
case 'd':
return smi()->get_valid_dev_types_str() + ", test";
case 'T':
return "normal, conservative, permissive, verypermissive";
case 'b':
return "warn, exit, ignore";
case 'r':
return "ioctl[,N], ataioctl[,N], scsiioctl[,N]";
case 's':
case 'o':
case 'S':
return "on, off";
case 'l':
return "error, selftest, selective, directory[,g|s], background, scttemp[sts|hist], scterc[,N,M], "
"sasphy[,reset], sataphy[,reset], gplog,N[,RANGE], smartlog,N[,RANGE], "
"xerror[,N][,error], xselftest[,N][,selftest]";
case 'P':
return "use, ignore, show, showall";
case 't':
return "offline, short, long, conveyance, select,M-N, pending,N, afterselect,[on|off], scttempint,N[,p]";
case 'F':
return "none, samsung, samsung2, samsung3, swapid";
case 'n':
return "never, sleep, standby, idle";
case 'v':
default:
return "";
}
}
/* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where
<LIST> is the list of valid arguments for option opt. */
void printvalidarglistmessage(char opt) {
if (opt=='v'){
pout("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n",
create_vendor_attribute_arg_list().c_str());
}
else {
// getvalidarglist() might produce a multiline or single line string. We
// need to figure out which to get the formatting right.
std::string s = getvalidarglist(opt);
char separator = strchr(s.c_str(), '\n') ? '\n' : ' ';
pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, s.c_str(), separator);
}
return;
}
// Checksum error mode
enum checksum_err_mode_t {
CHECKSUM_ERR_WARN, CHECKSUM_ERR_EXIT, CHECKSUM_ERR_IGNORE
};
static checksum_err_mode_t checksum_err_mode = CHECKSUM_ERR_WARN;
static void scan_devices(const char * type, bool with_open, const char * pattern);
/* Takes command options and sets features to be run */
const char * parse_options(int argc, char** argv,
ata_print_options & ataopts,
scsi_print_options & scsiopts)
{
// Please update getvalidarglist() if you edit shortopts
const char *shortopts = "h?Vq:d:T:b:r:s:o:S:HcAl:iaxv:P:t:CXF:n:B:";
// Please update getvalidarglist() if you edit longopts
enum { opt_scan = 1000, opt_scan_open = 1001 };
struct option longopts[] = {
{ "help", no_argument, 0, 'h' },
{ "usage", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'V' },
{ "copyright", no_argument, 0, 'V' },
{ "license", no_argument, 0, 'V' },
{ "quietmode", required_argument, 0, 'q' },
{ "device", required_argument, 0, 'd' },
{ "tolerance", required_argument, 0, 'T' },
{ "badsum", required_argument, 0, 'b' },
{ "report", required_argument, 0, 'r' },
{ "smart", required_argument, 0, 's' },
{ "offlineauto", required_argument, 0, 'o' },
{ "saveauto", required_argument, 0, 'S' },
{ "health", no_argument, 0, 'H' },
{ "capabilities", no_argument, 0, 'c' },
{ "attributes", no_argument, 0, 'A' },
{ "log", required_argument, 0, 'l' },
{ "info", no_argument, 0, 'i' },
{ "all", no_argument, 0, 'a' },
{ "xall", no_argument, 0, 'x' },
{ "vendorattribute", required_argument, 0, 'v' },
{ "presets", required_argument, 0, 'P' },
{ "test", required_argument, 0, 't' },
{ "captive", no_argument, 0, 'C' },
{ "abort", no_argument, 0, 'X' },
{ "firmwarebug", required_argument, 0, 'F' },
{ "nocheck", required_argument, 0, 'n' },
{ "drivedb", required_argument, 0, 'B' },
{ "scan", no_argument, 0, opt_scan },
{ "scan-open", no_argument, 0, opt_scan_open },
{ 0, 0, 0, 0 }
};
char extraerror[256];
memset(extraerror, 0, sizeof(extraerror));
memset(con,0,sizeof(*con));
opterr=optopt=0;
const char * type = 0; // set to -d optarg
bool no_defaultdb = false; // set true on '-B FILE'
int scan = 0; // set by --scan, --scan-open
bool badarg = false, captive = false;
int testcnt = 0; // number of self-tests requested
int optchar;
char *arg;
while ((optchar = getopt_long(argc, argv, shortopts, longopts, 0)) != -1) {
switch (optchar){
case 'V':
con->dont_print = false;
pout("%s", format_version_info("smartctl", true /*full*/).c_str());
EXIT(0);
break;
case 'q':
if (!strcmp(optarg,"errorsonly")) {
con->printing_switchable = true;
con->dont_print = false;
} else if (!strcmp(optarg,"silent")) {
con->printing_switchable = false;
con->dont_print = true;
} else if (!strcmp(optarg,"noserial")) {
con->dont_print_serial = true;
} else {
badarg = true;
}
break;
case 'd':
type = optarg;
break;
case 'T':
if (!strcmp(optarg,"normal")) {
con->conservative = false;
con->permissive = 0;
} else if (!strcmp(optarg,"conservative")) {
con->conservative = true;
} else if (!strcmp(optarg,"permissive")) {
if (con->permissive<0xff)
con->permissive++;
} else if (!strcmp(optarg,"verypermissive")) {
con->permissive = 0xff;
} else {
badarg = true;
}
break;
case 'b':
if (!strcmp(optarg,"warn")) {
checksum_err_mode = CHECKSUM_ERR_WARN;
} else if (!strcmp(optarg,"exit")) {
checksum_err_mode = CHECKSUM_ERR_EXIT;
} else if (!strcmp(optarg,"ignore")) {
checksum_err_mode = CHECKSUM_ERR_IGNORE;
} else {
badarg = true;
}
break;
case 'r':
{
int i;
char *s;
// split_report_arg() may modify its first argument string, so use a
// copy of optarg in case we want optarg for an error message.
if (!(s = strdup(optarg))) {
throw std::bad_alloc();
}
if (split_report_arg(s, &i)) {
badarg = true;
} else if (!strcmp(s,"ioctl")) {
con->reportataioctl = con->reportscsiioctl = i;
} else if (!strcmp(s,"ataioctl")) {
con->reportataioctl = i;
} else if (!strcmp(s,"scsiioctl")) {
con->reportscsiioctl = i;
} else {
badarg = true;
}
free(s);
}
break;
case 's':
if (!strcmp(optarg,"on")) {
ataopts.smart_enable = scsiopts.smart_enable = true;
ataopts.smart_disable = scsiopts.smart_disable = false;
} else if (!strcmp(optarg,"off")) {
ataopts.smart_disable = scsiopts.smart_disable = true;
ataopts.smart_enable = scsiopts.smart_enable = false;
} else {
badarg = true;
}
break;
case 'o':
if (!strcmp(optarg,"on")) {
ataopts.smart_auto_offl_enable = true;
ataopts.smart_auto_offl_disable = false;
} else if (!strcmp(optarg,"off")) {
ataopts.smart_auto_offl_disable = true;
ataopts.smart_auto_offl_enable = false;
} else {
badarg = true;
}
break;
case 'S':
if (!strcmp(optarg,"on")) {
ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = true;
ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = false;
} else if (!strcmp(optarg,"off")) {
ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = true;
ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = false;
} else {
badarg = true;
}
break;
case 'H':
ataopts.smart_check_status = scsiopts.smart_check_status = true;
break;
case 'F':
if (!strcmp(optarg,"none")) {
ataopts.fix_firmwarebug = FIX_NONE;
} else if (!strcmp(optarg,"samsung")) {
ataopts.fix_firmwarebug = FIX_SAMSUNG;
} else if (!strcmp(optarg,"samsung2")) {
ataopts.fix_firmwarebug = FIX_SAMSUNG2;
} else if (!strcmp(optarg,"samsung3")) {
ataopts.fix_firmwarebug = FIX_SAMSUNG3;
} else if (!strcmp(optarg,"swapid")) {
ataopts.fix_swapped_id = true;
} else {
badarg = true;
}
break;
case 'c':
ataopts.smart_general_values = true;
break;
case 'A':
ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true;
break;
case 'l':
if (!strcmp(optarg,"error")) {
ataopts.smart_error_log = scsiopts.smart_error_log = true;
} else if (!strcmp(optarg,"selftest")) {
ataopts.smart_selftest_log = scsiopts.smart_selftest_log = true;
} else if (!strcmp(optarg, "selective")) {
ataopts.smart_selective_selftest_log = true;
} else if (!strcmp(optarg,"directory")) {
ataopts.smart_logdir = ataopts.gp_logdir = true; // SMART+GPL
} else if (!strcmp(optarg,"directory,s")) {
ataopts.smart_logdir = true; // SMART
} else if (!strcmp(optarg,"directory,g")) {
ataopts.gp_logdir = true; // GPL
} else if (!strcmp(optarg,"sasphy")) {
scsiopts.sasphy = true;
} else if (!strcmp(optarg,"sasphy,reset")) {
scsiopts.sasphy = scsiopts.sasphy_reset = true;
} else if (!strcmp(optarg,"sataphy")) {
ataopts.sataphy = true;
} else if (!strcmp(optarg,"sataphy,reset")) {
ataopts.sataphy = ataopts.sataphy_reset = true;
} else if (!strcmp(optarg,"background")) {
scsiopts.smart_background_log = true;
} else if (!strcmp(optarg,"scterc")) {
ataopts.sct_erc_get = true;
} else if (!strcmp(optarg,"scttemp")) {
ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
} else if (!strcmp(optarg,"scttempsts")) {
ataopts.sct_temp_sts = true;
} else if (!strcmp(optarg,"scttemphist")) {
ataopts.sct_temp_hist = true;
} else if (!strncmp(optarg, "xerror", sizeof("xerror")-1)) {
int n1 = -1, n2 = -1, len = strlen(optarg);
unsigned val = 8;
sscanf(optarg, "xerror%n,error%n", &n1, &n2);
if (!(n1 == len || n2 == len)) {
n1 = n2 = -1;
sscanf(optarg, "xerror,%u%n,error%n", &val, &n1, &n2);
}
if ((n1 == len || n2 == len) && val > 0) {
ataopts.smart_ext_error_log = val;
ataopts.retry_error_log = (n2 == len);
}
else
badarg = true;
} else if (!strncmp(optarg, "xselftest", sizeof("xselftest")-1)) {
int n1 = -1, n2 = -1, len = strlen(optarg);
unsigned val = 25;
sscanf(optarg, "xselftest%n,selftest%n", &n1, &n2);
if (!(n1 == len || n2 == len)) {
n1 = n2 = -1;
sscanf(optarg, "xselftest,%u%n,selftest%n", &val, &n1, &n2);
}
if ((n1 == len || n2 == len) && val > 0) {
ataopts.smart_ext_selftest_log = val;
ataopts.retry_selftest_log = (n2 == len);
}
else
badarg = true;
} else if (!strncmp(optarg, "scterc,", sizeof("scterc,")-1)) {
unsigned rt = ~0, wt = ~0; int n = -1;
sscanf(optarg,"scterc,%u,%u%n", &rt, &wt, &n);
if (n == (int)strlen(optarg) && rt <= 999 && wt <= 999) {
ataopts.sct_erc_set = true;
ataopts.sct_erc_readtime = rt;
ataopts.sct_erc_writetime = wt;
}
else {
sprintf(extraerror, "Option -l scterc,[READTIME,WRITETIME] syntax error\n");
badarg = true;
}
} else if ( !strncmp(optarg, "gplog," , sizeof("gplog," )-1)
|| !strncmp(optarg, "smartlog,", sizeof("smartlog,")-1)) {
unsigned logaddr = ~0U; unsigned page = 0, nsectors = 1; char sign = 0;
int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg);
sscanf(optarg, "%*[a-z],0x%x%n,%u%n%c%u%n",
&logaddr, &n1, &page, &n2, &sign, &nsectors, &n3);
if (len > n2 && n3 == -1 && !strcmp(optarg+n2, "-max")) {
nsectors = ~0U; sign = '+'; n3 = len;
}
bool gpl = (optarg[0] == 'g');
const char * erropt = (gpl ? "gplog" : "smartlog");
if (!( n1 == len || n2 == len
|| (n3 == len && (sign == '+' || sign == '-')))) {
sprintf(extraerror, "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] syntax error\n", erropt);
badarg = true;
}
else if (!( logaddr <= 0xff && page <= (gpl ? 0xffffU : 0x00ffU)
&& 0 < nsectors
&& (nsectors <= (gpl ? 0xffffU : 0xffU) || nsectors == ~0U)
&& (sign != '-' || page <= nsectors) )) {
sprintf(extraerror, "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] parameter out of range\n", erropt);
badarg = true;
}
else {
ata_log_request req;
req.gpl = gpl; req.logaddr = logaddr; req.page = page;
req.nsectors = (sign == '-' ? nsectors-page+1 : nsectors);
ataopts.log_requests.push_back(req);
}
} else {
badarg = true;
}
break;
case 'i':
ataopts.drive_info = scsiopts.drive_info = true;
break;
case 'a':
ataopts.drive_info = scsiopts.drive_info = true;
ataopts.smart_check_status = scsiopts.smart_check_status = true;
ataopts.smart_general_values = true;
ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true;
ataopts.smart_error_log = scsiopts.smart_error_log = true;
ataopts.smart_selftest_log = scsiopts.smart_selftest_log = true;
ataopts.smart_selective_selftest_log = true;
/* scsiopts.smart_background_log = true; */
break;
case 'x':
ataopts.drive_info = scsiopts.drive_info = true;
ataopts.smart_check_status = scsiopts.smart_check_status = true;
ataopts.smart_general_values = true;
ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true;
ataopts.smart_ext_error_log = 8;
ataopts.retry_error_log = true;
ataopts.smart_ext_selftest_log = 25;
ataopts.retry_selftest_log = true;
scsiopts.smart_error_log = scsiopts.smart_selftest_log = true;
ataopts.smart_selective_selftest_log = true;
ataopts.smart_logdir = ataopts.gp_logdir = true;
ataopts.sct_temp_sts = ataopts.sct_temp_hist = true;
ataopts.sct_erc_get = true;
ataopts.sataphy = true;
scsiopts.smart_background_log = true;
scsiopts.sasphy = true;
break;
case 'v':
// parse vendor-specific definitions of attributes
if (!strcmp(optarg,"help")) {
con->dont_print = false;
printslogan();
pout("The valid arguments to -v are:\n\thelp\n%s\n",
create_vendor_attribute_arg_list().c_str());
EXIT(0);
}
if (!parse_attribute_def(optarg, ataopts.attribute_defs, PRIOR_USER))
badarg = true;
break;
case 'P':
if (!strcmp(optarg, "use")) {
ataopts.ignore_presets = false;
} else if (!strcmp(optarg, "ignore")) {
ataopts.ignore_presets = true;
} else if (!strcmp(optarg, "show")) {
ataopts.show_presets = true;
} else if (!strcmp(optarg, "showall")) {
if (!no_defaultdb && !read_default_drive_databases())
EXIT(FAILCMD);
if (optind < argc) { // -P showall MODEL [FIRMWARE]
int cnt = showmatchingpresets(argv[optind], (optind+1<argc ? argv[optind+1] : NULL));
EXIT(cnt); // report #matches
}
if (showallpresets())
EXIT(FAILCMD); // report regexp syntax error
EXIT(0);
} else {
badarg = true;
}
break;
case 't':
if (!strcmp(optarg,"offline")) {
testcnt++;
ataopts.smart_selftest_type = OFFLINE_FULL_SCAN;
scsiopts.smart_default_selftest = true;
} else if (!strcmp(optarg,"short")) {
testcnt++;
ataopts.smart_selftest_type = SHORT_SELF_TEST;
scsiopts.smart_short_selftest = true;
} else if (!strcmp(optarg,"long")) {
testcnt++;
ataopts.smart_selftest_type = EXTEND_SELF_TEST;
scsiopts.smart_extend_selftest = true;
} else if (!strcmp(optarg,"conveyance")) {
testcnt++;
ataopts.smart_selftest_type = CONVEYANCE_SELF_TEST;
} else if (!strcmp(optarg,"afterselect,on")) {
// scan remainder of disk after doing selected segment
ataopts.smart_selective_args.scan_after_select = 2;
} else if (!strcmp(optarg,"afterselect,off")) {
// don't scan remainder of disk after doing selected segments
ataopts.smart_selective_args.scan_after_select = 1;
} else if (!strncmp(optarg,"pending,",strlen("pending,"))) {
// parse number of minutes that test should be pending
int i;
char *tailptr=NULL;
errno=0;
i=(int)strtol(optarg+strlen("pending,"), &tailptr, 10);
if (errno || *tailptr != '\0') {
sprintf(extraerror, "Option -t pending,N requires N to be a non-negative integer\n");
badarg = true;
} else if (i<0 || i>65535) {
sprintf(extraerror, "Option -t pending,N (N=%d) must have 0 <= N <= 65535\n", i);
badarg = true;
} else {
ataopts.smart_selective_args.pending_time = i+1;
}
} else if (!strncmp(optarg,"select",strlen("select"))) {
if (ataopts.smart_selective_args.num_spans == 0)
testcnt++;
// parse range of LBAs to test
uint64_t start, stop; int mode;
if (split_selective_arg(optarg, &start, &stop, &mode)) {
sprintf(extraerror, "Option -t select,M-N must have non-negative integer M and N\n");
badarg = true;
} else {
if (ataopts.smart_selective_args.num_spans >= 5 || start > stop) {
if (start > stop) {
sprintf(extraerror, "ERROR: Start LBA (%"PRIu64") > ending LBA (%"PRId64") in argument \"%s\"\n",
start, stop, optarg);
} else {
sprintf(extraerror,"ERROR: No more than five selective self-test spans may be"
" defined\n");
}
badarg = true;
}
ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].start = start;
ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].end = stop;
ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].mode = mode;
ataopts.smart_selective_args.num_spans++;
ataopts.smart_selftest_type = SELECTIVE_SELF_TEST;
}
} else if (!strncmp(optarg, "scttempint,", sizeof("scstempint,")-1)) {
unsigned interval = 0; int n1 = -1, n2 = -1, len = strlen(optarg);
if (!( sscanf(optarg,"scttempint,%u%n,p%n", &interval, &n1, &n2) == 1
&& 0 < interval && interval <= 0xffff && (n1 == len || n2 == len))) {
strcpy(extraerror, "Option -t scttempint,N[,p] must have positive integer N\n");
badarg = true;
}
ataopts.sct_temp_int = interval;
ataopts.sct_temp_int_pers = (n2 == len);
} else {
badarg = true;
}
break;
case 'C':
captive = true;
break;
case 'X':
testcnt++;
scsiopts.smart_selftest_abort = true;
ataopts.smart_selftest_type = ABORT_SELF_TEST;
break;
case 'n':
// skip disk check if in low-power mode
if (!strcmp(optarg, "never"))
ataopts.powermode = 1; // do not skip, but print mode
else if (!strcmp(optarg, "sleep"))
ataopts.powermode = 2;
else if (!strcmp(optarg, "standby"))
ataopts.powermode = 3;
else if (!strcmp(optarg, "idle"))
ataopts.powermode = 4;
else
badarg = true;
break;
case 'B':
{
const char * path = optarg;
if (*path == '+' && path[1])
path++;
else
no_defaultdb = true;
if (!read_drive_database(path))
EXIT(FAILCMD);
}
break;
case 'h':
con->dont_print = false;
printslogan();
Usage();
EXIT(0);
break;
case opt_scan:
case opt_scan_open:
scan = optchar;
break;
case '?':
default:
con->dont_print = false;
printslogan();
// Point arg to the argument in which this option was found.
arg = argv[optind-1];
// Check whether the option is a long option that doesn't map to -h.
if (arg[1] == '-' && optchar != 'h') {
// Iff optopt holds a valid option then argument must be missing.
if (optopt && (strchr(shortopts, optopt) != NULL)) {
pout("=======> ARGUMENT REQUIRED FOR OPTION: %s\n", arg+2);
printvalidarglistmessage(optopt);
} else
pout("=======> UNRECOGNIZED OPTION: %s\n",arg+2);
if (extraerror[0])
pout("=======> %s", extraerror);
UsageSummary();
EXIT(FAILCMD);
}
if (optopt) {
// Iff optopt holds a valid option then argument must be
// missing. Note (BA) this logic seems to fail using Solaris
// getopt!
if (strchr(shortopts, optopt) != NULL) {
pout("=======> ARGUMENT REQUIRED FOR OPTION: %c\n", optopt);
printvalidarglistmessage(optopt);
} else
pout("=======> UNRECOGNIZED OPTION: %c\n",optopt);
if (extraerror[0])
pout("=======> %s", extraerror);
UsageSummary();
EXIT(FAILCMD);
}
Usage();
EXIT(0);
} // closes switch statement to process command-line options
// Check to see if option had an unrecognized or incorrect argument.
if (badarg) {
printslogan();
// It would be nice to print the actual option name given by the user
// here, but we just print the short form. Please fix this if you know
// a clean way to do it.
pout("=======> INVALID ARGUMENT TO -%c: %s\n", optchar, optarg);
printvalidarglistmessage(optchar);
if (extraerror[0])
pout("=======> %s", extraerror);
UsageSummary();
EXIT(FAILCMD);
}
}
// Special handling of --scan, --scanopen
if (scan) {
scan_devices(type, (scan == opt_scan_open), argv[optind]);
EXIT(0);
}
// At this point we have processed all command-line options. If the
// print output is switchable, then start with the print output
// turned off
if (con->printing_switchable)
con->dont_print = true;
// error message if user has asked for more than one test
if (testcnt > 1) {
con->dont_print = false;
printslogan();
pout("\nERROR: smartctl can only run a single test type (or abort) at a time.\n");
UsageSummary();
EXIT(FAILCMD);
}
// error message if user has set selective self-test options without
// asking for a selective self-test
if ( (ataopts.smart_selective_args.pending_time || ataopts.smart_selective_args.scan_after_select)
&& !ataopts.smart_selective_args.num_spans) {
con->dont_print = false;
printslogan();
if (ataopts.smart_selective_args.pending_time)
pout("\nERROR: smartctl -t pending,N must be used with -t select,N-M.\n");
else
pout("\nERROR: smartctl -t afterselect,(on|off) must be used with -t select,N-M.\n");
UsageSummary();
EXIT(FAILCMD);
}
// If captive option was used, change test type if appropriate.
if (captive)
switch (ataopts.smart_selftest_type) {
case SHORT_SELF_TEST:
ataopts.smart_selftest_type = SHORT_CAPTIVE_SELF_TEST;
scsiopts.smart_short_selftest = false;
scsiopts.smart_short_cap_selftest = true;
break;
case EXTEND_SELF_TEST:
ataopts.smart_selftest_type = EXTEND_CAPTIVE_SELF_TEST;
scsiopts.smart_extend_selftest = false;
scsiopts.smart_extend_cap_selftest = true;
break;
case CONVEYANCE_SELF_TEST:
ataopts.smart_selftest_type = CONVEYANCE_CAPTIVE_SELF_TEST;
break;
case SELECTIVE_SELF_TEST:
ataopts.smart_selftest_type = SELECTIVE_CAPTIVE_SELF_TEST;
break;
}
// From here on, normal operations...
printslogan();
// Warn if the user has provided no device name
if (argc-optind<1){
pout("ERROR: smartctl requires a device name as the final command-line argument.\n\n");
UsageSummary();
EXIT(FAILCMD);
}
// Warn if the user has provided more than one device name
if (argc-optind>1){
int i;
pout("ERROR: smartctl takes ONE device name as the final command-line argument.\n");
pout("You have provided %d device names:\n",argc-optind);
for (i=0; i<argc-optind; i++)
pout("%s\n",argv[optind+i]);
UsageSummary();
EXIT(FAILCMD);
}
// Read or init drive database
if (!no_defaultdb && !read_default_drive_databases())
EXIT(FAILCMD);
return type;
}
// Printing function (controlled by global con->dont_print)
// [From GLIBC Manual: Since the prototype doesn't specify types for
// optional arguments, in a call to a variadic function the default
// argument promotions are performed on the optional argument
// values. This means the objects of type char or short int (whether
// signed or not) are promoted to either int or unsigned int, as
// appropriate.]
void pout(const char *fmt, ...){
va_list ap;
// initialize variable argument list
va_start(ap,fmt);
if (con->dont_print){
va_end(ap);
return;
}
// print out
vprintf(fmt,ap);
va_end(ap);
fflush(stdout);
return;
}
// This function is used by utility.cpp to report LOG_CRIT errors.
// The smartctl version prints to stdout instead of syslog().
void PrintOut(int priority, const char *fmt, ...) {
va_list ap;
// avoid warning message about unused variable from gcc -W: just
// change value of local copy.
priority=0;
va_start(ap,fmt);
vprintf(fmt,ap);
va_end(ap);
return;
}
// Used to warn users about invalid checksums. Called from atacmds.cpp.
// Action to be taken may be altered by the user.
void checksumwarning(const char * string)
{
// user has asked us to ignore checksum errors
if (checksum_err_mode == CHECKSUM_ERR_IGNORE)
return;
pout("Warning! %s error: invalid SMART checksum.\n", string);
// user has asked us to fail on checksum errors
if (checksum_err_mode == CHECKSUM_ERR_EXIT)
EXIT(FAILSMART);
}
// Return info string about device protocol
static const char * get_protocol_info(const smart_device * dev)
{
switch ((int)dev->is_ata() | ((int)dev->is_scsi() << 1)) {
case 0x1: return "ATA";
case 0x2: return "SCSI";
case 0x3: return "ATA+SCSI";
default: return "Unknown";
}
}
// Device scan
// smartctl [-d type] --scan[-open] [PATTERN]
void scan_devices(const char * type, bool with_open, const char * pattern)
{
bool dont_print = !(con->reportataioctl || con->reportscsiioctl);
smart_device_list devlist;
con->dont_print = dont_print;
bool ok = smi()->scan_smart_devices(devlist, type , pattern);
con->dont_print = false;
if (!ok) {
pout("scan_smart_devices: %s\n", smi()->get_errmsg());
return;
}
for (unsigned i = 0; i < devlist.size(); i++) {
smart_device * dev = devlist.at(i);
std::string openmsg;
if (with_open) {
con->dont_print = dont_print;
dev = dev->autodetect_open();
con->dont_print = false;
if (dev->is_open())
openmsg = " (opened)";
else
openmsg = strprintf(" (open failed: %s)", dev->get_errmsg());
}
pout("%s -d %s [%s]%s\n", dev->get_info_name(), dev->get_dev_type(),
get_protocol_info(dev), openmsg.c_str());
if (dev->is_open())
dev->close();
}
}
// Main program without exception handling
int main_worker(int argc, char **argv)
{
// Throw if CPU endianess does not match compile time test.
check_endianness();
// Initialize interface
smart_interface::init();
if (!smi())
return 1;
// define control block for external functions
smartmonctrl control;
con=&control;
// Parse input arguments
ata_print_options ataopts;
scsi_print_options scsiopts;
const char * type = parse_options(argc, argv, ataopts, scsiopts);
// '-d test' -> Report result of autodetection
bool print_type_only = (type && !strcmp(type, "test"));
if (print_type_only)
type = 0;
const char * name = argv[argc-1];
smart_device_auto_ptr dev;
if (!strcmp(name,"-")) {
// Parse "smartctl -r ataioctl,2 ..." output from stdin
if (type || print_type_only) {
pout("Smartctl: -d option is not allowed in conjunction with device name \"-\".\n");
UsageSummary();
return FAILCMD;
}
dev = get_parsed_ata_device(smi(), name);
}
else
// get device of appropriate type
dev = smi()->get_smart_device(name, type);
if (!dev) {
pout("%s: %s\n", name, smi()->get_errmsg());
if (type)
printvalidarglistmessage('d');
else
pout("Smartctl: please specify device type with the -d option.\n");
UsageSummary();
return FAILCMD;
}
if (print_type_only)
// Report result of first autodetection
pout("%s: Device of type '%s' [%s] detected\n",
dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
// Open device
{
// Save old info
smart_device::device_info oldinfo = dev->get_info();
// Open with autodetect support, may return 'better' device
dev.replace( dev->autodetect_open() );
// Report if type has changed
if ((type || print_type_only) && oldinfo.dev_type != dev->get_dev_type())
pout("%s: Device open changed type from '%s' to '%s'\n",
dev->get_info_name(), oldinfo.dev_type.c_str(), dev->get_dev_type());
}
if (!dev->is_open()) {
pout("Smartctl open device: %s failed: %s\n", dev->get_info_name(), dev->get_errmsg());
return FAILDEV;
}
// now call appropriate ATA or SCSI routine
int retval = 0;
if (print_type_only)
pout("%s: Device of type '%s' [%s] opened\n",
dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get()));
else if (dev->is_ata())
retval = ataPrintMain(dev->to_ata(), ataopts);
else if (dev->is_scsi())
retval = scsiPrintMain(dev->to_scsi(), scsiopts);
else
// we should never fall into this branch!
pout("%s: Neither ATA nor SCSI device\n", dev->get_info_name());
dev->close();
return retval;
}
// Main program
int main(int argc, char **argv)
{
int status;
try {
// Do the real work ...
status = main_worker(argc, argv);
}
catch (int ex) {
// EXIT(status) arrives here
status = ex;
}
catch (const std::bad_alloc & /*ex*/) {
// Memory allocation failed (also thrown by std::operator new)
printf("Smartctl: Out of memory\n");
status = FAILCMD;
}
catch (const std::exception & ex) {
// Other fatal errors
printf("Smartctl: Exception: %s\n", ex.what());
status = FAILCMD;
}
return status;
}
|