resource.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. resource.h
  8. Abstract:
  9. This header contains definitions for system resource operations.
  10. Author:
  11. Evan Green 9-Jul-2014
  12. --*/
  13. #ifndef _SYS_RESOURCE_H
  14. #define _SYS_RESOURCE_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <sys/time.h>
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. //
  26. // Definitions for the "which" when getting resource usage.
  27. //
  28. //
  29. // Get resource usage for a specified process ID.
  30. //
  31. #define PRIO_PROCESS 1
  32. //
  33. // Get resource usage for a specified process group.
  34. //
  35. #define PRIO_PGRP 2
  36. //
  37. // Get resource usage for a specified user ID.
  38. //
  39. #define PRIO_USER 3
  40. //
  41. // Define the highest possible limit value.
  42. //
  43. #define RLIM_INFINITY (-1UL)
  44. #define RLIM64_INFINITY RLIM_INFINITY
  45. //
  46. // All values can be represented.
  47. //
  48. #define RLIM_SAVED_MAX RLIM_INFINITY
  49. #define RLIM_SAVED_CUR RLIM_INFINITY
  50. //
  51. // Definitinos for the "who" when getting and setting resource usage.
  52. //
  53. //
  54. // Return information about the current process. This is the sum of all the
  55. // process threads.
  56. //
  57. #define RUSAGE_SELF 1
  58. //
  59. // Return information about the children of the current process.
  60. //
  61. #define RUSAGE_CHILDREN 2
  62. //
  63. // Return information about the current thread.
  64. //
  65. #define RUSAGE_THREAD 3
  66. //
  67. // Define the different kinds of resource limits.
  68. //
  69. //
  70. // Limit the size of the core file. A value of 0 prevents the creation of core
  71. // files.
  72. //
  73. #define RLIMIT_CORE 0
  74. //
  75. // Limit the CPU time per process. If this time is exceeded, a SIGXCPU signal
  76. // is sent to the process once a second until the hard limit is reached.
  77. //
  78. #define RLIMIT_CPU 1
  79. //
  80. // Limit the data segment size (all memory used by the process). If this limit
  81. // is exceeded, additional system memory allocation requests will fail with
  82. // errno set to ENOMEM.
  83. //
  84. #define RLIMIT_DATA 2
  85. //
  86. // Limit the maximum file size. If a write or truncate operation would cause
  87. // this limit to be exceeded, SIGXFSZ will be sent to the thread. Continued
  88. // attempts to increase the file size beyond the limit will fail with EFBIG.
  89. //
  90. #define RLIMIT_FSIZE 3
  91. //
  92. // Limit the number of open files. This is one greater than the maximum value
  93. // the system may assign to a newly created descriptor. If this limit is
  94. // exceeded, functions that allocate a file descriptor shall fail with errno
  95. // set to EMFILE.
  96. //
  97. #define RLIMIT_NOFILE 4
  98. //
  99. // Limit the stack size. If this limit is exceeded, SIGSEGV is generated for
  100. // the thread. If the thread is blocking SIGSEGV, or the process is ignoring
  101. // or catching the signal and has not made arrangements to use an alternate
  102. // stack, the disposition of SIGSEGV shall be set to SIG_DFL before it is
  103. // generated.
  104. //
  105. #define RLIMIT_STACK 5
  106. //
  107. // Limit the address space size.
  108. //
  109. #define RLIMIT_AS 6
  110. //
  111. // Limit the number of processes that can be created. Attempts to fork beyond
  112. // this limit fail with errno set to EAGAIN.
  113. //
  114. #define RLIMIT_NPROC 7
  115. //
  116. // Limit the number of pending signals.
  117. //
  118. #define RLIMIT_SIGPENDING 8
  119. //
  120. // Limit the nice value.
  121. //
  122. #define RLIMIT_NICE 9
  123. //
  124. // Define the number of different resource limits. Any valid RLIMIT_* value
  125. // must be less than this value.
  126. //
  127. #define RLIM_NLIMITS 10
  128. //
  129. // ------------------------------------------------------ Data Type Definitions
  130. //
  131. //
  132. // Type used for describing resource limit values.
  133. //
  134. typedef unsigned long rlim_t, rlim64_t;
  135. /*++
  136. Structure Description:
  137. This structure defines information about a resource limit.
  138. Members:
  139. rlim_cur - Stores the current (soft) limit. This is the limit the system
  140. enforces.
  141. rlim_max - Stores the maximum value the soft limit can be set to.
  142. --*/
  143. struct rlimit {
  144. rlim_t rlim_cur;
  145. rlim_t rlim_max;
  146. };
  147. /*++
  148. Structure Description:
  149. This structure defines information about the resource usage of a process,
  150. process group, or user. Not all fields are currently filled in. Unused
  151. fields will be set to zero.
  152. Members:
  153. ru_utime - Stores the user time used.
  154. ru_stime - Stores the system time used.
  155. ru_maxrss - Stores the maximum resident set size used (in kilobytes). For
  156. RUSAGE_CHILDREN, this is the resident size of the largest child, not
  157. the sum total. Not currently used.
  158. ru_ixrss - Stores the integral shared memory size. Not currently used.
  159. ru_idrss - Stores the integral unshared memory data size. Not currently
  160. used.
  161. ru_isrss - Stores the integral unshared stack size. Not currently used.
  162. ru_minflt - Stores the number of page faults serviced without any I/O
  163. activity. Not currently used.
  164. ru_majflt - Stores the number of page faults serviced that required I/O
  165. activity. Not currently used.
  166. ru_nswap - Stores the number of times the process was swapped out. Not
  167. currently used.
  168. ru_inblock - Stores the number of times the file system performed input.
  169. Not currently used.
  170. ru_oublock - Stores the number of times the file system performed output.
  171. Not currently used.
  172. ru_msgsend - Stores the number of IPC messages sent. Not currently used.
  173. ru_msgrcv - Stores the number of IPC messages received. Not currently used.
  174. ru_nsignals - Stores the number of signals received. Not currently used.
  175. ru_nvcsw - Stores the number of voluntary context switches (yields).
  176. ru_nivcsw - Stores the number of involuntary context switches (preemptions).
  177. --*/
  178. struct rusage {
  179. struct timeval ru_utime;
  180. struct timeval ru_stime;
  181. long ru_maxrss;
  182. long ru_ixrss;
  183. long ru_idrss;
  184. long ru_isrss;
  185. long ru_minflt;
  186. long ru_majflt;
  187. long ru_nswap;
  188. long ru_inblock;
  189. long ru_oublock;
  190. long ru_msgsnd;
  191. long ru_msgrcv;
  192. long ru_nsignals;
  193. long ru_nvcsw;
  194. long ru_nivcsw;
  195. };
  196. //
  197. // -------------------------------------------------------------------- Globals
  198. //
  199. //
  200. // -------------------------------------------------------- Function Prototypes
  201. //
  202. LIBC_API
  203. int
  204. getpriority (
  205. int Which,
  206. id_t Who
  207. );
  208. /*++
  209. Routine Description:
  210. This routine returns the nice value of a process, process group, or user.
  211. Arguments:
  212. Which - Supplies which entity to get the nice value of. See PRIO_*
  213. definitions, which allow the caller to get the nice value of the
  214. process, process group, or user.
  215. Who - Supplies the identifier of the process, process group, or user to
  216. query. A value of zero specifies the current process, process group, or
  217. user.
  218. Return Value:
  219. Returns a value in the range of -NZERO to NZERO-1 representing the current
  220. nice value.
  221. -1 on failure, and the errno variable will be set to contain more
  222. information.
  223. --*/
  224. LIBC_API
  225. int
  226. setpriority (
  227. int Which,
  228. id_t Who,
  229. int Value
  230. );
  231. /*++
  232. Routine Description:
  233. This routine sets the nice value of a process, process group, or user.
  234. Arguments:
  235. Which - Supplies which entity to set the nice value for. See PRIO_*
  236. definitions, which allow the caller to set the nice value of the
  237. process, process group, or user.
  238. Who - Supplies the identifier of the process, process group, or user to
  239. set. A value of zero specifies the current process, process group, or
  240. user.
  241. Value - Supplies the new nice value to set.
  242. Return Value:
  243. 0 on success.
  244. -1 on failure, and the errno variable will be set to contain more
  245. information.
  246. --*/
  247. LIBC_API
  248. int
  249. getrlimit (
  250. int Resource,
  251. struct rlimit *Limit
  252. );
  253. /*++
  254. Routine Description:
  255. This routine returns the resource consumption limit of a given resource
  256. type.
  257. Arguments:
  258. Resource - Supplies the type of resource to get the limit for.
  259. Limit - Supplies a pointer where the soft (current) and hard limits for
  260. the resource are returned. If resource enforcement is not enabled for
  261. the given resource, RLIM_INFINITY is returned in these members.
  262. Return Value:
  263. 0 on success.
  264. -1 on failure, and the errno variable will be set to contain more
  265. information.
  266. --*/
  267. LIBC_API
  268. int
  269. setrlimit (
  270. int Resource,
  271. const struct rlimit *Limit
  272. );
  273. /*++
  274. Routine Description:
  275. This routine sets the resource consumption limit of a given resource
  276. type. Processes can adjust their soft limits between 0 and the hard limit
  277. (though for certain resource types the value may be adjusted). Processes
  278. can irreversibly decrease their hard limits. Only a process with
  279. appropriate permissions can increase the hard limit.
  280. Arguments:
  281. Resource - Supplies the type of resource to get the limit for.
  282. Limit - Supplies a pointer where the soft (current) and hard limits for
  283. the resource are returned. If resource enforcement is not enabled for
  284. the given resource, RLIM_INFINITY is returned in these members.
  285. Return Value:
  286. 0 on success.
  287. -1 on failure, and the errno variable will be set to contain more
  288. information.
  289. --*/
  290. LIBC_API
  291. int
  292. getrusage (
  293. int Who,
  294. struct rusage *Usage
  295. );
  296. /*++
  297. Routine Description:
  298. This routine returns the usage information for a given process, process
  299. group, or user.
  300. Arguments:
  301. Who - Supplies the entity or entities to get usage for. Valid values are
  302. RUSAGE_SELF to get resource usage for the current process, or
  303. RUSAGE_CHILDREN to get resource usage for terminated and waited for
  304. children of the current process. Additionally, RUSAGE_THREAD will get
  305. the usage information for the current thread, though this is a
  306. non-portable option.
  307. Usage - Supplies a pointer where the usage information is returned.
  308. Return Value:
  309. 0 on success.
  310. -1 on failure, and the errno variable will be set to contain more
  311. information.
  312. --*/
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #endif