Page MenuHomeFreeBSD

rc.subr: Do not apply limits unless requested
Needs ReviewPublic

Authored by 0mp on Dec 10 2021, 11:10 PM.
Tags
Referenced Files
Unknown Object (File)
Fri, Mar 22, 6:55 AM
Unknown Object (File)
Feb 13 2024, 5:26 PM
Unknown Object (File)
Dec 25 2023, 3:01 AM
Unknown Object (File)
Dec 20 2023, 2:43 AM
Unknown Object (File)
Nov 24 2023, 2:53 PM
Unknown Object (File)
Nov 23 2023, 1:10 AM
Unknown Object (File)
Nov 14 2023, 4:45 PM
Unknown Object (File)
Nov 13 2023, 6:25 PM
Subscribers

Details

Summary

Allow service scripts to declare the login class to be empty. This way
if both ${name}_login_class and ${name}_limits are set to empty strings
we can completely skip calling limits(1). As a result non-root users can
call run their own rc scripts.

In the past, a workaround for non-superuser accounts was to set login
class to "me". This way limits(1) would issue a warning, e.g.,

$ limits -C me echo ok
login class 'me' non-existent, using current settings
ok

and continue with the execution of the script. The behavior has
changed recently:

$ limits -C me echo ok
login class 'me' non-existent, using current settings
limits: setrlimit stacksize: Operation not permitted

I am not sure what commit caused this change in behavior.

While here, make the style of a comment consistent with the rest of the
comments in this section of the file.

Test Plan

Before the change:

$ sh -c ". /etc/rc.subr && name=rctest && command=/bin/echo && command_args=ok && rctest_login_class="" && run_rc_command start"
Starting rctest.
limits: setrlimit datasize: Operation not permitted
sh: WARNING: failed to start rctest

After the change:

$ sh -c ". ./rc.subr && name=rctest && command=/bin/echo && command_args=ok && rctest_login_class="" && run_rc_command start"
Starting rctest.
ok

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 46247
Build 43136: arc lint + arc unit

Event Timeline

0mp requested review of this revision.Dec 10 2021, 11:10 PM
libexec/rc/rc.subr
995

you dropped a : here. a - is an invalid character in a variable name.

I think you might want just:

libexec/rc/rc.subr
995

This is on purpose. This allows us to set name_login_class to an empty string. E.g.,

sh -c 'unset x; echo "unset and with a colon: ${x:-ok}"'
sh -c 'x=""; echo "empty and with a colon: ${x:-ok}"'
sh -c 'unset x; echo "unset and without a colon: ${x-ok}"'
sh -c 'x=""; echo "empty and without a colon: ${x-ok}"'

returns

unset and with a colon: ok
empty and with a colon: ok
unset and without a colon: ok
empty and without a colon:
dteske requested changes to this revision.Jul 1 2022, 8:16 PM

I have two suggestions, but you're only meant to pick one. I don't care which. Minor optimization or major optimization.

libexec/rc/rc.subr
1154

You could optimize it by shoving both into a single string and testing the results.

1154–1156

Alternatively, if you want to be avant-garde about it, you could optimize it further to just adding a single line above it which states "if both login_class and limits are null, skip running limts" (which I think is rather succinct)

This revision now requires changes to proceed.Jul 1 2022, 8:16 PM

Test login class and limits with a single [(1).

0mp marked an inline comment as done.Jul 4 2022, 9:23 AM
0mp added inline comments.
libexec/rc/rc.subr
1154

Hmm, I'm a bit worried that it muddles the code a bit. All speed gains we can get here are greatly appreciated by those running FreeBSD in emulators, however.

1154–1156

I think that the if statement is cleaner to readers.

0mp marked 3 inline comments as done.Jul 4 2022, 9:23 AM