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]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
30 #pragma ident "%Z%%M% %I% %E% SMI"
35 /* #include <sys/types.h> */
36 /* #include <sys/stat.h> */
38 static struct stat _st_buf
;
39 static char lockname
[BUFSIZ
];
49 * make a lock file with given 'name'
50 * If one already exists, send a signal 0 to the process--if
51 * it fails, then unlink it and make a new one.
54 * name - name of the lock file to make
65 static char pid
[SIZEOFPID
+2] = { '\0' }; /* +2 for '\n' and NULL */
66 static char tempfile
[MAXNAMESIZE
];
73 (void) sprintf(pid
, "%*ld\n", SIZEOFPID
, (long) getpid());
74 (void) sprintf(tempfile
, "%s/LTMP.%ld", X_LOCKDIR
, (long) getpid());
77 #ifdef V8 /* this wouldn't be a problem if we used lock directories */
78 /* some day the truncation of system names will bite us */
79 cp
= rindex(name
, '/');
81 if (strlen(cp
) > MAXBASENAME
)
82 *(cp
+MAXBASENAME
) = NULLCHAR
;
84 if (onelock(pid
, tempfile
, name
) == -1) {
85 (void) unlink(tempfile
);
89 if (onelock(pid
, tempfile
, name
)) {
90 (void) unlink(tempfile
);
91 DEBUG(4,"ulockf failed in onelock()\n%s", "");
102 * check to see if the lock file exists and is still active
106 * 0 -> success (lock file removed - no longer active
107 * FAIL -> lock file still active
115 char alpid
[SIZEOFPID
+2]; /* +2 for '\n' and NULL */
118 fd
= open(name
, O_RDONLY
);
119 DEBUG(4, "ulockf name %s\n", name
);
121 if (errno
== ENOENT
) /* file does not exist -- OK */
123 DEBUG(4,"Lock File--can't read (errno %d) --remove it!\n", errno
);
126 ret
= read(fd
, (char *) alpid
, SIZEOFPID
+1); /* +1 for '\n' */
128 if (ret
!= (SIZEOFPID
+1)) {
130 DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
133 lpid
= (pid_t
) strtol(alpid
, (char **) NULL
, 10);
134 if ((ret
=kill(lpid
, 0)) == 0 || errno
== EPERM
) {
135 DEBUG(4, "Lock File--process still active--not removed\n%s", "");
138 else { /* process no longer active */
139 DEBUG(4, "kill pid (%ld), ", (long) lpid
);
140 DEBUG(4, "returned %d", ret
);
141 DEBUG(4, "--ok to remove lock file (%s)\n", name
);
145 if (unlink(name
) != 0) {
146 DEBUG(4,"ulockf failed in unlink()\n%s", "");
152 #define MAXLOCKS 10 /* maximum number of lock files */
153 static char *Lockfile
[MAXLOCKS
];
154 GLOBAL
int Nlocks
= 0;
157 * put name in list of lock files
168 for (i
= 0; i
< Nlocks
; i
++) {
169 if (Lockfile
[i
] == NULL
)
172 ASSERT(i
< MAXLOCKS
, "TOO MANY LOCKS", "", i
);
175 p
= (char*) calloc((unsigned) strlen(name
) + 1, sizeof (char));
176 ASSERT(p
!= NULL
, "CAN NOT ALLOCATE FOR", name
, 0);
177 (void) strcpy(p
, name
);
183 * remove the named lock. If named lock is NULL,
184 * then remove all locks currently in list.
196 cp
= rindex(name
, '/');
198 if (strlen(cp
) > MAXBASENAME
)
199 *(cp
+MAXBASENAME
) = NULLCHAR
;
203 for (i
= 0; i
< Nlocks
; i
++) {
204 if (Lockfile
[i
] == NULL
)
206 if (name
== NULL
|| EQUALS(name
, Lockfile
[i
])) {
207 (void) unlink(Lockfile
[i
]);
221 * pre - Path and first part of file name of the lock file to be
223 * s - The suffix part of the lock file. The name of the lock file
224 * will be derrived by concatenating pre, a period, and s.
234 char ln
[MAXNAMESIZE
];
236 (void) sprintf(ln
, "%s.%s", pre
, s
);
237 BASENAME(ln
, '/')[MAXBASENAME
] = '\0';
247 * pre - Path and first part of file name of the lock file to be
249 * name - The suffix part of the lock file. The name of the lock file
250 * will be derrived by concatenating pre, a period, and name.
261 char lname
[MAXNAMESIZE
];
264 * if name has a '/' in it, then it's a device name and it's
265 * not in /dev (i.e., it's a remotely-mounted device or it's
266 * in a subdirectory of /dev). in either case, creating our normal
267 * lockfile (/var/spool/locks/LCK..<dev>) is going to bomb if
268 * <dev> is "/remote/dev/term/14" or "/dev/net/foo/clone", so never
269 * mind. since we're using advisory filelocks on the devices
270 * themselves, it'll be safe.
272 * of course, programs and people who are used to looking at the
273 * lockfiles to find out what's going on are going to be a trifle
274 * misled. we really need to re-consider the lockfile naming structure
275 * to accomodate devices in directories other than /dev ... maybe in
278 if ( strchr(name
, '/') != NULL
)
280 (void) sprintf(lname
, "%s.%s", pre
, BASENAME(name
, '/'));
281 BASENAME(lname
, '/')[MAXBASENAME
] = '\0';
282 return(mklock(lname
));
286 * makes a lock on behalf of pid.
289 * tempfile - name of a temporary in the same file system
290 * name - lock file name (full path name)
293 * 0 - lock made successfully
296 onelock(pid
,tempfile
,name
)
298 char *tempfile
, *name
;
303 fd
=creat(tempfile
, (mode_t
) 0444);
305 (void) sprintf(cb
, "%s %s %d",tempfile
, name
, errno
);
306 logent("ULOCKC", cb
);
307 if((errno
== EMFILE
) || (errno
== ENFILE
))
308 (void) unlink(tempfile
);
312 if (write(fd
, pid
, SIZEOFPID
+1) != (SIZEOFPID
+1)) {
313 (void) sprintf(cb
, "%s %s %d",tempfile
, name
, errno
);
314 logent("ULOCKW", cb
);
315 (void) unlink(tempfile
);
318 (void) chmod(tempfile
, (mode_t
) 0444);
319 (void) chown(tempfile
, UUCPUID
, UUCPGID
);
321 if(link(tempfile
,name
)<0){
322 DEBUG(4, "%s: ", strerror(errno
));
323 DEBUG(4, "link(%s, ", tempfile
);
324 DEBUG(4, "%s)\n", name
);
325 if(unlink(tempfile
)< 0){
326 (void) sprintf(cb
, "ULK err %s %d", tempfile
, errno
);
327 logent("ULOCKLNK", cb
);
331 if(unlink(tempfile
)<0){
332 (void) sprintf(cb
, "%s %d",tempfile
,errno
);
333 logent("ULOCKF", cb
);
339 * fd_mklock(fd) - lock the device indicated by fd is possible
342 * SUCCESS - this process now has the fd locked
343 * FAIL - this process was not able to lock the fd
352 if ( fstat(fd
, &_st_buf
) != 0 )
355 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
356 (unsigned long) major(_st_buf
.st_dev
),
357 (unsigned long) major(_st_buf
.st_rdev
),
358 (unsigned long) minor(_st_buf
.st_rdev
));
360 if ( mklock(lockname
) == FAIL
)
363 while ( lockf(fd
, F_TLOCK
, 0L) != 0 ) {
364 DEBUG(7, "fd_mklock: lockf returns %d\n", errno
);
365 if ( (++tries
>= MAX_LOCKTRY
) || (errno
!= EAGAIN
) ) {
367 logent("fd_mklock","lockf failed");
372 DEBUG(7, "fd_mklock: ok\n%s", "");
377 * fn_cklock(name) - determine if the device indicated by name is locked
380 * SUCCESS - the name is not locked
381 * FAIL - the name is locked by another process
388 /* we temporarily use lockname to hold full path name */
389 (void) sprintf(lockname
, "%s%s", (*name
== '/' ? "" : "/dev/"), name
);
391 if ( stat(lockname
, &_st_buf
) != 0 )
394 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
395 (unsigned long) major(_st_buf
.st_dev
),
396 (unsigned long) major(_st_buf
.st_rdev
),
397 (unsigned long) minor(_st_buf
.st_rdev
));
399 return(cklock(lockname
));
403 * fd_cklock(fd) - determine if the device indicated by fd is locked
406 * SUCCESS - the fd is not locked
407 * FAIL - the fd is locked by another process
414 if ( fstat(fd
, &_st_buf
) != 0 )
417 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
418 (unsigned long) major(_st_buf
.st_dev
),
419 (unsigned long) major(_st_buf
.st_rdev
),
420 (unsigned long) minor(_st_buf
.st_rdev
));
422 if ( cklock(lockname
) == FAIL
)
425 return( lockf(fd
, F_TEST
, 0L) );
429 * remove the locks associated with the device file descriptor
432 * SUCCESS - both BNU lock file and advisory locks removed
440 if ( fstat(fd
, &_st_buf
) == 0 ) {
441 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
442 (unsigned long) major(_st_buf
.st_dev
),
443 (unsigned long) major(_st_buf
.st_rdev
),
444 (unsigned long) minor(_st_buf
.st_rdev
));
447 (void) lockf(fd
, F_ULOCK
, 0L);