getc(3) and putc(3): document xxx_unlocked() functions from FreeBSD
[dragonfly.git] / usr.sbin / yppush / yppush_main.c
blob4b5264007dd88ab0b88f2c9c9ab98b0a42316b1b
1 /*
2 * Copyright (c) 1995
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
7 * are met:
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
30 * SUCH DAMAGE.
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 $
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <sys/socket.h>
45 #include <sys/fcntl.h>
46 #include <sys/wait.h>
47 #include <sys/param.h>
48 #include <rpc/rpc.h>
49 #include <rpc/clnt.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";
57 int debug = 1;
58 int _rpcpmstart = 0;
59 const 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. */
72 struct jobs {
73 unsigned long tid;
74 int port;
75 ypxfrstat stat;
76 unsigned long prognum;
77 char *server;
78 char *map;
79 int polled;
80 struct jobs *next;
83 struct jobs *yppush_joblist; /* Linked list of running jobs. */
85 static int yppush_svc_run(int);
88 * Local error messages.
90 static const char *
91 yppusherr_string(int err)
93 switch (err) {
94 case YPPUSH_TIMEDOUT:
95 return("transfer or callback timed out");
96 case YPPUSH_YPSERV:
97 return("failed to contact ypserv");
98 case YPPUSH_NOHOST:
99 return("no such host");
100 case YPPUSH_PMAP:
101 return("portmapper failure");
102 default:
103 return("unknown error code");
108 * Report state of a job.
110 static int
111 yppush_show_status(ypxfrstat status, unsigned long tid)
113 struct jobs *job;
115 job = yppush_joblist;
117 while (job != NULL) {
118 if (job->tid == tid)
119 break;
120 job = job->next;
123 if (job == NULL) {
124 yp_error("warning: received callback with invalid transaction ID: %lu",
125 tid);
126 return (0);
129 if (job->polled) {
130 yp_error("warning: received callback with duplicate transaction ID: %lu",
131 tid);
132 return (0);
135 if (verbose > 1) {
136 yp_error("checking return status: transaction ID: %lu",
137 job->tid);
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));
149 job->polled = 1;
151 svc_unregister(job->prognum, 1);
153 yppush_running_jobs--;
154 return(0);
157 /* Exit routine. */
158 static void
159 yppush_exit(int now)
161 struct jobs *jptr;
162 int still_pending = 1;
164 /* Let all the information trickle in. */
165 while (!now && still_pending) {
166 jptr = yppush_joblist;
167 still_pending = 0;
168 while (jptr) {
169 if (jptr->polled == 0) {
170 still_pending++;
171 if (verbose > 1)
172 yp_error("%s has not responded",
173 jptr->server);
174 } else {
175 if (verbose > 1)
176 yp_error("%s has responded",
177 jptr->server);
179 jptr = jptr->next;
181 if (still_pending) {
182 if (verbose > 1)
183 yp_error("%d transfer%sstill pending",
184 still_pending,
185 still_pending > 1 ? "s " : " ");
186 if (yppush_svc_run (YPPUSH_RESPONSE_TIMEOUT) == 0) {
187 yp_error("timed out");
188 now = 1;
190 } else {
191 if (verbose)
192 yp_error("all transfers complete");
193 break;
198 /* All stats collected and reported -- kill all the stragglers. */
199 jptr = yppush_joblist;
200 while (jptr) {
201 if (!jptr->polled)
202 yp_error("warning: exiting with transfer \
203 to %s (transid = %lu) still pending", jptr->server, jptr->tid);
204 svc_unregister(jptr->prognum, 1);
205 jptr = jptr->next;
208 exit(0);
212 * Handler for 'normal' signals.
215 static void
216 handler(int sig)
218 yppush_exit (1);
219 return;
223 * Dispatch loop for callback RPC services.
224 * Return value:
225 * -1 error
226 * 0 timeout
227 * >0 request serviced
229 static int
230 yppush_svc_run(int timeout_secs)
232 int rc;
233 fd_set readfds;
234 struct timeval timeout;
236 timeout.tv_usec = 0;
237 timeout.tv_sec = timeout_secs;
239 retry:
240 readfds = svc_fdset;
241 rc = select(svc_maxfd + 1, &readfds, NULL, NULL, &timeout);
242 switch (rc) {
243 case -1:
244 if (errno == EINTR)
245 goto retry;
246 yp_error("select failed: %s", strerror(errno));
247 break;
248 case 0:
249 yp_error("select() timed out");
250 break;
251 default:
252 svc_getreqset(&readfds);
253 break;
255 return rc;
259 * RPC service routines for callbacks.
261 void *
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);
269 void *
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.
280 static int
281 yppush_send_xfr(struct jobs *job)
283 ypreq_xfr req;
284 /* ypresp_xfr *resp; */
285 DBT key, data;
286 CLIENT *clnt;
287 struct rpc_err err;
288 struct timeval timeout;
290 timeout.tv_usec = 0;
291 timeout.tv_sec = 0;
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,
304 1) != YP_TRUE) {
305 yp_error("failed to read order number from %s: %s: %s",
306 yppush_mapname, yperr_string(yp_errno),
307 strerror(errno));
308 return(1);
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;
327 break;
328 case RPC_PMAPFAILURE:
329 job->stat = YPPUSH_PMAP;
330 break;
331 default:
332 job->stat = YPPUSH_RPC;
333 break;
335 return(1);
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,
351 "yp_xfr failed"));
352 job->stat = YPPUSH_YPSERV;
353 clnt_destroy(clnt);
354 return(1);
358 clnt_destroy(clnt);
360 return(0);
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;
373 SVCXPRT *xprt;
374 struct jobs *job;
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");
381 yppush_exit (1);
385 * Register the callback service on the first free transient
386 * program number.
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)
392 break;
394 if (prognum == 0x5FFFFFFF) {
395 yp_error ("can't register yppush_xfrrespprog_1");
396 yppush_exit (1);
399 /* Initialize the info for this job. */
400 job->stat = 0;
401 job->tid = tid;
402 job->port = xprt->xp_port;
403 job->server = strdup(server);
404 job->map = strdup(map);
405 job->prognum = prognum;
406 job->polled = 0;
407 job->next = yppush_joblist;
408 yppush_joblist = job;
410 if (verbose) {
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);
424 } else {
425 if (verbose > 1)
426 yp_error("%s has been called", server);
429 return(0);
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,
438 char *data)
440 char server[YPMAXRECORD + 2];
442 if (status != YP_TRUE)
443 return (status);
445 snprintf(server, sizeof(server), "%.*s", vallen, val);
446 if (skip_master && strcasecmp(server, yppush_master) == 0)
447 return (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))
459 return(yp_errno);
461 /* Bump the job counter and transaction ID. */
462 yppush_running_jobs++;
463 yppush_transid++;
464 return (0);
467 static void
468 usage(void)
470 fprintf (stderr, "%s\n%s\n",
471 "usage: yppush [-d domain] [-t timeout] [-j #parallel jobs] [-h host]",
472 " [-p path] mapname");
473 exit(1);
477 * Entry point. (About time!)
480 main(int argc, char *argv[])
482 int ch;
483 DBT key, data;
484 char myname[MAXHOSTNAMELEN];
485 struct hostlist {
486 char *name;
487 struct hostlist *next;
489 struct hostlist *yppush_hostlist = NULL;
490 struct hostlist *tmp;
491 struct sigaction sa;
493 while ((ch = getopt(argc, argv, "d:j:p:h:t:v")) != -1) {
494 switch (ch) {
495 case 'd':
496 yppush_domain = optarg;
497 break;
498 case 'j':
499 yppush_jobs = atoi(optarg);
500 if (yppush_jobs <= 0)
501 yppush_jobs = 1;
502 break;
503 case 'p':
504 yp_dir = optarg;
505 break;
506 case 'h': /* we can handle multiple hosts */
507 if ((tmp = (struct hostlist *)malloc(sizeof(struct hostlist))) == NULL) {
508 yp_error("malloc failed");
509 yppush_exit(1);
511 tmp->name = strdup(optarg);
512 tmp->next = yppush_hostlist;
513 yppush_hostlist = tmp;
514 break;
515 case 't':
516 yppush_timeout = atoi(optarg);
517 break;
518 case 'v':
519 verbose++;
520 break;
521 default:
522 usage();
523 break;
527 argc -= optind;
528 argv += optind;
530 yppush_mapname = argv[0];
532 if (yppush_mapname == NULL) {
533 /* "No guts, no glory." */
534 usage();
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");
546 usage();
547 } else
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",
555 strerror(errno));
556 yppush_exit(1);
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,
565 strerror(errno));
566 yppush_exit(1);
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)
574 skip_master = 1;
575 } else {
576 yp_error("warning: this host is not the master for %s",
577 yppush_mapname);
578 #ifdef NITPICKY
579 yppush_exit(1);
580 #endif
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;
600 while (tmp) {
601 yppush_foreach(YP_TRUE, NULL, 0, tmp->name,
602 strlen(tmp->name), NULL);
603 tmp = tmp->next;
605 } else {
607 * Do a yp_all() on the ypservers map and initiate a ypxfr
608 * for each one.
610 ypxfr_get_map("ypservers", yppush_domain,
611 "localhost", yppush_foreach);
614 if (verbose > 1)
615 yp_error("all jobs dispatched");
617 /* All done -- normal exit. */
618 yppush_exit(0);
620 /* Just in case. */
621 exit(0);