xref: /titanic_52/usr/src/lib/libbc/libc/compat/common/lockf.c (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <sys/syscall.h>
36 
37 /*
38  * convert lockf() into fcntl() for SystemV compatibility
39  */
40 
41 /* New SVR4 values */
42 #define SV_GETLK	5
43 #define SV_SETLK	6
44 #define SV_SETLKW	7
45 
46 lockf(fildes, function, size)
47 	int fildes;
48 	int function;
49 	long size;
50 {
51 	struct flock ld;
52 	register int cmd;
53 
54 	cmd = SV_SETLK;		/* assume non-blocking operation */
55 	ld.l_type = F_WRLCK;	/* lockf() only deals with exclusive locks */
56 	ld.l_whence = 1;	/* lock always starts at current position */
57 	if (size < 0) {
58 		ld.l_start = size;
59 		ld.l_len = -size;
60 	} else {
61 		ld.l_start = 0L;
62 		ld.l_len = size;
63 	}
64 
65 	switch (function) {
66 	case F_TEST:
67 		if (_syscall(SYS_fcntl, fildes, SV_GETLK, &ld) != -1) {
68 			if (ld.l_type == F_UNLCK) {
69 				ld.l_pid = ld.l_xxx;
70 					/* l_pid is the last field in the
71 					   SVr3 flock structure */
72 				return (0);
73 			} else
74 				errno = EACCES;		/* EAGAIN ?? */
75 		}
76 		return (-1);
77 
78 	default:
79 		errno = EINVAL;
80 		return (-1);
81 
82 			/* the rest fall thru to the fcntl() at the end */
83 	case F_ULOCK:
84 		ld.l_type = F_UNLCK;
85 		break;
86 
87 	case F_LOCK:
88 		cmd = SV_SETLKW;	/* block, if not available */
89 		break;
90 
91 	case F_TLOCK:
92 		break;
93 	}
94 	if (_syscall(SYS_fcntl, fildes, cmd, &ld) == -1) {
95 		switch (errno) {
96 		/* this hack is purported to be for /usr/group compatibility */
97 		case ENOLCK:
98 			errno = EDEADLK;
99 		}
100 		return(-1);
101 	} else {
102 		ld.l_pid = ld.l_xxx;	/* l_pid is the last field in the
103 					   SVr3 flock structure */
104 		return(0);
105 	}
106 }
107