From owner-freebsd-hackers Mon Aug 13 8:58:27 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from mail.teledis.be (mail.teledis.be [217.117.32.52]) by hub.freebsd.org (Postfix) with ESMTP id 1164237B40B for ; Mon, 13 Aug 2001 08:58:21 -0700 (PDT) (envelope-from lorenzo@linuxbe.org) Received: from natalie ([217.117.38.8]) by mail.teledis.be (Netscape Messaging Server 4.15) with SMTP id GI0KD501.E2Q for ; Mon, 13 Aug 2001 17:58:17 +0200 Message-ID: <003101c12411$294adaa0$0201a8c0@teledisnet.be> From: "Sansonetti Laurent" To: Subject: allocating userland space to call open() on a kernel module Date: Mon, 13 Aug 2001 18:01:00 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2615.200 X-Mimeole: Produced By Microsoft MimeOLE V5.00.2615.200 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Hello hackers, I'm currently working on a kld syscall module which needs to read a config file at startup (MOD_LOAD). Following the advice of Eugene L. Vorokov, I tried to allocate some userland space with mmap() to store a open_args struct, fill-it with copyout() / subyte()... and call open with curproc on first argument. open() returns 14, EFAULT = Path points outside the process's allocated space (man 2 open). Allocating userland memory works (mmap() returns 0), idem for munmap(). I need help.. it would be fine if you can help me. I'm new on modules coding... Here's the (bad) code, and thanks in advance (forgive-me for my horrible english :p). ---------------------------------------------------------------------------- -------- static void * malloc_space_userland(const int size) { struct mmap_args *ma; int ret=0; void *addr; MALLOC(ma,struct mmap_args *,sizeof(struct mmap_args),M_RING1,M_NOWAIT); ma->addr=0; ma->len=size; ma->prot=PROT_READ|PROT_WRITE; ma->flags=MAP_ANON; ma->fd=-1; if (mmap(curproc,ma)!=0) ret=-1; FREE(ma,M_RING1); if ((addr=(void *)curproc->p_retval[0])==MAP_FAILED) ret=-1; return (!ret)?addr:NULL; } static int free_space_userland(void *addr,const int size) { struct munmap_args *mua; int ret=0; MALLOC(mua,struct munmap_args *,sizeof(struct munmap_args),M_RING1,M_NOWAIT); mua->addr=addr; mua->len=size; if (munmap(curproc,mua)!=0) ret=-1; FREE(mua,M_RING1); return ret; } static int read_config(void) { struct open_args *oa=NULL; int filedes=0; char *fn=CFG_FILENAME; /* #define CFG_FILENAME "/etc/foo.bar" */ if (!(oa=malloc_space_userland(sizeof(struct open_args)))) { printf("Unable to allocate space\n"); return 0; } /* WORKS */ copyout(fn,&oa->path,strlen(CFG_FILENAME)); /* returns 0. */ susword(&oa->flags,O_RDONLY); /* returns 0 */ /* here's the problem, open() returns 14 (EFAULT) ... */ if (!open(curproc,oa) && (filedes=(int)curproc->p_retval[0])>0) { /* ... */ } if (free_space_userland(oa,sizeof(struct open_args))==-1) { printf("Unable to free space\n"); return 0; } /* WORKS */ return (filedes<=0)?0:1; } -- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message