sysdeps.h revision 73167412bc52ee370761e418366ddac504666567
1/*
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#include <unistd.h>
276
277#define OS_PATH_SEPARATOR '/'
278#define OS_PATH_SEPARATOR_STR "/"
279
280typedef  pthread_mutex_t          adb_mutex_t;
281
282#define  ADB_MUTEX_INITIALIZER    PTHREAD_MUTEX_INITIALIZER
283#define  adb_mutex_init           pthread_mutex_init
284#define  adb_mutex_lock           pthread_mutex_lock
285#define  adb_mutex_unlock         pthread_mutex_unlock
286#define  adb_mutex_destroy        pthread_mutex_destroy
287
288#define  ADB_MUTEX_DEFINE(m)      adb_mutex_t   m = PTHREAD_MUTEX_INITIALIZER
289
290#define  adb_cond_t               pthread_cond_t
291#define  adb_cond_init            pthread_cond_init
292#define  adb_cond_wait            pthread_cond_wait
293#define  adb_cond_broadcast       pthread_cond_broadcast
294#define  adb_cond_signal          pthread_cond_signal
295#define  adb_cond_destroy         pthread_cond_destroy
296
297/* declare all mutexes */
298#define  ADB_MUTEX(x)   extern adb_mutex_t  x;
299#include "mutex_list.h"
300
301static __inline__ void  close_on_exec(int  fd)
302{
303    fcntl( fd, F_SETFD, FD_CLOEXEC );
304}
305
306static __inline__ int  unix_open(const char*  path, int options,...)
307{
308    if ((options & O_CREAT) == 0)
309    {
310        return  TEMP_FAILURE_RETRY( open(path, options) );
311    }
312    else
313    {
314        int      mode;
315        va_list  args;
316        va_start( args, options );
317        mode = va_arg( args, int );
318        va_end( args );
319        return TEMP_FAILURE_RETRY( open( path, options, mode ) );
320    }
321}
322
323static __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode )
324{
325    return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
326}
327
328
329static __inline__ int  adb_open( const char*  pathname, int  options )
330{
331    int  fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
332    if (fd < 0)
333        return -1;
334    close_on_exec( fd );
335    return fd;
336}
337#undef   open
338#define  open    ___xxx_open
339
340static __inline__ int  adb_shutdown(int fd)
341{
342    return shutdown(fd, SHUT_RDWR);
343}
344#undef   shutdown
345#define  shutdown   ____xxx_shutdown
346
347static __inline__ int  adb_close(int fd)
348{
349    return close(fd);
350}
351#undef   close
352#define  close   ____xxx_close
353
354
355static __inline__  int  adb_read(int  fd, void*  buf, size_t  len)
356{
357    return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
358}
359
360#undef   read
361#define  read  ___xxx_read
362
363static __inline__  int  adb_write(int  fd, const void*  buf, size_t  len)
364{
365    return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
366}
367#undef   write
368#define  write  ___xxx_write
369
370static __inline__ int   adb_lseek(int  fd, int  pos, int  where)
371{
372    return lseek(fd, pos, where);
373}
374#undef   lseek
375#define  lseek   ___xxx_lseek
376
377static __inline__  int    adb_unlink(const char*  path)
378{
379    return  unlink(path);
380}
381#undef  unlink
382#define unlink  ___xxx_unlink
383
384static __inline__  int  adb_creat(const char*  path, int  mode)
385{
386    int  fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
387
388    if ( fd < 0 )
389        return -1;
390
391    close_on_exec(fd);
392    return fd;
393}
394#undef   creat
395#define  creat  ___xxx_creat
396
397static __inline__ int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen)
398{
399    int fd;
400
401    fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
402    if (fd >= 0)
403        close_on_exec(fd);
404
405    return fd;
406}
407
408#undef   accept
409#define  accept  ___xxx_accept
410
411#define  unix_read   adb_read
412#define  unix_write  adb_write
413#define  unix_close  adb_close
414
415typedef  pthread_t                 adb_thread_t;
416
417typedef void*  (*adb_thread_func_t)( void*  arg );
418
419static __inline__ int  adb_thread_create( adb_thread_t  *pthread, adb_thread_func_t  start, void*  arg )
420{
421    pthread_attr_t   attr;
422
423    pthread_attr_init (&attr);
424    pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
425
426    return pthread_create( pthread, &attr, start, arg );
427}
428
429static __inline__  int  adb_socket_setbufsize( int   fd, int  bufsize )
430{
431    int opt = bufsize;
432    return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
433}
434
435static __inline__ void  disable_tcp_nagle(int fd)
436{
437    int  on = 1;
438    setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
439}
440
441
442static __inline__ int  unix_socketpair( int  d, int  type, int  protocol, int sv[2] )
443{
444    return socketpair( d, type, protocol, sv );
445}
446
447static __inline__ int  adb_socketpair( int  sv[2] )
448{
449    int  rc;
450
451    rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
452    if (rc < 0)
453        return -1;
454
455    close_on_exec( sv[0] );
456    close_on_exec( sv[1] );
457    return 0;
458}
459
460#undef   socketpair
461#define  socketpair   ___xxx_socketpair
462
463static __inline__ void  adb_sleep_ms( int  mseconds )
464{
465    usleep( mseconds*1000 );
466}
467
468static __inline__ int  adb_mkdir(const char*  path, int mode)
469{
470    return mkdir(path, mode);
471}
472#undef   mkdir
473#define  mkdir  ___xxx_mkdir
474
475static __inline__ void  adb_sysdeps_init(void)
476{
477}
478
479static __inline__ char*  adb_dirstart(const char*  path)
480{
481    return strchr(path, '/');
482}
483
484static __inline__ char*  adb_dirstop(const char*  path)
485{
486    return strrchr(path, '/');
487}
488
489static __inline__  int  adb_is_absolute_host_path( const char*  path )
490{
491    return path[0] == '/';
492}
493
494#endif /* !_WIN32 */
495
496#endif /* _ADB_SYSDEPS_H */
497