Skip to content

Implement miniob on systems based on MUSL LIBC #453

@bLueriVerLHR

Description

@bLueriVerLHR

Enhancement

I was trying to implement miniob on alpine linux for further development. However, the MUSL LIBC do not support all the features that common GNU LIBC supports like address sanitizer. I did not find a solution and turn the option off sadly. But I did successfully compile the project on alpine linux after some fixes.

  1. Function get_process_name use basename() in its implementation while not including header file libgen.h. I try to fix this problem using following implementation. It needs filesystem instead, but more cpp way I thought.
string get_process_name(const char *prog_name)
{
  return std::filesystem::path(prog_name).filename().c_str();
}
  1. Systems not using GNU LIBC may not have macros like PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP and PTHREAD_ERRORCHECK_MUTEX_INITIALIZER. For example, MUSL LIBC has pthread api while not implementing the macros mentioned before. Here is the explanation. I do not know much about mutexattr right now, so I try to fix problem on source code using code list below. Bad as not detecting if using MUSL LIBC, but fixed the problem.
#if defined(LINUX)
  #if not defined(__USE_GNU__)
    static pthread_mutexattr_t mtxattr;
    static pthread_mutexattr_t *pmtxattr = nullptr;
    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    if (pmtxattr == nullptr) {
      pmtxattr = &mtxattr;
      pthread_mutexattr_init(pmtxattr);
      pthread_mutexattr_settype(pmtxattr, PTHREAD_MUTEX_ERRORCHECK);
      pthread_mutex_init(&mutex, pmtxattr);
    }
  #else
    static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
  #endif
#elif defined(__MACH__)
  static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
#endif
  1. This line use the wrong header file as it should be just errno.h.

  2. MUSL LIBC do not have libexecinfo, and need to be installed manually. After installation, the CMakeLists.txt in deps/common need to be added a link option to libexecinfo.

  3. MUSL LIBC has pthread api, but cmake may not detect pthread. It requires adding SET(CMAKE_THREAD_LIBS_INIT "-lpthread") to the top CMakeLists.txt.

  4. Missing header file stdint.h in ring_buffer.h.

After fixing the problems list above, I compiled the project and tested some functions successfully. The first two fixes may not follow your principle of code, so I did not give a PR. I gave an issue so that you can implement the fixes in your style.

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions