source: branches/minix3-book/servers/fs/misc.c@ 12

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

Importazione sorgenti libro

File size: 13.5 KB
Line 
1/* This file contains a collection of miscellaneous procedures. Some of them
2 * perform simple system calls. Some others do a little part of system calls
3 * that are mostly performed by the Memory Manager.
4 *
5 * The entry points into this file are
6 * do_dup: perform the DUP system call
7 * do_fcntl: perform the FCNTL system call
8 * do_sync: perform the SYNC system call
9 * do_fsync: perform the FSYNC system call
10 * do_reboot: sync disks and prepare for shutdown
11 * do_fork: adjust the tables after MM has performed a FORK system call
12 * do_exec: handle files with FD_CLOEXEC on after MM has done an EXEC
13 * do_exit: a process has exited; note that in the tables
14 * do_set: set uid or gid for some process
15 * do_revive: revive a process that was waiting for something (e.g. TTY)
16 * do_svrctl: file system control
17 * do_getsysinfo: request copy of FS data structure
18 */
19
20#include "fs.h"
21#include <fcntl.h>
22#include <unistd.h> /* cc runs out of memory with unistd.h :-( */
23#include <minix/callnr.h>
24#include <minix/com.h>
25#include <sys/svrctl.h>
26#include "buf.h"
27#include "file.h"
28#include "fproc.h"
29#include "inode.h"
30#include "param.h"
31#include "super.h"
32
33/*===========================================================================*
34 * do_getsysinfo *
35 *===========================================================================*/
36PUBLIC int do_getsysinfo()
37{
38 struct fproc *proc_addr;
39 vir_bytes src_addr, dst_addr;
40 size_t len;
41 int s;
42
43 switch(m_in.info_what) {
44 case SI_PROC_ADDR:
45 proc_addr = &fproc[0];
46 src_addr = (vir_bytes) &proc_addr;
47 len = sizeof(struct fproc *);
48 break;
49 case SI_PROC_TAB:
50 src_addr = (vir_bytes) fproc;
51 len = sizeof(struct fproc) * NR_PROCS;
52 break;
53 case SI_DMAP_TAB:
54 src_addr = (vir_bytes) dmap;
55 len = sizeof(struct dmap) * NR_DEVICES;
56 break;
57 default:
58 return(EINVAL);
59 }
60
61 dst_addr = (vir_bytes) m_in.info_where;
62 if (OK != (s=sys_datacopy(SELF, src_addr, who, dst_addr, len)))
63 return(s);
64 return(OK);
65
66}
67
68/*===========================================================================*
69 * do_dup *
70 *===========================================================================*/
71PUBLIC int do_dup()
72{
73/* Perform the dup(fd) or dup2(fd,fd2) system call. These system calls are
74 * obsolete. In fact, it is not even possible to invoke them using the
75 * current library because the library routines call fcntl(). They are
76 * provided to permit old binary programs to continue to run.
77 */
78
79 register int rfd;
80 register struct filp *f;
81 struct filp *dummy;
82 int r;
83
84 /* Is the file descriptor valid? */
85 rfd = m_in.fd & ~DUP_MASK; /* kill off dup2 bit, if on */
86 if ((f = get_filp(rfd)) == NIL_FILP) return(err_code);
87
88 /* Distinguish between dup and dup2. */
89 if (m_in.fd == rfd) { /* bit not on */
90 /* dup(fd) */
91 if ( (r = get_fd(0, 0, &m_in.fd2, &dummy)) != OK) return(r);
92 } else {
93 /* dup2(fd, fd2) */
94 if (m_in.fd2 < 0 || m_in.fd2 >= OPEN_MAX) return(EBADF);
95 if (rfd == m_in.fd2) return(m_in.fd2); /* ignore the call: dup2(x, x) */
96 m_in.fd = m_in.fd2; /* prepare to close fd2 */
97 (void) do_close(); /* cannot fail */
98 }
99
100 /* Success. Set up new file descriptors. */
101 f->filp_count++;
102 fp->fp_filp[m_in.fd2] = f;
103 return(m_in.fd2);
104}
105
106/*===========================================================================*
107 * do_fcntl *
108 *===========================================================================*/
109PUBLIC int do_fcntl()
110{
111/* Perform the fcntl(fd, request, ...) system call. */
112
113 register struct filp *f;
114 int new_fd, r, fl;
115 long cloexec_mask; /* bit map for the FD_CLOEXEC flag */
116 long clo_value; /* FD_CLOEXEC flag in proper position */
117 struct filp *dummy;
118
119 /* Is the file descriptor valid? */
120 if ((f = get_filp(m_in.fd)) == NIL_FILP) return(err_code);
121
122 switch (m_in.request) {
123 case F_DUPFD:
124 /* This replaces the old dup() system call. */
125 if (m_in.addr < 0 || m_in.addr >= OPEN_MAX) return(EINVAL);
126 if ((r = get_fd(m_in.addr, 0, &new_fd, &dummy)) != OK) return(r);
127 f->filp_count++;
128 fp->fp_filp[new_fd] = f;
129 return(new_fd);
130
131 case F_GETFD:
132 /* Get close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
133 return( ((fp->fp_cloexec >> m_in.fd) & 01) ? FD_CLOEXEC : 0);
134
135 case F_SETFD:
136 /* Set close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
137 cloexec_mask = 1L << m_in.fd; /* singleton set position ok */
138 clo_value = (m_in.addr & FD_CLOEXEC ? cloexec_mask : 0L);
139 fp->fp_cloexec = (fp->fp_cloexec & ~cloexec_mask) | clo_value;
140 return(OK);
141
142 case F_GETFL:
143 /* Get file status flags (O_NONBLOCK and O_APPEND). */
144 fl = f->filp_flags & (O_NONBLOCK | O_APPEND | O_ACCMODE);
145 return(fl);
146
147 case F_SETFL:
148 /* Set file status flags (O_NONBLOCK and O_APPEND). */
149 fl = O_NONBLOCK | O_APPEND;
150 f->filp_flags = (f->filp_flags & ~fl) | (m_in.addr & fl);
151 return(OK);
152
153 case F_GETLK:
154 case F_SETLK:
155 case F_SETLKW:
156 /* Set or clear a file lock. */
157 r = lock_op(f, m_in.request);
158 return(r);
159 default:
160 return(EINVAL);
161 }
162}
163
164/*===========================================================================*
165 * do_sync *
166 *===========================================================================*/
167PUBLIC int do_sync()
168{
169/* Perform the sync() system call. Flush all the tables.
170 * The order in which the various tables are flushed is critical. The
171 * blocks must be flushed last, since rw_inode() leaves its results in
172 * the block cache.
173 */
174 register struct inode *rip;
175 register struct buf *bp;
176
177 /* Write all the dirty inodes to the disk. */
178 for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
179 if (rip->i_count > 0 && rip->i_dirt == DIRTY) rw_inode(rip, WRITING);
180
181 /* Write all the dirty blocks to the disk, one drive at a time. */
182 for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++)
183 if (bp->b_dev != NO_DEV && bp->b_dirt == DIRTY) flushall(bp->b_dev);
184
185 return(OK); /* sync() can't fail */
186}
187
188/*===========================================================================*
189 * do_fsync *
190 *===========================================================================*/
191PUBLIC int do_fsync()
192{
193/* Perform the fsync() system call. For now, don't be unnecessarily smart. */
194
195 do_sync();
196
197 return(OK);
198}
199
200/*===========================================================================*
201 * do_reboot *
202 *===========================================================================*/
203PUBLIC int do_reboot()
204{
205 /* Perform the FS side of the reboot call. */
206 int i;
207 struct super_block *sp;
208 struct inode dummy;
209
210 /* Only PM may make this call directly. */
211 if (who != PM_PROC_NR) return(EGENERIC);
212
213 /* Do exit processing for all leftover processes and servers. */
214 for (i = 0; i < NR_PROCS; i++) { m_in.slot1 = i; do_exit(); }
215
216 /* The root file system is mounted onto itself, which keeps it from being
217 * unmounted. Pull an inode out of thin air and put the root on it.
218 */
219 put_inode(super_block[0].s_imount);
220 super_block[0].s_imount= &dummy;
221 dummy.i_count = 2; /* expect one "put" */
222
223 /* Unmount all filesystems. File systems are mounted on other file systems,
224 * so you have to pull off the loose bits repeatedly to get it all undone.
225 */
226 for (i= 0; i < NR_SUPERS; i++) {
227 /* Unmount at least one. */
228 for (sp= &super_block[0]; sp < &super_block[NR_SUPERS]; sp++) {
229 if (sp->s_dev != NO_DEV) (void) unmount(sp->s_dev);
230 }
231 }
232
233 return(OK);
234}
235
236/*===========================================================================*
237 * do_fork *
238 *===========================================================================*/
239PUBLIC int do_fork()
240{
241/* Perform those aspects of the fork() system call that relate to files.
242 * In particular, let the child inherit its parent's file descriptors.
243 * The parent and child parameters tell who forked off whom. The file
244 * system uses the same slot numbers as the kernel. Only MM makes this call.
245 */
246
247 register struct fproc *cp;
248 int i;
249
250 /* Only PM may make this call directly. */
251 if (who != PM_PROC_NR) return(EGENERIC);
252
253 /* Copy the parent's fproc struct to the child. */
254 fproc[m_in.child] = fproc[m_in.parent];
255
256 /* Increase the counters in the 'filp' table. */
257 cp = &fproc[m_in.child];
258 for (i = 0; i < OPEN_MAX; i++)
259 if (cp->fp_filp[i] != NIL_FILP) cp->fp_filp[i]->filp_count++;
260
261 /* Fill in new process id. */
262 cp->fp_pid = m_in.pid;
263
264 /* A child is not a process leader. */
265 cp->fp_sesldr = 0;
266
267 /* Record the fact that both root and working dir have another user. */
268 dup_inode(cp->fp_rootdir);
269 dup_inode(cp->fp_workdir);
270 return(OK);
271}
272
273/*===========================================================================*
274 * do_exec *
275 *===========================================================================*/
276PUBLIC int do_exec()
277{
278/* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec). When
279 * MM does an EXEC, it calls FS to allow FS to find these files and close them.
280 */
281
282 register int i;
283 long bitmap;
284
285 /* Only PM may make this call directly. */
286 if (who != PM_PROC_NR) return(EGENERIC);
287
288 /* The array of FD_CLOEXEC bits is in the fp_cloexec bit map. */
289 fp = &fproc[m_in.slot1]; /* get_filp() needs 'fp' */
290 bitmap = fp->fp_cloexec;
291 if (bitmap == 0) return(OK); /* normal case, no FD_CLOEXECs */
292
293 /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
294 for (i = 0; i < OPEN_MAX; i++) {
295 m_in.fd = i;
296 if ( (bitmap >> i) & 01) (void) do_close();
297 }
298
299 return(OK);
300}
301
302/*===========================================================================*
303 * do_exit *
304 *===========================================================================*/
305PUBLIC int do_exit()
306{
307/* Perform the file system portion of the exit(status) system call. */
308
309 register int i, exitee, task;
310 register struct fproc *rfp;
311 register struct filp *rfilp;
312 register struct inode *rip;
313 dev_t dev;
314
315 /* Only PM may do the EXIT call directly. */
316 if (who != PM_PROC_NR) return(EGENERIC);
317
318 /* Nevertheless, pretend that the call came from the user. */
319 fp = &fproc[m_in.slot1]; /* get_filp() needs 'fp' */
320 exitee = m_in.slot1;
321
322 if (fp->fp_suspended == SUSPENDED) {
323 task = -fp->fp_task;
324 if (task == XPIPE || task == XPOPEN) susp_count--;
325 m_in.pro = exitee;
326 (void) do_unpause(); /* this always succeeds for MM */
327 fp->fp_suspended = NOT_SUSPENDED;
328 }
329
330 /* Loop on file descriptors, closing any that are open. */
331 for (i = 0; i < OPEN_MAX; i++) {
332 m_in.fd = i;
333 (void) do_close();
334 }
335
336 /* Release root and working directories. */
337 put_inode(fp->fp_rootdir);
338 put_inode(fp->fp_workdir);
339 fp->fp_rootdir = NIL_INODE;
340 fp->fp_workdir = NIL_INODE;
341
342 /* If a session leader exits then revoke access to its controlling tty from
343 * all other processes using it.
344 */
345 if (!fp->fp_sesldr) {
346 fp->fp_pid = PID_FREE;
347 return(OK); /* not a session leader */
348 }
349 fp->fp_sesldr = FALSE;
350 if (fp->fp_tty == 0) {
351 fp->fp_pid = PID_FREE;
352 return(OK); /* no controlling tty */
353 }
354 dev = fp->fp_tty;
355
356 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
357 if (rfp->fp_tty == dev) rfp->fp_tty = 0;
358
359 for (i = 0; i < OPEN_MAX; i++) {
360 if ((rfilp = rfp->fp_filp[i]) == NIL_FILP) continue;
361 if (rfilp->filp_mode == FILP_CLOSED) continue;
362 rip = rfilp->filp_ino;
363 if ((rip->i_mode & I_TYPE) != I_CHAR_SPECIAL) continue;
364 if ((dev_t) rip->i_zone[0] != dev) continue;
365 dev_close(dev);
366 rfilp->filp_mode = FILP_CLOSED;
367 }
368 }
369
370 /* Mark slot as free. */
371 fp->fp_pid = PID_FREE;
372 return(OK);
373}
374
375/*===========================================================================*
376 * do_set *
377 *===========================================================================*/
378PUBLIC int do_set()
379{
380/* Set uid_t or gid_t field. */
381
382 register struct fproc *tfp;
383
384 /* Only PM may make this call directly. */
385 if (who != PM_PROC_NR) return(EGENERIC);
386
387 tfp = &fproc[m_in.slot1];
388 if (call_nr == SETUID) {
389 tfp->fp_realuid = (uid_t) m_in.real_user_id;
390 tfp->fp_effuid = (uid_t) m_in.eff_user_id;
391 }
392 if (call_nr == SETGID) {
393 tfp->fp_effgid = (gid_t) m_in.eff_grp_id;
394 tfp->fp_realgid = (gid_t) m_in.real_grp_id;
395 }
396 return(OK);
397}
398
399/*===========================================================================*
400 * do_revive *
401 *===========================================================================*/
402PUBLIC int do_revive()
403{
404/* A driver, typically TTY, has now gotten the characters that were needed for
405 * a previous read. The process did not get a reply when it made the call.
406 * Instead it was suspended. Now we can send the reply to wake it up. This
407 * business has to be done carefully, since the incoming message is from
408 * a driver (to which no reply can be sent), and the reply must go to a process
409 * that blocked earlier. The reply to the caller is inhibited by returning the
410 * 'SUSPEND' pseudo error, and the reply to the blocked process is done
411 * explicitly in revive().
412 */
413
414 revive(m_in.REP_PROC_NR, m_in.REP_STATUS);
415 return(SUSPEND); /* don't reply to the TTY task */
416}
417
418/*===========================================================================*
419 * do_svrctl *
420 *===========================================================================*/
421PUBLIC int do_svrctl()
422{
423 switch (m_in.svrctl_req) {
424 case FSSIGNON: {
425 /* A server in user space calls in to manage a device. */
426 struct fssignon device;
427 int r, major;
428
429 if (fp->fp_effuid != SU_UID) return(EPERM);
430
431 /* Try to copy request structure to FS. */
432 if ((r = sys_datacopy(who, (vir_bytes) m_in.svrctl_argp,
433 FS_PROC_NR, (vir_bytes) &device,
434 (phys_bytes) sizeof(device))) != OK)
435 return(r);
436
437 /* Try to update device mapping. */
438 major = (device.dev >> MAJOR) & BYTE;
439 r=map_driver(major, who, device.style);
440 return(r);
441 }
442 default:
443 return(EINVAL);
444 }
445}
Note: See TracBrowser for help on using the repository browser.