source: branches/minix3-book/servers/fs/stadir.c@ 4

Last change on this file since 4 was 4, checked in by Mattia Monga, 13 years ago

Importazione sorgenti libro

File size: 7.4 KB
Line 
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 * do_fstatfs: perform the FSTATFS system call
10 */
11
12#include "fs.h"
13#include <sys/stat.h>
14#include <sys/statfs.h>
15#include <minix/com.h>
16#include "file.h"
17#include "fproc.h"
18#include "inode.h"
19#include "param.h"
20#include "super.h"
21
22FORWARD _PROTOTYPE( int change, (struct inode **iip, char *name_ptr, int len));
23FORWARD _PROTOTYPE( int change_into, (struct inode **iip, struct inode *ip));
24FORWARD _PROTOTYPE( int stat_inode, (struct inode *rip, struct filp *fil_ptr,
25 char *user_addr) );
26
27/*===========================================================================*
28 * do_fchdir *
29 *===========================================================================*/
30PUBLIC int do_fchdir()
31{
32 /* Change directory on already-opened fd. */
33 struct filp *rfilp;
34
35 /* Is the file descriptor valid? */
36 if ( (rfilp = get_filp(m_in.fd)) == NIL_FILP) return(err_code);
37 return change_into(&fp->fp_workdir, rfilp->filp_ino);
38}
39
40/*===========================================================================*
41 * do_chdir *
42 *===========================================================================*/
43PUBLIC int do_chdir()
44{
45/* Change directory. This function is also called by MM to simulate a chdir
46 * in order to do EXEC, etc. It also changes the root directory, the uids and
47 * gids, and the umask.
48 */
49
50 int r;
51 register struct fproc *rfp;
52
53 if (who == PM_PROC_NR) {
54 rfp = &fproc[m_in.slot1];
55 put_inode(fp->fp_rootdir);
56 dup_inode(fp->fp_rootdir = rfp->fp_rootdir);
57 put_inode(fp->fp_workdir);
58 dup_inode(fp->fp_workdir = rfp->fp_workdir);
59
60 /* MM uses access() to check permissions. To make this work, pretend
61 * that the user's real ids are the same as the user's effective ids.
62 * FS calls other than access() do not use the real ids, so are not
63 * affected.
64 */
65 fp->fp_realuid =
66 fp->fp_effuid = rfp->fp_effuid;
67 fp->fp_realgid =
68 fp->fp_effgid = rfp->fp_effgid;
69 fp->fp_umask = rfp->fp_umask;
70 return(OK);
71 }
72
73 /* Perform the chdir(name) system call. */
74 r = change(&fp->fp_workdir, m_in.name, m_in.name_length);
75 return(r);
76}
77
78/*===========================================================================*
79 * do_chroot *
80 *===========================================================================*/
81PUBLIC int do_chroot()
82{
83/* Perform the chroot(name) system call. */
84
85 register int r;
86
87 if (!super_user) return(EPERM); /* only su may chroot() */
88 r = change(&fp->fp_rootdir, m_in.name, m_in.name_length);
89 return(r);
90}
91
92/*===========================================================================*
93 * change *
94 *===========================================================================*/
95PRIVATE int change(iip, name_ptr, len)
96struct inode **iip; /* pointer to the inode pointer for the dir */
97char *name_ptr; /* pointer to the directory name to change to */
98int len; /* length of the directory name string */
99{
100/* Do the actual work for chdir() and chroot(). */
101 struct inode *rip;
102
103 /* Try to open the new directory. */
104 if (fetch_name(name_ptr, len, M3) != OK) return(err_code);
105 if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
106 return change_into(iip, rip);
107}
108
109/*===========================================================================*
110 * change_into *
111 *===========================================================================*/
112PRIVATE int change_into(iip, rip)
113struct inode **iip; /* pointer to the inode pointer for the dir */
114struct inode *rip; /* this is what the inode has to become */
115{
116 register int r;
117
118 /* It must be a directory and also be searchable. */
119 if ( (rip->i_mode & I_TYPE) != I_DIRECTORY)
120 r = ENOTDIR;
121 else
122 r = forbidden(rip, X_BIT); /* check if dir is searchable */
123
124 /* If error, return inode. */
125 if (r != OK) {
126 put_inode(rip);
127 return(r);
128 }
129
130 /* Everything is OK. Make the change. */
131 put_inode(*iip); /* release the old directory */
132 *iip = rip; /* acquire the new one */
133 return(OK);
134}
135
136/*===========================================================================*
137 * do_stat *
138 *===========================================================================*/
139PUBLIC int do_stat()
140{
141/* Perform the stat(name, buf) system call. */
142
143 register struct inode *rip;
144 register int r;
145
146 /* Both stat() and fstat() use the same routine to do the real work. That
147 * routine expects an inode, so acquire it temporarily.
148 */
149 if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code);
150 if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
151 r = stat_inode(rip, NIL_FILP, m_in.name2); /* actually do the work.*/
152 put_inode(rip); /* release the inode */
153 return(r);
154}
155
156/*===========================================================================*
157 * do_fstat *
158 *===========================================================================*/
159PUBLIC int do_fstat()
160{
161/* Perform the fstat(fd, buf) system call. */
162
163 register struct filp *rfilp;
164
165 /* Is the file descriptor valid? */
166 if ( (rfilp = get_filp(m_in.fd)) == NIL_FILP) return(err_code);
167
168 return(stat_inode(rfilp->filp_ino, rfilp, m_in.buffer));
169}
170
171/*===========================================================================*
172 * stat_inode *
173 *===========================================================================*/
174PRIVATE int stat_inode(rip, fil_ptr, user_addr)
175register struct inode *rip; /* pointer to inode to stat */
176struct filp *fil_ptr; /* filp pointer, supplied by 'fstat' */
177char *user_addr; /* user space address where stat buf goes */
178{
179/* Common code for stat and fstat system calls. */
180
181 struct stat statbuf;
182 mode_t mo;
183 int r, s;
184
185 /* Update the atime, ctime, and mtime fields in the inode, if need be. */
186 if (rip->i_update) update_times(rip);
187
188 /* Fill in the statbuf struct. */
189 mo = rip->i_mode & I_TYPE;
190
191 /* true iff special */
192 s = (mo == I_CHAR_SPECIAL || mo == I_BLOCK_SPECIAL);
193
194 statbuf.st_dev = rip->i_dev;
195 statbuf.st_ino = rip->i_num;
196 statbuf.st_mode = rip->i_mode;
197 statbuf.st_nlink = rip->i_nlinks;
198 statbuf.st_uid = rip->i_uid;
199 statbuf.st_gid = rip->i_gid;
200 statbuf.st_rdev = (dev_t) (s ? rip->i_zone[0] : NO_DEV);
201 statbuf.st_size = rip->i_size;
202
203 if (rip->i_pipe == I_PIPE) {
204 statbuf.st_mode &= ~I_REGULAR; /* wipe out I_REGULAR bit for pipes */
205 if (fil_ptr != NIL_FILP && fil_ptr->filp_mode & R_BIT)
206 statbuf.st_size -= fil_ptr->filp_pos;
207 }
208
209 statbuf.st_atime = rip->i_atime;
210 statbuf.st_mtime = rip->i_mtime;
211 statbuf.st_ctime = rip->i_ctime;
212
213 /* Copy the struct to user space. */
214 r = sys_datacopy(FS_PROC_NR, (vir_bytes) &statbuf,
215 who, (vir_bytes) user_addr, (phys_bytes) sizeof(statbuf));
216 return(r);
217}
218
219/*===========================================================================*
220 * do_fstatfs *
221 *===========================================================================*/
222PUBLIC int do_fstatfs()
223{
224 /* Perform the fstatfs(fd, buf) system call. */
225 struct statfs st;
226 register struct filp *rfilp;
227 int r;
228
229 /* Is the file descriptor valid? */
230 if ( (rfilp = get_filp(m_in.fd)) == NIL_FILP) return(err_code);
231
232 st.f_bsize = rfilp->filp_ino->i_sp->s_block_size;
233
234 r = sys_datacopy(FS_PROC_NR, (vir_bytes) &st,
235 who, (vir_bytes) m_in.buffer, (phys_bytes) sizeof(st));
236
237 return(r);
238}
239
Note: See TracBrowser for help on using the repository browser.