kernel/arm: send SIGSEGV to processes
[minix3.git] / sbin / ifconfig / agr.c
blobfec580050f1302635852a7369c10f121bbd9e063
1 /* $NetBSD: agr.c,v 1.15 2008/07/15 21:27:58 dyoung Exp $ */
3 /*-
4 * Copyright (c)2005 YAMAMOTO Takashi,
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 #if !defined(lint)
31 __RCSID("$NetBSD: agr.c,v 1.15 2008/07/15 21:27:58 dyoung Exp $");
32 #endif /* !defined(lint) */
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
37 #include <net/if.h>
38 #include <net/agr/if_agrioctl.h>
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <util.h>
48 #include "env.h"
49 #include "extern.h"
50 #include "parse.h"
51 #include "util.h"
53 static int agrsetport(prop_dictionary_t, prop_dictionary_t);
54 static void agr_constructor(void) __attribute__((constructor));
55 static int checkifname(prop_dictionary_t);
56 static void assertifname(prop_dictionary_t);
58 static struct piface agrif = PIFACE_INITIALIZER(&agrif, "agr interface",
59 agrsetport, "agrport", &command_root.pb_parser);
61 static const struct kwinst agrkw[] = {
62 {.k_word = "agrport", .k_type = KW_T_INT, .k_int = AGRCMD_ADDPORT,
63 .k_nextparser = &agrif.pif_parser}
64 , {.k_word = "-agrport", .k_type = KW_T_INT, .k_int = AGRCMD_REMPORT,
65 .k_nextparser = &agrif.pif_parser}
68 struct pkw agr = PKW_INITIALIZER(&agr, "agr", NULL, "agrcmd",
69 agrkw, __arraycount(agrkw), NULL);
71 static int
72 checkifname(prop_dictionary_t env)
74 const char *ifname;
76 if ((ifname = getifname(env)) == NULL)
77 return 1;
79 return strncmp(ifname, "agr", 3) != 0 ||
80 !isdigit((unsigned char)ifname[3]);
83 static void
84 assertifname(prop_dictionary_t env)
86 if (checkifname(env))
87 errx(EXIT_FAILURE, "valid only with agr(4) interfaces");
90 int
91 agrsetport(prop_dictionary_t env, prop_dictionary_t oenv)
93 char buf[IFNAMSIZ];
94 struct agrreq ar;
95 const char *port;
96 int64_t cmd;
98 if (!prop_dictionary_get_int64(env, "agrcmd", &cmd)) {
99 warnx("%s.%d", __func__, __LINE__);
100 errno = ENOENT;
101 return -1;
104 if (!prop_dictionary_get_cstring_nocopy(env, "agrport", &port)) {
105 warnx("%s.%d", __func__, __LINE__);
106 errno = ENOENT;
107 return -1;
109 strlcpy(buf, port, sizeof(buf));
111 assertifname(env);
112 memset(&ar, 0, sizeof(ar));
113 ar.ar_version = AGRREQ_VERSION;
114 ar.ar_cmd = cmd;
115 ar.ar_buf = buf;
116 ar.ar_buflen = strlen(buf);
118 if (indirect_ioctl(env, SIOCSETAGR, &ar) == -1)
119 err(EXIT_FAILURE, "SIOCSETAGR");
120 return 0;
123 static void
124 agr_status(prop_dictionary_t env, prop_dictionary_t oenv)
126 struct agrreq ar;
127 void *buf = NULL;
128 size_t buflen = 0;
129 struct agrportlist *apl;
130 struct agrportinfo *api;
131 int i;
133 if (checkifname(env))
134 return;
136 again:
137 memset(&ar, 0, sizeof(ar));
138 ar.ar_version = AGRREQ_VERSION;
139 ar.ar_cmd = AGRCMD_PORTLIST;
140 ar.ar_buf = buf;
141 ar.ar_buflen = buflen;
143 if (indirect_ioctl(env, SIOCGETAGR, &ar) == -1) {
144 if (errno != E2BIG) {
145 warn("SIOCGETAGR");
146 return;
149 free(buf);
150 buf = NULL;
151 buflen = 0;
152 goto again;
155 if (buf == NULL) {
156 buflen = ar.ar_buflen;
157 buf = malloc(buflen);
158 if (buf == NULL) {
159 err(EXIT_FAILURE, "agr_status");
161 goto again;
164 apl = buf;
165 api = (void *)(apl + 1);
167 for (i = 0; i < apl->apl_nports; i++) {
168 char tmp[256];
170 snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags);
171 printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp);
172 api++;
176 static status_func_t status;
177 static usage_func_t usage;
178 static cmdloop_branch_t branch;
180 static void
181 agr_usage(prop_dictionary_t env)
183 fprintf(stderr, "\t[ agrport i ] [ -agrport i ]\n");
186 static void
187 agr_constructor(void)
189 status_func_init(&status, agr_status);
190 usage_func_init(&usage, agr_usage);
191 register_status(&status);
192 register_usage(&usage);
193 cmdloop_branch_init(&branch, &agr.pk_parser);
194 register_cmdloop_branch(&branch);