2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
29 #endif /* __cplusplus */
31 #include <dbus/dbus-glib.h>
32 #include <dbus/dbus-glib-bindings.h>
35 /* SLP library header */
39 #include "ServerIPC.h"
40 #include "ServerResource.h"
42 #include "SmartcardDbus.h"
43 #include "smartcard-service-binding.h"
49 using namespace smartcard_service_api;
50 #endif /* __cplusplus */
54 GObject *object = NULL;
55 DBusGConnection *connection = NULL;
59 static void daemonize(void)
63 /* already a daemon */
67 /* Fork off the parent process */
76 exit(EXIT_SUCCESS); /*Killing the Parent Process*/
79 /* At this point we are executing as the child process */
82 /* Create a new SID for the child process */
89 /* Change the current working directory. */
102 G_DEFINE_TYPE(Smartcard_Service, smartcard_service, G_TYPE_OBJECT)
104 /* Just Check the assert and set the error message */
105 #define __G_ASSERT(test, return_val, error, domain, error_code)\
108 if G_LIKELY (!(test)) { \
109 g_set_error (error, domain, error_code, #test); \
110 return (return_val); \
115 GQuark smartcard_service_error_quark(void)
117 SCARD_DEBUG("smartcard_service_error_quark entered");
119 return g_quark_from_static_string("smartcard_service_error");
122 static void smartcard_service_init(Smartcard_Service *smartcard_service)
124 SCARD_DEBUG("smartcard_service_init entered");
127 static void smartcard_service_class_init(Smartcard_ServiceClass *smartcard_service_class)
129 SCARD_DEBUG("smartcard_service_class_init entered");
131 dbus_g_object_type_install_info(SMARTCARD_SERVICE_TYPE, &dbus_glib_smartcard_service_object_info);
134 gboolean smartcard_service_launch(Smartcard_Service *smartcard_service, guint *result_val, GError **error)
136 SCARD_DEBUG("smartcard_service_launch entered");
141 static void _initialize_dbus()
143 GError *error = NULL;
144 DBusGProxy *proxy = NULL;
151 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
154 object = (GObject *)g_object_new(SMARTCARD_SERVICE_TYPE, NULL);
155 dbus_g_connection_register_g_object(connection, SMARTCARD_SERVICE_PATH, object);
157 /* register service */
158 proxy = dbus_g_proxy_new_for_name(connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
161 if (!org_freedesktop_DBus_request_name(proxy, SMARTCARD_SERVICE_NAME, 0, &ret, &error))
163 SCARD_DEBUG_ERR("Unable to register service: %s", error->message);
167 g_object_unref (proxy);
171 SCARD_DEBUG_ERR("dbus_g_proxy_new_for_name failed");
176 SCARD_DEBUG_ERR("ERROR: Can't get on system bus [%s]", error->message);
183 static void _finalize_dbus()
187 dbus_g_connection_unregister_g_object(connection, object);
188 g_object_unref(object);
194 static void __sighandler(int sig)
196 SCARD_DEBUG("signal!! [%d]", sig);
205 GMainLoop *loop = NULL;
207 signal(SIGTERM, &__sighandler);
209 #ifndef USE_AUTOSTART
213 if (!g_thread_supported())
218 loop = g_main_new(TRUE);
221 ServerResource::getInstance().setMainLoopInstance(loop);
222 ServerIPC::getInstance()->createListenSocket();
223 #else /* __cplusplus */
224 server_resource_set_main_loop_instance(loop);
225 server_ipc_create_listen_socket();
226 #endif /* __cplusplus */
231 g_main_loop_run(loop);
236 /* release secure element.. (pure virtual function problem..) */
237 ServerResource::getInstance().unloadSecureElements();