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
5*10754SGarrett.Damore@Sun.COM * Common Development and Distribution License (the "License").
6*10754SGarrett.Damore@Sun.COM * 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 */
210Sstevel@tonic-gate /*
22*10754SGarrett.Damore@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate * under license from the Regents of the University of California.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*LINTLIBRARY*/
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/file.h>
38*10754SGarrett.Damore@Sun.COM #include <sys/fcntl.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate int
flock(int fd,int operation)420Sstevel@tonic-gate flock(int fd, int operation)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate struct flock fl;
450Sstevel@tonic-gate int cmd;
460Sstevel@tonic-gate int ret;
470Sstevel@tonic-gate
480Sstevel@tonic-gate /* initialize the flock struct to set lock on entire file */
490Sstevel@tonic-gate fl.l_whence = 0;
500Sstevel@tonic-gate fl.l_start = 0;
510Sstevel@tonic-gate fl.l_len = 0;
520Sstevel@tonic-gate fl.l_type = 0;
530Sstevel@tonic-gate
540Sstevel@tonic-gate /* In non-blocking lock, use F_SETLK for cmd, F_SETLKW otherwise */
550Sstevel@tonic-gate if (operation & LOCK_NB) {
560Sstevel@tonic-gate cmd = F_SETLK;
570Sstevel@tonic-gate operation &= ~LOCK_NB; /* turn off this bit */
580Sstevel@tonic-gate } else
590Sstevel@tonic-gate cmd = F_SETLKW;
600Sstevel@tonic-gate
610Sstevel@tonic-gate switch (operation) {
620Sstevel@tonic-gate case LOCK_UN:
630Sstevel@tonic-gate fl.l_type |= F_UNLCK;
640Sstevel@tonic-gate break;
650Sstevel@tonic-gate case LOCK_SH:
660Sstevel@tonic-gate fl.l_type |= F_RDLCK;
670Sstevel@tonic-gate break;
680Sstevel@tonic-gate case LOCK_EX:
690Sstevel@tonic-gate fl.l_type |= F_WRLCK;
700Sstevel@tonic-gate break;
710Sstevel@tonic-gate default:
720Sstevel@tonic-gate errno = EINVAL;
730Sstevel@tonic-gate return (-1);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate ret = fcntl(fd, cmd, &fl);
770Sstevel@tonic-gate
780Sstevel@tonic-gate if (ret == -1 && errno == EACCES)
790Sstevel@tonic-gate errno = EWOULDBLOCK;
800Sstevel@tonic-gate
810Sstevel@tonic-gate return (ret);
820Sstevel@tonic-gate }
83