Hello, 

----- Mail original -----
> On Fri, Sep 11, 2020 at 6:18 PM Christian Eggers <cegg...@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 <cegg...@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 [...]" */
> 
> 
> I looked at it several times, but this looks not so good.
> These direct syscalls are less maintainable.
> 
> OTOH... by the looks of it, there is no alternative?

Maybe define a function or even a static inline that does the job? That should 
not cause any issue and it should be easier to maintain (not to mention that it 
could be added to libbb if needed in order to be reused in other places).

On a side note, instead of checking for __linux__ only, one can also check for 
!__GLIBC__ as well in order to limit the use case to libraries that are not the 
glibc (yes, I link busybox with the glibc, which more or less defeat the 
purpose of busybox).

#if defined(__linux__) && defined(SYS_sched_setscheduler) && !defined(__GLIBC__)

> I'll probably apply it.

Best regards, 

-- Emmanuel Deloget

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to