groups.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. #ifndef _LIBCGROUP_GROUPS_H
  2. #define _LIBCGROUP_GROUPS_H
  3. #ifndef _LIBCGROUP_H_INSIDE
  4. #error "Only <libcgroup.h> should be included directly."
  5. #endif
  6. #ifndef SWIG
  7. #include <features.h>
  8. #include <sys/types.h>
  9. #include <stdbool.h>
  10. #endif
  11. __BEGIN_DECLS
  12. /**
  13. * Flags for cgroup_delete_cgroup_ext().
  14. */
  15. enum cgroup_delete_flag {
  16. /**
  17. * Ignore errors caused by migration of tasks to parent group.
  18. */
  19. CGFLAG_DELETE_IGNORE_MIGRATION = 1,
  20. /**
  21. * Recursively delete all child groups.
  22. */
  23. CGFLAG_DELETE_RECURSIVE = 2,
  24. /**
  25. * Delete the cgroup only if it is empty, i.e. it has no subgroups and
  26. * no processes inside. This flag cannot be used with
  27. * CGFLAG_DELETE_RECURSIVE.
  28. */
  29. CGFLAG_DELETE_EMPTY_ONLY = 4,
  30. };
  31. /**
  32. * @defgroup group_groups 2. Group manipulation API
  33. * @{
  34. *
  35. * @name Basic infrastructure
  36. * @{
  37. * <tt>struct cgroup*</tt> is the heart of @c libcgroup API.
  38. * The structure is opaque to applications, all access to the structure is
  39. * through appropriate functions.
  40. *
  41. * The most important information is that <b> one <tt>struct cgroup*</tt> can
  42. * represent zero, one or more real control groups in kernel</b>.
  43. * The <tt>struct cgroup*</tt> is identified by name of the group, which must be
  44. * set by cgroup_new_cgroup(). Multiple controllers (aka subsystems) can be
  45. * attached to one <tt>struct cgroup*</tt> using cgroup_add_controller(). These
  46. * controllers <b>can belong to different hierarchies</b>.
  47. *
  48. * This approach is different to the one in the Linux kernel - a control group
  49. * must be part of exactly one hierarchy there. In @c libcgroup, a group can be
  50. * part of multiple hierarchies, as long as the group name is the same.
  51. *
  52. * @par Example:
  53. * Let there be following control groups:
  54. * @code
  55. * cpu,cpuacct:/
  56. * cpu,cpuacct:/foo
  57. * cpu,cpuacct:/bar
  58. * freezer:/
  59. * freezer:/foo
  60. * @endcode
  61. * I.e. there is @c cpu and @c cpuacct controller mounted together in one
  62. * hierarchy, with @c foo and @c bar groups. In addition, @c freezer is
  63. * mounted as separate hierarchy, with only one @c foo group.
  64. *
  65. * @par
  66. * Following code creates <tt>struct cgroup*</tt> structure, which represents
  67. * one group <tt>cpu,cpuacct:/foo</tt>:
  68. * @code
  69. * struct cgroup *foo = cgroup_new_cgroup("foo");
  70. * cgroup_add_controller(foo, "cpu");
  71. * @endcode
  72. * Now, you can call e.g. cgroup_delete_cgroup() and the group is deleted from
  73. * the hierarchy. You can note that it's enough to add only one controller to
  74. * the group to fully identify a group in <tt>cpu,cpuacct</tt> hierarchy.
  75. *
  76. * @par
  77. * Following code creates <tt>struct cgroup*</tt> structure, which represents
  78. * @b two groups, <tt>cpu,cpuacct:/foo</tt> and <tt>freezer:/foo</tt>:
  79. * @code
  80. * struct cgroup *foo = cgroup_new_cgroup("foo");
  81. * cgroup_add_controller(foo, "cpu");
  82. * cgroup_add_controller(foo, "freezer");
  83. * @endcode
  84. * Now, if you call e.g. cgroup_delete_cgroup(), the group gets deleted from
  85. * @b both hierarchies.
  86. *
  87. * @todo add some propaganda what's so great on this approach... I personally
  88. * think it is broken and confusing (see TODOs below).
  89. *
  90. * Following functions are provided to create/destroy various libcgroup
  91. * structures. Please note that none of these functions actually create or
  92. * delete a cgroup in kernel!
  93. */
  94. /**
  95. * @struct cgroup
  96. *
  97. * Structure describing one or more control groups. The structure is opaque to
  98. * applications.
  99. */
  100. struct cgroup;
  101. /**
  102. * @struct cgroup_controller
  103. * Structure describing a controller attached to one struct @c cgroup, including
  104. * parameters of the group and their values. The structure is opaque to
  105. * applications.
  106. * @see groups
  107. */
  108. struct cgroup_controller;
  109. /**
  110. * Uninitialized file/directory permissions used for task/control files.
  111. */
  112. #define NO_PERMS (-1U)
  113. /**
  114. * Uninitialized UID/GID used for task/control files.
  115. */
  116. #define NO_UID_GID (-1U)
  117. /**
  118. * Allocate new cgroup structure. This function itself does not create new
  119. * control group in kernel, only new <tt>struct cgroup</tt> inside libcgroup!
  120. *
  121. * @param name Path to the group, relative from root group. Use @c "/" or @c "."
  122. * for the root group itself and @c "/foo/bar/baz" or @c "foo/bar/baz" for
  123. * subgroups.
  124. * @todo suggest one preferred way, either "/foo" or "foo".
  125. * @returns Created group or NULL on error.
  126. */
  127. struct cgroup *cgroup_new_cgroup(const char *name);
  128. /**
  129. * Attach new controller to cgroup. This function just modifies internal
  130. * libcgroup structure, not the kernel control group.
  131. *
  132. * @param cgroup
  133. * @param name Name of the controller, e.g. "freezer".
  134. * @return Created controller or NULL on error.
  135. */
  136. struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
  137. const char *name);
  138. /**
  139. * Return appropriate controller from given group.
  140. * The controller must be added before using cgroup_add_controller() or loaded
  141. * from kernel using cgroup_get_cgroup().
  142. * @param cgroup
  143. * @param name Name of the controller, e.g. "freezer".
  144. */
  145. struct cgroup_controller *cgroup_get_controller(struct cgroup *cgroup,
  146. const char *name);
  147. /**
  148. * Free internal @c cgroup structure. This function frees also all controllers
  149. * attached to the @c cgroup, including all parameters and their values.
  150. * @param cgroup
  151. */
  152. void cgroup_free(struct cgroup **cgroup);
  153. /**
  154. * Free internal list of controllers from the group.
  155. * @todo should this function be public???
  156. * @param cgroup
  157. */
  158. void cgroup_free_controllers(struct cgroup *cgroup);
  159. /**
  160. * @}
  161. * @name Group manipulation API
  162. * Using following functions you can create and remove control groups and
  163. * change their parameters.
  164. * @note All access to kernel is through previously mounted cgroup filesystems.
  165. * @c libcgroup does not mount/unmount anything for you.
  166. * @{
  167. */
  168. /**
  169. * Physically create a control group in kernel. The group is created in all
  170. * hierarchies, which cover controllers added by cgroup_add_controller().
  171. * All parameters set by cgroup_add_value_* functions are written.
  172. * The created groups has owner which was set by cgroup_set_uid_gid() and
  173. * permissions set by cgroup_set_permissions.
  174. * @param cgroup
  175. * @param ignore_ownership When nozero, all errors are ignored when setting
  176. * owner of the group and/or its tasks file.
  177. * @todo what is ignore_ownership good for?
  178. * @retval #ECGROUPNOTEQUAL if not all specified controller parameters
  179. * were successfully set.
  180. */
  181. int cgroup_create_cgroup(struct cgroup *cgroup, int ignore_ownership);
  182. /**
  183. * Physically create new control group in kernel, with all parameters and values
  184. * copied from its parent group. The group is created in all hierarchies, where
  185. * the parent group exists. I.e. following code creates subgroup in all
  186. * hierarchies, because all of them have root (=parent) group.
  187. * @code
  188. * struct cgroup *foo = cgroup_new_cgroup("foo");
  189. * cgroup_create_cgroup_from_parent(foo, 0);
  190. * @endcode
  191. * @todo what is this good for? Why the list of controllers added by
  192. * cgroup_add_controller() is not used, like in cgroup_create_cgroup()? I can't
  193. * crate subgroup of root group in just one hierarchy with this function!
  194. *
  195. * @param cgroup The cgroup to create. Only it's name is used, everything else
  196. * is discarded.
  197. * @param ignore_ownership When nozero, all errors are ignored when setting
  198. * owner of the group and/or its tasks file.
  199. * @todo what is ignore_ownership good for?
  200. * @retval #ECGROUPNOTEQUAL if not all inherited controller parameters
  201. * were successfully set (this is expected).
  202. */
  203. int cgroup_create_cgroup_from_parent(struct cgroup *cgroup,
  204. int ignore_ownership);
  205. /**
  206. * Physically modify a control group in kernel. All parameters added by
  207. * cgroup_add_value_ or cgroup_set_value_ are written.
  208. * Currently it's not possible to change and owner of a group.
  209. *
  210. * @param cgroup
  211. */
  212. int cgroup_modify_cgroup(struct cgroup *cgroup);
  213. /**
  214. * Physically remove a control group from kernel. The group is removed from
  215. * all hierarchies, which cover controllers added by cgroup_add_controller()
  216. * or cgroup_get_cgroup(). All tasks inside the group are automatically moved
  217. * to parent group.
  218. *
  219. * The group being removed must be empty, i.e. without subgroups. Use
  220. * cgroup_delete_cgroup_ext() for recursive delete.
  221. *
  222. * @param cgroup
  223. * @param ignore_migration When nozero, all errors are ignored when migrating
  224. * tasks from the group to the parent group.
  225. * @todo what is ignore_migration good for? rmdir() will fail if tasks were not moved.
  226. */
  227. int cgroup_delete_cgroup(struct cgroup *cgroup, int ignore_migration);
  228. /**
  229. * Physically remove a control group from kernel.
  230. * All tasks are automatically moved to parent group.
  231. * If #CGFLAG_DELETE_IGNORE_MIGRATION flag is used, the errors that occurred
  232. * during the task movement are ignored.
  233. * #CGFLAG_DELETE_RECURSIVE flag specifies that all subgroups should be removed
  234. * too. If root group is being removed with this flag specified, all subgroups
  235. * are removed but the root group itself is left undeleted.
  236. * @see cgroup_delete_flag.
  237. *
  238. * @param cgroup
  239. * @param flags Combination of CGFLAG_DELETE_* flags, which indicate what and
  240. * how to delete.
  241. */
  242. int cgroup_delete_cgroup_ext(struct cgroup *cgroup, int flags);
  243. /**
  244. * @}
  245. * @name Other functions
  246. * @{
  247. * Helper functions to manipulate with control groups.
  248. */
  249. /**
  250. * Read all information regarding the group from kernel.
  251. * Based on name of the group, list of controllers and all parameters and their
  252. * values are read from all hierarchies, where a group with given name exists.
  253. * All existing controllers are replaced. I.e. following code will fill @c root
  254. * with controllers from all hierarchies, because the root group is available in
  255. * all of them.
  256. * @code
  257. * struct cgroup *root = cgroup_new_cgroup("/");
  258. * cgroup_get_cgroup(root);
  259. * @endcode
  260. *
  261. * @todo what is this function good for? Why is not considered only the list of
  262. * controllers attached by cgroup_add_controller()? What owners will return
  263. * cgroup_get_uid_gid() if the group is in multiple hierarchies, each with
  264. * different owner of tasks file?
  265. *
  266. * @param cgroup The cgroup to load. Only it's name is used, everything else
  267. * is replaced.
  268. */
  269. int cgroup_get_cgroup(struct cgroup *cgroup);
  270. /**
  271. * Copy all controllers, their parameters and values. Group name, permissions
  272. * and ownerships are not coppied. All existing controllers
  273. * in the source group are discarded.
  274. *
  275. * @param dst Destination group.
  276. * @param src Source group.
  277. */
  278. int cgroup_copy_cgroup(struct cgroup *dst, struct cgroup *src);
  279. /**
  280. * Compare names, owners, controllers, parameters and values of two groups.
  281. *
  282. * @param cgroup_a
  283. * @param cgroup_b
  284. *
  285. * @retval 0 if the groups are the same.
  286. * @retval #ECGROUPNOTEQUAL if the groups are not the same.
  287. * @retval #ECGCONTROLLERNOTEQUAL if the only difference are controllers,
  288. * parameters or their values.
  289. */
  290. int cgroup_compare_cgroup(struct cgroup *cgroup_a, struct cgroup *cgroup_b);
  291. /**
  292. * Compare names, parameters and values of two controllers.
  293. *
  294. * @param cgca
  295. * @param cgcb
  296. *
  297. * @retval 0 if the controllers are the same.
  298. * @retval #ECGCONTROLLERNOTEQUAL if the controllers are not equal.
  299. */
  300. int cgroup_compare_controllers(struct cgroup_controller *cgca,
  301. struct cgroup_controller *cgcb);
  302. /**
  303. * Set owner of the group control files and the @c tasks file. This function
  304. * modifies only @c libcgroup internal @c cgroup structure, use
  305. * cgroup_create_cgroup() afterwards to create the group with given owners.
  306. *
  307. * @param cgroup
  308. * @param tasks_uid UID of the owner of group's @c tasks file.
  309. * @param tasks_gid GID of the owner of group's @c tasks file.
  310. * @param control_uid UID of the owner of group's control files (i.e.
  311. * parameters).
  312. * @param control_gid GID of the owner of group's control files (i.e.
  313. * parameters).
  314. */
  315. int cgroup_set_uid_gid(struct cgroup *cgroup, uid_t tasks_uid, gid_t tasks_gid,
  316. uid_t control_uid, gid_t control_gid);
  317. /**
  318. * Return owners of the group's @c tasks file and control files.
  319. * The data is read from @c libcgroup internal @c cgroup structure, use
  320. * cgroup_set_uid_gid() or cgroup_get_cgroup() to fill it.
  321. */
  322. int cgroup_get_uid_gid(struct cgroup *cgroup, uid_t *tasks_uid,
  323. gid_t *tasks_gid, uid_t *control_uid, gid_t *control_gid);
  324. /**
  325. * Stores given file permissions of the group's control and tasks files
  326. * into the @c cgroup data structure. Use NO_PERMS if permissions shouldn't
  327. * be changed or a value which applicable to chmod(2). Please note that
  328. * the given permissions are masked with the file owner's permissions.
  329. * For example if a control file has permissions 640 and control_fperm is
  330. * 471 the result will be 460.
  331. * @param cgroup
  332. * @param control_dperm Directory permission for the group.
  333. * @param control_fperm File permission for the control files.
  334. * @param task_fperm File permissions for task file.
  335. */
  336. void cgroup_set_permissions(struct cgroup *cgroup,
  337. mode_t control_dperm, mode_t control_fperm,
  338. mode_t task_fperm);
  339. /**
  340. * @}
  341. * @name Group parameters
  342. * These are functions can read or modify parameter of a group.
  343. * @note All these functions read/write parameters to @c libcgorup internal
  344. * structures. Use cgroup_get_cgroup() to load parameters from kernel to these
  345. * internal structures and cgroup_modify_cgroup() or cgroup_create_cgroup() to
  346. * write changes to kernel.
  347. * @{
  348. */
  349. /**
  350. * Add parameter and its value to internal @c libcgroup structures.
  351. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  352. *
  353. * @param controller
  354. * @param name Name of the parameter.
  355. * @param value
  356. *
  357. */
  358. int cgroup_add_value_string(struct cgroup_controller *controller,
  359. const char *name, const char *value);
  360. /**
  361. * Add parameter and its value to internal @c libcgroup structures.
  362. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  363. * Content of the value is copied to internal structures and is not needed
  364. * after return from the function.
  365. *
  366. * @param controller
  367. * @param name Name of the parameter.
  368. * @param value
  369. *
  370. */
  371. int cgroup_add_value_int64(struct cgroup_controller *controller,
  372. const char *name, int64_t value);
  373. /**
  374. * Add parameter and its value to internal @c libcgroup structures.
  375. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  376. *
  377. * @param controller
  378. * @param name Name of the parameter.
  379. * @param value
  380. *
  381. */
  382. int cgroup_add_value_uint64(struct cgroup_controller *controller,
  383. const char *name, u_int64_t value);
  384. /**
  385. * Add parameter and its value to internal @c libcgroup structures.
  386. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  387. *
  388. * @param controller
  389. * @param name Name of the parameter.
  390. * @param value
  391. *
  392. */
  393. int cgroup_add_value_bool(struct cgroup_controller *controller,
  394. const char *name, bool value);
  395. /**
  396. * Read a parameter value from @c libcgroup internal structures.
  397. * Use @c cgroup_get_cgroup() to fill these structures with data from kernel.
  398. * It's up to the caller to free returned value.
  399. *
  400. * This function works only for 'short' parameters. Use
  401. * cgroup_read_stats_begin(), cgroup_read_stats_next() and
  402. * cgroup_read_stats_end() to read @c stats parameter, which can be longer
  403. * than libcgroup's internal buffers.
  404. * @todo rephrase, it's too vague... How big is the buffer actually?
  405. *
  406. * @param controller
  407. * @param name Name of the parameter.
  408. * @param value
  409. */
  410. int cgroup_get_value_string(struct cgroup_controller *controller,
  411. const char *name, char **value);
  412. /**
  413. * Read a parameter value from @c libcgroup internal structures.
  414. * Use @c cgroup_get_cgroup() to fill these structures with data from kernel.
  415. *
  416. * @param controller
  417. * @param name Name of the parameter.
  418. * @param value
  419. */
  420. int cgroup_get_value_int64(struct cgroup_controller *controller,
  421. const char *name, int64_t *value);
  422. /**
  423. * Read a parameter value from @c libcgroup internal structures.
  424. * Use @c cgroup_get_cgroup() to fill these structures with data from kernel.
  425. *
  426. * @param controller
  427. * @param name Name of the parameter.
  428. * @param value
  429. */
  430. int cgroup_get_value_uint64(struct cgroup_controller *controller,
  431. const char *name, u_int64_t *value);
  432. /**
  433. * Read a parameter value from @c libcgroup internal structures.
  434. * Use @c cgroup_get_cgroup() to fill these structures with data from kernel.
  435. *
  436. * @param controller
  437. * @param name Name of the parameter.
  438. * @param value
  439. */
  440. int cgroup_get_value_bool(struct cgroup_controller *controller,
  441. const char *name, bool *value);
  442. /**
  443. * Set a parameter value in @c libcgroup internal structures.
  444. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  445. *
  446. * @param controller
  447. * @param name Name of the parameter.
  448. * @param value
  449. */
  450. int cgroup_set_value_string(struct cgroup_controller *controller,
  451. const char *name, const char *value);
  452. /**
  453. * Set a parameter value in @c libcgroup internal structures.
  454. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  455. * Content of the value is copied to internal structures and is not needed
  456. * after return from the function.
  457. *
  458. * @param controller
  459. * @param name Name of the parameter.
  460. * @param value
  461. */
  462. int cgroup_set_value_int64(struct cgroup_controller *controller,
  463. const char *name, int64_t value);
  464. /**
  465. * Set a parameter value in @c libcgroup internal structures.
  466. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  467. *
  468. * @param controller
  469. * @param name Name of the parameter.
  470. * @param value
  471. */
  472. int cgroup_set_value_uint64(struct cgroup_controller *controller,
  473. const char *name, u_int64_t value);
  474. /**
  475. * Set a parameter value in @c libcgroup internal structures.
  476. * Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
  477. *
  478. * @param controller
  479. * @param name Name of the parameter.
  480. * @param value
  481. */
  482. int cgroup_set_value_bool(struct cgroup_controller *controller,
  483. const char *name, bool value);
  484. /**
  485. * Return the number of variables for the specified controller in @c libcgroup
  486. * internal structures. Use cgroup_get_cgroup() to fill these structures with
  487. * data from kernel. Use this function together with cgroup_get_value_name()
  488. * to list all parameters of a group.
  489. *
  490. * @param controller
  491. * @return Count of the parameters or -1 on error.
  492. */
  493. int cgroup_get_value_name_count(struct cgroup_controller *controller);
  494. /**
  495. * Return the name of parameter of controller at given index.
  496. * The index goes from 0 to cgroup_get_value_name_count()-1.
  497. * Use this function to list all parameter of the controller.
  498. *
  499. * @note The returned value is pointer to internal @c libcgroup structure,
  500. * do not free it.
  501. *
  502. * @param controller
  503. * @param index Index of the parameter.
  504. * @return Name of the parameter.
  505. */
  506. char *cgroup_get_value_name(struct cgroup_controller *controller, int index);
  507. /**
  508. * Get the list of process in a cgroup. This list is guaranteed to
  509. * be sorted. It is not necessary that it is unique.
  510. * @param name The name of the cgroup
  511. * @param controller The name of the controller
  512. * @param pids The list of pids. Should be uninitialized when passed
  513. * to the API. Should be freed by the caller using free.
  514. * @param size The size of the pids array returned by the API.
  515. */
  516. int cgroup_get_procs(char *name, char *controller, pid_t **pids, int *size);
  517. /**
  518. * Change permission of files and directories of given group
  519. * @param cgroup The cgroup which permissions should be changed
  520. * @param dir_mode The permission mode of group directory
  521. * @param dirm_change Denotes whether the directory change should be done
  522. * @param file_mode The permission mode of group files
  523. * @param filem_change Denotes whether the directory change should be done
  524. */
  525. int cg_chmod_recursive(struct cgroup *cgroup, mode_t dir_mode,
  526. int dirm_change, mode_t file_mode, int filem_change);
  527. /**
  528. * Get the name of the cgroup from a given cgroup
  529. * @param cgroup The cgroup whose name is needed
  530. */
  531. char *cgroup_get_cgroup_name(struct cgroup *cgroup);
  532. /**
  533. * @}
  534. * @}
  535. */
  536. __END_DECLS
  537. #endif /* _LIBCGROUP_GROUPS_H */