COPYIN(K) UNIX System V COPYIN(K)
Name
copyin, copyout - copies bytes between user and kernel space
Syntax
int
copyin(src, dst, cnt)
caddr_t src;
caddr_t dst;
int cnt;
int
copyout(src, dst, cnt)
caddr_t src;
caddr_t dst;
int cnt;
Description
The copyin routine copies bytes from user space to kernel
space. The copyout routine copies bytes from kernel space
to user space. After completion of calls to these routines,
increase u.u_base by the number of bytes transferred, and
decrease u.u_count by the number of bytes transferred. If
an error code is returned, call seterror(EFAULT) to return
EFAULT to the user process that is calling your driver.
Because these routines access a user process, neither can be
used in an interrupt or initialization routine. The driver
is not required to supply word-align addresses to these
routines.
Parameters
For copyin, the argument src is a 32-bit pointer that
contains the offset of the user address the data is copied
from. Often, src is obtained from either u.u_base or the
third argument passed to a driver's xxioctl routine (arg).
The argument dst is a pointer to the kernel address (buffer
address) that the data is transferred to.
The argument cnt specifies the number of bytes to transfer.
For copyout, the argument src is a pointer to the kernel
address (in the buffer) that the data is transferred from.
The argument dst is a 32-bit pointer that contains the
offset of the user address the data is copied to.
The argument cnt specifies the number of bytes to transfer.
Return Value
If successful, these routines perform the specified data
transfer; otherwise, -1 is returned for one of the following
reasons:
+ A page fault occurred between a transfer to user space.
+ The address in user space is invalid.
+ An address was specified that would have resulted in
data being copied into the user block.
Example
Assuming arg is a pointer to a user data structure that was
passed via xxioctl, use copyin to copy from user data space
to kernel data space.
xxioctl(dev, cmd, arg, mode)
dev_t dev;
int cmd, mode;
caddr_t arg;
{
struct foo dst;
.
. other ioctl code
.
/* copy from arg to dst */
if ( copyin(arg, &dst, sizeof(struct foo)) == -1)
{
u.u_error = EFAULT;
return;
}
Assuming arg is a pointer to a user's data structure that
was passed via xxioctl, use copyout to copy from kernel data
space to user data space.
xxioctl(dev, cmd, arg, mode)
dev_t dev;
int cmd, mode;
caddr_t arg;
{
struct foo dst;
.
. other ioctl code
.
/* copy from dst to arg */
if (copyout(&dst, arg, sizeof(struct foo)) == -1)
{
u.u_error = EFAULT;
return;
}
.
.
}
See Also
bcopy(K), copyio(K), seterror(K)
(printed 7/6/89)