rwall.c revision 50477
168673Sobrien/* 2218822Sdim * Copyright (c) 1993 Christopher G. Demetriou 368673Sobrien * Copyright (c) 1988, 1990 Regents of the University of California. 468673Sobrien * All rights reserved. 568673Sobrien * 668673Sobrien * Redistribution and use in source and binary forms, with or without 768673Sobrien * modification, are permitted provided that the following conditions 868673Sobrien * are met: 968673Sobrien * 1. Redistributions of source code must retain the above copyright 1068673Sobrien * notice, this list of conditions and the following disclaimer. 1168673Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1268673Sobrien * notice, this list of conditions and the following disclaimer in the 1368673Sobrien * documentation and/or other materials provided with the distribution. 1468673Sobrien * 3. All advertising materials mentioning features or use of this software 1568673Sobrien * must display the following acknowledgement: 1668673Sobrien * This product includes software developed by the University of 1768673Sobrien * California, Berkeley and its contributors. 18218822Sdim * 4. Neither the name of the University nor the names of its contributors 1968673Sobrien * may be used to endorse or promote products derived from this software 2068673Sobrien * without specific prior written permission. 2168673Sobrien * 2268673Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2368673Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2468673Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2568673Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2668673Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2768673Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2868673Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2968673Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3068673Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3168673Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3268673Sobrien * SUCH DAMAGE. 3368673Sobrien */ 3468673Sobrien 3568673Sobrien#ifndef lint 3668673Sobrienstatic const char copyright[] = 3768673Sobrien"@(#) Copyright (c) 1988 Regents of the University of California.\n\ 3868673Sobrien All rights reserved.\n"; 3968673Sobrien#endif /* not lint */ 4068673Sobrien 4168673Sobrien#ifndef lint 4268673Sobrien#if 0 4368673Sobrienstatic char sccsid[] = "from: @(#)wall.c 5.14 (Berkeley) 3/2/91"; 4468673Sobrien#endif 4568673Sobrienstatic const char rcsid[] = 4668673Sobrien "$FreeBSD: head/usr.bin/rwall/rwall.c 50477 1999-08-28 01:08:13Z peter $"; 4768673Sobrien#endif /* not lint */ 4868673Sobrien 4968673Sobrien/* 5068673Sobrien * This program is not related to David Wall, whose Stanford Ph.D. thesis 5168673Sobrien * is entitled "Mechanisms for Broadcast and Selective Broadcast". 5268673Sobrien */ 5377298Sobrien 5468673Sobrien#include <sys/param.h> 55218822Sdim#include <sys/stat.h> 56218822Sdim#include <sys/time.h> 57218822Sdim#include <sys/uio.h> 58218822Sdim#include <err.h> 59218822Sdim#include <paths.h> 60218822Sdim#include <pwd.h> 61218822Sdim#include <stdio.h> 62218822Sdim#include <stdlib.h> 6368673Sobrien#include <string.h> 64218822Sdim#include <unistd.h> 65218822Sdim#include <utmp.h> 66218822Sdim 67218822Sdim#include <rpc/rpc.h> 68218822Sdim#include <rpcsvc/rwall.h> 69218822Sdim 70218822Sdimint mbufsize; 71218822Sdimchar *mbuf; 72218822Sdim 73218822Sdimvoid makemsg __P((char *)); 74218822Sdimstatic void usage __P((void)); 75218822Sdim 76218822Sdim/* ARGSUSED */ 77218822Sdimint 78218822Sdimmain(argc, argv) 79218822Sdim int argc; 80218822Sdim char **argv; 8168673Sobrien{ 82 char *wallhost, res; 83 CLIENT *cl; 84 struct timeval tv; 85 86 if ((argc < 2) || (argc > 3)) 87 usage(); 88 89 wallhost = argv[1]; 90 91 makemsg(argv[2]); 92 93 /* 94 * Create client "handle" used for calling MESSAGEPROG on the 95 * server designated on the command line. We tell the rpc package 96 * to use the "tcp" protocol when contacting the server. 97 */ 98 cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp"); 99 if (cl == NULL) { 100 /* 101 * Couldn't establish connection with server. 102 * Print error message and die. 103 */ 104 errx(1, "%s", clnt_spcreateerror(wallhost)); 105 } 106 107 tv.tv_sec = 15; /* XXX ?? */ 108 tv.tv_usec = 0; 109 if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, &mbuf, xdr_void, &res, tv) != RPC_SUCCESS) { 110 /* 111 * An error occurred while calling the server. 112 * Print error message and die. 113 */ 114 errx(1, "%s", clnt_sperror(cl, wallhost)); 115 } 116 117 exit(0); 118} 119 120static void 121usage() 122{ 123 fprintf(stderr, "usage: rwall hostname [file]\n"); 124 exit(1); 125} 126 127void 128makemsg(fname) 129 char *fname; 130{ 131 struct tm *lt; 132 struct passwd *pw; 133 struct stat sbuf; 134 time_t now, time(); 135 FILE *fp; 136 int fd; 137 char *whom, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64]; 138 139 snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP); 140 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) 141 errx(1, "can't open temporary file"); 142 (void)unlink(tmpname); 143 144 if (!(whom = getlogin())) 145 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 146 (void)gethostname(hostname, sizeof(hostname)); 147 (void)time(&now); 148 lt = localtime(&now); 149 150 /* 151 * all this stuff is to blank out a square for the message; 152 * we wrap message lines at column 79, not 80, because some 153 * terminals wrap after 79, some do not, and we can't tell. 154 * Which means that we may leave a non-blank character 155 * in column 80, but that can't be helped. 156 */ 157 (void)fprintf(fp, "Remote Broadcast Message from %s@%s\n", 158 whom, hostname); 159 (void)fprintf(fp, " (%s) at %d:%02d ...\n", ttyname(2), 160 lt->tm_hour, lt->tm_min); 161 162 putc('\n', fp); 163 164 if (fname && !(freopen(fname, "r", stdin))) 165 errx(1, "can't read %s", fname); 166 while (fgets(lbuf, sizeof(lbuf), stdin)) 167 fputs(lbuf, fp); 168 rewind(fp); 169 170 if (fstat(fd, &sbuf)) 171 errx(1, "can't stat temporary file"); 172 mbufsize = sbuf.st_size; 173 if (!(mbuf = malloc((u_int)mbufsize))) 174 errx(1, "out of memory"); 175 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 176 errx(1, "can't read temporary file"); 177 (void)close(fd); 178} 179