Substitutions in the main process are not documented and may need more work.
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)
{
continue;
}
+ ProcessMacros(tmp_line, tmp_line_len);
debugs(3, 5, "Processing: '" << tmp_line << "'");
/* Handle includes here */
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
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
/** 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" */
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_
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));
}
}