Please wait until the page is fully downloaded and then press the "Expand" button or the blue line numbers.
0112000 /* This file contains a collection of miscellaneous procedures. Some of them
0112001 * perform simple system calls. Some others do a little part of system calls
0112002 * that are mostly performed by the Memory Manager.
0112003 *
0112004 * The entry points into this file are
0112005 * do_dup: perform the DUP system call
0112006 * do_fcntl: perform the FCNTL system call
0112007 * do_sync: perform the SYNC system call
0112008 * do_fork: adjust the tables after MM has performed a FORK system call
0112009 * do_exec: handle files with FD_CLOEXEC on after MM has done an EXEC
0112010 * do_exit: a process has exited; note that in the tables
0112011 * do_set: set uid or gid for some process
0112012 * do_revive: revive a process that was waiting for something (e.g. TTY)
0112013 * do_svrctl: file system control
0112014 */
0112015
0112016 #include "fs.h"
0112017 #include <fcntl.h>
0112018 #include <unistd.h> /* cc runs out of memory with unistd.h :-( */
0112019 #include <minix/callnr.h>
0112020 #include <minix/com.h>
0112021 #include <sys/svrctl.h>
0112022 #include "buf.h"
0112023 #include "file.h"
0112024 #include "fproc.h"
0112025 #include "inode.h"
0112026 #include "dev.h"
0112027 #include "param.h"
0112028
0112029
0112030 /*===========================================================================*
0112031 * do_dup *
0112032 *===========================================================================*/
0112033 PUBLIC int do_dup()
0112034 {
0112035 /* Perform the dup(fd) or dup2(fd,fd2) system call. These system calls are
0112036 * obsolete. In fact, it is not even possible to invoke them using the
0112037 * current library because the library routines call fcntl(). They are
0112038 * provided to permit old binary programs to continue to run.
0112039 */
0112040
0112041 register int rfd;
0112042 register struct filp *f;
0112043 struct filp *dummy;
0112044 int r;
0112045
0112046 /* Is the file descriptor valid? */
0112047 rfd = fd & ~DUP_MASK; /* kill off dup2 bit, if on */
0112048 if ((f = get_filp(rfd)) == NIL_FILP) return(err_code);
0112049
0112050 /* Distinguish between dup and dup2. */
0112051 if (fd == rfd) { /* bit not on */
0112052 /* dup(fd) */
0112053 if ( (r = get_fd(0, 0, &fd2, &dummy)) != OK) return(r);
0112054 } else {
0112055 /* dup2(fd, fd2) */
0112056 if (fd2 < 0 || fd2 >= OPEN_MAX) return(EBADF);
0112057 if (rfd == fd2) return(fd2); /* ignore the call: dup2(x, x) */
0112058 fd = fd2; /* prepare to close fd2 */
0112059 (void) do_close(); /* cannot fail */
0112060 }
0112061
0112062 /* Success. Set up new file descriptors. */
0112063 f->filp_count++;
0112064 fp->fp_filp[fd2] = f;
0112065 return(fd2);
0112066 }
0112067
0112068 /*===========================================================================*
0112069 * do_fcntl *
0112070 *===========================================================================*/
0112071 PUBLIC int do_fcntl()
0112072 {
0112073 /* Perform the fcntl(fd, request, ...) system call. */
0112074
0112075 register struct filp *f;
0112076 int new_fd, r, fl;
0112077 long cloexec_mask; /* bit map for the FD_CLOEXEC flag */
0112078 long clo_value; /* FD_CLOEXEC flag in proper position */
0112079 struct filp *dummy;
0112080
0112081 /* Is the file descriptor valid? */
0112082 if ((f = get_filp(fd)) == NIL_FILP) return(err_code);
0112083
0112084 switch (request) {
0112085 case F_DUPFD:
0112086 /* This replaces the old dup() system call. */
0112087 if (addr < 0 || addr >= OPEN_MAX) return(EINVAL);
0112088 if ((r = get_fd(addr, 0, &new_fd, &dummy)) != OK) return(r);
0112089 f->filp_count++;
0112090 fp->fp_filp[new_fd] = f;
0112091 return(new_fd);
0112092
0112093 case F_GETFD:
0112094 /* Get close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
0112095 return( ((fp->fp_cloexec >> fd) & 01) ? FD_CLOEXEC : 0);
0112096
0112097 case F_SETFD:
0112098 /* Set close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
0112099 cloexec_mask = 1L << fd; /* singleton set position ok */
0112100 clo_value = (addr & FD_CLOEXEC ? cloexec_mask : 0L);
0112101 fp->fp_cloexec = (fp->fp_cloexec & ~cloexec_mask) | clo_value;
0112102 return(OK);
0112103
0112104 case F_GETFL:
0112105 /* Get file status flags (O_NONBLOCK and O_APPEND). */
0112106 fl = f->filp_flags & (O_NONBLOCK | O_APPEND | O_ACCMODE);
0112107 return(fl);
0112108
0112109 case F_SETFL:
0112110 /* Set file status flags (O_NONBLOCK and O_APPEND). */
0112111 fl = O_NONBLOCK | O_APPEND;
0112112 f->filp_flags = (f->filp_flags & ~fl) | (addr & fl);
0112113 return(OK);
0112114
0112115 case F_GETLK:
0112116 case F_SETLK:
0112117 case F_SETLKW:
0112118 /* Set or clear a file lock. */
0112119 r = lock_op(f, request);
0112120 return(r);
0112121
0112122 default:
0112123 return(EINVAL);
0112124 }
0112125 }
0112126
0112127
0112128 /*===========================================================================*
0112129 * do_sync *
0112130 *===========================================================================*/
0112131 PUBLIC int do_sync()
0112132 {
0112133 /* Perform the sync() system call. Flush all the tables. */
0112134
0112135 register struct inode *rip;
0112136 register struct buf *bp;
0112137
0112138 /* The order in which the various tables are flushed is critical. The
0112139 * blocks must be flushed last, since rw_inode() leaves its results in
0112140 * the block cache.
0112141 */
0112142
0112143 /* Write all the dirty inodes to the disk. */
0112144 for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
0112145 if (rip->i_count > 0 && rip->i_dirt == DIRTY) rw_inode(rip, WRITING);
0112146
0112147 /* Write all the dirty blocks to the disk, one drive at a time. */
0112148 for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++)
0112149 if (bp->b_dev != NO_DEV && bp->b_dirt == DIRTY) flushall(bp->b_dev);
0112150
0112151 return(OK); /* sync() can't fail */
0112152 }
0112153
0112154
0112155 /*===========================================================================*
0112156 * do_fork *
0112157 *===========================================================================*/
0112158 PUBLIC int do_fork()
0112159 {
0112160 /* Perform those aspects of the fork() system call that relate to files.
0112161 * In particular, let the child inherit its parent's file descriptors.
0112162 * The parent and child parameters tell who forked off whom. The file
0112163 * system uses the same slot numbers as the kernel. Only MM makes this call.
0112164 */
0112165
0112166 register struct fproc *cp;
0112167 int i;
0112168
0112169 /* Only MM may make this call directly. */
0112170 if (who != MM_PROC_NR) return(EGENERIC);
0112171
0112172 /* Copy the parent's fproc struct to the child. */
0112173 fproc[child] = fproc[parent];
0112174
0112175 /* Increase the counters in the 'filp' table. */
0112176 cp = &fproc[child];
0112177 for (i = 0; i < OPEN_MAX; i++)
0112178 if (cp->fp_filp[i] != NIL_FILP) cp->fp_filp[i]->filp_count++;
0112179
0112180 /* Fill in new process id. */
0112181 cp->fp_pid = pid;
0112182
0112183 /* A child is not a process leader. */
0112184 cp->fp_sesldr = 0;
0112185
0112186 /* Record the fact that both root and working dir have another user. */
0112187 dup_inode(cp->fp_rootdir);
0112188 dup_inode(cp->fp_workdir);
0112189 return(OK);
0112190 }
0112191
0112192
0112193 /*===========================================================================*
0112194 * do_exec *
0112195 *===========================================================================*/
0112196 PUBLIC int do_exec()
0112197 {
0112198 /* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec). When
0112199 * MM does an EXEC, it calls FS to allow FS to find these files and close them.
0112200 */
0112201
0112202 register int i;
0112203 long bitmap;
0112204
0112205 /* Only MM may make this call directly. */
0112206 if (who != MM_PROC_NR) return(EGENERIC);
0112207
0112208 /* The array of FD_CLOEXEC bits is in the fp_cloexec bit map. */
0112209 fp = &fproc[slot1]; /* get_filp() needs 'fp' */
0112210 bitmap = fp->fp_cloexec;
0112211 if (bitmap == 0) return(OK); /* normal case, no FD_CLOEXECs */
0112212
0112213 /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
0112214 for (i = 0; i < OPEN_MAX; i++) {
0112215 fd = i;
0112216 if ( (bitmap >> i) & 01) (void) do_close();
0112217 }
0112218
0112219 return(OK);
0112220 }
0112221
0112222
0112223 /*===========================================================================*
0112224 * do_exit *
0112225 *===========================================================================*/
0112226 PUBLIC int do_exit()
0112227 {
0112228 /* Perform the file system portion of the exit(status) system call. */
0112229
0112230 register int i, exitee, task;
0112231 register struct fproc *rfp;
0112232 register struct filp *rfilp;
0112233 register struct inode *rip;
0112234 dev_t dev;
0112235
0112236 /* Only MM may do the EXIT call directly. */
0112237 if (who != MM_PROC_NR) return(EGENERIC);
0112238
0112239 /* Nevertheless, pretend that the call came from the user. */
0112240 fp = &fproc[slot1]; /* get_filp() needs 'fp' */
0112241 exitee = slot1;
0112242
0112243 if (fp->fp_suspended == SUSPENDED) {
0112244 task = -fp->fp_task;
0112245 if (task == XPIPE || task == XPOPEN) susp_count--;
0112246 pro = exitee;
0112247 (void) do_unpause(); /* this always succeeds for MM */
0112248 fp->fp_suspended = NOT_SUSPENDED;
0112249 }
0112250
0112251 /* Loop on file descriptors, closing any that are open. */
0112252 for (i = 0; i < OPEN_MAX; i++) {
0112253 fd = i;
0112254 (void) do_close();
0112255 }
0112256
0112257 /* Release root and working directories. */
0112258 put_inode(fp->fp_rootdir);
0112259 put_inode(fp->fp_workdir);
0112260 fp->fp_rootdir = NIL_INODE;
0112261 fp->fp_workdir = NIL_INODE;
0112262
0112263 /* If a session leader exits then revoke access to its controlling tty from
0112264 * all other processes using it.
0112265 */
0112266 if (!fp->fp_sesldr) return(OK); /* not a session leader */
0112267 fp->fp_sesldr = FALSE;
0112268 if (fp->fp_tty == 0) return(OK); /* no controlling tty */
0112269 dev = fp->fp_tty;
0112270
0112271 for (rfp = &fproc[LOW_USER]; rfp < &fproc[NR_PROCS]; rfp++) {
0112272 if (rfp->fp_tty == dev) rfp->fp_tty = 0;
0112273
0112274 for (i = 0; i < OPEN_MAX; i++) {
0112275 if ((rfilp = rfp->fp_filp[i]) == NIL_FILP) continue;
0112276 if (rfilp->filp_mode == FILP_CLOSED) continue;
0112277 rip = rfilp->filp_ino;
0112278 if ((rip->i_mode & I_TYPE) != I_CHAR_SPECIAL) continue;
0112279 if ((dev_t) rip->i_zone[0] != dev) continue;
0112280 dev_close(dev);
0112281 rfilp->filp_mode = FILP_CLOSED;
0112282 }
0112283 }
0112284
0112285 /* Truly exiting, or becoming a server? */
0112286 fp->fp_pid = PID_FREE;
0112287 return(OK);
0112288 }
0112289
0112290
0112291 /*===========================================================================*
0112292 * do_set *
0112293 *===========================================================================*/
0112294 PUBLIC int do_set()
0112295 {
0112296 /* Set uid_t or gid_t field. */
0112297
0112298 register struct fproc *tfp;
0112299
0112300 /* Only MM may make this call directly. */
0112301 if (who != MM_PROC_NR) return(EGENERIC);
0112302
0112303 tfp = &fproc[slot1];
0112304 if (fs_call == SETUID) {
0112305 tfp->fp_realuid = (uid_t) real_user_id;
0112306 tfp->fp_effuid = (uid_t) eff_user_id;
0112307 }
0112308 if (fs_call == SETGID) {
0112309 tfp->fp_effgid = (gid_t) eff_grp_id;
0112310 tfp->fp_realgid = (gid_t) real_grp_id;
0112311 }
0112312 return(OK);
0112313 }
0112314
0112315
0112316 /*===========================================================================*
0112317 * do_revive *
0112318 *===========================================================================*/
0112319 PUBLIC int do_revive()
0112320 {
0112321 /* A task, typically TTY, has now gotten the characters that were needed for a
0112322 * previous read. The process did not get a reply when it made the call.
0112323 * Instead it was suspended. Now we can send the reply to wake it up. This
0112324 * business has to be done carefully, since the incoming message is from
0112325 * a task (to which no reply can be sent), and the reply must go to a process
0112326 * that blocked earlier. The reply to the caller is inhibited by setting the
0112327 * 'dont_reply' flag, and the reply to the blocked process is done explicitly
0112328 * in revive().
0112329 */
0112330
0112331 if (who >= LOW_USER && fp->fp_pid != PID_SERVER) return(EPERM);
0112332
0112333 revive(m.REP_PROC_NR, m.REP_STATUS);
0112334 dont_reply = TRUE; /* don't reply to the TTY task */
0112335 return(OK);
0112336 }
0112337
0112338 /*===========================================================================*
0112339 * do_svrctl *
0112340 *===========================================================================*/
0112341 PUBLIC int do_svrctl()
0112342 {
0112343 switch (svrctl_req) {
0112344 case FSSIGNON: {
0112345 /* A server in user space calls in to manage a device. */
0112346 struct fssignon device;
0112347 int r, major;
0112348 struct dmap *dp;
0112349
0112350 if (fp->fp_effuid != SU_UID) return(EPERM);
0112351
0112352 r = sys_copy(who, D, (phys_bytes) svrctl_argp,
0112353 FS_PROC_NR, D, (phys_bytes) &device,
0112354 (phys_bytes) sizeof(device));
0112355 if (r != OK) return(r);
0112356
0112357 major= (device.dev >> MAJOR) & BYTE;
0112358 if (major >= max_major) return(ENODEV);
0112359 dp = &dmap[major];
0112360 if (dp->dmap_task != ANY) return(EBUSY);
0112361
0112362 switch (device.style) {
0112363 case STYLE_DEV: dp->dmap_opcl = gen_opcl; break;
0112364 case STYLE_TTY: dp->dmap_opcl = tty_opcl; break;
0112365 case STYLE_CLONE: dp->dmap_opcl = clone_opcl; break;
0112366 default: return(EINVAL);
0112367 }
0112368 dp->dmap_io = gen_io;
0112369 dp->dmap_task = who;
0112370 fp->fp_pid = PID_SERVER;
0112371 return(OK); }
0112372 default:
0112373 return(EINVAL);
0112374 }
0112375 }