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 $
33 * $DragonFly: src/usr.sbin/yppush/yppush_main.c,v 1.3 2005/11/24 22:23:02 swildner Exp $
44 #include <sys/socket.h>
45 #include <sys/fcntl.h>
47 #include <sys/param.h>
50 #include <rpc/pmap_clnt.h>
51 #include <rpcsvc/yp.h>
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 skip_master
= 0; /* Do not attempt to push map to master. */
65 int verbose
= 0; /* Toggle verbose mode. */
66 unsigned long yppush_transid
= 0;
67 int yppush_timeout
= 80; /* Default timeout. */
68 int yppush_jobs
= 1; /* Number of allowed concurrent jobs. */
69 int yppush_running_jobs
= 0; /* Number of currently running jobs. */
71 /* Structure for holding information about a running job. */
76 unsigned long prognum
;
83 struct jobs
*yppush_joblist
; /* Linked list of running jobs. */
85 static int yppush_svc_run(int);
88 * Local error messages.
91 yppusherr_string(int err
)
95 return("transfer or callback timed out");
97 return("failed to contact ypserv");
99 return("no such host");
101 return("portmapper failure");
103 return("unknown error code");
108 * Report state of a job.
111 yppush_show_status(ypxfrstat status
, unsigned long tid
)
115 job
= yppush_joblist
;
117 while (job
!= NULL
) {
124 yp_error("warning: received callback with invalid transaction ID: %lu",
130 yp_error("warning: received callback with duplicate transaction ID: %lu",
136 yp_error("checking return status: transaction ID: %lu",
140 if (status
!= YPPUSH_SUCC
|| verbose
) {
141 yp_error("transfer of map %s to server %s %s",
142 job
->map
, job
->server
, status
== YPPUSH_SUCC
?
143 "succeeded" : "failed");
144 yp_error("status returned by ypxfr: %s", status
> YPPUSH_AGE
?
145 yppusherr_string(status
) :
146 ypxfrerr_string(status
));
151 svc_unregister(job
->prognum
, 1);
153 yppush_running_jobs
--;
162 int still_pending
= 1;
164 /* Let all the information trickle in. */
165 while (!now
&& still_pending
) {
166 jptr
= yppush_joblist
;
169 if (jptr
->polled
== 0) {
172 yp_error("%s has not responded",
176 yp_error("%s has responded",
183 yp_error("%d transfer%sstill pending",
185 still_pending
> 1 ? "s " : " ");
186 if (yppush_svc_run (YPPUSH_RESPONSE_TIMEOUT
) == 0) {
187 yp_error("timed out");
192 yp_error("all transfers complete");
198 /* All stats collected and reported -- kill all the stragglers. */
199 jptr
= yppush_joblist
;
202 yp_error("warning: exiting with transfer \
203 to %s (transid = %lu) still pending", jptr
->server
, jptr
->tid
);
204 svc_unregister(jptr
->prognum
, 1);
212 * Handler for 'normal' signals.
223 * Dispatch loop for callback RPC services.
227 * >0 request serviced
230 yppush_svc_run(int timeout_secs
)
234 struct timeval timeout
;
237 timeout
.tv_sec
= timeout_secs
;
241 rc
= select(svc_maxfd
+ 1, &readfds
, NULL
, NULL
, &timeout
);
246 yp_error("select failed: %s", strerror(errno
));
249 yp_error("select() timed out");
252 svc_getreqset(&readfds
);
259 * RPC service routines for callbacks.
262 yppushproc_null_1_svc(void *argp
, struct svc_req
*rqstp
)
264 static char * result
;
265 /* Do nothing -- RPC conventions call for all a null proc. */
266 return((void *) &result
);
270 yppushproc_xfrresp_1_svc(yppushresp_xfr
*argp
, struct svc_req
*rqstp
)
272 static char * result
;
273 yppush_show_status(argp
->status
, argp
->transid
);
274 return((void *) &result
);
278 * Transmit a YPPROC_XFR request to ypserv.
281 yppush_send_xfr(struct jobs
*job
)
284 /* ypresp_xfr *resp; */
288 struct timeval timeout
;
294 * The ypreq_xfr structure has a member of type map_parms,
295 * which seems to require the order number of the map.
296 * It isn't actually used at the other end (at least the
297 * FreeBSD ypserv doesn't use it) but we fill it in here
298 * for the sake of completeness.
300 key
.data
= "YP_LAST_MODIFIED";
301 key
.size
= sizeof ("YP_LAST_MODIFIED") - 1;
303 if (yp_get_record(yppush_domain
, yppush_mapname
, &key
, &data
,
305 yp_error("failed to read order number from %s: %s: %s",
306 yppush_mapname
, yperr_string(yp_errno
),
311 /* Fill in the request arguments */
312 req
.map_parms
.ordernum
= atoi(data
.data
);
313 req
.map_parms
.domain
= yppush_domain
;
314 req
.map_parms
.peer
= yppush_master
;
315 req
.map_parms
.map
= job
->map
;
316 req
.transid
= job
->tid
;
317 req
.prog
= job
->prognum
;
318 req
.port
= job
->port
;
320 /* Get a handle to the remote ypserv. */
321 if ((clnt
= clnt_create(job
->server
, YPPROG
, YPVERS
, "udp")) == NULL
) {
322 yp_error("%s: %s",job
->server
,clnt_spcreateerror("couldn't \
323 create udp handle to NIS server"));
324 switch (rpc_createerr
.cf_stat
) {
325 case RPC_UNKNOWNHOST
:
326 job
->stat
= YPPUSH_NOHOST
;
328 case RPC_PMAPFAILURE
:
329 job
->stat
= YPPUSH_PMAP
;
332 job
->stat
= YPPUSH_RPC
;
339 * Reduce timeout to nothing since we may not
340 * get a response from ypserv and we don't want to block.
342 if (clnt_control(clnt
, CLSET_TIMEOUT
, (char *)&timeout
) == FALSE
)
343 yp_error("failed to set timeout on ypproc_xfr call");
345 /* Invoke the ypproc_xfr service. */
346 if (ypproc_xfr_2(&req
, clnt
) == NULL
) {
347 clnt_geterr(clnt
, &err
);
348 if (err
.re_status
!= RPC_SUCCESS
&&
349 err
.re_status
!= RPC_TIMEDOUT
) {
350 yp_error("%s: %s", job
->server
, clnt_sperror(clnt
,
352 job
->stat
= YPPUSH_YPSERV
;
364 * Main driver function. Register the callback service, add the transfer
365 * request to the internal list, send the YPPROC_XFR request to ypserv
366 * do other magic things.
369 yp_push(char *server
, char *map
, unsigned long tid
)
371 unsigned long prognum
;
372 int sock
= RPC_ANYSOCK
;
376 /* Register the job in our linked list of jobs. */
378 /* First allocate job structure */
379 if ((job
= (struct jobs
*)malloc(sizeof (struct jobs
))) == NULL
) {
380 yp_error("malloc failed");
385 * Register the callback service on the first free transient
388 xprt
= svcudp_create(sock
);
389 for (prognum
= 0x40000000; prognum
< 0x5FFFFFFF; prognum
++) {
390 if (svc_register(xprt
, prognum
, 1,
391 yppush_xfrrespprog_1
, IPPROTO_UDP
) == TRUE
)
394 if (prognum
== 0x5FFFFFFF) {
395 yp_error ("can't register yppush_xfrrespprog_1");
399 /* Initialize the info for this job. */
402 job
->port
= xprt
->xp_port
;
403 job
->server
= strdup(server
);
404 job
->map
= strdup(map
);
405 job
->prognum
= prognum
;
407 job
->next
= yppush_joblist
;
408 yppush_joblist
= job
;
411 yp_error("initiating transfer: %s -> %s (transid = %lu)",
412 yppush_mapname
, server
, tid
);
416 * Send the XFR request to ypserv. We don't have to wait for
417 * a response here since we handle them asynchronously.
420 if (yppush_send_xfr(job
)){
421 /* Transfer request blew up. */
422 yppush_show_status(job
->stat
? job
->stat
:
423 YPPUSH_YPSERV
,job
->tid
);
426 yp_error("%s has been called", server
);
433 * Called for each entry in the ypservers map from yp_get_map(), which
434 * is our private yp_all() routine.
437 yppush_foreach(int status
, char *key
, int keylen
, char *val
, int vallen
,
440 char server
[YPMAXRECORD
+ 2];
442 if (status
!= YP_TRUE
)
445 snprintf(server
, sizeof(server
), "%.*s", vallen
, val
);
446 if (skip_master
&& strcasecmp(server
, yppush_master
) == 0)
450 * Restrict the number of concurrent jobs: if yppush_jobs number
451 * of jobs have already been dispatched and are still pending,
452 * wait for one of them to finish so we can reuse its slot.
454 while (yppush_running_jobs
>= yppush_jobs
&& (yppush_svc_run (yppush_timeout
) > 0))
457 /* Cleared for takeoff: set everything in motion. */
458 if (yp_push(server
, yppush_mapname
, yppush_transid
))
461 /* Bump the job counter and transaction ID. */
462 yppush_running_jobs
++;
470 fprintf (stderr
, "%s\n%s\n",
471 "usage: yppush [-d domain] [-t timeout] [-j #parallel jobs] [-h host]",
472 " [-p path] mapname");
477 * Entry point. (About time!)
480 main(int argc
, char *argv
[])
484 char myname
[MAXHOSTNAMELEN
];
487 struct hostlist
*next
;
489 struct hostlist
*yppush_hostlist
= NULL
;
490 struct hostlist
*tmp
;
493 while ((ch
= getopt(argc
, argv
, "d:j:p:h:t:v")) != -1) {
496 yppush_domain
= optarg
;
499 yppush_jobs
= atoi(optarg
);
500 if (yppush_jobs
<= 0)
506 case 'h': /* we can handle multiple hosts */
507 if ((tmp
= (struct hostlist
*)malloc(sizeof(struct hostlist
))) == NULL
) {
508 yp_error("malloc failed");
511 tmp
->name
= strdup(optarg
);
512 tmp
->next
= yppush_hostlist
;
513 yppush_hostlist
= tmp
;
516 yppush_timeout
= atoi(optarg
);
530 yppush_mapname
= argv
[0];
532 if (yppush_mapname
== NULL
) {
533 /* "No guts, no glory." */
538 * If no domain was specified, try to find the default
539 * domain. If we can't find that, we're doomed and must bail.
541 if (yppush_domain
== NULL
) {
542 char *yppush_check_domain
;
543 if (!yp_get_default_domain(&yppush_check_domain
) &&
544 !_yp_check(&yppush_check_domain
)) {
545 yp_error("no domain specified and NIS not running");
548 yp_get_default_domain(&yppush_domain
);
551 /* Check to see that we are the master for this map. */
553 if (gethostname ((char *)&myname
, sizeof(myname
))) {
554 yp_error("failed to get name of local host: %s",
559 key
.data
= "YP_MASTER_NAME";
560 key
.size
= sizeof("YP_MASTER_NAME") - 1;
562 if (yp_get_record(yppush_domain
, yppush_mapname
,
563 &key
, &data
, 1) != YP_TRUE
) {
564 yp_error("couldn't open %s map: %s", yppush_mapname
,
569 if (strncasecmp(myname
, data
.data
, data
.size
) == 0) {
570 /* I am master server, and no explicit host list was
571 specified: do not push map to myself -- this will
572 fail with YPPUSH_AGE anyway. */
573 if (yppush_hostlist
== NULL
)
576 yp_error("warning: this host is not the master for %s",
583 yppush_master
= malloc(data
.size
+ 1);
584 strncpy(yppush_master
, data
.data
, data
.size
);
585 yppush_master
[data
.size
] = '\0';
587 /* Install some handy handlers. */
588 signal(SIGTERM
, handler
);
589 signal(SIGINT
, handler
);
591 /* set initial transaction ID */
592 yppush_transid
= time(NULL
);
594 if (yppush_hostlist
) {
596 * Host list was specified on the command line:
597 * kick off the transfers by hand.
599 tmp
= yppush_hostlist
;
601 yppush_foreach(YP_TRUE
, NULL
, 0, tmp
->name
,
602 strlen(tmp
->name
), NULL
);
607 * Do a yp_all() on the ypservers map and initiate a ypxfr
610 ypxfr_get_map("ypservers", yppush_domain
,
611 "localhost", yppush_foreach
);
615 yp_error("all jobs dispatched");
617 /* All done -- normal exit. */