#include "cgroup-util.h"
#include "macro.h"
#include "sd-daemon.h"
+#include "strv.h"
static int parse_argv(pam_handle_t *handle,
int argc, const char **argv,
bool *create_session,
bool *kill_session,
- bool *kill_user) {
+ bool *kill_user,
+ char ***controllers) {
unsigned i;
+ bool controller_set = false;
assert(argc >= 0);
assert(argc == 0 || argv);
if (create_session)
*create_session = k;
+
} else if (startswith(argv[i], "kill-session=")) {
if ((k = parse_boolean(argv[i] + 13)) < 0) {
pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
if (kill_user)
*kill_user = k;
+
+ } else if (startswith(argv[i], "controllers=")) {
+
+ if (controllers) {
+ char **l;
+
+ if (!(l = strv_split(argv[i] + 12, ","))) {
+ pam_syslog(handle, LOG_ERR, "Out of memory.");
+ return -ENOMEM;
+ }
+
+ strv_free(*controllers);
+ *controllers = l;
+ }
+
+ controller_set = true;
+
} else {
pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
return -EINVAL;
}
}
+ if (!controller_set && controllers) {
+ char **l;
+
+ if (!(l = strv_new("cpu", NULL))) {
+ pam_syslog(handle, LOG_ERR, "Out of memory");
+ return -ENOMEM;
+ }
+
+ *controllers = l;
+ }
+
+ if (controllers)
+ strv_remove(*controllers, "name=systemd");
+
if (kill_session && *kill_session && kill_user)
*kill_user = true;
return PAM_SUCCESS;
}
-static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach, bool remember) {
+static int create_user_group(
+ pam_handle_t *handle,
+ const char *controller,
+ const char *group,
+ struct passwd *pw,
+ bool attach,
+ bool remember) {
+
int r;
assert(handle);
assert(group);
if (attach)
- r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, group, 0);
+ r = cg_create_and_attach(controller, group, 0);
else
- r = cg_create(SYSTEMD_CGROUP_CONTROLLER, group);
+ r = cg_create(controller, group);
if (r < 0) {
pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
}
}
- if ((r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0 ||
- (r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
+ if ((r = cg_set_task_access(controller, group, 0644, pw->pw_uid, pw->pw_gid)) < 0 ||
+ (r = cg_set_group_access(controller, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r));
return PAM_SESSION_ERR;
}
char *buf = NULL;
int lock_fd = -1;
bool create_session = true;
+ char **controllers = NULL, **c;
assert(handle);
/* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
- if (parse_argv(handle, argc, argv, &create_session, NULL, NULL) < 0)
- return PAM_SESSION_ERR;
-
/* Make this a NOP on non-systemd systems */
if (sd_booted() <= 0)
return PAM_SUCCESS;
+ if (parse_argv(handle, argc, argv, &create_session, NULL, NULL, &controllers) < 0)
+ return PAM_SESSION_ERR;
+
if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
goto finish;
pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
- if ((r = create_user_group(handle, buf, pw, true, true)) != PAM_SUCCESS)
+ if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS)
goto finish;
+ /* The additional controllers don't really matter, so we
+ * ignore the return value */
+ STRV_FOREACH(c, controllers)
+ create_user_group(handle, *c, buf, pw, true, false);
+
r = PAM_SUCCESS;
finish:
if (lock_fd >= 0)
close_nointr_nofail(lock_fd);
+ strv_free(controllers);
+
return r;
}
const char *id;
struct passwd *pw;
const void *created = NULL;
+ char **controllers = NULL, **c;
assert(handle);
- if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user) < 0)
- return PAM_SESSION_ERR;
-
/* Make this a NOP on non-systemd systems */
if (sd_booted() <= 0)
return PAM_SUCCESS;
+ if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user, &controllers) < 0)
+ return PAM_SESSION_ERR;
+
if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
goto finish;
if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/user", 0)) < 0)
pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
+ STRV_FOREACH(c, controllers)
+ if ((r = cg_attach(*c, "/user", 0)) < 0)
+ pam_syslog(handle, LOG_ERR, "Failed to move us away in %s hierarchy: %s", *c, strerror(-r));
+
if (asprintf(&user_path, "/user/%s", username) < 0) {
r = PAM_BUF_ERR;
goto finish;
* cgroup. First, try to create the user group
* in case it doesn't exist yet. Also, delete
* the session group. */
- create_user_group(handle, nosession_path, pw, false, false);
+ create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, nosession_path, pw, false, false);
if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
}
+
+ STRV_FOREACH(c, controllers) {
+ create_user_group(handle, *c, nosession_path, pw, false, false);
+
+ if ((r = cg_migrate_recursive(*c, session_path, nosession_path, false, true)) < 0)
+ pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup in hierarchy %s: %s", *c, strerror(-r));
+ }
}
/* GC user tree */
r = -EBUSY;
}
+ STRV_FOREACH(c, controllers)
+ cg_trim(*c, user_path, true);
+
if (r >= 0) {
const char *runtime_dir;
free(nosession_path);
free(user_path);
+ strv_free(controllers);
+
return r;
}