From: Alex Rousskov Date: Tue, 16 Mar 2010 04:28:30 +0000 (-0600) Subject: Added ${process_name} and ${process_number} SMP macros. X-Git-Tag: SQUID_3_2_0_1~93^2~37 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d4a3e179f98fb048b514441ecee30eb530ba42ff;p=thirdparty%2Fsquid.git Added ${process_name} and ${process_number} SMP macros. Substitutions in the main process are not documented and may need more work. --- diff --git a/src/cache_cf.cc b/src/cache_cf.cc index 9004b8a69a..692f6f40cf 100644 --- a/src/cache_cf.cc +++ b/src/cache_cf.cc @@ -258,6 +258,42 @@ parseManyConfigFiles(char* files, int depth) return error_count; } +static void +ReplaceSubstr(char*& str, int& len, unsigned substrIdx, unsigned substrLen, const char* newSubstr) +{ + assert(str != NULL); + assert(newSubstr != NULL); + + unsigned newSubstrLen = strlen(newSubstr); + if (newSubstrLen > substrLen) + str = (char*)realloc(str, len - substrLen + newSubstrLen + 1); + + // move tail part including zero + memmove(str + substrIdx + newSubstrLen, str + substrIdx + substrLen, len - substrIdx - substrLen + 1); + // copy new substring in place + memcpy(str + substrIdx, newSubstr, newSubstrLen); + + len = strlen(str); +} + +static void +SubstituteMacro(char*& line, int& len, const char* macroName, const char* substStr) +{ + assert(line != NULL); + assert(macroName != NULL); + assert(substStr != NULL); + unsigned macroNameLen = strlen(macroName); + while (const char* macroPos = strstr(line, macroName)) // we would replace all occurrences + ReplaceSubstr(line, len, macroPos - line, macroNameLen, substStr); +} + +static void +ProcessMacros(char*& line, int& len) +{ + SubstituteMacro(line, len, "${process_name}", KidName.termedBuf()); + SubstituteMacro(line, len, "${process_number}", xitoa(KidIdentifier)); +} + static int parseOneConfigFile(const char *file_name, unsigned int depth) { @@ -357,6 +393,7 @@ parseOneConfigFile(const char *file_name, unsigned int depth) continue; } + ProcessMacros(tmp_line, tmp_line_len); debugs(3, 5, "Processing: '" << tmp_line << "'"); /* Handle includes here */ diff --git a/src/cf.data.pre b/src/cf.data.pre index 651193ce70..eb50206d5a 100644 --- a/src/cf.data.pre +++ b/src/cf.data.pre @@ -57,6 +57,18 @@ COMMENT_START This arbitrary restriction is to prevent recursive include references from causing Squid entering an infinite loop whilst trying to load configuration files. + + + SMP-Related Macros + + The following SMP-related preprocessor macros can be used. + + ${process_name} expands to the current Squid process "name" + (e.g., squid1, squid2, or cache1). + + ${process_number} expands to the current Squid process + identifier, which is an integer number (e.g., 1, 2, 3) unique + across all Squid processes. COMMENT_END COMMENT_START diff --git a/src/globals.h b/src/globals.h index 38a38de7f3..d31242ab28 100644 --- a/src/globals.h +++ b/src/globals.h @@ -173,6 +173,8 @@ extern "C" { extern const char *external_acl_message; /* NULL */ extern int opt_send_signal; /* -1 */ extern int opt_no_daemon; /* 0 */ + extern String KidName; /* APP_SHORTNAME */ + extern int KidIdentifier; /* 0 */ #ifdef __cplusplus diff --git a/src/main.cc b/src/main.cc index 8d5830ec05..2d5335b929 100644 --- a/src/main.cc +++ b/src/main.cc @@ -112,7 +112,6 @@ void WINAPI WIN32_svcHandler(DWORD); /** for error reporting from xmalloc and friends */ SQUIDCEXTERN void (*failure_notify) (const char *); -static int KidIdentifier = 0; static int opt_parse_cfg_only = 0; static char *opt_syslog_facility = NULL; static int icpPortNumOverride = 1; /* Want to detect "-u 0" */ @@ -1150,10 +1149,25 @@ SquidMainSafe(int argc, char **argv) return -1; // not reached } +/// computes name and ID for the current kid process +static void +ConfigureCurrentKid(const char *processName) +{ + // kids are marked with parenthesis around their process names + if (processName && processName[0] == '(') { + if (const char *idStart = strrchr(processName, '-')) { + KidIdentifier = atoi(idStart + 1); + const int nameLen = idStart - (processName + 1); + KidName.limitInit(processName + 1, nameLen); + } + } + // else use defaults, but it should not happen except for the main process +} + int SquidMain(int argc, char **argv) { - sscanf(argv[0], "(squid%d)", &KidIdentifier); + ConfigureCurrentKid(argv[0]); #ifdef _SQUID_WIN32_ @@ -1690,7 +1704,7 @@ public: for (size_t i = 1; i <= n; ++i) { char kid_name[32]; - snprintf(kid_name, sizeof(kid_name), "(squid%d)", (int)i); + snprintf(kid_name, sizeof(kid_name), "(squid-%d)", (int)i); storage.push_back(Kid(kid_name)); } }