ed2adadc61fe65d3e59e3f008d62fc57f46f78d1
[platform/core/connectivity/smartcard-service.git] / server / smartcard-daemon.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* standard library header */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <glib-object.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <signal.h>
28 #include <vector>
29
30 /* SLP library header */
31
32 /* local header */
33 #include "Debug.h"
34 #include "ServerResource.h"
35 #include "smartcard-service-gdbus.h"
36 #include "ServerGDBus.h"
37
38 /* definition */
39 using namespace std;
40 using namespace smartcard_service_api;
41
42 /* global variable */
43 GMainLoop *main_loop = NULL;
44
45 #if (BUILD_GCOV != 0)
46 extern "C" void __gcov_flush(void);
47 #endif
48
49 #ifndef USE_AUTOSTART
50 static void daemonize(void)
51 {
52         pid_t pid, sid;
53
54         /* already a daemon */
55         if (getppid() == 1)
56                 return;
57
58         /* Fork off the parent process */
59         pid = fork();
60         if (pid < 0)
61         {
62                 exit(EXIT_FAILURE);
63         }
64
65         if (pid > 0)
66         {
67                 exit(EXIT_SUCCESS); /*Killing the Parent Process*/
68         }
69
70         /* At this point we are executing as the child process */
71         umask(0);
72
73         /* Create a new SID for the child process */
74         sid = setsid();
75         if (sid < 0)
76         {
77                 exit(EXIT_FAILURE);
78         }
79
80         /* Change the current working directory. */
81         if ((chdir("/")) < 0)
82         {
83                 exit(EXIT_FAILURE);
84         }
85
86         close(STDIN_FILENO);
87         close(STDOUT_FILENO);
88         close(STDERR_FILENO);
89 }
90 #endif
91
92 static void _bus_acquired_cb(GDBusConnection *connection,
93         const gchar *path, gpointer user_data)
94 {
95         _DBG("bus path : %s", path);
96
97         ServerResource::getInstance();
98
99         ServerGDBus::getInstance().init();
100 }
101
102 static void _name_acquired_cb(GDBusConnection *connection,
103         const gchar *name, gpointer user_data)
104 {
105         _DBG("name : %s", name);
106 }
107
108 /* LCOV_EXCL_START */
109 static void _name_lost_cb(GDBusConnection *connnection,
110         const gchar *name, gpointer user_data)
111 {
112         _DBG("name : %s", name);
113
114         ServerGDBus::getInstance().deinit();
115 }
116
117 static void __sighandler(int sig)
118 {
119         _DBG("signal!! [%d]", sig);
120 #if (BUILD_GCOV != 0)
121         __gcov_flush();
122 #endif
123 }
124 /* LCOV_EXCL_STOP */
125
126 int main(int argc, char *argv[])
127 {
128         guint id = 0;
129         signal(SIGTERM, &__sighandler);
130
131 #ifndef USE_AUTOSTART
132         daemonize();
133 #endif
134
135 #if (BUILD_GCOV != 0)
136         setenv("GCOV_PREFIX", "/tmp/", 1);
137 #endif
138
139         main_loop = g_main_loop_new(NULL, FALSE);
140
141         id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
142                         "org.tizen.SmartcardService",
143                         G_BUS_NAME_OWNER_FLAGS_NONE,
144                         _bus_acquired_cb,
145                         _name_acquired_cb,
146                         _name_lost_cb,
147                         NULL,
148                         NULL);
149
150         g_main_loop_run(main_loop);
151
152         if (id)
153                 g_bus_unown_name(id);
154
155         /* release secure element.. (pure virtual function problem..) */
156         ServerResource::getInstance().unloadSecureElements();
157
158 #if (BUILD_GCOV != 0)
159         __gcov_flush();
160 #endif
161
162         return 0;
163 }
164
165 void smartcard_daemon_exit()
166 {
167         g_main_loop_quit(main_loop);
168         g_main_loop_unref(main_loop);
169 }