2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 /* moved from amflock.c by Dustin J. Mitchell <dustin@zmanda.com> */
31 static int ln_lock(char *res, int op);
32 char *_lnlock_dir = AMANDA_TMPDIR; /* amflock-test changes this; it's a constant otherwise */
34 /* XXX - error checking in this section needs to be tightened up */
36 /* Delete a lock file.
45 if (rc != 0 && errno == ENOENT) rc = 0;
50 /* Create a lock file.
61 (void)delete_lock(fn); /* that's MY file! */
64 fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0640);
66 if (fd == -1) return -1;
68 if((f = fdopen(fd, "w")) == NULL) {
72 g_fprintf(f, "%ld\n", pid);
78 /* Read the pid out of a lock file.
79 ** -1=error, otherwise pid.
83 char * fn) /* name of lock file */
89 if ((f = fopen(fn, "r")) == NULL) {
92 if (fscanf(f, "%ld", &pid) != 1) {
104 /* Link a lock if we can.
105 ** 0=done, 1=already locked, -1=error.
109 char * lk, /* real lock file */
110 char * tlk) /* temp lock file */
113 int serrno; /* saved errno */
114 struct stat lkstat, tlkstat;
116 /* an atomic check and set operation */
118 if (rc == 0) return 0; /* XXX do we trust it? */
120 /* link() says it failed - don't beleive it */
123 if (stat(lk, &lkstat) == 0 &&
124 stat(tlk, &tlkstat) == 0 &&
125 lkstat.st_ino == tlkstat.st_ino)
126 return 0; /* it did work! */
130 if (errno == EEXIST) rc = 1;
135 /* Steal a lock if we can.
136 ** 0=done; 1=still in use; -1 = error.
140 char * fn, /* name of lock file to steal */
141 long mypid, /* my process id */
142 char * sres) /* name of steal-resource to lock */
147 /* prevent a race with another stealer */
148 rc = ln_lock(sres, 1);
149 if (rc != 0) goto error;
153 if (errno == ENOENT) goto done;
157 if (pid == mypid) goto steal; /* i'm the locker! */
159 /* are they still there ? */
160 rc = kill((pid_t)pid, 0);
162 if (errno == ESRCH) goto steal; /* locker has gone */
166 rc = ln_lock(sres, 0);
167 if (rc != 0) goto error;
172 rc = delete_lock(fn);
173 if (rc != 0) goto error;
176 rc = ln_lock(sres, 0);
177 if (rc != 0) goto error;
182 rc = ln_lock(sres, 0);
189 char * res, /* name of resource to lock */
190 int op) /* true to lock; false to unlock */
193 char *lockfile = NULL;
194 char *tlockfile = NULL;
197 char pid_str[NUM_STR_SIZE];
199 mypid = (long)getpid();
201 lockfile = vstralloc(_lnlock_dir, "/am", res, ".lock", NULL);
204 /* unlock the resource */
205 assert(read_lock(lockfile) == mypid);
207 (void)delete_lock(lockfile);
212 /* lock the resource */
214 g_snprintf(pid_str, SIZEOF(pid_str), "%ld", mypid);
215 tlockfile = vstralloc(_lnlock_dir, "/am", res, ".", pid_str, NULL);
217 (void)create_lock(tlockfile, mypid);
219 mres = stralloc2(res, ".");
222 rc = link_lock(lockfile, tlockfile);
226 rc = steal_lock(lockfile, mypid, mres);
228 if (rc == 0) continue;
232 (void) delete_lock(tlockfile);
243 G_GNUC_UNUSED int fd,
246 return ln_lock(resource, 1);
251 G_GNUC_UNUSED int fd,
254 return ln_lock(resource, 0);
257 amflock_impl_t amflock_lnlock_impl = {
259 lnlock_lock, /* no read-only support */