1/*	$NetBSD$	*/
2
3/*++
4/* NAME
5/*	sane_socketpair 3
6/* SUMMARY
7/*	sanitize socketpair() error returns
8/* SYNOPSIS
9/*	#include <sane_socketpair.h>
10/*
11/*	int	sane_socketpair(domain, type, protocol, result)
12/*	int	domain;
13/*	int	type;
14/*	int	protocol;
15/*	int	*result;
16/* DESCRIPTION
17/*	sane_socketpair() implements the socketpair(2) socket call, and
18/*	skips over silly error results such as EINTR.
19/* BUGS
20/*	Bizarre systems may have other harmless error results. Such
21/*	systems encourage programmers to ignore error results, and
22/*	penalize programmers who code defensively.
23/* LICENSE
24/* .ad
25/* .fi
26/*	The Secure Mailer license must be distributed with this software.
27/* AUTHOR(S)
28/*	Wietse Venema
29/*	IBM T.J. Watson Research
30/*	P.O. Box 704
31/*	Yorktown Heights, NY 10598, USA
32/*--*/
33
34/* System library. */
35
36#include "sys_defs.h"
37#include <sys/socket.h>
38#include <unistd.h>
39#include <errno.h>
40
41/* Utility library. */
42
43#include "msg.h"
44#include "sane_socketpair.h"
45
46/* sane_socketpair - sanitize socketpair() error returns */
47
48int     sane_socketpair(int domain, int type, int protocol, int *result)
49{
50    static int socketpair_ok_errors[] = {
51	EINTR,
52	0,
53    };
54    int     count;
55    int     err;
56    int     ret;
57
58    /*
59     * Solaris socketpair() can fail with EINTR.
60     */
61    while ((ret = socketpair(domain, type, protocol, result)) < 0) {
62	for (count = 0; /* void */ ; count++) {
63	    if ((err = socketpair_ok_errors[count]) == 0)
64		return (ret);
65	    if (errno == err) {
66		msg_warn("socketpair: %m (trying again)");
67		sleep(1);
68		break;
69	    }
70	}
71    }
72    return (ret);
73}
74