xref: /netbsd-src/share/examples/refuse/pcifs/pcifs.c (revision cfb41894f86927de5bfbcbb7328670af96fd1aa7)
1 /*
2  * Copyright � 2007 Alistair Crooks.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote
13  *    products derived from this software without specific prior written
14  *    permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include <sys/types.h>
29 
30 #include <err.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <fuse.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include "virtdir.h"
40 #include "defs.h"
41 
42 static int		 verbose; /* how chatty are we? */
43 
44 static virtdir_t	 pci;
45 
46 
47 /* perform the stat operation */
48 /* if this is the root, then just synthesise the data */
49 /* otherwise, retrieve the data, and be sure to fill in the size */
50 static int
pcifs_getattr(const char * path,struct stat * st)51 pcifs_getattr(const char *path, struct stat *st)
52 {
53 	virt_dirent_t	*ep;
54 
55 	if (strcmp(path, "/") == 0) {
56 		(void) memset(st, 0x0, sizeof(*st));
57 		st->st_mode = S_IFDIR | 0755;
58 		st->st_nlink = 2;
59 		return 0;
60 	}
61 	if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
62 		return -ENOENT;
63 	}
64 	switch(ep->type) {
65 	case 'f':
66 		break;
67 	case 'd':
68 		(void) memcpy(st, &pci.dir, sizeof(*st));
69 		break;
70 	case 'l':
71 		(void) memcpy(st, &pci.lnk, sizeof(*st));
72 		st->st_size = ep->tgtlen;
73 		st->st_mode = S_IFLNK | 0644;
74 		break;
75 	}
76 	return 0;
77 }
78 
79 /* readdir operation */
80 static int
pcifs_readdir(const char * path,void * buf,fuse_fill_dir_t filler,off_t offset,struct fuse_file_info * fi)81 pcifs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
82 	      off_t offset, struct fuse_file_info * fi)
83 {
84 	virt_dirent_t	*dp;
85 	VIRTDIR		*dirp;
86 
87 	if ((dirp = openvirtdir(&pci, path)) == NULL) {
88 		return 0;
89 	}
90 	filler(buf, ".", NULL, 0);
91 	filler(buf, "..", NULL, 0);
92 	while ((dp = readvirtdir(dirp)) != NULL) {
93 		filler(buf, dp->d_name, NULL, 0);
94 	}
95 	closevirtdir(dirp);
96 	return 0;
97 }
98 
99 /* open the file in the file system */
100 static int
pcifs_open(const char * path,struct fuse_file_info * fi)101 pcifs_open(const char *path, struct fuse_file_info * fi)
102 {
103 	return 0;
104 }
105 
106 /* read the file's contents in the file system */
107 static int
pcifs_read(const char * path,char * buf,size_t size,off_t offset,struct fuse_file_info * fi)108 pcifs_read(const char *path, char *buf, size_t size, off_t offset,
109 	   struct fuse_file_info * fi)
110 {
111 	return 0;
112 }
113 
114 /* fill in the statvfs struct */
115 static int
pcifs_statfs(const char * path,struct statvfs * st)116 pcifs_statfs(const char *path, struct statvfs *st)
117 {
118 	(void) memset(st, 0x0, sizeof(*st));
119 	return 0;
120 }
121 
122 /* read the symbolic link */
123 static int
pcifs_readlink(const char * path,char * buf,size_t size)124 pcifs_readlink(const char *path, char *buf, size_t size)
125 {
126 	virt_dirent_t	*ep;
127 
128 	if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
129 		return -ENOENT;
130 	}
131 	if (ep->tgt == NULL) {
132 		return -ENOENT;
133 	}
134 	(void) strlcpy(buf, ep->tgt, size);
135 	return 0;
136 }
137 
138 /* operations struct */
139 static struct fuse_operations pcifs_oper = {
140 	.getattr = pcifs_getattr,
141 	.readlink = pcifs_readlink,
142 	.readdir = pcifs_readdir,
143 	.open = pcifs_open,
144 	.read = pcifs_read,
145 	.statfs = pcifs_statfs
146 };
147 
148 /* build up a fuse_tree from the information in the database */
149 static int
build_tree(virtdir_t * tp,int bus)150 build_tree(virtdir_t *tp, int bus)
151 {
152 	struct stat	 dir;
153 	struct stat	 f;
154 	FILE		*pp;
155 	char		 name[MAXPATHLEN];
156 	char		 buf[BUFSIZ];
157 	char		*cp;
158 	int		 cc;
159 
160 	(void) stat(".", &dir);
161 	(void) memcpy(&f, &dir, sizeof(f));
162 	(void) snprintf(buf, sizeof(buf), "pcictl pci%d list", bus);
163 	f.st_mode = S_IFREG | 0644;
164 	if ((pp = popen(buf, "r")) == NULL) {
165 		return 0;
166 	}
167 	while (fgets(buf, sizeof(buf), pp) != NULL) {
168 		buf[strlen(buf) - 1] = 0x0;
169 		if ((cp = strchr(buf, ' ')) == NULL) {
170 			continue;
171 		}
172 		cc = snprintf(name, sizeof(name), "/%.*s", (int)(cp - buf), buf);
173 		virtdir_add(tp, name, cc, 'l', cp + 1, strlen(cp + 1));
174 		if (verbose) {
175 			printf("pcifs: adding symbolic link `%s' -> `%s'\n", name, cp + 1);
176 		}
177 	}
178 	(void) pclose(pp);
179 	return 1;
180 }
181 
182 int
main(int argc,char ** argv)183 main(int argc, char **argv)
184 {
185 	int	 bus;
186 	int	 i;
187 
188 	bus = 0;
189 	while ((i = getopt(argc, argv, "b:v")) != -1) {
190 		switch(i) {
191 		case 'b':
192 			bus = atoi(optarg);
193 			break;
194 		case 'v':
195 			verbose += 1;
196 			break;
197 		}
198 	}
199 	if (!build_tree(&pci, bus)) {
200 		exit(EXIT_FAILURE);
201 	}
202 	return fuse_main(argc, argv, &pcifs_oper, NULL);
203 }
204