📄 smartctl.cpp
字号:
/* * smartctl.cpp * * Home page of code is: http://smartmontools.sourceforge.net * * Copyright (C) 2002-8 Bruce Allen <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 "config.h"#ifdef HAVE_GETOPT_LONG#include <getopt.h>#endif#if defined(__FreeBSD_version) && (__FreeBSD_version < 500000)#include <unistd.h>#endif#if defined(__QNXNTO__) #include <unistd.h>#endif#include "int64.h"#include "atacmds.h"#include "ataprint.h"#include "extern.h"#include "knowndrives.h"#include "scsicmds.h"#include "scsiprint.h"#include "smartctl.h"#include "utility.h"#ifdef NEED_SOLARIS_ATA_CODEextern const char *os_solaris_ata_s_cvsid;#endifextern const char *atacmdnames_c_cvsid, *atacmds_c_cvsid, *ataprint_c_cvsid, *knowndrives_c_cvsid, *os_XXXX_c_cvsid, *scsicmds_c_cvsid, *scsiprint_c_cvsid, *utility_c_cvsid;const char* smartctl_c_cvsid="$Id: smartctl.cpp,v 1.169 2008/03/04 22:09:47 ballen4705 Exp $"ATACMDS_H_CVSID ATAPRINT_H_CVSID CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID KNOWNDRIVES_H_CVSID SCSICMDS_H_CVSID SCSIPRINT_H_CVSID SMARTCTL_H_CVSID UTILITY_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;// to hold onto exit code for atexit routineextern int exitstatus;// Track memory useextern int64_t bytes;void printslogan(){#ifdef HAVE_GET_OS_VERSION_STR const char * ver = get_os_version_str();#else const char * ver = SMARTMONTOOLS_BUILD_HOST;#endif pout("smartctl version %s [%s] Copyright (C) 2002-8 Bruce Allen\n", PACKAGE_VERSION, ver); pout("Home page is " PACKAGE_HOMEPAGE "\n\n"); return;}void PrintOneCVS(const char *a_cvs_id){ char out[CVSMAXLEN]; printone(out,a_cvs_id); pout("%s",out); return;}void printcopy(){ const char *configargs=strlen(SMARTMONTOOLS_CONFIGURE_ARGS)?SMARTMONTOOLS_CONFIGURE_ARGS:"[no arguments given]"; pout("smartctl comes with ABSOLUTELY NO WARRANTY. This\n"); pout("is free software, and you are welcome to redistribute it\n"); pout("under the terms of the GNU General Public License Version 2.\n"); pout("See http://www.gnu.org for further details.\n\n"); pout("CVS version IDs of files used to build this code are:\n"); PrintOneCVS(atacmdnames_c_cvsid); PrintOneCVS(atacmds_c_cvsid); PrintOneCVS(ataprint_c_cvsid); PrintOneCVS(knowndrives_c_cvsid); PrintOneCVS(os_XXXX_c_cvsid);#ifdef NEED_SOLARIS_ATA_CODE PrintOneCVS(os_solaris_ata_s_cvsid);#endif PrintOneCVS(scsicmds_c_cvsid); PrintOneCVS(scsiprint_c_cvsid); PrintOneCVS(smartctl_c_cvsid); PrintOneCVS(utility_c_cvsid); pout("\nsmartmontools release " PACKAGE_VERSION " dated " SMARTMONTOOLS_RELEASE_DATE " at " SMARTMONTOOLS_RELEASE_TIME "\n"); pout("smartmontools build host: " SMARTMONTOOLS_BUILD_HOST "\n"); pout("smartmontools build configured: " SMARTMONTOOLS_CONFIGURE_DATE "\n"); pout("smartctl compile dated " __DATE__ " at "__TIME__ "\n"); pout("smartmontools configure arguments: %s\n", configargs); return;}void UsageSummary(){ pout("\nUse smartctl -h to get a usage summary\n\n"); return;}/* void prints help information for command syntax */void Usage (void){ printf("Usage: smartctl [options] device\n\n"); printf("============================================ SHOW INFORMATION OPTIONS =====\n\n");#ifdef HAVE_GETOPT_LONG printf(" -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" );#else printf(" -h Display this help and exit\n"" -V Print license, copyright, and version information\n"" -i Show identity information for device\n"" -a Show all SMART information for device\n\n" );#endif printf("================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n");#ifdef HAVE_GETOPT_LONG printf(" -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: ata, scsi, marvell, sat, 3ware,N\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" );#else printf(" -q TYPE Set smartctl quiet mode to one of: errorsonly, silent, (ATA)\n"" noserial\n"" -d TYPE Specify device type to one of: ata, scsi, 3ware,N\n"" -T TYPE Tolerance: normal, conservative,permissive,verypermissive (ATA)\n"" -b TYPE Set action on bad checksum to one of: warn, exit, ignore (ATA)\n"" -r TYPE Report transactions (see man page)\n"" -n MODE No check if: never, sleep, standby, idle (see man page) (ATA)\n\n" );#endif printf("============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n");#ifdef HAVE_GETOPT_LONG printf(" -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" );#else printf(" -s VALUE Enable/disable SMART on device (on/off)\n"" -o VALUE Enable/disable device automatic offline testing (on/off) (ATA)\n"" -S VALUE Enable/disable device Attribute autosave (on/off) (ATA)\n\n" );#endif printf("======================================= READ AND DISPLAY DATA OPTIONS =====\n\n");#ifdef HAVE_GETOPT_LONG printf(" -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,\n"" background, scttemp[sts,hist]\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" );#else printf(" -H Show device SMART health status\n"" -c Show device SMART capabilities (ATA)\n"" -A Show device SMART vendor-specific Attributes and values (ATA)\n"" -l TYPE Show device log. TYPE: error, selftest, selective, directory,\n"" background, scttemp[sts,hist]\n"" -v N,OPT Set display OPTion for vendor Attribute N (see man page) (ATA)\n"" -F TYPE Use firmware bug workaround: none, samsung, samsung2, (ATA)\n"" samsung3, swapid\n"" -P TYPE Drive-specific presets: use, ignore, show, showall (ATA)\n\n" );#endif printf("============================================ DEVICE SELF-TEST OPTIONS =====\n\n");#ifdef HAVE_GETOPT_LONG printf(" -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");#else printf(" -t TEST Run test. TEST: offline short long conveyance select,M-N\n"" pending,N afterselect,[on|off] scttempint,N[,p]\n"" -C Do test in captive mode (along with -t)\n"" -X Abort any non-captive test\n\n" );#endif print_smartctl_examples(); return;}/* Returns a pointer to a static string containing a formatted list of the valid arguments to the option opt or NULL on failure. Note 'v' case different */const char *getvalidarglist(char opt) { switch (opt) { case 'q': return "errorsonly, silent, noserial"; case 'd': return "ata, scsi, marvell, sat, 3ware,N, hpt,L/M/N cciss,N"; 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, background, scttemp[sts|hist]"; 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 NULL; }}/* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where <LIST> is the list of valid arguments for option opt. */void printvalidarglistmessage(char opt) { char *s; if (opt=='v') s=create_vendor_attribute_arg_list(); else s=(char *)getvalidarglist(opt); if (!s) { pout("Error whilst constructing argument list for option %c", opt); return; } if (opt=='v'){ pout("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n", s); free(s); } else { // getvalidarglist() might produce a multiline or single line string. We // need to figure out which to get the formatting right. char separator = strchr(s, '\n') ? '\n' : ' '; pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, (char *)s, separator); } return;}/* Takes command options and sets features to be run */ void ParseOpts (int argc, char** argv){ int optchar; int badarg; int captive; unsigned char *charp; extern char *optarg; extern int optopt, optind, opterr; char extraerror[256]; // Please update getvalidarglist() if you edit shortopts const char *shortopts = "h?Vq:d:T:b:r:s:o:S:HcAl:iav:P:t:CXF:n:";#ifdef HAVE_GETOPT_LONG char *arg; // Please update getvalidarglist() if you edit longopts 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' }, { "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' }, { 0, 0, 0, 0 } };#endif memset(extraerror, 0, sizeof(extraerror)); memset(con,0,sizeof(*con));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -