KSSysCtl.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // KSSysCtl.h
  3. //
  4. // Created by Karl Stenerud on 2012-02-19.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. /* Convenience wrapper functions for sysctl calls.
  27. */
  28. #ifndef HDR_KSSysCtl_h
  29. #define HDR_KSSysCtl_h
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include <stdbool.h>
  34. #include <stdint.h>
  35. #include <sys/sysctl.h>
  36. /** Get an int32 value via sysctl.
  37. *
  38. * @param major_cmd The major part of the command or name.
  39. *
  40. * @param minor_cmd The minor part of the command or name.
  41. *
  42. * @return The value returned by sysctl.
  43. */
  44. int32_t kssysctl_int32(int major_cmd, int minor_cmd);
  45. /** Get an int32 value via sysctl by name.
  46. *
  47. * @param name The name of the command.
  48. *
  49. * @return The value returned by sysctl.
  50. */
  51. int32_t kssysctl_int32ForName(const char* name);
  52. /** Get a uint32 value via sysctl.
  53. *
  54. * @param major_cmd The major part of the command or name.
  55. *
  56. * @param minor_cmd The minor part of the command or name.
  57. *
  58. * @return The value returned by sysctl.
  59. */
  60. uint32_t kssysctl_uint32(int major_cmd, int minor_cmd);
  61. /** Get a uint32 value via sysctl by name.
  62. *
  63. * @param name The name of the command.
  64. *
  65. * @return The value returned by sysctl.
  66. */
  67. uint32_t kssysctl_uint32ForName(const char* name);
  68. /** Get an int64 value via sysctl.
  69. *
  70. * @param major_cmd The major part of the command or name.
  71. *
  72. * @param minor_cmd The minor part of the command or name.
  73. *
  74. * @return The value returned by sysctl.
  75. */
  76. int64_t kssysctl_int64(int major_cmd, int minor_cmd);
  77. /** Get an int64 value via sysctl by name.
  78. *
  79. * @param name The name of the command.
  80. *
  81. * @return The value returned by sysctl.
  82. */
  83. int64_t kssysctl_int64ForName(const char* name);
  84. /** Get a uint64 value via sysctl.
  85. *
  86. * @param major_cmd The major part of the command or name.
  87. *
  88. * @param minor_cmd The minor part of the command or name.
  89. *
  90. * @return The value returned by sysctl.
  91. */
  92. uint64_t kssysctl_uint64(int major_cmd, int minor_cmd);
  93. /** Get a uint64 value via sysctl by name.
  94. *
  95. * @param name The name of the command.
  96. *
  97. * @return The value returned by sysctl.
  98. */
  99. uint64_t kssysctl_uint64ForName(const char* name);
  100. /** Get a string value via sysctl.
  101. *
  102. * @param major_cmd The major part of the command or name.
  103. *
  104. * @param minor_cmd The minor part of the command or name.
  105. *
  106. * @param value Pointer to a buffer to fill out. If NULL, does not fill
  107. * anything out.
  108. *
  109. * @param maxSize The size of the buffer pointed to by value.
  110. *
  111. * @return The number of bytes written (or that would have been written if
  112. * value is NULL).
  113. */
  114. int kssysctl_string(int major_cmd, int minor_cmd, char* value, int maxSize);
  115. /** Get a string value via sysctl by name.
  116. *
  117. * @param name The name of the command.
  118. *
  119. * @param value Pointer to a buffer to fill out. If NULL, does not fill
  120. * anything out.
  121. *
  122. * @param maxSize The size of the buffer pointed to by value.
  123. *
  124. * @return The number of bytes written (or that would have been written if
  125. * value is NULL).
  126. */
  127. int kssysctl_stringForName(const char* name, char* value, int maxSize);
  128. /** Get a timeval value via sysctl.
  129. *
  130. * @param major_cmd The major part of the command or name.
  131. *
  132. * @param minor_cmd The minor part of the command or name.
  133. *
  134. * @return The value returned by sysctl.
  135. */
  136. struct timeval kssysctl_timeval(int major_cmd, int minor_cmd);
  137. /** Get a timeval value via sysctl by name.
  138. *
  139. * @param name The name of the command.
  140. *
  141. * @return The value returned by sysctl.
  142. */
  143. struct timeval kssysctl_timevalForName(const char* name);
  144. /** Get information about a process.
  145. *
  146. * @param pid The process ID.
  147. *
  148. * @param procInfo Struct to hold the proces information.
  149. *
  150. * @return true if the operation was successful.
  151. */
  152. bool kssysctl_getProcessInfo(int pid, struct kinfo_proc* procInfo);
  153. /** Get the MAC address of the specified interface.
  154. * Note: As of iOS 7 this will always return a fixed value of 02:00:00:00:00:00.
  155. *
  156. * @param name Interface name (e.g. "en1").
  157. *
  158. * @param macAddressBuffer 6 bytes of storage to hold the MAC address.
  159. *
  160. * @return true if the address was successfully retrieved.
  161. */
  162. bool kssysctl_getMacAddress(const char* name, char* macAddressBuffer);
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #endif // HDR_KSSysCtl_h