remove key initialize in i386
[framework/appfw/aul-1.git] / src / app_signal.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <stdio.h>
24 #include "app_signal.h"
25 #include "aul_api.h"
26 #include "simple_util.h"
27 #include "aul.h"
28
29 static int (*_app_dead_handler) (int pid, void *data);
30 static void *_app_dead_data;
31
32 static int (*_app_launch_handler) (int pid, void *data);
33 static void *_app_launch_data;
34
35 static DBusConnection *bus;
36 static int app_dbus_signal_handler_initialized = 0;
37
38 static DBusHandlerResult
39 __app_dbus_signal_filter(DBusConnection *conn, DBusMessage *message,
40                        void *user_data)
41 {
42         const char *sender;
43         const char *interface;
44         int pid;
45
46         DBusError error;
47         dbus_error_init(&error);
48
49         sender = dbus_message_get_sender(message);
50         if (sender == NULL)
51                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
52
53         if (dbus_bus_get_unix_user(conn, sender, &error) != 0) {
54                 _E("reject by security issue - no allowed sender\n");
55                 dbus_error_free(&error);
56                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
57         }
58
59         interface = dbus_message_get_interface(message);
60         if (interface == NULL) {
61                 _E("reject by security issue - no interface\n");
62                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
63         }
64
65         if (dbus_message_is_signal(
66           message, interface, AUL_DBUS_APPDEAD_SIGNAL)) {
67                 if (dbus_message_get_args(message, &error, DBUS_TYPE_UINT32,
68                      &pid, DBUS_TYPE_INVALID) == FALSE) {
69                         _E("Failed to get data: %s", error.message);
70                         dbus_error_free(&error);
71                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
72                 }
73                 if (_app_dead_handler)
74                         _app_dead_handler(pid, _app_dead_data);
75         } else if (dbus_message_is_signal(
76           message, interface, AUL_DBUS_APPLAUNCH_SIGNAL)) {
77                 if (dbus_message_get_args(message, &error, DBUS_TYPE_UINT32,
78                      &pid, DBUS_TYPE_INVALID) == FALSE) {
79                         _E("Failed to get data: %s", error.message);
80                         dbus_error_free(&error);
81                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
82                 }
83                 if (_app_launch_handler)
84                         _app_launch_handler(pid, _app_launch_data);
85         }
86
87         return DBUS_HANDLER_RESULT_HANDLED;
88 }
89
90 int __app_dbus_signal_handler_init()
91 {
92         DBusError error;
93         char rule[MAX_LOCAL_BUFSZ];
94
95         if (app_dbus_signal_handler_initialized)
96                 return 0;
97
98         dbus_error_init(&error);
99         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
100         if (!bus) {
101                 _E("Failed to connect to the D-BUS daemon: %s", error.message);
102                 dbus_error_free(&error);
103                 return -1;
104         }
105         dbus_connection_setup_with_g_main(bus, NULL);
106
107         snprintf(rule, MAX_LOCAL_BUFSZ,
108                  "path='%s',type='signal',interface='%s'", AUL_DBUS_PATH,
109                  AUL_DBUS_SIGNAL_INTERFACE);
110         /* listening to messages */
111         dbus_bus_add_match(bus, rule, &error);
112         if (dbus_error_is_set(&error)) {
113                 _E("Fail to rule set: %s", error.message);
114                 dbus_error_free(&error);
115                 return -1;
116         }
117
118         if (dbus_connection_add_filter(bus, 
119                 __app_dbus_signal_filter, NULL, NULL) == FALSE)
120                 return -1;
121
122         app_dbus_signal_handler_initialized = 1;
123
124         _D("app signal initialized");
125
126         return 0;
127 }
128
129 int __app_dbus_signal_handler_fini()
130 {
131         DBusError error;
132         char rule[MAX_LOCAL_BUFSZ];
133
134         if (!app_dbus_signal_handler_initialized)
135                 return 0;
136
137         dbus_error_init(&error);
138
139         dbus_connection_remove_filter(bus, __app_dbus_signal_filter, NULL);
140
141         snprintf(rule, MAX_LOCAL_BUFSZ,
142                  "path='%s',type='signal',interface='%s'", AUL_DBUS_PATH,
143                  AUL_DBUS_SIGNAL_INTERFACE);
144         dbus_bus_remove_match(bus, rule, &error);
145         if (dbus_error_is_set(&error)) {
146                 _E("Fail to rule unset: %s", error.message);
147                 dbus_error_free(&error);
148                 return -1;
149         }
150
151         dbus_connection_close(bus);
152         dbus_connection_unref(bus);
153
154         app_dbus_signal_handler_initialized = 0;
155
156         _D("app signal finialized");
157
158         return 0;
159 }
160
161 SLPAPI int aul_listen_app_dead_signal(int (*func) (int, void *), void *data)
162 {
163         if (func) {
164                 if (__app_dbus_signal_handler_init() < 0) {
165                         _E("error app signal init");
166                         return AUL_R_ERROR;
167                 }
168         } else if (_app_launch_handler == NULL) {
169                 if (__app_dbus_signal_handler_fini() < 0) {
170                         _E("error app signal fini");
171                         return AUL_R_ERROR;
172                 }
173         }
174         _app_dead_handler = func;
175         _app_dead_data = data;
176
177         return AUL_R_OK;
178 }
179
180 SLPAPI int aul_listen_app_launch_signal(int (*func) (int, void *), void *data)
181 {
182         if (func) {
183                 if (__app_dbus_signal_handler_init() < 0) {
184                         _E("error app signal init");
185                         return AUL_R_ERROR;
186                 }
187         } else if (_app_dead_handler == NULL) {
188                 if (__app_dbus_signal_handler_fini() < 0) {
189                         _E("error app signal fini");
190                         return AUL_R_ERROR;
191                 }
192         }
193         _app_launch_handler = func;
194         _app_launch_data = data;
195
196         return AUL_R_OK;
197 }