Rename boot_i386.8 to boot_pc32.8 by repo-copy in order to enable the
[dragonfly.git] / sbin / ifconfig / ifclone.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sbin/ifconfig/ifclone.c,v 1.1 2004/12/08 19:18:07 sam Exp $
30  * $DragonFly: src/sbin/ifconfig/ifclone.c,v 1.1 2006/04/02 03:33:59 sephe Exp $
31  */
32
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <net/if.h>
37
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include "ifconfig.h"
45
46 static void
47 list_cloners(void)
48 {
49         struct if_clonereq ifcr;
50         char *cp, *buf;
51         int idx;
52         int s;
53
54         s = socket(AF_INET, SOCK_DGRAM, 0);
55         if (s == -1)
56                 err(1, "socket(AF_INET,SOCK_DGRAM)");
57
58         memset(&ifcr, 0, sizeof(ifcr));
59
60         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
61                 err(1, "SIOCIFGCLONERS for count");
62
63         buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
64         if (buf == NULL)
65                 err(1, "unable to allocate cloner name buffer");
66
67         ifcr.ifcr_count = ifcr.ifcr_total;
68         ifcr.ifcr_buffer = buf;
69
70         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
71                 err(1, "SIOCIFGCLONERS for names");
72
73         /*
74          * In case some disappeared in the mean time, clamp it down.
75          */
76         if (ifcr.ifcr_count > ifcr.ifcr_total)
77                 ifcr.ifcr_count = ifcr.ifcr_total;
78
79         for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
80                 if (idx > 0)
81                         putchar(' ');
82                 printf("%s", cp);
83         }
84
85         putchar('\n');
86         free(buf);
87 }
88
89 void
90 clone_create(void)
91 {
92         int s;
93
94         s = socket(AF_INET, SOCK_DGRAM, 0);
95         if (s == -1)
96                 err(1, "socket(AF_INET,SOCK_DGRAM)");
97
98         memset(&ifr, 0, sizeof(ifr));
99         (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
100         if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
101                 err(1, "SIOCIFCREATE");
102
103         /*
104          * If we get a different name back then we put in, we probably
105          * want to print it out, but we might change our mind later so
106          * we just signal our interest and leave the printout for later.
107          */
108         if (strcmp(name, ifr.ifr_name) != 0) {
109                 printname = 1;
110                 strlcpy(name, ifr.ifr_name, sizeof(name));
111         }
112
113         close(s);
114 }
115
116 static void
117 clone_destroy(const char *val, int d, int s, const struct afswtch *rafp)
118 {
119
120         (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
121         if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
122                 err(1, "SIOCIFDESTROY");
123         /*
124          * If we create and destroy an interface in the same command,
125          * there isn't any reason to print it's name.
126          */
127         printname = 0;
128 }
129
130 static struct cmd clone_cmds[] = {
131         DEF_CMD("destroy",      0,      clone_destroy),
132         DEF_CMD("unplumb",      0,      clone_destroy),
133 };
134
135 static void
136 clone_Copt_cb(const char *optarg __unused)
137 {
138         list_cloners();
139         exit(0);
140 }
141 static struct option clone_Copt = { "C", "[-C]", clone_Copt_cb };
142
143 static __constructor void
144 clone_ctor(void)
145 {
146 #define N(a)    (sizeof(a) / sizeof(a[0]))
147         int i;
148
149         for (i = 0; i < N(clone_cmds);  i++)
150                 cmd_register(&clone_cmds[i]);
151         opt_register(&clone_Copt);
152 #undef N
153 }