xref: /onnv-gate/usr/src/cmd/rpcsvc/rwall.c (revision 1914:8a8c5f225b1b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1914Scasper  * Common Development and Distribution License (the "License").
6*1914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
20*1914Scasper  */
21*1914Scasper /*
22*1914Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
260Sstevel@tonic-gate /* All Rights Reserved */
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
290Sstevel@tonic-gate  * The Regents of the University of California
300Sstevel@tonic-gate  * All Rights Reserved
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
330Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
340Sstevel@tonic-gate  * contributors.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * rwall.c
410Sstevel@tonic-gate  *	The client rwall program
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
45*1914Scasper #include <stdio_ext.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <stdlib.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate #include <thread.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <rpc/rpc.h>
520Sstevel@tonic-gate #include <signal.h>
530Sstevel@tonic-gate #include <pwd.h>
540Sstevel@tonic-gate #include <rpcsvc/rwall.h>
550Sstevel@tonic-gate #include <netconfig.h>
560Sstevel@tonic-gate #include <netdb.h>
570Sstevel@tonic-gate #include <sys/time.h>
580Sstevel@tonic-gate #include <sys/resource.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static void init_who(void);
610Sstevel@tonic-gate static void doall(void);
620Sstevel@tonic-gate static void doit(char *);
630Sstevel@tonic-gate static void *do_one(void *);
640Sstevel@tonic-gate static void usage(void);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	PATIENCE 10
670Sstevel@tonic-gate #define	MAX_THREADS 1024
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static mutex_t tty = DEFAULTMUTEX;
700Sstevel@tonic-gate static char who[9] = "???";
710Sstevel@tonic-gate static char *path;
720Sstevel@tonic-gate static mutex_t thr_mtx = DEFAULTMUTEX;
730Sstevel@tonic-gate static int thread_count = 8;	/* fudge factor for system threads/fds */
740Sstevel@tonic-gate static int qflag = 0;		/* quiet: we don't care about errors */
750Sstevel@tonic-gate 
76631Speteh int
main(int argc,char * argv[])77631Speteh main(int argc, char *argv[])
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	int msize;
800Sstevel@tonic-gate 	char buf[BUFSIZ+1];
81631Speteh 	int i;
820Sstevel@tonic-gate 	char hostname[256];
830Sstevel@tonic-gate 	int hflag;
840Sstevel@tonic-gate 	struct rlimit rl;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (argc < 2)
870Sstevel@tonic-gate 		usage();
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
900Sstevel@tonic-gate 		rl.rlim_cur = (rl.rlim_max < MAX_THREADS ?
910Sstevel@tonic-gate 		    rl.rlim_max : MAX_THREADS);
920Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rl);
93*1914Scasper 		(void) enable_extended_FILE_stdio(-1, -1);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	(void) gethostname(hostname, sizeof (hostname));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	init_who();
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	msize = snprintf(buf, sizeof (buf), "From %s@%s:  ", who, hostname);
1010Sstevel@tonic-gate 	while ((i = getchar()) != EOF) {
1020Sstevel@tonic-gate 		if (msize >= (sizeof (buf) - 1)) {
1030Sstevel@tonic-gate 			(void) fprintf(stderr, "Message too long\n");
1040Sstevel@tonic-gate 			exit(1);
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 		buf[msize++] = i;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	buf[msize] = '\0';
1090Sstevel@tonic-gate 	path = buf;
1100Sstevel@tonic-gate 	hflag = 1;
1110Sstevel@tonic-gate 	while (argc > 1) {
1120Sstevel@tonic-gate 		if (argv[1][0] == '-') {
1130Sstevel@tonic-gate 			switch (argv[1][1]) {
1140Sstevel@tonic-gate 				case 'h':
1150Sstevel@tonic-gate 					hflag = 1;
1160Sstevel@tonic-gate 					break;
1170Sstevel@tonic-gate 				case 'n':
1180Sstevel@tonic-gate 					hflag = 0;
1190Sstevel@tonic-gate 					break;
1200Sstevel@tonic-gate 				case 'q':
1210Sstevel@tonic-gate 					qflag = 1;
1220Sstevel@tonic-gate 					break;
1230Sstevel@tonic-gate 				default:
1240Sstevel@tonic-gate 					usage();
1250Sstevel@tonic-gate 					break;
1260Sstevel@tonic-gate 			}
1270Sstevel@tonic-gate 			argc--;
1280Sstevel@tonic-gate 			argv++;
1290Sstevel@tonic-gate 			continue;
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 		if (hflag) {
1320Sstevel@tonic-gate 			doit(argv[1]);
1330Sstevel@tonic-gate 		} else {
1340Sstevel@tonic-gate 			char *machine, *user, *domain;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 			(void) setnetgrent(argv[1]);
1370Sstevel@tonic-gate 			while (getnetgrent(&machine, &user, &domain)) {
1380Sstevel@tonic-gate 				if (machine)
1390Sstevel@tonic-gate 					doit(machine);
1400Sstevel@tonic-gate 				else
1410Sstevel@tonic-gate 					doall();
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 			(void) endnetgrent();
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 		argc--;
1460Sstevel@tonic-gate 		argv++;
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	thr_exit(NULL);
1490Sstevel@tonic-gate 	return (0);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate static void
init_who(void)1530Sstevel@tonic-gate init_who(void)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	char *wp;
1560Sstevel@tonic-gate 	struct passwd *pwd;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	wp = getlogin();
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (wp != NULL)
1610Sstevel@tonic-gate 		(void) strncpy(who, wp, sizeof (who));
1620Sstevel@tonic-gate 	else {
1630Sstevel@tonic-gate 		pwd = getpwuid(getuid());
1640Sstevel@tonic-gate 		if (pwd)
1650Sstevel@tonic-gate 			(void) strncpy(who, pwd->pw_name, sizeof (who));
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Saw a wild card, so do everything
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate static void
doall(void)1740Sstevel@tonic-gate doall(void)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	(void) mutex_lock(&tty);
1770Sstevel@tonic-gate 	(void) fprintf(stderr, "writing to everyone not supported\n");
1780Sstevel@tonic-gate 	(void) mutex_unlock(&tty);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * Fire off a detached thread for each host in the list, if the thread
1830Sstevel@tonic-gate  * create fails simply run synchronously.
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate static void
doit(char * hostname)1860Sstevel@tonic-gate doit(char *hostname)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	thread_t tid;
1890Sstevel@tonic-gate 	char *thread_hostname;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	(void) mutex_lock(&thr_mtx);
1920Sstevel@tonic-gate 	while (thread_count >= MAX_THREADS) {
1930Sstevel@tonic-gate 		(void) mutex_unlock(&thr_mtx);
1940Sstevel@tonic-gate 		(void) sleep(PATIENCE/2);
1950Sstevel@tonic-gate 		(void) mutex_lock(&thr_mtx);
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	thread_count++;
1990Sstevel@tonic-gate 	(void) mutex_unlock(&thr_mtx);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	thread_hostname = strdup(hostname);
2020Sstevel@tonic-gate 	if (thread_hostname == (char *)NULL) {
2030Sstevel@tonic-gate 		(void) mutex_lock(&tty);
2040Sstevel@tonic-gate 		(void) fprintf(stderr, "Ran out of memory\n");
2050Sstevel@tonic-gate 		(void) mutex_unlock(&tty);
2060Sstevel@tonic-gate 		exit(1);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (thr_create(NULL, 0, do_one, thread_hostname,
2100Sstevel@tonic-gate 			THR_DETACHED, &tid) != 0) {
2110Sstevel@tonic-gate 		(void) do_one(thread_hostname);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static void *
do_one(void * arg)2160Sstevel@tonic-gate do_one(void *arg)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	char *hostname = arg;
2190Sstevel@tonic-gate 	CLIENT *clnt;
2200Sstevel@tonic-gate 	struct timeval tp;
2210Sstevel@tonic-gate 	void *vp = NULL;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate #ifdef DEBUG
2240Sstevel@tonic-gate 	(void) mutex_lock(&tty);
2250Sstevel@tonic-gate 	(void) fprintf(stderr, "sending message to %s\n%s\n", hostname, path);
2260Sstevel@tonic-gate 	(void) mutex_unlock(&tty);
2270Sstevel@tonic-gate 	return (0);
2280Sstevel@tonic-gate #endif
2290Sstevel@tonic-gate 	tp.tv_sec = PATIENCE;
2300Sstevel@tonic-gate 	tp.tv_usec = 0;
2310Sstevel@tonic-gate 	clnt = clnt_create_timed(
2320Sstevel@tonic-gate 		hostname, WALLPROG, WALLVERS, "datagram_v", &tp);
2330Sstevel@tonic-gate 	if (clnt == NULL) {
2340Sstevel@tonic-gate 		if (!qflag) {
2350Sstevel@tonic-gate 			(void) mutex_lock(&tty);
2360Sstevel@tonic-gate 			(void) fprintf(stderr, "rwall: Can't send to %s\n",
2370Sstevel@tonic-gate 			    hostname);
2380Sstevel@tonic-gate 			clnt_pcreateerror(hostname);
2390Sstevel@tonic-gate 			(void) mutex_unlock(&tty);
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 		goto errout;
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if (wallproc_wall_1(&path, vp, clnt) != RPC_SUCCESS) {
2450Sstevel@tonic-gate 		if (!qflag) {
2460Sstevel@tonic-gate 			(void) mutex_lock(&tty);
2470Sstevel@tonic-gate 			clnt_perror(clnt, hostname);
2480Sstevel@tonic-gate 			(void) mutex_unlock(&tty);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	clnt_destroy(clnt);
2520Sstevel@tonic-gate errout:
2530Sstevel@tonic-gate 	(void) mutex_lock(&thr_mtx);
2540Sstevel@tonic-gate 	thread_count--;
2550Sstevel@tonic-gate 	(void) mutex_unlock(&thr_mtx);
2560Sstevel@tonic-gate 	free(hostname);
2570Sstevel@tonic-gate 	return (0);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate static void
usage(void)2610Sstevel@tonic-gate usage(void)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	(void) fprintf(stderr,
2640Sstevel@tonic-gate 	    "Usage: rwall [-q] host .... [-n netgroup ....] [-h host ...]\n");
2650Sstevel@tonic-gate 	exit(1);
2660Sstevel@tonic-gate }
267