blob: 800ddb7531a35d64338d452b95701fc9e405b3cc [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -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 */
47/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
48#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{
71 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
72 if (thread->tid == (unsigned)-1L) {
73 return -1;
74 }
75 return 0;
76}
77
78static __inline__ void close_on_exec(int fd)
79{
80 /* nothing really */
81}
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{
107 return _mkdir(path);
108}
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);
117extern int adb_shutdown(int fd);
118extern 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{
143 return adb_open(path, options);
144}
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
174/* normally provided by "fdevent.h" */
175
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;
199 int force_eof;
200
201 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{
210 Sleep( mseconds );
211}
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
257#else /* !_WIN32 a.k.a. Unix */
258
259#include "fdevent.h"
260#include <cutils/sockets.h>
261#include <cutils/properties.h>
262#include <cutils/misc.h>
263#include <signal.h>
264#include <sys/wait.h>
265#include <sys/stat.h>
266#include <fcntl.h>
267
268#include <pthread.h>
269#include <unistd.h>
270#include <fcntl.h>
271#include <stdarg.h>
272#include <netinet/in.h>
273#include <netinet/tcp.h>
274#include <string.h>
275
276#define OS_PATH_SEPARATOR '/'
277#define OS_PATH_SEPARATOR_STR "/"
278
279typedef pthread_mutex_t adb_mutex_t;
280
281#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
282#define adb_mutex_init pthread_mutex_init
283#define adb_mutex_lock pthread_mutex_lock
284#define adb_mutex_unlock pthread_mutex_unlock
285#define adb_mutex_destroy pthread_mutex_destroy
286
287#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
288
289#define adb_cond_t pthread_cond_t
290#define adb_cond_init pthread_cond_init
291#define adb_cond_wait pthread_cond_wait
292#define adb_cond_broadcast pthread_cond_broadcast
293#define adb_cond_signal pthread_cond_signal
294#define adb_cond_destroy pthread_cond_destroy
295
296/* declare all mutexes */
297#define ADB_MUTEX(x) extern adb_mutex_t x;
298#include "mutex_list.h"
299
300static __inline__ void close_on_exec(int fd)
301{
302 fcntl( fd, F_SETFD, FD_CLOEXEC );
303}
304
305static __inline__ int unix_open(const char* path, int options,...)
306{
307 if ((options & O_CREAT) == 0)
308 {
309 return open(path, options);
310 }
311 else
312 {
313 int mode;
314 va_list args;
315 va_start( args, options );
316 mode = va_arg( args, int );
317 va_end( args );
318 return open(path, options, mode);
319 }
320}
321
322static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
323{
324 return open( pathname, options, mode );
325}
326
Doug Zongker703ed152012-03-19 15:52:03 -0700327static __inline__ int adb_creat(const char* path, int mode)
328{
329 int fd = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
330
331 if ( fd < 0 )
332 return -1;
333
334 close_on_exec(fd);
335 return fd;
336}
337#undef creat
338#define creat ___xxx_creat
Doug Zongker9270a202012-01-09 15:16:13 -0800339
340static __inline__ int adb_open( const char* pathname, int options )
341{
342 int fd = open( pathname, options );
343 if (fd < 0)
344 return -1;
345 close_on_exec( fd );
346 return fd;
347}
348#undef open
349#define open ___xxx_open
350
351static __inline__ int adb_shutdown(int fd)
352{
353 return shutdown(fd, SHUT_RDWR);
354}
355#undef shutdown
356#define shutdown ____xxx_shutdown
357
358static __inline__ int adb_close(int fd)
359{
360 return close(fd);
361}
362#undef close
363#define close ____xxx_close
364
365
366static __inline__ int adb_read(int fd, void* buf, size_t len)
367{
368 return read(fd, buf, len);
369}
370
371#undef read
372#define read ___xxx_read
373
374static __inline__ int adb_write(int fd, const void* buf, size_t len)
375{
376 return write(fd, buf, len);
377}
378#undef write
379#define write ___xxx_write
380
381static __inline__ int adb_lseek(int fd, int pos, int where)
382{
383 return lseek(fd, pos, where);
384}
385#undef lseek
386#define lseek ___xxx_lseek
387
388static __inline__ int adb_unlink(const char* path)
389{
390 return unlink(path);
391}
392#undef unlink
393#define unlink ___xxx_unlink
394
Doug Zongker9270a202012-01-09 15:16:13 -0800395static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
396{
397 int fd;
398
399 fd = accept(serverfd, addr, addrlen);
400 if (fd >= 0)
401 close_on_exec(fd);
402
403 return fd;
404}
405
406#undef accept
407#define accept ___xxx_accept
408
409#define unix_read adb_read
410#define unix_write adb_write
411#define unix_close adb_close
412
413typedef pthread_t adb_thread_t;
414
415typedef void* (*adb_thread_func_t)( void* arg );
416
417static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
418{
419 pthread_attr_t attr;
420
421 pthread_attr_init (&attr);
422 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
423
424 return pthread_create( pthread, &attr, start, arg );
425}
426
427static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
428{
429 int opt = bufsize;
430 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
431}
432
433static __inline__ void disable_tcp_nagle(int fd)
434{
435 int on = 1;
436 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
437}
438
439
440static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
441{
442 return socketpair( d, type, protocol, sv );
443}
444
445static __inline__ int adb_socketpair( int sv[2] )
446{
447 int rc;
448
449 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
450 if (rc < 0)
451 return -1;
452
453 close_on_exec( sv[0] );
454 close_on_exec( sv[1] );
455 return 0;
456}
457
458#undef socketpair
459#define socketpair ___xxx_socketpair
460
461static __inline__ void adb_sleep_ms( int mseconds )
462{
463 usleep( mseconds*1000 );
464}
465
466static __inline__ int adb_mkdir(const char* path, int mode)
467{
468 return mkdir(path, mode);
469}
470#undef mkdir
471#define mkdir ___xxx_mkdir
472
473static __inline__ void adb_sysdeps_init(void)
474{
475}
476
477static __inline__ char* adb_dirstart(const char* path)
478{
479 return strchr(path, '/');
480}
481
482static __inline__ char* adb_dirstop(const char* path)
483{
484 return strrchr(path, '/');
485}
486
487static __inline__ int adb_is_absolute_host_path( const char* path )
488{
489 return path[0] == '/';
490}
491
492#endif /* !_WIN32 */
493
494#endif /* _ADB_SYSDEPS_H */