]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Added ${process_name} and ${process_number} SMP macros.
authorAlex Rousskov <rousskov@measurement-factory.com>
Tue, 16 Mar 2010 04:28:30 +0000 (22:28 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Tue, 16 Mar 2010 04:28:30 +0000 (22:28 -0600)
Substitutions in the main process are not documented and may need more work.

src/cache_cf.cc
src/cf.data.pre
src/globals.h
src/main.cc

index 9004b8a69a5173cc0ae44557fc976172f2361940..692f6f40cf25b0aec2c92bce8e5dd48dfd106bf0 100644 (file)
@@ -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 */
index 651193ce70765d0dc55f8618b8c2fb71e12cf730..eb50206d5a3c235f68929578c14a64a2f5c02e3f 100644 (file)
@@ -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
index 38a38de7f3207a54b0b9749855c6a61211bb7617..d31242ab2833a85d11e0bf553f10b616eb72a72a 100644 (file)
@@ -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
index 8d5830ec0574a80b7486cdfecde3c51349eec709..2d5335b929c807703de47f2f0625a723b0852f9c 100644 (file)
@@ -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));
         }
     }