Skip to contentSkip to conversation

Enabling message queue in 2.6.10 kernel

DISCUSSION Chinmaya Mishra 2 participants 2 messages Jul 7, 2006

lore
Share & tools
lore
#0 lore raw

Hi All,
I am trying for message queue in linux-2.6.10 kernel.
I export sys_msgget(), sys_msgctl(), sys_msgsnd() and sys_msgrcv() in the kernel with following EXPORT_SYMBOL(sys_msgget) and so on. Then compile the kernel, boot with the new image.

My code is like this.
#include <linux/module.h>
#include <linux/kernel.h>

#include <linux/types.h>
#include <linux/ipc.h>
#include <linux/msg.h>
#include <linux/syscalls.h>

#include <asm/system.h>

int msqid, ret;
/* this is the thread function that we are executing */ void mymsgSend(void)

{
       struct msgbuf sbuf;
       printk("Start send message \n");
       memset(&sbuf, '\0', sizeof(sbuf));
       if((msqid = sys_msgget((key_t)1234, 0666 | IPC_CREAT )) < 0) {
               printk("Err: sys_msgget \n");
               return ;
       }
       sbuf.mtype = 1;
       //sprintf(sbuf.mtext,"a");
       ret = sys_msgsnd(msqid, &sbuf, sizeof(sbuf), IPC_NOWAIT);
       printk("Send ret = %d \n", ret);
       if (ret < 0) {
               printk("Err: sys_msgsnd \n");
               return ;
       }
       return ;
}

void mymsgRecv(void)

{
       int msqid;
       struct msgbuf rbuf;
       printk("Start recv message  \n");
       memset(&rbuf, '\0', sizeof(rbuf));
       if ((msqid = sys_msgget((key_t)1234, 0666)) < 0) {
               printk("Err: sys_msgget \n");
               return ;
       }
       rbuf.mtype = 1;
       ret = sys_msgrcv(msqid, (struct msgbuf *)&rbuf, sizeof(rbuf), 
rbuf.mtype, IPC_NOWAIT);
       printk("Recv ret = %d \n", ret);
       if (ret < 0) {
               printk("Err: sys_msgrcv \n");
               return ;
       }
       printk("Mesg Recv = %s \n", rbuf.mtext);
       return;
}

/* load the module */
int init_module(void)

{
             mymsgSend();
             msleep(1000);
             mymsgRecv();
       }
       return(0);
}

/* remove the module */
void cleanup_module(void)

{
       printk("Stop Kernel Thread \n");
       sys_msgctl(msqid, IPC_RMID, NULL);
       return;
}

But the out put messages in # dmesg <enter> is like this

Start send message
Send ret = -14
Err: sys_msgsnd
Start recv message
Recv ret = -42
Err: sys_msgrcv
Stop Kernel Thread

Is there any thing I am missing. Please help soon.

With Regards,
Chinmaya.

#1 lore raw
Quoted 13 lines

But the out put messages in # dmesg <enter> is like this

Start send message
Send ret = -14
Err: sys_msgsnd
Start recv message
Recv ret = -42
Err: sys_msgrcv
Stop Kernel Thread

Quoted 1 line

Is there any thing I am missing. Please help soon.

you're missing that you're calling functions which expect a userspace pointer, but with a kernel space pointer. You really shouldn't do that, and in fact it doesn't work the way you did it. (you'd need to call set_fs() and friends first, but if you're not really careful you open security holes that way)

Can you explain why you'd want to do this from kernel space in the first place? It's not unlikely that it's the wrong approach....

Greetings,
Arjan van de Ven