1*d49e2683Sagc /*
2*d49e2683Sagc * Copyright � 2007 Alistair Crooks. All rights reserved.
3*d49e2683Sagc *
4*d49e2683Sagc * Redistribution and use in source and binary forms, with or without
5*d49e2683Sagc * modification, are permitted provided that the following conditions
6*d49e2683Sagc * are met:
7*d49e2683Sagc * 1. Redistributions of source code must retain the above copyright
8*d49e2683Sagc * notice, this list of conditions and the following disclaimer.
9*d49e2683Sagc * 2. Redistributions in binary form must reproduce the above copyright
10*d49e2683Sagc * notice, this list of conditions and the following disclaimer in the
11*d49e2683Sagc * documentation and/or other materials provided with the distribution.
12*d49e2683Sagc * 3. The name of the author may not be used to endorse or promote
13*d49e2683Sagc * products derived from this software without specific prior written
14*d49e2683Sagc * permission.
15*d49e2683Sagc *
16*d49e2683Sagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17*d49e2683Sagc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*d49e2683Sagc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*d49e2683Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20*d49e2683Sagc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*d49e2683Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22*d49e2683Sagc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*d49e2683Sagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24*d49e2683Sagc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25*d49e2683Sagc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26*d49e2683Sagc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*d49e2683Sagc */
28*d49e2683Sagc #include <sys/types.h>
29*d49e2683Sagc
30*d49e2683Sagc #include <err.h>
31*d49e2683Sagc #include <errno.h>
32*d49e2683Sagc #include <fcntl.h>
33*d49e2683Sagc #include <fuse.h>
34*d49e2683Sagc #include <regex.h>
35*d49e2683Sagc #include <stdio.h>
36*d49e2683Sagc #include <stdlib.h>
37*d49e2683Sagc #include <string.h>
38*d49e2683Sagc #include <unistd.h>
39*d49e2683Sagc
40*d49e2683Sagc #include "virtdir.h"
41*d49e2683Sagc #include "defs.h"
42*d49e2683Sagc
43*d49e2683Sagc static int verbose; /* how chatty are we? */
44*d49e2683Sagc
45*d49e2683Sagc static virtdir_t pci;
46*d49e2683Sagc
47*d49e2683Sagc /* a device node describes the device that is printed in the dmesg */
48*d49e2683Sagc typedef struct devicenode_t {
49*d49e2683Sagc char *dev; /* device name */
50*d49e2683Sagc int devlen; /* length of name */
51*d49e2683Sagc char *parent; /* its parent device name */
52*d49e2683Sagc int parentlen; /* length of parent name */
53*d49e2683Sagc char *descr; /* description of the device itself */
54*d49e2683Sagc int descrlen; /* length of description */
55*d49e2683Sagc int p; /* device node subscript of parent */
56*d49e2683Sagc int dir; /* nonzero if this is a directory */
57*d49e2683Sagc } devicenode_t;
58*d49e2683Sagc
59*d49e2683Sagc DEFINE_ARRAY(devices_t, devicenode_t);
60*d49e2683Sagc
61*d49e2683Sagc static devices_t devices;
62*d49e2683Sagc
63*d49e2683Sagc
64*d49e2683Sagc /* perform the stat operation */
65*d49e2683Sagc /* if this is the root, then just synthesise the data */
66*d49e2683Sagc static int
dmesgfs_getattr(const char * path,struct stat * st)67*d49e2683Sagc dmesgfs_getattr(const char *path, struct stat *st)
68*d49e2683Sagc {
69*d49e2683Sagc virt_dirent_t *ep;
70*d49e2683Sagc
71*d49e2683Sagc if (strcmp(path, "/") == 0) {
72*d49e2683Sagc (void) memset(st, 0x0, sizeof(*st));
73*d49e2683Sagc st->st_mode = (S_IFDIR | 0755);
74*d49e2683Sagc st->st_nlink = 2;
75*d49e2683Sagc return 0;
76*d49e2683Sagc }
77*d49e2683Sagc if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
78*d49e2683Sagc return -ENOENT;
79*d49e2683Sagc }
80*d49e2683Sagc switch(ep->type) {
81*d49e2683Sagc case 'f':
82*d49e2683Sagc (void) memcpy(st, &pci.file, sizeof(*st));
83*d49e2683Sagc st->st_size = ep->tgtlen;
84*d49e2683Sagc st->st_mode = S_IFREG | 0644;
85*d49e2683Sagc break;
86*d49e2683Sagc case 'd':
87*d49e2683Sagc (void) memcpy(st, &pci.dir, sizeof(*st));
88*d49e2683Sagc break;
89*d49e2683Sagc case 'l':
90*d49e2683Sagc (void) memcpy(st, &pci.lnk, sizeof(*st));
91*d49e2683Sagc st->st_size = ep->tgtlen;
92*d49e2683Sagc st->st_mode = S_IFLNK | 0755;
93*d49e2683Sagc break;
94*d49e2683Sagc }
95*d49e2683Sagc st->st_ino = virtdir_offset(&pci, ep) + 10;
96*d49e2683Sagc return 0;
97*d49e2683Sagc }
98*d49e2683Sagc
99*d49e2683Sagc /* readdir operation */
100*d49e2683Sagc static int
dmesgfs_readdir(const char * path,void * buf,fuse_fill_dir_t filler,off_t offset,struct fuse_file_info * fi)101*d49e2683Sagc dmesgfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
102*d49e2683Sagc off_t offset, struct fuse_file_info * fi)
103*d49e2683Sagc {
104*d49e2683Sagc static VIRTDIR *dirp;
105*d49e2683Sagc virt_dirent_t *dp;
106*d49e2683Sagc
107*d49e2683Sagc if (offset == 0) {
108*d49e2683Sagc if ((dirp = openvirtdir(&pci, path)) == NULL) {
109*d49e2683Sagc return 0;
110*d49e2683Sagc }
111*d49e2683Sagc filler(buf, ".", NULL, 0);
112*d49e2683Sagc filler(buf, "..", NULL, 0);
113*d49e2683Sagc }
114*d49e2683Sagc while ((dp = readvirtdir(dirp)) != NULL) {
115*d49e2683Sagc if (filler(buf, dp->d_name, NULL, 0) != 0) {
116*d49e2683Sagc return 0;
117*d49e2683Sagc }
118*d49e2683Sagc }
119*d49e2683Sagc closevirtdir(dirp);
120*d49e2683Sagc dirp = NULL;
121*d49e2683Sagc return 0;
122*d49e2683Sagc }
123*d49e2683Sagc
124*d49e2683Sagc /* open the file in the file system */
125*d49e2683Sagc static int
dmesgfs_open(const char * path,struct fuse_file_info * fi)126*d49e2683Sagc dmesgfs_open(const char *path, struct fuse_file_info * fi)
127*d49e2683Sagc {
128*d49e2683Sagc return 0;
129*d49e2683Sagc }
130*d49e2683Sagc
131*d49e2683Sagc /* read the file's contents in the file system */
132*d49e2683Sagc static int
dmesgfs_read(const char * path,char * buf,size_t size,off_t offset,struct fuse_file_info * fi)133*d49e2683Sagc dmesgfs_read(const char *path, char *buf, size_t size, off_t offset,
134*d49e2683Sagc struct fuse_file_info * fi)
135*d49e2683Sagc {
136*d49e2683Sagc virt_dirent_t *ep;
137*d49e2683Sagc
138*d49e2683Sagc if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
139*d49e2683Sagc return -ENOENT;
140*d49e2683Sagc }
141*d49e2683Sagc if (ep->tgt == NULL) {
142*d49e2683Sagc return -ENOENT;
143*d49e2683Sagc }
144*d49e2683Sagc (void) memcpy(buf, &ep->tgt[offset], size);
145*d49e2683Sagc return ep->tgtlen;
146*d49e2683Sagc }
147*d49e2683Sagc
148*d49e2683Sagc /* fill in the statvfs struct */
149*d49e2683Sagc static int
dmesgfs_statfs(const char * path,struct statvfs * st)150*d49e2683Sagc dmesgfs_statfs(const char *path, struct statvfs *st)
151*d49e2683Sagc {
152*d49e2683Sagc (void) memset(st, 0x0, sizeof(*st));
153*d49e2683Sagc return 0;
154*d49e2683Sagc }
155*d49e2683Sagc
156*d49e2683Sagc /* read the symbolic link */
157*d49e2683Sagc static int
dmesgfs_readlink(const char * path,char * buf,size_t size)158*d49e2683Sagc dmesgfs_readlink(const char *path, char *buf, size_t size)
159*d49e2683Sagc {
160*d49e2683Sagc virt_dirent_t *ep;
161*d49e2683Sagc
162*d49e2683Sagc if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
163*d49e2683Sagc return -ENOENT;
164*d49e2683Sagc }
165*d49e2683Sagc if (ep->tgt == NULL) {
166*d49e2683Sagc return -ENOENT;
167*d49e2683Sagc }
168*d49e2683Sagc (void) strlcpy(buf, ep->tgt, size);
169*d49e2683Sagc return 0;
170*d49e2683Sagc }
171*d49e2683Sagc
172*d49e2683Sagc /* operations struct */
173*d49e2683Sagc static struct fuse_operations dmesgfs_oper = {
174*d49e2683Sagc .getattr = dmesgfs_getattr,
175*d49e2683Sagc .readlink = dmesgfs_readlink,
176*d49e2683Sagc .readdir = dmesgfs_readdir,
177*d49e2683Sagc .open = dmesgfs_open,
178*d49e2683Sagc .read = dmesgfs_read,
179*d49e2683Sagc .statfs = dmesgfs_statfs
180*d49e2683Sagc };
181*d49e2683Sagc
182*d49e2683Sagc /* save `n' chars of `s' in malloc'd memory */
183*d49e2683Sagc static char *
strnsave(const char * s,int n)184*d49e2683Sagc strnsave(const char *s, int n)
185*d49e2683Sagc {
186*d49e2683Sagc char *cp;
187*d49e2683Sagc
188*d49e2683Sagc if (n < 0) {
189*d49e2683Sagc n = strlen(s);
190*d49e2683Sagc }
191*d49e2683Sagc NEWARRAY(char, cp, n + 1, "strnsave", exit(EXIT_FAILURE));
192*d49e2683Sagc (void) memcpy(cp, s, n);
193*d49e2683Sagc cp[n] = 0x0;
194*d49e2683Sagc return cp;
195*d49e2683Sagc }
196*d49e2683Sagc
197*d49e2683Sagc /* find a device in the device node tree */
198*d49e2683Sagc static int
finddev(const char * s,int len,int updir)199*d49e2683Sagc finddev(const char *s, int len, int updir)
200*d49e2683Sagc {
201*d49e2683Sagc int i;
202*d49e2683Sagc
203*d49e2683Sagc for (i = 0 ; i < devices.c ; i++) {
204*d49e2683Sagc if (strncmp(devices.v[i].dev, s, len) == 0 &&
205*d49e2683Sagc devices.v[i].devlen == len) {
206*d49e2683Sagc if (updir) {
207*d49e2683Sagc devices.v[i].dir = 1;
208*d49e2683Sagc }
209*d49e2683Sagc return i;
210*d49e2683Sagc }
211*d49e2683Sagc }
212*d49e2683Sagc return -1;
213*d49e2683Sagc }
214*d49e2683Sagc
215*d49e2683Sagc /* add a device to the device node tree */
216*d49e2683Sagc static void
add_dev(const char * dev,int len,const char * parent,int parentlen,const char * descr,int descrlen)217*d49e2683Sagc add_dev(const char *dev, int len, const char *parent, int parentlen, const char *descr, int descrlen)
218*d49e2683Sagc {
219*d49e2683Sagc int d;
220*d49e2683Sagc
221*d49e2683Sagc if ((d = finddev(dev, len, 0)) < 0) {
222*d49e2683Sagc ALLOC(devicenode_t, devices.v, devices.size, devices.c, 10, 10, "add_dev", exit(EXIT_FAILURE));
223*d49e2683Sagc devices.v[devices.c].dev = strnsave(dev, len);
224*d49e2683Sagc devices.v[devices.c].devlen = len;
225*d49e2683Sagc devices.v[devices.c].parent = strnsave(parent, parentlen);
226*d49e2683Sagc devices.v[devices.c].parentlen = parentlen;
227*d49e2683Sagc devices.v[devices.c].descr = strnsave(descr, descrlen);
228*d49e2683Sagc devices.v[devices.c].descrlen = descrlen;
229*d49e2683Sagc devices.c += 1;
230*d49e2683Sagc }
231*d49e2683Sagc }
232*d49e2683Sagc
233*d49e2683Sagc /* print the parent device pathname */
234*d49e2683Sagc static int
pparent(int d,char * buf,size_t size)235*d49e2683Sagc pparent(int d, char *buf, size_t size)
236*d49e2683Sagc {
237*d49e2683Sagc int cc;
238*d49e2683Sagc
239*d49e2683Sagc if (d != 0) {
240*d49e2683Sagc cc = pparent(devices.v[d].p, buf, size);
241*d49e2683Sagc size -= cc;
242*d49e2683Sagc }
243*d49e2683Sagc (void) strlcat(buf, "/", size);
244*d49e2683Sagc (void) strlcat(buf, devices.v[d].dev, size - 1);
245*d49e2683Sagc return devices.v[d].devlen + 1;
246*d49e2683Sagc }
247*d49e2683Sagc
248*d49e2683Sagc #define NEXUS_DESCRIPTION "Nexus - the root of everything"
249*d49e2683Sagc
250*d49e2683Sagc /* build up a fuse_tree from the information in the database */
251*d49e2683Sagc static int
build_tree(virtdir_t * tp,const char * nexus,char type)252*d49e2683Sagc build_tree(virtdir_t *tp, const char *nexus, char type)
253*d49e2683Sagc {
254*d49e2683Sagc struct stat dir;
255*d49e2683Sagc struct stat f;
256*d49e2683Sagc regmatch_t matchv[10];
257*d49e2683Sagc regex_t r;
258*d49e2683Sagc FILE *pp;
259*d49e2683Sagc char buf[BUFSIZ];
260*d49e2683Sagc int i;
261*d49e2683Sagc int p;
262*d49e2683Sagc
263*d49e2683Sagc (void) stat(".", &dir);
264*d49e2683Sagc (void) memcpy(&f, &dir, sizeof(f));
265*d49e2683Sagc f.st_mode = S_IFREG | 0644;
266*d49e2683Sagc if (regcomp(&r, "^([a-z0-9]+) at ([a-z0-9]+)(.*)", REG_EXTENDED) != 0) {
267*d49e2683Sagc warn("can't compile regular expression\n");
268*d49e2683Sagc return 0;
269*d49e2683Sagc }
270*d49e2683Sagc if ((pp = popen("dmesg", "r")) == NULL) {
271*d49e2683Sagc return 0;
272*d49e2683Sagc }
273*d49e2683Sagc add_dev(nexus, strlen(nexus), nexus, strlen(nexus), NEXUS_DESCRIPTION,
274*d49e2683Sagc strlen(NEXUS_DESCRIPTION));
275*d49e2683Sagc while (fgets(buf, sizeof(buf), pp) != NULL) {
276*d49e2683Sagc if (type == 'l') {
277*d49e2683Sagc buf[strlen(buf) - 1] = 0x0;
278*d49e2683Sagc }
279*d49e2683Sagc if (regexec(&r, buf, 10, matchv, 0) == 0) {
280*d49e2683Sagc add_dev(&buf[matchv[1].rm_so],
281*d49e2683Sagc matchv[1].rm_eo - matchv[1].rm_so,
282*d49e2683Sagc &buf[matchv[2].rm_so],
283*d49e2683Sagc matchv[2].rm_eo - matchv[2].rm_so,
284*d49e2683Sagc buf,
285*d49e2683Sagc matchv[0].rm_eo - matchv[0].rm_so);
286*d49e2683Sagc }
287*d49e2683Sagc }
288*d49e2683Sagc printf("%d devices\n", devices.c);
289*d49e2683Sagc (void) pclose(pp);
290*d49e2683Sagc for (i = 0 ; i < devices.c ; i++) {
291*d49e2683Sagc if ((p = finddev(devices.v[i].parent, devices.v[i].parentlen, 1)) < 0) {
292*d49e2683Sagc warn("No parent device for %s\n", devices.v[i].dev);
293*d49e2683Sagc }
294*d49e2683Sagc devices.v[i].p = p;
295*d49e2683Sagc }
296*d49e2683Sagc for (i = 0 ; i < devices.c ; i++) {
297*d49e2683Sagc pparent(i, buf, sizeof(buf));
298*d49e2683Sagc virtdir_add(tp, buf, strlen(buf),
299*d49e2683Sagc (devices.v[i].dir) ? 'd' : type,
300*d49e2683Sagc devices.v[i].descr, devices.v[i].descrlen);
301*d49e2683Sagc if (devices.v[i].dir) {
302*d49e2683Sagc (void) strlcat(buf, "/", sizeof(buf));
303*d49e2683Sagc (void) strlcat(buf, "Description", sizeof(buf));
304*d49e2683Sagc virtdir_add(tp, buf, strlen(buf), type,
305*d49e2683Sagc devices.v[i].descr, devices.v[i].descrlen);
306*d49e2683Sagc }
307*d49e2683Sagc if (verbose) {
308*d49e2683Sagc printf("dmesgfs: adding %s `%s' -> `%s'\n",
309*d49e2683Sagc (type == 'l') ? "symbolic link" : "file",
310*d49e2683Sagc buf, devices.v[i].descr);
311*d49e2683Sagc }
312*d49e2683Sagc buf[0] = 0x0;
313*d49e2683Sagc }
314*d49e2683Sagc return 1;
315*d49e2683Sagc }
316*d49e2683Sagc
317*d49e2683Sagc int
main(int argc,char ** argv)318*d49e2683Sagc main(int argc, char **argv)
319*d49e2683Sagc {
320*d49e2683Sagc char *nexus;
321*d49e2683Sagc char type;
322*d49e2683Sagc int i;
323*d49e2683Sagc
324*d49e2683Sagc nexus = NULL;
325*d49e2683Sagc type = 'l';
326*d49e2683Sagc while ((i = getopt(argc, argv, "fln:v")) != -1) {
327*d49e2683Sagc switch(i) {
328*d49e2683Sagc case 'f':
329*d49e2683Sagc type = 'f';
330*d49e2683Sagc break;
331*d49e2683Sagc case 'l':
332*d49e2683Sagc type = 'l';
333*d49e2683Sagc break;
334*d49e2683Sagc case 'n':
335*d49e2683Sagc nexus = optarg;
336*d49e2683Sagc break;
337*d49e2683Sagc case 'v':
338*d49e2683Sagc verbose += 1;
339*d49e2683Sagc break;
340*d49e2683Sagc }
341*d49e2683Sagc }
342*d49e2683Sagc if (!build_tree(&pci, (nexus) ? nexus : "mainbus0", type)) {
343*d49e2683Sagc exit(EXIT_FAILURE);
344*d49e2683Sagc }
345*d49e2683Sagc return fuse_main(argc, argv, &dmesgfs_oper, NULL);
346*d49e2683Sagc }
347