81 lines
1.5 KiB
C
81 lines
1.5 KiB
C
|
#include <sys/ipc.h>
|
||
|
#include <sys/msg.h>
|
||
|
|
||
|
#include "yasnd.h"
|
||
|
|
||
|
pid_t ipc_clone_pid;
|
||
|
|
||
|
int open_queue()
|
||
|
{
|
||
|
key_t keyval=ftok(".",'m');
|
||
|
int qid;
|
||
|
if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
|
||
|
{
|
||
|
log_event("Error opening/creating IPC message queue");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
return qid;
|
||
|
}
|
||
|
|
||
|
int send_message(int qid, mymsgbuf* qbuf)
|
||
|
{
|
||
|
int result;
|
||
|
/* The length is essentially the size of the structure minus sizeof(mtype) */
|
||
|
int length = sizeof(mymsgbuf) - sizeof(long);
|
||
|
|
||
|
if((result = msgsnd( qid, qbuf, length, 0)) == -1)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
int read_message(int qid, mymsgbuf* qbuf, long type)
|
||
|
{
|
||
|
int result;
|
||
|
int length = sizeof(mymsgbuf) - sizeof(long);
|
||
|
if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
int remove_queue( int qid )
|
||
|
{
|
||
|
if( msgctl(qid, IPC_RMID, 0) == -1)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int ipc_queue_loop()
|
||
|
{
|
||
|
// set default signal handlers
|
||
|
signal(SIGTERM,SIG_DFL);
|
||
|
signal(SIGHUP,SIG_DFL);
|
||
|
//
|
||
|
mymsgbuf qbuf;
|
||
|
while(1)
|
||
|
{
|
||
|
int result=read_message(qid, &qbuf, 1);
|
||
|
if (result==-1)
|
||
|
{
|
||
|
log_event("Error: finishing IPC loop process...");
|
||
|
return 1;
|
||
|
}
|
||
|
char dbg_txt[150];
|
||
|
sprintf(dbg_txt,"Read IPC message - Type: %ld Text: %s", qbuf.mtype, qbuf.mtext);
|
||
|
log_debug(dbg_txt,DEBUG_ALL);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ipc_init()
|
||
|
{
|
||
|
qid = open_queue();
|
||
|
unsigned char ipc_child_stack[16384];
|
||
|
ipc_clone_pid=clone(ipc_queue_loop,ipc_child_stack+8192,CLONE_VM,NULL);
|
||
|
}
|