Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 29 Apr 2010 23:19:54 +0200 (CEST)
From:      "=?ISO-8859-15?Q?Luk=E1=A8_Czerner?=" <czerner.lukas@gmail.com>
To:        John Baldwin <jhb@freebsd.org>
Cc:        freebsd-hackers@freebsd.org, =?ISO-8859-15?Q?Luk=E1=A8_Czerner?= <czerner.lukas@gmail.com>
Subject:   Re: ioctl, copy string from user
Message-ID:  <alpine.DEB.1.10.1004292314230.30007@a04-0215a.kn.vutbr.cz>
In-Reply-To: <201004291656.12565.jhb@freebsd.org>
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> <201004291656.12565.jhb@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--8323329-1067549723-1272575995=:30007
Content-Type: TEXT/PLAIN; charset=iso-8859-15
Content-Transfer-Encoding: 8BIT

On Thu, 29 Apr 2010, John Baldwin wrote:

> Date: Thu, 29 Apr 2010 16:56:12 -0400
> From: John Baldwin <jhb@freebsd.org>
> To: LukᨠCzerner <czerner.lukas@gmail.com>
> Cc: freebsd-hackers@freebsd.org
> Subject: Re: ioctl, copy string from user
> 
> On Thursday 29 April 2010 4:28:31 pm LukᨠCzerner wrote:
> > On Thu, 29 Apr 2010, John Baldwin wrote:
> > > > > 
> > > > > 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.
> > 
> > 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?

fprintf(stdout,"Name: %s\n",name);
Name: Dummyflt

uprintf("Name: %s\n", name)
Name:

When I set the name variable before copyinstr() it remains
unchanged.

> 
> > > 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.
> > 
> > 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.

Good to know, thanks!

-Lukas
--8323329-1067549723-1272575995=:30007--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?alpine.DEB.1.10.1004292314230.30007>