[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sockets: synchronous I/O



Jens Kramel wrote:
Hi guys,

I'm writing a kernel module that uses inet-sockets and I have been looking for something like the select(2) call for some days now, but couldn't find anything. What I want is to specify several sockets and wait (using a timeout) for incoming data simultaneously.
Would be really great if someone could give me a hint (or maybe even a little code example? ;)

One way to do this is to use a waitqueue. AFAIK, this is how TUX kernel web server handles multiple sockets with multiple threads attached to a waitqueue. Another way is to use the MSG_DONTWAIT in recvmsg, and check the return value to decide what to do. A common programming idiom is (copied from tux):


read_again:
   init_sync_kiocb(&iocb, NULL);
   len = sock->sk->sk_prot->recvmsg(&iocb, sock->sk, &msg, max_size,
                       MSG_DONTWAIT, MSG_PEEK, NULL);
   if (-EIOCBQUEUED == len)
       len = wait_on_sync_kiocb(&iocb);

   if ((len == -EAGAIN) || (len == -ERESTARTSYS)) {
       if (!signal_pending(current)) {
           len = 0;
           goto out;
       }
       flush_all_signals();
       goto read_again;
   }

There might be a way to directly call do_select, but I am not sure.

--
Pradeep Padala
http://ppadala.blogspot.com

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/