sysdeps.h revision 298b6c70a084e87e688d19d1b3d609410121cf1d
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 <winsock2.h>
30#include <windows.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#include <direct.h>
39
40#define OS_PATH_SEPARATOR '\\'
41#define OS_PATH_SEPARATOR_STR "\\"
42#define ENV_PATH_SEPARATOR_STR ";"
43
44typedef CRITICAL_SECTION          adb_mutex_t;
45
46#define  ADB_MUTEX_DEFINE(x)     adb_mutex_t   x
47
48/* declare all mutexes */
49/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
50#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{
73    thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
74    if (thread->tid == (unsigned)-1L) {
75        return -1;
76    }
77    return 0;
78}
79
80static __inline__  unsigned long adb_thread_id()
81{
82    return GetCurrentThreadId();
83}
84
85static __inline__ void  close_on_exec(int  fd)
86{
87    /* nothing really */
88}
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{
114	return _mkdir(path);
115}
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);
124extern int  adb_shutdown(int  fd);
125extern 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{
150    return adb_open(path, options);
151}
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);
178extern int socket_network_client_timeout(const char *host, int port, int type,
179                                         int timeout);
180extern int socket_loopback_server(int port, int type);
181extern int socket_inaddr_any_server(int port, int type);
182
183/* normally provided by "fdevent.h" */
184
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;
208    int force_eof;
209
210    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{
219    Sleep( mseconds );
220}
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
266extern char*  adb_strtok_r(char *str, const char *delim, char **saveptr);
267
268#else /* !_WIN32 a.k.a. Unix */
269
270#include "fdevent.h"
271#include <cutils/sockets.h>
272#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>
285#include <unistd.h>
286
287/*
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
302#define OS_PATH_SEPARATOR '/'
303#define OS_PATH_SEPARATOR_STR "/"
304#define ENV_PATH_SEPARATOR_STR ":"
305
306typedef  pthread_mutex_t          adb_mutex_t;
307
308#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
314#define  ADB_MUTEX_DEFINE(m)      adb_mutex_t   m = PTHREAD_MUTEX_INITIALIZER
315
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
323/* declare all mutexes */
324#define  ADB_MUTEX(x)   extern adb_mutex_t  x;
325#include "mutex_list.h"
326
327static __inline__ void  close_on_exec(int  fd)
328{
329    fcntl( fd, F_SETFD, FD_CLOEXEC );
330}
331
332static __inline__ int  unix_open(const char*  path, int options,...)
333{
334    if ((options & O_CREAT) == 0)
335    {
336        return  TEMP_FAILURE_RETRY( open(path, options) );
337    }
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 );
345        return TEMP_FAILURE_RETRY( open( path, options, mode ) );
346    }
347}
348
349static __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode )
350{
351    return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
352}
353
354
355static __inline__ int  adb_open( const char*  pathname, int  options )
356{
357    int  fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
358    if (fd < 0)
359        return -1;
360    close_on_exec( fd );
361    return fd;
362}
363#undef   open
364#define  open    ___xxx_open
365
366static __inline__ int  adb_shutdown(int fd)
367{
368    return shutdown(fd, SHUT_RDWR);
369}
370#undef   shutdown
371#define  shutdown   ____xxx_shutdown
372
373static __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{
383    return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
384}
385
386#undef   read
387#define  read  ___xxx_read
388
389static __inline__  int  adb_write(int  fd, const void*  buf, size_t  len)
390{
391    return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
392}
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{
412    int  fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
413
414    if ( fd < 0 )
415        return -1;
416
417    close_on_exec(fd);
418    return fd;
419}
420#undef   creat
421#define  creat  ___xxx_creat
422
423static __inline__ int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen)
424{
425    int fd;
426
427    fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
428    if (fd >= 0)
429        close_on_exec(fd);
430
431    return fd;
432}
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{
491    usleep( mseconds*1000 );
492}
493
494static __inline__ int  adb_mkdir(const char*  path, int mode)
495{
496    return mkdir(path, mode);
497}
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
520static __inline__ char*  adb_strtok_r(char *str, const char *delim, char **saveptr)
521{
522    return strtok_r(str, delim, saveptr);
523}
524
525static __inline__ unsigned long adb_thread_id()
526{
527    return (unsigned long)pthread_self();
528}
529
530#undef   strtok_r
531#define  strtok_r  ___xxx_strtok_r
532
533#endif /* !_WIN32 */
534
535#endif /* _ADB_SYSDEPS_H */
536