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