linux - printk导致内核模块出错

标签 linux ubuntu kernel linux-device-driver kernel-module

描述:我构建了一个IPC内核模块。在 Ubuntu 系统上,它可以将 IPC 数据传输到另一台运行 Ubuntu 的计算机。我使用模块 kernel_recvmsgkernel_sendmsg 通过网络传输 IPC 数据。

问题:当我评论printk 消息时(我的printk 代码是 printk("this is IPC-data"); ),并使用 kernel_sendmsg 将 IPC 数据发送到另一台计算机,发生以下情况。

kernel_sendmsg API 返回 -95。

#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */

谁能告诉我发生了什么以及如何调试

我的环境:

Ubuntu 12.04.3 LTS    
Linux ubuntu 3.9.0-030900-generic #201304291257 SMP Mon Apr 29 17:06:25 UTC
2013 i686 i686 i386 GNU/Linux

gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

当前代码:

// Save get_service transaction,
// when the service is in the remote,
// send this transaction to the remote

        if( target_proc->pid==smg_pid && tr->code!=3)
        {

            proc->temp_t.data.reply= reply;         
            proc->temp_t.data.ref_add= 0;
            if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) {
                binder_user_error("binder: %d:%d got transaction with "
                    "invalid offsets size, %zd\n",
                    proc->pid, thread->pid, tr->offsets_size);
                return_error = BR_FAILED_REPLY;
                goto err_temp_copy_data_failed;
            }

            proc->temp_t.tr=*tr;

            if (copy_from_user(tr_data, tr->data.ptr.buffer, tr->data_size)) {
                binder_user_error("binder: %d:%d got transaction with invalid "
                    "data ptr\n", proc->pid, thread->pid);
                return_error = BR_FAILED_REPLY;
                goto err_temp_copy_data_failed;
            }
            if (copy_from_user(tr_off, tr->data.ptr.offsets, tr->offsets_size)) {
                binder_user_error("binder: %d:%d got transaction with invalid "
                    "data ptr\n", proc->pid, thread->pid);
                return_error = BR_FAILED_REPLY;
                goto err_temp_copy_data_failed;
            }
            proc->temp_t.tr.data.ptr.buffer = tr_data;          
            proc->temp_t.tr.data.ptr.offsets = tr_off;
            binder_debug(BINDER_DEBUG_SOCKET, "proc->temp_t set tr.data_size=%d tr.offsets_size=%d", proc->temp_t.tr.data_size,proc->temp_t.tr.offsets_size);
        }


        if((proc->for_ip && tr->code==3 && target_proc->pid==smg_pid)|| (target_proc->ip && proc->pid!=smg_pid))
        {       

            binder_debug(SOCKET_DEBUG_REQ, "this is ipc data");

//              sock_t=kzalloc(sizeof(*sock_t), GFP_KERNEL);
            sock_t.reply= reply;


            if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) {
                binder_user_error("binder: %d:%d got transaction with "
                    "invalid offsets size, %zd\n",
                    proc->pid, thread->pid, tr->offsets_size);
                return_error = BR_FAILED_REPLY;
                goto err_temp_copy_data_failed;
            }



            if(target_proc->ip)
                success=send_add_t(proc,&sock_t,tr,target_proc->ip);
            else                
                success=send_add_t(proc,&sock_t,tr,proc->for_ip);

//              kfree(sock_t);
            if(success<0){
                return_error = BR_FAILED_REPLY;
                goto err_send_remote_failed;                
            }
            target_proc->rcomplete=1;           

        }

int send_add_t(struct binder_proc *proc,struct tr_ref *rref,struct binder_transaction_data *tr ,int tget_ip)
{
    int count;
    struct remote_tr remote_tr;

            remote_tr.fops = add_t_remote;
            remote_tr.pid=proc->pid;
            remote_tr.rref=*rref;
            remote_tr.tr=*tr;

            count=send(newsock[tget_ip],&remote_tr,sizeof(remote_tr));


            binder_debug(SOCKET_DEBUG_REQ, "Send tr.data_size=%d tr.offsets_size=%d count=%d", tr->data_size,tr->offsets_size,count);
            count=send(newsock[tget_ip],(void *) tr->data.ptr.buffer,tr->data_size);
            send(newsock[tget_ip],(void *) tr->data.ptr.offsets,tr->offsets_size);
            proc->r_has=tget_ip;
//              do_gettimeofday(&tv2);
//              binder_debug(SOCKET_TIME,"can recv time =%ld.%ldus\n",tv2.tv_sec,tv2.tv_usec);
//              binder_debug(SOCKET_TIME,"recv to send  finish=%ldus",(tv2.tv_sec-tv3.tv_sec)*1000000L+tv2.tv_usec-tv3.tv_usec);
//              binder_debug(SOCKET_DEBUG_REQ, "Send tr->code=%d count=%d", tr->code,count);
        return count;

}

int send(struct socket* send,void* buf, int len)
{
        struct msghdr msg;
        struct kvec iov={
        .iov_base = buf,
            .iov_len = len,

        };

        int size = 0;
        if(!send)
            return 0;
            size = kernel_sendmsg(send,&msg,&iov,1,len);
        return size;
}

最佳答案

我通过修改发送函数解决了这个问题。

struct msghdr msg= {.msg_flags= MSG_DONTWAIT+MSG_NOSIGNAL};

int send(struct socket* send,void* buf, int len)
{
    struct msghdr msg= {.msg_flags= MSG_DONTWAIT+MSG_NOSIGNAL};
    struct kvec iov={
            .iov_base = buf,
            .iov_len = len,     
        };
   int size = 0;

        if(!send)
            return 0;
            size = kernel_sendmsg(send,&msg,&iov,1,len);

        return size;
}

关于linux - printk导致内核模块出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26193389/

相关文章:

Linux 内核引导在 Uncompressing Linux 处停止...好的,引导内核

bash - 重新启动期间未捕获 SIGTERM

c - Linux 内核调试打印输出?

c - 使用 NETLINK 的 VLAN 信息

linux - 关于进程的pid

linux - 带有 sudo 的 Ansible adhoc 命令

c++ - Visual Studio 跨平台生成文件项目,找不到命令

java - cassandra 重新安装在 ubuntu 上不起作用

c - 在 gcc 命令中添加 linux 头文件

linux - 直到循环与 if 语句