[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

Michael Spencer via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 4 15:15:53 PDT 2023


================
@@ -0,0 +1,302 @@
+//===------- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon --------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/ThreadPool.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/Support/YAMLParser.h"
+#include "llvm/Support/YAMLTraits.h"
+
+// TODO: Make portable
+#if LLVM_ON_UNIX
+
+#include <errno.h>
+#include <fstream>
+#include <mutex>
+#include <optional>
+#include <signal.h>
+#include <sstream>
+#include <stdbool.h>
+#include <string>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <type_traits>
+#include <unistd.h>
+#include <unordered_map>
+
+using namespace llvm;
+using namespace clang;
+using namespace cc1modbuildd;
+
+// Create unbuffered STDOUT stream so that any logging done by module build
+// daemon can be viewed without having to terminate the process
+static raw_fd_ostream &unbuff_outs() {
+  static raw_fd_ostream S(STDOUT_FILENO, false, true);
+  return S;
+}
+
+namespace {
+
+struct ClientConnection {
+  int ClientFD;
+  std::string Buffer;
+};
+
+class ModuleBuildDaemonServer {
+public:
+  SmallString<256> SocketPath;
+  SmallString<256> STDERR;
+  SmallString<256> STDOUT;
+
+  ModuleBuildDaemonServer(StringRef Path, ArrayRef<const char *> Argv)
+      : SocketPath(Path), STDERR(Path), STDOUT(Path) {
+    llvm::sys::path::append(SocketPath, SOCKET_FILE_NAME);
+    llvm::sys::path::append(STDOUT, STDOUT_FILE_NAME);
+    llvm::sys::path::append(STDERR, STDERR_FILE_NAME);
+  }
+
+  ~ModuleBuildDaemonServer() { shutdownDaemon(); }
+
+  int forkDaemon();
+  int createDaemonSocket();
+  int listenForClients();
+
+  static void handleClient(ClientConnection Connection);
+
+  void shutdownDaemon() {
+    int SocketFD = ListenSocketFD.load();
+
+    unlink(SocketPath.c_str());
+    shutdown(SocketFD, SHUT_RD);
+    close(SocketFD);
+    exit(EXIT_SUCCESS);
----------------
Bigcheese wrote:

This should be `_Exit` as `exit` is not signal safe (it calls destructors and does some other work).

Longer term I think we'll want to make this just set a flag saying we want to shut down, and then stop accepting new clients and shut down when they are done.

https://github.com/llvm/llvm-project/pull/67562


More information about the cfe-commits mailing list