Add license file
[platform/upstream/csf-framework.git] / framework / IpcServerHdr.h
1 /*
2     Copyright (c) 2014, McAfee, Inc.
3
4     All rights reserved.
5
6     Redistribution and use in source and binary forms, with or without modification,
7     are permitted provided that the following conditions are met:
8
9     Redistributions of source code must retain the above copyright notice, this list
10     of conditions and the following disclaimer.
11
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.
15
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
18     written permission.
19
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.
30 */
31
32 #ifndef IPCSERVERHDR_H
33 #define IPCSERVERHDR_H
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #include "IpcServer.h"
40 #include "IpcThrdPool.h"
41
42 #define SAFE_FREE(x)    if (x) {free(x); x = NULL;}
43 #define FREE(x)         if (x) {free(x);}
44
45 /**
46  * Forward Declarations.
47  */
48 /*struct IpcServerInfo;*/
49 typedef struct _IpcServerInfo IpcServerInfo;
50
51 /**
52  * Keep registered methods list
53  */
54 typedef struct _IpcServerMethodList
55 {
56     IpcServerMethod *pMethod;
57     struct _IpcServerMethodList *pNext;
58 } IpcServerMethodList;
59
60 /**
61  * Keep the context for callback passed to stub method
62  */
63 typedef struct _IpcMethodHandle
64 {
65     IpcServerMethod *pMethod;
66     void *pData;
67     IpcServerInfo *pInfo;
68     char unique_id[MSGHANDLE_LEN + 1]; //uniquely identify the running method
69     int iCancel;  // 1 is cancel, 0 not cancel
70     char *cStatus;    // any status
71     pthread_mutex_t Lock;
72
73     struct _IpcMethodHandle *pNext;
74 } IpcMethodHandle;
75
76 /**
77  *  Single context per server connection.
78  */
79 struct _IpcServerInfo
80 {
81     char name[TSC_SERVER_NAME_LEN + 1];
82     DBusConnection *pConn;
83     IpcServerMethodList *pMethodList;   // available methods list
84     char rule[TSC_INFO_RULE_LEN];
85     int fd;
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
90     int count;
91     IpcMethodHandle *pRunningMethods;  // current running methods
92     pthread_mutex_t Lock;   // mutex to manage methods - iteration, add, remove.
93 };
94
95
96 /**
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
98  */
99 typedef struct _IpcAsyncInfo
100 {
101     DBusConnection *pConn;
102     IpcServerInfo *pInfo;
103     DBusMessage *pMsg;
104     int argc;
105     char **argv;
106     IpcServerMethod *pMethod;
107     IpcHandles *pHandle;
108     char async_unique_id[MSGHANDLE_LEN + 1]; //uniquely identify the running method
109 } IpcAsyncInfo;
110
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,
124                                        int iErrCode);
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);
130
131
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);
137
138 #ifdef __cplusplus
139 }
140 #endif
141
142 #endif
143