summaryrefslogtreecommitdiff
path: root/tool/mqtool.c
blob: e6ce706d1c72d667fcf98babcf9f93339f1dbc56 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#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;
	//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
	//print help
	int flagHelp = 0;

	//get arguments
	while ((c = getopt(argc, argv, "elp:c:s:q:h")) != -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;
	case 'h':
		flagHelp = 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)
		{
			perror("cant alloc memory");
			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);
	}
	if (flagSendQueue && flagSendStr)
	{
		size_t szStr=0;
		int mqFd = -1;
		int numRead = -1;
		int prio = 0;
		struct mq_attr mqAttr;

		mqFd = mq_open(mqNameSend, 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);
		}

		iRet = mq_send(mqFd, sStrSend, strlen(sStrSend), prio);
		if (iRet == -1)
		{
			perror("Cant send message");
			exit(1);
		}

		exit(0);
	}

	if (flagHelp)
	{
		printf("""Help: ./mqtool\n\
-l           - list all avaliable mq's\n\
-p [MQ_NAME] - show params of mq\n\
-c [MQ_NAME] - clear mq, and print all messages\n\
-s [MQ_NAME] - mq name, \"/mq-name\"\n\
-q [STR]     - string to send, combined with -s \n\
-e           - print extra info if there is\n\
-h           - print this help\n\
""");
		exit(0);
	}

}