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