blob: 2ad28fa1d4541f1c1fb0aff313d4f4501c99c285 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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
Dan Albert66a91b02015-02-24 21:26:58 -080027/*
28 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
29 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
30 * not already defined, then define it here.
31 */
32#ifndef TEMP_FAILURE_RETRY
33/* Used to retry syscalls that can return EINTR. */
34#define TEMP_FAILURE_RETRY(exp) ({ \
35 typeof (exp) _rc; \
36 do { \
37 _rc = (exp); \
38 } while (_rc == -1 && errno == EINTR); \
39 _rc; })
40#endif
41
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#ifdef _WIN32
43
Dan Albertbbbbea62014-11-24 23:34:35 -080044#include <ctype.h>
45#include <direct.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <io.h>
49#include <process.h>
50#include <sys/stat.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051#include <winsock2.h>
Stephen Hinesb1170852014-10-01 17:37:06 -070052#include <windows.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053#include <ws2tcpip.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080054
55#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080056
Dan Albert07c78812015-02-18 00:18:25 -080057#ifdef __cplusplus
58extern "C" {
59#endif
60
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080061#define OS_PATH_SEPARATOR '\\'
62#define OS_PATH_SEPARATOR_STR "\\"
Benoit Goby2cc19e42012-04-12 12:23:49 -070063#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064
65typedef CRITICAL_SECTION adb_mutex_t;
66
67#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
68
69/* declare all mutexes */
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070070/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080071#define ADB_MUTEX(x) extern adb_mutex_t x;
72#include "mutex_list.h"
73
74extern void adb_sysdeps_init(void);
75
76static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
77{
78 EnterCriticalSection( lock );
79}
80
81static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
82{
83 LeaveCriticalSection( lock );
84}
85
86typedef struct { unsigned tid; } adb_thread_t;
87
88typedef void* (*adb_thread_func_t)(void* arg);
89
90typedef void (*win_thread_func_t)(void* arg);
91
92static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
93{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020094 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
95 if (thread->tid == (unsigned)-1L) {
96 return -1;
97 }
98 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099}
100
leozwang1be54622014-08-15 09:51:27 -0700101static __inline__ unsigned long adb_thread_id()
102{
103 return GetCurrentThreadId();
104}
105
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800106static __inline__ void close_on_exec(int fd)
107{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200108 /* nothing really */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800109}
110
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800111#define lstat stat /* no symlinks on Win32 */
112
113#define S_ISLNK(m) 0 /* no symlinks on Win32 */
114
115static __inline__ int adb_unlink(const char* path)
116{
117 int rc = unlink(path);
118
119 if (rc == -1 && errno == EACCES) {
120 /* unlink returns EACCES when the file is read-only, so we first */
121 /* try to make it writable, then unlink again... */
122 rc = chmod(path, _S_IREAD|_S_IWRITE );
123 if (rc == 0)
124 rc = unlink(path);
125 }
126 return rc;
127}
128#undef unlink
129#define unlink ___xxx_unlink
130
131static __inline__ int adb_mkdir(const char* path, int mode)
132{
JP Abgrall1f501f32011-02-23 18:44:39 -0800133 return _mkdir(path);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800134}
135#undef mkdir
136#define mkdir ___xxx_mkdir
137
138extern int adb_open(const char* path, int options);
139extern int adb_creat(const char* path, int mode);
140extern int adb_read(int fd, void* buf, int len);
141extern int adb_write(int fd, const void* buf, int len);
142extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood81ffe172009-10-11 23:04:18 -0400143extern int adb_shutdown(int fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800144extern int adb_close(int fd);
145
146static __inline__ int unix_close(int fd)
147{
148 return close(fd);
149}
150#undef close
151#define close ____xxx_close
152
Spencer Lowbeb61982015-03-01 15:06:21 -0800153extern int unix_read(int fd, void* buf, size_t len);
154
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800155#undef read
156#define read ___xxx_read
157
158static __inline__ int unix_write(int fd, const void* buf, size_t len)
159{
160 return write(fd, buf, len);
161}
162#undef write
163#define write ___xxx_write
164
165static __inline__ int adb_open_mode(const char* path, int options, int mode)
166{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200167 return adb_open(path, options);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800168}
169
170static __inline__ int unix_open(const char* path, int options,...)
171{
172 if ((options & O_CREAT) == 0)
173 {
174 return open(path, options);
175 }
176 else
177 {
178 int mode;
179 va_list args;
180 va_start( args, options );
181 mode = va_arg( args, int );
182 va_end( args );
183 return open(path, options, mode);
184 }
185}
186#define open ___xxx_unix_open
187
188
189/* normally provided by <cutils/misc.h> */
190extern void* load_file(const char* pathname, unsigned* psize);
191
192/* normally provided by <cutils/sockets.h> */
193extern int socket_loopback_client(int port, int type);
194extern int socket_network_client(const char *host, int port, int type);
Elliott Hughes4fa7db02014-05-20 08:51:15 -0700195extern int socket_network_client_timeout(const char *host, int port, int type,
196 int timeout);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800197extern int socket_loopback_server(int port, int type);
198extern int socket_inaddr_any_server(int port, int type);
199
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200200/* normally provided by "fdevent.h" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201
202#define FDE_READ 0x0001
203#define FDE_WRITE 0x0002
204#define FDE_ERROR 0x0004
205#define FDE_DONT_CLOSE 0x0080
206
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800207typedef void (*fd_func)(int fd, unsigned events, void *userdata);
208
209fdevent *fdevent_create(int fd, fd_func func, void *arg);
210void fdevent_destroy(fdevent *fde);
211void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
212void fdevent_remove(fdevent *item);
213void fdevent_set(fdevent *fde, unsigned events);
214void fdevent_add(fdevent *fde, unsigned events);
215void fdevent_del(fdevent *fde, unsigned events);
216void fdevent_loop();
217
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800218static __inline__ void adb_sleep_ms( int mseconds )
219{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200220 Sleep( mseconds );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800221}
222
223extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
224
225#undef accept
226#define accept ___xxx_accept
227
Spencer Low31aafa62015-01-25 14:40:16 -0800228extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
229
230#undef setsockopt
231#define setsockopt ___xxx_setsockopt
232
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
234{
235 int opt = bufsize;
Spencer Low31aafa62015-01-25 14:40:16 -0800236 return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
237}
238
239static __inline__ void disable_tcp_nagle( int fd )
240{
241 int on = 1;
242 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243}
244
245extern int adb_socketpair( int sv[2] );
246
247static __inline__ char* adb_dirstart( const char* path )
248{
249 char* p = strchr(path, '/');
250 char* p2 = strchr(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__ char* adb_dirstop( const char* path )
261{
262 char* p = strrchr(path, '/');
263 char* p2 = strrchr(path, '\\');
264
265 if ( !p )
266 p = p2;
267 else if ( p2 && p2 > p )
268 p = p2;
269
270 return p;
271}
272
273static __inline__ int adb_is_absolute_host_path( const char* path )
274{
275 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
276}
277
Scott Anderson0fa59782012-06-05 17:54:27 -0700278extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
279
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800280#else /* !_WIN32 a.k.a. Unix */
281
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200282#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283#include <cutils/sockets.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800284#include <cutils/misc.h>
285#include <signal.h>
286#include <sys/wait.h>
287#include <sys/stat.h>
288#include <fcntl.h>
289
290#include <pthread.h>
291#include <unistd.h>
292#include <fcntl.h>
293#include <stdarg.h>
294#include <netinet/in.h>
295#include <netinet/tcp.h>
296#include <string.h>
Kenny Rooteac025c2012-10-12 15:26:45 -0700297#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800298
Dan Albert07c78812015-02-18 00:18:25 -0800299#ifdef __cplusplus
300extern "C" {
301#endif
302
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303#define OS_PATH_SEPARATOR '/'
304#define OS_PATH_SEPARATOR_STR "/"
Benoit Goby2cc19e42012-04-12 12:23:49 -0700305#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306
307typedef pthread_mutex_t adb_mutex_t;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700308
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
310#define adb_mutex_init pthread_mutex_init
311#define adb_mutex_lock pthread_mutex_lock
312#define adb_mutex_unlock pthread_mutex_unlock
313#define adb_mutex_destroy pthread_mutex_destroy
314
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700315#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316
317#define adb_cond_t pthread_cond_t
318#define adb_cond_init pthread_cond_init
319#define adb_cond_wait pthread_cond_wait
320#define adb_cond_broadcast pthread_cond_broadcast
321#define adb_cond_signal pthread_cond_signal
322#define adb_cond_destroy pthread_cond_destroy
323
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700324/* declare all mutexes */
325#define ADB_MUTEX(x) extern adb_mutex_t x;
326#include "mutex_list.h"
327
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800328static __inline__ void close_on_exec(int fd)
329{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200330 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800331}
332
333static __inline__ int unix_open(const char* path, int options,...)
334{
335 if ((options & O_CREAT) == 0)
336 {
Kenny Rooteac025c2012-10-12 15:26:45 -0700337 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800338 }
339 else
340 {
341 int mode;
342 va_list args;
343 va_start( args, options );
344 mode = va_arg( args, int );
345 va_end( args );
Kenny Rooteac025c2012-10-12 15:26:45 -0700346 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347 }
348}
349
350static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
351{
Kenny Rooteac025c2012-10-12 15:26:45 -0700352 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800353}
354
355
356static __inline__ int adb_open( const char* pathname, int options )
357{
Kenny Rooteac025c2012-10-12 15:26:45 -0700358 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800359 if (fd < 0)
360 return -1;
361 close_on_exec( fd );
362 return fd;
363}
364#undef open
365#define open ___xxx_open
366
Mike Lockwood81ffe172009-10-11 23:04:18 -0400367static __inline__ int adb_shutdown(int fd)
368{
369 return shutdown(fd, SHUT_RDWR);
370}
371#undef shutdown
372#define shutdown ____xxx_shutdown
373
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800374static __inline__ int adb_close(int fd)
375{
376 return close(fd);
377}
378#undef close
379#define close ____xxx_close
380
381
382static __inline__ int adb_read(int fd, void* buf, size_t len)
383{
Kenny Rooteac025c2012-10-12 15:26:45 -0700384 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800385}
386
387#undef read
388#define read ___xxx_read
389
390static __inline__ int adb_write(int fd, const void* buf, size_t len)
391{
Kenny Rooteac025c2012-10-12 15:26:45 -0700392 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800393}
394#undef write
395#define write ___xxx_write
396
397static __inline__ int adb_lseek(int fd, int pos, int where)
398{
399 return lseek(fd, pos, where);
400}
401#undef lseek
402#define lseek ___xxx_lseek
403
404static __inline__ int adb_unlink(const char* path)
405{
406 return unlink(path);
407}
408#undef unlink
409#define unlink ___xxx_unlink
410
411static __inline__ int adb_creat(const char* path, int mode)
412{
Kenny Rooteac025c2012-10-12 15:26:45 -0700413 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800414
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200415 if ( fd < 0 )
416 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417
418 close_on_exec(fd);
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200419 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420}
421#undef creat
422#define creat ___xxx_creat
423
424static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
425{
Benoit Gobyf4ded742011-02-01 18:57:41 -0800426 int fd;
427
Kenny Rooteac025c2012-10-12 15:26:45 -0700428 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Gobyf4ded742011-02-01 18:57:41 -0800429 if (fd >= 0)
430 close_on_exec(fd);
431
432 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800433}
434
435#undef accept
436#define accept ___xxx_accept
437
438#define unix_read adb_read
439#define unix_write adb_write
440#define unix_close adb_close
441
442typedef pthread_t adb_thread_t;
443
444typedef void* (*adb_thread_func_t)( void* arg );
445
446static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
447{
448 pthread_attr_t attr;
449
450 pthread_attr_init (&attr);
451 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
452
453 return pthread_create( pthread, &attr, start, arg );
454}
455
456static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
457{
458 int opt = bufsize;
459 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
460}
461
462static __inline__ void disable_tcp_nagle(int fd)
463{
464 int on = 1;
465 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
466}
467
Spencer Low31aafa62015-01-25 14:40:16 -0800468static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
469{
470 return setsockopt( fd, level, optname, optval, optlen );
471}
472
473#undef setsockopt
474#define setsockopt ___xxx_setsockopt
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800475
476static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
477{
478 return socketpair( d, type, protocol, sv );
479}
480
481static __inline__ int adb_socketpair( int sv[2] )
482{
483 int rc;
484
485 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
486 if (rc < 0)
487 return -1;
488
489 close_on_exec( sv[0] );
490 close_on_exec( sv[1] );
491 return 0;
492}
493
494#undef socketpair
495#define socketpair ___xxx_socketpair
496
497static __inline__ void adb_sleep_ms( int mseconds )
498{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200499 usleep( mseconds*1000 );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800500}
501
502static __inline__ int adb_mkdir(const char* path, int mode)
503{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200504 return mkdir(path, mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800505}
506#undef mkdir
507#define mkdir ___xxx_mkdir
508
509static __inline__ void adb_sysdeps_init(void)
510{
511}
512
513static __inline__ char* adb_dirstart(const char* path)
514{
515 return strchr(path, '/');
516}
517
518static __inline__ char* adb_dirstop(const char* path)
519{
520 return strrchr(path, '/');
521}
522
523static __inline__ int adb_is_absolute_host_path( const char* path )
524{
525 return path[0] == '/';
526}
527
Scott Anderson0fa59782012-06-05 17:54:27 -0700528static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
529{
530 return strtok_r(str, delim, saveptr);
531}
leozwang1be54622014-08-15 09:51:27 -0700532
533static __inline__ unsigned long adb_thread_id()
534{
leozwang9d23b532014-08-22 15:35:47 -0700535 return (unsigned long)pthread_self();
leozwang1be54622014-08-15 09:51:27 -0700536}
537
Scott Anderson0fa59782012-06-05 17:54:27 -0700538#undef strtok_r
539#define strtok_r ___xxx_strtok_r
540
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800541#endif /* !_WIN32 */
542
Dan Albert07c78812015-02-18 00:18:25 -0800543#ifdef __cplusplus
544}
545#endif
546
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800547#endif /* _ADB_SYSDEPS_H */