Exemplo n.º 1
0
int pidfile_write(struct pidfh *pfh)
{
    char pidstr[16];
    int error, fd;

    errno = pidfile_verify(pfh);
    if (errno != 0) {
        return (-1);
    }
    
    fd = pfh->pf_fd;
    
    if (ftruncate(fd, 0) == -1) {
        error = errno;
        _pidfile_remove(pfh, 0);
        errno = error;
        return (-1);
    }

    snprintf(pidstr, sizeof(pidstr), "%u", getpid());
    if(pwrite(fd, pidstr, strlen(pidstr), 0) != (size_t)strlen(pidstr)) {
        error = errno;
        _pidfile_remove(pfh, 0);
        errno = error;
        return (-1);
    }

    return (0);
}
Exemplo n.º 2
0
int
pidfile_write(struct pidfh *pfh)
{
	char pidstr[16];
	int error, fd;

	/*
	 * Check remembered descriptor, so we don't overwrite some other
	 * file if pidfile was closed and descriptor reused.
	 */
	errno = pidfile_verify(pfh);
	if (errno != 0) {
		/*
		 * Don't close descriptor, because we are not sure if it's ours.
		 */
		return (-1);
	}
	fd = pfh->pf_fd;

	/*
	 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
	 */
	if (ftruncate(fd, 0) == -1) {
		error = errno;
		_pidfile_remove(pfh, 0);
		errno = error;
		return (-1);
	}

	snprintf(pidstr, sizeof(pidstr), "%u", getpid());
	if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
		error = errno;
		_pidfile_remove(pfh, 0);
		errno = error;
		return (-1);
	}

	return (0);
}
Exemplo n.º 3
0
int pidfile_remove(struct pidfh *pfh)
{
    return (_pidfile_remove(pfh, 1));
}