1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1990-1996 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <unistd.h>
30*0Sstevel@tonic-gate #include <sys/errno.h>
31*0Sstevel@tonic-gate #include <sys/fcntl.h>
32*0Sstevel@tonic-gate #include <sys/filio.h>
33*0Sstevel@tonic-gate #include <sys/ioccom.h>
34*0Sstevel@tonic-gate #include <sys/syscall.h>
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate /* The following is an array of fcntl commands. The numbers listed
38*0Sstevel@tonic-gate * below are from SVR4. Array is indexed with SunOS 4.1 numbers to
39*0Sstevel@tonic-gate * obtain the SVR4 numbers.
40*0Sstevel@tonic-gate */
41*0Sstevel@tonic-gate int cmd_op[14] = {0, 1, 2, 3, 4, 23, 24, 14, 6, 7, 21, 20, -1, 22};
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate /* SVR4/SunOS 5.0 equivalent modes */
44*0Sstevel@tonic-gate #define N_O_NDELAY 0x04
45*0Sstevel@tonic-gate #define N_O_SYNC 0x10
46*0Sstevel@tonic-gate #define N_O_NONBLOCK 0x80
47*0Sstevel@tonic-gate #define N_O_CREAT 0x100
48*0Sstevel@tonic-gate #define N_O_TRUNC 0x200
49*0Sstevel@tonic-gate #define N_O_EXCL 0x400
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate #define S5_FASYNC 0x1000
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate /* from SVR4 stropts.h */
54*0Sstevel@tonic-gate #define S5_S_RDNORM 0x0040
55*0Sstevel@tonic-gate #define S5_S_WRNORM 0x0004
56*0Sstevel@tonic-gate #define S5_S_RDBAND 0x0080
57*0Sstevel@tonic-gate #define S5_S_BANDURG 0x0200
58*0Sstevel@tonic-gate #define S5_I_SETSIG (('S'<<8)|011)
59*0Sstevel@tonic-gate #define S5_I_GETSIG (('S'<<8)|012)
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate /* Mask corresponding to the bits above in SunOS 4.x */
62*0Sstevel@tonic-gate #define FLAGS_MASK (O_SYNC|O_NONBLOCK|O_CREAT|O_TRUNC|O_EXCL \
63*0Sstevel@tonic-gate |O_NDELAY|FNBIO|FASYNC)
64*0Sstevel@tonic-gate #define N_FLAGS_MASK (N_O_NDELAY|N_O_SYNC|N_O_NONBLOCK|N_O_CREAT \
65*0Sstevel@tonic-gate |N_O_TRUNC|N_O_EXCL|S5_FASYNC)
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate struct n_flock {
68*0Sstevel@tonic-gate short l_type;
69*0Sstevel@tonic-gate short l_whence;
70*0Sstevel@tonic-gate long l_start;
71*0Sstevel@tonic-gate long l_len; /* len == 0 means until end of file */
72*0Sstevel@tonic-gate long l_sysid;
73*0Sstevel@tonic-gate long l_pid;
74*0Sstevel@tonic-gate long pad[4]; /* reserve area */
75*0Sstevel@tonic-gate } ;
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate
fcntl(fd,cmd,arg)78*0Sstevel@tonic-gate int fcntl(fd, cmd, arg)
79*0Sstevel@tonic-gate int fd, cmd, arg;
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate return(bc_fcntl(fd, cmd, arg));
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate
bc_fcntl(fd,cmd,arg)85*0Sstevel@tonic-gate int bc_fcntl(fd, cmd, arg)
86*0Sstevel@tonic-gate int fd, cmd, arg;
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate int fds, ret;
89*0Sstevel@tonic-gate struct flock *savarg;
90*0Sstevel@tonic-gate struct n_flock nfl;
91*0Sstevel@tonic-gate extern int errno;
92*0Sstevel@tonic-gate int i, narg;
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate if ((cmd == F_SETOWN) || (cmd == F_GETOWN)) {
95*0Sstevel@tonic-gate ret = _s_fcntl(fd, cmd_op[cmd], arg);
96*0Sstevel@tonic-gate if ((ret != -1) || (errno != EINVAL))
97*0Sstevel@tonic-gate return (ret);
98*0Sstevel@tonic-gate else {
99*0Sstevel@tonic-gate if (cmd == F_GETOWN) {
100*0Sstevel@tonic-gate if (_ioctl(fd, S5_I_GETSIG, &i) < 0) {
101*0Sstevel@tonic-gate if (errno == EINVAL)
102*0Sstevel@tonic-gate i = 0;
103*0Sstevel@tonic-gate else
104*0Sstevel@tonic-gate return (-1);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate if (i & (S5_S_RDBAND|S5_S_BANDURG|
107*0Sstevel@tonic-gate S5_S_RDNORM|S5_S_WRNORM))
108*0Sstevel@tonic-gate return (getpid());
109*0Sstevel@tonic-gate return (0);
110*0Sstevel@tonic-gate } else { /* cmd == F_SETOWN */
111*0Sstevel@tonic-gate i = S5_S_RDNORM|S5_S_WRNORM|S5_S_RDBAND|S5_S_BANDURG;
112*0Sstevel@tonic-gate return (ioctl(fd, S5_I_SETSIG, i));
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate if (cmd == F_SETFL) {
117*0Sstevel@tonic-gate if (arg & FLAGS_MASK) {
118*0Sstevel@tonic-gate narg = arg & ~FLAGS_MASK;
119*0Sstevel@tonic-gate if (arg & FASYNC)
120*0Sstevel@tonic-gate narg |= S5_FASYNC;
121*0Sstevel@tonic-gate if (arg & O_SYNC)
122*0Sstevel@tonic-gate narg |= N_O_SYNC;
123*0Sstevel@tonic-gate if (arg & O_CREAT)
124*0Sstevel@tonic-gate narg |= N_O_CREAT;
125*0Sstevel@tonic-gate if (arg & O_TRUNC)
126*0Sstevel@tonic-gate narg |= N_O_TRUNC;
127*0Sstevel@tonic-gate if (arg & O_EXCL)
128*0Sstevel@tonic-gate narg |= N_O_EXCL;
129*0Sstevel@tonic-gate if (arg & (O_NDELAY))
130*0Sstevel@tonic-gate narg |= N_O_NDELAY;
131*0Sstevel@tonic-gate if (arg & O_NONBLOCK)
132*0Sstevel@tonic-gate narg |= N_O_NONBLOCK;
133*0Sstevel@tonic-gate if (arg & FNBIO)
134*0Sstevel@tonic-gate narg |= N_O_NDELAY;
135*0Sstevel@tonic-gate arg = narg;
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate } else if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK) {
138*0Sstevel@tonic-gate if (arg == 0 || arg == -1) {
139*0Sstevel@tonic-gate errno = EFAULT;
140*0Sstevel@tonic-gate return(-1);
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate savarg = (struct flock *)arg;
143*0Sstevel@tonic-gate arg = (int) &nfl;
144*0Sstevel@tonic-gate nfl.l_type = savarg->l_type;
145*0Sstevel@tonic-gate nfl.l_whence = savarg->l_whence;
146*0Sstevel@tonic-gate nfl.l_start = savarg->l_start;
147*0Sstevel@tonic-gate nfl.l_len = savarg->l_len;
148*0Sstevel@tonic-gate nfl.l_pid = savarg->l_pid;
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate ret = _s_fcntl(fd, cmd_op[cmd], arg);
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate if (ret != -1) {
154*0Sstevel@tonic-gate if (cmd == F_DUPFD) {
155*0Sstevel@tonic-gate if ((fds = fd_get(fd)) != -1)
156*0Sstevel@tonic-gate fd_add(ret, fds);
157*0Sstevel@tonic-gate } else if (cmd == F_GETFL) {
158*0Sstevel@tonic-gate if (ret & N_FLAGS_MASK) {
159*0Sstevel@tonic-gate narg = ret & ~N_FLAGS_MASK;
160*0Sstevel@tonic-gate if (ret & S5_FASYNC)
161*0Sstevel@tonic-gate narg |= FASYNC;
162*0Sstevel@tonic-gate if (ret & N_O_SYNC)
163*0Sstevel@tonic-gate narg |= O_SYNC;
164*0Sstevel@tonic-gate if (ret & N_O_NONBLOCK)
165*0Sstevel@tonic-gate narg |= O_NONBLOCK;
166*0Sstevel@tonic-gate if (ret & N_O_CREAT)
167*0Sstevel@tonic-gate narg |= O_CREAT;
168*0Sstevel@tonic-gate if (ret & N_O_TRUNC)
169*0Sstevel@tonic-gate narg |= O_TRUNC;
170*0Sstevel@tonic-gate if (ret & N_O_EXCL)
171*0Sstevel@tonic-gate narg |= O_EXCL;
172*0Sstevel@tonic-gate if (ret & (N_O_NDELAY))
173*0Sstevel@tonic-gate narg |= O_NDELAY;
174*0Sstevel@tonic-gate ret = narg;
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate } else if (cmd == F_SETLK || cmd == F_SETLKW ||
177*0Sstevel@tonic-gate cmd == F_GETLK) {
178*0Sstevel@tonic-gate savarg->l_type = nfl.l_type;
179*0Sstevel@tonic-gate savarg->l_whence = nfl.l_whence;
180*0Sstevel@tonic-gate savarg->l_start = nfl.l_start;
181*0Sstevel@tonic-gate savarg->l_len = nfl.l_len;
182*0Sstevel@tonic-gate savarg->l_pid = nfl.l_pid;
183*0Sstevel@tonic-gate arg = (int) savarg;
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate return(ret);
187*0Sstevel@tonic-gate }
188