sys_i386.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/arch/i386/kernel/sys_i386.c
  3.  *
  4.  * This file contains various random system calls that
  5.  * have a non-standard calling sequence on the Linux/i386
  6.  * platform.
  7.  */
  8. #include <linux/errno.h>
  9. #include <linux/sched.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/sem.h>
  14. #include <linux/msg.h>
  15. #include <linux/shm.h>
  16. #include <linux/stat.h>
  17. #include <linux/mman.h>
  18. #include <linux/file.h>
  19. #include <linux/utsname.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/ipc.h>
  22. /*
  23.  * sys_pipe() is the normal C calling standard for creating
  24.  * a pipe. It's not the way Unix traditionally does this, though.
  25.  */
  26. asmlinkage int sys_pipe(unsigned long * fildes)
  27. {
  28. int fd[2];
  29. int error;
  30. error = do_pipe(fd);
  31. if (!error) {
  32. if (copy_to_user(fildes, fd, 2*sizeof(int)))
  33. error = -EFAULT;
  34. }
  35. return error;
  36. }
  37. /* common code for old and new mmaps */
  38. static inline long do_mmap2(
  39. unsigned long addr, unsigned long len,
  40. unsigned long prot, unsigned long flags,
  41. unsigned long fd, unsigned long pgoff)
  42. {
  43. int error = -EBADF;
  44. struct file * file = NULL;
  45. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  46. if (!(flags & MAP_ANONYMOUS)) {
  47. file = fget(fd);
  48. if (!file)
  49. goto out;
  50. }
  51. down_write(&current->mm->mmap_sem);
  52. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  53. up_write(&current->mm->mmap_sem);
  54. if (file)
  55. fput(file);
  56. out:
  57. return error;
  58. }
  59. asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
  60. unsigned long prot, unsigned long flags,
  61. unsigned long fd, unsigned long pgoff)
  62. {
  63. return do_mmap2(addr, len, prot, flags, fd, pgoff);
  64. }
  65. /*
  66.  * Perform the select(nd, in, out, ex, tv) and mmap() system
  67.  * calls. Linux/i386 didn't use to be able to handle more than
  68.  * 4 system call parameters, so these system calls used a memory
  69.  * block for parameter passing..
  70.  */
  71. struct mmap_arg_struct {
  72. unsigned long addr;
  73. unsigned long len;
  74. unsigned long prot;
  75. unsigned long flags;
  76. unsigned long fd;
  77. unsigned long offset;
  78. };
  79. asmlinkage int old_mmap(struct mmap_arg_struct *arg)
  80. {
  81. struct mmap_arg_struct a;
  82. int err = -EFAULT;
  83. if (copy_from_user(&a, arg, sizeof(a)))
  84. goto out;
  85. err = -EINVAL;
  86. if (a.offset & ~PAGE_MASK)
  87. goto out;
  88. err = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
  89. out:
  90. return err;
  91. }
  92. extern asmlinkage int sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
  93. struct sel_arg_struct {
  94. unsigned long n;
  95. fd_set *inp, *outp, *exp;
  96. struct timeval *tvp;
  97. };
  98. asmlinkage int old_select(struct sel_arg_struct *arg)
  99. {
  100. struct sel_arg_struct a;
  101. if (copy_from_user(&a, arg, sizeof(a)))
  102. return -EFAULT;
  103. /* sys_select() does the appropriate kernel locking */
  104. return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  105. }
  106. /*
  107.  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  108.  *
  109.  * This is really horribly ugly.
  110.  */
  111. asmlinkage int sys_ipc (uint call, int first, int second,
  112. int third, void *ptr, long fifth)
  113. {
  114. int version, ret;
  115. version = call >> 16; /* hack for backward compatibility */
  116. call &= 0xffff;
  117. switch (call) {
  118. case SEMOP:
  119. return sys_semop (first, (struct sembuf *)ptr, second);
  120. case SEMGET:
  121. return sys_semget (first, second, third);
  122. case SEMCTL: {
  123. union semun fourth;
  124. if (!ptr)
  125. return -EINVAL;
  126. if (get_user(fourth.__pad, (void **) ptr))
  127. return -EFAULT;
  128. return sys_semctl (first, second, third, fourth);
  129. }
  130. case MSGSND:
  131. return sys_msgsnd (first, (struct msgbuf *) ptr, 
  132.    second, third);
  133. case MSGRCV:
  134. switch (version) {
  135. case 0: {
  136. struct ipc_kludge tmp;
  137. if (!ptr)
  138. return -EINVAL;
  139. if (copy_from_user(&tmp,
  140.    (struct ipc_kludge *) ptr, 
  141.    sizeof (tmp)))
  142. return -EFAULT;
  143. return sys_msgrcv (first, tmp.msgp, second,
  144.    tmp.msgtyp, third);
  145. }
  146. default:
  147. return sys_msgrcv (first,
  148.    (struct msgbuf *) ptr,
  149.    second, fifth, third);
  150. }
  151. case MSGGET:
  152. return sys_msgget ((key_t) first, second);
  153. case MSGCTL:
  154. return sys_msgctl (first, second, (struct msqid_ds *) ptr);
  155. case SHMAT:
  156. switch (version) {
  157. default: {
  158. ulong raddr;
  159. ret = sys_shmat (first, (char *) ptr, second, &raddr);
  160. if (ret)
  161. return ret;
  162. return put_user (raddr, (ulong *) third);
  163. }
  164. case 1: /* iBCS2 emulator entry point */
  165. if (!segment_eq(get_fs(), get_ds()))
  166. return -EINVAL;
  167. return sys_shmat (first, (char *) ptr, second, (ulong *) third);
  168. }
  169. case SHMDT: 
  170. return sys_shmdt ((char *)ptr);
  171. case SHMGET:
  172. return sys_shmget (first, second, third);
  173. case SHMCTL:
  174. return sys_shmctl (first, second,
  175.    (struct shmid_ds *) ptr);
  176. default:
  177. return -EINVAL;
  178. }
  179. }
  180. /*
  181.  * Old cruft
  182.  */
  183. asmlinkage int sys_uname(struct old_utsname * name)
  184. {
  185. int err;
  186. if (!name)
  187. return -EFAULT;
  188. down_read(&uts_sem);
  189. err=copy_to_user(name, &system_utsname, sizeof (*name));
  190. up_read(&uts_sem);
  191. return err?-EFAULT:0;
  192. }
  193. asmlinkage int sys_olduname(struct oldold_utsname * name)
  194. {
  195. int error;
  196. if (!name)
  197. return -EFAULT;
  198. if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
  199. return -EFAULT;
  200.   
  201.    down_read(&uts_sem);
  202. error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
  203. error |= __put_user(0,name->sysname+__OLD_UTS_LEN);
  204. error |= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
  205. error |= __put_user(0,name->nodename+__OLD_UTS_LEN);
  206. error |= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
  207. error |= __put_user(0,name->release+__OLD_UTS_LEN);
  208. error |= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
  209. error |= __put_user(0,name->version+__OLD_UTS_LEN);
  210. error |= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
  211. error |= __put_user(0,name->machine+__OLD_UTS_LEN);
  212. up_read(&uts_sem);
  213. error = error ? -EFAULT : 0;
  214. return error;
  215. }
  216. asmlinkage int sys_pause(void)
  217. {
  218. current->state = TASK_INTERRUPTIBLE;
  219. schedule();
  220. return -ERESTARTNOHAND;
  221. }