unix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  142. {
  143. const char *PROC_SELF_EXE = "/proc/self/exe";
  144. char *retval = NULL;
  145. char *envr = NULL;
  146. struct stat stbuf;
  147. /* fast path: default behaviour can handle this. */
  148. if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
  149. return(NULL); /* higher level will parse out real path from argv0. */
  150. /*
  151. * Try to avoid using argv0 unless forced to. If there's a Linux-like
  152. * /proc filesystem, you can get the full path to the current process from
  153. * the /proc/self/exe symlink.
  154. */
  155. if ((lstat(PROC_SELF_EXE, &stbuf) != -1) && (S_ISLNK(stbuf.st_mode)))
  156. {
  157. const size_t len = stbuf.st_size;
  158. char *buf = (char *) allocator.Malloc(len+1);
  159. if (buf != NULL) /* if NULL, maybe you'll get lucky later. */
  160. {
  161. if (readlink(PROC_SELF_EXE, buf, len) != len)
  162. allocator.Free(buf);
  163. else
  164. {
  165. buf[len] = '\0'; /* readlink doesn't null-terminate. */
  166. retval = buf; /* we're good to go. */
  167. } /* else */
  168. } /* if */
  169. } /* if */
  170. if ((retval == NULL) && (argv0 != NULL))
  171. {
  172. /* If there's no dirsep on argv0, then look through $PATH for it. */
  173. envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
  174. BAIL_IF_MACRO(!envr, NULL, NULL);
  175. retval = findBinaryInPath(argv0, envr);
  176. allocator.Free(envr);
  177. } /* if */
  178. return(retval);
  179. } /* __PHYSFS_platformCalcBaseDir */
  180. char *__PHYSFS_platformRealPath(const char *path)
  181. {
  182. char resolved_path[MAXPATHLEN];
  183. char *retval = NULL;
  184. errno = 0;
  185. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  186. retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
  187. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  188. strcpy(retval, resolved_path);
  189. return(retval);
  190. } /* __PHYSFS_platformRealPath */
  191. char *__PHYSFS_platformCurrentDir(void)
  192. {
  193. /*
  194. * This can't just do platformRealPath("."), since that would eventually
  195. * just end up calling back into here.
  196. */
  197. int allocSize = 0;
  198. char *retval = NULL;
  199. char *ptr;
  200. do
  201. {
  202. allocSize += 100;
  203. ptr = (char *) allocator.Realloc(retval, allocSize);
  204. if (ptr == NULL)
  205. {
  206. if (retval != NULL)
  207. allocator.Free(retval);
  208. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  209. } /* if */
  210. retval = ptr;
  211. ptr = getcwd(retval, allocSize);
  212. } while (ptr == NULL && errno == ERANGE);
  213. if (ptr == NULL && errno)
  214. {
  215. /*
  216. * getcwd() failed for some reason, for example current
  217. * directory not existing.
  218. */
  219. if (retval != NULL)
  220. allocator.Free(retval);
  221. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  222. } /* if */
  223. return(retval);
  224. } /* __PHYSFS_platformCurrentDir */
  225. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  226. {
  227. return(0); /* just use malloc() and friends. */
  228. } /* __PHYSFS_platformSetDefaultAllocator */
  229. #if (defined PHYSFS_NO_PTHREADS_SUPPORT)
  230. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
  231. void *__PHYSFS_platformCreateMutex(void) { return((void *) 0x0001); }
  232. void __PHYSFS_platformDestroyMutex(void *mutex) {}
  233. int __PHYSFS_platformGrabMutex(void *mutex) { return(1); }
  234. void __PHYSFS_platformReleaseMutex(void *mutex) {}
  235. #else
  236. typedef struct
  237. {
  238. pthread_mutex_t mutex;
  239. pthread_t owner;
  240. PHYSFS_uint32 count;
  241. } PthreadMutex;
  242. /* Just in case; this is a panic value. */
  243. #if ((!defined SIZEOF_INT) || (SIZEOF_INT <= 0))
  244. # define SIZEOF_INT 4
  245. #endif
  246. #if (SIZEOF_INT == 4)
  247. # define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint32) (thr)) )
  248. #elif (SIZEOF_INT == 2)
  249. # define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint16) (thr)) )
  250. #elif (SIZEOF_INT == 1)
  251. # define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint8) (thr)) )
  252. #else
  253. # define PHTREAD_TO_UI64(thr) ((PHYSFS_uint64) (thr))
  254. #endif
  255. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  256. {
  257. return(PHTREAD_TO_UI64(pthread_self()));
  258. } /* __PHYSFS_platformGetThreadID */
  259. void *__PHYSFS_platformCreateMutex(void)
  260. {
  261. int rc;
  262. PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
  263. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  264. rc = pthread_mutex_init(&m->mutex, NULL);
  265. if (rc != 0)
  266. {
  267. allocator.Free(m);
  268. BAIL_MACRO(strerror(rc), NULL);
  269. } /* if */
  270. m->count = 0;
  271. m->owner = (pthread_t) 0xDEADBEEF;
  272. return((void *) m);
  273. } /* __PHYSFS_platformCreateMutex */
  274. void __PHYSFS_platformDestroyMutex(void *mutex)
  275. {
  276. PthreadMutex *m = (PthreadMutex *) mutex;
  277. /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
  278. if ((m->owner == pthread_self()) && (m->count > 0))
  279. pthread_mutex_unlock(&m->mutex);
  280. pthread_mutex_destroy(&m->mutex);
  281. allocator.Free(m);
  282. } /* __PHYSFS_platformDestroyMutex */
  283. int __PHYSFS_platformGrabMutex(void *mutex)
  284. {
  285. PthreadMutex *m = (PthreadMutex *) mutex;
  286. pthread_t tid = pthread_self();
  287. if (m->owner != tid)
  288. {
  289. if (pthread_mutex_lock(&m->mutex) != 0)
  290. return(0);
  291. m->owner = tid;
  292. } /* if */
  293. m->count++;
  294. return(1);
  295. } /* __PHYSFS_platformGrabMutex */
  296. void __PHYSFS_platformReleaseMutex(void *mutex)
  297. {
  298. PthreadMutex *m = (PthreadMutex *) mutex;
  299. if (m->owner == pthread_self())
  300. {
  301. if (--m->count == 0)
  302. {
  303. m->owner = (pthread_t) 0xDEADBEEF;
  304. pthread_mutex_unlock(&m->mutex);
  305. } /* if */
  306. } /* if */
  307. } /* __PHYSFS_platformReleaseMutex */
  308. #endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
  309. #endif /* PHYSFS_PLATFORM_UNIX */
  310. /* end of unix.c ... */