chiark / gitweb /
add ID_TYPE to the id probers
[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[] = "bd: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 char *default_callout;
65 static int dev_specified;
66 static int sys_specified;
67 static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
68 static int display_bus_id;
69 static int default_page_code;
70 static int use_stderr;
71 static int debug;
72 static int hotplug_mode;
73 static int reformat_serial;
74 static int export;
75 static char vendor_str[64];
76 static char model_str[64];
77 static char revision_str[16];
78 static char type_str[16];
79
80 void log_message (int level, const char *format, ...)
81 {
82         va_list args;
83
84         if (!debug && level == LOG_DEBUG)
85                 return;
86
87         va_start (args, format);
88         if (!hotplug_mode || use_stderr) {
89                 vfprintf(stderr, format, args);
90         } else {
91                 static int logging_init = 0;
92                 if (!logging_init) {
93                         openlog ("scsi_id", LOG_PID, LOG_DAEMON);
94                         logging_init = 1;
95                 }
96
97                 vsyslog(level, format, args);
98         }
99         va_end (args);
100         return;
101 }
102
103 static void set_str(char *to, const unsigned char *from, int count)
104 {
105         int i, j;
106         int len;
107
108         /* strip trailing whitespace */
109         len = strnlen(from, count);
110         while (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, int count)
137 {
138         int type_num;
139         char *eptr;
140
141         type_num = strtoul(from, &eptr, 0);
142         if (eptr != from) {
143                 switch (type_num) {
144                 case 0:
145                         sprintf(to, "disk");
146                         break;
147                 case 1:
148                         sprintf(to, "tape");
149                         break;
150                 case 4:
151                         sprintf(to, "optical");
152                         break;
153                 case 5:
154                         sprintf(to, "cd");
155                         break;
156                 case 7:
157                         sprintf(to, "optical");
158                         break;
159                 case 0xe:
160                         sprintf(to, "disk");
161                         break;
162                 case 0xf:
163                         sprintf(to, "optical");
164                         break;
165                 default:
166                         sprintf(to, "generic");
167                         break;
168                 }
169         } else {
170                 sprintf(to, "generic");
171         }
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_options);
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 'b':
489                         all_good = 0;
490                         break;
491
492                 case 'c':
493                         default_callout = optarg;
494                         break;
495
496                 case 'd':
497                         dev_specified = 1;
498                         strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
499                         break;
500
501                 case 'e':
502                         use_stderr = 1;
503                         break;
504
505                 case 'f':
506                         strncpy(config_file, optarg, MAX_NAME_LEN);
507                         break;
508
509                 case 'g':
510                         all_good = 1;
511                         break;
512
513                 case 'i':
514                         display_bus_id = 1;
515                         break;
516
517                 case 'p':
518                         if (strcmp(optarg, "0x80") == 0) {
519                                 default_page_code = 0x80;
520                         } else if (strcmp(optarg, "0x83") == 0) {
521                                 default_page_code = 0x83;
522                         } else {
523                                 log_message(LOG_WARNING,
524                                             "Unknown page code '%s'\n", optarg);
525                                 return -1;
526                         }
527                         break;
528
529                 case 's':
530                         sys_specified = 1;
531                         strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
532                         strncat(target, optarg, MAX_NAME_LEN);
533                         break;
534
535                 case 'u':
536                         reformat_serial = 1;
537                         break;
538
539                 case 'x':
540                         export = 1;
541                         break;
542
543                 case 'v':
544                         debug++;
545                         break;
546
547                 case 'V':
548                         log_message(LOG_WARNING, "scsi_id version: %s\n",
549                                     SCSI_ID_VERSION);
550                         exit(0);
551                         break;
552
553                 default:
554                         log_message(LOG_WARNING,
555                                     "Unknown or bad option '%c' (0x%x)\n",
556                                     option, option);
557                         return -1;
558                 }
559         }
560         return 0;
561 }
562
563 static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
564                            int *page_code, char *callout)
565 {
566         int retval;
567         int newargc;
568         char **newargv = NULL;
569         struct sysfs_attribute *vendor, *model, *type;
570         int option;
571
572         *good_bad = all_good;
573         *page_code = default_page_code;
574         if (default_callout && (callout != default_callout))
575                 strncpy(callout, default_callout, MAX_NAME_LEN);
576         else
577                 callout[0] = '\0';
578
579         vendor = sysfs_get_device_attr(scsi_dev, "vendor");
580         if (!vendor) {
581                 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
582                             scsi_dev->name);
583                 return -1;
584         }
585         set_str(vendor_str, vendor->value, sizeof(vendor_str)-1);
586
587         model = sysfs_get_device_attr(scsi_dev, "model");
588         if (!model) {
589                 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
590                             scsi_dev->name);
591                 return -1;
592         }
593         set_str(model_str, model->value, sizeof(model_str)-1);
594
595         type = sysfs_get_device_attr(scsi_dev, "type");
596         if (!type) {
597                 log_message(LOG_WARNING, "%s: cannot get type attribute\n",
598                             scsi_dev->name);
599                 return -1;
600         }
601         set_type(type_str, type->value, sizeof(type_str)-1);
602
603         type = sysfs_get_device_attr(scsi_dev, "rev");
604         if (!type) {
605                 log_message(LOG_WARNING, "%s: cannot get type attribute\n",
606                             scsi_dev->name);
607                 return -1;
608         }
609         set_str(revision_str, type->value, sizeof(revision_str)-1);
610
611         retval = get_file_options(vendor->value, model->value, &newargc,
612                                   &newargv);
613
614         optind = 1; /* reset this global extern */
615         while (retval == 0) {
616                 option = getopt(newargc, newargv, dev_short_options);
617                 if (option == -1)
618                         break;
619
620                 if (optarg)
621                         dprintf("option '%c' arg '%s'\n", option, optarg);
622                 else
623                         dprintf("option '%c'\n", option);
624
625                 switch (option) {
626                 case 'b':
627                         *good_bad = 0;
628                         break;
629
630                 case 'c':
631                         strncpy(callout, default_callout, MAX_NAME_LEN);
632                         break;
633
634                 case 'g':
635                         *good_bad = 1;
636                         break;
637
638                 case 'p':
639                         if (strcmp(optarg, "0x80") == 0) {
640                                 *page_code = 0x80;
641                         } else if (strcmp(optarg, "0x83") == 0) {
642                                 *page_code = 0x83;
643                         } else {
644                                 log_message(LOG_WARNING,
645                                             "Unknown page code '%s'\n", optarg);
646                                 retval = -1;
647                         }
648                         break;
649
650                 default:
651                         log_message(LOG_WARNING,
652                                     "Unknown or bad option '%c' (0x%x)\n",
653                                     option, option);
654                         retval = -1;
655                         break;
656                 }
657         }
658
659         if (newargv) {
660                 free(newargv[0]);
661                 free(newargv);
662         }
663         return retval;
664 }
665
666 /*
667  * format_serial: replace to whitespaces by underscores for calling
668  * programs that use the serial for device naming (multipath, Suse
669  * naming, etc...)
670  */
671 static void format_serial(char *serial)
672 {
673         char *p = serial;
674
675         while (*p != '\0') {
676                 if (isspace(*p))
677                         *p = '_';
678                 p++;
679         }
680 }
681
682 /*
683  * scsi_id: try to get an id, if one is found, printf it to stdout.
684  * returns a value passed to exit() - 0 if printed an id, else 1. This
685  * could be expanded, for example, if we want to report a failure like no
686  * memory etc. return 2, and return 1 for expected cases (like broken
687  * device found) that do not print an id.
688  */
689 static int scsi_id(const char *target_path, char *maj_min_dev)
690 {
691         int retval;
692         int dev_type = 0;
693         char *serial, *unaligned_buf;
694         struct sysfs_class_device *class_dev; /* of target_path */
695         struct sysfs_class_device *class_dev_parent; /* for partitions */
696         struct sysfs_device *scsi_dev; /* the scsi_device */
697         int good_dev;
698         int page_code;
699         char callout[MAX_NAME_LEN];
700
701         dprintf("target_path %s\n", target_path);
702
703         /*
704          * Ugly: depend on the sysfs path to tell us whether this is a
705          * block or char device. This should probably be encoded in the
706          * "dev" along with the major/minor.
707          */
708         if (has_sysfs_prefix(target_path, "/block")) {
709                 dev_type = S_IFBLK;
710         } else if (has_sysfs_prefix(target_path, "/class")) {
711                 dev_type = S_IFCHR;
712         } else {
713                 if (!hotplug_mode) {
714                         log_message(LOG_WARNING,
715                                     "Non block or class device '%s'\n",
716                                     target_path);
717                         return 1;
718                 } else {
719                         /*
720                          * Expected in some cases.
721                          */
722                         dprintf("Non block or class device\n");
723                         return 0;
724                 }
725         }
726
727         class_dev = sysfs_open_class_device_path(target_path);
728         if (!class_dev) {
729                 log_message(LOG_WARNING, "open class %s failed: %s\n",
730                             target_path, strerror(errno));
731                 return 1;
732         }
733         class_dev_parent = sysfs_get_classdev_parent(class_dev);
734         dprintf("class_dev 0x%p; class_dev_parent 0x%p\n", class_dev,
735                 class_dev_parent);
736         if (class_dev_parent) {
737                 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
738         } else {
739                 scsi_dev = sysfs_get_classdev_device(class_dev);
740         }
741
742         /*
743          * The close of scsi_dev will close class_dev or class_dev_parent.
744          */
745
746         /*
747          * We assume we are called after the device is completely ready,
748          * so we don't have to loop here like udev. (And we are usually
749          * called via udev.)
750          */
751         if (!scsi_dev) {
752                 /*
753                  * errno is not set if we can't find the device link, so
754                  * don't print it out here.
755                  */
756                 log_message(LOG_WARNING, "Cannot find sysfs device associated with %s\n",
757                             target_path);
758                 return 1;
759         }
760
761
762         /*
763          * Allow only scsi devices.
764          *
765          * Other block devices can support SG IO, but only ide-cd does, so
766          * for now, don't bother with anything else.
767          */
768         if (strcmp(scsi_dev->bus, "scsi") != 0) {
769                 if (hotplug_mode)
770                         /*
771                          * Expected in some cases.
772                          */
773                         dprintf("%s is not a scsi device\n", target_path);
774                 else
775                         log_message(LOG_WARNING, "%s is not a scsi device\n",
776                                     target_path);
777                 return 1;
778         }
779
780         /*
781          * mknod a temp dev to communicate with the device.
782          */
783         if (!dev_specified && create_tmp_dev(class_dev, maj_min_dev,
784                                              dev_type)) {
785                 dprintf("create_tmp_dev failed\n");
786                 return 1;
787         }
788
789         /*
790          * Get any per device (vendor + model) options from the config
791          * file.
792          */
793         retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
794         dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
795                 good_dev, page_code, callout);
796
797 #define ALIGN   512
798         unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
799         serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
800                           & ~(ALIGN - 1));
801         dprintf("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
802 #undef ALIGN
803
804         if (!good_dev) {
805                 retval = 1;
806         } else if (callout[0] != '\0') {
807                 /*
808                  * XXX Disabled for now ('c' is not in any options[]).
809                  */
810                 retval = 1;
811         } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
812                                    serial, MAX_SERIAL_LEN)) {
813                 retval = 1;
814         } else {
815                 retval = 0;
816         }
817         if (!retval) {
818                 if (export) {
819                         static char serial_str[64];
820                         printf("ID_VENDOR=%s\n", vendor_str);
821                         printf("ID_MODEL=%s\n", model_str);
822                         printf("ID_REVISION=%s\n", revision_str);
823                         set_str(serial_str, serial, sizeof(serial_str));
824                         printf("ID_SERIAL=%s\n", serial_str);
825                         printf("ID_TYPE=%s\n", type_str);
826                 } else {
827                         if (reformat_serial)
828                                 format_serial(serial);
829                         if (display_bus_id)
830                                 printf("%s: ", scsi_dev->name);
831                         printf("%s\n", serial);
832                 }
833                 dprintf("%s\n", serial);
834                 retval = 0;
835         }
836         sysfs_close_device(scsi_dev);
837
838         if (!dev_specified)
839                 unlink(maj_min_dev);
840
841         return retval;
842 }
843
844 int main(int argc, char **argv)
845 {
846         int retval;
847         char *devpath;
848         char target_path[MAX_NAME_LEN];
849         char maj_min_dev[MAX_NAME_LEN];
850         int newargc;
851         char **newargv;
852
853         if (getenv("DEBUG"))
854                 debug++;
855
856         dprintf("argc is %d\n", argc);
857         if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
858                 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
859                         strerror(errno));
860                 exit(1);
861         }
862
863         devpath = getenv("DEVPATH");
864         if (devpath) {
865                 /*
866                  * This implies that we were invoked via udev or hotplug.
867                  */
868                 hotplug_mode = 1;
869                 sys_specified = 1;
870                 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
871                 strncat(target_path, devpath, MAX_NAME_LEN);
872         }
873
874         /*
875          * Get config file options.
876          */
877         newargv = NULL;
878         retval = get_file_options(NULL, NULL, &newargc, &newargv);
879         if (retval < 0) {
880                 exit(1);
881         } else if (newargv && (retval == 0)) {
882                 if (set_options(newargc, newargv, short_options, target_path,
883                                 maj_min_dev) < 0)
884                         exit(1);
885                 free(newargv);
886         }
887         /*
888          * Get command line options (overriding any config file or DEVPATH
889          * settings).
890          */
891         if (set_options(argc, argv, short_options, target_path,
892                         maj_min_dev) < 0)
893                 exit(1);
894
895         if (!sys_specified) {
896                 log_message(LOG_WARNING, "-s must be specified\n");
897                 exit(1);
898         }
899
900         retval = scsi_id(target_path, maj_min_dev);
901         exit(retval);
902 }