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