You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

linux_api.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * Implementation of most of the linux API.
  22. */
  23. #include <linux_api.h>
  24. #include <stdarg.h>
  25. #include <asm/unistd.h>
  26. #include <string.h>
  27. int linux_open ( const char *pathname, int flags ) {
  28. return linux_syscall ( __NR_open, pathname, flags );
  29. }
  30. int linux_close ( int fd ) {
  31. return linux_syscall ( __NR_close, fd );
  32. }
  33. off_t linux_lseek ( int fd, off_t offset, int whence ) {
  34. return linux_syscall ( __NR_lseek, fd, offset, whence );
  35. }
  36. __kernel_ssize_t linux_read ( int fd, void *buf, __kernel_size_t count ) {
  37. return linux_syscall ( __NR_read, fd, buf, count );
  38. }
  39. __kernel_ssize_t linux_write ( int fd, const void *buf,
  40. __kernel_size_t count ) {
  41. return linux_syscall ( __NR_write, fd, buf, count );
  42. }
  43. int linux_fcntl ( int fd, int cmd, ... ) {
  44. long arg;
  45. va_list list;
  46. va_start ( list, cmd );
  47. arg = va_arg ( list, long );
  48. va_end ( list );
  49. return linux_syscall ( __NR_fcntl, fd, cmd, arg );
  50. }
  51. int linux_ioctl ( int fd, int request, ... ) {
  52. void *arg;
  53. va_list list;
  54. va_start ( list, request );
  55. arg = va_arg ( list, void * );
  56. va_end ( list );
  57. return linux_syscall ( __NR_ioctl, fd, request, arg );
  58. }
  59. int linux_poll ( struct pollfd *fds, nfds_t nfds, int timeout ) {
  60. return linux_syscall ( __NR_poll, fds, nfds, timeout );
  61. }
  62. int linux_nanosleep ( const struct timespec *req, struct timespec *rem ) {
  63. return linux_syscall ( __NR_nanosleep, req, rem );
  64. }
  65. int linux_usleep ( useconds_t usec ) {
  66. struct timespec ts = {
  67. .tv_sec = ( ( long ) ( usec / 1000000 ) ),
  68. .tv_nsec = ( ( long ) ( usec % 1000000 ) * 1000UL ),
  69. };
  70. return linux_nanosleep ( &ts, NULL );
  71. }
  72. int linux_gettimeofday ( struct timeval *tv, struct timezone *tz ) {
  73. return linux_syscall ( __NR_gettimeofday, tv, tz );
  74. }
  75. void * linux_mmap ( void *addr, __kernel_size_t length, int prot, int flags,
  76. int fd, __kernel_off_t offset ) {
  77. return ( void * ) linux_syscall ( __SYSCALL_mmap, addr, length, prot,
  78. flags, fd, offset );
  79. }
  80. void * linux_mremap ( void *old_address, __kernel_size_t old_size,
  81. __kernel_size_t new_size, int flags ) {
  82. return ( void * ) linux_syscall ( __NR_mremap, old_address, old_size,
  83. new_size, flags );
  84. }
  85. int linux_munmap ( void *addr, __kernel_size_t length ) {
  86. return linux_syscall ( __NR_munmap, addr, length );
  87. }
  88. int linux_socket ( int domain, int type_, int protocol ) {
  89. #ifdef __NR_socket
  90. return linux_syscall ( __NR_socket, domain, type_, protocol );
  91. #else
  92. #ifndef SOCKOP_socket
  93. # define SOCKOP_socket 1
  94. #endif
  95. unsigned long sc_args[] = { domain, type_, protocol };
  96. return linux_syscall ( __NR_socketcall, SOCKOP_socket, sc_args );
  97. #endif
  98. }
  99. int linux_bind ( int fd, const struct sockaddr *addr, socklen_t addrlen ) {
  100. #ifdef __NR_bind
  101. return linux_syscall ( __NR_bind, fd, addr, addrlen );
  102. #else
  103. #ifndef SOCKOP_bind
  104. # define SOCKOP_bind 2
  105. #endif
  106. unsigned long sc_args[] = { fd, (unsigned long)addr, addrlen };
  107. return linux_syscall ( __NR_socketcall, SOCKOP_bind, sc_args );
  108. #endif
  109. }
  110. ssize_t linux_sendto ( int fd, const void *buf, size_t len, int flags,
  111. const struct sockaddr *daddr, socklen_t addrlen ) {
  112. #ifdef __NR_sendto
  113. return linux_syscall ( __NR_sendto, fd, buf, len, flags,
  114. daddr, addrlen );
  115. #else
  116. #ifndef SOCKOP_sendto
  117. # define SOCKOP_sendto 11
  118. #endif
  119. unsigned long sc_args[] = { fd, (unsigned long)buf, len,
  120. flags, (unsigned long)daddr, addrlen };
  121. return linux_syscall ( __NR_socketcall, SOCKOP_sendto, sc_args );
  122. #endif
  123. }