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
19 FILE_LICENCE ( GPL2_OR_LATER );
20
21 /** @file
22 *
23 * Implementation of most of the linux API.
24 */
25
26 #include <linux_api.h>
27
28 #include <stdarg.h>
29 #include <asm/unistd.h>
30 #include <string.h>
31
linux_open(const char * pathname,int flags)32 int linux_open ( const char *pathname, int flags ) {
33 return linux_syscall ( __NR_open, pathname, flags );
34 }
35
linux_close(int fd)36 int linux_close ( int fd ) {
37 return linux_syscall ( __NR_close, fd );
38 }
39
linux_lseek(int fd,off_t offset,int whence)40 off_t linux_lseek ( int fd, off_t offset, int whence ) {
41 return linux_syscall ( __NR_lseek, fd, offset, whence );
42 }
43
linux_read(int fd,void * buf,__kernel_size_t count)44 __kernel_ssize_t linux_read ( int fd, void *buf, __kernel_size_t count ) {
45 return linux_syscall ( __NR_read, fd, buf, count );
46 }
47
linux_write(int fd,const void * buf,__kernel_size_t count)48 __kernel_ssize_t linux_write ( int fd, const void *buf,
49 __kernel_size_t count ) {
50 return linux_syscall ( __NR_write, fd, buf, count );
51 }
52
linux_fcntl(int fd,int cmd,...)53 int linux_fcntl ( int fd, int cmd, ... ) {
54 long arg;
55 va_list list;
56
57 va_start ( list, cmd );
58 arg = va_arg ( list, long );
59 va_end ( list );
60
61 return linux_syscall ( __NR_fcntl, fd, cmd, arg );
62 }
63
linux_ioctl(int fd,int request,...)64 int linux_ioctl ( int fd, int request, ... ) {
65 void *arg;
66 va_list list;
67
68 va_start ( list, request );
69 arg = va_arg ( list, void * );
70 va_end ( list );
71
72 return linux_syscall ( __NR_ioctl, fd, request, arg );
73 }
74
linux_poll(struct pollfd * fds,nfds_t nfds,int timeout)75 int linux_poll ( struct pollfd *fds, nfds_t nfds, int timeout ) {
76 return linux_syscall ( __NR_poll, fds, nfds, timeout );
77 }
78
linux_nanosleep(const struct timespec * req,struct timespec * rem)79 int linux_nanosleep ( const struct timespec *req, struct timespec *rem ) {
80 return linux_syscall ( __NR_nanosleep, req, rem );
81 }
82
linux_usleep(useconds_t usec)83 int linux_usleep ( useconds_t usec ) {
84 struct timespec ts = {
85 .tv_sec = ( ( long ) ( usec / 1000000 ) ),
86 .tv_nsec = ( ( long ) ( usec % 1000000 ) * 1000UL ),
87 };
88
89 return linux_nanosleep ( &ts, NULL );
90 }
91
linux_gettimeofday(struct timeval * tv,struct timezone * tz)92 int linux_gettimeofday ( struct timeval *tv, struct timezone *tz ) {
93 return linux_syscall ( __NR_gettimeofday, tv, tz );
94 }
95
linux_mmap(void * addr,__kernel_size_t length,int prot,int flags,int fd,__kernel_off_t offset)96 void * linux_mmap ( void *addr, __kernel_size_t length, int prot, int flags,
97 int fd, __kernel_off_t offset ) {
98 return ( void * ) linux_syscall ( __SYSCALL_mmap, addr, length, prot,
99 flags, fd, offset );
100 }
101
linux_mremap(void * old_address,__kernel_size_t old_size,__kernel_size_t new_size,int flags)102 void * linux_mremap ( void *old_address, __kernel_size_t old_size,
103 __kernel_size_t new_size, int flags ) {
104 return ( void * ) linux_syscall ( __NR_mremap, old_address, old_size,
105 new_size, flags );
106 }
107
linux_munmap(void * addr,__kernel_size_t length)108 int linux_munmap ( void *addr, __kernel_size_t length ) {
109 return linux_syscall ( __NR_munmap, addr, length );
110 }
111
linux_socket(int domain,int type_,int protocol)112 int linux_socket ( int domain, int type_, int protocol ) {
113 #ifdef __NR_socket
114 return linux_syscall ( __NR_socket, domain, type_, protocol );
115 #else
116 #ifndef SOCKOP_socket
117 # define SOCKOP_socket 1
118 #endif
119 unsigned long sc_args[] = { domain, type_, protocol };
120 return linux_syscall ( __NR_socketcall, SOCKOP_socket, sc_args );
121 #endif
122 }
123
linux_bind(int fd,const struct sockaddr * addr,socklen_t addrlen)124 int linux_bind ( int fd, const struct sockaddr *addr, socklen_t addrlen ) {
125 #ifdef __NR_bind
126 return linux_syscall ( __NR_bind, fd, addr, addrlen );
127 #else
128 #ifndef SOCKOP_bind
129 # define SOCKOP_bind 2
130 #endif
131 unsigned long sc_args[] = { fd, (unsigned long)addr, addrlen };
132 return linux_syscall ( __NR_socketcall, SOCKOP_bind, sc_args );
133 #endif
134 }
135
linux_sendto(int fd,const void * buf,size_t len,int flags,const struct sockaddr * daddr,socklen_t addrlen)136 ssize_t linux_sendto ( int fd, const void *buf, size_t len, int flags,
137 const struct sockaddr *daddr, socklen_t addrlen ) {
138 #ifdef __NR_sendto
139 return linux_syscall ( __NR_sendto, fd, buf, len, flags,
140 daddr, addrlen );
141 #else
142 #ifndef SOCKOP_sendto
143 # define SOCKOP_sendto 11
144 #endif
145 unsigned long sc_args[] = { fd, (unsigned long)buf, len,
146 flags, (unsigned long)daddr, addrlen };
147 return linux_syscall ( __NR_socketcall, SOCKOP_sendto, sc_args );
148 #endif
149 }
150