xref: /minix3/minix/fs/procfs/service.c (revision 1facb0487c831b9ead49737c23d6f936b553009b)
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 = "restart" },
62                 { .label = "atl2", .policy_str = "restart" },
63                 { .label = "dec21140A", .policy_str = "restart" },
64                 { .label = "dp8390", .policy_str = "restart" },
65                 { .label = "dpeth", .policy_str = "restart" },
66                 { .label = "e1000", .policy_str = "restart" },
67                 { .label = "fxp", .policy_str = "restart" },
68                 { .label = "lance", .policy_str = "restart" },
69                 { .label = "lan8710a", .policy_str = "restart" },
70                 { .label = "rtl8139", .policy_str = "restart" },
71                 { .label = "rtl8169", .policy_str = "restart" },
72                 { .label = "uds", .policy_str = "reset" },
73                 { .label = "virtio_net", .policy_str = "restart" },
74 		/* power */
75                 { .label = "acpi", .policy_str = "" },
76                 { .label = "tps65217", .policy_str = "" },
77                 { .label = "tps65590", .policy_str = "" },
78 		/* printer */
79                 { .label = "printer", .policy_str = "restart" },
80 		/* sensors */
81                 { .label = "bmp085", .policy_str = "" },
82                 { .label = "sht21", .policy_str = "restart" },
83                 { .label = "tsl2550", .policy_str = "restart" },
84 		/* storage */
85                 { .label = "ahci", .policy_str = "reset" },
86                 { .label = "at_wini", .policy_str = "reset" },
87                 { .label = "fbd", .policy_str = "reset" },
88                 { .label = "filter", .policy_str = "reset" },
89                 { .label = "floppy", .policy_str = "reset" },
90                 { .label = "memory", .policy_str = "restart" },
91                 { .label = "mmc", .policy_str = "reset" },
92                 { .label = "virtio_blk", .policy_str = "reset" },
93                 { .label = "vnd", .policy_str = "reset" },
94 		/* system */
95                 { .label = "gpio", .policy_str = "restart" },
96                 { .label = "log", .policy_str = "reset" },
97                 { .label = "random", .policy_str = "restart" },
98 		/* tty */
99                 { .label = "pty", .policy_str = "restart" },
100                 { .label = "tty", .policy_str = "" },
101 		/* usb */
102                 { .label = "usbd", .policy_str = "" },
103                 { .label = "usb_hub", .policy_str = "" },
104                 { .label = "usb_storage", .policy_str = "" },
105 		/* video */
106                 { .label = "fb", .policy_str = "" },
107                 { .label = "tda19988", .policy_str = "" },
108 		/* vmm_guest */
109                 { .label = "vbox", .policy_str = "" },
110 		/* fs */
111                 { .label = "ext2", .policy_str = "" },
112                 { .label = "hgfs", .policy_str = "" },
113                 { .label = "isofs", .policy_str = "" },
114                 { .label = "mfs", .policy_str = "" },
115                 { .label = "pfs", .policy_str = "" },
116                 { .label = "procfs", .policy_str = "" },
117 		{ .label = "ptyfs", .policy_str = "" },
118                 { .label = "vbfs", .policy_str = "" },
119 		/* net */
120                 { .label = "inet", .policy_str = "reset" },
121                 { .label = "lwip", .policy_str = "" },
122 		/* servers */
123                 { .label = "devman", .policy_str = "" },
124                 { .label = "ds", .policy_str = "" },
125                 { .label = "input", .policy_str = "reset" },
126                 { .label = "ipc", .policy_str = "restart" },
127                 { .label = "is", .policy_str = "restart" },
128                 { .label = "pm", .policy_str = "" },
129                 { .label = "rs", .policy_str = "" },
130                 { .label = "sched", .policy_str = "" },
131                 { .label = "vfs", .policy_str = "" },
132                 { .label = "vm", .policy_str = "" },
133 		//{ .label = "", .policy_str = "" },
134 	};
135 
136 	/* Find the related policy, based on the file name of the service. */
137 	ref_label = strrchr(rproc.pub[slot].proc_name, '/');
138 	if (NULL == ref_label)
139 		ref_label = rproc.pub[slot].proc_name;
140 
141 	memset(pol[slot].formatted, 0, MAX_POL_FORMAT_SZ);
142 	for(pos = 0; pos < (sizeof(def_pol) / sizeof(def_pol[0])); pos++) {
143 		if (0 == strcmp(ref_label, def_pol[pos].label)) {
144 			(void)strncpy(pol[slot].formatted,
145 			    def_pol[pos].policy_str, MAX_POL_FORMAT_SZ);
146 			pol[slot].formatted[MAX_POL_FORMAT_SZ-1] = '\0';
147 			break;
148 		}
149 	}
150 #else
151 	/* Should do something sensible, based on flags from RS/SEF. */
152 #endif
153 
154 	return pol[slot].formatted;
155 }
156 
157 /*
158  * Return whether a slot is in use and active.  The purpose of this check is
159  * to ensure that after eliminating all slots that do not pass this check, we
160  * are left with a set of live services each with a unique label.
161  */
162 static int
163 service_active(index_t slot)
164 {
165 
166 	return ((rproc.proc[slot].r_flags & (RS_IN_USE | RS_ACTIVE)) ==
167 	    (RS_IN_USE | RS_ACTIVE));
168 }
169 
170 /*
171  * Update the contents of the service directory, by first updating the RS
172  * tables and then updating the directory contents.
173  */
174 static void
175 service_update(void)
176 {
177 	struct inode *node;
178 	struct inode_stat stat;
179 	index_t slot;
180 	static int warned = FALSE;
181 	int r;
182 
183 	/* There is not much we can do if this call fails. */
184 	r = getsysinfo(RS_PROC_NR, SI_PROCALL_TAB, &rproc, sizeof(rproc));
185 	if (r != OK && !warned) {
186 		printf("PROCFS: unable to obtain RS tables (%d)\n", r);
187 		warned = TRUE;
188 	}
189 
190 	/*
191 	 * As with PIDs, we make two passes.  Delete first, then add.  This
192 	 * prevents problems in the hypothetical case that between updates, one
193 	 * slot ends up with the label name of a previous, different slot.
194 	 */
195 	for (slot = 0; slot < NR_SYS_PROCS; slot++) {
196 		if ((node = get_inode_by_index(service_node, slot)) == NULL)
197 			continue;
198 
199 		/*
200 		 * If the slot is no longer in use, or the label name does not
201 		 * match, the node must be deleted.
202 		 */
203 		if (!service_active(slot) ||
204 		    strcmp(get_inode_name(node), rproc.pub[slot].label))
205 			delete_inode(node);
206 	}
207 
208 	memset(&stat, 0, sizeof(stat));
209 	stat.mode = REG_ALL_MODE;
210 	stat.uid = SUPER_USER;
211 	stat.gid = SUPER_USER;
212 
213 	for (slot = 0; slot < NR_SYS_PROCS; slot++) {
214 		if (!service_active(slot) ||
215 		    get_inode_by_index(service_node, slot) != NULL)
216 			continue;
217 
218 		node = add_inode(service_node, rproc.pub[slot].label, slot,
219 		    &stat, (index_t)0, (cbdata_t)slot);
220 
221 		if (node == NULL)
222 			out_of_inodes();
223 	}
224 }
225 
226 /*
227  * Initialize the service directory.
228  */
229 void
230 service_init(void)
231 {
232 	struct inode *root, *node;
233 	struct inode_stat stat;
234 
235 	root = get_root_inode();
236 
237 	memset(&stat, 0, sizeof(stat));
238 	stat.mode = DIR_ALL_MODE;
239 	stat.uid = SUPER_USER;
240 	stat.gid = SUPER_USER;
241 
242 	service_node = add_inode(root, "service", NO_INDEX, &stat,
243 	    NR_SYS_PROCS, NULL);
244 
245 	if (service_node == NULL)
246 		panic("unable to create service node");
247 }
248 
249 /*
250  * A lookup request is being performed.  If it is in the service directory,
251  * update the tables.  We do this lazily, to reduce overhead.
252  */
253 void
254 service_lookup(struct inode * parent, clock_t now)
255 {
256 	static clock_t last_update = 0;
257 
258 	if (parent != service_node)
259 		return;
260 
261 	if (last_update != now) {
262 		service_update();
263 
264 		last_update = now;
265 	}
266 }
267 
268 /*
269  * A getdents request is being performed.  If it is in the service directory,
270  * update the tables.
271  */
272 void
273 service_getdents(struct inode * node)
274 {
275 
276 	if (node != service_node)
277 		return;
278 
279 	service_update();
280 }
281 
282 /*
283  * A read request is being performed.  If it is on a file in the service
284  * directory, process the read request.  We rely on the fact that any read
285  * call will have been preceded by a lookup, so its table entry has been
286  * updated very recently.
287  */
288 void
289 service_read(struct inode * node)
290 {
291 	struct inode *parent;
292 	index_t slot;
293 	struct rprocpub *rpub;
294 	struct rproc *rp;
295 
296 	if (get_parent_inode(node) != service_node)
297 		return;
298 
299 	slot = get_inode_index(node);
300 	rpub = &rproc.pub[slot];
301 	rp = &rproc.proc[slot];
302 
303 	/* TODO: add a large number of other fields! */
304 	buf_printf("filename: %s\n", rpub->proc_name);
305 	buf_printf("endpoint: %d\n", rpub->endpoint);
306 	buf_printf("pid:      %d\n", rp->r_pid);
307 	buf_printf("restarts: %d\n", rp->r_restarts);
308 	buf_printf("policies: %s\n", service_get_policies(policies, slot));
309 }
310