3c2890abfd10b2163a45f890e439082f8351f662
[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 #ifndef USE_AUTOSTART
46 static void daemonize(void)
47 {
48         pid_t pid, sid;
49
50         /* already a daemon */
51         if (getppid() == 1)
52                 return;
53
54         /* Fork off the parent process */
55         pid = fork();
56         if (pid < 0)
57         {
58                 exit(EXIT_FAILURE);
59         }
60
61         if (pid > 0)
62         {
63                 exit(EXIT_SUCCESS); /*Killing the Parent Process*/
64         }
65
66         /* At this point we are executing as the child process */
67         umask(0);
68
69         /* Create a new SID for the child process */
70         sid = setsid();
71         if (sid < 0)
72         {
73                 exit(EXIT_FAILURE);
74         }
75
76         /* Change the current working directory. */
77         if ((chdir("/")) < 0)
78         {
79                 exit(EXIT_FAILURE);
80         }
81
82         close(STDIN_FILENO);
83         close(STDOUT_FILENO);
84         close(STDERR_FILENO);
85 }
86 #endif
87
88 static void _bus_acquired_cb(GDBusConnection *connection,
89         const gchar *path, gpointer user_data)
90 {
91         _DBG("bus path : %s", path);
92
93         ServerResource::getInstance();
94
95         ServerGDBus::getInstance().init();
96 }
97
98 static void _name_acquired_cb(GDBusConnection *connection,
99         const gchar *name, gpointer user_data)
100 {
101         _DBG("name : %s", name);
102 }
103
104 static void _name_lost_cb(GDBusConnection *connnection,
105         const gchar *name, gpointer user_data)
106 {
107         _DBG("name : %s", name);
108
109         ServerGDBus::getInstance().deinit();
110 }
111
112 static void __sighandler(int sig)
113 {
114         _DBG("signal!! [%d]", sig);
115 }
116
117 int main(int argc, char *argv[])
118 {
119         guint id = 0;
120         signal(SIGTERM, &__sighandler);
121
122 #ifndef USE_AUTOSTART
123         daemonize();
124 #endif
125
126         if (!g_thread_supported()) {
127                 g_thread_init(NULL);
128         }
129
130         g_type_init();
131
132         main_loop = g_main_new(TRUE);
133
134         id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
135                         "org.tizen.SmartcardService",
136                         G_BUS_NAME_OWNER_FLAGS_NONE,
137                         _bus_acquired_cb,
138                         _name_acquired_cb,
139                         _name_lost_cb,
140                         NULL,
141                         NULL);
142
143         g_main_loop_run(main_loop);
144
145         if (id)
146                 g_bus_unown_name(id);
147
148         /* release secure element.. (pure virtual function problem..) */
149         ServerResource::getInstance().unloadSecureElements();
150
151         return 0;
152 }
153
154 void smartcard_daemon_exit()
155 {
156         g_main_loop_quit(main_loop);
157 }