<br><tt><font size=2>libvir-list-bounces@redhat.com wrote on 10/22/2010
06:27:30 PM:<br>
<br>
<br>
> libvir-list</font></tt>
<br><tt><font size=2>> <br>
> On 10/22/2010 05:19 AM, Stefan Berger wrote:<br>
> > Using automated replacement with sed and editing I have now replaced
all<br>
> > occurrences of close() with VIR_(FORCE_)CLOSE() except for one,
of<br>
> > course. Some replacements were straight forward, others I needed
to pay<br>
> > attention. I hope I payed attention in all the right places...
Please<br>
> > have a look. This should have at least solved one more double-close<br>
> > error.<br>
> <br>
> Can you isolate any of those double-close errors into separate patches
<br>
> which we can apply now, rather than drowning them in the giant patch?<br>
> <br>
> ><br>
> > Signed-off-by: Stefan Berger<stefanb@us.ibm.com><br>
> ><br>
> >   src/phyp/phyp_driver.c          
         |   13 ++--<br>
> <br>
> Resuming...<br>
> </font></tt>
<br><tt><font size=2>[...]<br>
> <br>
> > Index: libvirt-acl/src/qemu/qemu_monitor.c<br>
> ><br>
> > @@ -694,8 +695,7 @@ void qemuMonitorClose(qemuMonitorPtr mon<br>
> >       if (!mon->closed) {<br>
> >           if (mon->watch)<br>
> >               virEventRemoveHandle(mon->watch);<br>
> > -        if (mon->fd != -1)<br>
> > -            close(mon->fd);<br>
> > +        VIR_FORCE_CLOSE(mon->fd);<br>
> >           /* NB: ordinarily one might
immediately set mon->watch to -1<br>
> >            * and mon->fd to
-1, but there may be a callback active<br>
> >            * that is still relying
on these fields being valid. So<br>
> <br>
> Ouch - given that comment, could we be frying a callback by setting
<br>
> mon->fd to -1 in VIR_FORCE_CLOSE?  We need to double check
this, and <br>
> possibly use a temporary variable if the callback indeed needs a <br>
> non-negative mon->fd for a bit longer, or tighten the specification
and <br>
> all existing callbacks to tolerate mon->fd changing to -1.<br>
> <br>
> Probably worth splitting this particular hunk into its own commit,
<br>
> rather than part of the giant patch.</font></tt>
<br>
<br><tt><font size=2>Yes, good idea. Obviously I did not read the comment.</font></tt>
<br><tt><font size=2><br>
> <br>
> > Index: libvirt-acl/src/remote/remote_driver.c<br>
> > ===================================================================<br>
> > --- libvirt-acl.orig/src/remote/remote_driver.c<br>
> > +++ libvirt-acl/src/remote/remote_driver.c<br>
> > @@ -82,6 +82,7 @@<br>
> >   #include "util.h"<br>
> >   #include "event.h"<br>
> >   #include "ignore-value.h"<br>
> > +#include "files.h"<br>
> ><br>
> >   #define VIR_FROM_THIS VIR_FROM_REMOTE<br>
> ><br>
> > @@ -711,7 +712,7 @@ doRemoteOpen (virConnectPtr conn,<br>
> >               if (errno ==
ECONNREFUSED&&<br>
> >                  
flags&  VIR_DRV_OPEN_REMOTE_AUTOSTART&&<br>
> >                  
trials<  20) {<br>
> > -                close(priv->sock);<br>
> > +                VIR_FORCE_CLOSE(priv->sock);<br>
> >                  
priv->sock = -1;<br>
> <br>
> This line is now redundant.</font></tt>
<br>
<br><tt><font size=2>Done.</font></tt>
<br><tt><font size=2><br>
> <br>
> > Index: libvirt-acl/src/uml/uml_driver.c<br>
> > @@ -811,7 +811,7 @@ static int umlStartVMDaemon(virConnectPt<br>
> >                  
            virDomainObjPtr vm) {<br>
> >       const char **argv = NULL, **tmp;<br>
> >       const char **progenv = NULL;<br>
> > -    int i, ret;<br>
> > +    int i, ret, tmpfd;<br>
> >       pid_t pid;<br>
> >       char *logfile;<br>
> >       int logfd = -1;<br>
> <br>
> >       for (i = 0; i<  FD_SETSIZE; i++)<br>
> > -        if (FD_ISSET(i,&keepfd))<br>
> > -            close(i);<br>
> > +        if (FD_ISSET(i,&keepfd)) {<br>
> > +            tmpfd = i;<br>
> > +            VIR_FORCE_CLOSE(tmpfd);<br>
> <br>
> Another awfully large scope for a needed temporary.</font></tt>
<br>
<br><tt><font size=2>Ok.</font></tt>
<br><tt><font size=2><br>
> <br>
> > Index: libvirt-acl/src/util/bridge.c<br>
> > @@ -107,7 +108,7 @@ brShutdown(brControl *ctl)<br>
> >       if (!ctl)<br>
> >           return;<br>
> ><br>
> > -    close(ctl->fd);<br>
> > +    VIR_FORCE_CLOSE(ctl->fd);<br>
> >       ctl->fd = 0;<br>
> <br>
> Huh - is this an existing logic bug? Can we end up accidentally <br>
> double-closing stdin?</font></tt>
<br>
<br><tt><font size=2>I'ld leave the patch for now as it is, i.e., do the
VIR_FORCE_CLOSE and remember to investigate. So far, the changed does not
have any further negative impact that already isn't there - but it also
doesn't solve a potential problem.</font></tt>
<br><tt><font size=2><br>
> <br>
> > Index: libvirt-acl/src/util/logging.c<br>
> > ===================================================================<br>
> > --- libvirt-acl.orig/src/util/logging.c<br>
> > +++ libvirt-acl/src/util/logging.c<br>
> > @@ -40,6 +40,7 @@<br>
> >   #include "util.h"<br>
> >   #include "buf.h"<br>
> >   #include "threads.h"<br>
> > +#include "files.h"<br>
> ><br>
> >   /*<br>
> >    * Macro used to format the message as a string in
virLogMessage<br>
> > @@ -603,8 +604,7 @@ static int virLogOutputToFd(const char *<br>
> >   static void virLogCloseFd(void *data) {<br>
> >       int fd = (long) data;<br>
> ><br>
> > -    if (fd>= 0)<br>
> > -        close(fd);<br>
> > +    VIR_FORCE_CLOSE(fd);<br>
> <br>
> Should we fix this function to return an int value, and return <br>
> VIR_CLOSE(fd) so that callers can choose to detect log close failures?</font></tt>
<br>
<br><tt><font size=2>Also that I would delay until further 'cause this
may have consequences for those calling the function.</font></tt>
<br><tt><font size=2><br>
> <br>
> > Index: libvirt-acl/src/util/macvtap.c<br>
> > ===================================================================<br>
> > --- libvirt-acl.orig/src/util/macvtap.c<br>
> > +++ libvirt-acl/src/util/macvtap.c<br>
> > @@ -52,6 +52,7 @@<br>
> >   # include "conf/domain_conf.h"<br>
> >   # include "virterror_internal.h"<br>
> >   # include "uuid.h"<br>
> > +# include "files.h"<br>
> ><br>
> >   # define VIR_FROM_THIS VIR_FROM_NET<br>
> ><br>
> > @@ -94,7 +95,7 @@ static int nlOpen(void)<br>
> ><br>
> >   static void nlClose(int fd)<br>
> >   {<br>
> > -    close(fd);<br>
> > +    VIR_FORCE_CLOSE(fd);<br>
> <br>
> Likewise?</font></tt>
<br>
<br><tt><font size=2>This here is a close of a netlink socket, which was
used to communicate with the kernel. I would just close it and discard
the returned value.</font></tt>
<br><tt><font size=2><br>
> <br>
> > Index: libvirt-acl/src/util/util.c<br>
> > ===================================================================<br>
> > --- libvirt-acl.orig/src/util/util.c<br>
> > +++ libvirt-acl/src/util/util.c<br>
> > @@ -71,6 +71,7 @@<br>
> >   #include "memory.h"<br>
> >   #include "threads.h"<br>
> >   #include "verify.h"<br>
> > +#include "files.h"<br>
> ><br>
> >   #ifndef NSIG<br>
> >   # define NSIG 32<br>
> > @@ -461,6 +462,7 @@ __virExec(const char *const*argv,<br>
> >       int pipeerr[2] = {-1,-1};<br>
> >       int childout = -1;<br>
> >       int childerr = -1;<br>
> > +    int tmpfd;<br>
> <br>
> > @@ -568,8 +570,10 @@ __virExec(const char *const*argv,<br>
> >               i != childout&&<br>
> >               i != childerr&&<br>
> >               (!keepfd ||<br>
> > -             !FD_ISSET(i, keepfd)))<br>
> > -            close(i);<br>
> > +             !FD_ISSET(i, keepfd)))
{<br>
> > +            tmpfd = i;<br>
> > +            VIR_FORCE_CLOSE(tmpfd);<br>
> <br>
> I thought this was another large scope for a temporary....<br>
> <br>
> > +        }<br>
> ><br>
> >       if (dup2(infd>= 0 ? infd : null, STDIN_FILENO)<
 0) {<br>
> >           virReportSystemError(errno,<br>
> > @@ -589,14 +593,15 @@ __virExec(const char *const*argv,<br>
> >           goto fork_error;<br>
> >       }<br>
> ><br>
> > -    if (infd>  0)<br>
> > -        close(infd);<br>
> > -    close(null);<br>
> > -    if (childout>  0)<br>
> > -        close(childout);<br>
> > +    VIR_FORCE_CLOSE(infd);<br>
> > +    VIR_FORCE_CLOSE(null);<br>
> > +    tmpfd = childout;   /* preserve childout
value */<br>
> > +    VIR_FORCE_CLOSE(tmpfd);<br>
> <br>
> ...but you needed it here too.</font></tt>
<br>
<br><tt><font size=2>Yes, here I need it twice. So not changing it.</font></tt>
<br><tt><font size=2><br>
> <br>
> >       if (childerr>  0&&<br>
> > -        childerr != childout)<br>
> > -        close(childerr);<br>
> > +        childerr != childout) {<br>
> > +        VIR_FORCE_CLOSE(childerr);<br>
> > +        childout = -1;<br>
> > +    }<br>
> <br>
> Looks okay after all - certainly not one of the trivial conversions
<br>
> though :)<br>
> <br>
> > Index: libvirt-acl/src/util/virtaudit.c<br>
> > ===================================================================<br>
> > --- libvirt-acl.orig/src/util/virtaudit.c<br>
> > +++ libvirt-acl/src/util/virtaudit.c<br>
> > @@ -30,6 +30,7 @@<br>
> >   #include "virterror_internal.h"<br>
> >   #include "logging.h"<br>
> >   #include "virtaudit.h"<br>
> > +#include "files.h"<br>
> ><br>
> >   /* Provide the macros in case the header file is old.<br>
> >      FIXME: should be removed. */<br>
> > @@ -133,6 +134,6 @@ void virAuditSend(const char *file ATTRI<br>
> >   void virAuditClose(void)<br>
> >   {<br>
> >   #if HAVE_AUDIT<br>
> > -    close(auditfd);<br>
> > +    VIR_CLOSE(auditfd);<br>
> <br>
> Must be VIR_FORCE_CLOSE; I'm guessing you didn't have audit turned
on in <br>
> your compile.</font></tt>
<br>
<br><tt><font size=2>Right...</font></tt>
<br><tt><font size=2><br>
> <br>
> > Index: libvirt-acl/src/xen/proxy_internal.c<br>
> > @@ -236,12 +237,11 @@ virProxyCloseSocket(xenUnifiedPrivatePtr<br>
> >       if (priv->proxy<  0)<br>
> >           return(-1);<br>
> ><br>
> > -    ret = close(priv->proxy);<br>
> > +    ret = VIR_CLOSE(priv->proxy);<br>
> >       if (ret != 0)<br>
> >           VIR_WARN("Failed to close
socket %d", priv->proxy);<br>
> >       else<br>
> >           VIR_DEBUG("Closed socket
%d", priv->proxy);<br>
> <br>
> Subtle unintended semantic change; you're now printing -1 instead
of the <br>
> fd you just closed; you'll need a temporary.</font></tt>
<br>
<br><tt><font size=2>There was another one like this that I got right :)<br>
</font></tt>
<br><tt><font size=2>> <br>
> > Index: libvirt-acl/src/xen/xend_internal.c<br>
> > @@ -137,9 +137,7 @@ do_connect(virConnectPtr xend)<br>
> ><br>
> ><br>
> >       if (connect(s, (struct sockaddr *)&priv->addr,
priv->addrlen) == -1) {<br>
> > -        serrno = errno;<br>
> > -        close(s);<br>
> > -        errno = serrno;<br>
> > +        VIR_FORCE_CLOSE(s);<br>
> >           s = -1;<br>
> <br>
> Redundant line.  Overall, I'm impressed with how many lines this
is <br>
> shaving off the code base!  I think we settled on a pretty good
calling <br>
> convention.</font></tt>
<br>
<br><tt><font size=2>Fixed. Will post v2 and if useful a diff(v1,v2).</font></tt>
<br>
<br><tt><font size=2>  Stefan</font></tt>
<br><tt><font size=2><br>
> <br>
> And with that, I've completed my review of v1.<br>
> <br>
> -- <br>
> Eric Blake   eblake@redhat.com    +1-801-349-2682<br>
> Libvirt virtualization library </font></tt><a href=http://libvirt.org/><tt><font size=2>http://libvirt.org</font></tt></a><tt><font size=2><br>
> <br>
> --<br>
> libvir-list mailing list<br>
> libvir-list@redhat.com<br>
> </font></tt><a href="https://www.redhat.com/mailman/listinfo/libvir-list"><tt><font size=2>https://www.redhat.com/mailman/listinfo/libvir-list</font></tt></a><tt><font size=2><br>
</font></tt>