?? lc_msg_q.c
字號:
/* * Copyright (C) 2002 Ricardo Arroyo <ricardo.arroyo@eresmas.net> * * This code may be used under the terms of Version 2 of the GPL, * read the file COPYING for details. * */#include <stdlib.h>#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <errno.h>#include "lc_syslog.h"#include "lc_debug.h"#include "lc_msg_q.h"/*----------------------------------------------------------------------------*//* FUNCTION : *//* CREATE QUEUE iCreateQmsg(key_t QKey); *//* .- returns queue ide (id_c) or -1 if error *//* .- calls iOpenQmsg, followed by a call to iDelQmsg *//* and finally calls iOpenQmsg again. This sequence assures *//* the queue is totally empty *//*----------------------------------------------------------------------------*/int iCreateQmsg(key_t QKey){ int id_c; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iCreateQmsg,lQKey=%ld\n",(long)QKey); } if ((id_c=msgget(QKey,0666 | IPC_CREAT)) == -1) { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iCreateQmsg(),msgget error, errno=%d\n",errno); } } else if (iDelQmsg(id_c) == -1) { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iCreateQmsg(),error in iDelQmsg(id_c=%d)\n",id_c); } id_c = -1; } else if ((id_c=msgget(QKey,0666 | IPC_CREAT)) == -1) { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iCreateQmsg(),msgget error, errno=%d\n",errno); } } else { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iCreateQmsg(),return=%d\n",id_c); } } return(id_c);}/*----------------------------------------------------------------------------*//* FUNCTION : *//* OPEN Queue iOpenQmsg(key_t QKey); *//* .- Returns the integer value of the message queue identifier *//* asociated to iQKey (queue key) used for queue access *//* *//* .- returns -1 in error *//* *//* *//*----------------------------------------------------------------------------*/int iOpenQmsg( key_t QKey){ int id_c; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iOpenQmsg(QKey=%d)\n",QKey); } id_c = msgget(QKey,0666); if (iDebug & DEP_MSGQ) { fprintf(stderr,"iOpenQmsg() QKey=%d,return=%d,error=%d\n", QKey, id_c, (id_c == -1 ? errno : 0)); } return(id_c);}/*----------------------------------------------------------------------------*//* *//* FUNCTION : *//* DELETE QUEUE iDelQmsg(id_c); *//* *//* .- returns 0 if queue is deleted, -1 if not *//* .- id_c is the queue identifier returned by iOpenQmsg *//* *//*----------------------------------------------------------------------------*/int iDelQmsg(int id_c){ int res; struct msqid_ds buff; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iDelQmsg(id_c=%d)\n",id_c); } if (id_c == 0) { fprintf(stderr,"iDelQmsg(id_c=%d). Queue not deleted\n",id_c); res = 0; } else if ((msgctl(id_c,IPC_STAT,&buff) == -1) && (errno == EINVAL)) { res = 0; /* queue not exist, not need to be deleted */ if (iDebug & DEP_MSGQ) { fprintf(stderr,"iDelQmsg() queue %d not exist\n",id_c); } } else { res = msgctl(id_c,IPC_RMID,NULL); } if (iDebug & DEP_MSGQ) { fprintf(stderr,"iDelQmsg(),return=%d,error=%d\n",res, (res == -1 ? errno : 0)); } return(res);}/*----------------------------------------------------------------------------*//* FUNCTION : *//* SEND MSG NO FREE MEMORY iSend2QnoFree(id_c,buf,iMsgSize) *//* .- Returns 0 = OK *//* -1 = error *//* *//* .- id_c is the identifier returned by iOpenQmsg *//* *//* .- buf is a pointer to a memory area that contains the message *//* to send *//* .- iMsgSize is the length of the message (mtext array) *//* .- not deallocates buf memory *//* *//*----------------------------------------------------------------------------*/int iSend2QnoFree(int id_c, void *buf, int iMsgSize){ int res; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iSend2QnoFree(id_c=%d,buf=%ld),MsgSize=%d\n",id_c,(long)buf,iMsgSize); } res = msgsnd(id_c, buf, iMsgSize - sizeof(long) ,0); if (iDebug & DEP_MSGQ) { fprintf(stderr,"iSend2QnoFree(),return=%d,error=%d\n",res, (res == -1 ? errno : 0)); } return(res);}/*----------------------------------------------------------------------------*//* FUNCTION : *//* SEND MSG iSend2Queue (id_c,buf,iMsgSize); *//* .- Returns 0 = OK *//* -1 = error *//* *//* .- id_c is the identifier returned by iOpenQmsg *//* *//* .- buf is a pointer to a memory area that contains the message *//* to send *//* *//* .- iMsgSize is the length of the message (mtext array) *//* *//*----------------------------------------------------------------------------*/int iSend2Queue(int id_c, void *buf, int iMsgSize){ int res; res = iSend2QnoFree(id_c, buf, iMsgSize); if (res == 0) { free (buf); } return(res);}/*----------------------------------------------------------------------------*//* FUNCTION : *//* READ QUEUE TYPE MSG pRecQueueType(id_c, lTypeMsg) *//* .- Returns a pointer to a msgbuf buffer with the receive *//* message, if error NULL is returned *//* *//* .- id_c is the identifier returned by iOpenQmsg *//* .- lTypeMsg is the message type to be read *//* *//*----------------------------------------------------------------------------*/struct msgbuf* pRecQueueType(int id_c, long lTypeMsg){ int res; struct msgbuf *buf; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueType(id_c=%d, lTypeMsg=%ld)\n",id_c,lTypeMsg); } if ((buf=(struct msgbuf *)malloc(MAX_BUF + sizeof(long))) == NULL) { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueType(), malloc error\n"); } vLC_syslog(LCLOG_ERR_MALLOC,"iRecQueueType"); } else if ((res=msgrcv(id_c,buf,MAX_BUF,lTypeMsg,0)) == -1)// msgrcv uses the id_c queue identifier returned by iOpenQmsg// the 2nd. parameter is a pointer to a msgbuf struct, where msgrcv// will put all received data// 3rd. parameter is the max_len of buffer buf// 4th. parameter selects the type of message to retrieve// 0 first message in queue// +n first message of msg_type "n"// -n receive the first message with msg_type of lower absolute value// less than or equal to "n"// 5th parameter controls the behavior of function// 0 normal behavior: wait for message and error if message// is longer than buf max_len// MSG_NOERROR truncate message if not fits in buf, an return// without error// IPC_NOWAIT not wait for message and return inmediatly// with errno = ENOMSG { free(buf); buf = NULL; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueType(),return=%d,error=%d\n",(int)buf,errno); } } else if ((buf=(struct msgbuf *)realloc(buf,sizeof(long)+res)) == NULL) { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueType(), realloc error\n"); } vLC_syslog(LCLOG_ERR_REALLOC,"iRecQueueType"); } else { // msg read and memory buffer reallocated if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueType(),return=%d,error=%d\n",(int)buf,0); } } return(buf);}/*----------------------------------------------------------------------------*//* FUNCTION : *//* READ MSG pRecQueue(id_c); *//* .- Returns a pointer to a msgbuf buffer with the receive *//* message *//* *//* .- id_c is the identifier returned by iOpenQmsg *//* *//*----------------------------------------------------------------------------*/struct msgbuf* pRecQueue(int id_c){ return(pRecQueueType(id_c, -MSG_MAXTYPE));}/*----------------------------------------------------------------------------*//* FUNCTION : *//* READ QUEUE NO WAIT pRecQueueNwait(id_c, lTypeMsg) *//* .- Returns a pointer to a msgbuf buffer with the receive *//* message, if errur NULL is returned *//* *//* .- id_c is the identifier returned by iOpenQmsg *//* .- lTypeMsg is the message type to be read *//* *//*----------------------------------------------------------------------------*/struct msgbuf* pRecQueueNwait(int id_c, long lTypeMsg){ int res; struct msgbuf *buf; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueNwait(id_c=%d, lTypeMsg=%ld)\n",id_c,lTypeMsg); } if ((buf=(struct msgbuf *)malloc(MAX_BUF + sizeof(long))) == NULL) { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueNwait(), malloc error\n"); } vLC_syslog(LCLOG_ERR_MALLOC,"iRecQueueNwait"); } else if ((res=msgrcv(id_c,buf,MAX_BUF,lTypeMsg,IPC_NOWAIT)) == -1)// if res == -1 and errno == ENOMSG, no message is in queue { free(buf); buf = NULL; if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueNwait(),return=%d,error=%d\n",(int)buf,errno); } } else if ((buf=(struct msgbuf *)realloc(buf,sizeof(long)+res)) == NULL) { if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueNwait(), realloc error\n"); } vLC_syslog(LCLOG_ERR_REALLOC,"iRecQueueNWait"); } else { // msg read and memory buffer reallocated if (iDebug & DEP_MSGQ) { fprintf(stderr,"iRecQueueNwait(),return=%d,error=%d\n",(int)buf,0); } } return(buf);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -