xref: /titanic_52/usr/src/lib/libc/port/sys/lockf.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7257d1b4Sraf 
327c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
35a574db85Sraf #define	__lockf		__lockf64
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
38*7257d1b4Sraf #include "lint.h"
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <fcntl.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate int
45a574db85Sraf __lockf(int fildes, int function, off_t size)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	struct flock l;
487c478bd9Sstevel@tonic-gate 	int rv;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	l.l_whence = 1;
517c478bd9Sstevel@tonic-gate 	if (size < 0) {
527c478bd9Sstevel@tonic-gate 		l.l_start = size;
537c478bd9Sstevel@tonic-gate 		l.l_len = -size;
547c478bd9Sstevel@tonic-gate 	} else {
557c478bd9Sstevel@tonic-gate 		l.l_start = (off_t)0;
567c478bd9Sstevel@tonic-gate 		l.l_len = size;
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 	switch (function) {
597c478bd9Sstevel@tonic-gate 	case F_ULOCK:
607c478bd9Sstevel@tonic-gate 		l.l_type = F_UNLCK;
617c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLK, &l);
627c478bd9Sstevel@tonic-gate 		break;
637c478bd9Sstevel@tonic-gate 	case F_LOCK:
647c478bd9Sstevel@tonic-gate 		l.l_type = F_WRLCK;
657c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLKW, &l);
667c478bd9Sstevel@tonic-gate 		break;
677c478bd9Sstevel@tonic-gate 	case F_TLOCK:
687c478bd9Sstevel@tonic-gate 		l.l_type = F_WRLCK;
697c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_SETLK, &l);
707c478bd9Sstevel@tonic-gate 		break;
717c478bd9Sstevel@tonic-gate 	case F_TEST:
727c478bd9Sstevel@tonic-gate 		l.l_type = F_WRLCK;
737c478bd9Sstevel@tonic-gate 		rv = fcntl(fildes, F_GETLK, &l);
747c478bd9Sstevel@tonic-gate 		if (rv != -1) {
757c478bd9Sstevel@tonic-gate 			if (l.l_type == F_UNLCK)
767c478bd9Sstevel@tonic-gate 				return (0);
777c478bd9Sstevel@tonic-gate 			else {
787c478bd9Sstevel@tonic-gate 				errno = EAGAIN;
797c478bd9Sstevel@tonic-gate 				return (-1);
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 		break;
837c478bd9Sstevel@tonic-gate 	default:
847c478bd9Sstevel@tonic-gate 		errno = EINVAL;
857c478bd9Sstevel@tonic-gate 		return (-1);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	if (rv < 0) {
887c478bd9Sstevel@tonic-gate 		switch (errno) {
897c478bd9Sstevel@tonic-gate 		case EMFILE:
907c478bd9Sstevel@tonic-gate 		case ENOSPC:
917c478bd9Sstevel@tonic-gate 		case ENOLCK:
927c478bd9Sstevel@tonic-gate 			/*
937c478bd9Sstevel@tonic-gate 			 * A deadlock error is given if we run out of resources,
947c478bd9Sstevel@tonic-gate 			 * in compliance with /usr/group standards.
957c478bd9Sstevel@tonic-gate 			 */
967c478bd9Sstevel@tonic-gate 			errno = EDEADLK;
977c478bd9Sstevel@tonic-gate 			break;
987c478bd9Sstevel@tonic-gate 		default:
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	return (rv);
1037c478bd9Sstevel@tonic-gate }
104