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
|
/* A program to lock a file exactly as Exim would, for investigation of
interlocking problems.
Options: -fcntl use fcntl lock only
-lockfile use lock file only
-mbx use mbx-lock only
Default is -fcntl -lockfile.
Argument: the name of the lock file
*/
#include "os.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <pwd.h>
#define FALSE 0
#define TRUE 1
/* Flag for timeout signal handler */
static int sigalrm_seen = FALSE;
/* We need to pull in strerror() and os_non_restarting_signal() from the
os.c source, if they are required for this OS. However, we don't need any of
the other stuff in os.c, so force the other macros to omit it. */
#ifndef OS_RESTARTING_SIGNAL
#define OS_RESTARTING_SIGNAL
#endif
#ifndef OS_STRSIGNAL
#define OS_STRSIGNAL
#endif
#ifndef OS_STREXIT
#define OS_STREXIT
#endif
#ifndef OS_LOAD_AVERAGE
#define OS_LOAD_AVERAGE
#endif
#ifndef FIND_RUNNING_INTERFACES
#define FIND_RUNNING_INTERFACES
#endif
#include "../src/os.c"
/*************************************************
* Timeout handler *
*************************************************/
static void
sigalrm_handler(int sig)
{
sig = sig; /* Keep picky compilers happy */
sigalrm_seen = TRUE;
}
/*************************************************
* Give usage and die *
*************************************************/
static void
usage(void)
{
printf("usage: exim_lock [-v] [-q] [-lockfile] [-fcntl | -mbx]\n"
" [-retries <n>] [-interval <n>] [-timeout <n>]\n"
" <file name> [command]\n");
exit(1);
}
/*************************************************
* Apply a lock to a file descriptor *
*************************************************/
static int
apply_lock(int fd, int type, int timeout)
{
int yield;
struct flock lock_data;
lock_data.l_type = type;
lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
sigalrm_seen = FALSE;
if (timeout > 0)
{
int save_errno;
os_non_restarting_signal(SIGALRM, sigalrm_handler);
alarm(timeout);
yield = fcntl(fd, F_SETLKW, &lock_data);
save_errno = errno;
alarm(0);
signal(SIGALRM, SIG_IGN);
errno = save_errno;
}
else yield = fcntl(fd, F_SETLK, &lock_data);
return yield;
}
/*************************************************
* The exim_lock program *
*************************************************/
int main(int argc, char **argv)
{
int lock_retries = 10;
int lock_interval = 3;
int lock_fcntl_timeout = 0;
int use_lockfile = FALSE;
int use_fcntl = FALSE;
int use_mbx = FALSE;
int verbose = FALSE;
int quiet = FALSE;
int i, j, len;
int fd = -1;
int hd = -1;
int md = -1;
int yield = 0;
int now = time(NULL);
char *filename;
char *lockname = NULL, *hitchname = NULL;
char *primary_hostname, *command;
struct utsname s;
char buffer[256];
char tempname[256];
/* Decode options */
for (i = 1; i < argc; i++)
{
char *arg = argv[i];
if (*arg != '-') break;
if (strcmp(arg, "-fcntl") == 0) use_fcntl = TRUE;
else if (strcmp(arg, "-lockfile") == 0) use_lockfile = TRUE;
else if (strcmp(arg, "-mbx") == 0) use_mbx = TRUE;
else if (strcmp(arg, "-v") == 0) verbose = TRUE;
else if (strcmp(arg, "-q") == 0) quiet = TRUE;
else if (++i < argc)
{
int value = atoi(argv[i]);
if (strcmp(arg, "-retries") == 0) lock_retries = value;
else if (strcmp(arg, "-interval") == 0) lock_interval = value;
else if (strcmp(arg, "-timeout") == 0) lock_fcntl_timeout = value;
else usage();
}
else usage();
}
if (quiet) verbose = 0;
/* Default is to use both non-MBX kinds of lock; fcntl and mbx are exclusive */
if (!use_lockfile && !use_fcntl && !use_mbx) use_lockfile = use_fcntl = TRUE;
if (use_fcntl && use_mbx) usage();
/* A file name is required */
if (i >= argc) usage();
filename = argv[i++];
/* Expand file names starting with ~ */
if (*filename == '~')
{
struct passwd *pw;
if (*(++filename) == '/')
pw = getpwuid(getuid());
else
{
char *s = buffer;
while (*filename != 0 && *filename != '/')
*s++ = *filename++;
*s = 0;
pw = getpwnam(buffer);
}
if (pw == NULL)
{
printf("exim_lock: unable to expand file name %s\n", argv[i-1]);
exit(1);
}
if ((int)strlen(pw->pw_dir) + (int)strlen(filename) + 1 > sizeof(buffer))
{
printf("exim_lock: expanded file name %s%s is too long", pw->pw_dir,
filename);
exit(1);
}
strcpy(buffer, pw->pw_dir);
strcat(buffer, filename);
filename = buffer;
}
/* If using a lock file, prepare by creating the lock file name and
the hitching post name. */
if (use_lockfile)
{
if (uname(&s) < 0)
{
printf("exim_lock: failed to find host name using uname()\n");
exit(1);
}
primary_hostname = s.nodename;
len = (int)strlen(filename);
lockname = malloc(len + 8);
sprintf(lockname, "%s.lock", filename);
hitchname = malloc(len + 32 + (int)strlen(primary_hostname));
sprintf(hitchname, "%s.%s.%08x.%08x", lockname, primary_hostname,
now, (int)getpid());
if (verbose)
printf("exim_lock: lockname = %s\n hitchname = %s\n", lockname,
hitchname);
}
/* Locking retry loop */
for (j = 0; j < lock_retries; j++)
{
int sleep_before_retry = TRUE;
struct stat statbuf;
/* Try to build a lock file if so configured */
if (use_lockfile)
{
int rc;
if (verbose) printf("exim_lock: creating lock file\n");
hd = open(hitchname, O_WRONLY | O_CREAT | O_EXCL, 0440);
if (hd < 0)
{
printf("exim_lock: failed to create hitching post %s: %s\n", hitchname,
strerror(errno));
exit(1);
}
/* Apply hitching post algorithm. */
if ((rc = link(hitchname, lockname)) != 0) fstat(hd, &statbuf);
close(hd);
unlink(hitchname);
if (rc != 0 && statbuf.st_nlink != 2)
{
printf("exim_lock: failed to link hitching post to lock file\n");
hd = -1;
goto RETRY;
}
if (!quiet) printf("exim_lock: lock file successfully created\n");
}
/* We are done if no other locking required. */
if (!use_fcntl && !use_mbx) break;
/* Open the file for writing. */
fd = open(filename, O_RDWR + O_APPEND);
if (fd < 0)
{
printf("exim_lock: failed to open %s for writing: %s\n", filename,
strerror(errno));
yield = 1;
goto CLEAN_UP;
}
/* If there is a timeout, implying blocked locking, we don't want to
sleep before any retries after this. */
if (lock_fcntl_timeout > 0) sleep_before_retry = FALSE;
/* Lock using fcntl. There are pros and cons to using a blocking call vs
a non-blocking call and retries. Exim is non-blocking by default, but setting
a timeout changes it to blocking. */
if (!use_mbx)
{
if (apply_lock(fd, F_WRLCK, lock_fcntl_timeout) >= 0)
{
if (!quiet) printf("exim_lock: fcntl() lock successfully applied\n");
break;
}
else
{
printf("exim_lock: fcntl() failed: %s\n", strerror(errno));
goto RETRY;
}
}
/* Lock using MBX rules. This is complicated and is documented with the
source of the c-client library that goes with Pine and IMAP. What has to
be done to interwork correctly is to take out a shared lock on the mailbox,
and an exclusive lock on a /tmp file. */
else
{
if (apply_lock(fd, F_RDLCK, lock_fcntl_timeout) >= 0)
{
if (!quiet) printf("exim_lock: fcntl() read lock successfully applied\n");
}
else
{
printf("exim_lock: fcntl() read lock failed: %s\n", strerror(errno));
goto RETRY;
}
if (fstat(fd, &statbuf) < 0)
{
printf("exim_lock: fstat() of %s failed: %s\n", filename,
strerror(errno));
yield = 1;
goto CLEAN_UP;
}
/* Set up file in /tmp and check its state if already existing. */
sprintf(tempname, "/tmp/.%lx.%lx", (long)statbuf.st_dev,
(long)statbuf.st_ino);
if (lstat(tempname, &statbuf) >= 0)
{
if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
{
printf("exim_lock: symbolic link on lock name %s\n", tempname);
yield = 1;
goto CLEAN_UP;
}
if (statbuf.st_nlink > 1)
{
printf("exim_lock: hard link to lock name %s\n", tempname);
yield = 1;
goto CLEAN_UP;
}
}
md = open(tempname, O_RDWR | O_CREAT, 0600);
if (md < 0)
{
printf("exim_lock: failed to create mbx lock file %s: %s\n",
tempname, strerror(errno));
goto CLEAN_UP;
}
chmod (tempname, 0600);
if (apply_lock(md, F_WRLCK, lock_fcntl_timeout) >= 0)
{
if (!quiet) printf("exim_lock: fcntl() lock successfully applied to mbx "
"lock file %s\n", tempname);
break;
}
else
{
printf("exim_lock: fcntl() failed for %s: %s\n", tempname,
strerror(errno));
goto RETRY;
}
}
/* Clean up before retrying */
RETRY:
if (md >= 0)
{
if (close(md) < 0)
printf("exim_lock: close %s failed: %s\n", tempname, strerror(errno));
else
if (!quiet) printf("exim_lock: %s closed\n", tempname);
md = -1;
}
if (fd >= 0)
{
if (close(fd) < 0)
printf("exim_lock: close failed: %s\n", strerror(errno));
else
if (!quiet) printf("exim_lock: file closed\n");
fd = -1;
}
if (hd >= 0)
{
if (unlink(lockname) < 0)
printf("exim_lock: unlink of %s failed: %s\n", lockname, strerror(errno));
else
if (!quiet) printf("exim_lock: lock file removed\n");
hd = -1;
}
/* If a blocking call timed out, break the retry loop if the total time
so far is not less than than retries * interval. */
if (sigalrm_seen &&
(j + 1) * lock_fcntl_timeout >= lock_retries * lock_interval)
j = lock_retries;
/* Wait a bit before retrying, except when it was a blocked fcntl() that
caused the problem. */
if (j < lock_retries && sleep_before_retry)
{
printf(" ... waiting\n");
sleep(lock_interval);
}
}
if (j >= lock_retries)
{
printf("exim_lock: locking failed too many times\n");
yield = 1;
goto CLEAN_UP;
}
if (!quiet) printf("exim_lock: locking %s succeeded: ", filename);
/* If there are no further arguments, run the user's shell; otherwise
the next argument is a command to run. */
if (i >= argc)
{
command = getenv("SHELL");
if (command == NULL || *command == 0) command = "/bin/sh";
if (!quiet) printf("running %s ...\n", command);
}
else
{
command = argv[i];
if (!quiet) printf("running the command ...\n");
}
/* Run the command */
system(command);
/* Remove the locks and exit. Unlink the /tmp file if we can get an exclusive
lock on the mailbox. This should be a non-blocking lock call, as there is no
point in waiting. */
CLEAN_UP:
if (md >= 0)
{
struct flock lock_data;
lock_data.l_type = F_WRLCK;
lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
if (fcntl(fd, F_SETLK, &lock_data) >= 0)
{
if (!quiet) printf("exim_lock: %s unlinked - no sharers\n", tempname);
unlink(tempname);
}
else if (!quiet)
printf("exim_lock: %s not unlinked - unable to get exclusive mailbox lock\n",
tempname);
if (close(md) < 0)
printf("exim_lock: close %s failed: %s\n", tempname, strerror(errno));
else
if (!quiet) printf("exim_lock: %s closed\n", tempname);
}
if (fd >= 0)
{
if (close(fd) < 0)
printf("exim_lock: close %s failed: %s\n", filename, strerror(errno));
else
if (!quiet) printf("exim_lock: %s closed\n", filename);
}
if (hd >= 0)
{
if (unlink(lockname) < 0)
printf("exim_lock: unlink %s failed: %s\n", lockname, strerror(errno));
else
if (!quiet) printf("exim_lock: lock file removed\n");
}
return yield;
}
/* End */
|