public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "MarKol" <markol4@wp.pl>
To: <linux-kernel@vger.kernel.org>
Subject: Re: select for UNIX sockets?
Date: Fri, 6 Jun 2003 14:20:58 +0200	[thread overview]
Message-ID: <002f01c32c26$18c85f30$010110ac@uran238> (raw)

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


             reply	other threads:[~2003-06-06 12:04 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-06 12:20 MarKol [this message]
2003-06-07  0:14 ` select for UNIX sockets? 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='002f01c32c26$18c85f30$010110ac@uran238' \
    --to=markol4@wp.pl \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox