unix/fiss

src/fsvs/stop.c in master
Repositories | Summary | Log | Files | LICENSE

stop.c (1060B) download


 1#include "service.h"
 2#include "util.h"
 3
 4#include <errno.h>
 5#include <limits.h>
 6#include <signal.h>
 7#include <stdio.h>
 8#include <string.h>
 9#include <unistd.h>
10
11
12void service_stop(struct service* s) {
13	switch (s->state) {
14		case STATE_ACTIVE_DUMMY:
15			service_handle_exit(s, false, 0);
16			break;
17		case STATE_ACTIVE_BACKGROUND:
18			if ((s->pid = fork_dup_cd_exec(s->dir, "./stop", null_fd, null_fd, null_fd)) == -1) {
19				fprint(1, "error: cannot execute ./stop: %r\n");
20				service_update_state(s, STATE_INACTIVE);
21			} else {
22				service_update_state(s, STATE_STOPPING);
23			}
24			break;
25		case STATE_ACTIVE_FOREGROUND:
26		case STATE_SETUP:
27		case STATE_STARTING:
28		case STATE_STOPPING:
29		case STATE_FINISHING:
30			s->stop_timeout = time(NULL);
31			kill(s->pid, SIGTERM);
32			service_update_state(s, -1);
33			break;
34		case STATE_DONE:
35			s->state = STATE_INACTIVE;
36		case STATE_INACTIVE:
37		case STATE_ERROR:
38			break;
39	}
40}
41
42void service_kill(struct service* s, int signal) {
43	if (!s->pid)
44		return;
45
46	if (s->state == STATE_ACTIVE_FOREGROUND)
47		kill(s->pid, signal);
48}