unix.c 11 KB

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