blob: 304a61327ee757e5a90bed4917fb437b36ae8880 [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
92extern void disable_tcp_nagle(int fd);
93
94#define lstat stat /* no symlinks on Win32 */
95
96#define S_ISLNK(m) 0 /* no symlinks on Win32 */
97
98static __inline__ int adb_unlink(const char* path)
99{
100 int rc = unlink(path);
101
102 if (rc == -1 && errno == EACCES) {
103 /* unlink returns EACCES when the file is read-only, so we first */
104 /* try to make it writable, then unlink again... */
105 rc = chmod(path, _S_IREAD|_S_IWRITE );
106 if (rc == 0)
107 rc = unlink(path);
108 }
109 return rc;
110}
111#undef unlink
112#define unlink ___xxx_unlink
113
114static __inline__ int adb_mkdir(const char* path, int mode)
115{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800116 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117}
118#undef mkdir
119#define mkdir ___xxx_mkdir
120
121extern int adb_open(const char* path, int options);
122extern int adb_creat(const char* path, int mode);
123extern int adb_read(int fd, void* buf, int len);
124extern int adb_write(int fd, const void* buf, int len);
125extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400126extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127extern int adb_close(int fd);
128
129static __inline__ int unix_close(int fd)
130{
131 return close(fd);
132}
133#undef close
134#define close ____xxx_close
135
136static __inline__ int unix_read(int fd, void* buf, size_t len)
137{
138 return read(fd, buf, len);
139}
140#undef read
141#define read ___xxx_read
142
143static __inline__ int unix_write(int fd, const void* buf, size_t len)
144{
145 return write(fd, buf, len);
146}
147#undef write
148#define write ___xxx_write
149
150static __inline__ int adb_open_mode(const char* path, int options, int mode)
151{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200152 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153}
154
155static __inline__ int unix_open(const char* path, int options,...)
156{
157 if ((options & O_CREAT) == 0)
158 {
159 return open(path, options);
160 }
161 else
162 {
163 int mode;
164 va_list args;
165 va_start( args, options );
166 mode = va_arg( args, int );
167 va_end( args );
168 return open(path, options, mode);
169 }
170}
171#define open ___xxx_unix_open
172
173
174/* normally provided by <cutils/misc.h> */
175extern void* load_file(const char* pathname, unsigned* psize);
176
177/* normally provided by <cutils/sockets.h> */
178extern int socket_loopback_client(int port, int type);
179extern int socket_network_client(const char *host, int port, int type);
Elliott Hughesb911cf02014-05-20 08:51:15 -0700180extern int socket_network_client_timeout(const char *host, int port, int type,
181 int timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182extern int socket_loopback_server(int port, int type);
183extern int socket_inaddr_any_server(int port, int type);
184
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200185/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
187#define FDE_READ 0x0001
188#define FDE_WRITE 0x0002
189#define FDE_ERROR 0x0004
190#define FDE_DONT_CLOSE 0x0080
191
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192typedef void (*fd_func)(int fd, unsigned events, void *userdata);
193
194fdevent *fdevent_create(int fd, fd_func func, void *arg);
195void fdevent_destroy(fdevent *fde);
196void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
197void fdevent_remove(fdevent *item);
198void fdevent_set(fdevent *fde, unsigned events);
199void fdevent_add(fdevent *fde, unsigned events);
200void fdevent_del(fdevent *fde, unsigned events);
201void fdevent_loop();
202
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203static __inline__ void adb_sleep_ms( int mseconds )
204{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200205 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206}
207
208extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
209
210#undef accept
211#define accept ___xxx_accept
212
213static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
214{
215 int opt = bufsize;
216 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
217}
218
219extern int adb_socketpair( int sv[2] );
220
221static __inline__ char* adb_dirstart( const char* path )
222{
223 char* p = strchr(path, '/');
224 char* p2 = strchr(path, '\\');
225
226 if ( !p )
227 p = p2;
228 else if ( p2 && p2 > p )
229 p = p2;
230
231 return p;
232}
233
234static __inline__ char* adb_dirstop( const char* path )
235{
236 char* p = strrchr(path, '/');
237 char* p2 = strrchr(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__ int adb_is_absolute_host_path( const char* path )
248{
249 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
250}
251
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700252extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
253
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254#else /* !_WIN32 a.k.a. Unix */
255
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200256#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258#include <cutils/misc.h>
259#include <signal.h>
260#include <sys/wait.h>
261#include <sys/stat.h>
262#include <fcntl.h>
263
264#include <pthread.h>
265#include <unistd.h>
266#include <fcntl.h>
267#include <stdarg.h>
268#include <netinet/in.h>
269#include <netinet/tcp.h>
270#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700271#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272
Kenny Rootec90f1d2012-10-13 11:59:01 -0700273/*
274 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
275 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
276 * not already defined, then define it here.
277 */
278#ifndef TEMP_FAILURE_RETRY
279/* Used to retry syscalls that can return EINTR. */
280#define TEMP_FAILURE_RETRY(exp) ({ \
281 typeof (exp) _rc; \
282 do { \
283 _rc = (exp); \
284 } while (_rc == -1 && errno == EINTR); \
285 _rc; })
286#endif
287
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288#define OS_PATH_SEPARATOR '/'
289#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700290#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291
292typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700293
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
295#define adb_mutex_init pthread_mutex_init
296#define adb_mutex_lock pthread_mutex_lock
297#define adb_mutex_unlock pthread_mutex_unlock
298#define adb_mutex_destroy pthread_mutex_destroy
299
JP Abgrall408fa572011-03-16 15:57:42 -0700300#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301
302#define adb_cond_t pthread_cond_t
303#define adb_cond_init pthread_cond_init
304#define adb_cond_wait pthread_cond_wait
305#define adb_cond_broadcast pthread_cond_broadcast
306#define adb_cond_signal pthread_cond_signal
307#define adb_cond_destroy pthread_cond_destroy
308
JP Abgrall408fa572011-03-16 15:57:42 -0700309/* declare all mutexes */
310#define ADB_MUTEX(x) extern adb_mutex_t x;
311#include "mutex_list.h"
312
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313static __inline__ void close_on_exec(int fd)
314{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200315 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316}
317
318static __inline__ int unix_open(const char* path, int options,...)
319{
320 if ((options & O_CREAT) == 0)
321 {
Kenny Root73167412012-10-12 15:26:45 -0700322 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 }
324 else
325 {
326 int mode;
327 va_list args;
328 va_start( args, options );
329 mode = va_arg( args, int );
330 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700331 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 }
333}
334
335static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
336{
Kenny Root73167412012-10-12 15:26:45 -0700337 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338}
339
340
341static __inline__ int adb_open( const char* pathname, int options )
342{
Kenny Root73167412012-10-12 15:26:45 -0700343 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 if (fd < 0)
345 return -1;
346 close_on_exec( fd );
347 return fd;
348}
349#undef open
350#define open ___xxx_open
351
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400352static __inline__ int adb_shutdown(int fd)
353{
354 return shutdown(fd, SHUT_RDWR);
355}
356#undef shutdown
357#define shutdown ____xxx_shutdown
358
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359static __inline__ int adb_close(int fd)
360{
361 return close(fd);
362}
363#undef close
364#define close ____xxx_close
365
366
367static __inline__ int adb_read(int fd, void* buf, size_t len)
368{
Kenny Root73167412012-10-12 15:26:45 -0700369 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370}
371
372#undef read
373#define read ___xxx_read
374
375static __inline__ int adb_write(int fd, const void* buf, size_t len)
376{
Kenny Root73167412012-10-12 15:26:45 -0700377 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378}
379#undef write
380#define write ___xxx_write
381
382static __inline__ int adb_lseek(int fd, int pos, int where)
383{
384 return lseek(fd, pos, where);
385}
386#undef lseek
387#define lseek ___xxx_lseek
388
389static __inline__ int adb_unlink(const char* path)
390{
391 return unlink(path);
392}
393#undef unlink
394#define unlink ___xxx_unlink
395
396static __inline__ int adb_creat(const char* path, int mode)
397{
Kenny Root73167412012-10-12 15:26:45 -0700398 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200400 if ( fd < 0 )
401 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
403 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200404 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405}
406#undef creat
407#define creat ___xxx_creat
408
409static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
410{
Benoit Goby95ef8282011-02-01 18:57:41 -0800411 int fd;
412
Kenny Root73167412012-10-12 15:26:45 -0700413 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800414 if (fd >= 0)
415 close_on_exec(fd);
416
417 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418}
419
420#undef accept
421#define accept ___xxx_accept
422
423#define unix_read adb_read
424#define unix_write adb_write
425#define unix_close adb_close
426
427typedef pthread_t adb_thread_t;
428
429typedef void* (*adb_thread_func_t)( void* arg );
430
431static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
432{
433 pthread_attr_t attr;
434
435 pthread_attr_init (&attr);
436 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
437
438 return pthread_create( pthread, &attr, start, arg );
439}
440
441static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
442{
443 int opt = bufsize;
444 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
445}
446
447static __inline__ void disable_tcp_nagle(int fd)
448{
449 int on = 1;
450 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
451}
452
453
454static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
455{
456 return socketpair( d, type, protocol, sv );
457}
458
459static __inline__ int adb_socketpair( int sv[2] )
460{
461 int rc;
462
463 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
464 if (rc < 0)
465 return -1;
466
467 close_on_exec( sv[0] );
468 close_on_exec( sv[1] );
469 return 0;
470}
471
472#undef socketpair
473#define socketpair ___xxx_socketpair
474
475static __inline__ void adb_sleep_ms( int mseconds )
476{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200477 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478}
479
480static __inline__ int adb_mkdir(const char* path, int mode)
481{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200482 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483}
484#undef mkdir
485#define mkdir ___xxx_mkdir
486
487static __inline__ void adb_sysdeps_init(void)
488{
489}
490
491static __inline__ char* adb_dirstart(const char* path)
492{
493 return strchr(path, '/');
494}
495
496static __inline__ char* adb_dirstop(const char* path)
497{
498 return strrchr(path, '/');
499}
500
501static __inline__ int adb_is_absolute_host_path( const char* path )
502{
503 return path[0] == '/';
504}
505
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700506static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
507{
508 return strtok_r(str, delim, saveptr);
509}
leozwangcbf02672014-08-15 09:51:27 -0700510
511static __inline__ unsigned long adb_thread_id()
512{
leozwang298b6c72014-08-22 15:35:47 -0700513 return (unsigned long)pthread_self();
leozwangcbf02672014-08-15 09:51:27 -0700514}
515
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700516#undef strtok_r
517#define strtok_r ___xxx_strtok_r
518
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519#endif /* !_WIN32 */
520
521#endif /* _ADB_SYSDEPS_H */