]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/scsi_id/scsi_id.c
tree-wide usage of %m specifier instead of strerror(errno)
[thirdparty/systemd.git] / src / udev / scsi_id / scsi_id.c
CommitLineData
e0f83fbe 1/*
c521693b 2 * Copyright (C) IBM Corp. 2003
3d94fb87 3 * Copyright (C) SUSE Linux Products GmbH, 2006
c521693b 4 *
8b5651bb
KS
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
c521693b 9 *
8b5651bb
KS
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c521693b
GKH
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
c521693b
GKH
21#include <unistd.h>
22#include <signal.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <string.h>
26#include <syslog.h>
27#include <stdarg.h>
28#include <ctype.h>
9eaa50d0 29#include <getopt.h>
c521693b 30#include <sys/stat.h>
c521693b 31
9060b066
KS
32#include "libudev.h"
33#include "libudev-private.h"
1aa1e248 34#include "scsi_id.h"
c521693b 35
9eaa50d0 36static const struct option options[] = {
912541b0
KS
37 { "device", required_argument, NULL, 'd' },
38 { "config", required_argument, NULL, 'f' },
39 { "page", required_argument, NULL, 'p' },
40 { "blacklisted", no_argument, NULL, 'b' },
41 { "whitelisted", no_argument, NULL, 'g' },
42 { "replace-whitespace", no_argument, NULL, 'u' },
43 { "sg-version", required_argument, NULL, 's' },
44 { "verbose", no_argument, NULL, 'v' },
45 { "version", no_argument, NULL, 'V' },
46 { "export", no_argument, NULL, 'x' },
47 { "help", no_argument, NULL, 'h' },
48 {}
9eaa50d0
KS
49};
50
753417db 51static const char short_options[] = "d:f:ghip:uvVx";
b484e436 52static const char dev_short_options[] = "bgp:";
c521693b 53
c521693b 54static int all_good;
c521693b 55static int dev_specified;
ff944daa 56static char config_file[MAX_PATH_LEN] = "/etc/scsi_id.config";
50be1401 57static enum page_code default_page_code;
025570aa 58static int sg_version = 4;
c521693b
GKH
59static int use_stderr;
60static int debug;
01f950e2 61static int reformat_serial;
34129109
KS
62static int export;
63static char vendor_str[64];
64static char model_str[64];
ad88f940
DZ
65static char vendor_enc_str[256];
66static char model_enc_str[256];
aaff09a3
KS
67static char revision_str[16];
68static char type_str[16];
c521693b 69
7d563a17 70static void log_fn(struct udev *udev, int priority,
912541b0
KS
71 const char *file, int line, const char *fn,
72 const char *format, va_list args)
c521693b 73{
912541b0 74 vsyslog(priority, format, args);
c521693b
GKH
75}
76
92f43136 77static void set_type(const char *from, char *to, size_t len)
aaff09a3 78{
912541b0
KS
79 int type_num;
80 char *eptr;
4b0060e6 81 const char *type = "generic";
912541b0
KS
82
83 type_num = strtoul(from, &eptr, 0);
84 if (eptr != from) {
85 switch (type_num) {
86 case 0:
87 type = "disk";
88 break;
89 case 1:
90 type = "tape";
91 break;
92 case 4:
93 type = "optical";
94 break;
95 case 5:
96 type = "cd";
97 break;
98 case 7:
99 type = "optical";
100 break;
101 case 0xe:
102 type = "disk";
103 break;
104 case 0xf:
105 type = "optical";
106 break;
107 default:
108 break;
109 }
110 }
d5a89d7d 111 strscpy(to, len, type);
34129109
KS
112}
113
c521693b
GKH
114/*
115 * get_value:
116 *
117 * buf points to an '=' followed by a quoted string ("foo") or a string ending
118 * with a space or ','.
119 *
120 * Return a pointer to the NUL terminated string, returns NULL if no
121 * matches.
122 */
123static char *get_value(char **buffer)
124{
4b0060e6
KS
125 static const char *quote_string = "\"\n";
126 static const char *comma_string = ",\n";
912541b0 127 char *val;
4b0060e6 128 const char *end;
912541b0
KS
129
130 if (**buffer == '"') {
131 /*
132 * skip leading quote, terminate when quote seen
133 */
134 (*buffer)++;
135 end = quote_string;
136 } else {
137 end = comma_string;
138 }
139 val = strsep(buffer, end);
140 if (val && end == quote_string)
141 /*
142 * skip trailing quote
143 */
144 (*buffer)++;
145
146 while (isspace(**buffer))
147 (*buffer)++;
148
149 return val;
c521693b
GKH
150}
151
152static int argc_count(char *opts)
153{
912541b0
KS
154 int i = 0;
155 while (*opts != '\0')
156 if (*opts++ == ' ')
157 i++;
158 return i;
c521693b
GKH
159}
160
161/*
162 * get_file_options:
163 *
164 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
165 * if vendor and model are set find the first OPTIONS line in the config
166 * file that matches. Set argc and argv to match the OPTIONS string.
167 *
168 * vendor and model can end in '\n'.
169 */
7d563a17 170static int get_file_options(struct udev *udev,
912541b0
KS
171 const char *vendor, const char *model,
172 int *argc, char ***newargv)
c521693b 173{
912541b0
KS
174 char *buffer;
175 FILE *fd;
176 char *buf;
177 char *str1;
178 char *vendor_in, *model_in, *options_in; /* read in from file */
179 int lineno;
180 int c;
181 int retval = 0;
182
47ef94ac 183 fd = fopen(config_file, "re");
912541b0 184 if (fd == NULL) {
912541b0
KS
185 if (errno == ENOENT) {
186 return 1;
187 } else {
f5f6d0e2 188 log_error("can't open %s: %m\n", config_file);
912541b0
KS
189 return -1;
190 }
191 }
192
193 /*
194 * Allocate a buffer rather than put it on the stack so we can
195 * keep it around to parse any options (any allocated newargv
196 * points into this buffer for its strings).
197 */
198 buffer = malloc(MAX_BUFFER_LEN);
199 if (!buffer) {
200 fclose(fd);
0d0f0c50 201 return log_oom();
912541b0
KS
202 }
203
204 *newargv = NULL;
205 lineno = 0;
206 while (1) {
207 vendor_in = model_in = options_in = NULL;
208
209 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
210 if (buf == NULL)
211 break;
212 lineno++;
213 if (buf[strlen(buffer) - 1] != '\n') {
baa30fbc 214 log_error("Config file line %d too long\n", lineno);
912541b0
KS
215 break;
216 }
217
218 while (isspace(*buf))
219 buf++;
220
221 /* blank or all whitespace line */
222 if (*buf == '\0')
223 continue;
224
225 /* comment line */
226 if (*buf == '#')
227 continue;
228
912541b0 229 str1 = strsep(&buf, "=");
b43d1d01 230 if (str1 && strcaseeq(str1, "VENDOR")) {
912541b0
KS
231 str1 = get_value(&buf);
232 if (!str1) {
0d0f0c50 233 retval = log_oom();
912541b0
KS
234 break;
235 }
236 vendor_in = str1;
237
238 str1 = strsep(&buf, "=");
b43d1d01 239 if (str1 && strcaseeq(str1, "MODEL")) {
912541b0
KS
240 str1 = get_value(&buf);
241 if (!str1) {
0d0f0c50 242 retval = log_oom();
912541b0
KS
243 break;
244 }
245 model_in = str1;
246 str1 = strsep(&buf, "=");
247 }
248 }
249
b43d1d01 250 if (str1 && strcaseeq(str1, "OPTIONS")) {
912541b0
KS
251 str1 = get_value(&buf);
252 if (!str1) {
0d0f0c50 253 retval = log_oom();
912541b0
KS
254 break;
255 }
256 options_in = str1;
257 }
baa30fbc 258
912541b0
KS
259 /*
260 * Only allow: [vendor=foo[,model=bar]]options=stuff
261 */
262 if (!options_in || (!vendor_in && model_in)) {
baa30fbc 263 log_error("Error parsing config file line %d '%s'\n", lineno, buffer);
912541b0
KS
264 retval = -1;
265 break;
266 }
267 if (vendor == NULL) {
baa30fbc 268 if (vendor_in == NULL)
912541b0 269 break;
641906e9
TA
270 } else if ((vendor_in && strneq(vendor, vendor_in,
271 strlen(vendor_in))) &&
272 (!model_in || (strneq(model, model_in,
273 strlen(model_in))))) {
912541b0
KS
274 /*
275 * Matched vendor and optionally model.
276 *
277 * Note: a short vendor_in or model_in can
278 * give a partial match (that is FOO
279 * matches FOOBAR).
280 */
912541b0 281 break;
912541b0
KS
282 }
283 }
284
285 if (retval == 0) {
286 if (vendor_in != NULL || model_in != NULL ||
287 options_in != NULL) {
288 /*
289 * Something matched. Allocate newargv, and store
290 * values found in options_in.
291 */
292 strcpy(buffer, options_in);
293 c = argc_count(buffer) + 2;
294 *newargv = calloc(c, sizeof(**newargv));
295 if (!*newargv) {
0d0f0c50 296 retval = log_oom();
912541b0
KS
297 } else {
298 *argc = c;
299 c = 0;
300 /*
301 * argv[0] at 0 is skipped by getopt, but
302 * store the buffer address there for
303 * later freeing
304 */
305 (*newargv)[c] = buffer;
306 for (c = 1; c < *argc; c++)
307 (*newargv)[c] = strsep(&buffer, " \t");
308 }
309 } else {
310 /* No matches */
311 retval = 1;
312 }
313 }
314 if (retval != 0)
315 free(buffer);
316 fclose(fd);
317 return retval;
c521693b
GKH
318}
319
7d563a17 320static int set_options(struct udev *udev,
912541b0
KS
321 int argc, char **argv, const char *short_opts,
322 char *maj_min_dev)
c521693b 323{
912541b0
KS
324 int option;
325
326 /*
327 * optind is a global extern used by getopt. Since we can call
328 * set_options twice (once for command line, and once for config
329 * file) we have to reset this back to 1.
330 */
331 optind = 1;
332 while (1) {
333 option = getopt_long(argc, argv, short_opts, options, NULL);
334 if (option == -1)
335 break;
336
912541b0
KS
337 switch (option) {
338 case 'b':
339 all_good = 0;
340 break;
341
342 case 'd':
343 dev_specified = 1;
d5a89d7d 344 strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
912541b0
KS
345 break;
346
347 case 'e':
348 use_stderr = 1;
349 break;
350
351 case 'f':
d5a89d7d 352 strscpy(config_file, MAX_PATH_LEN, optarg);
912541b0
KS
353 break;
354
355 case 'g':
356 all_good = 1;
357 break;
358
359 case 'h':
360 printf("Usage: scsi_id OPTIONS <device>\n"
361 " --device= device node for SG_IO commands\n"
362 " --config= location of config file\n"
363 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
364 " --sg-version=3|4 use SGv3 or SGv4\n"
365 " --blacklisted threat device as blacklisted\n"
366 " --whitelisted threat device as whitelisted\n"
367 " --replace-whitespace replace all whitespaces by underscores\n"
368 " --verbose verbose logging\n"
369 " --version print version\n"
370 " --export print values as environment keys\n"
371 " --help print this help text\n\n");
372 exit(0);
373
374 case 'p':
090be865 375 if (streq(optarg, "0x80")) {
912541b0 376 default_page_code = PAGE_80;
090be865 377 } else if (streq(optarg, "0x83")) {
912541b0 378 default_page_code = PAGE_83;
090be865 379 } else if (streq(optarg, "pre-spc3-83")) {
912541b0
KS
380 default_page_code = PAGE_83_PRE_SPC3;
381 } else {
baa30fbc 382 log_error("Unknown page code '%s'\n", optarg);
912541b0
KS
383 return -1;
384 }
385 break;
386
387 case 's':
388 sg_version = atoi(optarg);
389 if (sg_version < 3 || sg_version > 4) {
baa30fbc 390 log_error("Unknown SG version '%s'\n", optarg);
912541b0
KS
391 return -1;
392 }
393 break;
394
395 case 'u':
396 reformat_serial = 1;
397 break;
398
399 case 'x':
400 export = 1;
401 break;
402
403 case 'v':
404 debug++;
405 break;
406
407 case 'V':
408 printf("%s\n", VERSION);
409 exit(0);
410 break;
411
412 default:
413 exit(1);
414 }
415 }
416 if (optind < argc && !dev_specified) {
417 dev_specified = 1;
d5a89d7d 418 strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
912541b0
KS
419 }
420 return 0;
c521693b
GKH
421}
422
7d563a17 423static int per_dev_options(struct udev *udev,
912541b0 424 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
c521693b 425{
912541b0
KS
426 int retval;
427 int newargc;
428 char **newargv = NULL;
429 int option;
430
431 *good_bad = all_good;
432 *page_code = default_page_code;
433
434 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
435
436 optind = 1; /* reset this global extern */
437 while (retval == 0) {
438 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
439 if (option == -1)
440 break;
441
912541b0
KS
442 switch (option) {
443 case 'b':
444 *good_bad = 0;
445 break;
446
447 case 'g':
448 *good_bad = 1;
449 break;
450
451 case 'p':
090be865 452 if (streq(optarg, "0x80")) {
912541b0 453 *page_code = PAGE_80;
090be865 454 } else if (streq(optarg, "0x83")) {
912541b0 455 *page_code = PAGE_83;
090be865 456 } else if (streq(optarg, "pre-spc3-83")) {
912541b0
KS
457 *page_code = PAGE_83_PRE_SPC3;
458 } else {
baa30fbc 459 log_error("Unknown page code '%s'\n", optarg);
912541b0
KS
460 retval = -1;
461 }
462 break;
463
464 default:
baa30fbc 465 log_error("Unknown or bad option '%c' (0x%x)\n", option, option);
912541b0
KS
466 retval = -1;
467 break;
468 }
469 }
470
471 if (newargv) {
472 free(newargv[0]);
473 free(newargv);
474 }
475 return retval;
c521693b
GKH
476}
477
7d563a17 478static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
87cf9f5a 479{
912541b0 480 int retval;
87cf9f5a 481
912541b0 482 dev_scsi->use_sg = sg_version;
78d9ecfd 483
912541b0
KS
484 retval = scsi_std_inquiry(udev, dev_scsi, path);
485 if (retval)
486 return retval;
87cf9f5a 487
912541b0
KS
488 udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
489 udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
ad88f940 490
912541b0
KS
491 util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
492 util_replace_chars(vendor_str, NULL);
493 util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
494 util_replace_chars(model_str, NULL);
495 set_type(dev_scsi->type, type_str, sizeof(type_str));
496 util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
497 util_replace_chars(revision_str, NULL);
498 return 0;
87cf9f5a
HR
499}
500
c521693b
GKH
501/*
502 * scsi_id: try to get an id, if one is found, printf it to stdout.
caf4c97a 503 * returns a value passed to exit() - 0 if printed an id, else 1.
c521693b 504 */
7d563a17 505static int scsi_id(struct udev *udev, char *maj_min_dev)
c521693b 506{
912541b0
KS
507 struct scsi_id_device dev_scsi;
508 int good_dev;
509 int page_code;
510 int retval = 0;
511
512 memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
513
514 if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
515 retval = 1;
516 goto out;
517 }
518
519 /* get per device (vendor + model) options from the config file */
520 per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
912541b0
KS
521 if (!good_dev) {
522 retval = 1;
523 goto out;
524 }
525
526 /* read serial number from mode pages (no values for optical drives) */
527 scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
528
529 if (export) {
530 char serial_str[MAX_SERIAL_LEN];
531
532 printf("ID_SCSI=1\n");
533 printf("ID_VENDOR=%s\n", vendor_str);
534 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
535 printf("ID_MODEL=%s\n", model_str);
536 printf("ID_MODEL_ENC=%s\n", model_enc_str);
537 printf("ID_REVISION=%s\n", revision_str);
538 printf("ID_TYPE=%s\n", type_str);
539 if (dev_scsi.serial[0] != '\0') {
540 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
541 util_replace_chars(serial_str, NULL);
542 printf("ID_SERIAL=%s\n", serial_str);
543 util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
544 util_replace_chars(serial_str, NULL);
545 printf("ID_SERIAL_SHORT=%s\n", serial_str);
546 }
547 if (dev_scsi.wwn[0] != '\0') {
548 printf("ID_WWN=0x%s\n", dev_scsi.wwn);
549 if (dev_scsi.wwn_vendor_extension[0] != '\0') {
550 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
551 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
552 } else {
553 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
554 }
555 }
556 if (dev_scsi.tgpt_group[0] != '\0') {
557 printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
558 }
559 if (dev_scsi.unit_serial_number[0] != '\0') {
560 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
561 }
562 goto out;
563 }
564
565 if (dev_scsi.serial[0] == '\0') {
566 retval = 1;
567 goto out;
568 }
569
570 if (reformat_serial) {
571 char serial_str[MAX_SERIAL_LEN];
572
573 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
574 util_replace_chars(serial_str, NULL);
575 printf("%s\n", serial_str);
576 goto out;
577 }
578
579 printf("%s\n", dev_scsi.serial);
caf4c97a 580out:
912541b0 581 return retval;
c521693b
GKH
582}
583
584int main(int argc, char **argv)
585{
912541b0
KS
586 struct udev *udev;
587 int retval = 0;
588 char maj_min_dev[MAX_PATH_LEN];
589 int newargc;
590 char **newargv;
591
592 udev = udev_new();
593 if (udev == NULL)
594 goto exit;
595
baa30fbc 596 log_open();
912541b0
KS
597 udev_set_log_fn(udev, log_fn);
598
599 /*
600 * Get config file options.
601 */
602 newargv = NULL;
603 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
604 if (retval < 0) {
605 retval = 1;
606 goto exit;
607 }
608 if (newargv && (retval == 0)) {
609 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
610 retval = 2;
611 goto exit;
612 }
613 free(newargv);
614 }
615
616 /*
617 * Get command line options (overriding any config file settings).
618 */
619 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
620 exit(1);
621
622 if (!dev_specified) {
baa30fbc 623 log_error("no device specified\n");
912541b0
KS
624 retval = 1;
625 goto exit;
626 }
627
628 retval = scsi_id(udev, maj_min_dev);
1aa1e248
KS
629
630exit:
912541b0 631 udev_unref(udev);
baa30fbc 632 log_close();
912541b0 633 return retval;
c521693b 634}