#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEVMQPATH "/dev/mqueue/" #define AGNIPATH_IN "agni-%d-in" #define AGNIPATH_OUT "agni-%d-out" //------------------------------------------------------------------------------ int main(int argc, char **argv) { //generic variables int c; int i,j; int iRet; //any func return param //for directory listing DIR *dirPath; struct dirent *entryPath; //list all mqs int flagListAll = 0; //show mq param int flagMqParam = 0; char* mqName = NULL; //clear message queue int flagClear = 0; char* mqNameClear = NULL; //clear print extra info if there is int flagExtra = 0; //send string to queue int flagSendQueue = 0; char *mqNameSend = NULL; //mq name where to send //argument value that to send into queue int flagSendStr = 0; char *sStrSend = NULL; //mq string that should be push'ed in queue //get arguments while ((c = getopt(argc, argv, "elp:c:s:q:")) != -1) switch(c) { //list all mqueue's case 'l': flagListAll = 1; break; //show param of mq case 'p': flagMqParam = 1; mqName = optarg; break; //get all messages, print out and clean queue case 'c': flagClear = 1; mqNameClear = optarg; break; //send string to queue case 's': flagSendQueue = 1; mqNameSend = optarg; break; case 'q': flagSendStr = 1; sStrSend = optarg; break; //show extra information if avaliable case 'e': flagExtra = 1; break; default: break; } //---------------------------------------------------------------------- //main logic //list all if (flagListAll) { dirPath = opendir(DEVMQPATH); if (dirPath != NULL) { while (entryPath = readdir(dirPath)) { int id; if (entryPath->d_type == DT_REG) { if(sscanf(entryPath->d_name, "agni-%d", &id) == 1) { printf("%s\n",entryPath->d_name); } } //should free entryPath? maybe } closedir(dirPath); } else { perror("Cant open directory"); } exit(0); } //get mqueue params if (flagMqParam) { struct mq_attr mqAttr; int mqFd = -1; mqFd = mq_open(mqName, O_RDWR, 0666, NULL); if (mqFd != -1) { iRet = mq_getattr(mqFd, &mqAttr); if (iRet != -1) { printf("FLAGS %8ld, ", mqAttr.mq_flags); printf("MSG SIZE %8ld, ", mqAttr.mq_msgsize); printf("CUR MSGS %ld/%ld\n", mqAttr.mq_curmsgs,mqAttr.mq_maxmsg); } } else { printf("Cant open %s\n", mqName); } exit(0); } //clear mq from messages if (flagClear) { struct mq_attr mqAttr; int mqFd = -1; char *cBuf = NULL; int numRead = -1; int prio = 0; int iCountMsg = 0; mqFd = mq_open(mqNameClear, O_RDWR, 0666, NULL); if (mqFd == -1) { printf("Cant open %s\n", mqName); exit(1); } iRet = mq_getattr(mqFd, &mqAttr); if (iRet == -1) { perror("Cant get attributes"); exit(1); } cBuf = malloc(mqAttr.mq_msgsize); if (cBuf == NULL) { exit(1); } for (i=0; i