Date: Thu, 29 Apr 2010 16:56:12 -0400 From: John Baldwin <jhb@freebsd.org> To: "=?iso-8859-15?q?Luk=E1=A8?= Czerner" <czerner.lukas@gmail.com> Cc: freebsd-hackers@freebsd.org Subject: Re: ioctl, copy string from user Message-ID: <201004291656.12565.jhb@freebsd.org> In-Reply-To: <alpine.DEB.1.10.1004292220170.30007@a04-0215a.kn.vutbr.cz> References: <alpine.DEB.1.10.1004291938210.30007@a04-0215a.kn.vutbr.cz> <201004291606.35899.jhb@freebsd.org> <alpine.DEB.1.10.1004292220170.30007@a04-0215a.kn.vutbr.cz>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thursday 29 April 2010 4:28:31 pm Luk=E1=A8 Czerner wrote: > On Thu, 29 Apr 2010, John Baldwin wrote: > > > >=20 > > > > On Thursday 29 April 2010 1:52:45 pm Luk=E1=A8 Czerner wrote: > > > > > Hi, > > > > >=20 > > > > > I know that there are plenty of examples in the kernel code, but I > > > > > just can not get it working, so maybe I am doing some stupid mist= ake > > > > > I am not aware of. Please give me a hint if you can. > > > > >=20 > > > > > What I want to do is simply call the ioctl from the userspace with > > > > > (char *) argument. Then, in kernel ioctl handling function copy t= he > > > > > string argument into the kernel space. I have tried it various wa= ys, > > > > > everything without any success. > > > > >=20 > > > > > *** Userspace *** > > > > > char name[MAXLEN]; > > > > >=20 > > > > > strncpy(name, argv[1], MAXLEN); > > > > > fprintf(stdout,"Name: %s\n",name); > > > > >=20 > > > > > if (ioctl(fd, MYIOCTL, name)) { > > > >=20 > > > > On BSD systems, ioctl() copies the data into the kernel for you ahe= ad of=20 > > time. =20 > > > > What does the definition of MYIOCTL look like? > > >=20 > > > #define MYIOCTL _IOW('M', 0, char *) > >=20 > > Ok. In that case the argument to ioctl needs to be a pointer to a char= *, > > not the raw char * itself. Try doing 'ioctl(fd, MYIOCTL, &name)' from= =20 > > userland to see if that fixes it. >=20 > I have already tried that, but still without any success. The buffer > remains unchanged (which is weird IMO). Can you print out the value of 'ap->a_data' from the kernel and the value of '&name' in userland? > > Generally yes, but it depends on the lock. If it is the vn_lock lock t= hen it=20 > > is ok to do a blocking malloc(). As a general rule I do try to call ma= lloc() > > before acquiring locks (basically preallocating) whenever possible. >=20 > So I suppose M_NOWAIT will do the trick when there is no other way > (preallocations etc..) ? Of course I should test if it does not > return NULL then. Yes, but in a VOP_IOCTL() handler it should be safe to M_WAITOK malloc() as long as you do it before you acquire any mutexes. =2D-=20 John Baldwin
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201004291656.12565.jhb>