chiark / gitweb /
add "Persistent Device Naming" rules file for disks
[elogind.git] / extras / scsi_id / scsi_id.c
1 /*
2  * scsi_id.c
3  *
4  * Main section of the scsi_id program
5  *
6  * Copyright (C) IBM Corp. 2003
7  *
8  *  This library is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License as
10  *  published by the Free Software Foundation; either version 2.1 of the
11  *  License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  *  USA
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <stdarg.h>
33 #include <ctype.h>
34 #include <sys/stat.h>
35 #include <sysfs/libsysfs.h>
36 #include "scsi_id_version.h"
37 #include "scsi_id.h"
38
39 #ifndef SCSI_ID_VERSION
40 #warning No version
41 #define SCSI_ID_VERSION "unknown"
42 #endif
43
44 /*
45  * temporary names for mknod.
46  */
47 #define TMP_DIR "/tmp"
48 #define TMP_PREFIX "scsi"
49
50 /*
51  * XXX Note the 'e' (send output to stderr in all cases), and 'c' (callout)
52  * options are not supported, but other code is still left in place for
53  * now.
54  */
55 static const char short_options[] = "abd:f:gip:s:uvVx";
56 /*
57  * Just duplicate per dev options.
58  */
59 static const char dev_short_options[] = "bgp:";
60
61 char sysfs_mnt_path[SYSFS_PATH_MAX];
62
63 static int all_good;
64 static int always_info;
65 static char *default_callout;
66 static int dev_specified;
67 static int sys_specified;
68 static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
69 static int display_bus_id;
70 static enum page_code default_page_code;
71 static int use_stderr;
72 static int debug;
73 static int hotplug_mode;
74 static int reformat_serial;
75 static int export;
76 static char vendor_str[64];
77 static char model_str[64];
78 static char revision_str[16];
79 static char type_str[16];
80
81 void log_message (int level, const char *format, ...)
82 {
83         va_list args;
84
85         if (!debug && level == LOG_DEBUG)
86                 return;
87
88         va_start (args, format);
89         if (!hotplug_mode || use_stderr) {
90                 vfprintf(stderr, format, args);
91         } else {
92                 static int logging_init = 0;
93                 if (!logging_init) {
94                         openlog ("scsi_id", LOG_PID, LOG_DAEMON);
95                         logging_init = 1;
96                 }
97
98                 vsyslog(level, format, args);
99         }
100         va_end (args);
101         return;
102 }
103
104 static void set_str(char *to, const char *from, size_t count)
105 {
106         size_t i, j, len;
107
108         /* strip trailing whitespace */
109         len = strnlen(from, count);
110         while (len && isspace(from[len-1]))
111                 len--;
112
113         /* strip leading whitespace */
114         i = 0;
115         while (isspace(from[i]) && (i < len))
116                 i++;
117
118         j = 0;
119         while (i < len) {
120                 /* substitute multiple whitespace */
121                 if (isspace(from[i])) {
122                         while (isspace(from[i]))
123                                 i++;
124                         to[j++] = '_';
125                 }
126                 /* skip chars */
127                 if (from[i] == '/') {
128                         i++;
129                         continue;
130                 }
131                 to[j++] = from[i++];
132         }
133         to[j] = '\0';
134 }
135
136 static void set_type(char *to, const char *from, size_t len)
137 {
138         int type_num;
139         char *eptr;
140         char *type = "generic";
141
142         type_num = strtoul(from, &eptr, 0);
143         if (eptr != from) {
144                 switch (type_num) {
145                 case 0:
146                         type = "disk";
147                         break;
148                 case 1:
149                         type = "tape";
150                         break;
151                 case 4:
152                         type = "optical";
153                         break;
154                 case 5:
155                         type = "cd";
156                         break;
157                 case 7:
158                         type = "optical";
159                         break;
160                 case 0xe:
161                         type = "disk";
162                         break;
163                 case 0xf:
164                         type = "optical";
165                         break;
166                 default:
167                         break;
168                 }
169         }
170         strncpy(to, type, len);
171         to[len-1] = '\0';
172 }
173
174 static int get_major_minor(struct sysfs_class_device *class_dev, int *maj,
175                            int *min)
176 {
177         struct sysfs_attribute *dev_attr;
178
179         dev_attr = sysfs_get_classdev_attr(class_dev, "dev");
180         if (!dev_attr) {
181                 /*
182                  * XXX This happens a lot, since sg has no dev attr.
183                  * And now sysfsutils does not set a meaningful errno
184                  * value. Someday change this back to a LOG_WARNING.
185                  * And if sysfsutils changes, check for ENOENT and handle
186                  * it separately.
187                  */
188                 log_message(LOG_DEBUG, "%s: could not get dev attribute: %s\n",
189                         class_dev->name, strerror(errno));
190                 return -1;
191         }
192
193         dprintf("dev value %s", dev_attr->value); /* value has a trailing \n */
194         if (sscanf(dev_attr->value, "%u:%u", maj, min) != 2) {
195                 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
196                             class_dev->name);
197                 return -1;
198         }
199
200         return 0;
201 }
202
203 static int create_tmp_dev(struct sysfs_class_device *class_dev, char *tmpdev,
204                           int dev_type)
205 {
206         int maj, min;
207
208         dprintf("(%s)\n", class_dev->name);
209
210         if (get_major_minor(class_dev, &maj, &min))
211                 return -1;
212         snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
213                  TMP_DIR, TMP_PREFIX, maj, min, getpid());
214
215         dprintf("tmpdev '%s'\n", tmpdev);
216
217         if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
218                 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
219                 return -1;
220         }
221         return 0;
222 }
223
224 static int has_sysfs_prefix(const char *path, const char *prefix)
225 {
226         char match[MAX_NAME_LEN];
227
228         strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
229         strncat(match, prefix, MAX_NAME_LEN);
230         if (strncmp(path, match, strlen(match)) == 0)
231                 return 1;
232         else
233                 return 0;
234 }
235
236 /*
237  * get_value:
238  *
239  * buf points to an '=' followed by a quoted string ("foo") or a string ending
240  * with a space or ','.
241  *
242  * Return a pointer to the NUL terminated string, returns NULL if no
243  * matches.
244  */
245 static char *get_value(char **buffer)
246 {
247         static char *quote_string = "\"\n";
248         static char *comma_string = ",\n";
249         char *val;
250         char *end;
251
252         if (**buffer == '"') {
253                 /*
254                  * skip leading quote, terminate when quote seen
255                  */
256                 (*buffer)++;
257                 end = quote_string;
258         } else {
259                 end = comma_string;
260         }
261         val = strsep(buffer, end);
262         if (val && end == quote_string)
263                 /*
264                  * skip trailing quote
265                  */
266                 (*buffer)++;
267
268         while (isspace(**buffer))
269                 (*buffer)++;
270
271         return val;
272 }
273
274 static int argc_count(char *opts)
275 {
276         int i = 0;
277         while (*opts != '\0')
278                 if (*opts++ == ' ')
279                         i++;
280         return i;
281 }
282
283 /*
284  * get_file_options:
285  *
286  * If vendor == NULL, find a line in the config file with only "OPTIONS=";
287  * if vendor and model are set find the first OPTIONS line in the config
288  * file that matches. Set argc and argv to match the OPTIONS string.
289  *
290  * vendor and model can end in '\n'.
291  */
292 static int get_file_options(char *vendor, char *model, int *argc,
293                             char ***newargv)
294 {
295         char *buffer;
296         FILE *fd;
297         char *buf;
298         char *str1;
299         char *vendor_in, *model_in, *options_in; /* read in from file */
300         int lineno;
301         int c;
302         int retval = 0;
303
304         dprintf("vendor='%s'; model='%s'\n", vendor, model);
305         fd = fopen(config_file, "r");
306         if (fd == NULL) {
307                 dprintf("can't open %s\n", config_file);
308                 if (errno == ENOENT) {
309                         return 1;
310                 } else {
311                         log_message(LOG_WARNING, "can't open %s: %s\n",
312                                 config_file, strerror(errno));
313                         return -1;
314                 }
315         }
316
317         /*
318          * Allocate a buffer rather than put it on the stack so we can
319          * keep it around to parse any options (any allocated newargv
320          * points into this buffer for its strings).
321          */
322         buffer = malloc(MAX_BUFFER_LEN);
323         if (!buffer) {
324                 log_message(LOG_WARNING, "Can't allocate memory.\n");
325                 return -1;
326         }
327
328         *newargv = NULL;
329         lineno = 0;
330         while (1) {
331                 vendor_in = model_in = options_in = NULL;
332
333                 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
334                 if (buf == NULL)
335                         break;
336                 lineno++;
337                 if (buf[strlen(buffer) - 1] != '\n') {
338                         log_message(LOG_WARNING,
339                                     "Config file line %d too long.\n", lineno);
340                         break;
341                 }
342
343                 while (isspace(*buf))
344                         buf++;
345
346                 if (*buf == '\0')
347                         /*
348                          * blank or all whitespace line
349                          */
350                         continue;
351
352                 if (*buf == '#')
353                         /*
354                          * comment line
355                          */
356                         continue;
357
358 #ifdef LOTS
359                 dprintf("lineno %d: '%s'\n", lineno, buf);
360 #endif
361                 str1 = strsep(&buf, "=");
362                 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
363                         str1 = get_value(&buf);
364                         if (!str1) {
365                                 retval = -1;
366                                 break;
367                         }
368                         vendor_in = str1;
369
370                         str1 = strsep(&buf, "=");
371                         if (str1 && strcasecmp(str1, "MODEL") == 0) {
372                                 str1 = get_value(&buf);
373                                 if (!str1) {
374                                         retval = -1;
375                                         break;
376                                 }
377                                 model_in = str1;
378                                 str1 = strsep(&buf, "=");
379                         }
380                 }
381
382                 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
383                         str1 = get_value(&buf);
384                         if (!str1) {
385                                 retval = -1;
386                                 break;
387                         }
388                         options_in = str1;
389                 }
390                 dprintf("config file line %d:"
391                         " vendor '%s'; model '%s'; options '%s'\n",
392                         lineno, vendor_in, model_in, options_in);
393                 /*
394                  * Only allow: [vendor=foo[,model=bar]]options=stuff
395                  */
396                 if (!options_in || (!vendor_in && model_in)) {
397                         log_message(LOG_WARNING,
398                                     "Error parsing config file line %d '%s'\n",
399                                     lineno, buffer);
400                         retval = -1;
401                         break;
402                 }
403                 if (vendor == NULL) {
404                         if (vendor_in == NULL) {
405                                 dprintf("matched global option\n");
406                                 break;
407                         }
408                 } else if ((vendor_in && strncmp(vendor, vendor_in,
409                                                  strlen(vendor_in)) == 0) &&
410                            (!model_in || (strncmp(model, model_in,
411                                                   strlen(model_in)) == 0))) {
412                                 /*
413                                  * Matched vendor and optionally model.
414                                  *
415                                  * Note: a short vendor_in or model_in can
416                                  * give a partial match (that is FOO
417                                  * matches FOOBAR).
418                                  */
419                                 dprintf("matched vendor/model\n");
420                                 break;
421                 } else {
422                         dprintf("no match\n");
423                 }
424         }
425
426         if (retval == 0) {
427                 if (vendor_in != NULL || model_in != NULL ||
428                     options_in != NULL) {
429                         /*
430                          * Something matched. Allocate newargv, and store
431                          * values found in options_in.
432                          */
433                         strcpy(buffer, options_in);
434                         c = argc_count(buffer) + 2;
435                         *newargv = calloc(c, sizeof(**newargv));
436                         if (!*newargv) {
437                                 log_message(LOG_WARNING,
438                                             "Can't allocate memory.\n");
439                                 retval = -1;
440                         } else {
441                                 *argc = c;
442                                 c = 0;
443                                 /*
444                                  * argv[0] at 0 is skipped by getopt, but
445                                  * store the buffer address there for
446                                  * alter freeing.
447                                  */
448                                 (*newargv)[c] = buffer;
449                                 for (c = 1; c < *argc; c++)
450                                         (*newargv)[c] = strsep(&buffer, " ");
451                         }
452                 } else {
453                         /*
454                          * No matches.
455                          */
456                         retval = 1;
457                 }
458         }
459         if (retval != 0)
460                 free(buffer);
461         fclose(fd);
462         return retval;
463 }
464
465 static int set_options(int argc, char **argv, const char *short_opts,
466                        char *target, char *maj_min_dev)
467 {
468         int option;
469
470         /*
471          * optind is a global extern used by getopt. Since we can call
472          * set_options twice (once for command line, and once for config
473          * file) we have to reset this back to 1. [Note glibc handles
474          * setting this to 0, but klibc does not.]
475          */
476         optind = 1;
477         while (1) {
478                 option = getopt(argc, argv, short_opts);
479                 if (option == -1)
480                         break;
481
482                 if (optarg)
483                         dprintf("option '%c' arg '%s'\n", option, optarg);
484                 else
485                         dprintf("option '%c'\n", option);
486
487                 switch (option) {
488                 case 'a':
489                         always_info = 1;
490                         break;
491                 case 'b':
492                         all_good = 0;
493                         break;
494
495                 case 'c':
496                         default_callout = optarg;
497                         break;
498
499                 case 'd':
500                         dev_specified = 1;
501                         strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
502                         break;
503
504                 case 'e':
505                         use_stderr = 1;
506                         break;
507
508                 case 'f':
509                         strncpy(config_file, optarg, MAX_NAME_LEN);
510                         break;
511
512                 case 'g':
513                         all_good = 1;
514                         break;
515
516                 case 'i':
517                         display_bus_id = 1;
518                         break;
519
520                 case 'p':
521                         if (strcmp(optarg, "0x80") == 0) {
522                                 default_page_code = PAGE_80;
523                         } else if (strcmp(optarg, "0x83") == 0) {
524                                 default_page_code = PAGE_83;
525                         } else if (strcmp(optarg, "pre-spc3-83") == 0) {
526                                 default_page_code = PAGE_83_PRE_SPC3; 
527                         } else {
528                                 log_message(LOG_WARNING,
529                                             "Unknown page code '%s'\n", optarg);
530                                 return -1;
531                         }
532                         break;
533
534                 case 's':
535                         sys_specified = 1;
536                         strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
537                         strncat(target, optarg, MAX_NAME_LEN);
538                         break;
539
540                 case 'u':
541                         reformat_serial = 1;
542                         break;
543
544                 case 'x':
545                         export = 1;
546                         break;
547
548                 case 'v':
549                         debug++;
550                         break;
551
552                 case 'V':
553                         log_message(LOG_WARNING, "scsi_id version: %s\n",
554                                     SCSI_ID_VERSION);
555                         exit(0);
556                         break;
557
558                 default:
559                         log_message(LOG_WARNING,
560                                     "Unknown or bad option '%c' (0x%x)\n",
561                                     option, option);
562                         return -1;
563                 }
564         }
565         return 0;
566 }
567
568 static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
569                            int *page_code, char *callout)
570 {
571         int retval;
572         int newargc;
573         char **newargv = NULL;
574         struct sysfs_attribute *vendor, *model, *type;
575         int option;
576
577         *good_bad = all_good;
578         *page_code = default_page_code;
579         if (default_callout && (callout != default_callout))
580                 strncpy(callout, default_callout, MAX_NAME_LEN);
581         else
582                 callout[0] = '\0';
583
584         vendor = sysfs_get_device_attr(scsi_dev, "vendor");
585         if (!vendor) {
586                 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
587                             scsi_dev->name);
588                 return -1;
589         }
590         set_str(vendor_str, vendor->value, sizeof(vendor_str)-1);
591
592         model = sysfs_get_device_attr(scsi_dev, "model");
593         if (!model) {
594                 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
595                             scsi_dev->name);
596                 return -1;
597         }
598         set_str(model_str, model->value, sizeof(model_str)-1);
599
600         type = sysfs_get_device_attr(scsi_dev, "type");
601         if (!type) {
602                 log_message(LOG_WARNING, "%s: cannot get type attribute\n",
603                             scsi_dev->name);
604                 return -1;
605         }
606         set_type(type_str, type->value, sizeof(type_str));
607
608         type = sysfs_get_device_attr(scsi_dev, "rev");
609         if (!type) {
610                 log_message(LOG_WARNING, "%s: cannot get type attribute\n",
611                             scsi_dev->name);
612                 return -1;
613         }
614         set_str(revision_str, type->value, sizeof(revision_str)-1);
615
616         retval = get_file_options(vendor->value, model->value, &newargc,
617                                   &newargv);
618
619         optind = 1; /* reset this global extern */
620         while (retval == 0) {
621                 option = getopt(newargc, newargv, dev_short_options);
622                 if (option == -1)
623                         break;
624
625                 if (optarg)
626                         dprintf("option '%c' arg '%s'\n", option, optarg);
627                 else
628                         dprintf("option '%c'\n", option);
629
630                 switch (option) {
631                 case 'b':
632                         *good_bad = 0;
633                         break;
634
635                 case 'c':
636                         strncpy(callout, default_callout, MAX_NAME_LEN);
637                         break;
638
639                 case 'g':
640                         *good_bad = 1;
641                         break;
642
643                 case 'p':
644                         if (strcmp(optarg, "0x80") == 0) {
645                                 *page_code = PAGE_80;
646                         } else if (strcmp(optarg, "0x83") == 0) {
647                                 *page_code = PAGE_83;
648                         } else if (strcmp(optarg, "pre-spc3-83") == 0) {
649                                 *page_code = PAGE_83_PRE_SPC3; 
650                         } else {
651                                 log_message(LOG_WARNING,
652                                             "Unknown page code '%s'\n", optarg);
653                                 retval = -1;
654                         }
655                         break;
656
657                 default:
658                         log_message(LOG_WARNING,
659                                     "Unknown or bad option '%c' (0x%x)\n",
660                                     option, option);
661                         retval = -1;
662                         break;
663                 }
664         }
665
666         if (newargv) {
667                 free(newargv[0]);
668                 free(newargv);
669         }
670         return retval;
671 }
672
673 /*
674  * format_serial: replace to whitespaces by underscores for calling
675  * programs that use the serial for device naming (multipath, Suse
676  * naming, etc...)
677  */
678 static void format_serial(char *serial)
679 {
680         char *p = serial;
681
682         while (*p != '\0') {
683                 if (isspace(*p))
684                         *p = '_';
685                 p++;
686         }
687 }
688
689 /*
690  * scsi_id: try to get an id, if one is found, printf it to stdout.
691  * returns a value passed to exit() - 0 if printed an id, else 1. This
692  * could be expanded, for example, if we want to report a failure like no
693  * memory etc. return 2, and return 1 for expected cases (like broken
694  * device found) that do not print an id.
695  */
696 static int scsi_id(const char *target_path, char *maj_min_dev)
697 {
698         int retval;
699         int dev_type = 0;
700         char *serial, *unaligned_buf;
701         struct sysfs_class_device *class_dev; /* of target_path */
702         struct sysfs_class_device *class_dev_parent; /* for partitions */
703         struct sysfs_device *scsi_dev; /* the scsi_device */
704         int good_dev;
705         int page_code;
706         char callout[MAX_NAME_LEN];
707
708         dprintf("target_path %s\n", target_path);
709
710         /*
711          * Ugly: depend on the sysfs path to tell us whether this is a
712          * block or char device. This should probably be encoded in the
713          * "dev" along with the major/minor.
714          */
715         if (has_sysfs_prefix(target_path, "/block")) {
716                 dev_type = S_IFBLK;
717         } else if (has_sysfs_prefix(target_path, "/class")) {
718                 dev_type = S_IFCHR;
719         } else {
720                 if (!hotplug_mode) {
721                         log_message(LOG_WARNING,
722                                     "Non block or class device '%s'\n",
723                                     target_path);
724                         return 1;
725                 } else {
726                         /*
727                          * Expected in some cases.
728                          */
729                         dprintf("Non block or class device\n");
730                         return 0;
731                 }
732         }
733
734         class_dev = sysfs_open_class_device_path(target_path);
735         if (!class_dev) {
736                 log_message(LOG_WARNING, "open class %s failed: %s\n",
737                             target_path, strerror(errno));
738                 return 1;
739         }
740         class_dev_parent = sysfs_get_classdev_parent(class_dev);
741         dprintf("class_dev 0x%p; class_dev_parent 0x%p\n", class_dev,
742                 class_dev_parent);
743         if (class_dev_parent) {
744                 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
745         } else {
746                 scsi_dev = sysfs_get_classdev_device(class_dev);
747         }
748
749         /*
750          * The close of scsi_dev will close class_dev or class_dev_parent.
751          */
752
753         /*
754          * We assume we are called after the device is completely ready,
755          * so we don't have to loop here like udev. (And we are usually
756          * called via udev.)
757          */
758         if (!scsi_dev) {
759                 /*
760                  * errno is not set if we can't find the device link, so
761                  * don't print it out here.
762                  */
763                 log_message(LOG_WARNING, "Cannot find sysfs device associated with %s\n",
764                             target_path);
765                 return 1;
766         }
767
768
769         /*
770          * Allow only scsi devices.
771          *
772          * Other block devices can support SG IO, but only ide-cd does, so
773          * for now, don't bother with anything else.
774          */
775         if (strcmp(scsi_dev->bus, "scsi") != 0) {
776                 if (hotplug_mode)
777                         /*
778                          * Expected in some cases.
779                          */
780                         dprintf("%s is not a scsi device\n", target_path);
781                 else
782                         log_message(LOG_WARNING, "%s is not a scsi device\n",
783                                     target_path);
784                 return 1;
785         }
786
787         /*
788          * mknod a temp dev to communicate with the device.
789          */
790         if (!dev_specified && create_tmp_dev(class_dev, maj_min_dev,
791                                              dev_type)) {
792                 dprintf("create_tmp_dev failed\n");
793                 return 1;
794         }
795
796         /*
797          * Get any per device (vendor + model) options from the config
798          * file.
799          */
800         retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
801         dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
802                 good_dev, page_code, callout);
803
804 #define ALIGN   512
805         unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
806         serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
807                           & ~(ALIGN - 1));
808         dprintf("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
809 #undef ALIGN
810
811         if (!good_dev) {
812                 retval = 1;
813         } else if (callout[0] != '\0') {
814                 /*
815                  * XXX Disabled for now ('c' is not in any options[]).
816                  */
817                 retval = 1;
818         } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
819                                    serial, MAX_SERIAL_LEN)) {
820                 retval = always_info?0:1;
821         } else {
822                 retval = 0;
823         }
824         if (!retval) {
825                 if (export) {
826                         static char serial_str[64];
827                         printf("ID_VENDOR=%s\n", vendor_str);
828                         printf("ID_MODEL=%s\n", model_str);
829                         printf("ID_REVISION=%s\n", revision_str);
830                         set_str(serial_str, serial, sizeof(serial_str));
831                         printf("ID_SERIAL=%s\n", serial_str);
832                         printf("ID_TYPE=%s\n", type_str);
833                         printf("ID_BUS=scsi\n");
834                 } else {
835                         if (reformat_serial)
836                                 format_serial(serial);
837                         if (display_bus_id)
838                                 printf("%s: ", scsi_dev->name);
839                         printf("%s\n", serial);
840                 }
841                 dprintf("%s\n", serial);
842                 retval = 0;
843         }
844         sysfs_close_device(scsi_dev);
845
846         if (!dev_specified)
847                 unlink(maj_min_dev);
848
849         return retval;
850 }
851
852 int main(int argc, char **argv)
853 {
854         int retval;
855         char *devpath;
856         char target_path[MAX_NAME_LEN];
857         char maj_min_dev[MAX_NAME_LEN];
858         int newargc;
859         char **newargv;
860
861         if (getenv("DEBUG"))
862                 debug++;
863
864         dprintf("argc is %d\n", argc);
865         if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
866                 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
867                         strerror(errno));
868                 exit(1);
869         }
870
871         devpath = getenv("DEVPATH");
872         if (devpath) {
873                 /*
874                  * This implies that we were invoked via udev or hotplug.
875                  */
876                 hotplug_mode = 1;
877                 sys_specified = 1;
878                 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
879                 strncat(target_path, devpath, MAX_NAME_LEN);
880         }
881
882         /*
883          * Get config file options.
884          */
885         newargv = NULL;
886         retval = get_file_options(NULL, NULL, &newargc, &newargv);
887         if (retval < 0) {
888                 exit(1);
889         } else if (newargv && (retval == 0)) {
890                 if (set_options(newargc, newargv, short_options, target_path,
891                                 maj_min_dev) < 0)
892                         exit(1);
893                 free(newargv);
894         }
895         /*
896          * Get command line options (overriding any config file or DEVPATH
897          * settings).
898          */
899         if (set_options(argc, argv, short_options, target_path,
900                         maj_min_dev) < 0)
901                 exit(1);
902
903         if (!sys_specified) {
904                 log_message(LOG_WARNING, "-s must be specified\n");
905                 exit(1);
906         }
907
908         retval = scsi_id(target_path, maj_min_dev);
909         exit(retval);
910 }