Using open system call in KLD

Pranav Sawargaonkar pranav.sawargaonkar at gmail.com
Sun Mar 5 10:41:50 PST 2006


On 3/5/06, Anupam Deshpande <anupamdeshpande at gmail.com> wrote:
>
> >Hello,
>        >  I have used open system call in KLD to create >a file. But after
> >inserting the module the file is not created though >the file descriptor
> >returned is non zero. I also used close system call to >close the file,
> using
> >the descriptor returned by open system call.
>
>

Instead of
>int f_open(void)
>{
   >struct open_args o;
   >struct close_args c;
   >struct thread *td = curthread;
   >int fd;
   >o.path = "/home/file1.c";
   >o.flags = O_CREAT | O_RDWR | O_APPEND;
   >o.mode = 777;
   >fd = open(td,&a);
   >printf("\nFile descriptor  = %d",fd);
   >c.fd = fd;
   >close(td,&c);
>}

U need to change it to
int f_open(void)
{
   struct open_args o;
   struct close_args c;
   struct thread *td = curthread;
   int fd;
   o.path = "/home/file1.c";
   o.flags = O_CREAT | O_RDWR | O_APPEND;
   o.mode = 777;

 />>/fd = open(td,&a);

/*Beacuse open passes UIO_USERSPACE and you are   using open from KLD thus
you need to change it to UIO_SYSSPACE and also need to use  kern_open()
which is defined in #include <sys/syscallsubr.h>
as below*/

fd=kern_open(td,o.path, UIO_SYSSPACE, o.flags,o.mode);


   printf("\nFile descriptor  = %d",fd);
   c.fd = fd;
   close(td,&c);
}
  -Pranav Sawargaonkar


More information about the freebsd-hackers mailing list