2 * Copyright (c) 2002-2004 Marko Zec <zec@fer.hr>
3 * Copyright (c) 2009 University of Zagreb
4 * Copyright (c) 2009 FreeBSD Foundation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/vimage.h>
42 vi_print(struct vi_req *vi_req)
45 printf("\"%s\":\n", vi_req->vi_name);
46 printf(" %d sockets, %d ifnets, %d processes\n",
47 vi_req->vi_sock_count, vi_req->vi_if_count, vi_req->vi_proc_count);
51 main(int argc, char **argv)
55 int cmd = VI_SWITCHTO;
58 s = socket(AF_INET, SOCK_DGRAM, 0);
62 bzero(&vi_req, sizeof(vi_req));
63 strcpy(vi_req.vi_name, "."); /* . = this vimage. */
68 if (argc == 2 && strcmp(argv[1], "-l") == 0)
71 if (argc == 2 && strcmp(argv[1], "-lr") == 0)
72 cmd = VI_GETNEXT_RECURSE;
75 strcpy(vi_req.vi_name, argv[2]);
76 if (strcmp(argv[1], "-l") == 0)
78 if (strcmp(argv[1], "-c") == 0)
80 if (strcmp(argv[1], "-d") == 0)
85 strcpy(vi_req.vi_name, argv[2]);
86 if (strcmp(argv[1], "-c") == 0)
88 if (strcmp(argv[1], "-i") == 0)
92 vi_req.vi_api_cookie = VI_API_COOKIE;
93 vi_req.vi_req_action = cmd;
97 if (ioctl(s, SIOCGPVIMAGE, (caddr_t)&vi_req) < 0)
100 printf("%s\n", vi_req.vi_name);
106 case VI_GETNEXT_RECURSE:
107 vi_req.vi_req_action = VI_GET;
108 if (ioctl(s, SIOCGPVIMAGE, (caddr_t)&vi_req) < 0)
111 vi_req.vi_req_action = VI_GETNEXT_RECURSE;
112 while (ioctl(s, SIOCGPVIMAGE, (caddr_t)&vi_req) == 0) {
114 vi_req.vi_req_action = cmd;
119 strncpy(vi_req.vi_if_xname, argv[3],
120 sizeof(vi_req.vi_if_xname));
121 if (ioctl(s, SIOCSIFVIMAGE, (caddr_t)&vi_req) < 0)
123 printf("%s@%s\n", vi_req.vi_if_xname, vi_req.vi_name);
127 if (ioctl(s, SIOCSPVIMAGE, (caddr_t)&vi_req) < 0)
132 strcpy(vi_req.vi_name, argv[1]);
133 if (ioctl(s, SIOCSPVIMAGE, (caddr_t)&vi_req) < 0)
136 vi_req.vi_req_action = VI_GET;
137 strcpy(vi_req.vi_name, ".");
138 if (ioctl(s, SIOCGPVIMAGE, (caddr_t)&vi_req) < 0) {
139 printf("XXX this should have not happened!\n");
145 printf("Switched to vimage %s\n", argv[1]);
146 if ((shell = getenv("SHELL")) == NULL)
147 execlp("/bin/sh", argv[0], NULL);
149 execlp(shell, argv[0], NULL);
151 execvp(argv[2], &argv[2]);
155 if (ioctl(s, SIOCSPVIMAGE, (caddr_t)&vi_req) < 0)
160 fprintf(stderr, "usage: %s [-cdilr] vi_name [args]\n",