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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* $Id$ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "fdm.h"
int mklock(u_int, const char *);
void rmlock(u_int, const char *);
int lockfd(u_int, int);
/* Print path into buffer. */
int
ppath(char *buf, size_t len, const char *fmt, ...)
{
va_list ap;
int n;
va_start(ap, fmt);
n = vppath(buf, len, fmt, ap);
va_end(ap);
return (n);
}
/* Make path into buffer. */
int
vppath(char *buf, size_t len, const char *fmt, va_list ap)
{
if ((size_t) xvsnprintf(buf, len, fmt, ap) >= len) {
errno = ENAMETOOLONG;
return (-1);
}
return (0);
}
/* Make lock file. */
int
mklock(u_int locks, const char *path)
{
char lock[MAXPATHLEN];
int fd;
if (!(locks & LOCK_DOTLOCK))
return (0);
if (ppath(lock, sizeof lock, "%s.lock", path) != 0)
return (-1);
fd = xcreate(lock, O_WRONLY, -1, -1, S_IRUSR|S_IWUSR);
if (fd == -1) {
if (errno == EEXIST)
errno = EAGAIN;
return (-1);
}
close(fd);
cleanup_register(lock);
return (0);
}
/* Remove lock file. */
void
rmlock(u_int locks, const char *path)
{
char lock[MAXPATHLEN];
if (!(locks & LOCK_DOTLOCK))
return;
if (ppath(lock, sizeof lock, "%s.lock", path) != 0)
fatal("unlink failed");
if (unlink(lock) != 0)
fatal("unlink failed");
cleanup_deregister(lock);
}
/* Lock file descriptor. */
int
lockfd(u_int locks, int fd)
{
struct flock fl;
if (locks & LOCK_FLOCK) {
if (flock(fd, LOCK_EX|LOCK_NB) != 0) {
if (errno == EWOULDBLOCK)
errno = EAGAIN;
return (-1);
}
}
if (locks & LOCK_FCNTL) {
memset(&fl, 0, sizeof fl);
fl.l_start = 0;
fl.l_len = 0;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(fd, F_SETLK, &fl) == -1) {
/* fcntl already returns EAGAIN if needed. */
return (-1);
}
}
return (0);
}
/*
* Open a file, locking using the lock types specified. Returns EAGAIN if lock
* failed.
*/
int
openlock(const char *path, int flags, u_int locks)
{
int fd, saved_errno;
if (mklock(locks, path) != 0)
return (-1);
if ((fd = open(path, flags, 0)) == -1)
goto error;
if (lockfd(locks, fd) != 0)
goto error;
return (fd);
error:
saved_errno = errno;
close(fd);
rmlock(locks, path);
errno = saved_errno;
return (-1);
}
/* Sleep for lock. */
int
locksleep(const char *hdr, const char *path, long long *used)
{
useconds_t us;
char prefix[PATH_MAX * 2];
if (hdr != NULL)
snprintf(prefix, sizeof prefix, "%s: %s:", hdr, path);
else
snprintf(prefix, sizeof prefix, "%s:", path);
if (*used == 0)
srandom((u_int) getpid());
us = LOCKSLEEPTIME + (random() % LOCKSLEEPTIME);
log_debug3("%s sleeping %.3f seconds for lock", prefix, us / 1000000.0);
usleep(us);
*used += us;
if (*used < conf.lock_timeout * 1000000LL)
return (0);
log_warnx("%s couldn't get lock in %.3f seconds", prefix, *used / 1000000.0);
return (-1);
}
/* Create a locked file. */
int
createlock(
const char *path, int flags, uid_t uid, gid_t gid, mode_t mode, u_int locks)
{
int fd, saved_errno;
if (mklock(locks, path) != 0)
return (-1);
if ((fd = xcreate(path, flags, uid, gid, mode)) == -1)
goto error;
if (lockfd(locks, fd) != 0)
goto error;
return (fd);
error:
saved_errno = errno;
close(fd);
rmlock(locks, path);
errno = saved_errno;
return (-1);
}
/* Close locked file and remove locks. */
void
closelock(int fd, const char *path, u_int locks)
{
close(fd);
rmlock(locks, path);
}
/* Create a file. */
int
xcreate(const char *path, int flags, uid_t uid, gid_t gid, mode_t mode)
{
int fd;
if ((fd = open(path, flags|O_CREAT|O_EXCL, mode)) == -1)
return (-1);
if (uid != (uid_t) -1 || gid != (gid_t) -1) {
if (fchown(fd, uid, gid) != 0)
return (-1);
}
return (fd);
}
/* Make a directory. */
int
xmkdir(const char *path, uid_t uid, gid_t gid, mode_t mode)
{
if (mkdir(path, mode) != 0)
return (-1);
if (uid != (uid_t) -1 || gid != (gid_t) -1) {
if (chown(path, uid, gid) != 0)
return (-1);
}
return (0);
}
/* Create entire path. */
int
xmkpath(const char *path, uid_t uid, gid_t gid, mode_t mode)
{
struct stat sb;
char *copy, *ptr, ch;
copy = ptr = xstrdup(path);
do {
ptr += strspn(ptr, "/");
ptr += strcspn(ptr, "/");
ch = *ptr;
*ptr = '\0';
if (stat(copy, &sb) != 0) {
if (errno == ENOENT &&
xmkdir(copy, uid, gid, mode) != 0 &&
errno != EEXIST)
return (-1);
} else if (!S_ISDIR(sb.st_mode)) {
errno = ENOTDIR;
return (-1);
}
*ptr = ch;
} while (ch != '\0');
xfree(copy);
return (0);
}
/* Check mode of file. */
const char *
checkmode(struct stat *sb, mode_t mode)
{
static char msg[128];
if ((sb->st_mode & ACCESSPERMS) == mode)
return (NULL);
xsnprintf(msg, sizeof msg, "bad permissions:"
" %o%o%o, should be %o%o%o", MODE(sb->st_mode), MODE(mode));
return (msg);
}
/* Check owner of file. */
const char *
checkowner(struct stat *sb, uid_t uid)
{
static char msg[128];
if (uid == (uid_t) -1)
uid = geteuid();
if (sb->st_uid == uid)
return (NULL);
xsnprintf(msg, sizeof msg,
"bad owner: %lu, should be %lu", (u_long) sb->st_uid, (u_long) uid);
return (msg);
}
/* Check group of file. */
const char *
checkgroup(struct stat *sb, gid_t gid)
{
static char msg[128];
if (gid == (gid_t) -1)
gid = getgid();
if (sb->st_gid == gid)
return (NULL);
xsnprintf(msg, sizeof msg,
"bad group: %lu, should be %lu", (u_long) sb->st_gid, (u_long) gid);
return (msg);
}
/*
* Move file oldpath to newpath. oldpath is always removed even in case of
* failure.
*
* It returns 0 on success and -1 on failure with errno set.
*
* This function use link + unlink or stat + rename if link fail with EXDEV.
* (see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=538194)
*/
int
safemove(const char *oldpath, const char *newpath)
{
int ret;
int errsave;
struct stat sb;
ret = link(oldpath, newpath);
if (ret != 0 && errno == EXDEV) {
ret = stat(newpath, &sb);
if (ret == -1) {
if (errno == ENOENT)
ret = rename(oldpath, newpath);
} else {
ret = -1;
errno = EEXIST;
}
}
errsave = errno;
if (unlink(oldpath) != 0 && errno != ENOENT)
fatal("unlink failed");
errno = errsave;
return (ret);
}
|