From 7e81417530acd05125a485b7d586b0b322e26706 Mon Sep 17 00:00:00 2001
From: Guillaume Nault <g.nault@alphalink.fr>
Date: Thu, 12 Jul 2012 15:22:27 +0200
Subject: L2TP: Check for fcntl() errors in l2tp_connect()

Add error detection to ensure the FD_CLOEXEC flag gets set for
every new socket.

Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
---
 accel-pppd/ctrl/l2tp/l2tp.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/accel-pppd/ctrl/l2tp/l2tp.c b/accel-pppd/ctrl/l2tp/l2tp.c
index 469492d3..fa760487 100644
--- a/accel-pppd/ctrl/l2tp/l2tp.c
+++ b/accel-pppd/ctrl/l2tp/l2tp.c
@@ -374,6 +374,7 @@ static int l2tp_connect(struct l2tp_conn_t *conn)
 {
 	struct sockaddr_pppol2tp pppox_addr;
 	int arg = 1;
+	int flg;
 
 	memset(&pppox_addr, 0, sizeof(pppox_addr));
 	pppox_addr.sa_family = AF_PPPOX;
@@ -388,8 +389,17 @@ static int l2tp_connect(struct l2tp_conn_t *conn)
 		log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno));
 		goto out_err;
 	}
-	
-	fcntl(conn->tunnel_fd, F_SETFD, fcntl(conn->tunnel_fd, F_GETFD) | FD_CLOEXEC);
+
+	flg = fcntl(conn->tunnel_fd, F_GETFD);
+	if (flg < 0) {
+		log_ppp_error("l2tp: fcntl(F_GETFD): %s\n", strerror(errno));
+		goto out_err;
+	}
+	flg = fcntl(conn->tunnel_fd, F_SETFD, flg | FD_CLOEXEC);
+	if (flg < 0) {
+		log_ppp_error("l2tp: fcntl(F_SETFD): %s\n", strerror(errno));
+		goto out_err;
+	}
 
 	conn->ppp.fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
 	if (conn->ppp.fd < 0) {
@@ -397,7 +407,16 @@ static int l2tp_connect(struct l2tp_conn_t *conn)
 		goto out_err;
 	}
 
-	fcntl(conn->ppp.fd, F_SETFD, fcntl(conn->ppp.fd, F_GETFD) | FD_CLOEXEC);
+	flg = fcntl(conn->ppp.fd, F_GETFD);
+	if (flg < 0) {
+		log_ppp_error("l2tp: fcntl(F_GETFD): %s\n", strerror(errno));
+		goto out_err;
+	}
+	flg = fcntl(conn->ppp.fd, F_SETFD, flg | FD_CLOEXEC);
+	if (flg < 0) {
+		log_ppp_error("l2tp: fcntl(F_SETFD): %s\n", strerror(errno));
+		goto out_err;
+	}
 
 	if (connect(conn->tunnel_fd, (struct sockaddr *)&pppox_addr, sizeof(pppox_addr)) < 0) {
 		log_ppp_error("l2tp: connect(tunnel): %s\n", strerror(errno));
@@ -418,7 +437,7 @@ static int l2tp_connect(struct l2tp_conn_t *conn)
 	}
 
 	conn->ppp.chan_name = _strdup(inet_ntoa(conn->addr.sin_addr));
-	
+
 	triton_event_fire(EV_CTRL_STARTED, &conn->ppp);
 
 	if (establish_ppp(&conn->ppp))
-- 
cgit v1.2.3