tasks.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef _LIBCGROUP_TASKS_H
  2. #define _LIBCGROUP_TASKS_H
  3. #ifndef _LIBCGROUP_H_INSIDE
  4. #error "Only <libcgroup.h> should be included directly."
  5. #endif
  6. #include <libcgroup/groups.h>
  7. #ifndef SWIG
  8. #include <features.h>
  9. #include <stdbool.h>
  10. #endif
  11. __BEGIN_DECLS
  12. /** Flags for cgroup_change_cgroup_uid_gid(). */
  13. enum cgflags {
  14. /** Use cached rules, do not read rules from disk. */
  15. CGFLAG_USECACHE = 0x01,
  16. /** Use cached templates, do not read templates from disk. */
  17. CGFLAG_USE_TEMPLATE_CACHE = 0x02,
  18. };
  19. /** Flags for cgroup_register_unchanged_process(). */
  20. enum cgroup_daemon_type {
  21. /**
  22. * The daemon must not touch the given task, i.e. it never moves it
  23. * to any controlgroup.
  24. */
  25. CGROUP_DAEMON_UNCHANGE_CHILDREN = 0x1,
  26. CGROUP_DAEMON_CANCEL_UNCHANGE_PROCESS = 0x2,
  27. };
  28. /**
  29. * @defgroup group_tasks 4. Manipulation with tasks
  30. * @{
  31. *
  32. * @name Simple task assignment
  33. * @{
  34. * Applications can use following functions to simply put a task into given
  35. * control group and find a groups where given tasks is.
  36. */
  37. /**
  38. * Move current task (=thread) to given control group.
  39. * @param cgroup Destination control group.
  40. */
  41. int cgroup_attach_task(struct cgroup *cgroup);
  42. /**
  43. * Move given task (=thread) to to given control group.
  44. * @param cgroup Destination control group.
  45. * @param tid The task to move.
  46. */
  47. int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid);
  48. /**
  49. * Changes the cgroup of a task based on the path provided. In this case,
  50. * the user must already know into which cgroup the task should be placed and
  51. * no rules will be parsed.
  52. *
  53. * @param path Name of the destination group.
  54. * @param pid The task to move.
  55. * @param controllers List of controllers.
  56. *
  57. * @todo should this function be really public?
  58. */
  59. int cgroup_change_cgroup_path(const char *path, pid_t pid,
  60. const char * const controllers[]);
  61. /**
  62. * Get the current control group path where the given task is.
  63. * @param pid The task to find.
  64. * @param controller The controller (hierarchy), where to find the task.
  65. * @param current_path The path to control group, where the task has been found.
  66. * The patch is relative to the root of the hierarchy. The caller must
  67. * free this memory.
  68. */
  69. int cgroup_get_current_controller_path(pid_t pid, const char *controller,
  70. char **current_path);
  71. /**
  72. * @}
  73. *
  74. * @name Rules
  75. * @{
  76. * @c libcgroup can move tasks to control groups using simple rules, loaded
  77. * from configuration file. See cgrules.conf man page to see format of the file.
  78. * Following functions can be used to load these rules from a file.
  79. */
  80. /**
  81. * Initializes the rules cache and load it from /etc/cgrules.conf.
  82. * @todo add parameter with the filename?
  83. */
  84. int cgroup_init_rules_cache(void);
  85. /**
  86. * Reloads the rules list from /etc/cgrules.conf. This function
  87. * is probably NOT thread safe (calls cgroup_parse_rules_config()).
  88. */
  89. int cgroup_reload_cached_rules(void);
  90. /**
  91. * Print the cached rules table. This function should be called only after
  92. * first calling cgroup_parse_config(), but it will work with an empty rule
  93. * list.
  94. * @param fp Destination file, where the rules will be printed.
  95. */
  96. void cgroup_print_rules_config(FILE *fp);
  97. /**
  98. * @}
  99. * @name Rule based task assignment
  100. * @{
  101. * @c libcgroup can move tasks to control groups using simple rules, loaded
  102. * from configuration file. See cgrules.conf man page to see format of the file.
  103. * Applications can move tasks to control groups based on these rules using
  104. * following functions.
  105. */
  106. /**
  107. * Changes the cgroup of all running PIDs based on the rules in the config
  108. * file. If a rules exists for a PID, then the PID is placed in the correct
  109. * group.
  110. *
  111. * This function may be called after creating new control groups to move
  112. * running PIDs into the newly created control groups.
  113. * @return 0 on success, < 0 on error
  114. */
  115. int cgroup_change_all_cgroups(void);
  116. /**
  117. * Changes the cgroup of a program based on the rules in the config file.
  118. * If a rule exists for the given UID, GID or PROCESS NAME, then the given
  119. * PID is placed into the correct group. By default, this function parses
  120. * the configuration file each time it is called.
  121. *
  122. * The flags can alter the behavior of this function:
  123. * CGFLAG_USECACHE: Use cached rules instead of parsing the config file
  124. * CGFLAG_USE_TEMPLATE_CACHE: Use cached templates instead of
  125. * parsing the config file
  126. *
  127. * This function may NOT be thread safe.
  128. * @param uid The UID to match.
  129. * @param gid The GID to match.
  130. * @param procname The PROCESS NAME to match.
  131. * @param pid The PID of the process to move.
  132. * @param flags Bit flags to change the behavior, as defined in enum #cgflags.
  133. * @todo Determine thread-safeness and fix of not safe.
  134. */
  135. int cgroup_change_cgroup_flags(uid_t uid, gid_t gid,
  136. const char *procname, pid_t pid, int flags);
  137. /**
  138. * Changes the cgroup of a program based on the rules in the config file. If a
  139. * rule exists for the given UID or GID, then the given PID is placed into the
  140. * correct group. By default, this function parses the configuration file each
  141. * time it is called.
  142. *
  143. * This function may NOT be thread safe.
  144. * @param uid The UID to match.
  145. * @param gid The GID to match.
  146. * @param pid The PID of the process to move.
  147. * @param flags Bit flags to change the behavior, as defined in enum #cgflags.
  148. * @todo Determine thread-safeness and fix if not safe.
  149. */
  150. int cgroup_change_cgroup_uid_gid_flags(uid_t uid, gid_t gid,
  151. pid_t pid, int flags);
  152. /**
  153. * Provides backwards-compatibility with older versions of the API. This
  154. * function is deprecated, and cgroup_change_cgroup_uid_gid_flags() should be
  155. * used instead. In fact, this function simply calls the newer one with flags
  156. * set to 0 (none).
  157. * @param uid The UID to match.
  158. * @param gid The GID to match.
  159. * @param pid The PID of the process to move.
  160. */
  161. int cgroup_change_cgroup_uid_gid(uid_t uid, gid_t gid, pid_t pid);
  162. /**
  163. * @}
  164. * @name Communication with cgrulesengd daemon
  165. * @{
  166. * Users can use cgrulesengd daemon to move tasks to groups based on the rules
  167. * automatically when they change their UID, GID or executable name.
  168. * The daemon allows tasks to be 'sticky', i.e. all rules are ignored for these
  169. * tasks and the daemon never moves them.
  170. */
  171. /**
  172. * Register the unchanged process to a cgrulesengd daemon. This process
  173. * is never moved to another control group by the daemon.
  174. * If the daemon does not work, this function returns 0 as success.
  175. * @param pid The task id.
  176. * @param flags Bit flags to change the behavior, as defined in
  177. * #cgroup_daemon_type
  178. */
  179. int cgroup_register_unchanged_process(pid_t pid, int flags);
  180. /**
  181. * @}
  182. * @}
  183. */
  184. __END_DECLS
  185. #endif /* _LIBCGROUP_TASKS_H */