xref: /minix3/minix/fs/procfs/service.c (revision b5e2faaaaf60a8b9a02f8d72f64caa56a87eb312)
1 /* ProcFS - service.c - the service subdirectory */
2 
3 #include "inc.h"
4 
5 #include <minix/rs.h>
6 #include "rs/const.h"
7 #include "rs/type.h"
8 
9 enum policy {
10 	POL_NONE	= 0x00,	/*     user	| endpoint	*/
11 	POL_RESET	= 0x01,	/* visible	|  change	*/
12 	POL_RESTART	= 0x02,	/* transparent	| preserved	*/
13 	POL_LIVE_UPDATE	= 0x04	/* transparent	| preserved	*/
14 };
15 
16 struct policies {
17 	#define MAX_POL_FORMAT_SZ 20
18 	char formatted[MAX_POL_FORMAT_SZ];
19 	enum policy supported;
20 };
21 
22 static struct {
23 	struct rproc proc[NR_SYS_PROCS];
24 	struct rprocpub pub[NR_SYS_PROCS];
25 } rproc;
26 
27 static struct policies policies[NR_SYS_PROCS];
28 
29 static struct inode *service_node;
30 
31 /* Updates the policies state from RS. Always returns an ASCIIZ string.  */
32 static const char *
33 service_get_policies(struct policies * pol, index_t slot)
34 {
35 #if 1 /* The following should be retrieved from RS and formated instead. */
36 	int pos;
37 	char *ref_label;
38 	static const struct {
39 		const char *label;
40 		const char *policy_str;
41 	} def_pol[] = {
42 		/* audio */
43 		{ .label = "es1370", .policy_str = "reset" },
44 		{ .label = "es1371", .policy_str = "reset" },
45 		{ .label = "sb16", .policy_str = "reset" },
46 		/* bus */
47 		{ .label = "i2c", .policy_str = "restart" },
48 		{ .label = "pci", .policy_str = "restart" },
49 		{ .label = "ti1225", .policy_str = "restart" },
50 		/* clock */
51 		{ .label = "readclock.drv", .policy_str = "restart" },
52 		/* eeprom */
53 		{ .label = "cat24c256", .policy_str = "restart" },
54 		/* examples */
55 		{ .label = "hello", .policy_str = "restart" },
56 		/* hid */
57 		{ .label = "pckbd", .policy_str = "reset" },
58 		/* iommu */
59 		{ .label = "amddev", .policy_str = "" },
60 		/* net */
61 		{ .label = "3c90x", .policy_str = "reset" },
62 		{ .label = "atl2", .policy_str = "reset" },
63 		{ .label = "dec21140A", .policy_str = "reset" },
64 		{ .label = "dp8390", .policy_str = "reset" },
65 		{ .label = "dpeth", .policy_str = "reset" },
66 		{ .label = "e1000", .policy_str = "reset" },
67 		{ .label = "fxp", .policy_str = "reset" },
68 		{ .label = "lance", .policy_str = "reset" },
69 		{ .label = "lan8710a", .policy_str = "reset" },
70 		{ .label = "orinoco", .policy_str = "reset" },
71 		{ .label = "rtl8139", .policy_str = "reset" },
72 		{ .label = "rtl8169", .policy_str = "reset" },
73 		{ .label = "uds", .policy_str = "reset" },
74 		{ .label = "virtio_net", .policy_str = "reset" },
75 		/* power */
76 		{ .label = "acpi", .policy_str = "" },
77 		{ .label = "tps65217", .policy_str = "" },
78 		{ .label = "tps65590", .policy_str = "" },
79 		/* printer */
80 		{ .label = "printer", .policy_str = "restart" },
81 		/* sensors */
82 		{ .label = "bmp085", .policy_str = "" },
83 		{ .label = "sht21", .policy_str = "restart" },
84 		{ .label = "tsl2550", .policy_str = "restart" },
85 		/* storage */
86 		{ .label = "ahci", .policy_str = "reset" },
87 		{ .label = "at_wini", .policy_str = "reset" },
88 		{ .label = "fbd", .policy_str = "reset" },
89 		{ .label = "filter", .policy_str = "reset" },
90 		{ .label = "floppy", .policy_str = "reset" },
91 		{ .label = "memory", .policy_str = "restart" },
92 		{ .label = "mmc", .policy_str = "reset" },
93 		{ .label = "virtio_blk", .policy_str = "reset" },
94 		{ .label = "vnd", .policy_str = "reset" },
95 		/* system */
96 		{ .label = "gpio", .policy_str = "restart" },
97 		{ .label = "log", .policy_str = "reset" },
98 		{ .label = "random", .policy_str = "restart" },
99 		/* tty */
100 		{ .label = "pty", .policy_str = "restart" },
101 		{ .label = "tty", .policy_str = "restart" },
102 		/* usb */
103 		{ .label = "usbd", .policy_str = "" },
104 		{ .label = "usb_hub", .policy_str = "" },
105 		{ .label = "usb_storage", .policy_str = "" },
106 		/* video */
107 		{ .label = "fb", .policy_str = "" },
108 		{ .label = "tda19988", .policy_str = "" },
109 		/* vmm_guest */
110 		{ .label = "vbox", .policy_str = "" },
111 		/* fs */
112 		{ .label = "ext2", .policy_str = "" },
113 		{ .label = "hgfs", .policy_str = "" },
114 		{ .label = "isofs", .policy_str = "" },
115 		{ .label = "mfs", .policy_str = "restart" },
116 		{ .label = "pfs", .policy_str = "restart" },
117 		{ .label = "procfs", .policy_str = "restart" },
118 		{ .label = "ptyfs", .policy_str = "" },
119 		{ .label = "vbfs", .policy_str = "" },
120 		/* net */
121 		{ .label = "inet", .policy_str = "reset" },
122 		{ .label = "lwip", .policy_str = "" },
123 		/* servers */
124 		{ .label = "devman", .policy_str = "restart" },
125 		{ .label = "ds", .policy_str = "restart" },
126 		{ .label = "input", .policy_str = "reset" },
127 		{ .label = "ipc", .policy_str = "restart" },
128 		{ .label = "is", .policy_str = "restart" },
129 		{ .label = "pm", .policy_str = "restart" },
130 		{ .label = "rs", .policy_str = "restart" },
131 		{ .label = "sched", .policy_str = "restart" },
132 		{ .label = "vfs", .policy_str = "restart" },
133 		{ .label = "vm", .policy_str = "restart" },
134 		//{ .label = "", .policy_str = "" },
135 	};
136 
137 	/* Find the related policy, based on the file name of the service. */
138 	ref_label = strrchr(rproc.pub[slot].proc_name, '/');
139 	if (NULL == ref_label)
140 		ref_label = rproc.pub[slot].proc_name;
141 
142 	memset(pol[slot].formatted, 0, MAX_POL_FORMAT_SZ);
143 	for(pos = 0; pos < (sizeof(def_pol) / sizeof(def_pol[0])); pos++) {
144 		if (0 == strcmp(ref_label, def_pol[pos].label)) {
145 			(void)strncpy(pol[slot].formatted,
146 			    def_pol[pos].policy_str, MAX_POL_FORMAT_SZ);
147 			pol[slot].formatted[MAX_POL_FORMAT_SZ-1] = '\0';
148 			break;
149 		}
150 	}
151 #else
152 	/* Should do something sensible, based on flags from RS/SEF. */
153 #endif
154 
155 	return pol[slot].formatted;
156 }
157 
158 /* Returns a ASCIIZ string encoding RS flags.  */
159 static const char *
160 service_get_flags(index_t slot)
161 {
162 	static char str[10];
163 	int flags, sys_flags;
164 
165 	flags = rproc.proc[slot].r_flags;
166 	sys_flags = rproc.pub[slot].sys_flags;
167 
168 	str[0] = (flags & RS_ACTIVE)        ? 'A' : '-';
169 	str[1] = (flags & RS_UPDATING)      ? 'U' : '-';
170 	str[2] = (flags & RS_EXITING)       ? 'E' : '-';
171 	str[3] = (flags & RS_NOPINGREPLY)   ? 'N' : '-';
172 	str[4] = (sys_flags & SF_USE_COPY)  ? 'C' : '-';
173 	str[5] = (sys_flags & SF_USE_REPL)  ? 'R' : '-';
174 	str[6] = (sys_flags & SF_NEED_COPY) ? 'c' : '-';
175 	str[7] = (sys_flags & SF_NEED_REPL) ? 'r' : '-';
176 	str[8] = (sys_flags & SF_CORE_SRV)  ? 's' : '-';
177 	str[9] = '\0';
178 
179 	return str;
180 }
181 
182 /*
183  * Return whether a slot is in use and active.  The purpose of this check is
184  * to ensure that after eliminating all slots that do not pass this check, we
185  * are left with a set of live services each with a unique label.
186  */
187 static int
188 service_active(index_t slot)
189 {
190 
191 	/*
192 	 * Init is in RS's process tables as the representation of user
193 	 * processes.  It is not a system service.
194 	 */
195 	return ((rproc.proc[slot].r_flags & (RS_IN_USE | RS_ACTIVE)) ==
196 	    (RS_IN_USE | RS_ACTIVE) &&
197 	   rproc.pub[slot].endpoint != INIT_PROC_NR);
198 }
199 
200 /*
201  * Update the contents of the service directory, by first updating the RS
202  * tables and then updating the directory contents.
203  */
204 static void
205 service_update(void)
206 {
207 	struct inode *node;
208 	struct inode_stat stat;
209 	index_t slot;
210 	static int warned = FALSE;
211 	int r;
212 
213 	/* There is not much we can do if this call fails. */
214 	r = getsysinfo(RS_PROC_NR, SI_PROCALL_TAB, &rproc, sizeof(rproc));
215 	if (r != OK && !warned) {
216 		printf("PROCFS: unable to obtain RS tables (%d)\n", r);
217 		warned = TRUE;
218 	}
219 
220 	/*
221 	 * As with PIDs, we make two passes.  Delete first, then add.  This
222 	 * prevents problems in the hypothetical case that between updates, one
223 	 * slot ends up with the label name of a previous, different slot.
224 	 */
225 	for (slot = 0; slot < NR_SYS_PROCS; slot++) {
226 		if ((node = get_inode_by_index(service_node, slot)) == NULL)
227 			continue;
228 
229 		/*
230 		 * If the slot is no longer in use, or the label name does not
231 		 * match, the node must be deleted.
232 		 */
233 		if (!service_active(slot) ||
234 		    strcmp(get_inode_name(node), rproc.pub[slot].label))
235 			delete_inode(node);
236 	}
237 
238 	memset(&stat, 0, sizeof(stat));
239 	stat.mode = REG_ALL_MODE;
240 	stat.uid = SUPER_USER;
241 	stat.gid = SUPER_USER;
242 
243 	for (slot = 0; slot < NR_SYS_PROCS; slot++) {
244 		if (!service_active(slot) ||
245 		    get_inode_by_index(service_node, slot) != NULL)
246 			continue;
247 
248 		node = add_inode(service_node, rproc.pub[slot].label, slot,
249 		    &stat, (index_t)0, (cbdata_t)slot);
250 
251 		if (node == NULL)
252 			out_of_inodes();
253 	}
254 }
255 
256 /*
257  * Initialize the service directory.
258  */
259 void
260 service_init(void)
261 {
262 	struct inode *root, *node;
263 	struct inode_stat stat;
264 
265 	root = get_root_inode();
266 
267 	memset(&stat, 0, sizeof(stat));
268 	stat.mode = DIR_ALL_MODE;
269 	stat.uid = SUPER_USER;
270 	stat.gid = SUPER_USER;
271 
272 	service_node = add_inode(root, "service", NO_INDEX, &stat,
273 	    NR_SYS_PROCS, NULL);
274 
275 	if (service_node == NULL)
276 		panic("unable to create service node");
277 }
278 
279 /*
280  * A lookup request is being performed.  If it is in the service directory,
281  * update the tables.  We do this lazily, to reduce overhead.
282  */
283 void
284 service_lookup(struct inode * parent, clock_t now)
285 {
286 	static clock_t last_update = 0;
287 
288 	if (parent != service_node)
289 		return;
290 
291 	if (last_update != now) {
292 		service_update();
293 
294 		last_update = now;
295 	}
296 }
297 
298 /*
299  * A getdents request is being performed.  If it is in the service directory,
300  * update the tables.
301  */
302 void
303 service_getdents(struct inode * node)
304 {
305 
306 	if (node != service_node)
307 		return;
308 
309 	service_update();
310 }
311 
312 /*
313  * A read request is being performed.  If it is on a file in the service
314  * directory, process the read request.  We rely on the fact that any read
315  * call will have been preceded by a lookup, so its table entry has been
316  * updated very recently.
317  */
318 void
319 service_read(struct inode * node)
320 {
321 	struct inode *parent;
322 	index_t slot;
323 	struct rprocpub *rpub;
324 	struct rproc *rp;
325 
326 	if (get_parent_inode(node) != service_node)
327 		return;
328 
329 	slot = get_inode_index(node);
330 	rpub = &rproc.pub[slot];
331 	rp = &rproc.proc[slot];
332 
333 	/* TODO: add a large number of other fields! */
334 	buf_printf("filename: %s\n", rpub->proc_name);
335 	buf_printf("endpoint: %d\n", rpub->endpoint);
336 	buf_printf("pid:      %d\n", rp->r_pid);
337 	buf_printf("restarts: %d\n", rp->r_restarts);
338 	buf_printf("flags:    %s\n", service_get_flags(slot));
339 	buf_printf("policies: %s\n", service_get_policies(policies, slot));
340 }
341