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