2 * collectd - src/utils/proc_pids/proc_pids.h
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>
30 #ifndef UTILS_PROC_PIDS_PROC_PIDS_H
31 #define UTILS_PROC_PIDS_PROC_PIDS_H 1
34 #include <sys/types.h>
37 * Process name inside comm file is limited to 16 chars.
38 * More info here: http://man7.org/linux/man-pages/man5/proc.5.html
40 #define MAX_PROC_NAME_LEN 16
42 /* Helper typedef for process name array
43 * Extra 1 char is added for string null termination.
45 typedef char proc_comm_t[MAX_PROC_NAME_LEN + 1];
48 typedef struct pids_list_s {
54 /* Holds process name and list of pids assigned to that name */
55 typedef struct proc_pids_s {
56 proc_comm_t process_name;
66 * Free all elements of given pids list
69 * `list' Head of target pids_list.
71 void pids_list_free(pids_list_t *list);
78 * Adds pid at the end of the pids array.
79 * Reallocates memory for new pid element, it is up to user to free it.
82 * `list' Target pids_list.
83 * `pid' Pid to be added.
86 * On success, returns 0.
87 * -1 on memory allocation error.
89 int pids_list_add_pid(pids_list_t *list, const pid_t pid);
96 * Remove all pids from the list
99 * `list' Target pids_list.
102 * On success, return 0
104 int pids_list_clear(pids_list_t *list);
111 * Adds pids list at the end of the pids list.
112 * Allocates memory for new pid elements, it is up to user to free it.
115 * `dst' Target PIDs list.
116 * `src' Source PIDs list.
119 * On success, returns 0.
120 * -1 on memory allocation error.
122 int pids_list_add_list(pids_list_t *dst, pids_list_t *src);
126 * pids_list_contains_pid
129 * Tests if pids list contains specific pid.
132 * `list' pids_list to check.
133 * `pid' Pid to be searched for.
136 * If PID found in list, returns 1,
137 * Otherwise returns 0.
139 int pids_list_contains_pid(pids_list_t *list, const pid_t pid);
146 * Searches for differences in two given lists
149 * `proc' List of pids
150 * `added' New pids which appeared
151 * `removed' Result array storing pids which disappeared
153 * 0 on success. Negative number on error.
155 int pids_list_diff(proc_pids_t *proc, pids_list_t *added, pids_list_t *removed);
159 * proc_pids_is_name_valid
162 * Checks if given string is valid process name.
165 * `name' null-terminated char array
168 * If given name is a valid process name, returns 1,
169 * Otherwise returns 0.
171 int proc_pids_is_name_valid(const char *name);
178 * Helper function to properly initialize array of proc_pids.
179 * Allocates memory for proc_pids structs.
182 * `procs_names_array' Array of null-terminated strings with
183 * process' names to be copied to new array
184 * `procs_names_array_size' procs_names_array element count
185 * `proc_pids' Address of pointer, under which new
186 * array of proc_pids will be allocated.
189 * 0 on success. Negative number on error:
190 * -1: allocation error
192 int proc_pids_init(const char **procs_names_array,
193 const size_t procs_names_array_size,
194 proc_pids_t **proc_pids[]);
201 * Updates PIDs matching processes's names.
202 * Searches all PID directories in /proc fs and updates current pids_list.
205 * `procfs_path' Path to systems proc directory (e.g. /proc)
206 * `proc_pids' Array of proc_pids pointers to be updated.
207 * `proc_pids_num' proc_pids element count
210 * 0 on success. -1 on error.
212 int proc_pids_update(const char *procfs_path, proc_pids_t *proc_pids[],
213 size_t proc_pids_num);
220 * Releses memory allocatd for proc_pids
223 * `proc_pids' Array of proc_pids
224 * `proc_pids_num' proc_pids element count
227 * 0 on success. -1 on error.
229 int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num);
231 #endif /* UTILS_PROC_PIDS_PROC_PIDS_H */