Search code examples
schedulinglinux-capabilities

Linux: setting process priority AND dynamically loading libraries


I have a linux application which loads *.so libraries using a modified rpath (set during installation). It also needs to run with realtime priority.

To get realtime priority it does this:

sched_param sched;
sched.sched_priority = 70;
sched_setscheduler(getpid(), SCHED_FIFO, &sched);

However sched_setscheduler is a privilaged method, protected by the CAP_SYS_NICE capability. Therefore, to get realtime priority without running as root, I add setcap to my postinst:

setcap cap_sys_nice+ep /path/to/myapp

However, linux decides that programs should not be allowed to load libraries from rpath if they have extra capabilities.

Is there a way for me to set my own priority and load rpath libraries?

Note: I'd prefer to do this in the application or in the postinst. I'd like to avoid deploying scripts as the only way to launch the application. I know sudo chrt -f -p 70 $! could do it from a script.


Solution

  • I have two solutions which do not involve modifying libc. Both solutions require us to replace the calls to sched_setscheduler() with a call to launch another process directly.

    1. Install a file to /etc/sudoers.d/ with the following line:

      %users ALL=NOPASSWD: /usr/bin/chrt
      

      Then from our application launch sudo as a process with arguments chrt -f -p X Y where X is the configured priority and Y is the result of getpid().

    2. Create a custom chrt with:

      cp $(which chrt) $(DESTDIR)/bin/chrt
      sudo setcap cap_sys_nice+ep $(DESTDIR)/bin/chrt
      sudo chmod 755 $(DESTDIR)/bin/chrt
      

      Then from our application launch chrt as a process with arguments -f -p X Y

    Not sure which solution is better. Note this is effectively embedded (or at least purpose built) so I'm not too worried about the security exposure.