[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [RFC] Power Hypervisor Libvirt support



On Mon, May 04, 2009 at 05:50:03PM -0300, Eduardo Otubo wrote:
> 
> > > +
> > > +/* return the lpar_id given a name and a managed system name */
> > > +static int
> > > +phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system,
> > > +              const char *name)
> > > +{
> > > +    int exit_status = 0;
> > > +    virBuffer cmd = VIR_BUFFER_INITIALIZER;
> > > +
> > > +    virBufferVSprintf(&cmd,
> > > +                      "lssyscfg -r lpar -m %s --filter lpar_names=%s -F lpar_id",
> > > +                      managed_system, name);
> > > +    const char *tex_ret =
> > > +        __inner_exec_command(ssh_session, virBufferContentAndReset(&cmd),
> > > +                             &exit_status);
> > > +
> > > +    virBufferContentAndReset(&cmd);
> > 
> >    Huh ? you're supposed to get the resulting char *, and then free it
> >    later once you're done with the data. Here youre just leaking memory
> >    I'm afraid
> > 
> >  same thing for most of the commands in that file.
> 
> Here, I just would like to free the Buffer, and this was the best way I
> find since I couldn't find any better function to manipulate this. How
> do I simply free a buffer using the internal virBuffer* API?

The virBufferContentAndReset() method returns you the internal char *
string, and resets the virBuffer state to its inital value. You are
now owner of the char * string, and are responsible for free'ing it
when done.

You should also check virBufferError() and report OOM error if it fails.
So, in the above example. what you'd want todo is

  static int
  phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system,
                const char *name)
  {
      int exit_status = 0;
      virBuffer cmd = VIR_BUFFER_INITIALIZER;
      char *buf;
  
      virBufferVSprintf(&cmd,
                        "lssyscfg -r lpar -m %s --filter lpar_names=%s -F lpar_id",
                        managed_system, name);
      if (virBufferError(&cmd)) {
        virReportOOMError(conn);
        return NULL;
      }

      buf = virBufferContentAndReset(&cmd);
      const char *tex_ret =
          __inner_exec_command(ssh_session, buf
                               &exit_status);
      VIR_FREE(buf);
   }


That all said, in this particular function I it is overkill to use
the virBuffer APIs, since you've only got a single printf() call
to make. virBuffer is more appropriate when you have 2 or more
printfs() or strcats() to make.  If just doing a single printf,
then use virAsprintf, 

eg

  static int
  phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system,
                const char *name)
  {
      int exit_status = 0;
      char *buf;

      if (virAsprintf(&buf,
                      "lssyscfg -r lpar -m %s --filter lpar_names=%s -F lpar_id",
                      managed_system, name) < 0) {
    	virReportOOMError(conn);
    	return	NULL;
      }

      const char *tex_ret =
          __inner_exec_command(ssh_session, buf
                               &exit_status);
      VIR_FREE(buf);
   }


Regards,
Daniel
-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]