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