3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 2) as published by the FSF - Free Software
14 #include "os_net/os_net.h"
19 /* StartMQ v0.2, 2004/07/30
20 * Start the Message Queue. type: WRITE||READ
22 int StartMQ(char * path, short int type)
27 return(OS_BindUnixDomain(path, 0660, OS_MAXSTR + 512));
30 /* We give up to 21 seconds for the other end to
36 if(File_DateofChange(path) < 0)
39 if(File_DateofChange(path) < 0)
42 if(File_DateofChange(path) < 0)
44 merror(QUEUE_ERROR, __local_name, path, "Queue not found");
46 if(File_DateofChange(path) < 0)
54 /* Wait up to 3 seconds to connect to the unix domain.
55 * After three errors, exit.
57 if((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0)
60 if((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0)
63 if((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0)
65 merror(QUEUE_ERROR, __local_name, path,
72 debug1(MSG_SOCKET_SIZE, __local_name, OS_getsocketsize(rc));
78 /* SendMSG v0.1, 2005/02/15
79 * Send a message to the queue.
81 int SendMSG(int queue, char *message, char *locmsg, char loc)
84 char tmpstr[OS_MAXSTR+1];
86 tmpstr[OS_MAXSTR] = '\0';
89 /* Checking for global locks */
100 merror(FORMAT_ERROR, __local_name);
104 message++; /* Pointing now to the location */
106 snprintf(tmpstr,OS_MAXSTR,"%c:%s->%s",loc, locmsg, message);
109 snprintf(tmpstr,OS_MAXSTR,"%c:%s:%s",loc,locmsg,message);
112 /* queue not available */
117 /* We attempt 5 times to send the message if
118 * the receiver socket is busy.
119 * After the first error, we wait 1 second.
120 * After the second error, we wait more 3 seconds.
121 * After the third error, we wait 5 seconds.
122 * After the fourth error, we wait 10 seconds.
123 * If we failed again, the message is not going
124 * to be delivered and an error is sent back.
126 if((__mq_rcode = OS_SendUnix(queue, tmpstr,0)) < 0)
128 /* Error on the socket */
129 if(__mq_rcode == OS_SOCKTERR)
131 merror("%s: socketerr (not available).", __local_name);
138 /* Unable to send. Socket busy */
140 if(OS_SendUnix(queue, tmpstr, 0) < 0)
142 /* When the socket is to busy, we may get some
143 * error here. Just sleep 2 second and try
147 /* merror("%s: socket busy", __local_name); */
148 if(OS_SendUnix(queue, tmpstr,0) < 0)
151 merror("%s: socket busy ..", __local_name);
152 if(OS_SendUnix(queue, tmpstr,0) < 0)
155 merror("%s: socket busy ..", __local_name);
156 if(OS_SendUnix(queue, tmpstr,0) < 0)
158 /* Message is going to be lost
159 * if the application does not care
160 * about checking the error