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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate * 227c478bd9Sstevel@tonic-gate * Copyright 2005 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 #pragma ident "%Z%%M% %I% %E% SMI" 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * rwall.c 417c478bd9Sstevel@tonic-gate * The client rwall program 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <sys/types.h> 467c478bd9Sstevel@tonic-gate #include <stdlib.h> 477c478bd9Sstevel@tonic-gate #include <unistd.h> 487c478bd9Sstevel@tonic-gate #include <thread.h> 497c478bd9Sstevel@tonic-gate #include <string.h> 507c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 517c478bd9Sstevel@tonic-gate #include <signal.h> 527c478bd9Sstevel@tonic-gate #include <pwd.h> 537c478bd9Sstevel@tonic-gate #include <rpcsvc/rwall.h> 547c478bd9Sstevel@tonic-gate #include <netconfig.h> 557c478bd9Sstevel@tonic-gate #include <netdb.h> 567c478bd9Sstevel@tonic-gate #include <sys/time.h> 577c478bd9Sstevel@tonic-gate #include <sys/resource.h> 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static void init_who(void); 607c478bd9Sstevel@tonic-gate static void doall(void); 617c478bd9Sstevel@tonic-gate static void doit(char *); 627c478bd9Sstevel@tonic-gate static void *do_one(void *); 637c478bd9Sstevel@tonic-gate static void usage(void); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #define PATIENCE 10 667c478bd9Sstevel@tonic-gate #define MAX_THREADS 1024 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static mutex_t tty = DEFAULTMUTEX; 697c478bd9Sstevel@tonic-gate static char who[9] = "???"; 707c478bd9Sstevel@tonic-gate static char *path; 717c478bd9Sstevel@tonic-gate static mutex_t thr_mtx = DEFAULTMUTEX; 727c478bd9Sstevel@tonic-gate static int thread_count = 8; /* fudge factor for system threads/fds */ 737c478bd9Sstevel@tonic-gate static int qflag = 0; /* quiet: we don't care about errors */ 747c478bd9Sstevel@tonic-gate 75*49e7ca49Speteh int 76*49e7ca49Speteh main(int argc, char *argv[]) 777c478bd9Sstevel@tonic-gate { 787c478bd9Sstevel@tonic-gate int msize; 797c478bd9Sstevel@tonic-gate char buf[BUFSIZ+1]; 80*49e7ca49Speteh int i; 817c478bd9Sstevel@tonic-gate char hostname[256]; 827c478bd9Sstevel@tonic-gate int hflag; 837c478bd9Sstevel@tonic-gate struct rlimit rl; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (argc < 2) 867c478bd9Sstevel@tonic-gate usage(); 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { 897c478bd9Sstevel@tonic-gate rl.rlim_cur = (rl.rlim_max < MAX_THREADS ? 907c478bd9Sstevel@tonic-gate rl.rlim_max : MAX_THREADS); 917c478bd9Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rl); 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 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 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 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 * 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 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