2020-01-18 09:38:21 +01:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
*
* 1. Redistributions of source code must retain the above copyright notice , this
* list of conditions and the following disclaimer .
*
* 2. Redistributions in binary form must reproduce the above copyright notice ,
* this list of conditions and the following disclaimer in the documentation
* and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
* AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL
* DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
* OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2021-02-11 21:08:46 +01:00
# include <LibCore/ArgsParser.h>
2019-09-12 11:26:54 -03:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
2021-02-05 12:16:30 +01:00
# include <syscall.h>
2019-09-12 11:26:54 -03:00
# define SC_NARG 4
2020-03-08 10:36:51 +01:00
FlatPtr arg [ SC_NARG ] ;
2019-09-12 11:26:54 -03:00
char buf [ BUFSIZ ] ;
2021-02-11 21:08:46 +01:00
static FlatPtr parse ( const char * s ) ;
2019-09-12 11:26:54 -03:00
int main ( int argc , char * * argv )
{
2021-02-11 21:08:46 +01:00
bool output_buffer = false ;
bool list_syscalls = false ;
Vector < const char * > arguments ;
Core : : ArgsParser args_parser ;
args_parser . add_option ( output_buffer , " Output the contents of the buffer (beware of stray zero bytes!) " , " output-buffer " , ' o ' ) ;
args_parser . add_option ( list_syscalls , " List all existing syscalls " , " list-syscalls " , ' l ' ) ;
args_parser . add_positional_argument ( arguments , " Syscall arguments (can be strings, 'buf' for the output buffer, or numbers like 1234 or 0xffffffff) " , " syscall-arguments " ) ;
args_parser . parse ( argc , argv ) ;
for ( size_t i = 0 ; i < arguments . size ( ) ; i + + ) {
arg [ i ] = parse ( arguments [ i ] ) ;
2019-09-12 11:26:54 -03:00
}
2021-02-11 21:08:46 +01:00
for ( int sc = 0 ; sc < Syscall : : Function : : __Count ; + + sc ) {
if ( strcmp ( Syscall : : to_string ( ( Syscall : : Function ) sc ) , ( char * ) arg [ 0 ] ) = = 0 ) {
2019-09-12 11:26:54 -03:00
int rc = syscall ( sc , arg [ 1 ] , arg [ 2 ] , arg [ 3 ] ) ;
if ( rc = = - 1 ) {
perror ( " syscall " ) ;
} else {
2021-02-11 21:08:46 +01:00
if ( output_buffer )
2019-12-24 09:37:41 -03:00
fwrite ( buf , 1 , sizeof ( buf ) , stdout ) ;
2019-09-12 11:26:54 -03:00
}
fprintf ( stderr , " Syscall return: %d \n " , rc ) ;
return 0 ;
}
}
fprintf ( stderr , " Invalid syscall entry %s \n " , ( char * ) arg [ 0 ] ) ;
return - 1 ;
}
2021-02-11 21:08:46 +01:00
FlatPtr parse ( const char * s )
2019-09-12 11:26:54 -03:00
{
char * t ;
2020-03-08 10:36:51 +01:00
FlatPtr l ;
2019-09-12 11:26:54 -03:00
if ( strcmp ( s , " buf " ) = = 0 ) {
2020-03-08 10:36:51 +01:00
return ( FlatPtr ) buf ;
2019-09-12 11:26:54 -03:00
}
l = strtoul ( s , & t , 0 ) ;
if ( t > s & & * t = = 0 ) {
return l ;
}
2020-03-08 10:36:51 +01:00
return ( FlatPtr ) s ;
2019-09-12 11:26:54 -03:00
}