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
This commit is contained in:
Olivier Certner 2023-05-29 19:09:36 +02:00 committed by Olivier Certner
parent aa96945e71
commit 7b94ec550e
No known key found for this signature in database
GPG key ID: 8CA13040971E2627

View file

@ -455,41 +455,12 @@ setlogincontext(login_cap_t *lc, const struct passwd *pwd, unsigned long flags)
}
/*
* setusercontext()
*
* Given a login class <lc> and a user in <pwd>, with a uid <uid>,
* set the context as in setclasscontext(). <flags> controls which
* values are set.
*
* The difference between setclasscontext() and setusercontext() is
* that the former sets things up for an already-existing process,
* while the latter sets things up from a root context. Such as might
* be called from login(1).
*
* Private function to set process priority.
*/
int
setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
static void
setclasspriority(login_cap_t * const lc, struct passwd const * const pwd)
{
login_cap_t *llc = NULL;
int error;
if (lc == NULL) {
if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
llc = lc; /* free this when we're done */
}
if (flags & LOGIN_SETPATH)
pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
/* we need a passwd entry to set these */
if (pwd == NULL)
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;
@ -533,8 +504,44 @@ setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned in
pwd ? pwd->pw_name : "",
pwd ? "': " : "",
lc ? lc->lc_class : "<none>");
}
/*
* setusercontext()
*
* Given a login class <lc> and a user in <pwd>, with a uid <uid>,
* set the context as in setclasscontext(). <flags> controls which
* values are set.
*
* The difference between setclasscontext() and setusercontext() is
* that the former sets things up for an already-existing process,
* while the latter sets things up from a root context. Such as might
* be called from login(1).
*
*/
int
setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
{
login_cap_t *llc = NULL;
int error;
if (lc == NULL) {
if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
llc = lc; /* free this when we're done */
}
if (flags & LOGIN_SETPATH)
pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
/* we need a passwd entry to set these */
if (pwd == NULL)
flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC);
/* Set the process priority */
if (flags & LOGIN_SETPRIORITY)
setclasspriority(lc, pwd);
/* Setup the user's group permissions */
if (flags & LOGIN_SETGROUP) {
if (setgid(pwd->pw_gid) != 0) {