From 69acdad3196960ac0a23e87466703c8e3b6a93fd Mon Sep 17 00:00:00 2001 From: miker Date: Fri, 13 Jul 2007 03:15:51 +0000 Subject: [PATCH] Patch from Scott McKellar, with modifications, to remove unused code and provide some memory protection: This patch contains one minor change and one less minor change. -------- The less minor change is the elimination of some old code that once opened a pipe to send commands to bash. According to Bill Erickson in a private email, that code is a leftover remnant from an experiment and may be removed. Before a patch was applied several days ago, things worked like this: IF you compiled srfsh with a certain macro #defined, AND you entered a command that was invalid to srfsh, THEN srfsh would pipe the command to bash for execution. It is doubtful that anyone but Bill ever used this variation, but if anyone wants to keep it around, then this is his or her chance to protest. Before protesting, however, please note that the shell escape mechanism now provides shell-like command processing. I.e. if you start the srfsh command line with an exclamation point, srfsh passes the rest of the command to the default shell (normally /bin/sh) for execution. The command so passed can use environmental variables, pipes, IO redirection, wild card expansion, and most of the sorts of things you're used to from the shell command line. Note that not all shells behave the same. If you're used to using tcsh as your shell, for example, you may find that sh won't work quite the same way. For now, at least, that's just too bad, but that's the way shell escapes typically work. It is probably possible to invoke the user's default shell as identified by the environmental variable $SHELL. However that nicety would not be trivial to code. ---------- The minor change is that, after building an array of pointers pointing to tokens from the input command, I set the next pointer to NULL so that it mark the end of the token list. (miker: I changed to calloc/free instead of an array, but left the final NULL in place.) git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1030 9efc2488-bf62-4759-914b-345cdb29e865 --- src/srfsh/srfsh.c | 116 +++++++++++----------------------------------- 1 file changed, 26 insertions(+), 90 deletions(-) diff --git a/src/srfsh/srfsh.c b/src/srfsh/srfsh.c index a179fc1..872e87c 100644 --- a/src/srfsh/srfsh.c +++ b/src/srfsh/srfsh.c @@ -50,7 +50,6 @@ static int handle_router( char* words[] ); /* handles app level requests */ static int handle_request( char* words[], int relay ); -//static int handle_exec(char* words[], int new_shell); static int handle_set( char* words[]); static int handle_print( char* words[]); static int send_request( char* server, @@ -72,9 +71,6 @@ static int handle_login( char* words[]); static int recv_timeout = 120; static int is_from_script = 0; -static FILE* shell_writer = NULL; -// static FILE* shell_reader = NULL; - int main( int argc, char* argv[] ) { @@ -121,10 +117,6 @@ int main( int argc, char* argv[] ) { client = osrf_system_get_transport_client(); - /* open the shell handle */ - shell_writer = popen( "bash", "w"); - //shell_reader = popen( "bash", "r"); - /* main process loop */ char* request; while((request=readline(prompt))) { @@ -142,7 +134,6 @@ int main( int argc, char* argv[] ) { free(request); free(req_copy); - fflush(shell_writer); fflush(stderr); fflush(stdout); } @@ -212,31 +203,40 @@ static int parse_request( char* request ) { if( request == NULL ) return 0; - char * original_request = strdup( request ); + char* original_request = strdup( request ); + char** words = calloc(COMMAND_BUFSIZE, sizeof(char*)); int ret_val = 0; int i = 0; - char* words[COMMAND_BUFSIZE]; - memset(words,0,COMMAND_BUFSIZE); - char* req = request; + + char* req = request; char* cur_tok = strtok( req, " " ); if( cur_tok == NULL ) { free( original_request ); + free( words ); return 0; } + /* Load an array with pointers to */ + /* the tokens as defined by strtok() */ + while(cur_tok != NULL) { - words[i++] = cur_tok; - cur_tok = strtok( NULL, " " ); + if( i < COMMAND_BUFSIZE - 1 ) { + words[i++] = cur_tok; + cur_tok = strtok( NULL, " " ); + } else { + fprintf( stderr, "Too many tokens in command\n" ); + free( original_request ); + free( words ); + return 1; + } } - - // not sure why (strtok?), but this is necessary - memset( words + i, 0, COMMAND_BUFSIZE - i ); - + words[i] = NULL; + /* pass off to the top level command */ if( !strcmp(words[0],"router") ) ret_val = handle_router( words ); @@ -271,23 +271,19 @@ static int parse_request( char* request ) { ret_val = handle_login(words); else if (words[0][0] == '!') { - //ret_val = handle_exec( words, 1 ); system( original_request + 1 ); ret_val = 1; } - free( original_request ); - if(!ret_val) { - #ifdef EXEC_DEFAULT - return handle_exec( words, 0 ); - #else - return parse_error( words ); - #endif - } - - return 1; + if(!ret_val) + ret_val = parse_error( words ); + else + ret_val = 1; + free( original_request ); + free( words ); + return ret_val; } @@ -484,66 +480,6 @@ static int handle_router( char* words[] ) { } -/* if new shell, spawn a new child and subshell to do the work, - otherwise pipe the request to the currently open (piped) shell */ -/* -static int handle_exec(char* words[], int new_shell) { - - if(!words[0]) return 0; - - if( words[0] && words[0][0] == '!') { - int len = strlen(words[0]); - char command[len]; - memset(command,0,len); - - int i; // chop out the ! - for( i=1; i!= len; i++) { - command[i-1] = words[0][i]; - } - - free(words[0]); - words[0] = strdup(command); - } - - if(new_shell) { - signal(SIGCHLD, sig_child_handler); - - if(fork()) { - - waitpid(-1, 0, 0); - if(child_dead) { - signal(SIGCHLD,sig_child_handler); - child_dead = 0; - } - - } else { - execvp( words[0], words ); - exit(0); - } - - } else { - - - growing_buffer* b = buffer_init(64); - int i = 0; - while(words[i]) - buffer_fadd( b, "%s ", words[i++] ); - - buffer_add( b, "\n"); - - fprintf( shell_writer, b->buf ); - buffer_free(b); - - fflush(shell_writer); - usleep(1000); - - } - - - return 1; -} -*/ - static int handle_request( char* words[], int relay ) { -- 2.43.2