From c597cc447311b572edf6f8979a336bf7be1839ab Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jan=20Posp=C3=AD=C5=A1il?= Date: Tue, 26 Oct 2004 17:19:30 +0000 Subject: [PATCH] First attempt to L&B Proxy logging library --- .../interface/producer.h.T | 58 ++++- org.glite.lb.client/src/producer.c | 281 ++++++++++++++++++++- org.glite.lb.client/src/uiwrap.c.T | 16 +- 3 files changed, 350 insertions(+), 5 deletions(-) diff --git a/org.glite.lb.client-interface/interface/producer.h.T b/org.glite.lb.client-interface/interface/producer.h.T index b00b7d9..a37ab3c 100644 --- a/org.glite.lb.client-interface/interface/producer.h.T +++ b/org.glite.lb.client-interface/interface/producer.h.T @@ -120,7 +120,8 @@ for my $t (sort { $event->{order}->{$a} <=> $event->{order}->{$b} } * \\brief simple wrapper around edg_wll_LogEvent for event $t} . $doc . qq{ * \\see edg_wll_LogEvent\(\) */ }; - gen "\nextern int edg_wll_Log$t$a;\n\n"; + gen "\nextern int edg_wll_Log${t}$a;\n"; + gen "\nextern int edg_wll_Log${t}Proxy$a;\n"; # gen qq{ #int edg_wll_Log$t$a #\{ @@ -156,7 +157,8 @@ for my $t (sort { $event->{order}->{$a} <=> $event->{order}->{$b} } * \\brief simple wrapper around edg_wll_LogEvent for event $t, $fn $code} . $e . qq{ * \\see edg_wll_LogEvent\(\) */ }; - gen "\nextern int edg_wll_Log$t$code$c;\n\n"; + gen "\nextern int edg_wll_Log$t${code}$c;\n"; + gen "\nextern int edg_wll_Log$t${code}Proxy$c;\n"; # gen qq{ #int edg_wll_Log$t$code$c #\{ @@ -190,7 +192,6 @@ extern int edg_wll_LogEvent( edg_wll_EventCode event, char *fmt, ...); - /** * Formats a logging message and sends it synchronously to local-logger * \brief generic synchronous logging function @@ -213,6 +214,25 @@ extern int edg_wll_LogEventSync( edg_wll_EventCode event, char *fmt, ...); +/** + * Formats a logging message and sends it synchronously to L&B Proxy + * \brief generic synchronous logging function + * \param context INOUT context to work with, + * \param event IN type of the event, + * \param fmt IN printf()-like format string, + * \param ... IN event specific values/data according to fmt, + * \retval 0 successful completition, + * \retval EINVAL bad jobId, unknown event code, or the format string together with the remaining arguments does not form a valid event, + * \retval ENOSPC L&B infrastructure failed to accept the event due to lack of disk space etc., + * \retval ENOMEM failed to allocate memory, + * \retval ECONNREFUSED cannot connect to the specified L&B Proxy + * \retval EAGAIN non blocking return from the call, the event may or may not get logged, + * \retval EDG_WLL_ERROR_NOJOBID logging call attempted without assigning jobId to the context. + */ +extern int edg_wll_LogEventProxy( + edg_wll_Context context, + edg_wll_EventCode event, + char *fmt, ...); /** * Instructs interlogger to to deliver all pending events related to current job @@ -255,6 +275,23 @@ extern int edg_wll_SetLoggingJob( int flags ); +/** + * Set a current job for given context. + * \note Should be called before any logging call. + * \param context INOUT context to work with + * \param job IN further logging calls are related to this job + * \param code IN sequence code as obtained from previous component + * \param user IN user ceredentials + * \param flags IN flags on code handling (\see API documentation) + */ +extern int edg_wll_SetLoggingJobProxy( + edg_wll_Context context, + const edg_wlc_JobId job, + const char * code, + const char * user, + int flags +); + /** * Register job with L&B service. @@ -304,6 +341,21 @@ extern int edg_wll_RegisterJobSync( ); /** + * Register alternative for L&B Proxy + */ +extern int edg_wll_RegisterJobProxy( + edg_wll_Context context, + const edg_wlc_JobId job, + enum edg_wll_RegJobJobtype type, + const char * jdl, + const char * ns, + int num_subjobs, + const char * seed, + edg_wlc_JobId ** subjobs +); + + +/** * Register subjobs in a batch. * Mainly used to provide JDL's of individual subjobs in a more efficient * way than logging them one by one. diff --git a/org.glite.lb.client/src/producer.c b/org.glite.lb.client/src/producer.c index 6d264ba..d00ccad 100644 --- a/org.glite.lb.client/src/producer.c +++ b/org.glite.lb.client/src/producer.c @@ -5,9 +5,13 @@ #ident "$Header$" +#include +#include +#include +#include +#include #include #include -#include #include #include "glite/wmsutils/jobid/strmd5.h" @@ -17,9 +21,12 @@ #include "glite/lb/ulm_parse.h" #include "glite/lb/trio.h" #include "glite/lb/lb_gss.h" +#include "glite/lb/escape.h" #include "prod_proto.h" +static const char* socket_path="/tmp/lbproxy.sock"; + /** *---------------------------------------------------------------------- * Connects to local-logger and sends already formatted ULM string @@ -143,6 +150,89 @@ edg_wll_DoLogEvent_end: return edg_wll_Error(context, NULL, NULL); } +/** + *---------------------------------------------------------------------- + * Connects to L&B Proxy and sends already formatted ULM string + * \brief helper logging function + * \param context INOUT context to work with, + * \param logline IN formated ULM string + *---------------------------------------------------------------------- + */ +static int edg_wll_DoLogEventProxy( + edg_wll_Context context, + edg_wll_LogLine logline) +{ + int answer; + struct sockaddr_un saddr; + int sock,flags; + + edg_wll_ResetError(context); + answer = 0; + + /* open a connection to the L&B Proxy: */ + +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"Logging to L&B Proxy at socket %s\n", socketpath); +#endif + sock = socket(PF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + edg_wll_SetError(context,answer = errno,"socket() error"); + goto edg_wll_DoLogEventProxy_end; + } + memset(&saddr, 0, sizeof(saddr)); + saddr.sun_family = AF_UNIX; + strcpy(saddr.sun_path, socket_path); + if ((flags = fcntl(sock, F_GETFL, 0)) < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) { + edg_wll_SetError(context,answer = errno,"fcntl()"); + close(sock); + goto edg_wll_DoLogEventProxy_end; + } + if (connect(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { + if(errno != EISCONN) { + edg_wll_SetError(context,answer = errno,"connect()"); + close(sock); + goto edg_wll_DoLogEventProxy_end; + } + } + + + /* and send the message to the L&B Proxy: */ + +// answer = edg_wll_log_proto_client_proxy(context,&sock,logline); + answer = EAGAIN; + + close(sock); + +edg_wll_DoLogEventProxy_end: + + switch(answer) { + case 0: + case EINVAL: + case ENOSPC: + case ENOMEM: + case EDG_WLL_ERROR_GSS: + case EDG_WLL_ERROR_DNS: + case ENOTCONN: + case ECONNREFUSED: + case ETIMEDOUT: + case EAGAIN: + break; + case EDG_WLL_ERROR_PARSE_EVENT_UNDEF: + case EDG_WLL_ERROR_PARSE_MSG_INCOMPLETE: + case EDG_WLL_ERROR_PARSE_KEY_DUPLICITY: + case EDG_WLL_ERROR_PARSE_KEY_MISUSE: +// case EDG_WLL_ERROR_PARSE_OK_WITH_EXTRA_FIELDS: + edg_wll_UpdateError(context,EINVAL,"edg_wll_DoLogEventProxy(): Error code mapped to EINVAL"); + break; + + default: + edg_wll_UpdateError(context,EAGAIN,"edg_wll_DoLogEventProxy(): Error code mapped to EAGAIN"); + break; + } + + return edg_wll_Error(context, NULL, NULL); +} + /** *---------------------------------------------------------------------- @@ -259,6 +349,128 @@ edg_wll_logeventmaster_end: /** *---------------------------------------------------------------------- + * Formats a logging message and sends it to L&B Proxy + * \brief master proxy logging event function + * \param context INOUT context to work with, + * \param priority IN priority flag (0 for async, 1 for sync) + * \param event IN type of the event, + * \param fmt IN printf()-like format string, + * \param ... IN event specific values/data according to fmt. + *---------------------------------------------------------------------- + */ +static int edg_wll_LogEventMasterProxy( + edg_wll_Context context, + int priority, + edg_wll_EventCode event, + char *fmt, ...) +{ + va_list fmt_args; + int ret,answer; + char *fix,*var,*dguser; + char *source,*eventName,*lvl, *fullid,*seq,*name_esc; + struct timeval start_time; + char date[ULM_DATE_STRING_LENGTH+1]; + edg_wll_LogLine out; + size_t size; + int i; + + i = errno = size = 0; + seq = fix = var = dguser = out = source = eventName = lvl = fullid = NULL; + + edg_wll_ResetError(context); + + /* default return value is "Try Again" */ + answer = ret = EAGAIN; + + /* format the message: */ + va_start(fmt_args,fmt); + + gettimeofday(&start_time,0); + if (edg_wll_ULMTimevalToDate(start_time.tv_sec,start_time.tv_usec,date) != 0) { + edg_wll_SetError(context,ret = EINVAL,"edg_wll_LogEventMasterProxy(): edg_wll_ULMTimevalToDate() error"); + goto edg_wll_logeventmaster_end; + } + source = edg_wll_SourceToString(context->p_source); + lvl = edg_wll_LevelToString(context->p_level); + eventName = edg_wll_EventToString(event); + if (!eventName) { + edg_wll_SetError(context,ret = EINVAL,"edg_wll_LogEventMasterProxy(): event name not specified"); + goto edg_wll_logeventmaster_end; + } + if (!(fullid = edg_wlc_JobIdUnparse(context->p_jobid))) { + edg_wll_SetError(context,ret = EINVAL,"edg_wll_LogEventMasterProxy(): edg_wlc_JobIdUnparse() error"); + goto edg_wll_logeventmaster_end; + } + seq = edg_wll_GetSequenceCode(context); + if (edg_wll_IncSequenceCode(context)) { + ret = EINVAL; + goto edg_wll_logeventmaster_end; + } + if (trio_asprintf(&fix,EDG_WLL_FORMAT_COMMON, + date,context->p_host,lvl,priority, + source,context->p_instance ? context->p_instance : "", + eventName,fullid,seq) == -1) { + edg_wll_SetError(context,ret = ENOMEM,"edg_wll_LogEventMasterProxy(): trio_asprintf() error"); + goto edg_wll_logeventmaster_end; + } + if (trio_vasprintf(&var,fmt,fmt_args) == -1) { + edg_wll_SetError(context,ret = ENOMEM,"edg_wll_LogEventMasterProxy(): trio_vasprintf() error"); + goto edg_wll_logeventmaster_end; + } + /* format the DG.USER string */ +/* XXX: put user credentials here probably from context */ + name_esc = edg_wll_LogEscape("User credentials should go here"); + if (asprintf(&dguser,"DG.USER=\"%s\" ",name_esc) == -1) { + edg_wll_SetError(context,ret = ENOMEM,"edg_wll_LogEventMasterProxy(): asprintf() error"); + goto edg_wll_logeventmaster_end; + } + if (asprintf(&out,"%s%s%s\n",dguser,fix,var) == -1) { + edg_wll_SetError(context,ret = ENOMEM,"edg_wll_LogEventMasterProxy(): asprintf() error"); + goto edg_wll_logeventmaster_end; + } + size = strlen(out); + + if (priority && (size > EDG_WLL_LOG_SYNC_MAXMSGSIZE)) { + edg_wll_SetError(context,ret = ENOSPC,"edg_wll_LogEventMasterProxy(): Message size too large for synchronous transfer"); + goto edg_wll_logeventmaster_end; + } + +#ifdef EDG_WLL_LOG_STUB +// fprintf(stderr,"edg_wll_LogEvent (%d chars): %s",size,out); +#endif + + context->p_tmp_timeout.tv_sec = 0; + context->p_tmp_timeout.tv_usec = 0; + if (priority) { + context->p_tmp_timeout = context->p_sync_timeout; + } + else { + context->p_tmp_timeout = context->p_log_timeout; + } + + /* and send the message to the L&B Proxy: */ + ret = edg_wll_DoLogEventProxy(context, /* priority,*/ out); + +edg_wll_logeventmaster_end: + va_end(fmt_args); + if (seq) free(seq); + if (fix) free(fix); + if (var) free(var); + if (dguser) free(dguser); + if (out) free(out); + if (source) free(source); + if (lvl) free(lvl); + if (eventName) free(eventName); + if (fullid) free(fullid); + if (name_esc) free(name_esc); + + if (ret) edg_wll_UpdateError(context,0,"Logging library ERROR: "); + + return edg_wll_Error(context,NULL,NULL); +} + +/** + *---------------------------------------------------------------------- * Formats a logging message and sends it asynchronously to local-logger * \brief generic asynchronous logging function *---------------------------------------------------------------------- @@ -326,6 +538,40 @@ edg_wll_logeventsync_end: return edg_wll_Error(context,NULL,NULL); } +/** + *---------------------------------------------------------------------- + * Formats a logging message and sends it synchronously to L&B Proxy + * \brief generic synchronous logging function + *---------------------------------------------------------------------- + */ +int edg_wll_LogEventProxy( + edg_wll_Context context, + edg_wll_EventCode event, + char *fmt, ...) +{ + int ret=0; + char *list=NULL; + va_list fmt_args; + + edg_wll_ResetError(context); + + va_start(fmt_args,fmt); + if (trio_vasprintf(&list,fmt,fmt_args) == -1) { + edg_wll_SetError(context,ret = ENOMEM,"edg_wll_LogEventProxy(): trio_vasprintf() error"); + goto edg_wll_logevent_end; + } + + ret=edg_wll_LogEventMasterProxy(context,0,event,"%s",list); + +edg_wll_logevent_end: + va_end(fmt_args); + if (list) free(list); + + if (ret) edg_wll_UpdateError(context,0,"edg_wll_LogEventProxy(): "); + + return edg_wll_Error(context,NULL,NULL); +} + /** *----------------------------------------------------------------------- @@ -456,6 +702,39 @@ int edg_wll_SetLoggingJob( /** *----------------------------------------------------------------------- + * Set a current job for given context. + * \note Should be called before any logging call. + *----------------------------------------------------------------------- + */ +int edg_wll_SetLoggingJobProxy( + edg_wll_Context context, + const edg_wlc_JobId job, + const char *code, + const char *user, + int flags) +{ + int err; + + edg_wll_ResetError(context); + + if (!job) return edg_wll_SetError(context,EINVAL,"jobid is null"); + + edg_wlc_JobIdFree(context->p_jobid); + if ((err = edg_wlc_JobIdDup(job,&context->p_jobid))) + edg_wll_SetError(context,err,"edg_wll_SetLoggingJob(): edg_wlc_JobIdDup() error"); + + else { + if (!edg_wll_SetSequenceCode(context,code,flags)) +/* XXX: ask proxy for last known sequence code */ + edg_wll_IncSequenceCode(context); + } +/* XXX: add user credentials somewhere - to context? */ + + return edg_wll_Error(context,NULL,NULL); +} + +/** + *----------------------------------------------------------------------- * Register job with L&B service. *----------------------------------------------------------------------- */ diff --git a/org.glite.lb.client/src/uiwrap.c.T b/org.glite.lb.client/src/uiwrap.c.T index 983a43b..8ad66e0 100644 --- a/org.glite.lb.client/src/uiwrap.c.T +++ b/org.glite.lb.client/src/uiwrap.c.T @@ -58,6 +58,13 @@ $decl return edg_wll_LogEvent$b; \}\n }; + gen qq{ +int edg_wll_Log${t}Proxy$a +\{ +$decl + return edg_wll_LogEventProxy$b; +\}\n +}; for ($event->getFieldsOrdered) { my $f = selectField $event $_; my $fn = $f->getName; @@ -91,10 +98,17 @@ $decl gen qq{ int edg_wll_Log$t$code$c \{ -$decl; +$decl return edg_wll_LogEvent$d; \}\n }; + gen qq{ +int edg_wll_Log$t${code}Proxy$c +\{ +$decl + return edg_wll_LogEventProxy$d; +\}\n +}; } } } -- 1.8.2.3