platform_unix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Unix support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_platforms.h"
  10. #ifdef PHYSFS_PLATFORM_UNIX
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <pwd.h>
  18. #include <sys/stat.h>
  19. #include <sys/param.h>
  20. #include <dirent.h>
  21. #include <time.h>
  22. #include <errno.h>
  23. #if (!defined PHYSFS_NO_THREAD_SUPPORT)
  24. #include <pthread.h>
  25. #endif
  26. #ifdef PHYSFS_HAVE_SYS_UCRED_H
  27. # ifdef PHYSFS_HAVE_MNTENT_H
  28. # undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
  29. # endif
  30. # include <sys/mount.h>
  31. # include <sys/ucred.h>
  32. #endif
  33. #ifdef PHYSFS_HAVE_MNTENT_H
  34. #include <mntent.h>
  35. #endif
  36. #ifdef PHYSFS_HAVE_SYS_MNTTAB_H
  37. #include <sys/mnttab.h>
  38. #endif
  39. #include "physfs_internal.h"
  40. /* !!! FIXME: we should probably remove MAXPATHLEN entirely, if we can. */
  41. #ifndef MAXPATHLEN
  42. #define MAXPATHLEN 1024
  43. #endif
  44. int __PHYSFS_platformInit(void)
  45. {
  46. return 1; /* always succeed. */
  47. } /* __PHYSFS_platformInit */
  48. int __PHYSFS_platformDeinit(void)
  49. {
  50. return 1; /* always succeed. */
  51. } /* __PHYSFS_platformDeinit */
  52. /* Stub version for platforms without CD-ROM support. */
  53. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  54. {
  55. #if (defined PHYSFS_NO_CDROM_SUPPORT)
  56. /* no-op. */
  57. #elif (defined PHYSFS_HAVE_SYS_UCRED_H)
  58. int i;
  59. struct statfs *mntbufp = NULL;
  60. int mounts = getmntinfo(&mntbufp, MNT_WAIT);
  61. for (i = 0; i < mounts; i++)
  62. {
  63. int add_it = 0;
  64. if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
  65. add_it = 1;
  66. else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
  67. add_it = 1;
  68. /* add other mount types here */
  69. if (add_it)
  70. cb(data, mntbufp[i].f_mntonname);
  71. } /* for */
  72. #elif (defined PHYSFS_HAVE_MNTENT_H)
  73. FILE *mounts = NULL;
  74. struct mntent *ent = NULL;
  75. mounts = setmntent("/etc/mtab", "r");
  76. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
  77. while ( (ent = getmntent(mounts)) != NULL )
  78. {
  79. int add_it = 0;
  80. if (strcmp(ent->mnt_type, "iso9660") == 0)
  81. add_it = 1;
  82. else if (strcmp(ent->mnt_type, "udf") == 0)
  83. add_it = 1;
  84. /* !!! FIXME: these might pick up floppy drives, right? */
  85. else if (strcmp(ent->mnt_type, "auto") == 0)
  86. add_it = 1;
  87. else if (strcmp(ent->mnt_type, "supermount") == 0)
  88. add_it = 1;
  89. /* !!! FIXME: udf? automount? */
  90. /* add other mount types here */
  91. if (add_it)
  92. cb(data, ent->mnt_dir);
  93. } /* while */
  94. endmntent(mounts);
  95. #elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
  96. FILE *mounts = fopen(MNTTAB, "r");
  97. struct mnttab ent;
  98. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
  99. while (getmntent(mounts, &ent) == 0)
  100. {
  101. int add_it = 0;
  102. if (strcmp(ent.mnt_fstype, "hsfs") == 0)
  103. add_it = 1;
  104. /* add other mount types here */
  105. if (add_it)
  106. cb(data, ent.mnt_mountp);
  107. } /* while */
  108. fclose(mounts);
  109. #else
  110. #error Unknown platform. Should have defined PHYSFS_NO_CDROM_SUPPORT, perhaps.
  111. #endif
  112. } /* __PHYSFS_platformDetectAvailableCDs */
  113. /* this is in posix.c ... */
  114. extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
  115. /*
  116. * See where program (bin) resides in the $PATH specified by (envr).
  117. * returns a copy of the first element in envr that contains it, or NULL
  118. * if it doesn't exist or there were other problems. PHYSFS_SetError() is
  119. * called if we have a problem.
  120. *
  121. * (envr) will be scribbled over, and you are expected to allocator.Free() the
  122. * return value when you're done with it.
  123. */
  124. static char *findBinaryInPath(const char *bin, char *envr)
  125. {
  126. size_t alloc_size = 0;
  127. char *exe = NULL;
  128. char *start = envr;
  129. char *ptr;
  130. BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
  131. BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
  132. do
  133. {
  134. size_t size;
  135. ptr = strchr(start, ':'); /* find next $PATH separator. */
  136. if (ptr)
  137. *ptr = '\0';
  138. size = strlen(start) + strlen(bin) + 2;
  139. if (size > alloc_size)
  140. {
  141. char *x = (char *) allocator.Realloc(exe, size);
  142. if (x == NULL)
  143. {
  144. if (exe != NULL)
  145. allocator.Free(exe);
  146. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  147. } /* if */
  148. alloc_size = size;
  149. exe = x;
  150. } /* if */
  151. /* build full binary path... */
  152. strcpy(exe, start);
  153. if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
  154. strcat(exe, "/");
  155. strcat(exe, bin);
  156. if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
  157. {
  158. strcpy(exe, start); /* i'm lazy. piss off. */
  159. return exe;
  160. } /* if */
  161. start = ptr + 1; /* start points to beginning of next element. */
  162. } while (ptr != NULL);
  163. if (exe != NULL)
  164. allocator.Free(exe);
  165. return NULL; /* doesn't exist in path. */
  166. } /* findBinaryInPath */
  167. static char *readSymLink(const char *path)
  168. {
  169. ssize_t len = 64;
  170. ssize_t rc = -1;
  171. char *retval = NULL;
  172. while (1)
  173. {
  174. char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
  175. if (ptr == NULL)
  176. break; /* out of memory. */
  177. retval = ptr;
  178. rc = readlink(path, retval, len);
  179. if (rc == -1)
  180. break; /* not a symlink, i/o error, etc. */
  181. else if (rc < len)
  182. {
  183. retval[rc] = '\0'; /* readlink doesn't null-terminate. */
  184. return retval; /* we're good to go. */
  185. } /* else if */
  186. len *= 2; /* grow buffer, try again. */
  187. } /* while */
  188. if (retval != NULL)
  189. allocator.Free(retval);
  190. return NULL;
  191. } /* readSymLink */
  192. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  193. {
  194. char *retval = NULL;
  195. char *envr = NULL;
  196. /* fast path: default behaviour can handle this. */
  197. if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
  198. return NULL; /* higher level will parse out real path from argv0. */
  199. /*
  200. * Try to avoid using argv0 unless forced to. If there's a Linux-like
  201. * /proc filesystem, you can get the full path to the current process from
  202. * the /proc/self/exe symlink.
  203. */
  204. retval = readSymLink("/proc/self/exe");
  205. if (retval == NULL)
  206. {
  207. /* older kernels don't have /proc/self ... try PID version... */
  208. const unsigned long long pid = (unsigned long long) getpid();
  209. char path[64];
  210. const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
  211. if ( (rc > 0) && (rc < sizeof(path)) )
  212. retval = readSymLink(path);
  213. } /* if */
  214. if (retval != NULL) /* chop off filename. */
  215. {
  216. char *ptr = strrchr(retval, '/');
  217. if (ptr != NULL)
  218. *ptr = '\0';
  219. } /* if */
  220. if ((retval == NULL) && (argv0 != NULL))
  221. {
  222. /* If there's no dirsep on argv0, then look through $PATH for it. */
  223. envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
  224. BAIL_IF_MACRO(!envr, NULL, NULL);
  225. retval = findBinaryInPath(argv0, envr);
  226. allocator.Free(envr);
  227. } /* if */
  228. if (retval != NULL)
  229. {
  230. /* try to shrink buffer... */
  231. char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
  232. if (ptr != NULL)
  233. retval = ptr; /* oh well if it failed. */
  234. } /* if */
  235. return retval;
  236. } /* __PHYSFS_platformCalcBaseDir */
  237. char *__PHYSFS_platformRealPath(const char *path)
  238. {
  239. char resolved_path[MAXPATHLEN];
  240. char *retval = NULL;
  241. errno = 0;
  242. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  243. retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
  244. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  245. strcpy(retval, resolved_path);
  246. return retval;
  247. } /* __PHYSFS_platformRealPath */
  248. char *__PHYSFS_platformCurrentDir(void)
  249. {
  250. /*
  251. * This can't just do platformRealPath("."), since that would eventually
  252. * just end up calling back into here.
  253. */
  254. int allocSize = 0;
  255. char *retval = NULL;
  256. char *ptr;
  257. do
  258. {
  259. allocSize += 100;
  260. ptr = (char *) allocator.Realloc(retval, allocSize);
  261. if (ptr == NULL)
  262. {
  263. if (retval != NULL)
  264. allocator.Free(retval);
  265. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  266. } /* if */
  267. retval = ptr;
  268. ptr = getcwd(retval, allocSize);
  269. } while (ptr == NULL && errno == ERANGE);
  270. if (ptr == NULL && errno)
  271. {
  272. /*
  273. * getcwd() failed for some reason, for example current
  274. * directory not existing.
  275. */
  276. if (retval != NULL)
  277. allocator.Free(retval);
  278. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  279. } /* if */
  280. return retval;
  281. } /* __PHYSFS_platformCurrentDir */
  282. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  283. {
  284. return 0; /* just use malloc() and friends. */
  285. } /* __PHYSFS_platformSetDefaultAllocator */
  286. #if (defined PHYSFS_NO_THREAD_SUPPORT)
  287. void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
  288. void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
  289. void __PHYSFS_platformDestroyMutex(void *mutex) {}
  290. int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
  291. void __PHYSFS_platformReleaseMutex(void *mutex) {}
  292. #else
  293. typedef struct
  294. {
  295. pthread_mutex_t mutex;
  296. pthread_t owner;
  297. PHYSFS_uint32 count;
  298. } PthreadMutex;
  299. void *__PHYSFS_platformGetThreadID(void)
  300. {
  301. return ( (void *) ((size_t) pthread_self()) );
  302. } /* __PHYSFS_platformGetThreadID */
  303. void *__PHYSFS_platformCreateMutex(void)
  304. {
  305. int rc;
  306. PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
  307. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  308. rc = pthread_mutex_init(&m->mutex, NULL);
  309. if (rc != 0)
  310. {
  311. allocator.Free(m);
  312. BAIL_MACRO(strerror(rc), NULL);
  313. } /* if */
  314. m->count = 0;
  315. m->owner = (pthread_t) 0xDEADBEEF;
  316. return ((void *) m);
  317. } /* __PHYSFS_platformCreateMutex */
  318. void __PHYSFS_platformDestroyMutex(void *mutex)
  319. {
  320. PthreadMutex *m = (PthreadMutex *) mutex;
  321. /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
  322. if ((m->owner == pthread_self()) && (m->count > 0))
  323. pthread_mutex_unlock(&m->mutex);
  324. pthread_mutex_destroy(&m->mutex);
  325. allocator.Free(m);
  326. } /* __PHYSFS_platformDestroyMutex */
  327. int __PHYSFS_platformGrabMutex(void *mutex)
  328. {
  329. PthreadMutex *m = (PthreadMutex *) mutex;
  330. pthread_t tid = pthread_self();
  331. if (m->owner != tid)
  332. {
  333. if (pthread_mutex_lock(&m->mutex) != 0)
  334. return 0;
  335. m->owner = tid;
  336. } /* if */
  337. m->count++;
  338. return 1;
  339. } /* __PHYSFS_platformGrabMutex */
  340. void __PHYSFS_platformReleaseMutex(void *mutex)
  341. {
  342. PthreadMutex *m = (PthreadMutex *) mutex;
  343. if (m->owner == pthread_self())
  344. {
  345. if (--m->count == 0)
  346. {
  347. m->owner = (pthread_t) 0xDEADBEEF;
  348. pthread_mutex_unlock(&m->mutex);
  349. } /* if */
  350. } /* if */
  351. } /* __PHYSFS_platformReleaseMutex */
  352. #endif /* !PHYSFS_NO_THREAD_SUPPORT */
  353. #endif /* PHYSFS_PLATFORM_UNIX */
  354. /* end of unix.c ... */