shared_alloc_posix.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | https://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Stanislav Malyshev <stas@zend.com> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #include "zend_shared_alloc.h"
  22. #ifdef USE_SHM_OPEN
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27. #include <sys/mman.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. typedef struct {
  31. zend_shared_segment common;
  32. int shm_fd;
  33. } zend_shared_segment_posix;
  34. static int create_segments(size_t requested_size, zend_shared_segment_posix ***shared_segments_p, int *shared_segments_count, char **error_in)
  35. {
  36. zend_shared_segment_posix *shared_segment;
  37. char shared_segment_name[sizeof("/ZendAccelerator.") + 20];
  38. *shared_segments_count = 1;
  39. *shared_segments_p = (zend_shared_segment_posix **) calloc(1, sizeof(zend_shared_segment_posix) + sizeof(void *));
  40. if (!*shared_segments_p) {
  41. *error_in = "calloc";
  42. return ALLOC_FAILURE;
  43. }
  44. shared_segment = (zend_shared_segment_posix *)((char *)(*shared_segments_p) + sizeof(void *));
  45. (*shared_segments_p)[0] = shared_segment;
  46. sprintf(shared_segment_name, "/ZendAccelerator.%d", getpid());
  47. shared_segment->shm_fd = shm_open(shared_segment_name, O_RDWR|O_CREAT|O_TRUNC, 0600);
  48. if (shared_segment->shm_fd == -1) {
  49. *error_in = "shm_open";
  50. return ALLOC_FAILURE;
  51. }
  52. if (ftruncate(shared_segment->shm_fd, requested_size) != 0) {
  53. *error_in = "ftruncate";
  54. shm_unlink(shared_segment_name);
  55. return ALLOC_FAILURE;
  56. }
  57. shared_segment->common.p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_segment->shm_fd, 0);
  58. if (shared_segment->common.p == MAP_FAILED) {
  59. *error_in = "mmap";
  60. shm_unlink(shared_segment_name);
  61. return ALLOC_FAILURE;
  62. }
  63. shm_unlink(shared_segment_name);
  64. shared_segment->common.pos = 0;
  65. shared_segment->common.size = requested_size;
  66. return ALLOC_SUCCESS;
  67. }
  68. static int detach_segment(zend_shared_segment_posix *shared_segment)
  69. {
  70. munmap(shared_segment->common.p, shared_segment->common.size);
  71. close(shared_segment->shm_fd);
  72. return 0;
  73. }
  74. static size_t segment_type_size(void)
  75. {
  76. return sizeof(zend_shared_segment_posix);
  77. }
  78. zend_shared_memory_handlers zend_alloc_posix_handlers = {
  79. (create_segments_t)create_segments,
  80. (detach_segment_t)detach_segment,
  81. segment_type_size
  82. };
  83. #endif /* USE_SHM_OPEN */