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 unsigned int flags = 0;
139 struct iattr newattrs;
140
141 error = namei(filename,&inode);
142 if (error)
143 return error;
144 if (IS_RDONLY(inode)) {
145 iput(inode);
146 return -EROFS;
147 }
148
149 if (times) {
150 actime = get_fs_long((unsigned long *) ×->actime);
151 modtime = get_fs_long((unsigned long *) ×->modtime);
152 newattrs.ia_ctime = CURRENT_TIME;
153 flags = ATTR_ATIME_SET | ATTR_MTIME_SET;
154 } else {
155 if (!permission(inode,MAY_WRITE)) {
156 iput(inode);
157 return -EACCES;
158 }
159 actime = modtime = newattrs.ia_ctime = CURRENT_TIME;
160 }
161 newattrs.ia_atime = actime;
162 newattrs.ia_mtime = modtime;
163 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME | flags;
164 inode->i_dirt = 1;
165 error = notify_change(inode, &newattrs);
166 iput(inode);
167 return error;
168 }
169
170
171
172
173
174 asmlinkage int sys_access(const char * filename, int mode)
175 {
176 struct inode * inode;
177 int old_fsuid, old_fsgid;
178 int res;
179
180 if (mode != (mode & S_IRWXO))
181 return -EINVAL;
182 old_fsuid = current->fsuid;
183 old_fsgid = current->fsgid;
184 current->fsuid = current->uid;
185 current->fsgid = current->gid;
186 res = namei(filename,&inode);
187 if (!res) {
188 if (!permission(inode, mode))
189 res = -EACCES;
190 iput(inode);
191 }
192 current->fsuid = old_fsuid;
193 current->fsgid = old_fsgid;
194 return res;
195 }
196
197 asmlinkage int sys_chdir(const char * filename)
198 {
199 struct inode * inode;
200 int error;
201
202 error = namei(filename,&inode);
203 if (error)
204 return error;
205 if (!S_ISDIR(inode->i_mode)) {
206 iput(inode);
207 return -ENOTDIR;
208 }
209 if (!permission(inode,MAY_EXEC)) {
210 iput(inode);
211 return -EACCES;
212 }
213 iput(current->fs->pwd);
214 current->fs->pwd = inode;
215 return (0);
216 }
217
218 asmlinkage int sys_fchdir(unsigned int fd)
219 {
220 struct inode * inode;
221 struct file * file;
222
223 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
224 return -EBADF;
225 if (!(inode = file->f_inode))
226 return -ENOENT;
227 if (!S_ISDIR(inode->i_mode))
228 return -ENOTDIR;
229 if (!permission(inode,MAY_EXEC))
230 return -EACCES;
231 iput(current->fs->pwd);
232 current->fs->pwd = inode;
233 inode->i_count++;
234 return (0);
235 }
236
237 asmlinkage int sys_chroot(const char * filename)
238 {
239 struct inode * inode;
240 int error;
241
242 error = namei(filename,&inode);
243 if (error)
244 return error;
245 if (!S_ISDIR(inode->i_mode)) {
246 iput(inode);
247 return -ENOTDIR;
248 }
249 if (!fsuser()) {
250 iput(inode);
251 return -EPERM;
252 }
253 iput(current->fs->root);
254 current->fs->root = inode;
255 return (0);
256 }
257
258 asmlinkage int sys_fchmod(unsigned int fd, mode_t mode)
259 {
260 struct inode * inode;
261 struct file * file;
262 struct iattr newattrs;
263
264 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
265 return -EBADF;
266 if (!(inode = file->f_inode))
267 return -ENOENT;
268 if (IS_RDONLY(inode))
269 return -EROFS;
270 if (mode == (mode_t) -1)
271 mode = inode->i_mode;
272 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
273 newattrs.ia_ctime = CURRENT_TIME;
274 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
275 inode->i_dirt = 1;
276 return notify_change(inode, &newattrs);
277 }
278
279 asmlinkage int sys_chmod(const char * filename, mode_t mode)
280 {
281 struct inode * inode;
282 int error;
283 struct iattr newattrs;
284
285 error = namei(filename,&inode);
286 if (error)
287 return error;
288 if (IS_RDONLY(inode)) {
289 iput(inode);
290 return -EROFS;
291 }
292 if (mode == (mode_t) -1)
293 mode = inode->i_mode;
294 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
295 newattrs.ia_ctime = CURRENT_TIME;
296 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
297 inode->i_dirt = 1;
298 error = notify_change(inode, &newattrs);
299 iput(inode);
300 return error;
301 }
302
303 asmlinkage int sys_fchown(unsigned int fd, uid_t user, gid_t group)
304 {
305 struct inode * inode;
306 struct file * file;
307 struct iattr newattrs;
308
309 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
310 return -EBADF;
311 if (!(inode = file->f_inode))
312 return -ENOENT;
313 if (IS_RDONLY(inode))
314 return -EROFS;
315 if (user == (uid_t) -1)
316 user = inode->i_uid;
317 if (group == (gid_t) -1)
318 group = inode->i_gid;
319 newattrs.ia_mode = inode->i_mode;
320 newattrs.ia_uid = user;
321 newattrs.ia_gid = group;
322 newattrs.ia_ctime = CURRENT_TIME;
323 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
324
325
326
327 if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
328 newattrs.ia_mode &= ~S_ISUID;
329 newattrs.ia_valid |= ATTR_MODE;
330 }
331
332
333
334 if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
335 newattrs.ia_mode &= ~S_ISGID;
336 newattrs.ia_valid |= ATTR_MODE;
337 }
338 inode->i_dirt = 1;
339 return notify_change(inode, &newattrs);
340 }
341
342 asmlinkage int sys_chown(const char * filename, uid_t user, gid_t group)
343 {
344 struct inode * inode;
345 int error;
346 struct iattr newattrs;
347
348 error = lnamei(filename,&inode);
349 if (error)
350 return error;
351 if (IS_RDONLY(inode)) {
352 iput(inode);
353 return -EROFS;
354 }
355 if (user == (uid_t) -1)
356 user = inode->i_uid;
357 if (group == (gid_t) -1)
358 group = inode->i_gid;
359 newattrs.ia_mode = inode->i_mode;
360 newattrs.ia_uid = user;
361 newattrs.ia_gid = group;
362 newattrs.ia_ctime = CURRENT_TIME;
363 newattrs.ia_valid = ATTR_UID | ATTR_GID | ATTR_CTIME;
364
365
366
367 if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
368 newattrs.ia_mode &= ~S_ISUID;
369 newattrs.ia_valid |= ATTR_MODE;
370 }
371
372
373
374 if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
375 newattrs.ia_mode &= ~S_ISGID;
376 newattrs.ia_valid |= ATTR_MODE;
377 }
378 inode->i_dirt = 1;
379 error = notify_change(inode, &newattrs);
380 iput(inode);
381 return(error);
382 }
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 int do_open(const char * filename,int flags,int mode)
399 {
400 struct inode * inode;
401 struct file * f;
402 int flag,error,fd;
403
404 for(fd=0 ; fd<NR_OPEN ; fd++)
405 if (!current->files->fd[fd])
406 break;
407 if (fd>=NR_OPEN)
408 return -EMFILE;
409 FD_CLR(fd,¤t->files->close_on_exec);
410 f = get_empty_filp();
411 if (!f)
412 return -ENFILE;
413 current->files->fd[fd] = f;
414 f->f_flags = flag = flags;
415 f->f_mode = (flag+1) & O_ACCMODE;
416 if (f->f_mode)
417 flag++;
418 if (flag & (O_TRUNC | O_CREAT))
419 flag |= 2;
420 error = open_namei(filename,flag,mode,&inode,NULL);
421 if (!error && (f->f_mode & 2))
422 error = get_write_access(inode);
423 if (error) {
424 current->files->fd[fd]=NULL;
425 f->f_count--;
426 return error;
427 }
428
429 f->f_inode = inode;
430 f->f_pos = 0;
431 f->f_reada = 0;
432 f->f_op = NULL;
433 if (inode->i_op)
434 f->f_op = inode->i_op->default_file_ops;
435 if (f->f_op && f->f_op->open) {
436 error = f->f_op->open(inode,f);
437 if (error) {
438 if (f->f_mode & 2) put_write_access(inode);
439 iput(inode);
440 f->f_count--;
441 current->files->fd[fd]=NULL;
442 return error;
443 }
444 }
445 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
446 return (fd);
447 }
448
449 asmlinkage int sys_open(const char * filename,int flags,int mode)
450 {
451 char * tmp;
452 int error;
453
454 error = getname(filename, &tmp);
455 if (error)
456 return error;
457 error = do_open(tmp,flags,mode);
458 putname(tmp);
459 return error;
460 }
461
462 asmlinkage int sys_creat(const char * pathname, int mode)
463 {
464 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
465 }
466
467 int close_fp(struct file *filp, unsigned int fd)
468 {
469 struct inode *inode;
470
471 if (filp->f_count == 0) {
472 printk("VFS: Close: file count is 0\n");
473 return 0;
474 }
475 inode = filp->f_inode;
476 if (inode)
477 fcntl_remove_locks(current, filp, fd);
478 if (filp->f_count > 1) {
479 filp->f_count--;
480 return 0;
481 }
482 if (filp->f_op && filp->f_op->release)
483 filp->f_op->release(inode,filp);
484 filp->f_count--;
485 filp->f_inode = NULL;
486 if (filp->f_mode & 2) put_write_access(inode);
487 iput(inode);
488 return 0;
489 }
490
491 asmlinkage int sys_close(unsigned int fd)
492 {
493 struct file * filp;
494
495 if (fd >= NR_OPEN)
496 return -EBADF;
497 FD_CLR(fd, ¤t->files->close_on_exec);
498 if (!(filp = current->files->fd[fd]))
499 return -EBADF;
500 current->files->fd[fd] = NULL;
501 return (close_fp (filp, fd));
502 }
503
504
505
506
507
508 asmlinkage int sys_vhangup(void)
509 {
510 if (!suser())
511 return -EPERM;
512
513 if (current->tty)
514 tty_vhangup(current->tty);
515 return 0;
516 }