4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jungmin Cho <chivalry.cho@samsung.com>, Gwangho Hwang <gwang.hwang@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;
29 static int gdbserver_pid;
30 static int gdbserver_app_pid;
32 static inline void __socket_garbage_collector()
35 struct dirent *dentry;
36 char tmp[MAX_LOCAL_BUFSZ];
38 dp = opendir(AUL_SOCK_PREFIX);
42 while ((dentry = readdir(dp)) != NULL) {
43 if (!isdigit(dentry->d_name[0]))
46 snprintf(tmp, MAX_LOCAL_BUFSZ, "/proc/%s", dentry->d_name);
47 if (access(tmp, F_OK) < 0) { /* Flawfinder: ignore */
48 snprintf(tmp, MAX_LOCAL_BUFSZ, "%s/%s", AUL_SOCK_PREFIX,
57 static inline int __send_app_dead_signal(int dead_pid)
64 message = dbus_message_new_signal(AUL_DBUS_PATH,
65 AUL_DBUS_SIGNAL_INTERFACE,
66 AUL_DBUS_APPDEAD_SIGNAL);
68 if (dbus_message_append_args(message,
69 DBUS_TYPE_UINT32, &dead_pid,
70 DBUS_TYPE_INVALID) == FALSE) {
71 _E("Failed to load data error");
75 if (dbus_connection_send(bus, message, NULL) == FALSE) {
76 _E("dbus send error");
80 dbus_connection_flush(bus);
81 dbus_message_unref(message);
83 _D("send dead signal done\n");
88 static inline int __send_app_launch_signal(int launch_pid)
95 message = dbus_message_new_signal(AUL_DBUS_PATH,
96 AUL_DBUS_SIGNAL_INTERFACE,
97 AUL_DBUS_APPLAUNCH_SIGNAL);
99 if (dbus_message_append_args(message,
100 DBUS_TYPE_UINT32, &launch_pid,
101 DBUS_TYPE_INVALID) == FALSE) {
102 _E("Failed to load data error");
106 if (dbus_connection_send(bus, message, NULL) == FALSE) {
107 _E("dbus send error");
111 dbus_connection_flush(bus);
112 dbus_message_unref(message);
114 _D("send launch signal done\n");
119 static int __sigchild_action(void *data)
122 char buf[MAX_LOCAL_BUFSZ];
124 dead_pid = (pid_t) data;
128 /* send app pid instead of gdbserver pid */
129 if(dead_pid == gdbserver_pid)
130 dead_pid = gdbserver_app_pid;
132 __send_app_dead_signal(dead_pid);
134 snprintf(buf, MAX_LOCAL_BUFSZ, "%s/%d", AUL_SOCK_PREFIX, dead_pid);
137 __socket_garbage_collector();
142 static void __launchpad_sig_child(int signo, siginfo_t *info, void *data)
148 child_pgid = getpgid(info->si_pid);
149 _D("dead_pid = %d pgid = %d", info->si_pid, child_pgid);
151 while ((child_pid = waitpid(-1, &status, WNOHANG)) > 0) {
152 if (child_pid == child_pgid)
153 killpg(child_pgid, SIGKILL);
154 __sigchild_action((void *)child_pid);
160 static inline int __signal_init(void)
163 for (i = 0; i < _NSIG; i++) {
165 /* controlled by sys-assert package*/
183 static inline int __signal_set_sigchld(void)
185 struct sigaction act;
188 dbus_error_init(&error);
189 dbus_threads_init_default();
190 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
192 _E("Failed to connect to the D-BUS daemon: %s", error.message);
193 dbus_error_free(&error);
196 /* TODO: if process stop mechanism is included,
197 should be modified (SA_NOCLDSTOP)*/
198 act.sa_handler = NULL;
199 act.sa_sigaction = __launchpad_sig_child;
200 sigemptyset(&act.sa_mask);
201 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
203 if (sigaction(SIGCHLD, &act, &old_sigchild) < 0)
209 static inline int __signal_unset_sigchld(void)
211 struct sigaction dummy;
216 dbus_connection_close(bus);
217 if (sigaction(SIGCHLD, &old_sigchild, &dummy) < 0)
223 static inline int __signal_block_sigchld(void)
227 sigemptyset(&newmask);
228 sigaddset(&newmask, SIGCHLD);
230 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
231 _E("SIG_BLOCK error");
235 _D("SIGCHLD blocked");
240 static inline int __signal_unblock_sigchld(void)
242 if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
243 _E("SIG_SETMASK error");
247 _D("SIGCHLD unblocked");
251 static inline int __signal_fini(void)
253 #ifndef PRELOAD_ACTIVATE
255 for (i = 0; i < _NSIG; i++)