public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Sven Eckelmann <sven@narfation.org>
Subject: [B.A.T.M.A.N.] [PATCH] alfred: Close socket after connect failure
Date: Thu,  5 Feb 2015 15:47:13 +0100	[thread overview]
Message-ID: <1423147633-13147-1-git-send-email-sven@narfation.org> (raw)

The socket is still using one file descriptor of the process when the
connection fails. This fd leak is non-problematic in the current client process
code because the client would be closed in this situation anyway and therefore
the socket would be closed when the process is stopped. But people using this
as example code may not stop the process on this error. The unclosed and unused
sockets would accumulate and the process would hit the limit for open file
descriptors.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 gpsd/alfred-gpsd.c | 2 ++
 unix_sock.c        | 2 ++
 vis/vis.c          | 2 ++
 3 files changed, 6 insertions(+)

diff --git a/gpsd/alfred-gpsd.c b/gpsd/alfred-gpsd.c
index d736ad1..4bc18ae 100644
--- a/gpsd/alfred-gpsd.c
+++ b/gpsd/alfred-gpsd.c
@@ -40,6 +40,8 @@ static int alfred_open_sock(struct globals *globals)
 
 	if (connect(globals->unix_sock, (struct sockaddr *)&addr,
 		    sizeof(addr)) < 0) {
+		close(globals->unix_sock);
+		globals->unix_sock = -1;
 		perror("can't connect to unix socket");
 		return -1;
 	}
diff --git a/unix_sock.c b/unix_sock.c
index 8a99327..693b55d 100644
--- a/unix_sock.c
+++ b/unix_sock.c
@@ -84,6 +84,8 @@ int unix_sock_open_client(struct globals *globals)
 
 	if (connect(globals->unix_sock, (struct sockaddr *)&addr,
 		    sizeof(addr)) < 0) {
+		close(globals->unix_sock);
+		globals->unix_sock = -1;
 		perror("can't connect to unix socket");
 		return -1;
 	}
diff --git a/vis/vis.c b/vis/vis.c
index 3dd5c8c..1916ea3 100644
--- a/vis/vis.c
+++ b/vis/vis.c
@@ -173,6 +173,8 @@ static int alfred_open_sock(struct globals *globals)
 
 	if (connect(globals->unix_sock, (struct sockaddr *)&addr,
 		    sizeof(addr)) < 0) {
+		close(globals->unix_sock);
+		globals->unix_sock = -1;
 		perror("can't connect to unix socket");
 		return -1;
 	}
-- 
2.1.4


             reply	other threads:[~2015-02-05 14:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-05 14:47 Sven Eckelmann [this message]
2015-02-05 18:09 ` [B.A.T.M.A.N.] [PATCH] alfred: Close socket after connect failure Simon Wunderlich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1423147633-13147-1-git-send-email-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox