]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/scsi_id/scsi_id.c
use "Out of memory." consistantly (or with "\n")
[thirdparty/systemd.git] / src / udev / scsi_id / scsi_id.c
1 /*
2 * Copyright (C) IBM Corp. 2003
3 * Copyright (C) SUSE Linux Products GmbH, 2006
4 *
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.
9 *
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/>.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
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>
29 #include <getopt.h>
30 #include <sys/stat.h>
31
32 #include "libudev.h"
33 #include "libudev-private.h"
34 #include "scsi_id.h"
35
36 static const struct option options[] = {
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 {}
49 };
50
51 static const char short_options[] = "d:f:ghip:uvVx";
52 static const char dev_short_options[] = "bgp:";
53
54 static int all_good;
55 static int dev_specified;
56 static char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
57 static enum page_code default_page_code;
58 static int sg_version = 4;
59 static int use_stderr;
60 static int debug;
61 static int reformat_serial;
62 static int export;
63 static char vendor_str[64];
64 static char model_str[64];
65 static char vendor_enc_str[256];
66 static char model_enc_str[256];
67 static char revision_str[16];
68 static char type_str[16];
69
70 static void log_fn(struct udev *udev, int priority,
71 const char *file, int line, const char *fn,
72 const char *format, va_list args)
73 {
74 vsyslog(priority, format, args);
75 }
76
77 static void set_type(const char *from, char *to, size_t len)
78 {
79 int type_num;
80 char *eptr;
81 const char *type = "generic";
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 }
111 util_strscpy(to, len, type);
112 }
113
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 */
123 static char *get_value(char **buffer)
124 {
125 static const char *quote_string = "\"\n";
126 static const char *comma_string = ",\n";
127 char *val;
128 const char *end;
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;
150 }
151
152 static int argc_count(char *opts)
153 {
154 int i = 0;
155 while (*opts != '\0')
156 if (*opts++ == ' ')
157 i++;
158 return i;
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 */
170 static int get_file_options(struct udev *udev,
171 const char *vendor, const char *model,
172 int *argc, char ***newargv)
173 {
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
183 fd = fopen(config_file, "re");
184 if (fd == NULL) {
185 if (errno == ENOENT) {
186 return 1;
187 } else {
188 log_error("can't open %s: %s\n", config_file, strerror(errno));
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);
201 log_error("Out of memory.");
202 return -1;
203 }
204
205 *newargv = NULL;
206 lineno = 0;
207 while (1) {
208 vendor_in = model_in = options_in = NULL;
209
210 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
211 if (buf == NULL)
212 break;
213 lineno++;
214 if (buf[strlen(buffer) - 1] != '\n') {
215 log_error("Config file line %d too long\n", lineno);
216 break;
217 }
218
219 while (isspace(*buf))
220 buf++;
221
222 /* blank or all whitespace line */
223 if (*buf == '\0')
224 continue;
225
226 /* comment line */
227 if (*buf == '#')
228 continue;
229
230 str1 = strsep(&buf, "=");
231 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
232 str1 = get_value(&buf);
233 if (!str1) {
234 retval = -1;
235 break;
236 }
237 vendor_in = str1;
238
239 str1 = strsep(&buf, "=");
240 if (str1 && strcasecmp(str1, "MODEL") == 0) {
241 str1 = get_value(&buf);
242 if (!str1) {
243 retval = -1;
244 break;
245 }
246 model_in = str1;
247 str1 = strsep(&buf, "=");
248 }
249 }
250
251 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
252 str1 = get_value(&buf);
253 if (!str1) {
254 retval = -1;
255 break;
256 }
257 options_in = str1;
258 }
259
260 /*
261 * Only allow: [vendor=foo[,model=bar]]options=stuff
262 */
263 if (!options_in || (!vendor_in && model_in)) {
264 log_error("Error parsing config file line %d '%s'\n", lineno, buffer);
265 retval = -1;
266 break;
267 }
268 if (vendor == NULL) {
269 if (vendor_in == NULL)
270 break;
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 */
282 break;
283 }
284 }
285
286 if (retval == 0) {
287 if (vendor_in != NULL || model_in != NULL ||
288 options_in != NULL) {
289 /*
290 * Something matched. Allocate newargv, and store
291 * values found in options_in.
292 */
293 strcpy(buffer, options_in);
294 c = argc_count(buffer) + 2;
295 *newargv = calloc(c, sizeof(**newargv));
296 if (!*newargv) {
297 log_error("Out of memory.");
298 retval = -1;
299 } else {
300 *argc = c;
301 c = 0;
302 /*
303 * argv[0] at 0 is skipped by getopt, but
304 * store the buffer address there for
305 * later freeing
306 */
307 (*newargv)[c] = buffer;
308 for (c = 1; c < *argc; c++)
309 (*newargv)[c] = strsep(&buffer, " \t");
310 }
311 } else {
312 /* No matches */
313 retval = 1;
314 }
315 }
316 if (retval != 0)
317 free(buffer);
318 fclose(fd);
319 return retval;
320 }
321
322 static int set_options(struct udev *udev,
323 int argc, char **argv, const char *short_opts,
324 char *maj_min_dev)
325 {
326 int option;
327
328 /*
329 * optind is a global extern used by getopt. Since we can call
330 * set_options twice (once for command line, and once for config
331 * file) we have to reset this back to 1.
332 */
333 optind = 1;
334 while (1) {
335 option = getopt_long(argc, argv, short_opts, options, NULL);
336 if (option == -1)
337 break;
338
339 switch (option) {
340 case 'b':
341 all_good = 0;
342 break;
343
344 case 'd':
345 dev_specified = 1;
346 util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
347 break;
348
349 case 'e':
350 use_stderr = 1;
351 break;
352
353 case 'f':
354 util_strscpy(config_file, MAX_PATH_LEN, optarg);
355 break;
356
357 case 'g':
358 all_good = 1;
359 break;
360
361 case 'h':
362 printf("Usage: scsi_id OPTIONS <device>\n"
363 " --device= device node for SG_IO commands\n"
364 " --config= location of config file\n"
365 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
366 " --sg-version=3|4 use SGv3 or SGv4\n"
367 " --blacklisted threat device as blacklisted\n"
368 " --whitelisted threat device as whitelisted\n"
369 " --replace-whitespace replace all whitespaces by underscores\n"
370 " --verbose verbose logging\n"
371 " --version print version\n"
372 " --export print values as environment keys\n"
373 " --help print this help text\n\n");
374 exit(0);
375
376 case 'p':
377 if (strcmp(optarg, "0x80") == 0) {
378 default_page_code = PAGE_80;
379 } else if (strcmp(optarg, "0x83") == 0) {
380 default_page_code = PAGE_83;
381 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
382 default_page_code = PAGE_83_PRE_SPC3;
383 } else {
384 log_error("Unknown page code '%s'\n", optarg);
385 return -1;
386 }
387 break;
388
389 case 's':
390 sg_version = atoi(optarg);
391 if (sg_version < 3 || sg_version > 4) {
392 log_error("Unknown SG version '%s'\n", optarg);
393 return -1;
394 }
395 break;
396
397 case 'u':
398 reformat_serial = 1;
399 break;
400
401 case 'x':
402 export = 1;
403 break;
404
405 case 'v':
406 debug++;
407 break;
408
409 case 'V':
410 printf("%s\n", VERSION);
411 exit(0);
412 break;
413
414 default:
415 exit(1);
416 }
417 }
418 if (optind < argc && !dev_specified) {
419 dev_specified = 1;
420 util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
421 }
422 return 0;
423 }
424
425 static int per_dev_options(struct udev *udev,
426 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
427 {
428 int retval;
429 int newargc;
430 char **newargv = NULL;
431 int option;
432
433 *good_bad = all_good;
434 *page_code = default_page_code;
435
436 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
437
438 optind = 1; /* reset this global extern */
439 while (retval == 0) {
440 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
441 if (option == -1)
442 break;
443
444 switch (option) {
445 case 'b':
446 *good_bad = 0;
447 break;
448
449 case 'g':
450 *good_bad = 1;
451 break;
452
453 case 'p':
454 if (strcmp(optarg, "0x80") == 0) {
455 *page_code = PAGE_80;
456 } else if (strcmp(optarg, "0x83") == 0) {
457 *page_code = PAGE_83;
458 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
459 *page_code = PAGE_83_PRE_SPC3;
460 } else {
461 log_error("Unknown page code '%s'\n", optarg);
462 retval = -1;
463 }
464 break;
465
466 default:
467 log_error("Unknown or bad option '%c' (0x%x)\n", option, option);
468 retval = -1;
469 break;
470 }
471 }
472
473 if (newargv) {
474 free(newargv[0]);
475 free(newargv);
476 }
477 return retval;
478 }
479
480 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
481 {
482 int retval;
483
484 dev_scsi->use_sg = sg_version;
485
486 retval = scsi_std_inquiry(udev, dev_scsi, path);
487 if (retval)
488 return retval;
489
490 udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
491 udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
492
493 util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
494 util_replace_chars(vendor_str, NULL);
495 util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
496 util_replace_chars(model_str, NULL);
497 set_type(dev_scsi->type, type_str, sizeof(type_str));
498 util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
499 util_replace_chars(revision_str, NULL);
500 return 0;
501 }
502
503 /*
504 * scsi_id: try to get an id, if one is found, printf it to stdout.
505 * returns a value passed to exit() - 0 if printed an id, else 1.
506 */
507 static int scsi_id(struct udev *udev, char *maj_min_dev)
508 {
509 struct scsi_id_device dev_scsi;
510 int good_dev;
511 int page_code;
512 int retval = 0;
513
514 memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
515
516 if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
517 retval = 1;
518 goto out;
519 }
520
521 /* get per device (vendor + model) options from the config file */
522 per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
523 if (!good_dev) {
524 retval = 1;
525 goto out;
526 }
527
528 /* read serial number from mode pages (no values for optical drives) */
529 scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
530
531 if (export) {
532 char serial_str[MAX_SERIAL_LEN];
533
534 printf("ID_SCSI=1\n");
535 printf("ID_VENDOR=%s\n", vendor_str);
536 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
537 printf("ID_MODEL=%s\n", model_str);
538 printf("ID_MODEL_ENC=%s\n", model_enc_str);
539 printf("ID_REVISION=%s\n", revision_str);
540 printf("ID_TYPE=%s\n", type_str);
541 if (dev_scsi.serial[0] != '\0') {
542 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
543 util_replace_chars(serial_str, NULL);
544 printf("ID_SERIAL=%s\n", serial_str);
545 util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
546 util_replace_chars(serial_str, NULL);
547 printf("ID_SERIAL_SHORT=%s\n", serial_str);
548 }
549 if (dev_scsi.wwn[0] != '\0') {
550 printf("ID_WWN=0x%s\n", dev_scsi.wwn);
551 if (dev_scsi.wwn_vendor_extension[0] != '\0') {
552 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
553 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
554 } else {
555 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
556 }
557 }
558 if (dev_scsi.tgpt_group[0] != '\0') {
559 printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
560 }
561 if (dev_scsi.unit_serial_number[0] != '\0') {
562 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
563 }
564 goto out;
565 }
566
567 if (dev_scsi.serial[0] == '\0') {
568 retval = 1;
569 goto out;
570 }
571
572 if (reformat_serial) {
573 char serial_str[MAX_SERIAL_LEN];
574
575 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
576 util_replace_chars(serial_str, NULL);
577 printf("%s\n", serial_str);
578 goto out;
579 }
580
581 printf("%s\n", dev_scsi.serial);
582 out:
583 return retval;
584 }
585
586 int main(int argc, char **argv)
587 {
588 struct udev *udev;
589 int retval = 0;
590 char maj_min_dev[MAX_PATH_LEN];
591 int newargc;
592 char **newargv;
593
594 udev = udev_new();
595 if (udev == NULL)
596 goto exit;
597
598 log_open();
599 udev_set_log_fn(udev, log_fn);
600
601 /*
602 * Get config file options.
603 */
604 newargv = NULL;
605 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
606 if (retval < 0) {
607 retval = 1;
608 goto exit;
609 }
610 if (newargv && (retval == 0)) {
611 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
612 retval = 2;
613 goto exit;
614 }
615 free(newargv);
616 }
617
618 /*
619 * Get command line options (overriding any config file settings).
620 */
621 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
622 exit(1);
623
624 if (!dev_specified) {
625 log_error("no device specified\n");
626 retval = 1;
627 goto exit;
628 }
629
630 retval = scsi_id(udev, maj_min_dev);
631
632 exit:
633 udev_unref(udev);
634 log_close();
635 return retval;
636 }