Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 Aug 2001 18:01:00 +0200
From:      "Sansonetti Laurent" <lorenzo@linuxbe.org>
To:        <freebsd-hackers@freebsd.org>
Subject:   allocating userland space to call open() on a kernel module
Message-ID:  <003101c12411$294adaa0$0201a8c0@teledisnet.be>

next in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?003101c12411$294adaa0$0201a8c0>