📄 stadir.c
字号:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/fs/stadir.c
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25900 /* This file contains the code for performing four system calls relating to
25901 * status and directories.
25902 *
25903 * The entry points into this file are
25904 * do_chdir: perform the CHDIR system call
25905 * do_chroot: perform the CHROOT system call
25906 * do_stat: perform the STAT system call
25907 * do_fstat: perform the FSTAT system call
25908 */
25909
25910 #include "fs.h"
25911 #include <sys/stat.h>
25912 #include "file.h"
25913 #include "fproc.h"
25914 #include "inode.h"
25915 #include "param.h"
25916
25917 FORWARD _PROTOTYPE( int change, (struct inode **iip, char *name_ptr, int len));
25918 FORWARD _PROTOTYPE( int stat_inode, (struct inode *rip, struct filp *fil_ptr,
25919 char *user_addr) );
25920
25921 /*===========================================================================*
25922 * do_chdir *
25923 *===========================================================================*/
25924 PUBLIC int do_chdir()
25925 {
25926 /* Change directory. This function is also called by MM to simulate a chdir
25927 * in order to do EXEC, etc. It also changes the root directory, the uids and
25928 * gids, and the umask.
25929 */
25930
25931 int r;
25932 register struct fproc *rfp;
25933
25934 if (who == MM_PROC_NR) {
25935 rfp = &fproc[slot1];
25936 put_inode(fp->fp_rootdir);
25937 dup_inode(fp->fp_rootdir = rfp->fp_rootdir);
25938 put_inode(fp->fp_workdir);
25939 dup_inode(fp->fp_workdir = rfp->fp_workdir);
25940
25941 /* MM uses access() to check permissions. To make this work, pretend
25942 * that the user's real ids are the same as the user's effective ids.
25943 * FS calls other than access() do not use the real ids, so are not
25944 * affected.
25945 */
25946 fp->fp_realuid =
25947 fp->fp_effuid = rfp->fp_effuid;
25948 fp->fp_realgid =
25949 fp->fp_effgid = rfp->fp_effgid;
25950 fp->fp_umask = rfp->fp_umask;
25951 return(OK);
25952 }
25953
25954 /* Perform the chdir(name) system call. */
25955 r = change(&fp->fp_workdir, name, name_length);
25956 return(r);
25957 }
25960 /*===========================================================================*
25961 * do_chroot *
25962 *===========================================================================*/
25963 PUBLIC int do_chroot()
25964 {
25965 /* Perform the chroot(name) system call. */
25966
25967 register int r;
25968
25969 if (!super_user) return(EPERM); /* only su may chroot() */
25970 r = change(&fp->fp_rootdir, name, name_length);
25971 return(r);
25972 }
25975 /*===========================================================================*
25976 * change *
25977 *===========================================================================*/
25978 PRIVATE int change(iip, name_ptr, len)
25979 struct inode **iip; /* pointer to the inode pointer for the dir */
25980 char *name_ptr; /* pointer to the directory name to change to */
25981 int len; /* length of the directory name string */
25982 {
25983 /* Do the actual work for chdir() and chroot(). */
25984
25985 struct inode *rip;
25986 register int r;
25987
25988 /* Try to open the new directory. */
25989 if (fetch_name(name_ptr, len, M3) != OK) return(err_code);
25990 if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
25991
25992 /* It must be a directory and also be searchable. */
25993 if ( (rip->i_mode & I_TYPE) != I_DIRECTORY)
25994 r = ENOTDIR;
25995 else
25996 r = forbidden(rip, X_BIT); /* check if dir is searchable */
25997
25998 /* If error, return inode. */
25999 if (r != OK) {
26000 put_inode(rip);
26001 return(r);
26002 }
26003
26004 /* Everything is OK. Make the change. */
26005 put_inode(*iip); /* release the old directory */
26006 *iip = rip; /* acquire the new one */
26007 return(OK);
26008 }
26011 /*===========================================================================*
26012 * do_stat *
26013 *===========================================================================*/
26014 PUBLIC int do_stat()
26015 {
26016 /* Perform the stat(name, buf) system call. */
26017
26018 register struct inode *rip;
26019 register int r;
26020
26021 /* Both stat() and fstat() use the same routine to do the real work. That
26022 * routine expects an inode, so acquire it temporarily.
26023 */
26024 if (fetch_name(name1, name1_length, M1) != OK) return(err_code);
26025 if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
26026 r = stat_inode(rip, NIL_FILP, name2); /* actually do the work.*/
26027 put_inode(rip); /* release the inode */
26028 return(r);
26029 }
26032 /*===========================================================================*
26033 * do_fstat *
26034 *===========================================================================*/
26035 PUBLIC int do_fstat()
26036 {
26037 /* Perform the fstat(fd, buf) system call. */
26038
26039 register struct filp *rfilp;
26040
26041 /* Is the file descriptor valid? */
26042 if ( (rfilp = get_filp(fd)) == NIL_FILP) return(err_code);
26043
26044 return(stat_inode(rfilp->filp_ino, rfilp, buffer));
26045 }
26048 /*===========================================================================*
26049 * stat_inode *
26050 *===========================================================================*/
26051 PRIVATE int stat_inode(rip, fil_ptr, user_addr)
26052 register struct inode *rip; /* pointer to inode to stat */
26053 struct filp *fil_ptr; /* filp pointer, supplied by 'fstat' */
26054 char *user_addr; /* user space address where stat buf goes */
26055 {
26056 /* Common code for stat and fstat system calls. */
26057
26058 struct stat statbuf;
26059 mode_t mo;
26060 int r, s;
26061
26062 /* Update the atime, ctime, and mtime fields in the inode, if need be. */
26063 if (rip->i_update) update_times(rip);
26064
26065 /* Fill in the statbuf struct. */
26066 mo = rip->i_mode & I_TYPE;
26067 s = (mo == I_CHAR_SPECIAL || mo == I_BLOCK_SPECIAL); /* true iff special */
26068 statbuf.st_dev = rip->i_dev;
26069 statbuf.st_ino = rip->i_num;
26070 statbuf.st_mode = rip->i_mode;
26071 statbuf.st_nlink = rip->i_nlinks & BYTE;
26072 statbuf.st_uid = rip->i_uid;
26073 statbuf.st_gid = rip->i_gid & BYTE;
26074 statbuf.st_rdev = (dev_t) (s ? rip->i_zone[0] : NO_DEV);
26075 statbuf.st_size = rip->i_size;
26076
26077 if (rip->i_pipe == I_PIPE) {
26078 statbuf.st_mode &= ~I_REGULAR; /* wipe out I_REGULAR bit for pipes */
26079 if (fil_ptr != NIL_FILP && fil_ptr->filp_mode & R_BIT)
26080 statbuf.st_size -= fil_ptr->filp_pos;
26081 }
26082
26083 statbuf.st_atime = rip->i_atime;
26084 statbuf.st_mtime = rip->i_mtime;
26085 statbuf.st_ctime = rip->i_ctime;
26086
26087 /* Copy the struct to user space. */
26088 r = sys_copy(FS_PROC_NR, D, (phys_bytes) &statbuf,
26089 who, D, (phys_bytes) user_addr, (phys_bytes) sizeof(statbuf));
26090 return(r);
26091 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -