Date: Thu, 29 Apr 2010 16:06:35 -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: <201004291606.35899.jhb@freebsd.org> In-Reply-To: <alpine.DEB.1.10.1004292114360.30007@a04-0215a.kn.vutbr.cz> References: <alpine.DEB.1.10.1004291938210.30007@a04-0215a.kn.vutbr.cz> <201004291418.09768.jhb@freebsd.org> <alpine.DEB.1.10.1004292114360.30007@a04-0215a.kn.vutbr.cz>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thursday 29 April 2010 3:21:00 pm LukᨠCzerner wrote:
> On Thu, 29 Apr 2010, John Baldwin wrote:
>
> > Date: Thu, 29 Apr 2010 14:18:09 -0400
> > From: John Baldwin <jhb@freebsd.org>
> > To: freebsd-hackers@freebsd.org
> > Cc: LukᨠCzerner <czerner.lukas@gmail.com>
> > Subject: Re: ioctl, copy string from user
> >
> > On Thursday 29 April 2010 1:52:45 pm LukᨠCzerner wrote:
> > > Hi,
> > >
> > > 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 mistake
> > > I am not aware of. Please give me a hint if you can.
> > >
> > > What I want to do is simply call the ioctl from the userspace with
> > > (char *) argument. Then, in kernel ioctl handling function copy the
> > > string argument into the kernel space. I have tried it various ways,
> > > everything without any success.
> > >
> > > *** Userspace ***
> > > char name[MAXLEN];
> > >
> > > strncpy(name, argv[1], MAXLEN);
> > > fprintf(stdout,"Name: %s\n",name);
> > >
> > > if (ioctl(fd, MYIOCTL, name)) {
> >
> > On BSD systems, ioctl() copies the data into the kernel for you ahead of
time.
> > What does the definition of MYIOCTL look like?
>
> #define MYIOCTL _IOW('M', 0, char *)
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
userland to see if that fixes it.
> > > And the second question. I have commented that I can allocate buffer
> > > dynamically, but I suppose that there will be some locks involved so
> > > I think I can not just use M_WAITOK, am I right ?
> >
> > malloc() and free() acquire their own locks internally, you do not need to
> > hold any locks to call them.
>
> I probably does not express what I meant very clearly. My concern is
> that when I am calling malloc with M_WAITOK I can sleep (be
> rescheduled) and it may be bad thing if I am holding some lock,
> because I can block others, am I right ?
Generally yes, but it depends on the lock. If it is the vn_lock lock then it
is ok to do a blocking malloc(). As a general rule I do try to call malloc()
before acquiring locks (basically preallocating) whenever possible.
--
John Baldwin
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201004291606.35899.jhb>
