xref: /onnv-gate/usr/src/lib/libc/port/sys/lockf.c (revision 6812:febeba71273d)
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
55891Sraf  * Common Development and Distribution License (the "License").
65891Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215891Sraf 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 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) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*6812Sraf 
320Sstevel@tonic-gate #include <sys/feature_tests.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
355891Sraf #define	__lockf		__lockf64
360Sstevel@tonic-gate #endif
370Sstevel@tonic-gate 
38*6812Sraf #include "lint.h"
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <fcntl.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate int
__lockf(int fildes,int function,off_t size)455891Sraf __lockf(int fildes, int function, off_t size)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 	struct flock l;
480Sstevel@tonic-gate 	int rv;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	l.l_whence = 1;
510Sstevel@tonic-gate 	if (size < 0) {
520Sstevel@tonic-gate 		l.l_start = size;
530Sstevel@tonic-gate 		l.l_len = -size;
540Sstevel@tonic-gate 	} else {
550Sstevel@tonic-gate 		l.l_start = (off_t)0;
560Sstevel@tonic-gate 		l.l_len = size;
570Sstevel@tonic-gate 	}
580Sstevel@tonic-gate 	switch (function) {
590Sstevel@tonic-gate 	case F_ULOCK:
600Sstevel@tonic-gate 		l.l_type = F_UNLCK;
610Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLK, &l);
620Sstevel@tonic-gate 		break;
630Sstevel@tonic-gate 	case F_LOCK:
640Sstevel@tonic-gate 		l.l_type = F_WRLCK;
650Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLKW, &l);
660Sstevel@tonic-gate 		break;
670Sstevel@tonic-gate 	case F_TLOCK:
680Sstevel@tonic-gate 		l.l_type = F_WRLCK;
690Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLK, &l);
700Sstevel@tonic-gate 		break;
710Sstevel@tonic-gate 	case F_TEST:
720Sstevel@tonic-gate 		l.l_type = F_WRLCK;
730Sstevel@tonic-gate 		rv = fcntl(fildes, F_GETLK, &l);
740Sstevel@tonic-gate 		if (rv != -1) {
750Sstevel@tonic-gate 			if (l.l_type == F_UNLCK)
760Sstevel@tonic-gate 				return (0);
770Sstevel@tonic-gate 			else {
780Sstevel@tonic-gate 				errno = EAGAIN;
790Sstevel@tonic-gate 				return (-1);
800Sstevel@tonic-gate 			}
810Sstevel@tonic-gate 		}
820Sstevel@tonic-gate 		break;
830Sstevel@tonic-gate 	default:
840Sstevel@tonic-gate 		errno = EINVAL;
850Sstevel@tonic-gate 		return (-1);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	if (rv < 0) {
880Sstevel@tonic-gate 		switch (errno) {
890Sstevel@tonic-gate 		case EMFILE:
900Sstevel@tonic-gate 		case ENOSPC:
910Sstevel@tonic-gate 		case ENOLCK:
920Sstevel@tonic-gate 			/*
930Sstevel@tonic-gate 			 * A deadlock error is given if we run out of resources,
940Sstevel@tonic-gate 			 * in compliance with /usr/group standards.
950Sstevel@tonic-gate 			 */
960Sstevel@tonic-gate 			errno = EDEADLK;
970Sstevel@tonic-gate 			break;
980Sstevel@tonic-gate 		default:
990Sstevel@tonic-gate 			break;
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	return (rv);
1030Sstevel@tonic-gate }
104