From 0c32c26e8bbcd3c644ce8b2924d2a1f2c7dc6680 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 9 Oct 2009 13:06:19 -0700 Subject: [PATCH] moved AXIS stuff into euca_axis.[ch] to avoid pollution --- cluster/CCclient.c | 2 +- cluster/Makefile | 6 +- cluster/handlers.c | 2 +- net/vnetwork.c | 4 ++ node/Makefile | 6 +- node/NCclient.c | 3 +- node/handlers_kvm.c | 2 + node/handlers_xen.c | 2 + node/test_nc.c | 1 + storage/Makefile | 10 ++- util/Makefile | 2 +- util/euca_auth.c | 168 +------------------------------------------- util/euca_auth.h | 15 ---- util/ipc.c | 25 +++++-- util/ipc.h | 2 + util/misc.c | 127 ++++++++++++++------------------- util/misc.h | 11 +-- 17 files changed, 105 insertions(+), 283 deletions(-) diff --git a/cluster/CCclient.c b/cluster/CCclient.c index 4a070751916..dfdab5ef361 100644 --- a/cluster/CCclient.c +++ b/cluster/CCclient.c @@ -60,7 +60,7 @@ permission notice: #include #include #include - +#include #include #ifndef MODE diff --git a/cluster/Makefile b/cluster/Makefile index 9dd7e276646..c3a3cba46a9 100644 --- a/cluster/Makefile +++ b/cluster/Makefile @@ -16,7 +16,7 @@ CLIENTKILLALL=euca_killall NCLIBS=../util/data.o ../node/client-marshal-adb.o ../util/ipc.o SCLIBS=../storage/storage.o VNLIBS=../net/vnetwork.o ../util/misc.o -WSSECLIBS=../util/euca_auth.o +WSSECLIBS=../util/euca_axis.o CC_LIBS = ${LIBS} -lcurl all: server client @@ -50,10 +50,10 @@ $(SERVICE_SO): generated/stubs ../storage/storage.o server-marshal.o handlers.o client: $(CLIENT)_full $(CLIENTKILLALL) $(CLIENT)_full: generated/stubs $(CLIENT).c cc-client-marshal-adb.c handlers.o $(AXIS2C_HOME)/lib/libaxutil.so - $(CC) -o $(CLIENT)_full $(CFLAGS) $(INCLUDES) $(CC_LIBS) generated/adb_*.o generated/axis2_stub_*.o ../util/misc.o ../util/euca_auth.o $(CLIENT).c cc-client-marshal-adb.c -DMODE=1 + $(CC) -o $(CLIENT)_full $(CFLAGS) $(INCLUDES) $(CC_LIBS) generated/adb_*.o generated/axis2_stub_*.o ../util/misc.o $(WSSECLIBS) ../util/euca_auth.o $(CLIENT).c cc-client-marshal-adb.c -DMODE=1 $(CLIENTKILLALL): generated/stubs $(CLIENT).c cc-client-marshal-adb.c handlers.o $(AXIS2C_HOME)/lib/libaxutil.so - $(CC) -o $(CLIENTKILLALL) $(CFLAGS) $(INCLUDES) $(CC_LIBS) generated/adb_*.o generated/axis2_stub_*.o ../util/misc.o ../util/euca_auth.o $(CLIENT).c cc-client-marshal-adb.c -DMODE=0 + $(CC) -o $(CLIENTKILLALL) $(CFLAGS) $(INCLUDES) $(CC_LIBS) generated/adb_*.o generated/axis2_stub_*.o ../util/misc.o $(WSSECLIBS) ../util/euca_auth.o $(CLIENT).c cc-client-marshal-adb.c -DMODE=0 deploy: $(INSTALL) -d $(AXIS2C_SERVICES)/$(SERVICE_NAME)/ diff --git a/cluster/handlers.c b/cluster/handlers.c index 296bd4253ac..e056cd0b081 100644 --- a/cluster/handlers.c +++ b/cluster/handlers.c @@ -75,9 +75,9 @@ permission notice: #include #include #include -#include #include +#include #include "data.h" #include "client-marshal.h" diff --git a/net/vnetwork.c b/net/vnetwork.c index 1259edb7e5c..0dda9b679b2 100644 --- a/net/vnetwork.c +++ b/net/vnetwork.c @@ -73,6 +73,10 @@ permission notice: #include #include #include +#include /* log2 */ +#include +#include +#include #include #include diff --git a/node/Makefile b/node/Makefile index f1974914b4a..7b2cacdce59 100644 --- a/node/Makefile +++ b/node/Makefile @@ -57,17 +57,17 @@ server: $(SERVICE_SO) make -C ../util $(SERVICE_SO): generated/stubs ../storage/storage.o ../storage/walrus.o ../net/vnetwork.o ../util/misc.o ../util/data.o server-marshal.o handlers.o $(NC_HANDLERS) $(AXIS2C_HOME)/lib/libaxutil.so ../util/eucalyptus.h ../util/euca_auth.o - $(CC) -o $(SERVICE_SO) -shared $(NC_LIBS) generated/adb_*.o generated/axis2*_skel_*.o server-marshal.o handlers.o $(NC_HANDLERS) ../storage/*.o ../net/*.o ../util/*.o + $(CC) -o $(SERVICE_SO) -shared $(NC_LIBS) generated/adb_*.o generated/axis2*_skel_*.o server-marshal.o handlers.o $(NC_HANDLERS) ../storage/storage.o ../storage/walrus.o ../net/*.o ../util/*.o clientlib: generated/stubs ../util/data.o client-marshal-adb.o client: $(CLIENT) $(CLIENT)_local $(CLIENT): generated/stubs ../util/misc.o ../util/data.o client-marshal-adb.o client-marshal-local.o $(AXIS2C_HOME)/lib/libaxutil.so $(CLIENT).c - $(CC) -o $(CLIENT) $(CFLAGS) $(INCLUDES) $(NC_LIBS) generated/adb_*.o generated/axis2_stub_*.o client-marshal-adb.o ../util/*.o ../storage/*.o ../net/*.o $(CLIENT).c + $(CC) -o $(CLIENT) $(CFLAGS) $(INCLUDES) $(NC_LIBS) generated/adb_*.o generated/axis2_stub_*.o client-marshal-adb.o ../util/*.o ../storage/storage.o ../storage/walrus.o ../net/*.o $(CLIENT).c $(CLIENT)_local: generated/stubs ../util/misc.o ../util/data.o client-marshal-adb.o client-marshal-local.o handlers.o $(NC_HANDLERS) $(CLIENT).c ../util/euca_auth.o - $(CC) -o $(CLIENT)_local $(INCLUDES) $(CFLAGS) client-marshal-local.o ../util/*.o ../storage/*.o ../net/*.o handlers.o $(NC_HANDLERS) $(CLIENT).c $(NC_LIBS) + $(CC) -o $(CLIENT)_local $(INCLUDES) $(CFLAGS) client-marshal-local.o ../util/*.o ../storage/storage.o ../storage/walrus.o ../net/*.o handlers.o $(NC_HANDLERS) $(CLIENT).c $(NC_LIBS) test: test.c ../util/misc.o ../util/data.o $(CC) $(CFLAGS) $(INCLUDES) $(NC_LIBS) -o test test.c ../util/misc.o ../util/data.o diff --git a/node/NCclient.c b/node/NCclient.c index 26f899fe591..f6dcdf42a85 100644 --- a/node/NCclient.c +++ b/node/NCclient.c @@ -61,7 +61,8 @@ permission notice: #include /* getopt */ #include "data.h" #include "client-marshal.h" -#include +#include "misc.h" +#include "euca_axis.h" #define NC_ENDPOINT "/axis2/services/EucalyptusNC" #define WALRUS_ENDPOINT "/services/Walrus" diff --git a/node/handlers_kvm.c b/node/handlers_kvm.c index b96f2c4a55f..65f8becf51e 100644 --- a/node/handlers_kvm.c +++ b/node/handlers_kvm.c @@ -69,6 +69,8 @@ permission notice: #include #include #include /* SIGINT */ +#include +#include #include "ipc.h" #include "misc.h" diff --git a/node/handlers_xen.c b/node/handlers_xen.c index db054d32446..20d6e895d67 100644 --- a/node/handlers_xen.c +++ b/node/handlers_xen.c @@ -69,6 +69,8 @@ permission notice: #include #include #include /* SIGINT */ +#include +#include #include "ipc.h" #include "misc.h" diff --git a/node/test_nc.c b/node/test_nc.c index 88eece596fa..de4ba21cd82 100644 --- a/node/test_nc.c +++ b/node/test_nc.c @@ -59,6 +59,7 @@ permission notice: */ #include #include +#include #include #include #include "misc.h" diff --git a/storage/Makefile b/storage/Makefile index ea369badf04..0de81177733 100644 --- a/storage/Makefile +++ b/storage/Makefile @@ -5,11 +5,15 @@ include ../Makedefs STORAGE_LIBS = ${LIBS} -lcurl +IMAGER_LIBS = ../util/euca_auth.o ../util/misc.o ../util/ipc.o walrus.o img.o cache.o ami2vmx.o do_convert.o -all: storage.o walrus.o Wclient +all: storage.o walrus.o Wclient euca_imager build: all +euca_imager: Makefile euca_imager.c $(IMAGER_LIBS) + $(CC) $(CFLAGS) $(INCLUDES) euca_imager.c -o euca_imager $(IMAGER_LIBS) -lcurl + Wclient: Makefile Wclient.c ../util/euca_auth.o ../util/misc.o walrus.o $(CC) $(CFLAGS) $(INCLUDES) Wclient.c -o Wclient ../util/euca_auth.o ../util/misc.o walrus.o $(STORAGE_LIBS) @@ -38,11 +42,11 @@ test: test.c storage.h storage.o ../util/misc.o ../util/data.o ../util/ipc.o ../ make -C ../util clean: - rm -rf *~ *.o Wclient + rm -rf *~ *.o Wclient euca_imager distclean: install: - + @$(INSTALL) -m 0755 euca_imager $(usrdir)/lib/eucalyptus deploy: diff --git a/util/Makefile b/util/Makefile index 9beafd9d5c5..3600b0a801a 100644 --- a/util/Makefile +++ b/util/Makefile @@ -4,7 +4,7 @@ include ../Makedefs -all: misc.o data.o euca_auth.o ipc.o euca_rootwrap euca_mountwrap +all: misc.o data.o euca_auth.o euca_axis.o ipc.o euca_rootwrap euca_mountwrap build: all diff --git a/util/euca_auth.c b/util/euca_auth.c index a3f3ebe42dd..0071e4ff4fb 100644 --- a/util/euca_auth.c +++ b/util/euca_auth.c @@ -57,30 +57,6 @@ permission notice: WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS. */ -/* BRIEF EXAMPLE MSG: -. - - [..snip..] - - [..snip..] - [..snip..] - [..snip..] - - - - - - - - - - - ... -. -*/ #include #include @@ -95,152 +71,10 @@ permission notice: #include #include #include + #include "euca_auth.h" #include "misc.h" /* get_string_stats, logprintf */ -#ifndef NO_AXIS /* for compiling on systems without Axis */ -#include "oxs_axiom.h" -#include "oxs_x509_cert.h" -#include "oxs_key_mgr.h" -#include "rampart_handler_util.h" -#include "rampart_sec_processed_result.h" -#include "rampart_error.h" -#include "axis2_op_ctx.h" -#include "rampart_context.h" - -#define NO_U_FAIL(x) do{ \ -AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "[rampart][eucalyptus-verify] " #x );\ -AXIS2_ERROR_SET(env->error, RAMPART_ERROR_FAILED_AUTHENTICATION, AXIS2_FAILURE);\ -return AXIS2_FAILURE; \ -}while(0) - -axis2_status_t __euca_authenticate(const axutil_env_t *env,axis2_msg_ctx_t *out_msg_ctx, axis2_op_ctx_t *op_ctx) -{ - //***** First get the message context before doing anything dumb w/ a NULL pointer *****/ - axis2_msg_ctx_t *msg_ctx = NULL; //<--- incoming msg context, it is NULL, see? - msg_ctx = axis2_op_ctx_get_msg_ctx(op_ctx, env, AXIS2_WSDL_MESSAGE_LABEL_IN); - - //***** Print everything from the security results, just for testing now *****// - rampart_context_t *rampart_context = NULL; - axutil_property_t *property = NULL; - - property = axis2_msg_ctx_get_property(msg_ctx, env, RAMPART_CONTEXT); - if(property) - { - rampart_context = (rampart_context_t *)axutil_property_get_value(property, env); - // AXIS2_LOG_CRITICAL(env->log,AXIS2_LOG_SI," ======== PRINTING PROCESSED WSSEC TOKENS ======== "); - rampart_print_security_processed_results_set(env,msg_ctx); - } - - //***** Extract Security Node from header from enveloper from msg_ctx *****// - axiom_soap_envelope_t *soap_envelope = NULL; - axiom_soap_header_t *soap_header = NULL; - axiom_node_t *sec_node = NULL; - - - soap_envelope = axis2_msg_ctx_get_soap_envelope(msg_ctx, env); - if(!soap_envelope) NO_U_FAIL("SOAP envelope cannot be found."); - soap_header = axiom_soap_envelope_get_header(soap_envelope, env); - if (!soap_header) NO_U_FAIL("SOAP header cannot be found."); - sec_node = rampart_get_security_header(env, msg_ctx, soap_header); // <---- here it is! - if(!sec_node)NO_U_FAIL("No node wsse:Security -- required: ws-security"); - - //***** Find the wsse:Reference to the BinarySecurityToken *****// - //** Path is: Security/ - //** *sec_node must be non-NULL, kkthx **// - axiom_node_t *sig_node = NULL; - axiom_node_t *key_info_node = NULL; - axiom_node_t *sec_token_ref_node = NULL; - /** the ds:Signature node **/ - sig_node = oxs_axiom_get_first_child_node_by_name(env,sec_node, OXS_NODE_SIGNATURE, OXS_DSIG_NS, OXS_DS ); - if(!sig_node)NO_U_FAIL("No node ds:Signature -- required: signature"); - /** the ds:KeyInfo **/ - key_info_node = oxs_axiom_get_first_child_node_by_name(env, sig_node, OXS_NODE_KEY_INFO, OXS_DSIG_NS, NULL ); - if(!key_info_node)NO_U_FAIL("No node ds:KeyInfo -- required: signature key"); - /** the wsse:SecurityTokenReference **/ - sec_token_ref_node = oxs_axiom_get_first_child_node_by_name(env, key_info_node,OXS_NODE_SECURITY_TOKEN_REFRENCE, OXS_WSSE_XMLNS, NULL); - if(!sec_token_ref_node)NO_U_FAIL("No node wsse:SecurityTokenReference -- required: signing token"); - //** in theory this is the branching point for supporting all kinds of tokens -- we only do BST Direct Reference **/ - - //***** Find the wsse:Reference to the BinarySecurityToken *****// - //** *sec_token_ref_node must be non-NULL **/ - axis2_char_t *ref = NULL; - axis2_char_t *ref_id = NULL; - axiom_node_t *token_ref_node = NULL; - axiom_node_t *bst_node = NULL; - /** the wsse:Reference node **/ - token_ref_node = oxs_axiom_get_first_child_node_by_name(env, sec_token_ref_node,OXS_NODE_REFERENCE, OXS_WSSE_XMLNS, NULL); - /** pull out the name of the BST node **/ - ref = oxs_token_get_reference(env, token_ref_node); - ref_id = axutil_string_substring_starting_at(axutil_strdup(env, ref), 1); - /** get the wsse:BinarySecurityToken used to sign the message **/ - bst_node = oxs_axiom_get_node_by_id(env, sec_node, "Id", ref_id, OXS_WSU_XMLNS); - if(!bst_node){oxs_error(env, OXS_ERROR_LOCATION, OXS_ERROR_ELEMENT_FAILED, "Error retrieving elementwith ID=%s", ref_id);NO_U_FAIL("Cant find the required node");} - - - //***** Find the wsse:Reference to the BinarySecurityToken *****// - //** *bst_node must be non-NULL **/ - axis2_char_t *data = NULL; - oxs_x509_cert_t *_cert = NULL; - oxs_x509_cert_t *recv_cert = NULL; - axis2_char_t *file_name = NULL; - axis2_char_t *recv_x509_buf = NULL; - axis2_char_t *msg_x509_buf = NULL; - - /** pull out the data from the BST **/ - data = oxs_axiom_get_node_content(env, bst_node); - /** create an oxs_X509_cert **/ - _cert = oxs_key_mgr_load_x509_cert_from_string(env, data); - if(_cert) - { - //***** FINALLY -- we have the certificate used to sign the message. authenticate it HERE *****// - msg_x509_buf = oxs_x509_cert_get_data(_cert,env); - if(!msg_x509_buf)NO_U_FAIL("OMG WHAT NOW?!"); - /* - recv_x509_buf = (axis2_char_t *)rampart_context_get_receiver_certificate(rampart_context, env); - if(recv_x509_buf) - recv_cert = oxs_key_mgr_load_x509_cert_from_string(env, recv_x509_buf); - else - { - file_name = rampart_context_get_receiver_certificate_file(rampart_context, env); - if(!file_name) NO_U_FAIL("Policy for the service is incorrect -- ReceiverCertificate is not set!!"); - if (check_file(file_name)) NO_U_FAIL("No cert file ($EUCALYPTUS/var/lib/eucalyptus/keys/cloud-cert.pem) found, failing"); - recv_cert = oxs_key_mgr_load_x509_cert_from_pem_file(env, file_name); - } - */ - - file_name = rampart_context_get_receiver_certificate_file(rampart_context, env); - if(!file_name) NO_U_FAIL("Policy for the service is incorrect -- ReceiverCertificate is not set!!"); - if (check_file(file_name)) NO_U_FAIL("No cert file ($EUCALYPTUS/var/lib/eucalyptus/keys/cloud-cert.pem) found, failing"); - recv_cert = oxs_key_mgr_load_x509_cert_from_pem_file(env, file_name); - - if (recv_cert) { - recv_x509_buf = oxs_x509_cert_get_data(recv_cert,env); - } else { - NO_U_FAIL("could not populate receiver cert"); - } - - if( axutil_strcmp(recv_x509_buf,msg_x509_buf)!=0){ - AXIS2_LOG_CRITICAL(env->log,AXIS2_LOG_SI," --------- Received x509 certificate value ---------" ); - AXIS2_LOG_CRITICAL(env->log,AXIS2_LOG_SI, msg_x509_buf ); - AXIS2_LOG_CRITICAL(env->log,AXIS2_LOG_SI," --------- Local x509 certificate value! ---------" ); - AXIS2_LOG_CRITICAL(env->log,AXIS2_LOG_SI, recv_x509_buf ); - AXIS2_LOG_CRITICAL(env->log,AXIS2_LOG_SI," ---------------------------------------------------" ); - NO_U_FAIL("The certificate specified is invalid!"); - } - } - else - { - oxs_error(env, OXS_ERROR_LOCATION, OXS_ERROR_DEFAULT, "Cannot load certificate from string =%s", data); - NO_U_FAIL("Failed to build certificate from BinarySecurityToken"); - } - oxs_x509_cert_free(_cert, env); - oxs_x509_cert_free(recv_cert, env); - return AXIS2_SUCCESS; - -} -#endif /* NO_AXIS */ - static int initialized = 0; #define FILENAME 512 diff --git a/util/euca_auth.h b/util/euca_auth.h index 8f85f57b868..9273447e178 100644 --- a/util/euca_auth.h +++ b/util/euca_auth.h @@ -60,21 +60,6 @@ permission notice: #ifndef INCLUDE_EUCA_AUTH_H #define INCLUDE_EUCA_AUTH_H -#ifndef NO_AXIS /* for compiling on systems without Axis */ -#include "oxs_axiom.h" -#include "oxs_x509_cert.h" -#include "oxs_key_mgr.h" -#include "rampart_handler_util.h" -#include "rampart_sec_processed_result.h" -#include "rampart_error.h" -#include "axis2_op_ctx.h" -#include "rampart_context.h" - -axis2_status_t __euca_authenticate(const axutil_env_t *env,axis2_msg_ctx_t *out_msg_ctx, axis2_op_ctx_t *op_ctx); - -#define euca_authenticate(a,b,c) do{ if( __euca_authenticate(a,b,c) == AXIS2_FAILURE ) return NULL; }while(0) -#endif /* NO_AXIS */ - /* * functions for Walrus clients */ diff --git a/util/ipc.c b/util/ipc.c index 4c1f90e2ebc..2b767aed486 100644 --- a/util/ipc.c +++ b/util/ipc.c @@ -65,6 +65,9 @@ permission notice: #include #include #include /* For O_* */ +#include +#include + #include "misc.h" /* logprintfl */ #include "ipc.h" @@ -73,7 +76,12 @@ permission notice: #define DECLARE_ARG union semun { int val; struct semid_ds *buf; ushort *array; } arg -sem * sem_alloc (const int val, const char * name) +sem * sem_alloc (const int val, const char * name) +{ + return sem_realloc (val, name, O_EXCL); +} + +sem * sem_realloc (const int val, const char * name, int flags) { DECLARE_ARG; @@ -81,12 +89,15 @@ sem * sem_alloc (const int val, const char * name) if (s==NULL) return NULL; bzero (s, sizeof (sem)); s->sysv = -1; - + s->flags = flags; + if (name) { /* named semaphores */ - if ( sem_unlink (name) == 0) { /* clean up in case previous sem holder crashed */ - logprintfl (EUCAINFO, "sem_alloc(): cleaning up old semaphore %s\n", name); + if (s->flags & O_EXCL) { + if ( sem_unlink (name) == 0) { /* clean up in case previous sem holder crashed */ + logprintfl (EUCAINFO, "sem_alloc(): cleaning up old semaphore %s\n", name); + } } - if ((s->posix = sem_open (name, O_CREAT | O_EXCL, 0644, val))==SEM_FAILED) { + if ((s->posix = sem_open (name, O_CREAT | flags, 0644, val))==SEM_FAILED) { free (s); return NULL; } @@ -146,7 +157,9 @@ void sem_free (sem * s) if (s && s->posix) { sem_close (s->posix); - sem_unlink (s->name); + if (s->flags & O_EXCL) { + sem_unlink (s->name); + } free (s->name); } diff --git a/util/ipc.h b/util/ipc.h index 6f902a20d71..8cdae7ea29a 100644 --- a/util/ipc.h +++ b/util/ipc.h @@ -68,8 +68,10 @@ typedef struct sem_struct { int sysv; sem_t * posix; char * name; + int flags; } sem; +sem * sem_realloc (const int val, const char * name, const int flags); sem * sem_alloc (const int val, const char * name); int sem_p (sem * s); int sem_v (sem * s); diff --git a/util/misc.c b/util/misc.c index f38c5b0922a..e1412fd4c6a 100644 --- a/util/misc.c +++ b/util/misc.c @@ -73,13 +73,11 @@ permission notice: #include /* open */ #include /* utime */ #include - -#ifndef NO_AXIS /* for compiling on systems without Axis */ -#include -#include -#include -#include -#include +#include +#include // opendir, etc +#include // errno +#include // gettimeofday +#include int verify_helpers(char **helpers, char **helpers_path, int LASTHELPER) { int i, done, rc, j; @@ -143,31 +141,6 @@ pid_t timewait(pid_t pid, int *status, int timeout) { return(rc); } -int InitWSSEC(axutil_env_t *env, axis2_stub_t *stub, char *policyFile) { - axis2_svc_client_t *svc_client = NULL; - neethi_policy_t *policy = NULL; - axis2_status_t status = AXIS2_FAILURE; - - //return(0); - - svc_client = axis2_stub_get_svc_client(stub, env); - if (!svc_client) { - logprintfl (EUCAERROR, "InitWSSEC(): ERROR could not get svc_client from stub\n"); - return(1); - } - axis2_svc_client_engage_module(svc_client, env, "rampart"); - - policy = neethi_util_create_policy_from_file(env, policyFile); - if (!policy) { - logprintfl (EUCAERROR, "InitWSSEC(): ERROR could not initialize policy file %s\n", policyFile); - return(1); - } - status = axis2_svc_client_set_policy(svc_client, env, policy); - - return(0); -} -#endif /* NO_AXIS */ - int timelog=0; /* change to 1 for TIMELOG entries */ int logging=0; @@ -386,53 +359,61 @@ int sscanf_lines (char * lines, char * format, void * varp) return found; } -/* execute system(shell_command) and return stdout in new string - * pointed to by *stringp */ -char * system_output (char * shell_command ) +char * fp2str (FILE * fp) { # define INCREMENT 512 - int buf_max = INCREMENT; - int buf_current = 0; - char * buf = NULL; - char * last_read; - FILE * fp; - - /* forks off command (this doesn't fail if command doesn't exist */ - logprintfl (EUCADEBUG, "system_output(): [%s]\n", shell_command); - if ( (fp=popen(shell_command, "r")) == NULL) - return NULL; /* caller can check errno */ - - do { - /* create/enlarge the buffer */ - void * new_buf; - if ((new_buf = realloc (buf, buf_max)) == NULL) { - if ( buf != NULL ) { /* previous realloc()s worked */ - free (buf); /* free partial buffer */ - buf = NULL; - } - break; - } - buf = new_buf; - logprintfl (EUCADEBUG2, "system_output: enlarged buf to %d\n", buf_max); - - do { /* read in output until EOF or buffer is full */ - last_read = fgets (buf+buf_current, buf_max-buf_current, fp); - if ( last_read != NULL ) - buf_current = strlen(buf); - logprintfl (EUCADEBUG2, "system_output: read %d characters so far (max=%d, last=%s)\n", buf_current, buf_max, last_read?"no":"yes"); - } while ( last_read && buf_max > buf_current+1 ); /* +1 is needed for fgets() to put \0 */ + int buf_max = INCREMENT; + int buf_current = 0; + char * last_read; + char * buf = NULL; + + if (fp==NULL) return NULL; + do { + // create/enlarge the buffer + void * new_buf; + if ((new_buf = realloc (buf, buf_max)) == NULL) { + if ( buf != NULL ) { // previous realloc()s worked + free (buf); // free partial buffer + } + return NULL; + } + buf = new_buf; + logprintfl (EUCADEBUG2, "fp2str: enlarged buf to %d\n", buf_max); + + do { // read in until EOF or buffer is full + last_read = fgets (buf+buf_current, buf_max-buf_current, fp); + if ( last_read != NULL ) + buf_current = strlen(buf); + logprintfl (EUCADEBUG2, "fp2str: read %d characters so far (max=%d, last=%s)\n", buf_current, buf_max, last_read?"no":"yes"); + } while ( last_read && buf_max > buf_current+1 ); /* +1 is needed for fgets() to put \0 */ - buf_max += INCREMENT; /* in case it is full */ - } while (last_read); + buf_max += INCREMENT; /* in case it is full */ + } while (last_read); - if ( buf_current < 1 ) { - free (buf); - buf = NULL; - } - pclose(fp); - return buf; + if ( buf_current < 1 ) { + free (buf); + buf = NULL; + } + + return buf; } +/* execute system(shell_command) and return stdout in new string + * pointed to by *stringp */ +char * system_output (char * shell_command ) +{ + char * buf = NULL; + FILE * fp; + + /* forks off command (this doesn't fail if command doesn't exist */ + logprintfl (EUCADEBUG, "system_output(): [%s]\n", shell_command); + if ( (fp=popen(shell_command, "r")) == NULL) + return NULL; /* caller can check errno */ + buf = fp2str (fp); + + pclose(fp); + return buf; +} char *getConfString(char *configFile, char *key) { int rc; diff --git a/util/misc.h b/util/misc.h index 3e34b191d64..950d223211f 100644 --- a/util/misc.h +++ b/util/misc.h @@ -60,17 +60,9 @@ permission notice: #ifndef INCLUDE_MISC_H #define INCLUDE_MISC_H +#include #include -#ifndef NO_AXIS /* for compiling on systems without Axis */ -#include -#include -#include -#include -#include -int InitWSSEC(axutil_env_t *env, axis2_stub_t *stub, char *policyFile); -#endif - #define TIMERSTART(a) double a; \ { \ struct timeval UBERSTART; \ @@ -91,6 +83,7 @@ enum {EUCADEBUG2, EUCADEBUG, EUCAINFO, EUCAWARN, EUCAERROR, EUCAFATAL}; char * replace_string (char ** stringp, char * source, char * destination ); int sscanf_lines (char * lines, char * format, void * varp); +char * fp2str (FILE * fp); char * system_output (char * shell_command ); char *getConfString(char *configFile, char *key);