xref: /illumos-gate/usr/src/lib/libc/port/sys/lockf.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a574db85Sraf  * Common Development and Distribution License (the "License").
6a574db85Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a574db85Sraf 
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
33a574db85Sraf #define	__lockf		__lockf64
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
36*7257d1b4Sraf #include "lint.h"
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate int
__lockf(int fildes,int function,off_t size)43a574db85Sraf __lockf(int fildes, int function, off_t size)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	struct flock l;
467c478bd9Sstevel@tonic-gate 	int rv;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	l.l_whence = 1;
497c478bd9Sstevel@tonic-gate 	if (size < 0) {
507c478bd9Sstevel@tonic-gate 		l.l_start = size;
517c478bd9Sstevel@tonic-gate 		l.l_len = -size;
527c478bd9Sstevel@tonic-gate 	} else {
537c478bd9Sstevel@tonic-gate 		l.l_start = (off_t)0;
547c478bd9Sstevel@tonic-gate 		l.l_len = size;
557c478bd9Sstevel@tonic-gate 	}
567c478bd9Sstevel@tonic-gate 	switch (function) {
577c478bd9Sstevel@tonic-gate 	case F_ULOCK:
587c478bd9Sstevel@tonic-gate 		l.l_type = F_UNLCK;
597c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLK, &l);
607c478bd9Sstevel@tonic-gate 		break;
617c478bd9Sstevel@tonic-gate 	case F_LOCK:
627c478bd9Sstevel@tonic-gate 		l.l_type = F_WRLCK;
637c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLKW, &l);
647c478bd9Sstevel@tonic-gate 		break;
657c478bd9Sstevel@tonic-gate 	case F_TLOCK:
667c478bd9Sstevel@tonic-gate 		l.l_type = F_WRLCK;
677c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLK, &l);
687c478bd9Sstevel@tonic-gate 		break;
697c478bd9Sstevel@tonic-gate 	case F_TEST:
707c478bd9Sstevel@tonic-gate 		l.l_type = F_WRLCK;
717c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_GETLK, &l);
727c478bd9Sstevel@tonic-gate 		if (rv != -1) {
737c478bd9Sstevel@tonic-gate 			if (l.l_type == F_UNLCK)
747c478bd9Sstevel@tonic-gate 				return (0);
757c478bd9Sstevel@tonic-gate 			else {
767c478bd9Sstevel@tonic-gate 				errno = EAGAIN;
777c478bd9Sstevel@tonic-gate 				return (-1);
787c478bd9Sstevel@tonic-gate 			}
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 		break;
817c478bd9Sstevel@tonic-gate 	default:
827c478bd9Sstevel@tonic-gate 		errno = EINVAL;
837c478bd9Sstevel@tonic-gate 		return (-1);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	if (rv < 0) {
867c478bd9Sstevel@tonic-gate 		switch (errno) {
877c478bd9Sstevel@tonic-gate 		case EMFILE:
887c478bd9Sstevel@tonic-gate 		case ENOSPC:
897c478bd9Sstevel@tonic-gate 		case ENOLCK:
907c478bd9Sstevel@tonic-gate 			/*
917c478bd9Sstevel@tonic-gate 			 * A deadlock error is given if we run out of resources,
927c478bd9Sstevel@tonic-gate 			 * in compliance with /usr/group standards.
937c478bd9Sstevel@tonic-gate 			 */
947c478bd9Sstevel@tonic-gate 			errno = EDEADLK;
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		default:
977c478bd9Sstevel@tonic-gate 			break;
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	return (rv);
1017c478bd9Sstevel@tonic-gate }
102