Example #1
0
void __scan_ide_devices(void)
{
    FILE *proc_ide;
    gchar *device, iface, *model, *media, *pgeometry = NULL, *lgeometry = NULL;
    gint n = 0, i = 0, cache, nn = 0;
    gchar *capab = NULL, *speed = NULL, *driver = NULL, *ide_storage_list;

    /* remove old devices from global device table */
    moreinfo_del_with_prefix("DEV:IDE");

    ide_storage_list = g_strdup(_("\n[IDE Disks]\n"));

    iface = 'a';
    for (i = 0; i <= 16; i++) {
	device = g_strdup_printf("/proc/ide/hd%c/model", iface);
	if (g_file_test(device, G_FILE_TEST_EXISTS)) {
	    gchar buf[128];

	    cache = 0;

	    proc_ide = fopen(device, "r");
	    if (!proc_ide)
	        continue;

	    (void) fgets(buf, 128, proc_ide);
	    fclose(proc_ide);

	    buf[strlen(buf) - 1] = 0;

	    model = g_strdup(buf);

	    g_free(device);

	    device = g_strdup_printf("/proc/ide/hd%c/media", iface);
	    proc_ide = fopen(device, "r");
	    if (!proc_ide) {
	        free(model);
	        continue;
            }

	    (void) fgets(buf, 128, proc_ide);
	    fclose(proc_ide);
	    buf[strlen(buf) - 1] = 0;

	    media = g_strdup(buf);
	    if (g_str_equal(media, "cdrom")) {
		/* obtain cd-rom drive information from cdrecord */
		GTimer *timer;
		gchar *tmp = g_strdup_printf("cdrecord dev=/dev/hd%c -prcap 2>/dev/stdout", iface);
		FILE *prcap;

		if ((prcap = popen(tmp, "r"))) {
		    /* we need a timeout so cdrecord does not try to get information on cd drives
		       with inserted media, which is not possible currently. half second should be
		       enough. */
		    timer = g_timer_new();
		    g_timer_start(timer);

		    while (fgets(buf, 128, prcap)
			   && g_timer_elapsed(timer, NULL) < 0.5) {
			if (g_str_has_prefix(buf, "  Does")) {
			    if (g_str_has_suffix(buf, "media\n")
				&& !strstr(buf, "speed")) {
				gchar *media_type = g_strstrip(strstr(buf, "Does "));
				gchar **ttmp = g_strsplit(media_type, " ", 0);

				capab = h_strdup_cprintf("\nCan %s#%d=%s\n", capab, ttmp[1], ++nn, ttmp[2]);

				g_strfreev(ttmp);
			    } else if (strstr(buf, "Buffer-Underrun-Free")) {
				capab =
				    h_strdup_cprintf
				    ("\nSupports BurnProof=%s\n", capab, strstr(buf, "Does not") ? "No" : "Yes");
			    } else if (strstr(buf, "multi-session")) {
				capab =
				    h_strdup_cprintf
				    ("\nCan read multi-session CDs=%s\n",
				     capab, strstr(buf, "Does not") ? "No" : "Yes");
			    } else if (strstr(buf, "audio CDs")) {
				capab =
				    h_strdup_cprintf
				    ("\nCan play audio CDs=%s\n", capab, strstr(buf, "Does not") ? "No" : "Yes");
			    } else if (strstr(buf, "PREVENT/ALLOW")) {
				capab =
				    h_strdup_cprintf
				    ("\nCan lock media=%s\n", capab, strstr(buf, "Does not") ? "No" : "Yes");
			    }
			} else if ((strstr(buf, "read")
				    || strstr(buf, "write"))
				   && strstr(buf, "kB/s")) {
			    speed =
				g_strconcat(speed ? speed : "", strreplacechr(g_strstrip(buf), ":", '='), "\n", NULL);
			} else if (strstr(buf, "Device seems to be")) {
			    driver = g_strdup_printf(_("Driver=%s\n"), strchr(buf, ':') + 1);
			}
		    }

		    pclose(prcap);
		    g_timer_destroy(timer);
		}

		g_free(tmp);
	    }
	    g_free(device);

	    device = g_strdup_printf("/proc/ide/hd%c/cache", iface);
	    if (g_file_test(device, G_FILE_TEST_EXISTS)) {
		proc_ide = fopen(device, "r");
		if (proc_ide) {
                    (void) fscanf(proc_ide, "%d", &cache);
                    fclose(proc_ide);
                } else {
                    cache = 0;
                }
	    }
	    g_free(device);

	    device = g_strdup_printf("/proc/ide/hd%c/geometry", iface);
	    if (g_file_test(device, G_FILE_TEST_EXISTS)) {
		gchar *tmp;

		proc_ide = fopen(device, "r");
		if (proc_ide) {
                    (void) fgets(buf, 64, proc_ide);
                    for (tmp = buf; *tmp; tmp++) {
                        if (*tmp >= '0' && *tmp <= '9')
                            break;
                    }

                    pgeometry = g_strdup(g_strstrip(tmp));

                    (void) fgets(buf, 64, proc_ide);
                    for (tmp = buf; *tmp; tmp++) {
                        if (*tmp >= '0' && *tmp <= '9')
                            break;
                    }
                    lgeometry = g_strdup(g_strstrip(tmp));

                    fclose(proc_ide);
                } else {
                    pgeometry = g_strdup("Unknown");
                    lgeometry = g_strdup("Unknown");
                }

	    }
	    g_free(device);

	    n++;

	    gchar *devid = g_strdup_printf("IDE%d", n);

	    ide_storage_list = h_strdup_cprintf("$%s$%s=\n", ide_storage_list, devid, model);
	    storage_icons =
		h_strdup_cprintf("Icon$%s$%s=%s.png\n", storage_icons,
				 devid, model, g_str_equal(media, "cdrom") ? "cdrom" : "hdd");

	    gchar *strhash = g_strdup_printf(_("[Device Information]\n" "Model=%s\n"),
					     model);

	    const gchar *url = vendor_get_url(model);

	    if (url) {
		strhash = h_strdup_cprintf(_("Vendor=%s (%s)\n"), strhash, vendor_get_name(model), url);
	    } else {
		strhash = h_strdup_cprintf(_("Vendor=%s\n"), strhash, vendor_get_name(model));
	    }

	    strhash = h_strdup_cprintf(_("Device Name=hd%c\n"
					 "Media=%s\n" "Cache=%dkb\n"), strhash, iface, media, cache);
	    if (driver) {
		strhash = h_strdup_cprintf("%s\n", strhash, driver);

		g_free(driver);
		driver = NULL;
	    }

	    if (pgeometry && lgeometry) {
		strhash = h_strdup_cprintf(_("[Geometry]\n"
					     "Physical=%s\n" "Logical=%s\n"), strhash, pgeometry, lgeometry);

		g_free(pgeometry);
		pgeometry = NULL;
		g_free(lgeometry);
		lgeometry = NULL;
	    }

	    if (capab) {
		strhash = h_strdup_cprintf(_("[Capabilities]\n%s"), strhash, capab);

		g_free(capab);
		capab = NULL;
	    }

	    if (speed) {
		strhash = h_strdup_cprintf(_("[Speeds]\n%s"), strhash, speed);

		g_free(speed);
		speed = NULL;
	    }

	    moreinfo_add_with_prefix("DEV", devid, strhash);
	    g_free(devid);
	    g_free(model);
	} else {
	    g_free(device);
        }

	iface++;
    }

    if (n) {
	storage_list = h_strconcat(storage_list, ide_storage_list, NULL);
	g_free(ide_storage_list);
    }
}
Example #2
0
void
scan_printers_do(void)
{
    int num_dests, i, j;
    CUPSDest *dests;
    gchar *prn_id, *prn_moreinfo;

    g_free(printer_list);
    g_free(printer_icons);

    if (!cups_init) {
        init_cups();
        
        printer_icons = g_strdup("");
        printer_list = g_strdup(_("[Printers]\n"
                                "No suitable CUPS library found="));
        return;
    }

    /* remove old devices from global device table */
    moreinfo_del_with_prefix("DEV:PRN");

    num_dests = cups_dests_get(&dests);
    if (num_dests > 0) {
	printer_list = g_strdup_printf(_("[Printers (CUPS)]\n"));
        printer_icons = g_strdup("");
	for (i = 0; i < num_dests; i++) {
	    GHashTable *options;
	    
	    options = g_hash_table_new(g_str_hash, g_str_equal);
	    
	    for (j = 0; j < dests[i].num_options; j++) {
	      g_hash_table_insert(options,
	                          g_strdup(dests[i].options[j].name),
	                          g_strdup(dests[i].options[j].value));
	    }
	
            prn_id = g_strdup_printf("PRN%d", i);
	
	    printer_list = h_strdup_cprintf("\n$%s$%s=%s\n",
					    printer_list,
					    prn_id,						
					    dests[i].name,
					    dests[i].is_default ? "<i>Default</i>" : "");
            printer_icons = h_strdup_cprintf("\nIcon$%s$%s=printer.png",
                                             printer_icons,
                                             prn_id,
                                             dests[i].name);

            prn_moreinfo = g_strdup("");
            for (j = 0; j < G_N_ELEMENTS(cups_fields); j++) {
              if (!cups_fields[j].name) {
                prn_moreinfo = h_strdup_cprintf("[%s]\n",
                                                prn_moreinfo,
                                                cups_fields[j].key);
              } else {
                gchar *temp;
                
                temp = g_hash_table_lookup(options, cups_fields[j].key);
                
                if (cups_fields[j].callback) {
                  temp = cups_fields[j].callback(temp);
                } else {
                  if (temp) {
                    /* FIXME Do proper escaping */
                    temp = g_strdup(strreplacechr(temp, "&=", ' '));
                  } else {
                    temp = g_strdup(_("Unknown"));
                  }
                }
                
                prn_moreinfo = h_strdup_cprintf("%s=%s\n",
                                                prn_moreinfo,
                                                cups_fields[j].name,
                                                temp);
                
                g_free(temp);
              }
            }
            
            moreinfo_add_with_prefix("DEV", prn_id, prn_moreinfo);
            g_free(prn_id);
            g_hash_table_destroy(options);
	}
	
	cups_dests_free(num_dests, dests);
    } else {
	printer_list = g_strdup(_("[Printers]\n"
	                        "No printers found=\n"));
    }
}
Example #3
0
/* SCSI support by Pascal F.Martin <*****@*****.**> */
void __scan_scsi_devices(void)
{
    FILE *proc_scsi;
    gchar buffer[256], *buf;
    gint n = 0;
    gint scsi_controller = 0;
    gint scsi_channel = 0;
    gint scsi_id = 0 ;
    gint scsi_lun = 0;
    gchar *vendor = NULL, *revision = NULL, *model = NULL;
    gchar *scsi_storage_list;

    /* remove old devices from global device table */
    moreinfo_del_with_prefix("DEV:SCSI");

    scsi_storage_list = g_strdup(_("\n[SCSI Disks]\n"));

    int otype = 0;
    if (proc_scsi = fopen("/proc/scsi/scsi", "r")) {
        otype = 1;
    } else if (proc_scsi = popen("lsscsi -c", "r")) {
        otype = 2;
    }

    if (otype) {
        while (fgets(buffer, 256, proc_scsi)) {
            buf = g_strstrip(buffer);
            if (!strncmp(buf, "Host: scsi", 10)) {
                sscanf(buf,
                       "Host: scsi%d Channel: %d Id: %d Lun: %d",
                       &scsi_controller, &scsi_channel, &scsi_id, &scsi_lun);

                n++;
            } else if (!strncmp(buf, "Vendor: ", 8)) {
                buf[17] = '\0';
                buf[41] = '\0';
                buf[53] = '\0';

                vendor   = g_strdup(g_strstrip(buf + 8));
                model    = g_strdup_printf("%s %s", vendor, g_strstrip(buf + 24));
                revision = g_strdup(g_strstrip(buf + 46));
            } else if (!strncmp(buf, "Type:   ", 8)) {
                char *p;
                gchar *type = NULL, *icon = NULL;

                if (!(p = strstr(buf, "ANSI SCSI revision"))) {
                    p = strstr(buf, "ANSI  SCSI revision");
                }

                if (p != NULL) {
                    while (*(--p) == ' ');
                    *(++p) = 0;

                    static struct {
                        char *type;
                        char *label;
                        char *icon;
                    } type2icon[] = {
                        { "Direct-Access", "Disk", "hdd"},
                        { "Sequential-Access", "Tape", "tape"},
                        { "Printer", "Printer", "lpr"},
                        { "WORM", "CD-ROM", "cdrom"},
                        { "CD-ROM", "CD-ROM", "cdrom"},
                        { "Scanner", "Scanner", "scanner"},
                        { "Flash Disk", "USB Flash Disk", "usbfldisk" },
                        { NULL, "Generic", "scsi"}
                    };
                    int i;

                    if (model && strstr(model, "Flash Disk")) {
                      type = "Flash Disk";
                      icon = "usbfldisk";
                    } else {
                      for (i = 0; type2icon[i].type != NULL; i++)
                          if (g_str_equal(buf + 8, type2icon[i].type))
                              break;

                      type = type2icon[i].label;
                      icon = type2icon[i].icon;
                    }
                }

                gchar *devid = g_strdup_printf("SCSI%d", n);
                scsi_storage_list = h_strdup_cprintf("$%s$%s=\n", scsi_storage_list, devid, model);
                storage_icons = h_strdup_cprintf("Icon$%s$%s=%s.png\n", storage_icons, devid, model, icon);

                gchar *strhash = g_strdup_printf(_("[Device Information]\n"
                                                 "Model=%s\n"), model);

                const gchar *url = vendor_get_url(model);
                if (url) {
                  strhash = h_strdup_cprintf(_("Vendor=%s (%s)\n"),
                                             strhash,
                                             vendor_get_name(model),
                                             url);
                } else {
                  strhash = h_strdup_cprintf(_("Vendor=%s\n"),
                                             strhash,
                                             vendor_get_name(model));
                }

                strhash = h_strdup_cprintf(_("Type=%s\n"
                                           "Revision=%s\n"
                                           "[SCSI Controller]\n"
                                           "Controller=scsi%d\n"
                                           "Channel=%d\n"
                                           "ID=%d\n" "LUN=%d\n"),
                                           strhash,
                                           type,
                                           revision,
                                           scsi_controller,
                                           scsi_channel,
                                           scsi_id,
                                           scsi_lun);

                moreinfo_add_with_prefix("DEV", devid, strhash);
                g_free(devid);

                g_free(model);
                g_free(revision);
                g_free(vendor);

                scsi_controller = scsi_channel = scsi_id = scsi_lun = 0;
            }
        }
        if (otype == 1)
            fclose(proc_scsi);
        else if (otype == 2)
            pclose(proc_scsi);
    }

    if (n) {
      storage_list = h_strconcat(storage_list, scsi_storage_list, NULL);
      g_free(scsi_storage_list);
    }
}
Example #4
0
gboolean __scan_udisks2_devices(void) {
    GSList *node, *drives;
    udiskd *disk;
    gchar *udisks2_storage_list = NULL, *features = NULL, *moreinfo = NULL;
    gchar *devid, *label;
    const gchar *url, *vendor_str, *icon;
    int n = 0, i;

    static struct {
        char *media_prefix;
        char *icon;
    } media2icon[] = {
        { "thumb", "usbfldisk"},
        { "flash", "usbfldisk"},
        { "floppy", "media-floppy"},
        { "optical", "cdrom"},
        { NULL, NULL}
    };

    moreinfo_del_with_prefix("DEV:UDISKS");
    udisks2_storage_list = g_strdup(_("\n[UDisks2]\n"));

    drives = get_udisks2_all_drives_info();
    for (node = drives; node != NULL; node = node->next) {
        disk = (udiskd *)node->data;
        devid = g_strdup_printf("UDISKS%d", n++);

        if (disk->vendor && strlen(disk->vendor) > 0) {
            label = g_strdup_printf("%s %s", disk->vendor, disk->model);
            vendor_str = disk->vendor;
        }
        else{
            label = g_strdup(disk->model);
            vendor_str = disk->model;
        }

        icon = NULL;
        if (disk->media_compatibility){
            for (i = 0; media2icon[i].media_prefix != NULL; i++) {
                if (g_str_has_prefix(disk->media_compatibility,
                                     media2icon[i].media_prefix)) {
                    icon = media2icon[i].icon;
                    break;
                }
            }
        }
        if (icon == NULL && disk->ejectable && g_strcmp0(disk->connection_bus, "usb") == 0) {
            icon = "usbfldisk";
        }
        if (icon == NULL){
            icon = "hdd";
        }

        url = vendor_get_url(vendor_str);
        udisks2_storage_list = h_strdup_cprintf("$%s$%s=\n", udisks2_storage_list, devid, label);
        storage_icons = h_strdup_cprintf("Icon$%s$%s=%s.png\n", storage_icons, devid, label, icon);
        features = h_strdup_cprintf("%s", features, disk->removable ? _("Removable"): _("Fixed"));

        if (disk->ejectable) {
            features = h_strdup_cprintf(", %s", features, _("Ejectable"));
        }
        if (disk->smart_enabled) {
            features = h_strdup_cprintf(", %s", features, _("Smart monitoring"));
        }

        moreinfo = g_strdup_printf(_("[Drive Information]\n"
                                   "Model=%s\n"),
                                   label);
        if (url) {
            moreinfo = h_strdup_cprintf(_("Vendor=%s (%s)\n"),
                                         moreinfo,
                                         vendor_get_name(vendor_str),
                                         url);
        }
        else {
            moreinfo = h_strdup_cprintf(_("Vendor=%s\n"),
                                         moreinfo,
                                         vendor_get_name(vendor_str));
        }

        moreinfo = h_strdup_cprintf(_("Revision=%s\n"
                                    "Block Device=%s\n"
                                    "Serial=%s\n"
                                    "Size=%s\n"
                                    "Features=%s\n"),
                                    moreinfo,
                                    disk->revision,
                                    disk->block_dev,
                                    disk->serial,
                                    size_human_readable((gfloat) disk->size),
                                    features);

        if (disk->rotation_rate > 0) {
            moreinfo = h_strdup_cprintf(_("Rotation Rate=%d\n"), moreinfo, disk->rotation_rate);
        }
        if (disk->media_compatibility || disk->media) {
            moreinfo = h_strdup_cprintf(_("Media=%s\n"
                                        "Media compatibility=%s\n"),
                                        moreinfo,
                                        disk->media ? disk->media : _("(None)"),
                                        disk->media_compatibility ? disk->media_compatibility : _("(Unknown)"));
        }
        if (disk->connection_bus && strlen(disk->connection_bus) > 0) {
            moreinfo = h_strdup_cprintf(_("Connection bus=%s\n"), moreinfo, disk->connection_bus);
        }
        if (disk->smart_enabled) {
            moreinfo = h_strdup_cprintf(_("[Smart monitoring]\n"
                                        "Status=%s\n"
                                        "Bad Sectors=%ld\n"
                                        "Power on time=%d days %d hours\n"
                                        "Temperature=%d°C\n"),
                                        moreinfo,
                                        disk->smart_failing ? _("Failing"): _("OK"),
                                        disk->smart_bad_sectors,
                                        disk->smart_poweron/(60*60*24), (disk->smart_poweron/60/60) % 24,
                                        disk->smart_temperature);
        }

        moreinfo_add_with_prefix("DEV", devid, moreinfo);
        g_free(devid);
        g_free(features);
        g_free(label);

        features = NULL;
        moreinfo = NULL;
        devid = NULL;

        udiskd_free(disk);
    }
    g_slist_free(drives);

    if (n) {
        storage_list = h_strconcat(storage_list, udisks2_storage_list, NULL);
        g_free(udisks2_storage_list);
        return TRUE;
    }

    g_free(udisks2_storage_list);
    return FALSE;
}
Example #5
0
void __scan_usb_sysfs_add_device(gchar * endpoint, int n)
{
    gchar *manufacturer, *product, *mxpwr, *tmp, *strhash;
    gint bus, classid, vendor, prodid;
    gfloat version, speed;

    classid = h_sysfs_read_int(endpoint, "bDeviceClass");
    vendor = h_sysfs_read_int(endpoint, "idVendor");
    prodid = h_sysfs_read_int(endpoint, "idProduct");
    bus = h_sysfs_read_int(endpoint, "busnum");
    speed = h_sysfs_read_float(endpoint, "speed");
    version = h_sysfs_read_float(endpoint, "version");

    if (!(mxpwr = h_sysfs_read_string(endpoint, "bMaxPower"))) {
    	mxpwr = g_strdup("0 mA");
    }

    if (!(manufacturer = h_sysfs_read_string(endpoint, "manufacturer"))) {
    	manufacturer = g_strdup("Unknown");
    }

    if (!(product = h_sysfs_read_string(endpoint, "product"))) {
	if (classid == 9) {
	    product = g_strdup_printf("USB %.2f Hub", version);
	} else {
	    product = g_strdup_printf("Unknown USB %.2f Device (class %d)", version, classid);
	}
    }

    const gchar *url = vendor_get_url(manufacturer);
    if (url) {
	tmp = g_strdup_printf("%s (%s)", vendor_get_name(manufacturer), url);
	
	g_free(manufacturer);
	manufacturer = tmp;
    }

    tmp = g_strdup_printf("USB%d", n);
    usb_list = h_strdup_cprintf("$%s$%s=\n", usb_list, tmp, product);

    strhash = g_strdup_printf("[Device Information]\n"
			      "Product=%s\n"
			      "Manufacturer=%s\n"
			      "Speed=%.2fMbit/s\n"
			      "Max Current=%s\n"
			      "[Misc]\n"
			      "USB Version=%.2f\n"
			      "Class=0x%x\n"
			      "Vendor=0x%x\n"
			      "Product ID=0x%x\n"
			      "Bus=%d\n",
			      product,
			      manufacturer,
			      speed,
			      mxpwr,
			      version, classid, vendor, prodid, bus);

    moreinfo_add_with_prefix("DEV", tmp, strhash);
    g_free(tmp);
    g_free(manufacturer);
    g_free(product);
    g_free(mxpwr);
}
Example #6
0
void __scan_usb_lsusb_add_device(char *buffer, int bufsize, FILE *lsusb, int usb_device_number)
{
    gint bus, device, vendor_id, product_id;
    gchar *version = NULL, *product = NULL, *vendor = NULL, *dev_class = NULL, *int_class = NULL;
    gchar *max_power = NULL, *name=NULL;
    gchar *tmp, *strhash;
    long position;

	g_strstrip(buffer);
    sscanf(buffer, "Bus %d Device %d: ID %x:%x",
           &bus, &device, &vendor_id, &product_id);
	name = g_strdup (buffer + 33); 

    for (fgets(buffer, bufsize, lsusb); fgets(buffer, bufsize, lsusb); position = ftell(lsusb)) {
        g_strstrip(buffer);

        if (g_str_has_prefix(buffer, "idVendor")) {
            g_free(vendor);
            vendor = g_strdup(buffer + 26);
        } else if (g_str_has_prefix(buffer, "idProduct")) {
            g_free(product);
            product = g_strdup(buffer + 26);
        } else if (g_str_has_prefix(buffer, "MaxPower")) {
            g_free(max_power);
            max_power = g_strdup(buffer + 9);
        } else if (g_str_has_prefix(buffer, "bcdUSB")) {
            g_free(version);
            version = g_strdup(buffer + 7);
        } else if (g_str_has_prefix(buffer, "bDeviceClass")) {
            g_free(dev_class);
            dev_class = g_strdup(buffer + 14);
        } else if (g_str_has_prefix(buffer, "bInterfaceClass")) {
            g_free(int_class);
            int_class = g_strdup(buffer + 16);
        } else if (g_str_has_prefix(buffer, "Bus ")) {
            /* device separator */
            fseek(lsusb, position, SEEK_SET);
            break;
        }
    }

    if (dev_class && strstr(dev_class, "0 (Defined at Interface level)")) {
        g_free(dev_class);
        if (int_class) {
            dev_class = int_class;
        } else {
            dev_class = g_strdup("Unknown");
        }
    } else dev_class = g_strdup("Unknown");

    tmp = g_strdup_printf("USB%d", usb_device_number);
    usb_list = h_strdup_cprintf("$%s$%s=\n", usb_list, tmp, name);

    strhash = g_strdup_printf("[Device Information]\n"
			      "Product=%s\n"
			      "Manufacturer=%s\n"
			      "Max Current=%s\n"
			      "[Misc]\n"
			      "USB Version=%s\n"
			      "Class=%s\n"
			      "Vendor=0x%x\n"
			      "Product ID=0x%x\n"
			      "Bus=%d\n",
			      product   ? g_strstrip(product)   : "Unknown",
			      vendor    ? g_strstrip(vendor)    : "Unknown",
			      max_power ? g_strstrip(max_power) : "Unknown",
			      version   ? g_strstrip(version)   : "Unknown",
			      dev_class ? g_strstrip(dev_class) : "Unknown",
			      vendor_id, product_id, bus);

    moreinfo_add_with_prefix("DEV", tmp, strhash);
    g_free(vendor);
    g_free(product);
    g_free(max_power);
    g_free(dev_class);
    g_free(version);
    g_free(tmp);
    g_free(name);
}
Example #7
0
gboolean __scan_usb_procfs(void)
{
    FILE *dev;
    gchar buffer[128];
    gchar *tmp, *manuf = NULL, *product = NULL, *mxpwr;
    gint bus, level, port = 0, classid = 0, trash;
    gint vendor, prodid;
    gfloat ver, rev, speed;
    int n = 0;

    dev = fopen("/proc/bus/usb/devices", "r");
    if (!dev)
	return 0;

    if (usb_list) {
       moreinfo_del_with_prefix("DEV:USB");
	g_free(usb_list);
    }
    usb_list = g_strdup("[USB Devices]\n");

    while (fgets(buffer, 128, dev)) {
	tmp = buffer;

	switch (*tmp) {
	case 'T':
	    sscanf(tmp,
		   "T:  Bus=%d Lev=%d Prnt=%d Port=%d Cnt=%d Dev#=%d Spd=%f",
		   &bus, &level, &trash, &port, &trash, &trash, &speed);
	    break;
	case 'D':
	    sscanf(tmp, "D:  Ver=%f Cls=%x", &ver, &classid);
	    break;
	case 'P':
	    sscanf(tmp, "P:  Vendor=%x ProdID=%x Rev=%f",
		   &vendor, &prodid, &rev);
	    break;
	case 'S':
	    if (strstr(tmp, "Manufacturer=")) {
		manuf = g_strdup(strchr(tmp, '=') + 1);
		remove_linefeed(manuf);
	    } else if (strstr(tmp, "Product=")) {
		product = g_strdup(strchr(tmp, '=') + 1);
		remove_linefeed(product);
	    }
	    break;
	case 'C':
	    mxpwr = strstr(buffer, "MxPwr=") + 6;

	    tmp = g_strdup_printf("USB%d", ++n);

	    if (product && *product == '\0') {
		g_free(product);
		if (classid == 9) {
		    product = g_strdup_printf("USB %.2f Hub", ver);
		} else {
		    product =
			g_strdup_printf
			("Unknown USB %.2f Device (class %d)", ver,
			 classid);
		}
	    }

	    if (classid == 9) {	/* hub */
		usb_list = h_strdup_cprintf("[%s#%d]\n",
					    usb_list, product, n);
	    } else {		/* everything else */
		usb_list = h_strdup_cprintf("$%s$%s=\n",
					    usb_list, tmp, product);

		const gchar *url = vendor_get_url(manuf);
		if (url) {
		    gchar *tmp =
			g_strdup_printf("%s (%s)", vendor_get_name(manuf),
					url);
		    g_free(manuf);
		    manuf = tmp;
		}

		gchar *strhash = g_strdup_printf("[Device Information]\n"
						 "Product=%s\n",
						 product);
		if (manuf && strlen(manuf))
		    strhash = h_strdup_cprintf("Manufacturer=%s\n",
					       strhash, manuf);

		strhash = h_strdup_cprintf("[Port #%d]\n"
					   "Speed=%.2fMbit/s\n"
					   "Max Current=%s\n"
					   "[Misc]\n"
					   "USB Version=%.2f\n"
					   "Revision=%.2f\n"
					   "Class=0x%x\n"
					   "Vendor=0x%x\n"
					   "Product ID=0x%x\n"
					   "Bus=%d\n" "Level=%d\n",
					   strhash,
					   port, speed, mxpwr,
					   ver, rev, classid,
					   vendor, prodid, bus, level);

               moreinfo_add_with_prefix("DEV", tmp, strhash);
               g_free(tmp);
	    }

	    g_free(manuf);
	    g_free(product);
	    manuf = g_strdup("");
	    product = g_strdup("");
	    port = classid = 0;
	}
    }

    fclose(dev);

    return n > 0;
}
Example #8
0
void
scan_pci_do(void)
{
    FILE *lspci;
    gchar buffer[256], *buf, *strhash = NULL, *strdevice = NULL;
    gchar *category = NULL, *name = NULL, *icon, *lspci_path, *command_line = NULL;
    gint n = 0, x = 0;
    
    if ((lspci_path = find_program("lspci")) == NULL) {
      goto pci_error;
    } else {
      command_line = g_strdup_printf("%s -v", lspci_path);
    }
    
    if (!_pci_devices) {
      _pci_devices = g_hash_table_new(g_str_hash, g_str_equal);
    }

    buf = g_build_filename(g_get_home_dir(), ".hardinfo", "pci.ids", NULL);
    if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
      DEBUG("using system-provided PCI IDs");
      g_free(buf);
      if (!(lspci = popen(command_line, "r"))) {
        goto pci_error;
      }
    } else {
      gchar *tmp;
      
      tmp = g_strdup_printf("%s -i '%s'", command_line, buf);
      g_free(buf);
      buf = tmp;
      
      DEBUG("using updated PCI IDs (from %s)", buf);
      if (!(lspci = popen(tmp, "r"))) {
        g_free(buf);
        goto pci_error;
      } else {
        g_free(buf);
      }
    }

    while (fgets(buffer, 256, lspci)) {
	buf = g_strstrip(buffer);

	if (!strncmp(buf, "Flags", 5)) {
	    gint irq = 0, freq = 0, latency = 0, i;
	    gchar **list;
	    gboolean bus_master;

	    buf += 7;

	    bus_master = FALSE;

	    list = g_strsplit(buf, ", ", 10);
	    for (i = 0; i <= 10; i++) {
		if (!list[i])
		    break;

		if (!strncmp(list[i], "IRQ", 3))
		    sscanf(list[i], "IRQ %d", &irq);
		else if (strstr(list[i], "Mhz"))
		    sscanf(list[i], "%dMhz", &freq);
		else if (!strncmp(list[i], "bus master", 10))
		    bus_master = TRUE;
		else if (!strncmp(list[i], "latency", 7))
		    sscanf(list[i], "latency %d", &latency);
	    }
	    g_strfreev(list);

	    if (irq)
		strdevice = h_strdup_cprintf("IRQ=%d\n", strdevice, irq);
	    if (freq)
		strdevice = h_strdup_cprintf("Frequency=%dMHz\n", strdevice, freq);
	    if (latency)
		strdevice = h_strdup_cprintf("Latency=%d\n", strdevice, latency);

	    strdevice = h_strdup_cprintf("Bus Master=%s\n", strdevice, bus_master ? "Yes" : "No");
	} else if (!strncmp(buf, "Kernel modules", 14)) {
	    WALK_UNTIL(' ');
	    WALK_UNTIL(':');
	    buf++;
	    
	    strdevice = h_strdup_cprintf("Kernel modules=%s\n", strdevice, buf);
	} else if (!strncmp(buf, "Subsystem", 9)) {
	    WALK_UNTIL(' ');
	    buf++;
	    const gchar *oem_vendor_url = vendor_get_url(buf);
            if (oem_vendor_url) 
                strdevice = h_strdup_cprintf("OEM Vendor=%s (%s)\n",
                                            strdevice,
                                            vendor_get_name(buf),
                                            oem_vendor_url);
	} else if (!strncmp(buf, "Capabilities", 12)
		   && !strstr(buf, "only to root") && 
		      !strstr(buf, "access denied")) {
	    WALK_UNTIL(' ');
	    WALK_UNTIL(']');
	    buf++;
	    strdevice = h_strdup_cprintf("Capability#%d=%s\n", strdevice, ++x, buf);
	} else if (!strncmp(buf, "Memory at", 9) && strstr(buf, "[size=")) {
	    gint mem;
	    gchar unit;
	    gboolean prefetch;
	    gboolean _32bit;

	    prefetch = strstr(buf, "non-prefetchable") ? FALSE : TRUE;
	    _32bit = strstr(buf, "32-bit") ? TRUE : FALSE;

	    WALK_UNTIL('[');
	    sscanf(buf, "[size=%d%c", &mem, &unit);

	    strdevice = h_strdup_cprintf("Memory#%d=%d%cB (%s%s)\n",
					strdevice, ++x,
					mem,
					(unit == ']') ? ' ' : unit,
					_32bit ? "32-bit, " : "",
					prefetch ? "prefetchable" :
					"non-prefetchable");

	} else if (!strncmp(buf, "I/O ports at", 12)) {
	    guint io_addr, io_size;

	    sscanf(buf, "I/O ports at %x [size=%d]", &io_addr, &io_size);

	    strdevice =
		h_strdup_cprintf("I/O ports at#%d=0x%x - 0x%x\n",
				strdevice, ++x, io_addr,
				io_addr + io_size - 1);
	} else if ((buf[0] >= '0' && buf[0] <= '9') && (buf[4] == ':' || buf[2] == ':')) {
	    gint bus, device, function, domain;
	    gpointer start, end;

	    if (strdevice != NULL && strhash != NULL) {
                moreinfo_add_with_prefix("DEV", strhash, strdevice);
                g_free(strhash);
                g_free(category);
                g_free(name);
	    }

	    if (buf[4] == ':') {
		sscanf(buf, "%x:%x:%x.%d", &domain, &bus, &device, &function);
	    } else {
	    	/* lspci without domain field */
	    	sscanf(buf, "%x:%x.%x", &bus, &device, &function);
	    	domain = 0;
	    }

	    WALK_UNTIL(' ');

	    start = buf;

	    WALK_UNTIL(':');
	    end = buf + 1;
	    *buf = 0;

	    buf = start + 1;
	    category = g_strdup(buf);

	    buf = end;

            if (strstr(category, "RAM memory")) icon = "mem";
            else if (strstr(category, "Multimedia")) icon = "media";
            else if (strstr(category, "USB")) icon = "usb";
            else icon = "pci";
            
	    name = g_strdup(buf);
            g_hash_table_insert(_pci_devices,
                                g_strdup_printf("0000:%02x:%02x.%x", bus, device, function),
                                name);

	    strhash = g_strdup_printf("PCI%d", n);
	    strdevice = g_strdup_printf("[Device Information]\n"
					"Name=%s\n"
					"Class=%s\n"
					"Domain=%d\n"
					"Bus, device, function=%d, %d, %d\n",
					name, category, domain, bus,
					device, function);
            
            const gchar *url = vendor_get_url(name);
            if (url) {
                strdevice = h_strdup_cprintf("Vendor=%s (%s)\n",
                                            strdevice,
                                            vendor_get_name(name),
                                            url);
            }
            
            g_hash_table_insert(_pci_devices,
                                g_strdup_printf("0000:%02x:%02x.%x", bus, device, function),
                                g_strdup(name));
            
	    pci_list = h_strdup_cprintf("$PCI%d$%s=%s\n", pci_list, n, category, name);

	    n++;
	}
    }
    
    if (pclose(lspci)) {
pci_error:
        /* error (no pci, perhaps?) */
        pci_list = g_strconcat(pci_list, "No PCI devices found=\n", NULL);
    } else if (strhash) {
	/* insert the last device */
        moreinfo_add_with_prefix("DEV", strhash, strdevice);
        g_free(strhash);
        g_free(category);
        g_free(name);
    }
    
    g_free(lspci_path);
    g_free(command_line);
}
Example #9
0
void
scan_filesystems(void)
{
    FILE *mtab;
    gchar buf[1024];
    struct statfs sfs;
    int count = 0;

    g_free(fs_list);
    fs_list = g_strdup("");
    moreinfo_del_with_prefix("COMP:FS");

    mtab = fopen("/etc/mtab", "r");
    if (!mtab)
	return;

    while (fgets(buf, 1024, mtab)) {
	gfloat size, used, avail;
	gchar **tmp;

	tmp = g_strsplit(buf, " ", 0);
	if (!statfs(tmp[1], &sfs)) {
		gfloat use_ratio;

		size = (float) sfs.f_bsize * (float) sfs.f_blocks;
		avail = (float) sfs.f_bsize * (float) sfs.f_bavail;
		used = size - avail;

		if (size == 0.0f) {
			continue;
		}

		if (avail == 0.0f) {
			use_ratio = 100.0f;
		} else {
			use_ratio = 100.0f * (used / size);
		}

		gchar *strsize = size_human_readable(size),
		      *stravail = size_human_readable(avail),
	  	      *strused = size_human_readable(used);

		gchar *strhash;
		
		strreplacechr(tmp[0], "#", '_');
		strhash = g_strdup_printf("[%s]\n"
					  "Filesystem=%s\n"
					  "Mounted As=%s\n"
					  "Mount Point=%s\n"
					  "Size=%s\n"
					  "Used=%s\n"
					  "Available=%s\n",
					  tmp[0],
					  tmp[2],
					  strstr(tmp[3], "rw") ? "Read-Write" :
					  "Read-Only", tmp[1], strsize, strused,
					  stravail);
               gchar *key = g_strdup_printf("FS%d", ++count);
		moreinfo_add_with_prefix("COMP", key, strhash);
		g_free(key);

		fs_list = h_strdup_cprintf("$FS%d$%s=%.2f %% (%s of %s)|%s\n",
					  fs_list,
					  count, tmp[0], use_ratio, stravail, strsize, tmp[1]);

		g_free(strsize);
		g_free(stravail);
		g_free(strused);
	}
	g_strfreev(tmp);
    }

    fclose(mtab);
}