]>
Commit | Line | Data |
---|---|---|
c521693b GKH |
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 <getopt.h> | |
27 | #include <unistd.h> | |
28 | #include <signal.h> | |
29 | #include <fcntl.h> | |
30 | #include <errno.h> | |
31 | #include <string.h> | |
32 | #include <syslog.h> | |
33 | #include <stdarg.h> | |
34 | #include <ctype.h> | |
35 | #include <sys/stat.h> | |
36 | #include <sys/libsysfs.h> | |
37 | #include "scsi_id.h" | |
38 | ||
39 | #ifndef VERSION | |
40 | #warning No version | |
41 | #define VERSION "unknown" | |
42 | #endif | |
43 | ||
44 | /* | |
45 | * temporary names for mknod. | |
46 | */ | |
47 | #define TMP_DIR "/tmp" | |
48 | #define TMP_PREFIX "scsi" | |
49 | ||
50 | #define CONFIG_FILE "/etc/scsi_id.config" | |
51 | ||
52 | #define MAX_NAME_LEN 72 | |
53 | ||
54 | #define MAX_SERIAL_LEN 128 | |
55 | ||
56 | static const char short_options[] = "bc:d:ef:gip:s:vV"; | |
57 | static const struct option long_options[] = { | |
58 | {"broken", no_argument, NULL, 'b'}, /* also per dev */ | |
59 | {"callout", required_argument, NULL, 'c'}, /* also per dev */ | |
60 | {"device", required_argument, NULL, 'd'}, | |
61 | {"stderr", no_argument, NULL, 'e'}, | |
62 | {"file", required_argument, NULL, 'f'}, | |
63 | {"good", no_argument, NULL, 'g'}, /* also per dev */ | |
64 | {"busid", no_argument, NULL, 'i'}, /* also per dev */ | |
65 | {"page", required_argument, NULL, 'p'}, /* also per dev */ | |
66 | {"devpath", required_argument, NULL, 's'}, | |
67 | {"verbose", no_argument, NULL, 'v'}, | |
68 | {"version", no_argument, NULL, 'V'}, | |
69 | {0, 0, 0, 0} | |
70 | }; | |
71 | /* | |
72 | * Just duplicate per dev options. | |
73 | */ | |
74 | static const char dev_short_options[] = "bc:gp:"; | |
75 | static const struct option dev_long_options[] = { | |
76 | {"broken", no_argument, NULL, 'b'}, /* also per dev */ | |
77 | {"callout", required_argument, NULL, 'c'}, /* also per dev */ | |
78 | {"good", no_argument, NULL, 'g'}, /* also per dev */ | |
79 | {"page", required_argument, NULL, 'p'}, /* also per dev */ | |
80 | {0, 0, 0, 0} | |
81 | }; | |
82 | ||
83 | char sysfs_mnt_path[SYSFS_PATH_MAX]; | |
84 | ||
85 | static int all_good; | |
86 | static char *default_callout; | |
87 | static int dev_specified; | |
88 | static int sys_specified; | |
89 | static char config_file[MAX_NAME_LEN] = CONFIG_FILE; | |
90 | static int display_bus_id; | |
91 | static int default_page_code; | |
92 | static int use_stderr; | |
93 | static int debug; | |
94 | static int hotplug_mode; | |
95 | ||
96 | void log_message (int level, const char *format, ...) | |
97 | { | |
98 | va_list args; | |
99 | ||
100 | if (!debug && level == LOG_DEBUG) | |
101 | return; | |
102 | ||
103 | va_start (args, format); | |
104 | if (!hotplug_mode || use_stderr) { | |
105 | vfprintf(stderr, format, args); | |
106 | } else { | |
107 | static int logging_init = 0; | |
108 | if (!logging_init) { | |
109 | openlog ("scsi_id", LOG_PID, LOG_DAEMON); | |
110 | logging_init = 1; | |
111 | } | |
112 | ||
113 | vsyslog(level, format, args); | |
114 | } | |
115 | va_end (args); | |
116 | return; | |
117 | } | |
118 | ||
119 | static int sysfs_get_actual_dev(const char *sysfs_path, char *dev, int len) | |
120 | { | |
121 | dprintf("%s\n", sysfs_path); | |
122 | strncpy(dev, sysfs_path, len); | |
123 | strncat(dev, "/device", len); | |
124 | if (sysfs_get_link(dev, dev, len)) { | |
125 | if (!hotplug_mode) | |
126 | log_message(LOG_WARNING, "%s: %s\n", dev, | |
127 | strerror(errno)); | |
128 | return -1; | |
129 | } | |
130 | return 0; | |
131 | } | |
132 | ||
133 | /* | |
134 | * sysfs_is_bus: Given the sysfs_path to a device, return 1 if sysfs_path | |
135 | * is on bus, 0 if not on bus, and < 0 on error | |
136 | */ | |
137 | static int sysfs_is_bus(const char *sysfs_path, const char *bus) | |
138 | { | |
139 | char bus_dev_name[SYSFS_PATH_MAX]; | |
140 | char bus_id[SYSFS_NAME_LEN]; | |
141 | struct stat stat_buf; | |
142 | ino_t dev_inode; | |
143 | ||
144 | dprintf("%s\n", sysfs_path); | |
145 | ||
146 | if (sysfs_get_name_from_path(sysfs_path, bus_id, SYSFS_NAME_LEN)) | |
147 | return -1; | |
148 | ||
149 | snprintf(bus_dev_name, MAX_NAME_LEN, "%s/%s/%s/%s/%s", sysfs_mnt_path, | |
150 | SYSFS_BUS_DIR, bus, SYSFS_DEVICES_NAME, bus_id); | |
151 | ||
152 | if (stat(sysfs_path, &stat_buf)) | |
153 | return -1; | |
154 | dev_inode = stat_buf.st_ino; | |
155 | ||
156 | if (stat(bus_dev_name, &stat_buf)) { | |
157 | if (errno == ENOENT) | |
158 | return 0; | |
159 | else | |
160 | return -1; | |
161 | } | |
162 | if (dev_inode == stat_buf.st_ino) | |
163 | return 1; | |
164 | else | |
165 | return 0; | |
166 | } | |
167 | ||
168 | static int get_major_minor(const char *devpath, int *major, int *minor) | |
169 | { | |
170 | struct sysfs_class_device *class_dev; | |
171 | char dev_value[SYSFS_NAME_LEN]; | |
172 | char *dev; | |
173 | ||
174 | dprintf("%s\n", devpath); | |
175 | class_dev = sysfs_open_class_device(devpath); | |
176 | if (!class_dev) { | |
177 | log_message(LOG_WARNING, "open class %s failed: %s\n", devpath, | |
178 | strerror(errno)); | |
179 | return -1; | |
180 | } | |
181 | ||
182 | dev = sysfs_get_attr(class_dev, "dev"); | |
183 | if (dev) | |
184 | strncpy(dev_value, dev, SYSFS_NAME_LEN); | |
185 | sysfs_close_class_device(class_dev); | |
186 | if (!dev) { | |
187 | /* | |
188 | * XXX This happens a lot, since sg has no dev attr. | |
189 | * Someday change this back to a LOG_WARNING. | |
190 | */ | |
191 | log_message(LOG_DEBUG, "%s could not get dev attribute: %s\n", | |
192 | devpath, strerror(errno)); | |
193 | return -1; | |
194 | } | |
195 | dev = NULL; | |
196 | ||
197 | dprintf("dev %s", dev_value); /* dev_value has a trailing \n */ | |
198 | if (sscanf(dev_value, "%u:%u", major, minor) != 2) { | |
199 | log_message(LOG_WARNING, "%s: invalid dev major/minor\n", | |
200 | devpath); | |
201 | return -1; | |
202 | } | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
207 | static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type) | |
208 | { | |
209 | int major, minor; | |
210 | ||
211 | dprintf("(%s)\n", devpath); | |
212 | ||
213 | if (get_major_minor(devpath, &major, &minor)) | |
214 | return -1; | |
215 | snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u", | |
216 | TMP_DIR, TMP_PREFIX, major, minor, getpid()); | |
217 | ||
218 | dprintf("tmpdev '%s'\n", tmpdev); | |
219 | ||
220 | if (mknod(tmpdev, 0600 | dev_type, makedev(major, minor))) { | |
221 | log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno)); | |
222 | return -1; | |
223 | } | |
224 | return 0; | |
225 | } | |
226 | ||
227 | static int has_sysfs_prefix(const char *path, const char *prefix) | |
228 | { | |
229 | char match[MAX_NAME_LEN]; | |
230 | ||
231 | strncpy(match, sysfs_mnt_path, MAX_NAME_LEN); | |
232 | strncat(match, prefix, MAX_NAME_LEN); | |
233 | if (strncmp(path, match, strlen(match)) == 0) | |
234 | return 1; | |
235 | else | |
236 | return 0; | |
237 | } | |
238 | ||
239 | /* | |
240 | * get_value: | |
241 | * | |
242 | * buf points to an '=' followed by a quoted string ("foo") or a string ending | |
243 | * with a space or ','. | |
244 | * | |
245 | * Return a pointer to the NUL terminated string, returns NULL if no | |
246 | * matches. | |
247 | */ | |
248 | static char *get_value(char **buffer) | |
249 | { | |
250 | static char *quote_string = "\"\n"; | |
251 | static char *comma_string = ",\n"; | |
252 | char *val; | |
253 | char *end; | |
254 | ||
255 | if (**buffer == '"') { | |
256 | /* | |
257 | * skip leading quote, terminate when quote seen | |
258 | */ | |
259 | (*buffer)++; | |
260 | end = quote_string; | |
261 | } else { | |
262 | end = comma_string; | |
263 | } | |
264 | val = strsep(buffer, end); | |
265 | if (val && end == quote_string) | |
266 | /* | |
267 | * skip trailing quote | |
268 | */ | |
269 | (*buffer)++; | |
270 | ||
271 | while (isspace(**buffer)) | |
272 | (*buffer)++; | |
273 | ||
274 | return val; | |
275 | } | |
276 | ||
277 | static int argc_count(char *opts) | |
278 | { | |
279 | int i = 0; | |
280 | while (*opts != '\0') | |
281 | if (*opts++ == ' ') | |
282 | i++; | |
283 | return i; | |
284 | } | |
285 | ||
286 | /* | |
287 | * get_file_options: | |
288 | * | |
289 | * If vendor == NULL, find a line in the config file with only "OPTIONS="; | |
290 | * if vendor and model are set find the first OPTIONS line in the config | |
291 | * file that matches. Set argc and argv to match the OPTIONS string. | |
292 | * | |
293 | * vendor and model can end in '\n'. | |
294 | */ | |
295 | static int get_file_options(char *vendor, char *model, int *argc, | |
296 | char ***newargv) | |
297 | { | |
298 | char buffer[256]; | |
299 | FILE *fd; | |
300 | char *buf; | |
301 | char *str1; | |
302 | char *vendor_in, *model_in, *options_in; /* read in from file */ | |
303 | int lineno; | |
304 | int c; | |
305 | int retval = 0; | |
306 | static char *prog_string = "arg0"; | |
307 | ||
308 | dprintf("vendor='%s'; model='%s'\n", vendor, model); | |
309 | fd = fopen(config_file, "r"); | |
310 | if (fd == NULL) { | |
311 | dprintf("can't open %s\n", config_file); | |
312 | if (errno == ENOENT) { | |
313 | return 1; | |
314 | } else { | |
315 | log_message(LOG_WARNING, "can't open %s: %s\n", | |
316 | config_file, strerror(errno)); | |
317 | return -1; | |
318 | } | |
319 | } | |
320 | ||
321 | *newargv = NULL; | |
322 | lineno = 0; | |
323 | ||
324 | while (1) { | |
325 | vendor_in = model_in = options_in = NULL; | |
326 | ||
327 | buf = fgets(buffer, sizeof(buffer), fd); | |
328 | if (buf == NULL) | |
329 | break; | |
330 | lineno++; | |
331 | ||
332 | while (isspace(*buf)) | |
333 | buf++; | |
334 | ||
335 | if (*buf == '\0') | |
336 | /* | |
337 | * blank or all whitespace line | |
338 | */ | |
339 | continue; | |
340 | ||
341 | if (*buf == '#') | |
342 | /* | |
343 | * comment line | |
344 | */ | |
345 | continue; | |
346 | ||
347 | #ifdef LOTS | |
348 | dprintf("lineno %d: '%s'\n", lineno, buf); | |
349 | #endif | |
350 | str1 = strsep(&buf, "="); | |
351 | if (str1 && strcasecmp(str1, "VENDOR") == 0) { | |
352 | str1 = get_value(&buf); | |
353 | if (!str1) { | |
354 | retval = -1; | |
355 | break; | |
356 | } | |
357 | vendor_in = str1; | |
358 | ||
359 | str1 = strsep(&buf, "="); | |
360 | if (str1 && strcasecmp(str1, "MODEL") == 0) { | |
361 | str1 = get_value(&buf); | |
362 | if (!str1) { | |
363 | retval = -1; | |
364 | break; | |
365 | } | |
366 | model_in = str1; | |
367 | str1 = strsep(&buf, "="); | |
368 | } | |
369 | } | |
370 | ||
371 | if (str1 && strcasecmp(str1, "OPTIONS") == 0) { | |
372 | str1 = get_value(&buf); | |
373 | if (!str1) { | |
374 | retval = -1; | |
375 | break; | |
376 | } | |
377 | options_in = str1; | |
378 | } | |
379 | dprintf("config file line %d:" | |
380 | " vendor '%s'; model '%s'; options '%s'\n", | |
381 | lineno, vendor_in, model_in, options_in); | |
382 | /* | |
383 | * Only allow: [vendor=foo[,model=bar]]options=stuff | |
384 | */ | |
385 | if (!options_in || (!vendor_in && model_in)) { | |
386 | log_message(LOG_WARNING, | |
387 | "Error parsing config file line %d '%s'\n", | |
388 | lineno, buffer); | |
389 | retval = -1; | |
390 | break; | |
391 | } | |
392 | if (vendor == NULL) { | |
393 | if (vendor_in == NULL) { | |
394 | dprintf("matched global option\n"); | |
395 | break; | |
396 | } | |
397 | } else if ((vendor_in && strncmp(vendor, vendor_in, | |
398 | strlen(vendor_in)) == 0) && | |
399 | (!model_in || (strncmp(model, model_in, | |
400 | strlen(model_in)) == 0))) { | |
401 | /* | |
402 | * Matched vendor and optionally model. | |
403 | * | |
404 | * Note: a short vendor_in or model_in can | |
405 | * give a partial match (that is FOO | |
406 | * matches FOOBAR). | |
407 | */ | |
408 | dprintf("matched vendor/model\n"); | |
409 | break; | |
410 | } else { | |
411 | dprintf("no match\n"); | |
412 | } | |
413 | } | |
414 | ||
415 | if (retval == 0) { | |
416 | if (vendor_in != NULL || model_in != NULL || | |
417 | options_in != NULL) { | |
418 | /* | |
419 | * Something matched. Allocate newargv, and store | |
420 | * values found in options_in. | |
421 | */ | |
422 | c = argc_count(options_in) + 2; | |
423 | *newargv = calloc(c, sizeof(**newargv)); | |
424 | if (!*newargv) { | |
425 | log_message(LOG_WARNING, | |
426 | "Can't allocate memory\n"); | |
427 | retval = -1; | |
428 | } else { | |
429 | *argc = c; | |
430 | c = 0; | |
431 | (*newargv)[c] = prog_string; /* nothing */ | |
432 | for (c = 1; c < *argc; c++) | |
433 | (*newargv)[c] = strsep(&options_in, " "); | |
434 | } | |
435 | } else { | |
436 | /* | |
437 | * No matches. | |
438 | */ | |
439 | retval = 1; | |
440 | } | |
441 | } | |
442 | fclose(fd); | |
443 | return retval; | |
444 | } | |
445 | ||
446 | static int set_options(int argc, char **argv, const char *short_opts, | |
447 | const struct option *long_opts, char *target, | |
448 | char *maj_min_dev) | |
449 | { | |
450 | int option; | |
451 | int option_ind; | |
452 | ||
453 | /* | |
454 | * optind is a global extern used by getopt_long. Since we can | |
455 | * call set_options twice (once for command line, and once for | |
456 | * config file) we have to reset this back to 0. | |
457 | */ | |
458 | optind = 0; | |
459 | while (1) { | |
460 | option = getopt_long(argc, argv, short_options, long_options, | |
461 | &option_ind); | |
462 | if (option == -1) | |
463 | break; | |
464 | ||
465 | if (optarg) | |
466 | dprintf("option '%c' arg '%s'\n", option, optarg); | |
467 | else | |
468 | dprintf("option '%c'\n", option); | |
469 | ||
470 | switch (option) { | |
471 | case 'b': | |
472 | all_good = 0; | |
473 | break; | |
474 | ||
475 | case 'c': | |
476 | default_callout = optarg; | |
477 | break; | |
478 | ||
479 | case 'd': | |
480 | dev_specified = 1; | |
481 | strncpy(maj_min_dev, optarg, MAX_NAME_LEN); | |
482 | break; | |
483 | ||
484 | case 'e': | |
485 | use_stderr = 1; | |
486 | break; | |
487 | ||
488 | case 'f': | |
489 | strncpy(config_file, optarg, MAX_NAME_LEN); | |
490 | break; | |
491 | ||
492 | case 'g': | |
493 | all_good = 1; | |
494 | break; | |
495 | ||
496 | case 'i': | |
497 | display_bus_id = 1; | |
498 | break; | |
499 | ||
500 | case 'p': | |
501 | if (strcmp(optarg, "0x80") == 0) { | |
502 | default_page_code = 0x80; | |
503 | } else if (strcmp(optarg, "0x83") == 0) { | |
504 | default_page_code = 0x83; | |
505 | } else { | |
506 | log_message(LOG_WARNING, | |
507 | "Unknown page code '%s'\n", optarg); | |
508 | return -1; | |
509 | } | |
510 | break; | |
511 | ||
512 | case 's': | |
513 | sys_specified = 1; | |
514 | strncpy(target, sysfs_mnt_path, MAX_NAME_LEN); | |
515 | strncat(target, optarg, MAX_NAME_LEN); | |
516 | break; | |
517 | ||
518 | case 'v': | |
519 | debug++; | |
520 | break; | |
521 | ||
522 | case 'V': | |
523 | log_message(LOG_WARNING, "scsi_id version: %s\n", | |
524 | VERSION); | |
525 | exit(0); | |
526 | break; | |
527 | ||
528 | default: | |
529 | log_message(LOG_WARNING, | |
530 | "Unknown or bad option '%c' (0x%x)\n", | |
531 | option, option); | |
532 | return -1; | |
533 | } | |
534 | } | |
535 | return 0; | |
536 | } | |
537 | ||
538 | static int per_dev_options(struct sysfs_class_device *scsi_dev, int *good_bad, | |
539 | int *page_code, char *callout) | |
540 | { | |
541 | int retval; | |
542 | int newargc; | |
543 | char **newargv = NULL; | |
544 | char *vendor; | |
545 | char *model; | |
546 | int option; | |
547 | int option_ind; | |
548 | ||
549 | ||
550 | *good_bad = all_good; | |
551 | *page_code = default_page_code; | |
552 | if (default_callout && (callout != default_callout)) | |
553 | strncpy(callout, default_callout, MAX_NAME_LEN); | |
554 | else | |
555 | callout[0] = '\0'; | |
556 | ||
557 | vendor = sysfs_get_attr(scsi_dev, "vendor"); | |
558 | if (!vendor) { | |
559 | log_message(LOG_WARNING, "%s: no vendor attribute\n", | |
560 | scsi_dev->name); | |
561 | return -1; | |
562 | } | |
563 | ||
564 | model = sysfs_get_attr(scsi_dev, "model"); | |
565 | if (!vendor) { | |
566 | log_message(LOG_WARNING, "%s: no model attribute\n", | |
567 | scsi_dev->name); | |
568 | return -1; | |
569 | } | |
570 | ||
571 | retval = get_file_options(vendor, model, &newargc, &newargv); | |
572 | ||
573 | optind = 0; /* global extern, reset to 0 */ | |
574 | while (retval == 0) { | |
575 | option = getopt_long(newargc, newargv, dev_short_options, | |
576 | dev_long_options, &option_ind); | |
577 | if (option == -1) | |
578 | break; | |
579 | ||
580 | if (optarg) | |
581 | dprintf("option '%c' arg '%s'\n", option, optarg); | |
582 | else | |
583 | dprintf("option '%c'\n", option); | |
584 | ||
585 | switch (option) { | |
586 | case 'b': | |
587 | *good_bad = 0; | |
588 | break; | |
589 | ||
590 | case 'c': | |
591 | strncpy(callout, default_callout, MAX_NAME_LEN); | |
592 | break; | |
593 | ||
594 | case 'g': | |
595 | *good_bad = 1; | |
596 | break; | |
597 | ||
598 | case 'p': | |
599 | if (strcmp(optarg, "0x80") == 0) { | |
600 | *page_code = 0x80; | |
601 | } else if (strcmp(optarg, "0x83") == 0) { | |
602 | *page_code = 0x83; | |
603 | } else { | |
604 | log_message(LOG_WARNING, | |
605 | "Unknown page code '%s'\n", optarg); | |
606 | retval = -1; | |
607 | } | |
608 | break; | |
609 | ||
610 | default: | |
611 | log_message(LOG_WARNING, | |
612 | "Unknown or bad option '%c' (0x%x)\n", | |
613 | option, option); | |
614 | retval = -1; | |
615 | break; | |
616 | } | |
617 | } | |
618 | ||
619 | if (newargv) | |
620 | free(newargv); | |
621 | return retval; | |
622 | } | |
623 | ||
624 | /* | |
625 | * scsi_id: try to get an id, if one is found, printf it to stdout. | |
626 | * returns a value passed to exit() - 0 if printed an id, else 1. This | |
627 | * could be expanded, for example, if we want to report a failure like no | |
628 | * memory etc. return 2, and return 1 for expected cases (like broken | |
629 | * device found) that do not print an id. | |
630 | */ | |
631 | static int scsi_id(const char *target_path, char *maj_min_dev) | |
632 | { | |
633 | int retval; | |
634 | int dev_type = 0; | |
635 | char full_dev_path[MAX_NAME_LEN]; | |
636 | char serial[MAX_SERIAL_LEN]; | |
637 | struct sysfs_class_device *scsi_dev; /* of scsi_device full_dev_path */ | |
638 | int good_dev; | |
639 | int page_code; | |
640 | char callout[MAX_NAME_LEN]; | |
641 | ||
642 | dprintf("target_path %s\n", target_path); | |
643 | ||
644 | /* | |
645 | * Ugly: depend on the sysfs path to tell us whether this is a | |
646 | * block or char device. This should probably be encoded in the | |
647 | * "dev" along with the major/minor. | |
648 | */ | |
649 | if (has_sysfs_prefix(target_path, "/block")) { | |
650 | dev_type = S_IFBLK; | |
651 | } else if (has_sysfs_prefix(target_path, "/class")) { | |
652 | dev_type = S_IFCHR; | |
653 | } else { | |
654 | if (!hotplug_mode) { | |
655 | log_message(LOG_WARNING, | |
656 | "Non block or class device '%s'\n", | |
657 | target_path); | |
658 | return 1; | |
659 | } else { | |
660 | /* | |
661 | * Expected in some cases. | |
662 | */ | |
663 | dprintf("Non block or class device\n"); | |
664 | return 0; | |
665 | } | |
666 | } | |
667 | ||
668 | if (sysfs_get_actual_dev(target_path, full_dev_path, MAX_NAME_LEN)) | |
669 | return 1; | |
670 | ||
671 | dprintf("full_dev_path %s\n", full_dev_path); | |
672 | ||
673 | /* | |
674 | * Allow only scsi devices (those that have a matching device | |
675 | * under /bus/scsi/devices). | |
676 | * | |
677 | * Other block devices can support SG IO, but only ide-cd does, so | |
678 | * for now, don't bother with anything else. | |
679 | */ | |
680 | retval = sysfs_is_bus(full_dev_path, "scsi"); | |
681 | if (retval == 0) { | |
682 | if (hotplug_mode) | |
683 | /* | |
684 | * Expected in some cases. | |
685 | */ | |
686 | dprintf("%s is not a scsi device\n", target_path); | |
687 | else | |
688 | log_message(LOG_WARNING, "%s is not a scsi device\n", | |
689 | target_path); | |
690 | return 1; | |
691 | } else if (retval < 0) { | |
692 | log_message(LOG_WARNING, "sysfs_is_bus failed: %s\n", | |
693 | strerror(errno)); | |
694 | return 1; | |
695 | } | |
696 | ||
697 | /* | |
698 | * mknod a temp dev to communicate with the device. | |
699 | */ | |
700 | if (!dev_specified && create_tmp_dev(target_path, maj_min_dev, | |
701 | dev_type)) { | |
702 | dprintf("create_tmp_dev failed\n"); | |
703 | return 1; | |
704 | } | |
705 | ||
706 | scsi_dev = sysfs_open_class_device(full_dev_path); | |
707 | if (!scsi_dev) { | |
708 | log_message(LOG_WARNING, "open class %s failed: %s\n", | |
709 | full_dev_path, strerror(errno)); | |
710 | return 1; | |
711 | } | |
712 | ||
713 | /* | |
714 | * Get any per device (vendor + model) options from the config | |
715 | * file. | |
716 | */ | |
717 | retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout); | |
718 | dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n", | |
719 | good_dev, page_code, callout); | |
720 | ||
721 | if (!good_dev) { | |
722 | retval = 1; | |
723 | } else if (callout[0] != '\0') { | |
724 | /* | |
725 | * exec vendor callout, pass it only the "name" to be used | |
726 | * for error messages, and the dev to open. | |
727 | * | |
728 | * This won't work if we need to pass on the original | |
729 | * command line (when not hotplug mode) since the option | |
730 | * parsing and per dev parsing modify the argv's. | |
731 | * | |
732 | * XXX Not implemented yet. And not fully tested ;-) | |
733 | */ | |
734 | retval = 1; | |
735 | } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code, | |
736 | serial, MAX_SERIAL_LEN)) { | |
737 | retval = 1; | |
738 | } else { | |
739 | retval = 0; | |
740 | } | |
741 | if (!retval) { | |
742 | if (display_bus_id) | |
743 | printf("%s ", scsi_dev->name); | |
744 | printf("%s", serial); | |
745 | if (!hotplug_mode) | |
746 | printf("\n"); | |
747 | dprintf("%s\n", serial); | |
748 | retval = 0; | |
749 | } | |
750 | fflush(stdout); | |
751 | sysfs_close_class_device(scsi_dev); | |
752 | ||
753 | if (!dev_specified) | |
754 | unlink(maj_min_dev); | |
755 | ||
756 | return retval; | |
757 | } | |
758 | ||
759 | int main(int argc, char **argv) | |
760 | { | |
761 | int retval; | |
762 | char *devpath; | |
763 | char target_path[MAX_NAME_LEN]; | |
764 | char maj_min_dev[MAX_NAME_LEN]; | |
765 | int newargc; | |
766 | char **newargv; | |
767 | ||
768 | if (getenv("DEBUG")) | |
769 | debug++; | |
770 | ||
771 | if ((argc == 2) && (argv[1][0] != '-')) { | |
772 | hotplug_mode = 1; | |
773 | dprintf("hotplug assumed\n"); | |
774 | } | |
775 | ||
776 | dprintf("argc is %d\n", argc); | |
777 | if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) { | |
778 | log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n", | |
779 | strerror(errno)); | |
780 | exit(1); | |
781 | } | |
782 | ||
783 | if (hotplug_mode) { | |
784 | /* | |
785 | * There is a kernel race creating attributes, if called | |
786 | * directly, uncomment the sleep. | |
787 | */ | |
788 | /* sleep(1); */ | |
789 | ||
790 | devpath = getenv("DEVPATH"); | |
791 | if (!devpath) { | |
792 | log_message(LOG_WARNING, "DEVPATH is not set\n"); | |
793 | exit(1); | |
794 | } | |
795 | sys_specified = 1; | |
796 | ||
797 | strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN); | |
798 | strncat(target_path, devpath, MAX_NAME_LEN); | |
799 | } else { | |
800 | if (set_options(argc, argv, short_options, long_options, | |
801 | target_path, maj_min_dev) < 0) | |
802 | exit(1); | |
803 | } | |
804 | ||
805 | /* | |
806 | * Override any command line options set via the config file. This | |
807 | * is the only way to set options when in hotplug mode. | |
808 | */ | |
809 | newargv = NULL; | |
810 | retval = get_file_options(NULL, NULL, &newargc, &newargv); | |
811 | if (retval < 0) { | |
812 | exit(1); | |
813 | } else if (newargv && (retval == 0)) { | |
814 | if (set_options(newargc, newargv, short_options, long_options, | |
815 | target_path, maj_min_dev) < 0) | |
816 | exit(1); | |
817 | free(newargv); | |
818 | } | |
819 | ||
820 | if (!sys_specified) { | |
821 | log_message(LOG_WARNING, "-s must be specified\n"); | |
822 | exit(1); | |
823 | } | |
824 | ||
825 | retval = scsi_id(target_path, maj_min_dev); | |
826 | exit(retval); | |
827 | } |