From c5708240680c1a7539d23063955ccde7be069470 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jan=20Posp=C3=AD=C5=A1il?= Date: Wed, 4 Oct 2006 06:11:21 +0000 Subject: [PATCH] work in progress - symbol names cleanup - separate functions connect/write/read for all edg_wll_log{_proxy,_direct} versions - some memleaks corrections --- org.glite.lb.client/src/prod_proto.c | 621 ++++++++++++++++++++++++----------- org.glite.lb.client/src/prod_proto.h | 129 +++++++- org.glite.lb.client/src/producer.c | 317 ++++-------------- 3 files changed, 601 insertions(+), 466 deletions(-) diff --git a/org.glite.lb.client/src/prod_proto.c b/org.glite.lb.client/src/prod_proto.c index 478cd35..39655b4 100644 --- a/org.glite.lb.client/src/prod_proto.c +++ b/org.glite.lb.client/src/prod_proto.c @@ -1,34 +1,34 @@ #ident "$Header$" -/** - * \file prod_proto.c - */ -#include "prod_proto.h" +#include +#include +#include +#include +#include +#include +#include #include "glite/lb/producer.h" -#include "glite/lb/escape.h" #include "glite/lb/lb_plain_io.h" #include "glite/lb/il_msg.h" #include "glite/lb/il_string.h" -#include -#include -#include -#include +#include "prod_proto.h" +#include "connection.h" -/* +static const char* socket_path="/tmp/lb_proxy_store.sock"; + +/** *---------------------------------------------------------------------- - * edg_wll_log_proto_handle_gss_failures - handle GSS failures on the client side - * - * Returns: errno + * Handle GSS failures on the client side *---------------------------------------------------------------------- */ -int edg_wll_log_proto_handle_gss_failures(edg_wll_Context context, int code, edg_wll_GssStatus *gss_code, const char *text) +int edg_wll_log_proto_handle_gss_failures(edg_wll_Context ctx, int code, edg_wll_GssStatus *gss_code, const char *text) { static char err[256]; int ret = 0; - edg_wll_ResetError(context); + edg_wll_ResetError(ctx); if(code>0) return(0); @@ -36,19 +36,19 @@ int edg_wll_log_proto_handle_gss_failures(edg_wll_Context context, int code, edg switch(code) { case EDG_WLL_GSS_ERROR_EOF: snprintf(err, sizeof(err), "%s;; GSS Error: EOF occured;", text); - ret = edg_wll_SetError(context,ENOTCONN,err); + ret = edg_wll_SetError(ctx,ENOTCONN,err); break; case EDG_WLL_GSS_ERROR_TIMEOUT: snprintf(err, sizeof(err), "%s;; GSS Error: timeout expired;", text); - ret = edg_wll_SetError(context,ENOTCONN,err); + ret = edg_wll_SetError(ctx,ENOTCONN,err); break; case EDG_WLL_GSS_ERROR_ERRNO: snprintf(err, sizeof(err), "%s;; GSS Error: system error occured;", text); - ret = edg_wll_SetError(context,ENOTCONN,err); + ret = edg_wll_SetError(ctx,ENOTCONN,err); break; case EDG_WLL_GSS_ERROR_GSS: snprintf(err, sizeof(err), "%s;; GSS Error: GSS failure occured", text); - ret = edg_wll_SetErrorGss(context,err,gss_code); + ret = edg_wll_SetErrorGss(ctx,err,gss_code); break; case EDG_WLL_GSS_ERROR_HERRNO: { @@ -56,20 +56,36 @@ int edg_wll_log_proto_handle_gss_failures(edg_wll_Context context, int code, edg char *msg2; msg1 = hstrerror(errno); asprintf(&msg2, "%s;; GSS Error: %s", text, msg1); - ret = edg_wll_SetError(context,EDG_WLL_ERROR_DNS, msg2); + ret = edg_wll_SetError(ctx,EDG_WLL_ERROR_DNS, msg2); free(msg2); } break; default: snprintf(err, sizeof(err), "%s;; GSS Error: unknown failure", text); - ret = edg_wll_SetError(context,ECONNREFUSED,err); + ret = edg_wll_SetError(ctx,ECONNREFUSED,err); break; } return ret; } +/** + *---------------------------------------------------------------------- + * Handle UNIX socket failures on the client side + *---------------------------------------------------------------------- + */ +int edg_wll_log_proto_handle_plain_failures(edg_wll_Context ctx, int code, const char *text) +{ + return 0; +} - +/* + *---------------------------------------------------------------------- + * get_reply_plain, get_reply_gss - read reply from server + * + * Returns: -1 - error reading message, + * code > 0 - error code from server + *---------------------------------------------------------------------- + */ struct reader_data { edg_wll_Context ctx; void *conn; @@ -89,38 +105,30 @@ plain_reader(void *user_data, char *buffer, int max_len) return(len); } -/* - *---------------------------------------------------------------------- - * get_reply_plain, get_reply_gss - read reply from server - * - * Returns: -1 - error reading message, - * code > 0 - error code from server - *---------------------------------------------------------------------- - */ static int -get_reply_plain(edg_wll_Context context, edg_wll_PlainConnection *conn, char **buf, int *code_maj, int *code_min) +get_reply_plain(edg_wll_Context ctx, edg_wll_PlainConnection *conn, char **buf, int *code_maj, int *code_min) { char *msg=NULL; int len; struct reader_data data; - data.ctx = context; + data.ctx = ctx; data.conn = conn; len = read_il_data(&data, &msg, plain_reader); if(len < 0) { - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "get_reply_plain(): error reading message"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "get_reply_plain(): error reading message"); goto get_reply_plain_end; } if(decode_il_reply(code_maj, code_min, buf, msg) < 0) { - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "get_reply_plain(): error decoding message"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "get_reply_plain(): error decoding message"); goto get_reply_plain_end; } get_reply_plain_end: if(msg) free(msg); - return edg_wll_Error(context,NULL,NULL); + return edg_wll_Error(ctx,NULL,NULL); } @@ -145,149 +153,333 @@ gss_reader(void *user_data, char *buffer, int max_len) static int -get_reply_gss(edg_wll_Context context, edg_wll_GssConnection *conn, char **buf, int *code_maj, int *code_min) +get_reply_gss(edg_wll_Context ctx, edg_wll_GssConnection *conn, char **buf, int *code_maj, int *code_min) { char *msg = NULL; int code; struct reader_data data; - data.ctx = context; + data.ctx = ctx; data.conn = conn; code = read_il_data(&data, &msg, gss_reader); if(code < 0) { - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "get_reply_gss(): error reading reply"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "get_reply_gss(): error reading reply"); goto get_reply_gss_end; } if(decode_il_reply(code_maj, code_min, buf, msg) < 0) { - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "get_reply_gss(): error decoding reply"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "get_reply_gss(): error decoding reply"); goto get_reply_gss_end; } get_reply_gss_end: if(msg) free(msg); - return edg_wll_Error(context,NULL,NULL); + return edg_wll_Error(ctx,NULL,NULL); } -/* +/** *---------------------------------------------------------------------- - * - * edg_wll_log_proto_client - client part of the logging protocol - * used when sending messages to local logger - * - * Returns: 0 if done properly or errno - * - * Calls: - * - * Algorithm: - * + * client part of the logging protocol, used when sending messages to local logger + *---------------------------------------------------------------------- + */ +int edg_wll_log_proto_client(edg_wll_Context ctx, edg_wll_GssConnection *conn, edg_wll_LogLine logline) +{ + int err = 0; + + edg_wll_ResetError(ctx); + + /* send message */ + if ((err = edg_wll_log_write(ctx, conn, logline)) == -1) { + edg_wll_UpdateError(ctx,EDG_WLL_IL_PROTO,"edg_wll_log_proto_client: edg_wll_log_write error"); + goto edg_wll_log_proto_client_end; + } + + /* get answer */ + if ((err = edg_wll_log_read(ctx, conn)) != 0) { + edg_wll_UpdateError(ctx,err,"edg_wll_log_proto_client: edg_wll_log_read error"); + } + +edg_wll_log_proto_client_end: + + return edg_wll_Error(ctx,NULL,NULL); +} + +/** + *---------------------------------------------------------------------- + * connect to locallogger *---------------------------------------------------------------------- */ -int edg_wll_log_proto_client(edg_wll_Context context, edg_wll_GssConnection *con, edg_wll_LogLine logline) +int edg_wll_log_connect(edg_wll_Context ctx, edg_wll_GssConnection *conn) +{ + int ret,answer; + char *my_subject_name = NULL; + edg_wll_GssStatus gss_stat; + gss_cred_id_t cred = GSS_C_NO_CREDENTIAL; + OM_uint32 min_stat; + + edg_wll_ResetError(ctx); + +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_connect: setting up connection to local-logger\n"); +#endif + /* acquire gss credentials */ + ret = edg_wll_gss_acquire_cred_gsi( + ctx->p_proxy_filename ? ctx->p_proxy_filename : ctx->p_cert_filename, + ctx->p_proxy_filename ? ctx->p_proxy_filename : ctx->p_key_filename, + &cred, &my_subject_name, &gss_stat); + /* give up if unable to acquire prescribed credentials, otherwise go on anonymously */ + if (ret && ctx->p_proxy_filename) { + edg_wll_SetErrorGss(ctx, "edg_wll_gss_acquire_cred_gsi(): failed to load GSI credentials", &gss_stat); + goto edg_wll_log_connect_end; + } +#ifdef EDG_WLL_LOG_STUB + if (my_subject_name != NULL) { + fprintf(stderr,"edg_wll_log_connect: using certificate: %s\n",my_subject_name); + } else { + fprintf(stderr,"edg_wll_log_connect: going on anonymously!\n"); + } +#endif +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_connect: opening connection to local-logger host '%s', port '%d'\n", + ctx->p_destination, ctx->p_dest_port); +#endif + if ((answer = edg_wll_gss_connect(cred, + ctx->p_destination, ctx->p_dest_port, + &ctx->p_tmp_timeout, conn, &gss_stat)) < 0) { + answer = edg_wll_log_proto_handle_gss_failures(ctx,answer,&gss_stat,"edg_wll_gss_connect()"); + goto edg_wll_log_connect_end; + } + +edg_wll_log_connect_end: +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_connect: done\n"); +#endif + if (conn->context != GSS_C_NO_CONTEXT) + edg_wll_gss_close(conn,&ctx->p_tmp_timeout); + if (cred != GSS_C_NO_CREDENTIAL) + gss_release_cred(&min_stat, &cred); + if (my_subject_name) free(my_subject_name); + + return answer; +} + +/** + *---------------------------------------------------------------------- + * write/send to locallogger + *---------------------------------------------------------------------- + */ +int edg_wll_log_write(edg_wll_Context ctx, edg_wll_GssConnection *conn, edg_wll_LogLine logline) { char header[EDG_WLL_LOG_SOCKET_HEADER_LENGTH+1]; int err; int answer; - u_int8_t answer_end[4]; - size_t count; + size_t count,sent; int size; u_int8_t size_end[4]; edg_wll_GssStatus gss_code; - errno = err = answer = count = 0; + errno = err = answer = count = sent = 0; size = strlen(logline)+1; size_end[0] = size & 0xff; size >>= 8; size_end[1] = size & 0xff; size >>= 8; size_end[2] = size & 0xff; size >>= 8; size_end[3] = size; size = strlen(logline)+1; - edg_wll_ResetError(context); - /* send header */ + edg_wll_ResetError(ctx); + #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"log_proto_client: sending header...\n"); + fprintf(stderr,"edg_wll_log_write: sending header\n"); #endif sprintf(header,"%s",EDG_WLL_LOG_SOCKET_HEADER); header[EDG_WLL_LOG_SOCKET_HEADER_LENGTH]='\0'; - if ((err = edg_wll_gss_write_full(con, header, EDG_WLL_LOG_SOCKET_HEADER_LENGTH, &context->p_tmp_timeout, &count, &gss_code)) < 0) { - answer = edg_wll_log_proto_handle_gss_failures(context,err,&gss_code,"edg_wll_gss_write_full(}"); - edg_wll_UpdateError(context,answer,"edg_wll_log_proto_client(): error sending header"); - goto edg_wll_log_proto_client_end; + if ((err = edg_wll_gss_write_full(conn, header, EDG_WLL_LOG_SOCKET_HEADER_LENGTH, &ctx->p_tmp_timeout, &count, &gss_code)) < 0) { + answer = edg_wll_log_proto_handle_gss_failures(ctx,err,&gss_code,"edg_wll_gss_write_full(}"); + edg_wll_UpdateError(ctx,answer,"edg_wll_log_write: error sending header"); + return -1; } + sent += count; #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"log_proto_client: sending message size...\n"); + fprintf(stderr,"edg_wll_log_write: sending message size\n"); #endif count = 0; - if ((err = edg_wll_gss_write_full(con, size_end, 4, &context->p_tmp_timeout, &count, &gss_code)) < 0) { - answer = edg_wll_log_proto_handle_gss_failures(context,err,&gss_code,"edg_wll_gss_write_full()"); - edg_wll_UpdateError(context,answer,"edg_wll_log_proto_client(): error sending message size"); - goto edg_wll_log_proto_client_end; + if ((err = edg_wll_gss_write_full(conn, size_end, 4, &ctx->p_tmp_timeout, &count, &gss_code)) < 0) { + answer = edg_wll_log_proto_handle_gss_failures(ctx,err,&gss_code,"edg_wll_gss_write_full()"); + edg_wll_UpdateError(ctx,answer,"edg_wll_log_write: error sending message size"); + return -1; } + sent += count; - /* send message */ #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"log_proto_client: sending message...\n"); + fprintf(stderr,"edg_wll_log_write: sending message...\n"); #endif count = 0; - if (( err = edg_wll_gss_write_full(con, logline, size, &context->p_tmp_timeout, &count, &gss_code)) < 0) { - answer = edg_wll_log_proto_handle_gss_failures(context,err,&gss_code,"edg_wll_gss_write_full()"); - edg_wll_UpdateError(context,answer,"edg_wll_log_proto_client(): error sending message"); - goto edg_wll_log_proto_client_end; + if (( err = edg_wll_gss_write_full(conn, logline, size, &ctx->p_tmp_timeout, &count, &gss_code)) < 0) { + answer = edg_wll_log_proto_handle_gss_failures(ctx,err,&gss_code,"edg_wll_gss_write_full()"); + edg_wll_UpdateError(ctx,answer,"edg_wll_log_write: error sending message"); + return -1; } + sent += count; + +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_write: done\n"); +#endif + return sent; +} + +/** + *---------------------------------------------------------------------- + * read/receive from locallogger + *---------------------------------------------------------------------- + */ +int edg_wll_log_read(edg_wll_Context ctx, edg_wll_GssConnection *conn) +{ + int err; + int answer; + u_int8_t answer_end[4]; + size_t count; + edg_wll_GssStatus gss_code; + + errno = err = answer = count = 0; + + edg_wll_ResetError(ctx); - /* get answer */ #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"log_proto_client: reading answer from server...\n"); + fprintf(stderr,"edg_wll_log_read: reading answer from local-logger\n"); #endif count = 0; - if ((err = edg_wll_gss_read_full(con, answer_end, 4, &context->p_tmp_timeout, &count, &gss_code)) < 0 ) { - answer = edg_wll_log_proto_handle_gss_failures(context,err,&gss_code,"edg_wll_gss_read_full()"); -/* FIXME: update the answer (in context?) to EAGAIN or not? - answer = EAGAIN; -*/ - edg_wll_UpdateError(context,answer,"edg_wll_log_proto_client(): error getting answer"); + if ((err = edg_wll_gss_read_full(conn, answer_end, 4, &ctx->p_tmp_timeout, &count, &gss_code)) < 0 ) { + answer = edg_wll_log_proto_handle_gss_failures(ctx,err,&gss_code,"edg_wll_gss_read_full()"); + edg_wll_SetError(ctx,answer,"edg_wll_log_read: error getting answer"); } else { answer = answer_end[3]; answer <<=8; answer |= answer_end[2]; answer <<=8; answer |= answer_end[1]; answer <<=8; answer |= answer_end[0]; #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"log_proto_client: read answer \"%d\"\n",answer); + fprintf(stderr,"edg_wll_log_read: read answer \"%d\"\n",answer); #endif - edg_wll_SetError(context,answer,"answer read from locallogger"); + edg_wll_SetError(ctx,answer,"answer read from locallogger"); } -edg_wll_log_proto_client_end: +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_read: done\n"); +#endif + return edg_wll_Error(ctx,NULL,NULL); +} - return edg_wll_Error(context,NULL,NULL); +/** + *---------------------------------------------------------------------- + * client part of the logging protocol, used when sending messages to lbproxy + *---------------------------------------------------------------------- + */ +int edg_wll_log_proxy_proto_client(edg_wll_Context ctx, edg_wll_PlainConnection *conn, edg_wll_LogLine logline) +{ + int err = 0; + + edg_wll_ResetError(ctx); + + /* send message */ + if ((err = edg_wll_log_proxy_write(ctx, conn, logline)) == -1) { + edg_wll_UpdateError(ctx,EDG_WLL_IL_PROTO,"edg_wll_log_proxy_proto_client: edg_wll_log_proxy_write error"); + goto edg_wll_log_proxy_proto_client_end; + } + + /* get answer */ + if ((err = edg_wll_log_proxy_read(ctx, conn)) != 0) { + edg_wll_UpdateError(ctx,err,"edg_wll_log_proxy_proto_client: edg_wll_log_proxy_read error"); + } + +edg_wll_log_proxy_proto_client_end: + + return edg_wll_Error(ctx,NULL,NULL); } -/* +/** *---------------------------------------------------------------------- - * - * edg_wll_log_proto_client_proxy - client part of the logging protocol - * used when sending messages to L&B Proxy - * - * Returns: 0 if done properly or errno - * - * Calls: - * - * Algorithm: - * + * connect to lbproxy *---------------------------------------------------------------------- */ +int edg_wll_log_proxy_connect(edg_wll_Context ctx, edg_wll_PlainConnection *conn) +{ + int answer = 0, retries; + int flags; + struct sockaddr_un saddr; -int edg_wll_log_write_proxy(edg_wll_Context context, edg_wll_PlainConnection *conn, edg_wll_LogLine logline) + edg_wll_ResetError(ctx); + +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_proxy_connect: setting up connection to L&B Proxy\n"); +#endif + conn->sock = socket(PF_UNIX, SOCK_STREAM, 0); + if (conn->sock < 0) { + edg_wll_SetError(ctx,answer = errno,"socket() error"); + goto edg_wll_log_proxy_connect_end; + } + memset(&saddr, 0, sizeof(saddr)); + saddr.sun_family = AF_UNIX; + strcpy(saddr.sun_path, ctx->p_lbproxy_store_sock? + ctx->p_lbproxy_store_sock: socket_path); + if ((flags = fcntl(conn->sock, F_GETFL, 0)) < 0 || fcntl(conn->sock, F_SETFL, flags | O_NONBLOCK) < 0) { + edg_wll_SetError(ctx,answer = errno,"fcntl()"); + close(conn->sock); + goto edg_wll_log_proxy_connect_end; + } +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_proxy_connect: openning connection to L&B Proxy at socket %s\n", + ctx->p_lbproxy_store_sock? ctx->p_lbproxy_store_sock: socket_path); +#endif + retries = 0; + while ((answer = connect(conn->sock, (struct sockaddr *)&saddr, sizeof(saddr))) < 0 && + errno == EAGAIN && + ctx->p_tmp_timeout.tv_sec >= 0 && ctx->p_tmp_timeout.tv_usec >= 0 && + !(ctx->p_tmp_timeout.tv_sec == 0 && ctx->p_tmp_timeout.tv_usec == 0) + ) + { + struct timespec ns = { 0, PROXY_CONNECT_RETRY * 1000000 /* 10 ms */ },rem; + + nanosleep(&ns,&rem); + + ctx->p_tmp_timeout.tv_usec -= ns.tv_nsec/1000; + ctx->p_tmp_timeout.tv_usec += rem.tv_nsec/1000; + + ctx->p_tmp_timeout.tv_sec -= ns.tv_sec; + ctx->p_tmp_timeout.tv_sec += rem.tv_sec; + + if (ctx->p_tmp_timeout.tv_usec < 0) { + ctx->p_tmp_timeout.tv_usec += 1000000; + ctx->p_tmp_timeout.tv_sec--; + } + retries++; + } +#ifdef EDG_WLL_LOG_STUB + if (retries) fprintf(stderr,"edg_wll_log_proxy_connect: %d connect retries\n",retries); +#endif + +edg_wll_log_proxy_connect_end: +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_proxy_connect: done\n"); +#endif + if (conn) edg_wll_plain_close(conn); + + return answer; +} + +/** + *---------------------------------------------------------------------- + * write/send to lbproxy + *---------------------------------------------------------------------- + */ +int edg_wll_log_proxy_write(edg_wll_Context ctx, edg_wll_PlainConnection *conn, edg_wll_LogLine logline) { int len,count = 0; char *buffer; - edg_wll_ResetError(context); + edg_wll_ResetError(ctx); - /* encode message */ #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_write_proxy: encoding message\n"); + fprintf(stderr,"edg_wll_log_proxy_write: encoding message\n"); #endif { il_octet_string_t ll; @@ -297,28 +489,32 @@ int edg_wll_log_write_proxy(edg_wll_Context context, edg_wll_PlainConnection *co len = encode_il_msg(&buffer, &ll); } if(len < 0) { - edg_wll_SetError(context,ENOMEM,"edg_wll_log_write_proxy(): error encoding message"); + edg_wll_SetError(ctx,ENOMEM,"edg_wll_log_proxy_write: error encoding message"); return -1; } - /* send message */ #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_write_proxy: sending message\n"); + fprintf(stderr,"edg_wll_log_proxy_write: sending message\n"); #endif - if ((count = edg_wll_plain_write_full(conn, buffer, len, &context->p_tmp_timeout)) < 0) { - edg_wll_SetError(context, EDG_WLL_IL_PROTO,"edg_wll_log_write_proxy: error sending message to socket"); + if ((count = edg_wll_plain_write_full(conn, buffer, len, &ctx->p_tmp_timeout)) < 0) { + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO,"edg_wll_log_proxy_write: error sending message to socket"); return -1; } if (buffer) free(buffer); #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_write_proxy: done\n"); + fprintf(stderr,"edg_wll_log_proxy_write: done\n"); #endif return count; } -int edg_wll_log_read_proxy(edg_wll_Context context, edg_wll_PlainConnection *conn) +/** + *---------------------------------------------------------------------- + * read/receive from lbproxy + *---------------------------------------------------------------------- + */ +int edg_wll_log_proxy_read(edg_wll_Context ctx, edg_wll_PlainConnection *conn) { char *answer = NULL; static char et[256]; @@ -330,87 +526,148 @@ int edg_wll_log_read_proxy(edg_wll_Context context, edg_wll_PlainConnection *con errno = err = code = count = 0; lbproto_code = 0; - edg_wll_ResetError(context); + edg_wll_ResetError(ctx); - /* get answer */ #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_read_proxy: reading answer from server\n"); + fprintf(stderr,"edg_wll_log_proxy_read: reading answer from server\n"); #endif - if ((err = get_reply_plain(context, conn, &answer, &lbproto_code, &code)) != 0 ) { - edg_wll_SetError(context, EDG_WLL_IL_PROTO,"edg_wll_log_read_proxy: error reading answer from L&B Proxy server"); + if ((err = get_reply_plain(ctx, conn, &answer, &lbproto_code, &code)) != 0 ) { + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO,"edg_wll_log_proxy_read: error reading answer from L&B Proxy server"); } else { #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_read_proxy: read answer \"%d:%d: %s\"\n",lbproto_code,code,answer); + fprintf(stderr,"edg_wll_log_proxy_read: read answer \"%d:%d: %s\"\n",lbproto_code,code,answer); #endif switch (lbproto_code) { case LB_OK: break; case LB_NOMEM: - edg_wll_SetError(context, ENOMEM, "edg_wll_log_read_proxy: proxy out of memory"); + edg_wll_SetError(ctx, ENOMEM, "edg_wll_log_proxy_read: proxy out of memory"); break; case LB_PROTO: - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "edg_wll_log_read_proxy: received protocol error response"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "edg_wll_log_proxy_read: received protocol error response"); break; case LB_DBERR: - snprintf(et, sizeof(et), "edg_wll_log_read_proxy: error details from L&B Proxy server: %s", answer); - edg_wll_SetError(context, code, et); + snprintf(et, sizeof(et), "edg_wll_log_proxy_read: error details from L&B Proxy server: %s", answer); + edg_wll_SetError(ctx, code, et); break; default: - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "edg_wll_log_read_proxy: received unknown protocol response"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "edg_wll_log_proxy_read: received unknown protocol response"); break; } } #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_read_proxy: done\n"); + fprintf(stderr,"edg_wll_log_proxy_read: done\n"); #endif - return edg_wll_Error(context,NULL,NULL); + return edg_wll_Error(ctx,NULL,NULL); } -int edg_wll_log_proto_client_proxy(edg_wll_Context context, edg_wll_PlainConnection *conn, edg_wll_LogLine logline) -{ +/** + *---------------------------------------------------------------------- + * client part of the logging protocol, used when sending messages directly to bkserver + *---------------------------------------------------------------------- + */ +int edg_wll_log_direct_proto_client(edg_wll_Context ctx, edg_wll_GssConnection *conn, edg_wll_LogLine logline) +{ int err = 0; - edg_wll_ResetError(context); + edg_wll_ResetError(ctx); /* send message */ - if ((err = edg_wll_log_write_proxy(context, conn, logline)) == -1) { - edg_wll_UpdateError(context,EDG_WLL_IL_PROTO,"edg_wll_log_proto_client_proxy(): edg_wll_log_write_proxy error"); - goto edg_wll_log_proto_client_proxy_end; - } + if ((err = edg_wll_log_direct_write(ctx, conn, logline)) == -1) { + edg_wll_UpdateError(ctx,EDG_WLL_IL_PROTO,"edg_wll_log_direct_proto_client: edg_wll_log_direct_write error"); + goto edg_wll_log_direct_proto_client_end; + } /* get answer */ - if ((err = edg_wll_log_read_proxy(context, conn)) != 0) { - edg_wll_UpdateError(context,err,"edg_wll_log_proto_client_proxy(): edg_wll_log_read_proxy error"); + if ((err = edg_wll_log_direct_read(ctx, conn)) != 0) { + edg_wll_UpdateError(ctx,err,"edg_wll_log_direct_proto_client: edg_wll_log_direct_read error"); } -edg_wll_log_proto_client_proxy_end: +edg_wll_log_direct_proto_client_end: - return edg_wll_Error(context,NULL,NULL); + return edg_wll_Error(ctx,NULL,NULL); } -/* + +/** *---------------------------------------------------------------------- - * - * edg_wll_log_proto_client_direct - client part of the logging protocol - * used when sending messages directly to bkserver - * - * Returns: 0 if done properly or errno * - * Calls: - * - * Algorithm: - * + * connect to bkserver *---------------------------------------------------------------------- */ -int edg_wll_log_write_direct(edg_wll_Context context, edg_wll_GssConnection *con, edg_wll_LogLine logline) +int edg_wll_log_direct_connect(edg_wll_Context ctx, edg_wll_GssConnection *conn) +{ + int ret,answer; + char *my_subject_name = NULL; + edg_wll_GssStatus gss_stat; + gss_cred_id_t cred = GSS_C_NO_CREDENTIAL; + OM_uint32 min_stat; + char *host; + int port; + + ret = answer = 0; + + edg_wll_ResetError(ctx); + +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_direct_connect: setting up gss connection\n"); +#endif + /* get bkserver location: */ + edg_wlc_JobIdGetServerParts(ctx->p_jobid,&host,&port); + port +=1; + /* acquire gss credentials */ + ret = edg_wll_gss_acquire_cred_gsi( + ctx->p_proxy_filename ? ctx->p_proxy_filename : ctx->p_cert_filename, + ctx->p_proxy_filename ? ctx->p_proxy_filename : ctx->p_key_filename, + &cred, &my_subject_name, &gss_stat); + /* give up if unable to acquire prescribed credentials, otherwise go on anonymously */ + if (ret && ctx->p_proxy_filename) { + edg_wll_SetErrorGss(ctx, "edg_wll_gss_acquire_cred_gsi(): failed to load GSI credentials", &gss_stat); + goto edg_wll_log_direct_connect_end; + } +#ifdef EDG_WLL_LOG_STUB + if (my_subject_name) { + // XXX: shouldn't be probably ctx->p_user_lbproxy but some new parameter, eg. ctx->p_user + edg_wll_SetParamString(ctx, EDG_WLL_PARAM_LBPROXY_USER, my_subject_name); + fprintf(stderr,"edg_wll_log_direct_connect: using certificate: %s\n",my_subject_name); + } else { + fprintf(stderr,"edg_wll_log_direct_connect: going on anonymously\n"); + } + fprintf(stderr,"edg_wll_log_direct_connect: opening connection to bkserver host '%s', port '%d'\n", host, port); +#endif + if ((answer = edg_wll_gss_connect(cred,host,port, + &ctx->p_tmp_timeout, conn, &gss_stat)) < 0) { + answer = edg_wll_log_proto_handle_gss_failures(ctx,answer,&gss_stat,"edg_wll_gss_connect()"); + goto edg_wll_log_direct_connect_end; + } + +edg_wll_log_direct_connect_end: +#ifdef EDG_WLL_LOG_STUB + fprintf(stderr,"edg_wll_log_direct_connect: done\n"); +#endif + if (conn->context != GSS_C_NO_CONTEXT) + edg_wll_gss_close(conn,&ctx->p_tmp_timeout); + if (cred != GSS_C_NO_CREDENTIAL) + gss_release_cred(&min_stat, &cred); + if (my_subject_name) free(my_subject_name); + if (host) free(host); + + return answer; +} + +/** + *---------------------------------------------------------------------- + * write/send to bkserver + *---------------------------------------------------------------------- + */ +int edg_wll_log_direct_write(edg_wll_Context ctx, edg_wll_GssConnection *conn, edg_wll_LogLine logline) { int len,count = 0,err; char *buffer; edg_wll_GssStatus gss_code; - edg_wll_ResetError(context); + edg_wll_ResetError(ctx); - /* encode message */ #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_write_direct: encoding message\n"); + fprintf(stderr,"edg_wll_log_direct_write: encoding message\n"); #endif { il_octet_string_t ll; @@ -419,31 +676,31 @@ int edg_wll_log_write_direct(edg_wll_Context context, edg_wll_GssConnection *con len = encode_il_msg(&buffer, &ll); } if(len < 0) { - edg_wll_SetError(context, ENOMEM, "edg_wll_log_write_direct: error encoding message"); + edg_wll_SetError(ctx, ENOMEM, "edg_wll_log_direct_write: error encoding message"); return -1; } - - /* send message */ #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_write_direct: sending message\n"); + fprintf(stderr,"edg_wll_log_direct_write: sending message\n"); #endif count = 0; - if (( err = edg_wll_gss_write_full(con, buffer, len, &context->p_tmp_timeout, &count, &gss_code)) < 0) { - edg_wll_log_proto_handle_gss_failures(context,err,&gss_code,"edg_wll_gss_write_full()"); - edg_wll_UpdateError(context, EDG_WLL_IL_PROTO,"edg_wll_log_write_direct: error sending message"); + if (( err = edg_wll_gss_write_full(conn, buffer, len, &ctx->p_tmp_timeout, &count, &gss_code)) < 0) { + edg_wll_log_proto_handle_gss_failures(ctx,err,&gss_code,"edg_wll_gss_write_full()"); + edg_wll_UpdateError(ctx, EDG_WLL_IL_PROTO,"edg_wll_log_direct_write: error sending message"); return -1; } - if (buffer) free(buffer); - #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_write_direct: done\n"); + fprintf(stderr,"edg_wll_log_direct_write: done\n"); #endif return count; - } -int edg_wll_log_read_direct(edg_wll_Context context, edg_wll_GssConnection *con) +/** + *---------------------------------------------------------------------- + * read/receive from bkserver + *---------------------------------------------------------------------- + */ +int edg_wll_log_direct_read(edg_wll_Context ctx, edg_wll_GssConnection *con) { char *answer = NULL; static char et[256]; @@ -453,60 +710,36 @@ int edg_wll_log_read_direct(edg_wll_Context context, edg_wll_GssConnection *con) errno = err = code = count = 0; - /* get answer */ + edg_wll_ResetError(ctx); + #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_read_direct: reading answer from server...\n"); + fprintf(stderr,"edg_wll_log_direct_read: reading answer from server...\n"); #endif - if ((err = get_reply_gss(context, con, &answer, &lbproto_code, &code)) != 0 ) { - edg_wll_SetError(context, EDG_WLL_IL_PROTO,"edg_wll_edg_wll_log_read_direct: error reading answer from L&B direct server"); + if ((err = get_reply_gss(ctx, con, &answer, &lbproto_code, &code)) != 0 ) { + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO,"edg_wll_edg_wll_log_direct_read: error reading answer from L&B direct server"); } else { #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"log_proto_client_direct: read answer \"%d:%d: %s\"\n",lbproto_code,code,answer); + fprintf(stderr,"edg_wll_log_direct_read: read answer \"%d:%d: %s\"\n",lbproto_code,code,answer); #endif switch (lbproto_code) { case LB_OK: break; case LB_NOMEM: - edg_wll_SetError(context, ENOMEM, "edg_wll_log_read_direct: server out of memory"); + edg_wll_SetError(ctx, ENOMEM, "edg_wll_log_direct_read: server out of memory"); break; case LB_PROTO: - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "edg_wll_log_read_direct: received protocol error response"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "edg_wll_log_direct_read: received protocol error response"); break; case LB_DBERR: - snprintf(et, sizeof(et), "edg_wll_log_read_direct: error details from L&B server: %s", answer); - edg_wll_SetError(context, code, et); + snprintf(et, sizeof(et), "edg_wll_log_direct_read: error details from L&B server: %s", answer); + edg_wll_SetError(ctx, code, et); break; default: - edg_wll_SetError(context, EDG_WLL_IL_PROTO, "edg_wll_log_read_direct: received unknown protocol response"); + edg_wll_SetError(ctx, EDG_WLL_IL_PROTO, "edg_wll_log_direct_read: received unknown protocol response"); break; } } - #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_read_direct: done\n"); + fprintf(stderr,"edg_wll_log_direct_read: done\n"); #endif - return edg_wll_Error(context,NULL,NULL); - + return edg_wll_Error(ctx,NULL,NULL); } - -int edg_wll_log_proto_client_direct(edg_wll_Context context, edg_wll_GssConnection *conn, edg_wll_LogLine logline) -{ - int err = 0; - edg_wll_ResetError(context); - - /* send message */ - if ((err = edg_wll_log_write_direct(context, conn, logline)) == -1) { - edg_wll_UpdateError(context,EDG_WLL_IL_PROTO,"edg_wll_log_proto_client_direct(): edg_ell_log_write_direct error"); - goto edg_wll_log_proto_client_direct_end; - } - - /* get answer */ - if ((err = edg_wll_log_read_direct(context, conn)) != 0) { - edg_wll_UpdateError(context,err,"edg_wll_log_proto_client_direct(): edg_ell_log_read_direct error"); - } - - -edg_wll_log_proto_client_direct_end: - - return edg_wll_Error(context,NULL,NULL); -} - diff --git a/org.glite.lb.client/src/prod_proto.h b/org.glite.lb.client/src/prod_proto.h index 5eec4f3..642e58c 100644 --- a/org.glite.lb.client/src/prod_proto.h +++ b/org.glite.lb.client/src/prod_proto.h @@ -3,12 +3,6 @@ #ident "$Header$" -/** - * \file edg/workload/logging/client/prod_proto.h - * \brief client (producer) part of the logging protocol - * \note private - */ - #ifdef __cplusplus extern "C" { #endif @@ -18,19 +12,128 @@ extern "C" { #include "glite/lb/log_proto.h" #include "glite/lb/context-int.h" -int edg_wll_log_proto_client(edg_wll_Context context, edg_wll_GssConnection *con, edg_wll_LogLine logline); +/** + * client part of the logging protocol, used when sending messages to locallogger + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \param[in] logline message to send + * \return errno + */ +int edg_wll_log_proto_client(edg_wll_Context context, edg_wll_GssConnection *conn, edg_wll_LogLine logline); -int edg_wll_log_proto_client_proxy(edg_wll_Context context, edg_wll_PlainConnection *conn, edg_wll_LogLine logline); -int edg_wll_log_write_proxy(edg_wll_Context context, edg_wll_PlainConnection *conn, edg_wll_LogLine logline); -int edg_wll_log_read_proxy(edg_wll_Context context, edg_wll_PlainConnection *conn); +/** + * connect to locallogger + * \param[in,out] context context to work with + * \param[out] conn opened connection + * \return errno + */ +int edg_wll_log_connect(edg_wll_Context context, edg_wll_GssConnection *conn); -int edg_wll_log_proto_client_direct(edg_wll_Context context, edg_wll_GssConnection *con, edg_wll_LogLine logline); -int edg_wll_log_write_direct(edg_wll_Context context, edg_wll_GssConnection *con, edg_wll_LogLine logline); -int edg_wll_log_read_direct(edg_wll_Context context, edg_wll_GssConnection *con); +/** + * write/send to locallogger + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \param[in] logline message to send + * \return the number of bytes written (zero indicates nothing was written) or -1 on error + */ +int edg_wll_log_write(edg_wll_Context context, edg_wll_GssConnection *conn, edg_wll_LogLine logline); + +/** + * read/receive from locallogger + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \return the number of bytes read (zero indicates nothing was read) or -1 on error + */ +int edg_wll_log_read(edg_wll_Context context, edg_wll_GssConnection *conn); + +/** + * client part of the logging protocol, used when sending messages to lbproxy + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \param[in] logline message to send + * \return errno + */ +int edg_wll_log_proxy_proto_client(edg_wll_Context context, edg_wll_PlainConnection *conn, edg_wll_LogLine logline); + +/** + * connect to lbproxy + * \param[in,out] context context to work with + * \param[out] conn opened connection + * \return errno + */ +int edg_wll_log_proxy_connect(edg_wll_Context context, edg_wll_PlainConnection *conn); + +/** + * write/send to lbproxy + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \param[in] logline message to send + * \return the number of bytes written (zero indicates nothing was written) or -1 on error + */ +int edg_wll_log_proxy_write(edg_wll_Context context, edg_wll_PlainConnection *conn, edg_wll_LogLine logline); +/** + * read/receive from lbproxy + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \return the number of bytes read (zero indicates nothing was read) or -1 on error + */ +int edg_wll_log_proxy_read(edg_wll_Context context, edg_wll_PlainConnection *conn); + +/** + * client part of the logging protocol, used when sending messages directly to bkserver + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \param[in] logline message to send + * \return errno + */ +int edg_wll_log_direct_proto_client(edg_wll_Context context, edg_wll_GssConnection *conn, edg_wll_LogLine logline); + +/** + * connect to bkserver + * \param[in,out] context context to work with + * \param[out] conn opened connection + * \return errno + */ +int edg_wll_log_direct_connect(edg_wll_Context context, edg_wll_GssConnection *conn); + +/** + * write/send to bkserver + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \param[in] logline message to send + * \return the number of bytes written (zero indicates nothing was written) or -1 on error + */ +int edg_wll_log_direct_write(edg_wll_Context context, edg_wll_GssConnection *conn, edg_wll_LogLine logline); + +/** + * read/receive from bkserver + * \param[in,out] context context to work with + * \param[in] conn connection to use + * \return the number of bytes read (zero indicates nothing was read) or -1 on error + */ +int edg_wll_log_direct_read(edg_wll_Context context, edg_wll_GssConnection *conn); + +/** + * Handle GSS failures on the client side + * \param[in,out] context context to work with + * \param[in] code code returned by a previous call + * \param[in] gss_code GSS code returned by a previous call + * \param[in] test additional text to print + * \return errno + */ int edg_wll_log_proto_handle_gss_failures(edg_wll_Context context, int code, edg_wll_GssStatus *gss_code, const char *text); + +/** + * Handle UNIX socket failures on the client side + * \param[in,out] context context to work with + * \param[in] code code returned by a previous call + * \param[in] test additional text to print + * \return errno + */ int edg_wll_log_proto_handle_plain_failures(edg_wll_Context context, int code, const char *text); + #ifdef __cplusplus } #endif diff --git a/org.glite.lb.client/src/producer.c b/org.glite.lb.client/src/producer.c index f448ced..2c5f5db 100644 --- a/org.glite.lb.client/src/producer.c +++ b/org.glite.lb.client/src/producer.c @@ -9,22 +9,14 @@ #include #include #include -#include #include "glite/wmsutils/jobid/strmd5.h" -#include "glite/security/glite_gss.h" -#include "glite/lb/consumer.h" -#include "glite/lb/producer.h" -#include "glite/lb/context-int.h" #include "glite/lb/ulm_parse.h" #include "glite/lb/trio.h" -#include "glite/lb/lb_plain_io.h" -#include "glite/lb/escape.h" -#include "prod_proto.h" -#include "connection.h" +#include "glite/lb/producer.h" -static const char* socket_path="/tmp/lb_proxy_store.sock"; +#include "prod_proto.h" #ifdef FAKE_VERSION int edg_wll_DoLogEvent(edg_wll_Context context, edg_wll_LogLine logline); @@ -32,9 +24,9 @@ int edg_wll_DoLogEventProxy(edg_wll_Context context, edg_wll_LogLine logline); int edg_wll_DoLogEventDirect(edg_wll_Context context, edg_wll_LogLine logline); #else -/* +/** *---------------------------------------------------------------------- - * handle_answers - handle answers from edg_wll_log_proto_client* + * handle_answers - handle answers from edg_wll_log_*proto_client *---------------------------------------------------------------------- */ static @@ -79,69 +71,10 @@ int handle_answers(edg_wll_Context context, int code, const char *text) /** *---------------------------------------------------------------------- - * opens a GSS connection to local-logger - * \param context INOUT context to work with, - * \param con OUT openned connection - *---------------------------------------------------------------------- - */ -int edg_wll_log_connect(edg_wll_Context context, edg_wll_GssConnection *con) -{ - int ret,answer; - char *my_subject_name = NULL; - edg_wll_GssStatus gss_stat; - gss_cred_id_t cred = GSS_C_NO_CREDENTIAL; - OM_uint32 min_stat; - -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect: setting up connection to local-logger\n"); -#endif - /* acquire gss credentials */ - ret = edg_wll_gss_acquire_cred_gsi( - context->p_proxy_filename ? context->p_proxy_filename : context->p_cert_filename, - context->p_proxy_filename ? context->p_proxy_filename : context->p_key_filename, - &cred, &my_subject_name, &gss_stat); - - /* give up if unable to acquire prescribed credentials, otherwise go on anonymously */ - if (ret && context->p_proxy_filename) { - edg_wll_SetErrorGss(context, "edg_wll_gss_acquire_cred_gsi(): failed to load GSI credentials", &gss_stat); - goto edg_wll_log_connect_end; - } - -#ifdef EDG_WLL_LOG_STUB - if (my_subject_name != NULL) { - fprintf(stderr,"edg_wll_log_connect: using certificate: %s\n",my_subject_name); - } else { - fprintf(stderr,"edg_wll_log_connect: going on anonymously!\n"); - } -#endif - /* open an authenticated connection to the local-logger: */ -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect: opening connection to local-logger host %s, port %d\n", - context->p_destination, context->p_dest_port); -#endif - if ((answer = edg_wll_gss_connect(cred, - context->p_destination, context->p_dest_port, - &context->p_tmp_timeout, con, &gss_stat)) < 0) { - answer = edg_wll_log_proto_handle_gss_failures(context,answer,&gss_stat,"edg_wll_gss_connect()"); - goto edg_wll_log_connect_end; - } - -edg_wll_log_connect_end: - if (con->context != GSS_C_NO_CONTEXT) - edg_wll_gss_close(con,&context->p_tmp_timeout); - if (cred != GSS_C_NO_CREDENTIAL) - gss_release_cred(&min_stat, &cred); - if (my_subject_name) free(my_subject_name); - - return answer; -} - -/** - *---------------------------------------------------------------------- * Connects to local-logger and sends already formatted ULM string * \brief helper logging function - * \param context INOUT context to work with, - * \param logline IN formated ULM string + * \param[in,out] context context to work with, + * \param[in] logline formated ULM string *---------------------------------------------------------------------- */ int edg_wll_DoLogEvent( @@ -169,81 +102,10 @@ edg_wll_DoLogEvent_end: /** *---------------------------------------------------------------------- - * opens a plain (UNIX socket) connection to LB Proxy - * \param context INOUT context to work with, - * \param con OUT openned connection - *---------------------------------------------------------------------- - */ -int edg_wll_log_connect_proxy(edg_wll_Context context, edg_wll_PlainConnection *con) -{ - int answer = 0, retries; - int flags; - struct sockaddr_un saddr; - - /* open a connection to the L&B Proxy: */ -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect_proxy: setting up connection to L&B Proxy\n"); -#endif - con->sock = socket(PF_UNIX, SOCK_STREAM, 0); - if (con->sock < 0) { - edg_wll_SetError(context,answer = errno,"socket() error"); - goto edg_wll_log_connect_proxy_end; - } - memset(&saddr, 0, sizeof(saddr)); - saddr.sun_family = AF_UNIX; - strcpy(saddr.sun_path, context->p_lbproxy_store_sock? - context->p_lbproxy_store_sock: socket_path); - if ((flags = fcntl(con->sock, F_GETFL, 0)) < 0 || fcntl(con->sock, F_SETFL, flags | O_NONBLOCK) < 0) { - edg_wll_SetError(context,answer = errno,"fcntl()"); - close(con->sock); - goto edg_wll_log_connect_proxy_end; - } -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect_proxy: openning connection to L&B Proxy at socket %s\n", - context->p_lbproxy_store_sock? context->p_lbproxy_store_sock: socket_path); -#endif - retries = 0; - while ((answer = connect(con->sock, (struct sockaddr *)&saddr, sizeof(saddr))) < 0 && - errno == EAGAIN && - context->p_tmp_timeout.tv_sec >= 0 && context->p_tmp_timeout.tv_usec >= 0 && - !(context->p_tmp_timeout.tv_sec == 0 && context->p_tmp_timeout.tv_usec == 0) - ) - { - struct timespec ns = { 0, PROXY_CONNECT_RETRY * 1000000 /* 10 ms */ },rem; - - nanosleep(&ns,&rem); - - context->p_tmp_timeout.tv_usec -= ns.tv_nsec/1000; - context->p_tmp_timeout.tv_usec += rem.tv_nsec/1000; - - context->p_tmp_timeout.tv_sec -= ns.tv_sec; - context->p_tmp_timeout.tv_sec += rem.tv_sec; - - if (context->p_tmp_timeout.tv_usec < 0) { - context->p_tmp_timeout.tv_usec += 1000000; - context->p_tmp_timeout.tv_sec--; - } - retries++; - } -#ifdef EDG_WLL_LOG_STUB - if (retries) fprintf(stderr,"edg_wll_log_connect_proxy: %d connect retries\n",retries); -#endif - -edg_wll_log_connect_proxy_end: -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect_proxy: done\n"); -#endif - if (con) edg_wll_plain_close(con); - - return answer; -} - -/** - *---------------------------------------------------------------------- * 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 + * \param[in,out] context context to work with, + * \param[in] logline formated ULM string *---------------------------------------------------------------------- */ int edg_wll_DoLogEventProxy( @@ -257,12 +119,12 @@ int edg_wll_DoLogEventProxy( memset(&con, 0, sizeof(con)); /* open a plain connection to L&B Proxy: */ - if ((answer = edg_wll_log_connect_proxy(context,&con)) < 0) { + if ((answer = edg_wll_log_proxy_connect(context,&con)) < 0) { goto edg_wll_DoLogEventProxy_end; } /* and send the message to the L&B Proxy: */ - answer = edg_wll_log_proto_client_proxy(context,&con,logline); + answer = edg_wll_log_proxy_proto_client(context,&con,logline); edg_wll_DoLogEventProxy_end: if (con.sock) edg_wll_plain_close(&con); @@ -272,81 +134,10 @@ edg_wll_DoLogEventProxy_end: /** *---------------------------------------------------------------------- - * opens a GSS connection to bkserver - * \param context INOUT context to work with, - * \param con OUT openned connection - *---------------------------------------------------------------------- - */ -int edg_wll_log_connect_direct(edg_wll_Context context, edg_wll_GssConnection *con) -{ - int ret,answer; - char *my_subject_name = NULL; - edg_wll_GssStatus gss_stat; - gss_cred_id_t cred = GSS_C_NO_CREDENTIAL; - OM_uint32 min_stat; - char *host; - int port; - - ret = answer = 0; - -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect_direct: setting up gss connection\n"); -#endif - /* get bkserver location: */ - edg_wlc_JobIdGetServerParts(context->p_jobid,&host,&port); - port +=1; - - /* acquire gss credentials */ - ret = edg_wll_gss_acquire_cred_gsi( - context->p_proxy_filename ? context->p_proxy_filename : context->p_cert_filename, - context->p_proxy_filename ? context->p_proxy_filename : context->p_key_filename, - &cred, &my_subject_name, &gss_stat); - - /* give up if unable to acquire prescribed credentials, otherwise go on anonymously */ - if (ret && context->p_proxy_filename) { - edg_wll_SetErrorGss(context, "edg_wll_gss_acquire_cred_gsi(): failed to load GSI credentials", &gss_stat); - goto edg_wll_log_connect_direct_end; - } - -#ifdef EDG_WLL_LOG_STUB - if (my_subject_name) { - // XXX: shouldn't be probably context->p_user_lbproxy but some new parameter, eg. context->p_user - edg_wll_SetParamString(context, EDG_WLL_PARAM_LBPROXY_USER, my_subject_name); - fprintf(stderr,"edg_wll_log_connect_direct: using certificate: %s\n",my_subject_name); - } else { - fprintf(stderr,"edg_wll_log_connect_direct: going on anonymously\n"); - } -#endif - /* open an authenticated connection to the bkserver */ -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect_direct: opening connection to bkserver host %s, port %d\n", host, port); -#endif - if ((answer = edg_wll_gss_connect(cred,host,port, - &context->p_tmp_timeout, con, &gss_stat)) < 0) { - answer = edg_wll_log_proto_handle_gss_failures(context,answer,&gss_stat,"edg_wll_gss_connect()"); - goto edg_wll_log_connect_direct_end; - } - -edg_wll_log_connect_direct_end: -#ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_log_connect_direct: done\n"); -#endif - if (con->context != GSS_C_NO_CONTEXT) - edg_wll_gss_close(con,&context->p_tmp_timeout); - if (cred != GSS_C_NO_CREDENTIAL) - gss_release_cred(&min_stat, &cred); - if (my_subject_name) free(my_subject_name); - if (host) free(host); - - return answer; -} - -/** - *---------------------------------------------------------------------- * Connects to bkserver and sends already formatted ULM string * \brief helper logging function - * \param context INOUT context to work with, - * \param logline IN formated ULM string + * \param[in,out] context context to work with, + * \param[in] logline formated ULM string *---------------------------------------------------------------------- */ int edg_wll_DoLogEventDirect( @@ -360,12 +151,12 @@ int edg_wll_DoLogEventDirect( memset(&con, 0, sizeof(con)); /* open a gss connection to bkserver: */ - if ((answer = edg_wll_log_connect_direct(context,&con)) < 0) { + if ((answer = edg_wll_log_direct_connect(context,&con)) < 0) { goto edg_wll_DoLogEventDirect_end; } /* and send the message to the bkserver: */ - answer = edg_wll_log_proto_client_direct(context,&con,logline); + answer = edg_wll_log_direct_proto_client(context,&con,logline); edg_wll_DoLogEventDirect_end: edg_wll_gss_close(&con,&context->p_tmp_timeout); @@ -385,12 +176,12 @@ edg_wll_DoLogEventDirect_end: *---------------------------------------------------------------------- * Formats a logging message * \brief formats a logging message - * \param context INOUT context to work with, - * \param flags IN as defined by LOGFLAG_* - * \param event IN type of the event, - * \param logline OUT formated logging message - * \param fmt IN printf()-like format string, - * \param ... IN event specific values/data according to fmt. + * \param[in,out] context context to work with, + * \param[in] flags as defined by LOGFLAG_* + * \param[in] event type of the event, + * \param[out] logline formated logging message + * \param[in] fmt printf()-like format string, + * \param[in] ... event specific values/data according to fmt. *---------------------------------------------------------------------- */ static int edg_wll_FormatLogLine( @@ -419,11 +210,6 @@ static int edg_wll_FormatLogLine( /* format the message: */ va_start(fmt_args,fmt); - if (trio_vasprintf(&var,fmt,fmt_args) == -1) { - edg_wll_SetError(context,ret = ENOMEM,"edg_wll_FormatLogLine(): trio_vasprintf() error"); - goto edg_wll_formatlogline_end; - } - va_end(fmt_args); gettimeofday(&start_time,0); if (edg_wll_ULMTimevalToDate(start_time.tv_sec,start_time.tv_usec,date) != 0) { @@ -460,6 +246,10 @@ static int edg_wll_FormatLogLine( } else { dguser = strdup(""); } + if (trio_vasprintf(&var,fmt,fmt_args) == -1) { + edg_wll_SetError(context,ret = ENOMEM,"edg_wll_FormatLogLine(): trio_vasprintf() error"); + goto edg_wll_formatlogline_end; + } if (asprintf(&out,"%s%s%s\n",fix,dguser,var) == -1) { edg_wll_SetError(context,ret = ENOMEM,"edg_wll_FormatLogLine(): asprintf() error"); goto edg_wll_formatlogline_end; @@ -472,7 +262,7 @@ static int edg_wll_FormatLogLine( } #ifdef EDG_WLL_LOG_STUB - fprintf(stderr,"edg_wll_FormatLogLine (%d chars): %s",size,out); +// fprintf(stderr,"edg_wll_FormatLogLine (%d chars): %s",size,out); #endif if (out) { *logline = out; @@ -481,6 +271,7 @@ static int edg_wll_FormatLogLine( } edg_wll_formatlogline_end: + va_end(fmt_args); if (seq) free(seq); if (fix) free(fix); if (dguser) free(dguser); @@ -499,11 +290,11 @@ edg_wll_formatlogline_end: *---------------------------------------------------------------------- * Formats a logging message and sends it to local-logger * \brief master logging event function - * \param context INOUT context to work with, - * \param flags IN as defined by LOGFLAG_* - * \param event IN type of the event, - * \param fmt IN printf()-like format string, - * \param ... IN event specific values/data according to fmt. + * \param[in,out] context INOUT context to work with, + * \param[in] flags as defined by LOGFLAG_* + * \param[in] event type of the event, + * \param[in] fmt printf()-like format string, + * \param[in] ... event specific values/data according to fmt. *---------------------------------------------------------------------- */ static int edg_wll_LogEventMaster( @@ -515,7 +306,7 @@ static int edg_wll_LogEventMaster( va_list fmt_args; int priority; int ret; - edg_wll_LogLine out; + edg_wll_LogLine in,out; priority = flags & LOGFLAG_SYNC; @@ -527,7 +318,12 @@ static int edg_wll_LogEventMaster( /* format the message: */ va_start(fmt_args,fmt); - if (edg_wll_FormatLogLine(context,flags,event,&out,fmt,fmt_args) != 0 ) { + if (trio_vasprintf(&in,fmt,fmt_args) == -1) { + edg_wll_UpdateError(context,ret = ENOMEM,"edg_wll_LogEventMaster(): trio_vasprintf() error"); + goto edg_wll_logeventmaster_end; + } + + if (edg_wll_FormatLogLine(context,flags,event,&out,"%s",in) != 0 ) { edg_wll_UpdateError(context,ret = EINVAL,"edg_wll_LogEventMaster(): edg_wll_FormatLogLine() error"); goto edg_wll_logeventmaster_end; } @@ -563,6 +359,7 @@ static int edg_wll_LogEventMaster( edg_wll_logeventmaster_end: va_end(fmt_args); + if (in) free(in); if (out) free(out); if (!ret) if(edg_wll_IncSequenceCode(context)) { @@ -1071,33 +868,35 @@ int edg_wll_RegisterJobProxy( /* and now do the pseudo-parallel registration: */ /* connect to bkserver */ - if ((ret = edg_wll_log_connect_direct(context,&con_bkserver)) < 0) { - edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_connect_direct error"); + if ((ret = edg_wll_log_direct_connect(context,&con_bkserver)) < 0) { + edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_direct_connect error"); goto edg_wll_registerjobproxy_end; } /* connect to lbproxy */ - if ((ret = edg_wll_log_connect_proxy(context,&con_lbproxy)) < 0) { - edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_connect_proxy error"); + if ((ret = edg_wll_log_proxy_connect(context,&con_lbproxy)) < 0) { + edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_proxy_connect error"); goto edg_wll_registerjobproxy_end; } /* send to bkserver */ - if ((ret = edg_wll_log_write_direct(context,&con_bkserver,logline)) == -1) { - edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_write_direct error"); +/* + if ((ret = edg_wll_log_direct_write(context,&con_bkserver,logline)) == -1) { + edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_direct_write error"); goto edg_wll_registerjobproxy_end; } +*/ /* send to lbproxy */ - if ((ret = edg_wll_log_write_proxy(context,&con_lbproxy,logline)) == -1) { - edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_write_proxy error"); + if ((ret = edg_wll_log_proxy_write(context,&con_lbproxy,logline)) == -1) { + edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_proxy_write error"); goto edg_wll_registerjobproxy_end; } /* select and read the answers */ - FD_SET(con_bkserver.sock,&fdset); - n = con_bkserver.sock; - FD_SET(con_lbproxy.sock,&fdset); - if (con_lbproxy.sock > n) n = con_lbproxy.sock; - n += 1; count = 2; while (count > 0) { + FD_SET(con_bkserver.sock,&fdset); + n = con_bkserver.sock; + FD_SET(con_lbproxy.sock,&fdset); + if (con_lbproxy.sock > n) n = con_lbproxy.sock; + n += 1; fd = select(n,&fdset,NULL,NULL,&context->p_tmp_timeout); switch (fd) { case 0: /* timeout */ @@ -1116,16 +915,16 @@ int edg_wll_RegisterJobProxy( } if (FD_ISSET(con_bkserver.sock,&fdset)) { /* read answer from bkserver */ - if ((ret = edg_wll_log_read_direct(context,&con_bkserver)) == -1) { - edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_read_direct error"); + if ((ret = edg_wll_log_direct_read(context,&con_bkserver)) == -1) { + edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_direct_read error"); goto edg_wll_registerjobproxy_end; } count -= 1; } if (FD_ISSET(con_lbproxy.sock,&fdset)) { /* read answer from lbproxy */ - if ((ret = edg_wll_log_read_proxy(context,&con_lbproxy)) == -1) { - edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_read_proxy error"); + if ((ret = edg_wll_log_proxy_read(context,&con_lbproxy)) == -1) { + edg_wll_UpdateError(context,EAGAIN,"edg_wll_RegisterJobProxy(): edg_wll_log_proxy_read error"); goto edg_wll_registerjobproxy_end; } count -= 1; -- 1.8.2.3