1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 /*
  30  * lx_sendfile() and lx_sendfile64() are just branded versions of the
  31  * library calls available in the Solaris libsendfile (see sendfile(3EXT)).
  32  */
  33 
  34 #include <sys/types.h>
  35 #include <sys/syscall.h>
  36 #include <sys/sendfile.h>
  37 #include <string.h>
  38 #include <errno.h>
  39 #include <sys/lx_misc.h>
  40 
  41 int
  42 lx_sendfile(uintptr_t p1, uintptr_t p2, uintptr_t p3, uintptr_t p4)
  43 {
  44         sysret_t rval;
  45         off_t off = 0;
  46         off_t *offp = (off_t *)p3;
  47         int error;
  48         struct sendfilevec sfv;
  49         size_t xferred;
  50         size_t sz = (size_t)p4;
  51 
  52         if (sz > 0 && uucopy(offp, &off, sizeof (off)) != 0)
  53                 return (-errno);
  54 
  55         sfv.sfv_fd = p2;
  56         sfv.sfv_flag = 0;
  57         sfv.sfv_off = off;
  58         sfv.sfv_len = sz;
  59         error = __systemcall(&rval, SYS_sendfilev, SENDFILEV, p1, &sfv,
  60             1, &xferred);
  61 
  62         if (error == 0 && xferred > 0) {
  63                 off += xferred;
  64                 error = uucopy(&off, offp, sizeof (off));
  65         }
  66 
  67         return (error ? -error : (int)rval.sys_rval1);
  68 }
  69 
  70 int
  71 lx_sendfile64(uintptr_t p1, uintptr_t p2, uintptr_t p3, uintptr_t p4)
  72 {
  73         sysret_t rval;
  74         off64_t off = 0;
  75         off64_t *offp = (off64_t *)p3;
  76         size_t sz = (size_t)p4;
  77         int error;
  78         struct sendfilevec64 sfv;
  79         size_t xferred;
  80 
  81         if (sz > 0 && uucopy(offp, &off, sizeof (off)) != 0)
  82                 return (-errno);
  83 
  84         sfv.sfv_fd = p2;
  85         sfv.sfv_flag = 0;
  86         sfv.sfv_off = off;
  87         sfv.sfv_len = sz;
  88         error = __systemcall(&rval, SYS_sendfilev, SENDFILEV64, p1, &sfv,
  89             1, &xferred);
  90 
  91         if (error == 0 && xferred > 0) {
  92                 off += xferred;
  93                 error = uucopy(&off, offp, sizeof (off));
  94         }
  95 
  96         return (error ? -error : (int)rval.sys_rval1);
  97 }