[PATCH] chrt: support for musl C library

Denys Vlasenko vda.linux at googlemail.com
Sun Nov 15 19:28:00 UTC 2020


Applied with some edits, thank you.

On Fri, Sep 11, 2020 at 6:18 PM Christian Eggers <ceggers at arri.de> wrote:
>
> musl "implements" several sched_xxx() functions by returning ENOSYS. As
> an alternative, either pthread_(g|s)etschedparam() or direct syscalls
> can be used.
>
> References: https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/commit/schedutils/chrt.c?id=fcc3078754291d2f5121797eb91b364f8e24b2f1
> References: http://git.musl-libc.org/cgit/musl/commit/src/sched/sched_setscheduler.c?id=1e21e78bf7a5c24c217446d8760be7b7188711c2
> Signed-off-by: Christian Eggers <ceggers at arri.de>
> ---
>  util-linux/chrt.c | 31 +++++++++++++++++++++++++++++--
>  1 file changed, 29 insertions(+), 2 deletions(-)
>
> diff --git a/util-linux/chrt.c b/util-linux/chrt.c
> index 4dd78dabf..e465d7cec 100644
> --- a/util-linux/chrt.c
> +++ b/util-linux/chrt.c
> @@ -34,6 +34,9 @@
>  //usage:       "You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
>
>  #include <sched.h>
> +#if defined (__linux__)
> +# include <sys/syscall.h>
> +#endif
>  #include "libbb.h"
>  #ifndef SCHED_IDLE
>  # define SCHED_IDLE 5
> @@ -85,6 +88,7 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
>         char *priority = priority; /* for compiler */
>         const char *current_new;
>         int policy = SCHED_RR;
> +       int ret;
>
>         opt = getopt32(argv, "^"
>                         "+" "mprfobi"
> @@ -132,7 +136,15 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
>         if (opt & OPT_p) {
>                 int pol;
>   print_rt_info:
> +#if defined (__linux__) && defined(SYS_sched_getscheduler)
> +               /* musl libc returns ENOSYS for its sched_getscheduler library
> +                * function, because the sched_getscheduler Linux kernel system call
> +                * does not conform to Posix; so we use the system call directly
> +                */
> +               pol = syscall(SYS_sched_getscheduler, pid);
> +#else
>                 pol = sched_getscheduler(pid);
> +#endif
>                 if (pol < 0)
>                         bb_perror_msg_and_die("can't %cet pid %u's policy", 'g', (int)pid);
>  #ifdef SCHED_RESET_ON_FORK
> @@ -149,7 +161,12 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
>                 printf("pid %u's %s scheduling policy: SCHED_%s\n",
>                         pid, current_new, policy_name(pol)
>                 );
> -               if (sched_getparam(pid, &sp))
> +#if defined (__linux__) && defined(SYS_sched_getparam)
> +               ret = syscall(SYS_sched_getparam, pid, &sp);
> +#else
> +               ret = sched_getparam(pid, &sp);
> +#endif
> +               if (ret)
>                         bb_perror_msg_and_die("can't get pid %u's attributes", (int)pid);
>                 printf("pid %u's %s scheduling priority: %d\n",
>                         (int)pid, current_new, sp.sched_priority
> @@ -168,7 +185,17 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
>                 sched_get_priority_min(policy), sched_get_priority_max(policy)
>         );
>
> -       if (sched_setscheduler(pid, policy, &sp) < 0)
> +#if defined (__linux__) && defined(SYS_sched_setscheduler)
> +       /* musl libc returns ENOSYS for its sched_setscheduler library
> +        * function, because the sched_setscheduler Linux kernel system call
> +        * does not conform to Posix; so we use the system call directly
> +        */
> +       ret = syscall(SYS_sched_setscheduler, pid, policy, &sp);
> +#else
> +       ret = sched_setscheduler(pid, policy, &sp);
> +#endif
> +
> +       if (ret < 0)
>                 bb_perror_msg_and_die("can't %cet pid %u's policy", 's', (int)pid);
>
>         if (!argv[0]) /* "-p PRIO PID [...]" */
> --
> Christian Eggers
> Embedded software developer
>
> Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
> Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
> Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
> Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
> Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox


More information about the busybox mailing list