[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>&nbsp; if ((n = connect(sd, (struct sockaddr *)sa, sock_len) &lt;
0)) {</tt>
<br><tt>&nbsp;&nbsp; if (errno != EINPROGRESS) {</tt>
<br><tt>&nbsp;//&nbsp;&nbsp; (void)fprintf(stderr, "Failed to connect to
the daemon. errno=%d.\n", errno);</tt>
<br><tt>-&nbsp;&nbsp; return -EAGAIN;</tt>
<br><tt>+&nbsp;&nbsp; goto err_connect;</tt>
<br><tt>&nbsp;&nbsp; }</tt>
<br><tt>&nbsp; }</tt>
<p><tt>@@ -603,8 +603,7 @@</tt>
<p><tt>&nbsp; if ((n = select(sd + 1, &amp;rset, &amp;wset, NULL, nsec?
&amp;tval : NULL)) == 0) {</tt>
<br><tt>&nbsp;&nbsp; /* time out */</tt>
<br><tt>-&nbsp; close(sd);</tt>
<br><tt>-&nbsp; return -EAGAIN;</tt>
<br><tt>+&nbsp; goto err_connect;</tt>
<br><tt>&nbsp; }</tt>
<p><tt>&nbsp; if (FD_ISSET(sd, &amp;rset) || FD_ISSET(sd, &amp;wset)) {</tt>
<br><tt>@@ -612,10 +611,10 @@</tt>
<br><tt>&nbsp;&nbsp; if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &amp;error,
&amp;len) &lt; 0) {</tt>
<br><tt>&nbsp;&nbsp;&nbsp; /* some error */</tt>
<br><tt>&nbsp;&nbsp;&nbsp; (void)fprintf(stderr, "Error connecting to the
daemon.\n");</tt>
<br><tt>-&nbsp;&nbsp; return -EAGAIN;</tt>
<br><tt>+&nbsp;&nbsp; goto err_connect;</tt>
<br><tt>&nbsp;&nbsp; }</tt>
<br><tt>&nbsp; } else {</tt>
<br><tt>-&nbsp; return -EAGAIN;</tt>
<br><tt>+&nbsp; goto err_connect;</tt>
<br><tt>&nbsp; }</tt>
<p><tt>&nbsp;done:</tt>
<br><tt>@@ -625,9 +624,13 @@</tt>
<br><tt>&nbsp; flags |= FD_CLOEXEC;</tt>
<br><tt>&nbsp; if (fcntl(sd, F_SETFD, flags) == -1) {</tt>
<br><tt>&nbsp;&nbsp; perror("fcntl(F_SETFD)");</tt>
<br><tt>-&nbsp; return -EAGAIN;</tt>
<br><tt>+&nbsp; goto err_connect;</tt>
<br><tt>&nbsp; }</tt>
<br><tt>&nbsp; return sd;</tt>
<br><tt>+</tt>
<br><tt>+err_connect:</tt>
<br><tt>+&nbsp; close(sd);</tt>
<br><tt>+&nbsp; return -EAGAIN;</tt>
<br><tt>&nbsp;}</tt>
<br><tt>&nbsp;</tt>
<br><tt>&nbsp;int</tt>
<p>Bye,
<pre>--
Beno&icirc;t Guillon&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [email protected]</pre>
&nbsp;</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