login: make user->service static

Just like user->slice, there is no reason to store the unit name in /run,
nor should we allocate it dynamically on job instantiation/removal. Just
keep it statically around at all times and rely on user->started ||
user->stopping to figure out whether the unit exists or not.
This commit is contained in:
David Herrmann
2015-11-16 15:34:41 +01:00
parent 157f50577f
commit b690ef12b5
+20 -32
View File
@@ -67,6 +67,7 @@ int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
u->manager = m;
u->uid = uid;
u->gid = gid;
xsprintf(lu, UID_FMT, uid);
u->name = strdup(name);
if (!u->name)
@@ -78,11 +79,14 @@ int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
return -ENOMEM;
xsprintf(lu, UID_FMT, uid);
r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
if (r < 0)
return r;
r = unit_name_build("user", lu, ".service", &u->service);
if (r < 0)
return r;
r = hashmap_put(m->users, UID_TO_PTR(uid), u);
if (r < 0)
return r;
@@ -91,6 +95,10 @@ int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
if (r < 0)
return r;
r = hashmap_put(m->user_units, u->service, u);
if (r < 0)
return r;
*out = u;
u = NULL;
return 0;
@@ -155,8 +163,6 @@ static int user_save_internal(User *u) {
if (u->runtime_path)
fprintf(f, "RUNTIME=%s\n", u->runtime_path);
if (u->service)
fprintf(f, "SERVICE=%s\n", u->service);
if (u->service_job)
fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
@@ -297,7 +303,6 @@ int user_load(User *u) {
assert(u);
r = parse_env_file(u->state_file, NEWLINE,
"SERVICE", &u->service,
"SERVICE_JOB", &u->service_job,
"SLICE_JOB", &u->slice_job,
"DISPLAY", &display,
@@ -425,34 +430,20 @@ static int user_start_service(User *u) {
assert(u);
if (!u->service) {
char lu[DECIMAL_STR_MAX(uid_t) + 1], *service;
u->service_job = mfree(u->service_job);
xsprintf(lu, UID_FMT, u->uid);
r = unit_name_build("user", lu, ".service", &service);
if (r < 0)
return log_error_errno(r, "Failed to build service name: %m");
r = manager_start_unit(
u->manager,
service,
&error,
&job);
if (r < 0) {
log_error_errno(r, "Failed to start user service, ignoring: %s", bus_error_message(&error, r));
free(service);
/* we don't fail due to this, let's try to continue */
} else {
u->service = service;
free(u->service_job);
u->service_job = job;
}
r = manager_start_unit(
u->manager,
u->service,
&error,
&job);
if (r < 0) {
/* we don't fail due to this, let's try to continue */
log_error_errno(r, "Failed to start user service, ignoring: %s", bus_error_message(&error, r));
} else {
u->service_job = job;
}
if (u->service)
(void) hashmap_put(u->manager->user_units, u->service, u);
return 0;
}
@@ -526,9 +517,6 @@ static int user_stop_service(User *u) {
assert(u);
if (!u->service)
return 0;
r = manager_stop_unit(u->manager, u->service, &error, &job);
if (r < 0) {
log_error("Failed to stop user service: %s", bus_error_message(&error, r));