summaryrefslogtreecommitdiff
path: root/sysman-util.c
blob: fb5f8d1750d05e3f597872fca8fb214a06efacaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <poll.h>

#include <vconf.h>
#include "sysman.h"
#include "sysman-priv.h"

API int sysman_get_pid(const char *execpath)
{
	DIR *dp;
	struct dirent *dentry;
	int pid = -1, fd;
	char buf[BUFF_MAX];
	char buf2[BUFF_MAX];

	dp = opendir("/proc");
	if (!dp) {
		ERR("open /proc");
		return -1;
	}

	while ((dentry = readdir(dp)) != NULL) {
		if (!isdigit(dentry->d_name[0]))
			continue;

		pid = atoi(dentry->d_name);

		snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
		fd = open(buf, O_RDONLY);
		if (fd < 0)
			continue;
		if (read(fd, buf2, BUFF_MAX) < 0) {
			close(fd);
			continue;
		}
		close(fd);

		if (!strcmp(buf2, execpath)) {
			closedir(dp);
			return pid;
		}
	}

	errno = ESRCH;
	closedir(dp);
	return -1;
}

API int sysman_get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
{
	int fd, ret;
	char buf[PATH_MAX + 1];
	char *filename;

	snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
	fd = open(buf, O_RDONLY);
	if (fd < 0) {
		errno = ESRCH;
		return -1;
	}

	ret = read(fd, buf, PATH_MAX);
	close(fd);
	buf[PATH_MAX] = '\0';

	filename = strrchr(buf, '/');
	if (filename == NULL)
		filename = buf;
	else
		filename = filename + 1;

	if (cmdline_size < strlen(filename) + 1) {
		errno = EOVERFLOW;
		return -1;
	}

	strncpy(cmdline, filename, cmdline_size - 1);
	cmdline[cmdline_size - 1] = '\0';
	return 0;
}

API int sysman_get_apppath(pid_t pid, char *app_path, size_t app_path_size)
{
	char buf[PATH_MAX];
	int ret;

	snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
	if (app_path == NULL
	    || (ret = readlink(buf, app_path, app_path_size)) == -1)
		return -1;
	if (app_path_size == ret) {
		app_path[ret - 1] = '\0';
		errno = EOVERFLOW;
		return -1;
	}

	app_path[ret] = '\0';
	return 0;
}