NetBSD Problem Report #48292

From www@NetBSD.org  Tue Oct  8 21:23:13 2013
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [149.20.53.66])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 55203710DF
	for <gnats-bugs@gnats.NetBSD.org>; Tue,  8 Oct 2013 21:23:13 +0000 (UTC)
Message-Id: <20131008212311.7BD4E7257D@mollari.NetBSD.org>
Date: Tue,  8 Oct 2013 21:23:11 +0000 (UTC)
From: justin@specialbusservice.com
Reply-To: justin@specialbusservice.com
To: gnats-bugs@NetBSD.org
Subject: paccept creates sockets that cannot be made blocking
X-Send-Pr-Version: www-1.0

>Number:         48292
>Category:       kern
>Synopsis:       paccept creates sockets that cannot be made blocking
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Oct 08 21:25:00 +0000 2013
>Last-Modified:  Sat Oct 12 15:40:00 +0000 2013
>Originator:     Justin Cormack
>Release:        6.1 (and revent cvs)
>Organization:
>Environment:
NetBSD netbsd32.myriabit.eu 6.1 NetBSD 6.1 (XEN3PAE_DOMU) i386
>Description:
sockets created with paccept(2) when the parent socket is non blocking remain nonblocking even when set explicitly to blocking with fcntl. They report that they are blocking but still return EAGAIN. This does not happen with accept(2).

>How-To-Repeat:
/* Example code. Should block; does with accept not paccept. */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char **argv) {

  int ss, cs, as;
  int ok, fl, n;
  int addrlen = sizeof(struct sockaddr_in);
  char buf[10];
  struct sockaddr_in sa = {.sin_family = AF_INET, .sin_port = htons(0), .sin_addr = htonl(INADDR_LOOPBACK)};
  struct sockaddr_in ba;

  ss = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  if (ss == -1) {perror("socket"); return 1;}
  ok = bind(ss, (struct sockaddr *)&sa, sizeof(sa));
  if (ok == -1) {perror("bind"); return 1;}
  ok = getsockname(ss, (struct sockaddr *)&ba, &addrlen);
  if (ok == -1) {perror("getsockname"); return 1;}
  ok = listen(ss, 128);
  if (ok == -1) {perror("listen"); return 1;}
  cs = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  if (cs == -1) {perror("socket"); return 1;}
  /* may not connect first time */
  ok = connect(cs, (struct sockaddr *)&ba, addrlen);
  /* may get EAGAIN here */
  //as = accept(ss, NULL, NULL);
  as = paccept(ss, NULL, NULL, NULL, 0);
  ok = connect(cs, (struct sockaddr *)&ba, addrlen);
  if (ok == -1 && errno != EISCONN) {printf("both connects failed\n"); return 1;}

  fl = fcntl(ss, F_GETFL, 0);
  if (fl == -1) {perror("fnctl getfl"); return 1;}
  ok = fcntl(ss, F_SETFL, fl & ~O_NONBLOCK);
  if (ok == -1) {perror("fnctl setfl"); return 1;}

  if (as == -1) { /* not true under NetBSD */
  //as = accept(ss, NULL, NULL);
  as = paccept(ss, NULL, NULL, NULL, 0);
    if (as == -1) {perror("accept/paccept"); return 1;}
  }

 /* this has no effect, not possible to force blocking, same issue if removed */
  fl = fcntl(as, F_GETFL, 0);
  if (fl == -1) {perror("fnctl getfl"); return 1;}
  ok = fcntl(as, F_SETFL, fl & ~O_NONBLOCK);
  if (ok == -1) {perror("fnctl setfl"); return 1;}

  fl = fcntl(as, F_GETFL, 0);
  if (fl & O_NONBLOCK) {printf("accepted sock nonblocking, something wrong\n");}
  n = read(as, buf, 10);
  if (n == -1) {perror("read"); return 1;}

  return 0;
}

>Fix:
its in do_sys_accept I presume. The difference is clrflags which should clear nonblock in child if not set in flags. The code looks ok, but there must be an issue.

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/48292 CVS commit: src/sys/kern
Date: Wed, 9 Oct 2013 14:55:56 -0400

 Module Name:	src
 Committed By:	christos
 Date:		Wed Oct  9 18:55:56 UTC 2013

 Modified Files:
 	src/sys/kern: uipc_syscalls.c

 Log Message:
 PR/48292: Justin Cormack: paccept creates sockets that cannot be made blocking
 Reset the socket flags not just the file flags for non-blocking I/O.
 XXX: pullup 6


 To generate a diff of this commit:
 cvs rdiff -u -r1.163 -r1.164 src/sys/kern/uipc_syscalls.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: Justin Cormack <justin@specialbusservice.com>
To: gnats-bugs@netbsd.org
Cc: kern-bug-people@netbsd.org, gnats-admin@netbsd.org, netbsd-bugs@netbsd.org
Subject: Re: PR/48292 CVS commit: src/sys/kern
Date: Sat, 12 Oct 2013 11:13:17 +0100

 On Wed, Oct 9, 2013 at 8:00 PM, Christos Zoulas <christos@netbsd.org> wrote:
 > The following reply was made to PR kern/48292; it has been noted by GNATS.
 >
 > From: "Christos Zoulas" <christos@netbsd.org>
 > To: gnats-bugs@gnats.NetBSD.org
 > Cc:
 > Subject: PR/48292 CVS commit: src/sys/kern
 > Date: Wed, 9 Oct 2013 14:55:56 -0400
 >
 >  Module Name:   src
 >  Committed By:  christos
 >  Date:          Wed Oct  9 18:55:56 UTC 2013
 >
 >  Modified Files:
 >         src/sys/kern: uipc_syscalls.c
 >
 >  Log Message:
 >  PR/48292: Justin Cormack: paccept creates sockets that cannot be made blocking
 >  Reset the socket flags not just the file flags for non-blocking I/O.
 >  XXX: pullup 6
 >
 >
 >  To generate a diff of this commit:
 >  cvs rdiff -u -r1.163 -r1.164 src/sys/kern/uipc_syscalls.c

 Thanks, I have tested this and this does fix the issue.

 (this came up from the test suite of my NetBSD Lua bindings
 https://github.com/justincormack/ljsyscall BTW).

 Justin

From: christos@zoulas.com (Christos Zoulas)
To: Justin Cormack <justin@specialbusservice.com>, gnats-bugs@netbsd.org
Cc: kern-bug-people@netbsd.org, gnats-admin@netbsd.org, 
	netbsd-bugs@netbsd.org
Subject: Re: PR/48292 CVS commit: src/sys/kern
Date: Sat, 12 Oct 2013 11:36:51 -0400

 On Oct 12, 11:13am, justin@specialbusservice.com (Justin Cormack) wrote:
 -- Subject: Re: PR/48292 CVS commit: src/sys/kern

 | Thanks, I have tested this and this does fix the issue.
 | 
 | (this came up from the test suite of my NetBSD Lua bindings
 | https://github.com/justincormack/ljsyscall BTW).

 Great, and I've added your sample test to our test suite.

 christos

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.