[gl-como] kernel echo

Pietro Bertera gl-como@lists.linux.it
Tue, 11 Mar 2003 10:14:25 +0100


This is a multi-part message in MIME format.
--------------090901020905030900010403
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

eggià anche il kernel parla,

per chi interessa allego il codice di un semplice modulino che registra 
un netdevice e stampa delle stringhe (da kernel space) ricevetue dallo 
user land

Utilità: meno di zero, serve solo per chi vuole capire come dialogare a 
runtime con il kernel.

utilizzo:

compilare il modulo:
#gcc -Wall  -c kecho_mod.c -I/usr/src/linux/include

(-I/usr/src/linux/include non è detto che ci voglia, dipende da quali 
sorgenti avete installato)

compilare l'interfaccia in user space:
#gcc kecho.c -o kecho

installare il modulo:
#insmod kecho_mod.o

chiaccherare con il kernel:
# ./kecho weilaaa

il kernel vi rispondera' con voce da oltretomba ;) :
Sento una voce dallo UserSpace: weilaaa

(per vedere le risposte dal kernel space non dovete essere in X)

ma ciao

--------------090901020905030900010403
Content-Type: text/x-csrc;
 name="kecho.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="kecho.c"

/*
 *					Bertera Pietro 
 *		e-mail: p.bertera@valtellinux.it dr.iggy@iol.it
 *
 *					compile with:
 * gcc kechoctrl.c -o kechoctrl
 *
 *					run with:
 * kechoctrl [string]
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU 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 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h> 
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <asm/types.h>

#include "kecho_mod.h"

int main(int argc, char **argv)
{
    struct my_userinfo info;
    char *ifname = "kecho";
    struct ifreq req;
    int i, sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);

    if (sock < 0) {
        fprintf(stderr, "%s: socket():\n");
        exit(1);
    }

    strcpy(req.ifr_name, ifname);
    req.ifr_data = (caddr_t)&info;
	
	info.string=argv[1];

	if (ioctl(sock, PRINT, &req)<0) {
	    fprintf(stderr, "%s: ioctl():\n");
	    exit(1);
	}
	return 0;
}
--------------090901020905030900010403
Content-Type: text/x-csrc;
 name="kecho_mod.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="kecho_mod.c"

/*
 *					Bertera Pietro 
 *		e-mail: p.bertera@valtellinux.it dr.iggy@iol.it
 *
 *					compile with:
 * gcc -Wall  -c kecho.c -I/usr/src/linux/include
 *
 *					run with:
 * insmod kecho.o
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU 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 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.
 */
 
#ifndef __KERNEL__
#  define __KERNEL__
#endif
#ifndef MODULE
#  define MODULE
#endif

#include <linux/module.h>
#include <linux/kernel.h> 
#include <linux/types.h>  

#include <linux/netdevice.h>   
#include <linux/etherdevice.h> 

#include <asm/uaccess.h>
#include "kecho_mod.h"

MODULE_LICENSE("GPL");

int my_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	struct my_userinfo info,
    *uptr = (struct my_userinfo *)ifr->ifr_data;
   

    if (!suser()) return -EPERM;

   
    if (!access_ok(VERIFY_READ, uptr, sizeof(*uptr)))
        return -EFAULT;
    copy_from_user(&info, uptr, sizeof(info));

    switch(cmd) {
      case PRINT: 
	printk("Sento una voce dallo UserSpace: %s\n", info.string);

	    return 0;

      case REPRINT: 
		return 0;

      default:
        return -ENOTTY;
    }
    return 0;
}

int my_init_dev(struct net_device *dev)
{
    dev->do_ioctl = my_ioctl;
    return 0;
}

struct net_device my_dev = {
    name: "kecho",
    init: my_init_dev,
}; 

void cleanup_module(void)
{
        unregister_netdev(&my_dev);
}

int init_module(void)
{
   int result;    

    result = register_netdev(&my_dev);
    if (result) {
        printk(KERN_ERR "kecho: can't register netdevice");
        cleanup_module();
        return result;
    }
    
    return 0; 
}
--------------090901020905030900010403
Content-Type: text/x-chdr;
 name="kecho_mod.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="kecho_mod.h"

#ifndef __MYDRV_H__
#define __MYDRV_H__

#define PRINT SIOCDEVPRIVATE
#define REPRINT (SIOCDEVPRIVATE+1)

struct my_userinfo {
    char* string;          
};

#endif
--------------090901020905030900010403--