[evlog-dev] Socket freeing
Benoit Guillon <[email protected]> Wed, 14 Apr 2004 11:38:50 +0200
| Newsgroups | gmane.linux.kernel.event-logging |
|---|---|
| Message-ID | <[email protected]> |
--------------3EB32DC20EF423B97A3C0D88
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: Quoted-Printable
Hello,
I suggest the following patch in order to systematically free unused
sockets when connection to deamon fails.
--- posix2.c.ref Wed Apr 14 10:41:01 2004
+++ posix2.c.new Wed Apr 14 11:23:35 2004
@@ -587,7 +587,7 @@
if ((n =3D connect(sd, (struct sockaddr *)sa, sock_len) < 0)) {
if (errno !=3D EINPROGRESS) {
// (void)fprintf(stderr, "Failed to connect to the daemon.
errno=3D%d.\n", errno);
- return -EAGAIN;
+ goto err_connect;
}
}
@@ -603,8 +603,7 @@
if ((n =3D select(sd + 1, &rset, &wset, NULL, nsec? &tval : NULL)) =3D=3D=
0)
{
/* time out */
- close(sd);
- return -EAGAIN;
+ goto err_connect;
}
if (FD_ISSET(sd, &rset) || FD_ISSET(sd, &wset)) {
@@ -612,10 +611,10 @@
if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
/* some error */
(void)fprintf(stderr, "Error connecting to the daemon.\n");
- return -EAGAIN;
+ goto err_connect;
}
} else {
- return -EAGAIN;
+ goto err_connect;
}
done:
@@ -625,9 +624,13 @@
flags |=3D FD_CLOEXEC;
if (fcntl(sd, F_SETFD, flags) =3D=3D -1) {
perror("fcntl(F_SETFD)");
- return -EAGAIN;
+ goto err_connect;
}
return sd;
+
+err_connect:
+ close(sd);
+ return -EAGAIN;
}
int
Bye,
--
Beno=EEt Guillon [email protected]
--------------3EB32DC20EF423B97A3C0D88
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7Bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hello,
<p>I suggest the following patch in order to systematically free unused
sockets when connection to deamon fails.
<p><tt>--- posix2.c.ref Wed Apr 14 10:41:01 2004</tt>
<br><tt>+++ posix2.c.new Wed Apr 14 11:23:35 2004</tt>
<br><tt>@@ -587,7 +587,7 @@</tt>
<br><tt> if ((n = connect(sd, (struct sockaddr *)sa, sock_len) <
0)) {</tt>
<br><tt> if (errno != EINPROGRESS) {</tt>
<br><tt> // (void)fprintf(stderr, "Failed to connect to
the daemon. errno=%d.\n", errno);</tt>
<br><tt>- return -EAGAIN;</tt>
<br><tt>+ goto err_connect;</tt>
<br><tt> }</tt>
<br><tt> }</tt>
<p><tt>@@ -603,8 +603,7 @@</tt>
<p><tt> if ((n = select(sd + 1, &rset, &wset, NULL, nsec?
&tval : NULL)) == 0) {</tt>
<br><tt> /* time out */</tt>
<br><tt>- close(sd);</tt>
<br><tt>- return -EAGAIN;</tt>
<br><tt>+ goto err_connect;</tt>
<br><tt> }</tt>
<p><tt> if (FD_ISSET(sd, &rset) || FD_ISSET(sd, &wset)) {</tt>
<br><tt>@@ -612,10 +611,10 @@</tt>
<br><tt> if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &error,
&len) < 0) {</tt>
<br><tt> /* some error */</tt>
<br><tt> (void)fprintf(stderr, "Error connecting to the
daemon.\n");</tt>
<br><tt>- return -EAGAIN;</tt>
<br><tt>+ goto err_connect;</tt>
<br><tt> }</tt>
<br><tt> } else {</tt>
<br><tt>- return -EAGAIN;</tt>
<br><tt>+ goto err_connect;</tt>
<br><tt> }</tt>
<p><tt> done:</tt>
<br><tt>@@ -625,9 +624,13 @@</tt>
<br><tt> flags |= FD_CLOEXEC;</tt>
<br><tt> if (fcntl(sd, F_SETFD, flags) == -1) {</tt>
<br><tt> perror("fcntl(F_SETFD)");</tt>
<br><tt>- return -EAGAIN;</tt>
<br><tt>+ goto err_connect;</tt>
<br><tt> }</tt>
<br><tt> return sd;</tt>
<br><tt>+</tt>
<br><tt>+err_connect:</tt>
<br><tt>+ close(sd);</tt>
<br><tt>+ return -EAGAIN;</tt>
<br><tt> }</tt>
<br><tt> </tt>
<br><tt> int</tt>
<p>Bye,
<pre>--
Benoît Guillon [email protected]</pre>
</html>
--------------3EB32DC20EF423B97A3C0D88--
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click