blob: cc1f839e9fbd278b0d801e31bc41d16fc1e7d657 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070030#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include <ws2tcpip.h>
32#include <process.h>
33#include <fcntl.h>
34#include <io.h>
35#include <sys/stat.h>
36#include <errno.h>
37#include <ctype.h>
Andrew Hsiehb75d6f12014-05-07 20:21:11 +080038#include <direct.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
40#define OS_PATH_SEPARATOR '\\'
41#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070042#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
44typedef CRITICAL_SECTION adb_mutex_t;
45
46#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
47
48/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070049/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#define ADB_MUTEX(x) extern adb_mutex_t x;
51#include "mutex_list.h"
52
53extern void adb_sysdeps_init(void);
54
55static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
56{
57 EnterCriticalSection( lock );
58}
59
60static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
61{
62 LeaveCriticalSection( lock );
63}
64
65typedef struct { unsigned tid; } adb_thread_t;
66
67typedef void* (*adb_thread_func_t)(void* arg);
68
69typedef void (*win_thread_func_t)(void* arg);
70
71static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
72{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020073 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
74 if (thread->tid == (unsigned)-1L) {
75 return -1;
76 }
77 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078}
79
80static __inline__ void close_on_exec(int fd)
81{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020082 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083}
84
85extern void disable_tcp_nagle(int fd);
86
87#define lstat stat /* no symlinks on Win32 */
88
89#define S_ISLNK(m) 0 /* no symlinks on Win32 */
90
91static __inline__ int adb_unlink(const char* path)
92{
93 int rc = unlink(path);
94
95 if (rc == -1 && errno == EACCES) {
96 /* unlink returns EACCES when the file is read-only, so we first */
97 /* try to make it writable, then unlink again... */
98 rc = chmod(path, _S_IREAD|_S_IWRITE );
99 if (rc == 0)
100 rc = unlink(path);
101 }
102 return rc;
103}
104#undef unlink
105#define unlink ___xxx_unlink
106
107static __inline__ int adb_mkdir(const char* path, int mode)
108{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800109 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110}
111#undef mkdir
112#define mkdir ___xxx_mkdir
113
114extern int adb_open(const char* path, int options);
115extern int adb_creat(const char* path, int mode);
116extern int adb_read(int fd, void* buf, int len);
117extern int adb_write(int fd, const void* buf, int len);
118extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400119extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120extern int adb_close(int fd);
121
122static __inline__ int unix_close(int fd)
123{
124 return close(fd);
125}
126#undef close
127#define close ____xxx_close
128
129static __inline__ int unix_read(int fd, void* buf, size_t len)
130{
131 return read(fd, buf, len);
132}
133#undef read
134#define read ___xxx_read
135
136static __inline__ int unix_write(int fd, const void* buf, size_t len)
137{
138 return write(fd, buf, len);
139}
140#undef write
141#define write ___xxx_write
142
143static __inline__ int adb_open_mode(const char* path, int options, int mode)
144{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200145 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146}
147
148static __inline__ int unix_open(const char* path, int options,...)
149{
150 if ((options & O_CREAT) == 0)
151 {
152 return open(path, options);
153 }
154 else
155 {
156 int mode;
157 va_list args;
158 va_start( args, options );
159 mode = va_arg( args, int );
160 va_end( args );
161 return open(path, options, mode);
162 }
163}
164#define open ___xxx_unix_open
165
166
167/* normally provided by <cutils/misc.h> */
168extern void* load_file(const char* pathname, unsigned* psize);
169
170/* normally provided by <cutils/sockets.h> */
171extern int socket_loopback_client(int port, int type);
172extern int socket_network_client(const char *host, int port, int type);
Elliott Hughesb911cf02014-05-20 08:51:15 -0700173extern int socket_network_client_timeout(const char *host, int port, int type,
174 int timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175extern int socket_loopback_server(int port, int type);
176extern int socket_inaddr_any_server(int port, int type);
177
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200178/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
180#define FDE_READ 0x0001
181#define FDE_WRITE 0x0002
182#define FDE_ERROR 0x0004
183#define FDE_DONT_CLOSE 0x0080
184
185typedef struct fdevent fdevent;
186
187typedef void (*fd_func)(int fd, unsigned events, void *userdata);
188
189fdevent *fdevent_create(int fd, fd_func func, void *arg);
190void fdevent_destroy(fdevent *fde);
191void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
192void fdevent_remove(fdevent *item);
193void fdevent_set(fdevent *fde, unsigned events);
194void fdevent_add(fdevent *fde, unsigned events);
195void fdevent_del(fdevent *fde, unsigned events);
196void fdevent_loop();
197
198struct fdevent {
199 fdevent *next;
200 fdevent *prev;
201
202 int fd;
JP Abgrall408fa572011-03-16 15:57:42 -0700203 int force_eof;
204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 unsigned short state;
206 unsigned short events;
207
208 fd_func func;
209 void *arg;
210};
211
212static __inline__ void adb_sleep_ms( int mseconds )
213{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200214 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215}
216
217extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
218
219#undef accept
220#define accept ___xxx_accept
221
222static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
223{
224 int opt = bufsize;
225 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
226}
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
462
463static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
464{
465 return socketpair( d, type, protocol, sv );
466}
467
468static __inline__ int adb_socketpair( int sv[2] )
469{
470 int rc;
471
472 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
473 if (rc < 0)
474 return -1;
475
476 close_on_exec( sv[0] );
477 close_on_exec( sv[1] );
478 return 0;
479}
480
481#undef socketpair
482#define socketpair ___xxx_socketpair
483
484static __inline__ void adb_sleep_ms( int mseconds )
485{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200486 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487}
488
489static __inline__ int adb_mkdir(const char* path, int mode)
490{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200491 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492}
493#undef mkdir
494#define mkdir ___xxx_mkdir
495
496static __inline__ void adb_sysdeps_init(void)
497{
498}
499
500static __inline__ char* adb_dirstart(const char* path)
501{
502 return strchr(path, '/');
503}
504
505static __inline__ char* adb_dirstop(const char* path)
506{
507 return strrchr(path, '/');
508}
509
510static __inline__ int adb_is_absolute_host_path( const char* path )
511{
512 return path[0] == '/';
513}
514
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700515static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
516{
517 return strtok_r(str, delim, saveptr);
518}
519#undef strtok_r
520#define strtok_r ___xxx_strtok_r
521
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522#endif /* !_WIN32 */
523
524#endif /* _ADB_SYSDEPS_H */