1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* gensio - A library for abstracting stream I/O
* Copyright (C) 2018 Corey Minyard <minyard@acm.org>
*
* SPDX-License-Identifier: LGPL-2.1-only
*/
#include "config.h"
#include "uucplock.h"
bool gensio_uucp_locking_enabled = true;
#ifdef USE_UUCP_LOCKING
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
static char *uucp_lck_dir = "/var/lock/";
static char *dev_prefix = "/dev/";
static size_t
uucp_fname_lock_size(char *devname)
{
size_t dev_prefix_len = strlen(dev_prefix);
if (strncmp(dev_prefix, devname, dev_prefix_len) == 0)
devname += dev_prefix_len;
/*
* Format is "/var/lock/LCK..<devname>". The 6 is for
* the "LCK.." and the final nil char.
*/
return 6 + strlen(uucp_lck_dir) + strlen(devname);
}
static void
uucp_fname_lock(char *buf, char *devname)
{
size_t i, dev_prefix_len = strlen(dev_prefix);
if (strncmp(dev_prefix, devname, dev_prefix_len) == 0)
devname += dev_prefix_len;
sprintf(buf, "%sLCK..%s", uucp_lck_dir, devname);
for (i = strlen(uucp_lck_dir); buf[i]; i++) {
if (buf[i] == '/')
buf[i] = '_';
}
}
void
uucp_rm_lock(char *devname)
{
char *lck_file;
if (!gensio_uucp_locking_enabled) return;
lck_file = malloc(uucp_fname_lock_size(devname));
if (lck_file == NULL) {
return;
}
uucp_fname_lock(lck_file, devname);
unlink(lck_file);
free(lck_file);
}
static int
write_full(int fd, char *data, size_t count)
{
ssize_t written;
restart:
while ((written = write(fd, data, count)) > 0) {
data += written;
count -= written;
}
if (written < 0) {
if (errno == EAGAIN)
goto restart;
return -1;
}
return 0;
}
int
uucp_mk_lock(char *devname)
{
struct stat stt;
int pid = -1;
if (!gensio_uucp_locking_enabled)
return 0;
if (stat(uucp_lck_dir, &stt) == 0) { /* is lock file directory present? */
char *lck_file;
union {
uint32_t ival;
char str[64];
} buf;
int fd;
lck_file = malloc(uucp_fname_lock_size(devname));
if (lck_file == NULL)
return -1;
uucp_fname_lock(lck_file, devname);
pid = 0;
if ((fd = open(lck_file, O_RDONLY)) >= 0) {
int n;
n = read(fd, &buf, sizeof(buf) - 1);
close(fd);
if (n == 4) /* Kermit-style lockfile. */
pid = buf.ival;
else if (n > 0) { /* Ascii lockfile. */
buf.str[n] = '\0';
sscanf(buf.str, "%10d", &pid);
}
if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
/* death lockfile - remove it */
unlink(lck_file);
pid = 0;
} else
pid = 1;
}
if (pid == 0) {
int mask;
mask = umask(022);
fd = open(lck_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
umask(mask);
if (fd >= 0) {
ssize_t rv;
snprintf(buf.str, sizeof(buf), "%10ld\n",
(long)getpid());
rv = write_full(fd, buf.str, strlen(buf.str));
close(fd);
if (rv < 0) {
pid = -1;
unlink(lck_file);
}
} else {
pid = -1;
}
}
free(lck_file);
}
return pid;
}
#else
void
uucp_rm_lock(char *devname)
{
}
int
uucp_mk_lock(char *devname)
{
return 0;
}
#endif /* USE_UUCP_LOCKING */
|