Stan
Stan

Reputation: 11

Printk causes error in the kernel module

Description: I build an IPC kernel module. On a Ubuntu system, it can transfer IPC-data to another computer running Ubuntu. I use the modules kernel_recvmsg and kernel_sendmsg to transfer IPC-data by network.

Question: when I comment the printk message ( my printk code is printk("this is IPC-data"); ), and use kernel_sendmsg to send IPC-data to another computer the following happens.

The kernel_sendmsg API return -95 .

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

Can anyone tell me what happened and how to debug this?

My environment:

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)

Current code:

// 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;
}

Upvotes: 0

Views: 634

Answers (1)

Stan
Stan

Reputation: 11

I solved the problem by modifying the the send function.

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;
}

Upvotes: 1

Related Questions