PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/minadbd/sysdeps.h

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