Trouble with copyout, memcpy.... Plain-Text version =)
Leunam Elebek
forensec at yahoo.de
Mon Sep 28 17:19:09 UTC 2009
Hey list,
I currently code a driver under Current 8.0 for Current 8.0.
But there are some problems with kernel/user-space interaction.
I've the following structure:
struct daq_kitinfo {
uint32_t ki_maxdata;
uint32_t ki_flags;
uint32_t ki_rng_type;
int ki_type;
int ki_nchan;
int ki_len_chanl;
};
The above structure is used in my user-space app:
int main(void) {
struct daq_kitinfo *info;
struct daq_kit kit;
int fd, size;
...
...
...
/*
* At this point I'll try to alloc memory. Notice that
* the size i dependet from another struct entry.
*/
size = sizeof(*info) * kit.k_nkits;
info = malloc(size);
if (info == NULL)
exit(ENOMEM);
/*
* The next step is to call the drivers ioctl() interface
* (the reason for that is described below).
*/
if (ioctl(fd, DAQ_KITINFO, info)) {
printf("errno: %d\n", errno);
exit(errno);
}
printf("[ki_nchan] %d\n", info.ki_nchan);
...
...
return (0);
}
and inside the driver (put it simply):
static int
my_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flags,
struct thread *td)
{
struct daq_kitinfo *info;
struct daq_kit = dev->si_drv1;
int size;
...
/* Do something useful e.g mutex'ing... */
...
/* The same as in user-space... */
size = sizeof(*info) * kit.k_nkits;
info = malloc(sz, M_DAQ, M_NOWAIT | M_ZERO);
if (info == NULL)
....
/*
* Here I want to copy struct info from kernel to user-space.
* If i use memcpy, the result is that the system hangs
* and I need to reboot the machine. OK, I thought
* copyout() should be able to do the job for me...
*/
if (copyout(info, arg, sz))
/*
* Fuc[k-k] i still come inside this block. I always
* get an EFAULT error.
*/
}
I really don't know what I should do to let the driver working
properly. The driver should grap some informations/attributes,
and fill up the info structure, so we can copy the filled info struct
to the user's app.
I hope somebody can help me to resolve that problem.
Ah, the corresponding ioctl is:
#define GRP 'd'
#define DAQ_KITINFO _IOR(GRP, 3, struct daq_kitinfo)
Thanks for attention and greatz from germany
MG
More information about the freebsd-hackers
mailing list