procd: add service instance watchdog
[project/procd.git] / service / instance.h
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #ifndef __PROCD_INSTANCE_H
16 #define __PROCD_INSTANCE_H
17
18 #include <libubox/vlist.h>
19 #include <libubox/uloop.h>
20 #include <libubox/ustream.h>
21 #include "../utils/utils.h"
22
23 #define RESPAWN_ERROR (5 * 60)
24 #define SIGNALLED_OFFSET 128
25
26 struct jail {
27 bool procfs;
28 bool sysfs;
29 bool ubus;
30 bool log;
31 bool ronly;
32 bool netns;
33 bool userns;
34 bool cgroupsns;
35 bool console;
36 char *name;
37 char *hostname;
38 struct blobmsg_list mount;
39 int argc;
40 };
41
42 typedef enum instance_watchdog {
43 INSTANCE_WATCHDOG_MODE_DISABLED,
44 INSTANCE_WATCHDOG_MODE_PASSIVE,
45 INSTANCE_WATCHDOG_MODE_ACTIVE,
46 __INSTANCE_WATCHDOG_MODE_MAX,
47 } instance_watchdog_mode_t;
48
49 struct watchdog {
50 instance_watchdog_mode_t mode;
51 uint32_t freq;
52 struct uloop_timeout timeout;
53 };
54
55 struct service_instance {
56 struct vlist_node node;
57 struct service *srv;
58 const char *name;
59
60 int8_t nice;
61 bool valid;
62
63 char *user;
64 uid_t uid;
65 gid_t pw_gid;
66 char *group;
67 gid_t gr_gid;
68
69 bool halt;
70 bool restart;
71 bool respawn;
72 int respawn_count;
73 int reload_signal;
74 struct timespec start;
75
76 bool trace;
77 bool has_jail;
78 bool require_jail;
79 bool no_new_privs;
80 struct jail jail;
81 char *seccomp;
82 char *pidfile;
83 char *extroot;
84 char *overlaydir;
85 char *tmpoverlaysize;
86 char *bundle;
87 int syslog_facility;
88 int exit_code;
89
90 uint32_t term_timeout;
91 uint32_t respawn_timeout;
92 uint32_t respawn_threshold;
93 uint32_t respawn_retry;
94
95 struct blob_attr *config;
96 struct uloop_process proc;
97 struct uloop_timeout timeout;
98 struct ustream_fd _stdout;
99 struct ustream_fd _stderr;
100 struct ustream_fd console;
101 struct ustream_fd console_client;
102
103 struct blob_attr *command;
104 struct blob_attr *trigger;
105 struct blobmsg_list env;
106 struct blobmsg_list data;
107 struct blobmsg_list netdev;
108 struct blobmsg_list file;
109 struct blobmsg_list limits;
110 struct blobmsg_list errors;
111
112 struct watchdog watchdog;
113 };
114
115 void instance_start(struct service_instance *in);
116 void instance_stop(struct service_instance *in, bool halt);
117 void instance_update(struct service_instance *in, struct service_instance *in_new);
118 void instance_init(struct service_instance *in, struct service *s, struct blob_attr *config);
119 void instance_free(struct service_instance *in);
120 void instance_dump(struct blob_buf *b, struct service_instance *in, int debug);
121
122 #endif