blob: 0aa1570987a62040bdcf69fc9c82b0748f4de3e5 [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
57#define OS_PATH_SEPARATOR '\\'
58#define OS_PATH_SEPARATOR_STR "\\"
Benoit Goby2cc19e42012-04-12 12:23:49 -070059#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060
61typedef CRITICAL_SECTION adb_mutex_t;
62
63#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
64
65/* declare all mutexes */
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070066/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067#define ADB_MUTEX(x) extern adb_mutex_t x;
68#include "mutex_list.h"
69
70extern void adb_sysdeps_init(void);
71
72static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
73{
74 EnterCriticalSection( lock );
75}
76
77static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
78{
79 LeaveCriticalSection( lock );
80}
81
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082typedef void* (*adb_thread_func_t)(void* arg);
83
84typedef void (*win_thread_func_t)(void* arg);
85
Elliott Hughesf2517142015-05-05 13:41:21 -070086static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) {
Elliott Hughes30f2e2b2015-05-05 14:34:41 -070087 uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg);
88 return (tid != static_cast<uintptr_t>(-1L));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080089}
90
leozwang1be54622014-08-15 09:51:27 -070091static __inline__ unsigned long adb_thread_id()
92{
93 return GetCurrentThreadId();
94}
95
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080096static __inline__ void close_on_exec(int fd)
97{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020098 /* nothing really */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099}
100
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101#define lstat stat /* no symlinks on Win32 */
102
103#define S_ISLNK(m) 0 /* no symlinks on Win32 */
104
105static __inline__ int adb_unlink(const char* path)
106{
107 int rc = unlink(path);
108
109 if (rc == -1 && errno == EACCES) {
110 /* unlink returns EACCES when the file is read-only, so we first */
111 /* try to make it writable, then unlink again... */
112 rc = chmod(path, _S_IREAD|_S_IWRITE );
113 if (rc == 0)
114 rc = unlink(path);
115 }
116 return rc;
117}
118#undef unlink
119#define unlink ___xxx_unlink
120
121static __inline__ int adb_mkdir(const char* path, int mode)
122{
JP Abgrall1f501f32011-02-23 18:44:39 -0800123 return _mkdir(path);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800124}
125#undef mkdir
126#define mkdir ___xxx_mkdir
127
128extern int adb_open(const char* path, int options);
129extern int adb_creat(const char* path, int mode);
130extern int adb_read(int fd, void* buf, int len);
131extern int adb_write(int fd, const void* buf, int len);
132extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood81ffe172009-10-11 23:04:18 -0400133extern int adb_shutdown(int fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800134extern int adb_close(int fd);
135
136static __inline__ int unix_close(int fd)
137{
138 return close(fd);
139}
140#undef close
141#define close ____xxx_close
142
Spencer Lowbeb61982015-03-01 15:06:21 -0800143extern int unix_read(int fd, void* buf, size_t len);
144
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800145#undef read
146#define read ___xxx_read
147
148static __inline__ int unix_write(int fd, const void* buf, size_t len)
149{
150 return write(fd, buf, len);
151}
152#undef write
153#define write ___xxx_write
154
155static __inline__ int adb_open_mode(const char* path, int options, int mode)
156{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200157 return adb_open(path, options);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800158}
159
160static __inline__ int unix_open(const char* path, int options,...)
161{
162 if ((options & O_CREAT) == 0)
163 {
164 return open(path, options);
165 }
166 else
167 {
168 int mode;
169 va_list args;
170 va_start( args, options );
171 mode = va_arg( args, int );
172 va_end( args );
173 return open(path, options, mode);
174 }
175}
176#define open ___xxx_unix_open
177
178
179/* normally provided by <cutils/misc.h> */
180extern void* load_file(const char* pathname, unsigned* psize);
181
182/* normally provided by <cutils/sockets.h> */
183extern int socket_loopback_client(int port, int type);
184extern int socket_network_client(const char *host, int port, int type);
Elliott Hughes4fa7db02014-05-20 08:51:15 -0700185extern int socket_network_client_timeout(const char *host, int port, int type,
186 int timeout);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800187extern int socket_loopback_server(int port, int type);
188extern int socket_inaddr_any_server(int port, int type);
189
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200190/* normally provided by "fdevent.h" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191
192#define FDE_READ 0x0001
193#define FDE_WRITE 0x0002
194#define FDE_ERROR 0x0004
195#define FDE_DONT_CLOSE 0x0080
196
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800197typedef void (*fd_func)(int fd, unsigned events, void *userdata);
198
199fdevent *fdevent_create(int fd, fd_func func, void *arg);
200void fdevent_destroy(fdevent *fde);
201void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
202void fdevent_remove(fdevent *item);
203void fdevent_set(fdevent *fde, unsigned events);
204void fdevent_add(fdevent *fde, unsigned events);
205void fdevent_del(fdevent *fde, unsigned events);
206void fdevent_loop();
207
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208static __inline__ void adb_sleep_ms( int mseconds )
209{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200210 Sleep( mseconds );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800211}
212
213extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
214
215#undef accept
216#define accept ___xxx_accept
217
Spencer Low31aafa62015-01-25 14:40:16 -0800218extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
219
220#undef setsockopt
221#define setsockopt ___xxx_setsockopt
222
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
224{
225 int opt = bufsize;
Spencer Low31aafa62015-01-25 14:40:16 -0800226 return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
227}
228
229static __inline__ void disable_tcp_nagle( int fd )
230{
231 int on = 1;
232 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233}
234
235extern int adb_socketpair( int sv[2] );
236
237static __inline__ char* adb_dirstart( const char* path )
238{
239 char* p = strchr(path, '/');
240 char* p2 = strchr(path, '\\');
241
242 if ( !p )
243 p = p2;
244 else if ( p2 && p2 > p )
245 p = p2;
246
247 return p;
248}
249
250static __inline__ char* adb_dirstop( const char* path )
251{
252 char* p = strrchr(path, '/');
253 char* p2 = strrchr(path, '\\');
254
255 if ( !p )
256 p = p2;
257 else if ( p2 && p2 > p )
258 p = p2;
259
260 return p;
261}
262
263static __inline__ int adb_is_absolute_host_path( const char* path )
264{
265 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
266}
267
268#else /* !_WIN32 a.k.a. Unix */
269
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200270#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800271#include <cutils/sockets.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272#include <cutils/misc.h>
273#include <signal.h>
274#include <sys/wait.h>
275#include <sys/stat.h>
276#include <fcntl.h>
277
278#include <pthread.h>
279#include <unistd.h>
280#include <fcntl.h>
281#include <stdarg.h>
282#include <netinet/in.h>
283#include <netinet/tcp.h>
284#include <string.h>
Kenny Rooteac025c2012-10-12 15:26:45 -0700285#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800286
287#define OS_PATH_SEPARATOR '/'
288#define OS_PATH_SEPARATOR_STR "/"
Benoit Goby2cc19e42012-04-12 12:23:49 -0700289#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800290
291typedef pthread_mutex_t adb_mutex_t;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700292
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
294#define adb_mutex_init pthread_mutex_init
295#define adb_mutex_lock pthread_mutex_lock
296#define adb_mutex_unlock pthread_mutex_unlock
297#define adb_mutex_destroy pthread_mutex_destroy
298
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700299#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300
301#define adb_cond_t pthread_cond_t
302#define adb_cond_init pthread_cond_init
303#define adb_cond_wait pthread_cond_wait
304#define adb_cond_broadcast pthread_cond_broadcast
305#define adb_cond_signal pthread_cond_signal
306#define adb_cond_destroy pthread_cond_destroy
307
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700308/* declare all mutexes */
309#define ADB_MUTEX(x) extern adb_mutex_t x;
310#include "mutex_list.h"
311
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800312static __inline__ void close_on_exec(int fd)
313{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200314 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800315}
316
317static __inline__ int unix_open(const char* path, int options,...)
318{
319 if ((options & O_CREAT) == 0)
320 {
Kenny Rooteac025c2012-10-12 15:26:45 -0700321 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322 }
323 else
324 {
325 int mode;
326 va_list args;
327 va_start( args, options );
328 mode = va_arg( args, int );
329 va_end( args );
Kenny Rooteac025c2012-10-12 15:26:45 -0700330 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800331 }
332}
333
334static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
335{
Kenny Rooteac025c2012-10-12 15:26:45 -0700336 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337}
338
339
340static __inline__ int adb_open( const char* pathname, int options )
341{
Kenny Rooteac025c2012-10-12 15:26:45 -0700342 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800343 if (fd < 0)
344 return -1;
345 close_on_exec( fd );
346 return fd;
347}
348#undef open
349#define open ___xxx_open
350
Mike Lockwood81ffe172009-10-11 23:04:18 -0400351static __inline__ int adb_shutdown(int fd)
352{
353 return shutdown(fd, SHUT_RDWR);
354}
355#undef shutdown
356#define shutdown ____xxx_shutdown
357
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800358static __inline__ int adb_close(int fd)
359{
360 return close(fd);
361}
362#undef close
363#define close ____xxx_close
364
365
366static __inline__ int adb_read(int fd, void* buf, size_t len)
367{
Kenny Rooteac025c2012-10-12 15:26:45 -0700368 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800369}
370
371#undef read
372#define read ___xxx_read
373
374static __inline__ int adb_write(int fd, const void* buf, size_t len)
375{
Kenny Rooteac025c2012-10-12 15:26:45 -0700376 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800377}
378#undef write
379#define write ___xxx_write
380
381static __inline__ int adb_lseek(int fd, int pos, int where)
382{
383 return lseek(fd, pos, where);
384}
385#undef lseek
386#define lseek ___xxx_lseek
387
388static __inline__ int adb_unlink(const char* path)
389{
390 return unlink(path);
391}
392#undef unlink
393#define unlink ___xxx_unlink
394
395static __inline__ int adb_creat(const char* path, int mode)
396{
Kenny Rooteac025c2012-10-12 15:26:45 -0700397 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200399 if ( fd < 0 )
400 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800401
402 close_on_exec(fd);
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200403 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404}
405#undef creat
406#define creat ___xxx_creat
407
408static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
409{
Benoit Gobyf4ded742011-02-01 18:57:41 -0800410 int fd;
411
Kenny Rooteac025c2012-10-12 15:26:45 -0700412 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Gobyf4ded742011-02-01 18:57:41 -0800413 if (fd >= 0)
414 close_on_exec(fd);
415
416 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417}
418
419#undef accept
420#define accept ___xxx_accept
421
422#define unix_read adb_read
423#define unix_write adb_write
424#define unix_close adb_close
425
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800426typedef void* (*adb_thread_func_t)( void* arg );
427
Elliott Hughesf2517142015-05-05 13:41:21 -0700428static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) {
429 pthread_attr_t attr;
430 pthread_attr_init(&attr);
431 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800432
Elliott Hughesf2517142015-05-05 13:41:21 -0700433 pthread_t thread;
434 errno = pthread_create(&thread, &attr, start, arg);
435 return (errno == 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800436}
437
438static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
439{
440 int opt = bufsize;
441 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
442}
443
444static __inline__ void disable_tcp_nagle(int fd)
445{
446 int on = 1;
447 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
448}
449
Spencer Low31aafa62015-01-25 14:40:16 -0800450static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
451{
452 return setsockopt( fd, level, optname, optval, optlen );
453}
454
455#undef setsockopt
456#define setsockopt ___xxx_setsockopt
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800457
458static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
459{
460 return socketpair( d, type, protocol, sv );
461}
462
463static __inline__ int adb_socketpair( int sv[2] )
464{
465 int rc;
466
467 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
468 if (rc < 0)
469 return -1;
470
471 close_on_exec( sv[0] );
472 close_on_exec( sv[1] );
473 return 0;
474}
475
476#undef socketpair
477#define socketpair ___xxx_socketpair
478
479static __inline__ void adb_sleep_ms( int mseconds )
480{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200481 usleep( mseconds*1000 );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482}
483
484static __inline__ int adb_mkdir(const char* path, int mode)
485{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200486 return mkdir(path, mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800487}
488#undef mkdir
489#define mkdir ___xxx_mkdir
490
491static __inline__ void adb_sysdeps_init(void)
492{
493}
494
495static __inline__ char* adb_dirstart(const char* path)
496{
497 return strchr(path, '/');
498}
499
500static __inline__ char* adb_dirstop(const char* path)
501{
502 return strrchr(path, '/');
503}
504
505static __inline__ int adb_is_absolute_host_path( const char* path )
506{
507 return path[0] == '/';
508}
509
leozwang1be54622014-08-15 09:51:27 -0700510static __inline__ unsigned long adb_thread_id()
511{
leozwang9d23b532014-08-22 15:35:47 -0700512 return (unsigned long)pthread_self();
leozwang1be54622014-08-15 09:51:27 -0700513}
514
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800515#endif /* !_WIN32 */
516
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800517#endif /* _ADB_SYSDEPS_H */