Skip to content

Commit 06b40ae

Browse files
ankor2023squid-anubis
authored andcommitted
Reduce UDS/segment name clashes across same-service instances (#2023)
Add a PID file name hash to the names of the shared memory segments and Unix Domain Sockets. Since all instances running on the same host are supposed to have unique PID files, this addition significantly reduces the probability of name clashes when running multiple Squid instances with the same service name (i.e. the same `squid -n` parameter value that defaults to "squid"). A clash may still happen if two different PID file names have the same hash or if multiple instances disable PID file management with `pid_filename none`. Clashes may also happen in environments where Squid does not even use service name for naming shared memory segments. Examples of UDS and shared memory segment names (while using default service name): /var/run/squid/squid-SLWQ-kid-1.ipc /var/run/squid/squid-SLWQ-coordinator.ipc /dev/shm/squid-SLWQ-tls_session_cache.shm /dev/shm/squid-SLWQ-transients_map_slices.shm This change is a reference point for automated CONTRIBUTORS updates.
1 parent daa76f4 commit 06b40ae

File tree

8 files changed

+87
-9
lines changed

8 files changed

+87
-9
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Thank you!
5454
Andrew Tridgell
5555
5656
Andrey Shorin <[email protected]>
57-
ankor2023 <138755079+ankor2023@users.noreply.github.com>
57+
Ankor <ankor2023@gmail.com>
5858
Anonymous <[email protected]>
5959
Anonymous <[email protected]>
6060
Anonymous Pootle User

src/Instance.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "debug/Messages.h"
1212
#include "fs_io.h"
1313
#include "Instance.h"
14+
#include "md5.h"
1415
#include "parser/Tokenizer.h"
1516
#include "sbuf/Stream.h"
1617
#include "SquidConfig.h"
@@ -222,3 +223,43 @@ Instance::WriteOurPid()
222223
debugs(50, Important(23), "Created " << TheFile);
223224
}
224225

226+
/// A hash that is likely to be unique across instances running on the same host
227+
/// because such concurrent instances should use unique PID filenames.
228+
/// All instances with disabled PID file maintenance have the same hash value.
229+
/// \returns a 4-character string suitable for use in file names and similar contexts
230+
static SBuf
231+
PidFilenameHash()
232+
{
233+
uint8_t hash[SQUID_MD5_DIGEST_LENGTH];
234+
235+
SquidMD5_CTX ctx;
236+
SquidMD5Init(&ctx);
237+
const auto name = PidFilenameCalc();
238+
SquidMD5Update(&ctx, name.rawContent(), name.length());
239+
SquidMD5Final(hash, &ctx);
240+
241+
// converts raw hash byte at a given position to a filename-suitable character
242+
const auto hashAt = [&hash](const size_t idx) {
243+
const auto safeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
244+
return safeChars[hash[idx] % strlen(safeChars)];
245+
};
246+
247+
SBuf buf;
248+
buf.appendf("%c%c%c%c", hashAt(0), hashAt(1), hashAt(2), hashAt(3));
249+
return buf;
250+
}
251+
252+
SBuf
253+
Instance::NamePrefix(const char * const head, const char * const tail)
254+
{
255+
SBuf buf(head);
256+
buf.append(service_name);
257+
buf.append("-");
258+
buf.append(PidFilenameHash());
259+
if (tail) {
260+
// TODO: Remove leading "-" from callers and explicitly add it here.
261+
buf.append(tail);
262+
}
263+
return buf;
264+
}
265+

src/Instance.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef SQUID_SRC_INSTANCE_H
1010
#define SQUID_SRC_INSTANCE_H
1111

12+
#include "sbuf/forward.h"
13+
1214
#if HAVE_SYS_TYPES_H
1315
#include <sys/types.h>
1416
#endif
@@ -30,6 +32,14 @@ void WriteOurPid();
3032
/// Throws if PID file maintenance is disabled.
3133
pid_t Other();
3234

35+
/// A service_name-derived string that is likely to be unique across all Squid
36+
/// instances concurrently running on the same host (as long as they do not
37+
/// disable PID file maintenance).
38+
/// \param head is used at the beginning of the generated name
39+
/// \param tail is used at the end of the generated name (when not nil)
40+
/// \returns a head-...tail string suitable for making file and shm segment names
41+
SBuf NamePrefix(const char *head, const char *tail = nullptr);
42+
3343
} // namespace Instance
3444

3545
#endif /* SQUID_SRC_INSTANCE_H */

src/Makefile.am

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ tests_testRock_SOURCES = \
10321032
HttpHeaderTools.h \
10331033
HttpReply.cc \
10341034
tests/stub_HttpRequest.cc \
1035+
tests/stub_Instance.cc \
10351036
LogTags.cc \
10361037
MasterXaction.cc \
10371038
MasterXaction.h \
@@ -1202,6 +1203,7 @@ tests_testUfs_SOURCES = \
12021203
HttpHeaderTools.h \
12031204
HttpReply.cc \
12041205
tests/stub_HttpRequest.cc \
1206+
tests/stub_Instance.cc \
12051207
LogTags.cc \
12061208
MasterXaction.cc \
12071209
MasterXaction.h \
@@ -1374,6 +1376,7 @@ tests_testStore_SOURCES = \
13741376
HttpHeaderTools.h \
13751377
tests/stub_HttpReply.cc \
13761378
tests/stub_HttpRequest.cc \
1379+
tests/stub_Instance.cc \
13771380
MasterXaction.cc \
13781381
MasterXaction.h \
13791382
MemBuf.cc \
@@ -1537,6 +1540,7 @@ tests_testDiskIO_SOURCES = \
15371540
HttpHeaderTools.h \
15381541
HttpReply.cc \
15391542
tests/stub_HttpRequest.cc \
1543+
tests/stub_Instance.cc \
15401544
LogTags.cc \
15411545
MasterXaction.cc \
15421546
MasterXaction.h \
@@ -1800,6 +1804,7 @@ tests_testHttpRange_SOURCES = \
18001804
HttpReply.cc \
18011805
HttpRequest.cc \
18021806
tests/stub_HttpUpgradeProtocolAccess.cc \
1807+
tests/stub_Instance.cc \
18031808
IoStats.h \
18041809
tests/stub_IpcIoFile.cc \
18051810
LogTags.cc \
@@ -2060,6 +2065,7 @@ tests_testHttpReply_SOURCES = \
20602065
tests/testHttpReply.cc \
20612066
HttpReply.h \
20622067
tests/stub_HttpRequest.cc \
2068+
tests/stub_Instance.cc \
20632069
MasterXaction.cc \
20642070
MasterXaction.h \
20652071
MemBuf.cc \
@@ -2187,6 +2193,7 @@ tests_testHttpRequest_SOURCES = \
21872193
tests/testHttpRequest.cc \
21882194
tests/testHttpRequestMethod.cc \
21892195
tests/stub_HttpUpgradeProtocolAccess.cc \
2196+
tests/stub_Instance.cc \
21902197
IoStats.h \
21912198
tests/stub_IpcIoFile.cc \
21922199
LogTags.cc \
@@ -2484,6 +2491,7 @@ tests_testCacheManager_SOURCES = \
24842491
HttpReply.cc \
24852492
HttpRequest.cc \
24862493
tests/stub_HttpUpgradeProtocolAccess.cc \
2494+
tests/stub_Instance.cc \
24872495
IoStats.h \
24882496
tests/stub_IpcIoFile.cc \
24892497
LogTags.cc \

src/ipc/Port.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "comm/Connection.h"
1414
#include "comm/Read.h"
1515
#include "CommCalls.h"
16+
#include "Instance.h"
1617
#include "ipc/Port.h"
1718
#include "sbuf/Stream.h"
1819
#include "tools.h"
@@ -52,9 +53,8 @@ bool Ipc::Port::doneAll() const
5253
String Ipc::Port::MakeAddr(const char* processLabel, int id)
5354
{
5455
assert(id >= 0);
55-
String addr = channelPathPfx;
56-
addr.append(service_name);
57-
addr.append(processLabel);
56+
String addr;
57+
addr.append(Instance::NamePrefix(channelPathPfx, processLabel));
5858
addr.append('-');
5959
addr.append(xitoa(id));
6060
addr.append(".ipc");
@@ -66,9 +66,7 @@ Ipc::Port::CoordinatorAddr()
6666
{
6767
static String coordinatorAddr;
6868
if (!coordinatorAddr.size()) {
69-
coordinatorAddr= channelPathPfx;
70-
coordinatorAddr.append(service_name);
71-
coordinatorAddr.append(coordinatorAddrLabel);
69+
coordinatorAddr.append(Instance::NamePrefix(channelPathPfx, coordinatorAddrLabel));
7270
coordinatorAddr.append(".ipc");
7371
}
7472
return coordinatorAddr;

src/ipc/mem/Segment.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "compat/shm.h"
1414
#include "debug/Stream.h"
1515
#include "fatal.h"
16+
#include "Instance.h"
1617
#include "ipc/mem/Segment.h"
1718
#include "sbuf/SBuf.h"
1819
#include "SquidConfig.h"
@@ -277,8 +278,7 @@ Ipc::Mem::Segment::GenerateName(const char *id)
277278
if (name[name.size()-1] != '/')
278279
name.append('/');
279280
} else {
280-
name.append('/');
281-
name.append(service_name);
281+
name.append(Instance::NamePrefix("/"));
282282
name.append('-');
283283
}
284284

src/tests/Stub.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ STUB_SOURCE = \
2222
tests/stub_HttpReply.cc \
2323
tests/stub_HttpRequest.cc \
2424
tests/stub_HttpUpgradeProtocolAccess.cc \
25+
tests/stub_Instance.cc \
2526
tests/stub_IpcIoFile.cc \
2627
tests/stub_MemBuf.cc \
2728
tests/stub_MemObject.cc \

src/tests/stub_Instance.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3+
*
4+
* Squid software is distributed under GPLv2+ license and includes
5+
* contributions from numerous individuals and organizations.
6+
* Please see the COPYING and CONTRIBUTORS files for details.
7+
*/
8+
9+
#include "squid.h"
10+
#include "Instance.h"
11+
#include "sbuf/SBuf.h"
12+
13+
#define STUB_API "Instance.cc"
14+
#include "tests/STUB.h"
15+
16+
void Instance::ThrowIfAlreadyRunning() STUB
17+
void Instance::WriteOurPid() STUB
18+
pid_t Instance::Other() STUB_RETVAL({})
19+
SBuf Instance::NamePrefix(const char *, const char *) STUB_RETVAL_NOP(SBuf("squid-0"))
20+

0 commit comments

Comments
 (0)