hotplug2stdout C program

Under development: PCMCIA, wireless, etc.
Post Reply
Message
Author
User avatar
BarryK
Puppy Master
Posts: 9392
Joined: Mon 09 May 2005, 09:23
Location: Perth, Western Australia
Contact:

hotplug2stdout C program

#1 Post by BarryK »

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
Attachments
hotplug2stdout.c.gz
(804 Bytes) Downloaded 542 times
[url]https://bkhome.org/news/[/url]

User avatar
Nathan F
Posts: 1764
Joined: Wed 08 Jun 2005, 14:45
Location: Wadsworth, OH (occasionally home)
Contact:

#2 Post by Nathan F »

Thanks Barry.

Nathan
Bring on the locusts ...

Everitt
Posts: 331
Joined: Tue 19 Dec 2006, 21:59
Location: Leeds,UK or Birmingham, UK

#3 Post by Everitt »

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*

*

Code: Select all

printf("\nNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATIONNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATION\n");
    printf("\nNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATIONNICELONGSTRINGHEREASAFORMOFTESTOHGODITSSOHARDTOTYPEWITHOUTADDINGSPACESORPUNCTUATION\n");

User avatar
prit1
Posts: 542
Joined: Fri 04 Jan 2008, 00:10
Location: Los Angeles

#4 Post by prit1 »

@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.

User avatar
BarryK
Puppy Master
Posts: 9392
Joined: Mon 09 May 2005, 09:23
Location: Perth, Western Australia
Contact:

#5 Post by BarryK »

Everitt, where is your code? I'm interested in seeing it!
[url]https://bkhome.org/news/[/url]

Everitt
Posts: 331
Joined: Tue 19 Dec 2006, 21:59
Location: Leeds,UK or Birmingham, UK

#6 Post by Everitt »

Sorry, thought I'd attached it. I'll try again.
Attachments
hotplug2stdout.c.gz
(1.03 KiB) Downloaded 498 times

Everitt
Posts: 331
Joined: Tue 19 Dec 2006, 21:59
Location: Leeds,UK or Birmingham, UK

#7 Post by Everitt »

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.

Code: Select all

( hotplug2stdout & while [ 1 ]; do inotifywait /var/mnt &>/dev/null & cat /var/mnt;done ) | while read ONE
do 
  echo "$ONE"; 
done
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.

Post Reply