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>
31 #include <sys/types.h>
34 * Process name inside comm file is limited to 16 chars.
35 * More info here: http://man7.org/linux/man-pages/man5/proc.5.html
37 #define MAX_PROC_NAME_LEN 16
39 /* Helper typedef for process name array
40 * Extra 1 char is added for string null termination.
42 typedef char proc_comm_t[MAX_PROC_NAME_LEN + 1];
45 typedef struct pids_list_s {
51 /* Holds process name and list of pids assigned to that name */
52 typedef struct proc_pids_s {
53 proc_comm_t process_name;
63 * Free all elements of given pids list
66 * `list' Head of target pids_list.
68 void pids_list_free(pids_list_t *list);
75 * Adds pid at the end of the pids array.
76 * Reallocates memory for new pid element, it is up to user to free it.
79 * `list' Target pids_list.
80 * `pid' Pid to be added.
83 * On success, returns 0.
84 * -1 on memory allocation error.
86 int pids_list_add_pid(pids_list_t *list, const pid_t pid);
93 * Remove all pids from the list
96 * `list' Target pids_list.
99 * On success, return 0
101 int pids_list_clear(pids_list_t *list);
108 * Adds pids list at the end of the pids list.
109 * Allocates memory for new pid elements, it is up to user to free it.
112 * `dst' Target PIDs list.
113 * `src' Source PIDs list.
116 * On success, returns 0.
117 * -1 on memory allocation error.
119 int pids_list_add_list(pids_list_t *dst, pids_list_t *src);
123 * pids_list_contains_pid
126 * Tests if pids list contains specific pid.
129 * `list' pids_list to check.
130 * `pid' Pid to be searched for.
133 * If PID found in list, returns 1,
134 * Otherwise returns 0.
136 int pids_list_contains_pid(pids_list_t *list, const pid_t pid);
143 * Searches for differences in two given lists
146 * `proc' List of pids
147 * `added' New pids which appeared
148 * `removed' Result array storing pids which disappeared
150 * 0 on success. Negative number on error.
152 int pids_list_diff(proc_pids_t *proc, pids_list_t *added, pids_list_t *removed);
156 * proc_pids_is_name_valid
159 * Checks if given string is valid process name.
162 * `name' null-terminated char array
165 * If given name is a valid process name, returns 1,
166 * Otherwise returns 0.
168 int proc_pids_is_name_valid(const char *name);
175 * Helper function to properly initialize array of proc_pids.
176 * Allocates memory for proc_pids structs.
179 * `procs_names_array' Array of null-terminated strings with
180 * process' names to be copied to new array
181 * `procs_names_array_size' procs_names_array element count
182 * `proc_pids' Address of pointer, under which new
183 * array of proc_pids will be allocated.
186 * 0 on success. Negative number on error:
187 * -1: allocation error
189 int proc_pids_init(const char **procs_names_array,
190 const size_t procs_names_array_size,
191 proc_pids_t **proc_pids[]);
198 * Updates PIDs matching processes's names.
199 * Searches all PID directories in /proc fs and updates current pids_list.
202 * `procfs_path' Path to systems proc directory (e.g. /proc)
203 * `proc_pids' Array of proc_pids pointers to be updated.
204 * `proc_pids_num' proc_pids element count
207 * 0 on success. -1 on error.
209 int proc_pids_update(const char *procfs_path, proc_pids_t *proc_pids[],
210 size_t proc_pids_num);
217 * Releses memory allocatd for proc_pids
220 * `proc_pids' Array of proc_pids
221 * `proc_pids_num' proc_pids element count
224 * 0 on success. -1 on error.
226 int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num);