shm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_arch_shm.h"
  17. #include "fspr_general.h"
  18. #include "fspr_errno.h"
  19. #include "fspr_user.h"
  20. #include "fspr_strings.h"
  21. static fspr_status_t shm_cleanup_owner(void *m_)
  22. {
  23. fspr_shm_t *m = (fspr_shm_t *)m_;
  24. /* anonymous shared memory */
  25. if (m->filename == NULL) {
  26. #if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
  27. if (munmap(m->base, m->realsize) == -1) {
  28. return errno;
  29. }
  30. return APR_SUCCESS;
  31. #endif
  32. #if APR_USE_SHMEM_SHMGET_ANON
  33. if (shmdt(m->base) == -1) {
  34. return errno;
  35. }
  36. /* This segment will automatically remove itself after all
  37. * references have detached. */
  38. return APR_SUCCESS;
  39. #endif
  40. }
  41. /* name-based shared memory */
  42. else {
  43. #if APR_USE_SHMEM_MMAP_TMP
  44. if (munmap(m->base, m->realsize) == -1) {
  45. return errno;
  46. }
  47. return fspr_file_remove(m->filename, m->pool);
  48. #endif
  49. #if APR_USE_SHMEM_MMAP_SHM
  50. if (munmap(m->base, m->realsize) == -1) {
  51. return errno;
  52. }
  53. if (shm_unlink(m->filename) == -1) {
  54. return errno;
  55. }
  56. return APR_SUCCESS;
  57. #endif
  58. #if APR_USE_SHMEM_SHMGET
  59. /* Indicate that the segment is to be destroyed as soon
  60. * as all processes have detached. This also disallows any
  61. * new attachments to the segment. */
  62. if (shmctl(m->shmid, IPC_RMID, NULL) == -1) {
  63. return errno;
  64. }
  65. if (shmdt(m->base) == -1) {
  66. return errno;
  67. }
  68. return fspr_file_remove(m->filename, m->pool);
  69. #endif
  70. }
  71. return APR_ENOTIMPL;
  72. }
  73. APR_DECLARE(fspr_status_t) fspr_shm_create(fspr_shm_t **m,
  74. fspr_size_t reqsize,
  75. const char *filename,
  76. fspr_pool_t *pool)
  77. {
  78. fspr_shm_t *new_m;
  79. fspr_status_t status;
  80. #if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON
  81. struct shmid_ds shmbuf;
  82. fspr_uid_t uid;
  83. fspr_gid_t gid;
  84. #endif
  85. #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || \
  86. APR_USE_SHMEM_MMAP_ZERO
  87. int tmpfd;
  88. #endif
  89. #if APR_USE_SHMEM_SHMGET
  90. fspr_size_t nbytes;
  91. key_t shmkey;
  92. #endif
  93. #if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_SHMGET || \
  94. APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
  95. fspr_file_t *file; /* file where metadata is stored */
  96. #endif
  97. /* Check if they want anonymous or name-based shared memory */
  98. if (filename == NULL) {
  99. #if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
  100. new_m = fspr_palloc(pool, sizeof(fspr_shm_t));
  101. new_m->pool = pool;
  102. new_m->reqsize = reqsize;
  103. new_m->realsize = reqsize +
  104. APR_ALIGN_DEFAULT(sizeof(fspr_size_t)); /* room for metadata */
  105. new_m->filename = NULL;
  106. #if APR_USE_SHMEM_MMAP_ZERO
  107. status = fspr_file_open(&file, "/dev/zero", APR_READ | APR_WRITE,
  108. APR_OS_DEFAULT, pool);
  109. if (status != APR_SUCCESS) {
  110. return status;
  111. }
  112. status = fspr_os_file_get(&tmpfd, file);
  113. if (status != APR_SUCCESS) {
  114. return status;
  115. }
  116. new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
  117. MAP_SHARED, tmpfd, 0);
  118. if (new_m->base == (void *)MAP_FAILED) {
  119. return errno;
  120. }
  121. status = fspr_file_close(file);
  122. if (status != APR_SUCCESS) {
  123. return status;
  124. }
  125. /* store the real size in the metadata */
  126. *(fspr_size_t*)(new_m->base) = new_m->realsize;
  127. /* metadata isn't usable */
  128. new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(fspr_size_t));
  129. fspr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
  130. fspr_pool_cleanup_null);
  131. *m = new_m;
  132. return APR_SUCCESS;
  133. #elif APR_USE_SHMEM_MMAP_ANON
  134. new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
  135. MAP_ANON|MAP_SHARED, -1, 0);
  136. if (new_m->base == (void *)MAP_FAILED) {
  137. return errno;
  138. }
  139. /* store the real size in the metadata */
  140. *(fspr_size_t*)(new_m->base) = new_m->realsize;
  141. /* metadata isn't usable */
  142. new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(fspr_size_t));
  143. fspr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
  144. fspr_pool_cleanup_null);
  145. *m = new_m;
  146. return APR_SUCCESS;
  147. #endif /* APR_USE_SHMEM_MMAP_ZERO */
  148. #endif /* APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON */
  149. #if APR_USE_SHMEM_SHMGET_ANON
  150. new_m = fspr_palloc(pool, sizeof(fspr_shm_t));
  151. new_m->pool = pool;
  152. new_m->reqsize = reqsize;
  153. new_m->realsize = reqsize;
  154. new_m->filename = NULL;
  155. if ((new_m->shmid = shmget(IPC_PRIVATE, new_m->realsize,
  156. SHM_R | SHM_W | IPC_CREAT)) < 0) {
  157. return errno;
  158. }
  159. if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
  160. return errno;
  161. }
  162. new_m->usable = new_m->base;
  163. if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
  164. return errno;
  165. }
  166. fspr_uid_current(&uid, &gid, pool);
  167. shmbuf.shm_perm.uid = uid;
  168. shmbuf.shm_perm.gid = gid;
  169. if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
  170. return errno;
  171. }
  172. /* Remove the segment once use count hits zero.
  173. * We will not attach to this segment again, since it is
  174. * anonymous memory, so it is ok to mark it for deletion.
  175. */
  176. if (shmctl(new_m->shmid, IPC_RMID, NULL) == -1) {
  177. return errno;
  178. }
  179. fspr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
  180. fspr_pool_cleanup_null);
  181. *m = new_m;
  182. return APR_SUCCESS;
  183. #endif /* APR_USE_SHMEM_SHMGET_ANON */
  184. /* It is an error if they want anonymous memory but we don't have it. */
  185. return APR_ENOTIMPL; /* requested anonymous but we don't have it */
  186. }
  187. /* Name-based shared memory */
  188. else {
  189. new_m = fspr_palloc(pool, sizeof(fspr_shm_t));
  190. new_m->pool = pool;
  191. new_m->reqsize = reqsize;
  192. new_m->filename = fspr_pstrdup(pool, filename);
  193. #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
  194. new_m->realsize = reqsize +
  195. APR_ALIGN_DEFAULT(sizeof(fspr_size_t)); /* room for metadata */
  196. /* FIXME: Ignore error for now. *
  197. * status = fspr_file_remove(file, pool);*/
  198. status = APR_SUCCESS;
  199. #if APR_USE_SHMEM_MMAP_TMP
  200. /* FIXME: Is APR_OS_DEFAULT sufficient? */
  201. status = fspr_file_open(&file, filename,
  202. APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
  203. APR_OS_DEFAULT, pool);
  204. if (status != APR_SUCCESS) {
  205. return status;
  206. }
  207. status = fspr_os_file_get(&tmpfd, file);
  208. if (status != APR_SUCCESS) {
  209. fspr_file_close(file); /* ignore errors, we're failing */
  210. fspr_file_remove(new_m->filename, new_m->pool);
  211. return status;
  212. }
  213. status = fspr_file_trunc(file, new_m->realsize);
  214. if (status != APR_SUCCESS) {
  215. fspr_file_close(file); /* ignore errors, we're failing */
  216. fspr_file_remove(new_m->filename, new_m->pool);
  217. return status;
  218. }
  219. new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
  220. MAP_SHARED, tmpfd, 0);
  221. /* FIXME: check for errors */
  222. status = fspr_file_close(file);
  223. if (status != APR_SUCCESS) {
  224. return status;
  225. }
  226. #endif /* APR_USE_SHMEM_MMAP_TMP */
  227. #if APR_USE_SHMEM_MMAP_SHM
  228. tmpfd = shm_open(filename, O_RDWR | O_CREAT | O_EXCL, 0644);
  229. if (tmpfd == -1) {
  230. return errno;
  231. }
  232. status = fspr_os_file_put(&file, &tmpfd,
  233. APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
  234. pool);
  235. if (status != APR_SUCCESS) {
  236. return status;
  237. }
  238. status = fspr_file_trunc(file, new_m->realsize);
  239. if (status != APR_SUCCESS) {
  240. shm_unlink(filename); /* we're failing, remove the object */
  241. return status;
  242. }
  243. new_m->base = mmap(NULL, reqsize, PROT_READ | PROT_WRITE,
  244. MAP_SHARED, tmpfd, 0);
  245. /* FIXME: check for errors */
  246. status = fspr_file_close(file);
  247. if (status != APR_SUCCESS) {
  248. return status;
  249. }
  250. #endif /* APR_USE_SHMEM_MMAP_SHM */
  251. /* store the real size in the metadata */
  252. *(fspr_size_t*)(new_m->base) = new_m->realsize;
  253. /* metadata isn't usable */
  254. new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(fspr_size_t));
  255. fspr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
  256. fspr_pool_cleanup_null);
  257. *m = new_m;
  258. return APR_SUCCESS;
  259. #endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
  260. #if APR_USE_SHMEM_SHMGET
  261. new_m->realsize = reqsize;
  262. /* FIXME: APR_OS_DEFAULT is too permissive, switch to 600 I think. */
  263. status = fspr_file_open(&file, filename,
  264. APR_WRITE | APR_CREATE | APR_EXCL,
  265. APR_OS_DEFAULT, pool);
  266. if (status != APR_SUCCESS) {
  267. return status;
  268. }
  269. /* ftok() (on solaris at least) requires that the file actually
  270. * exist before calling ftok(). */
  271. shmkey = ftok(filename, 1);
  272. if (shmkey == (key_t)-1) {
  273. return errno;
  274. }
  275. if ((new_m->shmid = shmget(shmkey, new_m->realsize,
  276. SHM_R | SHM_W | IPC_CREAT | IPC_EXCL)) < 0) {
  277. return errno;
  278. }
  279. if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
  280. return errno;
  281. }
  282. new_m->usable = new_m->base;
  283. if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
  284. return errno;
  285. }
  286. fspr_uid_current(&uid, &gid, pool);
  287. shmbuf.shm_perm.uid = uid;
  288. shmbuf.shm_perm.gid = gid;
  289. if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
  290. return errno;
  291. }
  292. nbytes = sizeof(reqsize);
  293. status = fspr_file_write(file, (const void *)&reqsize,
  294. &nbytes);
  295. if (status != APR_SUCCESS) {
  296. return status;
  297. }
  298. status = fspr_file_close(file);
  299. if (status != APR_SUCCESS) {
  300. return status;
  301. }
  302. fspr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
  303. fspr_pool_cleanup_null);
  304. *m = new_m;
  305. return APR_SUCCESS;
  306. #endif /* APR_USE_SHMEM_SHMGET */
  307. }
  308. return APR_ENOTIMPL;
  309. }
  310. APR_DECLARE(fspr_status_t) fspr_shm_remove(const char *filename,
  311. fspr_pool_t *pool)
  312. {
  313. #if APR_USE_SHMEM_SHMGET
  314. fspr_status_t status;
  315. fspr_file_t *file;
  316. key_t shmkey;
  317. int shmid;
  318. #endif
  319. #if APR_USE_SHMEM_MMAP_TMP
  320. return fspr_file_remove(filename, pool);
  321. #endif
  322. #if APR_USE_SHMEM_MMAP_SHM
  323. if (shm_unlink(filename) == -1) {
  324. return errno;
  325. }
  326. return APR_SUCCESS;
  327. #endif
  328. #if APR_USE_SHMEM_SHMGET
  329. /* Presume that the file already exists; just open for writing */
  330. status = fspr_file_open(&file, filename, APR_WRITE,
  331. APR_OS_DEFAULT, pool);
  332. if (status) {
  333. return status;
  334. }
  335. /* ftok() (on solaris at least) requires that the file actually
  336. * exist before calling ftok(). */
  337. shmkey = ftok(filename, 1);
  338. if (shmkey == (key_t)-1) {
  339. goto shm_remove_failed;
  340. }
  341. fspr_file_close(file);
  342. if ((shmid = shmget(shmkey, 0, SHM_R | SHM_W)) < 0) {
  343. goto shm_remove_failed;
  344. }
  345. /* Indicate that the segment is to be destroyed as soon
  346. * as all processes have detached. This also disallows any
  347. * new attachments to the segment. */
  348. if (shmctl(shmid, IPC_RMID, NULL) == -1) {
  349. goto shm_remove_failed;
  350. }
  351. return fspr_file_remove(filename, pool);
  352. shm_remove_failed:
  353. status = errno;
  354. /* ensure the file has been removed anyway. */
  355. fspr_file_remove(filename, pool);
  356. return status;
  357. #endif
  358. /* No support for anonymous shm */
  359. return APR_ENOTIMPL;
  360. }
  361. APR_DECLARE(fspr_status_t) fspr_shm_destroy(fspr_shm_t *m)
  362. {
  363. return fspr_pool_cleanup_run(m->pool, m, shm_cleanup_owner);
  364. }
  365. static fspr_status_t shm_cleanup_attach(void *m_)
  366. {
  367. fspr_shm_t *m = (fspr_shm_t *)m_;
  368. if (m->filename == NULL) {
  369. /* It doesn't make sense to detach from an anonymous memory segment. */
  370. return APR_EINVAL;
  371. }
  372. else {
  373. #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
  374. if (munmap(m->base, m->realsize) == -1) {
  375. return errno;
  376. }
  377. return APR_SUCCESS;
  378. #endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
  379. #if APR_USE_SHMEM_SHMGET
  380. if (shmdt(m->base) == -1) {
  381. return errno;
  382. }
  383. return APR_SUCCESS;
  384. #endif
  385. }
  386. return APR_ENOTIMPL;
  387. }
  388. APR_DECLARE(fspr_status_t) fspr_shm_attach(fspr_shm_t **m,
  389. const char *filename,
  390. fspr_pool_t *pool)
  391. {
  392. if (filename == NULL) {
  393. /* It doesn't make sense to attach to a segment if you don't know
  394. * the filename. */
  395. return APR_EINVAL;
  396. }
  397. else {
  398. #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
  399. fspr_shm_t *new_m;
  400. fspr_status_t status;
  401. int tmpfd;
  402. fspr_file_t *file; /* file where metadata is stored */
  403. fspr_size_t nbytes;
  404. new_m = fspr_palloc(pool, sizeof(fspr_shm_t));
  405. new_m->pool = pool;
  406. new_m->filename = fspr_pstrdup(pool, filename);
  407. status = fspr_file_open(&file, filename,
  408. APR_READ | APR_WRITE,
  409. APR_OS_DEFAULT, pool);
  410. if (status != APR_SUCCESS) {
  411. return status;
  412. }
  413. status = fspr_os_file_get(&tmpfd, file);
  414. if (status != APR_SUCCESS) {
  415. return status;
  416. }
  417. nbytes = sizeof(new_m->realsize);
  418. status = fspr_file_read(file, (void *)&(new_m->realsize),
  419. &nbytes);
  420. if (status != APR_SUCCESS) {
  421. return status;
  422. }
  423. status = fspr_os_file_get(&tmpfd, file);
  424. if (status != APR_SUCCESS) {
  425. fspr_file_close(file); /* ignore errors, we're failing */
  426. fspr_file_remove(new_m->filename, new_m->pool);
  427. return status;
  428. }
  429. new_m->reqsize = new_m->realsize - sizeof(fspr_size_t);
  430. new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
  431. MAP_SHARED, tmpfd, 0);
  432. /* FIXME: check for errors */
  433. status = fspr_file_close(file);
  434. if (status != APR_SUCCESS) {
  435. return status;
  436. }
  437. /* metadata isn't part of the usable segment */
  438. new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(fspr_size_t));
  439. fspr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
  440. fspr_pool_cleanup_null);
  441. *m = new_m;
  442. return APR_SUCCESS;
  443. #endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
  444. #if APR_USE_SHMEM_SHMGET
  445. fspr_shm_t *new_m;
  446. fspr_status_t status;
  447. fspr_file_t *file; /* file where metadata is stored */
  448. fspr_size_t nbytes;
  449. key_t shmkey;
  450. new_m = fspr_palloc(pool, sizeof(fspr_shm_t));
  451. status = fspr_file_open(&file, filename,
  452. APR_READ, APR_OS_DEFAULT, pool);
  453. if (status != APR_SUCCESS) {
  454. return status;
  455. }
  456. nbytes = sizeof(new_m->reqsize);
  457. status = fspr_file_read(file, (void *)&(new_m->reqsize),
  458. &nbytes);
  459. if (status != APR_SUCCESS) {
  460. return status;
  461. }
  462. status = fspr_file_close(file);
  463. if (status != APR_SUCCESS) {
  464. return status;
  465. }
  466. new_m->filename = fspr_pstrdup(pool, filename);
  467. new_m->pool = pool;
  468. shmkey = ftok(filename, 1);
  469. if (shmkey == (key_t)-1) {
  470. return errno;
  471. }
  472. if ((new_m->shmid = shmget(shmkey, 0, SHM_R | SHM_W)) == -1) {
  473. return errno;
  474. }
  475. if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
  476. return errno;
  477. }
  478. new_m->usable = new_m->base;
  479. new_m->realsize = new_m->reqsize;
  480. fspr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
  481. fspr_pool_cleanup_null);
  482. *m = new_m;
  483. return APR_SUCCESS;
  484. #endif /* APR_USE_SHMEM_SHMGET */
  485. }
  486. return APR_ENOTIMPL;
  487. }
  488. APR_DECLARE(fspr_status_t) fspr_shm_detach(fspr_shm_t *m)
  489. {
  490. fspr_status_t rv = shm_cleanup_attach(m);
  491. fspr_pool_cleanup_kill(m->pool, m, shm_cleanup_attach);
  492. return rv;
  493. }
  494. APR_DECLARE(void *) fspr_shm_baseaddr_get(const fspr_shm_t *m)
  495. {
  496. return m->usable;
  497. }
  498. APR_DECLARE(fspr_size_t) fspr_shm_size_get(const fspr_shm_t *m)
  499. {
  500. return m->reqsize;
  501. }
  502. APR_POOL_IMPLEMENT_ACCESSOR(shm)
  503. APR_DECLARE(fspr_status_t) fspr_os_shm_get(fspr_os_shm_t *osshm,
  504. fspr_shm_t *shm)
  505. {
  506. return APR_ENOTIMPL;
  507. }
  508. APR_DECLARE(fspr_status_t) fspr_os_shm_put(fspr_shm_t **m,
  509. fspr_os_shm_t *osshm,
  510. fspr_pool_t *pool)
  511. {
  512. return APR_ENOTIMPL;
  513. }