masaori335 commented on code in PR #13328:
URL: https://github.com/apache/trafficserver/pull/13328#discussion_r3471958789


##########
src/iocore/cache/CacheShmPurge.h:
##########
@@ -0,0 +1,241 @@
+/** @file
+
+  Shared "enumerate and unlink the shm segments for a prefix" primitive, used 
by
+  both the cache subsystem (purge-on-disabled-start) and `traffic_ctl cache shm
+  clear`. Header-only since traffic_ctl does not link the cache library.
+  purge_segments() does no logging; it returns a report each caller formats 
itself.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#pragma once
+
+#include "CacheShmLayout.h"
+
+#include <fcntl.h>
+#include <sys/file.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <csignal>
+#include <algorithm>
+#include <cerrno>
+#include <cstdint>
+#include <cstring>
+#include <string>
+#include <vector>
+
+namespace cache_shm
+{
+
+/// True if `pid` names a live process (pid <= 0 is not). EPERM counts as 
alive (it
+/// exists, we just may not signal it). Backs the owner-liveness backstop used 
where
+/// the control-segment flock is not honored.
+inline bool
+process_is_alive(int32_t pid)
+{
+  if (pid <= 0) {
+    return false;
+  }
+  return ::kill(static_cast<pid_t>(pid), 0) == 0 || errno == EPERM;
+}
+
+/// Outcome of trying to take the control segment's exclusive lock.
+enum class LockResult {
+  Acquired,    ///< We hold the exclusive lock; no other process does.
+  HeldByOther, ///< Another live process holds it (flock returned EWOULDBLOCK).
+  Unsupported, ///< flock is not honored for this fd (e.g. macOS POSIX shm).
+};
+
+/// Take the exclusive, non-blocking advisory lock on the control fd. 
Authoritative
+/// on Linux/tmpfs (auto-released on crash); macOS POSIX shm returns 
Unsupported, so
+/// the owner_pid liveness check is used there instead. On Unsupported the 
flock errno
+/// is reported via `unexpected_errno` (when non-null) so a caller with 
logging can
+/// surface an otherwise-silent failure (EBADF/EINVAL/ENOLCK vs the expected 
macOS case).
+inline LockResult
+try_lock_control(int fd, int *unexpected_errno = nullptr)
+{
+  if (::flock(fd, LOCK_EX | LOCK_NB) == 0) {
+    return LockResult::Acquired;
+  }
+  // EWOULDBLOCK is the only errno meaning "another process holds it"; 
anything else
+  // means flock is unusable here -> fall back to the owner_pid backstop.
+  if (errno == EWOULDBLOCK) {
+    return LockResult::HeldByOther;
+  }
+  if (unexpected_errno != nullptr) {
+    *unexpected_errno = errno;
+  }
+  return LockResult::Unsupported;
+}

Review Comment:
   Not an issue - calling `flock` with `LOCK_NB` never returns `EINTR`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to