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