1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sys/types.h>
30 #include <sys/ioctl.h>
34 #include <portals/api-support.h>
35 #include <portals/ptlctl.h>
44 static ioc_handler_t do_ioctl; /* forward ref */
45 static ioc_handler_t *current_ioc_handler = &do_ioctl;
48 const char * dev_name;
52 static struct ioc_dev ioc_dev_list[10];
63 set_ioc_handler (ioc_handler_t *handler)
66 current_ioc_handler = do_ioctl;
68 current_ioc_handler = handler;
72 open_ioc_dev(int dev_id)
74 const char * dev_name;
76 if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list))
79 dev_name = ioc_dev_list[dev_id].dev_name;
80 if (dev_name == NULL) {
81 fprintf(stderr, "unknown device id: %d\n", dev_id);
85 if (ioc_dev_list[dev_id].dev_fd < 0) {
86 int fd = open(dev_name, O_RDWR);
89 fprintf(stderr, "opening %s failed: %s\n"
90 "hint: the kernel modules may not be loaded\n",
91 dev_name, strerror(errno));
94 ioc_dev_list[dev_id].dev_fd = fd;
97 return ioc_dev_list[dev_id].dev_fd;
102 do_ioctl(int dev_id, int opc, void *buf)
106 fd = open_ioc_dev(dev_id);
110 rc = ioctl(fd, opc, buf);
120 if (!dump_filename) {
121 fprintf(stderr, "no dump filename\n");
123 fp = fopen(dump_filename, "a");
128 * The dump file should start with a description of which devices are
129 * used, but for now it will assumed whatever app reads the file will
130 * know what to do. */
132 dump(int dev_id, int opc, void *buf)
135 struct dump_hdr dump_hdr;
136 struct portal_ioctl_hdr * ioc_hdr = (struct portal_ioctl_hdr *) buf;
139 printf("dumping opc %x to %s\n", opc, dump_filename);
142 dump_hdr.magic = 0xdeadbeef;
143 dump_hdr.dev_id = dev_id;
146 fp = get_dump_file();
148 fprintf(stderr, "%s: %s\n", dump_filename,
153 rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp);
155 rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp);
158 fprintf(stderr, "%s: %s\n", dump_filename,
166 /* register a device to send ioctls to. */
168 register_ioc_dev(int dev_id, const char * dev_name)
171 if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list))
174 unregister_ioc_dev(dev_id);
176 ioc_dev_list[dev_id].dev_name = dev_name;
177 ioc_dev_list[dev_id].dev_fd = -1;
183 unregister_ioc_dev(int dev_id)
186 if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list))
188 if (ioc_dev_list[dev_id].dev_name != NULL &&
189 ioc_dev_list[dev_id].dev_fd >= 0)
190 close(ioc_dev_list[dev_id].dev_fd);
192 ioc_dev_list[dev_id].dev_name = NULL;
193 ioc_dev_list[dev_id].dev_fd = -1;
196 /* If this file is set, then all ioctl buffers will be
197 appended to the file. */
199 set_ioctl_dump(char * file)
204 dump_filename = strdup(file);
205 if (dump_filename == NULL)
208 set_ioc_handler(&dump);
213 l_ioctl(int dev_id, int opc, void *buf)
215 return current_ioc_handler(dev_id, opc, buf);
218 /* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
219 * in the file. For example:
221 * parse_dump("lctl.dump", l_ioctl);
223 * Note: if using l_ioctl, then you also need to register_ioc_dev() for
224 * each device used in the dump.
227 parse_dump(char * dump_file, int (*ioc_func)(int dev_id, int opc, void *))
231 char *start, *buf, *end;
240 fd = syscall(SYS_open, dump_file, O_RDONLY);
242 fprintf(stderr, "couldn't open %s: %s\n", dump_file,
248 # define __SYS_fstat__ SYS_fstat
250 # define __SYS_fstat__ SYS_fstat64
252 if (syscall(__SYS_fstat__, fd, &st)) {
253 perror("stat fails");
257 if (st.st_size < 1) {
258 fprintf(stderr, "KML is empty\n");
262 start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
263 end = start + st.st_size;
265 if (start == MAP_FAILED) {
266 fprintf(stderr, "can't create file mapping\n");
270 fd = CreateFile(dump_file, GENERIC_READ, FILE_SHARE_READ, NULL,
271 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
272 size = GetFileSize(fd, NULL);
274 fprintf(stderr, "KML is empty\n");
278 hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL);
279 start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
283 fprintf(stderr, "can't create file mapping\n");
286 #endif /* __CYGWIN__ */
289 struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
290 struct portal_ioctl_hdr * data;
296 data = (struct portal_ioctl_hdr *) (buf + sizeof(*dump_hdr));
297 if (buf + data->ioc_len > end ) {
298 fprintf(stderr, "dump file overflow, %p + %d > %p\n", buf,
303 printf ("dump_hdr: %lx data: %lx\n",
304 (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
306 printf("%d: opcode %x len: %d ver: %x ", line, dump_hdr->opc,
307 data->ioc_len, data->ioc_version);
310 memcpy(tmp, data, data->ioc_len);
312 rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
314 printf("failed: %d\n", rc);
318 buf += data->ioc_len + sizeof(*dump_hdr);
322 munmap(start, end - start);
324 UnmapViewOfFile(start);
332 jt_ioc_dump(int argc, char **argv)
335 fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
338 printf("setting dumpfile to: %s\n", argv[1]);
340 set_ioctl_dump(argv[1]);