3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
43 #include <sys/socket.h>
44 #include <sys/fcntl.h>
46 #include <sys/param.h>
49 #include <rpc/pmap_clnt.h>
50 #include <rpcsvc/yp.h>
51 struct dom_binding {};
52 #include <rpcsvc/ypclnt.h>
53 #include "ypxfr_extern.h"
54 #include "yppush_extern.h"
56 char *progname = "yppush";
59 char *yp_dir = _PATH_YP;
61 char *yppush_mapname = NULL; /* Map to transfer. */
62 char *yppush_domain = NULL; /* Domain in which map resides. */
63 char *yppush_master = NULL; /* Master NIS server for said domain. */
64 int verbose = 0; /* Toggle verbose mode. */
65 unsigned long yppush_transid = 0;
66 int yppush_timeout = 80; /* Default timeout. */
67 int yppush_jobs = 0; /* Number of allowed concurrent jobs. */
68 int yppush_running_jobs = 0; /* Number of currently running jobs. */
69 int yppush_alarm_tripped = 0;
71 /* Structure for holding information about a running job. */
77 unsigned long prognum;
84 struct jobs *yppush_joblist; /* Linked list of running jobs. */
87 * Local error messages.
90 yppusherr_string(int err)
94 return("transfer or callback timed out");
96 return("failed to contact ypserv");
98 return("no such host");
100 return("portmapper failure");
102 return("unknown error code");
107 * Report state of a job.
110 yppush_show_status(ypxfrstat status, unsigned long tid)
114 job = yppush_joblist;
127 yp_error("checking return status: transaction ID: %lu",
129 if (status != YPPUSH_SUCC || verbose) {
130 yp_error("transfer of map %s to server %s %s",
131 job->map, job->server, status == YPPUSH_SUCC ?
132 "succeeded" : "failed");
133 yp_error("status returned by ypxfr: %s", status > YPPUSH_AGE ?
134 yppusherr_string(status) :
135 ypxfrerr_string(status));
140 svc_unregister(job->prognum, 1);
142 yppush_running_jobs--;
151 int still_pending = 1;
153 /* Let all the information trickle in. */
154 while (!now && still_pending) {
155 jptr = yppush_joblist;
158 if (jptr->polled == 0) {
161 yp_error("%s has not responded",
165 yp_error("%s has responded",
172 yp_error("%d transfer%sstill pending",
174 still_pending > 1 ? "s " : " ");
175 yppush_alarm_tripped = 0;
176 alarm(YPPUSH_RESPONSE_TIMEOUT);
179 if (yppush_alarm_tripped == 1) {
180 yp_error("timed out");
185 yp_error("all transfers complete");
191 /* All stats collected and reported -- kill all the stragglers. */
192 jptr = yppush_joblist;
195 yp_error("warning: exiting with transfer \
196 to %s (transid = %lu) still pending", jptr->server, jptr->tid);
197 svc_unregister(jptr->prognum, 1);
205 * Handler for 'normal' signals.
211 if (sig == SIGTERM || sig == SIGINT || sig == SIGABRT) {
216 if (sig == SIGALRM) {
218 yppush_alarm_tripped++;
225 * Dispatch loop for callback RPC services.
234 #endif /* def FD_SETSIZE */
235 struct timeval timeout;
245 #endif /* def FD_SETSIZE */
246 switch (select(_rpc_dtablesize(), &readfds, NULL, NULL, &timeout)) {
250 yp_error("select failed: %s", strerror(errno));
253 yp_error("select() timed out");
256 svc_getreqset(&readfds);
263 * Special handler for asynchronous socket I/O. We mark the
264 * sockets of the callback handlers as O_ASYNC and handle SIGIO
265 * events here, which will occur when the callback handler has
266 * something interesting to tell us.
269 async_handler(int sig)
273 /* reset any pending alarms. */
275 yppush_alarm_tripped++;
276 kill(getpid(), SIGALRM);
281 * RPC service routines for callbacks.
284 yppushproc_null_1_svc(void *argp, struct svc_req *rqstp)
286 static char * result;
287 /* Do nothing -- RPC conventions call for all a null proc. */
288 return((void *) &result);
292 yppushproc_xfrresp_1_svc(yppushresp_xfr *argp, struct svc_req *rqstp)
294 static char * result;
295 yppush_show_status(argp->status, argp->transid);
296 return((void *) &result);
300 * Transmit a YPPROC_XFR request to ypserv.
303 yppush_send_xfr(struct jobs *job)
306 /* ypresp_xfr *resp; */
310 struct timeval timeout;
316 * The ypreq_xfr structure has a member of type map_parms,
317 * which seems to require the order number of the map.
318 * It isn't actually used at the other end (at least the
319 * FreeBSD ypserv doesn't use it) but we fill it in here
320 * for the sake of completeness.
322 key.data = "YP_LAST_MODIFIED";
323 key.size = sizeof ("YP_LAST_MODIFIED") - 1;
325 if (yp_get_record(yppush_domain, yppush_mapname, &key, &data,
327 yp_error("failed to read order number from %s: %s: %s",
328 yppush_mapname, yperr_string(yp_errno),
333 /* Fill in the request arguments */
334 req.map_parms.ordernum = atoi(data.data);
335 req.map_parms.domain = yppush_domain;
336 req.map_parms.peer = yppush_master;
337 req.map_parms.map = job->map;
338 req.transid = job->tid;
339 req.prog = job->prognum;
340 req.port = job->port;
342 /* Get a handle to the remote ypserv. */
343 if ((clnt = clnt_create(job->server, YPPROG, YPVERS, "udp")) == NULL) {
344 yp_error("%s: %s",job->server,clnt_spcreateerror("couldn't \
345 create udp handle to NIS server"));
346 switch (rpc_createerr.cf_stat) {
347 case RPC_UNKNOWNHOST:
348 job->stat = YPPUSH_NOHOST;
350 case RPC_PMAPFAILURE:
351 job->stat = YPPUSH_PMAP;
354 job->stat = YPPUSH_RPC;
361 * Reduce timeout to nothing since we may not
362 * get a response from ypserv and we don't want to block.
364 if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
365 yp_error("failed to set timeout on ypproc_xfr call");
367 /* Invoke the ypproc_xfr service. */
368 if (ypproc_xfr_2(&req, clnt) == NULL) {
369 clnt_geterr(clnt, &err);
370 if (err.re_status != RPC_SUCCESS &&
371 err.re_status != RPC_TIMEDOUT) {
372 yp_error("%s: %s", job->server, clnt_sperror(clnt,
374 job->stat = YPPUSH_YPSERV;
386 * Main driver function. Register the callback service, add the transfer
387 * request to the internal list, send the YPPROC_XFR request to ypserv
388 * do other magic things.
391 yp_push(char *server, char *map, unsigned long tid)
393 unsigned long prognum;
394 int sock = RPC_ANYSOCK;
399 * Register the callback service on the first free
400 * transient program number.
402 xprt = svcudp_create(sock);
403 for (prognum = 0x40000000; prognum < 0x5FFFFFFF; prognum++) {
404 if (svc_register(xprt, prognum, 1,
405 yppush_xfrrespprog_1, IPPROTO_UDP) == TRUE)
409 /* Register the job in our linked list of jobs. */
410 if ((job = (struct jobs *)malloc(sizeof (struct jobs))) == NULL) {
411 yp_error("malloc failed");
415 /* Initialize the info for this job. */
418 job->port = xprt->xp_port;
419 job->sock = xprt->xp_fd; /*XXX: Evil!! EEEEEEEVIL!!! */
420 job->server = strdup(server);
421 job->map = strdup(map);
422 job->prognum = prognum;
424 job->next = yppush_joblist;
425 yppush_joblist = job;
428 * Set the RPC sockets to asynchronous mode. This will
429 * cause the system to smack us with a SIGIO when an RPC
430 * callback is delivered. This in turn allows us to handle
431 * the callback even though we may be in the middle of doing
432 * something else at the time.
434 * XXX This is a horrible thing to do for two reasons,
435 * both of which have to do with portability:
436 * 1) We really ought not to be sticking our grubby mits
437 * into the RPC service transport handle like this.
438 * 2) Even in this day and age, there are still some *NIXes
439 * that don't support async socket I/O.
441 if (fcntl(xprt->xp_fd, F_SETOWN, getpid()) == -1 ||
442 fcntl(xprt->xp_fd, F_SETFL, O_ASYNC) == -1) {
443 yp_error("failed to set async I/O mode: %s",
449 yp_error("initiating transfer: %s -> %s (transid = %lu)",
450 yppush_mapname, server, tid);
454 * Send the XFR request to ypserv. We don't have to wait for
455 * a response here since we can handle them asynchronously.
458 if (yppush_send_xfr(job)){
459 /* Transfer request blew up. */
460 yppush_show_status(job->stat ? job->stat :
461 YPPUSH_YPSERV,job->tid);
464 yp_error("%s has been called", server);
471 * Called for each entry in the ypservers map from yp_get_map(), which
472 * is our private yp_all() routine.
475 yppush_foreach(int status, char *key, int keylen, char *val, int vallen,
478 char server[YPMAXRECORD + 2];
480 if (status != YP_TRUE)
483 snprintf(server, sizeof(server), "%.*s", vallen, val);
486 * Restrict the number of concurrent jobs. If yppush_jobs number
487 * of jobs have already been dispatched and are still pending,
488 * wait for one of them to finish so we can reuse its slot.
490 if (yppush_jobs <= 1) {
491 yppush_alarm_tripped = 0;
492 while (!yppush_alarm_tripped && yppush_running_jobs) {
493 alarm(yppush_timeout);
494 yppush_alarm_tripped = 0;
499 yppush_alarm_tripped = 0;
500 while (!yppush_alarm_tripped && yppush_running_jobs >= yppush_jobs) {
501 alarm(yppush_timeout);
502 yppush_alarm_tripped = 0;
508 /* Cleared for takeoff: set everything in motion. */
509 if (yp_push(server, yppush_mapname, yppush_transid))
512 /* Bump the job counter and transaction ID. */
513 yppush_running_jobs++;
520 fprintf (stderr, "%s\n%s\n",
521 "usage: yppush [-d domain] [-t timeout] [-j #parallel jobs] [-h host]",
522 " [-p path] mapname");
527 * Entry point. (About time!)
530 main(int argc, char *argv[])
534 char myname[MAXHOSTNAMELEN];
537 struct hostlist *next;
539 struct hostlist *yppush_hostlist = NULL;
540 struct hostlist *tmp;
543 while ((ch = getopt(argc, argv, "d:j:p:h:t:v")) != -1) {
546 yppush_domain = optarg;
549 yppush_jobs = atoi(optarg);
550 if (yppush_jobs <= 0)
556 case 'h': /* we can handle multiple hosts */
557 if ((tmp = (struct hostlist *)malloc(sizeof(struct hostlist))) == NULL) {
558 yp_error("malloc failed");
561 tmp->name = strdup(optarg);
562 tmp->next = yppush_hostlist;
563 yppush_hostlist = tmp;
566 yppush_timeout = atoi(optarg);
580 yppush_mapname = argv[0];
582 if (yppush_mapname == NULL) {
583 /* "No guts, no glory." */
588 * If no domain was specified, try to find the default
589 * domain. If we can't find that, we're doomed and must bail.
591 if (yppush_domain == NULL) {
592 char *yppush_check_domain;
593 if (!yp_get_default_domain(&yppush_check_domain) &&
594 !_yp_check(&yppush_check_domain)) {
595 yp_error("no domain specified and NIS not running");
598 yp_get_default_domain(&yppush_domain);
601 /* Check to see that we are the master for this map. */
603 if (gethostname ((char *)&myname, sizeof(myname))) {
604 yp_error("failed to get name of local host: %s",
609 key.data = "YP_MASTER_NAME";
610 key.size = sizeof("YP_MASTER_NAME") - 1;
612 if (yp_get_record(yppush_domain, yppush_mapname,
613 &key, &data, 1) != YP_TRUE) {
614 yp_error("couldn't open %s map: %s", yppush_mapname,
619 if (strncmp(myname, data.data, data.size)) {
620 yp_error("warning: this host is not the master for %s",
627 yppush_master = malloc(data.size + 1);
628 strncpy(yppush_master, data.data, data.size);
629 yppush_master[data.size] = '\0';
631 /* Install some handy handlers. */
632 signal(SIGALRM, handler);
633 signal(SIGTERM, handler);
634 signal(SIGINT, handler);
635 signal(SIGABRT, handler);
638 * Set up the SIGIO handler. Make sure that some of the
639 * other signals are blocked while the handler is running so
640 * select() doesn't get interrupted.
642 sigemptyset(&sa.sa_mask);
643 sigaddset(&sa.sa_mask, SIGIO); /* Goes without saying. */
644 sigaddset(&sa.sa_mask, SIGPIPE);
645 sigaddset(&sa.sa_mask, SIGCHLD);
646 sigaddset(&sa.sa_mask, SIGALRM);
647 sigaddset(&sa.sa_mask, SIGINT);
648 sa.sa_handler = async_handler;
651 sigaction(SIGIO, &sa, NULL);
653 /* set initial transaction ID */
654 yppush_transid = time((time_t *)NULL);
656 if (yppush_hostlist) {
658 * Host list was specified on the command line:
659 * kick off the transfers by hand.
661 tmp = yppush_hostlist;
663 yppush_foreach(YP_TRUE, NULL, 0, tmp->name,
664 strlen(tmp->name), NULL);
669 * Do a yp_all() on the ypservers map and initiate a ypxfr
672 ypxfr_get_map("ypservers", yppush_domain,
673 "localhost", yppush_foreach);
677 yp_error("all jobs dispatched");
679 /* All done -- normal exit. */