Skip to content

Commit f0e6629

Browse files
David NgLinux Build Service Account
authored andcommitted
libusbhost: Skip /dev inotify if /dev/bus/usb already exists
Skip watching /dev changes if the final /dev/bus/usb already exists. Register only for /dev/bus in this scenario on systems where the last "/usb" path may be deleted (currently handled by libusbhost). Removal of inotify watch on /dev reduces runtime overhead as /dev nodes are modified frequently. Change-Id: I6823a68c3977ae01832808ae5f8c32fa913b02cf
1 parent 92c6bbb commit f0e6629

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

libusbhost/usbhost.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include <sys/ioctl.h>
3939
#include <sys/types.h>
40+
#include <sys/stat.h>
4041
#include <sys/time.h>
4142
#include <sys/inotify.h>
4243
#include <dirent.h>
@@ -189,6 +190,7 @@ int usb_host_load(struct usb_host_context *context,
189190
{
190191
int done = 0;
191192
int i;
193+
struct stat usbfsdir;
192194

193195
context->cb_added = added_cb;
194196
context->cb_removed = removed_cb;
@@ -197,17 +199,23 @@ int usb_host_load(struct usb_host_context *context,
197199
D("Created device discovery thread\n");
198200

199201
/* watch for files added and deleted within USB_FS_DIR */
202+
context->wdd = -1;
200203
context->wddbus = -1;
201204
for (i = 0; i < MAX_USBFS_WD_COUNT; i++)
202205
context->wds[i] = -1;
203206

204-
/* watch the root for new subdirectories */
205-
context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE);
206-
if (context->wdd < 0) {
207-
fprintf(stderr, "inotify_add_watch failed\n");
208-
if (discovery_done_cb)
209-
discovery_done_cb(client_data);
210-
return done;
207+
/* watch the root for new subdirectories; skip if final USB_FS_DIR
208+
path already exist
209+
*/
210+
if (stat(USB_FS_DIR, &usbfsdir) == -1) {
211+
context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE);
212+
if (context->wdd < 0)
213+
goto failed;
214+
} else {
215+
/* Final USB_FS_DIR already exists; watch base bus dir in case of bus/usb removal */
216+
context->wddbus = inotify_add_watch(context->fd, DEV_BUS_DIR, IN_CREATE | IN_DELETE);
217+
if (context->wddbus < 0)
218+
goto failed;
211219
}
212220

213221
watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
@@ -218,6 +226,12 @@ int usb_host_load(struct usb_host_context *context,
218226
done |= discovery_done_cb(client_data);
219227

220228
return done;
229+
230+
failed:
231+
fprintf(stderr, "inotify_add_watch failed\n");
232+
if (discovery_done_cb)
233+
discovery_done_cb(client_data);
234+
return done;
221235
} /* usb_host_load() */
222236

223237
int usb_host_read_event(struct usb_host_context *context)

0 commit comments

Comments
 (0)