Instance.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 "base/File.h"
11 #include "debug/Messages.h"
12 #include "fs_io.h"
13 #include "Instance.h"
14 #include "parser/Tokenizer.h"
15 #include "sbuf/Stream.h"
16 #include "SquidConfig.h"
17 #include "tools.h"
18 
19 #include <cerrno>
20 
21 /* To support concurrent PID files, convert local statics into PidFile class */
22 
25 static SBuf TheFile;
26 
29 static SBuf
31 {
32  if (!Config.pidFilename || strcmp(Config.pidFilename, "none") == 0)
33  return SBuf();
34 
35  // If chroot has been requested, then we first read the PID file before
36  // chroot() and then create/update it inside a chrooted environment.
37  // TODO: Consider removing half-baked chroot support from Squid.
38  extern bool Chrooted;
39  if (!Config.chroot_dir || Chrooted) // no need to compensate
40  return SBuf(Config.pidFilename);
41 
42  SBuf filename;
43  filename.append(Config.chroot_dir);
44  filename.append("/");
45  filename.append(Config.pidFilename);
46  debugs(50, 3, "outside chroot: " << filename);
47  return filename;
48 }
49 
51 static SBuf
52 PidFileDescription(const SBuf &filename)
53 {
54  return ToSBuf("PID file (", filename, ')');
55 }
56 
59 static SBuf
61 {
62  const auto name = PidFilenameCalc();
64  return name;
65 }
66 
68 static pid_t
69 GetOtherPid(File &pidFile)
70 {
71  const auto input = pidFile.readSmall(1, 32);
72  int64_t rawPid = -1;
73 
74  Parser::Tokenizer tok(input);
75  if (!(tok.int64(rawPid, 10, false) && // PID digits
76  (tok.skipOne(CharacterSet::CR)||true) && // optional CR (Windows/etc.)
77  tok.skipOne(CharacterSet::LF) && // required end of line
78  tok.atEnd())) { // no trailing garbage
79  throw TexcHere(ToSBuf("Malformed ", TheFile));
80  }
81 
82  debugs(50, 7, "found PID " << rawPid << " in " << TheFile);
83 
84  if (rawPid < 1)
85  throw TexcHere(ToSBuf("Bad ", TheFile, " contains unreasonably small PID value: ", rawPid));
86  const auto finalPid = static_cast<pid_t>(rawPid);
87  if (static_cast<int64_t>(finalPid) != rawPid)
88  throw TexcHere(ToSBuf("Bad ", TheFile, " contains unreasonably large PID value: ", rawPid));
89 
90  return finalPid;
91 }
92 
94 static bool
95 ProcessIsRunning(const pid_t pid)
96 {
97  const auto result = kill(pid, 0);
98  const auto savedErrno = errno;
99  if (result != 0)
100  debugs(50, 3, "kill(" << pid << ", 0) failed: " << xstrerr(savedErrno));
101  // if we do not have permissions to signal the process, then it is running
102  return (result == 0 || savedErrno == EPERM);
103 }
104 
106 static void
108 {
109  bool running = false;
110  SBuf description;
111  try {
112  const auto pid = GetOtherPid(pidFile);
113  description = ToSBuf(TheFile, " with PID ", pid);
114  running = ProcessIsRunning(pid);
115  }
116  catch (const std::exception &ex) {
117  debugs(50, 5, "assuming no other Squid instance: " << ex.what());
118  return;
119  }
120 
121  if (running)
122  throw TexcHere(ToSBuf("Squid is already running: Found fresh instance ", description));
123 
124  debugs(50, 5, "assuming stale instance " << description);
125 }
126 
127 pid_t
129 {
130  const auto filename = PidFilename();
131  if (filename.isEmpty())
132  throw TexcHere("no pid_filename configured");
133 
134  File pidFile(filename, File::Be::ReadOnly().locked());
135  return GetOtherPid(pidFile);
136 }
137 
138 void
140 {
141  const auto filename = PidFilename();
142  if (filename.isEmpty())
143  return; // the check is impossible
144 
145  if (const auto filePtr = File::Optional(filename, File::Be::ReadOnly().locked())) {
146  const std::unique_ptr<File> pidFile(filePtr);
147  ThrowIfAlreadyRunningWith(*pidFile);
148  } else {
149  // It is best to assume then to check because checking without a lock
150  // might lead to false positives that lead to no Squid starting at all!
151  debugs(50, 5, "cannot lock " << TheFile << "; assuming no other Squid is running");
152  // If our assumption is false, we will fail to _create_ the PID file,
153  // and, hence, will not start, allowing that other Squid to run.
154  }
155 }
156 
159 
161 static void
163 {
164  if (ThePidFileToRemove.isEmpty()) // not the PidFilename()!
165  return; // nothing to do
166 
167  debugs(50, Important(22), "Removing " << PidFileDescription(ThePidFileToRemove));
168 
169  // Do not write to cache_log after our PID file is removed because another
170  // instance may already be logging there. Stop logging now because, if we
171  // wait until safeunlink(), some debugs() may slip through into the now
172  // "unlocked" cache_log, especially if we avoid the sensitive suid() area.
173  // Use stderr to capture late debugs() that did not make it into cache_log.
175 
176  const char *filename = ThePidFileToRemove.c_str(); // avoid complex operations inside enter_suid()
177  enter_suid();
178  safeunlink(filename, 0);
179  leave_suid();
180 
182 }
183 
185 void
187 {
188  // Instance code assumes that we do not support PID filename reconfiguration
189  static bool called = false;
190  Must(!called);
191  called = true;
192 
193  const auto filename = PidFilename();
194  if (filename.isEmpty())
195  return; // nothing to do
196 
197  File pidFile(filename, File::Be::ReadWrite().locked().createdIfMissing().openedByRoot());
198 
199  // another instance may have started after the caller checked (if it did)
200  ThrowIfAlreadyRunningWith(pidFile);
201 
202  /* now we know that we own the PID file created and/or locked above */
203 
204  // Cleanup is scheduled through atexit() to ensure both:
205  // - cleanup upon fatal() and similar "unplanned" exits and
206  // - enter_suid() existence and proper logging support during cleanup.
207  // Even without PID filename reconfiguration support, we have to remember
208  // the file name we have used because Config.pidFilename may change!
209  (void)std::atexit(&RemoveInstance); // failures leave the PID file on disk
210  ThePidFileToRemove = filename;
211 
212  /* write our PID to the locked file */
213  SBuf pidBuf;
214  pidBuf.Printf("%d\n", static_cast<int>(getpid()));
215  pidFile.truncate();
216  pidFile.writeAll(pidBuf);
217 
218  // We must fsync before releasing the lock or other Squid processes may not see
219  // our written PID (and decide that they are dealing with a corrupted PID file).
220  pidFile.synchronize();
221 
222  debugs(50, Important(23), "Created " << TheFile);
223 }
224 
const char * xstrerr(int error)
Definition: xstrerror.cc:83
static pid_t GetOtherPid(File &pidFile)
Definition: Instance.cc:69
static SBuf ThePidFileToRemove
ties Instance::WriteOurPid() scheduler and RemoveInstance(void) handler
Definition: Instance.cc:158
static FileOpeningConfig ReadOnly()
Definition: File.cc:32
bool isEmpty() const
Definition: SBuf.h:435
Definition: SBuf.h:93
static File * Optional(const SBuf &aName, const FileOpeningConfig &cfg)
Definition: File.cc:124
static const CharacterSet LF
Definition: CharacterSet.h:92
static SBuf PidFileDescription(const SBuf &filename)
Definition: Instance.cc:52
static bool ProcessIsRunning(const pid_t pid)
determines whether a given process is running at the time of the call
Definition: Instance.cc:95
static SBuf PidFilename()
Definition: Instance.cc:60
void clear()
Definition: SBuf.cc:175
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
Definition: TextException.h:63
a portable locking-aware exception-friendly file (with RAII API)
Definition: File.h:66
bool Chrooted
Definition: main.cc:1061
void synchronize()
fsync(2)
Definition: File.cc:304
static pid_t pid
Definition: IcmpSquid.cc:34
static const CharacterSet CR
Definition: CharacterSet.h:80
pid_t Other()
Definition: Instance.cc:128
void leave_suid(void)
Definition: tools.cc:559
SBuf & Printf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition: SBuf.cc:214
char * chroot_dir
Definition: SquidConfig.h:473
void WriteOurPid()
creates a PID file; throws on error
Definition: Instance.cc:186
SBuf readSmall(SBuf::size_type minBytes, SBuf::size_type maxBytes)
read(2) for small files
Definition: File.cc:241
static SBuf TheFile
Definition: Instance.cc:25
const char * c_str()
Definition: SBuf.cc:516
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
void writeAll(const SBuf &data)
write(2) with a "wrote everything" check
Definition: File.cc:280
void truncate()
makes the file size (and the current I/O offset) zero
Definition: File.cc:215
char * pidFilename
Definition: SquidConfig.h:224
static void RemoveInstance()
atexit() handler; removes the PID file created with Instance::WriteOurPid()
Definition: Instance.cc:162
Definition: parse.c:160
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:63
#define Must(condition)
Definition: TextException.h:75
#define Important(id)
Definition: Messages.h:93
void enter_suid(void)
Definition: tools.cc:623
void safeunlink(const char *s, int quiet)
Definition: fs_io.cc:433
static SBuf PidFilenameCalc()
Definition: Instance.cc:30
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
static void ThrowIfAlreadyRunningWith(File &pidFile)
quits if another Squid instance (that owns the given PID file) is running
Definition: Instance.cc:107
void ThrowIfAlreadyRunning()
Definition: Instance.cc:139
static FileOpeningConfig ReadWrite()
Definition: File.cc:57
class SquidConfig Config
Definition: SquidConfig.cc:12
static void StopCacheLogUse()
Definition: debug.cc:1132

 

Introduction

Documentation

Support

Miscellaneous