tizen_2.0_build
[framework/system/libslp-sysman.git] / sysman-util.c
1 /*
2  *  libslp-sysman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  * 
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20 */ 
21
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <dirent.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 #include <poll.h>
37
38 #include <vconf.h>
39 #include "sysman.h"
40 #include "sysman-priv.h"
41
42 API int sysman_get_pid(const char *execpath)
43 {
44         DIR *dp;
45         struct dirent *dentry;
46         int pid = -1, fd;
47         char buf[BUFF_MAX];
48         char buf2[BUFF_MAX];
49
50         dp = opendir("/proc");
51         if (!dp) {
52                 ERR("open /proc");
53                 return -1;
54         }
55
56         while ((dentry = readdir(dp)) != NULL) {
57                 if (!isdigit(dentry->d_name[0]))
58                         continue;
59
60                 pid = atoi(dentry->d_name);
61
62                 snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
63                 fd = open(buf, O_RDONLY);
64                 if (fd < 0)
65                         continue;
66                 if (read(fd, buf2, BUFF_MAX) < 0) {
67                         close(fd);
68                         continue;
69                 }
70                 close(fd);
71
72                 if (!strcmp(buf2, execpath)) {
73                         closedir(dp);
74                         return pid;
75                 }
76         }
77
78         errno = ESRCH;
79         closedir(dp);
80         return -1;
81 }
82
83 API int sysman_get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
84 {
85         int fd, ret;
86         char buf[PATH_MAX + 1];
87         char *filename;
88
89         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
90         fd = open(buf, O_RDONLY);
91         if (fd < 0) {
92                 errno = ESRCH;
93                 return -1;
94         }
95
96         ret = read(fd, buf, PATH_MAX);
97         close(fd);
98         buf[PATH_MAX] = '\0';
99
100         filename = strrchr(buf, '/');
101         if (filename == NULL)
102                 filename = buf;
103         else
104                 filename = filename + 1;
105
106         if (cmdline_size < strlen(filename) + 1) {
107                 errno = EOVERFLOW;
108                 return -1;
109         }
110
111         strncpy(cmdline, filename, cmdline_size - 1);
112         cmdline[cmdline_size - 1] = '\0';
113         return 0;
114 }
115
116 API int sysman_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
117 {
118         char buf[PATH_MAX];
119         int ret;
120
121         snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
122         if (app_path == NULL
123             || (ret = readlink(buf, app_path, app_path_size)) == -1)
124                 return -1;
125         if (app_path_size == ret) {
126                 app_path[ret - 1] = '\0';
127                 errno = EOVERFLOW;
128                 return -1;
129         }
130
131         app_path[ret] = '\0';
132         return 0;
133 }
134