1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1998, 2000, 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1990 Mentat Inc.
* ndd.c 2.1, last change 11/14/90
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <stropts.h>
#include <inet/nd.h>
#include <string.h>
#include <stdlib.h>
static boolean_t do_getset(int fd, int cmd, char *buf, int buf_len);
static int get_value(char *msg, char *buf, int buf_len);
static void name_print(char *buf);
static void getset_interactive(int fd);
static int open_device(void);
static char *errmsg(int err);
static void fatal(char *fmt, ...);
static void printe(boolean_t print_errno, char *fmt, ...);
static char gbuf[65536]; /* Need 20k for 160 IREs ... */
static char usage_str[] = "usage: ndd -set device_name name value\n"
" ndd [-get] device_name name [name ...]";
/* ARGSUSED */
int
main(int argc, char **argv)
{
char *cp, *value;
int cmd;
int fd;
if (!(cp = *++argv)) {
while ((fd = open_device()) != -1) {
getset_interactive(fd);
(void) close(fd);
}
return (EXIT_SUCCESS);
}
cmd = ND_GET;
if (cp[0] == '-') {
if (strncmp(&cp[1], "set", 3) == 0)
cmd = ND_SET;
else if (strncmp(&cp[1], "get", 3) != 0)
fatal(usage_str);
if (!(cp = *++argv))
fatal(usage_str);
}
if ((fd = open(cp, O_RDWR)) == -1)
fatal("open of %s failed: %s", cp, errmsg(errno));
if (!isastream(fd))
fatal("%s is not a streams device", cp);
if (!(cp = *++argv)) {
getset_interactive(fd);
(void) close(fd);
return (EXIT_SUCCESS);
}
if (cmd == ND_SET) {
if (!(value = *++argv))
fatal(usage_str);
(void) snprintf(gbuf, sizeof (gbuf), "%s%c%s%c", cp, '\0',
value, '\0');
if (!do_getset(fd, cmd, gbuf, sizeof (gbuf)))
return (EXIT_FAILURE);
} else {
do {
(void) memset(gbuf, '\0', sizeof (gbuf));
(void) strlcpy(gbuf, cp, sizeof (gbuf));
if (!do_getset(fd, cmd, gbuf, sizeof (gbuf)))
return (EXIT_FAILURE);
if (cp = *++argv)
(void) putchar('\n');
} while (cp);
}
(void) close(fd);
return (EXIT_SUCCESS);
}
static void
name_print(char *buf)
{
char *cp, *rwtag;
for (cp = buf; cp[0]; ) {
for (rwtag = cp; !isspace(*rwtag); rwtag++)
;
*rwtag++ = '\0';
while (isspace(*rwtag))
rwtag++;
(void) printf("%-30s%s\n", cp, rwtag);
for (cp = rwtag; *cp++; )
;
}
}
/*
* This function is vile, but it's better here than in the kernel.
*/
static boolean_t
is_obsolete(const char *param)
{
if (strcmp(param, "ip_enable_group_ifs") == 0 ||
strcmp(param, "ifgrp_status") == 0) {
(void) fprintf(stderr, "The \"%s\" tunable has been superseded "
"by IP Multipathing.\nPlease see the IP Network "
"Multipathing Administration Guide for details.\n", param);
return (B_TRUE);
}
return (B_FALSE);
}
static boolean_t
do_getset(int fd, int cmd, char *buf, int buf_len)
{
char *cp;
struct strioctl stri;
boolean_t is_name_get;
if (is_obsolete(buf))
return (B_TRUE);
stri.ic_cmd = cmd;
stri.ic_timout = 0;
stri.ic_len = buf_len;
stri.ic_dp = buf;
is_name_get = stri.ic_cmd == ND_GET && buf[0] == '?' && buf[1] == '\0';
if (ioctl(fd, I_STR, &stri) == -1) {
if (errno == ENOENT)
(void) printf("name is non-existent for this module\n"
"for a list of valid names, use name '?'\n");
else
(void) printf("operation failed: %s\n", errmsg(errno));
return (B_FALSE);
}
if (is_name_get)
name_print(buf);
else if (stri.ic_cmd == ND_GET) {
for (cp = buf; *cp != '\0'; cp += strlen(cp) + 1)
(void) puts(cp);
}
(void) fflush(stdout);
return (B_TRUE);
}
static int
get_value(char *msg, char *buf, int buf_len)
{
int len;
(void) printf("%s", msg);
(void) fflush(stdout);
buf[buf_len-1] = '\0';
if (fgets(buf, buf_len-1, stdin) == NULL)
exit(EXIT_SUCCESS);
len = strlen(buf);
if (buf[len-1] == '\n')
buf[len - 1] = '\0';
else
len++;
return (len);
}
static void
getset_interactive(int fd)
{
int cmd;
char *cp;
int len, buf_len;
char len_buf[10];
for (;;) {
(void) memset(gbuf, '\0', sizeof (gbuf));
len = get_value("name to get/set ? ", gbuf, sizeof (gbuf));
if (len == 1 || (gbuf[0] == 'q' && gbuf[1] == '\0'))
return;
for (cp = gbuf; cp < &gbuf[len]; cp++) {
if (isspace(*cp))
*cp = '\0';
}
cmd = ND_GET;
if (gbuf[0] != '?' &&
get_value("value ? ", &gbuf[len], sizeof (gbuf) - len) > 1)
cmd = ND_SET;
if (cmd == ND_GET && gbuf[0] != '?' &&
get_value("length ? ", len_buf, sizeof (len_buf)) > 1) {
if (!isdigit(len_buf[0])) {
(void) printf("invalid length\n");
continue;
}
buf_len = atoi(len_buf);
} else
buf_len = sizeof (gbuf);
(void) do_getset(fd, cmd, gbuf, buf_len);
}
}
static void
printe(boolean_t print_errno, char *fmt, ...)
{
va_list ap;
int error = errno;
va_start(ap, fmt);
(void) printf("*ERROR* ");
(void) vprintf(fmt, ap);
va_end(ap);
if (print_errno)
(void) printf(": %s\n", errmsg(error));
else
(void) printf("\n");
}
static int
open_device(void)
{
char name[80];
int fd, len;
for (;;) {
len = get_value("module to query ? ", name, sizeof (name));
if (len <= 1 ||
(len == 2 && (name[0] == 'q' || name[0] == 'Q')))
return (-1);
if ((fd = open(name, O_RDWR)) == -1) {
printe(B_TRUE, "open of %s failed", name);
continue;
}
if (isastream(fd))
return (fd);
(void) close(fd);
printe(B_FALSE, "%s is not a streams device", name);
}
}
static void
fatal(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
(void) fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
static char *
errmsg(int error)
{
char *msg = strerror(error);
return (msg != NULL ? msg : "unknown error");
}
|