public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Juergen Quade <quade@hsnr.de>
To: Mohamed El Dawy <msdawy@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: What's wrong with this really simple function?
Date: Mon, 28 Nov 2005 08:55:05 +0100	[thread overview]
Message-ID: <20051128075505.GA7945@hsnr.de> (raw)
In-Reply-To: <afd776760511271057l5e3c4e3fq14b0b9ba4cdc7c9a@mail.gmail.com>

On Sun, Nov 27, 2005 at 12:57:47PM -0600, Mohamed El Dawy wrote:
> Hi,
>  I have created this 5-liner system call, which basically opens a
> file, write "Hello World" to it, and then returns. That's all.
> 
> Now, when I actually call it, it creates the file successfully but
> writes nothing to it. The file is created and is only zero bytes. So,
> either write didn't write, or close didn't close. Any help would be
> greatly appreciated.
> ...

The following (module-) code will create and write a file from
inside a kernel. Ok -- you know -- you should not use it
without really good reasons ...

          Juergen.

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

static char filename[255];
module_param_string( filename, filename, sizeof(filename), 666 );
struct file *log_file;

static int __init mod_init(void)
{
	mm_segment_t oldfs;

	if( filename[0]=='\0' )
		strncpy( filename, "/tmp/kernel_file", sizeof(filename) );
	printk("opening filename: %s\n", filename);
	log_file = filp_open( filename, O_WRONLY | O_CREAT, 0644 );
	printk("log_file: %p\n", log_file );
	if( IS_ERR( log_file ) )
		return -EIO;

	oldfs = get_fs();
	set_fs( KERNEL_DS );
	vfs_write( log_file, "hallo\n", 6, &log_file->f_pos );
	set_fs( oldfs );
	filp_close( log_file, NULL );
	return 0;
}

static void __exit mod_exit(void)
{
}
module_init( mod_init );
module_exit( mod_exit );
MODULE_LICENSE("GPL");
/* vim:set ts=4 sw=4 ic aw: */

  reply	other threads:[~2005-11-28  7:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-27 18:57 What's wrong with this really simple function? Mohamed El Dawy
2005-11-28  7:55 ` Juergen Quade [this message]
2005-11-28 12:46   ` Nikita Danilov
2005-11-28 13:09     ` linux-os (Dick Johnson)
     [not found] <5dCs1-7zM-853@gated-at.bofh.it>
2005-11-28  1:01 ` Robert Hancock

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051128075505.GA7945@hsnr.de \
    --to=quade@hsnr.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=msdawy@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox