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 <sys/types.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include "simple_util.h"
36 #define BINSH_NAME "/bin/sh"
38 #define VALGRIND_NAME "/home/developer/sdk_tools/valgrind/usr/bin/valgrind"
39 #define VALGRIND_SIZE 51
40 #define BASH_NAME "/bin/bash"
42 #define OPROFILE_NAME "/usr/bin/oprofile_command"
43 #define OPROFILE_SIZE 25
44 #define OPTION_VALGRIND_NAME "valgrind"
45 #define OPTION_VALGRIND_SIZE 8
48 #define PROC_STAT_GID_POS 5
51 static inline int __read_proc(const char *path, char *buf, int size);
52 static inline int __find_pid_by_cmdline(const char *dname,
53 const char *cmdline, void *priv);
54 static inline int __get_pgid_from_stat(int pid);
57 static inline int __read_proc(const char *path, char *buf, int size)
62 if (buf == NULL || path == NULL)
65 fd = open(path, O_RDONLY);
69 ret = read(fd, buf, size - 1);
81 static inline int __find_pid_by_cmdline(const char *dname,
82 const char *cmdline, void *priv)
87 apppath = (char *)priv;
88 if (strncmp(cmdline, apppath, MAX_LOCAL_BUFSZ-1) == 0) {
90 if (pid != getpgid(pid))
97 int __proc_iter_cmdline(
98 int (*iterfunc)(const char *dname, const char *cmdline, void *priv),
102 struct dirent *dentry;
105 char buf[MAX_LOCAL_BUFSZ];
107 dp = opendir("/proc");
112 if (iterfunc == NULL)
113 iterfunc = __find_pid_by_cmdline;
115 while ((dentry = readdir(dp)) != NULL) {
116 if (!isdigit(dentry->d_name[0]))
119 snprintf(buf, sizeof(buf), "/proc/%s/cmdline", dentry->d_name);
120 ret = __read_proc(buf, buf, sizeof(buf));
124 /* support app launched by shell script*/
125 if (strncmp(buf, BINSH_NAME, BINSH_SIZE) == 0)
127 iterfunc(dentry->d_name, &buf[BINSH_SIZE + 1],
130 pid = iterfunc(dentry->d_name, buf, priv);
142 char *__proc_get_cmdline_bypid(int pid)
144 #define MAX_CMD_BUFSZ 1024
146 char buf[MAX_CMD_BUFSZ];
149 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
150 ret = __read_proc(buf, buf, sizeof(buf));
154 /* support app launched by shell script*/
155 if (strncmp(buf, BINSH_NAME, BINSH_SIZE) == 0) {
156 return strdup(&buf[BINSH_SIZE + 1]);
158 else if (strncmp(buf, VALGRIND_NAME, VALGRIND_SIZE) == 0) {
161 // buf comes with double null-terminated string
171 // ignore trailing "--"
172 if (strncmp(ptr, "-", 1) != 0)
178 else if (strncmp(buf, BASH_NAME, BASH_SIZE) == 0) {
179 if (strncmp(&buf[BASH_SIZE + 1], OPROFILE_NAME, OPROFILE_SIZE) == 0) {
180 if (strncmp(&buf[BASH_SIZE + OPROFILE_SIZE + 2], OPTION_VALGRIND_NAME, OPTION_VALGRIND_SIZE) == 0) {
181 return strdup(&buf[BASH_SIZE + OPROFILE_SIZE + OPTION_VALGRIND_SIZE + 3]);
189 static inline int __get_pgid_from_stat(int pid)
191 char buf[MAX_LOCAL_BUFSZ];
200 snprintf(buf, sizeof(buf), "/proc/%d/stat", pid);
201 ret = __read_proc(buf, buf, sizeof(buf));
205 for (i = 0; i < (ret - 1); i++) {
208 if (count == PROC_STAT_GID_POS - 1)
210 else if (count == PROC_STAT_GID_POS) {
217 if (count == PROC_STAT_GID_POS && str)
225 int __proc_iter_pgid(int pgid, int (*iterfunc) (int pid, void *priv),
229 struct dirent *dentry;
233 dp = opendir("/proc");
238 while ((dentry = readdir(dp)) != NULL) {
239 if (!isdigit(dentry->d_name[0]))
242 _pgid = __get_pgid_from_stat(atoi(dentry->d_name));
244 ret = iterfunc(atoi(dentry->d_name), priv);
254 void __trm_app_info_send_socket(char *write_buf)
256 const char trm_socket_for_app_info[] = "/dev/socket/app_info";
259 struct sockaddr_un addr;
261 _D("__trm_app_info_send_socket");
263 if (access(trm_socket_for_app_info, F_OK) != 0) {
268 socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
274 memset(&addr, 0, sizeof(addr));
275 snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", trm_socket_for_app_info);
276 addr.sun_family = AF_LOCAL;
278 ret = connect(socket_fd, (struct sockaddr *) &addr ,sizeof(sa_family_t) + strlen(trm_socket_for_app_info) );
284 ret = send(socket_fd, write_buf, strlen(write_buf), MSG_DONTWAIT | MSG_NOSIGNAL);
286 _E("Unable to send data. Error is %s\n",strerror(errno));