summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mount.c28
-rw-r--r--service.c63
-rw-r--r--socket.c32
3 files changed, 85 insertions, 38 deletions
diff --git a/mount.c b/mount.c
index d52c11d3ca..5a386a6116 100644
--- a/mount.c
+++ b/mount.c
@@ -65,6 +65,16 @@ static const char* const state_string_table[_MOUNT_STATE_MAX] = {
[MOUNT_MAINTAINANCE] = "maintainance"
};
+static void service_unwatch_control_pid(Mount *m) {
+ assert(m);
+
+ if (m->control_pid <= 0)
+ return;
+
+ unit_unwatch_pid(UNIT(m), m->control_pid);
+ m->control_pid = 0;
+}
+
static void mount_parameters_done(MountParameters *p) {
assert(p);
@@ -91,10 +101,7 @@ static void mount_done(Unit *u) {
exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
m->control_command = NULL;
- if (m->control_pid > 0) {
- unit_unwatch_pid(u, m->control_pid);
- m->control_pid = 0;
- }
+ service_unwatch_control_pid(m);
unit_unwatch_timer(u, &m->timer_watch);
}
@@ -322,12 +329,7 @@ static void mount_set_state(Mount *m, MountState state) {
state != MOUNT_REMOUNTING_SIGTERM &&
state != MOUNT_REMOUNTING_SIGKILL) {
unit_unwatch_timer(UNIT(m), &m->timer_watch);
-
- if (m->control_pid > 0) {
- unit_unwatch_pid(UNIT(m), m->control_pid);
- m->control_pid = 0;
- }
-
+ service_unwatch_control_pid(m);
m->control_command = NULL;
}
@@ -505,6 +507,8 @@ static void mount_enter_unmounting(Mount *m, bool success) {
NULL)) < 0)
goto fail;
+ service_unwatch_control_pid(m);
+
if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
goto fail;
@@ -549,6 +553,8 @@ static void mount_enter_mounting(Mount *m, bool success) {
if (r < 0)
goto fail;
+ service_unwatch_control_pid(m);
+
if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
goto fail;
@@ -620,6 +626,8 @@ static void mount_enter_remounting(Mount *m, bool success) {
goto fail;
}
+ service_unwatch_control_pid(m);
+
if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
goto fail;
diff --git a/service.c b/service.c
index 73e733bd84..ef43537357 100644
--- a/service.c
+++ b/service.c
@@ -63,6 +63,26 @@ static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
[SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
};
+static void service_unwatch_control_pid(Service *s) {
+ assert(s);
+
+ if (s->control_pid <= 0)
+ return;
+
+ unit_unwatch_pid(UNIT(s), s->control_pid);
+ s->control_pid = 0;
+}
+
+static void service_unwatch_main_pid(Service *s) {
+ assert(s);
+
+ if (s->main_pid <= 0)
+ return;
+
+ unit_unwatch_pid(UNIT(s), s->main_pid);
+ s->main_pid = 0;
+}
+
static void service_done(Unit *u) {
Service *s = SERVICE(u);
@@ -83,15 +103,8 @@ static void service_done(Unit *u) {
/* This will leak a process, but at least no memory or any of
* our resources */
- if (s->main_pid > 0) {
- unit_unwatch_pid(u, s->main_pid);
- s->main_pid = 0;
- }
-
- if (s->control_pid > 0) {
- unit_unwatch_pid(u, s->control_pid);
- s->control_pid = 0;
- }
+ service_unwatch_main_pid(s);
+ service_unwatch_control_pid(s);
unit_unwatch_timer(u, &s->timer_watch);
}
@@ -831,6 +844,8 @@ static int service_load_pid_file(Service *s) {
if (s->main_pid_known)
return 0;
+ assert(s->main_pid <= 0);
+
if (!s->pid_file)
return -ENOENT;
@@ -955,10 +970,7 @@ static void service_set_state(Service *s, ServiceState state) {
state != SERVICE_STOP &&
state != SERVICE_STOP_SIGTERM &&
state != SERVICE_STOP_SIGKILL)
- if (s->main_pid > 0) {
- unit_unwatch_pid(UNIT(s), s->main_pid);
- s->main_pid = 0;
- }
+ service_unwatch_main_pid(s);
if (state != SERVICE_START_PRE &&
state != SERVICE_START &&
@@ -970,11 +982,7 @@ static void service_set_state(Service *s, ServiceState state) {
state != SERVICE_STOP_POST &&
state != SERVICE_FINAL_SIGTERM &&
state != SERVICE_FINAL_SIGKILL) {
- if (s->control_pid > 0) {
- unit_unwatch_pid(UNIT(s), s->control_pid);
- s->control_pid = 0;
- }
-
+ service_unwatch_control_pid(s);
s->control_command = NULL;
}
@@ -1145,6 +1153,8 @@ static void service_enter_stop_post(Service *s, bool success) {
if (!success)
s->failure = true;
+ service_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
if ((r = service_spawn(s,
s->control_command,
@@ -1238,6 +1248,8 @@ static void service_enter_stop(Service *s, bool success) {
if (!success)
s->failure = true;
+ service_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
if ((r = service_spawn(s,
s->control_command,
@@ -1264,6 +1276,8 @@ static void service_enter_start_post(Service *s) {
int r;
assert(s);
+ service_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
if ((r = service_spawn(s,
s->control_command,
@@ -1311,6 +1325,8 @@ static void service_enter_start(Service *s) {
/* For simple services we immediately start
* the START_POST binaries. */
+ service_unwatch_main_pid(s);
+
s->main_pid = pid;
s->main_pid_known = true;
service_enter_start_post(s);
@@ -1320,6 +1336,8 @@ static void service_enter_start(Service *s) {
/* For forking services we wait until the start
* process exited. */
+ service_unwatch_control_pid(s);
+
s->control_pid = pid;
s->control_command = s->exec_command[SERVICE_EXEC_START];
} else if (s->type == SERVICE_FINISH) {
@@ -1327,7 +1345,10 @@ static void service_enter_start(Service *s) {
/* For finishing services we wait until the start
* process exited, too, but it is our main process. */
+ service_unwatch_main_pid(s);
+
s->main_pid = pid;
+ s->main_pid_known = true;
s->control_command = s->exec_command[SERVICE_EXEC_START];
} else
assert_not_reached("Unknown service type");
@@ -1344,6 +1365,8 @@ static void service_enter_start_pre(Service *s) {
assert(s);
+ service_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
if ((r = service_spawn(s,
s->control_command,
@@ -1388,6 +1411,8 @@ static void service_enter_reload(Service *s) {
assert(s);
+ service_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
if ((r = service_spawn(s,
s->control_command,
@@ -1422,6 +1447,8 @@ static void service_run_next(Service *s, bool success) {
s->control_command = s->control_command->command_next;
+ service_unwatch_control_pid(s);
+
if ((r = service_spawn(s,
s->control_command,
true,
diff --git a/socket.c b/socket.c
index 00994daa29..f387ebed86 100644
--- a/socket.c
+++ b/socket.c
@@ -63,6 +63,16 @@ static const char* const state_string_table[_SOCKET_STATE_MAX] = {
[SOCKET_MAINTAINANCE] = "maintainance"
};
+static void socket_unwatch_control_pid(Socket *s) {
+ assert(s);
+
+ if (s->control_pid <= 0)
+ return;
+
+ unit_unwatch_pid(UNIT(s), s->control_pid);
+ s->control_pid = 0;
+}
+
static void socket_done(Unit *u) {
Socket *s = SOCKET(u);
SocketPort *p;
@@ -82,10 +92,7 @@ static void socket_done(Unit *u) {
exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
s->control_command = NULL;
- if (s->control_pid > 0) {
- unit_unwatch_pid(u, s->control_pid);
- s->control_pid = 0;
- }
+ socket_unwatch_control_pid(s);
s->service = NULL;
@@ -361,12 +368,7 @@ static void socket_set_state(Socket *s, SocketState state) {
state != SOCKET_STOP_POST_SIGTERM &&
state != SOCKET_STOP_POST_SIGKILL) {
unit_unwatch_timer(UNIT(s), &s->timer_watch);
-
- if (s->control_pid > 0) {
- unit_unwatch_pid(UNIT(s), s->control_pid);
- s->control_pid = 0;
- }
-
+ socket_unwatch_control_pid(s);
s->control_command = NULL;
}
@@ -437,6 +439,8 @@ static void socket_enter_stop_post(Socket *s, bool success) {
if (!success)
s->failure = true;
+ socket_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]))
if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
goto fail;
@@ -506,6 +510,8 @@ static void socket_enter_stop_pre(Socket *s, bool success) {
if (!success)
s->failure = true;
+ socket_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]))
if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
goto fail;
@@ -547,6 +553,8 @@ static void socket_enter_start_post(Socket *s) {
goto fail;
}
+ socket_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST]))
if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) {
log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
@@ -568,6 +576,8 @@ static void socket_enter_start_pre(Socket *s) {
int r;
assert(s);
+ socket_unwatch_control_pid(s);
+
if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE]))
if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
goto fail;
@@ -610,6 +620,8 @@ static void socket_run_next(Socket *s, bool success) {
if (!success)
s->failure = true;
+ socket_unwatch_control_pid(s);
+
s->control_command = s->control_command->command_next;
if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)