1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "os_net/os_net.h"
16 /* Start the Message Queue. type: WRITE||READ */
17 int StartMQ(const char *path, short int type)
20 return (OS_BindUnixDomain(path, 0660, OS_MAXSTR + 512));
23 /* We give up to 21 seconds for the other end to start */
26 if (File_DateofChange(path) < 0) {
28 if (File_DateofChange(path) < 0) {
30 if (File_DateofChange(path) < 0) {
32 if (File_DateofChange(path) < 0) {
33 merror(QUEUE_ERROR, __local_name, path, "Queue not found");
40 /* Wait up to 3 seconds to connect to the unix domain.
41 * After three errors, exit.
43 if ((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0) {
45 if ((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0) {
47 if ((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0) {
48 merror(QUEUE_ERROR, __local_name, path,
55 debug1(MSG_SOCKET_SIZE, __local_name, OS_getsocketsize(rc));
60 /* Send a message to the queue */
61 int SendMSG(int queue, const char *message, const char *locmsg, char loc)
64 char tmpstr[OS_MAXSTR + 1];
66 tmpstr[OS_MAXSTR] = '\0';
68 /* Check for global locks */
71 if (loc == SECURE_MQ) {
75 if (message[0] != ':') {
76 merror(FORMAT_ERROR, __local_name);
79 message++; /* Pointing now to the location */
81 if (strncmp(message, "keepalive", 9) == 0) {
85 snprintf(tmpstr, OS_MAXSTR, "%c:%s->%s", loc, locmsg, message);
87 snprintf(tmpstr, OS_MAXSTR, "%c:%s:%s", loc, locmsg, message);
90 /* Queue not available */
95 /* We attempt 5 times to send the message if
96 * the receiver socket is busy.
97 * After the first error, we wait 1 second.
98 * After the second error, we wait more 3 seconds.
99 * After the third error, we wait 5 seconds.
100 * After the fourth error, we wait 10 seconds.
101 * If we failed again, the message is not going
102 * to be delivered and an error is sent back.
104 if ((__mq_rcode = OS_SendUnix(queue, tmpstr, 0)) < 0) {
105 /* Error on the socket */
106 if (__mq_rcode == OS_SOCKTERR) {
107 merror("%s: socketerr (not available).", __local_name);
112 /* Unable to send. Socket busy */
114 if (OS_SendUnix(queue, tmpstr, 0) < 0) {
115 /* When the socket is to busy, we may get some
116 * error here. Just sleep 2 second and try
120 /* merror("%s: socket busy", __local_name); */
121 if (OS_SendUnix(queue, tmpstr, 0) < 0) {
123 merror("%s: socket busy ..", __local_name);
124 if (OS_SendUnix(queue, tmpstr, 0) < 0) {
126 merror("%s: socket busy ..", __local_name);
127 if (OS_SendUnix(queue, tmpstr, 0) < 0) {
128 /* Message is going to be lost
129 * if the application does not care
130 * about checking the error