blob: ec847b5e4adbd44fe52cb596e21a1ee93b301597 [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
Dan Albert630b9af2014-11-24 23:34:35 -080029#include <ctype.h>
30#include <direct.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <io.h>
34#include <process.h>
35#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070037#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080039
40#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Dan Albert818fb4b2015-02-18 00:18:25 -080042#ifdef __cplusplus
43extern "C" {
44#endif
45
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#define OS_PATH_SEPARATOR '\\'
47#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070048#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50typedef CRITICAL_SECTION adb_mutex_t;
51
52#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
53
54/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070055/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056#define ADB_MUTEX(x) extern adb_mutex_t x;
57#include "mutex_list.h"
58
59extern void adb_sysdeps_init(void);
60
61static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
62{
63 EnterCriticalSection( lock );
64}
65
66static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
67{
68 LeaveCriticalSection( lock );
69}
70
71typedef struct { unsigned tid; } adb_thread_t;
72
73typedef void* (*adb_thread_func_t)(void* arg);
74
75typedef void (*win_thread_func_t)(void* arg);
76
77static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
78{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020079 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
80 if (thread->tid == (unsigned)-1L) {
81 return -1;
82 }
83 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084}
85
leozwangcbf02672014-08-15 09:51:27 -070086static __inline__ unsigned long adb_thread_id()
87{
88 return GetCurrentThreadId();
89}
90
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091static __inline__ void close_on_exec(int fd)
92{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020093 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094}
95
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096#define lstat stat /* no symlinks on Win32 */
97
98#define S_ISLNK(m) 0 /* no symlinks on Win32 */
99
100static __inline__ int adb_unlink(const char* path)
101{
102 int rc = unlink(path);
103
104 if (rc == -1 && errno == EACCES) {
105 /* unlink returns EACCES when the file is read-only, so we first */
106 /* try to make it writable, then unlink again... */
107 rc = chmod(path, _S_IREAD|_S_IWRITE );
108 if (rc == 0)
109 rc = unlink(path);
110 }
111 return rc;
112}
113#undef unlink
114#define unlink ___xxx_unlink
115
116static __inline__ int adb_mkdir(const char* path, int mode)
117{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800118 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119}
120#undef mkdir
121#define mkdir ___xxx_mkdir
122
123extern int adb_open(const char* path, int options);
124extern int adb_creat(const char* path, int mode);
125extern int adb_read(int fd, void* buf, int len);
126extern int adb_write(int fd, const void* buf, int len);
127extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400128extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129extern int adb_close(int fd);
130
131static __inline__ int unix_close(int fd)
132{
133 return close(fd);
134}
135#undef close
136#define close ____xxx_close
137
138static __inline__ int unix_read(int fd, void* buf, size_t len)
139{
140 return read(fd, buf, len);
141}
142#undef read
143#define read ___xxx_read
144
145static __inline__ int unix_write(int fd, const void* buf, size_t len)
146{
147 return write(fd, buf, len);
148}
149#undef write
150#define write ___xxx_write
151
152static __inline__ int adb_open_mode(const char* path, int options, int mode)
153{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200154 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155}
156
157static __inline__ int unix_open(const char* path, int options,...)
158{
159 if ((options & O_CREAT) == 0)
160 {
161 return open(path, options);
162 }
163 else
164 {
165 int mode;
166 va_list args;
167 va_start( args, options );
168 mode = va_arg( args, int );
169 va_end( args );
170 return open(path, options, mode);
171 }
172}
173#define open ___xxx_unix_open
174
175
176/* normally provided by <cutils/misc.h> */
177extern void* load_file(const char* pathname, unsigned* psize);
178
179/* normally provided by <cutils/sockets.h> */
180extern int socket_loopback_client(int port, int type);
181extern int socket_network_client(const char *host, int port, int type);
Elliott Hughesb911cf02014-05-20 08:51:15 -0700182extern int socket_network_client_timeout(const char *host, int port, int type,
183 int timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184extern int socket_loopback_server(int port, int type);
185extern int socket_inaddr_any_server(int port, int type);
186
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200187/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
189#define FDE_READ 0x0001
190#define FDE_WRITE 0x0002
191#define FDE_ERROR 0x0004
192#define FDE_DONT_CLOSE 0x0080
193
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194typedef void (*fd_func)(int fd, unsigned events, void *userdata);
195
196fdevent *fdevent_create(int fd, fd_func func, void *arg);
197void fdevent_destroy(fdevent *fde);
198void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
199void fdevent_remove(fdevent *item);
200void fdevent_set(fdevent *fde, unsigned events);
201void fdevent_add(fdevent *fde, unsigned events);
202void fdevent_del(fdevent *fde, unsigned events);
203void fdevent_loop();
204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205static __inline__ void adb_sleep_ms( int mseconds )
206{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200207 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208}
209
210extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
211
212#undef accept
213#define accept ___xxx_accept
214
Spencer Lowf055c192015-01-25 14:40:16 -0800215extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
216
217#undef setsockopt
218#define setsockopt ___xxx_setsockopt
219
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
221{
222 int opt = bufsize;
Spencer Lowf055c192015-01-25 14:40:16 -0800223 return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
224}
225
226static __inline__ void disable_tcp_nagle( int fd )
227{
228 int on = 1;
229 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230}
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
Dan Albert818fb4b2015-02-18 00:18:25 -0800267#ifdef __cplusplus
268}
269#endif
270
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271#else /* !_WIN32 a.k.a. Unix */
272
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200273#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275#include <cutils/misc.h>
276#include <signal.h>
277#include <sys/wait.h>
278#include <sys/stat.h>
279#include <fcntl.h>
280
281#include <pthread.h>
282#include <unistd.h>
283#include <fcntl.h>
284#include <stdarg.h>
285#include <netinet/in.h>
286#include <netinet/tcp.h>
287#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700288#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289
Dan Albert818fb4b2015-02-18 00:18:25 -0800290#ifdef __cplusplus
291extern "C" {
292#endif
293
Kenny Rootec90f1d2012-10-13 11:59:01 -0700294/*
295 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
296 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
297 * not already defined, then define it here.
298 */
299#ifndef TEMP_FAILURE_RETRY
300/* Used to retry syscalls that can return EINTR. */
301#define TEMP_FAILURE_RETRY(exp) ({ \
302 typeof (exp) _rc; \
303 do { \
304 _rc = (exp); \
305 } while (_rc == -1 && errno == EINTR); \
306 _rc; })
307#endif
308
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309#define OS_PATH_SEPARATOR '/'
310#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700311#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312
313typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700314
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
316#define adb_mutex_init pthread_mutex_init
317#define adb_mutex_lock pthread_mutex_lock
318#define adb_mutex_unlock pthread_mutex_unlock
319#define adb_mutex_destroy pthread_mutex_destroy
320
JP Abgrall408fa572011-03-16 15:57:42 -0700321#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322
323#define adb_cond_t pthread_cond_t
324#define adb_cond_init pthread_cond_init
325#define adb_cond_wait pthread_cond_wait
326#define adb_cond_broadcast pthread_cond_broadcast
327#define adb_cond_signal pthread_cond_signal
328#define adb_cond_destroy pthread_cond_destroy
329
JP Abgrall408fa572011-03-16 15:57:42 -0700330/* declare all mutexes */
331#define ADB_MUTEX(x) extern adb_mutex_t x;
332#include "mutex_list.h"
333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334static __inline__ void close_on_exec(int fd)
335{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200336 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337}
338
339static __inline__ int unix_open(const char* path, int options,...)
340{
341 if ((options & O_CREAT) == 0)
342 {
Kenny Root73167412012-10-12 15:26:45 -0700343 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 }
345 else
346 {
347 int mode;
348 va_list args;
349 va_start( args, options );
350 mode = va_arg( args, int );
351 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700352 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 }
354}
355
356static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
357{
Kenny Root73167412012-10-12 15:26:45 -0700358 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359}
360
361
362static __inline__ int adb_open( const char* pathname, int options )
363{
Kenny Root73167412012-10-12 15:26:45 -0700364 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 if (fd < 0)
366 return -1;
367 close_on_exec( fd );
368 return fd;
369}
370#undef open
371#define open ___xxx_open
372
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400373static __inline__ int adb_shutdown(int fd)
374{
375 return shutdown(fd, SHUT_RDWR);
376}
377#undef shutdown
378#define shutdown ____xxx_shutdown
379
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380static __inline__ int adb_close(int fd)
381{
382 return close(fd);
383}
384#undef close
385#define close ____xxx_close
386
387
388static __inline__ int adb_read(int fd, void* buf, size_t len)
389{
Kenny Root73167412012-10-12 15:26:45 -0700390 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391}
392
393#undef read
394#define read ___xxx_read
395
396static __inline__ int adb_write(int fd, const void* buf, size_t len)
397{
Kenny Root73167412012-10-12 15:26:45 -0700398 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399}
400#undef write
401#define write ___xxx_write
402
403static __inline__ int adb_lseek(int fd, int pos, int where)
404{
405 return lseek(fd, pos, where);
406}
407#undef lseek
408#define lseek ___xxx_lseek
409
410static __inline__ int adb_unlink(const char* path)
411{
412 return unlink(path);
413}
414#undef unlink
415#define unlink ___xxx_unlink
416
417static __inline__ int adb_creat(const char* path, int mode)
418{
Kenny Root73167412012-10-12 15:26:45 -0700419 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200421 if ( fd < 0 )
422 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423
424 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200425 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426}
427#undef creat
428#define creat ___xxx_creat
429
430static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
431{
Benoit Goby95ef8282011-02-01 18:57:41 -0800432 int fd;
433
Kenny Root73167412012-10-12 15:26:45 -0700434 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800435 if (fd >= 0)
436 close_on_exec(fd);
437
438 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439}
440
441#undef accept
442#define accept ___xxx_accept
443
444#define unix_read adb_read
445#define unix_write adb_write
446#define unix_close adb_close
447
448typedef pthread_t adb_thread_t;
449
450typedef void* (*adb_thread_func_t)( void* arg );
451
452static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
453{
454 pthread_attr_t attr;
455
456 pthread_attr_init (&attr);
457 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
458
459 return pthread_create( pthread, &attr, start, arg );
460}
461
462static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
463{
464 int opt = bufsize;
465 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
466}
467
468static __inline__ void disable_tcp_nagle(int fd)
469{
470 int on = 1;
471 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
472}
473
Spencer Lowf055c192015-01-25 14:40:16 -0800474static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
475{
476 return setsockopt( fd, level, optname, optval, optlen );
477}
478
479#undef setsockopt
480#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481
482static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
483{
484 return socketpair( d, type, protocol, sv );
485}
486
487static __inline__ int adb_socketpair( int sv[2] )
488{
489 int rc;
490
491 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
492 if (rc < 0)
493 return -1;
494
495 close_on_exec( sv[0] );
496 close_on_exec( sv[1] );
497 return 0;
498}
499
500#undef socketpair
501#define socketpair ___xxx_socketpair
502
503static __inline__ void adb_sleep_ms( int mseconds )
504{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200505 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506}
507
508static __inline__ int adb_mkdir(const char* path, int mode)
509{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200510 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511}
512#undef mkdir
513#define mkdir ___xxx_mkdir
514
515static __inline__ void adb_sysdeps_init(void)
516{
517}
518
519static __inline__ char* adb_dirstart(const char* path)
520{
521 return strchr(path, '/');
522}
523
524static __inline__ char* adb_dirstop(const char* path)
525{
526 return strrchr(path, '/');
527}
528
529static __inline__ int adb_is_absolute_host_path( const char* path )
530{
531 return path[0] == '/';
532}
533
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700534static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
535{
536 return strtok_r(str, delim, saveptr);
537}
leozwangcbf02672014-08-15 09:51:27 -0700538
539static __inline__ unsigned long adb_thread_id()
540{
leozwang298b6c72014-08-22 15:35:47 -0700541 return (unsigned long)pthread_self();
leozwangcbf02672014-08-15 09:51:27 -0700542}
543
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700544#undef strtok_r
545#define strtok_r ___xxx_strtok_r
546
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547#endif /* !_WIN32 */
548
Dan Albert818fb4b2015-02-18 00:18:25 -0800549#ifdef __cplusplus
550}
551#endif
552
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553#endif /* _ADB_SYSDEPS_H */