source: rtems-libbsd/freebsd/sbin/ifconfig/ifclone.c @ f244de9

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f244de9 was 56e9de9, checked in by Sebastian Huber <sebastian.huber@…>, on 10/22/13 at 08:04:33

IFCONFIG(8): Wrap exit()

  • Property mode set to 100644
File size: 5.5 KB
Line 
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
30#ifndef lint
31static const char rcsid[] =
32  "$FreeBSD$";
33#endif /* not lint */
34
35#ifdef __rtems__
36#include <machine/rtems-bsd-program.h>
37#endif /* __rtems__ */
38#include <sys/queue.h>
39#include <rtems/bsd/sys/types.h>
40#include <sys/ioctl.h>
41#include <sys/socket.h>
42#include <net/if.h>
43
44#include <err.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50#include "ifconfig.h"
51
52static void
53list_cloners(void)
54{
55        struct if_clonereq ifcr;
56        char *cp, *buf;
57        int idx;
58        int s;
59
60        s = socket(AF_LOCAL, SOCK_DGRAM, 0);
61        if (s == -1)
62                err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
63
64        memset(&ifcr, 0, sizeof(ifcr));
65
66        if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
67                err(1, "SIOCIFGCLONERS for count");
68
69        buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
70        if (buf == NULL)
71                err(1, "unable to allocate cloner name buffer");
72
73        ifcr.ifcr_count = ifcr.ifcr_total;
74        ifcr.ifcr_buffer = buf;
75
76        if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) {
77                free(buf);
78                err(1, "SIOCIFGCLONERS for names");
79        }
80
81        /*
82         * In case some disappeared in the mean time, clamp it down.
83         */
84        if (ifcr.ifcr_count > ifcr.ifcr_total)
85                ifcr.ifcr_count = ifcr.ifcr_total;
86
87        for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
88                if (idx > 0)
89                        putchar(' ');
90                printf("%s", cp);
91        }
92
93        putchar('\n');
94        free(buf);
95}
96
97struct clone_defcb {
98        char ifprefix[IFNAMSIZ];
99        clone_callback_func *clone_cb;
100        SLIST_ENTRY(clone_defcb) next;
101};
102
103static SLIST_HEAD(, clone_defcb) clone_defcbh =
104   SLIST_HEAD_INITIALIZER(clone_defcbh);
105
106void
107clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
108{
109        struct clone_defcb *dcp;
110
111        dcp = malloc(sizeof(*dcp));
112#ifndef __rtems__
113        if (dcp == NULL) {
114                errx(1, "unable to allocate clone");
115        }
116#endif /* __rtems__ */
117        strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
118        dcp->clone_cb = p;
119        SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
120}
121
122/*
123 * Do the actual clone operation.  Any parameters must have been
124 * setup by now.  If a callback has been setup to do the work
125 * then defer to it; otherwise do a simple create operation with
126 * no parameters.
127 */
128static void
129ifclonecreate(int s, void *arg)
130{
131        struct ifreq ifr;
132        struct clone_defcb *dcp;
133        clone_callback_func *clone_cb = NULL;
134
135        memset(&ifr, 0, sizeof(ifr));
136        (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
137
138        if (clone_cb == NULL) {
139                /* Try to find a default callback */
140                SLIST_FOREACH(dcp, &clone_defcbh, next) {
141                        if (strncmp(dcp->ifprefix, ifr.ifr_name,
142                            strlen(dcp->ifprefix)) == 0) {
143                                clone_cb = dcp->clone_cb;
144                                break;
145                        }
146                }
147        }
148        if (clone_cb == NULL) {
149                /* NB: no parameters */
150                if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
151                        err(1, "SIOCIFCREATE2");
152        } else {
153                clone_cb(s, &ifr);
154        }
155
156        /*
157         * If we get a different name back than we put in, print it.
158         */
159        if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
160                strlcpy(name, ifr.ifr_name, sizeof(name));
161                printf("%s\n", name);
162        }
163}
164
165static
166DECL_CMD_FUNC(clone_create, arg, d)
167{
168        callback_register(ifclonecreate, NULL);
169}
170
171static
172DECL_CMD_FUNC(clone_destroy, arg, d)
173{
174        (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
175        if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
176                err(1, "SIOCIFDESTROY");
177}
178
179static struct cmd clone_cmds[] = {
180        DEF_CLONE_CMD("create", 0,      clone_create),
181        DEF_CMD("destroy",      0,      clone_destroy),
182        DEF_CLONE_CMD("plumb",  0,      clone_create),
183        DEF_CMD("unplumb",      0,      clone_destroy),
184};
185
186static void
187clone_Copt_cb(const char *optarg __unused)
188{
189        list_cloners();
190        exit(0);
191}
192static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
193
194#ifndef __rtems__
195static __constructor void
196#else /* __rtems__ */
197void
198#endif /* __rtems__ */
199clone_ctor(void)
200{
201#ifdef __rtems__
202        SLIST_INIT(&clone_defcbh);
203#endif /* __rtems__ */
204#define N(a)    (sizeof(a) / sizeof(a[0]))
205        size_t i;
206
207        for (i = 0; i < N(clone_cmds);  i++)
208                cmd_register(&clone_cmds[i]);
209        opt_register(&clone_Copt);
210#undef N
211}
212#ifdef __rtems__
213void
214clone_dtor(void)
215{
216        struct clone_defcb *dcp;
217        struct clone_defcb *dcp_tmp;
218
219        SLIST_FOREACH_SAFE(dcp, &clone_defcbh, next, dcp_tmp) {
220                free(dcp);
221        }
222}
223#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.