Using open system call in KLD

Robert Watson rwatson at FreeBSD.org
Sun Mar 5 09:23:50 PST 2006


On Sun, 5 Mar 2006, Anupam Deshpande wrote:

>         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.
>         I called the following function from my module:
>
> int f_open(void)
> {
>   struct open_args o;
>   struct close_args c;
>   struct thread *td = curthread;
>   int fd;
>   o.path = "/home/file1.c";

There are a couple of things going on here:

- open() accepts a pointer to a pathname in user address space.  If this code
   is running in kernel, then the above string is in the kernel address space.
   You probably want to look at kern_open(), which accepts a path pointer and
   also an address space identifier, which can be set to UIO_SYSSPACE to
   indicate that the path argument is being copied from a kernel address.

- In kernel, system calls return (0) for success, or an error value, not a
   file descriptor number.  This is placed in the thread context return values
   to be returned to user space.  Specifically, in td->td_retval[0].  So you're
   not checking to make sure the call succeeded, and you're also not getting
   the file descriptor from the right place.  You'll probably find that the
   value you're getting back is EFAULT, indicating that the path pointer was
   not valid for a user process.

Robert N M Watson

>   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);
> }
>
>
>         Can anyone help me in this regard?
>
> TIA,
> Anupam Deshpande
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
>


More information about the freebsd-hackers mailing list