1*ce099b40Smartin /* $NetBSD: procfs_fd.c,v 1.14 2008/04/28 20:24:08 martin Exp $ */
2dbe6c38bSchristos
3dbe6c38bSchristos /*-
4a9ca7a37Sad * Copyright (c) 2003, 2008 The NetBSD Foundation, Inc.
5dbe6c38bSchristos * All rights reserved.
6dbe6c38bSchristos *
7dbe6c38bSchristos * This code is derived from software contributed to The NetBSD Foundation
8dbe6c38bSchristos * by Christos Zoulas.
9dbe6c38bSchristos *
10dbe6c38bSchristos * Redistribution and use in source and binary forms, with or without
11dbe6c38bSchristos * modification, are permitted provided that the following conditions
12dbe6c38bSchristos * are met:
13dbe6c38bSchristos * 1. Redistributions of source code must retain the above copyright
14dbe6c38bSchristos * notice, this list of conditions and the following disclaimer.
15dbe6c38bSchristos * 2. Redistributions in binary form must reproduce the above copyright
16dbe6c38bSchristos * notice, this list of conditions and the following disclaimer in the
17dbe6c38bSchristos * documentation and/or other materials provided with the distribution.
18dbe6c38bSchristos *
19dbe6c38bSchristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20dbe6c38bSchristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21dbe6c38bSchristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22dbe6c38bSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23dbe6c38bSchristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24dbe6c38bSchristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25dbe6c38bSchristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26dbe6c38bSchristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27dbe6c38bSchristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28dbe6c38bSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29dbe6c38bSchristos * POSSIBILITY OF SUCH DAMAGE.
30dbe6c38bSchristos */
31b07ec3fcSad
32dbe6c38bSchristos #include <sys/cdefs.h>
33*ce099b40Smartin __KERNEL_RCSID(0, "$NetBSD: procfs_fd.c,v 1.14 2008/04/28 20:24:08 martin Exp $");
34dbe6c38bSchristos
35dbe6c38bSchristos #include <sys/param.h>
36dbe6c38bSchristos #include <sys/systm.h>
37dbe6c38bSchristos #include <sys/time.h>
38dbe6c38bSchristos #include <sys/kernel.h>
39dbe6c38bSchristos #include <sys/proc.h>
40dbe6c38bSchristos #include <sys/vnode.h>
41dbe6c38bSchristos #include <sys/file.h>
42ed9a5a74Sjdolecek #include <sys/filedesc.h>
43a9ca7a37Sad
44dbe6c38bSchristos #include <miscfs/procfs/procfs.h>
45dbe6c38bSchristos
46dbe6c38bSchristos int
procfs_dofd(lwp_t * curl,proc_t * p,struct pfsnode * pfs,struct uio * uio)47a9ca7a37Sad procfs_dofd(lwp_t *curl, proc_t *p, struct pfsnode *pfs, struct uio *uio)
48dbe6c38bSchristos {
49dbe6c38bSchristos int error;
50a9ca7a37Sad file_t *fp;
51dbe6c38bSchristos off_t offs;
52dbe6c38bSchristos
53a9ca7a37Sad if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL)
54a9ca7a37Sad return EBADF;
55ed9a5a74Sjdolecek
56dbe6c38bSchristos offs = fp->f_offset;
57dbe6c38bSchristos
58dbe6c38bSchristos switch (uio->uio_rw) {
59dbe6c38bSchristos case UIO_READ:
60f474dcebSad error = (*fp->f_ops->fo_read)(fp, &offs, uio, curl->l_cred, 0);
61bbdab699Snakayama break;
62dbe6c38bSchristos case UIO_WRITE:
63f474dcebSad error = (*fp->f_ops->fo_write)(fp, &offs, uio, curl->l_cred, 0);
64bbdab699Snakayama break;
65dbe6c38bSchristos default:
66dbe6c38bSchristos panic("bad uio op");
67dbe6c38bSchristos }
68ed9a5a74Sjdolecek
69a9ca7a37Sad closef(fp);
70ed9a5a74Sjdolecek
71ed9a5a74Sjdolecek return (error);
72dbe6c38bSchristos }
73