On 11/10/20 12:59 PM, Denys Vlasenko wrote:
> On Fri, Sep 11, 2020 at 6:18 PM Christian Eggers <[email protected]> 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 <[email protected]>
>> ---
>>  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 [...]" */
> 
> 
> I looked at it several times, but this looks not so good.
> These direct syscalls are less maintainable.

It's less bad if you put it all at the top:

  https://github.com/landley/toybox/commit/833fb23fe8b4

You can even wrap it in #ifdef __musl__ if you like. Not that it would ever
trigger because Rich insists that musl shouldn't identify itself.

(The TT.pid -> TT.p change in the same commit is unrelated.)

> OTOH... by the looks of it, there is no alternative?

Not that I could find, and I complained to Rich directly first.

Rob
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to