Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 808600 - sys-apps/sg3_utils-1.46: Fails to compile on musl libc due to missing reentrant versions of srand48/mrand48 and struct drand48_data
Summary: sys-apps/sg3_utils-1.46: Fails to compile on musl libc due to missing reentra...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal
Assignee: Gentoo's Team for Core System packages
URL: https://github.com/doug-gilbert/sg3_u...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-08-16 14:27 UTC by Joshua Kinard
Modified: 2021-08-17 04:08 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
convert to non-reentrant drand48(3) functions for portability (sg3_utils-1.46-musl-drand48-compat.patch,1.18 KB, patch)
2021-08-16 17:31 UTC, Joshua Kinard
Details | Diff
ebuild patch (sg3_utils-1.46-r1_ebuild-808600.patch,582 bytes, patch)
2021-08-16 17:33 UTC, Joshua Kinard
Details | Diff
convert to non-reentrant drand48(3) functions for portability (sg3_utils-1.46-musl-drand48-compat.patch,4.43 KB, patch)
2021-08-16 18:24 UTC, Joshua Kinard
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joshua Kinard gentoo-dev 2021-08-16 14:27:22 UTC
In sg3_utils-1.46, code was added that utilized the reentrant versions of the mrand48 and srand48 functions (mrand48_r and srand48_r, respectively; see man page drand_r(3)), as well as struct drand48_data.  These three elements are not implemented in current musl libc, and not likely to be supported, since it looks like these functions are not defined by POSIX.  The last version of sg3_utils that I compiled under musl libc was 1.45.

The one lead I have on a patch is for a different package, mtproxy, that had a similar compilation failure under musl libc.  Its solution was to locally implement the missing functions by copying code from glibc:
https://github.com/alexdoesh/mtproxy/blob/master/patches/randr_compat.patch
Comment 1 Joshua Kinard gentoo-dev 2021-08-16 17:31:46 UTC
Created attachment 733312 [details, diff]
convert to non-reentrant drand48(3) functions for portability

I took a look at where in sg3_utils' code the reentrant functions were being used.  It's just a single file, src/sg_dd.c, and it contains a single invocation of srand48_r() followed ~300 lines later by mrand48_r().  Converting these to their non-reentrant functions is trivial, so I opted to go that route.  This allows the source to compile, and a simple test of sg_dd to copy 20M from /dev/urandom to a file works without issue under a MIPS musl chroot.
Comment 2 Joshua Kinard gentoo-dev 2021-08-16 17:33:48 UTC
Created attachment 733315 [details, diff]
ebuild patch

Quick patch to the ebuild to bump it to EAPI 8 (note, I quoted the EAPI value, since PMS notes it's not always an integer) and apply the previous patch to convert to the non-reentrant versions of the drand48(3) family of functions in src/sg_dd.c.
Comment 3 Joshua Kinard gentoo-dev 2021-08-16 17:46:29 UTC
Swapping bug ownership to base-system for review.  The proposed changes are not specific to musl and should work under other POSIX-compliant libc's as well.  I'll pass the patch on to the author as well.  Doesn't seem like there's a public bug tracker for this package.
Comment 4 Joshua Kinard gentoo-dev 2021-08-16 18:24:18 UTC
Created attachment 733324 [details, diff]
convert to non-reentrant drand48(3) functions for portability

Missed some cases in the testing/ subdirectory and also tweaked the sg_dd manpage.
Comment 5 Mike Gilbert gentoo-dev 2021-08-16 18:32:02 UTC
Let's wait a bit to see if the patch is accepted by the author. Say, 2 weeks? 

If the author accepts the patch or there's no response, please go ahead and add it to the gentoo repo.
Comment 6 Joshua Kinard gentoo-dev 2021-08-16 18:37:10 UTC
(In reply to Mike Gilbert from comment #5)
> Let's wait a bit to see if the patch is accepted by the author. Say, 2
> weeks? 
> 
> If the author accepts the patch or there's no response, please go ahead and
> add it to the gentoo repo.

Sure, there's no rush on my end.  I will resurrect the older 1.45 release in a local repo for my musl environment, since that was the last legit version I built for MIPS netboots last year.
Comment 7 Joshua Kinard gentoo-dev 2021-08-16 21:41:49 UTC
(In reply to Mike Gilbert from comment #5)
> Let's wait a bit to see if the patch is accepted by the author. Say, 2
> weeks? 
> 
> If the author accepts the patch or there's no response, please go ahead and
> add it to the gentoo repo.

Looks like we're good, the author replied:

"""
Hi,
Thanks for finding, analyzing and providing a patch for this problem. Not
often someone goes to the extra effort of finding all occurrences, including
in the manpage. I have applied that patch and it is in revision 909 (of my
beta sg3_utils-1.47). My primary sg3_utils subversion repository is mirrored
at:
    https://github.com/doug-gilbert/sg3_utils
thanks to the git-svn tools.
"""

If there are no additional issues, I'll commit this change later tonight.
Comment 8 Larry the Git Cow gentoo-dev 2021-08-17 03:59:05 UTC
The bug has been closed via the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=38cd2e8f37c164f42abaf9de696610d9053d27c1

commit 38cd2e8f37c164f42abaf9de696610d9053d27c1
Author:     Joshua Kinard <kumba@gentoo.org>
AuthorDate: 2021-08-17 03:57:24 +0000
Commit:     Joshua Kinard <kumba@gentoo.org>
CommitDate: 2021-08-17 03:58:34 +0000

    sys-apps/sg3_utils: Fix a compile issue under musl libc
    
    Fix a compile issue under musl libc where the sg_dd utility
    internally uses the reentrant functions srand48_r(3) and
    mrand48_r(3), which are glibc-specific extensions.  Changed
    to using the portable non-reentrant versions, and sent a patch
    upstream, which was acknowledged by the author for the next
    release.
    
    Closes: https://bugs.gentoo.org/808600
    Signed-off-by: Joshua Kinard <kumba@gentoo.org>
    Package-Manager: Portage-3.0.20, Repoman-3.0.3

 .../files/sg3_utils-1.46-musl-drand48-compat.patch | 99 ++++++++++++++++++++++
 sys-apps/sg3_utils/sg3_utils-1.46-r1.ebuild        |  7 +-
 2 files changed, 105 insertions(+), 1 deletion(-)
Comment 9 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-08-17 04:08:30 UTC
Great work, thank you!