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.
18 #include "app_signal.h"
20 static struct sigaction old_sigchild;
21 static DBusConnection *bus = NULL;
24 static inline void __socket_garbage_collector()
27 struct dirent *dentry;
28 char tmp[MAX_LOCAL_BUFSZ];
30 dp = opendir(AUL_SOCK_PREFIX);
35 while ((dentry = readdir(dp)) != NULL) {
36 if (!isdigit(dentry->d_name[0])) {
40 snprintf(tmp, MAX_LOCAL_BUFSZ, "/proc/%s", dentry->d_name);
41 if (access(tmp, F_OK) < 0) { /* Flawfinder: ignore */
42 snprintf(tmp, MAX_LOCAL_BUFSZ, "%s/%s", AUL_SOCK_PREFIX,
51 static inline int __send_app_dead_signal(int dead_pid)
59 message = dbus_message_new_signal(AUL_DBUS_PATH,
60 AUL_DBUS_SIGNAL_INTERFACE,
61 AUL_DBUS_APPDEAD_SIGNAL);
63 if (dbus_message_append_args(message,
64 DBUS_TYPE_UINT32, &dead_pid,
65 DBUS_TYPE_INVALID) == FALSE)
67 _E("Failed to load data error");
71 if (dbus_connection_send(bus, message, NULL) == FALSE) {
72 _E("dbus send error");
76 dbus_connection_flush(bus);
77 dbus_message_unref(message);
79 _D("send dead signal done\n");
84 static inline int __send_app_launch_signal(int launch_pid)
92 message = dbus_message_new_signal(AUL_DBUS_PATH,
93 AUL_DBUS_SIGNAL_INTERFACE,
94 AUL_DBUS_APPLAUNCH_SIGNAL);
96 if (dbus_message_append_args(message,
97 DBUS_TYPE_UINT32, &launch_pid,
98 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)
120 char buf[MAX_LOCAL_BUFSZ];
122 dead_pid = (pid_t) data;
127 __send_app_dead_signal(dead_pid);
129 snprintf(buf, MAX_LOCAL_BUFSZ, "%s/%d", AUL_SOCK_PREFIX, dead_pid);
132 __socket_garbage_collector();
137 static void __launchpad_sig_child(int signo, siginfo_t *info, void *data)
143 // warning: unused parameter
147 child_pgid = getpgid(info->si_pid);
148 _D("dead_pid = %d pgid = %d", info->si_pid, child_pgid);
150 while ((child_pid = waitpid(-1, &status, WNOHANG)) > 0) {
151 if (child_pid == child_pgid) {
152 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) {
210 static inline int __signal_unset_sigchld(void)
212 struct sigaction dummy;
218 dbus_connection_close(bus);
219 if (sigaction(SIGCHLD, &old_sigchild, &dummy) < 0) {
226 static inline int __signal_block_sigchld(void)
230 sigemptyset(&newmask);
231 sigaddset(&newmask, SIGCHLD);
233 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
234 _E("SIG_BLOCK error");
238 _D("SIGCHLD blocked");
243 static inline int __signal_unblock_sigchld(void)
245 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
246 _E("SIG_SETMASK error");
250 _D("SIGCHLD unblocked");
254 static inline int __signal_fini(void)
256 #ifndef PRELOAD_ACTIVATE
258 for (i = 0; i < _NSIG; i++) {