Skip to content

Commit

Permalink
util: New functions pidfile_path_create(), pidfile_fd_close()
Browse files Browse the repository at this point in the history
Uses the core of CTDB's create_pidfile_context() for
pidfile_path_create(). pidfile_fd_close() is a subset of CTDB's
pidfile_context_destructor().

Signed-off-by: Martin Schwenke <[email protected]>
Reviewed-by: Volker Lendecke <[email protected]>
  • Loading branch information
martin-schwenke authored and Martin Schwenke committed Aug 2, 2017
1 parent 59ebb29 commit 411b7c8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
99 changes: 99 additions & 0 deletions lib/util/pidfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Unix SMB/CIFS implementation.
pidfile handling
Copyright (C) Andrew Tridgell 1998
Copyright (C) Amitay Isaccs 2016
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,6 +32,104 @@
* @brief Pid file handling
*/

int pidfile_path_create(const char *path, int *outfd)
{
struct flock lck;
char tmp[64] = { 0 };
pid_t pid;
int fd, ret = 0;
int len;
ssize_t nwritten;

pid = getpid();

fd = open(path, O_CREAT|O_WRONLY|O_NONBLOCK, 0644);
if (fd == -1) {
return errno;
}

if (! set_close_on_exec(fd)) {
close(fd);
return EIO;
}

lck = (struct flock) {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
};

do {
ret = fcntl(fd, F_SETLK, &lck);
} while ((ret == -1) && (errno == EINTR));

if (ret != 0) {
ret = errno;
close(fd);
return ret;
}

/*
* PID file is locked by us so from here on we should unlink
* on failure
*/

do {
ret = ftruncate(fd, 0);
} while ((ret == -1) && (errno == EINTR));

if (ret == -1) {
ret = EIO;
goto fail_unlink;
}

len = snprintf(tmp, sizeof(tmp), "%u\n", pid);
if (len < 0) {
ret = errno;
goto fail_unlink;
}
if (len >= sizeof(tmp)) {
ret = ENOSPC;
goto fail_unlink;
}

do {
nwritten = write(fd, tmp, len);
} while ((nwritten == -1) && (errno == EINTR));

if ((nwritten == -1) || (nwritten != len)) {
ret = EIO;
goto fail_unlink;
}

if (outfd != NULL) {
*outfd = fd;
}
return 0;

fail_unlink:
unlink(path);
close(fd);
return ret;
}

void pidfile_fd_close(int fd)
{
struct flock lck = {
.l_type = F_UNLCK,
.l_whence = SEEK_SET,
};
int ret;

do {
ret = fcntl(fd, F_SETLK, &lck);
} while ((ret == -1) && (errno == EINTR));

do {
ret = close(fd);
} while ((ret == -1) && (errno == EINTR));
}


/**
* return the pid in a pidfile. return 0 if the process (or pidfile)
* does not exist
Expand Down
3 changes: 3 additions & 0 deletions lib/util/pidfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#ifndef _SAMBA_PIDFILE_H_
#define _SAMBA_PIDFILE_H_

int pidfile_path_create(const char *path, int *outfd);
void pidfile_fd_close(int fd);

pid_t pidfile_pid(const char *piddir, const char *name);
void pidfile_create(const char *piddir, const char *program_name);
void pidfile_unlink(const char *piddir, const char *program_name);
Expand Down

0 comments on commit 411b7c8

Please sign in to comment.