2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
22 #include <sys/types.h>
35 #include "sysman-priv.h"
37 API int sysman_get_pid(const char *execpath)
40 struct dirent *dentry;
45 dp = opendir("/proc");
51 while ((dentry = readdir(dp)) != NULL) {
52 if (!isdigit(dentry->d_name[0]))
55 pid = atoi(dentry->d_name);
57 snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
58 fd = open(buf, O_RDONLY);
61 if (read(fd, buf2, BUFF_MAX) < 0) {
67 if (!strcmp(buf2, execpath)) {
78 API int sysman_get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
81 char buf[PATH_MAX + 1];
84 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
85 fd = open(buf, O_RDONLY);
91 ret = read(fd, buf, PATH_MAX);
95 filename = strrchr(buf, '/');
99 filename = filename + 1;
101 if (cmdline_size < strlen(filename) + 1) {
106 strncpy(cmdline, filename, cmdline_size - 1);
107 cmdline[cmdline_size - 1] = '\0';
111 API int sysman_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
116 snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
118 || (ret = readlink(buf, app_path, app_path_size)) == -1)
120 if (app_path_size == ret) {
121 app_path[ret - 1] = '\0';
126 app_path[ret] = '\0';