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