Date: Thu, 1 Feb 2024 21:31:18 GMT From: Olivier Certner <olce@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: f49953dd49f2 - stable/14 - setusercontext(): Move priority setting in new setclasspriority() Message-ID: <202402012131.411LVIDX080863@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=f49953dd49f28169a5f3b86a414936c095c19ea9 commit f49953dd49f28169a5f3b86a414936c095c19ea9 Author: Olivier Certner <olce@FreeBSD.org> AuthorDate: 2023-05-29 17:09:36 +0000 Commit: Olivier Certner <olce@FreeBSD.org> CommitDate: 2024-02-01 21:29:48 +0000 setusercontext(): Move priority setting in new setclasspriority() In preparation for setting priorities from '~/.login_conf' and to ease reading of setusercontext(). No functional change. Reviewed by: emaste Approved by: emaste (mentor) MFC after: 3 days Sponsored by: Kumacom SAS Differential Revision: https://reviews.freebsd.org/D40350 (cherry picked from commit 7b94ec550ef6e7b75d608e125e9b78478197d4e7) Approved by: markj (mentor) --- lib/libutil/login_class.c | 97 +++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/lib/libutil/login_class.c b/lib/libutil/login_class.c index 9c3285736f3b..9d8dc652d678 100644 --- a/lib/libutil/login_class.c +++ b/lib/libutil/login_class.c @@ -456,6 +456,56 @@ setlogincontext(login_cap_t *lc, const struct passwd *pwd, unsigned long flags) } +/* + * Private function to set process priority. + */ +static void +setclasspriority(login_cap_t * const lc, struct passwd const * const pwd) +{ + const rlim_t def_val = LOGIN_DEFPRI, err_val = INT64_MIN; + rlim_t p = login_getcapnum(lc, "priority", def_val, err_val); + int rc; + + if (p == err_val) { + /* Invariant: 'lc' != NULL. */ + syslog(LOG_WARNING, + "%s%s%sLogin class '%s': " + "Invalid priority specification: '%s'", + pwd ? "Login '" : "", + pwd ? pwd->pw_name : "", + pwd ? "': " : "", + lc->lc_class, + login_getcapstr(lc, "priority", "", "")); + /* Reset the priority, as if the capability was not present. */ + p = def_val; + } + + if (p > PRIO_MAX) { + struct rtprio rtp; + + rtp.type = RTP_PRIO_IDLE; + p += RTP_PRIO_MIN - (PRIO_MAX + 1); + rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p; + rc = rtprio(RTP_SET, 0, &rtp); + } else if (p < PRIO_MIN) { + struct rtprio rtp; + + rtp.type = RTP_PRIO_REALTIME; + p += RTP_PRIO_MAX - (PRIO_MIN - 1); + rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p; + rc = rtprio(RTP_SET, 0, &rtp); + } else + rc = setpriority(PRIO_PROCESS, 0, (int)p); + + if (rc != 0) + syslog(LOG_WARNING, + "%s%s%sLogin class '%s': " + "Setting priority failed: %m", + pwd ? "Login '" : "", + pwd ? pwd->pw_name : "", + pwd ? "': " : "", + lc ? lc->lc_class : "<none>"); +} /* * setusercontext() @@ -490,51 +540,8 @@ setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned in flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); /* Set the process priority */ - if (flags & LOGIN_SETPRIORITY) { - const rlim_t def_val = LOGIN_DEFPRI, err_val = INT64_MIN; - rlim_t p = login_getcapnum(lc, "priority", def_val, err_val); - int rc; - - if (p == err_val) { - /* Invariant: 'lc' != NULL. */ - syslog(LOG_WARNING, - "%s%s%sLogin class '%s': " - "Invalid priority specification: '%s'", - pwd ? "Login '" : "", - pwd ? pwd->pw_name : "", - pwd ? "': " : "", - lc->lc_class, - login_getcapstr(lc, "priority", "", "")); - /* Reset the priority, as if the capability was not present. */ - p = def_val; - } - - if (p > PRIO_MAX) { - struct rtprio rtp; - - rtp.type = RTP_PRIO_IDLE; - p += RTP_PRIO_MIN - (PRIO_MAX + 1); - rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p; - rc = rtprio(RTP_SET, 0, &rtp); - } else if (p < PRIO_MIN) { - struct rtprio rtp; - - rtp.type = RTP_PRIO_REALTIME; - p += RTP_PRIO_MAX - (PRIO_MIN - 1); - rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p; - rc = rtprio(RTP_SET, 0, &rtp); - } else - rc = setpriority(PRIO_PROCESS, 0, (int)p); - - if (rc != 0) - syslog(LOG_WARNING, - "%s%s%sLogin class '%s': " - "Setting priority failed: %m", - pwd ? "Login '" : "", - pwd ? pwd->pw_name : "", - pwd ? "': " : "", - lc ? lc->lc_class : "<none>"); - } + if (flags & LOGIN_SETPRIORITY) + setclasspriority(lc, pwd); /* Setup the user's group permissions */ if (flags & LOGIN_SETGROUP) {
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202402012131.411LVIDX080863>