Andrew Tridgell
Andrey <rybakovandrey85@gmail.com>
Andrey Shorin <tolsty@tushino.com>
- ankor2023 <138755079+ankor2023@users.noreply.github.com>
+ Ankor <ankor2023@gmail.com>
Anonymous <bigparrot@pirateperfection.com>
Anonymous <redskilldough@gmail.com>
Anonymous Pootle User
#include "debug/Messages.h"
#include "fs_io.h"
#include "Instance.h"
+#include "md5.h"
#include "parser/Tokenizer.h"
#include "sbuf/Stream.h"
#include "SquidConfig.h"
debugs(50, Important(23), "Created " << TheFile);
}
+/// A hash that is likely to be unique across instances running on the same host
+/// because such concurrent instances should use unique PID filenames.
+/// All instances with disabled PID file maintenance have the same hash value.
+/// \returns a 4-character string suitable for use in file names and similar contexts
+static SBuf
+PidFilenameHash()
+{
+ uint8_t hash[SQUID_MD5_DIGEST_LENGTH];
+
+ SquidMD5_CTX ctx;
+ SquidMD5Init(&ctx);
+ const auto name = PidFilenameCalc();
+ SquidMD5Update(&ctx, name.rawContent(), name.length());
+ SquidMD5Final(hash, &ctx);
+
+ // converts raw hash byte at a given position to a filename-suitable character
+ const auto hashAt = [&hash](const size_t idx) {
+ const auto safeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ return safeChars[hash[idx] % strlen(safeChars)];
+ };
+
+ SBuf buf;
+ buf.appendf("%c%c%c%c", hashAt(0), hashAt(1), hashAt(2), hashAt(3));
+ return buf;
+}
+
+SBuf
+Instance::NamePrefix(const char * const head, const char * const tail)
+{
+ SBuf buf(head);
+ buf.append(service_name);
+ buf.append("-");
+ buf.append(PidFilenameHash());
+ if (tail) {
+ // TODO: Remove leading "-" from callers and explicitly add it here.
+ buf.append(tail);
+ }
+ return buf;
+}
+
#ifndef SQUID_SRC_INSTANCE_H
#define SQUID_SRC_INSTANCE_H
+#include "sbuf/forward.h"
+
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
/// Throws if PID file maintenance is disabled.
pid_t Other();
+/// A service_name-derived string that is likely to be unique across all Squid
+/// instances concurrently running on the same host (as long as they do not
+/// disable PID file maintenance).
+/// \param head is used at the beginning of the generated name
+/// \param tail is used at the end of the generated name (when not nil)
+/// \returns a head-...tail string suitable for making file and shm segment names
+SBuf NamePrefix(const char *head, const char *tail = nullptr);
+
} // namespace Instance
#endif /* SQUID_SRC_INSTANCE_H */
HttpHeaderTools.h \
HttpReply.cc \
tests/stub_HttpRequest.cc \
+ tests/stub_Instance.cc \
LogTags.cc \
MasterXaction.cc \
MasterXaction.h \
HttpHeaderTools.h \
HttpReply.cc \
tests/stub_HttpRequest.cc \
+ tests/stub_Instance.cc \
LogTags.cc \
MasterXaction.cc \
MasterXaction.h \
HttpHeaderTools.h \
tests/stub_HttpReply.cc \
tests/stub_HttpRequest.cc \
+ tests/stub_Instance.cc \
MasterXaction.cc \
MasterXaction.h \
MemBuf.cc \
HttpHeaderTools.h \
HttpReply.cc \
tests/stub_HttpRequest.cc \
+ tests/stub_Instance.cc \
LogTags.cc \
MasterXaction.cc \
MasterXaction.h \
HttpReply.cc \
HttpRequest.cc \
tests/stub_HttpUpgradeProtocolAccess.cc \
+ tests/stub_Instance.cc \
IoStats.h \
tests/stub_IpcIoFile.cc \
LogTags.cc \
tests/testHttpReply.cc \
HttpReply.h \
tests/stub_HttpRequest.cc \
+ tests/stub_Instance.cc \
MasterXaction.cc \
MasterXaction.h \
MemBuf.cc \
tests/testHttpRequest.cc \
tests/testHttpRequestMethod.cc \
tests/stub_HttpUpgradeProtocolAccess.cc \
+ tests/stub_Instance.cc \
IoStats.h \
tests/stub_IpcIoFile.cc \
LogTags.cc \
HttpReply.cc \
HttpRequest.cc \
tests/stub_HttpUpgradeProtocolAccess.cc \
+ tests/stub_Instance.cc \
IoStats.h \
tests/stub_IpcIoFile.cc \
LogTags.cc \
#include "comm/Connection.h"
#include "comm/Read.h"
#include "CommCalls.h"
+#include "Instance.h"
#include "ipc/Port.h"
#include "sbuf/Stream.h"
#include "tools.h"
String Ipc::Port::MakeAddr(const char* processLabel, int id)
{
assert(id >= 0);
- String addr = channelPathPfx;
- addr.append(service_name);
- addr.append(processLabel);
+ String addr;
+ addr.append(Instance::NamePrefix(channelPathPfx, processLabel));
addr.append('-');
addr.append(xitoa(id));
addr.append(".ipc");
{
static String coordinatorAddr;
if (!coordinatorAddr.size()) {
- coordinatorAddr= channelPathPfx;
- coordinatorAddr.append(service_name);
- coordinatorAddr.append(coordinatorAddrLabel);
+ coordinatorAddr.append(Instance::NamePrefix(channelPathPfx, coordinatorAddrLabel));
coordinatorAddr.append(".ipc");
}
return coordinatorAddr;
#include "compat/shm.h"
#include "debug/Stream.h"
#include "fatal.h"
+#include "Instance.h"
#include "ipc/mem/Segment.h"
#include "sbuf/SBuf.h"
#include "SquidConfig.h"
if (name[name.size()-1] != '/')
name.append('/');
} else {
- name.append('/');
- name.append(service_name);
+ name.append(Instance::NamePrefix("/"));
name.append('-');
}
tests/stub_HttpReply.cc \
tests/stub_HttpRequest.cc \
tests/stub_HttpUpgradeProtocolAccess.cc \
+ tests/stub_Instance.cc \
tests/stub_IpcIoFile.cc \
tests/stub_MemBuf.cc \
tests/stub_MemObject.cc \
--- /dev/null
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
+#include "Instance.h"
+#include "sbuf/SBuf.h"
+
+#define STUB_API "Instance.cc"
+#include "tests/STUB.h"
+
+void Instance::ThrowIfAlreadyRunning() STUB
+void Instance::WriteOurPid() STUB
+pid_t Instance::Other() STUB_RETVAL({})
+SBuf Instance::NamePrefix(const char *, const char *) STUB_RETVAL_NOP(SBuf("squid-0"))
+