4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "app_signal.h"
26 static struct sigaction old_sigchild;
27 static DBusConnection *bus = NULL;
30 static inline void __socket_garbage_collector()
33 struct dirent *dentry;
34 char tmp[MAX_LOCAL_BUFSZ];
36 dp = opendir(AUL_SOCK_PREFIX);
40 while ((dentry = readdir(dp)) != NULL) {
41 if (!isdigit(dentry->d_name[0]))
44 snprintf(tmp, MAX_LOCAL_BUFSZ, "/proc/%s", dentry->d_name);
45 if (access(tmp, F_OK) < 0) { /* Flawfinder: ignore */
46 snprintf(tmp, MAX_LOCAL_BUFSZ, "%s/%s", AUL_SOCK_PREFIX,
55 static inline int __send_app_dead_signal(int dead_pid)
62 message = dbus_message_new_signal(AUL_DBUS_PATH,
63 AUL_DBUS_SIGNAL_INTERFACE,
64 AUL_DBUS_APPDEAD_SIGNAL);
66 if (dbus_message_append_args(message,
67 DBUS_TYPE_UINT32, &dead_pid,
68 DBUS_TYPE_INVALID) == FALSE) {
69 _E("Failed to load data error");
73 if (dbus_connection_send(bus, message, NULL) == FALSE) {
74 _E("dbus send error");
78 dbus_connection_flush(bus);
79 dbus_message_unref(message);
81 _D("send dead signal done\n");
86 static inline int __send_app_launch_signal(int launch_pid)
93 message = dbus_message_new_signal(AUL_DBUS_PATH,
94 AUL_DBUS_SIGNAL_INTERFACE,
95 AUL_DBUS_APPLAUNCH_SIGNAL);
97 if (dbus_message_append_args(message,
98 DBUS_TYPE_UINT32, &launch_pid,
99 DBUS_TYPE_INVALID) == FALSE) {
100 _E("Failed to load data error");
104 if (dbus_connection_send(bus, message, NULL) == FALSE) {
105 _E("dbus send error");
109 dbus_connection_flush(bus);
110 dbus_message_unref(message);
112 _D("send launch signal done\n");
117 static int __sigchild_action(void *data)
122 dead_pid = (pid_t) data;
123 _I("dead_pid(%d)", dead_pid);
127 ret = __send_app_dead_signal(dead_pid);
129 _I("__send_app_dead_signal(%d)", ret);
135 static void __launchpad_sig_child(int signo, siginfo_t *info, void *data)
141 child_pgid = getpgid(info->si_pid);
142 _I("dead_pid = %d pgid = %d", info->si_pid, child_pgid);
144 while ((child_pid = waitpid(-1, &status, WNOHANG)) > 0) {
145 if (child_pid == child_pgid)
146 killpg(child_pgid, SIGKILL);
147 __sigchild_action((void *)child_pid);
150 _I("after __sigchild_action");
155 static inline int __signal_init(void)
158 for (i = 0; i < _NSIG; i++) {
160 /* controlled by sys-assert package*/
178 static inline int __signal_set_sigchld(void)
180 struct sigaction act;
183 dbus_error_init(&error);
184 dbus_threads_init_default();
185 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
187 _E("Failed to connect to the D-BUS daemon: %s", error.message);
188 dbus_error_free(&error);
191 /* TODO: if process stop mechanism is included,
192 should be modified (SA_NOCLDSTOP)*/
193 act.sa_handler = NULL;
194 act.sa_sigaction = __launchpad_sig_child;
195 sigemptyset(&act.sa_mask);
196 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
198 if (sigaction(SIGCHLD, &act, &old_sigchild) < 0)
204 static inline int __signal_unset_sigchld(void)
206 struct sigaction dummy;
211 dbus_connection_close(bus);
212 if (sigaction(SIGCHLD, &old_sigchild, &dummy) < 0)
218 static inline int __signal_block_sigchld(void)
222 sigemptyset(&newmask);
223 sigaddset(&newmask, SIGCHLD);
225 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
226 _E("SIG_BLOCK error");
230 _D("SIGCHLD blocked");
235 static inline int __signal_unblock_sigchld(void)
237 if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
238 _E("SIG_SETMASK error");
242 _D("SIGCHLD unblocked");
246 static inline int __signal_fini(void)
248 #ifndef PRELOAD_ACTIVATE
250 for (i = 0; i < _NSIG; i++)