This source file includes following definitions.
- sys_ustat
- sys_statfs
- sys_fstatfs
- sys_truncate
- sys_ftruncate
- sys_utime
- sys_access
- sys_chdir
- sys_fchdir
- sys_chroot
- sys_fchmod
- sys_chmod
- sys_fchown
- sys_chown
- do_open
- sys_open
- sys_creat
- close_fp
- sys_close
- sys_vhangup
1
2
3
4
5
6
7 #include <linux/vfs.h>
8 #include <linux/types.h>
9 #include <linux/utime.h>
10 #include <linux/errno.h>
11 #include <linux/fcntl.h>
12 #include <linux/stat.h>
13 #include <linux/string.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/tty.h>
18 #include <linux/time.h>
19
20 #include <asm/segment.h>
21
22 extern void fcntl_remove_locks(struct task_struct *, struct file *, unsigned int fd);
23
24 asmlinkage int sys_ustat(int dev, struct ustat * ubuf)
25 {
26 return -ENOSYS;
27 }
28
29 asmlinkage int sys_statfs(const char * path, struct statfs * buf)
30 {
31 struct inode * inode;
32 int error;
33
34 error = verify_area(VERIFY_WRITE, buf, sizeof(struct statfs));
35 if (error)
36 return error;
37 error = namei(path,&inode);
38 if (error)
39 return error;
40 if (!inode->i_sb->s_op->statfs) {
41 iput(inode);
42 return -ENOSYS;
43 }
44 inode->i_sb->s_op->statfs(inode->i_sb, buf);
45 iput(inode);
46 return 0;
47 }
48
49 asmlinkage int sys_fstatfs(unsigned int fd, struct statfs * buf)
50 {
51 struct inode * inode;
52 struct file * file;
53 int error;
54
55 error = verify_area(VERIFY_WRITE, buf, sizeof(struct statfs));
56 if (error)
57 return error;
58 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
59 return -EBADF;
60 if (!(inode = file->f_inode))
61 return -ENOENT;
62 if (!inode->i_sb->s_op->statfs)
63 return -ENOSYS;
64 inode->i_sb->s_op->statfs(inode->i_sb, buf);
65 return 0;
66 }
67
68 asmlinkage int sys_truncate(const char * path, unsigned int length)
69 {
70 struct inode * inode;
71 int error;
72 struct iattr newattrs;
73
74 error = namei(path,&inode);
75 if (error)
76 return error;
77 if (S_ISDIR(inode->i_mode) || !permission(inode,MAY_WRITE)) {
78 iput(inode);
79 return -EACCES;
80 }
81 if (IS_RDONLY(inode)) {
82 iput(inode);
83 return -EROFS;
84 }
85 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
86 iput(inode);
87 return -EPERM;
88 }
89 error = get_write_access(inode);
90 if (error) {
91 iput(inode);
92 return error;
93 }
94 inode->i_size = newattrs.ia_size = length;
95 if (inode->i_op && inode->i_op->truncate)
96 inode->i_op->truncate(inode);
97 newattrs.ia_ctime = newattrs.ia_mtime = CURRENT_TIME;
98 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME;
99 inode->i_dirt = 1;
100 error = notify_change(inode, &newattrs);
101 put_write_access(inode);
102 iput(inode);
103 return error;
104 }
105
106 asmlinkage int sys_ftruncate(unsigned int fd, unsigned int length)
107 {
108 struct inode * inode;
109 struct file * file;
110 struct iattr newattrs;
111
112 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
113 return -EBADF;
114 if (!(inode = file->f_inode))
115 return -ENOENT;
116 if (S_ISDIR(inode->i_mode) || !(file->f_mode & 2))
117 return -EACCES;
118 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
119 return -EPERM;
120 inode->i_size = newattrs.ia_size = length;
121 if (inode->i_op && inode->i_op->truncate)
122 inode->i_op->truncate(inode);
123 newattrs.ia_ctime = newattrs.ia_mtime = CURRENT_TIME;
124 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME;
125 inode->i_dirt = 1;
126 return notify_change(inode, &newattrs);
127 }
128
129
130
131
132
133 asmlinkage int sys_utime(char * filename, struct utimbuf * times)
134 {
135 struct inode * inode;
136 long actime,modtime;
137 int error;
138 struct iattr newattrs;
139
140 error = namei(filename,&inode);
141 if (error)
142 return error;
143 if (IS_RDONLY(inode)) {
144 iput(inode);
145 return -EROFS;
146 }
147 if (times) {
148 if ((current->fsuid != inode->i_uid) && !fsuser()) {
149 iput(inode);
150 return -EPERM;
151 }
152 actime = get_fs_long((unsigned long *) ×->actime);
153 modtime = get_fs_long((unsigned long *) ×->modtime);
154 newattrs.ia_ctime = CURRENT_TIME;
155 } else {
156 if ((current->fsuid != inode->i_uid) &&
157 !permission(inode,MAY_WRITE)) {
158 iput(inode);
159 return -EACCES;
160 }
161 actime = modtime = newattrs.ia_ctime = CURRENT_TIME;
162 }
163 newattrs.ia_atime = actime;
164 newattrs.ia_mtime = modtime;
165 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
166 inode->i_dirt = 1;
167 error = notify_change(inode, &newattrs);
168 iput(inode);
169 return error;
170 }
171
172
173
174
175
176 asmlinkage int sys_access(const char * filename, int mode)
177 {
178 struct inode * inode;
179 int old_fsuid, old_fsgid;
180 int res;
181
182 if (mode != (mode & S_IRWXO))
183 return -EINVAL;
184 old_fsuid = current->fsuid;
185 old_fsgid = current->fsgid;
186 current->fsuid = current->uid;
187 current->fsgid = current->gid;
188 res = namei(filename,&inode);
189 if (!res) {
190 if (!permission(inode, mode))
191 res = -EACCES;
192 iput(inode);
193 }
194 current->fsuid = old_fsuid;
195 current->fsgid = old_fsgid;
196 return res;
197 }
198
199 asmlinkage int sys_chdir(const char * filename)
200 {
201 struct inode * inode;
202 int error;
203
204 error = namei(filename,&inode);
205 if (error)
206 return error;
207 if (!S_ISDIR(inode->i_mode)) {
208 iput(inode);
209 return -ENOTDIR;
210 }
211 if (!permission(inode,MAY_EXEC)) {
212 iput(inode);
213 return -EACCES;
214 }
215 iput(current->fs->pwd);
216 current->fs->pwd = inode;
217 return (0);
218 }
219
220 asmlinkage int sys_fchdir(unsigned int fd)
221 {
222 struct inode * inode;
223 struct file * file;
224
225 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
226 return -EBADF;
227 if (!(inode = file->f_inode))
228 return -ENOENT;
229 if (!S_ISDIR(inode->i_mode))
230 return -ENOTDIR;
231 if (!permission(inode,MAY_EXEC))
232 return -EACCES;
233 iput(current->fs->pwd);
234 current->fs->pwd = inode;
235 inode->i_count++;
236 return (0);
237 }
238
239 asmlinkage int sys_chroot(const char * filename)
240 {
241 struct inode * inode;
242 int error;
243
244 error = namei(filename,&inode);
245 if (error)
246 return error;
247 if (!S_ISDIR(inode->i_mode)) {
248 iput(inode);
249 return -ENOTDIR;
250 }
251 if (!fsuser()) {
252 iput(inode);
253 return -EPERM;
254 }
255 iput(current->fs->root);
256 current->fs->root = inode;
257 return (0);
258 }
259
260 asmlinkage int sys_fchmod(unsigned int fd, mode_t mode)
261 {
262 struct inode * inode;
263 struct file * file;
264 struct iattr newattrs;
265
266 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
267 return -EBADF;
268 if (!(inode = file->f_inode))
269 return -ENOENT;
270 if (IS_RDONLY(inode))
271 return -EROFS;
272 if (mode == (mode_t) -1)
273 mode = inode->i_mode;
274 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
275 newattrs.ia_ctime = CURRENT_TIME;
276 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
277 inode->i_dirt = 1;
278 return notify_change(inode, &newattrs);
279 }
280
281 asmlinkage int sys_chmod(const char * filename, mode_t mode)
282 {
283 struct inode * inode;
284 int error;
285 struct iattr newattrs;
286
287 error = namei(filename,&inode);
288 if (error)
289 return error;
290 if (IS_RDONLY(inode)) {
291 iput(inode);
292 return -EROFS;
293 }
294 if (mode == (mode_t) -1)
295 mode = inode->i_mode;
296 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
297 newattrs.ia_ctime = CURRENT_TIME;
298 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
299 inode->i_dirt = 1;
300 error = notify_change(inode, &newattrs);
301 iput(inode);
302 return error;
303 }
304
305 asmlinkage int sys_fchown(unsigned int fd, uid_t user, gid_t group)
306 {
307 struct inode * inode;
308 struct file * file;
309 struct iattr newattrs;
310
311 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
312 return -EBADF;
313 if (!(inode = file->f_inode))
314 return -ENOENT;
315 if (IS_RDONLY(inode))
316 return -EROFS;
317 if (user == (uid_t) -1)
318 user = inode->i_uid;
319 if (group == (gid_t) -1)
320 group = inode->i_gid;
321 newattrs.ia_mode = inode->i_mode;
322 newattrs.ia_uid = user;
323 newattrs.ia_gid = group;
324 newattrs.ia_ctime = CURRENT_TIME;
325 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
326
327
328
329 if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
330 newattrs.ia_mode &= ~S_ISUID;
331 newattrs.ia_valid |= ATTR_MODE;
332 }
333
334
335
336 if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
337 newattrs.ia_mode &= ~S_ISGID;
338 newattrs.ia_valid |= ATTR_MODE;
339 }
340 inode->i_dirt = 1;
341 return notify_change(inode, &newattrs);
342 }
343
344 asmlinkage int sys_chown(const char * filename, uid_t user, gid_t group)
345 {
346 struct inode * inode;
347 int error;
348 struct iattr newattrs;
349
350 error = lnamei(filename,&inode);
351 if (error)
352 return error;
353 if (IS_RDONLY(inode)) {
354 iput(inode);
355 return -EROFS;
356 }
357 if (user == (uid_t) -1)
358 user = inode->i_uid;
359 if (group == (gid_t) -1)
360 group = inode->i_gid;
361 newattrs.ia_mode = inode->i_mode;
362 newattrs.ia_uid = user;
363 newattrs.ia_gid = group;
364 newattrs.ia_ctime = CURRENT_TIME;
365 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
366
367
368
369 if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
370 newattrs.ia_mode &= ~S_ISUID;
371 newattrs.ia_valid |= ATTR_MODE;
372 }
373
374
375
376 if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
377 newattrs.ia_mode &= ~S_ISGID;
378 newattrs.ia_valid |= ATTR_MODE;
379 }
380 inode->i_dirt = 1;
381 error = notify_change(inode, &newattrs);
382 iput(inode);
383 return(error);
384 }
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400 int do_open(const char * filename,int flags,int mode)
401 {
402 struct inode * inode;
403 struct file * f;
404 int flag,error,fd;
405
406 for(fd=0 ; fd<NR_OPEN ; fd++)
407 if (!current->files->fd[fd])
408 break;
409 if (fd>=NR_OPEN)
410 return -EMFILE;
411 FD_CLR(fd,¤t->files->close_on_exec);
412 f = get_empty_filp();
413 if (!f)
414 return -ENFILE;
415 current->files->fd[fd] = f;
416 f->f_flags = flag = flags;
417 f->f_mode = (flag+1) & O_ACCMODE;
418 if (f->f_mode)
419 flag++;
420 if (flag & (O_TRUNC | O_CREAT))
421 flag |= 2;
422 error = open_namei(filename,flag,mode,&inode,NULL);
423 if (!error && (f->f_mode & 2))
424 error = get_write_access(inode);
425 if (error) {
426 current->files->fd[fd]=NULL;
427 f->f_count--;
428 return error;
429 }
430
431 f->f_inode = inode;
432 f->f_pos = 0;
433 f->f_reada = 0;
434 f->f_op = NULL;
435 if (inode->i_op)
436 f->f_op = inode->i_op->default_file_ops;
437 if (f->f_op && f->f_op->open) {
438 error = f->f_op->open(inode,f);
439 if (error) {
440 if (f->f_mode & 2) put_write_access(inode);
441 iput(inode);
442 f->f_count--;
443 current->files->fd[fd]=NULL;
444 return error;
445 }
446 }
447 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
448 return (fd);
449 }
450
451 asmlinkage int sys_open(const char * filename,int flags,int mode)
452 {
453 char * tmp;
454 int error;
455
456 error = getname(filename, &tmp);
457 if (error)
458 return error;
459 error = do_open(tmp,flags,mode);
460 putname(tmp);
461 return error;
462 }
463
464 asmlinkage int sys_creat(const char * pathname, int mode)
465 {
466 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
467 }
468
469 int close_fp(struct file *filp, unsigned int fd)
470 {
471 struct inode *inode;
472
473 if (filp->f_count == 0) {
474 printk("VFS: Close: file count is 0\n");
475 return 0;
476 }
477 inode = filp->f_inode;
478 if (inode)
479 fcntl_remove_locks(current, filp, fd);
480 if (filp->f_count > 1) {
481 filp->f_count--;
482 return 0;
483 }
484 if (filp->f_op && filp->f_op->release)
485 filp->f_op->release(inode,filp);
486 filp->f_count--;
487 filp->f_inode = NULL;
488 if (filp->f_mode & 2) put_write_access(inode);
489 iput(inode);
490 return 0;
491 }
492
493 asmlinkage int sys_close(unsigned int fd)
494 {
495 struct file * filp;
496
497 if (fd >= NR_OPEN)
498 return -EBADF;
499 FD_CLR(fd, ¤t->files->close_on_exec);
500 if (!(filp = current->files->fd[fd]))
501 return -EBADF;
502 current->files->fd[fd] = NULL;
503 return (close_fp (filp, fd));
504 }
505
506
507
508
509
510 asmlinkage int sys_vhangup(void)
511 {
512 if (!suser())
513 return -EPERM;
514
515 if (current->tty)
516 tty_vhangup(current->tty);
517 return 0;
518 }