Bug #80322 mysql 5.7 fails to build with musl libc due to not finding SIGEV_THREAD_ID
Submitted: 9 Feb 2016 16:13 Modified: 27 Apr 2017 16:05
Reporter: Natanael Copa Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:5.7.10 OS:Linux (Alpine linux (musl libc))
Assigned to: CPU Architecture:Any

[9 Feb 2016 16:13] Natanael Copa
Description:
## What i did:

Tried to compile mysql-5.7.10 on Alpine Linux, which uses musl libc instead of GNU libc.

## What I expected to happen:

I expected the compile to complete and generate a working mysql server binary.

## What actually happened:

cmake failed with the following error:
...
-- Looking for SIGEV_THREAD_ID
-- Looking for SIGEV_THREAD_ID - not found
-- Looking for SIGEV_PORT
-- Looking for SIGEV_PORT - not found
...
CMake Error at configure.cmake:540 (MESSAGE):
  No mysys timer support detected!
Call Stack (most recent call first):
  CMakeLists.txt:449 (INCLUDE)

cmake failed because it did not find SIGEV_THREAD_ID. musl libc does not expose it.

Also, the mysys/posix_timers.c will try use the field `_sigev_un._tid` from `struct sigevent`.
https://github.com/mysql/mysql-serve/blob/a2757a60a7527407d08115e44e889a25f22c96c6/mysys/p...

In musl, this field is not exposed at all.

How to repeat:
Try compile from source with alpine. Example Dockerfile:

FROM alpine:3.3
ENV MYSQL_MAJOR 5.7
ENV MYSQL_VERSION 5.7.10
ENV GPG_KEY "A4A9406876FCBD3C456770C88C718D3B5072E1F5"
RUN apk add --no-cache --virtual .build-deps \
                boost-dev \
                cmake \
                curl \
                g++ \
                gcc \
                gnupg \
                libaio-dev \
                libc-dev \
                libedit-dev \
                linux-headers \
                make \
                perl \
                pwgen \
        && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" \
        && curl -fSL http://cdn.mysql.com/Downloads/MySQL-${MYSQL_MAJOR}/mysql-${MYSQL_VERSION}.tar.gz -o mysql.tar.gz \
        && curl -fSL http://cdn.mysql.com/Downloads/MySQL-${MYSQL_MAJOR}/mysql-${MYSQL_VERSION}.tar.gz.asc -o mysql.tar.gz.asc \
        && gpg --verify mysql.tar.gz.asc \
        && mkdir -p /usr/src \
        && tar -xzC /usr/src -f mysql.tar.gz

RUN \
        cd /usr/src/mysql-$MYSQL_VERSION \
#       && CFLAGS="-DSIGEV_THREAD_ID=4" cmake . -DBUILD_CONFIG=mysql_release \
        && cmake . -DBUILD_CONFIG=mysql_release \
        && make -j$(getconf _NPROCESSORS_ONLN) \
        && make install

Suggested fix:
the configure.cmake has this on line 503:

CHECK_SYMBOL_EXISTS(SIGEV_THREAD_ID "signal.h;time.h" HAVE_SIGEV_THREAD_ID)

A possible fix is to also search for SIGEV_THREAD_ID in the header linux/signal.h which comes from kernel headers.

A workaround for cmake is to set CFLAGS="-DSIGEV_THREAD_ID=4", however, this alone is not enough.

in mysys/posix_timers.c you will have to declare a `struct sigevent` that also includes the `tid` field. If you for example call it `struct linux_sigevent` then you need to typecast it when calling [timer_create](https://github.com/mysql/mysql-server/blob/a2757a60a7527407d08115e44e889a25f22c96c6/mysys/...)

For example:

struct linux_sigevent {
    union sigval sigev_value;
    int sigev_signo;
    int sigev_notify;
    union {
       int _pad[64-sizeof(int) * 2 + sizeof(union sigval)];
       int _tid;
    } _sigev_un;
};

...

int
my_timer_create(my_timer_t *timer)
{
    struct linux_sigevent sigev;
    ...
    return timer_create(CLOCK_MONOTONIC, (struct sigevent *)&sigev, &timer->id);
}
...

Well, you get the idea...
[6 Feb 2017 16:08] Kamil Dziedzic
Any plans for fixing this or any known workaround?
[19 Apr 2017 13:40] Tor Didriksen
OK, I gave it a try for our current developer version, 8.0.2
$uname -a
Linux vide21 4.4.59 #1-Alpine SMP Tue Apr 4 08:13:08 GMT 2017 x86_64 Linux

I get 'Invalid argument' from timer_create. Not surprising really. This is what I see for struct sigevent:

/usr/include/asm-generic/siginfo.h
typedef struct sigevent {
        sigval_t sigev_value;
        int sigev_signo;
        int sigev_notify;
        union {
                int _pad[SIGEV_PAD_SIZE];
                 int _tid;

                struct {
                        void (*_function)(sigval_t);
                        void *_attribute;       /* really pthread_attr_t */
                } _sigev_thread;
        } _sigev_un;
} sigevent_t;

/usr/include/signal.h
truct sigevent {
        union sigval sigev_value;
        int sigev_signo;
        int sigev_notify;
        void (*sigev_notify_function)(union sigval);
        pthread_attr_t *sigev_notify_attributes;
        char __pad[56-3*sizeof(long)];
};
[27 Apr 2017 16:05] Paul DuBois
Posted by developer:
 
Noted in 8.0.2 changelog.

MySQL now compiles on Alpine Linux.
[19 Oct 2018 14:55] Stijn Tintel
Can the fix be backported to MySQL 5.7? Thanks.
[6 Nov 2018 8:06] Tor Didriksen
Posted by developer:
 
We have about a dozen patches on top of this one, in order to make mysql actually *work* on Alpine Linux, so a backport is not trivial.
[1 Feb 2019 9:55] Terje Røsten
Patch to add MySQL 8 to Alpine Linux aports

Attachment: 0001-Add-MySQL-package.patch (text/x-patch), 17.31 KiB.

[1 Feb 2019 9:57] Terje Røsten
Hi Natanael!

Congrats with release of Alpine Linux 3.9!

Attached is patch to add MySQL 8 to aports.

It builds and runs fine on my local Alpine Linux 3.9 VM.
[28 Jun 2019 10:10] Terje Røsten
Patch to add MySQL 8.0 to Alpine Linux aports

Attachment: 0001-Add-MySQL-8.0-package.patch (text/x-patch), 15.64 KiB.

[28 Jun 2019 10:11] Terje Røsten
Hi Natanael!

Congrats with release of Alpine Linux 3.10!

Attached is patch to add MySQL 8.0.16 to aports.

It builds and runs fine on my local Alpine Linux 3.10 VM.
[2 Feb 2022 23:38] Andy Syam
did anyone make this patch in mysql-5.7? i applied mysql-8.0.2 patch but its not working in mysql-5.7