From: Vsevolod Stakhov Date: Thu, 13 Oct 2011 11:32:37 +0000 (+0400) Subject: Add make_socketpair utility function. X-Git-Tag: 0.4.5~59 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=45732c640a5bb310ffc2e67b520eb008f516c899;p=thirdparty%2Frspamd.git Add make_socketpair utility function. --- diff --git a/src/util.c b/src/util.c index 277806721a..9956b8da06 100644 --- a/src/util.c +++ b/src/util.c @@ -279,6 +279,37 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gboolean is_serve return (-1); } +gint +make_socketpair (gint pair[2]) +{ + gint s_error, r, optlen, serrno, on = 1; + + r = socketpair (PF_LOCAL, SOCK_STREAM, 0, pair); + + if (r == -1) { + msg_warn ("socketpair failed: %d, '%s'", errno, strerror (errno)); + return -1; + } + /* Set close on exec */ + if (fcntl (pair[0], F_SETFD, FD_CLOEXEC) == -1) { + msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno)); + goto out; + } + if (fcntl (pair[1], F_SETFD, FD_CLOEXEC) == -1) { + msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno)); + goto out; + } + + return 0; + +out: + serrno = errno; + close (pair[0]); + close (pair[1]); + errno = serrno; + return (-1); +} + gint write_pid (struct rspamd_main *main) { diff --git a/src/util.h b/src/util.h index 3e5985d1bf..7a5a00915c 100644 --- a/src/util.h +++ b/src/util.h @@ -46,6 +46,11 @@ gint accept_from_socket (gint listen_sock, struct sockaddr *addr, socklen_t *len */ gint make_unix_socket (const gchar *, struct sockaddr_un *, gboolean is_server); +/* + * Create socketpair + */ +gint make_socketpair (gint pair[2]); + /* * Write pid to file */