HelloWorld

xiaoxiao2021-03-06  44

hello_world drive module / * * 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 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. . See the GNU * 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 #include #include #include #include

#define module_version "1.0" #define module_name "HelloWorld Proc Module"

/ * this is howling 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 *) Data;

/ * 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 do not * 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 *) data;

/ * Do a Range Checking, Don't overflow buffers in kernel modules * / if (count> hw) len = hw_len; else len = count

/ * Use the copy_from_user function to copy Page Data to * to yUR 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 -ENMEM;

/ * set the default value of our data to default. This Way a read Operation on * / 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_init (Init_HelloWorld); Module_exit (Cleanup_helloWorld);

Module_author ("Craig Hollabaugh"); Module_Description ("HelloWorld Proc Module"); Module_License ("GPL");

EXPORT_NO_SYMBOLS;

转载请注明原文地址:https://www.9cbs.com/read-63117.html

New Post(0)