chiark / gitweb /
Only build v4l_id if V4L1 header file is available
[elogind.git] / extras / 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         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 char *quote_string = "\"\n";
126         static char *comma_string = ",\n";
127         char *val;
128         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         dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
184         fd = fopen(config_file, "r");
185         if (fd == NULL) {
186                 dbg(udev, "can't open %s\n", config_file);
187                 if (errno == ENOENT) {
188                         return 1;
189                 } else {
190                         err(udev, "can't open %s: %s\n", config_file, strerror(errno));
191                         return -1;
192                 }
193         }
194
195         /*
196          * Allocate a buffer rather than put it on the stack so we can
197          * keep it around to parse any options (any allocated newargv
198          * points into this buffer for its strings).
199          */
200         buffer = malloc(MAX_BUFFER_LEN);
201         if (!buffer) {
202                 err(udev, "can't allocate memory\n");
203                 return -1;
204         }
205
206         *newargv = NULL;
207         lineno = 0;
208         while (1) {
209                 vendor_in = model_in = options_in = NULL;
210
211                 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
212                 if (buf == NULL)
213                         break;
214                 lineno++;
215                 if (buf[strlen(buffer) - 1] != '\n') {
216                         err(udev, "Config file line %d too long\n", lineno);
217                         break;
218                 }
219
220                 while (isspace(*buf))
221                         buf++;
222
223                 /* blank or all whitespace line */
224                 if (*buf == '\0')
225                         continue;
226
227                 /* comment line */
228                 if (*buf == '#')
229                         continue;
230
231                 dbg(udev, "lineno %d: '%s'\n", lineno, buf);
232                 str1 = strsep(&buf, "=");
233                 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
234                         str1 = get_value(&buf);
235                         if (!str1) {
236                                 retval = -1;
237                                 break;
238                         }
239                         vendor_in = str1;
240
241                         str1 = strsep(&buf, "=");
242                         if (str1 && strcasecmp(str1, "MODEL") == 0) {
243                                 str1 = get_value(&buf);
244                                 if (!str1) {
245                                         retval = -1;
246                                         break;
247                                 }
248                                 model_in = str1;
249                                 str1 = strsep(&buf, "=");
250                         }
251                 }
252
253                 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
254                         str1 = get_value(&buf);
255                         if (!str1) {
256                                 retval = -1;
257                                 break;
258                         }
259                         options_in = str1;
260                 }
261                 dbg(udev, "config file line %d:\n"
262                         " vendor '%s'; model '%s'; options '%s'\n",
263                         lineno, vendor_in, model_in, options_in);
264                 /*
265                  * Only allow: [vendor=foo[,model=bar]]options=stuff
266                  */
267                 if (!options_in || (!vendor_in && model_in)) {
268                         err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
269                         retval = -1;
270                         break;
271                 }
272                 if (vendor == NULL) {
273                         if (vendor_in == NULL) {
274                                 dbg(udev, "matched global option\n");
275                                 break;
276                         }
277                 } else if ((vendor_in && strncmp(vendor, vendor_in,
278                                                  strlen(vendor_in)) == 0) &&
279                            (!model_in || (strncmp(model, model_in,
280                                                   strlen(model_in)) == 0))) {
281                                 /*
282                                  * Matched vendor and optionally model.
283                                  *
284                                  * Note: a short vendor_in or model_in can
285                                  * give a partial match (that is FOO
286                                  * matches FOOBAR).
287                                  */
288                                 dbg(udev, "matched vendor/model\n");
289                                 break;
290                 } else {
291                         dbg(udev, "no match\n");
292                 }
293         }
294
295         if (retval == 0) {
296                 if (vendor_in != NULL || model_in != NULL ||
297                     options_in != NULL) {
298                         /*
299                          * Something matched. Allocate newargv, and store
300                          * values found in options_in.
301                          */
302                         strcpy(buffer, options_in);
303                         c = argc_count(buffer) + 2;
304                         *newargv = calloc(c, sizeof(**newargv));
305                         if (!*newargv) {
306                                 err(udev, "can't allocate memory\n");
307                                 retval = -1;
308                         } else {
309                                 *argc = c;
310                                 c = 0;
311                                 /*
312                                  * argv[0] at 0 is skipped by getopt, but
313                                  * store the buffer address there for
314                                  * later freeing
315                                  */
316                                 (*newargv)[c] = buffer;
317                                 for (c = 1; c < *argc; c++)
318                                         (*newargv)[c] = strsep(&buffer, " \t");
319                         }
320                 } else {
321                         /* No matches  */
322                         retval = 1;
323                 }
324         }
325         if (retval != 0)
326                 free(buffer);
327         fclose(fd);
328         return retval;
329 }
330
331 static int set_options(struct udev *udev,
332                        int argc, char **argv, const char *short_opts,
333                        char *maj_min_dev)
334 {
335         int option;
336
337         /*
338          * optind is a global extern used by getopt. Since we can call
339          * set_options twice (once for command line, and once for config
340          * file) we have to reset this back to 1.
341          */
342         optind = 1;
343         while (1) {
344                 option = getopt_long(argc, argv, short_opts, options, NULL);
345                 if (option == -1)
346                         break;
347
348                 if (optarg)
349                         dbg(udev, "option '%c' arg '%s'\n", option, optarg);
350                 else
351                         dbg(udev, "option '%c'\n", option);
352
353                 switch (option) {
354                 case 'b':
355                         all_good = 0;
356                         break;
357
358                 case 'd':
359                         dev_specified = 1;
360                         util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
361                         break;
362
363                 case 'e':
364                         use_stderr = 1;
365                         break;
366
367                 case 'f':
368                         util_strscpy(config_file, MAX_PATH_LEN, optarg);
369                         break;
370
371                 case 'g':
372                         all_good = 1;
373                         break;
374
375                 case 'h':
376                         printf("Usage: scsi_id OPTIONS <device>\n"
377                                "  --device=                     device node for SG_IO commands\n"
378                                "  --config=                     location of config file\n"
379                                "  --page=0x80|0x83|pre-spc3-83  SCSI page (0x80, 0x83, pre-spc3-83)\n"
380                                "  --sg-version=3|4              use SGv3 or SGv4\n"
381                                "  --blacklisted                 threat device as blacklisted\n"
382                                "  --whitelisted                 threat device as whitelisted\n"
383                                "  --replace-whitespace          replace all whitespaces by underscores\n"
384                                "  --verbose                     verbose logging\n"
385                                "  --version                     print version\n"
386                                "  --export                      print values as environment keys\n"
387                                "  --help                        print this help text\n\n");
388                         exit(0);
389
390                 case 'p':
391                         if (strcmp(optarg, "0x80") == 0) {
392                                 default_page_code = PAGE_80;
393                         } else if (strcmp(optarg, "0x83") == 0) {
394                                 default_page_code = PAGE_83;
395                         } else if (strcmp(optarg, "pre-spc3-83") == 0) {
396                                 default_page_code = PAGE_83_PRE_SPC3; 
397                         } else {
398                                 err(udev, "Unknown page code '%s'\n", optarg);
399                                 return -1;
400                         }
401                         break;
402
403                 case 's':
404                         sg_version = atoi(optarg);
405                         if (sg_version < 3 || sg_version > 4) {
406                                 err(udev, "Unknown SG version '%s'\n", optarg);
407                                 return -1;
408                         }
409                         break;
410
411                 case 'u':
412                         reformat_serial = 1;
413                         break;
414
415                 case 'x':
416                         export = 1;
417                         break;
418
419                 case 'v':
420                         debug++;
421                         break;
422
423                 case 'V':
424                         printf("%s\n", VERSION);
425                         exit(0);
426                         break;
427
428                 default:
429                         exit(1);
430                 }
431         }
432         if (optind < argc && !dev_specified) {
433                 dev_specified = 1;
434                 util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
435         }
436         return 0;
437 }
438
439 static int per_dev_options(struct udev *udev,
440                            struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
441 {
442         int retval;
443         int newargc;
444         char **newargv = NULL;
445         int option;
446
447         *good_bad = all_good;
448         *page_code = default_page_code;
449
450         retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
451
452         optind = 1; /* reset this global extern */
453         while (retval == 0) {
454                 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
455                 if (option == -1)
456                         break;
457
458                 if (optarg)
459                         dbg(udev, "option '%c' arg '%s'\n", option, optarg);
460                 else
461                         dbg(udev, "option '%c'\n", option);
462
463                 switch (option) {
464                 case 'b':
465                         *good_bad = 0;
466                         break;
467
468                 case 'g':
469                         *good_bad = 1;
470                         break;
471
472                 case 'p':
473                         if (strcmp(optarg, "0x80") == 0) {
474                                 *page_code = PAGE_80;
475                         } else if (strcmp(optarg, "0x83") == 0) {
476                                 *page_code = PAGE_83;
477                         } else if (strcmp(optarg, "pre-spc3-83") == 0) {
478                                 *page_code = PAGE_83_PRE_SPC3; 
479                         } else {
480                                 err(udev, "Unknown page code '%s'\n", optarg);
481                                 retval = -1;
482                         }
483                         break;
484
485                 default:
486                         err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
487                         retval = -1;
488                         break;
489                 }
490         }
491
492         if (newargv) {
493                 free(newargv[0]);
494                 free(newargv);
495         }
496         return retval;
497 }
498
499 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
500 {
501         int retval;
502
503         dev_scsi->use_sg = sg_version;
504
505         retval = scsi_std_inquiry(udev, dev_scsi, path);
506         if (retval)
507                 return retval;
508
509         udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
510         udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
511
512         udev_util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
513         udev_util_replace_chars(vendor_str, NULL);
514         udev_util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
515         udev_util_replace_chars(model_str, NULL);
516         set_type(dev_scsi->type, type_str, sizeof(type_str));
517         udev_util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
518         udev_util_replace_chars(revision_str, NULL);
519         return 0;
520 }
521
522 /*
523  * scsi_id: try to get an id, if one is found, printf it to stdout.
524  * returns a value passed to exit() - 0 if printed an id, else 1.
525  */
526 static int scsi_id(struct udev *udev, char *maj_min_dev)
527 {
528         struct scsi_id_device dev_scsi;
529         int good_dev;
530         int page_code;
531         int retval = 0;
532
533         memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
534
535         if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
536                 retval = 1;
537                 goto out;
538         }
539
540         /* get per device (vendor + model) options from the config file */
541         per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
542         dbg(udev, "per dev options: good %d; page code 0x%x\n", good_dev, page_code);
543         if (!good_dev) {
544                 retval = 1;
545                 goto out;
546         }
547
548         /* read serial number from mode pages (no values for optical drives) */
549         scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
550
551         if (export) {
552                 char serial_str[MAX_SERIAL_LEN];
553
554                 printf("ID_SCSI=1\n");
555                 printf("ID_VENDOR=%s\n", vendor_str);
556                 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
557                 printf("ID_MODEL=%s\n", model_str);
558                 printf("ID_MODEL_ENC=%s\n", model_enc_str);
559                 printf("ID_REVISION=%s\n", revision_str);
560                 printf("ID_TYPE=%s\n", type_str);
561                 if (dev_scsi.serial[0] != '\0') {
562                         udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
563                         udev_util_replace_chars(serial_str, NULL);
564                         printf("ID_SERIAL=%s\n", serial_str);
565                         udev_util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
566                         udev_util_replace_chars(serial_str, NULL);
567                         printf("ID_SERIAL_SHORT=%s\n", serial_str);
568                 }
569                 if (dev_scsi.wwn[0] != '\0') {
570                         printf("ID_WWN=0x%s\n", dev_scsi.wwn);
571                         if (dev_scsi.wwn_vendor_extension[0] != '\0') {
572                                 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
573                                 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
574                         } else {
575                                 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
576                         }
577                 }
578                 if (dev_scsi.tgpt_group[0] != '\0') {
579                         printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
580                 }
581                 if (dev_scsi.unit_serial_number[0] != '\0') {
582                         printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
583                 }
584                 goto out;
585         }
586
587         if (dev_scsi.serial[0] == '\0') {
588                 retval = 1;
589                 goto out;
590         }
591
592         if (reformat_serial) {
593                 char serial_str[MAX_SERIAL_LEN];
594
595                 udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
596                 udev_util_replace_chars(serial_str, NULL);
597                 printf("%s\n", serial_str);
598                 goto out;
599         }
600
601         printf("%s\n", dev_scsi.serial);
602 out:
603         return retval;
604 }
605
606 int main(int argc, char **argv)
607 {
608         struct udev *udev;
609         int retval = 0;
610         char maj_min_dev[MAX_PATH_LEN];
611         int newargc;
612         char **newargv;
613
614         udev = udev_new();
615         if (udev == NULL)
616                 goto exit;
617
618         udev_log_init("scsi_id");
619         udev_set_log_fn(udev, log_fn);
620
621         /*
622          * Get config file options.
623          */
624         newargv = NULL;
625         retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
626         if (retval < 0) {
627                 retval = 1;
628                 goto exit;
629         }
630         if (newargv && (retval == 0)) {
631                 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
632                         retval = 2;
633                         goto exit;
634                 }
635                 free(newargv);
636         }
637
638         /*
639          * Get command line options (overriding any config file settings).
640          */
641         if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
642                 exit(1);
643
644         if (!dev_specified) {
645                 err(udev, "no device specified\n");
646                 retval = 1;
647                 goto exit;
648         }
649
650         retval = scsi_id(udev, maj_min_dev);
651
652 exit:
653         udev_unref(udev);
654         udev_log_close();
655         return retval;
656 }