From 7823320e2b23667f08b2a49d8971ad1fd7014199 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Wed, 10 Oct 2007 16:34:18 +0000 Subject: [PATCH] lbjp-common.server-bones: - finalize ETICSisms - merge branch changes - switch LB and JP (moved daemonize function to this module) --- org.glite.jp.index/Makefile | 5 +- org.glite.jp.index/src/bones_server.c | 5 +- org.glite.jp.index/src/common_server.c | 75 ------------------ org.glite.jp.index/src/common_server.h | 6 -- org.glite.jp.primary/Makefile | 2 +- org.glite.jp.primary/src/bones_server.c | 2 +- org.glite.lb.server-bones/Makefile | 10 +-- org.glite.lb.server-bones/src/srvbones.c | 26 ++++++- org.glite.lb.server/Makefile | 4 +- org.glite.lb.server/src/bkserverd.c | 2 +- org.glite.lbjp-common.server-bones/Makefile | 38 +++------ .../interface/srvbones.h | 7 ++ .../project/version.properties | 4 + org.glite.lbjp-common.server-bones/src/srvbones.c | 90 +++++++++++++++++++++- 14 files changed, 143 insertions(+), 133 deletions(-) delete mode 100644 org.glite.jp.index/src/common_server.c delete mode 100644 org.glite.jp.index/src/common_server.h create mode 100644 org.glite.lbjp-common.server-bones/project/version.properties diff --git a/org.glite.jp.index/Makefile b/org.glite.jp.index/Makefile index 062b12e..8037980 100644 --- a/org.glite.jp.index/Makefile +++ b/org.glite.jp.index/Makefile @@ -54,7 +54,7 @@ ps_prefix:=${ws_prefix} SRCS:= conf.c bones_server.c soap_ops.c soap_ps_calls.c common.c \ ${is_prefix}Server.c ${ps_prefix}Client.c ${ws_prefix}C.c \ - ws_ps_typeref.c ws_is_typeref.c db_ops.c context.c common_server.c + ws_ps_typeref.c ws_is_typeref.c db_ops.c context.c EXA_TEST_SRCS:=jpis-test.c ${is_prefix}Client.c ${is_prefix}C.c context.c db_ops.c conf.c ws_is_typeref.c common.c EXA_DB_SRCS:=jpis-db-internal.c db_ops.c conf.c context.c ws_is_typeref.c common.c @@ -68,7 +68,7 @@ EXA_CLIENT_OBJS:=${EXA_CLIENT_SRCS:.c=.o} COMMONLIB:=-lglite_jp_common_${nothrflavour} SRVCOMMONLIB:=-lglite_jp_server_common -BONESLIB:=-lglite_lb_server_bones +BONESLIB:=-lglite_lbu_server_bones TRIOLIB:=-lglite_lbu_trio GSSLIB:=-lglite_security_gss_${nothrflavour} @@ -159,7 +159,6 @@ soap_ps_calls.o: soap_ps_calls.c ${ps_prefix}H.h ${ps_prefix}.nsmap soap_version soap_ops.o: soap_ops.c jp_H.h ${is_prefix}.nsmap soap_version.h db_ops.h ws_ps_typeref.h ws_is_typeref.h context.h ws_ps_typeref.o: ws_ps_typeref.c ${ps_prefix}H.h ws_typemap.h ws_ps_typeref.h soap_version.h ws_is_typeref.o: ws_is_typeref.c ${is_prefix}H.h ws_typemap.h ws_is_typeref.h soap_version.h -comon_server.o: common_server.c common_server.h jpis-client.o: jpis-client.c ${is_prefix}H.h soap_version.h jpis-test.o: jpis-client.c ${is_prefix}H.h soap_version.h conf.o: conf.c ${is_prefix}H.h soap_version.h diff --git a/org.glite.jp.index/src/bones_server.c b/org.glite.jp.index/src/bones_server.c index c798f29..7e0ffc1 100644 --- a/org.glite.jp.index/src/bones_server.c +++ b/org.glite.jp.index/src/bones_server.c @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include @@ -20,7 +20,6 @@ #include "soap_ps_calls.h" #include "context.h" #include "common.h" -#include "common_server.h" #include "soap_version.h" #include "jp_H.h" @@ -96,7 +95,7 @@ int main(int argc, char *argv[]) } /* daemonize */ - if (!conf->debug) glite_jpis_daemonize("glite-jp-indexd", conf->pidfile, conf->logfile); + if (!conf->debug) glite_srvbones_daemonize("glite-jp-indexd", conf->pidfile, conf->logfile); /* XXX preliminary support for plugins for (i=0; conf->plugins[i]; i++) diff --git a/org.glite.jp.index/src/common_server.c b/org.glite.jp.index/src/common_server.c deleted file mode 100644 index a815293..0000000 --- a/org.glite.jp.index/src/common_server.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* TODO: belongs to server-bones */ - -int glite_jpis_daemonize(const char *servername, const char *custom_pidfile, const char *custom_logfile) { - int lfd, opid; - FILE *fpid; - pid_t master; - char *pidfile, *logfile; - - if (!custom_logfile) { - asprintf(&logfile, "%s/%s.log", geteuid() == 0 ? "/var/log" : getenv("HOME"), servername); - } else { - logfile = NULL; - } - lfd = open(logfile ? logfile : custom_logfile,O_CREAT|O_TRUNC|O_WRONLY,0600); - if (lfd < 0) { - fprintf(stderr,"%s: %s: %s\n",servername,logfile,strerror(errno)); - free(logfile); - return 0; - } -// printf("logfile: %s\n", logfile ? logfile : custom_logfile); - free(logfile); - - if (daemon(0,1) == -1) { - perror("can't daemonize"); - return 0; - } - dup2(lfd,1); - dup2(lfd,2); - - if (!custom_pidfile) { - asprintf(&pidfile, "%s/%s.pid", geteuid() == 0 ? "/var/run" : getenv("HOME"), servername); - } else { - pidfile = strdup(custom_pidfile); - } -// printf("pidfile: %s\n", pidfile ? pidfile : custom_pidfile); - setpgrp(); /* needs for signalling */ - master = getpid(); - fpid = fopen(pidfile,"r"); - if ( fpid ) - { - opid = -1; - - if ( fscanf(fpid,"%d",&opid) == 1 ) - { - if ( !kill(opid,0) ) - { - fprintf(stderr,"%s: another instance running, pid = %d\n",servername,opid); - return 0; - } - else if (errno != ESRCH) { perror("kill()"); return 0; } - } - fclose(fpid); - } else if (errno != ENOENT) { perror(pidfile); free(pidfile); return 0; } - - if (((fpid = fopen(pidfile, "w")) == NULL) || - (fprintf(fpid, "%d", getpid()) <= 0) || - (fclose(fpid) != 0)) { - perror(pidfile); - free(pidfile); - return 0; - } - - free(pidfile); - return 1; -} diff --git a/org.glite.jp.index/src/common_server.h b/org.glite.jp.index/src/common_server.h deleted file mode 100644 index 45db839..0000000 --- a/org.glite.jp.index/src/common_server.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef GLITE_JPIS_COMMON_SERVER_H -#define GLITE_JPIS_COMMON_SERVER_H - -int glite_jpis_daemonize(const char *servername, const char *custom_pidfile, const char *custom_logfile); - -#endif diff --git a/org.glite.jp.primary/Makefile b/org.glite.jp.primary/Makefile index 564a19e..e7cc748 100644 --- a/org.glite.jp.primary/Makefile +++ b/org.glite.jp.primary/Makefile @@ -79,7 +79,7 @@ DAG_OBJS:=${DAG_SRCS:.c=.o} dotless_soap_ver:=${shell echo ${gsoap_version} | tr -d . } COMMONLIB:=-lglite_jp_common_${nothrflavour} -BONESLIB:=-lglite_lb_server_bones +BONESLIB:=-lglite_lbu_server_bones GSOAPLIB:=-L${stagedir}/lib -lglite_security_gsoap_plugin_${dotless_soap_ver}_${nothrflavour} TRIOLIB:=-lglite_lbu_trio LIBTARLIB:=-L${libtar_prefix}/lib -ltar diff --git a/org.glite.jp.primary/src/bones_server.c b/org.glite.jp.primary/src/bones_server.c index 25e055b..a15ba94 100644 --- a/org.glite.jp.primary/src/bones_server.c +++ b/org.glite.jp.primary/src/bones_server.c @@ -7,7 +7,7 @@ #include "glite/jp/types.h" #include "glite/jp/context.h" -#include "glite/lb/srvbones.h" +#include "glite/lbu/srvbones.h" #include "glite/security/glite_gss.h" #include diff --git a/org.glite.lb.server-bones/Makefile b/org.glite.lb.server-bones/Makefile index 947df59..9448405 100644 --- a/org.glite.lb.server-bones/Makefile +++ b/org.glite.lb.server-bones/Makefile @@ -2,7 +2,7 @@ top_srcdir=. stagedir=. globalprefix=glite -lbprefix=lb +lbutilsprefix=lbu package=glite-lb-server-bones version=0.0.1 PREFIX=/opt/glite @@ -29,8 +29,8 @@ COMPILE:=libtool --mode=compile ${CC} ${CFLAGS} LINK:=libtool --mode=link ${CC} -rpath ${stagedir}/lib ${LDFLAGS} INSTALL:=libtool --mode=install install -STATICLIB:=libglite_lb_server_bones.a -LTLIB:=libglite_lb_server_bones.la +STATICLIB:=libglite_lbu_server_bones.a +LTLIB:=libglite_lbu_server_bones.la OBJS:=srvbones.o LOBJS:=${OBJS:.o=.lo} @@ -65,12 +65,12 @@ cnt_example: cnt_example.o doc: install: - mkdir -p ${PREFIX}/include/${globalprefix}/${lbprefix} + mkdir -p ${PREFIX}/include/${globalprefix}/${lbutilsprefix} mkdir -p ${PREFIX}/lib ${INSTALL} -m 644 ${LTLIB} ${PREFIX}/lib if [ x${DOSTAGE} = xyes ]; then \ ${INSTALL} -m 644 ${STATICLIB} ${PREFIX}/lib ; \ - cd ${top_srcdir}/interface && install -m 644 ${HDRS} ${PREFIX}/include/${globalprefix}/${lbprefix} ; \ + cd ${top_srcdir}/interface && install -m 644 ${HDRS} ${PREFIX}/include/${globalprefix}/${lbutilsprefix} ; \ fi clean: diff --git a/org.glite.lb.server-bones/src/srvbones.c b/org.glite.lb.server-bones/src/srvbones.c index bced95c..9cb1b65 100644 --- a/org.glite.lb.server-bones/src/srvbones.c +++ b/org.glite.lb.server-bones/src/srvbones.c @@ -105,6 +105,7 @@ int glite_srvbones_run( struct sigaction sa; sigset_t sset; int sock_slave[2], i; + int pstat; assert(service_table); @@ -174,8 +175,25 @@ int glite_srvbones_run( { int pid; - while ( (pid = waitpid(-1, NULL, WNOHANG)) > 0 ) + while ( (pid = waitpid(-1, &pstat, WNOHANG)) > 0 ) { + if (WIFEXITED(pstat)) { + dprintf(("[master] Slave %d exited with return code %d.\n", pid, WEXITSTATUS(pstat))); + if (WEXITSTATUS(pstat)) { + syslog(LOG_ERR, "Slave %d exited with return code %d.\n", pid, WEXITSTATUS(pstat)); + } + } + if (WIFSIGNALED(pstat)) { + dprintf(("[master] Slave %d terminated with signal %d.\n", pid, WTERMSIG(pstat))); + switch (WTERMSIG(pstat)) { + case SIGINT: + case SIGTERM: + case SIGUSR1: if (die) break; + default: + syslog(LOG_ERR, "Slave %d terminated with signal %d.\n", pid, WTERMSIG(pstat)); + break; + } + } if ( !die ) { int newpid = slave(slave_data_init, sock_slave[1]); @@ -356,7 +374,7 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock) FD_ZERO(&fds); - FD_SET(sock, &fds); + if ( conn < 0 || !first_request) FD_SET(sock, &fds); if ( conn >= 0 ) FD_SET(conn, &fds); if ( conn > sock ) max = conn; @@ -510,8 +528,8 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock) if ( services[srv].on_new_conn_hnd && (ret = services[srv].on_new_conn_hnd(conn, to.tv_sec >= 0 ? &to : NULL, clnt_data)) ) { - dprintf(("[%d] Connection not estabilished, err = %d.\n", getpid(),ret)); - if ( !debug ) syslog(LOG_ERR, "Connection not estabilished, err = %d.\n",ret); + dprintf(("[%d] Connection not established, err = %d.\n", getpid(),ret)); + if ( !debug ) syslog(LOG_ERR, "Connection not established, err = %d.\n",ret); close(conn); conn = srv = -1; if (ret < 0) exit(1); diff --git a/org.glite.lb.server/Makefile b/org.glite.lb.server/Makefile index c9b8e92..1516418 100644 --- a/org.glite.lb.server/Makefile +++ b/org.glite.lb.server/Makefile @@ -92,12 +92,12 @@ CFLAGS:= \ -I${globus_prefix}/include/${nothrflavour} #gridsite needs this ifdef LB_PROF - SRVBONES_LIB:= ${stagedir}/lib/libglite_lb_server_bones.la + SRVBONES_LIB:= ${stagedir}/lib/libglite_lbu_server_bones.la LB_COMMON_LIB:=${stagedir}/lib/libglite_lb_common_${nothrflavour}.la CFLAGS:=${CFLAGS} -pg -g LDFLAGS:=${LDFLAGS} -pg else - SRVBONES_LIB:= -L${stagedir}/lib -lglite_lb_server_bones + SRVBONES_LIB:= -L${stagedir}/lib -lglite_lbu_server_bones LB_COMMON_LIB:=-lglite_lb_common_${nothrflavour} LDFLAGS:= -L${stagedir}/lib endif diff --git a/org.glite.lb.server/src/bkserverd.c b/org.glite.lb.server/src/bkserverd.c index 13503db..914cb81 100644 --- a/org.glite.lb.server/src/bkserverd.c +++ b/org.glite.lb.server/src/bkserverd.c @@ -37,7 +37,7 @@ #include "glite/jobid/cjobid.h" #include "glite/security/glite_gss.h" -#include "glite/lb/srvbones.h" +#include "glite/lbu/srvbones.h" #include "glite/lb/context.h" #include "glite/lb/mini_http.h" #include "glite/lb/context-int.h" diff --git a/org.glite.lbjp-common.server-bones/Makefile b/org.glite.lbjp-common.server-bones/Makefile index 100ef16..4bfdffd 100644 --- a/org.glite.lbjp-common.server-bones/Makefile +++ b/org.glite.lbjp-common.server-bones/Makefile @@ -1,26 +1,21 @@ # defaults -top_srcdir=. +top_srcdir=.. stagedir=. -distdir=. globalprefix=glite -lbprefix=lb +lbutilsprefix=lbu package=glite-lb-server-bones version=0.0.1 PREFIX=/opt/glite -glite_location=/opt/glite - CC=gcc -include Makefile.inc -include ../project/version.properties -version=${module.version} - VPATH=${top_srcdir}/src:${top_srcdir}/examples DEBUG:=-g -O0 -Wall -CFLAGS:= ${DEBUG} -I${top_srcdir}/interface +CFLAGS:= ${DEBUG} -I${top_srcdir}/interface -D_GNU_SOURCE LDFLAGS:= ifdef LB_PROF @@ -32,8 +27,8 @@ COMPILE:=libtool --mode=compile ${CC} ${CFLAGS} LINK:=libtool --mode=link ${CC} -rpath ${stagedir}/lib ${LDFLAGS} INSTALL:=libtool --mode=install install -STATICLIB:=libglite_lb_server_bones.a -LTLIB:=libglite_lb_server_bones.la +STATICLIB:=libglite_lbu_server_bones.a +LTLIB:=libglite_lbu_server_bones.la OBJS:=srvbones.o LOBJS:=${OBJS:.o=.lo} @@ -67,29 +62,16 @@ cnt_example: cnt_example.o doc: -dist: distsrc distbin - -distsrc: - mkdir -p ${top_srcdir}/${package}-${version} - cd ${top_srcdir} && GLOBIGNORE="${package}-${version}" && cp -Rf * ${package}-${version} - cd ${top_srcdir} && tar -czf ${distdir}/${package}-${version}_src.tar.gz --exclude-from=project/tar_exclude ${package}-${version} - rm -rf ${top_srcdir}/${package}-${version} - -distbin: - $(MAKE) install PREFIX=`pwd`/tmpbuilddir${stagedir} - save_dir=`pwd`; cd tmpbuilddir${stagedir} && tar -czf $$save_dir/${top_srcdir}/${distdir}/${package}-${version}_bin.tar.gz *; cd $$save_dir - rm -rf tmpbuilddir - install: - mkdir -p ${PREFIX}/include/${globalprefix}/${lbprefix} + mkdir -p ${PREFIX}/include/${globalprefix}/${lbutilsprefix} mkdir -p ${PREFIX}/lib ${INSTALL} -m 644 ${LTLIB} ${PREFIX}/lib - if [ x${DOSTAGE} = xyes ]; then \ - ${INSTALL} -m 644 ${STATICLIB} ${PREFIX}/lib ; \ - cd ${top_srcdir}/interface && install -m 644 ${HDRS} ${PREFIX}/include/${globalprefix}/${lbprefix} ; \ - fi + ${INSTALL} -m 644 ${STATICLIB} ${PREFIX}/lib ; \ + cd ${top_srcdir}/interface && install -m 644 ${HDRS} ${PREFIX}/include/${globalprefix}/${lbutilsprefix} clean: + rm -rvf *.o *.lo .libs lib* srv_example cnt_example + rm -rvf log.xml project/ rpmbuild/ RPMS/ tgz/ %.o: %.c ${COMPILE} -c $< diff --git a/org.glite.lbjp-common.server-bones/interface/srvbones.h b/org.glite.lbjp-common.server-bones/interface/srvbones.h index f238705..31ab16f 100644 --- a/org.glite.lbjp-common.server-bones/interface/srvbones.h +++ b/org.glite.lbjp-common.server-bones/interface/srvbones.h @@ -85,6 +85,13 @@ extern int glite_srvbones_run( size_t table_sz, int dbg); +/** + * helper common function to daemonize server + * + * \returns 1 OK, 0 error writtten to stderr + */ +int glite_srvbones_daemonize(const char *servername, const char *custom_pidfile, const char *custom_logfile); + #ifdef __cplusplus } #endif diff --git a/org.glite.lbjp-common.server-bones/project/version.properties b/org.glite.lbjp-common.server-bones/project/version.properties new file mode 100644 index 0000000..32cfddd --- /dev/null +++ b/org.glite.lbjp-common.server-bones/project/version.properties @@ -0,0 +1,4 @@ +# $Id$ +# $Name$ +module.version=1.0.0 +module.age=1 diff --git a/org.glite.lbjp-common.server-bones/src/srvbones.c b/org.glite.lbjp-common.server-bones/src/srvbones.c index bced95c..3fcc41c 100644 --- a/org.glite.lbjp-common.server-bones/src/srvbones.c +++ b/org.glite.lbjp-common.server-bones/src/srvbones.c @@ -105,6 +105,7 @@ int glite_srvbones_run( struct sigaction sa; sigset_t sset; int sock_slave[2], i; + int pstat; assert(service_table); @@ -174,8 +175,25 @@ int glite_srvbones_run( { int pid; - while ( (pid = waitpid(-1, NULL, WNOHANG)) > 0 ) + while ( (pid = waitpid(-1, &pstat, WNOHANG)) > 0 ) { + if (WIFEXITED(pstat)) { + dprintf(("[master] Slave %d exited with return code %d.\n", pid, WEXITSTATUS(pstat))); + if (WEXITSTATUS(pstat)) { + syslog(LOG_ERR, "Slave %d exited with return code %d.\n", pid, WEXITSTATUS(pstat)); + } + } + if (WIFSIGNALED(pstat)) { + dprintf(("[master] Slave %d terminated with signal %d.\n", pid, WTERMSIG(pstat))); + switch (WTERMSIG(pstat)) { + case SIGINT: + case SIGTERM: + case SIGUSR1: if (die) break; + default: + syslog(LOG_ERR, "Slave %d terminated with signal %d.\n", pid, WTERMSIG(pstat)); + break; + } + } if ( !die ) { int newpid = slave(slave_data_init, sock_slave[1]); @@ -217,6 +235,70 @@ int glite_srvbones_run( return 0; } +int glite_srvbones_daemonize(const char *servername, const char *custom_pidfile, const char *custom_logfile) { + int lfd, opid; + FILE *fpid; + pid_t master; + char *pidfile, *logfile; + + if (!custom_logfile) { + asprintf(&logfile, "%s/%s.log", geteuid() == 0 ? "/var/log" : getenv("HOME"), servername); + } else { + logfile = NULL; + } + lfd = open(logfile ? logfile : custom_logfile,O_CREAT|O_TRUNC|O_WRONLY,0600); + if (lfd < 0) { + fprintf(stderr,"%s: %s: %s\n",servername,logfile,strerror(errno)); + free(logfile); + return 0; + } +// printf("logfile: %s\n", logfile ? logfile : custom_logfile); + free(logfile); + + if (daemon(0,1) == -1) { + perror("can't daemonize"); + return 0; + } + dup2(lfd,1); + dup2(lfd,2); + + if (!custom_pidfile) { + asprintf(&pidfile, "%s/%s.pid", geteuid() == 0 ? "/var/run" : getenv("HOME"), servername); + } else { + pidfile = strdup(custom_pidfile); + } +// printf("pidfile: %s\n", pidfile ? pidfile : custom_pidfile); + setpgrp(); /* needs for signalling */ + master = getpid(); + fpid = fopen(pidfile,"r"); + if ( fpid ) + { + opid = -1; + + if ( fscanf(fpid,"%d",&opid) == 1 ) + { + if ( !kill(opid,0) ) + { + fprintf(stderr,"%s: another instance running, pid = %d\n",servername,opid); + return 0; + } + else if (errno != ESRCH) { perror("kill()"); return 0; } + } + fclose(fpid); + } else if (errno != ENOENT) { perror(pidfile); free(pidfile); return 0; } + + if (((fpid = fopen(pidfile, "w")) == NULL) || + (fprintf(fpid, "%d", getpid()) <= 0) || + (fclose(fpid) != 0)) { + perror(pidfile); + free(pidfile); + return 0; + } + + free(pidfile); + return 1; +} + static int dispatchit(int sock_slave, int sock, int sidx) { struct sockaddr_in a; @@ -356,7 +438,7 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock) FD_ZERO(&fds); - FD_SET(sock, &fds); + if ( conn < 0 || !first_request) FD_SET(sock, &fds); if ( conn >= 0 ) FD_SET(conn, &fds); if ( conn > sock ) max = conn; @@ -510,8 +592,8 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock) if ( services[srv].on_new_conn_hnd && (ret = services[srv].on_new_conn_hnd(conn, to.tv_sec >= 0 ? &to : NULL, clnt_data)) ) { - dprintf(("[%d] Connection not estabilished, err = %d.\n", getpid(),ret)); - if ( !debug ) syslog(LOG_ERR, "Connection not estabilished, err = %d.\n",ret); + dprintf(("[%d] Connection not established, err = %d.\n", getpid(),ret)); + if ( !debug ) syslog(LOG_ERR, "Connection not established, err = %d.\n",ret); close(conn); conn = srv = -1; if (ret < 0) exit(1); -- 1.8.2.3