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