1
0

shm.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*++
  2. Copyright (c) 2017 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. shm.h
  8. Abstract:
  9. This header contains definitions for the older SystemV style shared memory
  10. objects.
  11. Author:
  12. Evan Green 24-Mar-2017
  13. --*/
  14. #ifndef _SYS_SHM_H
  15. #define _SYS_SHM_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <libcbase.h>
  20. #include <sys/ipc.h>
  21. #include <time.h>
  22. //
  23. // Include unistd.h for sysconf.
  24. //
  25. #include <unistd.h>
  26. //
  27. // --------------------------------------------------------------------- Macros
  28. //
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. //
  33. // ---------------------------------------------------------------- Definitions
  34. //
  35. //
  36. // Define flags for shmat.
  37. //
  38. //
  39. // Set this flag to map the region read-only. The caller must have read
  40. // permissions on the object. If this is not set, the caller must have read and
  41. // write permissions on the object.
  42. //
  43. #define SHM_RDONLY 0x00001000
  44. //
  45. // Set this bit to round the attachment address down to SHMLBA if it is not
  46. // page aligned.
  47. //
  48. #define SHM_RND 0x00002000
  49. //
  50. // Set this bit to map the region as executable. The caller must have execute
  51. // permission on the region.
  52. //
  53. #define SHM_EXEC 0x00004000
  54. //
  55. // This bit is set if the shared memory object is scheduled for deletion after
  56. // the last handle is closed.
  57. //
  58. #define SHM_DEST 0x00010000
  59. //
  60. // Define the rounding granularity used when SHM_RND is set.
  61. //
  62. #define SHMLBA sysconf(_SC_PAGE_SIZE)
  63. //
  64. // Define the surrogate structure members for the traditional names in
  65. // struct shmid_ds.
  66. //
  67. #define shm_atime shm_atim.tv_sec
  68. #define shm_dtime shm_dtim.tv_sec
  69. #define shm_ctime shm_ctim.tv_sec
  70. //
  71. // ------------------------------------------------------ Data Type Definitions
  72. //
  73. typedef unsigned long shmatt_t;
  74. /*++
  75. Structure Description:
  76. This structure defines the properties of a shared memory object.
  77. Members:
  78. shm_perm - Stores the permission information for the object.
  79. shm_segsz - Stores the segment size in bytes.
  80. shm_atim - Stores the last time a process attached to the segment.
  81. shm_dtim - Stores the last time a process detached from the segment.
  82. shm_ctim - Stores the last time a process changed the segment using shmctl.
  83. shm_cpid - Stores the creator process ID.
  84. shm_lpid - Stores the process ID of the last process to attach or detach.
  85. shm_nattch - Stores the number of attachments.
  86. --*/
  87. struct shmid_ds {
  88. struct ipc_perm shm_perm;
  89. off_t shm_segsz;
  90. struct timespec shm_atim;
  91. struct timespec shm_dtim;
  92. struct timespec shm_ctim;
  93. pid_t shm_cpid;
  94. pid_t shm_lpid;
  95. shmatt_t shm_nattch;
  96. };
  97. //
  98. // -------------------------------------------------------------------- Globals
  99. //
  100. //
  101. // -------------------------------------------------------- Function Prototypes
  102. //
  103. LIBC_API
  104. int
  105. shmget (
  106. key_t Key,
  107. size_t Size,
  108. int Flags
  109. );
  110. /*++
  111. Routine Description:
  112. This routine creates or opens a shared memory object.
  113. Arguments:
  114. Key - Supplies the key associated with the new or existing object to open.
  115. Supply IPC_PRIVATE to always create a new object.
  116. Size - Supplies the minimum number of bytes in the region.
  117. Flags - Supplies a set of flags governing how the region is created. The
  118. bottom nine bits contain permission bits for the region. See IPC_*
  119. definitions for additional flags that can be passed like IPC_CREAT and
  120. IPC_EXCL.
  121. Return Value:
  122. Returns an integer representing the new or existing shared memory object
  123. on success.
  124. -1 on failure, and errno will contain more information.
  125. --*/
  126. LIBC_API
  127. void *
  128. shmat (
  129. int SharedMemoryObject,
  130. const void *Address,
  131. int Flags
  132. );
  133. /*++
  134. Routine Description:
  135. This routine attaches the current process to the given shared memory object,
  136. and maps it into the process' address space.
  137. Arguments:
  138. SharedMemoryObject - Supplies the value returned from shmget identifying
  139. the shared memory object.
  140. Address - Supplies an optional pointer to the address to map the object at.
  141. Supply NULL to allow the kernel to choose an address. If SHM_RND is
  142. supplied in the flags, this address may be rounded down to the nearest
  143. page. Otherwise, this address must be page aligned.
  144. Flags - Supplies a bitfield of flags governing the mapping.
  145. Return Value:
  146. Returns a pointer to the mapped region on success.
  147. -1 on failure, and errno will be set to contain more information.
  148. --*/
  149. LIBC_API
  150. int
  151. shmdt (
  152. const void *Address
  153. );
  154. /*++
  155. Routine Description:
  156. This routine detaches the current process from the shared memory object
  157. mapped at the given address, and unmaps the address.
  158. Arguments:
  159. Address - Supplies a pointer to the base address the shared memory object
  160. is mapped at.
  161. Return Value:
  162. 0 on success. The mapping will no longer be valid.
  163. -1 on failure, and errno will be set to contain more information.
  164. --*/
  165. LIBC_API
  166. int
  167. shmctl (
  168. int SharedMemoryObject,
  169. int Command,
  170. struct shmid_ds *Buffer
  171. );
  172. /*++
  173. Routine Description:
  174. This routine performs a control function on the given shared memory object.
  175. Arguments:
  176. SharedMemoryObject - Supplies the identifier returned by shmget.
  177. Command - Supplies the control command to execute. See IPC_* definitions.
  178. Buffer - Supplies a pointer to the shared memory information buffer.
  179. Return Value:
  180. 0 on success.
  181. -1 on error, and errno will be set to the stat error information for the
  182. given file path.
  183. --*/
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif