From: Alastair B. <nye...@li...> - 2008-06-28 02:57:19
|
Julian Stecklina wrote: > Hello, > > I tried to update StumpWMs battery contrib to use Linux' sysfs and hit > the following issue: > >> (with-open-file (in "/sys/class/power_supply/AC/type") > (let ((buf (make-string 32 :initial-element #\Space))) > (read-sequence buf in) > buf)) > > READ-SEQUENCE blocks and does not return. > /sys/class/power_supply/AC/type only contains the null-terminated string > "Mains". Interrupting with C-c gives the following backtrace: [ Backtrace elided ] > Opening the file with :element-type '(unsigned-byte 8) does not work > either. > > This is with SBCL 1.0.16 on Gentoo and I am running Linux 2.6.25.4 on an > x86 box. > > Regards, What I expect is happening here is that the Linux kernel is using the "data ready for reading" notification as a "file contents changed" notification. This, of course, breaks SBCL's assumption of straight POSIX semantics for select() on files. I ran into the same problem with /proc/bus/usb/devices. The following (untested) nasty hack (based on what I ended up doing) might behave approximately the way you want: (with-open-file (in "/sys/class/power_supply/AC/type") (let ((buf (make-array 32 :initial-element (char-code #\Space)))) ;; FIXME: Add error handling here. (sb-unix:unix-read (sb-sys:fd-stream-fd in) (sb-sys:vector-sap buf) 32) (sb-ext:octets-to-string buf))) Note that this would mean that you can hold a stream open to a file that behaves this way, register an fd-handler on it for input, and recieve notification of when the file changes via serve-event. Might be useful for detecting battery level changes and AC adaptor connect/disconnect. The only problem is that you can't use the standard Lisp stream I/O functions to read the file data. The only way that I can think of to have SBCL do "The Right Thing" with files that behave in this manner is for there to be some method whereby it can determine for itself if a file descriptor actually has this particular set of brain-damaged semantics ahead of time, and raise end-of-file instead of entering wait-until-fd-usable on such files. I don't know of such a method, but that's more indicative of my not having looked for one than anything else. --Alastair Bridgewater |