writting kernel modules on redhat 7.3 linux kernel 2.4.18-3

writting kernel modules on redhat 7.3 linux kernel 2.4.18-3

Post by Hemanshu Kanji Bhadra, Noid » Thu, 20 Mar 2003 09:20:11


I want to write a simple kernel module, this is the following 2 files i used
when i try to compile 2nd file i get many errors in proc_fs.h

any help will be appreciated,

thanks in advance.
- hemanshu

 * $Id: hello.c,v 1.10 2001/07/17 10:30:02 rubini Exp $
#define MODULE
#include <linux/module.h>

 * These lines, although not shown in the book,          
 * are needed to make hello.c run properly even when      
 * your kernel has version support enabled                

int init_module(void)      { printk("<1>Hello, world\n"); return 0; }
void cleanup_module(void)  { printk("<1>Goodbye cruel world\n"); }

however if i using the following code for writing /proc modules I get many
compile errors.

have a look at the code :-

 * helloworld_proc_module v1.1 3/11/03
 * www.embeddedlinuxinterfacing.com
 * The original location of this code is
 * http://www.embeddedlinuxinterfacing.com/chapters/07/
 * helloworld_proc_module.c
 * Copyright (C) 2001 by Craig Hollabaugh
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

/* v1.1 changed the variable name buffer to page in proc_write function,
 * use of term buffer was confusing to students.

 * helloworld_proc_module.c is based on procfs_example.c by Erik Mouw.
 * For more information, please see The Linux Kernel Procfs Guide,
 * http://kernelnewbies.org/documents/kdoc/procfs-guide/lkprocfsguide.html

/* helloworld_proc_module
 * helloworld_proc_module demonstrates the use of a /proc directory entry.
 * The init function, init_helloworld, creates /proc/helloworld and
 * populates its data, read_proc, write_proc and owner fields. The exit
 * function, cleanup_helloworld, removes the /proc/helloworld entry.
 * The proc_read function, proc_read_helloworld, is called whenever
 * a file read operation occurs on /proc/helloworld. The
 * proc_write function, proc_write_helloworld, is called whenever a file
 * file write operation occurs on /proc/helloworld.
 * To demonstrate read and write operations, this module uses data
 * structure called helloworld_data containing a char field called value.
 * Read and write operations on /proc/helloworld manipulate
 * helloworld_data->value. The init function sets value = 'Default'.

gcc -O2 -D__KERNEL__ -DMODULE -I/usr/src/linux/include -c
helloworld_proc_module.c -o helloworld_proc_module.o

arm-linux-gcc -O2 -D__KERNEL__ -DMODULE -I/usr/src/arm-linux/include -c
helloworld_proc_module.c -o /tftpboot/arm-rootfs/helloworld_proc_module.o

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>

#define MODULE_VERSION "1.0"
#define MODULE_NAME "helloworld proc module"

/* this is how long our data->value char array can be */
#define HW_LEN 8

struct helloworld_data_t {
  char value[HW_LEN + 1];


static struct proc_dir_entry *helloworld_file;

struct helloworld_data_t helloworld_data;

/* proc_read - proc_read_helloworld
 * proc_read_helloworld is the callback function that the kernel calls when
 * there's a read file operation on the /proc file (for example,
 * cat /proc/helloworld). The file's data pointer (&helloworld_data) is
 * passed in the data parameter. You first cast it to the helloworld_data_t
 * structure. This proc_read function then uses the sprintf function to
 * create a string that is pointed to by the page pointer. The function then
 * returns the length of page. Because helloworld_data->value is set to
 * "Default", the command cat /proc/helloworld should return
 * helloworld Default
static int proc_read_helloworld(char *page, char **start, off_t off,
                                int count, int *eof, void *data)
  int len;

/* cast the void pointer of data to helloworld_data_t*/
  struct helloworld_data_t *helloworld_data=(struct helloworld_data_t

/* use sprintf to fill the page array with a string */
  len = sprintf(page, "helloworld %s\n", helloworld_data->value);

  return len;


/* proc_write - proc_write_helloworld
 * proc_write_helloworld is the callback function that the kernel calls
 * when there's a write file operation on the /proc file, (for example,
 * echo test > /proc/helloworld). The file's data pointer
 * (&helloworld_data) is passed in the data parameter. You first cast it to
 * the helloworld_data_t structure. The page parameter points to the
 * incoming data. You use the copy_from_user function to copy the page
 * contents to the data->value field. Before you do that, though, you check
 * the page length, which is stored in count to ensure that you don't
 * overrun the length of data->value. This function then returns the length
 * of the data copied.
static int proc_write_helloworld(struct file *file, const char *page,
                                 unsigned long count, void *data)
  int len;

/* cast the void pointer of data to helloworld_data_t*/
  struct helloworld_data_t *helloworld_data=(struct helloworld_data_t

/* do a range checking, don't overflow buffers in kernel modules */
  if(count > HW_LEN)
    len = HW_LEN;
    len = count;

/* use the copy_from_user function to copy page data to
 * to our helloworld_data->value */
  if(copy_from_user(helloworld_data->value, page, len)) {
    return -EFAULT;

/* zero terminate helloworld_data->value */
  helloworld_data->value[len] = '\0';

  return len;


/* init - init_helloworld
 * init_helloworld creates the /proc/helloworld entry file and obtains its
 * pointer called helloworld_file. The helloworld_file fields, data,
 * read_proc, write_proc and owner, are filled.  init_helloworld completes
 * by writing an entry to the system log using printk.
static int __init init_helloworld(void)
  int rv = 0;

/* Create the proc entry and make it readable and writeable by all - 0666 */
  helloworld_file = create_proc_entry("helloworld", 0666, NULL);
  if(helloworld_file == NULL) {
    return -ENOMEM;

/* set the default value of our data to Default. This way a read operation
 * /proc/helloworld will return something. */
  strcpy(helloworld_data.value, "Default");

/* Set helloworld_file fields */
  helloworld_file->data = &helloworld_data;
  helloworld_file->read_proc = &proc_read_helloworld;
  helloworld_file->write_proc = &proc_write_helloworld;
  helloworld_file->owner = THIS_MODULE;

/* everything initialized */
  printk(KERN_INFO "%s %s initialized\n",MODULE_NAME, MODULE_VERSION);
  return 0;


/* exit - cleanup_helloworld
 * cleanup_helloworld removes the /proc file entry helloworld and
 * prints a message to the system log.
static void __exit cleanup_helloworld(void)
  remove_proc_entry("helloworld", NULL);

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


/* here are the compiler macros for module operation */

MODULE_AUTHOR("Craig Hollabaugh");
MODULE_DESCRIPTION("helloworld proc module");


To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


writting kernel modules on redhat 7.3 linux kernel 2.4.18-3

Post by Arjan van de Ve » Thu, 20 Mar 2003 10:20:07

> gcc -O2 -D__KERNEL__ -DMODULE -I/usr/src/linux/include -c
> helloworld_proc_module.c -o helloworld_proc_module.o

You are using the glibc headers to compile kernel modules, that's not
going to work. You need more than that; you could check the makefile at
http://people.redhat.com/arjanv/xircom_cb/Makefile for more info.

< 1K Download


1. Scheduler issues with 2.4.18-3 (Redhat 7.3) kernel

I'm having a problem porting code that runs correctly on a 2.4.18-3 kernel
to newer kernels.  My code consists of several processes running in SCHED_RR
policy mode with a priority of 1, so each of them is getting about 20% of
the CPU. With Redhat 7.3 when the application is run, it detaches the
processes correctly, and gets me back to the command prompt in my terminal
window where my system continues to behave normally.  When I run top it
shows that my processes are the highest priority and are getting most of the

However, when I run this application on any other kernel than 2.4.18-3
running the application results in the entire system locking up, presumably
because my processes are now consuming 100% of the CPU.

If anyone can give me some ideas as to what I can do to get a stock
kernel.org kernel patched to behave like the Redhat 7.3 kernel it would be
much appreciated.  I have already tried adding the low latency patch to no

Thanks in advance,

2. DOSEMU boot files: ext .EMU work???

3. Redhat 7.3 ,2.4.18-3 SMP Kernel High %CPU Utilization Problem

4. PPP PROBLEMS!!! Please help!

5. RedHat 7.3 (2.4.18-3) and APM

6. Application Development System for Accounting/Invoicing System?

7. Promise TX2 driver for RedHat kernel above 2.4.18-3?

8. PCMCIA E-NET and Diskette Drive for 850

9. gcc 2.96 to compile redhat kernel 2.4.18-3?

10. Sure-fire wireless PCMCIA card for RH 7.3 (k=2.4.18-3)?

11. ext3 corruption using IDE in RH 7.3 (2.4.18-3)

12. Changes between Linux kernel 2.2.5-15 and 2.4.18-3???

13. PROBLEM: I/O ports allocation bug in Linux kernel 2.4.18-3