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>
30 #include "simple_util.h"
32 #define BINSH_NAME "/bin/sh"
35 #define PROC_STAT_GID_POS 5
38 static inline int __read_proc(const char *path, char *buf, int size);
39 static inline int __find_pid_by_cmdline(const char *dname,
40 const char *cmdline, void *priv);
41 static inline int __get_pgid_from_stat(int pid);
44 static inline int __read_proc(const char *path, char *buf, int size)
49 if (buf == NULL || path == NULL)
52 fd = open(path, O_RDONLY);
56 ret = read(fd, buf, size - 1);
68 static inline int __find_pid_by_cmdline(const char *dname,
69 const char *cmdline, void *priv)
74 apppath = (char *)priv;
75 if (strncmp(cmdline, apppath, MAX_LOCAL_BUFSZ-1) == 0) {
77 if (pid != getpgid(pid))
84 int __proc_iter_cmdline(
85 int (*iterfunc)(const char *dname, const char *cmdline, void *priv),
89 struct dirent *dentry;
92 char buf[MAX_LOCAL_BUFSZ];
94 dp = opendir("/proc");
100 iterfunc = __find_pid_by_cmdline;
102 while ((dentry = readdir(dp)) != NULL) {
103 if (!isdigit(dentry->d_name[0]))
106 snprintf(buf, sizeof(buf), "/proc/%s/cmdline", dentry->d_name);
107 ret = __read_proc(buf, buf, sizeof(buf));
111 /* support app launched by shell script*/
112 if (strncmp(buf, BINSH_NAME, BINSH_SIZE) == 0)
114 iterfunc(dentry->d_name, &buf[BINSH_SIZE + 1],
117 pid = iterfunc(dentry->d_name, buf, priv);
129 char *__proc_get_cmdline_bypid(int pid)
131 char buf[MAX_LOCAL_BUFSZ];
134 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
135 ret = __read_proc(buf, buf, sizeof(buf));
139 /* support app launched by shell script*/
140 if (strncmp(buf, BINSH_NAME, BINSH_SIZE) == 0)
141 return strdup(&buf[BINSH_SIZE + 1]);
146 static inline int __get_pgid_from_stat(int pid)
148 char buf[MAX_LOCAL_BUFSZ];
157 snprintf(buf, sizeof(buf), "/proc/%d/stat", pid);
158 ret = __read_proc(buf, buf, sizeof(buf));
162 for (i = 0; i < (ret - 1); i++) {
165 if (count == PROC_STAT_GID_POS - 1)
167 else if (count == PROC_STAT_GID_POS) {
174 if (count == PROC_STAT_GID_POS)
182 int __proc_iter_pgid(int pgid, int (*iterfunc) (int pid, void *priv),
186 struct dirent *dentry;
190 dp = opendir("/proc");
195 while ((dentry = readdir(dp)) != NULL) {
196 if (!isdigit(dentry->d_name[0]))
199 _pgid = __get_pgid_from_stat(atoi(dentry->d_name));
201 ret = iterfunc(atoi(dentry->d_name), priv);