iterators.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #ifndef _LIBCGROUP_ITERATORS_H
  2. #define _LIBCGROUP_ITERATORS_H
  3. #ifndef _LIBCGROUP_H_INSIDE
  4. #error "Only <libcgroup.h> should be included directly."
  5. #endif
  6. #ifndef SWIG
  7. #include <sys/types.h>
  8. #include <stdio.h>
  9. #include <features.h>
  10. #endif
  11. __BEGIN_DECLS
  12. /**
  13. * @defgroup group_iterators 3. Iterators
  14. * @{
  15. * So-called iterators are a code pattern to retrieve various data from
  16. * libcgroup in distinct chunks, for example when an application needs to read
  17. * list of groups in a hierarchy, it uses iterator to get one group at a time.
  18. * Iterator is opaque to the application, the application sees only
  19. * <tt>void* handle</tt> pointer, which is managed internally by @c libcgroup.
  20. * Each iterator provides at least these functions:
  21. * - <tt>int <i>iterator_name</i>_begin(void **handle, my_type *item)</tt>
  22. * - Initialize the iterator, store pointer to it into the @c handle.
  23. * - Return the first element in the iterator, let's say it's @c my_type.
  24. * - Return @c 0, if the operation succeeded.
  25. * - Return #ECGEOF, if the operation succeeded, but the iterator is empty.
  26. * The value of @c item is undefined in this case.
  27. * - Return any other error code on error.
  28. * - <tt>int <i>iterator_name</i>_next(void **handle, my_type *item)</tt>
  29. * - Advance to next element in the iterator and return it.
  30. * - Return @c 0, if the operation succeeded.
  31. * - Return #ECGEOF, if there is no item to advance to, i.e. the iterator
  32. * is already at its end. The value of @c item is undefined in this case.
  33. * - Return any other error code on error.
  34. * - <tt>void <i>iterator_name</i>_end(void **handle)</tt>
  35. * - Free any data associated with the iterator. This function must be
  36. * called even when <tt><i>iterator_name</i>_begin()</tt> fails.
  37. *
  38. * @todo not all iterators follow this pattern, e.g. cgroup_walk_tree_begin()
  39. * can result both in a state that cgroup_walk_tree_end() is not needed
  40. * and will sigsegv and in a state that cgroup_walk_tree_end() is needed
  41. * to free allocated memory. Complete review is needed!
  42. * @par Example of iterator usage:
  43. * @code
  44. * void *handle; // our iterator handle
  45. * my_type item; // the data returned by the iterator
  46. * int ret;
  47. * ret = iterator_name_begin(&handle, &item);
  48. * while (ret == 0) {
  49. * // process the item here
  50. * ret = iterator_name_begin(&handle, &item);
  51. * }
  52. * if (ret != ECGEOF) {
  53. * // process the error here
  54. * }
  55. * iterator_name_end(&handle);
  56. * @endcode
  57. *
  58. * @name Walk through control group filesystem
  59. * @{
  60. * This iterator returns all subgroups of given control group. It can be used
  61. * to return all groups in given hierarchy, when root control group is provided.
  62. */
  63. /**
  64. * Type of the walk.
  65. */
  66. enum cgroup_walk_type {
  67. /**
  68. * Pre-order directory walk, return a directory first and then its
  69. * subdirectories.
  70. * E.g. directories would be returned in this order:
  71. * @code
  72. * /
  73. * /group
  74. * /group/subgroup1
  75. * /group/subgroup1/subsubgroup
  76. * /group/subgroup2
  77. * @endcode
  78. */
  79. CGROUP_WALK_TYPE_PRE_DIR = 0x1,
  80. /**
  81. * Post-order directory walk, return subdirectories of a directory
  82. * first and then the directory itself.
  83. * E.g. directories would be returned in this order:
  84. * @code
  85. * /group/subgroup1/subsubgroup
  86. * /group/subgroup1
  87. * /group/subgroup2
  88. * /group
  89. * /
  90. * @endcode
  91. */
  92. CGROUP_WALK_TYPE_POST_DIR = 0x2,
  93. };
  94. /**
  95. * Type of returned entity.
  96. */
  97. enum cgroup_file_type {
  98. CGROUP_FILE_TYPE_FILE, /**< File. */
  99. CGROUP_FILE_TYPE_DIR, /**< Directory. */
  100. CGROUP_FILE_TYPE_OTHER, /**< Directory. @todo really? */
  101. };
  102. /**
  103. * Information about found directory (= a control group).
  104. */
  105. struct cgroup_file_info {
  106. /** Type of the entity. */
  107. enum cgroup_file_type type;
  108. /** Name of the entity. */
  109. const char *path;
  110. /** Name of its parent. */
  111. const char *parent;
  112. /**
  113. * Full path to the entity. To get path relative to the root of the
  114. * walk, you must store its @c full_path (or its length)
  115. * and calculate the relative path by yourself.
  116. */
  117. const char *full_path;
  118. /**
  119. * Depth of the entity, how many directories below the root of
  120. * walk it is.
  121. */
  122. short depth;
  123. };
  124. /**
  125. * Walk through the directory tree for the specified controller.
  126. * The directory representing @c base_path is returned in @c info.
  127. * Use cgroup_walk_tree_set_flags() to specify, in which order should be next
  128. * directories returned.
  129. * @param controller Name of the controller, for which we want to walk
  130. * the directory tree.
  131. * @param base_path Begin walking from this path. Use "/" to walk through
  132. * full hierarchy.
  133. * @param depth The maximum depth to which the function should walk, 0
  134. * implies all the way down.
  135. * @param handle Handle to be used during iteration.
  136. * @param info Info filled and returned about directory information.
  137. * @param base_level Opaque integer which you must pass to subsequent
  138. * cgroup_walk_tree_next.
  139. * @todo why base_level is not hidden in **handle?
  140. * @return #ECGEOF when there is no node.
  141. */
  142. int cgroup_walk_tree_begin(const char *controller, const char *base_path, int depth,
  143. void **handle, struct cgroup_file_info *info,
  144. int *base_level);
  145. /**
  146. * Get the next directory in the walk.
  147. * @param depth The maximum depth to which the function should walk, 0
  148. * implies all the way down.
  149. * @param handle Handle to be used during iteration.
  150. * @param info Info filled and returned about the next directory.
  151. * @param base_level Value of base_level returned by cgroup_walk_tree_begin().
  152. * @return #ECGEOF when we are done walking through the nodes.
  153. */
  154. int cgroup_walk_tree_next(int depth, void **handle,
  155. struct cgroup_file_info *info, int base_level);
  156. /**
  157. * Release the iterator.
  158. */
  159. int cgroup_walk_tree_end(void **handle);
  160. /**
  161. * Set the flags for walk_tree. Currently available flags are in
  162. * #cgroup_walk_type enum.
  163. * @param handle Handle of the iterator.
  164. * @param flags
  165. */
  166. int cgroup_walk_tree_set_flags(void **handle, int flags);
  167. /**
  168. * Read the value of the given variable for the specified
  169. * controller and control group.
  170. * The value is read up to newline character or at most max-1 characters,
  171. * whichever comes first (i.e. similar to fgets()).
  172. * @param controller Name of the controller for which stats are requested.
  173. * @param path Path to control group, relative to hierarchy root.
  174. * @param name is variable name.
  175. * @param handle Handle to be used during iteration.
  176. * @param buffer Buffer to read the value into.
  177. * The buffer is always zero-terminated.
  178. * @param max Maximal lenght of the buffer
  179. * @return #ECGEOF when the stats file is empty.
  180. */
  181. int cgroup_read_value_begin(const char *controller, const char *path,
  182. char *name, void **handle, char *buffer, int max);
  183. /**
  184. * Read the next string from the given variable handle
  185. * which is generated by cgroup_read_stats_begin() function.
  186. * the value is read up to newline character or at most max-1 characters,
  187. * whichever comes first (i.e. similar to fgets()) per
  188. * cgroup_read_stats_next() call
  189. * @param handle Handle to be used during iteration.
  190. * @param data returned the string.
  191. * @param buffer Buffer to read the value into.
  192. * The buffer is always zero-terminated.
  193. * @param max Maximal lenght of the buffer
  194. * @return #ECGEOF when the iterator finishes getting the list of stats.
  195. */
  196. int cgroup_read_value_next(void **handle, char *buffer, int max);
  197. /**
  198. * Release the iterator.
  199. */
  200. int cgroup_read_value_end(void **handle);
  201. /**
  202. * @}
  203. *
  204. * @name Read group stats
  205. * libcgroup's cgroup_get_value_string() reads only relatively short parametrs
  206. * of a group. Use following functions to read @c stats parameter, which can
  207. * be quite long.
  208. */
  209. /**
  210. * Maximum length of a value in stats file.
  211. */
  212. #define CG_VALUE_MAX 100
  213. /**
  214. * One item in stats file.
  215. */
  216. struct cgroup_stat {
  217. char name[FILENAME_MAX];
  218. char value[CG_VALUE_MAX];
  219. };
  220. /**
  221. * Read the statistics values (= @c stats parameter) for the specified
  222. * controller and control group. One line is returned per
  223. * cgroup_read_stats_begin() and cgroup_read_stats_next() call.
  224. * @param controller Name of the controller for which stats are requested.
  225. * @param path Path to control group, relative to hierarchy root.
  226. * @param handle Handle to be used during iteration.
  227. * @param stat Returned first item in the stats file.
  228. * @return #ECGEOF when the stats file is empty.
  229. */
  230. int cgroup_read_stats_begin(const char *controller, const char *path, void **handle,
  231. struct cgroup_stat *stat);
  232. /**
  233. * Read the next stat value.
  234. * @param handle Handle to be used during iteration.
  235. * @param stat Returned next item in the stats file.
  236. * @return #ECGEOF when the iterator finishes getting the list of stats.
  237. */
  238. int cgroup_read_stats_next(void **handle, struct cgroup_stat *stat);
  239. /**
  240. * Release the iterator.
  241. */
  242. int cgroup_read_stats_end(void **handle);
  243. /**
  244. * @}
  245. *
  246. * @name List all tasks in a group
  247. * Use following functions to read @c tasks file of a group.
  248. * @{
  249. */
  250. /**
  251. * Read the tasks file to get the list of tasks in a cgroup.
  252. * @param cgroup Name of the cgroup.
  253. * @param controller Name of the cgroup subsystem.
  254. * @param handle Handle to be used in the iteration.
  255. * @param pid The pid read from the tasks file.
  256. * @return #ECGEOF when the group does not contain any tasks.
  257. */
  258. int cgroup_get_task_begin(const char *cgroup, const char *controller, void **handle,
  259. pid_t *pid);
  260. /**
  261. * Read the next task value.
  262. * @param handle The handle used for iterating.
  263. * @param pid The variable where the value will be stored.
  264. *
  265. * @return #ECGEOF when the iterator finishes getting the list of tasks.
  266. */
  267. int cgroup_get_task_next(void **handle, pid_t *pid);
  268. /**
  269. * Release the iterator.
  270. */
  271. int cgroup_get_task_end(void **handle);
  272. /**
  273. * @}
  274. *
  275. * @name List mounted controllers
  276. * Use following function to list mounted controllers and to see, how they
  277. * are mounted together in hierarchies.
  278. * Use cgroup_get_all_controller_begin() (see later) to list all controllers,
  279. * including those which are not mounted.
  280. * @{
  281. */
  282. /**
  283. * Information about mounted controller.
  284. */
  285. struct cgroup_mount_point {
  286. /** Name of the controller. */
  287. char name[FILENAME_MAX];
  288. /** Mount point of the controller. */
  289. char path[FILENAME_MAX];
  290. };
  291. /**
  292. * Read the mount table to give a list where each controller is
  293. * mounted.
  294. * @param handle Handle to be used for iteration.
  295. * @param info The variable where the path to the controller is stored.
  296. * @return #ECGEOF when no controllers are mounted.
  297. */
  298. int cgroup_get_controller_begin(void **handle, struct cgroup_mount_point *info);
  299. /**
  300. * Read the next mounted controller.
  301. * While walking through the mount table, the controllers are
  302. * returned in order of their mount points, i.e. controllers mounted together
  303. * in one hierarchy are returned next to each other.
  304. * @param handle Handle to be used for iteration.
  305. * @param info The variable where the path to the controller is stored.
  306. * @return #ECGEOF when all controllers were already returned.
  307. */
  308. int cgroup_get_controller_next(void **handle, struct cgroup_mount_point *info);
  309. /**
  310. * Release the iterator.
  311. */
  312. int cgroup_get_controller_end(void **handle);
  313. /**
  314. * @}
  315. *
  316. * @name List all controllers
  317. * Use following functions to list all controllers, including those which are
  318. * not mounted. The controllers are returned in the same order as in
  319. * /proc/cgroups file, i.e. mostly random.
  320. */
  321. /**
  322. * Detailed information about available controller.
  323. */
  324. struct controller_data {
  325. /** Controller name. */
  326. char name[FILENAME_MAX];
  327. /**
  328. * Hierarchy ID. Controllers with the same hierarchy ID
  329. * are mounted together as one hierarchy. Controllers with
  330. * ID 0 are not currently mounted anywhere.
  331. */
  332. int hierarchy;
  333. /** Number of groups. */
  334. int num_cgroups;
  335. /** Enabled flag. */
  336. int enabled;
  337. };
  338. /**
  339. * Read the first of controllers from /proc/cgroups.
  340. * @param handle Handle to be used for iteration.
  341. * @param info The structure which will be filled with controller data.
  342. */
  343. int cgroup_get_all_controller_begin(void **handle,
  344. struct controller_data *info);
  345. /**
  346. * Read next controllers from /proc/cgroups.
  347. * @param handle Handle to be used for iteration.
  348. * @param info The structure which will be filled with controller data.
  349. */
  350. int cgroup_get_all_controller_next(void **handle, struct controller_data *info);
  351. /**
  352. * Release the iterator
  353. */
  354. int cgroup_get_all_controller_end(void **handle);
  355. /**
  356. * @}
  357. *
  358. * @name List all mount points of a controller.
  359. * Use following functions to list all mount points of a hierarchy with given
  360. * controller.
  361. */
  362. /**
  363. * Read the first mount point of the hierarchy with given controller.
  364. * The first is the same as the mount point returned by
  365. * cgroup_get_subsys_mount_point().
  366. * @param handle Handle to be used for iteration.
  367. * @param controller Controller name.
  368. * @param path Buffer to fill the path into. The buffer must be at least
  369. * FILENAME_MAX characters long.
  370. */
  371. int cgroup_get_subsys_mount_point_begin(const char *controller, void **handle,
  372. char *path);
  373. /**
  374. * Read next mount point of the hierarchy with given controller.
  375. * @param handle Handle to be used for iteration.
  376. * @param path Buffer to fill the path into. The buffer must be at least
  377. * FILENAME_MAX characters long.
  378. */
  379. int cgroup_get_subsys_mount_point_next(void **handle,
  380. char *path);
  381. /**
  382. * Release the iterator.
  383. */
  384. int cgroup_get_subsys_mount_point_end(void **handle);
  385. /**
  386. * @}
  387. * @}
  388. */
  389. __END_DECLS
  390. #endif /* _LIBCGROUP_ITERATORS_H */