#include #include #include "yasnd.h" pid_t ipc_clone_pid; pid_t ipc_sms_read_clone_pid=-1; // pid of thread, that checks for new incoming SMS int qid=-1; // IPC queue id 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, 0); 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); if (qbuf.mtype==SMS_SEND_TYPE && sms_send_enable) { log_debug(qbuf.mtext,DEBUG_BASE); gammu_send_sms(qbuf.mtext); } sleep(6); } } int ipc_sms_reading_loop() { // set default signal handlers signal(SIGTERM,SIG_DFL); signal(SIGHUP,SIG_DFL); // while(1) { sleep(30); /* wait 30 seconds */ //gammu_read_sms(); } } 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); // start loop, that handles new incoming SMS // unsigned char sms_child_stack[16384]; // ipc_sms_read_clone_pid=clone(ipc_sms_reading_loop,sms_child_stack+8192,CLONE_VM,NULL); // } void ipc_free() { kill(ipc_clone_pid,SIGTERM); waitpid(ipc_clone_pid,NULL,__WCLONE); // kill(ipc_sms_read_clone_pid,SIGTERM); // waitpid(ipc_sms_read_clone_pid,NULL,__WCLONE); remove_queue(qid); }