dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / ypcmd / yppoll.c
blobadcd427ad4b539b7f30fe132bbf3bd4c784314c7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
32 * California.
36 * This is a user command which asks a particular ypserv which version of a
37 * map it is using. Usage is:
39 * yppoll [-h <host>] [-d <domainname>] mapname
41 * If the host is ommitted, the local host will be used. If host is specified
42 * as an internet address, no yp services need to be locally available.
45 #include <stdio.h>
46 #include <ctype.h>
47 #include <rpc/rpc.h>
48 #include <rpcsvc/ypclnt.h>
49 #include <rpcsvc/yp_prot.h>
50 #include <netdir.h>
51 #include <arpa/inet.h>
52 #include "yp_b.h"
54 #define TIMEOUT 30 /* Total seconds for timeout */
56 static int status = 0; /* exit status */
57 static char *domain = NULL;
58 static char default_domain_name[YPMAXDOMAIN];
59 static char *map = NULL;
60 static char *host = NULL;
61 static char default_host_name[256];
63 static char err_usage[] =
64 "Usage:\n\
65 yppoll [ -h host ] [ -d domainname ] mapname\n\n";
66 static char err_bad_args[] =
67 "Bad %s argument.\n";
68 static char err_cant_get_kname[] =
69 "Can't get %s back from system call.\n";
70 static char err_null_kname[] =
71 "%s hasn't been set on this machine.\n";
72 static char err_bad_hostname[] = "hostname";
73 static char err_bad_mapname[] = "mapname";
74 static char err_bad_domainname[] = "domainname";
75 static char err_bad_resp[] =
76 "Ill-formed response returned from ypserv on host %s.\n";
78 static void get_command_line_args();
79 static void getdomain();
80 static void getlochost();
81 static void getmapparms();
82 static void newresults();
83 static void getypserv();
85 extern void exit();
86 extern int getdomainname();
87 extern int gethostname();
88 extern unsigned int strlen();
89 extern int strcmp();
92 * This is the mainline for the yppoll process.
95 int
96 main(argc, argv)
97 int argc;
98 char **argv;
101 get_command_line_args(argc, argv);
103 if (!domain) {
104 getdomain();
107 if (!host) {
108 getypserv();
111 getmapparms();
112 return (status);
116 * This does the command line argument processing.
118 static void
119 get_command_line_args(argc, argv)
120 int argc;
121 char **argv;
124 argv++;
126 while (--argc) {
128 if ((*argv)[0] == '-') {
130 switch ((*argv)[1]) {
132 case 'h':
134 if (argc > 1) {
135 argv++;
136 argc--;
137 host = *argv;
138 argv++;
140 if ((int)strlen(host) > 256) {
141 (void) fprintf(stderr,
142 err_bad_args,
143 err_bad_hostname);
144 exit(1);
147 } else {
148 (void) fprintf(stderr, err_usage);
149 exit(1);
152 break;
154 case 'd':
156 if (argc > 1) {
157 argv++;
158 argc--;
159 domain = *argv;
160 argv++;
162 if ((int)strlen(domain) > YPMAXDOMAIN) {
163 (void) fprintf(stderr,
164 err_bad_args,
165 err_bad_domainname);
166 exit(1);
169 } else {
170 (void) fprintf(stderr, err_usage);
171 exit(1);
174 break;
176 default:
177 (void) fprintf(stderr, err_usage);
178 exit(1);
182 } else {
183 if (!map) {
184 map = *argv;
186 if ((int)strlen(map) > YPMAXMAP) {
187 (void) fprintf(stderr, err_bad_args,
188 err_bad_mapname);
189 exit(1);
192 } else {
193 (void) fprintf(stderr, err_usage);
194 exit(1);
199 if (!map) {
200 (void) fprintf(stderr, err_usage);
201 exit(1);
206 * This gets the local default domainname, and makes sure that it's set
207 * to something reasonable. domain is set here.
209 static void
210 getdomain()
212 if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
213 domain = default_domain_name;
214 } else {
215 (void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
216 exit(1);
219 if ((int)strlen(domain) == 0) {
220 (void) fprintf(stderr, err_null_kname, err_bad_domainname);
221 exit(1);
226 * This gets the local hostname back from the kernel
228 static void
229 getlochost()
232 if (! gethostname(default_host_name, 256)) {
233 host = default_host_name;
234 } else {
235 (void) fprintf(stderr, err_cant_get_kname, err_bad_hostname);
236 exit(1);
240 static void
241 getmapparms()
243 CLIENT * map_clnt;
244 struct ypresp_order oresp;
245 struct ypreq_nokey req;
246 struct ypresp_master mresp;
247 struct ypresp_master *mresults = NULL;
248 struct ypresp_order *oresults = NULL;
250 struct timeval timeout;
251 enum clnt_stat s;
253 if ((map_clnt = clnt_create(host, YPPROG, YPVERS,
254 "netpath")) == NULL) {
255 (void) fprintf(stderr,
256 "Can't create connection to %s.\n", host);
257 clnt_pcreateerror("Reason");
258 exit(1);
261 timeout.tv_sec = TIMEOUT;
262 timeout.tv_usec = 0;
263 req.domain = domain;
264 req.map = map;
265 mresp.master = NULL;
267 if (clnt_call(map_clnt, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey,
268 (caddr_t)&req, (xdrproc_t)xdr_ypresp_master,
269 (caddr_t)&mresp, timeout) == RPC_SUCCESS) {
270 mresults = &mresp;
271 s = (enum clnt_stat) clnt_call(map_clnt, YPPROC_ORDER,
272 (xdrproc_t)xdr_ypreq_nokey, (char *)&req,
273 (xdrproc_t)xdr_ypresp_order, (char *)&oresp, timeout);
275 if (s == RPC_SUCCESS) {
276 oresults = &oresp;
277 newresults(mresults, oresults);
278 } else {
279 (void) fprintf(stderr,
280 "Can't make YPPROC_ORDER call to ypserv"
281 " at %s.\n ",
282 host);
283 clnt_perror(map_clnt, "Reason");
284 exit(1);
287 } else {
288 clnt_destroy(map_clnt);
292 static void
293 newresults(m, o)
294 struct ypresp_master *m;
295 struct ypresp_order *o;
297 char *s_domok = "Domain %s is supported.\n";
298 char *s_ook = "Map %s has order number %d.\n";
299 char *s_mok = "The master server is %s.\n";
300 char *s_mbad = "Can't get master for map %s.\n Reason: %s\n";
301 char *s_obad = "Can't get order number for map %s.\n Reason: %s\n";
303 if (m->status == YP_TRUE && o->status == YP_TRUE) {
304 (void) printf(s_domok, domain);
305 (void) printf(s_ook, map, o->ordernum);
306 (void) printf(s_mok, m->master);
307 } else if (o->status == YP_TRUE) {
308 (void) printf(s_domok, domain);
309 (void) printf(s_ook, map, o->ordernum);
310 (void) fprintf(stderr, s_mbad, map,
311 yperr_string(ypprot_err(m->status)));
312 status = 1;
313 } else if (m->status == YP_TRUE) {
314 (void) printf(s_domok, domain);
315 (void) fprintf(stderr, s_obad, map,
316 yperr_string(ypprot_err(o->status)));
317 (void) printf(s_mok, m->master);
318 status = 1;
319 } else {
320 (void) fprintf(stderr,
321 "Can't get any map parameter information.\n");
322 (void) fprintf(stderr, s_obad, map,
323 yperr_string(ypprot_err(o->status)));
324 (void) fprintf(stderr, s_mbad, map,
325 yperr_string(ypprot_err(m->status)));
326 status = 1;
330 static void
331 getypserv()
333 struct ypbind_resp response;
334 struct ypbind_domain ypdomain;
335 struct ypbind_binding *binding;
336 static char hostbuf[256];
338 getlochost();
340 (void) memset((char *)&response, 0, sizeof (response));
341 ypdomain.ypbind_domainname = domain;
342 ypdomain.ypbind_vers = YPBINDVERS;
343 (void) rpc_call(host, YPBINDPROG, YPBINDVERS, YPBINDPROC_DOMAIN,
344 xdr_ypbind_domain, (char *)&ypdomain, xdr_ypbind_resp,
345 (char *)&response, "netpath");
346 if (response.ypbind_status != YPBIND_SUCC_VAL) {
347 (void) fprintf(stderr, "couldn't get yp server - status %u\n",
348 response.ypbind_status);
349 exit(1);
351 binding = response.ypbind_resp_u.ypbind_bindinfo;
352 host = binding->ypbind_servername;
355 * When ypbind is running in broadcast mode, it sets the
356 * servername to "". To get the real name of the server,
357 * we need to do a host lookup on the svcaddr. This code
358 * is similar to code in ypwhich.
360 if (strcmp(host, "") == 0) {
361 struct nd_hostservlist *nhs;
362 struct netconfig *nconf = binding->ypbind_nconf;
363 struct netbuf *svcaddr = binding->ypbind_svcaddr;
365 if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) {
366 struct sockaddr_in *sa;
368 sa = (struct sockaddr_in *)svcaddr->buf;
370 strcpy(hostbuf, inet_ntoa(sa->sin_addr));
371 } else {
372 sprintf(hostbuf, "%s", nhs->h_hostservs->h_host);
374 host = hostbuf;
375 netdir_free((char *)nhs, ND_HOSTSERVLIST);