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