47 debugs(50, 3,
"outside chroot: " << filename);
55 return ToSBuf(
"PID file (", filename,
')');
72 const auto input = pidFile.
readSmall(1, 32);
76 if (!(
tok.int64(rawPid, 10,
false) &&
87 const auto finalPid =
static_cast<pid_t
>(rawPid);
88 if (
static_cast<int64_t
>(finalPid) != rawPid)
98 const auto result = kill(
pid, 0);
99 const auto savedErrno = errno;
103 return (result == 0 || savedErrno == EPERM);
110 bool running =
false;
117 catch (
const std::exception &ex) {
118 debugs(50, 5,
"assuming no other Squid instance: " << ex.what());
123 throw TexcHere(
ToSBuf(
"Squid is already running: Found fresh instance ", description));
125 debugs(50, 5,
"assuming stale instance " << description);
132 if (filename.isEmpty())
133 throw TexcHere(
"no pid_filename configured");
143 if (filename.isEmpty())
147 const std::unique_ptr<File> pidFile(filePtr);
152 debugs(50, 5,
"cannot lock " <<
TheFile <<
"; assuming no other Squid is running");
190 static bool called =
false;
195 if (filename.isEmpty())
215 pidBuf.
Printf(
"%d\n",
static_cast<int>(getpid()));
242 const auto hashAt = [&
hash](
const size_t idx) {
243 const auto safeChars =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
244 return safeChars[
hash[idx] % strlen(safeChars)];
248 buf.
appendf(
"%c%c%c%c", hashAt(0), hashAt(1), hashAt(2), hashAt(3));
static SBuf PidFilenameHash()
static bool ProcessIsRunning(const pid_t pid)
determines whether a given process is running at the time of the call
static SBuf PidFilenameCalc()
static pid_t GetOtherPid(File &pidFile)
static void ThrowIfAlreadyRunningWith(File &pidFile)
quits if another Squid instance (that owns the given PID file) is running
static void RemoveInstance()
atexit() handler; removes the PID file created with Instance::WriteOurPid()
static SBuf PidFilename()
static SBuf ThePidFileToRemove
ties Instance::WriteOurPid() scheduler and RemoveInstance(void) handler
static SBuf PidFileDescription(const SBuf &filename)
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
squidaio_request_t * head
static const CharacterSet LF
static const CharacterSet CR
static void StopCacheLogUse()
static FileOpeningConfig ReadWrite()
static FileOpeningConfig ReadOnly()
a portable locking-aware exception-friendly file (with RAII API)
void writeAll(const SBuf &data)
write(2) with a "wrote everything" check
static File * Optional(const SBuf &aName, const FileOpeningConfig &cfg)
void truncate()
makes the file size (and the current I/O offset) zero
void synchronize()
fsync(2)
SBuf readSmall(SBuf::size_type minBytes, SBuf::size_type maxBytes)
read(2) for small files
SBuf & appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
SBuf & Printf(const char *fmt,...) PRINTF_FORMAT_ARG2
SBuf & append(const SBuf &S)
#define debugs(SECTION, LEVEL, CONTENT)
void safeunlink(const char *s, int quiet)
SQUIDCEXTERN void SquidMD5Init(struct SquidMD5Context *context)
#define SQUID_MD5_DIGEST_LENGTH
SQUIDCEXTERN void SquidMD5Update(struct SquidMD5Context *context, const void *buf, unsigned len)
SQUIDCEXTERN void SquidMD5Final(uint8_t digest[16], struct SquidMD5Context *context)
void WriteOurPid()
creates a PID file; throws on error
void ThrowIfAlreadyRunning()
SBuf NamePrefix(const char *head, const char *tail=nullptr)
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
const char * xstrerr(int error)