4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/types.h>
34 #include "dd-deviced.h"
35 #include "deviced-priv.h"
37 API int deviced_get_pid(const char *execpath)
40 struct dirent *dentry;
46 dp = opendir("/proc");
52 while ((dentry = readdir(dp)) != NULL) {
53 if (!isdigit(dentry->d_name[0]))
56 pid = atoi(dentry->d_name);
58 snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
59 fd = open(buf, O_RDONLY);
63 ret = read(fd, buf2, BUFF_MAX);
66 if (ret < 0 || ret >=BUFF_MAX)
71 if (!strcmp(buf2, execpath)) {
82 API int deviced_get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
85 char buf[PATH_MAX + 1];
88 if (cmdline == NULL) {
93 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
94 fd = open(buf, O_RDONLY);
100 ret = read(fd, buf, PATH_MAX);
102 buf[PATH_MAX] = '\0';
104 filename = strrchr(buf, '/');
105 if (filename == NULL)
108 filename = filename + 1;
110 if (cmdline_size < strlen(filename) + 1) {
115 strncpy(cmdline, filename, cmdline_size - 1);
116 cmdline[cmdline_size - 1] = '\0';
120 API int deviced_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
125 snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
127 || (ret = readlink(buf, app_path, app_path_size)) == -1)
129 if (app_path_size == ret) {
130 app_path[ret - 1] = '\0';
135 app_path[ret] = '\0';