chiark / gitweb /
add -x to scsi_id to export the queried values in env format
[elogind.git] / extras / scsi_id / scsi_id.c
index 507fa27766fe2d5fd02055a8d05dd3d2fff96a0a..86b84bee30e6337e4c04680d75771b98fa3003f9 100644 (file)
@@ -52,7 +52,7 @@
  * options are not supported, but other code is still left in place for
  * now.
  */
-static const char short_options[] = "bd:f:gip:s:uvV";
+static const char short_options[] = "bd:f:gip:s:uvVx";
 /*
  * Just duplicate per dev options.
  */
@@ -71,6 +71,9 @@ static int use_stderr;
 static int debug;
 static int hotplug_mode;
 static int reformat_serial;
+static int export;
+static char vendor_str[64];
+static char model_str[64];
 
 void log_message (int level, const char *format, ...)
 {
@@ -84,13 +87,8 @@ void log_message (int level, const char *format, ...)
                vfprintf(stderr, format, args);
        } else {
                static int logging_init = 0;
-               static unsigned char logname[32];
                if (!logging_init) {
-                       /*
-                        * klibc does not have LOG_PID.
-                        */
-                       snprintf(logname, 32, "scsi_id[%d]", getpid());
-                       openlog (logname, 0, LOG_DAEMON);
+                       openlog ("scsi_id", LOG_PID, LOG_DAEMON);
                        logging_init = 1;
                }
 
@@ -100,23 +98,41 @@ void log_message (int level, const char *format, ...)
        return;
 }
 
-int sysfs_get_attr(const char *devpath, const char *attr, char *value,
-                  size_t bufsize)
+static void set_str(char *to, const unsigned char *from, int count)
 {
-       char attr_path[SYSFS_PATH_MAX];
-
-       strncpy(attr_path, devpath, SYSFS_PATH_MAX);
-       strncat(attr_path, "/", SYSFS_PATH_MAX);
-       strncat(attr_path, attr,  SYSFS_PATH_MAX);
-       dprintf("%s\n", attr_path);
-       return sysfs_read_attribute_value(attr_path, value, SYSFS_NAME_LEN);
+       int i, j;
+       int len;
+
+       len = strnlen(from, count);
+       while (isspace(from[len-1]))
+               len--;
+
+       i = 0;
+       while (isspace(from[i]) && (i < len))
+               i++;
+
+       j = 0;
+       while (i < len) {
+               switch(from[i]) {
+               case '/':
+               case ' ':
+                       to[j++] = '_';
+                       break;
+               default:
+                       to[j++] = from[i];
+               }
+               i++;
+       }
+       to[j] = '\0';
 }
 
-static int get_major_minor(const char *devpath, int *maj, int *min)
+static int get_major_minor(struct sysfs_class_device *class_dev, int *maj,
+                          int *min)
 {
-       char dev_value[MAX_ATTR_LEN];
+       struct sysfs_attribute *dev_attr;
 
-       if (sysfs_get_attr(devpath, "dev", dev_value, MAX_ATTR_LEN)) {
+       dev_attr = sysfs_get_classdev_attr(class_dev, "dev");
+       if (!dev_attr) {
                /*
                 * XXX This happens a lot, since sg has no dev attr.
                 * And now sysfsutils does not set a meaningful errno
@@ -125,27 +141,28 @@ static int get_major_minor(const char *devpath, int *maj, int *min)
                 * it separately.
                 */
                log_message(LOG_DEBUG, "%s: could not get dev attribute: %s\n",
-                       devpath, strerror(errno));
+                       class_dev->name, strerror(errno));
                return -1;
        }
 
-       dprintf("dev value %s", dev_value); /* dev_value has a trailing \n */
-       if (sscanf(dev_value, "%u:%u", maj, min) != 2) {
+       dprintf("dev value %s", dev_attr->value); /* value has a trailing \n */
+       if (sscanf(dev_attr->value, "%u:%u", maj, min) != 2) {
                log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
-                           devpath);
+                           class_dev->name);
                return -1;
        }
 
        return 0;
 }
 
-static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
+static int create_tmp_dev(struct sysfs_class_device *class_dev, char *tmpdev,
+                         int dev_type)
 {
        int maj, min;
 
-       dprintf("(%s)\n", devpath);
+       dprintf("(%s)\n", class_dev->name);
 
-       if (get_major_minor(devpath, &maj, &min))
+       if (get_major_minor(class_dev, &maj, &min))
                return -1;
        snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
                 TMP_DIR, TMP_PREFIX, maj, min, getpid());
@@ -474,6 +491,10 @@ static int set_options(int argc, char **argv, const char *short_opts,
                        reformat_serial = 1;
                        break;
 
+               case 'x':
+                       export = 1;
+                       break;
+
                case 'v':
                        debug++;
                        break;
@@ -500,8 +521,7 @@ static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
        int retval;
        int newargc;
        char **newargv = NULL;
-       char vendor[MAX_ATTR_LEN];
-       char model[MAX_ATTR_LEN];
+       struct sysfs_attribute *vendor, *model;
        int option;
 
        *good_bad = all_good;
@@ -511,19 +531,24 @@ static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
        else
                callout[0] = '\0';
 
-       if (sysfs_get_attr(scsi_dev->path, "vendor", vendor, MAX_ATTR_LEN)) {
+       vendor = sysfs_get_device_attr(scsi_dev, "vendor");
+       if (!vendor) {
                log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
                            scsi_dev->name);
                return -1;
        }
+       set_str(vendor_str, vendor->value, sizeof(vendor_str)-1);
 
-       if (sysfs_get_attr(scsi_dev->path, "model", model, MAX_ATTR_LEN)) {
+       model = sysfs_get_device_attr(scsi_dev, "model");
+       if (!model) {
                log_message(LOG_WARNING, "%s: cannot get model attribute\n",
                            scsi_dev->name);
                return -1;
        }
+       set_str(model_str, model->value, sizeof(model_str)-1);
 
-       retval = get_file_options(vendor, model, &newargc, &newargv);
+       retval = get_file_options(vendor->value, model->value, &newargc,
+                                 &newargv);
 
        optind = 1; /* reset this global extern */
        while (retval == 0) {
@@ -591,7 +616,7 @@ static void format_serial(char *serial)
                        *p = '_';
                p++;
        }
-       return;
+return;
 }
 
 /*
@@ -694,10 +719,8 @@ static int scsi_id(const char *target_path, char *maj_min_dev)
 
        /*
         * mknod a temp dev to communicate with the device.
-        *
-        * XXX pass down class_dev or class_dev_parent.
         */
-       if (!dev_specified && create_tmp_dev(target_path, maj_min_dev,
+       if (!dev_specified && create_tmp_dev(class_dev, maj_min_dev,
                                             dev_type)) {
                dprintf("create_tmp_dev failed\n");
                return 1;
@@ -732,11 +755,19 @@ static int scsi_id(const char *target_path, char *maj_min_dev)
                retval = 0;
        }
        if (!retval) {
-               if (reformat_serial)
-                       format_serial(serial);
-               if (display_bus_id)
-                       printf("%s: ", scsi_dev->name);
-               printf("%s\n", serial);
+               if (export) {
+                       static char serial_str[64];
+                       printf("ID_VENDOR=%s\n", vendor_str);
+                       printf("ID_MODEL=%s\n", model_str);
+                       set_str(serial_str, serial, sizeof(serial_str));
+                       printf("ID_SERIAL=%s\n", serial_str);
+               } else {
+                       if (reformat_serial)
+                               format_serial(serial);
+                       if (display_bus_id)
+                               printf("%s: ", scsi_dev->name);
+                       printf("%s\n", serial);
+               }
                dprintf("%s\n", serial);
                retval = 0;
        }