LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-21-2005, 10:47 AM   #1
cipicip
Member
 
Registered: Oct 2005
Location: Montreal
Distribution: Ubuntu, Kubuntu, Kamikaze
Posts: 38

Rep: Reputation: 15
Question sockets usage in kernel modules


Hi guys!

I got a question for you related to the linux kernel modules. I was trying to build a module which would implement a server socket. For this I used an echo server example found on the net. Bellow is the code. And yes, you guessed right: it doesn’t compile. The server code compiles and works fine if compiled as a regular application.

Anyone has any idea why not? Can anyone help me?

Thanks


Code:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/stdlib.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/string.h>
#include <linux/signal.h>
#include <linux/sys/wait.h>
#include <linux/sys/socket.h>
#include <linux/resolv.h>
#include <linux/arpa/inet.h>

void PANIC(char* msg);
#define PANIC(msg)  { printk(KERN_INFO msg); exit(-1); }

/*--------------------------------------------------------------------*/
/*--- Signal capture: SIGCHLD                                      ---*/
/*--------------------------------------------------------------------*/
void sig_handler(int sig)
{
    if ( sig == SIGCHLD )
    {   int retval;
        wait(&retval);
    }
}

/*--------------------------------------------------------------------*/
/*--- Child - echo servlet                                         ---*/
/*--------------------------------------------------------------------*/
void Child(void* arg)
{   char line[100];
    int bytes_read;
    int client = *(int *)arg;

    do
    {
        bytes_read = recv(client, line, sizeof(line), 0);
		if ( bytes_read < 0 )
			printk(KERN_INFO "Read socket");
        send(client, line, bytes_read, 0);
    }
    while (strncmp(line, "bye\r", 4) != 0  ||  bytes_read < 0 );
    close(client);
    exit(0);
}


static int __init mod_init(void) {

int sd;
struct sigaction act;
struct sockaddr_in addr;

    console_print("module loaded!\n");
    printk(KERN_INFO "module loaded!\n");

    bzero(&act, sizeof(act));
	act.sa_handler = sig_handler;
	act.sa_flags = SA_NOCLDSTOP;
    sigaction(SIGCHLD, &act, 0);
    if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
        PANIC("Socket");
    addr.sin_family = AF_INET;
    addr.sin_port = htons(9999);
    addr.sin_addr.s_addr = INADDR_ANY;
    if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
        PANIC("Bind");
    if ( listen(sd, 20) != 0 )
        PANIC("Listen");
    while (1)
    {   int client, addr_size = sizeof(addr);

        client = accept(sd, (struct sockaddr*)&addr, &addr_size);
		if ( client < 0 )
			printk(KERN_INFO "Accept");
		else
		{
	        printk(KERN_INFO "Connected: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
    	    if ( fork() )
        	    close(client);
	        else
    	    {
        	    close(sd);
            	Child(&client);
	            exit(0);
    	    }
		}
    }
    return 0;
}

static void __exit mod_exit(void) {
    console_print("module unloaded!\n");
}

module_init(mod_init);
module_exit(mod_exit);
MODULE_LICENSE("GPL");
 
Old 12-22-2005, 03:11 AM   #2
cipicip
Member
 
Registered: Oct 2005
Location: Montreal
Distribution: Ubuntu, Kubuntu, Kamikaze
Posts: 38

Original Poster
Rep: Reputation: 15
The Makefile content is this:

Code:
obj-m    := listen.o

KDIR    := /lib/modules/$(shell uname -r)/build
PWD    := /home/shared/module

default:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
 
Old 12-22-2005, 08:54 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
First off, a few of those includes are wrong or just don't exist.

Fixed include list:
Code:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/string.h>
#include <linux/signal.h>
#include <linux/wait.h>
#include <linux/socket.h>
Once you fix that you are going to have to re-write the parts that use sockaddr_in and such to use the correct kernel space structures (because things like struct sockaddr_in don't live in kernel space).

Also, when you compile this you have to make sure your CFLAGS are set to something like "-c -D__KERNEL__ -DMODULE".
 
Old 12-22-2005, 09:10 AM   #4
cipicip
Member
 
Registered: Oct 2005
Location: Montreal
Distribution: Ubuntu, Kubuntu, Kamikaze
Posts: 38

Original Poster
Rep: Reputation: 15
Thank you very much, Jtshaw.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what are kernel sockets? bhuvan007 Linux - Networking 5 12-24-2005 03:35 AM
Raw sockets in standard OpenSuSE 10.0 kernel? klas_a Suse/Novell 1 12-12-2005 09:13 AM
Kernel module using netlink sockets arunka Programming 1 03-31-2005 07:42 AM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
sockets in linux 2.4.2 kernel arumash Linux - Networking 1 06-29-2002 09:21 PM


All times are GMT -5. The time now is 11:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration