blob: 8d63d14b3514da7175c20a584b4b8f494f450779 [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
leozwangcbf02672014-08-15 09:51:27 -070080static __inline__ unsigned long adb_thread_id()
81{
82 return GetCurrentThreadId();
83}
84
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085static __inline__ void close_on_exec(int fd)
86{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020087 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088}
89
90extern void disable_tcp_nagle(int fd);
91
92#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
190typedef struct fdevent fdevent;
191
192typedef 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
203struct fdevent {
204 fdevent *next;
205 fdevent *prev;
206
207 int fd;
JP Abgrall408fa572011-03-16 15:57:42 -0700208 int force_eof;
209
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 unsigned short state;
211 unsigned short events;
212
213 fd_func func;
214 void *arg;
215};
216
217static __inline__ void adb_sleep_ms( int mseconds )
218{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200219 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220}
221
222extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
223
224#undef accept
225#define accept ___xxx_accept
226
227static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
228{
229 int opt = bufsize;
230 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
231}
232
233extern int adb_socketpair( int sv[2] );
234
235static __inline__ char* adb_dirstart( const char* path )
236{
237 char* p = strchr(path, '/');
238 char* p2 = strchr(path, '\\');
239
240 if ( !p )
241 p = p2;
242 else if ( p2 && p2 > p )
243 p = p2;
244
245 return p;
246}
247
248static __inline__ char* adb_dirstop( const char* path )
249{
250 char* p = strrchr(path, '/');
251 char* p2 = strrchr(path, '\\');
252
253 if ( !p )
254 p = p2;
255 else if ( p2 && p2 > p )
256 p = p2;
257
258 return p;
259}
260
261static __inline__ int adb_is_absolute_host_path( const char* path )
262{
263 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
264}
265
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700266extern char* adb_strtok_r(char *str, const char *delim, char **saveptr);
267
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268#else /* !_WIN32 a.k.a. Unix */
269
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200270#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272#include <cutils/misc.h>
273#include <signal.h>
274#include <sys/wait.h>
275#include <sys/stat.h>
276#include <fcntl.h>
277
278#include <pthread.h>
279#include <unistd.h>
280#include <fcntl.h>
281#include <stdarg.h>
282#include <netinet/in.h>
283#include <netinet/tcp.h>
284#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700285#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286
Kenny Rootec90f1d2012-10-13 11:59:01 -0700287/*
288 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
289 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
290 * not already defined, then define it here.
291 */
292#ifndef TEMP_FAILURE_RETRY
293/* Used to retry syscalls that can return EINTR. */
294#define TEMP_FAILURE_RETRY(exp) ({ \
295 typeof (exp) _rc; \
296 do { \
297 _rc = (exp); \
298 } while (_rc == -1 && errno == EINTR); \
299 _rc; })
300#endif
301
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302#define OS_PATH_SEPARATOR '/'
303#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700304#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305
306typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700307
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
309#define adb_mutex_init pthread_mutex_init
310#define adb_mutex_lock pthread_mutex_lock
311#define adb_mutex_unlock pthread_mutex_unlock
312#define adb_mutex_destroy pthread_mutex_destroy
313
JP Abgrall408fa572011-03-16 15:57:42 -0700314#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315
316#define adb_cond_t pthread_cond_t
317#define adb_cond_init pthread_cond_init
318#define adb_cond_wait pthread_cond_wait
319#define adb_cond_broadcast pthread_cond_broadcast
320#define adb_cond_signal pthread_cond_signal
321#define adb_cond_destroy pthread_cond_destroy
322
JP Abgrall408fa572011-03-16 15:57:42 -0700323/* declare all mutexes */
324#define ADB_MUTEX(x) extern adb_mutex_t x;
325#include "mutex_list.h"
326
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327static __inline__ void close_on_exec(int fd)
328{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200329 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330}
331
332static __inline__ int unix_open(const char* path, int options,...)
333{
334 if ((options & O_CREAT) == 0)
335 {
Kenny Root73167412012-10-12 15:26:45 -0700336 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 }
338 else
339 {
340 int mode;
341 va_list args;
342 va_start( args, options );
343 mode = va_arg( args, int );
344 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700345 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 }
347}
348
349static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
350{
Kenny Root73167412012-10-12 15:26:45 -0700351 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352}
353
354
355static __inline__ int adb_open( const char* pathname, int options )
356{
Kenny Root73167412012-10-12 15:26:45 -0700357 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 if (fd < 0)
359 return -1;
360 close_on_exec( fd );
361 return fd;
362}
363#undef open
364#define open ___xxx_open
365
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400366static __inline__ int adb_shutdown(int fd)
367{
368 return shutdown(fd, SHUT_RDWR);
369}
370#undef shutdown
371#define shutdown ____xxx_shutdown
372
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373static __inline__ int adb_close(int fd)
374{
375 return close(fd);
376}
377#undef close
378#define close ____xxx_close
379
380
381static __inline__ int adb_read(int fd, void* buf, size_t len)
382{
Kenny Root73167412012-10-12 15:26:45 -0700383 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384}
385
386#undef read
387#define read ___xxx_read
388
389static __inline__ int adb_write(int fd, const void* buf, size_t len)
390{
Kenny Root73167412012-10-12 15:26:45 -0700391 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392}
393#undef write
394#define write ___xxx_write
395
396static __inline__ int adb_lseek(int fd, int pos, int where)
397{
398 return lseek(fd, pos, where);
399}
400#undef lseek
401#define lseek ___xxx_lseek
402
403static __inline__ int adb_unlink(const char* path)
404{
405 return unlink(path);
406}
407#undef unlink
408#define unlink ___xxx_unlink
409
410static __inline__ int adb_creat(const char* path, int mode)
411{
Kenny Root73167412012-10-12 15:26:45 -0700412 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200414 if ( fd < 0 )
415 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416
417 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200418 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419}
420#undef creat
421#define creat ___xxx_creat
422
423static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
424{
Benoit Goby95ef8282011-02-01 18:57:41 -0800425 int fd;
426
Kenny Root73167412012-10-12 15:26:45 -0700427 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800428 if (fd >= 0)
429 close_on_exec(fd);
430
431 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432}
433
434#undef accept
435#define accept ___xxx_accept
436
437#define unix_read adb_read
438#define unix_write adb_write
439#define unix_close adb_close
440
441typedef pthread_t adb_thread_t;
442
443typedef void* (*adb_thread_func_t)( void* arg );
444
445static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
446{
447 pthread_attr_t attr;
448
449 pthread_attr_init (&attr);
450 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
451
452 return pthread_create( pthread, &attr, start, arg );
453}
454
455static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
456{
457 int opt = bufsize;
458 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
459}
460
461static __inline__ void disable_tcp_nagle(int fd)
462{
463 int on = 1;
464 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
465}
466
467
468static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
469{
470 return socketpair( d, type, protocol, sv );
471}
472
473static __inline__ int adb_socketpair( int sv[2] )
474{
475 int rc;
476
477 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
478 if (rc < 0)
479 return -1;
480
481 close_on_exec( sv[0] );
482 close_on_exec( sv[1] );
483 return 0;
484}
485
486#undef socketpair
487#define socketpair ___xxx_socketpair
488
489static __inline__ void adb_sleep_ms( int mseconds )
490{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200491 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492}
493
494static __inline__ int adb_mkdir(const char* path, int mode)
495{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200496 return mkdir(path, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497}
498#undef mkdir
499#define mkdir ___xxx_mkdir
500
501static __inline__ void adb_sysdeps_init(void)
502{
503}
504
505static __inline__ char* adb_dirstart(const char* path)
506{
507 return strchr(path, '/');
508}
509
510static __inline__ char* adb_dirstop(const char* path)
511{
512 return strrchr(path, '/');
513}
514
515static __inline__ int adb_is_absolute_host_path( const char* path )
516{
517 return path[0] == '/';
518}
519
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700520static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr)
521{
522 return strtok_r(str, delim, saveptr);
523}
leozwangcbf02672014-08-15 09:51:27 -0700524
525static __inline__ unsigned long adb_thread_id()
526{
leozwang298b6c72014-08-22 15:35:47 -0700527 return (unsigned long)pthread_self();
leozwangcbf02672014-08-15 09:51:27 -0700528}
529
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700530#undef strtok_r
531#define strtok_r ___xxx_strtok_r
532
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533#endif /* !_WIN32 */
534
535#endif /* _ADB_SYSDEPS_H */