4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/types.h>
34 #include <sys/param.h>
40 * Format a message telling why the lock could not be created.
44 file_lock_error(char *msg
, char *file
, int err
, const char *str
,
45 char *arg1
, char *arg2
, size_t mlen
)
50 (void) snprintf(msg
, mlen
, "Could not lock file `%s'; ", file
);
52 (void) snprintf(&msg
[len
], (mlen
- len
), str
, arg1
, arg2
);
53 (void) strcat(msg
, " failed - ");
54 if ((errstr
= strerror(err
)) != NULL
) {
55 (void) strlcat(msg
, errstr
, mlen
);
58 (void) sprintf(&msg
[len
], "errno %d", err
);
63 * This code stolen from the NSE library and changed to not depend
64 * upon any NSE routines or header files.
66 * Simple file locking.
67 * Create a symlink to a file. The "test and set" will be
68 * atomic as creating the symlink provides both functions.
70 * The timeout value specifies how long to wait for stale locks
71 * to disappear. If the lock is more than 'timeout' seconds old
72 * then it is ok to blow it away. This part has a small window
73 * of vunerability as the operations of testing the time,
74 * removing the lock and creating a new one are not atomic.
75 * It would be possible for two processes to both decide to blow
76 * away the lock and then have process A remove the lock and establish
77 * its own, and then then have process B remove the lock which accidentily
78 * removes A's lock rather than the stale one.
80 * A further complication is with the NFS. If the file in question is
81 * being served by an NFS server, then its time is set by that server.
82 * We can not use the time on the client machine to check for a stale
83 * lock. Therefore, a temp file on the server is created to get
84 * the servers current time.
86 * Returns an error message. NULL return means the lock was obtained.
90 file_lock(char *name
, char *lockname
, int timeout
)
96 char tmpname
[MAXPATHLEN
];
97 static char msg
[MAXPATHLEN
];
103 r
= symlink(name
, lockname
);
107 if (errno
!= EEXIST
) {
108 file_lock_error(msg
, name
, errno
,
109 (const char *)"symlink(%s, %s)", name
, lockname
,
115 r
= lstat(lockname
, &statb
);
118 * The lock must have just gone away - try
125 * With the NFS the time given a file is the time on
126 * the file server. This time may vary from the
127 * client's time. Therefore, we create a tmpfile in
128 * the same directory to establish the time on the
129 * server and use this time to see if the lock has
132 (void) snprintf(tmpname
, MAXPATHLEN
, "%s.XXXXXX",
134 (void) mktemp(tmpname
);
135 fd
= creat(tmpname
, 0666);
139 file_lock_error(msg
, name
, errno
,
140 (const char *)"creat(%s)", tmpname
, 0,
144 if (stat(tmpname
, &fs_statb
) == -1) {
145 file_lock_error(msg
, name
, errno
,
146 (const char *)"stat(%s)", tmpname
, 0,
150 (void) unlink(tmpname
);
151 if (statb
.st_mtime
+ timeout
< fs_statb
.st_mtime
) {
153 * The lock has expired - blow it away.
155 (void) unlink(lockname
);