kern_descrip.c revision 1.21 1 1.16 cgd /*
2 1.17 cgd * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 1.17 cgd * The Regents of the University of California. All rights reserved.
4 1.16 cgd * (c) UNIX System Laboratories, Inc.
5 1.16 cgd * All or some portions of this file are derived from material licensed
6 1.16 cgd * to the University of California by American Telephone and Telegraph
7 1.16 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.16 cgd * the permission of UNIX System Laboratories, Inc.
9 1.16 cgd *
10 1.16 cgd * Redistribution and use in source and binary forms, with or without
11 1.16 cgd * modification, are permitted provided that the following conditions
12 1.16 cgd * are met:
13 1.16 cgd * 1. Redistributions of source code must retain the above copyright
14 1.16 cgd * notice, this list of conditions and the following disclaimer.
15 1.16 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.16 cgd * notice, this list of conditions and the following disclaimer in the
17 1.16 cgd * documentation and/or other materials provided with the distribution.
18 1.16 cgd * 3. All advertising materials mentioning features or use of this software
19 1.16 cgd * must display the following acknowledgement:
20 1.16 cgd * This product includes software developed by the University of
21 1.16 cgd * California, Berkeley and its contributors.
22 1.16 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.16 cgd * may be used to endorse or promote products derived from this software
24 1.16 cgd * without specific prior written permission.
25 1.16 cgd *
26 1.16 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.16 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.16 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.16 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.16 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.16 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.16 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.16 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.16 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.16 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.16 cgd * SUCH DAMAGE.
37 1.16 cgd *
38 1.17 cgd * from: @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
39 1.21 mycroft * $Id: kern_descrip.c,v 1.21 1994/06/22 03:00:21 mycroft Exp $
40 1.16 cgd */
41 1.16 cgd
42 1.16 cgd #include <sys/param.h>
43 1.16 cgd #include <sys/systm.h>
44 1.16 cgd #include <sys/filedesc.h>
45 1.16 cgd #include <sys/kernel.h>
46 1.16 cgd #include <sys/vnode.h>
47 1.16 cgd #include <sys/proc.h>
48 1.16 cgd #include <sys/file.h>
49 1.16 cgd #include <sys/socket.h>
50 1.16 cgd #include <sys/socketvar.h>
51 1.16 cgd #include <sys/stat.h>
52 1.16 cgd #include <sys/ioctl.h>
53 1.16 cgd #include <sys/fcntl.h>
54 1.16 cgd #include <sys/malloc.h>
55 1.16 cgd #include <sys/syslog.h>
56 1.17 cgd #include <sys/unistd.h>
57 1.16 cgd #include <sys/resourcevar.h>
58 1.16 cgd
59 1.16 cgd /*
60 1.16 cgd * Descriptor management.
61 1.16 cgd */
62 1.16 cgd struct file *filehead; /* head of list of open files */
63 1.16 cgd int nfiles; /* actual number of open files */
64 1.16 cgd
65 1.16 cgd /*
66 1.16 cgd * System calls on descriptors.
67 1.16 cgd */
68 1.18 cgd
69 1.21 mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_ULTRIX) || defined(COMPAT_HPUX)
70 1.16 cgd /* ARGSUSED */
71 1.18 cgd ogetdtablesize(p, uap, retval)
72 1.16 cgd struct proc *p;
73 1.17 cgd void *uap;
74 1.16 cgd int *retval;
75 1.16 cgd {
76 1.16 cgd
77 1.17 cgd *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
78 1.16 cgd return (0);
79 1.16 cgd }
80 1.18 cgd #endif
81 1.16 cgd
82 1.16 cgd /*
83 1.16 cgd * Duplicate a file descriptor.
84 1.16 cgd */
85 1.16 cgd struct dup_args {
86 1.17 cgd u_int fd;
87 1.16 cgd };
88 1.16 cgd /* ARGSUSED */
89 1.16 cgd dup(p, uap, retval)
90 1.16 cgd struct proc *p;
91 1.16 cgd struct dup_args *uap;
92 1.16 cgd int *retval;
93 1.16 cgd {
94 1.17 cgd register struct filedesc *fdp;
95 1.17 cgd u_int old;
96 1.17 cgd int new, error;
97 1.16 cgd
98 1.17 cgd old = uap->fd;
99 1.16 cgd /*
100 1.16 cgd * XXX Compatibility
101 1.16 cgd */
102 1.17 cgd if (old &~ 077) { uap->fd &= 077; return (dup2(p, uap, retval)); }
103 1.16 cgd
104 1.17 cgd fdp = p->p_fd;
105 1.17 cgd if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
106 1.16 cgd return (EBADF);
107 1.17 cgd if (error = fdalloc(p, 0, &new))
108 1.16 cgd return (error);
109 1.17 cgd return (finishdup(fdp, (int)old, new, retval));
110 1.16 cgd }
111 1.16 cgd
112 1.16 cgd /*
113 1.16 cgd * Duplicate a file descriptor to a particular value.
114 1.16 cgd */
115 1.16 cgd struct dup2_args {
116 1.16 cgd u_int from;
117 1.16 cgd u_int to;
118 1.16 cgd };
119 1.16 cgd /* ARGSUSED */
120 1.16 cgd dup2(p, uap, retval)
121 1.16 cgd struct proc *p;
122 1.16 cgd struct dup2_args *uap;
123 1.16 cgd int *retval;
124 1.16 cgd {
125 1.16 cgd register struct filedesc *fdp = p->p_fd;
126 1.16 cgd register u_int old = uap->from, new = uap->to;
127 1.16 cgd int i, error;
128 1.16 cgd
129 1.16 cgd if (old >= fdp->fd_nfiles ||
130 1.17 cgd fdp->fd_ofiles[old] == NULL ||
131 1.16 cgd new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
132 1.17 cgd new >= maxfiles)
133 1.16 cgd return (EBADF);
134 1.17 cgd if (old == new) {
135 1.17 cgd *retval = new;
136 1.16 cgd return (0);
137 1.17 cgd }
138 1.16 cgd if (new >= fdp->fd_nfiles) {
139 1.16 cgd if (error = fdalloc(p, new, &i))
140 1.16 cgd return (error);
141 1.16 cgd if (new != i)
142 1.16 cgd panic("dup2: fdalloc");
143 1.16 cgd } else if (fdp->fd_ofiles[new]) {
144 1.16 cgd if (fdp->fd_ofileflags[new] & UF_MAPPED)
145 1.16 cgd (void) munmapfd(p, new);
146 1.16 cgd /*
147 1.16 cgd * dup2() must succeed even if the close has an error.
148 1.16 cgd */
149 1.16 cgd (void) closef(fdp->fd_ofiles[new], p);
150 1.16 cgd }
151 1.17 cgd return (finishdup(fdp, (int)old, (int)new, retval));
152 1.16 cgd }
153 1.16 cgd
154 1.16 cgd /*
155 1.16 cgd * The file control system call.
156 1.16 cgd */
157 1.16 cgd struct fcntl_args {
158 1.16 cgd int fd;
159 1.16 cgd int cmd;
160 1.16 cgd int arg;
161 1.16 cgd };
162 1.16 cgd /* ARGSUSED */
163 1.16 cgd fcntl(p, uap, retval)
164 1.16 cgd struct proc *p;
165 1.16 cgd register struct fcntl_args *uap;
166 1.16 cgd int *retval;
167 1.16 cgd {
168 1.16 cgd register struct filedesc *fdp = p->p_fd;
169 1.16 cgd register struct file *fp;
170 1.16 cgd register char *pop;
171 1.16 cgd struct vnode *vp;
172 1.16 cgd int i, tmp, error, flg = F_POSIX;
173 1.16 cgd struct flock fl;
174 1.17 cgd u_int newmin;
175 1.16 cgd
176 1.16 cgd if ((unsigned)uap->fd >= fdp->fd_nfiles ||
177 1.16 cgd (fp = fdp->fd_ofiles[uap->fd]) == NULL)
178 1.16 cgd return (EBADF);
179 1.16 cgd pop = &fdp->fd_ofileflags[uap->fd];
180 1.17 cgd switch (uap->cmd) {
181 1.17 cgd
182 1.16 cgd case F_DUPFD:
183 1.17 cgd newmin = uap->arg;
184 1.17 cgd if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
185 1.17 cgd newmin >= maxfiles)
186 1.16 cgd return (EINVAL);
187 1.17 cgd if (error = fdalloc(p, newmin, &i))
188 1.16 cgd return (error);
189 1.17 cgd return (finishdup(fdp, uap->fd, i, retval));
190 1.16 cgd
191 1.16 cgd case F_GETFD:
192 1.16 cgd *retval = *pop & 1;
193 1.16 cgd return (0);
194 1.16 cgd
195 1.16 cgd case F_SETFD:
196 1.16 cgd *pop = (*pop &~ 1) | (uap->arg & 1);
197 1.16 cgd return (0);
198 1.16 cgd
199 1.16 cgd case F_GETFL:
200 1.16 cgd *retval = OFLAGS(fp->f_flag);
201 1.16 cgd return (0);
202 1.16 cgd
203 1.16 cgd case F_SETFL:
204 1.16 cgd fp->f_flag &= ~FCNTLFLAGS;
205 1.16 cgd fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
206 1.16 cgd tmp = fp->f_flag & FNONBLOCK;
207 1.16 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
208 1.16 cgd if (error)
209 1.16 cgd return (error);
210 1.16 cgd tmp = fp->f_flag & FASYNC;
211 1.16 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
212 1.16 cgd if (!error)
213 1.16 cgd return (0);
214 1.16 cgd fp->f_flag &= ~FNONBLOCK;
215 1.16 cgd tmp = 0;
216 1.16 cgd (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
217 1.16 cgd return (error);
218 1.16 cgd
219 1.16 cgd case F_GETOWN:
220 1.16 cgd if (fp->f_type == DTYPE_SOCKET) {
221 1.16 cgd *retval = ((struct socket *)fp->f_data)->so_pgid;
222 1.16 cgd return (0);
223 1.16 cgd }
224 1.16 cgd error = (*fp->f_ops->fo_ioctl)
225 1.16 cgd (fp, (int)TIOCGPGRP, (caddr_t)retval, p);
226 1.16 cgd *retval = -*retval;
227 1.16 cgd return (error);
228 1.16 cgd
229 1.16 cgd case F_SETOWN:
230 1.16 cgd if (fp->f_type == DTYPE_SOCKET) {
231 1.16 cgd ((struct socket *)fp->f_data)->so_pgid = uap->arg;
232 1.16 cgd return (0);
233 1.16 cgd }
234 1.16 cgd if (uap->arg <= 0) {
235 1.16 cgd uap->arg = -uap->arg;
236 1.16 cgd } else {
237 1.16 cgd struct proc *p1 = pfind(uap->arg);
238 1.16 cgd if (p1 == 0)
239 1.16 cgd return (ESRCH);
240 1.16 cgd uap->arg = p1->p_pgrp->pg_id;
241 1.16 cgd }
242 1.16 cgd return ((*fp->f_ops->fo_ioctl)
243 1.16 cgd (fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
244 1.16 cgd
245 1.16 cgd case F_SETLKW:
246 1.16 cgd flg |= F_WAIT;
247 1.16 cgd /* Fall into F_SETLK */
248 1.16 cgd
249 1.16 cgd case F_SETLK:
250 1.16 cgd if (fp->f_type != DTYPE_VNODE)
251 1.16 cgd return (EBADF);
252 1.16 cgd vp = (struct vnode *)fp->f_data;
253 1.16 cgd /* Copy in the lock structure */
254 1.16 cgd error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
255 1.16 cgd if (error)
256 1.16 cgd return (error);
257 1.16 cgd if (fl.l_whence == SEEK_CUR)
258 1.16 cgd fl.l_start += fp->f_offset;
259 1.16 cgd switch (fl.l_type) {
260 1.16 cgd
261 1.16 cgd case F_RDLCK:
262 1.16 cgd if ((fp->f_flag & FREAD) == 0)
263 1.16 cgd return (EBADF);
264 1.16 cgd p->p_flag |= P_ADVLOCK;
265 1.16 cgd return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
266 1.16 cgd
267 1.16 cgd case F_WRLCK:
268 1.16 cgd if ((fp->f_flag & FWRITE) == 0)
269 1.16 cgd return (EBADF);
270 1.16 cgd p->p_flag |= P_ADVLOCK;
271 1.16 cgd return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
272 1.16 cgd
273 1.16 cgd case F_UNLCK:
274 1.16 cgd return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
275 1.16 cgd F_POSIX));
276 1.16 cgd
277 1.16 cgd default:
278 1.16 cgd return (EINVAL);
279 1.16 cgd }
280 1.16 cgd
281 1.16 cgd case F_GETLK:
282 1.16 cgd if (fp->f_type != DTYPE_VNODE)
283 1.16 cgd return (EBADF);
284 1.16 cgd vp = (struct vnode *)fp->f_data;
285 1.16 cgd /* Copy in the lock structure */
286 1.16 cgd error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
287 1.16 cgd if (error)
288 1.16 cgd return (error);
289 1.16 cgd if (fl.l_whence == SEEK_CUR)
290 1.16 cgd fl.l_start += fp->f_offset;
291 1.16 cgd if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
292 1.16 cgd return (error);
293 1.16 cgd return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl)));
294 1.16 cgd
295 1.16 cgd default:
296 1.16 cgd return (EINVAL);
297 1.16 cgd }
298 1.16 cgd /* NOTREACHED */
299 1.16 cgd }
300 1.16 cgd
301 1.16 cgd /*
302 1.17 cgd * Common code for dup, dup2, and fcntl(F_DUPFD).
303 1.17 cgd */
304 1.17 cgd int
305 1.17 cgd finishdup(fdp, old, new, retval)
306 1.17 cgd register struct filedesc *fdp;
307 1.17 cgd register int old, new, *retval;
308 1.17 cgd {
309 1.17 cgd register struct file *fp;
310 1.17 cgd
311 1.17 cgd fp = fdp->fd_ofiles[old];
312 1.17 cgd fdp->fd_ofiles[new] = fp;
313 1.17 cgd fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
314 1.17 cgd fp->f_count++;
315 1.17 cgd if (new > fdp->fd_lastfile)
316 1.17 cgd fdp->fd_lastfile = new;
317 1.17 cgd *retval = new;
318 1.17 cgd return (0);
319 1.17 cgd }
320 1.17 cgd
321 1.17 cgd /*
322 1.16 cgd * Close a file descriptor.
323 1.16 cgd */
324 1.16 cgd struct close_args {
325 1.16 cgd int fd;
326 1.16 cgd };
327 1.16 cgd /* ARGSUSED */
328 1.16 cgd close(p, uap, retval)
329 1.16 cgd struct proc *p;
330 1.16 cgd struct close_args *uap;
331 1.16 cgd int *retval;
332 1.16 cgd {
333 1.16 cgd register struct filedesc *fdp = p->p_fd;
334 1.16 cgd register struct file *fp;
335 1.16 cgd register int fd = uap->fd;
336 1.16 cgd register u_char *pf;
337 1.16 cgd
338 1.16 cgd if ((unsigned)fd >= fdp->fd_nfiles ||
339 1.16 cgd (fp = fdp->fd_ofiles[fd]) == NULL)
340 1.16 cgd return (EBADF);
341 1.16 cgd pf = (u_char *)&fdp->fd_ofileflags[fd];
342 1.16 cgd if (*pf & UF_MAPPED)
343 1.16 cgd (void) munmapfd(p, fd);
344 1.16 cgd fdp->fd_ofiles[fd] = NULL;
345 1.16 cgd while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
346 1.16 cgd fdp->fd_lastfile--;
347 1.16 cgd if (fd < fdp->fd_freefile)
348 1.16 cgd fdp->fd_freefile = fd;
349 1.16 cgd *pf = 0;
350 1.16 cgd return (closef(fp, p));
351 1.16 cgd }
352 1.16 cgd
353 1.17 cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
354 1.16 cgd /*
355 1.16 cgd * Return status information about a file descriptor.
356 1.16 cgd */
357 1.16 cgd struct ofstat_args {
358 1.16 cgd int fd;
359 1.17 cgd struct ostat *sb;
360 1.16 cgd };
361 1.16 cgd /* ARGSUSED */
362 1.16 cgd ofstat(p, uap, retval)
363 1.16 cgd struct proc *p;
364 1.16 cgd register struct ofstat_args *uap;
365 1.16 cgd int *retval;
366 1.16 cgd {
367 1.16 cgd register struct filedesc *fdp = p->p_fd;
368 1.16 cgd register struct file *fp;
369 1.17 cgd struct stat ub;
370 1.17 cgd struct ostat oub;
371 1.16 cgd int error;
372 1.16 cgd
373 1.16 cgd if ((unsigned)uap->fd >= fdp->fd_nfiles ||
374 1.16 cgd (fp = fdp->fd_ofiles[uap->fd]) == NULL)
375 1.16 cgd return (EBADF);
376 1.16 cgd switch (fp->f_type) {
377 1.16 cgd
378 1.16 cgd case DTYPE_VNODE:
379 1.17 cgd error = vn_stat((struct vnode *)fp->f_data, &ub, p);
380 1.16 cgd break;
381 1.16 cgd
382 1.16 cgd case DTYPE_SOCKET:
383 1.17 cgd error = soo_stat((struct socket *)fp->f_data, &ub);
384 1.16 cgd break;
385 1.16 cgd
386 1.16 cgd default:
387 1.16 cgd panic("ofstat");
388 1.16 cgd /*NOTREACHED*/
389 1.16 cgd }
390 1.17 cgd cvtstat(&ub, &oub);
391 1.17 cgd if (error == 0)
392 1.17 cgd error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
393 1.16 cgd return (error);
394 1.16 cgd }
395 1.17 cgd #endif /* COMPAT_43 || COMPAT_SUNOS */
396 1.16 cgd
397 1.17 cgd /*
398 1.17 cgd * Return status information about a file descriptor.
399 1.17 cgd */
400 1.16 cgd struct fstat_args {
401 1.16 cgd int fd;
402 1.17 cgd struct stat *sb;
403 1.16 cgd };
404 1.16 cgd /* ARGSUSED */
405 1.16 cgd fstat(p, uap, retval)
406 1.16 cgd struct proc *p;
407 1.16 cgd register struct fstat_args *uap;
408 1.16 cgd int *retval;
409 1.16 cgd {
410 1.16 cgd register struct filedesc *fdp = p->p_fd;
411 1.16 cgd register struct file *fp;
412 1.16 cgd struct stat ub;
413 1.16 cgd int error;
414 1.16 cgd
415 1.16 cgd if ((unsigned)uap->fd >= fdp->fd_nfiles ||
416 1.16 cgd (fp = fdp->fd_ofiles[uap->fd]) == NULL)
417 1.16 cgd return (EBADF);
418 1.16 cgd switch (fp->f_type) {
419 1.16 cgd
420 1.16 cgd case DTYPE_VNODE:
421 1.16 cgd error = vn_stat((struct vnode *)fp->f_data, &ub, p);
422 1.16 cgd break;
423 1.16 cgd
424 1.16 cgd case DTYPE_SOCKET:
425 1.16 cgd error = soo_stat((struct socket *)fp->f_data, &ub);
426 1.16 cgd break;
427 1.16 cgd
428 1.16 cgd default:
429 1.16 cgd panic("fstat");
430 1.16 cgd /*NOTREACHED*/
431 1.16 cgd }
432 1.16 cgd if (error == 0)
433 1.16 cgd error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
434 1.16 cgd return (error);
435 1.16 cgd }
436 1.16 cgd
437 1.16 cgd /*
438 1.16 cgd * Return pathconf information about a file descriptor.
439 1.16 cgd */
440 1.16 cgd struct fpathconf_args {
441 1.16 cgd int fd;
442 1.16 cgd int name;
443 1.16 cgd };
444 1.16 cgd /* ARGSUSED */
445 1.16 cgd fpathconf(p, uap, retval)
446 1.16 cgd struct proc *p;
447 1.16 cgd register struct fpathconf_args *uap;
448 1.16 cgd int *retval;
449 1.16 cgd {
450 1.17 cgd struct filedesc *fdp = p->p_fd;
451 1.17 cgd struct file *fp;
452 1.17 cgd struct vnode *vp;
453 1.17 cgd
454 1.17 cgd if ((unsigned)uap->fd >= fdp->fd_nfiles ||
455 1.17 cgd (fp = fdp->fd_ofiles[uap->fd]) == NULL)
456 1.17 cgd return (EBADF);
457 1.17 cgd switch (fp->f_type) {
458 1.16 cgd
459 1.17 cgd case DTYPE_SOCKET:
460 1.17 cgd if (uap->name != _PC_PIPE_BUF)
461 1.17 cgd return (EINVAL);
462 1.17 cgd *retval = PIPE_BUF;
463 1.17 cgd return (0);
464 1.17 cgd
465 1.17 cgd case DTYPE_VNODE:
466 1.17 cgd vp = (struct vnode *)fp->f_data;
467 1.17 cgd #ifdef notyet
468 1.17 cgd return (VOP_PATHCONF(vp, uap->name, retval));
469 1.17 cgd #else
470 1.17 cgd return (ENOSYS);
471 1.17 cgd #endif
472 1.17 cgd
473 1.17 cgd default:
474 1.17 cgd panic("fpathconf");
475 1.17 cgd }
476 1.17 cgd /*NOTREACHED*/
477 1.16 cgd }
478 1.16 cgd
479 1.16 cgd /*
480 1.16 cgd * Allocate a file descriptor for the process.
481 1.16 cgd */
482 1.16 cgd int fdexpand;
483 1.16 cgd
484 1.16 cgd fdalloc(p, want, result)
485 1.16 cgd struct proc *p;
486 1.16 cgd int want;
487 1.16 cgd int *result;
488 1.16 cgd {
489 1.16 cgd register struct filedesc *fdp = p->p_fd;
490 1.16 cgd register int i;
491 1.16 cgd int lim, last, nfiles;
492 1.16 cgd struct file **newofile;
493 1.16 cgd char *newofileflags;
494 1.16 cgd
495 1.16 cgd /*
496 1.16 cgd * Search for a free descriptor starting at the higher
497 1.16 cgd * of want or fd_freefile. If that fails, consider
498 1.16 cgd * expanding the ofile array.
499 1.16 cgd */
500 1.17 cgd lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
501 1.16 cgd for (;;) {
502 1.16 cgd last = min(fdp->fd_nfiles, lim);
503 1.16 cgd if ((i = want) < fdp->fd_freefile)
504 1.16 cgd i = fdp->fd_freefile;
505 1.16 cgd for (; i < last; i++) {
506 1.16 cgd if (fdp->fd_ofiles[i] == NULL) {
507 1.16 cgd fdp->fd_ofileflags[i] = 0;
508 1.16 cgd if (i > fdp->fd_lastfile)
509 1.16 cgd fdp->fd_lastfile = i;
510 1.16 cgd if (want <= fdp->fd_freefile)
511 1.16 cgd fdp->fd_freefile = i;
512 1.16 cgd *result = i;
513 1.16 cgd return (0);
514 1.16 cgd }
515 1.16 cgd }
516 1.16 cgd
517 1.16 cgd /*
518 1.16 cgd * No space in current array. Expand?
519 1.16 cgd */
520 1.16 cgd if (fdp->fd_nfiles >= lim)
521 1.16 cgd return (EMFILE);
522 1.16 cgd if (fdp->fd_nfiles < NDEXTENT)
523 1.16 cgd nfiles = NDEXTENT;
524 1.16 cgd else
525 1.16 cgd nfiles = 2 * fdp->fd_nfiles;
526 1.16 cgd MALLOC(newofile, struct file **, nfiles * OFILESIZE,
527 1.16 cgd M_FILEDESC, M_WAITOK);
528 1.16 cgd newofileflags = (char *) &newofile[nfiles];
529 1.16 cgd /*
530 1.16 cgd * Copy the existing ofile and ofileflags arrays
531 1.16 cgd * and zero the new portion of each array.
532 1.16 cgd */
533 1.16 cgd bcopy(fdp->fd_ofiles, newofile,
534 1.16 cgd (i = sizeof(struct file *) * fdp->fd_nfiles));
535 1.16 cgd bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
536 1.16 cgd bcopy(fdp->fd_ofileflags, newofileflags,
537 1.16 cgd (i = sizeof(char) * fdp->fd_nfiles));
538 1.16 cgd bzero(newofileflags + i, nfiles * sizeof(char) - i);
539 1.16 cgd if (fdp->fd_nfiles > NDFILE)
540 1.16 cgd FREE(fdp->fd_ofiles, M_FILEDESC);
541 1.16 cgd fdp->fd_ofiles = newofile;
542 1.16 cgd fdp->fd_ofileflags = newofileflags;
543 1.16 cgd fdp->fd_nfiles = nfiles;
544 1.16 cgd fdexpand++;
545 1.16 cgd }
546 1.16 cgd }
547 1.16 cgd
548 1.16 cgd /*
549 1.16 cgd * Check to see whether n user file descriptors
550 1.16 cgd * are available to the process p.
551 1.16 cgd */
552 1.16 cgd fdavail(p, n)
553 1.16 cgd struct proc *p;
554 1.16 cgd register int n;
555 1.16 cgd {
556 1.16 cgd register struct filedesc *fdp = p->p_fd;
557 1.16 cgd register struct file **fpp;
558 1.17 cgd register int i, lim;
559 1.16 cgd
560 1.17 cgd lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
561 1.17 cgd if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
562 1.16 cgd return (1);
563 1.16 cgd fpp = &fdp->fd_ofiles[fdp->fd_freefile];
564 1.16 cgd for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
565 1.16 cgd if (*fpp == NULL && --n <= 0)
566 1.16 cgd return (1);
567 1.16 cgd return (0);
568 1.16 cgd }
569 1.16 cgd
570 1.16 cgd /*
571 1.16 cgd * Create a new open file structure and allocate
572 1.16 cgd * a file decriptor for the process that refers to it.
573 1.16 cgd */
574 1.16 cgd falloc(p, resultfp, resultfd)
575 1.16 cgd register struct proc *p;
576 1.16 cgd struct file **resultfp;
577 1.16 cgd int *resultfd;
578 1.16 cgd {
579 1.16 cgd register struct file *fp, *fq, **fpp;
580 1.16 cgd int error, i;
581 1.16 cgd
582 1.16 cgd if (error = fdalloc(p, 0, &i))
583 1.16 cgd return (error);
584 1.16 cgd if (nfiles >= maxfiles) {
585 1.16 cgd tablefull("file");
586 1.16 cgd return (ENFILE);
587 1.16 cgd }
588 1.16 cgd /*
589 1.16 cgd * Allocate a new file descriptor.
590 1.16 cgd * If the process has file descriptor zero open, add to the list
591 1.16 cgd * of open files at that point, otherwise put it at the front of
592 1.16 cgd * the list of open files.
593 1.16 cgd */
594 1.16 cgd nfiles++;
595 1.16 cgd MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
596 1.17 cgd bzero(fp, sizeof(struct file));
597 1.16 cgd if (fq = p->p_fd->fd_ofiles[0])
598 1.16 cgd fpp = &fq->f_filef;
599 1.16 cgd else
600 1.16 cgd fpp = &filehead;
601 1.16 cgd p->p_fd->fd_ofiles[i] = fp;
602 1.16 cgd if (fq = *fpp)
603 1.16 cgd fq->f_fileb = &fp->f_filef;
604 1.16 cgd fp->f_filef = fq;
605 1.16 cgd fp->f_fileb = fpp;
606 1.16 cgd *fpp = fp;
607 1.16 cgd fp->f_count = 1;
608 1.16 cgd fp->f_cred = p->p_ucred;
609 1.16 cgd crhold(fp->f_cred);
610 1.16 cgd if (resultfp)
611 1.16 cgd *resultfp = fp;
612 1.16 cgd if (resultfd)
613 1.16 cgd *resultfd = i;
614 1.16 cgd return (0);
615 1.16 cgd }
616 1.16 cgd
617 1.16 cgd /*
618 1.16 cgd * Free a file descriptor.
619 1.16 cgd */
620 1.16 cgd ffree(fp)
621 1.16 cgd register struct file *fp;
622 1.16 cgd {
623 1.16 cgd register struct file *fq;
624 1.16 cgd
625 1.16 cgd if (fq = fp->f_filef)
626 1.16 cgd fq->f_fileb = fp->f_fileb;
627 1.16 cgd *fp->f_fileb = fq;
628 1.16 cgd crfree(fp->f_cred);
629 1.16 cgd #ifdef DIAGNOSTIC
630 1.16 cgd fp->f_filef = NULL;
631 1.16 cgd fp->f_fileb = NULL;
632 1.16 cgd fp->f_count = 0;
633 1.16 cgd #endif
634 1.16 cgd nfiles--;
635 1.16 cgd FREE(fp, M_FILE);
636 1.16 cgd }
637 1.16 cgd
638 1.16 cgd /*
639 1.16 cgd * Copy a filedesc structure.
640 1.16 cgd */
641 1.16 cgd struct filedesc *
642 1.16 cgd fdcopy(p)
643 1.16 cgd struct proc *p;
644 1.16 cgd {
645 1.16 cgd register struct filedesc *newfdp, *fdp = p->p_fd;
646 1.16 cgd register struct file **fpp;
647 1.16 cgd register int i;
648 1.16 cgd
649 1.16 cgd MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
650 1.16 cgd M_FILEDESC, M_WAITOK);
651 1.16 cgd bcopy(fdp, newfdp, sizeof(struct filedesc));
652 1.16 cgd VREF(newfdp->fd_cdir);
653 1.16 cgd if (newfdp->fd_rdir)
654 1.16 cgd VREF(newfdp->fd_rdir);
655 1.16 cgd newfdp->fd_refcnt = 1;
656 1.16 cgd
657 1.16 cgd /*
658 1.16 cgd * If the number of open files fits in the internal arrays
659 1.16 cgd * of the open file structure, use them, otherwise allocate
660 1.16 cgd * additional memory for the number of descriptors currently
661 1.16 cgd * in use.
662 1.16 cgd */
663 1.16 cgd if (newfdp->fd_lastfile < NDFILE) {
664 1.16 cgd newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
665 1.16 cgd newfdp->fd_ofileflags =
666 1.16 cgd ((struct filedesc0 *) newfdp)->fd_dfileflags;
667 1.16 cgd i = NDFILE;
668 1.16 cgd } else {
669 1.16 cgd /*
670 1.16 cgd * Compute the smallest multiple of NDEXTENT needed
671 1.16 cgd * for the file descriptors currently in use,
672 1.16 cgd * allowing the table to shrink.
673 1.16 cgd */
674 1.16 cgd i = newfdp->fd_nfiles;
675 1.17 cgd while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
676 1.16 cgd i /= 2;
677 1.16 cgd MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
678 1.16 cgd M_FILEDESC, M_WAITOK);
679 1.16 cgd newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
680 1.16 cgd }
681 1.16 cgd newfdp->fd_nfiles = i;
682 1.16 cgd bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
683 1.16 cgd bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
684 1.16 cgd fpp = newfdp->fd_ofiles;
685 1.16 cgd for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
686 1.16 cgd if (*fpp != NULL)
687 1.16 cgd (*fpp)->f_count++;
688 1.16 cgd return (newfdp);
689 1.16 cgd }
690 1.16 cgd
691 1.16 cgd /*
692 1.16 cgd * Release a filedesc structure.
693 1.16 cgd */
694 1.16 cgd void
695 1.16 cgd fdfree(p)
696 1.16 cgd struct proc *p;
697 1.16 cgd {
698 1.16 cgd register struct filedesc *fdp = p->p_fd;
699 1.16 cgd struct file **fpp;
700 1.16 cgd register int i;
701 1.16 cgd
702 1.16 cgd if (--fdp->fd_refcnt > 0)
703 1.16 cgd return;
704 1.16 cgd fpp = fdp->fd_ofiles;
705 1.16 cgd for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
706 1.16 cgd if (*fpp)
707 1.16 cgd (void) closef(*fpp, p);
708 1.16 cgd if (fdp->fd_nfiles > NDFILE)
709 1.16 cgd FREE(fdp->fd_ofiles, M_FILEDESC);
710 1.16 cgd vrele(fdp->fd_cdir);
711 1.16 cgd if (fdp->fd_rdir)
712 1.16 cgd vrele(fdp->fd_rdir);
713 1.16 cgd FREE(fdp, M_FILEDESC);
714 1.16 cgd }
715 1.16 cgd
716 1.16 cgd /*
717 1.16 cgd * Close any files on exec?
718 1.16 cgd */
719 1.16 cgd void
720 1.16 cgd fdcloseexec(p)
721 1.16 cgd struct proc *p;
722 1.16 cgd {
723 1.16 cgd struct filedesc *fdp = p->p_fd;
724 1.16 cgd struct file **fpp;
725 1.16 cgd char *fdfp;
726 1.16 cgd register int i;
727 1.16 cgd
728 1.16 cgd fpp = fdp->fd_ofiles;
729 1.16 cgd fdfp = fdp->fd_ofileflags;
730 1.16 cgd for (i = 0; i <= fdp->fd_lastfile; i++, fpp++, fdfp++)
731 1.16 cgd if (*fpp != NULL && (*fdfp & UF_EXCLOSE)) {
732 1.16 cgd if (*fdfp & UF_MAPPED)
733 1.16 cgd (void) munmapfd(p, i);
734 1.16 cgd (void) closef(*fpp, p);
735 1.16 cgd *fpp = NULL;
736 1.16 cgd *fdfp = 0;
737 1.16 cgd if (i < fdp->fd_freefile)
738 1.16 cgd fdp->fd_freefile = i;
739 1.16 cgd }
740 1.16 cgd while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
741 1.16 cgd fdp->fd_lastfile--;
742 1.16 cgd }
743 1.16 cgd
744 1.16 cgd /*
745 1.16 cgd * Internal form of close.
746 1.16 cgd * Decrement reference count on file structure.
747 1.17 cgd * Note: p may be NULL when closing a file
748 1.17 cgd * that was being passed in a message.
749 1.16 cgd */
750 1.16 cgd closef(fp, p)
751 1.16 cgd register struct file *fp;
752 1.16 cgd register struct proc *p;
753 1.16 cgd {
754 1.16 cgd struct vnode *vp;
755 1.16 cgd struct flock lf;
756 1.16 cgd int error;
757 1.16 cgd
758 1.16 cgd if (fp == NULL)
759 1.16 cgd return (0);
760 1.16 cgd /*
761 1.16 cgd * POSIX record locking dictates that any close releases ALL
762 1.16 cgd * locks owned by this process. This is handled by setting
763 1.16 cgd * a flag in the unlock to free ONLY locks obeying POSIX
764 1.16 cgd * semantics, and not to free BSD-style file locks.
765 1.17 cgd * If the descriptor was in a message, POSIX-style locks
766 1.17 cgd * aren't passed with the descriptor.
767 1.16 cgd */
768 1.16 cgd if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
769 1.16 cgd lf.l_whence = SEEK_SET;
770 1.16 cgd lf.l_start = 0;
771 1.16 cgd lf.l_len = 0;
772 1.16 cgd lf.l_type = F_UNLCK;
773 1.16 cgd vp = (struct vnode *)fp->f_data;
774 1.16 cgd (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
775 1.16 cgd }
776 1.16 cgd if (--fp->f_count > 0)
777 1.16 cgd return (0);
778 1.16 cgd if (fp->f_count < 0)
779 1.16 cgd panic("closef: count < 0");
780 1.16 cgd if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
781 1.16 cgd lf.l_whence = SEEK_SET;
782 1.16 cgd lf.l_start = 0;
783 1.16 cgd lf.l_len = 0;
784 1.16 cgd lf.l_type = F_UNLCK;
785 1.16 cgd vp = (struct vnode *)fp->f_data;
786 1.16 cgd (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
787 1.16 cgd }
788 1.17 cgd if (fp->f_ops)
789 1.17 cgd error = (*fp->f_ops->fo_close)(fp, p);
790 1.17 cgd else
791 1.17 cgd error = 0;
792 1.16 cgd ffree(fp);
793 1.16 cgd return (error);
794 1.16 cgd }
795 1.16 cgd
796 1.16 cgd /*
797 1.16 cgd * Apply an advisory lock on a file descriptor.
798 1.16 cgd *
799 1.16 cgd * Just attempt to get a record lock of the requested type on
800 1.16 cgd * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
801 1.16 cgd */
802 1.16 cgd struct flock_args {
803 1.16 cgd int fd;
804 1.16 cgd int how;
805 1.16 cgd };
806 1.16 cgd /* ARGSUSED */
807 1.16 cgd flock(p, uap, retval)
808 1.16 cgd struct proc *p;
809 1.16 cgd register struct flock_args *uap;
810 1.16 cgd int *retval;
811 1.16 cgd {
812 1.16 cgd register struct filedesc *fdp = p->p_fd;
813 1.16 cgd register struct file *fp;
814 1.16 cgd struct vnode *vp;
815 1.16 cgd struct flock lf;
816 1.16 cgd
817 1.16 cgd if ((unsigned)uap->fd >= fdp->fd_nfiles ||
818 1.16 cgd (fp = fdp->fd_ofiles[uap->fd]) == NULL)
819 1.16 cgd return (EBADF);
820 1.16 cgd if (fp->f_type != DTYPE_VNODE)
821 1.16 cgd return (EOPNOTSUPP);
822 1.16 cgd vp = (struct vnode *)fp->f_data;
823 1.16 cgd lf.l_whence = SEEK_SET;
824 1.16 cgd lf.l_start = 0;
825 1.16 cgd lf.l_len = 0;
826 1.16 cgd if (uap->how & LOCK_UN) {
827 1.16 cgd lf.l_type = F_UNLCK;
828 1.16 cgd fp->f_flag &= ~FHASLOCK;
829 1.16 cgd return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
830 1.16 cgd }
831 1.16 cgd if (uap->how & LOCK_EX)
832 1.16 cgd lf.l_type = F_WRLCK;
833 1.16 cgd else if (uap->how & LOCK_SH)
834 1.16 cgd lf.l_type = F_RDLCK;
835 1.16 cgd else
836 1.16 cgd return (EBADF);
837 1.16 cgd fp->f_flag |= FHASLOCK;
838 1.16 cgd if (uap->how & LOCK_NB)
839 1.16 cgd return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
840 1.16 cgd return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
841 1.16 cgd }
842 1.16 cgd
843 1.16 cgd /*
844 1.16 cgd * File Descriptor pseudo-device driver (/dev/fd/).
845 1.16 cgd *
846 1.16 cgd * Opening minor device N dup()s the file (if any) connected to file
847 1.16 cgd * descriptor N belonging to the calling process. Note that this driver
848 1.16 cgd * consists of only the ``open()'' routine, because all subsequent
849 1.16 cgd * references to this file will be direct to the other driver.
850 1.16 cgd */
851 1.16 cgd /* ARGSUSED */
852 1.16 cgd fdopen(dev, mode, type, p)
853 1.16 cgd dev_t dev;
854 1.16 cgd int mode, type;
855 1.16 cgd struct proc *p;
856 1.16 cgd {
857 1.16 cgd
858 1.16 cgd /*
859 1.16 cgd * XXX Kludge: set curproc->p_dupfd to contain the value of the
860 1.16 cgd * the file descriptor being sought for duplication. The error
861 1.16 cgd * return ensures that the vnode for this device will be released
862 1.16 cgd * by vn_open. Open will detect this special error and take the
863 1.16 cgd * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
864 1.16 cgd * will simply report the error.
865 1.16 cgd */
866 1.17 cgd p->p_dupfd = minor(dev);
867 1.16 cgd return (ENODEV);
868 1.16 cgd }
869 1.16 cgd
870 1.16 cgd /*
871 1.16 cgd * Duplicate the specified descriptor to a free descriptor.
872 1.16 cgd */
873 1.16 cgd dupfdopen(fdp, indx, dfd, mode, error)
874 1.16 cgd register struct filedesc *fdp;
875 1.16 cgd register int indx, dfd;
876 1.16 cgd int mode;
877 1.16 cgd int error;
878 1.16 cgd {
879 1.16 cgd register struct file *wfp;
880 1.16 cgd struct file *fp;
881 1.16 cgd
882 1.16 cgd /*
883 1.16 cgd * If the to-be-dup'd fd number is greater than the allowed number
884 1.16 cgd * of file descriptors, or the fd to be dup'd has already been
885 1.16 cgd * closed, reject. Note, check for new == old is necessary as
886 1.16 cgd * falloc could allocate an already closed to-be-dup'd descriptor
887 1.16 cgd * as the new descriptor.
888 1.16 cgd */
889 1.16 cgd fp = fdp->fd_ofiles[indx];
890 1.16 cgd if ((u_int)dfd >= fdp->fd_nfiles ||
891 1.16 cgd (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
892 1.16 cgd return (EBADF);
893 1.16 cgd
894 1.16 cgd /*
895 1.16 cgd * There are two cases of interest here.
896 1.16 cgd *
897 1.16 cgd * For ENODEV simply dup (dfd) to file descriptor
898 1.16 cgd * (indx) and return.
899 1.16 cgd *
900 1.16 cgd * For ENXIO steal away the file structure from (dfd) and
901 1.16 cgd * store it in (indx). (dfd) is effectively closed by
902 1.16 cgd * this operation.
903 1.16 cgd *
904 1.16 cgd * Any other error code is just returned.
905 1.16 cgd */
906 1.16 cgd switch (error) {
907 1.16 cgd case ENODEV:
908 1.16 cgd /*
909 1.17 cgd * Check that the mode the file is being opened for is a
910 1.17 cgd * subset of the mode of the existing descriptor.
911 1.16 cgd */
912 1.16 cgd if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
913 1.16 cgd return (EACCES);
914 1.16 cgd fdp->fd_ofiles[indx] = wfp;
915 1.16 cgd fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
916 1.16 cgd wfp->f_count++;
917 1.16 cgd if (indx > fdp->fd_lastfile)
918 1.16 cgd fdp->fd_lastfile = indx;
919 1.16 cgd return (0);
920 1.16 cgd
921 1.16 cgd case ENXIO:
922 1.16 cgd /*
923 1.16 cgd * Steal away the file pointer from dfd, and stuff it into indx.
924 1.16 cgd */
925 1.16 cgd fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
926 1.16 cgd fdp->fd_ofiles[dfd] = NULL;
927 1.16 cgd fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
928 1.16 cgd fdp->fd_ofileflags[dfd] = 0;
929 1.16 cgd /*
930 1.17 cgd * Complete the clean up of the filedesc structure by
931 1.17 cgd * recomputing the various hints.
932 1.16 cgd */
933 1.16 cgd if (indx > fdp->fd_lastfile)
934 1.16 cgd fdp->fd_lastfile = indx;
935 1.16 cgd else
936 1.16 cgd while (fdp->fd_lastfile > 0 &&
937 1.16 cgd fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
938 1.16 cgd fdp->fd_lastfile--;
939 1.16 cgd if (dfd < fdp->fd_freefile)
940 1.16 cgd fdp->fd_freefile = dfd;
941 1.16 cgd return (0);
942 1.16 cgd
943 1.16 cgd default:
944 1.16 cgd return (error);
945 1.16 cgd }
946 1.17 cgd /* NOTREACHED */
947 1.16 cgd }
948