logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

shm_open.3p (13512B)


  1. '\" et
  2. .TH SHM_OPEN "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. shm_open
  12. \(em open a shared memory object
  13. (\fBREALTIME\fP)
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <sys/mman.h>
  18. .P
  19. int shm_open(const char *\fIname\fP, int \fIoflag\fP, mode_t \fImode\fP);
  20. .fi
  21. .SH DESCRIPTION
  22. The
  23. \fIshm_open\fR()
  24. function shall establish a connection between a shared memory object
  25. and a file descriptor. It shall create an open file description that
  26. refers to the shared memory object and a file descriptor that refers to
  27. that open file description. The file descriptor shall be allocated as
  28. described in
  29. .IR "Section 2.14" ", " "File Descriptor Allocation",
  30. and can be used by other functions to refer to that shared memory object.
  31. The
  32. .IR name
  33. argument points to a string naming a shared memory object. It is
  34. unspecified whether the name appears in the file system and is visible
  35. to other functions that take pathnames as arguments. The
  36. .IR name
  37. argument conforms to the construction rules for a pathname, except that
  38. the interpretation of
  39. <slash>
  40. characters other than the leading
  41. <slash>
  42. character in
  43. .IR name
  44. is implementation-defined, and that the length limits for the
  45. .IR name
  46. argument are implementation-defined and need not be the same as the
  47. pathname limits
  48. {PATH_MAX}
  49. and
  50. {NAME_MAX}.
  51. If
  52. .IR name
  53. begins with the
  54. <slash>
  55. character, then processes calling
  56. \fIshm_open\fR()
  57. with the same value of
  58. .IR name
  59. refer to the same shared memory object, as long as that name has not
  60. been removed. If
  61. .IR name
  62. does not begin with the
  63. <slash>
  64. character, the effect is implementation-defined.
  65. .P
  66. If successful,
  67. \fIshm_open\fR()
  68. shall return a file descriptor for the shared memory object.
  69. The open file description is new, and therefore the file descriptor
  70. does not share it with any other processes. It is unspecified whether
  71. the file offset is set. The FD_CLOEXEC
  72. file descriptor flag associated with the new file descriptor is set.
  73. .P
  74. The file status flags and file access modes of the open file
  75. description are according to the value of
  76. .IR oflag .
  77. The
  78. .IR oflag
  79. argument is the bitwise-inclusive OR of the following flags defined in
  80. the
  81. .IR <fcntl.h>
  82. header. Applications specify exactly one of the first two values
  83. (access modes) below in the value of
  84. .IR oflag :
  85. .IP O_RDONLY 12
  86. Open for read access only.
  87. .IP O_RDWR 12
  88. Open for read or write access.
  89. .P
  90. Any combination of the remaining flags may be specified in the value of
  91. .IR oflag :
  92. .IP O_CREAT 12
  93. If the shared memory object exists, this flag has no effect, except
  94. as noted under O_EXCL below. Otherwise, the shared memory object is
  95. created. The user ID of the shared memory object shall be set to the
  96. effective user ID of the process. The group ID of the shared memory object
  97. shall be set to the effective group ID of the process; however, if the
  98. .IR name
  99. argument is visible in the file system, the group ID may be set to the
  100. group ID of the containing directory. The permission bits of the shared
  101. memory object shall be set to the value of the
  102. .IR mode
  103. argument except those set in the file mode creation mask of the
  104. process. When bits in
  105. .IR mode
  106. other than the file permission bits are set, the effect is
  107. unspecified. The
  108. .IR mode
  109. argument does not affect whether the shared memory object is opened for
  110. reading, for writing, or for both. The shared memory object has a size
  111. of zero.
  112. .IP O_EXCL 12
  113. If O_EXCL and O_CREAT are set,
  114. \fIshm_open\fR()
  115. fails if the shared memory object exists. The check for the existence
  116. of the shared memory object and the creation of the object if it does
  117. not exist is atomic with respect to other processes executing
  118. \fIshm_open\fR()
  119. naming the same shared memory object with O_EXCL and O_CREAT set. If
  120. O_EXCL is set and O_CREAT is not set, the result is undefined.
  121. .IP O_TRUNC 12
  122. If the shared memory object exists, and it is successfully opened
  123. O_RDWR, the object shall be truncated to zero length and the mode and
  124. owner shall be unchanged by this function call. The result of using
  125. O_TRUNC with O_RDONLY is undefined.
  126. .P
  127. When a shared memory object is created, the state of the shared memory
  128. object, including all data associated with the shared memory object,
  129. persists until the shared memory object is unlinked and all other
  130. references are gone. It is unspecified whether the name and shared
  131. memory object state remain valid after a system reboot.
  132. .SH "RETURN VALUE"
  133. Upon successful completion, the
  134. \fIshm_open\fR()
  135. function shall return a non-negative integer representing the
  136. file descriptor. Otherwise, it shall return \-1 and
  137. set
  138. .IR errno
  139. to indicate the error.
  140. .SH ERRORS
  141. The
  142. \fIshm_open\fR()
  143. function shall fail if:
  144. .TP
  145. .BR EACCES
  146. The shared memory object exists and the permissions specified by
  147. .IR oflag
  148. are denied, or the shared memory object does not exist and permission
  149. to create the shared memory object is denied, or O_TRUNC is specified
  150. and write permission is denied.
  151. .TP
  152. .BR EEXIST
  153. O_CREAT and O_EXCL are set and
  154. the named shared memory object already exists.
  155. .TP
  156. .BR EINTR
  157. The
  158. \fIshm_open\fR()
  159. operation was interrupted by a signal.
  160. .TP
  161. .BR EINVAL
  162. The
  163. \fIshm_open\fR()
  164. operation is not supported for the given name.
  165. .TP
  166. .BR EMFILE
  167. All file descriptors available to the process are currently open.
  168. .TP
  169. .BR ENFILE
  170. Too many shared memory objects are currently open in the system.
  171. .TP
  172. .BR ENOENT
  173. O_CREAT is not set and the named shared memory object does not exist.
  174. .TP
  175. .BR ENOSPC
  176. There is insufficient space for the creation of the new shared memory
  177. object.
  178. .P
  179. The
  180. \fIshm_open\fR()
  181. function may fail if:
  182. .TP
  183. .BR ENAMETOOLONG
  184. .br
  185. The length of the
  186. .IR name
  187. argument exceeds
  188. {_POSIX_PATH_MAX}
  189. on systems that do not support the XSI option
  190. or exceeds
  191. {_XOPEN_PATH_MAX}
  192. on XSI systems,
  193. or has a pathname component that is longer than
  194. {_POSIX_NAME_MAX}
  195. on systems that do not support the XSI option
  196. or longer than
  197. {_XOPEN_NAME_MAX}
  198. on XSI systems.
  199. .LP
  200. .IR "The following sections are informative."
  201. .SH EXAMPLES
  202. .SS "Creating and Mapping a Shared Memory Object"
  203. .P
  204. The following code segment demonstrates the use of
  205. \fIshm_open\fR()
  206. to create a shared memory object which is then sized using
  207. \fIftruncate\fR()
  208. before being mapped into the process address space using
  209. \fImmap\fR():
  210. .sp
  211. .RS 4
  212. .nf
  213. #include <unistd.h>
  214. #include <sys/mman.h>
  215. \&...
  216. .P
  217. #define MAX_LEN 10000
  218. struct region { /* Defines "structure" of shared memory */
  219. int len;
  220. char buf[MAX_LEN];
  221. };
  222. struct region *rptr;
  223. int fd;
  224. .P
  225. /* Create shared memory object and set its size */
  226. .P
  227. fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  228. if (fd == -1)
  229. /* Handle error */;
  230. .P
  231. if (ftruncate(fd, sizeof(struct region)) == -1)
  232. /* Handle error */;
  233. .P
  234. /* Map shared memory object */
  235. .P
  236. rptr = mmap(NULL, sizeof(struct region),
  237. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  238. if (rptr == MAP_FAILED)
  239. /* Handle error */;
  240. .P
  241. /* Now we can refer to mapped region using fields of rptr;
  242. for example, rptr->len */
  243. \&...
  244. .fi
  245. .P
  246. .RE
  247. .SH "APPLICATION USAGE"
  248. None.
  249. .SH RATIONALE
  250. When the Memory Mapped Files option is supported, the normal
  251. \fIopen\fR()
  252. call is used to obtain a descriptor to a file to be mapped according to
  253. existing practice with
  254. \fImmap\fR().
  255. When the Shared Memory Objects option is supported, the
  256. \fIshm_open\fR()
  257. function shall obtain a descriptor to the shared memory object
  258. to be mapped.
  259. .P
  260. There is ample precedent for having a file descriptor represent several
  261. types of objects. In the POSIX.1\(hy1990 standard, a file descriptor can represent a
  262. file, a pipe, a FIFO, a tty, or a directory. Many implementations
  263. simply have an operations vector, which is indexed by the file
  264. descriptor type and does very different operations. Note that in some
  265. cases the file descriptor passed to generic operations on file
  266. descriptors is returned by
  267. \fIopen\fR()
  268. or
  269. \fIcreat\fR()
  270. and in some cases returned by alternate functions, such as
  271. \fIpipe\fR().
  272. The latter technique is used by
  273. \fIshm_open\fR().
  274. .P
  275. Note that such shared memory objects can actually be implemented as
  276. mapped files. In both cases, the size can be set after the open using
  277. \fIftruncate\fR().
  278. The
  279. \fIshm_open\fR()
  280. function itself does not create a shared object of a specified size
  281. because this would duplicate an extant function that set the size of
  282. an object referenced by a file descriptor.
  283. .P
  284. On implementations where memory objects are implemented using the
  285. existing file system, the
  286. \fIshm_open\fR()
  287. function may be implemented using a macro that invokes
  288. \fIopen\fR(),
  289. and the
  290. \fIshm_unlink\fR()
  291. function may be implemented using a macro that invokes
  292. \fIunlink\fR().
  293. .P
  294. For implementations without a permanent file system, the definition of
  295. the name of the memory objects is allowed not to survive a system
  296. reboot. Note that this allows systems with a permanent file system to
  297. implement memory objects as data structures internal to the
  298. implementation as well.
  299. .P
  300. On implementations that choose to implement memory objects using memory
  301. directly, a
  302. \fIshm_open\fR()
  303. followed by an
  304. \fIftruncate\fR()
  305. and
  306. \fIclose\fR()
  307. can be used to preallocate a shared memory area and to set the size of
  308. that preallocation. This may be necessary for systems without virtual
  309. memory hardware support in order to ensure that the memory is
  310. contiguous.
  311. .P
  312. The set of valid open flags to
  313. \fIshm_open\fR()
  314. was restricted to O_RDONLY, O_RDWR, O_CREAT, and O_TRUNC
  315. because these could be easily implemented on most memory mapping
  316. systems. This volume of POSIX.1\(hy2017 is silent on the results if the implementation
  317. cannot supply the requested file access because of
  318. implementation-defined reasons, including hardware ones.
  319. .P
  320. The error conditions
  321. .BR [EACCES]
  322. and
  323. .BR [ENOTSUP]
  324. are provided to inform the application that the implementation cannot
  325. complete a request.
  326. .P
  327. .BR [EACCES]
  328. indicates for implementation-defined reasons, probably
  329. hardware-related, that the implementation cannot comply with a
  330. requested mode because it conflicts with another requested mode. An
  331. example might be that an application desires to open a memory object
  332. two times, mapping different areas with different access modes. If the
  333. implementation cannot map a single area into a process space in two
  334. places, which would be required if different access modes were required
  335. for the two areas, then the implementation may inform the application
  336. at the time of the second open.
  337. .P
  338. .BR [ENOTSUP]
  339. indicates for implementation-defined reasons, probably
  340. hardware-related, that the implementation cannot comply with a
  341. requested mode at all. An example would be that the hardware of the
  342. implementation cannot support write-only shared memory areas.
  343. .P
  344. On all implementations, it may be desirable to restrict the location of
  345. the memory objects to specific file systems for performance (such as a
  346. RAM disk) or implementation-defined reasons (shared memory supported
  347. directly only on certain file systems). The
  348. \fIshm_open\fR()
  349. function may be used to enforce these restrictions. There are a number
  350. of methods available to the application to determine an appropriate
  351. name of the file or the location of an appropriate directory. One way
  352. is from the environment via
  353. \fIgetenv\fR().
  354. Another would be from a configuration file.
  355. .P
  356. This volume of POSIX.1\(hy2017 specifies that memory objects have initial contents of
  357. zero when created. This is consistent with current behavior for both
  358. files and newly allocated memory. For those implementations that use
  359. physical memory, it would be possible that such implementations could
  360. simply use available memory and give it to the process uninitialized.
  361. This, however, is not consistent with standard behavior for the
  362. uninitialized data area, the stack, and of course, files. Finally, it
  363. is highly desirable to set the allocated memory to zero for security
  364. reasons. Thus, initializing memory objects to zero is required.
  365. .SH "FUTURE DIRECTIONS"
  366. A future version might require the
  367. \fIshm_open\fR()
  368. and
  369. \fIshm_unlink\fR()
  370. functions to have semantics similar to normal file system operations.
  371. .SH "SEE ALSO"
  372. .IR "Section 2.14" ", " "File Descriptor Allocation",
  373. .IR "\fIclose\fR\^(\|)",
  374. .IR "\fIdup\fR\^(\|)",
  375. .IR "\fIexec\fR\^",
  376. .IR "\fIfcntl\fR\^(\|)",
  377. .IR "\fImmap\fR\^(\|)",
  378. .IR "\fIshmat\fR\^(\|)",
  379. .IR "\fIshmctl\fR\^(\|)",
  380. .IR "\fIshmdt\fR\^(\|)",
  381. .IR "\fIshm_unlink\fR\^(\|)",
  382. .IR "\fIumask\fR\^(\|)"
  383. .P
  384. The Base Definitions volume of POSIX.1\(hy2017,
  385. .IR "\fB<fcntl.h>\fP",
  386. .IR "\fB<sys_mman.h>\fP"
  387. .\"
  388. .SH COPYRIGHT
  389. Portions of this text are reprinted and reproduced in electronic form
  390. from IEEE Std 1003.1-2017, Standard for Information Technology
  391. -- Portable Operating System Interface (POSIX), The Open Group Base
  392. Specifications Issue 7, 2018 Edition,
  393. Copyright (C) 2018 by the Institute of
  394. Electrical and Electronics Engineers, Inc and The Open Group.
  395. In the event of any discrepancy between this version and the original IEEE and
  396. The Open Group Standard, the original IEEE and The Open Group Standard
  397. is the referee document. The original Standard can be obtained online at
  398. http://www.opengroup.org/unix/online.html .
  399. .PP
  400. Any typographical or formatting errors that appear
  401. in this page are most likely
  402. to have been introduced during the conversion of the source files to
  403. man page format. To report such errors, see
  404. https://www.kernel.org/doc/man-pages/reporting_bugs.html .