2 * collectd - src/utils/proc_pids/proc_pids.c
4 * Copyright(c) 2018-2019 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Starzyk, Mateusz <mateuszx.starzyk@intel.com>
26 * Wojciech Andralojc <wojciechx.andralojc@intel.com>
27 * Michał Aleksiński <michalx.aleksinski@intel.com>
31 #include "utils/common/common.h"
32 #include "utils/proc_pids/proc_pids.h"
34 #define UTIL_NAME "utils_proc_pids"
36 void pids_list_free(pids_list_t *list) {
43 int proc_pids_is_name_valid(const char *name) {
46 unsigned len = strlen(name);
47 if (len > 0 && len <= MAX_PROC_NAME_LEN)
51 ": Process name \'%s\' is too long. Max supported len is %d chars.",
52 name, MAX_PROC_NAME_LEN);
59 int pids_list_add_pid(pids_list_t *list, const pid_t pid) {
62 if (list->allocated == list->size) {
63 size_t new_allocated = list->allocated + 1 + list->allocated / 10;
64 pid_t *new_pids = realloc(list->pids, sizeof(pid_t) * new_allocated);
66 if (NULL == new_pids) {
67 ERROR(UTIL_NAME ": Alloc error\n");
71 list->pids = new_pids;
72 list->allocated = new_allocated;
75 list->pids[list->size] = pid;
81 int pids_list_add_list(pids_list_t *dst, pids_list_t *src) {
85 if (dst->allocated < dst->size + src->size) {
87 realloc(dst->pids, sizeof(pid_t) * (dst->size + src->size));
89 if (NULL == new_pids) {
90 ERROR(UTIL_NAME ": Alloc error\n");
94 dst->allocated = dst->size + src->size;
98 memcpy(dst->pids + dst->size, src->pids, src->size * sizeof(*(src->pids)));
99 dst->size += src->size;
104 int pids_list_clear(pids_list_t *list) {
107 if (list->pids != NULL)
116 int pids_list_contains_pid(pids_list_t *list, const pid_t pid) {
119 for (int i = 0; i < list->size; i++)
120 if (list->pids[i] == pid)
131 * Reads process name from given pid directory.
132 * Strips new-line character (\n).
135 * `procfs_path' Path to systems proc directory (e.g. /proc)
136 * `pid_entry' Dirent for PID directory
137 * `name' Output buffer for process name, recommended proc_comm.
138 * `out_size' Output buffer size, recommended sizeof(proc_comm)
141 * On success, the number of read bytes (includes stripped \n).
142 * -1 on file open error
144 static int read_proc_name(const char *procfs_path,
145 const struct dirent *pid_entry, char *name,
146 const size_t out_size) {
150 memset(name, 0, out_size);
152 const char *comm_file_name = "comm";
154 char *path = ssnprintf_alloc("%s/%s/%s", procfs_path, pid_entry->d_name,
158 FILE *f = fopen(path, "r");
160 ERROR(UTIL_NAME ": Failed to open comm file, error: %d\n", errno);
164 size_t read_length = fread(name, sizeof(char), out_size, f);
165 name[out_size - 1] = '\0';
168 /* strip new line ending */
169 char *newline = strchr(name, '\n');
182 * Gets pid number for given /proc/pid directory entry or
183 * returns error if input directory does not hold PID information.
186 * `entry' Dirent for PID directory
187 * `pid' PID number to be filled
190 * 0 on success. -1 on error.
192 static int get_pid_number(struct dirent *entry, pid_t *pid) {
193 char *tmp_end; /* used for strtoul error check*/
195 if (pid == NULL || entry == NULL)
198 if (entry->d_type != DT_DIR)
201 /* trying to get pid number from directory name*/
202 *pid = strtoul(entry->d_name, &tmp_end, 10);
203 if (*tmp_end != '\0') {
204 return -1; /* conversion failed, not proc-pid */
206 /* all checks passed, marking as success */
210 int proc_pids_init(const char **procs_names_array,
211 const size_t procs_names_array_size,
212 proc_pids_t **proc_pids[]) {
214 proc_pids_t **proc_pids_array;
216 assert(NULL == *proc_pids);
218 /* Copy procs names to output array. Initialize pids list with NULL value. */
219 proc_pids_array = calloc(procs_names_array_size, sizeof(*proc_pids_array));
221 if (NULL == proc_pids_array)
224 for (size_t i = 0; i < procs_names_array_size; ++i) {
225 proc_pids_array[i] = calloc(1, sizeof(**proc_pids_array));
226 if (NULL == proc_pids_array[i])
227 goto proc_pids_init_error;
229 sstrncpy(proc_pids_array[i]->process_name, procs_names_array[i],
230 STATIC_ARRAY_SIZE(proc_pids_array[i]->process_name));
231 proc_pids_array[i]->prev = NULL;
232 proc_pids_array[i]->curr = NULL;
235 *proc_pids = proc_pids_array;
238 proc_pids_init_error:
239 if (NULL != proc_pids_array) {
240 for (size_t i = 0; i < procs_names_array_size; ++i) {
241 free(proc_pids_array[i]);
243 free(proc_pids_array);
248 static void swap_proc_pids(proc_pids_t **proc_pids, size_t proc_pids_num) {
249 for (size_t i = 0; i < proc_pids_num; i++) {
250 pids_list_t *swap = proc_pids[i]->prev;
251 proc_pids[i]->prev = proc_pids[i]->curr;
252 proc_pids[i]->curr = swap;
256 int proc_pids_update(const char *procfs_path, proc_pids_t **proc_pids,
257 size_t proc_pids_num) {
261 DIR *proc_dir = opendir(procfs_path);
262 if (proc_dir == NULL) {
263 ERROR(UTIL_NAME ": Could not open %s directory, error: %d", procfs_path,
268 swap_proc_pids(proc_pids, proc_pids_num);
270 for (size_t i = 0; i < proc_pids_num; i++) {
271 if (NULL == proc_pids[i]->curr)
272 proc_pids[i]->curr = calloc(1, sizeof(*(proc_pids[i]->curr)));
274 if (NULL == proc_pids[i]->curr) {
275 ERROR(UTIL_NAME ": Alloc error\n");
279 proc_pids[i]->curr->size = 0;
282 /* Go through procfs and find PIDS and their comms */
283 struct dirent *entry;
284 while ((entry = readdir(proc_dir)) != NULL) {
286 int pid_conversion = get_pid_number(entry, &pid);
287 if (pid_conversion < 0)
292 read_proc_name(procfs_path, entry, comm, sizeof(proc_comm_t));
293 if (read_result <= 0)
296 /* Try to find comm in input procs array */
297 for (size_t i = 0; i < proc_pids_num; ++i) {
299 strncmp(comm, proc_pids[i]->process_name, STATIC_ARRAY_SIZE(comm)))
300 pids_list_add_pid(proc_pids[i]->curr, pid);
304 int close_result = closedir(proc_dir);
305 if (0 != close_result) {
306 ERROR(UTIL_NAME ": failed to close /proc directory, error: %d", errno);
312 swap_proc_pids(proc_pids, proc_pids_num);
316 int pids_list_diff(proc_pids_t *proc, pids_list_t *added,
317 pids_list_t *removed) {
325 if (NULL == proc->prev || 0 == proc->prev->size) {
326 /* append all PIDs from curr to added*/
327 return pids_list_add_list(added, proc->curr);
328 } else if (NULL == proc->curr || 0 == proc->curr->size) {
329 /* append all PIDs from prev to removed*/
330 return pids_list_add_list(removed, proc->prev);
333 for (int i = 0; i < proc->prev->size; i++)
334 if (0 == pids_list_contains_pid(proc->curr, proc->prev->pids[i])) {
335 int add_result = pids_list_add_pid(removed, proc->prev->pids[i]);
340 for (int i = 0; i < proc->curr->size; i++)
341 if (0 == pids_list_contains_pid(proc->prev, proc->curr->pids[i])) {
342 int add_result = pids_list_add_pid(added, proc->curr->pids[i]);
350 int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num) {
351 for (size_t i = 0; i < proc_pids_num; i++) {
352 if (NULL != proc_pids[i]->curr)
353 pids_list_free(proc_pids[i]->curr);
354 if (NULL != proc_pids[i]->prev)
355 pids_list_free(proc_pids[i]->prev);