<commit_before>// 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.

#include <string.h>

#include <linux/capability.h>

#include <sys/prctl.h>

#include <set>
#include <string>

#include <stout/numify.hpp>
#include <stout/os.hpp>
#include <stout/stringify.hpp>

#include "linux/capabilities.hpp"

using std::hex;
using std::ostream;
using std::set;
using std::string;
using std::vector;

// We declare two functions provided in the libcap headers here to
// prevent introduction of that build-time dependency.
extern "C" {
extern int capset(cap_user_header_t header, cap_user_data_t data);
extern int capget(cap_user_header_t header, const cap_user_data_t data);
}


namespace mesos {
namespace internal {
namespace capabilities {

constexpr char PROC_CAP_LAST_CAP[] = "/proc/sys/kernel/cap_last_cap";
constexpr int CAPABIILITY_PROTOBUF_OFFSET = 1000;


// System call payload (for linux capability version 3).
struct SyscallPayload
{
  __user_cap_header_struct head;
  __user_cap_data_struct set[_LINUX_CAPABILITY_U32S_3];

  SyscallPayload()
  {
    memset(this, 0, sizeof(SyscallPayload));
  }

  uint64_t effective()
  {
    uint64_t low = static_cast<uint64_t>(set[0].effective);
    uint64_t high = static_cast<uint64_t>(set[1].effective) << 32;
    return low | high;
  }

  uint64_t permitted()
  {
    uint64_t low = static_cast<uint64_t>(set[0].permitted);
    uint64_t high = static_cast<uint64_t>(set[1].permitted) << 32;
    return low | high;
  }

  uint64_t inheritable()
  {
    uint64_t low = static_cast<uint64_t>(set[0].inheritable);
    uint64_t high = static_cast<uint64_t>(set[1].inheritable) << 32;
    return low | high;
  }

  void setEffective(uint64_t effective)
  {
    set[0].effective = static_cast<uint32_t>(effective);
    set[1].effective = static_cast<uint32_t>(effective >> 32);
  }

  void setPermitted(uint64_t permitted)
  {
    set[0].permitted = static_cast<uint32_t>(permitted);
    set[1].permitted = static_cast<uint32_t>(permitted >> 32);
  }

  void setInheritable(uint64_t inheritable)
  {
    set[0].inheritable = static_cast<uint32_t>(inheritable);
    set[1].inheritable = static_cast<uint32_t>(inheritable >> 32);
  }
};


// Helper funciton to convert capability set to bitset.
static uint64_t toCapabilityBitset(const Set<Capability>& capabilities)
{
  uint64_t result = 0;

  for (int i = 0; i < MAX_CAPABILITY; i++) {
    if (capabilities.count(static_cast<Capability>(i)) > 0) {
      result |= (1ULL << i);
    }
  }

  return result;
}


// Helper function to convert capability bitset to Set.
static Set<Capability> toCapabilitySet(uint64_t bitset)
{
  Set<Capability> result;

  for (int i = 0; i < MAX_CAPABILITY; i++) {
    if ((bitset & (1ULL << i)) != 0) {
      result.insert(Capability(i));
    }
  }

  return result;
}


Set<Capability> ProcessCapabilities::get(const Type& type) const
{
  switch (type) {
    case EFFECTIVE:   return effective;
    case INHERITABLE: return inheritable;
    case PERMITTED:   return permitted;
    case BOUNDING:    return bounding;
  }

  UNREACHABLE();
}


void ProcessCapabilities::set(
    const Type& type,
    const Set<Capability>& capabilities)
{
  switch (type) {
    case EFFECTIVE:   effective = capabilities;
    case PERMITTED:   permitted = capabilities;
    case INHERITABLE: inheritable = capabilities;
    case BOUNDING:    bounding = capabilities;
    default:          UNREACHABLE();
  }
}<commit_msg>Removed `default` case from `switch` over enum value.<commit_after>// 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.

#include <string.h>

#include <linux/capability.h>

#include <sys/prctl.h>

#include <set>
#include <string>

#include <stout/numify.hpp>
#include <stout/os.hpp>
#include <stout/stringify.hpp>

#include "linux/capabilities.hpp"

using std::hex;
using std::ostream;
using std::set;
using std::string;
using std::vector;

// We declare two functions provided in the libcap headers here to
// prevent introduction of that build-time dependency.
extern "C" {
extern int capset(cap_user_header_t header, cap_user_data_t data);
extern int capget(cap_user_header_t header, const cap_user_data_t data);
}


namespace mesos {
namespace internal {
namespace capabilities {

constexpr char PROC_CAP_LAST_CAP[] = "/proc/sys/kernel/cap_last_cap";
constexpr int CAPABIILITY_PROTOBUF_OFFSET = 1000;


// System call payload (for linux capability version 3).
struct SyscallPayload
{
  __user_cap_header_struct head;
  __user_cap_data_struct set[_LINUX_CAPABILITY_U32S_3];

  SyscallPayload()
  {
    memset(this, 0, sizeof(SyscallPayload));
  }

  uint64_t effective()
  {
    uint64_t low = static_cast<uint64_t>(set[0].effective);
    uint64_t high = static_cast<uint64_t>(set[1].effective) << 32;
    return low | high;
  }

  uint64_t permitted()
  {
    uint64_t low = static_cast<uint64_t>(set[0].permitted);
    uint64_t high = static_cast<uint64_t>(set[1].permitted) << 32;
    return low | high;
  }

  uint64_t inheritable()
  {
    uint64_t low = static_cast<uint64_t>(set[0].inheritable);
    uint64_t high = static_cast<uint64_t>(set[1].inheritable) << 32;
    return low | high;
  }

  void setEffective(uint64_t effective)
  {
    set[0].effective = static_cast<uint32_t>(effective);
    set[1].effective = static_cast<uint32_t>(effective >> 32);
  }

  void setPermitted(uint64_t permitted)
  {
    set[0].permitted = static_cast<uint32_t>(permitted);
    set[1].permitted = static_cast<uint32_t>(permitted >> 32);
  }

  void setInheritable(uint64_t inheritable)
  {
    set[0].inheritable = static_cast<uint32_t>(inheritable);
    set[1].inheritable = static_cast<uint32_t>(inheritable >> 32);
  }
};


// Helper funciton to convert capability set to bitset.
static uint64_t toCapabilityBitset(const Set<Capability>& capabilities)
{
  uint64_t result = 0;

  for (int i = 0; i < MAX_CAPABILITY; i++) {
    if (capabilities.count(static_cast<Capability>(i)) > 0) {
      result |= (1ULL << i);
    }
  }

  return result;
}


// Helper function to convert capability bitset to Set.
static Set<Capability> toCapabilitySet(uint64_t bitset)
{
  Set<Capability> result;

  for (int i = 0; i < MAX_CAPABILITY; i++) {
    if ((bitset & (1ULL << i)) != 0) {
      result.insert(Capability(i));
    }
  }

  return result;
}


Set<Capability> ProcessCapabilities::get(const Type& type) const
{
  switch (type) {
    case EFFECTIVE:   return effective;
    case INHERITABLE: return inheritable;
    case PERMITTED:   return permitted;
    case BOUNDING:    return bounding;
  }

  UNREACHABLE();
}


void ProcessCapabilities::set(
    const Type& type,
    const Set<Capability>& capabilities)
{
  switch (type) {
    case EFFECTIVE:   effective = capabilities;
    case PERMITTED:   permitted = capabilities;
    case INHERITABLE: inheritable = capabilities;
    case BOUNDING:    bounding = capabilities;
  }

  UNREACHABLE();
}
{'fraction_non_alphanumeric': 0.11080531291133182, 'fraction_numerical': 0.023453392365681464, 'mean_word_length': 4.010791366906475, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6244329', 'n_tokens_mistral': 2857, 'n_tokens_neox': 2692, 'n_words': 760}