2
* Copyright 2010-2011 Red Hat, Inc.
4
* This copyrighted material is made available to anyone wishing to use,
5
* modify, copy, or redistribute it subject to the terms and conditions
6
* of the GNU General Public License v2 or (at your option) any later version.
21
#include <sys/types.h>
24
#include <sys/socket.h>
27
#include "sanlock_internal.h"
28
#include "sanlock_sock.h"
32
int lockfile(const char *dir, const char *name)
40
old_umask = umask(0022);
41
rv = mkdir(SANLK_RUN_DIR, 0777);
42
if (rv < 0 && errno != EEXIST) {
48
snprintf(path, PATH_MAX, "%s/%s", dir, name);
50
fd = open(path, O_CREAT|O_WRONLY|O_CLOEXEC, 0666);
52
log_error("lockfile open error %s: %s",
53
path, strerror(errno));
57
lock.l_type = F_WRLCK;
59
lock.l_whence = SEEK_SET;
62
rv = fcntl(fd, F_SETLK, &lock);
64
log_error("lockfile setlk error %s: %s",
65
path, strerror(errno));
69
rv = ftruncate(fd, 0);
71
log_error("lockfile truncate error %s: %s",
72
path, strerror(errno));
76
memset(buf, 0, sizeof(buf));
77
snprintf(buf, sizeof(buf), "%d\n", getpid());
79
rv = write(fd, buf, strlen(buf));
81
log_error("lockfile write error %s: %s",
82
path, strerror(errno));
92
void unlink_lockfile(int fd, const char *dir, const char *name)
96
snprintf(path, PATH_MAX, "%s/%s", dir, name);