From efd64da989f1b6585e5413b1b61413509ae49eaf Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sat, 7 Mar 2020 22:26:49 +0100 Subject: nusb/usbd: fix /env/usbbusy bug run the usb hub poll "work()" proc in the same filedescriptor group as the fileserver by forking the process in Srv.start callback. this also prevents the usbbusy filedescriptor from being kept open by the fileserver process. --- sys/src/cmd/nusb/usbd/dat.h | 2 ++ sys/src/cmd/nusb/usbd/hub.c | 13 ------------- sys/src/cmd/nusb/usbd/usbd.c | 46 +++++++++++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/sys/src/cmd/nusb/usbd/dat.h b/sys/src/cmd/nusb/usbd/dat.h index 821920ce3..7d1831bb6 100644 --- a/sys/src/cmd/nusb/usbd/dat.h +++ b/sys/src/cmd/nusb/usbd/dat.h @@ -122,3 +122,5 @@ struct DSSHub uchar wHubDelay[2]; uchar DeviceRemovable[1]; /* variable length */ }; + +extern Hub *hubs; diff --git a/sys/src/cmd/nusb/usbd/hub.c b/sys/src/cmd/nusb/usbd/hub.c index 22d10926a..b6d1a915b 100644 --- a/sys/src/cmd/nusb/usbd/hub.c +++ b/sys/src/cmd/nusb/usbd/hub.c @@ -670,22 +670,9 @@ dump(void) void work(void) { - char *fn; Hub *h; int i; - hubs = nil; - while((fn = rendezvous(work, nil)) != nil){ - dprint(2, "%s: %s starting\n", argv0, fn); - h = newhub(fn, nil); - if(h == nil) - fprint(2, "%s: %s: newhub failed: %r\n", argv0, fn); - free(fn); - } - - if(hubs == nil) - return; - /* * Enumerate (and acknowledge after first enumeration). * Do NOT perform enumeration concurrently for the same diff --git a/sys/src/cmd/nusb/usbd/usbd.c b/sys/src/cmd/nusb/usbd/usbd.c index 804b69277..fa56f6866 100644 --- a/sys/src/cmd/nusb/usbd/usbd.c +++ b/sys/src/cmd/nusb/usbd/usbd.c @@ -329,7 +329,17 @@ usbdflush(Req *req) respond(req, nil); } +static void +usbdstart(Srv*) +{ + switch(rfork(RFPROC|RFMEM|RFNOWAIT)){ + case -1: sysfatal("rfork: %r"); + case 0: work(); exits(nil); + } +} + Srv usbdsrv = { + .start = usbdstart, .attach = usbdattach, .walk1 = usbdwalk, .read = usbdread, @@ -447,6 +457,7 @@ void main(int argc, char **argv) { int fd, i, nd; + char *fn; Dir *d; ARGBEGIN { @@ -458,34 +469,33 @@ main(int argc, char **argv) break; } ARGEND; - busyfd = create("/env/usbbusy", ORCLOSE, 0600); quotefmtinstall(); fmtinstall('U', Ufmt); initevent(); - rfork(RFNOTEG); - switch(rfork(RFPROC|RFMEM|RFNOWAIT)){ - case -1: sysfatal("rfork: %r"); - case 0: work(); exits(nil); - } + + hubs = nil; if(argc == 0){ - if((fd = open("/dev/usb", OREAD)) < 0){ - rendezvous(work, nil); + if((fd = open("/dev/usb", OREAD)) < 0) sysfatal("/dev/usb: %r"); - } nd = dirreadall(fd, &d); close(fd); - if(nd < 2){ - rendezvous(work, nil); - sysfatal("/dev/usb: no hubs"); + for(i = 0; i < nd; i++){ + if(strcmp(d[i].name, "ctl") != 0){ + fn = smprint("/dev/usb/%s", d[i].name); + newhub(fn, nil); + free(fn); + } } - for(i = 0; i < nd; i++) - if(strcmp(d[i].name, "ctl") != 0) - rendezvous(work, smprint("/dev/usb/%s", d[i].name)); free(d); - }else + }else { for(i = 0; i < argc; i++) - rendezvous(work, estrdup9p(argv[i])); - rendezvous(work, nil); + newhub(argv[i], nil); + } + + if(hubs == nil) + sysfatal("no hubs"); + + busyfd = create("/env/usbbusy", ORCLOSE, 0600); postsharesrv(&usbdsrv, nil, "usb", "usbd"); exits(nil); } -- cgit v1.2.3