summaryrefslogtreecommitdiffstats
path: root/src/util/sane_socketpair.c
blob: a889934c38e9ec471362c3e2d7d9c154922bdc43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*++
/* NAME
/*	sane_socketpair 3
/* SUMMARY
/*	sanitize socketpair() error returns
/* SYNOPSIS
/*	#include <sane_socketpair.h>
/*
/*	int	sane_socketpair(domain, type, protocol, result)
/*	int	domain;
/*	int	type;
/*	int	protocol;
/*	int	*result;
/* DESCRIPTION
/*	sane_socketpair() implements the socketpair(2) socket call, and
/*	skips over silly error results such as EINTR.
/* BUGS
/*	Bizarre systems may have other harmless error results. Such
/*	systems encourage programmers to ignore error results, and
/*	penalize programmers who code defensively.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include "sys_defs.h"
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>

/* Utility library. */

#include "msg.h"
#include "sane_socketpair.h"

/* sane_socketpair - sanitize socketpair() error returns */

int     sane_socketpair(int domain, int type, int protocol, int *result)
{
    static int socketpair_ok_errors[] = {
	EINTR,
	0,
    };
    int     count;
    int     err;
    int     ret;

    /*
     * Solaris socketpair() can fail with EINTR.
     */
    while ((ret = socketpair(domain, type, protocol, result)) < 0) {
	for (count = 0; /* void */ ; count++) {
	    if ((err = socketpair_ok_errors[count]) == 0)
		return (ret);
	    if (errno == err) {
		msg_warn("socketpair: %m (trying again)");
		sleep(1);
		break;
	    }
	}
    }
    return (ret);
}