From: Dave H. <hel...@us...> - 2013-10-31 23:00:48
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "sfcb - Small Footprint CIM Broker". The branch, master has been updated via 2fd9b09d6dd874ee1dc222260753b9648acfe043 (commit) from cc5e641739542c764d9803119720641447d9ea32 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 2fd9b09d6dd874ee1dc222260753b9648acfe043 Author: Dave Heller <hel...@us...> Date: Thu Oct 31 19:00:18 2013 -0400 [sfcb-tix:#76] Identify running SFCB processes in ps output ----------------------------------------------------------------------- Summary of changes: contributions.txt | 1 + control.c | 51 ++++++++++++++++++++++++++++++++++++++++++ httpAdapter.c | 20 ++++++++++++++++ mlog.c | 7 ++++++ providerDrv.c | 13 +++++++++++ sfcBroker.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++----- sfcb.cfg.pre.in | 11 ++++++++- sfcbproc.c | 2 +- 8 files changed, 160 insertions(+), 8 deletions(-) diff --git a/contributions.txt b/contributions.txt index bad54f6..1d55c32 100644 --- a/contributions.txt +++ b/contributions.txt @@ -125,6 +125,7 @@ Klaus Kampf, Novell 09/20/2013 [sfcb-tix:#75] fileRepository: show which directory could not be opened 09/30/2013 [sfcb-tix:#77] Fix prototype for stopBroker 10/01/2013 [sfcb-tix:#62] SIGSEGV in ClassProvider, return of 0 from addClParameter() not checked +10/31/2013 [sfcb-tix:#76] Identify running SFCB processes in ps output (contributions) Mike Brasher, Inova ------------------- diff --git a/control.c b/control.c index 1e64c42..0566dfd 100644 --- a/control.c +++ b/control.c @@ -71,9 +71,60 @@ char *configfile = NULL; char *ip4List= NULL; char *ip6List= NULL; +char **origArgv; +int origArgc; +unsigned int labelProcs; + +/** + * Kindly null terminate, always, even if might overwrite + * the last char of the truncated string. +*/ +inline char *strncpy_kind(char *to, char *from, size_t size) { + strncpy(to, from, size); + *(to + size - 1) = '\0'; + return to; +} + +/** + * Helper functions for labeling a process by modifying the process argv string. + * On the first call, convert the original argv[] to a contiguous string by + * replacing inter-arg nulls with spaces and adding a trailing space; then + * append appendstr. On subsequent calls only append appendstr. Passing + * NULL for appendstr forces the function to act as if called for the first + * time, i.e. reset the static ptr and prepare the argv[] string. + */ +void append2Argv(char *appendstr) { + int i; + static char *ptr = NULL; + if (!ptr || !appendstr) { /* called for the 1st time or intentionally reset */ + for (i=1 ; i < origArgc ; i++) + *(*(origArgv+i) - 1) = ' '; /* replace inter-arg nulls with spaces */ + ptr = origArgv[origArgc - 1]; + } + if (appendstr) + /* Enforce limit of appending no more than labelProcs chars */ + ptr += strlen(strncpy_kind(ptr, appendstr, + labelProcs - (ptr - origArgv[origArgc - 1]) + 1)); +} + +/** + * Restore the process argv[] to its original form by replacing inter-arg spaces + * with nulls. May be required prior to spawning a child process. To prepare for + * a full restart, additionally remove the pad argument. + */ +void restoreOrigArgv(int removePad) { + int i; + for (i=1 ; i < origArgc ; i++) + *(*(origArgv+i) - 1) = '\0'; /* restore inter-arg nulls */ + + if (removePad) + origArgv[origArgc - 1] = NULL; +} + /* Control initial values { property, type, string value, numeric value} */ static Control init[] = { + {"labelProcs", CTL_UINT, NULL, {.uint=0}}, {"ip4AddrList", CTL_STRING, NULL, {0}}, {"ip6AddrList", CTL_STRING, NULL, {0}}, {"httpPort", CTL_LONG, NULL, {.slong=5988}}, diff --git a/httpAdapter.c b/httpAdapter.c index 6ebf481..1c6003c 100644 --- a/httpAdapter.c +++ b/httpAdapter.c @@ -2282,6 +2282,26 @@ httpDaemon(int argc, char *argv[], int sslMode, char *ipAddr, keepaliveMaxRequest); } + /* Label the process by modifying the cmdline */ + extern void append2Argv(char *appendstr); + extern unsigned int labelProcs; + if (labelProcs) { + append2Argv(NULL); + append2Argv("-proc:HttpDaemon "); + append2Argv("-ip:"); + char *l = ""; + char *r = ""; +#ifdef HAVE_IPV6 + if (ipAddrFam == AF_INET6) { + l = "["; + r = "]"; + } +#endif + append2Argv(l); + append2Argv(ipAddr); + append2Argv(r); + } + if (enableHttp) { httpListenFd = getSocket(ipAddrFam); } diff --git a/mlog.c b/mlog.c index 177dfcc..176cc59 100644 --- a/mlog.c +++ b/mlog.c @@ -109,6 +109,13 @@ startLogging(int level, int thread) setSignal(SIGHUP, SIG_IGN, 0); setSignal(SIGUSR2, SIG_IGN, 0); + /* Label the process by modifying the cmdline */ + extern void append2Argv(char *appendstr); + extern unsigned int labelProcs; + if (labelProcs) { + append2Argv("-proc:Logger"); + } + runLogger(logfds[0], level); close(logfds[0]); diff --git a/providerDrv.c b/providerDrv.c index 2c3cb50..03f561b 100644 --- a/providerDrv.c +++ b/providerDrv.c @@ -896,6 +896,19 @@ getProcess(ProviderInfo * info, ProviderProcess ** proc) setSignal(SIGSEGV, handleSigSegv, SA_ONESHOT); + /* Label the process by modifying the cmdline */ + extern void append2Argv(char *appendstr); + extern unsigned int labelProcs; + if (labelProcs) { + append2Argv(NULL); + append2Argv("-proc:"); + append2Argv(info->providerName);; + append2Argv(" -class:"); + append2Argv(info->className); + append2Argv(" -location:"); + append2Argv(info->location); + } + // If requested, change the uid of the provider if (info->uid != -1) { _SFCB_TRACE(1, diff --git a/sfcBroker.c b/sfcBroker.c index c39eaa3..2e3e25c 100644 --- a/sfcBroker.c +++ b/sfcBroker.c @@ -104,6 +104,12 @@ extern char *configfile; extern char *ip4List; extern char *ip6List; +extern char **origArgv; +extern int origArgc; +extern unsigned int labelProcs; +extern void append2Argv(char *appendstr); +extern void restoreOrigArgv(int removePad); + #ifdef HAVE_IPV6 int fallback_ipv4 = 1; #endif @@ -338,6 +344,8 @@ handleSigHup(int sig) if (sfcBrokerPid == currentProc) { restartBroker = 1; + if (labelProcs) + restoreOrigArgv(1); fprintf(stderr, "--- Restarting %s\n", processName); pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); @@ -397,7 +405,17 @@ handleSigChld(int __attribute__ ((unused)) sig) static int reStartHttpd(void) { - return startHttpd(restartArgc, restartArgv, sslMode); + /* Restore orig argv or startup banner will be hosed on httpd restart */ + if (labelProcs) + restoreOrigArgv(0); + + int rc = startHttpd(restartArgc, restartArgv, sslMode); + + /* Relabel the process */ + if (labelProcs) + append2Argv(NULL); + + return rc; } static void @@ -637,8 +655,6 @@ main(int argc, char *argv[]) char *pauseStr; int daemonize=0; - sfcbUseSyslog=1; - name = strrchr(argv[0], '/'); if (name != NULL) ++name; @@ -651,8 +667,8 @@ main(int argc, char *argv[]) provPauseStr = getenv("SFCB_PAUSE_PROVIDER"); httpPauseStr = getenv("SFCB_PAUSE_CODEC"); currentProc = sfcBrokerPid = getpid(); - restartArgc = argc; - restartArgv = argv; + restartArgc = origArgc = argc; + restartArgv = origArgv = argv; exFlags = 0; @@ -748,7 +764,7 @@ main(int argc, char *argv[]) } } - if (optind < argc) { + if (optind < argc && *argv[optind] != 'X') { /* Ignore the pad argument */ fprintf(stderr, "SFCB not started: unrecognized config property %s\n", argv[optind]); usage(1); @@ -766,6 +782,36 @@ main(int argc, char *argv[]) currentProc=sfcBrokerPid=getpid(); /* req. on some systems */ } + setupControl(configfile); /* enable reading the config file */ + if ((getControlUNum("labelProcs", &labelProcs) == 0) && (labelProcs > 0)) { + if (*argv[argc-1] != 'X') { + /* Create an expanded argv */ + char **newArgv = malloc(sizeof(char*) * (argc + 2)); + memcpy(newArgv, argv, sizeof(char*) * argc); + + /* Create a pad argument of appropriate length */ + char *padArg = malloc(labelProcs + 1); + for (i=0; i < labelProcs; i++) + padArg[i] = 'X'; + padArg[i] = '\0'; + + /* Add pad argument and null terminator */ + newArgv[argc] = padArg; + newArgv[argc + 1] = NULL; + + /* Restart with expanded argv */ + fprintf(stderr, + "--- %s V" sfcHttpDaemonVersion " performing labelProcs allocation - %d\n", + name, currentProc); + execvp(newArgv[0], newArgv); + fprintf(stderr, "--- failed to execv for labelProcs allocation: %s\n", + strerror(errno)); + exit(1); + } + } + sunsetControl(); /* ensures setupControl() is re-run after startLogging() */ + + sfcbUseSyslog=1; startLogging(syslogLevel,1); mlogf(M_NOTICE, M_SHOW, "--- %s V" sfcHttpDaemonVersion " started - %d\n", @@ -1006,6 +1052,11 @@ mlogf(M_INFO, M_SHOW, "--- Request handlers enabled:%s\n",rtmsg); setSignal(SIGCHLD, handleSigChld, 0); setSignal(SIGUSR2, handleSigUsr2, 0); + /* Label the process by modifying the cmdline */ + if (labelProcs) { + append2Argv("-proc:Main"); + } + processProviderMgrRequests(); stopBroker(NULL); diff --git a/sfcb.cfg.pre.in b/sfcb.cfg.pre.in index d575a7e..41dbfb5 100644 --- a/sfcb.cfg.pre.in +++ b/sfcb.cfg.pre.in @@ -313,7 +313,7 @@ sslCiphers: ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH #slpRefreshInterval: 600 -##------------------------------------ Trace ---------------------------------- +##------------------------------- Trace and Debug ----------------------------- ## Location of the trace file. ## Can be overriden by setting environment variable SFCB_TRACE_FILE @@ -331,6 +331,15 @@ sslCiphers: ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH ## Default is 0. If trace mask is set (by any method) the default is 1. #traceLevel: 0 +## Label each process by adding identifying information to the process cmdline +## so that it appears in 'ps' output. The identifying info will be appended to +## the original argv data, following any passed arguments. The number of bytes +## appended is limited by the value of labelProcs. A value of about 100 should +## be enough in most cases. A smaller value may truncate the data, but this may +## be desired. A value of 0 means the feature is disabled. +## Default is 0 +#labelProcs: 100 + ##---------------------------- Indications ---------------------------- ## Indication provider calls to CBDeliverIndication() cause a thread to spawn diff --git a/sfcbproc.c b/sfcbproc.c index 6f25659..a332462 100644 --- a/sfcbproc.c +++ b/sfcbproc.c @@ -23,7 +23,7 @@ #define ERROR -1 #define BUFFER_SZ 32 -#define MAX_CMD_SZ 64 +#define MAX_CMD_SZ 256 #define MAX_PATH_SZ 64 #define MAX_PATTERN_SZ 32 #define MAX_PORTS 6 hooks/post-receive -- sfcb - Small Footprint CIM Broker |