source: rtems-libbsd/freebsd/sys/sys/file.h @ caf8ede

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since caf8ede was caf8ede, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/13 at 09:29:33

Import socket() implementation from FreeBSD

Add new test syscalls01.

  • Property mode set to 100644
File size: 11.6 KB
Line 
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *      @(#)file.h      8.3 (Berkeley) 1/9/95
30 * $FreeBSD$
31 */
32
33#ifndef _SYS_FILE_H_
34#define _SYS_FILE_H_
35
36#ifndef _KERNEL
37#include <rtems/bsd/sys/types.h> /* XXX */
38#include <sys/fcntl.h>
39#include <rtems/bsd/sys/unistd.h>
40#else
41#include <sys/queue.h>
42#include <sys/refcount.h>
43#include <sys/_lock.h>
44#include <sys/_mutex.h>
45
46struct stat;
47struct thread;
48struct uio;
49struct knote;
50struct vnode;
51struct socket;
52
53
54#endif /* _KERNEL */
55
56#define DTYPE_VNODE     1       /* file */
57#define DTYPE_SOCKET    2       /* communications endpoint */
58#define DTYPE_PIPE      3       /* pipe */
59#define DTYPE_FIFO      4       /* fifo (named pipe) */
60#define DTYPE_KQUEUE    5       /* event queue */
61#define DTYPE_CRYPTO    6       /* crypto */
62#define DTYPE_MQUEUE    7       /* posix message queue */
63#define DTYPE_SHM       8       /* swap-backed shared memory */
64#define DTYPE_SEM       9       /* posix semaphore */
65#define DTYPE_PTS       10      /* pseudo teletype master device */
66
67#ifdef _KERNEL
68
69struct file;
70struct ucred;
71
72typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
73                    struct ucred *active_cred, int flags,
74                    struct thread *td);
75#define FOF_OFFSET      1       /* Use the offset in uio argument */
76typedef int fo_truncate_t(struct file *fp, off_t length,
77                    struct ucred *active_cred, struct thread *td);
78typedef int fo_ioctl_t(struct file *fp, u_long com, void *data,
79                    struct ucred *active_cred, struct thread *td);
80typedef int fo_poll_t(struct file *fp, int events,
81                    struct ucred *active_cred, struct thread *td);
82typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
83typedef int fo_stat_t(struct file *fp, struct stat *sb,
84                    struct ucred *active_cred, struct thread *td);
85typedef int fo_close_t(struct file *fp, struct thread *td);
86typedef int fo_flags_t;
87
88struct fileops {
89        fo_rdwr_t       *fo_read;
90        fo_rdwr_t       *fo_write;
91        fo_truncate_t   *fo_truncate;
92        fo_ioctl_t      *fo_ioctl;
93        fo_poll_t       *fo_poll;
94        fo_kqfilter_t   *fo_kqfilter;
95        fo_stat_t       *fo_stat;
96        fo_close_t      *fo_close;
97        fo_flags_t      fo_flags;       /* DFLAG_* below */
98};
99
100#define DFLAG_PASSABLE  0x01    /* may be passed via unix sockets. */
101#define DFLAG_SEEKABLE  0x02    /* seekable / nonsequential */
102#endif /* _KERNEL */
103
104#if defined(_KERNEL) || defined(_WANT_FILE)
105#ifdef __rtems__
106#include <rtems/libio_.h>
107#include <sys/fcntl.h>
108#endif /* __rtems__ */
109/*
110 * Kernel descriptor table.
111 * One entry for each open kernel vnode and socket.
112 *
113 * Below is the list of locks that protects members in struct file.
114 *
115 * (f) protected with mtx_lock(mtx_pool_find(fp))
116 * (d) cdevpriv_mtx
117 * none not locked
118 */
119
120struct file {
121#ifndef __rtems__
122        void            *f_data;        /* file descriptor specific data */
123        struct fileops  *f_ops;         /* File operations */
124        struct ucred    *f_cred;        /* associated credentials. */
125        struct vnode    *f_vnode;       /* NULL or applicable vnode */
126        short           f_type;         /* descriptor type */
127        short           f_vnread_flags; /* (f) Sleep lock for f_offset */
128        volatile u_int  f_flag;         /* see fcntl.h */
129        volatile u_int  f_count;        /* reference count */
130        /*
131         *  DTYPE_VNODE specific fields.
132         */
133        int             f_seqcount;     /* Count of sequential accesses. */
134        off_t           f_nextoff;      /* next expected read/write offset. */
135        struct cdev_privdata *f_cdevpriv; /* (d) Private data for the cdev. */
136        /*
137         *  DFLAG_SEEKABLE specific fields
138         */
139        off_t           f_offset;
140        /*
141         * Mandatory Access control information.
142         */
143        void            *f_label;       /* Place-holder for MAC label. */
144#else /* __rtems__ */
145        rtems_libio_t   f_io;
146#endif /* __rtems__ */
147};
148#ifdef __rtems__
149#define f_data f_io.pathinfo.node_access
150
151static inline struct file *
152rtems_bsd_iop_to_fp(rtems_libio_t *iop)
153{
154        return (struct file *) iop;
155}
156
157static inline struct file *
158rtems_bsd_fd_to_fp(int fd)
159{
160        return rtems_bsd_iop_to_fp(&rtems_libio_iops[fd]);
161}
162
163static inline int
164rtems_bsd_fp_to_fd(struct file *fp)
165{
166        return fp - rtems_bsd_iop_to_fp(&rtems_libio_iops[0]);
167}
168
169static inline void *
170rtems_bsd_loc_to_f_data(const rtems_filesystem_location_info_t *loc)
171{
172        return loc->node_access;
173}
174
175static inline uint32_t
176rtems_bsd_fflag_to_libio_flags(u_int fflag)
177{
178        uint32_t libio_flags = 0;
179
180        if ((fflag & FREAD) == FREAD) {
181                libio_flags |= LIBIO_FLAGS_READ;
182        }
183
184        if ((fflag & FWRITE) == FWRITE) {
185                libio_flags |= LIBIO_FLAGS_WRITE;
186        }
187
188        if ((fflag & FNONBLOCK) == FNONBLOCK) {
189                libio_flags |= LIBIO_FLAGS_NO_DELAY;
190        }
191
192        return (libio_flags);
193}
194
195static inline u_int
196rtems_bsd_libio_flags_to_fflag(uint32_t libio_flags)
197{
198        u_int fflag = 0;
199
200        if ((libio_flags & LIBIO_FLAGS_READ) == LIBIO_FLAGS_READ) {
201                fflag |= FREAD;
202        }
203
204        if ((libio_flags & LIBIO_FLAGS_WRITE) == LIBIO_FLAGS_WRITE) {
205                fflag |= FWRITE;
206        }
207
208        if ((libio_flags & LIBIO_FLAGS_NO_DELAY) == LIBIO_FLAGS_NO_DELAY) {
209                fflag |= FNONBLOCK;
210        }
211
212        return (fflag);
213}
214
215static int inline
216rtems_bsd_error_to_status_and_errno(int error)
217{
218        if (error == 0) {
219                return 0;
220        } else {
221                rtems_set_errno_and_return_minus_one(error);
222        }
223}
224#endif /* __rtems__ */
225
226#define FOFFSET_LOCKED       0x1
227#define FOFFSET_LOCK_WAITING 0x2                 
228
229#endif /* _KERNEL || _WANT_FILE */
230
231/*
232 * Userland version of struct file, for sysctl
233 */
234struct xfile {
235        size_t  xf_size;        /* size of struct xfile */
236        pid_t   xf_pid;         /* owning process */
237        uid_t   xf_uid;         /* effective uid of owning process */
238        int     xf_fd;          /* descriptor number */
239        void    *xf_file;       /* address of struct file */
240        short   xf_type;        /* descriptor type */
241        int     xf_count;       /* reference count */
242        int     xf_msgcount;    /* references from message queue */
243        off_t   xf_offset;      /* file offset */
244        void    *xf_data;       /* file descriptor specific data */
245        void    *xf_vnode;      /* vnode pointer */
246        u_int   xf_flag;        /* flags (see fcntl.h) */
247};
248
249#ifdef _KERNEL
250
251#ifdef MALLOC_DECLARE
252MALLOC_DECLARE(M_FILE);
253#endif
254
255extern struct fileops vnops;
256extern struct fileops badfileops;
257#ifndef __rtems__
258extern struct fileops socketops;
259#else /* __rtems__ */
260extern const rtems_filesystem_file_handlers_r socketops;
261#endif /* __rtems__ */
262extern int maxfiles;            /* kernel limit on number of open files */
263extern int maxfilesperproc;     /* per process limit on number of open files */
264extern volatile int openfiles;  /* actual number of open files */
265
266#ifndef __rtems__
267int fget(struct thread *td, int fd, struct file **fpp);
268#else /* __rtems__ */
269struct file *rtems_bsd_get_file(int fd);
270
271static inline int
272fget(struct thread *td, int fd, struct file **fpp)
273{
274        struct file *fp = rtems_bsd_get_file(fd);
275
276        (void) td;
277
278        *fpp = fp;
279
280        return fp != NULL ? 0 : EBADF;
281}
282#endif /* __rtems__ */
283int fget_read(struct thread *td, int fd, struct file **fpp);
284int fget_write(struct thread *td, int fd, struct file **fpp);
285int _fdrop(struct file *fp, struct thread *td);
286
287#ifndef __rtems__
288/*
289 * The socket operations are used a couple of places.
290 * XXX: This is wrong, they should go through the operations vector for
291 * XXX: sockets instead of going directly for the individual functions. /phk
292 */
293fo_rdwr_t       soo_read;
294fo_rdwr_t       soo_write;
295fo_truncate_t   soo_truncate;
296fo_ioctl_t      soo_ioctl;
297fo_poll_t       soo_poll;
298fo_kqfilter_t   soo_kqfilter;
299fo_stat_t       soo_stat;
300fo_close_t      soo_close;
301#endif /* __rtems__ */
302
303#ifndef __rtems__
304void finit(struct file *, u_int, short, void *, struct fileops *);
305#else /* __rtems__ */
306static inline void
307finit(struct file *fp, u_int fflag, short type, void *data,
308    const rtems_filesystem_file_handlers_r *ops)
309{
310        rtems_filesystem_location_info_t *pathinfo = &fp->f_io.pathinfo;
311
312        (void) type;
313
314        fp->f_data = data;
315        fp->f_io.flags |= rtems_bsd_fflag_to_libio_flags(fflag);
316
317        pathinfo->handlers = ops;
318        pathinfo->mt_entry = &rtems_filesystem_null_mt_entry;
319        rtems_filesystem_location_add_to_mt_entry(pathinfo);
320}
321#endif /* __rtems__ */
322int fgetvp(struct thread *td, int fd, struct vnode **vpp);
323int fgetvp_read(struct thread *td, int fd, struct vnode **vpp);
324int fgetvp_write(struct thread *td, int fd, struct vnode **vpp);
325
326int fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp);
327void fputsock(struct socket *sp);
328
329#define fhold(fp)                                                       \
330        (refcount_acquire(&(fp)->f_count))
331#ifndef __rtems__
332#define fdrop(fp, td)                                                   \
333        (refcount_release(&(fp)->f_count) ? _fdrop((fp), (td)) : 0)
334#else /* __rtems__ */
335#define fdrop(fp, td) do { } while (0)
336#endif /* __rtems__ */
337
338#ifndef __rtems__
339static __inline fo_rdwr_t       fo_read;
340static __inline fo_rdwr_t       fo_write;
341static __inline fo_truncate_t   fo_truncate;
342static __inline fo_ioctl_t      fo_ioctl;
343static __inline fo_poll_t       fo_poll;
344static __inline fo_kqfilter_t   fo_kqfilter;
345static __inline fo_stat_t       fo_stat;
346static __inline fo_close_t      fo_close;
347
348static __inline int
349fo_read(fp, uio, active_cred, flags, td)
350        struct file *fp;
351        struct uio *uio;
352        struct ucred *active_cred;
353        int flags;
354        struct thread *td;
355{
356
357        return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
358}
359
360static __inline int
361fo_write(fp, uio, active_cred, flags, td)
362        struct file *fp;
363        struct uio *uio;
364        struct ucred *active_cred;
365        int flags;
366        struct thread *td;
367{
368
369        return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
370}
371
372static __inline int
373fo_truncate(fp, length, active_cred, td)
374        struct file *fp;
375        off_t length;
376        struct ucred *active_cred;
377        struct thread *td;
378{
379
380        return ((*fp->f_ops->fo_truncate)(fp, length, active_cred, td));
381}
382#endif /* __rtems__ */
383
384static __inline int
385fo_ioctl(fp, com, data, active_cred, td)
386        struct file *fp;
387        u_long com;
388        void *data;
389        struct ucred *active_cred;
390        struct thread *td;
391{
392
393#ifndef __rtems__
394        return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
395#else /* __rtems__ */
396        int rv;
397
398        (void) active_cred;
399        (void) td;
400
401        errno = 0;
402        rv = ((*fp->f_io.pathinfo.handlers->ioctl_h)(&fp->f_io, com, data));
403        if (rv == 0) {
404                return (0);
405        } else {
406                return (errno);
407        }
408#endif /* __rtems__ */
409}
410
411#ifndef __rtems__
412static __inline int
413fo_poll(fp, events, active_cred, td)
414        struct file *fp;
415        int events;
416        struct ucred *active_cred;
417        struct thread *td;
418{
419
420        return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
421}
422
423static __inline int
424fo_stat(fp, sb, active_cred, td)
425        struct file *fp;
426        struct stat *sb;
427        struct ucred *active_cred;
428        struct thread *td;
429{
430
431        return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
432}
433
434static __inline int
435fo_close(fp, td)
436        struct file *fp;
437        struct thread *td;
438{
439
440        return ((*fp->f_ops->fo_close)(fp, td));
441}
442
443static __inline int
444fo_kqfilter(fp, kn)
445        struct file *fp;
446        struct knote *kn;
447{
448
449        return ((*fp->f_ops->fo_kqfilter)(fp, kn));
450}
451#endif /* __rtems__ */
452
453#endif /* _KERNEL */
454
455#endif /* !SYS_FILE_H */
Note: See TracBrowser for help on using the repository browser.