change system header file from dd-system to dd-deviced
[platform/core/system/system-server.git] / src / shared / deviced-util.c
1 /*
2  *  deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <dirent.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <errno.h>
34 #include <poll.h>
35
36 #include <vconf.h>
37 #include "log.h"
38 #include "dd-deviced.h"
39 #include "deviced-priv.h"
40
41 API int deviced_get_pid(const char *execpath)
42 {
43         DIR *dp;
44         struct dirent *dentry;
45         int pid = -1, fd;
46         char buf[BUFF_MAX];
47         char buf2[BUFF_MAX];
48
49         dp = opendir("/proc");
50         if (!dp) {
51                 _E("open /proc");
52                 return -1;
53         }
54
55         while ((dentry = readdir(dp)) != NULL) {
56                 if (!isdigit(dentry->d_name[0]))
57                         continue;
58
59                 pid = atoi(dentry->d_name);
60
61                 snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
62                 fd = open(buf, O_RDONLY);
63                 if (fd < 0)
64                         continue;
65                 if (read(fd, buf2, BUFF_MAX) < 0) {
66                         close(fd);
67                         continue;
68                 }
69                 close(fd);
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         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
89         fd = open(buf, O_RDONLY);
90         if (fd < 0) {
91                 errno = ESRCH;
92                 return -1;
93         }
94
95         ret = read(fd, buf, PATH_MAX);
96         close(fd);
97         buf[PATH_MAX] = '\0';
98
99         filename = strrchr(buf, '/');
100         if (filename == NULL)
101                 filename = buf;
102         else
103                 filename = filename + 1;
104
105         if (cmdline_size < strlen(filename) + 1) {
106                 errno = EOVERFLOW;
107                 return -1;
108         }
109
110         strncpy(cmdline, filename, cmdline_size - 1);
111         cmdline[cmdline_size - 1] = '\0';
112         return 0;
113 }
114
115 API int deviced_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
116 {
117         char buf[PATH_MAX];
118         int ret;
119
120         snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
121         if (app_path == NULL
122             || (ret = readlink(buf, app_path, app_path_size)) == -1)
123                 return -1;
124         if (app_path_size == ret) {
125                 app_path[ret - 1] = '\0';
126                 errno = EOVERFLOW;
127                 return -1;
128         }
129
130         app_path[ret] = '\0';
131         return 0;
132 }
133