11 #include <linux/usbdevice_fs.h>
13 /* For building without an updated set of headers */
14 #ifndef USBDEVFS_DROP_PRIVILEGES
15 #define USBDEVFS_DROP_PRIVILEGES _IOW('U', 30, __u32)
16 #define USBDEVFS_CAP_DROP_PRIVILEGES 0x40
19 void drop_privileges(int fd, uint32_t mask)
23 res = ioctl(fd, USBDEVFS_DROP_PRIVILEGES, &mask);
25 printf("ERROR: USBDEVFS_DROP_PRIVILEGES returned %d\n", res);
27 printf("OK: privileges dropped!\n");
30 void reset_device(int fd)
34 res = ioctl(fd, USBDEVFS_RESET);
36 printf("OK: USBDEVFS_RESET succeeded\n");
38 printf("ERROR: reset failed! (%d - %s)\n",
39 -res, strerror(-res));
42 void claim_some_intf(int fd)
46 for (i = 0; i < 4; i++) {
47 res = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &i);
49 printf("OK: claimed if %d\n", i);
51 printf("ERROR claiming if %d (%d - %s)\n",
52 i, -res, strerror(-res));
56 int main(int argc, char *argv[])
61 fd = open(argv[1], O_RDWR);
63 printf("Failed to open file\n");
68 * check if dropping privileges is supported,
69 * bail on systems where the capability is not present
71 ioctl(fd, USBDEVFS_GET_CAPABILITIES, &caps);
72 if (!(caps & USBDEVFS_CAP_DROP_PRIVILEGES)) {
73 printf("DROP_PRIVILEGES not supported\n");
78 * Drop privileges but keep the ability to claim all
79 * free interfaces (i.e., those not used by kernel drivers)
81 drop_privileges(fd, -1U);
83 printf("Available options:\n"
85 "[1] Reset device. Should fail if device is in use\n"
86 "[2] Claim 4 interfaces. Should succeed where not in use\n"
87 "[3] Narrow interface permission mask\n"
88 "Which option shall I run?: ");
90 while (scanf("%d", &c) == 1) {
101 printf("Insert new mask: ");
103 drop_privileges(fd, mask);
106 printf("I don't recognize that\n");
109 printf("Which test shall I run next?: ");