From owner-freebsd-hackers@FreeBSD.ORG Thu Apr 29 20:06:59 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 9C0CB106566B for ; Thu, 29 Apr 2010 20:06:59 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [65.122.17.42]) by mx1.freebsd.org (Postfix) with ESMTP id 695038FC1B for ; Thu, 29 Apr 2010 20:06:59 +0000 (UTC) Received: from bigwig.baldwin.cx (66.111.2.69.static.nyinternet.net [66.111.2.69]) by cyrus.watson.org (Postfix) with ESMTPSA id 1098C46B0D; Thu, 29 Apr 2010 16:06:59 -0400 (EDT) Received: from jhbbsd.localnet (smtp.hudson-trading.com [209.249.190.9]) by bigwig.baldwin.cx (Postfix) with ESMTPA id 509DC8A01F; Thu, 29 Apr 2010 16:06:58 -0400 (EDT) From: John Baldwin To: "=?iso-8859-15?q?Luk=E1=A8?= Czerner" Date: Thu, 29 Apr 2010 16:06:35 -0400 User-Agent: KMail/1.12.1 (FreeBSD/7.3-CBSD-20100217; KDE/4.3.1; amd64; ; ) References: <201004291418.09768.jhb@freebsd.org> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable Message-Id: <201004291606.35899.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0.1 (bigwig.baldwin.cx); Thu, 29 Apr 2010 16:06:58 -0400 (EDT) X-Virus-Scanned: clamav-milter 0.95.1 at bigwig.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=4.2 tests=AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on bigwig.baldwin.cx Cc: freebsd-hackers@freebsd.org Subject: Re: ioctl, copy string from user X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Apr 2010 20:06:59 -0000 On Thursday 29 April 2010 3:21:00 pm Luk=E1=A8 Czerner wrote: > On Thu, 29 Apr 2010, John Baldwin wrote: >=20 > > Date: Thu, 29 Apr 2010 14:18:09 -0400 > > From: John Baldwin > > To: freebsd-hackers@freebsd.org > > Cc: Luk=E1=A8 Czerner > > Subject: Re: ioctl, copy string from user > >=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 mistake > > > 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 the > > > string argument into the kernel space. I have tried it various ways, > > > 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 ahead o= f=20 time. =20 > > What does the definition of MYIOCTL look like? >=20 > #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=20 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 ? > >=20 > > malloc() and free() acquire their own locks internally, you do not need= to=20 > > hold any locks to call them. >=20 > 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=20 is ok to do a blocking malloc(). As a general rule I do try to call malloc= () before acquiring locks (basically preallocating) whenever possible. =2D-=20 John Baldwin