Initial commit
[platform/core/security/tef-simulator.git] / simulatordaemon / src / SimulatorDaemon.cpp
1 /*
2  * =====================================================================================
3  *
4  *       Filename:  SimulatorDaemon.cpp
5  *
6  *    Description:  SimulatorDaemon main file
7  *
8  *        Version:  1.0
9  *        Created:  16 April 2015 12:42:03  IST
10  *       Revision:  Original
11  *       Compiler:  gcc
12  *
13  *         Author:  CHERYL (cb), cheryl.b@samsung.com
14  *   Organization:  Samsung Electronics
15  *
16  * =====================================================================================
17  */
18
19 /*-----------------------------------------------------------------------------
20  *  Include files
21  *-----------------------------------------------------------------------------*/
22 #include "path.h"
23 #include "SimulatorDaemonServer.h"
24
25 /*-----------------------------------------------------------------------------
26  *  Local functions
27  *-----------------------------------------------------------------------------*/
28 /**
29  * Create shm file for shared memory implementation (IPC)
30  */
31 void initializeShm() {
32         LOGD(SIM_DAEMON, "Entry");
33         ::unlink(SHM_PATH);
34         int fd = creat(SHM_PATH, S_IRWXU);
35         if (-1 == fd) {
36                 LOGE(SIM_DAEMON, "shm file creation failed");
37                 exit(0);
38         }
39         close(fd);
40 }
41
42 /**
43  * Starts the Simulator Daemon as server which listens for connection from
44  * TEECLib
45  * @param io_service
46  */
47 void startServer(boost::asio::io_service& io_service) {
48         LOGD(SIM_DAEMON, "Entry");
49         try {
50                 io_service.run();
51         } catch (std::exception& e) {
52                 LOGE(SIM_DAEMON, "Exception: %s", e.what());
53         }
54 }
55
56 /**
57  * Stops the Simulator Daemon server
58  */
59 void stopServer(boost::asio::io_service& io_service) {
60         LOGD(SIM_DAEMON, "Entry");
61         io_service.stop();
62 }
63
64 /**
65  * main function for Simulator Daemon
66  * @return
67  */
68 int main() {
69         LOGD(SIM_DAEMON, "Entry");
70         uint32_t result = 0;
71         try {
72                 ::unlink(SIMDAEMON_PATH);
73                 //initializeShm();
74                 SimulatorDaemonServer s(ioService::getInstance(), SIMDAEMON_PATH);
75                 // Once the server is started, it exits only after the
76                 // connection is lost or gracefully disconnected.
77                 startServer(ioService::getInstance());
78                 syslog(LOG_INFO | LOG_USER, "Daemon stopped");
79         } catch (std::exception& e) {
80                 syslog(LOG_ERR | LOG_USER, "Exception: %s", e.what());
81                 LOGE(SIM_DAEMON, "Exception: %s", e.what());
82         }
83         stopServer(ioService::getInstance());
84         return result;
85 }