xref: /illumos-gate/usr/src/cmd/rpcsvc/rwall.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
20*004388ebScasper  */
21*004388ebScasper /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
267c478bd9Sstevel@tonic-gate /* All Rights Reserved */
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
297c478bd9Sstevel@tonic-gate  * The Regents of the University of California
307c478bd9Sstevel@tonic-gate  * All Rights Reserved
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
337c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
347c478bd9Sstevel@tonic-gate  * contributors.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * rwall.c
397c478bd9Sstevel@tonic-gate  *	The client rwall program
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <stdio.h>
43*004388ebScasper #include <stdio_ext.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <unistd.h>
477c478bd9Sstevel@tonic-gate #include <thread.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
507c478bd9Sstevel@tonic-gate #include <signal.h>
517c478bd9Sstevel@tonic-gate #include <pwd.h>
527c478bd9Sstevel@tonic-gate #include <rpcsvc/rwall.h>
537c478bd9Sstevel@tonic-gate #include <netconfig.h>
547c478bd9Sstevel@tonic-gate #include <netdb.h>
557c478bd9Sstevel@tonic-gate #include <sys/time.h>
567c478bd9Sstevel@tonic-gate #include <sys/resource.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void init_who(void);
597c478bd9Sstevel@tonic-gate static void doall(void);
607c478bd9Sstevel@tonic-gate static void doit(char *);
617c478bd9Sstevel@tonic-gate static void *do_one(void *);
627c478bd9Sstevel@tonic-gate static void usage(void);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	PATIENCE 10
657c478bd9Sstevel@tonic-gate #define	MAX_THREADS 1024
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static mutex_t tty = DEFAULTMUTEX;
687c478bd9Sstevel@tonic-gate static char who[9] = "???";
697c478bd9Sstevel@tonic-gate static char *path;
707c478bd9Sstevel@tonic-gate static mutex_t thr_mtx = DEFAULTMUTEX;
717c478bd9Sstevel@tonic-gate static int thread_count = 8;	/* fudge factor for system threads/fds */
727c478bd9Sstevel@tonic-gate static int qflag = 0;		/* quiet: we don't care about errors */
737c478bd9Sstevel@tonic-gate 
7449e7ca49Speteh int
main(int argc,char * argv[])7549e7ca49Speteh main(int argc, char *argv[])
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	int msize;
787c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
7949e7ca49Speteh 	int i;
807c478bd9Sstevel@tonic-gate 	char hostname[256];
817c478bd9Sstevel@tonic-gate 	int hflag;
827c478bd9Sstevel@tonic-gate 	struct rlimit rl;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (argc < 2)
857c478bd9Sstevel@tonic-gate 		usage();
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
887c478bd9Sstevel@tonic-gate 		rl.rlim_cur = (rl.rlim_max < MAX_THREADS ?
897c478bd9Sstevel@tonic-gate 		    rl.rlim_max : MAX_THREADS);
907c478bd9Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rl);
91*004388ebScasper 		(void) enable_extended_FILE_stdio(-1, -1);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	(void) gethostname(hostname, sizeof (hostname));
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	init_who();
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	msize = snprintf(buf, sizeof (buf), "From %s@%s:  ", who, hostname);
997c478bd9Sstevel@tonic-gate 	while ((i = getchar()) != EOF) {
1007c478bd9Sstevel@tonic-gate 		if (msize >= (sizeof (buf) - 1)) {
1017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Message too long\n");
1027c478bd9Sstevel@tonic-gate 			exit(1);
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 		buf[msize++] = i;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	buf[msize] = '\0';
1077c478bd9Sstevel@tonic-gate 	path = buf;
1087c478bd9Sstevel@tonic-gate 	hflag = 1;
1097c478bd9Sstevel@tonic-gate 	while (argc > 1) {
1107c478bd9Sstevel@tonic-gate 		if (argv[1][0] == '-') {
1117c478bd9Sstevel@tonic-gate 			switch (argv[1][1]) {
1127c478bd9Sstevel@tonic-gate 				case 'h':
1137c478bd9Sstevel@tonic-gate 					hflag = 1;
1147c478bd9Sstevel@tonic-gate 					break;
1157c478bd9Sstevel@tonic-gate 				case 'n':
1167c478bd9Sstevel@tonic-gate 					hflag = 0;
1177c478bd9Sstevel@tonic-gate 					break;
1187c478bd9Sstevel@tonic-gate 				case 'q':
1197c478bd9Sstevel@tonic-gate 					qflag = 1;
1207c478bd9Sstevel@tonic-gate 					break;
1217c478bd9Sstevel@tonic-gate 				default:
1227c478bd9Sstevel@tonic-gate 					usage();
1237c478bd9Sstevel@tonic-gate 					break;
1247c478bd9Sstevel@tonic-gate 			}
1257c478bd9Sstevel@tonic-gate 			argc--;
1267c478bd9Sstevel@tonic-gate 			argv++;
1277c478bd9Sstevel@tonic-gate 			continue;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		if (hflag) {
1307c478bd9Sstevel@tonic-gate 			doit(argv[1]);
1317c478bd9Sstevel@tonic-gate 		} else {
1327c478bd9Sstevel@tonic-gate 			char *machine, *user, *domain;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 			(void) setnetgrent(argv[1]);
1357c478bd9Sstevel@tonic-gate 			while (getnetgrent(&machine, &user, &domain)) {
1367c478bd9Sstevel@tonic-gate 				if (machine)
1377c478bd9Sstevel@tonic-gate 					doit(machine);
1387c478bd9Sstevel@tonic-gate 				else
1397c478bd9Sstevel@tonic-gate 					doall();
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 			(void) endnetgrent();
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 		argc--;
1447c478bd9Sstevel@tonic-gate 		argv++;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	thr_exit(NULL);
1477c478bd9Sstevel@tonic-gate 	return (0);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static void
init_who(void)1517c478bd9Sstevel@tonic-gate init_who(void)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	char *wp;
1547c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	wp = getlogin();
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (wp != NULL)
1597c478bd9Sstevel@tonic-gate 		(void) strncpy(who, wp, sizeof (who));
1607c478bd9Sstevel@tonic-gate 	else {
1617c478bd9Sstevel@tonic-gate 		pwd = getpwuid(getuid());
1627c478bd9Sstevel@tonic-gate 		if (pwd)
1637c478bd9Sstevel@tonic-gate 			(void) strncpy(who, pwd->pw_name, sizeof (who));
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Saw a wild card, so do everything
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate static void
doall(void)1727c478bd9Sstevel@tonic-gate doall(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&tty);
1757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "writing to everyone not supported\n");
1767c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&tty);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Fire off a detached thread for each host in the list, if the thread
1817c478bd9Sstevel@tonic-gate  * create fails simply run synchronously.
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate static void
doit(char * hostname)1847c478bd9Sstevel@tonic-gate doit(char *hostname)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	thread_t tid;
1877c478bd9Sstevel@tonic-gate 	char *thread_hostname;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&thr_mtx);
1907c478bd9Sstevel@tonic-gate 	while (thread_count >= MAX_THREADS) {
1917c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&thr_mtx);
1927c478bd9Sstevel@tonic-gate 		(void) sleep(PATIENCE/2);
1937c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&thr_mtx);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	thread_count++;
1977c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&thr_mtx);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	thread_hostname = strdup(hostname);
2007c478bd9Sstevel@tonic-gate 	if (thread_hostname == (char *)NULL) {
2017c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&tty);
2027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Ran out of memory\n");
2037c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&tty);
2047c478bd9Sstevel@tonic-gate 		exit(1);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, 0, do_one, thread_hostname,
2087c478bd9Sstevel@tonic-gate 			THR_DETACHED, &tid) != 0) {
2097c478bd9Sstevel@tonic-gate 		(void) do_one(thread_hostname);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate static void *
do_one(void * arg)2147c478bd9Sstevel@tonic-gate do_one(void *arg)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	char *hostname = arg;
2177c478bd9Sstevel@tonic-gate 	CLIENT *clnt;
2187c478bd9Sstevel@tonic-gate 	struct timeval tp;
2197c478bd9Sstevel@tonic-gate 	void *vp = NULL;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate #ifdef DEBUG
2227c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&tty);
2237c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "sending message to %s\n%s\n", hostname, path);
2247c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&tty);
2257c478bd9Sstevel@tonic-gate 	return (0);
2267c478bd9Sstevel@tonic-gate #endif
2277c478bd9Sstevel@tonic-gate 	tp.tv_sec = PATIENCE;
2287c478bd9Sstevel@tonic-gate 	tp.tv_usec = 0;
2297c478bd9Sstevel@tonic-gate 	clnt = clnt_create_timed(
2307c478bd9Sstevel@tonic-gate 		hostname, WALLPROG, WALLVERS, "datagram_v", &tp);
2317c478bd9Sstevel@tonic-gate 	if (clnt == NULL) {
2327c478bd9Sstevel@tonic-gate 		if (!qflag) {
2337c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&tty);
2347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "rwall: Can't send to %s\n",
2357c478bd9Sstevel@tonic-gate 			    hostname);
2367c478bd9Sstevel@tonic-gate 			clnt_pcreateerror(hostname);
2377c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tty);
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 		goto errout;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (wallproc_wall_1(&path, vp, clnt) != RPC_SUCCESS) {
2437c478bd9Sstevel@tonic-gate 		if (!qflag) {
2447c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&tty);
2457c478bd9Sstevel@tonic-gate 			clnt_perror(clnt, hostname);
2467c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tty);
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	clnt_destroy(clnt);
2507c478bd9Sstevel@tonic-gate errout:
2517c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&thr_mtx);
2527c478bd9Sstevel@tonic-gate 	thread_count--;
2537c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&thr_mtx);
2547c478bd9Sstevel@tonic-gate 	free(hostname);
2557c478bd9Sstevel@tonic-gate 	return (0);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static void
usage(void)2597c478bd9Sstevel@tonic-gate usage(void)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
2627c478bd9Sstevel@tonic-gate 	    "Usage: rwall [-q] host .... [-n netgroup ....] [-h host ...]\n");
2637c478bd9Sstevel@tonic-gate 	exit(1);
2647c478bd9Sstevel@tonic-gate }
265