blob: ee7cd495ead1753d7cbc81aca2440a781c4951d1 [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
29#include <windows.h>
30#include <winsock2.h>
31#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>
38
39#define OS_PATH_SEPARATOR '\\'
40#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070041#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43typedef CRITICAL_SECTION adb_mutex_t;
44
45#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
46
47/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070048/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#define ADB_MUTEX(x) extern adb_mutex_t x;
50#include "mutex_list.h"
51
52extern void adb_sysdeps_init(void);
53
54static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
55{
56 EnterCriticalSection( lock );
57}
58
59static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
60{
61 LeaveCriticalSection( lock );
62}
63
64typedef struct { unsigned tid; } adb_thread_t;
65
66typedef void* (*adb_thread_func_t)(void* arg);
67
68typedef void (*win_thread_func_t)(void* arg);
69
70static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
71{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020072 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
73 if (thread->tid == (unsigned)-1L) {
74 return -1;
75 }
76 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077}
78
79static __inline__ void close_on_exec(int fd)
80{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020081 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082}
83
84extern void disable_tcp_nagle(int fd);
85
86#define lstat stat /* no symlinks on Win32 */
87
88#define S_ISLNK(m) 0 /* no symlinks on Win32 */
89
90static __inline__ int adb_unlink(const char* path)
91{
92 int rc = unlink(path);
93
94 if (rc == -1 && errno == EACCES) {
95 /* unlink returns EACCES when the file is read-only, so we first */
96 /* try to make it writable, then unlink again... */
97 rc = chmod(path, _S_IREAD|_S_IWRITE );
98 if (rc == 0)
99 rc = unlink(path);
100 }
101 return rc;
102}
103#undef unlink
104#define unlink ___xxx_unlink
105
106static __inline__ int adb_mkdir(const char* path, int mode)
107{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800108 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109}
110#undef mkdir
111#define mkdir ___xxx_mkdir
112
113extern int adb_open(const char* path, int options);
114extern int adb_creat(const char* path, int mode);
115extern int adb_read(int fd, void* buf, int len);
116extern int adb_write(int fd, const void* buf, int len);
117extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400118extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119extern int adb_close(int fd);
120
121static __inline__ int unix_close(int fd)
122{
123 return close(fd);
124}
125#undef close
126#define close ____xxx_close
127
128static __inline__ int unix_read(int fd, void* buf, size_t len)
129{
130 return read(fd, buf, len);
131}
132#undef read
133#define read ___xxx_read
134
135static __inline__ int unix_write(int fd, const void* buf, size_t len)
136{
137 return write(fd, buf, len);
138}
139#undef write
140#define write ___xxx_write
141
142static __inline__ int adb_open_mode(const char* path, int options, int mode)
143{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200144 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145}
146
147static __inline__ int unix_open(const char* path, int options,...)
148{
149 if ((options & O_CREAT) == 0)
150 {
151 return open(path, options);
152 }
153 else
154 {
155 int mode;
156 va_list args;
157 va_start( args, options );
158 mode = va_arg( args, int );
159 va_end( args );
160 return open(path, options, mode);
161 }
162}
163#define open ___xxx_unix_open
164
165
166/* normally provided by <cutils/misc.h> */
167extern void* load_file(const char* pathname, unsigned* psize);
168
169/* normally provided by <cutils/sockets.h> */
170extern int socket_loopback_client(int port, int type);
171extern int socket_network_client(const char *host, int port, int type);
172extern int socket_loopback_server(int port, int type);
173extern int socket_inaddr_any_server(int port, int type);
174
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200175/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
177#define FDE_READ 0x0001
178#define FDE_WRITE 0x0002
179#define FDE_ERROR 0x0004
180#define FDE_DONT_CLOSE 0x0080
181
182typedef struct fdevent fdevent;
183
184typedef void (*fd_func)(int fd, unsigned events, void *userdata);
185
186fdevent *fdevent_create(int fd, fd_func func, void *arg);
187void fdevent_destroy(fdevent *fde);
188void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
189void fdevent_remove(fdevent *item);
190void fdevent_set(fdevent *fde, unsigned events);
191void fdevent_add(fdevent *fde, unsigned events);
192void fdevent_del(fdevent *fde, unsigned events);
193void fdevent_loop();
194
195struct fdevent {
196 fdevent *next;
197 fdevent *prev;
198
199 int fd;
JP Abgrall408fa572011-03-16 15:57:42 -0700200 int force_eof;
201
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 unsigned short state;
203 unsigned short events;
204
205 fd_func func;
206 void *arg;
207};
208
209static __inline__ void adb_sleep_ms( int mseconds )
210{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200211 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212}
213
214extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
215
216#undef accept
217#define accept ___xxx_accept
218
219static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
220{
221 int opt = bufsize;
222 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
223}
224
225extern int adb_socketpair( int sv[2] );
226
227static __inline__ char* adb_dirstart( const char* path )
228{
229 char* p = strchr(path, '/');
230 char* p2 = strchr(path, '\\');
231
232 if ( !p )
233 p = p2;
234 else if ( p2 && p2 > p )
235 p = p2;
236
237 return p;
238}
239
240static __inline__ char* adb_dirstop( const char* path )
241{
242 char* p = strrchr(path, '/');
243 char* p2 = strrchr(path, '\\');
244
245 if ( !p )
246 p = p2;
247 else if ( p2 && p2 > p )
248 p = p2;
249
250 return p;
251}
252
253static __inline__ int adb_is_absolute_host_path( const char* path )
254{
255 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
256}
257
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700258extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
259
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260#else /* !_WIN32 a.k.a. Unix */
261
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200262#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263#include <cutils/sockets.h>
264#include <cutils/properties.h>
265#include <cutils/misc.h>
266#include <signal.h>
267#include <sys/wait.h>
268#include <sys/stat.h>
269#include <fcntl.h>
270
271#include <pthread.h>
272#include <unistd.h>
273#include <fcntl.h>
274#include <stdarg.h>
275#include <netinet/in.h>
276#include <netinet/tcp.h>
277#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700278#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279
280#define OS_PATH_SEPARATOR '/'
281#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700282#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283
284typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700285
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
287#define adb_mutex_init pthread_mutex_init
288#define adb_mutex_lock pthread_mutex_lock
289#define adb_mutex_unlock pthread_mutex_unlock
290#define adb_mutex_destroy pthread_mutex_destroy
291
JP Abgrall408fa572011-03-16 15:57:42 -0700292#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293
294#define adb_cond_t pthread_cond_t
295#define adb_cond_init pthread_cond_init
296#define adb_cond_wait pthread_cond_wait
297#define adb_cond_broadcast pthread_cond_broadcast
298#define adb_cond_signal pthread_cond_signal
299#define adb_cond_destroy pthread_cond_destroy
300
JP Abgrall408fa572011-03-16 15:57:42 -0700301/* declare all mutexes */
302#define ADB_MUTEX(x) extern adb_mutex_t x;
303#include "mutex_list.h"
304
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305static __inline__ void close_on_exec(int fd)
306{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200307 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308}
309
310static __inline__ int unix_open(const char* path, int options,...)
311{
312 if ((options & O_CREAT) == 0)
313 {
Kenny Root73167412012-10-12 15:26:45 -0700314 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 }
316 else
317 {
318 int mode;
319 va_list args;
320 va_start( args, options );
321 mode = va_arg( args, int );
322 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700323 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 }
325}
326
327static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
328{
Kenny Root73167412012-10-12 15:26:45 -0700329 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330}
331
332
333static __inline__ int adb_open( const char* pathname, int options )
334{
Kenny Root73167412012-10-12 15:26:45 -0700335 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 if (fd < 0)
337 return -1;
338 close_on_exec( fd );
339 return fd;
340}
341#undef open
342#define open ___xxx_open
343
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400344static __inline__ int adb_shutdown(int fd)
345{
346 return shutdown(fd, SHUT_RDWR);
347}
348#undef shutdown
349#define shutdown ____xxx_shutdown
350
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351static __inline__ int adb_close(int fd)
352{
353 return close(fd);
354}
355#undef close
356#define close ____xxx_close
357
358
359static __inline__ int adb_read(int fd, void* buf, size_t len)
360{
Kenny Root73167412012-10-12 15:26:45 -0700361 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362}
363
364#undef read
365#define read ___xxx_read
366
367static __inline__ int adb_write(int fd, const void* buf, size_t len)
368{
Kenny Root73167412012-10-12 15:26:45 -0700369 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370}
371#undef write
372#define write ___xxx_write
373
374static __inline__ int adb_lseek(int fd, int pos, int where)
375{
376 return lseek(fd, pos, where);
377}
378#undef lseek
379#define lseek ___xxx_lseek
380
381static __inline__ int adb_unlink(const char* path)
382{
383 return unlink(path);
384}
385#undef unlink
386#define unlink ___xxx_unlink
387
388static __inline__ int adb_creat(const char* path, int mode)
389{
Kenny Root73167412012-10-12 15:26:45 -0700390 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200392 if ( fd < 0 )
393 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394
395 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200396 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397}
398#undef creat
399#define creat ___xxx_creat
400
401static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
402{
Benoit Goby95ef8282011-02-01 18:57:41 -0800403 int fd;
404
Kenny Root73167412012-10-12 15:26:45 -0700405 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800406 if (fd >= 0)
407 close_on_exec(fd);
408
409 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410}
411
412#undef accept
413#define accept ___xxx_accept
414
415#define unix_read adb_read
416#define unix_write adb_write
417#define unix_close adb_close
418
419typedef pthread_t adb_thread_t;
420
421typedef void* (*adb_thread_func_t)( void* arg );
422
423static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
424{
425 pthread_attr_t attr;
426
427 pthread_attr_init (&attr);
428 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
429
430 return pthread_create( pthread, &attr, start, arg );
431}
432
433static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
434{
435 int opt = bufsize;
436 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
437}
438
439static __inline__ void disable_tcp_nagle(int fd)
440{
441 int on = 1;
442 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
443}
444
445
446static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
447{
448 return socketpair( d, type, protocol, sv );
449}
450
451static __inline__ int adb_socketpair( int sv[2] )
452{
453 int rc;
454
455 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
456 if (rc < 0)
457 return -1;
458
459 close_on_exec( sv[0] );
460 close_on_exec( sv[1] );
461 return 0;
462}
463
464#undef socketpair
465#define socketpair ___xxx_socketpair
466
467static __inline__ void adb_sleep_ms( int mseconds )
468{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200469 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470}
471
472static __inline__ int adb_mkdir(const char* path, int mode)
473{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200474 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475}
476#undef mkdir
477#define mkdir ___xxx_mkdir
478
479static __inline__ void adb_sysdeps_init(void)
480{
481}
482
483static __inline__ char* adb_dirstart(const char* path)
484{
485 return strchr(path, '/');
486}
487
488static __inline__ char* adb_dirstop(const char* path)
489{
490 return strrchr(path, '/');
491}
492
493static __inline__ int adb_is_absolute_host_path( const char* path )
494{
495 return path[0] == '/';
496}
497
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700498static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
499{
500 return strtok_r(str, delim, saveptr);
501}
502#undef strtok_r
503#define strtok_r ___xxx_strtok_r
504
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505#endif /* !_WIN32 */
506
507#endif /* _ADB_SYSDEPS_H */