linux32.c
来自「linux-2.4.29操作系统的源码」· C语言 代码 · 共 2,612 行 · 第 1/5 页
C
2,612 行
static longdo_readv_writev32(int type, struct file *file, const struct iovec32 *vector, u32 count){ unsigned long tot_len; struct iovec iovstack[UIO_FASTIOV]; struct iovec *iov=iovstack, *ivp; struct inode *inode; long retval, i; IO_fn_t fn; /* First get the "struct iovec" from user memory and * verify all the pointers */ if (!count) return 0; if(verify_area(VERIFY_READ, vector, sizeof(struct iovec32)*count)) return -EFAULT; if (count > UIO_MAXIOV) return -EINVAL; if (count > UIO_FASTIOV) { iov = kmalloc(count*sizeof(struct iovec), GFP_KERNEL); if (!iov) return -ENOMEM; } tot_len = 0; i = count; ivp = iov; while (i > 0) { u32 len; u32 buf; __get_user(len, &vector->iov_len); __get_user(buf, &vector->iov_base); tot_len += len; ivp->iov_base = (void *)A(buf); ivp->iov_len = (__kernel_size_t) len; vector++; ivp++; i--; } inode = file->f_dentry->d_inode; /* VERIFY_WRITE actually means a read, as we write to user space */ retval = locks_verify_area((type == VERIFY_WRITE ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE), inode, file, file->f_pos, tot_len); if (retval) { if (iov != iovstack) kfree(iov); return retval; } /* Then do the actual IO. Note that sockets need to be handled * specially as they have atomicity guarantees and can handle * iovec's natively */ if (inode->i_sock) { int err; err = sock_readv_writev(type, inode, file, iov, count, tot_len); if (iov != iovstack) kfree(iov); return err; } if (!file->f_op) { if (iov != iovstack) kfree(iov); return -EINVAL; } /* VERIFY_WRITE actually means a read, as we write to user space */ fn = file->f_op->read; if (type == VERIFY_READ) fn = (IO_fn_t) file->f_op->write; ivp = iov; while (count > 0) { void * base; int len, nr; base = ivp->iov_base; len = ivp->iov_len; ivp++; count--; nr = fn(file, base, len, &file->f_pos); if (nr < 0) { if (retval) break; retval = nr; break; } retval += nr; if (nr != len) break; } if (iov != iovstack) kfree(iov); return retval;}asmlinkage longsys32_readv(int fd, struct iovec32 *vector, u32 count){ struct file *file; ssize_t ret; ret = -EBADF; file = fget(fd); if (!file) goto bad_file; if (file->f_op && (file->f_mode & FMODE_READ) && (file->f_op->readv || file->f_op->read)) ret = do_readv_writev32(VERIFY_WRITE, file, vector, count); fput(file);bad_file: return ret;}asmlinkage longsys32_writev(int fd, struct iovec32 *vector, u32 count){ struct file *file; ssize_t ret; ret = -EBADF; file = fget(fd); if(!file) goto bad_file; if (file->f_op && (file->f_mode & FMODE_WRITE) && (file->f_op->writev || file->f_op->write)) ret = do_readv_writev32(VERIFY_READ, file, vector, count); fput(file);bad_file: return ret;}/* From the Single Unix Spec: pread & pwrite act like lseek to pos + op + lseek back to original location. They fail just like lseek does on non-seekable files. */asmlinkage ssize_t sys32_pread(unsigned int fd, char * buf, size_t count, u32 unused, u64 a4, u64 a5){ ssize_t ret; struct file * file; ssize_t (*read)(struct file *, char *, size_t, loff_t *); loff_t pos; ret = -EBADF; file = fget(fd); if (!file) goto bad_file; if (!(file->f_mode & FMODE_READ)) goto out; pos = merge_64(a4, a5); ret = locks_verify_area(FLOCK_VERIFY_READ, file->f_dentry->d_inode, file, pos, count); if (ret) goto out; ret = -EINVAL; if (!file->f_op || !(read = file->f_op->read)) goto out; if (pos < 0) goto out; ret = read(file, buf, count, &pos); if (ret > 0) dnotify_parent(file->f_dentry, DN_ACCESS);out: fput(file);bad_file: return ret;}asmlinkage ssize_t sys32_pwrite(unsigned int fd, const char * buf, size_t count, u32 unused, u64 a4, u64 a5){ ssize_t ret; struct file * file; ssize_t (*write)(struct file *, const char *, size_t, loff_t *); loff_t pos; ret = -EBADF; file = fget(fd); if (!file) goto bad_file; if (!(file->f_mode & FMODE_WRITE)) goto out; pos = merge_64(a4, a5); ret = locks_verify_area(FLOCK_VERIFY_WRITE, file->f_dentry->d_inode, file, pos, count); if (ret) goto out; ret = -EINVAL; if (!file->f_op || !(write = file->f_op->write)) goto out; if (pos < 0) goto out; ret = write(file, buf, count, &pos); if (ret > 0) dnotify_parent(file->f_dentry, DN_MODIFY);out: fput(file);bad_file: return ret;}/* * Ooo, nasty. We need here to frob 32-bit unsigned longs to * 64-bit unsigned longs. */static inline intget_fd_set32(unsigned long n, unsigned long *fdset, u32 *ufdset){ if (ufdset) { unsigned long odd; if (verify_area(VERIFY_WRITE, ufdset, n*sizeof(u32))) return -EFAULT; odd = n & 1UL; n &= ~1UL; while (n) { unsigned long h, l; __get_user(l, ufdset); __get_user(h, ufdset+1); ufdset += 2; *fdset++ = h << 32 | l; n -= 2; } if (odd) __get_user(*fdset, ufdset); } else { /* Tricky, must clear full unsigned long in the * kernel fdset at the end, this makes sure that * actually happens. */ memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32)); } return 0;}static inline voidset_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset){ unsigned long odd; if (!ufdset) return; odd = n & 1UL; n &= ~1UL; while (n) { unsigned long h, l; l = *fdset++; h = l >> 32; __put_user(l, ufdset); __put_user(h, ufdset+1); ufdset += 2; n -= 2; } if (odd) __put_user(*fdset, ufdset);}/* * We can actually return ERESTARTSYS instead of EINTR, but I'd * like to be certain this leads to no problems. So I return * EINTR just for safety. * * Update: ERESTARTSYS breaks at least the xview clock binary, so * I'm trying ERESTARTNOHAND which restart only when you want to. */#define MAX_SELECT_SECONDS \ ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)asmlinkage int sys32_select(int n, u32 *inp, u32 *outp, u32 *exp, struct timeval32 *tvp){ fd_set_bits fds; char *bits; unsigned long nn; long timeout; int ret, size; timeout = MAX_SCHEDULE_TIMEOUT; if (tvp) { time_t sec, usec; if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp))) || (ret = __get_user(sec, &tvp->tv_sec)) || (ret = __get_user(usec, &tvp->tv_usec))) goto out_nofds; ret = -EINVAL; if(sec < 0 || usec < 0) goto out_nofds; if ((unsigned long) sec < MAX_SELECT_SECONDS) { timeout = (usec + 1000000/HZ - 1) / (1000000/HZ); timeout += sec * (unsigned long) HZ; } } ret = -EINVAL; if (n < 0) goto out_nofds; if (n > current->files->max_fdset) n = current->files->max_fdset; /* * We need 6 bitmaps (in/out/ex for both incoming and outgoing), * since we used fdset we need to allocate memory in units of * long-words. */ ret = -ENOMEM; size = FDS_BYTES(n); bits = kmalloc(6 * size, GFP_KERNEL); if (!bits) goto out_nofds; fds.in = (unsigned long *) bits; fds.out = (unsigned long *) (bits + size); fds.ex = (unsigned long *) (bits + 2*size); fds.res_in = (unsigned long *) (bits + 3*size); fds.res_out = (unsigned long *) (bits + 4*size); fds.res_ex = (unsigned long *) (bits + 5*size); nn = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32)); if ((ret = get_fd_set32(nn, fds.in, inp)) || (ret = get_fd_set32(nn, fds.out, outp)) || (ret = get_fd_set32(nn, fds.ex, exp))) goto out; zero_fd_set(n, fds.res_in); zero_fd_set(n, fds.res_out); zero_fd_set(n, fds.res_ex); ret = do_select(n, &fds, &timeout); if (tvp && !(current->personality & STICKY_TIMEOUTS)) { time_t sec = 0, usec = 0; if (timeout) { sec = timeout / HZ; usec = timeout % HZ; usec *= (1000000/HZ); } put_user(sec, &tvp->tv_sec); put_user(usec, &tvp->tv_usec); } if (ret < 0) goto out; if (!ret) { ret = -ERESTARTNOHAND; if (signal_pending(current)) goto out; ret = 0; } set_fd_set32(nn, inp, fds.res_in); set_fd_set32(nn, outp, fds.res_out); set_fd_set32(nn, exp, fds.res_ex);out: kfree(bits);out_nofds: return ret;}struct timespec32 { int tv_sec; int tv_nsec;};extern asmlinkage int sys_sched_rr_get_interval(pid_t pid, struct timespec *interval);asmlinkage intsys32_sched_rr_get_interval(__kernel_pid_t32 pid, struct timespec32 *interval){ struct timespec t; int ret; mm_segment_t old_fs = get_fs (); set_fs (KERNEL_DS); ret = sys_sched_rr_get_interval(pid, &t); set_fs (old_fs); if (put_user (t.tv_sec, &interval->tv_sec) || __put_user (t.tv_nsec, &interval->tv_nsec)) return -EFAULT; return ret;}extern asmlinkage int sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp);asmlinkage intsys32_nanosleep(struct timespec32 *rqtp, struct timespec32 *rmtp){ struct timespec t; int ret; mm_segment_t old_fs = get_fs (); if (get_user (t.tv_sec, &rqtp->tv_sec) || __get_user (t.tv_nsec, &rqtp->tv_nsec)) return -EFAULT; set_fs (KERNEL_DS); ret = sys_nanosleep(&t, rmtp ? &t : NULL); set_fs (old_fs); if (rmtp && ret == -EINTR) { if (__put_user (t.tv_sec, &rmtp->tv_sec) || __put_user (t.tv_nsec, &rmtp->tv_nsec)) return -EFAULT; } return ret;}struct tms32 { int tms_utime; int tms_stime; int tms_cutime; int tms_cstime;};extern asmlinkage long sys_times(struct tms * tbuf);asmlinkage long sys32_times(struct tms32 *tbuf){ struct tms t; long ret; mm_segment_t old_fs = get_fs(); int err; set_fs(KERNEL_DS); ret = sys_times(tbuf ? &t : NULL); set_fs(old_fs); if (tbuf) { err = put_user (t.tms_utime, &tbuf->tms_utime); err |= __put_user (t.tms_stime, &tbuf->tms_stime); err |= __put_user (t.tms_cutime, &tbuf->tms_cutime); err |= __put_user (t.tms_cstime, &tbuf->tms_cstime); if (err) ret = -EFAULT; } return ret;}static int do_set_attach_filter(int fd, int level, int optname, char *optval, int optlen){ struct sock_fprog32 { __u16 len; __u32 filter; } *fprog32 = (struct sock_fprog32 *)optval; struct sock_fprog kfprog; struct sock_filter *kfilter; unsigned int fsize; mm_segment_t old_fs; __u32 uptr; int ret; if (get_user(kfprog.len, &fprog32->len) || __get_user(uptr, &fprog32->filter)) return -EFAULT; kfprog.filter = (struct sock_filter *)A(uptr); fsize = kfprog.len * sizeof(struct sock_filter); kfilter = (struct sock_filter *)kmalloc(fsize, GFP_KERNEL); if (kfilter == NULL) return -ENOMEM; if (copy_from_user(kfilter, kfprog.filter, fsize)) { kfree(kfilter); return -EFAULT; } kfprog.filter = kfilter; old_fs = get_fs(); set_fs(KERNEL_DS); ret = sys_setsockopt(fd, level, optname, (char *)&kfprog, sizeof(kfprog)); set_fs(old_fs); kfree(kfilter); return ret;}static int do_set_icmpv6_filter(int fd, int level, int optname, char *optval, int optlen){ struct icmp6_filter kfilter; mm_segment_t old_fs; int ret, i; if (copy_from_user(&kfilter, optval, sizeof(kfilter))) return -EFAULT; for (i = 0; i < 8; i += 2) { u32 tmp = kfilter.data[i]; kfilter.data[i] = kfilter.data[i + 1]; kfilter.data[i + 1] = tmp; } old_fs = get_fs(); set_fs(KERNEL_DS); ret = sys_setsockopt(fd, level, optname, (char *) &kfilter, sizeof(kfilter)); set_fs(old_fs); return ret;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?