sysdeps.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* this file contains system-dependent definitions used by ADB
  17. * they're related to threads, sockets and file descriptors
  18. */
  19. #ifndef _ADB_SYSDEPS_H
  20. #define _ADB_SYSDEPS_H
  21. #ifdef __CYGWIN__
  22. # undef _WIN32
  23. #endif
  24. #ifdef _WIN32
  25. #include <windows.h>
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. #include <process.h>
  29. #include <fcntl.h>
  30. #include <io.h>
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #include <ctype.h>
  34. #define OS_PATH_SEPARATOR '\\'
  35. #define OS_PATH_SEPARATOR_STR "\\"
  36. #define ENV_PATH_SEPARATOR_STR ";"
  37. typedef CRITICAL_SECTION adb_mutex_t;
  38. #define ADB_MUTEX_DEFINE(x) adb_mutex_t x
  39. /* declare all mutexes */
  40. /* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
  41. #define ADB_MUTEX(x) extern adb_mutex_t x;
  42. #include "mutex_list.h"
  43. extern void adb_sysdeps_init(void);
  44. static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
  45. {
  46. EnterCriticalSection( lock );
  47. }
  48. static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
  49. {
  50. LeaveCriticalSection( lock );
  51. }
  52. typedef struct { unsigned tid; } adb_thread_t;
  53. typedef void* (*adb_thread_func_t)(void* arg);
  54. typedef void (*win_thread_func_t)(void* arg);
  55. static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
  56. {
  57. thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
  58. if (thread->tid == (unsigned)-1L) {
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static __inline__ void close_on_exec(int fd)
  64. {
  65. /* nothing really */
  66. }
  67. extern void disable_tcp_nagle(int fd);
  68. #define lstat stat /* no symlinks on Win32 */
  69. #define S_ISLNK(m) 0 /* no symlinks on Win32 */
  70. static __inline__ int adb_unlink(const char* path)
  71. {
  72. int rc = unlink(path);
  73. if (rc == -1 && errno == EACCES) {
  74. /* unlink returns EACCES when the file is read-only, so we first */
  75. /* try to make it writable, then unlink again... */
  76. rc = chmod(path, _S_IREAD|_S_IWRITE );
  77. if (rc == 0)
  78. rc = unlink(path);
  79. }
  80. return rc;
  81. }
  82. #undef unlink
  83. #define unlink ___xxx_unlink
  84. static __inline__ int adb_mkdir(const char* path, int mode)
  85. {
  86. return _mkdir(path);
  87. }
  88. #undef mkdir
  89. #define mkdir ___xxx_mkdir
  90. extern int adb_open(const char* path, int options);
  91. extern int adb_creat(const char* path, int mode);
  92. extern int adb_read(int fd, void* buf, int len);
  93. extern int adb_write(int fd, const void* buf, int len);
  94. extern int adb_lseek(int fd, int pos, int where);
  95. extern int adb_shutdown(int fd);
  96. extern int adb_close(int fd);
  97. static __inline__ int unix_close(int fd)
  98. {
  99. return close(fd);
  100. }
  101. #undef close
  102. #define close ____xxx_close
  103. static __inline__ int unix_read(int fd, void* buf, size_t len)
  104. {
  105. return read(fd, buf, len);
  106. }
  107. #undef read
  108. #define read ___xxx_read
  109. static __inline__ int unix_write(int fd, const void* buf, size_t len)
  110. {
  111. return write(fd, buf, len);
  112. }
  113. #undef write
  114. #define write ___xxx_write
  115. static __inline__ int adb_open_mode(const char* path, int options, int mode)
  116. {
  117. return adb_open(path, options);
  118. }
  119. static __inline__ int unix_open(const char* path, int options,...)
  120. {
  121. if ((options & O_CREAT) == 0)
  122. {
  123. return open(path, options);
  124. }
  125. else
  126. {
  127. int mode;
  128. va_list args;
  129. va_start( args, options );
  130. mode = va_arg( args, int );
  131. va_end( args );
  132. return open(path, options, mode);
  133. }
  134. }
  135. #define open ___xxx_unix_open
  136. /* normally provided by <cutils/misc.h> */
  137. extern void* load_file(const char* pathname, unsigned* psize);
  138. /* normally provided by <cutils/sockets.h> */
  139. extern int socket_loopback_client(int port, int type);
  140. extern int socket_network_client(const char *host, int port, int type);
  141. extern int socket_loopback_server(int port, int type);
  142. extern int socket_inaddr_any_server(int port, int type);
  143. /* normally provided by "fdevent.h" */
  144. #define FDE_READ 0x0001
  145. #define FDE_WRITE 0x0002
  146. #define FDE_ERROR 0x0004
  147. #define FDE_DONT_CLOSE 0x0080
  148. typedef struct fdevent fdevent;
  149. typedef void (*fd_func)(int fd, unsigned events, void *userdata);
  150. fdevent *fdevent_create(int fd, fd_func func, void *arg);
  151. void fdevent_destroy(fdevent *fde);
  152. void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
  153. void fdevent_remove(fdevent *item);
  154. void fdevent_set(fdevent *fde, unsigned events);
  155. void fdevent_add(fdevent *fde, unsigned events);
  156. void fdevent_del(fdevent *fde, unsigned events);
  157. void fdevent_loop();
  158. struct fdevent {
  159. fdevent *next;
  160. fdevent *prev;
  161. int fd;
  162. int force_eof;
  163. unsigned short state;
  164. unsigned short events;
  165. fd_func func;
  166. void *arg;
  167. };
  168. static __inline__ void adb_sleep_ms( int mseconds )
  169. {
  170. Sleep( mseconds );
  171. }
  172. extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
  173. #undef accept
  174. #define accept ___xxx_accept
  175. static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
  176. {
  177. int opt = bufsize;
  178. return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
  179. }
  180. extern int adb_socketpair( int sv[2] );
  181. static __inline__ char* adb_dirstart( const char* path )
  182. {
  183. char* p = strchr(path, '/');
  184. char* p2 = strchr(path, '\\');
  185. if ( !p )
  186. p = p2;
  187. else if ( p2 && p2 > p )
  188. p = p2;
  189. return p;
  190. }
  191. static __inline__ char* adb_dirstop( const char* path )
  192. {
  193. char* p = strrchr(path, '/');
  194. char* p2 = strrchr(path, '\\');
  195. if ( !p )
  196. p = p2;
  197. else if ( p2 && p2 > p )
  198. p = p2;
  199. return p;
  200. }
  201. static __inline__ int adb_is_absolute_host_path( const char* path )
  202. {
  203. return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
  204. }
  205. extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
  206. #else /* !_WIN32 a.k.a. Unix */
  207. #include "fdevent.h"
  208. #include <cutils/sockets.h>
  209. #include <cutils/properties.h>
  210. #include <cutils/misc.h>
  211. #include <signal.h>
  212. #include <sys/wait.h>
  213. #include <sys/stat.h>
  214. #include <fcntl.h>
  215. #include <pthread.h>
  216. #include <unistd.h>
  217. #include <fcntl.h>
  218. #include <stdarg.h>
  219. #include <netinet/in.h>
  220. #include <netinet/tcp.h>
  221. #include <string.h>
  222. #define OS_PATH_SEPARATOR '/'
  223. #define OS_PATH_SEPARATOR_STR "/"
  224. #define ENV_PATH_SEPARATOR_STR ":"
  225. typedef pthread_mutex_t adb_mutex_t;
  226. #define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  227. #define adb_mutex_init pthread_mutex_init
  228. #define adb_mutex_lock pthread_mutex_lock
  229. #define adb_mutex_unlock pthread_mutex_unlock
  230. #define adb_mutex_destroy pthread_mutex_destroy
  231. #define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
  232. #define adb_cond_t pthread_cond_t
  233. #define adb_cond_init pthread_cond_init
  234. #define adb_cond_wait pthread_cond_wait
  235. #define adb_cond_broadcast pthread_cond_broadcast
  236. #define adb_cond_signal pthread_cond_signal
  237. #define adb_cond_destroy pthread_cond_destroy
  238. /* declare all mutexes */
  239. #define ADB_MUTEX(x) extern adb_mutex_t x;
  240. #include "mutex_list.h"
  241. static __inline__ void close_on_exec(int fd)
  242. {
  243. fcntl( fd, F_SETFD, FD_CLOEXEC );
  244. }
  245. static __inline__ int unix_open(const char* path, int options,...)
  246. {
  247. if ((options & O_CREAT) == 0)
  248. {
  249. return open(path, options);
  250. }
  251. else
  252. {
  253. int mode;
  254. va_list args;
  255. va_start( args, options );
  256. mode = va_arg( args, int );
  257. va_end( args );
  258. return open(path, options, mode);
  259. }
  260. }
  261. static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
  262. {
  263. return open( pathname, options, mode );
  264. }
  265. static __inline__ int adb_open( const char* pathname, int options )
  266. {
  267. int fd = open( pathname, options );
  268. if (fd < 0)
  269. return -1;
  270. close_on_exec( fd );
  271. return fd;
  272. }
  273. #undef open
  274. #define open ___xxx_open
  275. static __inline__ int adb_shutdown(int fd)
  276. {
  277. return shutdown(fd, SHUT_RDWR);
  278. }
  279. #undef shutdown
  280. #define shutdown ____xxx_shutdown
  281. static __inline__ int adb_close(int fd)
  282. {
  283. return close(fd);
  284. }
  285. #undef close
  286. #define close ____xxx_close
  287. static __inline__ int adb_read(int fd, void* buf, size_t len)
  288. {
  289. return read(fd, buf, len);
  290. }
  291. #undef read
  292. #define read ___xxx_read
  293. static __inline__ int adb_write(int fd, const void* buf, size_t len)
  294. {
  295. return write(fd, buf, len);
  296. }
  297. #undef write
  298. #define write ___xxx_write
  299. static __inline__ int adb_lseek(int fd, int pos, int where)
  300. {
  301. return lseek(fd, pos, where);
  302. }
  303. #undef lseek
  304. #define lseek ___xxx_lseek
  305. static __inline__ int adb_unlink(const char* path)
  306. {
  307. return unlink(path);
  308. }
  309. #undef unlink
  310. #define unlink ___xxx_unlink
  311. static __inline__ int adb_creat(const char* path, int mode)
  312. {
  313. int fd = creat(path, mode);
  314. if ( fd < 0 )
  315. return -1;
  316. close_on_exec(fd);
  317. return fd;
  318. }
  319. #undef creat
  320. #define creat ___xxx_creat
  321. static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
  322. {
  323. int fd;
  324. fd = accept(serverfd, addr, addrlen);
  325. if (fd >= 0)
  326. close_on_exec(fd);
  327. return fd;
  328. }
  329. #undef accept
  330. #define accept ___xxx_accept
  331. #define unix_read adb_read
  332. #define unix_write adb_write
  333. #define unix_close adb_close
  334. typedef pthread_t adb_thread_t;
  335. typedef void* (*adb_thread_func_t)( void* arg );
  336. static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
  337. {
  338. pthread_attr_t attr;
  339. pthread_attr_init (&attr);
  340. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  341. return pthread_create( pthread, &attr, start, arg );
  342. }
  343. static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
  344. {
  345. int opt = bufsize;
  346. return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
  347. }
  348. static __inline__ void disable_tcp_nagle(int fd)
  349. {
  350. int on = 1;
  351. setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
  352. }
  353. static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
  354. {
  355. return socketpair( d, type, protocol, sv );
  356. }
  357. static __inline__ int adb_socketpair( int sv[2] )
  358. {
  359. int rc;
  360. rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
  361. if (rc < 0)
  362. return -1;
  363. close_on_exec( sv[0] );
  364. close_on_exec( sv[1] );
  365. return 0;
  366. }
  367. #undef socketpair
  368. #define socketpair ___xxx_socketpair
  369. static __inline__ void adb_sleep_ms( int mseconds )
  370. {
  371. usleep( mseconds*1000 );
  372. }
  373. static __inline__ int adb_mkdir(const char* path, int mode)
  374. {
  375. return mkdir(path, mode);
  376. }
  377. #undef mkdir
  378. #define mkdir ___xxx_mkdir
  379. static __inline__ void adb_sysdeps_init(void)
  380. {
  381. }
  382. static __inline__ char* adb_dirstart(const char* path)
  383. {
  384. return strchr(path, '/');
  385. }
  386. static __inline__ char* adb_dirstop(const char* path)
  387. {
  388. return strrchr(path, '/');
  389. }
  390. static __inline__ int adb_is_absolute_host_path( const char* path )
  391. {
  392. return path[0] == '/';
  393. }
  394. static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
  395. {
  396. return strtok_r(str, delim, saveptr);
  397. }
  398. #undef strtok_r
  399. #define strtok_r ___xxx_strtok_r
  400. #endif /* !_WIN32 */
  401. #endif /* _ADB_SYSDEPS_H */