vimage.c revision 193583
1/* 2 * Copyright (c) 2002-2004 Marko Zec <zec@fer.hr> 3 * Copyright (c) 2009 University of Zagreb 4 * Copyright (c) 2009 FreeBSD Foundation 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/tools/tools/vimage/vimage.c 193583 2009-06-06 12:50:10Z zec $ 28 */ 29 30#include <sys/types.h> 31#include <sys/ioctl.h> 32#include <sys/socket.h> 33#include <sys/vimage.h> 34 35#include <errno.h> 36#include <stdio.h> 37#include <stdlib.h> 38#include <string.h> 39#include <unistd.h> 40 41void 42vi_print(struct vi_req *vi_req) 43{ 44 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); 48} 49 50int 51main(int argc, char **argv) 52{ 53 int s; 54 char *shell; 55 int cmd = VI_SWITCHTO; 56 struct vi_req vi_req; 57 58 s = socket(AF_INET, SOCK_DGRAM, 0); 59 if (s == -1) 60 goto abort; 61 62 bzero(&vi_req, sizeof(vi_req)); 63 strcpy(vi_req.vi_name, "."); /* . = this vimage. */ 64 65 if (argc == 1) 66 cmd = VI_GET; 67 68 if (argc == 2 && strcmp(argv[1], "-l") == 0) 69 cmd = VI_GETNEXT; 70 71 if (argc == 2 && strcmp(argv[1], "-lr") == 0) 72 cmd = VI_GETNEXT_RECURSE; 73 74 if (argc == 3) { 75 strcpy(vi_req.vi_name, argv[2]); 76 if (strcmp(argv[1], "-l") == 0) 77 cmd = VI_GET; 78 if (strcmp(argv[1], "-c") == 0) 79 cmd = VI_CREATE; 80 if (strcmp(argv[1], "-d") == 0) 81 cmd = VI_DESTROY; 82 } 83 84 if (argc >= 3) { 85 strcpy(vi_req.vi_name, argv[2]); 86 if (strcmp(argv[1], "-c") == 0) 87 cmd = VI_CREATE; 88 if (strcmp(argv[1], "-i") == 0) 89 cmd = VI_IFACE; 90 } 91 92 vi_req.vi_api_cookie = VI_API_COOKIE; 93 vi_req.vi_req_action = cmd; 94 switch (cmd) { 95 96 case VI_GET: 97 if (ioctl(s, SIOCGPVIMAGE, (caddr_t)&vi_req) < 0) 98 goto abort; 99 if (argc == 1) 100 printf("%s\n", vi_req.vi_name); 101 else 102 vi_print(&vi_req); 103 exit(0); 104 105 case VI_GETNEXT: 106 case VI_GETNEXT_RECURSE: 107 vi_req.vi_req_action = VI_GET; 108 if (ioctl(s, SIOCGPVIMAGE, (caddr_t)&vi_req) < 0) 109 goto abort; 110 vi_print(&vi_req); 111 vi_req.vi_req_action = VI_GETNEXT_RECURSE; 112 while (ioctl(s, SIOCGPVIMAGE, (caddr_t)&vi_req) == 0) { 113 vi_print(&vi_req); 114 vi_req.vi_req_action = cmd; 115 } 116 exit(0); 117 118 case VI_IFACE: 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) 122 goto abort; 123 printf("%s@%s\n", vi_req.vi_if_xname, vi_req.vi_name); 124 exit(0); 125 126 case VI_CREATE: 127 if (ioctl(s, SIOCSPVIMAGE, (caddr_t)&vi_req) < 0) 128 goto abort; 129 exit(0); 130 131 case VI_SWITCHTO: 132 strcpy(vi_req.vi_name, argv[1]); 133 if (ioctl(s, SIOCSPVIMAGE, (caddr_t)&vi_req) < 0) 134 goto abort; 135 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"); 140 goto abort; 141 } 142 close(s); 143 144 if (argc == 2) { 145 printf("Switched to vimage %s\n", argv[1]); 146 if ((shell = getenv("SHELL")) == NULL) 147 execlp("/bin/sh", argv[0], NULL); 148 else 149 execlp(shell, argv[0], NULL); 150 } else 151 execvp(argv[2], &argv[2]); 152 break; 153 154 case VI_DESTROY: 155 if (ioctl(s, SIOCSPVIMAGE, (caddr_t)&vi_req) < 0) 156 goto abort; 157 exit(0); 158 159 default: 160 fprintf(stderr, "usage: %s [-cdilr] vi_name [args]\n", 161 argv[0]); 162 exit(1); 163 } 164 165abort: 166 perror("Error"); 167 exit(1); 168} 169