~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Minix Cross Reference
Minix/fs/stadir.c


  1 /* This file contains the code for performing four system calls relating to
  2  * status and directories.
  3  *
  4  * The entry points into this file are
  5  *   do_chdir:  perform the CHDIR system call
  6  *   do_chroot: perform the CHROOT system call
  7  *   do_stat:   perform the STAT system call
  8  *   do_fstat:  perform the FSTAT system call
  9  */
 10 
 11 #include "fs.h"
 12 #include <sys/stat.h>
 13 #include "file.h"
 14 #include "fproc.h"
 15 #include "inode.h"
 16 #include "param.h"
 17 
 18 FORWARD _PROTOTYPE( int change, (struct inode **iip, char *name_ptr, int len));
 19 FORWARD _PROTOTYPE( int stat_inode, (struct inode *rip, struct filp *fil_ptr,
 20                         char *user_addr)                                );
 21 
 22 /*===========================================================================*
 23  *                              do_chdir                                     *
 24  *===========================================================================*/
 25 PUBLIC int do_chdir()
 26 {
 27 /* Change directory.  This function is  also called by MM to simulate a chdir
 28  * in order to do EXEC, etc.  It also changes the root directory, the uids and
 29  * gids, and the umask. 
 30  */
 31 
 32   int r;
 33   register struct fproc *rfp;
 34 
 35   if (who == MM_PROC_NR) {
 36         rfp = &fproc[slot1];
 37         put_inode(fp->fp_rootdir);
 38         dup_inode(fp->fp_rootdir = rfp->fp_rootdir);
 39         put_inode(fp->fp_workdir);
 40         dup_inode(fp->fp_workdir = rfp->fp_workdir);
 41 
 42         /* MM uses access() to check permissions.  To make this work, pretend
 43          * that the user's real ids are the same as the user's effective ids.
 44          * FS calls other than access() do not use the real ids, so are not
 45          * affected.
 46          */
 47         fp->fp_realuid =
 48         fp->fp_effuid = rfp->fp_effuid;
 49         fp->fp_realgid =
 50         fp->fp_effgid = rfp->fp_effgid;
 51         fp->fp_umask = rfp->fp_umask;
 52         return(OK);
 53   }
 54 
 55   /* Perform the chdir(name) system call. */
 56   r = change(&fp->fp_workdir, name, name_length);
 57   return(r);
 58 }
 59 
 60 
 61 /*===========================================================================*
 62  *                              do_chroot                                    *
 63  *===========================================================================*/
 64 PUBLIC int do_chroot()
 65 {
 66 /* Perform the chroot(name) system call. */
 67 
 68   register int r;
 69 
 70   if (!super_user) return(EPERM);       /* only su may chroot() */
 71   r = change(&fp->fp_rootdir, name, name_length);
 72   return(r);
 73 }
 74 
 75 
 76 /*===========================================================================*
 77  *                              change                                       *
 78  *===========================================================================*/
 79 PRIVATE int change(iip, name_ptr, len)
 80 struct inode **iip;             /* pointer to the inode pointer for the dir */
 81 char *name_ptr;                 /* pointer to the directory name to change to */
 82 int len;                        /* length of the directory name string */
 83 {
 84 /* Do the actual work for chdir() and chroot(). */
 85 
 86   struct inode *rip;
 87   register int r;
 88 
 89   /* Try to open the new directory. */
 90   if (fetch_name(name_ptr, len, M3) != OK) return(err_code);
 91   if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
 92 
 93   /* It must be a directory and also be searchable. */
 94   if ( (rip->i_mode & I_TYPE) != I_DIRECTORY)
 95         r = ENOTDIR;
 96   else
 97         r = forbidden(rip, X_BIT);      /* check if dir is searchable */
 98 
 99   /* If error, return inode. */
100   if (r != OK) {
101         put_inode(rip);
102         return(r);
103   }
104 
105   /* Everything is OK.  Make the change. */
106   put_inode(*iip);              /* release the old directory */
107   *iip = rip;                   /* acquire the new one */
108   return(OK);
109 }
110 
111 
112 /*===========================================================================*
113  *                              do_stat                                      *
114  *===========================================================================*/
115 PUBLIC int do_stat()
116 {
117 /* Perform the stat(name, buf) system call. */
118 
119   register struct inode *rip;
120   register int r;
121 
122   /* Both stat() and fstat() use the same routine to do the real work.  That
123    * routine expects an inode, so acquire it temporarily.
124    */
125   if (fetch_name(name1, name1_length, M1) != OK) return(err_code);
126   if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
127   r = stat_inode(rip, NIL_FILP, name2); /* actually do the work.*/
128   put_inode(rip);               /* release the inode */
129   return(r);
130 }
131 
132 
133 /*===========================================================================*
134  *                              do_fstat                                     *
135  *===========================================================================*/
136 PUBLIC int do_fstat()
137 {
138 /* Perform the fstat(fd, buf) system call. */
139 
140   register struct filp *rfilp;
141 
142   /* Is the file descriptor valid? */
143   if ( (rfilp = get_filp(fd)) == NIL_FILP) return(err_code);
144 
145   return(stat_inode(rfilp->filp_ino, rfilp, buffer));
146 }
147 
148 
149 /*===========================================================================*
150  *                              stat_inode                                   *
151  *===========================================================================*/
152 PRIVATE int stat_inode(rip, fil_ptr, user_addr)
153 register struct inode *rip;     /* pointer to inode to stat */
154 struct filp *fil_ptr;           /* filp pointer, supplied by 'fstat' */
155 char *user_addr;                /* user space address where stat buf goes */
156 {
157 /* Common code for stat and fstat system calls. */
158 
159   struct stat statbuf;
160   mode_t mo;
161   int r, s;
162 
163   /* Update the atime, ctime, and mtime fields in the inode, if need be. */
164   if (rip->i_update) update_times(rip);
165 
166   /* Fill in the statbuf struct. */
167   mo = rip->i_mode & I_TYPE;
168   s = (mo == I_CHAR_SPECIAL || mo == I_BLOCK_SPECIAL);  /* true iff special */
169   statbuf.st_dev = rip->i_dev;
170   statbuf.st_ino = rip->i_num;
171   statbuf.st_mode = rip->i_mode;
172   statbuf.st_nlink = rip->i_nlinks & BYTE;
173   statbuf.st_uid = rip->i_uid;
174   statbuf.st_gid = rip->i_gid & BYTE;
175   statbuf.st_rdev = (dev_t) (s ? rip->i_zone[0] : NO_DEV);
176   statbuf.st_size = rip->i_size;
177 
178   if (rip->i_pipe == I_PIPE) {
179         statbuf.st_mode &= ~I_REGULAR;  /* wipe out I_REGULAR bit for pipes */
180         if (fil_ptr != NIL_FILP && fil_ptr->filp_mode & R_BIT) 
181                 statbuf.st_size -= fil_ptr->filp_pos;
182   }
183 
184   statbuf.st_atime = rip->i_atime;
185   statbuf.st_mtime = rip->i_mtime;
186   statbuf.st_ctime = rip->i_ctime;
187 
188   /* Copy the struct to user space. */
189   r = sys_copy(FS_PROC_NR, D, (phys_bytes) &statbuf,
190                 who, D, (phys_bytes) user_addr, (phys_bytes) sizeof(statbuf));
191   return(r);
192 }
193 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.