pidfile_close prevents pidfile_remove from working

Bug #1836219 reported by jklowden
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
libbsd (Ubuntu)
New
Undecided
Unassigned

Bug Description

The whole pidfile_xxx suite, besides being unnecessarily complicated compared to 1 function in NetBSD, does not do what it says on the tin.

Complications. 4 functions instead of one. pidfile_open requires pidptr, but doesn't use it. The other functions require an argument that the documentation says isn't needed, but is.

Error. Most important, pidfile_close modifies its argument such that pidfile_remove can't use it: both the file descriptor and the name are destroyed.

The following program and associated output illustrates the problem.

#include <err.h>
#include <stdio.h>
#include <stdlib.h>

#include <bsd/libutil.h>

void
show_pfh( const char func[], struct pidfh * pfh ) {
  printf( "%s: fh = %d, name = %s\n", func, pfh->pf_fd, pfh->pf_path );
}

int
main(int argc, char *argv[])
{
  pid_t pid;

  struct pidfh * pfh = pidfile_open("/tmp/foo.pid", 0644, &pid);

  if( !pfh ) {
    err(EXIT_FAILURE, "pidfile_open");
  }
  show_pfh("open", pfh);

  if( -1 == pidfile_write(pfh) ) {
    err(EXIT_FAILURE, "pidfile_write");
  }
  show_pfh("write", pfh);

  if( -1 == pidfile_close(pfh) ) {
    err(EXIT_FAILURE, "pidfile_close");
  }
  show_pfh("close", pfh);

  if( -1 == pidfile_remove(pfh) ) {
    err(EXIT_FAILURE, "pidfile_remove");
  }
  show_pfh("remove", pfh);

  return 0;
}

$ ./pid
open: fh = 3, name = /tmp/foo.pid
write: fh = 3, name = /tmp/foo.pid
close: fh = 0, name =
pid: pidfile_remove: Invalid argument

$ grep -iE 'name|version' /etc/os-release
NAME="Ubuntu"
VERSION="16.04 LTS (Xenial Xerus)"
PRETTY_NAME="Ubuntu 16.04 LTS"
VERSION_ID="16.04"
UBUNTU_CODENAME=xenial

$ gcc --version | grep ^gcc
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609

$ ldd pid
        linux-vdso.so.1 => (0x00007fffc79b6000)
        libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f72ee343000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f72edf79000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f72ee558000)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.