Add exception check
[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         ssize_t readBytes = 0;
307
308         int server = -1;
309         int client = -1;
310         struct sockaddr_un clientAddress;
311         socklen_t clientLen = sizeof(clientAddress);
312
313         SysAssertf(pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
314         SysAssertf(pIpcServer->__pListener != null, "Listener is null.\n");
315
316         server = g_io_channel_unix_get_fd(source);
317
318         client = accept(server, (struct sockaddr*) &clientAddress, &clientLen);
319         SysTryCatch(NID_IO, client != -1, , E_SYSTEM, "[E_SYSTEM] Accept failed.");
320
321         readBytes = read(client, &helloMessage, sizeof(helloMessage));
322         SysTryCatch(NID_IO, readBytes >= 0, , E_SYSTEM, "[E_SYSTEM] Failed to receive hello message (%d, %s).",
323                         errno, strerror(errno));
324
325         pChannel = g_io_channel_unix_new(client);
326         SysTryCatch(NID_IO, pChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
327
328         g_io_channel_set_encoding(pChannel, NULL, &pGError);
329         g_io_channel_set_flags(pChannel, G_IO_FLAG_NONBLOCK, &pGError);
330
331         g_io_channel_set_close_on_unref(pChannel, TRUE);
332         client = -1;
333
334         pClientInfo = pIpcServer->__clients[helloMessage.pid];
335         if (pClientInfo == null) // first connection request from this client
336         {
337                 pClientInfo = new (std::nothrow) _ClientInfo;
338                 SysTryCatch(NID_IO, pClientInfo != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
339
340                 pClientInfo->pIpcServer = pIpcServer;
341                 pClientInfo->clientId = helloMessage.pid;
342
343                 char* pAppId = NULL;
344                 int ret = app_manager_get_app_id(helloMessage.pid, &pAppId);
345                 SysTryCatch(NID_IO, ret >= 0, delete pClientInfo, E_SYSTEM, "[E_SYSTEM] Failed to get_app_id: %d", ret);
346
347                 pClientInfo->appId = pAppId;
348                 free(pAppId);
349
350                 pClientInfo->pReverseChannel = null;
351
352                 pIpcServer->__clients[helloMessage.pid] = pClientInfo;
353                 pIpcServer->__pCurrentClientInfo = pClientInfo;
354                 pIpcServer->__pListener->OnIpcClientConnected(*pIpcServer, helloMessage.pid);
355                 pIpcServer->__pCurrentClientInfo = null;
356         }
357
358         if (helloMessage.reverse != 0)
359         {
360                 pClientInfo->pReverseChannel = pChannel;
361         }
362         else
363         {
364                 pChannelInfo = new (std::nothrow) _ChannelInfo;
365                 SysTryCatch(NID_IO, pChannelInfo != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
366
367                 pGSource = g_io_create_watch(pChannel, (GIOCondition) (G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
368                 g_source_set_callback(pGSource, (GSourceFunc) OnReadMessage, pChannelInfo, NULL);
369                 g_source_attach(pGSource, pIpcServer->__pHandlerGMainContext);
370
371                 pChannelInfo->pClientInfo = pClientInfo;
372                 pChannelInfo->pGIOChannel = pChannel;
373                 pChannelInfo->pGSource = pGSource;
374
375                 pClientInfo->channels.push_back(pChannelInfo);
376         }
377
378         return true;
379
380 CATCH:
381         if (pChannel != null)
382         {
383                 g_io_channel_unref(pChannel);
384         }
385
386         if (client != -1)
387         {
388                 close(client);
389         }
390
391         return true;
392 }
393
394 int
395 IpcServer::GetClientId(void) const
396 {
397         if (__pCurrentClientInfo)
398         {
399                 return __pCurrentClientInfo->clientId;
400         }
401
402         return -1;
403 }
404
405 AppId
406 IpcServer::GetClientApplicationId(void) const
407 {
408         static String nullString;
409
410         if (__pCurrentClientInfo)
411         {
412                 return __pCurrentClientInfo->appId;
413         }
414
415         return nullString;
416 }
417
418 gboolean
419 IpcServer::HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data)
420 {
421         GError* pGError = NULL;
422         GIOStatus status;
423         IPC::Message* pMessage = NULL;
424         _ChannelInfo* pChannelInfo = (_ChannelInfo*) data;
425         _ClientInfo* pClientInfo = pChannelInfo->pClientInfo;
426
427         if (condition & G_IO_HUP)
428         {
429                 SysLog(NID_IO, "Connection closed");
430                 int clientId = pClientInfo->clientId;
431
432                 g_io_channel_shutdown(source, FALSE, &pGError);
433
434                 for (unsigned int i = 0; i < pClientInfo->channels.size(); i++)
435                 {
436                         if (pChannelInfo == pClientInfo->channels[i])
437                         {
438                                 pClientInfo->channels.erase(pClientInfo->channels.begin() + i);
439
440                                 // Do not destroy a source in a dispatch callback
441                                 // because main loop will do it if the callback return FALSE.
442                                 pChannelInfo->destroySource = false;
443                                 delete pChannelInfo;
444
445                                 break;
446                         }
447                 }
448
449                 if (pClientInfo->channels.size() == 0)
450                 {
451                         SysLog(NID_IO, "All connections of client(%d) are closed. delete client info", clientId);
452
453                         __pListener->OnIpcClientDisconnected(*this, clientId);
454
455                         __clients[clientId] = null;
456
457                         delete pClientInfo;
458                 }
459
460                 return FALSE;
461         }
462         else if (condition & G_IO_IN)
463         {
464                 gsize readSize = 0;
465                 const char* pStart = NULL;
466                 const char* pEnd = NULL;
467                 const char* pEndOfMessage = NULL;
468
469                 while (true)
470                 {
471                         pGError = null;
472                         status = g_io_channel_read_chars(source, (char*) __messageBuffer, __MAX_MESSAGE_BUFFER_SIZE, &readSize, &pGError);
473                         if (status != G_IO_STATUS_NORMAL)
474                         {
475                                 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
476                                 {
477                                         if (status == G_IO_STATUS_EOF)
478                                         {
479                                                 SysLog(NID_IO, "G_IO_STATUS_EOF, the connection is closed.");
480                                         }
481                                         else
482                                         {
483                                                 SysLog(NID_IO, "G_IO_STATUS_ERROR, the connection is closed. ");
484                                         }
485
486                                         pGError = null;
487                                         g_io_channel_shutdown(source, FALSE, &pGError);
488
489                                         int clientId = pClientInfo->clientId;
490
491                                         for (unsigned int i = 0; i < pClientInfo->channels.size(); i++)
492                                         {
493                                                 if (pChannelInfo == pClientInfo->channels[i])
494                                                 {
495                                                         pClientInfo->channels.erase(pClientInfo->channels.begin() + i);
496
497                                                         pChannelInfo->destroySource = false;
498                                                         delete pChannelInfo;
499                                                         break;
500                                                 }
501                                         }
502
503                                         if (pClientInfo->channels.size() == 0)
504                                         {
505                                                 SysLog(NID_IO, "All connections of client(%d) are closed normally by the client.", clientId);
506
507                                                 if (__pListener)
508                                                 {
509                                                         __pListener->OnIpcClientDisconnected(*this, clientId);
510                                                 }
511
512                                                 __clients[clientId] = null;
513
514                                                 delete pClientInfo;
515                                         }
516
517                                         return FALSE;
518                                 }
519                         }
520
521                         if (readSize == 0)
522                         {
523                                 break;
524                         }
525
526                         if (__pending.empty())
527                         {
528                                 pStart = __messageBuffer;
529                                 pEnd = pStart + readSize;
530                         }
531                         else
532                         {
533                                 __pending.append(__messageBuffer, readSize);
534                                 pStart = __pending.data();
535                                 pEnd = pStart + __pending.size();
536                         }
537
538                         while (true)
539                         {
540                                 pEndOfMessage = IPC::Message::FindNext(pStart, pEnd);
541                                 if (pEndOfMessage == NULL)
542                                 {
543                                         __pending.assign(pStart, pEnd - pStart);
544                                         break;
545                                 }
546
547                                 pMessage = new (std::nothrow) IPC::Message(pStart, pEndOfMessage - pStart);
548                                 SysTryReturn(NID_IO, pMessage != null, FALSE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
549
550                                 __pCurrentChannel = source;
551
552                                 if (__pListener)
553                                 {
554                                         __pListener->OnIpcRequestReceived(*this, *pMessage);
555                                 }
556
557                                 delete pMessage;
558
559                                 __pCurrentChannel = NULL;
560
561                                 pStart = pEndOfMessage;
562                         }
563                 }
564         }
565         else
566         {
567                 // empty statement
568         }
569
570         return TRUE;
571 }
572
573 gboolean
574 IpcServer::OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data)
575 {
576         gboolean ret = FALSE;
577         _ChannelInfo* pChannelInfo = (_ChannelInfo*) data;
578         _ClientInfo* pClientInfo = pChannelInfo->pClientInfo;
579         IpcServer* pIpcServer = (IpcServer*) pClientInfo->pIpcServer;
580
581         pIpcServer->__pCurrentClientInfo = pClientInfo;
582         ret = pIpcServer->HandleReceivedMessage(source, condition, data);
583         pIpcServer->__pCurrentClientInfo = null;
584
585         return ret;
586 }
587
588 void*
589 IpcServer::ThreadProc(void* pParam)
590 {
591         IpcServer* pIpcServer = (IpcServer*) pParam;
592         if (pIpcServer != NULL)
593         {
594                 pIpcServer->Run(NULL);
595         }
596
597         return NULL;
598 }
599
600 void
601 IpcServer::Run(void* pParam)
602 {
603         result r = E_SUCCESS;
604
605         if (__pListener == null)
606         {
607                 return;
608         }
609
610         __pEventDispatcher = new (std::nothrow) _EventDispatcher;
611         SysTryReturnVoidResult(NID_IO, __pEventDispatcher != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
612
613         r = __pEventDispatcher->Construct(__pHandlerGMainContext);
614         if (IsFailed(r))
615         {
616                 delete __pEventDispatcher;
617                 __pEventDispatcher = null;
618         }
619
620         __pListener->OnIpcServerStarted(*this);
621
622         g_main_loop_run(__pHandlerGMainLoop);
623
624         __pListener->OnIpcServerStopped(*this);
625 }
626
627 result
628 IpcServer::Start(void)
629 {
630         return E_SUCCESS;
631 }
632
633 String
634 IpcServer::GetName(void) const
635 {
636         return __name;
637 }
638
639 result
640 IpcServer::Stop(void)
641 {
642         result r = E_SUCCESS;
643         int ret = 0;
644
645         SysTryReturnResult(NID_IO, __pListener != null, E_SYSTEM, "Listener is null.");
646
647         if (!__runOnCallerThread)
648         {
649                 pthread_t self = pthread_self();
650
651                 if (__pHandlerGMainLoop)
652                 {
653                         g_main_loop_quit(__pHandlerGMainLoop);
654                 }
655
656                 if (__handlerThread != self)
657                 {
658                         ret = pthread_join(__handlerThread, null);
659                         SysTryLog(NID_IO, ret == 0, "Join an IPC thread returns an error");
660                 }
661         }
662         else
663         {
664                 __pListener->OnIpcServerStopped(*this);
665         }
666
667         return r;
668 }
669
670 bool
671 IpcServer::Send(IPC::Message* msg)
672 {
673         gsize remain = 0;
674         gsize written = 0;
675         char* pData = NULL;
676         GError* pGError = NULL;
677
678
679         pData = (char*) msg->data();
680         remain = msg->size();
681
682         if (msg->is_reply())
683         {
684                 while (remain > 0)
685                 {
686                         pGError = NULL;
687                         g_io_channel_write_chars(__pCurrentChannel, (char*) pData, remain, &written, &pGError);
688
689                         remain -= written;
690                         pData += written;
691                 }
692
693                 g_io_channel_flush(__pCurrentChannel, &pGError);
694         }
695         else
696         {
697                 // empty statement;
698         }
699
700         delete msg;
701
702         return true;
703 }
704
705 result
706 IpcServer::SendResponse(int client, IPC::Message* pMessage)
707 {
708         result r = E_SUCCESS;
709         gsize remain = 0;
710         gsize written = 0;
711         char* pData = null;
712         GError* pGError = null;
713         _ClientInfo* pClientInfo = null;
714         int ret = 0;
715
716         SysTryReturn(NID_IO, client >= 0 && pMessage != null, false, E_INVALID_ARG,
717                                 "[E_INVALID_ARG] pMessage(0x%x) is null or clinet(%d) < 0", pMessage,
718                                 client);
719         SysTryCatch(NID_IO, !pMessage->is_sync(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Can't send sync. messagee.");
720
721         pClientInfo = __clients[client];
722         SysTryCatch(NID_IO, pClientInfo != null, r = E_INVALID_ARG, E_INVALID_ARG,
723                            "[E_INVALID_ARG] client(%d) has not been registered.",
724                            client);
725
726         pData = (char*) pMessage->data();
727         remain = pMessage->size();
728
729         while (remain > 0)
730         {
731                 pGError = NULL;
732                 ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
733                 if (ret != G_IO_STATUS_NORMAL)
734                 {
735                         SysLog(NID_IO, "Failed to send a response: %d", ret);
736                         SysTryCatch(NID_IO, ret != G_IO_STATUS_ERROR, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
737                 }
738
739                 remain -= written;
740                 pData += written;
741         }
742
743         g_io_channel_flush(pClientInfo->pReverseChannel, &pGError);
744
745         delete pMessage;
746
747         return E_SUCCESS;
748
749 CATCH:
750         delete pMessage;
751         return r;
752 }
753
754 result
755 IpcServer::SendResponse(int client, const IPC::Message& message)
756 {
757         result r = E_SUCCESS;
758         gsize remain = 0;
759         gsize written = 0;
760         char* pData = null;
761         GError* pGError = null;
762         _ClientInfo* pClientInfo = null;
763         int ret = 0;
764
765         SysTryReturn(NID_IO, client >= 0, false, E_INVALID_ARG, "[E_INVALID_ARG] clinet(%d) < 0", client);
766         SysTryCatch(NID_IO, !message.is_sync(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Can't send sync. messagee.");
767
768         pClientInfo = __clients[client];
769         SysTryCatch(NID_IO, pClientInfo != null, r = E_INVALID_ARG, E_INVALID_ARG,
770                            "[E_INVALID_ARG] client(%d) has not been registered.",
771                            client);
772
773         pData = (char*) message.data();
774         remain = message.size();
775
776         while (remain > 0)
777         {
778                 pGError = NULL;
779                 ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
780                 if (ret != G_IO_STATUS_NORMAL)
781                 {
782                         SysLog(NID_IO, "Failed to send a response: %d", ret);
783                         SysTryCatch(NID_IO, ret != G_IO_STATUS_ERROR, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
784                 }
785
786                 remain -= written;
787                 pData += written;
788         }
789
790         g_io_channel_flush(pClientInfo->pReverseChannel, &pGError);
791
792         return E_SUCCESS;
793
794 CATCH:
795         return r;
796 }