From 3675016aefbe7da5613a9ec7b632b95ca840602d Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Tue, 16 Mar 2010 08:21:41 +0000 Subject: print-camera-list: print udevrules for usbdiskdirect port devices Add support for printing udev rules for usbdiskdirect port cameras (such as the st2205). Note that the udev rule applies to the /dev/sdx node instead of to the /dev/usb/.... one, as that is the one which needs to get its acl's set properly to allow users access. git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@12777 67ed7778-7388-44ab-90cf-0a291f65f57c --- packaging/generic/print-camera-list.c | 110 +++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 7 deletions(-) diff --git a/packaging/generic/print-camera-list.c b/packaging/generic/print-camera-list.c index 0d2070c52..521e1a32e 100644 --- a/packaging/generic/print-camera-list.c +++ b/packaging/generic/print-camera-list.c @@ -90,6 +90,8 @@ typedef struct { typedef int (* begin_func_t) (const func_params_t *params, void **data); +typedef int (* middle_func_t) (const func_params_t *params, + void **data); typedef int (* camera_func_t) (const func_params_t *params, const int i, const int total, @@ -386,6 +388,7 @@ typedef struct { char *script; const char *begin_string; const char *usbcam_string; + const char *usbdisk_string; } udev_persistent_data_t; @@ -397,15 +400,15 @@ udev_parse_params (const func_params_t *params, void **data) */ static const char * const begin_strings[] = { /* UDEV_PRE_0_98 */ - "BUS!=\"usb_device\", GOTO=\"libgphoto2_rules_end\"\n" - "ACTION!=\"add\", GOTO=\"libgphoto2_rules_end\"\n\n", + "ACTION!=\"add\", GOTO=\"libgphoto2_rules_end\"\n" + "BUS!=\"usb_device\", GOTO=\"libgphoto2_usb_end\"\n\n", /* UDEV_0_98 */ - "SUBSYSTEM!=\"usb|usb_device\", GOTO=\"libgphoto2_rules_end\"\n" - "ACTION!=\"add\", GOTO=\"libgphoto2_rules_end\"\n\n", + "ACTION!=\"add\", GOTO=\"libgphoto2_rules_end\"\n" + "SUBSYSTEM!=\"usb|usb_device\", GOTO=\"libgphoto2_usb_end\"\n\n", /* UDEV_136 */ - "SUBSYSTEM!=\"usb\", GOTO=\"libgphoto2_rules_end\"\n" - "ENV{DEVTYPE}!=\"usb_device\", GOTO=\"libgphoto2_rules_end\"\n" - "ACTION!=\"add\", GOTO=\"libgphoto2_rules_end\"\n\n" + "ACTION!=\"add\", GOTO=\"libgphoto2_rules_end\"\n" + "SUBSYSTEM!=\"usb\", GOTO=\"libgphoto2_usb_end\"\n" + "ENV{DEVTYPE}!=\"usb_device\", GOTO=\"libgphoto2_usb_end\"\n\n" "ENV{ID_USB_INTERFACES}==\"\", IMPORT{program}=\"usb_id --export %%p\"\n\n" }; static const char * const usbcam_strings[] = { @@ -416,6 +419,14 @@ udev_parse_params (const func_params_t *params, void **data) /* UDEV_136 */ "ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", ENV{ID_GPHOTO2}=\"1\", ENV{GPHOTO2_DRIVER}=\"proprietary\"" }; + static const char * const usbdisk_strings[] = { + /* UDEV_PRE_0_98 */ + "KERNEL==\"%s\", SYSFS{idVendor}==\"%04x\", SYSFS{idProduct}==\"%04x\"", + /* UDEV_0_98 */ + "KERNEL==\"%s\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\"", + /* UDEV_136 */ + "KERNEL==\"%s\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", ENV{ID_GPHOTO2}=\"1\", ENV{GPHOTO2_DRIVER}=\"proprietary\"" + }; udev_persistent_data_t *pdata; pdata = calloc(1, sizeof(udev_persistent_data_t)); pdata->version = UDEV_0_98; @@ -466,6 +477,7 @@ udev_parse_params (const func_params_t *params, void **data) pdata->begin_string = begin_strings[pdata->version]; pdata->usbcam_string = usbcam_strings[pdata->version]; + pdata->usbdisk_string = usbdisk_strings[pdata->version]; } @@ -495,6 +507,14 @@ udev_begin_func (const func_params_t *params, void **data) } +static int +udev_middle_func (const func_params_t *params, void *data) +{ + printf ("\nLABEL=\"libgphoto2_usb_end\"\n\n"); + return 0; +} + + static int udev_end_func (const func_params_t *params, void *data) { @@ -618,6 +638,52 @@ udev_camera_func (const func_params_t *params, } +static int +udev_camera_func2 (const func_params_t *params, + const int i, + const int total, + const CameraAbilities *a, + void *data) +{ + udev_persistent_data_t *pdata = (udev_persistent_data_t *) data; + ASSERT(pdata != NULL); + + if (a->port & GP_PORT_USB_DISK_DIRECT) { + printf (pdata->usbdisk_string, "sd[a-z]*", + a->usb_vendor, a->usb_product); + if (pdata->script != NULL || pdata->mode != NULL || pdata->owner != NULL || pdata->group != NULL) + printf(", "); + + if (pdata->script != NULL) { + printf("RUN+=\"%s\"\n", pdata->script); + } else if (pdata->mode != NULL || pdata->owner != NULL || pdata->group != NULL) { + if (pdata->mode != NULL) { + printf("MODE=\"%s\"", pdata->mode); + if (pdata->owner != NULL || pdata->group != NULL) { + printf(", "); + } + } + if (pdata->owner != NULL) { + printf("OWNER=\"%s\"", pdata->owner); + if (pdata->group != NULL) { + printf(", "); + } + } + if (pdata->group != NULL) { + printf("GROUP=\"%s\"", pdata->group); + } + printf("\n"); + } else { + printf("\n"); + if (pdata->version < UDEV_136) + FATAL("udev_camera_func(): illegal branch"); + } + } + + return 0; +} + + static int empty_begin_func (const func_params_t *params, void **data) { @@ -1055,6 +1121,8 @@ typedef struct { char *paramdescr; begin_func_t begin_func; camera_func_t camera_func; + middle_func_t middle_func; + camera_func_t camera_func2; end_func_t end_func; } output_format_t; @@ -1101,6 +1169,20 @@ iterate_camera_list (const int add_comments, } } + if (format->middle_func != NULL) { + format->middle_func(¶ms, &data); + } + + if (format->camera_func2 != NULL) { + int i; + for (i = 0; i < number_of_cameras; i++) { + ret = gp_abilities_list_get_abilities (al, i, &a); + if (ret < GP_OK) + continue; + format->camera_func2(¶ms, i, number_of_cameras, &a, data); + } + } + if (format->end_func != NULL) { format->end_func(¶ms, data); } @@ -1119,6 +1201,8 @@ static const output_format_t formats[] = { NULL, human_begin_func, human_camera_func, + NULL, + NULL, human_end_func }, {"usb-usermap", @@ -1129,6 +1213,8 @@ static const output_format_t formats[] = { "", hotplug_begin_func, hotplug_camera_func, + NULL, + NULL, empty_end_func }, {"hal-fdi", @@ -1137,6 +1223,8 @@ static const output_format_t formats[] = { NULL, fdi_begin_func, fdi_camera_func, + NULL, + NULL, fdi_end_func }, {"hal-fdi-device", @@ -1145,6 +1233,8 @@ static const output_format_t formats[] = { NULL, fdi_device_begin_func, fdi_device_camera_func, + NULL, + NULL, fdi_device_end_func }, {"udev-rules", @@ -1161,6 +1251,8 @@ static const output_format_t formats[] = { "[script |version |mode |owner |group ]*", udev_begin_func, udev_camera_func, + udev_middle_func, + udev_camera_func2, udev_end_func }, {"idlist", @@ -1169,6 +1261,8 @@ static const output_format_t formats[] = { NULL, empty_begin_func, idlist_camera_func, + NULL, + NULL, empty_end_func }, #ifdef ENABLED_GP2DDB @@ -1178,6 +1272,8 @@ static const output_format_t formats[] = { NULL, ddb_begin_func, ddb_camera_func, + NULL, + NULL, ddb_end_func }, #endif -- cgit v1.2.1