8cd7a4f13f1e79b08061715ec87fca8529f75d59
[platform/framework/native/channel-service.git] / src / IpcServer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file  FIo_IpcServer.cpp
20  * @brief This is the implementation file for the IpcServer class.
21  *
22  */
23
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cstring>
27 #include <cerrno>
28 #include <iostream>
29 #include <new>
30
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/un.h>
36
37 #include <sys/smack.h>
38
39 #include <ipc/ipc_message.h>
40
41 #include <app_manager.h>
42
43 #include <FBaseRtMutex.h>
44 #include <FBaseSysLog.h>
45 #include <FBase_StringConverter.h>
46 #include <FBaseRt_EventDispatcher.h>
47
48 #include "IIpcServerEventListener.h"
49 #include "IpcServer.h"
50
51 using namespace std;
52 using namespace IPC;
53
54 using namespace Tizen::Base;
55 using namespace Tizen::Base::Runtime;
56 using namespace Tizen::App;
57
58 IpcServer::_ChannelInfo::_ChannelInfo(void)
59         : pClientInfo(null)
60         , pGIOChannel(null)
61         , pGSource(null)
62         , destroySource(true)
63 {
64
65 }
66
67 IpcServer::_ChannelInfo::~_ChannelInfo(void)
68 {
69         if (pGIOChannel != null)
70         {
71                 g_io_channel_unref(pGIOChannel);
72         }
73
74         if (pGSource != null)
75         {
76                 if (destroySource)
77                 {
78                         g_source_destroy(pGSource);
79                 }
80
81                 g_source_unref(pGSource);
82         }
83 }
84
85 IpcServer::_ClientInfo::_ClientInfo(void)
86         : clientId(-1)
87         , pIpcServer(null)
88         , pReverseChannel(null)
89 {
90
91 }
92
93 IpcServer::_ClientInfo::~_ClientInfo(void)
94 {
95         if (pReverseChannel != null)
96         {
97                 g_io_channel_unref(pReverseChannel);
98         }
99
100         channels.clear();
101 }
102
103 IpcServer::IpcServer(void)
104         : __runOnCallerThread(false)
105         , __pEventDispatcher(null)
106         , __pListener(null)
107         , __handlerThread(0)
108         , __pHandlerGMainContext(null)
109         , __pHandlerGMainLoop(null)
110         , __pConnectGSource(null)
111         , __pCurrentChannel(null)
112         , __pCurrentClientInfo(null)
113 {
114         __messageBuffer[0] = '\0';
115 }
116
117
118 IpcServer::~IpcServer(void)
119 {
120         if (__pConnectGSource != null)
121         {
122                 g_source_destroy(__pConnectGSource);
123                 g_source_unref(__pConnectGSource);
124                 __pConnectGSource = null;
125         }
126
127         if (!__runOnCallerThread)
128         {
129                 if (__pEventDispatcher)
130                 {
131                         delete __pEventDispatcher;
132                         __pEventDispatcher = null;
133                 }
134
135                 if (__pHandlerGMainLoop)
136                 {
137                         g_main_loop_unref(__pHandlerGMainLoop);
138                         __pHandlerGMainLoop = null;
139                 }
140
141                 if (__pHandlerGMainContext)
142                 {
143                         g_main_context_unref(__pHandlerGMainContext);
144                         __pHandlerGMainContext = null;
145                 }
146         }
147
148         // clean-up clients
149         __clients.clear();
150 }
151
152 result
153 IpcServer::Construct(const String& name, const IIpcServerEventListener& listener, bool runOnCallerThread)
154 {
155         result r = E_SUCCESS;
156
157         GIOChannel* pGIOChannel = null;
158         GSource* pGSource = null;
159
160         struct sockaddr_un serverAddress;
161         int serverSocket = -1;
162         int serverLen = 0;
163         int ret = 0;
164         std::string socketName;
165         char* pName = null;
166         size_t socketNameLength = 0;
167
168         __name = name;
169         __pListener = const_cast <IIpcServerEventListener*>(&listener);
170         __runOnCallerThread = runOnCallerThread;
171
172         pName = _StringConverter::CopyToCharArrayN(name);
173         SysTryReturnResult(NID_IO, pName != null, E_OUT_OF_MEMORY, "Not enough memory");
174
175         socketName.append("/tmp/");
176         socketName.append(pName);
177
178         delete[] pName;
179
180         socketNameLength = socketName.size() + 1;
181         SysTryReturnResult(NID_IO, socketNameLength < 108, E_INVALID_ARG, "Server name is too long");
182
183         if (!__runOnCallerThread)
184         {
185                 __pHandlerGMainContext = g_main_context_new();
186                 __pHandlerGMainLoop = g_main_loop_new(__pHandlerGMainContext, FALSE);
187
188         }
189         else
190         {
191                 __pHandlerGMainContext = g_main_context_get_thread_default();
192                 if (__pHandlerGMainContext == null) // is global?
193                 {
194                         __pHandlerGMainContext = g_main_context_default();
195                         if (__pHandlerGMainContext == null)
196                         {
197                                 return E_SYSTEM;
198                         }
199                 }
200         }
201
202         ret = unlink(socketName.c_str());
203         SysTryLog(NID_IO, ret == 0, "Unlink a socket has failed.. but it is not a problem.");
204
205         serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);
206         SysTryReturnResult(NID_IO, serverSocket != -1, E_SYSTEM, "Failed to create a socket.");
207
208         // SMACK (Add a * label to socket)
209         if(smack_fsetlabel(serverSocket, "@", SMACK_LABEL_IPOUT) != 0)
210         {
211                 SysTryCatch(NID_IO, errno == EOPNOTSUPP, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] SMACK labeling failed");
212                 SysLog(NID_IO, "Kernel doesn't have Smack.");
213         }
214
215         if(smack_fsetlabel(serverSocket, "*", SMACK_LABEL_IPIN) != 0)
216         {
217                 SysTryCatch(NID_IO, errno == EOPNOTSUPP, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] SMACK labeling failed");
218                 SysLog(NID_IO, "Kernel doesn't have Smack.");
219         }
220
221         bzero(&serverAddress, sizeof(serverAddress));
222         serverAddress.sun_family = AF_UNIX;
223         strncpy(serverAddress.sun_path, socketName.c_str(), socketNameLength);
224         serverLen = sizeof(serverAddress);
225
226         ret = bind(serverSocket, (const struct sockaddr*) &serverAddress, serverLen);
227         SysTryCatch(NID_IO, ret != -1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to bind a socket(%d, %s): %s", serverSocket,
228                            socketName.c_str(), strerror(errno));
229
230         ret = chmod(socketName.c_str(), 0666);
231         SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to change permission of a socket(%d, %s): %s", serverSocket,
232                            socketName.c_str(), strerror(errno));
233
234         ret = listen(serverSocket, 128);
235         SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to listen a socket(%d, %s): %s", serverSocket,
236                            socketName.c_str(), strerror(errno));
237
238         pGIOChannel = g_io_channel_unix_new(serverSocket);
239         SysTryCatch(NID_IO, pGIOChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
240
241         // socket will be closed when pGIOChannel is deleted.
242         g_io_channel_set_close_on_unref(pGIOChannel, TRUE);
243         serverSocket = -1;
244
245         pGSource = g_io_create_watch(pGIOChannel, (GIOCondition) (G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
246         SysTryCatch(NID_IO, pGSource != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
247                            "[E_OUT_OF_MEMORY] Failed to create watch for socket.");
248
249         // channel will be delete when pGSource is deleted.
250         g_io_channel_unref(pGIOChannel);
251         pGIOChannel = null;
252
253         g_source_set_callback(pGSource, (GSourceFunc) OnConnectionRequest, this, NULL);
254         g_source_attach(pGSource, __pHandlerGMainContext);
255
256         if (__runOnCallerThread)
257         {
258                 __pListener->OnIpcServerStarted(*this);
259         }
260         else
261         {
262                 ret = pthread_create(&__handlerThread, null, &ThreadProc, (void*) this);
263                 SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to an IPC thread");
264         }
265
266         __pConnectGSource = pGSource;
267
268         return E_SUCCESS;
269
270 CATCH:
271         if (pGIOChannel != null)
272         {
273                 g_io_channel_unref(pGIOChannel);
274         }
275
276         if (serverSocket != -1)
277         {
278                 close(serverSocket);
279         }
280
281         if (runOnCallerThread && __pHandlerGMainContext)
282         {
283                 g_main_context_unref(__pHandlerGMainContext);
284                 __pHandlerGMainContext = null;
285         }
286
287         return r;
288 }
289
290 struct HelloMessage
291 {
292         int pid;
293         int reverse;  // if the connection is for reverse message
294 };
295
296 gboolean
297 IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data)
298 {
299         IpcServer* pIpcServer = (IpcServer*) data;
300         GError* pGError = null;
301         HelloMessage helloMessage;
302         _ClientInfo* pClientInfo = null;
303         _ChannelInfo* pChannelInfo = null;
304         GSource* pGSource = null;
305         GIOChannel* pChannel = null;
306
307         int server = -1;
308         int client = -1;
309         struct sockaddr_un clientAddress;
310         socklen_t clientLen = sizeof(clientAddress);
311
312         SysAssertf(pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
313         SysAssertf(pIpcServer->__pListener != null, "Listener is null.\n");
314
315         server = g_io_channel_unix_get_fd(source);
316
317         client = accept(server, (struct sockaddr*) &clientAddress, &clientLen);
318         SysTryCatch(NID_IO, client != -1, , E_SYSTEM, "[E_SYSTEM] Accept failed.");
319
320         read(client, &helloMessage, sizeof(helloMessage));
321
322         pChannel = g_io_channel_unix_new(client);
323         SysTryCatch(NID_IO, pChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
324
325         g_io_channel_set_encoding(pChannel, NULL, &pGError);
326         g_io_channel_set_flags(pChannel, G_IO_FLAG_NONBLOCK, &pGError);
327
328         g_io_channel_set_close_on_unref(pChannel, TRUE);
329         client = -1;
330
331         pClientInfo = pIpcServer->__clients[helloMessage.pid];
332         if (pClientInfo == null) // first connection request from this client
333         {
334                 pClientInfo = new (std::nothrow) _ClientInfo;
335                 SysTryCatch(NID_IO, pClientInfo != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
336
337                 pClientInfo->pIpcServer = pIpcServer;
338                 pClientInfo->clientId = helloMessage.pid;
339
340                 char* pAppId = NULL;
341                 int ret = app_manager_get_app_id(helloMessage.pid, &pAppId);
342                 SysTryCatch(NID_IO, ret >= 0, delete pClientInfo, E_SYSTEM, "[E_SYSTEM] Failed to get_app_id: %d", ret);
343
344                 pClientInfo->appId = pAppId;
345                 free(pAppId);
346
347                 pClientInfo->pReverseChannel = null;
348
349                 pIpcServer->__clients[helloMessage.pid] = pClientInfo;
350                 pIpcServer->__pCurrentClientInfo = pClientInfo;
351                 pIpcServer->__pListener->OnIpcClientConnected(*pIpcServer, helloMessage.pid);
352                 pIpcServer->__pCurrentClientInfo = null;
353         }
354
355         if (helloMessage.reverse != 0)
356         {
357                 pClientInfo->pReverseChannel = pChannel;
358         }
359         else
360         {
361                 pChannelInfo = new (std::nothrow) _ChannelInfo;
362                 SysTryCatch(NID_IO, pChannelInfo != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
363
364                 pGSource = g_io_create_watch(pChannel, (GIOCondition) (G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
365                 g_source_set_callback(pGSource, (GSourceFunc) OnReadMessage, pChannelInfo, NULL);
366                 g_source_attach(pGSource, pIpcServer->__pHandlerGMainContext);
367
368                 pChannelInfo->pClientInfo = pClientInfo;
369                 pChannelInfo->pGIOChannel = pChannel;
370                 pChannelInfo->pGSource = pGSource;
371
372                 pClientInfo->channels.push_back(pChannelInfo);
373         }
374
375         return true;
376
377 CATCH:
378         if (pChannel != null)
379         {
380                 g_io_channel_unref(pChannel);
381         }
382
383         if (client != -1)
384         {
385                 close(client);
386         }
387
388         return true;
389 }
390
391 int
392 IpcServer::GetClientId(void) const
393 {
394         if (__pCurrentClientInfo)
395         {
396                 return __pCurrentClientInfo->clientId;
397         }
398
399         return -1;
400 }
401
402 AppId
403 IpcServer::GetClientApplicationId(void) const
404 {
405         static String nullString;
406
407         if (__pCurrentClientInfo)
408         {
409                 return __pCurrentClientInfo->appId;
410         }
411
412         return nullString;
413 }
414
415 gboolean
416 IpcServer::HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data)
417 {
418         GError* pGError = NULL;
419         GIOStatus status;
420         IPC::Message* pMessage = NULL;
421         _ChannelInfo* pChannelInfo = (_ChannelInfo*) data;
422         _ClientInfo* pClientInfo = pChannelInfo->pClientInfo;
423
424         if (condition & G_IO_HUP)
425         {
426                 SysLog(NID_IO, "Connection closed");
427                 int clientId = pClientInfo->clientId;
428
429                 g_io_channel_shutdown(source, FALSE, &pGError);
430
431                 for (unsigned int i = 0; i < pClientInfo->channels.size(); i++)
432                 {
433                         if (pChannelInfo == pClientInfo->channels[i])
434                         {
435                                 pClientInfo->channels.erase(pClientInfo->channels.begin() + i);
436
437                                 // Do not destroy a source in a dispatch callback
438                                 // because main loop will do it if the callback return FALSE.
439                                 pChannelInfo->destroySource = false;
440                                 delete pChannelInfo;
441
442                                 break;
443                         }
444                 }
445
446                 if (pClientInfo->channels.size() == 0)
447                 {
448                         SysLog(NID_IO, "All connections of client(%d) are closed. delete client info", clientId);
449
450                         __pListener->OnIpcClientDisconnected(*this, clientId);
451
452                         __clients[clientId] = null;
453
454                         delete pClientInfo;
455                 }
456
457                 return FALSE;
458         }
459         else if (condition & G_IO_IN)
460         {
461                 gsize readSize = 0;
462                 const char* pStart = NULL;
463                 const char* pEnd = NULL;
464                 const char* pEndOfMessage = NULL;
465
466                 while (true)
467                 {
468                         pGError = null;
469                         status = g_io_channel_read_chars(source, (char*) __messageBuffer, __MAX_MESSAGE_BUFFER_SIZE, &readSize, &pGError);
470                         if (status != G_IO_STATUS_NORMAL)
471                         {
472                                 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
473                                 {
474                                         if (status == G_IO_STATUS_EOF)
475                                         {
476                                                 SysLog(NID_IO, "G_IO_STATUS_EOF, the connection is closed.");
477                                         }
478                                         else
479                                         {
480                                                 SysLog(NID_IO, "G_IO_STATUS_ERROR, the connection is closed. ");
481                                         }
482
483                                         pGError = null;
484                                         g_io_channel_shutdown(source, FALSE, &pGError);
485
486                                         int clientId = pClientInfo->clientId;
487
488                                         for (unsigned int i = 0; i < pClientInfo->channels.size(); i++)
489                                         {
490                                                 if (pChannelInfo == pClientInfo->channels[i])
491                                                 {
492                                                         pClientInfo->channels.erase(pClientInfo->channels.begin() + i);
493
494                                                         pChannelInfo->destroySource = false;
495                                                         delete pChannelInfo;
496                                                         break;
497                                                 }
498                                         }
499
500                                         if (pClientInfo->channels.size() == 0)
501                                         {
502                                                 SysLog(NID_IO, "All connections of client(%d) are closed normally by the client.", clientId);
503
504                                                 if (__pListener)
505                                                 {
506                                                         __pListener->OnIpcClientDisconnected(*this, clientId);
507                                                 }
508
509                                                 __clients[clientId] = null;
510
511                                                 delete pClientInfo;
512                                         }
513
514                                         return FALSE;
515                                 }
516                         }
517
518                         if (readSize == 0)
519                         {
520                                 break;
521                         }
522
523                         if (__pending.empty())
524                         {
525                                 pStart = __messageBuffer;
526                                 pEnd = pStart + readSize;
527                         }
528                         else
529                         {
530                                 __pending.append(__messageBuffer, readSize);
531                                 pStart = __pending.data();
532                                 pEnd = pStart + __pending.size();
533                         }
534
535                         while (true)
536                         {
537                                 pEndOfMessage = IPC::Message::FindNext(pStart, pEnd);
538                                 if (pEndOfMessage == NULL)
539                                 {
540                                         __pending.assign(pStart, pEnd - pStart);
541                                         break;
542                                 }
543
544                                 pMessage = new (std::nothrow) IPC::Message(pStart, pEndOfMessage - pStart);
545                                 SysTryReturn(NID_IO, pMessage != null, FALSE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
546
547                                 __pCurrentChannel = source;
548
549                                 if (__pListener)
550                                 {
551                                         __pListener->OnIpcRequestReceived(*this, *pMessage);
552                                 }
553
554                                 delete pMessage;
555
556                                 __pCurrentChannel = NULL;
557
558                                 pStart = pEndOfMessage;
559                         }
560                 }
561         }
562         else
563         {
564                 // empty statement
565         }
566
567         return TRUE;
568 }
569
570 gboolean
571 IpcServer::OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data)
572 {
573         gboolean ret = FALSE;
574         _ChannelInfo* pChannelInfo = (_ChannelInfo*) data;
575         _ClientInfo* pClientInfo = pChannelInfo->pClientInfo;
576         IpcServer* pIpcServer = (IpcServer*) pClientInfo->pIpcServer;
577
578         pIpcServer->__pCurrentClientInfo = pClientInfo;
579         ret = pIpcServer->HandleReceivedMessage(source, condition, data);
580         pIpcServer->__pCurrentClientInfo = null;
581
582         return ret;
583 }
584
585 void*
586 IpcServer::ThreadProc(void* pParam)
587 {
588         IpcServer* pIpcServer = (IpcServer*) pParam;
589         if (pIpcServer != NULL)
590         {
591                 pIpcServer->Run(NULL);
592         }
593
594         return NULL;
595 }
596
597 void
598 IpcServer::Run(void* pParam)
599 {
600         result r = E_SUCCESS;
601
602         if (__pListener == null)
603         {
604                 return;
605         }
606
607         __pEventDispatcher = new (std::nothrow) _EventDispatcher;
608         SysTryReturnVoidResult(NID_IO, __pEventDispatcher != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
609
610         r = __pEventDispatcher->Construct(__pHandlerGMainContext);
611         if (IsFailed(r))
612         {
613                 delete __pEventDispatcher;
614                 __pEventDispatcher = null;
615         }
616
617         __pListener->OnIpcServerStarted(*this);
618
619         g_main_loop_run(__pHandlerGMainLoop);
620
621         __pListener->OnIpcServerStopped(*this);
622 }
623
624 result
625 IpcServer::Start(void)
626 {
627         return E_SUCCESS;
628 }
629
630 String
631 IpcServer::GetName(void) const
632 {
633         return __name;
634 }
635
636 result
637 IpcServer::Stop(void)
638 {
639         result r = E_SUCCESS;
640         int ret = 0;
641
642         SysTryReturnResult(NID_IO, __pListener != null, E_SYSTEM, "Listener is null.");
643
644         if (!__runOnCallerThread)
645         {
646                 pthread_t self = pthread_self();
647
648                 if (__pHandlerGMainLoop)
649                 {
650                         g_main_loop_quit(__pHandlerGMainLoop);
651                 }
652
653                 if (__handlerThread != self)
654                 {
655                         ret = pthread_join(__handlerThread, null);
656                         SysTryLog(NID_IO, ret == 0, "Join an IPC thread returns an error");
657                 }
658         }
659         else
660         {
661                 __pListener->OnIpcServerStopped(*this);
662         }
663
664         return r;
665 }
666
667 bool
668 IpcServer::Send(IPC::Message* msg)
669 {
670         gsize remain = 0;
671         gsize written = 0;
672         char* pData = NULL;
673         GError* pGError = NULL;
674
675
676         pData = (char*) msg->data();
677         remain = msg->size();
678
679         if (msg->is_reply())
680         {
681                 while (remain > 0)
682                 {
683                         pGError = NULL;
684                         g_io_channel_write_chars(__pCurrentChannel, (char*) pData, remain, &written, &pGError);
685
686                         remain -= written;
687                         pData += written;
688                 }
689
690                 g_io_channel_flush(__pCurrentChannel, &pGError);
691         }
692         else
693         {
694                 // empty statement;
695         }
696
697         return true;
698 }
699
700 result
701 IpcServer::SendResponse(int client, IPC::Message* pMessage)
702 {
703         result r = E_SUCCESS;
704         gsize remain = 0;
705         gsize written = 0;
706         char* pData = null;
707         GError* pGError = null;
708         _ClientInfo* pClientInfo = null;
709         int ret = 0;
710
711         SysTryReturn(NID_IO, client >= 0 && pMessage != null, false, E_INVALID_ARG,
712                                 "[E_INVALID_ARG] pMessage(0x%x) is null or clinet(%d) < 0", pMessage,
713                                 client);
714         SysTryCatch(NID_IO, !pMessage->is_sync(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Can't send sync. messagee.");
715
716         pClientInfo = __clients[client];
717         SysTryCatch(NID_IO, pClientInfo != null, r = E_INVALID_ARG, E_INVALID_ARG,
718                            "[E_INVALID_ARG] client(%d) has not been registered.",
719                            client);
720
721         pData = (char*) pMessage->data();
722         remain = pMessage->size();
723
724         while (remain > 0)
725         {
726                 pGError = NULL;
727                 ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
728                 if (ret != G_IO_STATUS_NORMAL)
729                 {
730                         SysLog(NID_IO, "Failed to send a response: %d", ret);
731                         SysTryCatch(NID_IO, ret != G_IO_STATUS_ERROR, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
732                 }
733
734                 remain -= written;
735                 pData += written;
736         }
737
738         g_io_channel_flush(pClientInfo->pReverseChannel, &pGError);
739
740         delete pMessage;
741
742         return E_SUCCESS;
743
744 CATCH:
745         delete pMessage;
746         return r;
747 }
748
749 result
750 IpcServer::SendResponse(int client, const IPC::Message& message)
751 {
752         result r = E_SUCCESS;
753         gsize remain = 0;
754         gsize written = 0;
755         char* pData = null;
756         GError* pGError = null;
757         _ClientInfo* pClientInfo = null;
758         int ret = 0;
759
760         SysTryReturn(NID_IO, client >= 0, false, E_INVALID_ARG, "[E_INVALID_ARG] clinet(%d) < 0", client);
761         SysTryCatch(NID_IO, !message.is_sync(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Can't send sync. messagee.");
762
763         pClientInfo = __clients[client];
764         SysTryCatch(NID_IO, pClientInfo != null, r = E_INVALID_ARG, E_INVALID_ARG,
765                            "[E_INVALID_ARG] client(%d) has not been registered.",
766                            client);
767
768         pData = (char*) message.data();
769         remain = message.size();
770
771         while (remain > 0)
772         {
773                 pGError = NULL;
774                 ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
775                 if (ret != G_IO_STATUS_NORMAL)
776                 {
777                         SysLog(NID_IO, "Failed to send a response: %d", ret);
778                         SysTryCatch(NID_IO, ret != G_IO_STATUS_ERROR, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
779                 }
780
781                 remain -= written;
782                 pData += written;
783         }
784
785         g_io_channel_flush(pClientInfo->pReverseChannel, &pGError);
786
787         return E_SUCCESS;
788
789 CATCH:
790         return r;
791 }