source: rtems-libbsd/freebsd/sbin/ifconfig/ifgroup.c @ fef6dd1

4.11
Last change on this file since fef6dd1 was fef6dd1, checked in by Christian Mauderer <Christian.Mauderer@…>, on Jul 15, 2016 at 5:32:56 AM

freebsd: Don't use new wrappers for old ports.

Some of the commands have been adapted manually. So the wrapper
currently don't necessarily work as expected. For example ifconfig calls
malloc outside of the program call.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*-
4 * Copyright (c) 2006 Max Laier. All rights reserved.
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
28#ifndef lint
29static const char rcsid[] =
30  "$FreeBSD$";
31#endif /* not lint */
32
33#ifdef __rtems__
34#define RTEMS_BSD_PROGRAM_NO_OPEN_WRAP
35#define RTEMS_BSD_PROGRAM_NO_SOCKET_WRAP
36#define RTEMS_BSD_PROGRAM_NO_CLOSE_WRAP
37#define RTEMS_BSD_PROGRAM_NO_FOPEN_WRAP
38#define RTEMS_BSD_PROGRAM_NO_FCLOSE_WRAP
39#define RTEMS_BSD_PROGRAM_NO_MALLOC_WRAP
40#define RTEMS_BSD_PROGRAM_NO_CALLOC_WRAP
41#define RTEMS_BSD_PROGRAM_NO_REALLOC_WRAP
42#define RTEMS_BSD_PROGRAM_NO_FREE_WRAP
43#include <machine/rtems-bsd-program.h>
44#endif /* __rtems__ */
45#include <rtems/bsd/sys/types.h>
46#include <sys/ioctl.h>
47#include <sys/socket.h>
48#include <net/if.h>
49
50#include <ctype.h>
51#include <err.h>
52#include <errno.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57
58#include "ifconfig.h"
59
60/* ARGSUSED */
61static void
62setifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
63{
64        struct ifgroupreq ifgr;
65
66        memset(&ifgr, 0, sizeof(ifgr));
67        strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
68
69        if (group_name[0] && isdigit((unsigned char)group_name[strlen(group_name) - 1]))
70                errx(1, "setifgroup: group names may not end in a digit");
71
72        if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
73                errx(1, "setifgroup: group name too long");
74        if (ioctl(s, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
75                err(1," SIOCAIFGROUP");
76}
77
78/* ARGSUSED */
79static void
80unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
81{
82        struct ifgroupreq ifgr;
83
84        memset(&ifgr, 0, sizeof(ifgr));
85        strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
86
87        if (group_name[0] && isdigit((unsigned char)group_name[strlen(group_name) - 1]))
88                errx(1, "unsetifgroup: group names may not end in a digit");
89
90        if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
91                errx(1, "unsetifgroup: group name too long");
92        if (ioctl(s, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
93                err(1, "SIOCDIFGROUP");
94}
95
96static void
97getifgroups(int s)
98{
99        int                      len, cnt;
100        struct ifgroupreq        ifgr;
101        struct ifg_req          *ifg;
102
103        if (!verbose)
104                return;
105
106        memset(&ifgr, 0, sizeof(ifgr));
107        strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
108
109        if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
110                if (errno == EINVAL || errno == ENOTTY)
111                        return;
112                else
113                        err(1, "SIOCGIFGROUP");
114        }
115
116        len = ifgr.ifgr_len;
117        ifgr.ifgr_groups =
118            (struct ifg_req *)calloc(len / sizeof(struct ifg_req),
119            sizeof(struct ifg_req));
120        if (ifgr.ifgr_groups == NULL)
121                err(1, "getifgroups");
122        if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
123                err(1, "SIOCGIFGROUP");
124
125        cnt = 0;
126        ifg = ifgr.ifgr_groups;
127        for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
128                len -= sizeof(struct ifg_req);
129                if (strcmp(ifg->ifgrq_group, "all")) {
130                        if (cnt == 0)
131                                printf("\tgroups: ");
132                        cnt++;
133                        printf("%s ", ifg->ifgrq_group);
134                }
135        }
136        if (cnt)
137                printf("\n");
138}
139
140static void
141printgroup(const char *groupname)
142{
143        struct ifgroupreq        ifgr;
144        struct ifg_req          *ifg;
145        int                      len, cnt = 0;
146        int                      s;
147
148        s = socket(AF_LOCAL, SOCK_DGRAM, 0);
149        if (s == -1)
150                err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
151        bzero(&ifgr, sizeof(ifgr));
152        strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
153        if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
154                close(s);
155                if (errno == EINVAL || errno == ENOTTY ||
156                    errno == ENOENT)
157                        exit(0);
158                else
159                        err(1, "SIOCGIFGMEMB");
160        }
161
162        len = ifgr.ifgr_len;
163        if ((ifgr.ifgr_groups = calloc(1, len)) == NULL) {
164                close(s);
165                err(1, "printgroup");
166        }
167        if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
168                free(ifgr.ifgr_groups);
169                close(s);
170                err(1, "SIOCGIFGMEMB");
171        }
172
173        for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
174            ifg++) {
175                len -= sizeof(struct ifg_req);
176                printf("%s\n", ifg->ifgrq_member);
177                cnt++;
178        }
179        free(ifgr.ifgr_groups);
180        close(s);
181
182        exit(0);
183}
184
185static struct cmd group_cmds[] = {
186        DEF_CMD_ARG("group",    setifgroup),
187        DEF_CMD_ARG("-group",   unsetifgroup),
188};
189static struct afswtch af_group = {
190        .af_name        = "af_group",
191        .af_af          = AF_UNSPEC,
192        .af_other_status = getifgroups,
193};
194static struct option group_gopt = { "g:", "[-g groupname]", printgroup };
195
196#ifndef __rtems__
197static __constructor void
198#else /* __rtems__ */
199void
200#endif /* __rtems__ */
201group_ctor(void)
202{
203#define N(a)    (sizeof(a) / sizeof(a[0]))
204        int i;
205
206        for (i = 0; i < N(group_cmds);  i++)
207                cmd_register(&group_cmds[i]);
208        af_register(&af_group);
209        opt_register(&group_gopt);
210#undef N
211}
Note: See TracBrowser for help on using the repository browser.