source: trunk/minix/servers/fs/stadir.c@ 9

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

Minix 3.1.2a

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