当前位置: 首页>>代码示例>>C++>>正文


C++ Try::shared方法代码示例

本文整理汇总了C++中Try::shared方法的典型用法代码示例。如果您正苦于以下问题:C++ Try::shared方法的具体用法?C++ Try::shared怎么用?C++ Try::shared使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Try的用法示例。


在下文中一共展示了Try::shared方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Error

// Make sure that the specified target directory is in a shared mount
// so that when forking a child process (with a new mount namespace),
// the child process does not hold extra references to the mounts
// underneath the target directory. For instance, container's
// persistent volume mounts and provisioner mounts (e.g., when using
// the bind/overlayfs backend) under agent's `work_dir`. This ensures
// that cleanup operations (i.e., unmount) on the host mount namespace
// can be propagated to child's mount namespaces. See MESOS-3483 for
// more details.
// TODO(jieyu): Consider moving this helper to 'src/linux/fs.hpp|cpp'.
static Try<Nothing> ensureSharedMount(const string& _targetDir)
{
  // Mount table entries use realpaths. Therefore, we first get the
  // realpath of the target directory.
  Result<string> targetDir = os::realpath(_targetDir);
  if (!targetDir.isSome()) {
    return Error(
        "Failed to get the realpath of '" + _targetDir + "': " +
        (targetDir.isError() ? targetDir.error() : "Not found"));
  }

  Try<fs::MountInfoTable> table = fs::MountInfoTable::read();
  if (table.isError()) {
    return Error("Failed to get mount table: " + table.error());
  }

  // Trying to find the mount entry that contains the target
  // directory. We achieve that by doing a reverse traverse of the
  // mount table to find the first entry whose target is a prefix of
  // the target directory.
  Try<fs::MountInfoTable::Entry> targetDirMount =
    table->findByTarget(_targetDir);

  if (targetDirMount.isError()) {
    return Error(
        "Failed to find the mount containing '" + _targetDir +
        "': " + targetDirMount.error());
  }

  // If 'targetDirMount' is a shared mount in its own peer group, then
  // we don't need to do anything. Otherwise, we need to do a self
  // bind mount of the target directory to make sure it's a shared
  // mount in its own peer group.
  bool bindMountNeeded = false;

  if (targetDirMount->shared().isNone()) {
    bindMountNeeded = true;
  } else {
    foreach (const fs::MountInfoTable::Entry& entry, table->entries) {
      // Skip 'targetDirMount' and any mount underneath it. Also, we
      // skip those mounts whose targets are not the parent of the
      // target directory because even if they are in the same peer
      // group as the working directory mount, it won't affect it.
      if (entry.id != targetDirMount->id &&
          !strings::startsWith(entry.target, path::join(targetDir.get(), "")) &&
          entry.shared() == targetDirMount->shared() &&
          strings::startsWith(targetDir.get(), path::join(entry.target, ""))) {
        bindMountNeeded = true;
        break;
      }
    }
  }

  if (bindMountNeeded) {
    if (targetDirMount->target != targetDir.get()) {
      // This is the case where the target directory mount does not
      // exist in the mount table (e.g., a new host running Mesos
      // slave for the first time).
      LOG(INFO) << "Bind mounting '" << targetDir.get()
                << "' and making it a shared mount";

      // NOTE: Instead of using fs::mount to perform the bind mount,
      // we use the shell command here because the syscall 'mount'
      // does not update the mount table (i.e., /etc/mtab). In other
      // words, the mount will not be visible if the operator types
      // command 'mount'. Since this mount will still be presented
      // after all containers and the slave are stopped, it's better
      // to make it visible. It's OK to use the blocking os::shell
      // here because 'create' will only be invoked during
      // initialization.
      Try<string> mount = os::shell(
          "mount --bind %s %s && "
          "mount --make-private %s && "
          "mount --make-shared %s",
          targetDir.get(),
          targetDir.get(),
          targetDir.get(),
          targetDir.get());

      if (mount.isError()) {
        return Error(
            "Failed to bind mount '" + targetDir.get() +
            "' and make it a shared mount: " + mount.error());
      }
    } else {
      // This is the case where the target directory mount is in the
      // mount table, but it's not a shared mount in its own peer
      // group (possibly due to slave crash while preparing the
      // target directory mount). It's safe to re-do the following.
      LOG(INFO) << "Making '" << targetDir.get() << "' a shared mount";
//.........这里部分代码省略.........
开发者ID:jfrazelle,项目名称:mesos,代码行数:101,代码来源:linux.cpp


注:本文中的Try::shared方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。