proc_mutex.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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.h"
  17. #include "fspr_strings.h"
  18. #include "fspr_arch_proc_mutex.h"
  19. #include "fspr_arch_file_io.h" /* for fspr_mkstemp() */
  20. APR_DECLARE(fspr_status_t) fspr_proc_mutex_destroy(fspr_proc_mutex_t *mutex)
  21. {
  22. return fspr_pool_cleanup_run(mutex->pool, mutex, fspr_proc_mutex_cleanup);
  23. }
  24. static fspr_status_t proc_mutex_no_tryacquire(fspr_proc_mutex_t *new_mutex)
  25. {
  26. return APR_ENOTIMPL;
  27. }
  28. #if APR_HAS_POSIXSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || \
  29. APR_HAS_PROC_PTHREAD_SERIALIZE || APR_HAS_SYSVSEM_SERIALIZE
  30. static fspr_status_t proc_mutex_no_child_init(fspr_proc_mutex_t **mutex,
  31. fspr_pool_t *cont,
  32. const char *fname)
  33. {
  34. return APR_SUCCESS;
  35. }
  36. #endif
  37. #if APR_HAS_POSIXSEM_SERIALIZE
  38. #ifndef SEM_FAILED
  39. #define SEM_FAILED (-1)
  40. #endif
  41. static fspr_status_t proc_mutex_posix_cleanup(void *mutex_)
  42. {
  43. fspr_proc_mutex_t *mutex = mutex_;
  44. if (sem_close(mutex->psem_interproc) < 0) {
  45. return errno;
  46. }
  47. return APR_SUCCESS;
  48. }
  49. static fspr_status_t proc_mutex_posix_create(fspr_proc_mutex_t *new_mutex,
  50. const char *fname)
  51. {
  52. sem_t *psem;
  53. char semname[31];
  54. fspr_time_t now;
  55. unsigned long sec;
  56. unsigned long usec;
  57. new_mutex->interproc = fspr_palloc(new_mutex->pool,
  58. sizeof(*new_mutex->interproc));
  59. /*
  60. * This bogusness is to follow what appears to be the
  61. * lowest common denominator in Posix semaphore naming:
  62. * - start with '/'
  63. * - be at most 14 chars
  64. * - be unique and not match anything on the filesystem
  65. *
  66. * Because of this, we ignore fname, and try our
  67. * own naming system. We tuck the name away, since it might
  68. * be useful for debugging. to make this as robust as possible,
  69. * we initially try something larger (and hopefully more unique)
  70. * and gracefully fail down to the LCD above.
  71. *
  72. * NOTE: Darwin (Mac OS X) seems to be the most restrictive
  73. * implementation. Versions previous to Darwin 6.2 had the 14
  74. * char limit, but later rev's allow up to 31 characters.
  75. *
  76. * FIXME: There is a small window of opportunity where
  77. * instead of getting a new semaphore descriptor, we get
  78. * a previously obtained one. This can happen if the requests
  79. * are made at the "same time" and in the small span of time between
  80. * the sem_open and the sem_unlink. Use of O_EXCL does not
  81. * help here however...
  82. *
  83. */
  84. now = fspr_time_now();
  85. sec = fspr_time_sec(now);
  86. usec = fspr_time_usec(now);
  87. fspr_snprintf(semname, sizeof(semname), "/ApR.%lxZ%lx", sec, usec);
  88. psem = sem_open(semname, O_CREAT, 0644, 1);
  89. if ((psem == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
  90. /* Oh well, good try */
  91. semname[13] = '\0';
  92. psem = sem_open(semname, O_CREAT, 0644, 1);
  93. }
  94. if (psem == (sem_t *)SEM_FAILED) {
  95. return errno;
  96. }
  97. /* Ahhh. The joys of Posix sems. Predelete it... */
  98. sem_unlink(semname);
  99. new_mutex->psem_interproc = psem;
  100. new_mutex->fname = fspr_pstrdup(new_mutex->pool, semname);
  101. fspr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
  102. fspr_proc_mutex_cleanup,
  103. fspr_pool_cleanup_null);
  104. return APR_SUCCESS;
  105. }
  106. static fspr_status_t proc_mutex_posix_acquire(fspr_proc_mutex_t *mutex)
  107. {
  108. if (sem_wait(mutex->psem_interproc) < 0) {
  109. return errno;
  110. }
  111. mutex->curr_locked = 1;
  112. return APR_SUCCESS;
  113. }
  114. static fspr_status_t proc_mutex_posix_release(fspr_proc_mutex_t *mutex)
  115. {
  116. mutex->curr_locked = 0;
  117. if (sem_post(mutex->psem_interproc) < 0) {
  118. /* any failure is probably fatal, so no big deal to leave
  119. * ->curr_locked at 0. */
  120. return errno;
  121. }
  122. return APR_SUCCESS;
  123. }
  124. static const fspr_proc_mutex_unix_lock_methods_t mutex_posixsem_methods =
  125. {
  126. #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(POSIXSEM_IS_GLOBAL)
  127. APR_PROCESS_LOCK_MECH_IS_GLOBAL,
  128. #else
  129. 0,
  130. #endif
  131. proc_mutex_posix_create,
  132. proc_mutex_posix_acquire,
  133. proc_mutex_no_tryacquire,
  134. proc_mutex_posix_release,
  135. proc_mutex_posix_cleanup,
  136. proc_mutex_no_child_init,
  137. "posixsem"
  138. };
  139. #endif /* Posix sem implementation */
  140. #if APR_HAS_SYSVSEM_SERIALIZE
  141. static struct sembuf proc_mutex_op_on;
  142. static struct sembuf proc_mutex_op_off;
  143. static void proc_mutex_sysv_setup(void)
  144. {
  145. proc_mutex_op_on.sem_num = 0;
  146. proc_mutex_op_on.sem_op = -1;
  147. proc_mutex_op_on.sem_flg = SEM_UNDO;
  148. proc_mutex_op_off.sem_num = 0;
  149. proc_mutex_op_off.sem_op = 1;
  150. proc_mutex_op_off.sem_flg = SEM_UNDO;
  151. }
  152. static fspr_status_t proc_mutex_sysv_cleanup(void *mutex_)
  153. {
  154. fspr_proc_mutex_t *mutex=mutex_;
  155. union semun ick;
  156. if (mutex->interproc->filedes != -1) {
  157. ick.val = 0;
  158. semctl(mutex->interproc->filedes, 0, IPC_RMID, ick);
  159. }
  160. return APR_SUCCESS;
  161. }
  162. static fspr_status_t proc_mutex_sysv_create(fspr_proc_mutex_t *new_mutex,
  163. const char *fname)
  164. {
  165. union semun ick;
  166. fspr_status_t rv;
  167. new_mutex->interproc = fspr_palloc(new_mutex->pool, sizeof(*new_mutex->interproc));
  168. new_mutex->interproc->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
  169. if (new_mutex->interproc->filedes < 0) {
  170. rv = errno;
  171. proc_mutex_sysv_cleanup(new_mutex);
  172. return rv;
  173. }
  174. ick.val = 1;
  175. if (semctl(new_mutex->interproc->filedes, 0, SETVAL, ick) < 0) {
  176. rv = errno;
  177. proc_mutex_sysv_cleanup(new_mutex);
  178. return rv;
  179. }
  180. new_mutex->curr_locked = 0;
  181. fspr_pool_cleanup_register(new_mutex->pool,
  182. (void *)new_mutex, fspr_proc_mutex_cleanup,
  183. fspr_pool_cleanup_null);
  184. return APR_SUCCESS;
  185. }
  186. static fspr_status_t proc_mutex_sysv_acquire(fspr_proc_mutex_t *mutex)
  187. {
  188. int rc;
  189. do {
  190. rc = semop(mutex->interproc->filedes, &proc_mutex_op_on, 1);
  191. } while (rc < 0 && errno == EINTR);
  192. if (rc < 0) {
  193. return errno;
  194. }
  195. mutex->curr_locked = 1;
  196. return APR_SUCCESS;
  197. }
  198. static fspr_status_t proc_mutex_sysv_release(fspr_proc_mutex_t *mutex)
  199. {
  200. int rc;
  201. mutex->curr_locked = 0;
  202. do {
  203. rc = semop(mutex->interproc->filedes, &proc_mutex_op_off, 1);
  204. } while (rc < 0 && errno == EINTR);
  205. if (rc < 0) {
  206. return errno;
  207. }
  208. return APR_SUCCESS;
  209. }
  210. static const fspr_proc_mutex_unix_lock_methods_t mutex_sysv_methods =
  211. {
  212. #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(SYSVSEM_IS_GLOBAL)
  213. APR_PROCESS_LOCK_MECH_IS_GLOBAL,
  214. #else
  215. 0,
  216. #endif
  217. proc_mutex_sysv_create,
  218. proc_mutex_sysv_acquire,
  219. proc_mutex_no_tryacquire,
  220. proc_mutex_sysv_release,
  221. proc_mutex_sysv_cleanup,
  222. proc_mutex_no_child_init,
  223. "sysvsem"
  224. };
  225. #endif /* SysV sem implementation */
  226. #if APR_HAS_PROC_PTHREAD_SERIALIZE
  227. static fspr_status_t proc_mutex_proc_pthread_cleanup(void *mutex_)
  228. {
  229. fspr_proc_mutex_t *mutex=mutex_;
  230. fspr_status_t rv;
  231. if (mutex->curr_locked == 1) {
  232. if ((rv = pthread_mutex_unlock(mutex->pthread_interproc))) {
  233. #ifdef PTHREAD_SETS_ERRNO
  234. rv = errno;
  235. #endif
  236. return rv;
  237. }
  238. }
  239. /* curr_locked is set to -1 until the mutex has been created */
  240. if (mutex->curr_locked != -1) {
  241. if ((rv = pthread_mutex_destroy(mutex->pthread_interproc))) {
  242. #ifdef PTHREAD_SETS_ERRNO
  243. rv = errno;
  244. #endif
  245. return rv;
  246. }
  247. }
  248. if (munmap((caddr_t)mutex->pthread_interproc, sizeof(pthread_mutex_t))) {
  249. return errno;
  250. }
  251. return APR_SUCCESS;
  252. }
  253. static fspr_status_t proc_mutex_proc_pthread_create(fspr_proc_mutex_t *new_mutex,
  254. const char *fname)
  255. {
  256. fspr_status_t rv;
  257. int fd;
  258. pthread_mutexattr_t mattr;
  259. fd = open("/dev/zero", O_RDWR);
  260. if (fd < 0) {
  261. return errno;
  262. }
  263. new_mutex->pthread_interproc = (pthread_mutex_t *)mmap(
  264. (caddr_t) 0,
  265. sizeof(pthread_mutex_t),
  266. PROT_READ | PROT_WRITE, MAP_SHARED,
  267. fd, 0);
  268. if (new_mutex->pthread_interproc == (pthread_mutex_t *) (caddr_t) -1) {
  269. close(fd);
  270. return errno;
  271. }
  272. close(fd);
  273. new_mutex->curr_locked = -1; /* until the mutex has been created */
  274. if ((rv = pthread_mutexattr_init(&mattr))) {
  275. #ifdef PTHREAD_SETS_ERRNO
  276. rv = errno;
  277. #endif
  278. proc_mutex_proc_pthread_cleanup(new_mutex);
  279. return rv;
  280. }
  281. if ((rv = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED))) {
  282. #ifdef PTHREAD_SETS_ERRNO
  283. rv = errno;
  284. #endif
  285. proc_mutex_proc_pthread_cleanup(new_mutex);
  286. pthread_mutexattr_destroy(&mattr);
  287. return rv;
  288. }
  289. #ifdef HAVE_PTHREAD_MUTEX_ROBUST
  290. if ((rv = pthread_mutexattr_setrobust_np(&mattr,
  291. PTHREAD_MUTEX_ROBUST_NP))) {
  292. #ifdef PTHREAD_SETS_ERRNO
  293. rv = errno;
  294. #endif
  295. proc_mutex_proc_pthread_cleanup(new_mutex);
  296. pthread_mutexattr_destroy(&mattr);
  297. return rv;
  298. }
  299. if ((rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT))) {
  300. #ifdef PTHREAD_SETS_ERRNO
  301. rv = errno;
  302. #endif
  303. proc_mutex_proc_pthread_cleanup(new_mutex);
  304. pthread_mutexattr_destroy(&mattr);
  305. return rv;
  306. }
  307. #endif /* HAVE_PTHREAD_MUTEX_ROBUST */
  308. if ((rv = pthread_mutex_init(new_mutex->pthread_interproc, &mattr))) {
  309. #ifdef PTHREAD_SETS_ERRNO
  310. rv = errno;
  311. #endif
  312. proc_mutex_proc_pthread_cleanup(new_mutex);
  313. pthread_mutexattr_destroy(&mattr);
  314. return rv;
  315. }
  316. new_mutex->curr_locked = 0; /* mutex created now */
  317. if ((rv = pthread_mutexattr_destroy(&mattr))) {
  318. #ifdef PTHREAD_SETS_ERRNO
  319. rv = errno;
  320. #endif
  321. proc_mutex_proc_pthread_cleanup(new_mutex);
  322. return rv;
  323. }
  324. fspr_pool_cleanup_register(new_mutex->pool,
  325. (void *)new_mutex,
  326. fspr_proc_mutex_cleanup,
  327. fspr_pool_cleanup_null);
  328. return APR_SUCCESS;
  329. }
  330. static fspr_status_t proc_mutex_proc_pthread_acquire(fspr_proc_mutex_t *mutex)
  331. {
  332. fspr_status_t rv;
  333. if ((rv = pthread_mutex_lock(mutex->pthread_interproc))) {
  334. #ifdef PTHREAD_SETS_ERRNO
  335. rv = errno;
  336. #endif
  337. #ifdef HAVE_PTHREAD_MUTEXATTR_SETROBUST_NP
  338. /* Okay, our owner died. Let's try to make it consistent again. */
  339. if (rv == EOWNERDEAD) {
  340. pthread_mutex_consistent_np(mutex->pthread_interproc);
  341. }
  342. else
  343. return rv;
  344. #else
  345. return rv;
  346. #endif
  347. }
  348. mutex->curr_locked = 1;
  349. return APR_SUCCESS;
  350. }
  351. /* TODO: Add proc_mutex_proc_pthread_tryacquire(fspr_proc_mutex_t *mutex) */
  352. static fspr_status_t proc_mutex_proc_pthread_release(fspr_proc_mutex_t *mutex)
  353. {
  354. fspr_status_t rv;
  355. mutex->curr_locked = 0;
  356. if ((rv = pthread_mutex_unlock(mutex->pthread_interproc))) {
  357. #ifdef PTHREAD_SETS_ERRNO
  358. rv = errno;
  359. #endif
  360. return rv;
  361. }
  362. return APR_SUCCESS;
  363. }
  364. static const fspr_proc_mutex_unix_lock_methods_t mutex_proc_pthread_methods =
  365. {
  366. APR_PROCESS_LOCK_MECH_IS_GLOBAL,
  367. proc_mutex_proc_pthread_create,
  368. proc_mutex_proc_pthread_acquire,
  369. proc_mutex_no_tryacquire,
  370. proc_mutex_proc_pthread_release,
  371. proc_mutex_proc_pthread_cleanup,
  372. proc_mutex_no_child_init,
  373. "pthread"
  374. };
  375. #endif
  376. #if APR_HAS_FCNTL_SERIALIZE
  377. static struct flock proc_mutex_lock_it;
  378. static struct flock proc_mutex_unlock_it;
  379. static fspr_status_t proc_mutex_fcntl_release(fspr_proc_mutex_t *);
  380. static void proc_mutex_fcntl_setup(void)
  381. {
  382. proc_mutex_lock_it.l_whence = SEEK_SET; /* from current point */
  383. proc_mutex_lock_it.l_start = 0; /* -"- */
  384. proc_mutex_lock_it.l_len = 0; /* until end of file */
  385. proc_mutex_lock_it.l_type = F_WRLCK; /* set exclusive/write lock */
  386. proc_mutex_lock_it.l_pid = 0; /* pid not actually interesting */
  387. proc_mutex_unlock_it.l_whence = SEEK_SET; /* from current point */
  388. proc_mutex_unlock_it.l_start = 0; /* -"- */
  389. proc_mutex_unlock_it.l_len = 0; /* until end of file */
  390. proc_mutex_unlock_it.l_type = F_UNLCK; /* set exclusive/write lock */
  391. proc_mutex_unlock_it.l_pid = 0; /* pid not actually interesting */
  392. }
  393. static fspr_status_t proc_mutex_fcntl_cleanup(void *mutex_)
  394. {
  395. fspr_status_t status;
  396. fspr_proc_mutex_t *mutex=mutex_;
  397. if (mutex->curr_locked == 1) {
  398. status = proc_mutex_fcntl_release(mutex);
  399. if (status != APR_SUCCESS)
  400. return status;
  401. }
  402. return fspr_file_close(mutex->interproc);
  403. }
  404. static fspr_status_t proc_mutex_fcntl_create(fspr_proc_mutex_t *new_mutex,
  405. const char *fname)
  406. {
  407. int rv;
  408. if (fname) {
  409. new_mutex->fname = fspr_pstrdup(new_mutex->pool, fname);
  410. rv = fspr_file_open(&new_mutex->interproc, new_mutex->fname,
  411. APR_CREATE | APR_WRITE | APR_EXCL,
  412. APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD,
  413. new_mutex->pool);
  414. }
  415. else {
  416. new_mutex->fname = fspr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX");
  417. rv = fspr_file_mktemp(&new_mutex->interproc, new_mutex->fname,
  418. APR_CREATE | APR_WRITE | APR_EXCL,
  419. new_mutex->pool);
  420. }
  421. if (rv != APR_SUCCESS) {
  422. return rv;
  423. }
  424. new_mutex->curr_locked = 0;
  425. unlink(new_mutex->fname);
  426. fspr_pool_cleanup_register(new_mutex->pool,
  427. (void*)new_mutex,
  428. fspr_proc_mutex_cleanup,
  429. fspr_pool_cleanup_null);
  430. return APR_SUCCESS;
  431. }
  432. static fspr_status_t proc_mutex_fcntl_acquire(fspr_proc_mutex_t *mutex)
  433. {
  434. int rc;
  435. do {
  436. rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_lock_it);
  437. } while (rc < 0 && errno == EINTR);
  438. if (rc < 0) {
  439. return errno;
  440. }
  441. mutex->curr_locked=1;
  442. return APR_SUCCESS;
  443. }
  444. static fspr_status_t proc_mutex_fcntl_release(fspr_proc_mutex_t *mutex)
  445. {
  446. int rc;
  447. mutex->curr_locked=0;
  448. do {
  449. rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_unlock_it);
  450. } while (rc < 0 && errno == EINTR);
  451. if (rc < 0) {
  452. return errno;
  453. }
  454. return APR_SUCCESS;
  455. }
  456. static const fspr_proc_mutex_unix_lock_methods_t mutex_fcntl_methods =
  457. {
  458. #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(FCNTL_IS_GLOBAL)
  459. APR_PROCESS_LOCK_MECH_IS_GLOBAL,
  460. #else
  461. 0,
  462. #endif
  463. proc_mutex_fcntl_create,
  464. proc_mutex_fcntl_acquire,
  465. proc_mutex_no_tryacquire,
  466. proc_mutex_fcntl_release,
  467. proc_mutex_fcntl_cleanup,
  468. proc_mutex_no_child_init,
  469. "fcntl"
  470. };
  471. #endif /* fcntl implementation */
  472. #if APR_HAS_FLOCK_SERIALIZE
  473. static fspr_status_t proc_mutex_flock_release(fspr_proc_mutex_t *);
  474. static fspr_status_t proc_mutex_flock_cleanup(void *mutex_)
  475. {
  476. fspr_status_t status;
  477. fspr_proc_mutex_t *mutex=mutex_;
  478. if (mutex->curr_locked == 1) {
  479. status = proc_mutex_flock_release(mutex);
  480. if (status != APR_SUCCESS)
  481. return status;
  482. }
  483. if (mutex->interproc) { /* if it was opened properly */
  484. fspr_file_close(mutex->interproc);
  485. }
  486. unlink(mutex->fname);
  487. return APR_SUCCESS;
  488. }
  489. static fspr_status_t proc_mutex_flock_create(fspr_proc_mutex_t *new_mutex,
  490. const char *fname)
  491. {
  492. int rv;
  493. if (fname) {
  494. new_mutex->fname = fspr_pstrdup(new_mutex->pool, fname);
  495. rv = fspr_file_open(&new_mutex->interproc, new_mutex->fname,
  496. APR_CREATE | APR_WRITE | APR_EXCL,
  497. APR_UREAD | APR_UWRITE,
  498. new_mutex->pool);
  499. }
  500. else {
  501. new_mutex->fname = fspr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX");
  502. rv = fspr_file_mktemp(&new_mutex->interproc, new_mutex->fname,
  503. APR_CREATE | APR_WRITE | APR_EXCL,
  504. new_mutex->pool);
  505. }
  506. if (rv != APR_SUCCESS) {
  507. proc_mutex_flock_cleanup(new_mutex);
  508. return errno;
  509. }
  510. new_mutex->curr_locked = 0;
  511. fspr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
  512. fspr_proc_mutex_cleanup,
  513. fspr_pool_cleanup_null);
  514. return APR_SUCCESS;
  515. }
  516. static fspr_status_t proc_mutex_flock_acquire(fspr_proc_mutex_t *mutex)
  517. {
  518. int rc;
  519. do {
  520. rc = flock(mutex->interproc->filedes, LOCK_EX);
  521. } while (rc < 0 && errno == EINTR);
  522. if (rc < 0) {
  523. return errno;
  524. }
  525. mutex->curr_locked = 1;
  526. return APR_SUCCESS;
  527. }
  528. static fspr_status_t proc_mutex_flock_release(fspr_proc_mutex_t *mutex)
  529. {
  530. int rc;
  531. mutex->curr_locked = 0;
  532. do {
  533. rc = flock(mutex->interproc->filedes, LOCK_UN);
  534. } while (rc < 0 && errno == EINTR);
  535. if (rc < 0) {
  536. return errno;
  537. }
  538. return APR_SUCCESS;
  539. }
  540. static fspr_status_t proc_mutex_flock_child_init(fspr_proc_mutex_t **mutex,
  541. fspr_pool_t *pool,
  542. const char *fname)
  543. {
  544. fspr_proc_mutex_t *new_mutex;
  545. int rv;
  546. new_mutex = (fspr_proc_mutex_t *)fspr_palloc(pool, sizeof(fspr_proc_mutex_t));
  547. memcpy(new_mutex, *mutex, sizeof *new_mutex);
  548. new_mutex->pool = pool;
  549. if (!fname) {
  550. fname = (*mutex)->fname;
  551. }
  552. new_mutex->fname = fspr_pstrdup(pool, fname);
  553. rv = fspr_file_open(&new_mutex->interproc, new_mutex->fname,
  554. APR_WRITE, 0, new_mutex->pool);
  555. if (rv != APR_SUCCESS) {
  556. return rv;
  557. }
  558. *mutex = new_mutex;
  559. return APR_SUCCESS;
  560. }
  561. static const fspr_proc_mutex_unix_lock_methods_t mutex_flock_methods =
  562. {
  563. #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(FLOCK_IS_GLOBAL)
  564. APR_PROCESS_LOCK_MECH_IS_GLOBAL,
  565. #else
  566. 0,
  567. #endif
  568. proc_mutex_flock_create,
  569. proc_mutex_flock_acquire,
  570. proc_mutex_no_tryacquire,
  571. proc_mutex_flock_release,
  572. proc_mutex_flock_cleanup,
  573. proc_mutex_flock_child_init,
  574. "flock"
  575. };
  576. #endif /* flock implementation */
  577. void fspr_proc_mutex_unix_setup_lock(void)
  578. {
  579. /* setup only needed for sysvsem and fnctl */
  580. #if APR_HAS_SYSVSEM_SERIALIZE
  581. proc_mutex_sysv_setup();
  582. #endif
  583. #if APR_HAS_FCNTL_SERIALIZE
  584. proc_mutex_fcntl_setup();
  585. #endif
  586. }
  587. static fspr_status_t proc_mutex_choose_method(fspr_proc_mutex_t *new_mutex, fspr_lockmech_e mech)
  588. {
  589. switch (mech) {
  590. case APR_LOCK_FCNTL:
  591. #if APR_HAS_FCNTL_SERIALIZE
  592. new_mutex->inter_meth = &mutex_fcntl_methods;
  593. #else
  594. return APR_ENOTIMPL;
  595. #endif
  596. break;
  597. case APR_LOCK_FLOCK:
  598. #if APR_HAS_FLOCK_SERIALIZE
  599. new_mutex->inter_meth = &mutex_flock_methods;
  600. #else
  601. return APR_ENOTIMPL;
  602. #endif
  603. break;
  604. case APR_LOCK_SYSVSEM:
  605. #if APR_HAS_SYSVSEM_SERIALIZE
  606. new_mutex->inter_meth = &mutex_sysv_methods;
  607. #else
  608. return APR_ENOTIMPL;
  609. #endif
  610. break;
  611. case APR_LOCK_POSIXSEM:
  612. #if APR_HAS_POSIXSEM_SERIALIZE
  613. new_mutex->inter_meth = &mutex_posixsem_methods;
  614. #else
  615. return APR_ENOTIMPL;
  616. #endif
  617. break;
  618. case APR_LOCK_PROC_PTHREAD:
  619. #if APR_HAS_PROC_PTHREAD_SERIALIZE
  620. new_mutex->inter_meth = &mutex_proc_pthread_methods;
  621. #else
  622. return APR_ENOTIMPL;
  623. #endif
  624. break;
  625. case APR_LOCK_DEFAULT:
  626. #if APR_USE_FLOCK_SERIALIZE
  627. new_mutex->inter_meth = &mutex_flock_methods;
  628. #elif APR_USE_SYSVSEM_SERIALIZE
  629. new_mutex->inter_meth = &mutex_sysv_methods;
  630. #elif APR_USE_FCNTL_SERIALIZE
  631. new_mutex->inter_meth = &mutex_fcntl_methods;
  632. #elif APR_USE_PROC_PTHREAD_SERIALIZE
  633. new_mutex->inter_meth = &mutex_proc_pthread_methods;
  634. #elif APR_USE_POSIXSEM_SERIALIZE
  635. new_mutex->inter_meth = &mutex_posixsem_methods;
  636. #else
  637. return APR_ENOTIMPL;
  638. #endif
  639. break;
  640. default:
  641. return APR_ENOTIMPL;
  642. }
  643. return APR_SUCCESS;
  644. }
  645. APR_DECLARE(const char *) fspr_proc_mutex_defname(void)
  646. {
  647. fspr_proc_mutex_t mutex;
  648. if (proc_mutex_choose_method(&mutex, APR_LOCK_DEFAULT) != APR_SUCCESS) {
  649. return "unknown";
  650. }
  651. mutex.meth = mutex.inter_meth;
  652. return fspr_proc_mutex_name(&mutex);
  653. }
  654. static fspr_status_t proc_mutex_create(fspr_proc_mutex_t *new_mutex, fspr_lockmech_e mech, const char *fname)
  655. {
  656. fspr_status_t rv;
  657. if ((rv = proc_mutex_choose_method(new_mutex, mech)) != APR_SUCCESS) {
  658. return rv;
  659. }
  660. new_mutex->meth = new_mutex->inter_meth;
  661. if ((rv = new_mutex->meth->create(new_mutex, fname)) != APR_SUCCESS) {
  662. return rv;
  663. }
  664. return APR_SUCCESS;
  665. }
  666. APR_DECLARE(fspr_status_t) fspr_proc_mutex_create(fspr_proc_mutex_t **mutex,
  667. const char *fname,
  668. fspr_lockmech_e mech,
  669. fspr_pool_t *pool)
  670. {
  671. fspr_proc_mutex_t *new_mutex;
  672. fspr_status_t rv;
  673. new_mutex = fspr_pcalloc(pool, sizeof(fspr_proc_mutex_t));
  674. new_mutex->pool = pool;
  675. if ((rv = proc_mutex_create(new_mutex, mech, fname)) != APR_SUCCESS)
  676. return rv;
  677. *mutex = new_mutex;
  678. return APR_SUCCESS;
  679. }
  680. APR_DECLARE(fspr_status_t) fspr_proc_mutex_child_init(fspr_proc_mutex_t **mutex,
  681. const char *fname,
  682. fspr_pool_t *pool)
  683. {
  684. return (*mutex)->meth->child_init(mutex, pool, fname);
  685. }
  686. APR_DECLARE(fspr_status_t) fspr_proc_mutex_lock(fspr_proc_mutex_t *mutex)
  687. {
  688. return mutex->meth->acquire(mutex);
  689. }
  690. APR_DECLARE(fspr_status_t) fspr_proc_mutex_trylock(fspr_proc_mutex_t *mutex)
  691. {
  692. return mutex->meth->tryacquire(mutex);
  693. }
  694. APR_DECLARE(fspr_status_t) fspr_proc_mutex_unlock(fspr_proc_mutex_t *mutex)
  695. {
  696. return mutex->meth->release(mutex);
  697. }
  698. APR_DECLARE(fspr_status_t) fspr_proc_mutex_cleanup(void *mutex)
  699. {
  700. return ((fspr_proc_mutex_t *)mutex)->meth->cleanup(mutex);
  701. }
  702. APR_DECLARE(const char *) fspr_proc_mutex_name(fspr_proc_mutex_t *mutex)
  703. {
  704. return mutex->meth->name;
  705. }
  706. APR_DECLARE(const char *) fspr_proc_mutex_lockfile(fspr_proc_mutex_t *mutex)
  707. {
  708. /* POSIX sems use the fname field but don't use a file,
  709. * so be careful. */
  710. #if APR_HAS_FLOCK_SERIALIZE
  711. if (mutex->meth == &mutex_flock_methods) {
  712. return mutex->fname;
  713. }
  714. #endif
  715. #if APR_HAS_FCNTL_SERIALIZE
  716. if (mutex->meth == &mutex_fcntl_methods) {
  717. return mutex->fname;
  718. }
  719. #endif
  720. return NULL;
  721. }
  722. APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
  723. /* Implement OS-specific accessors defined in fspr_portable.h */
  724. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_get(fspr_os_proc_mutex_t *ospmutex,
  725. fspr_proc_mutex_t *pmutex)
  726. {
  727. #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE || APR_HAS_POSIXSEM_SERIALIZE
  728. ospmutex->crossproc = pmutex->interproc->filedes;
  729. #endif
  730. #if APR_HAS_PROC_PTHREAD_SERIALIZE
  731. ospmutex->pthread_interproc = pmutex->pthread_interproc;
  732. #endif
  733. return APR_SUCCESS;
  734. }
  735. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_put(fspr_proc_mutex_t **pmutex,
  736. fspr_os_proc_mutex_t *ospmutex,
  737. fspr_pool_t *pool)
  738. {
  739. if (pool == NULL) {
  740. return APR_ENOPOOL;
  741. }
  742. if ((*pmutex) == NULL) {
  743. (*pmutex) = (fspr_proc_mutex_t *)fspr_pcalloc(pool,
  744. sizeof(fspr_proc_mutex_t));
  745. (*pmutex)->pool = pool;
  746. }
  747. #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE || APR_HAS_POSIXSEM_SERIALIZE
  748. fspr_os_file_put(&(*pmutex)->interproc, &ospmutex->crossproc, 0, pool);
  749. #endif
  750. #if APR_HAS_PROC_PTHREAD_SERIALIZE
  751. (*pmutex)->pthread_interproc = ospmutex->pthread_interproc;
  752. #endif
  753. return APR_SUCCESS;
  754. }