blob: 3edaef472ef6530019e8657af52dfdadabaa65c7 [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
Dan Albertd808d212015-02-18 10:21:54 -080039#ifdef __cplusplus
40extern "C" {
41#endif
42
Doug Zongker9270a202012-01-09 15:16:13 -080043#define OS_PATH_SEPARATOR '\\'
44#define OS_PATH_SEPARATOR_STR "\\"
45
46typedef CRITICAL_SECTION adb_mutex_t;
47
48#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
49
50/* declare all mutexes */
51/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
52#define ADB_MUTEX(x) extern adb_mutex_t x;
53#include "mutex_list.h"
54
55extern void adb_sysdeps_init(void);
56
57static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
58{
59 EnterCriticalSection( lock );
60}
61
62static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
63{
64 LeaveCriticalSection( lock );
65}
66
67typedef struct { unsigned tid; } adb_thread_t;
68
69typedef void* (*adb_thread_func_t)(void* arg);
70
71typedef void (*win_thread_func_t)(void* arg);
72
73static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
74{
75 thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
76 if (thread->tid == (unsigned)-1L) {
77 return -1;
78 }
79 return 0;
80}
81
82static __inline__ void close_on_exec(int fd)
83{
84 /* nothing really */
85}
86
87extern void disable_tcp_nagle(int fd);
88
89#define lstat stat /* no symlinks on Win32 */
90
91#define S_ISLNK(m) 0 /* no symlinks on Win32 */
92
93static __inline__ int adb_unlink(const char* path)
94{
95 int rc = unlink(path);
96
97 if (rc == -1 && errno == EACCES) {
98 /* unlink returns EACCES when the file is read-only, so we first */
99 /* try to make it writable, then unlink again... */
100 rc = chmod(path, _S_IREAD|_S_IWRITE );
101 if (rc == 0)
102 rc = unlink(path);
103 }
104 return rc;
105}
106#undef unlink
107#define unlink ___xxx_unlink
108
109static __inline__ int adb_mkdir(const char* path, int mode)
110{
111 return _mkdir(path);
112}
113#undef mkdir
114#define mkdir ___xxx_mkdir
115
116extern int adb_open(const char* path, int options);
117extern int adb_creat(const char* path, int mode);
118extern int adb_read(int fd, void* buf, int len);
119extern int adb_write(int fd, const void* buf, int len);
120extern int adb_lseek(int fd, int pos, int where);
121extern int adb_shutdown(int fd);
122extern int adb_close(int fd);
123
124static __inline__ int unix_close(int fd)
125{
126 return close(fd);
127}
128#undef close
129#define close ____xxx_close
130
131static __inline__ int unix_read(int fd, void* buf, size_t len)
132{
133 return read(fd, buf, len);
134}
135#undef read
136#define read ___xxx_read
137
138static __inline__ int unix_write(int fd, const void* buf, size_t len)
139{
140 return write(fd, buf, len);
141}
142#undef write
143#define write ___xxx_write
144
145static __inline__ int adb_open_mode(const char* path, int options, int mode)
146{
147 return adb_open(path, options);
148}
149
150static __inline__ int unix_open(const char* path, int options,...)
151{
152 if ((options & O_CREAT) == 0)
153 {
154 return open(path, options);
155 }
156 else
157 {
158 int mode;
159 va_list args;
160 va_start( args, options );
161 mode = va_arg( args, int );
162 va_end( args );
163 return open(path, options, mode);
164 }
165}
166#define open ___xxx_unix_open
167
168
169/* normally provided by <cutils/misc.h> */
170extern void* load_file(const char* pathname, unsigned* psize);
171
172/* normally provided by <cutils/sockets.h> */
173extern int socket_loopback_client(int port, int type);
174extern int socket_network_client(const char *host, int port, int type);
175extern int socket_loopback_server(int port, int type);
176extern int socket_inaddr_any_server(int port, int type);
177
178/* normally provided by "fdevent.h" */
179
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;
203 int force_eof;
204
205 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{
214 Sleep( mseconds );
215}
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
Dan Albertd808d212015-02-18 10:21:54 -0800261#ifdef __cplusplus
262}
263#endif
264
Doug Zongker9270a202012-01-09 15:16:13 -0800265#else /* !_WIN32 a.k.a. Unix */
266
267#include "fdevent.h"
268#include <cutils/sockets.h>
269#include <cutils/properties.h>
270#include <cutils/misc.h>
271#include <signal.h>
272#include <sys/wait.h>
273#include <sys/stat.h>
274#include <fcntl.h>
275
276#include <pthread.h>
277#include <unistd.h>
278#include <fcntl.h>
279#include <stdarg.h>
280#include <netinet/in.h>
281#include <netinet/tcp.h>
282#include <string.h>
283
284#define OS_PATH_SEPARATOR '/'
285#define OS_PATH_SEPARATOR_STR "/"
286
287typedef pthread_mutex_t adb_mutex_t;
288
289#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
290#define adb_mutex_init pthread_mutex_init
291#define adb_mutex_lock pthread_mutex_lock
292#define adb_mutex_unlock pthread_mutex_unlock
293#define adb_mutex_destroy pthread_mutex_destroy
294
295#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
296
297#define adb_cond_t pthread_cond_t
298#define adb_cond_init pthread_cond_init
299#define adb_cond_wait pthread_cond_wait
300#define adb_cond_broadcast pthread_cond_broadcast
301#define adb_cond_signal pthread_cond_signal
302#define adb_cond_destroy pthread_cond_destroy
303
304/* declare all mutexes */
305#define ADB_MUTEX(x) extern adb_mutex_t x;
306#include "mutex_list.h"
307
308static __inline__ void close_on_exec(int fd)
309{
310 fcntl( fd, F_SETFD, FD_CLOEXEC );
311}
312
313static __inline__ int unix_open(const char* path, int options,...)
314{
315 if ((options & O_CREAT) == 0)
316 {
317 return open(path, options);
318 }
319 else
320 {
321 int mode;
322 va_list args;
323 va_start( args, options );
324 mode = va_arg( args, int );
325 va_end( args );
326 return open(path, options, mode);
327 }
328}
329
330static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
331{
332 return open( pathname, options, mode );
333}
334
Doug Zongker703ed152012-03-19 15:52:03 -0700335static __inline__ int adb_creat(const char* path, int mode)
336{
337 int fd = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
338
339 if ( fd < 0 )
340 return -1;
341
342 close_on_exec(fd);
343 return fd;
344}
345#undef creat
346#define creat ___xxx_creat
Doug Zongker9270a202012-01-09 15:16:13 -0800347
348static __inline__ int adb_open( const char* pathname, int options )
349{
350 int fd = open( pathname, options );
351 if (fd < 0)
352 return -1;
353 close_on_exec( fd );
354 return fd;
355}
356#undef open
357#define open ___xxx_open
358
359static __inline__ int adb_shutdown(int fd)
360{
361 return shutdown(fd, SHUT_RDWR);
362}
363#undef shutdown
364#define shutdown ____xxx_shutdown
365
366static __inline__ int adb_close(int fd)
367{
368 return close(fd);
369}
370#undef close
371#define close ____xxx_close
372
373
374static __inline__ int adb_read(int fd, void* buf, size_t len)
375{
376 return read(fd, buf, len);
377}
378
379#undef read
380#define read ___xxx_read
381
382static __inline__ int adb_write(int fd, const void* buf, size_t len)
383{
384 return write(fd, buf, len);
385}
386#undef write
387#define write ___xxx_write
388
389static __inline__ int adb_lseek(int fd, int pos, int where)
390{
391 return lseek(fd, pos, where);
392}
393#undef lseek
394#define lseek ___xxx_lseek
395
396static __inline__ int adb_unlink(const char* path)
397{
398 return unlink(path);
399}
400#undef unlink
401#define unlink ___xxx_unlink
402
Doug Zongker9270a202012-01-09 15:16:13 -0800403static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
404{
405 int fd;
406
407 fd = accept(serverfd, addr, addrlen);
408 if (fd >= 0)
409 close_on_exec(fd);
410
411 return fd;
412}
413
414#undef accept
415#define accept ___xxx_accept
416
417#define unix_read adb_read
418#define unix_write adb_write
419#define unix_close adb_close
420
421typedef pthread_t adb_thread_t;
422
423typedef void* (*adb_thread_func_t)( void* arg );
424
425static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
426{
427 pthread_attr_t attr;
428
429 pthread_attr_init (&attr);
430 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
431
432 return pthread_create( pthread, &attr, start, arg );
433}
434
435static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
436{
437 int opt = bufsize;
438 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
439}
440
441static __inline__ void disable_tcp_nagle(int fd)
442{
443 int on = 1;
444 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
445}
446
447
448static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
449{
450 return socketpair( d, type, protocol, sv );
451}
452
453static __inline__ int adb_socketpair( int sv[2] )
454{
455 int rc;
456
457 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
458 if (rc < 0)
459 return -1;
460
461 close_on_exec( sv[0] );
462 close_on_exec( sv[1] );
463 return 0;
464}
465
466#undef socketpair
467#define socketpair ___xxx_socketpair
468
469static __inline__ void adb_sleep_ms( int mseconds )
470{
471 usleep( mseconds*1000 );
472}
473
474static __inline__ int adb_mkdir(const char* path, int mode)
475{
476 return mkdir(path, mode);
477}
478#undef mkdir
479#define mkdir ___xxx_mkdir
480
481static __inline__ void adb_sysdeps_init(void)
482{
483}
484
485static __inline__ char* adb_dirstart(const char* path)
486{
487 return strchr(path, '/');
488}
489
490static __inline__ char* adb_dirstop(const char* path)
491{
492 return strrchr(path, '/');
493}
494
495static __inline__ int adb_is_absolute_host_path( const char* path )
496{
497 return path[0] == '/';
498}
499
500#endif /* !_WIN32 */
501
Dan Albertd808d212015-02-18 10:21:54 -0800502#ifdef __cplusplus
503}
504#endif
505
Doug Zongker9270a202012-01-09 15:16:13 -0800506#endif /* _ADB_SYSDEPS_H */