source: trunk/minix/servers/fs/filedes.c@ 22

Last change on this file since 22 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.6 KB
RevLine 
[9]1/* This file contains the procedures that manipulate file descriptors.
2 *
3 * The entry points into this file are
4 * get_fd: look for free file descriptor and free filp slots
5 * get_filp: look up the filp entry for a given file descriptor
6 * find_filp: find a filp slot that points to a given inode
7 * inval_filp: invalidate a filp and associated fd's, only let close()
8 * happen on it
9 */
10
11#include <sys/select.h>
12
13#include "fs.h"
14#include "file.h"
15#include "fproc.h"
16#include "inode.h"
17
18/*===========================================================================*
19 * get_fd *
20 *===========================================================================*/
21PUBLIC int get_fd(int start, mode_t bits, int *k, struct filp **fpt)
22{
23/* Look for a free file descriptor and a free filp slot. Fill in the mode word
24 * in the latter, but don't claim either one yet, since the open() or creat()
25 * may yet fail.
26 */
27
28 register struct filp *f;
29 register int i;
30
31 *k = -1; /* we need a way to tell if file desc found */
32
33 /* Search the fproc fp_filp table for a free file descriptor. */
34 for (i = start; i < OPEN_MAX; i++) {
35 if (fp->fp_filp[i] == NIL_FILP && !FD_ISSET(i, &fp->fp_filp_inuse)) {
36 /* A file descriptor has been located. */
37 *k = i;
38 break;
39 }
40 }
41
42 /* Check to see if a file descriptor has been found. */
43 if (*k < 0) return(EMFILE); /* this is why we initialized k to -1 */
44
45 /* Now that a file descriptor has been found, look for a free filp slot. */
46 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
47 if (f->filp_count == 0) {
48 f->filp_mode = bits;
49 f->filp_pos = 0L;
50 f->filp_selectors = 0;
51 f->filp_select_ops = 0;
52 f->filp_pipe_select_ops = 0;
53 f->filp_flags = 0;
54 *fpt = f;
55 return(OK);
56 }
57 }
58
59 /* If control passes here, the filp table must be full. Report that back. */
60 return(ENFILE);
61}
62
63/*===========================================================================*
64 * get_filp *
65 *===========================================================================*/
66PUBLIC struct filp *get_filp(fild)
67int fild; /* file descriptor */
68{
69/* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
70
71 err_code = EBADF;
72 if (fild < 0 || fild >= OPEN_MAX ) return(NIL_FILP);
73 return(fp->fp_filp[fild]); /* may also be NIL_FILP */
74}
75
76/*===========================================================================*
77 * find_filp *
78 *===========================================================================*/
79PUBLIC struct filp *find_filp(register struct inode *rip, mode_t bits)
80{
81/* Find a filp slot that refers to the inode 'rip' in a way as described
82 * by the mode bit 'bits'. Used for determining whether somebody is still
83 * interested in either end of a pipe. Also used when opening a FIFO to
84 * find partners to share a filp field with (to shared the file position).
85 * Like 'get_fd' it performs its job by linear search through the filp table.
86 */
87
88 register struct filp *f;
89
90 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
91 if (f->filp_count != 0 && f->filp_ino == rip && (f->filp_mode & bits)){
92 return(f);
93 }
94 }
95
96 /* If control passes here, the filp wasn't there. Report that back. */
97 return(NIL_FILP);
98}
99
100/*===========================================================================*
101 * inval_filp *
102 *===========================================================================*/
103PUBLIC int inval_filp(struct filp *fp)
104{
105 int f, fd, n = 0;
106 for(f = 0; f < NR_PROCS; f++) {
107 if(fproc[f].fp_pid == PID_FREE) continue;
108 for(fd = 0; fd < OPEN_MAX; fd++) {
109 if(fproc[f].fp_filp[fd] && fproc[f].fp_filp[fd] == fp) {
110 fproc[f].fp_filp[fd] = NIL_FILP;
111 n++;
112 }
113 }
114 }
115
116 return n;
117}
Note: See TracBrowser for help on using the repository browser.