source: trunk/minix/servers/fs/misc.c@ 10

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

Minix 3.1.2a

File size: 17.6 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/endpoint.h>
25#include <minix/com.h>
26#include <sys/svrctl.h>
27#include "buf.h"
28#include "file.h"
29#include "fproc.h"
30#include "inode.h"
31#include "param.h"
32#include "super.h"
33
34FORWARD _PROTOTYPE( int free_proc, (struct fproc *freed, int flags));
35
36#define FP_EXITING 1
37
38/*===========================================================================*
39 * do_getsysinfo *
40 *===========================================================================*/
41PUBLIC int do_getsysinfo()
42{
43 struct fproc *proc_addr;
44 vir_bytes src_addr, dst_addr;
45 size_t len;
46 int s;
47
48 switch(m_in.info_what) {
49 case SI_PROC_ADDR:
50 proc_addr = &fproc[0];
51 src_addr = (vir_bytes) &proc_addr;
52 len = sizeof(struct fproc *);
53 break;
54 case SI_PROC_TAB:
55 src_addr = (vir_bytes) fproc;
56 len = sizeof(struct fproc) * NR_PROCS;
57 break;
58 case SI_DMAP_TAB:
59 src_addr = (vir_bytes) dmap;
60 len = sizeof(struct dmap) * NR_DEVICES;
61 break;
62 default:
63 return(EINVAL);
64 }
65
66 dst_addr = (vir_bytes) m_in.info_where;
67 if (OK != (s=sys_datacopy(SELF, src_addr, who_e, dst_addr, len)))
68 return(s);
69 return(OK);
70
71}
72
73/*===========================================================================*
74 * do_dup *
75 *===========================================================================*/
76PUBLIC int do_dup()
77{
78/* Perform the dup(fd) or dup2(fd,fd2) system call. These system calls are
79 * obsolete. In fact, it is not even possible to invoke them using the
80 * current library because the library routines call fcntl(). They are
81 * provided to permit old binary programs to continue to run.
82 */
83
84 register int rfd;
85 register struct filp *f;
86 struct filp *dummy;
87 int r;
88
89 /* Is the file descriptor valid? */
90 rfd = m_in.fd & ~DUP_MASK; /* kill off dup2 bit, if on */
91 if ((f = get_filp(rfd)) == NIL_FILP) return(err_code);
92
93 /* Distinguish between dup and dup2. */
94 if (m_in.fd == rfd) { /* bit not on */
95 /* dup(fd) */
96 if ( (r = get_fd(0, 0, &m_in.fd2, &dummy)) != OK) return(r);
97 } else {
98 /* dup2(fd, fd2) */
99 if (m_in.fd2 < 0 || m_in.fd2 >= OPEN_MAX) return(EBADF);
100 if (rfd == m_in.fd2) return(m_in.fd2); /* ignore the call: dup2(x, x) */
101 m_in.fd = m_in.fd2; /* prepare to close fd2 */
102 (void) do_close(); /* cannot fail */
103 }
104
105 /* Success. Set up new file descriptors. */
106 f->filp_count++;
107 fp->fp_filp[m_in.fd2] = f;
108 FD_SET(m_in.fd2, &fp->fp_filp_inuse);
109 return(m_in.fd2);
110}
111
112/*===========================================================================*
113 * do_fcntl *
114 *===========================================================================*/
115PUBLIC int do_fcntl()
116{
117/* Perform the fcntl(fd, request, ...) system call. */
118
119 register struct filp *f;
120 int new_fd, r, fl;
121 long cloexec_mask; /* bit map for the FD_CLOEXEC flag */
122 long clo_value; /* FD_CLOEXEC flag in proper position */
123 struct filp *dummy;
124
125 /* Is the file descriptor valid? */
126 if ((f = get_filp(m_in.fd)) == NIL_FILP) return(err_code);
127
128 switch (m_in.request) {
129 case F_DUPFD:
130 /* This replaces the old dup() system call. */
131 if (m_in.addr < 0 || m_in.addr >= OPEN_MAX) return(EINVAL);
132 if ((r = get_fd(m_in.addr, 0, &new_fd, &dummy)) != OK) return(r);
133 f->filp_count++;
134 fp->fp_filp[new_fd] = f;
135 return(new_fd);
136
137 case F_GETFD:
138 /* Get close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
139 return( ((fp->fp_cloexec >> m_in.fd) & 01) ? FD_CLOEXEC : 0);
140
141 case F_SETFD:
142 /* Set close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
143 cloexec_mask = 1L << m_in.fd; /* singleton set position ok */
144 clo_value = (m_in.addr & FD_CLOEXEC ? cloexec_mask : 0L);
145 fp->fp_cloexec = (fp->fp_cloexec & ~cloexec_mask) | clo_value;
146 return(OK);
147
148 case F_GETFL:
149 /* Get file status flags (O_NONBLOCK and O_APPEND). */
150 fl = f->filp_flags & (O_NONBLOCK | O_APPEND | O_ACCMODE);
151 return(fl);
152
153 case F_SETFL:
154 /* Set file status flags (O_NONBLOCK and O_APPEND). */
155 fl = O_NONBLOCK | O_APPEND;
156 f->filp_flags = (f->filp_flags & ~fl) | (m_in.addr & fl);
157 return(OK);
158
159 case F_GETLK:
160 case F_SETLK:
161 case F_SETLKW:
162 /* Set or clear a file lock. */
163 r = lock_op(f, m_in.request);
164 return(r);
165
166 case F_FREESP:
167 {
168 /* Free a section of a file. Preparation is done here,
169 * actual freeing in freesp_inode().
170 */
171 off_t start, end;
172 struct flock flock_arg;
173 signed long offset;
174
175 /* Check if it's a regular file. */
176 if((f->filp_ino->i_mode & I_TYPE) != I_REGULAR) {
177 return EINVAL;
178 }
179
180 /* Copy flock data from userspace. */
181 if((r = sys_datacopy(who_e, (vir_bytes) m_in.name1,
182 SELF, (vir_bytes) &flock_arg,
183 (phys_bytes) sizeof(flock_arg))) != OK)
184 return r;
185
186 /* Convert starting offset to signed. */
187 offset = (signed long) flock_arg.l_start;
188
189 /* Figure out starting position base. */
190 switch(flock_arg.l_whence) {
191 case SEEK_SET: start = 0; if(offset < 0) return EINVAL; break;
192 case SEEK_CUR: start = f->filp_pos; break;
193 case SEEK_END: start = f->filp_ino->i_size; break;
194 default: return EINVAL;
195 }
196
197 /* Check for overflow or underflow. */
198 if(offset > 0 && start + offset < start) { return EINVAL; }
199 if(offset < 0 && start + offset > start) { return EINVAL; }
200 start += offset;
201 if(flock_arg.l_len > 0) {
202 end = start + flock_arg.l_len;
203 if(end <= start) {
204 return EINVAL;
205 }
206 r = freesp_inode(f->filp_ino, start, end);
207 } else {
208 r = truncate_inode(f->filp_ino, start);
209 }
210 return r;
211 }
212
213 default:
214 return(EINVAL);
215 }
216}
217
218/*===========================================================================*
219 * do_sync *
220 *===========================================================================*/
221PUBLIC int do_sync()
222{
223/* Perform the sync() system call. Flush all the tables.
224 * The order in which the various tables are flushed is critical. The
225 * blocks must be flushed last, since rw_inode() leaves its results in
226 * the block cache.
227 */
228 register struct inode *rip;
229 register struct buf *bp;
230
231 /* Write all the dirty inodes to the disk. */
232 for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
233 if (rip->i_count > 0 && rip->i_dirt == DIRTY) rw_inode(rip, WRITING);
234
235 /* Write all the dirty blocks to the disk, one drive at a time. */
236 for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++)
237 if (bp->b_dev != NO_DEV && bp->b_dirt == DIRTY) flushall(bp->b_dev);
238
239 return(OK); /* sync() can't fail */
240}
241
242/*===========================================================================*
243 * do_fsync *
244 *===========================================================================*/
245PUBLIC int do_fsync()
246{
247/* Perform the fsync() system call. For now, don't be unnecessarily smart. */
248
249 do_sync();
250
251 return(OK);
252}
253
254/*===========================================================================*
255 * do_reboot *
256 *===========================================================================*/
257PUBLIC int do_reboot()
258{
259 /* Perform the FS side of the reboot call. */
260 int i;
261 struct super_block *sp;
262 struct inode dummy;
263
264 /* Only PM may make this call directly. */
265 if (who_e != PM_PROC_NR) return(EGENERIC);
266
267 /* Do exit processing for all leftover processes and servers,
268 * but don't actually exit them (if they were really gone, PM
269 * will tell us about it).
270 */
271 for (i = 0; i < NR_PROCS; i++)
272 if((m_in.endpt1 = fproc[i].fp_endpoint) != NONE)
273 free_proc(&fproc[i], 0);
274
275 /* The root file system is mounted onto itself, which keeps it from being
276 * unmounted. Pull an inode out of thin air and put the root on it.
277 */
278 put_inode(super_block[0].s_imount);
279 super_block[0].s_imount= &dummy;
280 dummy.i_count = 2; /* expect one "put" */
281
282 /* Unmount all filesystems. File systems are mounted on other file systems,
283 * so you have to pull off the loose bits repeatedly to get it all undone.
284 */
285 for (i= 0; i < NR_SUPERS; i++) {
286 /* Unmount at least one. */
287 for (sp= &super_block[0]; sp < &super_block[NR_SUPERS]; sp++) {
288 if (sp->s_dev != NO_DEV) (void) unmount(sp->s_dev);
289 }
290 }
291
292 /* Sync any unwritten buffers. */
293 do_sync();
294
295 return(OK);
296}
297
298/*===========================================================================*
299 * do_fork *
300 *===========================================================================*/
301PUBLIC int do_fork()
302{
303/* Perform those aspects of the fork() system call that relate to files.
304 * In particular, let the child inherit its parent's file descriptors.
305 * The parent and child parameters tell who forked off whom. The file
306 * system uses the same slot numbers as the kernel. Only MM makes this call.
307 */
308
309 register struct fproc *cp;
310 int i, parentno, childno;
311
312 /* Only PM may make this call directly. */
313 if (who_e != PM_PROC_NR) return(EGENERIC);
314
315 /* Check up-to-dateness of fproc. */
316 okendpt(m_in.parent_endpt, &parentno);
317
318 /* PM gives child endpoint, which implies process slot information.
319 * Don't call isokendpt, because that will verify if the endpoint
320 * number is correct in fproc, which it won't be.
321 */
322 childno = _ENDPOINT_P(m_in.child_endpt);
323 if(childno < 0 || childno >= NR_PROCS)
324 panic(__FILE__, "FS: bogus child for forking", m_in.child_endpt);
325 if(fproc[childno].fp_pid != PID_FREE)
326 panic(__FILE__, "FS: forking on top of in-use child", childno);
327
328 /* Copy the parent's fproc struct to the child. */
329 fproc[childno] = fproc[parentno];
330
331 /* Increase the counters in the 'filp' table. */
332 cp = &fproc[childno];
333 for (i = 0; i < OPEN_MAX; i++)
334 if (cp->fp_filp[i] != NIL_FILP) cp->fp_filp[i]->filp_count++;
335
336 /* Fill in new process and endpoint id. */
337 cp->fp_pid = m_in.pid;
338 cp->fp_endpoint = m_in.child_endpt;
339
340 /* A child is not a process leader. */
341 cp->fp_sesldr = 0;
342
343 /* This child has not exec()ced yet. */
344 cp->fp_execced = 0;
345#if 0
346printf("do_fork: child %d, slot %d\n", m_in.child_endpt, cp-fproc);
347#endif
348
349 /* Record the fact that both root and working dir have another user. */
350 dup_inode(cp->fp_rootdir);
351 dup_inode(cp->fp_workdir);
352 return(OK);
353}
354
355/*===========================================================================*
356 * do_exec *
357 *===========================================================================*/
358PUBLIC int do_exec()
359{
360/* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec). When
361 * MM does an EXEC, it calls FS to allow FS to find these files and close them.
362 */
363
364 int i, proc;
365 long bitmap;
366
367 /* Only PM may make this call directly. */
368 if (who_e != PM_PROC_NR) return(EGENERIC);
369
370 /* The array of FD_CLOEXEC bits is in the fp_cloexec bit map. */
371 okendpt(m_in.endpt1, &proc);
372 fp = &fproc[proc]; /* get_filp() needs 'fp' */
373 bitmap = fp->fp_cloexec;
374 if (bitmap) {
375 /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
376 for (i = 0; i < OPEN_MAX; i++) {
377 m_in.fd = i;
378 if ( (bitmap >> i) & 01) (void) do_close();
379 }
380 }
381
382 /* This child has now exec()ced. */
383 fp->fp_execced = 1;
384
385 /* Reply to caller (PM) directly. */
386 reply(who_e, OK);
387
388 /* Check if this is a driver that can now be useful. */
389 dmap_endpt_up(fp->fp_endpoint);
390
391 /* Suppress reply to caller (caller already replied to). */
392 return SUSPEND;
393}
394
395/*===========================================================================*
396 * free_proc *
397 *===========================================================================*/
398PRIVATE int free_proc(struct fproc *exiter, int flags)
399{
400 int i, task;
401 register struct fproc *rfp;
402 register struct filp *rfilp;
403 register struct inode *rip;
404 dev_t dev;
405
406 fp = exiter; /* get_filp() needs 'fp' */
407
408 if (fp->fp_suspended == SUSPENDED) {
409 task = -fp->fp_task;
410 if (task == XPIPE || task == XPOPEN) susp_count--;
411 m_in.ENDPT = fp->fp_endpoint;
412 (void) do_unpause(); /* this always succeeds for MM */
413 fp->fp_suspended = NOT_SUSPENDED;
414 }
415
416 /* Loop on file descriptors, closing any that are open. */
417 for (i = 0; i < OPEN_MAX; i++) {
418 m_in.fd = i;
419 (void) do_close();
420 }
421
422 /* Release root and working directories. */
423 put_inode(fp->fp_rootdir);
424 put_inode(fp->fp_workdir);
425 fp->fp_rootdir = NIL_INODE;
426 fp->fp_workdir = NIL_INODE;
427
428 /* Check if any process is SUSPENDed on this driver.
429 * If a driver exits, unmap its entries in the dmap table.
430 * (unmapping has to be done after the first step, because the
431 * dmap table is used in the first step.)
432 */
433 unsuspend_by_endpt(fp->fp_endpoint);
434
435 /* The rest of these actions is only done when processes actually
436 * exit.
437 */
438 if(!(flags & FP_EXITING))
439 return OK;
440
441 dmap_unmap_by_endpt(fp->fp_endpoint);
442 /* Invalidate endpoint number for error and sanity checks. */
443 fp->fp_endpoint = NONE;
444
445 /* If a session leader exits and it has a controlling tty, then revoke
446 * access to its controlling tty from all other processes using it.
447 */
448 if (fp->fp_sesldr && fp->fp_tty != 0) {
449
450 dev = fp->fp_tty;
451
452 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
453 if(rfp->fp_pid == PID_FREE) continue;
454 if (rfp->fp_tty == dev) rfp->fp_tty = 0;
455
456 for (i = 0; i < OPEN_MAX; i++) {
457 if ((rfilp = rfp->fp_filp[i]) == NIL_FILP) continue;
458 if (rfilp->filp_mode == FILP_CLOSED) continue;
459 rip = rfilp->filp_ino;
460 if ((rip->i_mode & I_TYPE) != I_CHAR_SPECIAL) continue;
461 if ((dev_t) rip->i_zone[0] != dev) continue;
462 dev_close(dev);
463 rfilp->filp_mode = FILP_CLOSED;
464 }
465 }
466 }
467
468 /* Exit done. Mark slot as free. */
469 fp->fp_pid = PID_FREE;
470 return(OK);
471
472}
473
474/*===========================================================================*
475 * do_exit *
476 *===========================================================================*/
477PUBLIC int do_exit()
478{
479 int exitee_p, exitee_e;
480/* Perform the file system portion of the exit(status) system call. */
481
482 /* Only PM may do the EXIT call directly. */
483 if (who_e != PM_PROC_NR) return(EGENERIC);
484
485 /* Nevertheless, pretend that the call came from the user. */
486 exitee_e = m_in.endpt1;
487 okendpt(exitee_e, &exitee_p);
488 return free_proc(&fproc[exitee_p], FP_EXITING);
489}
490
491/*===========================================================================*
492 * do_set *
493 *===========================================================================*/
494PUBLIC int do_set()
495{
496/* Set uid_t or gid_t field. */
497
498 register struct fproc *tfp;
499 int proc;
500
501 /* Only PM may make this call directly. */
502 if (who_e != PM_PROC_NR) return(EGENERIC);
503
504 okendpt(m_in.endpt1, &proc);
505 tfp = &fproc[proc];
506 if (call_nr == SETUID) {
507 tfp->fp_realuid = (uid_t) m_in.real_user_id;
508 tfp->fp_effuid = (uid_t) m_in.eff_user_id;
509 }
510 if (call_nr == SETGID) {
511 tfp->fp_effgid = (gid_t) m_in.eff_grp_id;
512 tfp->fp_realgid = (gid_t) m_in.real_grp_id;
513 }
514 return(OK);
515}
516
517/*===========================================================================*
518 * do_revive *
519 *===========================================================================*/
520PUBLIC int do_revive()
521{
522/* A driver, typically TTY, has now gotten the characters that were needed for
523 * a previous read. The process did not get a reply when it made the call.
524 * Instead it was suspended. Now we can send the reply to wake it up. This
525 * business has to be done carefully, since the incoming message is from
526 * a driver (to which no reply can be sent), and the reply must go to a process
527 * that blocked earlier. The reply to the caller is inhibited by returning the
528 * 'SUSPEND' pseudo error, and the reply to the blocked process is done
529 * explicitly in revive().
530 */
531 revive(m_in.REP_ENDPT, m_in.REP_STATUS);
532 return(SUSPEND); /* don't reply to the TTY task */
533}
534
535/*===========================================================================*
536 * do_svrctl *
537 *===========================================================================*/
538PUBLIC int do_svrctl()
539{
540 switch (m_in.svrctl_req) {
541 case FSSIGNON: {
542 /* A server in user space calls in to manage a device. */
543 struct fssignon device;
544 int r, major, proc_nr_n;
545
546 if (fp->fp_effuid != SU_UID && fp->fp_effuid != SERVERS_UID)
547 return(EPERM);
548
549 /* Try to copy request structure to FS. */
550 if ((r = sys_datacopy(who_e, (vir_bytes) m_in.svrctl_argp,
551 FS_PROC_NR, (vir_bytes) &device,
552 (phys_bytes) sizeof(device))) != OK)
553 return(r);
554
555 if (isokendpt(who_e, &proc_nr_n) != OK)
556 return(EINVAL);
557
558 /* Try to update device mapping. */
559 major = (device.dev >> MAJOR) & BYTE;
560 r=map_driver(major, who_e, device.style);
561 if (r == OK)
562 {
563 /* If a driver has completed its exec(), it can be announced
564 * to be up.
565 */
566 if(fproc[proc_nr_n].fp_execced) {
567 dev_up(major);
568 } else {
569 dmap[major].dmap_flags |= DMAP_BABY;
570 }
571 }
572
573 return(r);
574 }
575 case FSDEVUNMAP: {
576 struct fsdevunmap fdu;
577 int r, major;
578 /* Try to copy request structure to FS. */
579 if ((r = sys_datacopy(who_e, (vir_bytes) m_in.svrctl_argp,
580 FS_PROC_NR, (vir_bytes) &fdu,
581 (phys_bytes) sizeof(fdu))) != OK)
582 return(r);
583 major = (fdu.dev >> MAJOR) & BYTE;
584 r=map_driver(major, NONE, 0);
585 return(r);
586 }
587 default:
588 return(EINVAL);
589 }
590}
Note: See TracBrowser for help on using the repository browser.