From owner-freebsd-hackers@FreeBSD.ORG Sun Feb 6 13:00:31 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1612916A4CF for ; Sun, 6 Feb 2005 13:00:31 +0000 (GMT) Received: from cyrus.watson.org (cyrus.watson.org [204.156.12.53]) by mx1.FreeBSD.org (Postfix) with ESMTP id CD42443D1D for ; Sun, 6 Feb 2005 13:00:28 +0000 (GMT) (envelope-from robert@fledge.watson.org) Received: from fledge.watson.org (fledge.watson.org [204.156.12.50]) by cyrus.watson.org (Postfix) with SMTP id 8966646B16; Sun, 6 Feb 2005 08:00:28 -0500 (EST) Date: Sun, 6 Feb 2005 12:59:33 +0000 (GMT) From: Robert Watson X-Sender: robert@fledge.watson.org To: Ashwin Chandra In-Reply-To: <001301c50c3e$1a8003b0$58e243a4@ash> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-hackers@freebsd.org Subject: Re: Opening and wriiting to file in Kern X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 06 Feb 2005 13:00:31 -0000 On Sun, 6 Feb 2005, Ashwin Chandra wrote: > Does anyone know the correct calls to open a file, write to it, and > close it, IN *kernel* mode. I fyou want to be file system independent, you can currently do it using two different currently available kernel abstractions: - VFS interface. Using vn_open(), return a reference to an opened vnode. Use vn_rdwr() to perform I/O on the vnode, and vn_close() to close it. Make sure to properly lock the vnode during I/O and other operations. This interface is abstract in the sense that it's file systme independent, but not in most other senses. This can be done from many contexts in the kernel, including kernel worker threads, etc. - File interface. Using kern_open(), return a reference to an open 'struct file' Use the calls fo_read(), fo_write(), etc. This is more abstract than the VFS interface, and strongly resembles the interface used via file descriptors (since that's what it implements). It's less functionally complete than the VFS interface, so you can't do stuff like changing the file mode, etc, directly (you have to perform them on the file's vnode). It also requires file descriptor context, so possibly one associated with a user process and then "borrowed" by the kernel. Linux exports a FILE stream like interface in its kernel, and for the purposes of porting the FLASK/TE components from DTOS/SELinux to FreeBSD, we also ported a subset of this functionality. Feel free to grab and reuse as appropriate in a kernel module or the like. You can find it at: http://fxr.watson.org/fxr/source/security/sebsd/ss/fileutils.c?v=TRUSTEDBSD-SEBSD http://fxr.watson.org/fxr/source/security/sebsd/ss/fileutils.h?v=TRUSTEDBSD-SEBSD Right now it just implements fopen(), fread(), and fclose(), but it would be easy to imagine implementing fwrite(), feof(), etc by wrapping the vnode interface. Robert N M Watson