xref: /titanic_44/usr/src/lib/libc/port/sys/lockf.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #include <sys/feature_tests.h>
34 
35 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
36 #define	_lockf		_lockf64
37 #endif
38 
39 #include "synonyms.h"
40 #include <sys/types.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 
45 int
46 _lockf(int fildes, int function, off_t size)
47 {
48 	struct flock l;
49 	int rv;
50 
51 	l.l_whence = 1;
52 	if (size < 0) {
53 		l.l_start = size;
54 		l.l_len = -size;
55 	} else {
56 		l.l_start = (off_t)0;
57 		l.l_len = size;
58 	}
59 	switch (function) {
60 	case F_ULOCK:
61 		l.l_type = F_UNLCK;
62 		rv = fcntl(fildes, F_SETLK, &l);
63 		break;
64 	case F_LOCK:
65 		l.l_type = F_WRLCK;
66 		rv = fcntl(fildes, F_SETLKW, &l);
67 		break;
68 	case F_TLOCK:
69 		l.l_type = F_WRLCK;
70 		rv = fcntl(fildes, F_SETLK, &l);
71 		break;
72 	case F_TEST:
73 		l.l_type = F_WRLCK;
74 		rv = fcntl(fildes, F_GETLK, &l);
75 		if (rv != -1) {
76 			if (l.l_type == F_UNLCK)
77 				return (0);
78 			else {
79 				errno = EAGAIN;
80 				return (-1);
81 			}
82 		}
83 		break;
84 	default:
85 		errno = EINVAL;
86 		return (-1);
87 	}
88 	if (rv < 0) {
89 		switch (errno) {
90 		case EMFILE:
91 		case ENOSPC:
92 		case ENOLCK:
93 			/*
94 			 * A deadlock error is given if we run out of resources,
95 			 * in compliance with /usr/group standards.
96 			 */
97 			errno = EDEADLK;
98 			break;
99 		default:
100 			break;
101 		}
102 	}
103 	return (rv);
104 }
105