Tizen 2.0 Release
[platform/core/system/libslp-sysman.git] / sysman-util.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <signal.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 <vconf.h>
34 #include "sysman.h"
35 #include "sysman-priv.h"
36
37 API int sysman_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
45         dp = opendir("/proc");
46         if (!dp) {
47                 ERR("open /proc");
48                 return -1;
49         }
50
51         while ((dentry = readdir(dp)) != NULL) {
52                 if (!isdigit(dentry->d_name[0]))
53                         continue;
54
55                 pid = atoi(dentry->d_name);
56
57                 snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
58                 fd = open(buf, O_RDONLY);
59                 if (fd < 0)
60                         continue;
61                 if (read(fd, buf2, BUFF_MAX) < 0) {
62                         close(fd);
63                         continue;
64                 }
65                 close(fd);
66
67                 if (!strcmp(buf2, execpath)) {
68                         closedir(dp);
69                         return pid;
70                 }
71         }
72
73         errno = ESRCH;
74         closedir(dp);
75         return -1;
76 }
77
78 API int sysman_get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
79 {
80         int fd, ret;
81         char buf[PATH_MAX + 1];
82         char *filename;
83
84         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
85         fd = open(buf, O_RDONLY);
86         if (fd < 0) {
87                 errno = ESRCH;
88                 return -1;
89         }
90
91         ret = read(fd, buf, PATH_MAX);
92         close(fd);
93         buf[PATH_MAX] = '\0';
94
95         filename = strrchr(buf, '/');
96         if (filename == NULL)
97                 filename = buf;
98         else
99                 filename = filename + 1;
100
101         if (cmdline_size < strlen(filename) + 1) {
102                 errno = EOVERFLOW;
103                 return -1;
104         }
105
106         strncpy(cmdline, filename, cmdline_size - 1);
107         cmdline[cmdline_size - 1] = '\0';
108         return 0;
109 }
110
111 API int sysman_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
112 {
113         char buf[PATH_MAX];
114         int ret;
115
116         snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
117         if (app_path == NULL
118             || (ret = readlink(buf, app_path, app_path_size)) == -1)
119                 return -1;
120         if (app_path_size == ret) {
121                 app_path[ret - 1] = '\0';
122                 errno = EOVERFLOW;
123                 return -1;
124         }
125
126         app_path[ret] = '\0';
127         return 0;
128 }
129