* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: amflock.c,v 1.17.4.6.6.1 2003/10/22 17:43:33 martinea Exp $
+ * $Id: amflock.c,v 1.28 2006/05/25 01:47:11 johnfranks Exp $
*
* file locking routines, put here to hide the system dependant stuff
* from the rest of the code
# endif
# if !defined(HAVE_FLOCK_DECL) && !defined(CONFIGURE_TEST)
- extern int flock P((int fd, int operation));
+ extern int flock(int fd, int operation);
# endif
#endif
** - returns errors for some non-files like pipes.
** - probably only works for files open for writing.
*/
-int use_lockf(fd, op)
-int fd; /* fd of file to operate on */
-int op; /* true to lock; false to unlock */
+int
+use_lockf(
+ int fd, /* fd of file to operate on */
+ int op) /* true to lock; false to unlock */
{
off_t pos;
/* Delete a lock file.
*/
-int delete_lock(fn)
-char *fn;
+int
+delete_lock(
+ char *fn)
{
int rc;
/* Create a lock file.
*/
-int create_lock(fn, pid)
-char *fn;
-long pid;
+int
+create_lock(
+ char *fn,
+ pid_t pid)
{
int fd;
FILE *f;
(void)delete_lock(fn); /* that's MY file! */
mask = umask(0027);
- fd = open(fn, O_WRONLY|O_CREAT|O_EXCL, 0640);
+ fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0640);
umask(mask);
if (fd == -1) return -1;
/* Read the pid out of a lock file.
** -1=error, otherwise pid.
*/
-long read_lock(fn)
-char *fn; /* name of lock file */
+long
+read_lock(
+ char * fn) /* name of lock file */
{
int save_errno;
FILE *f;
/* Link a lock if we can.
** 0=done, 1=already locked, -1=error.
*/
-int link_lock(lk, tlk)
-char *lk; /* real lock file */
-char *tlk; /* temp lock file */
+int
+link_lock(
+ char * lk, /* real lock file */
+ char * tlk) /* temp lock file */
{
int rc;
int serrno; /* saved errno */
/* Steal a lock if we can.
** 0=done; 1=still in use; -1 = error.
*/
-int steal_lock(fn, mypid, sres)
-char *fn; /* name of lock file to steal */
-long mypid; /* my process id */
-char *sres; /* name of steal-resource to lock */
+int
+steal_lock(
+ char * fn, /* name of lock file to steal */
+ pid_t mypid, /* my process id */
+ char * sres) /* name of steal-resource to lock */
{
int fd;
char buff[64];
/* Locking using existance of a file.
*/
-int ln_lock(res, op)
-char *res; /* name of resource to lock */
-int op; /* true to lock; false to unlock */
+int
+ln_lock(
+ char * res, /* name of resource to lock */
+ int op) /* true to lock; false to unlock */
{
long mypid;
char *lockfile = NULL;
/* lock the resource */
- ap_snprintf(pid_str, sizeof(pid_str), "%ld", mypid);
+ snprintf(pid_str, SIZEOF(pid_str), "%ld", mypid);
tlockfile = vstralloc(AMANDA_TMPDIR, "am", res, ".", pid_str, NULL);
(void)create_lock(tlockfile, mypid);
#endif
-/* Get a file lock (for read-only files).
-*/
-int amroflock(fd, resource)
-int fd;
-char *resource;
+/*
+ * Get a file lock (for read-only files).
+ */
+int
+amroflock(
+ int fd,
+ char * resource)
{
int r;
#ifdef USE_POSIX_FCNTL
+ (void)resource; /* Quiet unused paramater warning */
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
r = fcntl(fd, F_SETLKW, &lock);
#else
+ (void)fd; /* Quiet unused paramater warning */
r = amflock(fd, resource);
#endif
/* Get a file lock (for read/write files).
*/
-int amflock(fd, resource)
-int fd;
-char *resource;
+int
+amflock(
+ int fd,
+ char * resource)
{
int r;
#ifdef USE_POSIX_FCNTL
+ (void)resource; /* Quiet unused paramater warning */
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
r = fcntl(fd, F_SETLKW, &lock);
#else
#ifdef USE_FLOCK
+ (void)resource; /* Quiet unused paramater warning */
r = flock(fd, LOCK_EX);
#else
#ifdef USE_LOCKF
+ (void)resource; /* Quiet unused paramater warning */
r = use_lockf(fd, 1);
#else
#ifdef USE_LNLOCK
+ (void)fd; /* Quiet unused paramater warning */
r = ln_lock(resource, 1);
#else
+ (void)fd; /* Quiet unused paramater warning */
+ (void)resource; /* Quiet unused paramater warning */
r = 0;
#endif
#endif
/* Release a file lock.
*/
-int amfunlock(fd, resource)
-int fd;
-char *resource;
+int
+amfunlock(
+ int fd,
+ char * resource)
{
int r;
#ifdef USE_POSIX_FCNTL
+ (void)resource; /* Quiet unused paramater warning */
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
r = fcntl(fd, F_SETLK, &lock);
#else
#ifdef USE_FLOCK
+ (void)resource; /* Quiet unused paramater warning */
r = flock(fd, LOCK_UN);
#else
#ifdef USE_LOCKF
+ (void)fd; /* Quiet unused paramater warning */
r = use_lockf(fd, 0);
#else
#ifdef USE_LNLOCK
+ (void)fd; /* Quiet unused paramater warning */
r = ln_lock(resource, 0);
#else
+ (void)fd; /* Quiet unused paramater warning */
+ (void)resource; /* Quiet unused paramater warning */
r = 0;
#endif
#endif
** for ever.
*/
#ifdef CONFIGURE_TEST
-main()
+int
+main(
+ int argc,
+ char **argv)
{
int lockfd;
char *filen = "/tmp/conftest.lock";
char *resn = "test";
- int fd;
-
- for(fd = 3; fd < FD_SETSIZE; fd++) {
- /*
- * Make sure nobody spoofs us with a lot of extra open files
- * that would cause an open we do to get a very high file
- * descriptor, which in turn might be used as an index into
- * an array (e.g. an fd_set).
- */
- close(fd);
- }
+
+ (void)argc; /* Quiet compiler warning */
+ (void)argv; /* Quiet compiler warning */
unlink(filen);
- if ((lockfd = open(filen, O_RDONLY|O_CREAT|O_EXCL, 0600)) == -1) {
+ if ((lockfd = open(filen, O_RDONLY | O_CREAT | O_EXCL, 0600)) == -1) {
perror (filen);
exit(10);
}
close(lockfd);
unlink(filen);
- if ((lockfd = open(filen, O_WRONLY|O_CREAT|O_EXCL, 0600)) == -1) {
+ if ((lockfd = open(filen, O_WRONLY | O_CREAT | O_EXCL, 0600)) == -1) {
perror (filen);
exit(20);
}