内核模块在某个语句后无法执行语句

时间:2014-03-20 08:44:40

标签: c linux-kernel kernel-module

我根据自己的需要修改了“kernelnewbies.com的简单UDP服务器”。它应该监听主机的端口2325和5555。代码

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kthread.h>

#include <linux/errno.h>
#include <linux/types.h>

#include <linux/netdevice.h>
#include <linux/ip.h>
#include <linux/in.h>

#include <linux/delay.h>

#define DEFAULT_PORT 2325
#define CONNECT_PORT 23
#define LOCAL_PORT 5555
#define MODULE_NAME "ksocket"
#define INADDR_SEND INADDR_LOOPBACK
#define INADDR_LOCAL INADDR_LOOPBACK

struct kthread_t {
    struct task_struct *thread;
    struct socket *sock;
    struct sockaddr_in addr;
    struct socket *sock_send;
    struct sockaddr_in addr_send;
    struct socket *sock_local;
    struct sockaddr_in addr_local;
    int running;
};

struct kthread_t *kthread = NULL;

int ksocket_receive(struct socket *sock, struct sockaddr_in *addr, unsigned char *buf,  int len);
int ksocket_send(struct socket *sock, struct sockaddr_in *addr, unsigned char *buf, int len);

static void ksocket_start(void)
{
    int size, err,i;
    int bufsize = 100;
    unsigned char ip[16];
    unsigned char buf[bufsize+1];
struct mutex mutex_ks;

mutex_init(&mutex_ks);
    /* kernel thread initialization */
    mutex_lock(&mutex_ks);
    kthread->running = 1;
    current->flags |= PF_NOFREEZE;

/* daemonize (take care with signals, after daemonize() they are disabled) */
    allow_signal(SIGKILL);
mutex_unlock(&mutex_ks);

/* create a socket */
    if ( ( (err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &kthread->sock)) < 0) ||
         ( (err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &kthread->sock_send)) < 0 ) ||
     ( (err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &kthread->sock_local))< 0 ) )
    {
            printk(KERN_INFO MODULE_NAME": Could not create a datagram socket, error = %d\n", -ENXIO);
            goto out;
    }

memset(&kthread->addr, 0, sizeof(struct sockaddr));
    memset(&kthread->addr_send, 0, sizeof(struct sockaddr));
    kthread->addr.sin_family      = AF_INET;
    kthread->addr_send.sin_family = AF_INET;
kthread->addr_local.sin_family= AF_INET;

kthread->addr.sin_addr.s_addr      = htonl(INADDR_LOCAL);
   // kthread->addr_send.sin_addr.s_addr = htonl(INADDR_SEND);
kthread->addr_local.sin_addr.s_addr= htonl(INADDR_LOCAL);

kthread->addr.sin_port      = htons(DEFAULT_PORT);
    //kthread->addr_send.sin_port = htons(CONNECT_PORT);
kthread->addr_local.sin_port= htons(LOCAL_PORT);

if ( ( (err = kthread->sock->ops->bind(kthread->sock, (struct sockaddr *)&kthread->addr, sizeof(struct sockaddr) ) ) < 0) ||
         //( (err = kthread->sock_send->ops->connect(kthread->sock_send, (struct sockaddr *)&kthread->addr_send, sizeof(struct sockaddr), 0) ) < 0 ) ||
     ( (err = kthread->sock_local->ops->bind(kthread->sock_local, (struct sockaddr *)&kthread->addr_local, sizeof(struct sockaddr) ) ) < 0 ) )
    {
            printk(KERN_INFO MODULE_NAME": Could not bind to socket, error = %d\n", -err);
            goto close_and_out;
    }

printk(KERN_INFO MODULE_NAME": listening on port %d\n", DEFAULT_PORT);

printk(KERN_INFO MODULE_NAME": Hello");

/* main loop */
    for (;;)
    {
    again :
            memset(&buf, 0, bufsize+1);
            size = ksocket_receive(kthread->sock_local, &kthread->addr, buf, bufsize);

            if (signal_pending(current))
                    break;

            if (size < 0)
                    printk(KERN_INFO MODULE_NAME": error getting datagram, sock_recvmsg error = %d\n", size);
            else 
            {
                    printk(KERN_INFO MODULE_NAME": received %d bytes\n", size);
        if (buf[0] == 'I' && buf[1] == 'P') {
            i = 0;
            memset(&ip,0 ,16);
            while(buf[i+3] != '\0'){
                ip[i] = buf[i+3];
                i++;
            }

            printk(KERN_INFO MODULE_NAME": Got IP %s",ip);
            goto close_and_out;
            kthread->addr_send.sin_addr.s_addr = htonl((long int)ip);
            kthread->addr_send.sin_port = htons(CONNECT_PORT);
            if ( (err = kthread->sock_send->ops->connect(kthread->sock_send, (struct sockaddr *)&kthread->addr_send, sizeof(struct sockaddr), 0) ) < 0 ) {
                printk(KERN_INFO MODULE_NAME": Could not connect to socket, error = %d\n", -err);
                goto again;
            }

        }
                    /* data processing */
                //    printk("\n data: %s\n", buf);

                    /* sending */
                  //  memset(&buf, 0, bufsize+1);

                  //  strcat(buf, "testing...");
                  //  ksocket_send(kthread->sock_send, &kthread->addr_send, buf, bufsize + 1);
            } 
    }

close_and_out:
    sock_release(kthread->sock);
    sock_release(kthread->sock_send);
    kthread->sock = NULL;
    kthread->sock_send = NULL;

out:
    kthread->thread = NULL;
    kthread->running = 0;
}

int ksocket_send(struct socket *sock, struct sockaddr_in *addr, unsigned char *buf, int len)
{
    struct msghdr msg;
    struct iovec iov;
    mm_segment_t oldfs;
    int size = 0;

    if (sock->sk==NULL)
       return 0;

    iov.iov_base = buf;
    iov.iov_len = len;

    msg.msg_flags = 0;
    msg.msg_name = addr;
    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);
    size = sock_sendmsg(sock,&msg,len);
    set_fs(oldfs);

    return size;
}

int ksocket_receive(struct socket* sock, struct sockaddr_in* addr, unsigned char* buf, int len)
{
    struct msghdr msg;
    struct iovec iov;
    mm_segment_t oldfs;
    int size = 0;

    if (sock->sk==NULL) return 0;

    iov.iov_base = buf;
    iov.iov_len = len;

    msg.msg_flags = 0;
    msg.msg_name = addr;
    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);
    size = sock_recvmsg(sock,&msg,len,msg.msg_flags);
    set_fs(oldfs);

    return size;
}

int __init ksocket_init(void)
{
    kthread = kmalloc(sizeof(struct kthread_t), GFP_KERNEL);
    memset(kthread, 0, sizeof(struct kthread_t));

    /* start kernel thread */
    kthread->thread = kthread_run((void *)ksocket_start, NULL, MODULE_NAME);
    if (IS_ERR(kthread->thread)) 
    {
            printk(KERN_INFO MODULE_NAME": unable to start kernel thread\n");
            kfree(kthread);
            kthread = NULL;
            return -ENOMEM;
    }

    return 0;
}

void __exit ksocket_exit(void)
{
    int err;
struct mutex mutex_ks;

mutex_init(&mutex_ks);
    if (kthread->thread==NULL)
            printk(KERN_INFO MODULE_NAME": no kernel thread to kill\n");
    else 
    {
            mutex_lock(&mutex_ks);
            err = kthread_stop(kthread->thread);
            mutex_unlock(&mutex_ks);

            /* wait for kernel thread to die */
            if (err < 0)
                    printk(KERN_INFO MODULE_NAME": unknown error %d while trying to terminate kernel thread\n",-err);
            else 
            {
                    while (kthread->running == 1)
                            msleep_interruptible(10);
                    printk(KERN_INFO MODULE_NAME": succesfully killed kernel thread!\n");
            }
    }

    /* free allocated resources before exit */
    if (kthread->sock != NULL) 
    {
            sock_release(kthread->sock);
            kthread->sock = NULL;
    }

    kfree(kthread);
    kthread = NULL;

    printk(KERN_INFO MODULE_NAME": module unloaded\n");
 }

/* init and cleanup functions */
module_init(ksocket_init);
module_exit(ksocket_exit);

/* module information */
MODULE_DESCRIPTION("test");

现在打印

printk(KERN_INFO MODULE_NAME": listening on port %d\n", DEFAULT_PORT);

在日志中。但不打印

printk(KERN_INFO MODULE_NAME": Hello");

为什么呢?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

&#34;当内核输出部分消息时(通过将字符串传递给不以换行符结尾的printk()),日志记录系统将缓冲文本,直到消息的其余部分到达。 &#34; - source

因此,在其他人记录换行符之前,您不会看到任何日志。

您应该使用换行符结束消息:": Hello\n"