Re: cannot release a UDP kernel socket - why ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi,
i think there are two issues,one thanks Mulyadi reminds me to use
kthread_should_stop,
One is i think your socket should use non-block mode,reference the
http://mail.nl.linux.org/kernelnewbies/2005-12/msg00193.html
maybe block mode makes the kthread uninterrupted. we need check the sock codes.
So i change your code to below,i test it works.
I hope it is what you expect.

BRs

Lin

#include <linux/version.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kmod.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <linux/proc_fs.h>
#include <linux/compiler.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/byteorder/generic.h>
#include <linux/inet.h>
#include <linux/udp.h>
#include <linux/netdevice.h>
#include <linux/time.h>
#include <linux/spinlock.h>
#include <net/ip.h>
#include <net/checksum.h>
#include <linux/vmalloc.h>
#include <linux/net.h>
#include <linux/in_route.h>
#include <net/route.h>
#include <linux/kthread.h>
static struct socket *udpInserversocket=NULL;
static struct task_struct *p;

static int createUdpInSocket(void)
{
       int res;
       struct sockaddr_in server;
       int servererror;
       unsigned char *buf;
       printk( KERN_ERR "createUdpInSocket\n" );
	
      if( sock_create( PF_INET,SOCK_DGRAM,IPPROTO_UDP,&udpInserversocket)<0 ) {
               printk(KERN_ERR "server: Error creating udpserversocket.\n" );
               return -EIO;
       }
       server.sin_family      = AF_INET;
       server.sin_addr.s_addr = INADDR_ANY;
       server.sin_port = htons((unsigned short)4000);
       servererror = udpInserversocket->ops->bind(udpInserversocket,
                       (struct sockaddr *) &server, sizeof(server) );
               {
               struct msghdr msg;
               struct iovec iov;
               mm_segment_t oldfs;
               int size = 0;
               int len = 160;
               iov.iov_base = buf;
           //    msg.msg_flags = 0;
               msg.msg_flags = MSG_DONTWAIT;
               msg.msg_name = &server;
               msg.msg_namelen  = sizeof(struct sockaddr_in);
               msg.msg_control = NULL;
               msg.msg_controllen = 0;
               msg.msg_iov = &iov;
               msg.msg_iovlen = 1;
               msg.msg_control = NULL;

               oldfs = get_fs();
               set_fs(KERNEL_DS);
               printk("before sock_recvmsg in %s %s\n",__FUNCTION__,__FILE__);
		while(0 == kthread_should_stop()){
		/*do something else you need*/
               	size = sock_recvmsg(udpInserversocket,&msg,len,msg.msg_flags);
	 	}

               printk("after sock_recvmsg in %s %s\n",__FUNCTION__,__FILE__);
               set_fs(oldfs);
               }

release:
	 if (udpInserversocket) {
              //udpInserversocket->ops->shutdown(udpInserversocket,2);
               sock_release(udpInserversocket);
               udpInserversocket=NULL;
       }


}


static int kernThread(void *arg)
{
       allow_signal(SIGKILL);

 createUdpInSocket();

       return 1;
}


static int __init nic_init(void)
       {
       int ret;
       void *kdata;
       p = kthread_run(kernThread,kdata,"myKernThread");

       return 0;
       }

static void __exit nic_exit(void)
       {
       printk("in nic_exit\n");

      if (p)
         kthread_stop(p);
    	printk("stop kthread!\n");
 	}

module_init(nic_init)
module_exit(nic_exit)

MODULE_DESCRIPTION("test");
MODULE_LICENSE("GPL");

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux