xref: /illumos-gate/usr/src/cmd/rpcsvc/rwall.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
29*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
30*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
33*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
34*7c478bd9Sstevel@tonic-gate  * contributors.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * rwall.c
41*7c478bd9Sstevel@tonic-gate  *	The client rwall program
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <stdio.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
46*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
47*7c478bd9Sstevel@tonic-gate #include <unistd.h>
48*7c478bd9Sstevel@tonic-gate #include <thread.h>
49*7c478bd9Sstevel@tonic-gate #include <string.h>
50*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
51*7c478bd9Sstevel@tonic-gate #include <signal.h>
52*7c478bd9Sstevel@tonic-gate #include <pwd.h>
53*7c478bd9Sstevel@tonic-gate #include <rpcsvc/rwall.h>
54*7c478bd9Sstevel@tonic-gate #include <netconfig.h>
55*7c478bd9Sstevel@tonic-gate #include <netdb.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static void init_who(void);
60*7c478bd9Sstevel@tonic-gate static void doall(void);
61*7c478bd9Sstevel@tonic-gate static void doit(char *);
62*7c478bd9Sstevel@tonic-gate static void *do_one(void *);
63*7c478bd9Sstevel@tonic-gate static void usage(void);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate #define	PATIENCE 10
66*7c478bd9Sstevel@tonic-gate #define	MAX_THREADS 1024
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate static mutex_t tty = DEFAULTMUTEX;
69*7c478bd9Sstevel@tonic-gate static char who[9] = "???";
70*7c478bd9Sstevel@tonic-gate static char *path;
71*7c478bd9Sstevel@tonic-gate static mutex_t thr_mtx = DEFAULTMUTEX;
72*7c478bd9Sstevel@tonic-gate static int thread_count = 8;	/* fudge factor for system threads/fds */
73*7c478bd9Sstevel@tonic-gate static int qflag = 0;		/* quiet: we don't care about errors */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate main(argc, argv)
76*7c478bd9Sstevel@tonic-gate 	int argc;
77*7c478bd9Sstevel@tonic-gate 	char **argv;
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	int msize;
80*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
81*7c478bd9Sstevel@tonic-gate 	register i;
82*7c478bd9Sstevel@tonic-gate 	char hostname[256];
83*7c478bd9Sstevel@tonic-gate 	int hflag;
84*7c478bd9Sstevel@tonic-gate 	struct rlimit rl;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if (argc < 2)
87*7c478bd9Sstevel@tonic-gate 		usage();
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
90*7c478bd9Sstevel@tonic-gate 		rl.rlim_cur = (rl.rlim_max < MAX_THREADS ?
91*7c478bd9Sstevel@tonic-gate 		    rl.rlim_max : MAX_THREADS);
92*7c478bd9Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rl);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	(void) gethostname(hostname, sizeof (hostname));
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	init_who();
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	msize = snprintf(buf, sizeof (buf), "From %s@%s:  ", who, hostname);
100*7c478bd9Sstevel@tonic-gate 	while ((i = getchar()) != EOF) {
101*7c478bd9Sstevel@tonic-gate 		if (msize >= (sizeof (buf) - 1)) {
102*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Message too long\n");
103*7c478bd9Sstevel@tonic-gate 			exit(1);
104*7c478bd9Sstevel@tonic-gate 		}
105*7c478bd9Sstevel@tonic-gate 		buf[msize++] = i;
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 	buf[msize] = '\0';
108*7c478bd9Sstevel@tonic-gate 	path = buf;
109*7c478bd9Sstevel@tonic-gate 	hflag = 1;
110*7c478bd9Sstevel@tonic-gate 	while (argc > 1) {
111*7c478bd9Sstevel@tonic-gate 		if (argv[1][0] == '-') {
112*7c478bd9Sstevel@tonic-gate 			switch (argv[1][1]) {
113*7c478bd9Sstevel@tonic-gate 				case 'h':
114*7c478bd9Sstevel@tonic-gate 					hflag = 1;
115*7c478bd9Sstevel@tonic-gate 					break;
116*7c478bd9Sstevel@tonic-gate 				case 'n':
117*7c478bd9Sstevel@tonic-gate 					hflag = 0;
118*7c478bd9Sstevel@tonic-gate 					break;
119*7c478bd9Sstevel@tonic-gate 				case 'q':
120*7c478bd9Sstevel@tonic-gate 					qflag = 1;
121*7c478bd9Sstevel@tonic-gate 					break;
122*7c478bd9Sstevel@tonic-gate 				default:
123*7c478bd9Sstevel@tonic-gate 					usage();
124*7c478bd9Sstevel@tonic-gate 					break;
125*7c478bd9Sstevel@tonic-gate 			}
126*7c478bd9Sstevel@tonic-gate 			argc--;
127*7c478bd9Sstevel@tonic-gate 			argv++;
128*7c478bd9Sstevel@tonic-gate 			continue;
129*7c478bd9Sstevel@tonic-gate 		}
130*7c478bd9Sstevel@tonic-gate 		if (hflag) {
131*7c478bd9Sstevel@tonic-gate 			doit(argv[1]);
132*7c478bd9Sstevel@tonic-gate 		} else {
133*7c478bd9Sstevel@tonic-gate 			char *machine, *user, *domain;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 			(void) setnetgrent(argv[1]);
136*7c478bd9Sstevel@tonic-gate 			while (getnetgrent(&machine, &user, &domain)) {
137*7c478bd9Sstevel@tonic-gate 				if (machine)
138*7c478bd9Sstevel@tonic-gate 					doit(machine);
139*7c478bd9Sstevel@tonic-gate 				else
140*7c478bd9Sstevel@tonic-gate 					doall();
141*7c478bd9Sstevel@tonic-gate 			}
142*7c478bd9Sstevel@tonic-gate 			(void) endnetgrent();
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 		argc--;
145*7c478bd9Sstevel@tonic-gate 		argv++;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	thr_exit(NULL);
148*7c478bd9Sstevel@tonic-gate 	return (0);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate static void
152*7c478bd9Sstevel@tonic-gate init_who(void)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	char *wp;
155*7c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	wp = getlogin();
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	if (wp != NULL)
160*7c478bd9Sstevel@tonic-gate 		(void) strncpy(who, wp, sizeof (who));
161*7c478bd9Sstevel@tonic-gate 	else {
162*7c478bd9Sstevel@tonic-gate 		pwd = getpwuid(getuid());
163*7c478bd9Sstevel@tonic-gate 		if (pwd)
164*7c478bd9Sstevel@tonic-gate 			(void) strncpy(who, pwd->pw_name, sizeof (who));
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  * Saw a wild card, so do everything
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate static void
173*7c478bd9Sstevel@tonic-gate doall(void)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&tty);
176*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "writing to everyone not supported\n");
177*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&tty);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate /*
181*7c478bd9Sstevel@tonic-gate  * Fire off a detached thread for each host in the list, if the thread
182*7c478bd9Sstevel@tonic-gate  * create fails simply run synchronously.
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate static void
185*7c478bd9Sstevel@tonic-gate doit(char *hostname)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	thread_t tid;
188*7c478bd9Sstevel@tonic-gate 	char *thread_hostname;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&thr_mtx);
191*7c478bd9Sstevel@tonic-gate 	while (thread_count >= MAX_THREADS) {
192*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&thr_mtx);
193*7c478bd9Sstevel@tonic-gate 		(void) sleep(PATIENCE/2);
194*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&thr_mtx);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	thread_count++;
198*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&thr_mtx);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	thread_hostname = strdup(hostname);
201*7c478bd9Sstevel@tonic-gate 	if (thread_hostname == (char *)NULL) {
202*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&tty);
203*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Ran out of memory\n");
204*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&tty);
205*7c478bd9Sstevel@tonic-gate 		exit(1);
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, 0, do_one, thread_hostname,
209*7c478bd9Sstevel@tonic-gate 			THR_DETACHED, &tid) != 0) {
210*7c478bd9Sstevel@tonic-gate 		(void) do_one(thread_hostname);
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate static void *
215*7c478bd9Sstevel@tonic-gate do_one(void *arg)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	char *hostname = arg;
218*7c478bd9Sstevel@tonic-gate 	CLIENT *clnt;
219*7c478bd9Sstevel@tonic-gate 	struct timeval tp;
220*7c478bd9Sstevel@tonic-gate 	void *vp = NULL;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
223*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&tty);
224*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "sending message to %s\n%s\n", hostname, path);
225*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&tty);
226*7c478bd9Sstevel@tonic-gate 	return (0);
227*7c478bd9Sstevel@tonic-gate #endif
228*7c478bd9Sstevel@tonic-gate 	tp.tv_sec = PATIENCE;
229*7c478bd9Sstevel@tonic-gate 	tp.tv_usec = 0;
230*7c478bd9Sstevel@tonic-gate 	clnt = clnt_create_timed(
231*7c478bd9Sstevel@tonic-gate 		hostname, WALLPROG, WALLVERS, "datagram_v", &tp);
232*7c478bd9Sstevel@tonic-gate 	if (clnt == NULL) {
233*7c478bd9Sstevel@tonic-gate 		if (!qflag) {
234*7c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&tty);
235*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "rwall: Can't send to %s\n",
236*7c478bd9Sstevel@tonic-gate 			    hostname);
237*7c478bd9Sstevel@tonic-gate 			clnt_pcreateerror(hostname);
238*7c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tty);
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 		goto errout;
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	if (wallproc_wall_1(&path, vp, clnt) != RPC_SUCCESS) {
244*7c478bd9Sstevel@tonic-gate 		if (!qflag) {
245*7c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&tty);
246*7c478bd9Sstevel@tonic-gate 			clnt_perror(clnt, hostname);
247*7c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tty);
248*7c478bd9Sstevel@tonic-gate 		}
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate 	clnt_destroy(clnt);
251*7c478bd9Sstevel@tonic-gate errout:
252*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&thr_mtx);
253*7c478bd9Sstevel@tonic-gate 	thread_count--;
254*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&thr_mtx);
255*7c478bd9Sstevel@tonic-gate 	free(hostname);
256*7c478bd9Sstevel@tonic-gate 	return (0);
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate static void
260*7c478bd9Sstevel@tonic-gate usage(void)
261*7c478bd9Sstevel@tonic-gate {
262*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
263*7c478bd9Sstevel@tonic-gate 	    "Usage: rwall [-q] host .... [-n netgroup ....] [-h host ...]\n");
264*7c478bd9Sstevel@tonic-gate 	exit(1);
265*7c478bd9Sstevel@tonic-gate }
266