1 /* This file contains the procedures that manipulate file descriptors.
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()
11 #include <sys/select.h>
18 /*===========================================================================*
20 *===========================================================================*/
21 PUBLIC
int get_fd(int start
, mode_t bits
, int *k
, struct filp
**fpt
)
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()
28 register struct filp
*f
;
31 *k
= -1; /* we need a way to tell if file desc found */
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. */
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 */
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) {
50 f
->filp_selectors
= 0;
51 f
->filp_select_ops
= 0;
52 f
->filp_pipe_select_ops
= 0;
59 /* If control passes here, the filp table must be full. Report that back. */
63 /*===========================================================================*
65 *===========================================================================*/
66 PUBLIC
struct filp
*get_filp(fild
)
67 int fild
; /* file descriptor */
69 /* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
71 return get_filp2(fp
, fild
);
74 /*===========================================================================*
76 *===========================================================================*/
77 PUBLIC
struct filp
*get_filp2(rfp
, fild
)
78 register struct fproc
*rfp
;
79 int fild
; /* file descriptor */
81 /* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
84 if (fild
< 0 || fild
>= OPEN_MAX
) return(NIL_FILP
);
85 return(rfp
->fp_filp
[fild
]); /* may also be NIL_FILP */
88 /*===========================================================================*
90 *===========================================================================*/
91 PUBLIC
struct filp
*find_filp(register struct inode
*rip
, mode_t bits
)
93 /* Find a filp slot that refers to the inode 'rip' in a way as described
94 * by the mode bit 'bits'. Used for determining whether somebody is still
95 * interested in either end of a pipe. Also used when opening a FIFO to
96 * find partners to share a filp field with (to shared the file position).
97 * Like 'get_fd' it performs its job by linear search through the filp table.
100 register struct filp
*f
;
102 for (f
= &filp
[0]; f
< &filp
[NR_FILPS
]; f
++) {
103 if (f
->filp_count
!= 0 && f
->filp_ino
== rip
&& (f
->filp_mode
& bits
)){
108 /* If control passes here, the filp wasn't there. Report that back. */
112 /*===========================================================================*
114 *===========================================================================*/
115 PUBLIC
int inval_filp(struct filp
*fp
)
118 for(f
= 0; f
< NR_PROCS
; f
++) {
119 if(fproc
[f
].fp_pid
== PID_FREE
) continue;
120 for(fd
= 0; fd
< OPEN_MAX
; fd
++) {
121 if(fproc
[f
].fp_filp
[fd
] && fproc
[f
].fp_filp
[fd
] == fp
) {
122 fproc
[f
].fp_filp
[fd
] = NIL_FILP
;