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 /* chmod and chsmack to read file without root privilege */
120 static void __chmod_chsmack_toread(const char * path)
123 if(dlp_chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 0))
125 _E("unable to set 644 to %s", path);
127 _D("set 644 to %s", path);
131 if(smack_setlabel(path, "*", SMACK_LABEL_ACCESS))
133 _E("failed chsmack -a \"*\" %s", path);
135 _D("chsmack -a \"*\" %s", path);
141 static int __sigchild_action(void *data)
144 char buf[MAX_LOCAL_BUFSZ];
146 dead_pid = (pid_t) data;
150 /* send app pid instead of gdbserver pid */
151 if(dead_pid == gdbserver_pid)
152 dead_pid = gdbserver_app_pid;
154 /* valgrind xml file */
155 if(access(PATH_VALGRIND_XMLFILE,F_OK)==0)
157 __chmod_chsmack_toread(PATH_VALGRIND_XMLFILE);
160 __send_app_dead_signal(dead_pid);
162 snprintf(buf, MAX_LOCAL_BUFSZ, "%s/%d", AUL_SOCK_PREFIX, dead_pid);
165 __socket_garbage_collector();
170 static void __launchpad_sig_child(int signo, siginfo_t *info, void *data)
176 child_pgid = getpgid(info->si_pid);
177 _D("dead_pid = %d pgid = %d", info->si_pid, child_pgid);
179 while ((child_pid = waitpid(-1, &status, WNOHANG)) > 0) {
180 if (child_pid == child_pgid)
181 killpg(child_pgid, SIGKILL);
182 __sigchild_action((void *)child_pid);
188 static inline int __signal_init(void)
191 for (i = 0; i < _NSIG; i++) {
193 /* controlled by sys-assert package*/
211 static inline int __signal_set_sigchld(void)
213 struct sigaction act;
216 dbus_error_init(&error);
217 dbus_threads_init_default();
218 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
220 _E("Failed to connect to the D-BUS daemon: %s", error.message);
221 dbus_error_free(&error);
224 /* TODO: if process stop mechanism is included,
225 should be modified (SA_NOCLDSTOP)*/
226 act.sa_handler = NULL;
227 act.sa_sigaction = __launchpad_sig_child;
228 sigemptyset(&act.sa_mask);
229 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
231 if (sigaction(SIGCHLD, &act, &old_sigchild) < 0)
237 static inline int __signal_unset_sigchld(void)
239 struct sigaction dummy;
244 dbus_connection_close(bus);
245 if (sigaction(SIGCHLD, &old_sigchild, &dummy) < 0)
251 static inline int __signal_block_sigchld(void)
255 sigemptyset(&newmask);
256 sigaddset(&newmask, SIGCHLD);
258 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
259 _E("SIG_BLOCK error");
263 _D("SIGCHLD blocked");
268 static inline int __signal_unblock_sigchld(void)
270 if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
271 _E("SIG_SETMASK error");
275 _D("SIGCHLD unblocked");
279 static inline int __signal_fini(void)
281 #ifndef PRELOAD_ACTIVATE
283 for (i = 0; i < _NSIG; i++)