1 /* @(#) $Id: ./src/shared/mq_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
15 #include "os_net/os_net.h"
20 /* StartMQ v0.2, 2004/07/30
21 * Start the Message Queue. type: WRITE||READ
23 int StartMQ(char * path, short int type)
28 return(OS_BindUnixDomain(path, 0660, OS_MAXSTR + 512));
31 /* We give up to 21 seconds for the other end to
37 if(File_DateofChange(path) < 0)
40 if(File_DateofChange(path) < 0)
43 if(File_DateofChange(path) < 0)
45 merror(QUEUE_ERROR, __local_name, path, "Queue not found");
47 if(File_DateofChange(path) < 0)
55 /* Wait up to 3 seconds to connect to the unix domain.
56 * After three errors, exit.
58 if((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0)
61 if((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0)
64 if((rc = OS_ConnectUnixDomain(path, OS_MAXSTR + 256)) < 0)
66 merror(QUEUE_ERROR, __local_name, path,
73 debug1(MSG_SOCKET_SIZE, __local_name, OS_getsocketsize(rc));
79 /* SendMSG v0.1, 2005/02/15
80 * Send a message to the queue.
82 int SendMSG(int queue, char *message, char *locmsg, char loc)
85 char tmpstr[OS_MAXSTR+1];
87 tmpstr[OS_MAXSTR] = '\0';
90 /* Checking for global locks */
101 merror(FORMAT_ERROR, __local_name);
105 message++; /* Pointing now to the location */
107 if(strncmp(message, "keepalive",9) == 0)
112 snprintf(tmpstr,OS_MAXSTR,"%c:%s->%s",loc, locmsg, message);
115 snprintf(tmpstr,OS_MAXSTR,"%c:%s:%s",loc,locmsg,message);
118 /* queue not available */
123 /* We attempt 5 times to send the message if
124 * the receiver socket is busy.
125 * After the first error, we wait 1 second.
126 * After the second error, we wait more 3 seconds.
127 * After the third error, we wait 5 seconds.
128 * After the fourth error, we wait 10 seconds.
129 * If we failed again, the message is not going
130 * to be delivered and an error is sent back.
132 if((__mq_rcode = OS_SendUnix(queue, tmpstr,0)) < 0)
134 /* Error on the socket */
135 if(__mq_rcode == OS_SOCKTERR)
137 merror("%s: socketerr (not available).", __local_name);
144 /* Unable to send. Socket busy */
146 if(OS_SendUnix(queue, tmpstr, 0) < 0)
148 /* When the socket is to busy, we may get some
149 * error here. Just sleep 2 second and try
153 /* merror("%s: socket busy", __local_name); */
154 if(OS_SendUnix(queue, tmpstr,0) < 0)
157 merror("%s: socket busy ..", __local_name);
158 if(OS_SendUnix(queue, tmpstr,0) < 0)
161 merror("%s: socket busy ..", __local_name);
162 if(OS_SendUnix(queue, tmpstr,0) < 0)
164 /* Message is going to be lost
165 * if the application does not care
166 * about checking the error