2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
19 #include "app_signal.h"
21 static struct sigaction old_sigchild;
22 static DBusConnection *bus = NULL;
25 static inline void __socket_garbage_collector()
28 struct dirent *dentry;
29 char tmp[MAX_LOCAL_BUFSZ];
31 dp = opendir(AUL_SOCK_PREFIX);
35 while ((dentry = readdir(dp)) != NULL) {
36 if (!isdigit(dentry->d_name[0]))
39 snprintf(tmp, MAX_LOCAL_BUFSZ, "/proc/%s", dentry->d_name);
40 if (access(tmp, F_OK) < 0) { /* Flawfinder: ignore */
41 snprintf(tmp, MAX_LOCAL_BUFSZ, "%s/%s", AUL_SOCK_PREFIX,
50 static inline int __send_app_dead_signal(int dead_pid)
57 message = dbus_message_new_signal(AUL_DBUS_PATH,
58 AUL_DBUS_SIGNAL_INTERFACE,
59 AUL_DBUS_APPDEAD_SIGNAL);
61 if (dbus_message_append_args(message,
62 DBUS_TYPE_UINT32, &dead_pid,
63 DBUS_TYPE_INVALID) == FALSE) {
64 _E("Failed to load data error");
68 if (dbus_connection_send(bus, message, NULL) == FALSE) {
69 _E("dbus send error");
73 dbus_connection_flush(bus);
74 dbus_message_unref(message);
76 _D("send dead signal done\n");
81 static inline int __send_app_launch_signal(int launch_pid)
88 message = dbus_message_new_signal(AUL_DBUS_PATH,
89 AUL_DBUS_SIGNAL_INTERFACE,
90 AUL_DBUS_APPLAUNCH_SIGNAL);
92 if (dbus_message_append_args(message,
93 DBUS_TYPE_UINT32, &launch_pid,
94 DBUS_TYPE_INVALID) == FALSE) {
95 _E("Failed to load data error");
99 if (dbus_connection_send(bus, message, NULL) == FALSE) {
100 _E("dbus send error");
104 dbus_connection_flush(bus);
105 dbus_message_unref(message);
107 _D("send launch signal done\n");
112 static int __sigchild_action(void *data)
115 char buf[MAX_LOCAL_BUFSZ];
117 dead_pid = (pid_t) data;
121 __send_app_dead_signal(dead_pid);
123 snprintf(buf, MAX_LOCAL_BUFSZ, "%s/%d", AUL_SOCK_PREFIX, dead_pid);
126 __socket_garbage_collector();
131 static void __launchpad_sig_child(int signo, siginfo_t *info, void *data)
137 // warning: unused parameter
141 child_pgid = getpgid(info->si_pid);
142 _D("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);
153 static inline int __signal_init(void)
156 for (i = 0; i < _NSIG; i++) {
158 /* controlled by sys-assert package*/
176 static inline int __signal_set_sigchld(void)
178 struct sigaction act;
181 dbus_error_init(&error);
182 dbus_threads_init_default();
183 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
185 _E("Failed to connect to the D-BUS daemon: %s", error.message);
186 dbus_error_free(&error);
189 /* TODO: if process stop mechanism is included,
190 should be modified (SA_NOCLDSTOP)*/
191 act.sa_handler = NULL;
192 act.sa_sigaction = __launchpad_sig_child;
193 sigemptyset(&act.sa_mask);
194 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
196 if (sigaction(SIGCHLD, &act, &old_sigchild) < 0)
202 static inline int __signal_unset_sigchld(void)
204 struct sigaction dummy;
209 dbus_connection_close(bus);
210 if (sigaction(SIGCHLD, &old_sigchild, &dummy) < 0)
216 static inline int __signal_block_sigchld(void)
220 sigemptyset(&newmask);
221 sigaddset(&newmask, SIGCHLD);
223 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
224 _E("SIG_BLOCK error");
228 _D("SIGCHLD blocked");
233 static inline int __signal_unblock_sigchld(void)
235 if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
236 _E("SIG_SETMASK error");
240 _D("SIGCHLD unblocked");
244 static inline int __signal_fini(void)
246 #ifndef PRELOAD_ACTIVATE
248 for (i = 0; i < _NSIG; i++)