blob: 086dd61e9b957567e93ececfb47293888e982c38 [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
42#define OS_PATH_SEPARATOR '\\'
43#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070044#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
46typedef CRITICAL_SECTION adb_mutex_t;
47
48#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
49
50/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070051/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#define ADB_MUTEX(x) extern adb_mutex_t x;
53#include "mutex_list.h"
54
55extern void adb_sysdeps_init(void);
56
57static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
58{
59 EnterCriticalSection( lock );
60}
61
62static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
63{
64 LeaveCriticalSection( lock );
65}
66
67typedef struct { unsigned tid; } adb_thread_t;
68
69typedef void* (*adb_thread_func_t)(void* arg);
70
71typedef void (*win_thread_func_t)(void* arg);
72
73static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
74{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020075 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
76 if (thread->tid == (unsigned)-1L) {
77 return -1;
78 }
79 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080}
81
leozwangcbf02672014-08-15 09:51:27 -070082static __inline__ unsigned long adb_thread_id()
83{
84 return GetCurrentThreadId();
85}
86
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087static __inline__ void close_on_exec(int fd)
88{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020089 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090}
91
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092#define lstat stat /* no symlinks on Win32 */
93
94#define S_ISLNK(m) 0 /* no symlinks on Win32 */
95
96static __inline__ int adb_unlink(const char* path)
97{
98 int rc = unlink(path);
99
100 if (rc == -1 && errno == EACCES) {
101 /* unlink returns EACCES when the file is read-only, so we first */
102 /* try to make it writable, then unlink again... */
103 rc = chmod(path, _S_IREAD|_S_IWRITE );
104 if (rc == 0)
105 rc = unlink(path);
106 }
107 return rc;
108}
109#undef unlink
110#define unlink ___xxx_unlink
111
112static __inline__ int adb_mkdir(const char* path, int mode)
113{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800114 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115}
116#undef mkdir
117#define mkdir ___xxx_mkdir
118
119extern int adb_open(const char* path, int options);
120extern int adb_creat(const char* path, int mode);
121extern int adb_read(int fd, void* buf, int len);
122extern int adb_write(int fd, const void* buf, int len);
123extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400124extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125extern int adb_close(int fd);
126
127static __inline__ int unix_close(int fd)
128{
129 return close(fd);
130}
131#undef close
132#define close ____xxx_close
133
134static __inline__ int unix_read(int fd, void* buf, size_t len)
135{
136 return read(fd, buf, len);
137}
138#undef read
139#define read ___xxx_read
140
141static __inline__ int unix_write(int fd, const void* buf, size_t len)
142{
143 return write(fd, buf, len);
144}
145#undef write
146#define write ___xxx_write
147
148static __inline__ int adb_open_mode(const char* path, int options, int mode)
149{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200150 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151}
152
153static __inline__ int unix_open(const char* path, int options,...)
154{
155 if ((options & O_CREAT) == 0)
156 {
157 return open(path, options);
158 }
159 else
160 {
161 int mode;
162 va_list args;
163 va_start( args, options );
164 mode = va_arg( args, int );
165 va_end( args );
166 return open(path, options, mode);
167 }
168}
169#define open ___xxx_unix_open
170
171
172/* normally provided by <cutils/misc.h> */
173extern void* load_file(const char* pathname, unsigned* psize);
174
175/* normally provided by <cutils/sockets.h> */
176extern int socket_loopback_client(int port, int type);
177extern int socket_network_client(const char *host, int port, int type);
Elliott Hughesb911cf02014-05-20 08:51:15 -0700178extern int socket_network_client_timeout(const char *host, int port, int type,
179 int timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180extern int socket_loopback_server(int port, int type);
181extern int socket_inaddr_any_server(int port, int type);
182
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200183/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
185#define FDE_READ 0x0001
186#define FDE_WRITE 0x0002
187#define FDE_ERROR 0x0004
188#define FDE_DONT_CLOSE 0x0080
189
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190typedef void (*fd_func)(int fd, unsigned events, void *userdata);
191
192fdevent *fdevent_create(int fd, fd_func func, void *arg);
193void fdevent_destroy(fdevent *fde);
194void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
195void fdevent_remove(fdevent *item);
196void fdevent_set(fdevent *fde, unsigned events);
197void fdevent_add(fdevent *fde, unsigned events);
198void fdevent_del(fdevent *fde, unsigned events);
199void fdevent_loop();
200
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201static __inline__ void adb_sleep_ms( int mseconds )
202{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200203 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204}
205
206extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
207
208#undef accept
209#define accept ___xxx_accept
210
Spencer Lowf055c192015-01-25 14:40:16 -0800211extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
212
213#undef setsockopt
214#define setsockopt ___xxx_setsockopt
215
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
217{
218 int opt = bufsize;
Spencer Lowf055c192015-01-25 14:40:16 -0800219 return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
220}
221
222static __inline__ void disable_tcp_nagle( int fd )
223{
224 int on = 1;
225 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226}
227
228extern int adb_socketpair( int sv[2] );
229
230static __inline__ char* adb_dirstart( const char* path )
231{
232 char* p = strchr(path, '/');
233 char* p2 = strchr(path, '\\');
234
235 if ( !p )
236 p = p2;
237 else if ( p2 && p2 > p )
238 p = p2;
239
240 return p;
241}
242
243static __inline__ char* adb_dirstop( const char* path )
244{
245 char* p = strrchr(path, '/');
246 char* p2 = strrchr(path, '\\');
247
248 if ( !p )
249 p = p2;
250 else if ( p2 && p2 > p )
251 p = p2;
252
253 return p;
254}
255
256static __inline__ int adb_is_absolute_host_path( const char* path )
257{
258 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
259}
260
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700261extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
262
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263#else /* !_WIN32 a.k.a. Unix */
264
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200265#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267#include <cutils/misc.h>
268#include <signal.h>
269#include <sys/wait.h>
270#include <sys/stat.h>
271#include <fcntl.h>
272
273#include <pthread.h>
274#include <unistd.h>
275#include <fcntl.h>
276#include <stdarg.h>
277#include <netinet/in.h>
278#include <netinet/tcp.h>
279#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700280#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281
Kenny Rootec90f1d2012-10-13 11:59:01 -0700282/*
283 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
284 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
285 * not already defined, then define it here.
286 */
287#ifndef TEMP_FAILURE_RETRY
288/* Used to retry syscalls that can return EINTR. */
289#define TEMP_FAILURE_RETRY(exp) ({ \
290 typeof (exp) _rc; \
291 do { \
292 _rc = (exp); \
293 } while (_rc == -1 && errno == EINTR); \
294 _rc; })
295#endif
296
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297#define OS_PATH_SEPARATOR '/'
298#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700299#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300
301typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700302
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
304#define adb_mutex_init pthread_mutex_init
305#define adb_mutex_lock pthread_mutex_lock
306#define adb_mutex_unlock pthread_mutex_unlock
307#define adb_mutex_destroy pthread_mutex_destroy
308
JP Abgrall408fa572011-03-16 15:57:42 -0700309#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310
311#define adb_cond_t pthread_cond_t
312#define adb_cond_init pthread_cond_init
313#define adb_cond_wait pthread_cond_wait
314#define adb_cond_broadcast pthread_cond_broadcast
315#define adb_cond_signal pthread_cond_signal
316#define adb_cond_destroy pthread_cond_destroy
317
JP Abgrall408fa572011-03-16 15:57:42 -0700318/* declare all mutexes */
319#define ADB_MUTEX(x) extern adb_mutex_t x;
320#include "mutex_list.h"
321
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322static __inline__ void close_on_exec(int fd)
323{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200324 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325}
326
327static __inline__ int unix_open(const char* path, int options,...)
328{
329 if ((options & O_CREAT) == 0)
330 {
Kenny Root73167412012-10-12 15:26:45 -0700331 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 }
333 else
334 {
335 int mode;
336 va_list args;
337 va_start( args, options );
338 mode = va_arg( args, int );
339 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700340 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 }
342}
343
344static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
345{
Kenny Root73167412012-10-12 15:26:45 -0700346 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347}
348
349
350static __inline__ int adb_open( const char* pathname, int options )
351{
Kenny Root73167412012-10-12 15:26:45 -0700352 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 if (fd < 0)
354 return -1;
355 close_on_exec( fd );
356 return fd;
357}
358#undef open
359#define open ___xxx_open
360
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400361static __inline__ int adb_shutdown(int fd)
362{
363 return shutdown(fd, SHUT_RDWR);
364}
365#undef shutdown
366#define shutdown ____xxx_shutdown
367
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368static __inline__ int adb_close(int fd)
369{
370 return close(fd);
371}
372#undef close
373#define close ____xxx_close
374
375
376static __inline__ int adb_read(int fd, void* buf, size_t len)
377{
Kenny Root73167412012-10-12 15:26:45 -0700378 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379}
380
381#undef read
382#define read ___xxx_read
383
384static __inline__ int adb_write(int fd, const void* buf, size_t len)
385{
Kenny Root73167412012-10-12 15:26:45 -0700386 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387}
388#undef write
389#define write ___xxx_write
390
391static __inline__ int adb_lseek(int fd, int pos, int where)
392{
393 return lseek(fd, pos, where);
394}
395#undef lseek
396#define lseek ___xxx_lseek
397
398static __inline__ int adb_unlink(const char* path)
399{
400 return unlink(path);
401}
402#undef unlink
403#define unlink ___xxx_unlink
404
405static __inline__ int adb_creat(const char* path, int mode)
406{
Kenny Root73167412012-10-12 15:26:45 -0700407 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200409 if ( fd < 0 )
410 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411
412 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200413 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414}
415#undef creat
416#define creat ___xxx_creat
417
418static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
419{
Benoit Goby95ef8282011-02-01 18:57:41 -0800420 int fd;
421
Kenny Root73167412012-10-12 15:26:45 -0700422 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800423 if (fd >= 0)
424 close_on_exec(fd);
425
426 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427}
428
429#undef accept
430#define accept ___xxx_accept
431
432#define unix_read adb_read
433#define unix_write adb_write
434#define unix_close adb_close
435
436typedef pthread_t adb_thread_t;
437
438typedef void* (*adb_thread_func_t)( void* arg );
439
440static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
441{
442 pthread_attr_t attr;
443
444 pthread_attr_init (&attr);
445 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
446
447 return pthread_create( pthread, &attr, start, arg );
448}
449
450static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
451{
452 int opt = bufsize;
453 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
454}
455
456static __inline__ void disable_tcp_nagle(int fd)
457{
458 int on = 1;
459 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
460}
461
Spencer Lowf055c192015-01-25 14:40:16 -0800462static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
463{
464 return setsockopt( fd, level, optname, optval, optlen );
465}
466
467#undef setsockopt
468#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
470static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
471{
472 return socketpair( d, type, protocol, sv );
473}
474
475static __inline__ int adb_socketpair( int sv[2] )
476{
477 int rc;
478
479 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
480 if (rc < 0)
481 return -1;
482
483 close_on_exec( sv[0] );
484 close_on_exec( sv[1] );
485 return 0;
486}
487
488#undef socketpair
489#define socketpair ___xxx_socketpair
490
491static __inline__ void adb_sleep_ms( int mseconds )
492{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200493 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494}
495
496static __inline__ int adb_mkdir(const char* path, int mode)
497{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200498 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499}
500#undef mkdir
501#define mkdir ___xxx_mkdir
502
503static __inline__ void adb_sysdeps_init(void)
504{
505}
506
507static __inline__ char* adb_dirstart(const char* path)
508{
509 return strchr(path, '/');
510}
511
512static __inline__ char* adb_dirstop(const char* path)
513{
514 return strrchr(path, '/');
515}
516
517static __inline__ int adb_is_absolute_host_path( const char* path )
518{
519 return path[0] == '/';
520}
521
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700522static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
523{
524 return strtok_r(str, delim, saveptr);
525}
leozwangcbf02672014-08-15 09:51:27 -0700526
527static __inline__ unsigned long adb_thread_id()
528{
leozwang298b6c72014-08-22 15:35:47 -0700529 return (unsigned long)pthread_self();
leozwangcbf02672014-08-15 09:51:27 -0700530}
531
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700532#undef strtok_r
533#define strtok_r ___xxx_strtok_r
534
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535#endif /* !_WIN32 */
536
537#endif /* _ADB_SYSDEPS_H */