fstab.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * <fstab.h> wrapper functions.
  3. *
  4. * Authors:
  5. * Jonathan Pryor ([email protected])
  6. *
  7. * Copyright (C) 2004-2005 Jonathan Pryor
  8. */
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stddef.h>
  14. #include "map.h"
  15. #include "mph.h"
  16. #if defined (HAVE_CHECKLIST_H)
  17. #include <checklist.h>
  18. #elif defined (HAVE_FSTAB_H)
  19. #include <fstab.h>
  20. #endif /* def HAVE_FSTAB_H */
  21. #ifdef HAVE_SYS_VFSTAB_H
  22. #include <sys/vfstab.h>
  23. #endif /* def HAVE_SYS_VFSTAB_H */
  24. G_BEGIN_DECLS
  25. #ifdef HAVE_CHECKLIST_H
  26. typedef struct checklist mph_fstab;
  27. static const mph_string_offset_t
  28. fstab_offsets[] = {
  29. MPH_STRING_OFFSET (struct checklist, fs_spec, MPH_STRING_OFFSET_PTR),
  30. MPH_STRING_OFFSET (struct checklist, fs_dir, MPH_STRING_OFFSET_PTR),
  31. MPH_STRING_OFFSET (struct checklist, fs_type, MPH_STRING_OFFSET_PTR)
  32. };
  33. static const mph_string_offset_t
  34. mph_fstab_offsets[] = {
  35. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR),
  36. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR),
  37. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_type, MPH_STRING_OFFSET_PTR)
  38. };
  39. #elif defined (HAVE_FSTAB_H)
  40. typedef struct fstab mph_fstab;
  41. static const mph_string_offset_t
  42. fstab_offsets[] = {
  43. MPH_STRING_OFFSET (struct fstab, fs_spec, MPH_STRING_OFFSET_PTR),
  44. MPH_STRING_OFFSET (struct fstab, fs_file, MPH_STRING_OFFSET_PTR),
  45. MPH_STRING_OFFSET (struct fstab, fs_vfstype, MPH_STRING_OFFSET_PTR),
  46. MPH_STRING_OFFSET (struct fstab, fs_mntops, MPH_STRING_OFFSET_PTR),
  47. MPH_STRING_OFFSET (struct fstab, fs_type, MPH_STRING_OFFSET_PTR)
  48. };
  49. static const mph_string_offset_t
  50. mph_fstab_offsets[] = {
  51. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR),
  52. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR),
  53. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_vfstype, MPH_STRING_OFFSET_PTR),
  54. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_mntops, MPH_STRING_OFFSET_PTR),
  55. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_type, MPH_STRING_OFFSET_PTR)
  56. };
  57. #endif /* def HAVE_FSTAB_H */
  58. #if defined (HAVE_CHECKLIST_H) || defined (HAVE_FSTAB_H)
  59. /*
  60. * Copy the native `fstab' structure to it's managed representation.
  61. *
  62. * To minimize separate mallocs, all the strings are allocated within the same
  63. * memory block (stored in _fs_buf_).
  64. */
  65. static int
  66. copy_fstab (struct Mono_Posix_Syscall__Fstab *to, mph_fstab *from)
  67. {
  68. char *buf;
  69. memset (to, 0, sizeof(*to));
  70. buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
  71. from, fstab_offsets, sizeof(fstab_offsets)/sizeof(fstab_offsets[0]));
  72. to->fs_freq = from->fs_freq;
  73. to->fs_passno = from->fs_passno;
  74. to->_fs_buf_ = buf;
  75. if (buf == NULL) {
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. #endif /* def HAVE_CHECKLIST_H || def HAVE_FSTAB_H */
  81. #ifdef HAVE_SYS_VFSTAB_H
  82. /*
  83. * Solaris doesn't provide <fstab.h> but has equivalent functionality in
  84. * <sys/fstab.h> via getvfsent(3C) and company.
  85. */
  86. typedef struct vfstab mph_fstab;
  87. static const mph_string_offset_t
  88. vfstab_offsets[] = {
  89. MPH_STRING_OFFSET (struct vfstab, vfs_special, MPH_STRING_OFFSET_PTR),
  90. MPH_STRING_OFFSET (struct vfstab, vfs_mountp, MPH_STRING_OFFSET_PTR),
  91. MPH_STRING_OFFSET (struct vfstab, vfs_fstype, MPH_STRING_OFFSET_PTR),
  92. MPH_STRING_OFFSET (struct vfstab, vfs_mntopts, MPH_STRING_OFFSET_PTR)
  93. };
  94. static const mph_string_offset_t
  95. mph_fstab_offsets[] = {
  96. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR),
  97. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR),
  98. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_vfstype, MPH_STRING_OFFSET_PTR),
  99. MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_mntops, MPH_STRING_OFFSET_PTR)
  100. };
  101. /*
  102. * Copy the native `vfstab' structure to it's managed representation.
  103. *
  104. * To minimize separate mallocs, all the strings are allocated within the same
  105. * memory block (stored in _fs_buf_).
  106. */
  107. static int
  108. copy_fstab (struct Mono_Posix_Syscall__Fstab *to, struct vfstab *from)
  109. {
  110. char *buf;
  111. memset (to, 0, sizeof(*to));
  112. buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
  113. from, vfstab_offsets, sizeof(vfstab_offsets)/sizeof(vfstab_offsets[0]));
  114. to->fs_type = NULL;
  115. to->fs_freq = -1;
  116. to->fs_passno = -1;
  117. to->_fs_buf_ = buf;
  118. if (buf == NULL) {
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. /*
  124. * Implement Linux/BSD getfsent(3) in terms of Solaris getvfsent(3C)...
  125. */
  126. static FILE*
  127. etc_fstab;
  128. static int
  129. setfsent (void)
  130. {
  131. /* protect from bad users calling setfsent(), setfsent(), ... endfsent() */
  132. if (etc_fstab != NULL)
  133. fclose (etc_fstab);
  134. etc_fstab = fopen ("/etc/vfstab", "r");
  135. if (etc_fstab != NULL)
  136. return 1;
  137. return 0;
  138. }
  139. static void
  140. endfsent (void)
  141. {
  142. fclose (etc_fstab);
  143. etc_fstab = NULL;
  144. }
  145. static struct vfstab
  146. cur_vfstab_entry;
  147. static struct vfstab*
  148. getfsent (void)
  149. {
  150. int r;
  151. r = getvfsent (etc_fstab, &cur_vfstab_entry);
  152. if (r == 0)
  153. return &cur_vfstab_entry;
  154. return NULL;
  155. }
  156. static struct vfstab*
  157. getfsfile (const char *mount_point)
  158. {
  159. int r;
  160. int close = 0;
  161. if (etc_fstab == 0) {
  162. close = 1;
  163. if (setfsent () != 1)
  164. return NULL;
  165. }
  166. rewind (etc_fstab);
  167. r = getvfsfile (etc_fstab, &cur_vfstab_entry, (char*) mount_point);
  168. if (close)
  169. endfsent ();
  170. if (r == 0)
  171. return &cur_vfstab_entry;
  172. return NULL;
  173. }
  174. static struct vfstab*
  175. getfsspec (const char *special_file)
  176. {
  177. int r;
  178. int close = 0;
  179. if (etc_fstab == 0) {
  180. close = 1;
  181. if (setfsent () != 1)
  182. return NULL;
  183. }
  184. rewind (etc_fstab);
  185. r = getvfsspec (etc_fstab, &cur_vfstab_entry, (char*) special_file);
  186. if (close)
  187. endfsent ();
  188. if (r == 0)
  189. return &cur_vfstab_entry;
  190. return NULL;
  191. }
  192. #endif /* def HAVE_SYS_VFSTAB_H */
  193. #if defined (HAVE_FSTAB_H) || defined (HAVE_CHECKPOINT_H) || defined (HAVE_SYS_VFSTAB_H)
  194. int
  195. Mono_Posix_Syscall_endfsent (void)
  196. {
  197. endfsent ();
  198. return 0;
  199. }
  200. gint32
  201. Mono_Posix_Syscall_getfsent (struct Mono_Posix_Syscall__Fstab *fsbuf)
  202. {
  203. mph_fstab *fs;
  204. if (fsbuf == NULL) {
  205. errno = EFAULT;
  206. return -1;
  207. }
  208. fs = getfsent ();
  209. if (fs == NULL)
  210. return -1;
  211. if (copy_fstab (fsbuf, fs) == -1) {
  212. errno = ENOMEM;
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. gint32
  218. Mono_Posix_Syscall_getfsfile (const char *mount_point,
  219. struct Mono_Posix_Syscall__Fstab *fsbuf)
  220. {
  221. mph_fstab *fs;
  222. if (fsbuf == NULL) {
  223. errno = EFAULT;
  224. return -1;
  225. }
  226. fs = getfsfile (mount_point);
  227. if (fs == NULL)
  228. return -1;
  229. if (copy_fstab (fsbuf, fs) == -1) {
  230. errno = ENOMEM;
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. gint32
  236. Mono_Posix_Syscall_getfsspec (const char *special_file,
  237. struct Mono_Posix_Syscall__Fstab *fsbuf)
  238. {
  239. mph_fstab *fs;
  240. if (fsbuf == NULL) {
  241. errno = EFAULT;
  242. return -1;
  243. }
  244. fs = getfsspec (special_file);
  245. if (fs == NULL)
  246. return -1;
  247. if (copy_fstab (fsbuf, fs) == -1) {
  248. errno = ENOMEM;
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. gint32
  254. Mono_Posix_Syscall_setfsent (void)
  255. {
  256. return setfsent ();
  257. }
  258. #endif /* def HAVE_FSTAB_H || def HAVE_CHECKPOINT_H || def HAVE_SYS_VFSTAB_H */
  259. G_END_DECLS
  260. /*
  261. * vim: noexpandtab
  262. */