ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am currently trying to integrate avahi into some software.
For that, I need to wait for events on sockets avahi gives me.
Avahi also tells me what events to wait for, these are
POLLIN
POLLOUT
POLLERR
POLLHUP
So exactly the events "poll" deals with.
For compatibility reasons I do not want to use poll but select.
So, I put all the POLLOUT event sockets in the "writefds" and all the others in the "readfds".
Select should return on connection closed or connection error for any socket in the "readfds", correct?
But how do I determine the event type? I do I distinguish between POLLIN, POLLERR and POLLHUP without reading from the socket?
I need this to react with the correct event.
I don't know anything about avahi, but does each different event come from a different socket descriptor? If so, you can use FD_ISSET(fd, *fdset) after select() returns to find out which descriptors changed status. And thus also determine which event occurred. FD_ISSET will not read or write to the descriptor. (also see "man select" and "man select_tut").
If there are no different socket descriptors for each event types, how do they do it with polling? AFAICS it should be possible to use the same method using select().
It is possible that for one socket descriptor, different events are requestet. So I can not determine the event type by the socket .
For poll, you give arrays with the socket descriptors and the request events in this structur:
Code:
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
The events that occured is than returned in revents.
That is how they do it with polling...
This might sound ironic, but I think poll is the way to find out what you need to know. poll is as POSIX-conforming as select, so I don't see why compatibility is an issue. They're both from the same POSIX specification.
ta0kira
edit:
If the compatibility problem has to do with pollfd, remember that different implementations might use a different variable order, but you can fix that by explicit initialization using .fd =, etc.
I had a look at it again. And I think it should be possible to determine which of those 4 events have occurred when select() is used instead of poll(). But you you'll have to (try to) write to writable descriptors in order to find out which event occured (BTW you said you don't want to read from a descriptor to find out. Why not?)
If select() flags a read-only descriptor you can be sure that only (the equivalent) for POLLIN occurred since all the other 3 events are for writable descriptors only.
For writable you can find out which event occurred with the following psuedo-ish C-code (here I assumed POLLHUP is equivalent to a socket descriptor for which the other (reading) end closed the connection. I am not 100% sure of this):
But then I also -like ta0kira- have doubts about the compatibility issues you expect. In "man select_tut" I read:
Quote:
Originally Posted by man select_tut
"The poll(2) system call has the same functionality as select(), and is somewhat more efficient when monitoring sparse file descriptor sets. It is nowadays widely available, but historically was less portable than select()."
So then I think, how many OS's will be there that have historically incompatible poll() implementation but still compile/run avahi cleanly?
I would stick to poll() which seems to be much easier to use with avahi.
Last edited by Hko; 08-11-2008 at 03:02 AM.
Reason: fixed few typos
Thanks for the reply.
I do not write nor read from the sockets because these are the sockets avahi uses for its connections. And when I read or write to them I am corrupting avahi data, right?
OK, I really thought select would be more portable.
The other thing is, that I have to integrate avahi into a bunch of programs which use select. And switching to poll in all of them is just more work. But maybe I just have to .
I do not write nor read from the sockets because these are the sockets avahi uses for its connections. And when I read or write to them I am corrupting avahi data, right?
Yes. that makes sense to me now.
I did not realize you were snooping on avahi's internal communication channels. I thought you were trying to code some kind of avahi client. As I said in my first post, I hardly know anything about avahi.
you can use the poll syscall also for ur avahi deamon (if there is no compatibility issues), and select is also fine.
Now as far the guessing of events is concerned I recommends tht when select returns file descriptor set then u can check for the descriptor bits using some kinda '&' operation, like i have the following function in which I am cheking for the POLLHUP and POLLIN events to be occured on set of file descriptors.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.