tizen 2.3 release
[framework/system/deviced.git] / src / libdeviced / deviced-util.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <errno.h>
31 #include <poll.h>
32
33 #include "log.h"
34 #include "dd-deviced.h"
35 #include "deviced-priv.h"
36
37 API int deviced_get_pid(const char *execpath)
38 {
39         DIR *dp;
40         struct dirent *dentry;
41         int pid = -1, fd;
42         char buf[BUFF_MAX];
43         char buf2[BUFF_MAX];
44         int ret;
45
46         dp = opendir("/proc");
47         if (!dp) {
48                 _E("open /proc");
49                 return -1;
50         }
51
52         while ((dentry = readdir(dp)) != NULL) {
53                 if (!isdigit(dentry->d_name[0]))
54                         continue;
55
56                 pid = atoi(dentry->d_name);
57
58                 snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
59                 fd = open(buf, O_RDONLY);
60                 if (fd < 0)
61                         continue;
62
63                 ret = read(fd, buf2, BUFF_MAX);
64                 close(fd);
65
66                 if (ret < 0 || ret >=BUFF_MAX)
67                         continue;
68
69                 buf2[ret] = '\0';
70
71                 if (!strcmp(buf2, execpath)) {
72                         closedir(dp);
73                         return pid;
74                 }
75         }
76
77         errno = ESRCH;
78         closedir(dp);
79         return -1;
80 }
81
82 API int deviced_get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
83 {
84         int fd, ret;
85         char buf[PATH_MAX + 1];
86         char *filename;
87
88         if (cmdline == NULL) {
89                 errno = EINVAL;
90                 return -EINVAL;
91         }
92
93         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
94         fd = open(buf, O_RDONLY);
95         if (fd < 0) {
96                 errno = ESRCH;
97                 return -ESRCH;
98         }
99
100         ret = read(fd, buf, PATH_MAX);
101         close(fd);
102         buf[PATH_MAX] = '\0';
103
104         filename = strrchr(buf, '/');
105         if (filename == NULL)
106                 filename = buf;
107         else
108                 filename = filename + 1;
109
110         if (cmdline_size < strlen(filename) + 1) {
111                 errno = EOVERFLOW;
112                 return -EOVERFLOW;
113         }
114
115         strncpy(cmdline, filename, cmdline_size - 1);
116         cmdline[cmdline_size - 1] = '\0';
117         return 0;
118 }
119
120 API int deviced_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
121 {
122         char buf[PATH_MAX];
123         int ret;
124
125         snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
126         if (app_path == NULL
127             || (ret = readlink(buf, app_path, app_path_size)) == -1)
128                 return -1;
129         if (app_path_size == ret) {
130                 app_path[ret - 1] = '\0';
131                 errno = EOVERFLOW;
132                 return -1;
133         }
134
135         app_path[ret] = '\0';
136         return 0;
137 }