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 #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> 45*004388ebScasper #include <stdio_ext.h> 467c478bd9Sstevel@tonic-gate #include <sys/types.h> 477c478bd9Sstevel@tonic-gate #include <stdlib.h> 487c478bd9Sstevel@tonic-gate #include <unistd.h> 497c478bd9Sstevel@tonic-gate #include <thread.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 527c478bd9Sstevel@tonic-gate #include <signal.h> 537c478bd9Sstevel@tonic-gate #include <pwd.h> 547c478bd9Sstevel@tonic-gate #include <rpcsvc/rwall.h> 557c478bd9Sstevel@tonic-gate #include <netconfig.h> 567c478bd9Sstevel@tonic-gate #include <netdb.h> 577c478bd9Sstevel@tonic-gate #include <sys/time.h> 587c478bd9Sstevel@tonic-gate #include <sys/resource.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate static void init_who(void); 617c478bd9Sstevel@tonic-gate static void doall(void); 627c478bd9Sstevel@tonic-gate static void doit(char *); 637c478bd9Sstevel@tonic-gate static void *do_one(void *); 647c478bd9Sstevel@tonic-gate static void usage(void); 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #define PATIENCE 10 677c478bd9Sstevel@tonic-gate #define MAX_THREADS 1024 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate static mutex_t tty = DEFAULTMUTEX; 707c478bd9Sstevel@tonic-gate static char who[9] = "???"; 717c478bd9Sstevel@tonic-gate static char *path; 727c478bd9Sstevel@tonic-gate static mutex_t thr_mtx = DEFAULTMUTEX; 737c478bd9Sstevel@tonic-gate static int thread_count = 8; /* fudge factor for system threads/fds */ 747c478bd9Sstevel@tonic-gate static int qflag = 0; /* quiet: we don't care about errors */ 757c478bd9Sstevel@tonic-gate 7649e7ca49Speteh int 7749e7ca49Speteh main(int argc, char *argv[]) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate int msize; 807c478bd9Sstevel@tonic-gate char buf[BUFSIZ+1]; 8149e7ca49Speteh int i; 827c478bd9Sstevel@tonic-gate char hostname[256]; 837c478bd9Sstevel@tonic-gate int hflag; 847c478bd9Sstevel@tonic-gate struct rlimit rl; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (argc < 2) 877c478bd9Sstevel@tonic-gate usage(); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { 907c478bd9Sstevel@tonic-gate rl.rlim_cur = (rl.rlim_max < MAX_THREADS ? 917c478bd9Sstevel@tonic-gate rl.rlim_max : MAX_THREADS); 927c478bd9Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rl); 93*004388ebScasper (void) enable_extended_FILE_stdio(-1, -1); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate (void) gethostname(hostname, sizeof (hostname)); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate init_who(); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate msize = snprintf(buf, sizeof (buf), "From %s@%s: ", who, hostname); 1017c478bd9Sstevel@tonic-gate while ((i = getchar()) != EOF) { 1027c478bd9Sstevel@tonic-gate if (msize >= (sizeof (buf) - 1)) { 1037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Message too long\n"); 1047c478bd9Sstevel@tonic-gate exit(1); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate buf[msize++] = i; 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate buf[msize] = '\0'; 1097c478bd9Sstevel@tonic-gate path = buf; 1107c478bd9Sstevel@tonic-gate hflag = 1; 1117c478bd9Sstevel@tonic-gate while (argc > 1) { 1127c478bd9Sstevel@tonic-gate if (argv[1][0] == '-') { 1137c478bd9Sstevel@tonic-gate switch (argv[1][1]) { 1147c478bd9Sstevel@tonic-gate case 'h': 1157c478bd9Sstevel@tonic-gate hflag = 1; 1167c478bd9Sstevel@tonic-gate break; 1177c478bd9Sstevel@tonic-gate case 'n': 1187c478bd9Sstevel@tonic-gate hflag = 0; 1197c478bd9Sstevel@tonic-gate break; 1207c478bd9Sstevel@tonic-gate case 'q': 1217c478bd9Sstevel@tonic-gate qflag = 1; 1227c478bd9Sstevel@tonic-gate break; 1237c478bd9Sstevel@tonic-gate default: 1247c478bd9Sstevel@tonic-gate usage(); 1257c478bd9Sstevel@tonic-gate break; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate argc--; 1287c478bd9Sstevel@tonic-gate argv++; 1297c478bd9Sstevel@tonic-gate continue; 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate if (hflag) { 1327c478bd9Sstevel@tonic-gate doit(argv[1]); 1337c478bd9Sstevel@tonic-gate } else { 1347c478bd9Sstevel@tonic-gate char *machine, *user, *domain; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate (void) setnetgrent(argv[1]); 1377c478bd9Sstevel@tonic-gate while (getnetgrent(&machine, &user, &domain)) { 1387c478bd9Sstevel@tonic-gate if (machine) 1397c478bd9Sstevel@tonic-gate doit(machine); 1407c478bd9Sstevel@tonic-gate else 1417c478bd9Sstevel@tonic-gate doall(); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate (void) endnetgrent(); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate argc--; 1467c478bd9Sstevel@tonic-gate argv++; 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate thr_exit(NULL); 1497c478bd9Sstevel@tonic-gate return (0); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate static void 1537c478bd9Sstevel@tonic-gate init_who(void) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate char *wp; 1567c478bd9Sstevel@tonic-gate struct passwd *pwd; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate wp = getlogin(); 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate if (wp != NULL) 1617c478bd9Sstevel@tonic-gate (void) strncpy(who, wp, sizeof (who)); 1627c478bd9Sstevel@tonic-gate else { 1637c478bd9Sstevel@tonic-gate pwd = getpwuid(getuid()); 1647c478bd9Sstevel@tonic-gate if (pwd) 1657c478bd9Sstevel@tonic-gate (void) strncpy(who, pwd->pw_name, sizeof (who)); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* 1717c478bd9Sstevel@tonic-gate * Saw a wild card, so do everything 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate static void 1747c478bd9Sstevel@tonic-gate doall(void) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate (void) mutex_lock(&tty); 1777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "writing to everyone not supported\n"); 1787c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tty); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * Fire off a detached thread for each host in the list, if the thread 1837c478bd9Sstevel@tonic-gate * create fails simply run synchronously. 1847c478bd9Sstevel@tonic-gate */ 1857c478bd9Sstevel@tonic-gate static void 1867c478bd9Sstevel@tonic-gate doit(char *hostname) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate thread_t tid; 1897c478bd9Sstevel@tonic-gate char *thread_hostname; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate (void) mutex_lock(&thr_mtx); 1927c478bd9Sstevel@tonic-gate while (thread_count >= MAX_THREADS) { 1937c478bd9Sstevel@tonic-gate (void) mutex_unlock(&thr_mtx); 1947c478bd9Sstevel@tonic-gate (void) sleep(PATIENCE/2); 1957c478bd9Sstevel@tonic-gate (void) mutex_lock(&thr_mtx); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate thread_count++; 1997c478bd9Sstevel@tonic-gate (void) mutex_unlock(&thr_mtx); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate thread_hostname = strdup(hostname); 2027c478bd9Sstevel@tonic-gate if (thread_hostname == (char *)NULL) { 2037c478bd9Sstevel@tonic-gate (void) mutex_lock(&tty); 2047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Ran out of memory\n"); 2057c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tty); 2067c478bd9Sstevel@tonic-gate exit(1); 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate if (thr_create(NULL, 0, do_one, thread_hostname, 2107c478bd9Sstevel@tonic-gate THR_DETACHED, &tid) != 0) { 2117c478bd9Sstevel@tonic-gate (void) do_one(thread_hostname); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate static void * 2167c478bd9Sstevel@tonic-gate do_one(void *arg) 2177c478bd9Sstevel@tonic-gate { 2187c478bd9Sstevel@tonic-gate char *hostname = arg; 2197c478bd9Sstevel@tonic-gate CLIENT *clnt; 2207c478bd9Sstevel@tonic-gate struct timeval tp; 2217c478bd9Sstevel@tonic-gate void *vp = NULL; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate #ifdef DEBUG 2247c478bd9Sstevel@tonic-gate (void) mutex_lock(&tty); 2257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "sending message to %s\n%s\n", hostname, path); 2267c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tty); 2277c478bd9Sstevel@tonic-gate return (0); 2287c478bd9Sstevel@tonic-gate #endif 2297c478bd9Sstevel@tonic-gate tp.tv_sec = PATIENCE; 2307c478bd9Sstevel@tonic-gate tp.tv_usec = 0; 2317c478bd9Sstevel@tonic-gate clnt = clnt_create_timed( 2327c478bd9Sstevel@tonic-gate hostname, WALLPROG, WALLVERS, "datagram_v", &tp); 2337c478bd9Sstevel@tonic-gate if (clnt == NULL) { 2347c478bd9Sstevel@tonic-gate if (!qflag) { 2357c478bd9Sstevel@tonic-gate (void) mutex_lock(&tty); 2367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "rwall: Can't send to %s\n", 2377c478bd9Sstevel@tonic-gate hostname); 2387c478bd9Sstevel@tonic-gate clnt_pcreateerror(hostname); 2397c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tty); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate goto errout; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (wallproc_wall_1(&path, vp, clnt) != RPC_SUCCESS) { 2457c478bd9Sstevel@tonic-gate if (!qflag) { 2467c478bd9Sstevel@tonic-gate (void) mutex_lock(&tty); 2477c478bd9Sstevel@tonic-gate clnt_perror(clnt, hostname); 2487c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tty); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate clnt_destroy(clnt); 2527c478bd9Sstevel@tonic-gate errout: 2537c478bd9Sstevel@tonic-gate (void) mutex_lock(&thr_mtx); 2547c478bd9Sstevel@tonic-gate thread_count--; 2557c478bd9Sstevel@tonic-gate (void) mutex_unlock(&thr_mtx); 2567c478bd9Sstevel@tonic-gate free(hostname); 2577c478bd9Sstevel@tonic-gate return (0); 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate static void 2617c478bd9Sstevel@tonic-gate usage(void) 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2647c478bd9Sstevel@tonic-gate "Usage: rwall [-q] host .... [-n netgroup ....] [-h host ...]\n"); 2657c478bd9Sstevel@tonic-gate exit(1); 2667c478bd9Sstevel@tonic-gate } 267