4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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.
25 #include <sys/types.h>
38 #include "dd-deviced.h"
39 #include "deviced-priv.h"
41 API int deviced_get_pid(const char *execpath)
44 struct dirent *dentry;
49 dp = opendir("/proc");
55 while ((dentry = readdir(dp)) != NULL) {
56 if (!isdigit(dentry->d_name[0]))
59 pid = atoi(dentry->d_name);
61 snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
62 fd = open(buf, O_RDONLY);
65 if (read(fd, buf2, BUFF_MAX) < 0) {
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 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
89 fd = open(buf, O_RDONLY);
95 ret = read(fd, buf, PATH_MAX);
99 filename = strrchr(buf, '/');
100 if (filename == NULL)
103 filename = filename + 1;
105 if (cmdline_size < strlen(filename) + 1) {
110 strncpy(cmdline, filename, cmdline_size - 1);
111 cmdline[cmdline_size - 1] = '\0';
115 API int deviced_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
120 snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
122 || (ret = readlink(buf, app_path, app_path_size)) == -1)
124 if (app_path_size == ret) {
125 app_path[ret - 1] = '\0';
130 app_path[ret] = '\0';