I have introduced this on my blog, see posts circa May 8 and 9 2008.
A couple of guys have expressed an interest in the code, so attached.
I compiled it like this:
# gcc -o hotplug2stdout hotplug2stdout.c
hotplug2stdout C program
- BarryK
- Puppy Master
- Posts: 9392
- Joined: Mon 09 May 2005, 09:23
- Location: Perth, Western Australia
- Contact:
hotplug2stdout C program
- Attachments
-
- hotplug2stdout.c.gz
- (804 Bytes) Downloaded 542 times
[url]https://bkhome.org/news/[/url]
Hmm, that is really, really weird!
I've been playing with the code, and to be honest I really don't know enough about computers to tell what's going on. If we pass a load of data through the pipe * then the output seems perfectly reliable for my 4in1 card reader or my single flash disk. I sujest this might mean that the output is being buffered, and only let through in packets of a certain size (which makes sense in someways) Seems unlikely though.
Perhaps though... Haha, I love moments like this, when you hit on something whilst posting...
It would seem, that flushing the stdout buffer using fflush causes it to behave! Seems ok for the two devices I listed above. I'll attach the source code below. *crosses fingers*
*
I've been playing with the code, and to be honest I really don't know enough about computers to tell what's going on. If we pass a load of data through the pipe * then the output seems perfectly reliable for my 4in1 card reader or my single flash disk. I sujest this might mean that the output is being buffered, and only let through in packets of a certain size (which makes sense in someways) Seems unlikely though.
Perhaps though... Haha, I love moments like this, when you hit on something whilst posting...
It would seem, that flushing the stdout buffer using fflush causes it to behave! Seems ok for the two devices I listed above. I'll attach the source code below. *crosses fingers*
*
Code: Select all
printf("\nNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATIONNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATION\n");
printf("\nNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATIONNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATION\n");
@Barry: I am not really familiar with the internals and have not written any C code in the last 8 years or more. But I did compile and run the code you provided and it was really interesting to see the add@, remove@ etc coming up. I tried multiple USB's several times and it kept picking up the signal everytime. Thanks for provoking interest in people like me. . I really want to brush up my C knowledge.
Sorry, thought I'd attached it. I'll try again.
- Attachments
-
- hotplug2stdout.c.gz
- (1.03 KiB) Downloaded 498 times
Barry, futher to my many, many posts on your blog, here's a proof of concept for your consideration.
It both runs the hotplug2stdout deamon, and keeps a constant eye on a file /var/mnt. When hotplug2stdout has something to report it is passed to the while loop, and when the file /var/mnt is changed somehow (open, modified, whatever) it's contents is passed to the loop.
It's a pretty messy solution to the problem, but, as far as I can tell, avoids polling, and is similar to the solution you put on your blog. I'm wondering if it isn't worth making the updates to /var/mnt (made by (u)mount) similar in format to the kernel uevents, and putting this in a script. That way it should be pretty easy to get the output into any program that needs it.
Just an idea I've been playnig with. I hope it's of some use to you.
It both runs the hotplug2stdout deamon, and keeps a constant eye on a file /var/mnt. When hotplug2stdout has something to report it is passed to the while loop, and when the file /var/mnt is changed somehow (open, modified, whatever) it's contents is passed to the loop.
Code: Select all
( hotplug2stdout & while [ 1 ]; do inotifywait /var/mnt &>/dev/null & cat /var/mnt;done ) | while read ONE
do
echo "$ONE";
done
Just an idea I've been playnig with. I hope it's of some use to you.