<commit_before>/*
 * Copyright (c) 2015 - 2022, Intel Corporation
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "config.h"

#include "SharedMemoryImp.hpp"

#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <glob.h>

#include <iostream>
#include <sstream>
#include <utility>

#include "geopm_time.h"
#include "geopm/Exception.hpp"
#include "geopm/Helper.hpp"

namespace geopm
{
    void SharedMemory::cleanup_shmem(void)
    {
        std::string pattern = "/geopm-shm-" + std::to_string(geteuid()) + "-*";
        pattern = SharedMemoryImp::construct_shm_path(pattern);
        glob_t globbuf;
        int err = glob(pattern.c_str(), 0, nullptr, &globbuf);
        if (err == 0) {
            for (size_t path_idx = 0; path_idx != globbuf.gl_pathc; ++path_idx) {
                int unlink_err = ::unlink(globbuf.gl_pathv[path_idx]);
                if (unlink_err != 0) {
#ifdef GEOPM_DEBUG
                    std::cerr << "Warning: <geopm> SharedMemory:cleanup_shmem(): unable to unlink file: " << globbuf.gl_pathv[path_idx] << "\n";
#endif
                }
            }
            globfree(&globbuf);
        }
    }

    /// @brief Size of the lock in memory.
    static constexpr size_t M_LOCK_SIZE = geopm::hardware_destructive_interference_size;
    static_assert(sizeof(pthread_mutex_t) <= M_LOCK_SIZE, "M_LOCK_SIZE not large enough for mutex type");

    static void setup_mutex(pthread_mutex_t *lock)
    {
        pthread_mutexattr_t lock_attr;
        int err = pthread_mutexattr_init(&lock_attr);
        if (err) {
            throw Exception("SharedMemory::setup_mutex(): pthread mutex initialization", GEOPM_ERROR_RUNTIME, __FILE__, __LINE__);
        }
        err = pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_ERRORCHECK);
        if (err) {
            (void) pthread_mutexattr_destroy(&lock_attr);
            throw Exception("SharedMemory::setup_mutex(): pthread mutex initialization", GEOPM_ERROR_RUNTIME, __FILE__, __LINE__);<commit_msg>Avoid memory leak if glob fails<commit_after>/*
 * Copyright (c) 2015 - 2022, Intel Corporation
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "config.h"

#include "SharedMemoryImp.hpp"

#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <glob.h>

#include <iostream>
#include <sstream>
#include <utility>

#include "geopm_time.h"
#include "geopm/Exception.hpp"
#include "geopm/Helper.hpp"

namespace geopm
{
    void SharedMemory::cleanup_shmem(void)
    {
        std::string pattern = "/geopm-shm-" + std::to_string(geteuid()) + "-*";
        pattern = SharedMemoryImp::construct_shm_path(pattern);
        glob_t globbuf;
        int err = glob(pattern.c_str(), 0, nullptr, &globbuf);
        if (err == 0) {
            for (size_t path_idx = 0; path_idx != globbuf.gl_pathc; ++path_idx) {
                int unlink_err = ::unlink(globbuf.gl_pathv[path_idx]);
                if (unlink_err != 0) {
#ifdef GEOPM_DEBUG
                    std::cerr << "Warning: <geopm> SharedMemory:cleanup_shmem(): unable to unlink file: " << globbuf.gl_pathv[path_idx] << "\n";
#endif
                }
            }
        }
        globfree(&globbuf);
    }

    /// @brief Size of the lock in memory.
    static constexpr size_t M_LOCK_SIZE = geopm::hardware_destructive_interference_size;
    static_assert(sizeof(pthread_mutex_t) <= M_LOCK_SIZE, "M_LOCK_SIZE not large enough for mutex type");

    static void setup_mutex(pthread_mutex_t *lock)
    {
        pthread_mutexattr_t lock_attr;
        int err = pthread_mutexattr_init(&lock_attr);
        if (err) {
            throw Exception("SharedMemory::setup_mutex(): pthread mutex initialization", GEOPM_ERROR_RUNTIME, __FILE__, __LINE__);
        }
        err = pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_ERRORCHECK);
        if (err) {
            (void) pthread_mutexattr_destroy(&lock_attr);
            throw Exception("SharedMemory::setup_mutex(): pthread mutex initialization", GEOPM_ERROR_RUNTIME, __FILE__, __LINE__);
{'fraction_non_alphanumeric': 0.12804586717630195, 'fraction_numerical': 0.006211180124223602, 'mean_word_length': 3.2038152610441766, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5368345', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1412, 'n_words': 292}