summaryrefslogtreecommitdiff
path: root/tool/mqtool.c
blob: 61e78aff9c3f9771639dfa37021884886cf9ae3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#define _GNU_SOURCE

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/syscall.h>
#include <sched.h>
#include <stdatomic.h>
#include <sys/types.h>
#include <dirent.h>

#include <sys/stat.h>
#include <sched.h>
#include <mqueue.h>

#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;


	//get arguments
	while ((c = getopt(argc, argv, "elp:c:")) != -1)
	switch(c)
	{
	case 'l':
		flagListAll = 1;
		break;
	case 'p':
		flagMqParam = 1;
		mqName = optarg;
		break;
	case 'c':
		flagClear = 1;
		mqNameClear = optarg;
		break;
	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<mqAttr.mq_curmsgs; i++)
		{
			numRead = mq_receive(mqFd, cBuf, mqAttr.mq_msgsize, &prio);
			if (numRead == -1)
			{
				free(cBuf);
				exit(0);
			}
			iCountMsg += 1;

			//extra output of message value
			if (flagExtra)
			{
				for (j=0; j<numRead; j++)
				{
					printf("%c",cBuf[j]);
				}
				printf("\n");
			}
		}

		free(cBuf);

		printf("Readed %d messages from %ld\n", iCountMsg, mqAttr.mq_curmsgs);


		exit(0);
	}

}