FreeBSD/Linux Kernel Cross Reference
sys/servers/fs/misc.c
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 *===========================================================================*/
36 PUBLIC 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 *===========================================================================*/
71 PUBLIC 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 *===========================================================================*/
109 PUBLIC 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 *===========================================================================*/
167 PUBLIC 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 *===========================================================================*/
191 PUBLIC 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 *===========================================================================*/
203 PUBLIC 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 *===========================================================================*/
239 PUBLIC 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 /* This child has not exec()ced yet. */
268 cp->fp_execced = 0;
269
270 /* Record the fact that both root and working dir have another user. */
271 dup_inode(cp->fp_rootdir);
272 dup_inode(cp->fp_workdir);
273 return(OK);
274 }
275
276 /*===========================================================================*
277 * do_exec *
278 *===========================================================================*/
279 PUBLIC int do_exec()
280 {
281 /* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec). When
282 * MM does an EXEC, it calls FS to allow FS to find these files and close them.
283 */
284
285 register int i;
286 long bitmap;
287
288 /* Only PM may make this call directly. */
289 if (who != PM_PROC_NR) return(EGENERIC);
290
291 /* The array of FD_CLOEXEC bits is in the fp_cloexec bit map. */
292 fp = &fproc[m_in.slot1]; /* get_filp() needs 'fp' */
293 bitmap = fp->fp_cloexec;
294 if (bitmap) {
295 /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
296 for (i = 0; i < OPEN_MAX; i++) {
297 m_in.fd = i;
298 if ( (bitmap >> i) & 01) (void) do_close();
299 }
300 }
301
302 /* This child has now exec()ced. */
303 fp->fp_execced = 1;
304
305 /* Reply to caller (PM) directly. */
306 reply(who, OK);
307
308 /* Check if this is a driver that can now be useful. */
309 dmap_proc_up(fp - fproc);
310
311 /* Suppress reply to caller (caller already replied to). */
312 return SUSPEND;
313 }
314
315 /*===========================================================================*
316 * do_exit *
317 *===========================================================================*/
318 PUBLIC int do_exit()
319 {
320 /* Perform the file system portion of the exit(status) system call. */
321
322 register int i, exitee, task;
323 register struct fproc *rfp;
324 register struct filp *rfilp;
325 register struct inode *rip;
326 dev_t dev;
327
328 /* Only PM may do the EXIT call directly. */
329 if (who != PM_PROC_NR) return(EGENERIC);
330
331 /* Nevertheless, pretend that the call came from the user. */
332 fp = &fproc[m_in.slot1]; /* get_filp() needs 'fp' */
333 exitee = m_in.slot1;
334
335 if (fp->fp_suspended == SUSPENDED) {
336 task = -fp->fp_task;
337 if (task == XPIPE || task == XPOPEN) susp_count--;
338 m_in.pro = exitee;
339 (void) do_unpause(); /* this always succeeds for MM */
340 fp->fp_suspended = NOT_SUSPENDED;
341 }
342
343 /* Loop on file descriptors, closing any that are open. */
344 for (i = 0; i < OPEN_MAX; i++) {
345 m_in.fd = i;
346 (void) do_close();
347 }
348
349 /* Release root and working directories. */
350 put_inode(fp->fp_rootdir);
351 put_inode(fp->fp_workdir);
352 fp->fp_rootdir = NIL_INODE;
353 fp->fp_workdir = NIL_INODE;
354
355 /* Check if any process is SUSPENDed on this driver.
356 * If a driver exits, unmap its entries in the dmap table.
357 * (unmapping has to be done after the first step, because the
358 * dmap table is used in the first step.)
359 */
360 unsuspend_by_proc(exitee);
361 dmap_unmap_by_proc(exitee);
362
363 /* If a session leader exits then revoke access to its controlling tty from
364 * all other processes using it.
365 */
366 if (!fp->fp_sesldr) {
367 fp->fp_pid = PID_FREE;
368 return(OK); /* not a session leader */
369 }
370 fp->fp_sesldr = FALSE;
371 if (fp->fp_tty == 0) {
372 fp->fp_pid = PID_FREE;
373 return(OK); /* no controlling tty */
374 }
375 dev = fp->fp_tty;
376
377 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
378 if (rfp->fp_tty == dev) rfp->fp_tty = 0;
379
380 for (i = 0; i < OPEN_MAX; i++) {
381 if ((rfilp = rfp->fp_filp[i]) == NIL_FILP) continue;
382 if (rfilp->filp_mode == FILP_CLOSED) continue;
383 rip = rfilp->filp_ino;
384 if ((rip->i_mode & I_TYPE) != I_CHAR_SPECIAL) continue;
385 if ((dev_t) rip->i_zone[0] != dev) continue;
386 dev_close(dev);
387 rfilp->filp_mode = FILP_CLOSED;
388 }
389 }
390
391 /* Mark slot as free. */
392 fp->fp_pid = PID_FREE;
393
394 return(OK);
395 }
396
397 /*===========================================================================*
398 * do_set *
399 *===========================================================================*/
400 PUBLIC int do_set()
401 {
402 /* Set uid_t or gid_t field. */
403
404 register struct fproc *tfp;
405
406 /* Only PM may make this call directly. */
407 if (who != PM_PROC_NR) return(EGENERIC);
408
409 tfp = &fproc[m_in.slot1];
410 if (call_nr == SETUID) {
411 tfp->fp_realuid = (uid_t) m_in.real_user_id;
412 tfp->fp_effuid = (uid_t) m_in.eff_user_id;
413 }
414 if (call_nr == SETGID) {
415 tfp->fp_effgid = (gid_t) m_in.eff_grp_id;
416 tfp->fp_realgid = (gid_t) m_in.real_grp_id;
417 }
418 return(OK);
419 }
420
421 /*===========================================================================*
422 * do_revive *
423 *===========================================================================*/
424 PUBLIC int do_revive()
425 {
426 /* A driver, typically TTY, has now gotten the characters that were needed for
427 * a previous read. The process did not get a reply when it made the call.
428 * Instead it was suspended. Now we can send the reply to wake it up. This
429 * business has to be done carefully, since the incoming message is from
430 * a driver (to which no reply can be sent), and the reply must go to a process
431 * that blocked earlier. The reply to the caller is inhibited by returning the
432 * 'SUSPEND' pseudo error, and the reply to the blocked process is done
433 * explicitly in revive().
434 */
435
436 revive(m_in.REP_PROC_NR, m_in.REP_STATUS);
437 return(SUSPEND); /* don't reply to the TTY task */
438 }
439
440 /*===========================================================================*
441 * do_svrctl *
442 *===========================================================================*/
443 PUBLIC int do_svrctl()
444 {
445 switch (m_in.svrctl_req) {
446 case FSSIGNON: {
447 /* A server in user space calls in to manage a device. */
448 struct fssignon device;
449 int r, major;
450
451 if (fp->fp_effuid != SU_UID) return(EPERM);
452
453 /* Try to copy request structure to FS. */
454 if ((r = sys_datacopy(who, (vir_bytes) m_in.svrctl_argp,
455 FS_PROC_NR, (vir_bytes) &device,
456 (phys_bytes) sizeof(device))) != OK)
457 return(r);
458
459 /* Try to update device mapping. */
460 major = (device.dev >> MAJOR) & BYTE;
461 r=map_driver(major, who, device.style);
462 return(r);
463 }
464 case FSDEVUNMAP: {
465 struct fsdevunmap fdu;
466 int r, major;
467 /* Try to copy request structure to FS. */
468 if ((r = sys_datacopy(who, (vir_bytes) m_in.svrctl_argp,
469 FS_PROC_NR, (vir_bytes) &fdu,
470 (phys_bytes) sizeof(fdu))) != OK)
471 return(r);
472 major = (fdu.dev >> MAJOR) & BYTE;
473 r=map_driver(major, NONE, 0);
474 return(r);
475 }
476 default:
477 return(EINVAL);
478 }
479 }
Cache object: b287d1a956c0c771a97d59a5612e27d7
|