Skip to content

Commit cc6c321

Browse files
committed
native service: Move service termination (SIGTERM / exec thread stop) into own method
1 parent 34b0913 commit cc6c321

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

service/src/main.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ int daemon_spawn(pid_t *pid)
6262
return res;
6363
}
6464

65+
int daemon_terminate(service_t *service)
66+
{
67+
int res = 0;
68+
69+
if (service->daemon_pid > 0) {
70+
res = kill(service->daemon_pid, SIGTERM);
71+
if (res != 0) {
72+
ERR("kill failed, res=%d", res);
73+
return 1;
74+
}
75+
} else {
76+
ERR("PID not set: %d", service->daemon_pid);
77+
return 2;
78+
}
79+
80+
81+
if (service->execution_thread != (pthread_t) NULL) {
82+
res = pthread_join(service->execution_thread, NULL);
83+
if (res != 0) {
84+
ERR("pthread_join failed, res=%d", res);
85+
return 3;
86+
}
87+
service->execution_thread = (pthread_t) NULL;
88+
}
89+
else {
90+
ERR("Execution thread is not up");
91+
return 4;
92+
}
93+
94+
return res;
95+
}
96+
6597
void *execution_task(void *data)
6698
{
6799
int res = 0;
@@ -151,19 +183,12 @@ int daemon_stop(service_t* service)
151183
return 2;
152184
}
153185

154-
res = kill(service->daemon_pid, SIGTERM);
186+
res = daemon_terminate(service);
155187
if (res != 0) {
156-
ERR("kill failed, res=%d", res);
188+
ERR("Daemon termination failed, res=%d", res);
157189
return 3;
158190
}
159191

160-
res = pthread_join(service->execution_thread, NULL);
161-
if (res != 0) {
162-
ERR("pthread_join failed, res=%d", res);
163-
return 4;
164-
}
165-
service->execution_thread = (pthread_t) NULL;
166-
167192
return 0;
168193
}
169194

0 commit comments

Comments
 (0)