Revert "Remove TPCS and TWPServer features"
[platform/upstream/csf-framework.git] / framework / IpcStructs.c
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 /**
33  * \file IpcStructs.c
34  * \brief Source File for structures needed by Ipc.
35  *  
36  * This file implements the structures needed by IPC Client, used by Security framework.
37  */
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #include "IpcStructs.h"
44
45 /**
46  * Assign new fields to existing call handle.
47  * A NULL pPendingCall will not replace the existing one.
48  */
49 int _AssignToClientCallHandle(ClientCallHandle *pHandle, const char *szPrefix, dbus_uint32_t serial,
50                               DBusPendingCall *pPendingCall)
51 {
52     int iRet = 1;
53
54     do
55     {
56         if (!pHandle)
57             break;
58
59         if (0 > (iRet = snprintf(pHandle->idUnique, MSGHANDLE_LEN, TSC_MID_SVR_FORMAT, szPrefix,
60                                  serial)))
61             break;
62
63         if (pPendingCall)
64             pHandle->pPendingCall = pPendingCall;
65
66         iRet = 0;
67
68     } while(0);
69
70     return iRet;
71 }
72
73 int _CreateClientCallHandle(ClientCallHandle **ppHandle, const char *szPrefix, dbus_uint32_t serial,
74                             DBusPendingCall *pPendingCall)
75 {
76     int iRet = 1;
77     ClientCallHandle *pHandle = NULL;
78
79     do
80     {
81         if (!ppHandle)
82             break;
83
84         pHandle = (ClientCallHandle *) malloc(sizeof(ClientCallHandle));
85         if (!pHandle)
86             break;
87
88         if ((iRet = _AssignToClientCallHandle(pHandle, szPrefix, serial, pPendingCall)))
89             break;
90
91         *ppHandle = pHandle;
92         iRet = 0;
93
94     } while(0);
95
96     if (iRet && pHandle)
97     {
98         free(pHandle);
99         pHandle = NULL;
100     }
101
102     return iRet;
103 }
104
105 void _FreeClientCallHandle(ClientCallHandle *pHandle)
106 {
107     free(pHandle);
108 }
109
110 void _FreeSharedData(SharedData *pSharedData)
111 {
112     if (pSharedData)
113     {
114         pthread_mutex_destroy(&pSharedData->lock);
115         pthread_cond_destroy(&pSharedData->cond);
116         free(pSharedData);
117     }
118 }
119
120 // Create the handle to monitor send completion and receive sent message details.
121 // Maintains only reference of DBusMessage; Ownership is not changed.
122 SharedData *_CreateSharedData(const char *szCallHandlePrefix, DBusMessage *pMsg)
123 {
124     int iDone = 0;
125     SharedData *pSharedData = NULL;
126
127     do
128     {
129         if (!pMsg)
130             break;
131
132         if ((pSharedData = (SharedData *)calloc(1, sizeof(SharedData))) == NULL)
133             break;
134
135         if (0 > snprintf(pSharedData->szCallHandlePrefix, MSGHANDLE_LEN, "%s", szCallHandlePrefix))
136             break;
137
138         pSharedData->pMsg = pMsg;
139         pSharedData->pCallHandle = NULL;
140         pSharedData->iSent = 0;
141
142         if (pthread_mutex_init(&(pSharedData->lock), NULL))
143             break;
144
145         if (pthread_cond_init(&(pSharedData->cond), NULL))
146             break;
147
148         iDone = 1;
149     } while(0);
150
151     if (!iDone)
152     {
153         _FreeSharedData(pSharedData);
154         return NULL;
155     }
156     return pSharedData;
157 }
158
159 ThreadData *_AllocThreadData(DBusConnection *pConn, int timeout_milliseconds,
160                              SharedData *pSharedData, TSCCallback pCallback, void *pPrivate)
161 {
162     ThreadData *pThreadData = (ThreadData *)dbus_malloc(sizeof(ThreadData));
163     if (!pThreadData)
164         return NULL;
165
166     pThreadData->pConn = pConn;
167     pThreadData->timeout_milliseconds = timeout_milliseconds;
168     pThreadData->pSharedData = pSharedData;
169     pThreadData->pCallBack = pCallback;
170     pThreadData->pPrivate = pPrivate;
171
172     return pThreadData;
173 }
174
175 void _FreeThreadData(void *pThreadData)
176 {
177     if (pThreadData)
178     {
179         _FreeSharedData(((ThreadData*)pThreadData)->pSharedData);
180     }
181     dbus_free(pThreadData);
182 }
183
184
185 #include "IpcStructs.h"