smartmontools  SVN Rev 4325
Utility to control and monitor storage systems with "S.M.A.R.T."
os_solaris.cpp
Go to the documentation of this file.
1 /*
2  * os_solaris.c
3  *
4  * Home page of code is: http://www.smartmontools.org
5  *
6  * Copyright (C) 2003-08 SAWADA Keiji
7  * Copyright (C) 2003-15 Casper Dik
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * You should have received a copy of the GNU General Public License
15  * (for example COPYING); if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/param.h>
27 
28 // These are needed to define prototypes for the functions defined below
29 #include "config.h"
30 #include "int64.h"
31 #include "atacmds.h"
32 #include "scsicmds.h"
33 #include "utility.h"
34 
35 // This is to include whatever prototypes you define in os_solaris.h
36 #include "os_solaris.h"
37 
38 #define ARGUSED(x) ((void)(x))
39 
40 const char *os_XXXX_c_cvsid="$Id: os_solaris.cpp 4253 2016-03-26 19:47:47Z chrfranke $" \
42 
43 // The printwarning() function warns about unimplemented functions
44 int printedout[2];
45 char *unimplemented[2]={
46  "ATA command routine ata_command_interface()",
47  "3ware Escalade Controller command routine escalade_command_interface()",
48 };
49 
50 int printwarning(int which){
51  if (!unimplemented[which])
52  return 0;
53 
54  if (printedout[which])
55  return 1;
56 
57  printedout[which]=1;
58 
59  pout("\n"
60  "#######################################################################\n"
61  "%s NOT IMPLEMENTED under Solaris.\n"
62  "Please contact " PACKAGE_BUGREPORT " if\n"
63  "you want to help in porting smartmontools to Solaris.\n"
64  "#######################################################################\n"
65  "\n",
66  unimplemented[which]);
67 
68  return 1;
69 }
70 
71 // print examples for smartctl
73  printf("=================================================== SMARTCTL EXAMPLES =====\n\n");
74 #ifdef HAVE_GETOPT_LONG
75  printf(
76  " smartctl -a /dev/rdsk/c0t0d0s0 (Prints all SMART information)\n\n"
77  " smartctl --smart=on --offlineauto=on --saveauto=on /dev/rdsk/c0t0d0s0\n"
78  " (Enables SMART on first disk)\n\n"
79  " smartctl -t long /dev/rdsk/c0t0d0s0 (Executes extended disk self-test)\n\n"
80  " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/rdsk/c0t0d0s0\n"
81  " (Prints Self-Test & Attribute errors)\n"
82  );
83 #else
84  printf(
85  " smartctl -a /dev/rdsk/c0t0d0s0 (Prints all SMART information)\n"
86  " smartctl -s on -o on -S on /dev/rdsk/c0t0d0s0 (Enables SMART on first disk)\n"
87  " smartctl -t long /dev/rdsk/c0t0d0s0 (Executes extended disk self-test)\n"
88  " smartctl -A -l selftest -q errorsonly /dev/rdsk/c0t0d0s0\n"
89  " (Prints Self-Test & Attribute errors)\n"
90  );
91 #endif
92  return;
93 }
94 
95 static const char *uscsidrvrs[] = {
96  "sd",
97  "ssd",
98  "disk", // SATA devices
99  "st"
100 };
101 
102 static const char *atadrvrs[] = {
103  "cmdk",
104  "dad",
105 };
106 
107 static int
108 isdevtype(const char *dev_name, const char *table[], int tsize)
109 {
110  char devpath[MAXPATHLEN];
111  int i;
112  char *basename;
113 
114  if (realpath(dev_name, devpath) == NULL)
115  return 0;
116 
117  if ((basename = strrchr(devpath, '/')) == NULL)
118  return 0;
119 
120  basename++;
121 
122  for (i = 0; i < tsize; i++) {
123  int l = strlen(table[i]);
124  if (strncmp(basename, table[i], l) == 0 && basename[l] == '@')
125  return 1;
126  }
127  return 0;
128 }
129 
130 static int
131 isscsidev(const char *path)
132 {
133  return isdevtype(path, uscsidrvrs, sizeof (uscsidrvrs) / sizeof (char *));
134 }
135 
136 static int
137 isatadev(const char *path)
138 {
139  return isdevtype(path, atadrvrs, sizeof (atadrvrs) / sizeof (char *));
140 }
141 
142 // tries to guess device type given the name (a path)
143 int guess_device_type (const char* dev_name) {
144  if (isscsidev(dev_name))
145  return CONTROLLER_SCSI;
146  else if (isatadev(dev_name))
147  return CONTROLLER_ATA;
148  else
149  return CONTROLLER_UNKNOWN;
150 }
151 
152 struct pathlist {
153  char **names;
154  int nnames;
155  int maxnames;
156 };
157 
158 static int
159 addpath(const char *path, struct pathlist *res)
160 {
161  if (++res->nnames > res->maxnames) {
162  res->maxnames += 16;
163  res->names = static_cast<char**>(realloc(res->names, res->maxnames * sizeof (char *)));
164  if (res->names == NULL)
165  return -1;
166  }
167  if (!(res->names[res->nnames-1] = strdup(path)))
168  return -1;
169  return 0;
170 }
171 
172 static int
173 grokdir(const char *dir, struct pathlist *res, int testfun(const char *))
174 {
175  char pathbuf[MAXPATHLEN];
176  size_t len;
177  DIR *dp;
178  struct dirent *de;
179  int isdisk = strstr(dir, "dsk") != NULL;
180  char *p;
181 
182  len = snprintf(pathbuf, sizeof (pathbuf), "%s/", dir);
183  if (len >= sizeof (pathbuf))
184  return -1;
185 
186  dp = opendir(dir);
187  if (dp == NULL)
188  return 0;
189 
190  while ((de = readdir(dp)) != NULL) {
191  if (de->d_name[0] == '.')
192  continue;
193 
194  if (strlen(de->d_name) + len >= sizeof (pathbuf))
195  continue;
196 
197  if (isdisk) {
198  /* Disk represented by slice 0 */
199  p = strstr(de->d_name, "s0");
200  /* String doesn't end in "s0\0" */
201  if (p == NULL || p[2] != '\0')
202  continue;
203  } else {
204  /* Tape drive represented by the all-digit device */
205  for (p = de->d_name; *p; p++)
206  if (!isdigit((int)(*p)))
207  break;
208  if (*p != '\0')
209  continue;
210  }
211  strcpy(&pathbuf[len], de->d_name);
212  if (testfun(pathbuf)) {
213  if (addpath(pathbuf, res) == -1) {
214  closedir(dp);
215  return -1;
216  }
217  }
218  }
219  closedir(dp);
220 
221  return 0;
222 }
223 
224 // makes a list of ATA or SCSI devices for the DEVICESCAN directive of
225 // smartd. Returns number of devices, or -1 if out of memory.
226 int make_device_names (char*** devlist, const char* name) {
227  struct pathlist res;
228 
229  res.nnames = res.maxnames = 0;
230  res.names = NULL;
231  if (strcmp(name, "SCSI") == 0) {
232  if (grokdir("/dev/rdsk", &res, isscsidev) == -1)
233  return -1;
234  if (grokdir("/dev/rmt", &res, isscsidev) == -1)
235  return -1;
236  } else if (strcmp(name, "ATA") == 0) {
237  if (grokdir("/dev/rdsk", &res, isatadev) == -1)
238  return -1;
239  } else {
240  // non-SCSI and non-ATA case not implemented
241  *devlist=NULL;
242  return 0;
243  }
244 
245  // shrink array to min possible size
246  res.names = static_cast<char**>(realloc(res.names, res.nnames * sizeof (char *)));
247 
248  // pass list back
249  *devlist = res.names;
250  return res.nnames;
251 }
252 
253 // Like open(). Return integer handle, used by functions below only.
254 // type="ATA" or "SCSI".
255 int deviceopen(const char *pathname, char *type){
256  if (!strcmp(type,"SCSI"))
257  return open(pathname, O_RDWR | O_NONBLOCK);
258  else if (!strcmp(type,"ATA"))
259  return open(pathname, O_RDONLY | O_NONBLOCK);
260  else
261  return -1;
262 }
263 
264 // Like close(). Acts on handles returned by above function.
265 int deviceclose(int fd){
266  return close(fd);
267 }
268 
269 #if defined(WITH_SOLARIS_SPARC_ATA)
270 // swap each 2-byte pairs in a sector
271 static void swap_sector(void *p)
272 {
273  int i;
274  char t, *cp = static_cast<char*>(p);
275  for(i = 0; i < 256; i++) {
276  t = cp[0]; cp[0] = cp[1]; cp[1] = t;
277  cp += 2;
278  }
279 }
280 #endif
281 
282 // Interface to ATA devices. See os_linux.c
283 int ata_command_interface(int fd, smart_command_set command, int select, char *data){
284 #if defined(WITH_SOLARIS_SPARC_ATA)
285  int err;
286 
287  switch (command){
288  case CHECK_POWER_MODE:
289  /* currently not recognized */
290  return -1;
291  case READ_VALUES:
292  return smart_read_data(fd, data);
293  case READ_THRESHOLDS:
294  return smart_read_thresholds(fd, data);
295  case READ_LOG:
296  return smart_read_log(fd, select, 1, data);
297  case IDENTIFY:
298  err = ata_identify(fd, data);
299  if(err) return err;
300  swap_sector(static_cast<void*>(data));
301  return 0;
302  case PIDENTIFY:
303  err = ata_pidentify(fd, data);
304  if(err) return err;
305  swap_sector(static_cast<void*>(data));
306  return 0;
307  case ENABLE:
308  return smart_enable(fd);
309  case DISABLE:
310  return smart_disable(fd);
311  case STATUS:
312  return smart_status(fd);
313  case AUTO_OFFLINE:
314  return smart_auto_offline(fd, select);
315  case AUTOSAVE:
316  return smart_auto_save(fd, select);
317  case IMMEDIATE_OFFLINE:
318  return smart_immediate_offline(fd, select);
319  case STATUS_CHECK:
320  return smart_status_check(fd);
321  default:
322  pout("Unrecognized command %d in ata_command_interface() of os_solaris.c\n", command);
323  EXIT(1);
324  break;
325  }
326 #else /* WITH_SOLARIS_SPARC_ATA */
327  ARGUSED(fd); ARGUSED(command); ARGUSED(select); ARGUSED(data);
328 
329  /* Above smart_* routines uses undocumented ioctls of "dada"
330  * driver, which is specific to SPARC Solaris. See
331  * os_solaris_ata.s for further details. x86 Solaris seems not to
332  * provide similar or alternative interface... */
333  if (printwarning(0))
334  return -1;
335 #endif
336  return -1;
337 }
338 
339 #include <errno.h>
340 #include <sys/scsi/generic/commands.h>
341 #include <sys/scsi/generic/status.h>
342 #include <sys/scsi/impl/types.h>
343 #include <sys/scsi/impl/uscsi.h>
344 
345 // Interface to SCSI devices. See os_linux.c
346 int do_scsi_cmnd_io(int fd, struct scsi_cmnd_io * iop, int report)
347 {
348  struct uscsi_cmd uscsi;
349 
350  if (report > 0) {
351  int k;
352  const unsigned char * ucp = iop->cmnd;
353  const char * np;
354 
355  np = scsi_get_opcode_name(ucp[0]);
356  pout(" [%s: ", np ? np : "<unknown opcode>");
357  for (k = 0; k < (int)iop->cmnd_len; ++k)
358  pout("%02x ", ucp[k]);
359  pout("]\n");
360  if ((report > 1) &&
361  (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) {
362  int trunc = (iop->dxfer_len > 256) ? 1 : 0;
363 
364  pout(" Outgoing data, len=%d%s:\n", (int)iop->dxfer_len,
365  (trunc ? " [only first 256 bytes shown]" : ""));
366  dStrHex((char *)iop->dxferp, (trunc ? 256 : iop->dxfer_len) , 1);
367  }
368  }
369  memset(&uscsi, 0, sizeof (uscsi));
370 
371  uscsi.uscsi_cdb = reinterpret_cast<char*>(iop->cmnd);
372  uscsi.uscsi_cdblen = iop->cmnd_len;
373  if (iop->timeout == 0)
374  uscsi.uscsi_timeout = 60; /* 60 seconds */
375  else
376  uscsi.uscsi_timeout = iop->timeout;
377  uscsi.uscsi_bufaddr = reinterpret_cast<char*>(iop->dxferp);
378  uscsi.uscsi_buflen = iop->dxfer_len;
379  uscsi.uscsi_rqbuf = reinterpret_cast<char*>(iop->sensep);
380  uscsi.uscsi_rqlen = iop->max_sense_len;
381 
382  switch (iop->dxfer_dir) {
383  case DXFER_NONE:
384  case DXFER_FROM_DEVICE:
385  uscsi.uscsi_flags = USCSI_READ;
386  break;
387  case DXFER_TO_DEVICE:
388  uscsi.uscsi_flags = USCSI_WRITE;
389  break;
390  default:
391  return -EINVAL;
392  }
393  uscsi.uscsi_flags |= (USCSI_ISOLATE | USCSI_RQENABLE | USCSI_SILENT);
394 
395  if (ioctl(fd, USCSICMD, &uscsi)) {
396  int err = errno;
397 
398  if (! ((EIO == err) && uscsi.uscsi_status))
399  return -err;
400  /* errno is set to EIO when a non-zero SCSI completion status given */
401  }
402 
403  iop->scsi_status = uscsi.uscsi_status;
404  iop->resid = uscsi.uscsi_resid;
405  iop->resp_sense_len = iop->max_sense_len - uscsi.uscsi_rqresid;
406 
407  if (report > 0) {
408  int trunc;
409  int len = iop->resp_sense_len;
410 
412  iop->sensep && (len > 3)) {
413  if ((iop->sensep[0] & 0x7f) > 0x71)
414  pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
415  iop->scsi_status, iop->sensep[1] & 0xf,
416  iop->sensep[2], iop->sensep[3]);
417  else
418  pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
419  iop->scsi_status, iop->sensep[2] & 0xf,
420  iop->sensep[12], iop->sensep[13]);
421  if (report > 1) {
422  pout(" >>> Sense buffer, len=%d:\n", len);
423  dStrHex((const char *)iop->sensep, ((len > 252) ? 252 : len) , 1);
424  }
425  } else if (iop->scsi_status)
426  pout(" status=%x\n", iop->scsi_status);
427  if (iop->resid)
428  pout(" dxfer_len=%d, resid=%d\n", iop->dxfer_len, iop->resid);
429  if (report > 1) {
430  len = iop->dxfer_len - iop->resid;
431  if (len > 0) {
432  trunc = (len > 256) ? 1 : 0;
433  pout(" Incoming data, len=%d%s:\n", len,
434  (trunc ? " [only first 256 bytes shown]" : ""));
435  dStrHex((char *)iop->dxferp, (trunc ? 256 : len) , 1);
436  }
437  }
438  }
439  return 0;
440 }
int printedout[2]
Definition: os_solaris.cpp:44
static int isdevtype(const char *dev_name, const char *table[], int tsize)
Definition: os_solaris.cpp:108
#define ATACMDS_H_CVSID
Definition: atacmds.h:28
#define SCSI_STATUS_CHECK_CONDITION
Definition: scsicmds.h:258
UINT8 * sensep
Definition: scsicmds.h:122
int smart_auto_save(int fd, int s)
int ata_identify(int fd, void *data)
#define ARGUSED(x)
Definition: os_solaris.cpp:38
#define DXFER_FROM_DEVICE
Definition: scsicmds.h:111
static const char * uscsidrvrs[]
Definition: os_solaris.cpp:95
const char * scsi_get_opcode_name(UINT8 opcode)
Definition: scsicmds.cpp:177
int smart_status_check(int fd)
unsigned timeout
Definition: scsicmds.h:125
static int isscsidev(const char *path)
Definition: os_solaris.cpp:131
int smart_read_thresholds(int fd, void *data)
const char * os_XXXX_c_cvsid
Definition: os_solaris.cpp:40
int guess_device_type(const char *dev_name)
Definition: os_solaris.cpp:143
#define snprintf
Definition: utility.h:68
void print_smartctl_examples()
Definition: os_solaris.cpp:72
size_t resp_sense_len
Definition: scsicmds.h:126
static int isatadev(const char *path)
Definition: os_solaris.cpp:137
#define INT64_H_CVSID
Definition: int64.h:23
#define SCSICMDS_H_CVSID
Definition: scsicmds.h:33
UINT8 * dxferp
Definition: scsicmds.h:120
int deviceclose(int fd)
Definition: os_solaris.cpp:265
UINT8 * cmnd
Definition: scsicmds.h:116
int smart_status(int fd)
ptr_t data
Definition: megaraid.h:94
int smart_auto_offline(int fd, int s)
int do_scsi_cmnd_io(int fd, struct scsi_cmnd_io *iop, int report)
Definition: os_solaris.cpp:346
int smart_read_log(int fd, int s, int count, void *data)
void dStrHex(const char *str, int len, int no_ascii)
Definition: scsicmds.cpp:87
char ** names
Definition: os_solaris.cpp:153
int ata_command_interface(int fd, smart_command_set command, int select, char *data)
Definition: os_solaris.cpp:283
static const char * atadrvrs[]
Definition: os_solaris.cpp:102
int smart_read_data(int fd, void *data)
void pout(const char *fmt,...)
Definition: smartctl.cpp:1133
size_t max_sense_len
Definition: scsicmds.h:124
int printwarning(int which)
Definition: os_solaris.cpp:50
int dxfer_dir
Definition: scsicmds.h:118
Definition: atacmds.h:55
#define DXFER_NONE
Definition: scsicmds.h:110
smart_command_set
Definition: atacmds.h:48
#define DXFER_TO_DEVICE
Definition: scsicmds.h:112
static int addpath(const char *path, struct pathlist *res)
Definition: os_solaris.cpp:159
char * unimplemented[2]
Definition: os_solaris.cpp:45
size_t cmnd_len
Definition: scsicmds.h:117
#define EXIT(status)
Definition: utility.h:100
#define OS_SOLARIS_H_CVSID
Definition: os_solaris.h:28
UINT8 scsi_status
Definition: scsicmds.h:127
int ata_pidentify(int fd, void *data)
int smart_disable(int fd)
size_t dxfer_len
Definition: scsicmds.h:121
Definition: atacmds.h:50
int smart_immediate_offline(int fd, int s)
static int grokdir(const char *dir, struct pathlist *res, int testfun(const char *))
Definition: os_solaris.cpp:173
int deviceopen(const char *pathname, char *type)
Definition: os_solaris.cpp:255
int smart_enable(int fd)
int make_device_names(char ***devlist, const char *name)
Definition: os_solaris.cpp:226
int maxnames
Definition: os_solaris.cpp:155
#define UTILITY_H_CVSID
Definition: utility.h:28