aboutsummaryrefslogtreecommitdiff
path: root/lib/include/service.h
blob: f388f8710600e40897a0d56c0be8722c098b09b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* SPDX-License-Identifier: ISC */
#ifndef SERVICE_H
#define SERVICE_H

#include <sys/types.h>
#include <stdint.h>

typedef struct exec_t {
	struct exec_t *next;
	int argc;		/* number of elements in argument vector */
	char args[];		/* argument vectot string blob */
} exec_t;

enum {
	STATE_OFF,
	STATE_RUNNING,
	STATE_QUEUED,
	STATE_COMPLETED,
	STATE_FAILED,
};

enum {
	/*
		Start the service in the background and continue with
		other services. The service will eventually terminate.
	*/
	SVC_ONCE = 0,
	SVC_WAIT,		/* run service and wait until it finishes */

	/*
		Similar to SVC_ONCE, but restart the service when
		it terminates.
	*/
	SVC_RESPAWN,

	SVC_MAX
};

enum {
	TGT_BOOT = 0,		/* run service when the system boots */
	TGT_SHUTDOWN,		/* run service when at system shut down */
	TGT_REBOOT,		/* run service when during system reboot */

	TGT_MAX
};

enum {
	/* truncate stdout */
	SVC_FLAG_TRUNCATE_OUT = 0x01,

	SVC_FLAG_SUB_REAPER = 0x08,

	SVC_FLAG_HAS_EXEC = 0x10,
};

/* persistent service configuration read from disk */
typedef struct service_t {
	struct service_t *next;

	char *fname;		/* source file name */
	char *desc;		/* description string */
	char *ctty;		/* controlling tty or log file */

	/* linked list of command lines to execute */
	exec_t *exec;

	char *before;	/* services that must be executed later */
	char *after;	/* services that must be executed first */

	uint32_t num_before;
	uint32_t num_after;

	uint16_t rspwn_limit;	/* maximum respawn count */
	uint16_t flags;		/* SVC_FLAG_* bit field */
	uint8_t type;		/* SVC_* service type */
	uint8_t target;		/* TGT_* service target */

	char name[];		/* canonical service name */
} service_t;

typedef struct {
	service_t *targets[TGT_MAX];
} service_list_t;

/* run time configuration in-memory for managing services */
typedef struct {
	service_t *svc;		/* the underlying service description */
	pid_t pid;		/* if still running, the pid */
	uint16_t rspwn_count;	/* services respawn counter */
	uint8_t state;		/* what STATE_* the service is currently in */
	uint8_t status;		/* if exited, process exit status */
} svc_run_data_t;

/*
	Read a service from a file.
*/
service_t *rdsvc(int dirfd, const char *filename);

void delsvc(service_t *svc);

/*
	Rebuild a service list by scanning a directory and parsing all
	service descriptions.

	Returns 0 on success, -1 on failure. The function takes care of
	printing error messages on failure.
*/
int svcscan(const char *directory, service_list_t *list);

void del_svc_list(service_list_t *list);

/*
	Sort a list of services by dependencies.
*/
service_t *svc_tsort(service_t *list);

const char *svc_type_to_string(int type);

int svc_type_from_string(const char *type);

const char *svc_target_to_string(int target);

int svc_target_from_string(const char *target);

#endif /* SERVICE_H */