2 Copyright (c) 2014, McAfee, Inc.
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
9 Redistributions of source code must retain the above copyright notice, this list
10 of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice, this
13 list of conditions and the following disclaimer in the documentation and/or other
14 materials provided with the distribution.
16 Neither the name of McAfee, Inc. nor the names of its contributors may be used
17 to endorse or promote products derived from this software without specific prior
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef IPCSERVERHDR_H
33 #define IPCSERVERHDR_H
39 #include "IpcServer.h"
40 #include "IpcThrdPool.h"
42 #define SAFE_FREE(x) if (x) {free(x); x = NULL;}
43 #define FREE(x) if (x) {free(x);}
46 * Forward Declarations.
48 /*struct IpcServerInfo;*/
52 * Keep registered methods list
54 typedef struct _IpcServerMethodList
56 IpcServerMethod *pMethod;
57 struct _IpcServerMethodList *pNext;
58 } IpcServerMethodList;
61 * Keep the context for callback passed to stub method
63 typedef struct _IpcMethodHandle
65 IpcServerMethod *pMethod;
67 struct IpcServerInfo *pInfo;
68 char unique_id[MSGHANDLE_LEN]; //uniquely identify the running method
69 int iCancel; // 1 is cancel, 0 not cancel
70 char *cStatus; // any status
73 struct _IpcMethodHandle *pNext;
77 * Single context per server connection.
79 typedef struct _IpcServerInfo
81 char name[TSC_SERVER_NAME_LEN];
82 DBusConnection *pConn;
83 IpcServerMethodList *pMethodList; // available methods list
84 char rule[TSC_INFO_RULE_LEN];
86 bool start_server_flag;
87 DBusObjectPathVTable *pTable; //TODO: it is core dump if new thread the running method through pTable registered list
88 pthread_t lDbus_listen_thread; //listen thread on the socket for reading message
89 IpcHandlePool *pHandlePool; // thread pool for the running methods
91 IpcMethodHandle *pRunningMethods; // current running methods
92 pthread_mutex_t Lock; // mutex to manage methods - iteration, add, remove.
97 * Copy the data from the message read from the listening/reading socket thread, and pass it to the reply thread, where executing the method
99 typedef struct _IpcAsyncInfo
101 DBusConnection *pConn;
102 IpcServerInfo *pInfo;
106 IpcServerMethod *pMethod;
108 char async_unique_id[MSGHANDLE_LEN]; //uniquely identify the running method
111 int _IpcServerInit(IpcServerInfo *pServerInfo, char *szServiceName);
112 void _IpcServerDeInit(TSC_SERVER_HANDLE hServer);
113 DBusHandlerResult _IpcServerReplyMessage(DBusConnection *pConn, DBusMessage *pMsg,
114 char **pReply, int size);
115 DBusHandlerResult _IpcServerMsgFilter(DBusConnection *pConn, DBusMessage *pMsg, void *pData);
116 int _ParseDBusMessage(DBusMessage *pMsg, int *argc, char ***argv);
117 DBusHandlerResult _IpcServerMsgHandler(void *data);
118 DBusHandlerResult _IpcServerProcessMessage(void *data);
119 void *_IpcPopMessage(void *hServer);
120 void CleanupArray(char*** pArr, int len);
121 void CleanupAsync(IpcAsyncInfo *pAsync);
122 void FreeIpcServerHandle(TSC_SERVER_HANDLE hServer);
123 DBusHandlerResult _IpcServerReplyError(DBusConnection *pConn, DBusMessage *pMsg,
125 int IpcCancelMethod(void *pData, int argc, char **argv, char ***szReply, int *len, CALLBACKFUNC callback, TSC_METHOD_HANDLE *handle);
126 int IpcGetProgressMethod(void *pData, int argc, char **argv, char ***szReply, int *len, CALLBACKFUNC callback, TSC_METHOD_HANDLE *handle);
127 int IpcShutdown(void *pData, int argc, char **argv, char ***szReply, int *len, CALLBACKFUNC callback, TSC_METHOD_HANDLE *handle);
128 int IpcServerCallbackMethod(TSC_METHOD_HANDLE *pMHandle, TSC_METHOD_REASON_CODE iReason, void *reason_params);
129 int _RunDetachedThread(void *pfWorkerFunc, void *pThreadData);
132 void _FreeHandleMethods(IpcServerInfo *pServerInfo);
133 void _FreeHandlePool(IpcServerInfo *pServerInfo);
134 void _FreeHandleTable(IpcServerInfo *pServerInfo);
135 void _FreeHandleConn(IpcServerInfo *pServerInfo);
136 void _WaitForListenThreadClose(IpcServerInfo *pServerInfo);