blob: 11de54882aaae209846ddd54d487788f1381252b [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
17/* this file contains system-dependent definitions used by ADB
18 * they're related to threads, sockets and file descriptors
19 */
20#ifndef _ADB_SYSDEPS_H
21#define _ADB_SYSDEPS_H
22
23#ifdef __CYGWIN__
24# undef _WIN32
25#endif
26
27#ifdef _WIN32
28
29#include <windows.h>
30#include <winsock2.h>
31#include <ws2tcpip.h>
32#include <process.h>
33#include <fcntl.h>
34#include <io.h>
35#include <sys/stat.h>
36#include <errno.h>
37#include <ctype.h>
38
39#define OS_PATH_SEPARATOR '\\'
40#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070041#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43typedef CRITICAL_SECTION adb_mutex_t;
44
45#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
46
47/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070048/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#define ADB_MUTEX(x) extern adb_mutex_t x;
50#include "mutex_list.h"
51
52extern void adb_sysdeps_init(void);
53
54static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
55{
56 EnterCriticalSection( lock );
57}
58
59static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
60{
61 LeaveCriticalSection( lock );
62}
63
64typedef struct { unsigned tid; } adb_thread_t;
65
66typedef void* (*adb_thread_func_t)(void* arg);
67
68typedef void (*win_thread_func_t)(void* arg);
69
70static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
71{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020072 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
73 if (thread->tid == (unsigned)-1L) {
74 return -1;
75 }
76 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077}
78
leozwangadb09fa2014-08-15 09:51:27 -070079static __inline__ unsigned long adb_thread_id()
80{
81 return GetCurrentThreadId();
82}
83
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084static __inline__ void close_on_exec(int fd)
85{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020086 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087}
88
89extern void disable_tcp_nagle(int fd);
90
91#define lstat stat /* no symlinks on Win32 */
92
93#define S_ISLNK(m) 0 /* no symlinks on Win32 */
94
95static __inline__ int adb_unlink(const char* path)
96{
97 int rc = unlink(path);
98
99 if (rc == -1 && errno == EACCES) {
100 /* unlink returns EACCES when the file is read-only, so we first */
101 /* try to make it writable, then unlink again... */
102 rc = chmod(path, _S_IREAD|_S_IWRITE );
103 if (rc == 0)
104 rc = unlink(path);
105 }
106 return rc;
107}
108#undef unlink
109#define unlink ___xxx_unlink
110
111static __inline__ int adb_mkdir(const char* path, int mode)
112{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800113 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114}
115#undef mkdir
116#define mkdir ___xxx_mkdir
117
118extern int adb_open(const char* path, int options);
119extern int adb_creat(const char* path, int mode);
120extern int adb_read(int fd, void* buf, int len);
121extern int adb_write(int fd, const void* buf, int len);
122extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400123extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124extern int adb_close(int fd);
125
126static __inline__ int unix_close(int fd)
127{
128 return close(fd);
129}
130#undef close
131#define close ____xxx_close
132
133static __inline__ int unix_read(int fd, void* buf, size_t len)
134{
135 return read(fd, buf, len);
136}
137#undef read
138#define read ___xxx_read
139
140static __inline__ int unix_write(int fd, const void* buf, size_t len)
141{
142 return write(fd, buf, len);
143}
144#undef write
145#define write ___xxx_write
146
147static __inline__ int adb_open_mode(const char* path, int options, int mode)
148{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200149 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150}
151
152static __inline__ int unix_open(const char* path, int options,...)
153{
154 if ((options & O_CREAT) == 0)
155 {
156 return open(path, options);
157 }
158 else
159 {
160 int mode;
161 va_list args;
162 va_start( args, options );
163 mode = va_arg( args, int );
164 va_end( args );
165 return open(path, options, mode);
166 }
167}
168#define open ___xxx_unix_open
169
170
171/* normally provided by <cutils/misc.h> */
172extern void* load_file(const char* pathname, unsigned* psize);
173
174/* normally provided by <cutils/sockets.h> */
175extern int socket_loopback_client(int port, int type);
176extern int socket_network_client(const char *host, int port, int type);
Elliott Hughesb911cf02014-05-20 08:51:15 -0700177extern int socket_network_client_timeout(const char *host, int port, int type,
178 int timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179extern int socket_loopback_server(int port, int type);
180extern int socket_inaddr_any_server(int port, int type);
181
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200182/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
184#define FDE_READ 0x0001
185#define FDE_WRITE 0x0002
186#define FDE_ERROR 0x0004
187#define FDE_DONT_CLOSE 0x0080
188
189typedef struct fdevent fdevent;
190
191typedef void (*fd_func)(int fd, unsigned events, void *userdata);
192
193fdevent *fdevent_create(int fd, fd_func func, void *arg);
194void fdevent_destroy(fdevent *fde);
195void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
196void fdevent_remove(fdevent *item);
197void fdevent_set(fdevent *fde, unsigned events);
198void fdevent_add(fdevent *fde, unsigned events);
199void fdevent_del(fdevent *fde, unsigned events);
200void fdevent_loop();
201
202struct fdevent {
203 fdevent *next;
204 fdevent *prev;
205
206 int fd;
JP Abgrall408fa572011-03-16 15:57:42 -0700207 int force_eof;
208
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 unsigned short state;
210 unsigned short events;
211
212 fd_func func;
213 void *arg;
214};
215
216static __inline__ void adb_sleep_ms( int mseconds )
217{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200218 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219}
220
221extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
222
223#undef accept
224#define accept ___xxx_accept
225
226static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
227{
228 int opt = bufsize;
229 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
230}
231
232extern int adb_socketpair( int sv[2] );
233
234static __inline__ char* adb_dirstart( const char* path )
235{
236 char* p = strchr(path, '/');
237 char* p2 = strchr(path, '\\');
238
239 if ( !p )
240 p = p2;
241 else if ( p2 && p2 > p )
242 p = p2;
243
244 return p;
245}
246
247static __inline__ char* adb_dirstop( const char* path )
248{
249 char* p = strrchr(path, '/');
250 char* p2 = strrchr(path, '\\');
251
252 if ( !p )
253 p = p2;
254 else if ( p2 && p2 > p )
255 p = p2;
256
257 return p;
258}
259
260static __inline__ int adb_is_absolute_host_path( const char* path )
261{
262 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
263}
264
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700265extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
266
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267#else /* !_WIN32 a.k.a. Unix */
268
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200269#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271#include <cutils/misc.h>
272#include <signal.h>
273#include <sys/wait.h>
274#include <sys/stat.h>
275#include <fcntl.h>
276
277#include <pthread.h>
278#include <unistd.h>
279#include <fcntl.h>
280#include <stdarg.h>
281#include <netinet/in.h>
282#include <netinet/tcp.h>
283#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700284#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285
Kenny Rootec90f1d2012-10-13 11:59:01 -0700286/*
287 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
288 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
289 * not already defined, then define it here.
290 */
291#ifndef TEMP_FAILURE_RETRY
292/* Used to retry syscalls that can return EINTR. */
293#define TEMP_FAILURE_RETRY(exp) ({ \
294 typeof (exp) _rc; \
295 do { \
296 _rc = (exp); \
297 } while (_rc == -1 && errno == EINTR); \
298 _rc; })
299#endif
300
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301#define OS_PATH_SEPARATOR '/'
302#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700303#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304
305typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700306
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
308#define adb_mutex_init pthread_mutex_init
309#define adb_mutex_lock pthread_mutex_lock
310#define adb_mutex_unlock pthread_mutex_unlock
311#define adb_mutex_destroy pthread_mutex_destroy
312
JP Abgrall408fa572011-03-16 15:57:42 -0700313#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314
315#define adb_cond_t pthread_cond_t
316#define adb_cond_init pthread_cond_init
317#define adb_cond_wait pthread_cond_wait
318#define adb_cond_broadcast pthread_cond_broadcast
319#define adb_cond_signal pthread_cond_signal
320#define adb_cond_destroy pthread_cond_destroy
321
JP Abgrall408fa572011-03-16 15:57:42 -0700322/* declare all mutexes */
323#define ADB_MUTEX(x) extern adb_mutex_t x;
324#include "mutex_list.h"
325
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326static __inline__ void close_on_exec(int fd)
327{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200328 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329}
330
331static __inline__ int unix_open(const char* path, int options,...)
332{
333 if ((options & O_CREAT) == 0)
334 {
Kenny Root73167412012-10-12 15:26:45 -0700335 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 }
337 else
338 {
339 int mode;
340 va_list args;
341 va_start( args, options );
342 mode = va_arg( args, int );
343 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700344 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 }
346}
347
348static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
349{
Kenny Root73167412012-10-12 15:26:45 -0700350 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351}
352
353
354static __inline__ int adb_open( const char* pathname, int options )
355{
Kenny Root73167412012-10-12 15:26:45 -0700356 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 if (fd < 0)
358 return -1;
359 close_on_exec( fd );
360 return fd;
361}
362#undef open
363#define open ___xxx_open
364
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400365static __inline__ int adb_shutdown(int fd)
366{
367 return shutdown(fd, SHUT_RDWR);
368}
369#undef shutdown
370#define shutdown ____xxx_shutdown
371
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372static __inline__ int adb_close(int fd)
373{
374 return close(fd);
375}
376#undef close
377#define close ____xxx_close
378
379
380static __inline__ int adb_read(int fd, void* buf, size_t len)
381{
Kenny Root73167412012-10-12 15:26:45 -0700382 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383}
384
385#undef read
386#define read ___xxx_read
387
388static __inline__ int adb_write(int fd, const void* buf, size_t len)
389{
Kenny Root73167412012-10-12 15:26:45 -0700390 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391}
392#undef write
393#define write ___xxx_write
394
395static __inline__ int adb_lseek(int fd, int pos, int where)
396{
397 return lseek(fd, pos, where);
398}
399#undef lseek
400#define lseek ___xxx_lseek
401
402static __inline__ int adb_unlink(const char* path)
403{
404 return unlink(path);
405}
406#undef unlink
407#define unlink ___xxx_unlink
408
409static __inline__ int adb_creat(const char* path, int mode)
410{
Kenny Root73167412012-10-12 15:26:45 -0700411 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200413 if ( fd < 0 )
414 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415
416 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200417 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418}
419#undef creat
420#define creat ___xxx_creat
421
422static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
423{
Benoit Goby95ef8282011-02-01 18:57:41 -0800424 int fd;
425
Kenny Root73167412012-10-12 15:26:45 -0700426 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800427 if (fd >= 0)
428 close_on_exec(fd);
429
430 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431}
432
433#undef accept
434#define accept ___xxx_accept
435
436#define unix_read adb_read
437#define unix_write adb_write
438#define unix_close adb_close
439
440typedef pthread_t adb_thread_t;
441
442typedef void* (*adb_thread_func_t)( void* arg );
443
444static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
445{
446 pthread_attr_t attr;
447
448 pthread_attr_init (&attr);
449 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
450
451 return pthread_create( pthread, &attr, start, arg );
452}
453
454static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
455{
456 int opt = bufsize;
457 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
458}
459
460static __inline__ void disable_tcp_nagle(int fd)
461{
462 int on = 1;
463 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
464}
465
466
467static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
468{
469 return socketpair( d, type, protocol, sv );
470}
471
472static __inline__ int adb_socketpair( int sv[2] )
473{
474 int rc;
475
476 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
477 if (rc < 0)
478 return -1;
479
480 close_on_exec( sv[0] );
481 close_on_exec( sv[1] );
482 return 0;
483}
484
485#undef socketpair
486#define socketpair ___xxx_socketpair
487
488static __inline__ void adb_sleep_ms( int mseconds )
489{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200490 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491}
492
493static __inline__ int adb_mkdir(const char* path, int mode)
494{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200495 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496}
497#undef mkdir
498#define mkdir ___xxx_mkdir
499
500static __inline__ void adb_sysdeps_init(void)
501{
502}
503
504static __inline__ char* adb_dirstart(const char* path)
505{
506 return strchr(path, '/');
507}
508
509static __inline__ char* adb_dirstop(const char* path)
510{
511 return strrchr(path, '/');
512}
513
514static __inline__ int adb_is_absolute_host_path( const char* path )
515{
516 return path[0] == '/';
517}
518
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700519static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
520{
521 return strtok_r(str, delim, saveptr);
522}
leozwangadb09fa2014-08-15 09:51:27 -0700523
524static __inline__ unsigned long adb_thread_id()
525{
leozwang0f1fda92014-08-22 15:35:47 -0700526 return (unsigned long)pthread_self();
leozwangadb09fa2014-08-15 09:51:27 -0700527}
528
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700529#undef strtok_r
530#define strtok_r ___xxx_strtok_r
531
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532#endif /* !_WIN32 */
533
534#endif /* _ADB_SYSDEPS_H */