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 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
36 /* #include <sys/types.h> */
37 /* #include <sys/stat.h> */
39 static struct stat _st_buf
;
40 static char lockname
[BUFSIZ
];
50 * make a lock file with given 'name'
51 * If one already exists, send a signal 0 to the process--if
52 * it fails, then unlink it and make a new one.
55 * name - name of the lock file to make
66 static char pid
[SIZEOFPID
+2] = { '\0' }; /* +2 for '\n' and NULL */
67 static char tempfile
[MAXNAMESIZE
];
74 (void) sprintf(pid
, "%*ld\n", SIZEOFPID
, (long) getpid());
75 (void) sprintf(tempfile
, "%s/LTMP.%ld", X_LOCKDIR
, (long) getpid());
78 #ifdef V8 /* this wouldn't be a problem if we used lock directories */
79 /* some day the truncation of system names will bite us */
80 cp
= rindex(name
, '/');
82 if (strlen(cp
) > MAXBASENAME
)
83 *(cp
+MAXBASENAME
) = NULLCHAR
;
85 if (onelock(pid
, tempfile
, name
) == -1) {
86 (void) unlink(tempfile
);
90 if (onelock(pid
, tempfile
, name
)) {
91 (void) unlink(tempfile
);
92 DEBUG(4,"ulockf failed in onelock()\n%s", "");
103 * check to see if the lock file exists and is still active
107 * 0 -> success (lock file removed - no longer active
108 * FAIL -> lock file still active
116 char alpid
[SIZEOFPID
+2]; /* +2 for '\n' and NULL */
119 fd
= open(name
, O_RDONLY
);
120 DEBUG(4, "ulockf name %s\n", name
);
122 if (errno
== ENOENT
) /* file does not exist -- OK */
124 DEBUG(4,"Lock File--can't read (errno %d) --remove it!\n", errno
);
127 ret
= read(fd
, (char *) alpid
, SIZEOFPID
+1); /* +1 for '\n' */
129 if (ret
!= (SIZEOFPID
+1)) {
131 DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
134 lpid
= (pid_t
) strtol(alpid
, (char **) NULL
, 10);
135 if ((ret
=kill(lpid
, 0)) == 0 || errno
== EPERM
) {
136 DEBUG(4, "Lock File--process still active--not removed\n%s", "");
139 else { /* process no longer active */
140 DEBUG(4, "kill pid (%ld), ", (long) lpid
);
141 DEBUG(4, "returned %d", ret
);
142 DEBUG(4, "--ok to remove lock file (%s)\n", name
);
146 if (unlink(name
) != 0) {
147 DEBUG(4,"ulockf failed in unlink()\n%s", "");
153 #define MAXLOCKS 10 /* maximum number of lock files */
154 static char *Lockfile
[MAXLOCKS
];
155 GLOBAL
int Nlocks
= 0;
158 * put name in list of lock files
169 for (i
= 0; i
< Nlocks
; i
++) {
170 if (Lockfile
[i
] == NULL
)
173 ASSERT(i
< MAXLOCKS
, "TOO MANY LOCKS", "", i
);
176 p
= (char*) calloc((unsigned) strlen(name
) + 1, sizeof (char));
177 ASSERT(p
!= NULL
, "CAN NOT ALLOCATE FOR", name
, 0);
178 (void) strcpy(p
, name
);
184 * remove the named lock. If named lock is NULL,
185 * then remove all locks currently in list.
197 cp
= rindex(name
, '/');
199 if (strlen(cp
) > MAXBASENAME
)
200 *(cp
+MAXBASENAME
) = NULLCHAR
;
204 for (i
= 0; i
< Nlocks
; i
++) {
205 if (Lockfile
[i
] == NULL
)
207 if (name
== NULL
|| EQUALS(name
, Lockfile
[i
])) {
208 (void) unlink(Lockfile
[i
]);
222 * pre - Path and first part of file name of the lock file to be
224 * s - The suffix part of the lock file. The name of the lock file
225 * will be derrived by concatenating pre, a period, and s.
235 char ln
[MAXNAMESIZE
];
237 (void) sprintf(ln
, "%s.%s", pre
, s
);
238 BASENAME(ln
, '/')[MAXBASENAME
] = '\0';
248 * pre - Path and first part of file name of the lock file to be
250 * name - The suffix part of the lock file. The name of the lock file
251 * will be derrived by concatenating pre, a period, and name.
262 char lname
[MAXNAMESIZE
];
265 * if name has a '/' in it, then it's a device name and it's
266 * not in /dev (i.e., it's a remotely-mounted device or it's
267 * in a subdirectory of /dev). in either case, creating our normal
268 * lockfile (/var/spool/locks/LCK..<dev>) is going to bomb if
269 * <dev> is "/remote/dev/term/14" or "/dev/net/foo/clone", so never
270 * mind. since we're using advisory filelocks on the devices
271 * themselves, it'll be safe.
273 * of course, programs and people who are used to looking at the
274 * lockfiles to find out what's going on are going to be a trifle
275 * misled. we really need to re-consider the lockfile naming structure
276 * to accomodate devices in directories other than /dev ... maybe in
279 if ( strchr(name
, '/') != NULL
)
281 (void) sprintf(lname
, "%s.%s", pre
, BASENAME(name
, '/'));
282 BASENAME(lname
, '/')[MAXBASENAME
] = '\0';
283 return(mklock(lname
));
287 * makes a lock on behalf of pid.
290 * tempfile - name of a temporary in the same file system
291 * name - lock file name (full path name)
294 * 0 - lock made successfully
297 onelock(pid
,tempfile
,name
)
299 char *tempfile
, *name
;
304 fd
=creat(tempfile
, (mode_t
) 0444);
306 (void) sprintf(cb
, "%s %s %d",tempfile
, name
, errno
);
307 logent("ULOCKC", cb
);
308 if((errno
== EMFILE
) || (errno
== ENFILE
))
309 (void) unlink(tempfile
);
313 if (write(fd
, pid
, SIZEOFPID
+1) != (SIZEOFPID
+1)) {
314 (void) sprintf(cb
, "%s %s %d",tempfile
, name
, errno
);
315 logent("ULOCKW", cb
);
316 (void) unlink(tempfile
);
319 (void) chmod(tempfile
, (mode_t
) 0444);
320 (void) chown(tempfile
, UUCPUID
, UUCPGID
);
322 if(link(tempfile
,name
)<0){
323 DEBUG(4, "%s: ", strerror(errno
));
324 DEBUG(4, "link(%s, ", tempfile
);
325 DEBUG(4, "%s)\n", name
);
326 if(unlink(tempfile
)< 0){
327 (void) sprintf(cb
, "ULK err %s %d", tempfile
, errno
);
328 logent("ULOCKLNK", cb
);
332 if(unlink(tempfile
)<0){
333 (void) sprintf(cb
, "%s %d",tempfile
,errno
);
334 logent("ULOCKF", cb
);
340 * fd_mklock(fd) - lock the device indicated by fd if possible
343 * SUCCESS - this process now has the fd locked
344 * FAIL - this process was not able to lock the fd
353 if ( fstat(fd
, &_st_buf
) != 0 )
356 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
357 (unsigned long) major(_st_buf
.st_dev
),
358 (unsigned long) major(_st_buf
.st_rdev
),
359 (unsigned long) minor(_st_buf
.st_rdev
));
361 if ( mklock(lockname
) == FAIL
)
364 while ( lockf(fd
, F_TLOCK
, 0L) != 0 ) {
365 DEBUG(7, "fd_mklock: lockf returns %d\n", errno
);
366 if ( (++tries
>= MAX_LOCKTRY
) || (errno
!= EAGAIN
) ) {
368 logent("fd_mklock","lockf failed");
373 DEBUG(7, "fd_mklock: ok\n%s", "");
378 * fn_cklock(name) - determine if the device indicated by name is locked
381 * SUCCESS - the name is not locked
382 * FAIL - the name is locked by another process
389 /* we temporarily use lockname to hold full path name */
390 (void) sprintf(lockname
, "%s%s", (*name
== '/' ? "" : "/dev/"), name
);
392 if ( stat(lockname
, &_st_buf
) != 0 )
395 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
396 (unsigned long) major(_st_buf
.st_dev
),
397 (unsigned long) major(_st_buf
.st_rdev
),
398 (unsigned long) minor(_st_buf
.st_rdev
));
400 return(cklock(lockname
));
404 * fd_cklock(fd) - determine if the device indicated by fd is locked
407 * SUCCESS - the fd is not locked
408 * FAIL - the fd is locked by another process
415 if ( fstat(fd
, &_st_buf
) != 0 )
418 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
419 (unsigned long) major(_st_buf
.st_dev
),
420 (unsigned long) major(_st_buf
.st_rdev
),
421 (unsigned long) minor(_st_buf
.st_rdev
));
423 if ( cklock(lockname
) == FAIL
)
426 return( lockf(fd
, F_TEST
, 0L) );
430 * remove the locks associated with the device file descriptor
433 * SUCCESS - both BNU lock file and advisory locks removed
441 if ( fstat(fd
, &_st_buf
) == 0 ) {
442 (void) sprintf(lockname
, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK
,
443 (unsigned long) major(_st_buf
.st_dev
),
444 (unsigned long) major(_st_buf
.st_rdev
),
445 (unsigned long) minor(_st_buf
.st_rdev
));
448 (void) lockf(fd
, F_ULOCK
, 0L);
453 #include <sys/file.h>
471 cmd
= LOCK_EX
|LOCK_NB
;
477 if (flock(fd
, LOCK_EX
|LOCK_NB
) == 0 && flock(fd
, LOCK_UN
) == 0)
485 ret
= flock(fd
, cmd
);
486 if (ret
< 0 && errno
== EWOULDBLOCK
)