def remove_if_stale_pidfile(pidfile):
    """
    @pidfile: PID file to remove if it is staled.

    Exceptions are logged and are not propagated.
    """
    c14n_prog_name = lambda arg: os.path.basename(re.sub(r'\.py$', '', arg))
    try:
        try:
            pid_maydaemon = int(file(pidfile).readline().strip())
        except IOError, e:
            if e.errno == errno.ENOENT:
                return # nothing to suppress, so do nothing...
            raise
        # Who are we?
        i_am = c14n_prog_name(sys.argv[0])
        try:
            other_cmdline = file(os.path.join(SLASH_PROC, str(pid_maydaemon), PROG_CMDLN)).read().split('\0')
            if len(other_cmdline) and other_cmdline[-1] == "":
                other_cmdline.pop()
        except IOError, e:
            if e.errno == errno.ENOENT:
                # no process with the PID extracted from the
                # pidfile, so no problem to remove the latter
                os.unlink(pidfile)
                return
            raise
        # Check the whole command line of the other process
        if i_am in map(c14n_prog_name, other_cmdline):
            log.warning("A pidfile %r already exists (contains pid %d) and the correponding process command line contains our own name %r",
                        pidfile, pid_maydaemon, i_am)
            return
        # It may not be us, but we must be quite sure about that so also try
        # to validate with the name of the executable.
        full_pgm = lock_pgm = None
        try:
            full_pgm = os.readlink(os.path.join(SLASH_PROC, str(pid_maydaemon), PROG_SLINK))
            lock_pgm = os.path.basename(full_pgm)
        except OSError, e:
            if e.errno == errno.EACCES:
                # We consider it's ok not being able to access
                # "/proc/<pid>/exe" if we could previously access
                # "/proc/<pid>/cmdline", because if we do not have
                # the needed permissions to run the daemon this will
                # be catched latter (potentially when creating our
                # own pidfile)
                lock_pgm = None
            else:
                raise
        if i_am == lock_pgm:
            log.warning("A pidfile %r already exists (contains pid %d) and an executable with our name %r is runnning with that pid.",
                        pidfile, pid_maydaemon, i_am)
            return
        # Ok to remove the previously existing pidfile now.
        log.info("A pidfile %r already exists (contains pid %d) but the corresponding process does not seem to match with our own name %r.  "
                 "Will remove the pidfile.", pidfile, pid_maydaemon, i_am)
        log.info("Splitted command line of the other process: %s", other_cmdline)
        if lock_pgm:
            log.info("Name of the executable the other process comes from: %s", full_pgm)
        os.unlink(pidfile)
        return
    except Exception: # pylint: disable-msg=W0703
        log.exception("unexpected error")