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