summaryrefslogtreecommitdiffstats
path: root/perl-install/c/stuff.xs.pl
diff options
context:
space:
mode:
Diffstat (limited to 'perl-install/c/stuff.xs.pl')
-rwxr-xr-xperl-install/c/stuff.xs.pl285
1 files changed, 177 insertions, 108 deletions
diff --git a/perl-install/c/stuff.xs.pl b/perl-install/c/stuff.xs.pl
index de4800529..1a148b497 100755
--- a/perl-install/c/stuff.xs.pl
+++ b/perl-install/c/stuff.xs.pl
@@ -36,17 +36,10 @@ print '
#include <net/route.h>
#include <netinet/in.h>
#include <linux/sockios.h>
+#include <linux/ethtool.h>
#include <linux/input.h>
#include <execinfo.h>
-// for ethtool structs:
-typedef unsigned long long u64;
-typedef __uint32_t u32;
-typedef __uint16_t u16;
-typedef __uint8_t u8;
-
-#include <linux/ethtool.h>
-
// for UPS on USB:
# define HID_MAX_USAGES 1024
#include <linux/hiddev.h>
@@ -97,14 +90,14 @@ void log_perror(const char *msg) {
HV* common_pciusb_hash_init(struct pciusb_entry *e) {
HV *rh = (HV *)sv_2mortal((SV *)newHV());
- hv_store(rh, "vendor", 6, newSVnv(e->vendor), 0);
- hv_store(rh, "subvendor", 9, newSVnv(e->subvendor), 0);
- hv_store(rh, "id", 2, newSVnv(e->device), 0);
- hv_store(rh, "subid", 5, newSVnv(e->subdevice), 0);
+ hv_store(rh, "vendor", 6, newSViv(e->vendor), 0);
+ hv_store(rh, "subvendor", 9, newSViv(e->subvendor), 0);
+ hv_store(rh, "id", 2, newSViv(e->device), 0);
+ hv_store(rh, "subid", 5, newSViv(e->subdevice), 0);
hv_store(rh, "driver", 6, newSVpv(e->module ? e->module : "unknown", 0), 0);
hv_store(rh, "description", 11, newSVpv(e->text, 0), 0);
- hv_store(rh, "pci_bus", 7, newSVnv(e->pci_bus), 0);
- hv_store(rh, "pci_device", 10, newSVnv(e->pci_device), 0);
+ hv_store(rh, "pci_bus", 7, newSViv(e->pci_bus), 0);
+ hv_store(rh, "pci_device", 10, newSViv(e->pci_device), 0);
return rh;
}
@@ -118,6 +111,31 @@ int length_of_space_padded(char *str, int len) {
return len;
}
+PedPartitionFlag string_to_pedpartflag(char*type) {
+ PedPartitionFlag flag = 0;
+ if (!strcmp(type, "ESP")) {
+ flag = PED_PARTITION_ESP;
+ } else if (!strcmp(type, "BIOS_GRUB")) {
+ flag = PED_PARTITION_BIOS_GRUB;
+ } else if (!strcmp(type, "LVM")) {
+ flag = PED_PARTITION_LVM;
+ } else if (!strcmp(type, "RAID")) {
+ flag = PED_PARTITION_RAID;
+ } else {
+ printf("set_partition_flag: unknown type: %s\n", type);
+ }
+ return flag;
+}
+
+int is_recovery_partition(PedPartition*part) {
+ /* FIXME: not sure everything is covered ... */
+ return ped_partition_get_flag(part, PED_PARTITION_HPSERVICE) // HP-UX service partition
+ || ped_partition_get_flag(part, PED_PARTITION_MSFT_RESERVED) // Microsoft Reserved Partition -> LDM metadata, ...
+ || ped_partition_get_flag(part, PED_PARTITION_DIAG) // ==> PARTITION_MSFT_RECOVERY (Windows Recovery Environment)
+ || ped_partition_get_flag(part, PED_PARTITION_APPLE_TV_RECOVERY)
+ || ped_partition_get_flag(part, PED_PARTITION_HIDDEN);
+}
+
MODULE = c::stuff PACKAGE = c::stuff
';
@@ -178,7 +196,7 @@ init_setlocale()
setlocale(LC_NUMERIC, "C"); /* otherwise eval "1.5" returns 1 in fr_FR for example */
char *
-setlocale(category, locale = 0)
+setlocale(category, locale = NULL)
int category
char * locale
@@ -271,20 +289,35 @@ char*
get_pci_description(int vendor_id,int device_id)
void
+hid_probe()
+ PPCODE:
+ struct hid_entries entries = hid_probe();
+ int i;
+
+ EXTEND(SP, entries.nb);
+ for (i = 0; i < entries.nb; i++) {
+ struct hid_entry *e = &entries.entries[i];
+ HV *rh = (HV *)sv_2mortal((SV *)newHV());
+ hv_store(rh, "description", 11, newSVpv(e->text, 0), 0);
+ hv_store(rh, "driver", 6, newSVpv(e->module, 0), 0);
+ PUSHs(newRV((SV *)rh));
+ }
+ hid_entries_free(&entries);
+
+void
pci_probe()
PPCODE:
struct pciusb_entries entries = pci_probe();
- char buf[2048];
int i;
EXTEND(SP, entries.nb);
for (i = 0; i < entries.nb; i++) {
struct pciusb_entry *e = &entries.entries[i];
HV * rh = common_pciusb_hash_init(e);
- hv_store(rh, "pci_domain", 10, newSVnv(e->pci_domain), 0);
- hv_store(rh, "pci_function", 12, newSVnv(e->pci_function), 0);
- hv_store(rh, "pci_revision", 12, newSVnv(e->pci_revision), 0);
- hv_store(rh, "is_pciexpress", 13, newSVnv(e->is_pciexpress), 0);
+ hv_store(rh, "pci_domain", 10, newSViv(e->pci_domain), 0);
+ hv_store(rh, "pci_function", 12, newSViv(e->pci_function), 0);
+ hv_store(rh, "pci_revision", 12, newSViv(e->pci_revision), 0);
+ hv_store(rh, "is_pciexpress", 13, newSViv(e->is_pciexpress), 0);
hv_store(rh, "nice_media_type", 15, newSVpv(e->class, 0), 0);
hv_store(rh, "media_type", 10, newSVpv(pci_class2text(e->class_id), 0), 0);
PUSHs(newRV((SV *)rh));
@@ -304,7 +337,7 @@ usb_probe()
struct usb_class_text class_text = usb_class2text(e->class_id);
snprintf(buf, sizeof(buf), "%s|%s|%s", class_text.usb_class_text, class_text.usb_sub_text, class_text.usb_prot_text);
HV * rh = common_pciusb_hash_init(e);
- hv_store(rh, "usb_port", 8, newSVnv(e->usb_port), 0);
+ hv_store(rh, "usb_port", 8, newSViv(e->usb_port), 0);
hv_store(rh, "media_type", 10, newSVpv(buf, 0), 0);
PUSHs(newRV((SV *)rh));
}
@@ -317,7 +350,6 @@ dmi_probe()
//dmidecode_file = "../../soft/ldetect-lst/test/dmidecode.Laptop.Sony-Vaio-GRX316MP";
struct dmi_entries entries = dmi_probe();
- char buf[2048];
int i;
EXTEND(SP, entries.nb);
@@ -525,7 +557,7 @@ EVIocGBitKey (char *file)
fd = open (file, O_RDONLY);
if (fd < 0) {
- perror("Cannot open /dev/input/eventX");
+ warn("Cannot open %s: %s\n", file, strerror(errno));
return;
}
@@ -578,16 +610,38 @@ get_iso_volume_ids(int fd)
print '
+TYPEMAP: <<HERE
+PedDisk* T_PTROBJ
+HERE
+
+
+
+int
+set_partition_flag(PedDisk *disk, int part_number, char * type)
+ CODE:
+ RETVAL = 0;
+ PedPartition* part = ped_disk_get_partition(disk, part_number);
+ if (!part) {
+ printf("set_partition_flag: failed to find partition\n");
+ } else {
+ PedPartitionFlag flag = string_to_pedpartflag(type);
+ if (flag) {
+ RETVAL = ped_partition_set_flag(part, flag, 1);
+ }
+ }
+ OUTPUT:
+ RETVAL
+
+
const char *
get_disk_type(char * device_path)
CODE:
PedDevice *dev = ped_device_get(device_path);
RETVAL = NULL;
if(dev) {
- PedDisk* disk = ped_disk_new(dev);
- if(disk) {
- RETVAL = disk->type->name;
- ped_disk_destroy(disk);
+ PedDiskType* type = ped_disk_probe(dev);
+ if(type) {
+ RETVAL = type->name;
}
}
OUTPUT:
@@ -599,123 +653,139 @@ get_disk_partitions(char * device_path)
PedDevice *dev = ped_device_get(device_path);
if(dev) {
PedDisk* disk = ped_disk_new(dev);
- PedPartition *part = NULL;
- if(disk)
- part = ped_disk_next_partition(disk, NULL);
+ PedPartition *part = NULL, *first_part = NULL;
+ int count = 1;
+ if(!disk)
+ return;
+ first_part = part = ped_disk_next_partition(disk, NULL);
while(part) {
- if(part->num != -1) {
- char desc[4196];
- char *path = ped_partition_get_path(part);
- sprintf(desc, "%d ", part->num);
- sprintf(desc+strlen(desc), "%s ", path);
- free(path);
- if(part->fs_type)
- strcat(desc, part->fs_type->name);
- if(part->type == 0x0)
- strcat(desc, " normal");
- else {
- if(part->type & PED_PARTITION_LOGICAL)
- strcat(desc, " logical");
- if(part->type & PED_PARTITION_EXTENDED)
- strcat(desc, " extended");
- if(part->type & PED_PARTITION_FREESPACE)
- strcat(desc, " freespace");
- if(part->type & PED_PARTITION_METADATA)
- strcat(desc, " metadata");
- if(part->type & PED_PARTITION_PROTECTED)
- strcat(desc, " protected");
- }
- sprintf(desc+strlen(desc), " (%lld,%lld,%lld)", part->geom.start, part->geom.end, part->geom.length);
- XPUSHs(sv_2mortal(newSVpv(desc, 0)));
+ part = ped_disk_next_partition(disk, part);
+ count++;
+ }
+ EXTEND(SP, count);
+ part = first_part;
+ while(part) {
+ if(part->num == -1) {
+ part = ped_disk_next_partition(disk, part);
+ continue;
+ }
+ char *path = ped_partition_get_path(part);
+ char *flag = "";
+ if (ped_partition_get_flag(part, PED_PARTITION_ESP)) {
+ flag = "ESP";
+ } else if (ped_partition_get_flag(part, PED_PARTITION_BIOS_GRUB)) {
+ flag = "BIOS_GRUB";
+ } else if (ped_partition_get_flag(part, PED_PARTITION_LVM)) {
+ flag = "LVM";
+ } else if (ped_partition_get_flag(part, PED_PARTITION_RAID)) {
+ flag = "RAID";
+ } else if (is_recovery_partition(part)) {
+ flag = "RECOVERY";
}
+ HV * rh = (HV *)sv_2mortal((SV *)newHV());
+ hv_store(rh, "part_number", 11, newSViv(part->num), 0);
+ hv_store(rh, "real_device", 11, newSVpv(path, 0), 0);
+ hv_store(rh, "start", 5, newSViv(part->geom.start), 0);
+ hv_store(rh, "size", 4, newSViv(part->geom.length), 0);
+ hv_store(rh, "pt_type", 7, newSViv(0xba), 0);
+ hv_store(rh, "flag", 4, newSVpv(flag, 0), 0);
+ free(path);
+ if(part->fs_type)
+ hv_store(rh, "fs_type", 7, newSVpv(part->fs_type->name, 0), 0);
+ PUSHs(newRV((SV *)rh));
part = ped_disk_next_partition(disk, part);
}
- if(disk)
- ped_disk_destroy(disk);
+ ped_disk_destroy(disk);
}
-int
-set_disk_type(char * device_path, const char * type_name)
+PedDisk*
+disk_open(char * device_path, const char * type_name = NULL)
CODE:
PedDevice *dev = ped_device_get(device_path);
- RETVAL = 0;
+ RETVAL = NULL;
if(dev) {
- PedDiskType* type = ped_disk_type_get(type_name);
- if(type) {
- PedDisk* disk = ped_disk_new_fresh(dev, type);
- if(disk) {
- RETVAL = ped_disk_commit(disk);
- ped_disk_destroy(disk);
+ if(type_name) {
+ PedDiskType* type = ped_disk_type_get(type_name);
+ if(type) {
+ RETVAL = ped_disk_new_fresh(dev, type);
}
+ } else {
+ RETVAL = ped_disk_new(dev);
}
}
OUTPUT:
RETVAL
int
-disk_delete_all(char * device_path)
+disk_delete_all(PedDisk* disk)
CODE:
- PedDevice *dev = ped_device_get(device_path);
RETVAL = 0;
- if(dev) {
- PedDisk* disk = ped_disk_new(dev);
- if(disk) {
- RETVAL = ped_disk_delete_all(disk);
- if(RETVAL)
- RETVAL = ped_disk_commit(disk);
- ped_disk_destroy(disk);
- }
+ if (ped_disk_delete_all(disk)) {
+ RETVAL = 1;
}
OUTPUT:
RETVAL
int
-disk_del_partition(char * device_path, int part_number)
+disk_del_partition(PedDisk* disk, int part_number)
CODE:
- PedDevice *dev = ped_device_get(device_path);
RETVAL = 0;
- if(dev) {
- PedDisk* disk = ped_disk_new(dev);
- if(disk) {
- PedPartition* part = ped_disk_get_partition(disk, part_number);
- if(!part) {
- printf("disk_del_partition: failed to find partition\n");
- } else {
- RETVAL=ped_disk_delete_partition(disk, part);
- if(RETVAL) {
- RETVAL = ped_disk_commit(disk);
- } else {
- printf("del_partition failed\n");
- }
+ PedPartition* part = ped_disk_get_partition(disk, part_number);
+ if(!part) {
+ printf("disk_del_partition: failed to find partition\n");
+ } else {
+ RETVAL = ped_disk_delete_partition(disk, part);
+ }
+ OUTPUT:
+ RETVAL
+
+int
+disk_add_partition(PedDisk* disk, double start, double length, const char * fs_type)
+ CODE:
+ RETVAL=0;
+ PedGeometry* geom = ped_geometry_new(disk->dev, (long long)start, (long long)length);
+ PedPartition* part = ped_partition_new (disk, PED_PARTITION_NORMAL, ped_file_system_type_get(fs_type), (long long)start, (long long)start+length-1);
+ PedConstraint* constraint = ped_constraint_new_from_max(geom);
+ if(!part) {
+ printf("ped_partition_new failed\n");
+ } else {
+ RETVAL = ped_disk_add_partition (disk, part, constraint);
+ }
+ ped_geometry_destroy(geom);
+ ped_constraint_destroy(constraint);
+ OUTPUT:
+ RETVAL
+
+int
+disk_commit(PedDisk *disk)
+ CODE:
+ RETVAL = 0;
+ /* As done in ped_disk_commit(), open the device here, so that the underlying
+ file descriptor is not closed between the call to ped_disk_commit_to_dev()
+ and the call to ped_disk_commit_to_os(). This avoids unwanted udev events. */
+ if (ped_device_open(disk->dev)) {
+ if (ped_disk_commit_to_dev(disk)) {
+ RETVAL = 1;
+ if (ped_disk_commit_to_os(disk)) {
+ RETVAL = 2;
}
- ped_disk_destroy(disk);
}
+ ped_device_close(disk->dev);
}
+ ped_disk_destroy(disk);
OUTPUT:
RETVAL
int
-disk_add_partition(char * device_path, double start, double length, const char * fs_type)
+tell_kernel_to_reread_partition_table(char * device_path)
CODE:
PedDevice *dev = ped_device_get(device_path);
RETVAL=0;
if(dev) {
PedDisk* disk = ped_disk_new(dev);
- if(disk) {
- PedGeometry* geom = ped_geometry_new(dev, (long long)start, (long long)length);
- PedPartition* part = ped_partition_new (disk, PED_PARTITION_NORMAL, ped_file_system_type_get(fs_type), (long long)start, (long long)start+length-1);
- PedConstraint* constraint = ped_constraint_new_from_max(geom);
- if(!part) {
- printf("ped_partition_new failed\n");
- } else
- RETVAL = ped_disk_add_partition (disk, part, constraint);
- if(RETVAL) {
- RETVAL = ped_disk_commit(disk);
- } else {
- printf("add_partition failed\n");
- }
- ped_geometry_destroy(geom);
- ped_constraint_destroy(constraint);
+ if (disk) {
+ if (ped_disk_commit_to_os (disk))
+ RETVAL=1;
ped_disk_destroy(disk);
}
}
@@ -785,4 +855,3 @@ print '
PROTOTYPES: DISABLE
';
-