linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: select for UNIX sockets?
@ 2003-06-06 12:20 MarKol
  2003-06-07  0:14 ` David Schwartz
  0 siblings, 1 reply; 30+ messages in thread
From: MarKol @ 2003-06-06 12:20 UTC (permalink / raw)
  To: linux-kernel

Hi!

Since I was an initiator of this topic on one of the Polish Linux groups
I'd like to explain some issues. We've been porting some larger piece of
software from Solaris to Linux and problem has arisen. Below is
corrected
example (with errors checking after function calls), where isolated
problem
is presented. I hope this will cut off any suggestions that some of
function
calls return errors which aren't detected and handled.

An experiment shows that there is no error occurrences while running
these
examples on Linux and sender blocks on sendto() (after sending
_successfully_
some datagrams to the receiver) when select() returns with ready to
write descriptor.

The same example works _correct_ on Solaris and QNX (sender blocks on
select() call and _never_ on sendto() ).

Question is:
Am I doing something wrong or maybe there is a bug in select() function
under Linux?



/* ---------- start: sender source ----------- */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int                 socketUn;
struct sockaddr_un  addrUn;
int                 lenUn;
char                datagram[2000];
int                 dgramCounter = 0;
fd_set              writeFdToWatch;


void sockInit(void);


int main(){
  sockInit();

  while(1) {
    FD_ZERO(&writeFdToWatch);
    FD_SET(socketUn, &writeFdToWatch);

    printf("slct:"); fflush(stdout);
    int retval = select(socketUn+1, (fd_set *)NULL,
                        &writeFdToWatch,(fd_set *)NULL,
                        (struct timeval *)NULL);

    if (retval==-1){       // select returned error
      perror("select() : "); exit(-1);
    }else if (retval==0){  // timeout or another wakeup reason
      printf("????\n"); fflush(stdout);
    }else{                 // there are ready descriptors
      if ( FD_ISSET(socketUn, &writeFdToWatch) ) {
        printf("sndt:"); fflush(stdout);
        int size = sendto(socketUn, &datagram, sizeof(datagram),
                          0, (struct sockaddr *)&addrUn, lenUn);
        if (size == -1){
          perror("sendto() : "); exit(-1);
        }else if ( size!=sizeof(datagram) ){
          perror("sendto() - size incorrect : "); exit(-1);
        }
        printf("sent - %3d\n", ++dgramCounter); fflush(stdout);
      }else{               // disaster??
        printf("????\n"); fflush(stdout);
      }
    }
  }
  return 0;
}

void sockInit(void) {
  socketUn = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (socketUn == -1){
    perror("socket() : "); exit(-1);
  }
  addrUn.sun_family = AF_UNIX;
  strcpy(addrUn.sun_path, "/tmp/tempUn");
  lenUn = strlen(addrUn.sun_path) + sizeof(addrUn.sun_family);
}
/* ---------- end: sender source ----------- */

/* ---------- start: receiver source ----------- */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int                 socketUn;
struct sockaddr_un  addrUn;
int                 lenUn;
int                 size;
char                datagram[2000];

void sockInit(void);

int main() {
  sockInit();

  while(1) {
    printf("Sleep ...\n");
    sleep(15);

    do {
      size = recvfrom(socketUn, &datagram, sizeof(datagram), 0,
                      (struct sockaddr*)NULL, (socklen_t *)NULL);
      if (size == -1){
        if (errno != EAGAIN){ // there is no data available now
          perror("recvfrom() : ");  exit(-1);
        }
        break;
      }else if ( size != sizeof datagram ){
        perror("recvfrom() - size : ");  exit(-1);
      }
      printf ("Ok:"); fflush(stdout);
    }
    while( size!=-1 );
  }
}


void sockInit(void) {
  if ( unlink("/tmp/tempUn") == -1 ){
    perror("unlink() : ");
  }

  socketUn = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (socketUn == -1){
    perror("socket() : "); exit(-1);
  }

  addrUn.sun_family = AF_UNIX;

  strcpy(addrUn.sun_path, "/tmp/tempUn");
  lenUn = strlen(addrUn.sun_path) + sizeof(addrUn.sun_family);
  if ( bind(socketUn, (struct sockaddr *)&addrUn, lenUn) == -1 ){
    perror("bind() : "); exit(-1);
  }

  if ( fcntl(socketUn, F_SETFL, O_APPEND|O_NONBLOCK) == -1 ){
    perror("fcntl() : "); exit(-1);
  }
}
/* ---------- end: receiver source ----------- */

PS.
You should run receiver before sender in order to perform this test
successfully

Regards
--
Marek Kolacz


^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: select for UNIX sockets?
@ 2003-06-04 12:19 Petr Vandrovec
  2003-06-06  0:28 ` Valdis.Kletnieks
  0 siblings, 1 reply; 30+ messages in thread
From: Petr Vandrovec @ 2003-06-04 12:19 UTC (permalink / raw)
  To: Jesse Pollard; +Cc: linux-kernel, khc

On  4 Jun 03 at 6:55, Jesse Pollard wrote:
> On Monday 02 June 2003 19:08, Krzysztof Halasa wrote:
> > Hi,
> >
> > Should something like this work correctly?
> >
> > while(1) {
> >         FD_ZERO(&set);
> >         FD_SET(fd, &set);
> >         select(FD_SETSIZE, NULL, &set, NULL, NULL); <<<<<<< for writing
> >
> >         if (FD_ISSET(fd, &set))
> >                 sendto(fd, &datagram, 1, 0, ...);
> > }
> >
> > fd is a normal local datagram socket. It looks select() returns with
> > "fd ready for write" and sendto() then blocks as the queue is full.
> >
> > I don't know if it's expected behaviour or just a not yet known bug.
> > Of course, I have a more complete test program if needed.
> >
> > 2.4.21rc6, haven't tried any other version.
> >
> > strace shows:
> >
> > select(1024, NULL, [3], NULL, NULL)     = 1 (out [3])
> > sendto(3, "\0", 1, 0, {sa_family=AF_UNIX, path="/tmp/tempUn"}, 13 <<<
> > blocks
> 
> Could. There may be room for the buffer, but unless it is set to nonblock, 
> you may have a stream open to another host that may not accept the data (busy,
> network congestion...) With the required acks, the return may (should?) be
> delayed until the ack arrives.

Besides that select() on unconnected socket is nonsense... If you'll
change code to do connect(), select(), send(), then it should work,
unless I missed something.
                                    Petr Vandrovec
                                    vandrove@vc.cvut.cz


^ permalink raw reply	[flat|nested] 30+ messages in thread
* select for UNIX sockets?
@ 2003-06-03  0:08 Krzysztof Halasa
  2003-06-03 14:51 ` Alan Cox
  2003-06-04 11:55 ` Jesse Pollard
  0 siblings, 2 replies; 30+ messages in thread
From: Krzysztof Halasa @ 2003-06-03  0:08 UTC (permalink / raw)
  To: linux-kernel

Hi,

Should something like this work correctly?

while(1) {
        FD_ZERO(&set);
        FD_SET(fd, &set);
        select(FD_SETSIZE, NULL, &set, NULL, NULL); <<<<<<< for writing

        if (FD_ISSET(fd, &set))
                sendto(fd, &datagram, 1, 0, ...);
}

fd is a normal local datagram socket. It looks select() returns with
"fd ready for write" and sendto() then blocks as the queue is full.

I don't know if it's expected behaviour or just a not yet known bug.
Of course, I have a more complete test program if needed.

2.4.21rc6, haven't tried any other version.

strace shows:

select(1024, NULL, [3], NULL, NULL)     = 1 (out [3])
sendto(3, "\0", 1, 0, {sa_family=AF_UNIX, path="/tmp/tempUn"}, 13 <<< blocks
-- 
Krzysztof Halasa
Network Administrator

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2003-06-11 22:37 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-06 12:20 select for UNIX sockets? MarKol
2003-06-07  0:14 ` David Schwartz
2003-06-08  0:04   ` Krzysztof Halasa
2003-06-09  3:11     ` David Schwartz
2003-06-09 17:18       ` Krzysztof Halasa
2003-06-09 17:55         ` David Schwartz
2003-06-09 22:24           ` Krzysztof Halasa
2003-06-10 13:34             ` Timothy Miller
2003-06-10 13:52               ` Richard B. Johnson
2003-06-10 14:21               ` Krzysztof Halasa
2003-06-10 19:04                 ` Jesse Pollard
2003-06-11 21:55                   ` Krzysztof Halasa
2003-06-11 22:50                     ` David Schwartz
2003-06-11 12:51                 ` Edgar Toernig
2003-06-10 21:40             ` David Schwartz
2003-06-11 22:04               ` Krzysztof Halasa
2003-06-09 23:45         ` James Stevenson
2003-06-08  4:15   ` Chris Friesen
2003-06-09  3:05     ` David Schwartz
2003-06-09 16:46   ` MarKol
2003-06-09 17:05     ` David Schwartz
  -- strict thread matches above, loose matches on Subject: below --
2003-06-04 12:19 Petr Vandrovec
2003-06-06  0:28 ` Valdis.Kletnieks
2003-06-06  0:38   ` Petr Vandrovec
2003-06-03  0:08 Krzysztof Halasa
2003-06-03 14:51 ` Alan Cox
2003-06-04 23:27   ` Krzysztof Halasa
2003-06-05 13:17     ` Krzysztof Halasa
2003-06-04 11:55 ` Jesse Pollard
2003-06-04 12:42   ` Krzysztof Halasa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).