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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
|
/*
* lock.c -- deal with file locking on various architectures and UNIXs.
* dot_lock() creates a file with the same name as the parameter passed
* with the appendage ".lock" -- this is to be compatible with certain
* systems that don't use flock or lockf or whatever they have available
* that they don't use.
*/
#ifdef USG
#include <unistd.h>
#endif /* USG */
#include "mush.h"
#if defined(SYSV) && !defined(USG)
#include <sys/locking.h>
#endif /* SYSV && !USG */
#include <errno.h>
#ifdef __linux__
#undef USG
#undef SYSV
#define BSD
#define setregid(r, s) setgid((s))
#endif
#ifdef DOT_LOCK
#ifndef DOLOCK_PATH
#define DOLOCK_PATH "/usr/sbin/dotlock" /* Path to the executable */
#endif /* DOLOCK_PATH */
#ifndef DOLOCK_NAME
#define DOLOCK_NAME "dotlock" /* Name of the executable */
#endif /* DOLOCK_NAME */
#define DOLOCKIT "-l"
#define UNLOCKIT "-u"
#ifdef LOCK_PROG /* Define for standalone locking program */
#define MAILBOXORFILE "File"
#ifdef DEBIAN
#define DOLOCKLONG "--lock"
#define UNLOCKLONG "--unlock"
#endif
/* System V Release 2 does not support saved-set-group-id, so it is not
* sufficient for mush to be setgid mail (or whatever group has write
* permission on /usr/mail). Instead, we need an external program that
* can be setgid, which mush then runs to create and remove lock files.
* Compiling this file with -DDOT_LOCK -DLOCK_PROG added to your CFLAGS
* will generate such a program:
*
* cc -o dotlock -DDOT_LOCK -DLOCK_PROG lock.c xcreat.c
*
* For mush purposes, you should hardwire the DOLOCK_PATH to the full path
* name of the installed executable. This helps prevent malicious users
* from substituting a different program.
*
* The program generated can also be used to perform mail locking from
* shell scripts, so you may wish to consider installing it (or a copy)
* in a publicly accessible location, e.g. /usr/local/bin. Note that
* this program is not compatible with Xenix mail locking!
*/
#undef on_intr()
#define on_intr() 0
#undef off_intr()
#define off_intr() 0
#undef print
#define print printf
#undef print_more
#define print_more fflush(stdout), printf
#undef sprintf
/* Simple dotlock program. Exits 0 for success, nonzero for failure.
*
* Usage:
* dotlock -lock filename
* dotlock -unlock filename
*
* The options can be abbreviated to their first letter (-l or -u);
* any other usage fails silently.
*
* On Debian systems, usage is --lock, -l or --unlock, -u and any other
* usage fails verbosely. And the program can have any name, too.
*
*/
void
error(fmt, arg1, arg2, arg3, arg4)
register char *fmt;
char *arg1, *arg2, *arg3, *arg4;
{
char buf[BUFSIZ];
(void) sprintf(buf, fmt, arg1, arg2, arg3, arg4);
fprintf(stderr, "%s: %s\n", buf, sys_errlist[errno]);
}
#ifdef DEBIAN
static usage(name)
char* name;
{
fprintf(stderr,
"usage: %s %s, %s filename\n %s %s, %s filename\n",
name, DOLOCKIT, DOLOCKLONG,
name, UNLOCKIT, UNLOCKLONG);
exit(1);
}
#endif
extern void init_host();
main(argc, argv)
int argc;
char **argv;
{
char *myname = rindex(argv[0], '/');
int res = 0;
const char* action = "do anything to";
if (myname)
++myname;
else
myname = argv[0];
#ifdef DEBIAN
if (argc != 3) usage(myname);
#else
if (strcmp(myname, DOLOCK_NAME) != 0 || argc != 3)
exit(2);
#endif
init_host();
#ifdef DEBIAN
if (!strcmp(argv[1], DOLOCKIT)
|| !strcmp(argv[1], DOLOCKLONG)) {
action = "lock";
res = dot_lock(argv[2]);
} else if (!strcmp(argv[1], UNLOCKIT)
|| !strcmp(argv[1], UNLOCKLONG)) {
action = "unlock";
res = dot_unlock(argv[2]);
} else {
usage(myname);
}
#else
if (strncmp(argv[1], DOLOCKIT, 2) == 0) {
action = "lock";
res = dot_lock(argv[2]);
} else if (strncmp(argv[1], UNLOCKIT, 2) == 0)
action = "unlock";
res = dot_unlock(argv[2]);
}
#endif
if (res) {
error("Cannot %s \"%s\"", action, argv[2]);
}
exit(res ? errno : 0);
}
#else /* !LOCK_PROG */
#define MAILBOXORFILE "Mailbox"
extern int sgid;
#ifdef __linux__
#define rgid -2
#else
#ifdef BSD
extern int rgid;
#endif /* BSD */
#endif /* __linux__ */
#ifdef EXTERNAL_DOTLOCK
/* No saved-setgid, so fork just long enough to create the lockfile. */
lock_proc(filename, how)
char *filename, *how;
{
int kid, pid, status;
errno = 0;
switch (kid = fork()) {
case 0:
execle(DOLOCK_PATH, DOLOCK_NAME, how, filename, NULL, environ);
return kid;
case -1:
error("Unable to fork to change group id");
return -1;
default:
/* For SYSV, we're not doing SIGCLD handling, so go ahead
* and reap everything in sight until we get the one we want.
*/
while ((pid = wait(&status)) != -1 && pid != kid)
;
if (pid == kid)
errno = ((status & 0xf) == 0) ? (status >> 8) & 0xf : 0
return errno ? -1 : 0;
}
}
#endif /* EXTERNAL_DOTLOCK */
#endif /* LOCK_PROG */
dot_lock(filename)
char *filename;
{
char buf[MAXPATHLEN];
int lockfd, cnt = 0;
SIGRET (*oldint)(), (*oldquit)();
/* Note (YA): lockfd is *not* a fd and should not be closed,
* see xcreat.c!
*/
#ifndef LOCK_PROG
#if defined(SYSV) && !defined(HPUX) && !defined(IRIX4)
/* Only the spoolfile needs to be dot_locked -- other files are
* handled by lock_fopen, below. To avoid collisions with 14-char
* file name limits, we allow dot_locking ONLY of the spoolfile.
*/
if (issysspoolfile(filename) == 0)
return 0;
#ifdef EXTERNAL_DOTLOCK
return lock_proc(filename, DOLOCKIT);
#endif /* EXTERNAL_DOTLOCK */
#endif /* SYSV && !HPUX && !IRIX4 */
#ifdef BSD
setregid(rgid, sgid);
#else /* BSD */
setgid(sgid);
#endif /* BSD */
#endif /* !LOCK_PROG */
#ifdef M_XENIX
(void) sprintf(buf, "/tmp/%.10s.mlk", login);
#else /* M_XENIX */
(void) sprintf(buf, "%s.lock", filename);
#endif /* M_XENIX */
on_intr();
while ((lockfd = xcreat(buf, 0444)) == -1) {
if (errno != EEXIST) {
error("Unable to lock %s", filename);
break;
}
#ifndef SILENT_LOCKWAIT
if (cnt++ == 0)
print("%s \"%s\" already locked, waiting...",
MAILBOXORFILE, filename);
else
print_more(".");
#endif
sleep(DOT_LOCK_SLEEP);
if (ison(glob_flags, WAS_INTR)) {
#ifdef SILENT_LOCKWAIT
print("%s \"%s\" still locked, aborting\n",
MAILBOXORFILE, filename);
#else
print_more(" aborted!\n");
#endif
break;
}
}
off_intr();
#ifndef SILENT_LOCKWAIT
if (lockfd != -1) {
if (cnt)
print_more(" locked\n");
/* (void) close(lockfd); */
}
#endif
if (lockfd < 0)
if (cnt) errno = EAGAIN;
#ifdef LOCK_PROG
return lockfd < 0? (errno ? -1 : errno) : 0;
#else /* LOCK_PROG */
#ifdef BSD
setregid(sgid, rgid);
#else
setgid(rgid);
#endif /* BSD */
return lockfd == -1? -1 : 0;
#endif /* LOCK_PROG */
}
dot_unlock(filename)
char *filename;
{
char buf[MAXPATHLEN], *p;
#if !defined(M_XENIX) || defined(LOCK_PROG)
(void) sprintf(buf, "%s.lock", filename);
#ifndef LOCK_PROG
{
/* If the file was locked through open_file(), we may not have
* a complete pathname to work with here. Expand it and test
* whether we need to unlink at all. This should really be
* handled by having open_file() return the name it used, but
* that breaks too many other things at the moment.
*/
int isdir = 0;
p = getpath(buf, &isdir);
if (isdir)
return 0;
(void) strcpy(buf, p);
}
#if defined(SYSV) && !defined(HPUX) && !defined(IRIX4)
if (strncmp(spoolfile, buf, strlen(spoolfile)) != 0)
return 0;
#ifdef SVR2
p = rindex(buf, '.');
*p = 0;
#endif /* SVR2 */
#ifdef EXTERNAL_DOTLOCK
return lock_proc(buf, UNLOCKIT);
#endif
#endif /* SYSV && !HPUX && !IRIX4 */
#else /* LOCK_PROG */
errno = 0;
#endif /* !LOCK_PROG */
#else /* M_XENIX && !LOCK_PROG */
(void) sprintf(buf, "/tmp/%.10s.mlk", login);
#endif /* !M_XENIX || LOCK_PROG */
#ifndef LOCK_PROG
#ifdef BSD
setregid(rgid, sgid);
#else /* BSD */
setgid(sgid);
#endif /* BSD */
#endif /* !LOCK_PROG */
(void) unlink(buf);
#ifdef LOCK_PROG
return errno;
#else /* !LOCK_PROG */
#ifdef BSD
setregid(sgid, rgid);
#else
setgid(rgid);
#endif /* BSD */
#endif /* LOCK_PROG */
return 0;
}
#endif /* DOT_LOCK */
#ifndef LOCK_PROG
#ifdef SYSV
/*
* Define some BSD names for the SYSV world
*/
#ifndef __linux__
#ifdef USG
#define LOCK_SH F_RDLCK
#define LOCK_EX F_WRLCK
#define LOCK_UN F_UNLCK
#else /* USG */
#define LOCK_SH LK_LOCK
#define LOCK_EX LK_LOCK
#define LOCK_UN LK_UNLCK
#endif /* USG */
#define LOCK_NB 0 /* Always non-blocking in this case */
#endif /* __linux__ */
#ifdef EWOULDBLOCK
#undef EWOULDBLOCK
#endif /* EWOULDBLOCK */
#define EWOULDBLOCK EAGAIN
#ifndef F_SETLKW
#define F_SETLKW F_SETLK
#endif /* F_SETLKW */
flock(fd, op)
int fd, op;
{
#ifndef USG
(void) locking(fd, op, 0); /* old xenix (sys III) */
return 0;
#else
struct flock l;
l.l_len = 0L;
l.l_start = 0L;
l.l_whence = 1;
l.l_type = op;
return fcntl(fd, F_SETLKW, &l);
#endif /* USG */
}
#endif /* SYSV */
static struct options *exclude_list;
/* Quick'n'dirty test to avoid opening the same file multiple times.
* Fails if we aren't passed full paths or if the file is known by
* more than one name, but you can't have everything.
*/
static FILE *
exclusive_fopen(filename, mode)
char *filename, *mode;
{
struct options *tmp;
FILE *fp;
for (tmp = exclude_list; tmp; tmp = tmp->next)
if (strcmp(tmp->option, filename) == 0) {
errno = EWOULDBLOCK;
return NULL_FILE;
}
#ifdef DOT_LOCK
/* For additional assurance that each file is opened only once,
* and for ease of recovery from interrupts and errors, do the
* dot-locking here and unlock in exclusive_fclose().
*/
if (dot_lock(filename) != 0)
return NULL_FILE;
#endif /* DOT_LOCK */
if (!(fp = mask_fopen(filename, mode)))
return NULL_FILE;
if (tmp = (struct options *)malloc(sizeof(struct options))) {
tmp->option = savestr(filename);
tmp->value = (char *)fp;
/*
* NOTE: The LCKDFLDIR code below depends on this stackwise
* insertion to be able to close/reopen the file pointer.
* These routines therefore cannot cleanly be used outside
* of lock_fopen() and close_lock(), which handle LCKDFLDIR.
*/
tmp->next = exclude_list;
exclude_list = tmp;
return fp;
} else
(void) fclose(fp);
return NULL_FILE;
}
static int
exclusive_fclose(fileptr)
FILE *fileptr;
{
struct options *tmp1, *tmp2;
int n = 0;
for (tmp1 = tmp2 = exclude_list; tmp1; tmp2 = tmp1, tmp1 = tmp1->next)
if ((FILE *)(tmp1->value) == fileptr) {
if (tmp1 == tmp2)
exclude_list = tmp1->next;
else
tmp2->next = tmp1->next;
#ifdef DOT_LOCK
dot_unlock(tmp1->option);
#endif /* DOT_LOCK */
xfree(tmp1->option);
#ifndef LCKDFLDIR
/* LCKDFLDIR needs lk_fclose(), so let caller do it */
n = fclose(fileptr);
#endif /* !LCKDFLDIR */
xfree(tmp1);
break;
}
return n;
}
FILE *
lock_fopen(filename, mode)
char *filename;
char *mode;
{
FILE *mail_fp = NULL_FILE;
struct options exclude;
int fd, lk;
int cnt = 0;
SIGRET (*oldint)(), (*oldquit)();
#ifdef LCKDFLDIR
extern FILE *lk_fopen();
#endif /* !LCKDFLDIR */
if (debug && do_set(set_options, "deadlock")) {
(void) un_set(&set_options, "deadlock");
return NULL_FILE;
}
if (!(mail_fp = exclusive_fopen(filename, mode)))
return NULL_FILE;
fd = fileno(mail_fp);
if (mode[0] != 'r' || mode[1] == '+')
lk = LOCK_EX | LOCK_NB;
else
lk = LOCK_SH | LOCK_NB;
on_intr();
#ifdef LCKDFLDIR
(void) fclose(mail_fp);
while (isoff(glob_flags, WAS_INTR))
if (mail_fp = lk_fopen(filename, mode, NULL, NULL, 0)) {
/* See note in exclusive_fopen() above */
exclude_list->value = (char *)mail_fp;
break;
} else /* uses the open brace below the #endif LCKDFLDIR */
#else /* !LCKDFLDIR */
while (isoff(glob_flags, WAS_INTR) && flock(fd, lk))
#endif /* LCKDFLDIR */
{
#ifdef LCKDFLDIR
if (Access(filename, any(mode, "aw+") ? W_OK : R_OK) == 0)
#else /* !LCKDFLDIR */
if (errno == EWOULDBLOCK
#ifdef M_UNIX
|| errno == EACCES
#endif /* M_UNIX */
)
#endif /* LCKDFLDIR */
{
#ifndef SILENT_LOCKWAIT
if (isoff(glob_flags, REDIRECT))
if (!cnt++)
print("Waiting to lock \"%s\"...", filename);
else
print_more(".");
#endif
} else {
error("Unable to lock \"%s\"", filename);
(void) exclusive_fclose(mail_fp);
off_intr();
return NULL_FILE;
}
(void) fflush(stdout);
sleep(DOT_LOCK_SLEEP);
}
#ifndef SILENT_LOCKWAIT
if (cnt)
print_more(" locked\n");
#endif
cnt = (ison(glob_flags, WAS_INTR) != 0);
if (cnt) {
#ifdef SILENT_LOCKWAIT
print("Waiting to lock \"%s\" interrupted, aborting\n",
MAILBOXORFILE, filename);
#else
print_more(" aborted!\n");
#endif
}
off_intr();
if (cnt) {
(void) exclusive_fclose(mail_fp);
errno = EAGAIN;
return NULL_FILE;
}
return mail_fp;
}
/*ARGSUSED*/
close_lock(filename, fp)
char *filename;
FILE *fp;
{
#ifdef LCKDFLDIR
(void) exclusive_fclose(fp); /* Only removes the list elem */
return lk_fclose(fp, filename, NULL, NULL);
#else /* !LCKDFLDIR */
fflush(fp);
(void) flock(fileno(fp), LOCK_UN);
return exclusive_fclose(fp);
#endif /* LCKDFLDIR */
}
/* Make sure we don't leave dead lockfiles lying around */
void
droplocks()
{
while (exclude_list)
close_lock(exclude_list->option, (FILE *)exclude_list->value);
}
#endif /* LOCK_PROG */
|