rw_lock.C 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $XConsortium: rw_lock.cc /main/3 1996/06/11 17:38:50 cde-hal $
  25. *
  26. * Copyright (c) 1993 HAL Computer Systems International, Ltd.
  27. * All rights reserved. Unpublished -- rights reserved under
  28. * the Copyright Laws of the United States. USE OF A COPYRIGHT
  29. * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
  30. * OR DISCLOSURE.
  31. *
  32. * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
  33. * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
  34. * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
  35. * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
  36. * INTERNATIONAL, LTD.
  37. *
  38. * RESTRICTED RIGHTS LEGEND
  39. * Use, duplication, or disclosure by the Government is subject
  40. * to the restrictions as set forth in subparagraph (c)(l)(ii)
  41. * of the Rights in Technical Data and Computer Software clause
  42. * at DFARS 252.227-7013.
  43. *
  44. * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
  45. * 1315 Dell Avenue
  46. * Campbell, CA 95008
  47. *
  48. */
  49. #include "atomic_lock.h"
  50. #include "rw_lock.h"
  51. Boolean read_lock(char* lock_file_path,
  52. char* writing_lock_file_path,
  53. char* ai_path,
  54. char* reader_info, int& offset,
  55. char*& ai_info
  56. )
  57. {
  58. atomic_lock l(lock_file_path);
  59. if ( l.lock() == false ) {
  60. MESSAGE(cerr, "read_lock(): can't do atomic locking");
  61. return false;
  62. }
  63. Boolean ok;
  64. if ( false == exist_file( writing_lock_file_path ) ) {
  65. fstream x(ai_path, ios::app);
  66. if ( !x ) {
  67. MESSAGE(cerr, "read_lock(): can't open lock file");
  68. throw(streamException(x.rdstate()));
  69. }
  70. #ifdef C_API
  71. offset = bytes(x.rdbuf() -> fd());
  72. #else
  73. offset = bytes(ai_info);
  74. #endif
  75. x << "A-" << reader_info << "\n";
  76. x.close();
  77. ok = true;
  78. } else {
  79. fstream x(ai_path, ios::in);
  80. if ( !x ) {
  81. MESSAGE(cerr, "read_lock(): can't open lock file");
  82. throw(streamException(x.rdstate()));
  83. }
  84. #ifdef C_API
  85. int sz = bytes(x.rdbuf() -> fd());
  86. #else
  87. int sz = bytes(ai_info);
  88. #endif
  89. ai_info = new char[sz+1];
  90. ai_info[0] = 0;
  91. x.getline(ai_info, sz);
  92. x.close();
  93. ok = false;
  94. }
  95. if ( l.unlock() == false ) {
  96. MESSAGE(cerr, "read_lock(): can't do atomic unlocking");
  97. return false;
  98. }
  99. return ok;
  100. }
  101. Boolean read_unlock(char* lock_file_path, char* ai_path, int offset)
  102. {
  103. atomic_lock l(lock_file_path);
  104. if ( l.lock() == false ) {
  105. MESSAGE(cerr, "read_lock(): can't do atomic locking");
  106. return false;
  107. }
  108. Boolean ok ;
  109. fstream x(ai_path, ios::in|ios::out);
  110. if ( !x ) {
  111. MESSAGE(cerr, "read_unlock(): can't open lock file");
  112. throw(streamException(x.rdstate()));
  113. }
  114. x.seekg( offset, ios::beg );
  115. x.put('I');
  116. ///////////////////////////////////////////////
  117. // truncate the info_file if no active readers
  118. // and the file size is over 1k
  119. ///////////////////////////////////////////////
  120. #ifdef C_API
  121. if ( bytes(x.rdbuf() -> fd()) > 1024 ) {
  122. #else
  123. if ( bytes(ai_path) > 1024 ) {
  124. #endif
  125. ok = false;
  126. char buf[BUFSIZ];
  127. /////////////////////////////////////////
  128. // scan the info file for active readers
  129. /////////////////////////////////////////
  130. while ( x.getline(buf, BUFSIZ) ) {
  131. if ( buf[0] == 'A' ) {
  132. ok = true;
  133. break;
  134. }
  135. }
  136. if ( ok == false )
  137. if ( truncate(ai_path, 0) != 0 ) {
  138. MESSAGE(cerr, "read_unlock(): can't truncate");
  139. throw(systemException(errno));
  140. }
  141. }
  142. x.close();
  143. if ( l.unlock() == false ) {
  144. MESSAGE(cerr, "read_lock(): can't do atomic locking");
  145. return false;
  146. }
  147. return true;
  148. }
  149. Boolean write_lock(char* lock_file_path,
  150. char* writing_lock_path,
  151. char* ai_path, char* writer_info,
  152. char*& ai_info
  153. )
  154. {
  155. unsigned int len, slen;
  156. atomic_lock l(lock_file_path);
  157. if ( l.lock() == false ) {
  158. MESSAGE(cerr, "write_lock(): can't do atomic locking");
  159. return false;
  160. }
  161. Boolean ok = true;
  162. fstream x(ai_path, ios::in|ios::out);
  163. if (!x) {
  164. MESSAGE(cerr, "write_lock(): can't open info file");
  165. throw(streamException(x.rdstate()));
  166. }
  167. char buf[BUFSIZ];
  168. #ifdef C_API
  169. int sz = bytes(x.rdbuf() -> fd());
  170. #else
  171. int sz = bytes(ai_path);
  172. #endif
  173. ai_info = new char[sz+1];
  174. ai_info[0] = 0;
  175. /////////////////////////////////////////
  176. // scan the info file for active readers
  177. /////////////////////////////////////////
  178. while ( x.getline(buf, BUFSIZ) ) {
  179. if ( buf[0] == 'A' ) {
  180. ok = false;
  181. slen = strlen(ai_info);
  182. len = MIN(strlen(buf+1), BUFSIZ - 1 - slen);
  183. *((char *) memcpy(ai_info + slen, buf+1, len) + len) = '\0';
  184. slen = strlen(ai_info);
  185. len = MIN(1, BUFSIZ - 1 - slen);
  186. *((char *) memcpy(ai_info + slen, "\n", len) + len) = '\0';
  187. }
  188. }
  189. x.close();
  190. if ( exist_file( writing_lock_path ) == false ) {
  191. if ( ok == true ) {
  192. delete [] ai_info;
  193. /////////////////////////////////////////
  194. // create the access info file
  195. /////////////////////////////////////////
  196. if(truncate(ai_path, 0) != 0 ) {
  197. throw(systemException(errno));
  198. }
  199. fstream x(ai_path, ios::out);
  200. x << "A-" << writer_info << "\n";
  201. /////////////////////////////////////////
  202. // create the writing lock file
  203. /////////////////////////////////////////
  204. if ( creat(writing_lock_path, 0755) == -1 )
  205. ok = false;
  206. }
  207. } else
  208. ok = false;
  209. if ( l.unlock() == false ) {
  210. MESSAGE(cerr, "write_lock(): can't do atomic unlocking");
  211. return false;
  212. }
  213. return ok;
  214. }
  215. Boolean write_unlock(char* lock_file_path, char* writing_lock_path,
  216. char* ai_path
  217. )
  218. {
  219. atomic_lock l(lock_file_path);
  220. if ( l.lock() == false ) {
  221. MESSAGE(cerr, "write_unlock(): can't do atomic locking");
  222. return false;
  223. }
  224. Boolean ok;
  225. if ( del_file(writing_lock_path) == 0 &&
  226. del_file(ai_path) == 0
  227. )
  228. ok = true;
  229. else
  230. ok = false;
  231. if ( l.unlock() == false ) {
  232. MESSAGE(cerr, "write_unlock(): can't do atomic unlocking");
  233. return false;
  234. }
  235. return ok;
  236. }