CopyRight GPL 2004 mmc Mike Chirico
   Created: Sun Mar 14 14:31:32 EST 2004
   Updated: Sun Mar 14 17:11:19 EST 2004
   Dev home: cpearls/src/development/kernel_module/proc/readwrite

   This is a sample program that will create proc directories

   $ cat /proc/procreadwrite/count
        Test count 0  
   $ cat /proc/procreadwrite/count
        Test count 1

   $ echo -n "my test data" > /proc/procreadwrite/count 
         in the WRITE mbuf=->my test data<-

   $ cat /proc/procreadwrite/sequence 

   $ dd if=/proc/procreadwrite/sequence of=/tmp/seq count=1024
          By default dd uses block of 512 so the size will be 1024*512                           


 unsigned long copy_from_user(void *to, const void *from, 
        unsigned long count);

	Think copy from user to kernel.  You'll see this in 
        the " in read from user"



#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/tty.h>		/* For the tty declarations */
#include <linux/console.h>
#include <linux/usb.h>

MODULE_AUTHOR ("Mike Chirico");
MODULE_LICENSE ("Dual BSD/GPL");	/* gets rid of that tainting message */

#define MY_MODULE_VERSION "1.3"
#define MODULE_NAME "procreadwrite"

char rbuf[80];
static struct proc_dir_entry *e = NULL;
static struct proc_dir_entry *entry = NULL;
static struct proc_dir_entry *eroot = NULL;

static void *proc_seq_count_start (struct seq_file *s, loff_t * pos);
static void *proc_seq_count_next (struct seq_file *s, void *v, loff_t * pos);
static void proc_seq_count_stop (struct seq_file *s, void *v);
static int proc_seq_count_show (struct seq_file *s, void *v);
static int proc_seq_count_open (struct inode *inode, struct file *file);

static struct seq_operations proc_seq_count_ops = {
  .start = proc_seq_count_start,
  .next = proc_seq_count_next,
  .stop = proc_seq_count_stop,
  .show = proc_seq_count_show

static void *
proc_seq_count_start (struct seq_file *s, loff_t * pos)
  loff_t *spos = kmalloc (sizeof (loff_t), GFP_KERNEL);
  if (!spos)
    return NULL;
  *spos = *pos;
  return spos;

static void *
proc_seq_count_next (struct seq_file *s, void *v, loff_t * pos)
  loff_t *spos = (loff_t *) v;
  *pos = ++(*spos);
  return spos;

static void
proc_seq_count_stop (struct seq_file *s, void *v)
  kfree (v);

/*  This could list the sequence
 *  $cat /proc/procreadwrite/sequence
static int
proc_seq_count_show (struct seq_file *s, void *v)
  loff_t *spos = (loff_t *) v;
  seq_printf (s, "%Ld\n", *spos);
  return 0;

static int
proc_seq_count_open (struct inode *inode, struct file *file)
  return seq_open (file, &proc_seq_count_ops);

static int
proc_procreadwrite_show (struct seq_file *m, void *v)
  static int count = 0;
  char buf[80];

  snprintf (buf, 80, " Test count %d  \n", count);

  seq_puts (m, buf);
  return 0;

static int
proc_procreadwrite_open (struct inode *inode, struct file *file)
  return single_open (file, proc_procreadwrite_show, NULL);

/*  This is for writing to the tty device
 *  It's good for debugging.  

static int
mtty_write (char *text)
  struct tty_struct *my_tty;
  static int null_tty = 0;

  /* my_tty = current->tty; */	/* old  */
  my_tty = current->signal->tty; /* For kernel 2.6.6+  current->tty has been replaced with current->signal->tty */

  if (my_tty != NULL)
      ((my_tty->driver)->write) (my_tty,	/* The tty itself */
				 0,	/* We don't take the string from user space */
				 text,	/* String */
				 strlen (text));	/* Length */
      /* Currently caller will drive this

      null_tty++;		/* debug for is calling with no does nothing at the moment */

  return strlen (text);


/* write is for write to the kernel 

   echo -n "stuff" /proc/procreadwrite/count

static ssize_t
proc_procreadwrite_write (struct file *file,
			  const char * buf, size_t size, loff_t * ppos)

  size_t len = 20;
  char mbuf[21];

  if (len > size)
    len = size;

  if (copy_from_user (mbuf, buf, len))
    return -EFAULT;

  mbuf[len] = '\0';

  mtty_write ("in the WRITE mbuf=->");
  mtty_write (mbuf);
  mtty_write ("<-");
  mtty_write ("\015\012");	/* crlf */

  return len;

static struct file_operations proc_procreadwrite_operations = {
  .open = proc_procreadwrite_open,
  .read = seq_read,
  .write = proc_procreadwrite_write,
  .llseek = seq_lseek,
  .release = single_release,

static struct file_operations proc_seq_count_file_ops = {
  .owner = THIS_MODULE,
  .open = proc_seq_count_open,
  .read = seq_read,
  .llseek = seq_lseek,
  .release = seq_release

static int __init
proc_procreadwrite_init (void)
  int merr = 0;

  eroot = proc_mkdir (MODULE_NAME, NULL);
  if (eroot == NULL)
      merr = -ENOMEM;
      return merr;

  eroot->owner = THIS_MODULE;

  e = create_proc_entry ("count", 0666, eroot);	/* 0666 everyone can write to this */
  if (e)
    e->proc_fops = &proc_procreadwrite_operations;

  entry = create_proc_entry ("sequence", 0, eroot);
  if (entry)
    entry->proc_fops = &proc_seq_count_file_ops;

  return 0;

static void __exit
cleanup_procfs_procreadwrite (void)

  remove_proc_entry ("count", eroot);
  remove_proc_entry ("sequence", eroot);

  remove_proc_entry (MODULE_NAME, NULL);

  printk (KERN_INFO "%s %s removed\n", MODULE_NAME, MY_MODULE_VERSION);

module_init (proc_procreadwrite_init);
module_exit (cleanup_procfs_procreadwrite);

MODULE_DESCRIPTION ("procfs examples procreadwrite");


Linux System Admin Tips: There are over 200 Linux tips and tricks in this article. That is over 100 pages covering everything from NTP, setting up 2 IP address on one NIC, sharing directories among several users, putting running jobs in the background, find out who is doing what on your system by examining open sockets and the ps command, how to watch a file, how to prevent even root from deleting a file, tape commands, setting up cron jobs, using rsync, using screen conveniently with emacs, how to kill every process for a user, security tips and a lot more. These tip grow weekly. The above link will download the text version for easy grep searching. There is also an html version here.

Breaking Firewalls with OpenSSH and PuTTY: If the system administrator deliberately filters out all traffic except port 22 (ssh), to a single server, it is very likely that you can still gain access other computers behind the firewall. This article shows how remote Linux and Windows users can gain access to firewalled samba, mail, and http servers. In essence, it shows how openSSH and Putty can be used as a VPN solution for your home or workplace.

MySQL Tips and Tricks: Find out who is doing what in MySQL and how to kill the process, create binary log files, connect, create and select with Perl and Java, remove duplicates in a table with the index command, rollback and how to apply, merging several tables into one, updating foreign keys, monitor port 3306 with the tcpdump command, creating a C API, complex selects, and much more.

Create a Live Linux CD - BusyBox and OpenSSH Included: These steps will show you how to create a functioning Linux system, with the latest 2.6 kernel compiled from source, and how to integrate the BusyBox utilities including the installation of DHCP. Plus, how to compile in the OpenSSH package on this CD based system. On system boot-up a filesystem will be created and the contents from the CD will be uncompressed and completely loaded into RAM -- the CD could be removed at this point for boot-up on a second computer. The remaining functioning system will have full ssh capabilities. You can take over any PC assuming, of course, you have configured the kernel with the appropriate drivers and the PC can boot from a CD. This tutorial steps you through the whole processes.

SQLite Tutorial : This article explores the power and simplicity of sqlite3, first by starting with common commands and triggers, then the attach statement with the union operation is introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data is demonstrated, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.

The Lemon Parser Tutorial: This article explains how to build grammars and programs using the lemon parser, which is faster than yacc. And, unlike yacc, it is thread safe.

How to Compile the 2.6 kernel for Red Hat 9 and 8.0 and get Fedora Updates: This is a step by step tutorial on how to compile the 2.6 kernel from source.

Virtual Filesystem: Building A Linux Filesystem From An Ordinary File. You can take a disk file, format it as ext2, ext3, or reiser filesystem and then mount it, just like a physical drive. Yes, it then possible to read and write files to this newly mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem, and mount it with ACL (Access Control Lists), which give you rights beyond the traditional read (r) write (w) and execute (x) for the 3 user groups file, owner and other.

Working With Time: What? There are 61 seconds in a minute? We can go back in time? We still tell time by the sun?

Chirico img Mike Chirico, a father of triplets (all girls) lives outside of Philadelphia, PA, USA. He has worked with Linux since 1996, has a Masters in Computer Science and Mathematics from Villanova University, and has worked in computer-related jobs from Wall Street to the University of Pennsylvania. His hero is Paul Erdos, a brilliant number theorist who was known for his open collaboration with others.

Mike's notes page is souptonuts. For open source consulting needs, please send an email to All consulting work must include a donation to Logo Logo