blob: 605fa1785dfdc93c8c88bf017ea8752483fb5c7c [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 "\\"
41
42typedef CRITICAL_SECTION adb_mutex_t;
43
44#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
45
46/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070047/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#define ADB_MUTEX(x) extern adb_mutex_t x;
49#include "mutex_list.h"
50
51extern void adb_sysdeps_init(void);
52
53static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
54{
55 EnterCriticalSection( lock );
56}
57
58static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
59{
60 LeaveCriticalSection( lock );
61}
62
63typedef struct { unsigned tid; } adb_thread_t;
64
65typedef void* (*adb_thread_func_t)(void* arg);
66
67typedef void (*win_thread_func_t)(void* arg);
68
69static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
70{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020071 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
72 if (thread->tid == (unsigned)-1L) {
73 return -1;
74 }
75 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076}
77
78static __inline__ void close_on_exec(int fd)
79{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020080 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081}
82
83extern void disable_tcp_nagle(int fd);
84
85#define lstat stat /* no symlinks on Win32 */
86
87#define S_ISLNK(m) 0 /* no symlinks on Win32 */
88
89static __inline__ int adb_unlink(const char* path)
90{
91 int rc = unlink(path);
92
93 if (rc == -1 && errno == EACCES) {
94 /* unlink returns EACCES when the file is read-only, so we first */
95 /* try to make it writable, then unlink again... */
96 rc = chmod(path, _S_IREAD|_S_IWRITE );
97 if (rc == 0)
98 rc = unlink(path);
99 }
100 return rc;
101}
102#undef unlink
103#define unlink ___xxx_unlink
104
105static __inline__ int adb_mkdir(const char* path, int mode)
106{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800107 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108}
109#undef mkdir
110#define mkdir ___xxx_mkdir
111
112extern int adb_open(const char* path, int options);
113extern int adb_creat(const char* path, int mode);
114extern int adb_read(int fd, void* buf, int len);
115extern int adb_write(int fd, const void* buf, int len);
116extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400117extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118extern int adb_close(int fd);
119
120static __inline__ int unix_close(int fd)
121{
122 return close(fd);
123}
124#undef close
125#define close ____xxx_close
126
127static __inline__ int unix_read(int fd, void* buf, size_t len)
128{
129 return read(fd, buf, len);
130}
131#undef read
132#define read ___xxx_read
133
134static __inline__ int unix_write(int fd, const void* buf, size_t len)
135{
136 return write(fd, buf, len);
137}
138#undef write
139#define write ___xxx_write
140
141static __inline__ int adb_open_mode(const char* path, int options, int mode)
142{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200143 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144}
145
146static __inline__ int unix_open(const char* path, int options,...)
147{
148 if ((options & O_CREAT) == 0)
149 {
150 return open(path, options);
151 }
152 else
153 {
154 int mode;
155 va_list args;
156 va_start( args, options );
157 mode = va_arg( args, int );
158 va_end( args );
159 return open(path, options, mode);
160 }
161}
162#define open ___xxx_unix_open
163
164
165/* normally provided by <cutils/misc.h> */
166extern void* load_file(const char* pathname, unsigned* psize);
167
168/* normally provided by <cutils/sockets.h> */
169extern int socket_loopback_client(int port, int type);
170extern int socket_network_client(const char *host, int port, int type);
171extern int socket_loopback_server(int port, int type);
172extern int socket_inaddr_any_server(int port, int type);
173
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200174/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
176#define FDE_READ 0x0001
177#define FDE_WRITE 0x0002
178#define FDE_ERROR 0x0004
179#define FDE_DONT_CLOSE 0x0080
180
181typedef struct fdevent fdevent;
182
183typedef void (*fd_func)(int fd, unsigned events, void *userdata);
184
185fdevent *fdevent_create(int fd, fd_func func, void *arg);
186void fdevent_destroy(fdevent *fde);
187void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
188void fdevent_remove(fdevent *item);
189void fdevent_set(fdevent *fde, unsigned events);
190void fdevent_add(fdevent *fde, unsigned events);
191void fdevent_del(fdevent *fde, unsigned events);
192void fdevent_loop();
193
194struct fdevent {
195 fdevent *next;
196 fdevent *prev;
197
198 int fd;
JP Abgrall408fa572011-03-16 15:57:42 -0700199 int force_eof;
200
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 unsigned short state;
202 unsigned short events;
203
204 fd_func func;
205 void *arg;
206};
207
208static __inline__ void adb_sleep_ms( int mseconds )
209{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200210 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-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
218static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
219{
220 int opt = bufsize;
221 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
222}
223
224extern int adb_socketpair( int sv[2] );
225
226static __inline__ char* adb_dirstart( const char* path )
227{
228 char* p = strchr(path, '/');
229 char* p2 = strchr(path, '\\');
230
231 if ( !p )
232 p = p2;
233 else if ( p2 && p2 > p )
234 p = p2;
235
236 return p;
237}
238
239static __inline__ char* adb_dirstop( const char* path )
240{
241 char* p = strrchr(path, '/');
242 char* p2 = strrchr(path, '\\');
243
244 if ( !p )
245 p = p2;
246 else if ( p2 && p2 > p )
247 p = p2;
248
249 return p;
250}
251
252static __inline__ int adb_is_absolute_host_path( const char* path )
253{
254 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
255}
256
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700257extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
258
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259#else /* !_WIN32 a.k.a. Unix */
260
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200261#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262#include <cutils/sockets.h>
263#include <cutils/properties.h>
264#include <cutils/misc.h>
265#include <signal.h>
266#include <sys/wait.h>
267#include <sys/stat.h>
268#include <fcntl.h>
269
270#include <pthread.h>
271#include <unistd.h>
272#include <fcntl.h>
273#include <stdarg.h>
274#include <netinet/in.h>
275#include <netinet/tcp.h>
276#include <string.h>
277
278#define OS_PATH_SEPARATOR '/'
279#define OS_PATH_SEPARATOR_STR "/"
280
281typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700282
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
284#define adb_mutex_init pthread_mutex_init
285#define adb_mutex_lock pthread_mutex_lock
286#define adb_mutex_unlock pthread_mutex_unlock
287#define adb_mutex_destroy pthread_mutex_destroy
288
JP Abgrall408fa572011-03-16 15:57:42 -0700289#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290
291#define adb_cond_t pthread_cond_t
292#define adb_cond_init pthread_cond_init
293#define adb_cond_wait pthread_cond_wait
294#define adb_cond_broadcast pthread_cond_broadcast
295#define adb_cond_signal pthread_cond_signal
296#define adb_cond_destroy pthread_cond_destroy
297
JP Abgrall408fa572011-03-16 15:57:42 -0700298/* declare all mutexes */
299#define ADB_MUTEX(x) extern adb_mutex_t x;
300#include "mutex_list.h"
301
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302static __inline__ void close_on_exec(int fd)
303{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200304 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305}
306
307static __inline__ int unix_open(const char* path, int options,...)
308{
309 if ((options & O_CREAT) == 0)
310 {
311 return open(path, options);
312 }
313 else
314 {
315 int mode;
316 va_list args;
317 va_start( args, options );
318 mode = va_arg( args, int );
319 va_end( args );
320 return open(path, options, mode);
321 }
322}
323
324static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
325{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200326 return open( pathname, options, mode );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327}
328
329
330static __inline__ int adb_open( const char* pathname, int options )
331{
332 int fd = open( pathname, options );
333 if (fd < 0)
334 return -1;
335 close_on_exec( fd );
336 return fd;
337}
338#undef open
339#define open ___xxx_open
340
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400341static __inline__ int adb_shutdown(int fd)
342{
343 return shutdown(fd, SHUT_RDWR);
344}
345#undef shutdown
346#define shutdown ____xxx_shutdown
347
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348static __inline__ int adb_close(int fd)
349{
350 return close(fd);
351}
352#undef close
353#define close ____xxx_close
354
355
356static __inline__ int adb_read(int fd, void* buf, size_t len)
357{
358 return read(fd, buf, len);
359}
360
361#undef read
362#define read ___xxx_read
363
364static __inline__ int adb_write(int fd, const void* buf, size_t len)
365{
366 return write(fd, buf, len);
367}
368#undef write
369#define write ___xxx_write
370
371static __inline__ int adb_lseek(int fd, int pos, int where)
372{
373 return lseek(fd, pos, where);
374}
375#undef lseek
376#define lseek ___xxx_lseek
377
378static __inline__ int adb_unlink(const char* path)
379{
380 return unlink(path);
381}
382#undef unlink
383#define unlink ___xxx_unlink
384
385static __inline__ int adb_creat(const char* path, int mode)
386{
387 int fd = creat(path, mode);
388
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200389 if ( fd < 0 )
390 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391
392 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200393 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394}
395#undef creat
396#define creat ___xxx_creat
397
398static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
399{
Benoit Goby95ef8282011-02-01 18:57:41 -0800400 int fd;
401
402 fd = accept(serverfd, addr, addrlen);
403 if (fd >= 0)
404 close_on_exec(fd);
405
406 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407}
408
409#undef accept
410#define accept ___xxx_accept
411
412#define unix_read adb_read
413#define unix_write adb_write
414#define unix_close adb_close
415
416typedef pthread_t adb_thread_t;
417
418typedef void* (*adb_thread_func_t)( void* arg );
419
420static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
421{
422 pthread_attr_t attr;
423
424 pthread_attr_init (&attr);
425 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
426
427 return pthread_create( pthread, &attr, start, arg );
428}
429
430static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
431{
432 int opt = bufsize;
433 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
434}
435
436static __inline__ void disable_tcp_nagle(int fd)
437{
438 int on = 1;
439 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
440}
441
442
443static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
444{
445 return socketpair( d, type, protocol, sv );
446}
447
448static __inline__ int adb_socketpair( int sv[2] )
449{
450 int rc;
451
452 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
453 if (rc < 0)
454 return -1;
455
456 close_on_exec( sv[0] );
457 close_on_exec( sv[1] );
458 return 0;
459}
460
461#undef socketpair
462#define socketpair ___xxx_socketpair
463
464static __inline__ void adb_sleep_ms( int mseconds )
465{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200466 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467}
468
469static __inline__ int adb_mkdir(const char* path, int mode)
470{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200471 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472}
473#undef mkdir
474#define mkdir ___xxx_mkdir
475
476static __inline__ void adb_sysdeps_init(void)
477{
478}
479
480static __inline__ char* adb_dirstart(const char* path)
481{
482 return strchr(path, '/');
483}
484
485static __inline__ char* adb_dirstop(const char* path)
486{
487 return strrchr(path, '/');
488}
489
490static __inline__ int adb_is_absolute_host_path( const char* path )
491{
492 return path[0] == '/';
493}
494
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700495static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
496{
497 return strtok_r(str, delim, saveptr);
498}
499#undef strtok_r
500#define strtok_r ___xxx_strtok_r
501
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502#endif /* !_WIN32 */
503
504#endif /* _ADB_SYSDEPS_H */