blob: 9cffc628a37e0f9505f6da9a8283eaf0ee368e2f [file] [log] [blame]
Jeff Dike1d3468a2006-07-10 04:45:13 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/file.h"
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04007#include "linux/fs.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -07008#include "linux/mm.h"
9#include "linux/sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "linux/utsname.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include "asm/current.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "asm/mman.h"
13#include "asm/uaccess.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070014#include "asm/unistd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016long sys_fork(void)
17{
18 long ret;
19
20 current->thread.forking = 1;
Jeff Dikee0877f02005-06-25 14:55:21 -070021 ret = do_fork(SIGCHLD, UPT_SP(&current->thread.regs.regs),
22 &current->thread.regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 current->thread.forking = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -070024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
26
27long sys_vfork(void)
28{
29 long ret;
30
31 current->thread.forking = 1;
Jeff Dikee0877f02005-06-25 14:55:21 -070032 ret = do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
33 UPT_SP(&current->thread.regs.regs),
34 &current->thread.regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 current->thread.forking = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -070036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39/* common code for old and new mmaps */
40long sys_mmap2(unsigned long addr, unsigned long len,
41 unsigned long prot, unsigned long flags,
42 unsigned long fd, unsigned long pgoff)
43{
44 long error = -EBADF;
45 struct file * file = NULL;
46
47 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
48 if (!(flags & MAP_ANONYMOUS)) {
49 file = fget(fd);
50 if (!file)
51 goto out;
52 }
53
54 down_write(&current->mm->mmap_sem);
55 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
56 up_write(&current->mm->mmap_sem);
57
58 if (file)
59 fput(file);
60 out:
61 return error;
62}
63
64long old_mmap(unsigned long addr, unsigned long len,
65 unsigned long prot, unsigned long flags,
66 unsigned long fd, unsigned long offset)
67{
68 long err = -EINVAL;
69 if (offset & ~PAGE_MASK)
70 goto out;
71
72 err = sys_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
73 out:
74 return err;
75}
76/*
77 * sys_pipe() is the normal C calling standard for creating
78 * a pipe. It's not the way unix traditionally does this, though.
79 */
80long sys_pipe(unsigned long __user * fildes)
81{
Jeff Dikeba180fd2007-10-16 01:27:00 -070082 int fd[2];
83 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jeff Dikeba180fd2007-10-16 01:27:00 -070085 error = do_pipe(fd);
86 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (copy_to_user(fildes, fd, sizeof(fd)))
Jeff Dikeba180fd2007-10-16 01:27:00 -070088 error = -EFAULT;
89 }
90 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93
Al Viro4d338e12006-03-31 02:30:15 -080094long sys_uname(struct old_utsname __user * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 long err;
97 if (!name)
98 return -EFAULT;
99 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700100 err = copy_to_user(name, utsname(), sizeof (*name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 up_read(&uts_sem);
102 return err?-EFAULT:0;
103}
104
Al Viro4d338e12006-03-31 02:30:15 -0800105long sys_olduname(struct oldold_utsname __user * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 long error;
108
109 if (!name)
110 return -EFAULT;
111 if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
112 return -EFAULT;
Jeff Dike1d3468a2006-07-10 04:45:13 -0700113
Jeff Dikeba180fd2007-10-16 01:27:00 -0700114 down_read(&uts_sem);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700115
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700116 error = __copy_to_user(&name->sysname, &utsname()->sysname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 __OLD_UTS_LEN);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700118 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
119 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 __OLD_UTS_LEN);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700121 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
122 error |= __copy_to_user(&name->release, &utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 __OLD_UTS_LEN);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700124 error |= __put_user(0, name->release + __OLD_UTS_LEN);
125 error |= __copy_to_user(&name->version, &utsname()->version,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 __OLD_UTS_LEN);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700127 error |= __put_user(0, name->version + __OLD_UTS_LEN);
128 error |= __copy_to_user(&name->machine, &utsname()->machine,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 __OLD_UTS_LEN);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700130 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 up_read(&uts_sem);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 error = error ? -EFAULT : 0;
135
136 return error;
137}
138
Arnd Bergmann3db03b42006-10-02 02:18:31 -0700139int kernel_execve(const char *filename, char *const argv[], char *const envp[])
140{
141 mm_segment_t fs;
142 int ret;
143
144 fs = get_fs();
145 set_fs(KERNEL_DS);
146 ret = um_execve(filename, argv, envp);
147 set_fs(fs);
148
149 return ret;
150}