From owner-freebsd-hackers@FreeBSD.ORG Thu Apr 29 21:10:01 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 903891065674; Thu, 29 Apr 2010 21:10:01 +0000 (UTC) (envelope-from czerner.lukas@gmail.com) Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.158]) by mx1.freebsd.org (Postfix) with ESMTP id D9DD48FC19; Thu, 29 Apr 2010 21:10:00 +0000 (UTC) Received: by fg-out-1718.google.com with SMTP id 22so3415394fge.13 for ; Thu, 29 Apr 2010 14:09:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:date:from:x-x-sender:to:cc :subject:in-reply-to:message-id:references:user-agent:mime-version :content-type; bh=vGax1vB/lMcGNpB2aSm/sd4Srp1PbJSh0zkxXTZNtDM=; b=nrPGfLvVR0QrusBegYfzD2K7hxZgCCZdCyHguKNAJgFIAgR/tMZJvuFGYTMcL5RKRG QxotVJuZui+kuHIoFPV34bndPUNbfU9SS2/gbZgiNJZCej5vrqs73qBGKMP5AJ8FOPoo w+pkOSlBKDXejyIbUNrErIPb9vVQBztYbULFU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=date:from:x-x-sender:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version:content-type; b=ccHpG1grEkrdMI51pBYOPIrdEsKLhmKSdUXT7mquqRFzHEMCEDWNxU/ZqaJ5Exjf7w UBQq5ZKY/Zh4dKImOyIelnZGuSmvPRPBOMgKqgfeqUcHAGg98tTN1psJW/Lad60doX4U VBtPPw29u7KGF37RpCL5sSgyVTTiA8ycPFeyY= Received: by 10.86.22.31 with SMTP id 31mr2316442fgv.24.1272575389966; Thu, 29 Apr 2010 14:09:49 -0700 (PDT) Received: from a04-0215a.kn.vutbr.cz (a04-0215a.kn.vutbr.cz [147.229.216.20]) by mx.google.com with ESMTPS id 4sm3196669fgg.27.2010.04.29.14.09.47 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 29 Apr 2010 14:09:48 -0700 (PDT) Date: Thu, 29 Apr 2010 23:09:51 +0200 (CEST) From: "=?ISO-8859-15?Q?Luk=E1=A8_Czerner?=" X-X-Sender: bratt@a04-0215a.kn.vutbr.cz To: =?ISO-8859-15?Q?Luk=E1=A8_Czerner?= In-Reply-To: Message-ID: References: <201004291418.09768.jhb@freebsd.org> <201004291606.35899.jhb@freebsd.org> User-Agent: Alpine 1.10 (DEB 962 2008-03-14) MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="8323329-1438505303-1272575392=:30007" 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 21:10:01 -0000 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-1438505303-1272575392=:30007 Content-Type: TEXT/PLAIN; charset=iso-8859-15 Content-Transfer-Encoding: 8BIT On Thu, 29 Apr 2010, LukᨠCzerner wrote: > Date: Thu, 29 Apr 2010 22:28:31 +0200 (CEST) > From: LukᨠCzerner > To: John Baldwin > Cc: LukᨠCzerner , freebsd-hackers@freebsd.org > Subject: Re: ioctl, copy string from user > > 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). > > Just FYI I am using FreeBSD-8. > > > > > > > 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. > > 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. > > Thanks. Ok, so I have manage to get it working, but I doubt this is the proper way of doing this: #define MYIOCTL _IOW('M', 0, char[MAXLEN]) ** Userspace ** ioctl(fd, MYIOCTL, &name) ** Kernel ** name = (char *)ap->a_data; uprintf("Name : %s\n", name); Apparently I need to tell ioctl how big is the variable I am providing to it ([MAXLEN]). The odd thing is, when I have a structure like this: struct lrfs_attach_info { char *name; int priority; }; and I pass the pointer to that structure to the ioctl, it just works. I can even use the 'name' string from the structure without any problems, apparently it translates the pointer properly, but I did not expect this... --8323329-1438505303-1272575392=:30007--