Merge pull request #3019 from sradco/master
[collectd.git] / src / utils / proc_pids / proc_pids.h
1 /**
2  * collectd - src/utils/proc_pids/proc_pids.h
3  *
4  * Copyright(c) 2018-2019 Intel Corporation. All rights reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * SOFTWARE.
23  *
24  * Authors:
25  *   Starzyk, Mateusz <mateuszx.starzyk@intel.com>
26  *   Wojciech Andralojc <wojciechx.andralojc@intel.com>
27  *   Michał Aleksiński <michalx.aleksinski@intel.com>
28  **/
29
30 #include <dirent.h>
31 #include <sys/types.h>
32
33 /*
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
36  */
37 #define MAX_PROC_NAME_LEN 16
38
39 /* Helper typedef for process name array
40  * Extra 1 char is added for string null termination.
41  */
42 typedef char proc_comm_t[MAX_PROC_NAME_LEN + 1];
43
44 /* List of pids. */
45 typedef struct pids_list_s {
46   pid_t *pids;
47   size_t size;
48   size_t allocated;
49 } pids_list_t;
50
51 /* Holds process name and list of pids assigned to that name */
52 typedef struct proc_pids_s {
53   proc_comm_t process_name;
54   pids_list_t *prev;
55   pids_list_t *curr;
56 } proc_pids_t;
57
58 /*
59  * NAME
60  *   pids_list_free
61  *
62  * DESCRIPTION
63  *   Free all elements of given pids list
64  *
65  * PARAMETERS
66  *   `list'     Head of target pids_list.
67  */
68 void pids_list_free(pids_list_t *list);
69
70 /*
71  * NAME
72  *   pids_list_add_pid
73  *
74  * DESCRIPTION
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.
77  *
78  * PARAMETERS
79  *   `list'     Target pids_list.
80  *   `pid'      Pid to be added.
81  *
82  * RETURN VALUE
83  *   On success, returns 0.
84  *   -1 on memory allocation error.
85  */
86 int pids_list_add_pid(pids_list_t *list, const pid_t pid);
87
88 /*
89  * NAME
90  *   pids_list_clear
91  *
92  * DESCRIPTION
93  *   Remove all pids from the list
94  *
95  * PARAMETERS
96  *   `list'     Target pids_list.
97  *
98  * RETURN VALUE
99  *   On success, return 0
100  */
101 int pids_list_clear(pids_list_t *list);
102
103 /*
104  * NAME
105  *   pids_list_add_list
106  *
107  * DESCRIPTION
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.
110  *
111  * PARAMETERS
112  *   `dst'      Target PIDs list.
113  *   `src'      Source PIDs list.
114  *
115  * RETURN VALUE
116  *   On success, returns 0.
117  *   -1 on memory allocation error.
118  */
119 int pids_list_add_list(pids_list_t *dst, pids_list_t *src);
120
121 /*
122  * NAME
123  *   pids_list_contains_pid
124  *
125  * DESCRIPTION
126  *   Tests if pids list contains specific pid.
127  *
128  * PARAMETERS
129  *   `list'     pids_list to check.
130  *   `pid'      Pid to be searched for.
131  *
132  * RETURN VALUE
133  *   If PID found in list, returns 1,
134  *   Otherwise returns 0.
135  */
136 int pids_list_contains_pid(pids_list_t *list, const pid_t pid);
137
138 /*
139  * NAME
140  *   pids_list_diff
141  *
142  * DESCRIPTION
143  *   Searches for differences in two given lists
144  *
145  * PARAMETERS
146  *   `proc'            List of pids
147  *   `added'           New pids which appeared
148  *   `removed'         Result array storing pids which disappeared
149  * RETURN VALUE
150  *   0 on success. Negative number on error.
151  */
152 int pids_list_diff(proc_pids_t *proc, pids_list_t *added, pids_list_t *removed);
153
154 /*
155  * NAME
156  *   proc_pids_is_name_valid
157  *
158  * DESCRIPTION
159  *   Checks if given string is valid process name.
160  *
161  * PARAMETERS
162  *   `name'     null-terminated char array
163  *
164  * RETURN VALUE
165  *   If given name is a valid process name, returns 1,
166  *   Otherwise returns 0.
167  */
168 int proc_pids_is_name_valid(const char *name);
169
170 /*
171  * NAME
172  *   proc_pids_init
173  *
174  * DESCRIPTION
175  *   Helper function to properly initialize array of proc_pids.
176  *   Allocates memory for proc_pids structs.
177  *
178  * PARAMETERS
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.
184  *                            Must be NULL.
185  * RETURN VALUE
186  *   0 on success. Negative number on error:
187  *   -1: allocation error
188  */
189 int proc_pids_init(const char **procs_names_array,
190                    const size_t procs_names_array_size,
191                    proc_pids_t **proc_pids[]);
192
193 /*
194  * NAME
195  *   proc_pids_update
196  *
197  * DESCRIPTION
198  *   Updates PIDs matching processes's names.
199  *   Searches all PID directories in /proc fs and updates current pids_list.
200  *
201  * PARAMETERS
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
205  *
206  * RETURN VALUE
207  *   0 on success. -1 on error.
208  */
209 int proc_pids_update(const char *procfs_path, proc_pids_t *proc_pids[],
210                      size_t proc_pids_num);
211
212 /*
213  * NAME
214  *   proc_pids_free
215  *
216  * DESCRIPTION
217  *   Releses memory allocatd for proc_pids
218  *
219  * PARAMETERS
220  *   `proc_pids'       Array of proc_pids
221  *   `proc_pids_num'   proc_pids element count
222  *
223  * RETURN VALUE
224  *   0 on success. -1 on error.
225  */
226 int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num);