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
32 * $FreeBSD: src/usr.sbin/yppush/yppush_main.c,v 1.22 2006/08/16 12:58:41 thomas Exp $
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 #include <rpcsvc/ypclnt.h>
52 #include "ypxfr_extern.h"
53 #include "yppush_extern.h"
55 char *progname
= "yppush";
58 const char *yp_dir
= _PATH_YP
;
60 char *yppush_mapname
= NULL
; /* Map to transfer. */
61 char *yppush_domain
= NULL
; /* Domain in which map resides. */
62 char *yppush_master
= NULL
; /* Master NIS server for said domain. */
63 int skip_master
= 0; /* Do not attempt to push map to master. */
64 int verbose
= 0; /* Toggle verbose mode. */
65 unsigned long yppush_transid
= 0;
66 int yppush_timeout
= 80; /* Default timeout. */
67 int yppush_jobs
= 1; /* Number of allowed concurrent jobs. */
68 int yppush_running_jobs
= 0; /* Number of currently running jobs. */
70 /* Structure for holding information about a running job. */
75 unsigned long prognum
;
82 struct jobs
*yppush_joblist
; /* Linked list of running jobs. */
84 static int yppush_svc_run(int);
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
;
116 while (job
!= NULL
) {
123 yp_error("warning: received callback with invalid transaction ID: %lu",
129 yp_error("warning: received callback with duplicate transaction ID: %lu",
135 yp_error("checking return status: transaction ID: %lu",
139 if (status
!= YPXFR_SUCC
|| verbose
) {
140 yp_error("transfer of map %s to server %s %s",
141 job
->map
, job
->server
, status
== YPXFR_SUCC
?
142 "succeeded" : "failed");
143 yp_error("status returned by ypxfr: %s", status
> YPXFR_AGE
?
144 yppusherr_string(status
) :
145 ypxfrerr_string(status
));
150 svc_unregister(job
->prognum
, 1);
152 yppush_running_jobs
--;
161 int still_pending
= 1;
163 /* Let all the information trickle in. */
164 while (!now
&& still_pending
) {
165 jptr
= yppush_joblist
;
168 if (jptr
->polled
== 0) {
171 yp_error("%s has not responded",
175 yp_error("%s has responded",
182 yp_error("%d transfer%sstill pending",
184 still_pending
> 1 ? "s " : " ");
185 if (yppush_svc_run (YPPUSH_RESPONSE_TIMEOUT
) == 0) {
186 yp_error("timed out");
191 yp_error("all transfers complete");
197 /* All stats collected and reported -- kill all the stragglers. */
198 jptr
= yppush_joblist
;
201 yp_error("warning: exiting with transfer \
202 to %s (transid = %lu) still pending", jptr
->server
, jptr
->tid
);
203 svc_unregister(jptr
->prognum
, 1);
211 * Handler for 'normal' signals.
222 * Dispatch loop for callback RPC services.
226 * >0 request serviced
229 yppush_svc_run(int timeout_secs
)
233 struct timeval timeout
;
236 timeout
.tv_sec
= timeout_secs
;
240 rc
= select(svc_maxfd
+ 1, &readfds
, NULL
, NULL
, &timeout
);
245 yp_error("select failed: %s", strerror(errno
));
248 yp_error("select() timed out");
251 svc_getreqset(&readfds
);
258 * RPC service routines for callbacks.
261 yppushproc_null_1_svc(void *argp
, struct svc_req
*rqstp
)
263 static char * result
;
264 /* Do nothing -- RPC conventions call for all a null proc. */
265 return((void *) &result
);
269 yppushproc_xfrresp_1_svc(yppushresp_xfr
*argp
, struct svc_req
*rqstp
)
271 static char * result
;
272 yppush_show_status(argp
->status
, argp
->transid
);
273 return((void *) &result
);
277 * Transmit a YPPROC_XFR request to ypserv.
280 yppush_send_xfr(struct jobs
*job
)
283 /* ypresp_xfr *resp; */
287 struct timeval timeout
;
293 * The ypreq_xfr structure has a member of type map_parms,
294 * which seems to require the order number of the map.
295 * It isn't actually used at the other end (at least the
296 * FreeBSD ypserv doesn't use it) but we fill it in here
297 * for the sake of completeness.
299 key
.data
= "YP_LAST_MODIFIED";
300 key
.size
= sizeof ("YP_LAST_MODIFIED") - 1;
302 if (yp_get_record(yppush_domain
, yppush_mapname
, &key
, &data
,
304 yp_error("failed to read order number from %s: %s: %s",
305 yppush_mapname
, yperr_string(yp_errno
),
310 /* Fill in the request arguments */
311 req
.map_parms
.ordernum
= atoi(data
.data
);
312 req
.map_parms
.domain
= yppush_domain
;
313 req
.map_parms
.peer
= yppush_master
;
314 req
.map_parms
.map
= job
->map
;
315 req
.transid
= job
->tid
;
316 req
.prog
= job
->prognum
;
317 req
.port
= job
->port
;
319 /* Get a handle to the remote ypserv. */
320 if ((clnt
= clnt_create(job
->server
, YPPROG
, YPVERS
, "udp")) == NULL
) {
321 yp_error("%s: %s",job
->server
,clnt_spcreateerror("couldn't \
322 create udp handle to NIS server"));
323 switch (rpc_createerr
.cf_stat
) {
324 case RPC_UNKNOWNHOST
:
325 job
->stat
= YPPUSH_NOHOST
;
327 case RPC_PMAPFAILURE
:
328 job
->stat
= YPPUSH_PMAP
;
331 job
->stat
= YPPUSH_RPC
;
338 * Reduce timeout to nothing since we may not
339 * get a response from ypserv and we don't want to block.
341 if (clnt_control(clnt
, CLSET_TIMEOUT
, (char *)&timeout
) == FALSE
)
342 yp_error("failed to set timeout on ypproc_xfr call");
344 /* Invoke the ypproc_xfr service. */
345 if (ypproc_xfr_2(&req
, clnt
) == NULL
) {
346 clnt_geterr(clnt
, &err
);
347 if (err
.re_status
!= RPC_SUCCESS
&&
348 err
.re_status
!= RPC_TIMEDOUT
) {
349 yp_error("%s: %s", job
->server
, clnt_sperror(clnt
,
351 job
->stat
= YPPUSH_YPSERV
;
363 * Main driver function. Register the callback service, add the transfer
364 * request to the internal list, send the YPPROC_XFR request to ypserv
365 * do other magic things.
368 yp_push(char *server
, char *map
, unsigned long tid
)
370 unsigned long prognum
;
371 int sock
= RPC_ANYSOCK
;
375 /* Register the job in our linked list of jobs. */
377 /* First allocate job structure */
378 if ((job
= (struct jobs
*)malloc(sizeof (struct jobs
))) == NULL
) {
379 yp_error("malloc failed");
384 * Register the callback service on the first free transient
387 xprt
= svcudp_create(sock
);
388 for (prognum
= 0x40000000; prognum
< 0x5FFFFFFF; prognum
++) {
389 if (svc_register(xprt
, prognum
, 1,
390 yppush_xfrrespprog_1
, IPPROTO_UDP
) == TRUE
)
393 if (prognum
== 0x5FFFFFFF) {
394 yp_error ("can't register yppush_xfrrespprog_1");
398 /* Initialize the info for this job. */
401 job
->port
= xprt
->xp_port
;
402 job
->server
= strdup(server
);
403 job
->map
= strdup(map
);
404 job
->prognum
= prognum
;
406 job
->next
= yppush_joblist
;
407 yppush_joblist
= job
;
410 yp_error("initiating transfer: %s -> %s (transid = %lu)",
411 yppush_mapname
, server
, tid
);
415 * Send the XFR request to ypserv. We don't have to wait for
416 * a response here since we handle them asynchronously.
419 if (yppush_send_xfr(job
)){
420 /* Transfer request blew up. */
421 yppush_show_status(job
->stat
? job
->stat
:
422 YPPUSH_YPSERV
,job
->tid
);
425 yp_error("%s has been called", server
);
432 * Called for each entry in the ypservers map from yp_get_map(), which
433 * is our private yp_all() routine.
436 yppush_foreach(int status
, char *key
, int keylen
, char *val
, int vallen
,
439 char server
[YPMAXRECORD
+ 2];
441 if (status
!= YP_TRUE
)
444 snprintf(server
, sizeof(server
), "%.*s", vallen
, val
);
445 if (skip_master
&& strcasecmp(server
, yppush_master
) == 0)
449 * Restrict the number of concurrent jobs: if yppush_jobs number
450 * of jobs have already been dispatched and are still pending,
451 * wait for one of them to finish so we can reuse its slot.
453 while (yppush_running_jobs
>= yppush_jobs
&& (yppush_svc_run (yppush_timeout
) > 0))
456 /* Cleared for takeoff: set everything in motion. */
457 if (yp_push(server
, yppush_mapname
, yppush_transid
))
460 /* Bump the job counter and transaction ID. */
461 yppush_running_jobs
++;
469 fprintf (stderr
, "%s\n%s\n",
470 "usage: yppush [-d domain] [-t timeout] [-j #parallel jobs] [-h host]",
471 " [-p path] mapname");
476 * Entry point. (About time!)
479 main(int argc
, char *argv
[])
483 char myname
[MAXHOSTNAMELEN
];
486 struct hostlist
*next
;
488 struct hostlist
*yppush_hostlist
= NULL
;
489 struct hostlist
*tmp
;
491 while ((ch
= getopt(argc
, argv
, "d:j:p:h:t:v")) != -1) {
494 yppush_domain
= optarg
;
497 yppush_jobs
= atoi(optarg
);
498 if (yppush_jobs
<= 0)
504 case 'h': /* we can handle multiple hosts */
505 if ((tmp
= (struct hostlist
*)malloc(sizeof(struct hostlist
))) == NULL
) {
506 yp_error("malloc failed");
509 tmp
->name
= strdup(optarg
);
510 tmp
->next
= yppush_hostlist
;
511 yppush_hostlist
= tmp
;
514 yppush_timeout
= atoi(optarg
);
528 yppush_mapname
= argv
[0];
530 if (yppush_mapname
== NULL
) {
531 /* "No guts, no glory." */
536 * If no domain was specified, try to find the default
537 * domain. If we can't find that, we're doomed and must bail.
539 if (yppush_domain
== NULL
) {
540 char *yppush_check_domain
;
541 if (!yp_get_default_domain(&yppush_check_domain
) &&
542 !_yp_check(&yppush_check_domain
)) {
543 yp_error("no domain specified and NIS not running");
546 yp_get_default_domain(&yppush_domain
);
549 /* Check to see that we are the master for this map. */
551 if (gethostname ((char *)&myname
, sizeof(myname
))) {
552 yp_error("failed to get name of local host: %s",
557 key
.data
= "YP_MASTER_NAME";
558 key
.size
= sizeof("YP_MASTER_NAME") - 1;
560 if (yp_get_record(yppush_domain
, yppush_mapname
,
561 &key
, &data
, 1) != YP_TRUE
) {
562 yp_error("couldn't open %s map: %s", yppush_mapname
,
567 if (strncasecmp(myname
, data
.data
, data
.size
) == 0) {
568 /* I am master server, and no explicit host list was
569 specified: do not push map to myself -- this will
570 fail with YPPUSH_AGE anyway. */
571 if (yppush_hostlist
== NULL
)
574 yp_error("warning: this host is not the master for %s",
581 yppush_master
= malloc(data
.size
+ 1);
582 strncpy(yppush_master
, data
.data
, data
.size
);
583 yppush_master
[data
.size
] = '\0';
585 /* Install some handy handlers. */
586 signal(SIGTERM
, handler
);
587 signal(SIGINT
, handler
);
589 /* set initial transaction ID */
590 yppush_transid
= time(NULL
);
592 if (yppush_hostlist
) {
594 * Host list was specified on the command line:
595 * kick off the transfers by hand.
597 tmp
= yppush_hostlist
;
599 yppush_foreach(YP_TRUE
, NULL
, 0, tmp
->name
,
600 strlen(tmp
->name
), NULL
);
605 * Do a yp_all() on the ypservers map and initiate a ypxfr
608 ypxfr_get_map("ypservers", yppush_domain
,
609 "localhost", yppush_foreach
);
613 yp_error("all jobs dispatched");
615 /* All done -- normal exit. */