10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <sys/syscall.h>
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * convert lockf() into fcntl() for SystemV compatibility
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate /* New SVR4 values */
420Sstevel@tonic-gate #define SV_GETLK 5
430Sstevel@tonic-gate #define SV_SETLK 6
440Sstevel@tonic-gate #define SV_SETLKW 7
450Sstevel@tonic-gate
46*722Smuffin int
lockf(int fildes,int function,long size)47*722Smuffin lockf(int fildes, int function, long size)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate struct flock ld;
50*722Smuffin int cmd;
510Sstevel@tonic-gate
520Sstevel@tonic-gate cmd = SV_SETLK; /* assume non-blocking operation */
530Sstevel@tonic-gate ld.l_type = F_WRLCK; /* lockf() only deals with exclusive locks */
540Sstevel@tonic-gate ld.l_whence = 1; /* lock always starts at current position */
550Sstevel@tonic-gate if (size < 0) {
560Sstevel@tonic-gate ld.l_start = size;
570Sstevel@tonic-gate ld.l_len = -size;
580Sstevel@tonic-gate } else {
590Sstevel@tonic-gate ld.l_start = 0L;
600Sstevel@tonic-gate ld.l_len = size;
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate switch (function) {
640Sstevel@tonic-gate case F_TEST:
650Sstevel@tonic-gate if (_syscall(SYS_fcntl, fildes, SV_GETLK, &ld) != -1) {
660Sstevel@tonic-gate if (ld.l_type == F_UNLCK) {
670Sstevel@tonic-gate ld.l_pid = ld.l_xxx;
680Sstevel@tonic-gate /* l_pid is the last field in the
690Sstevel@tonic-gate SVr3 flock structure */
700Sstevel@tonic-gate return (0);
710Sstevel@tonic-gate } else
720Sstevel@tonic-gate errno = EACCES; /* EAGAIN ?? */
730Sstevel@tonic-gate }
740Sstevel@tonic-gate return (-1);
750Sstevel@tonic-gate
760Sstevel@tonic-gate default:
770Sstevel@tonic-gate errno = EINVAL;
780Sstevel@tonic-gate return (-1);
790Sstevel@tonic-gate
800Sstevel@tonic-gate /* the rest fall thru to the fcntl() at the end */
810Sstevel@tonic-gate case F_ULOCK:
820Sstevel@tonic-gate ld.l_type = F_UNLCK;
830Sstevel@tonic-gate break;
840Sstevel@tonic-gate
850Sstevel@tonic-gate case F_LOCK:
860Sstevel@tonic-gate cmd = SV_SETLKW; /* block, if not available */
870Sstevel@tonic-gate break;
880Sstevel@tonic-gate
890Sstevel@tonic-gate case F_TLOCK:
900Sstevel@tonic-gate break;
910Sstevel@tonic-gate }
920Sstevel@tonic-gate if (_syscall(SYS_fcntl, fildes, cmd, &ld) == -1) {
930Sstevel@tonic-gate switch (errno) {
940Sstevel@tonic-gate /* this hack is purported to be for /usr/group compatibility */
950Sstevel@tonic-gate case ENOLCK:
960Sstevel@tonic-gate errno = EDEADLK;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate return(-1);
990Sstevel@tonic-gate } else {
1000Sstevel@tonic-gate ld.l_pid = ld.l_xxx; /* l_pid is the last field in the
1010Sstevel@tonic-gate SVr3 flock structure */
1020Sstevel@tonic-gate return(0);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate }
105