File: rup.c

package info (click to toggle)
rstatd 4.0.1-9
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 640 kB
  • ctags: 231
  • sloc: ansic: 3,895; sh: 697; makefile: 83
file content (367 lines) | stat: -rw-r--r-- 8,806 bytes parent folder | download | duplicates (4)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *  rup version 3.03 rpc ruptime client for use with rpc.rstatd 3.03
 *  Copyright (C) 1995  Adam Migus, Memorial University of Newfoundland
 *	(MUN)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  If you make modifications to the source, I would be happy to have
 *  them to include in future releases.  Feel free to send them to:
 *      Adam Migus	      				
 *		amigus@cs.mun.ca 
 *    amigus@ucs.mun.ca   
 *
 **************************************************************************/

#include <unistd.h>
#include <stdio.h>
#include <rpc/rpc.h>
#include <rpc/clnt.h>
#include <rpc/pmap_clnt.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "rstat.h"

void bzero();
void bcopy();
char *rindex();

char *version = "@(#)rup version 3.03 Copyright (Adam Migus) 1/9/96\n";
char *program_name;

struct host_type {
	struct in_addr host_addr;
	struct host_type *next;
};

struct host_type *host_list;

int save_hostname(struct in_addr new_addr)
{
	struct host_type *new_item;

	if(!(new_item=(struct host_type *)malloc(sizeof(struct host_type)))) {
		fprintf(stderr,"%s: out of memory...\n",program_name);
		return(0);
	}
	new_item->host_addr.s_addr = new_addr.s_addr;
	new_item->next = host_list;
	host_list = new_item;
	return(1);
}
int find_hostname(struct in_addr the_addr)
{
	struct host_type *current_item;

	current_item = host_list;
	while(current_item != NULL) {
		if(current_item->host_addr.s_addr == the_addr.s_addr) {
			return(1);
		}
	current_item= current_item->next;
	}
	return(0);
}

static void convuptime(
      long host_uptime, char *days_buf, size_t days_len, char *hours_buf
) {
      int hours, mins;
      long days;

      *days_buf = 0;
      *hours_buf = 0;

      if (host_uptime <= 0) {
	      return;
      }

      host_uptime = (host_uptime + 59) / 60;
      mins = host_uptime % 60;
      host_uptime /= 60;
      hours = host_uptime % 24;
      days = host_uptime / 24;

      if (days) {
	      snprintf(
		      days_buf, days_len, "%3ld day%s, ",
		      days, (days > 1) ? "s" : ""
	      );
      }

      if (hours) {
	      sprintf(hours_buf, "%2d:%02d, ", hours, mins);
      } else if (mins) {
	      sprintf(
		      hours_buf, "%2d min%s, ", mins,
		      (mins > 1) ? "s" : ""
	      );
      }
}

int	rup_answer_v5(struct statsusers *replyp, struct sockaddr_in *raddrp)
{
	struct tm *tmp_time;
	struct hostent *hp;
	char *host;
	struct statsusers *host_stat;
	struct tm host_time;
	char days_buf[16];
	char hours_buf[16];
	char users_buf[16];
	long host_uptime;

	if(find_hostname(raddrp->sin_addr)) {
		return(0);
	}

	save_hostname(raddrp->sin_addr);

	host_stat = (statsusers *)replyp;

	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
			   sizeof(struct in_addr), AF_INET);
	if (hp)
		host = (char *)hp->h_name;
	else
		host = (char *)inet_ntoa(raddrp->sin_addr);

	tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
	host_time = *tmp_time;

	convuptime(
		host_stat->curtime.tv_sec - host_stat->boottime.tv_sec,
		days_buf, sizeof(days_buf), hours_buf
	);

	sprintf(users_buf, "%3d user%s, ", host_stat->users,
		(host_stat->users > 1) ? "s" : "");

	printf("%-*.*s %2d:%02d up %10.10s%9.9s%11.11sload %.2f %.2f %.2f\n",
		21, 21, host, host_time.tm_hour, host_time.tm_min,
		days_buf, hours_buf, users_buf,
		(double)host_stat->avenrun[0]/FSCALE,
		(double)host_stat->avenrun[1]/FSCALE,
		(double)host_stat->avenrun[2]/FSCALE);

	return(0);
}
/* This function is mostly hacked NetBSD code */
int	rup_answer_v3(struct statstime *replyp, struct sockaddr_in *raddrp)
{

	struct tm *tmp_time;
	struct hostent *hp;
	char *host;
	struct statstime *host_stat;
	struct tm host_time;
	char days_buf[16];
	char hours_buf[16];
	long host_uptime;

	if(find_hostname(raddrp->sin_addr)) {
		return(0);
	}

	save_hostname(raddrp->sin_addr);

	host_stat = (statstime *)replyp;

	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
			   sizeof(struct in_addr), AF_INET);
	if (hp)
		host = (char *)hp->h_name;
	else
		host = (char *)inet_ntoa(raddrp->sin_addr);

	tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
	host_time = *tmp_time;

	convuptime(
		host_stat->curtime.tv_sec - host_stat->boottime.tv_sec,
		days_buf, sizeof(days_buf), hours_buf
	);

	printf("%-*.*s %2d:%02d up %10.10s%9.9sload %.2f %.2f %.2f\n",
		21, 21, host, host_time.tm_hour, host_time.tm_min,
		days_buf, hours_buf,
		(double)host_stat->avenrun[0]/FSCALE,
		(double)host_stat->avenrun[1]/FSCALE,
		(double)host_stat->avenrun[2]/FSCALE);

	return(0);
}
int	broadcast_v5()
{
	struct statsusers host_stat;
	enum clnt_stat clnt_stat;

	clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_USERS, RSTATPROC_STATS,
		(xdrproc_t) xdr_void, 0, (void *)xdr_statsusers,
		(void *)&host_stat, (void *)rup_answer_v5);
	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
		fprintf(stderr, "rup: %s\n", clnt_sperrno(clnt_stat));
		exit(1);
	}
	exit(0);
}
int	broadcast_v3()
{
	struct statstime host_stat;
	enum clnt_stat clnt_stat;

	clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
		(xdrproc_t)xdr_void, 0, (void *)xdr_statstime,
		(void *)&host_stat, (void *)rup_answer_v3);
	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
		fprintf(stderr, "rup: %s\n", clnt_sperrno(clnt_stat));
		exit(1);
	}
	exit(0);
}
int pointopoint_v5(struct sockaddr_in *server_addr)
{
	struct statsusers host_stat;
	struct statsusers *s;
	struct timeval pertry_timeout, total_timeout;
	enum clnt_stat clnt_stat;
	int sock=RPC_ANYSOCK;
	CLIENT *client;

	pertry_timeout.tv_sec = 3;
	pertry_timeout.tv_usec = 0;
	total_timeout.tv_sec = 10;
	total_timeout.tv_usec = 0;
	s = &host_stat;

	if((client = clntudp_create(server_addr, RSTATPROG, RSTATVERS_USERS,
	pertry_timeout, &sock)) == NULL) {
		close(sock);
		return(0);
	}

	clnt_stat = clnt_call(client, RSTATPROC_STATS, (xdrproc_t) xdr_void,0,
	(xdrproc_t) xdr_statsusers, (void *)&host_stat, total_timeout);

	if(clnt_stat == RPC_SUCCESS) {
		rup_answer_v5(&host_stat, server_addr);
		clnt_destroy(client);
		close(sock);
		return(1);
	}
	else {
		clnt_destroy(client);
		close(sock);
		return(0);
	}
	return(0);
}

int pointopoint_v3(struct sockaddr_in *server_addr)
{
	struct statstime host_stat;
	struct timeval pertry_timeout, total_timeout;
	enum clnt_stat clnt_stat;
	int sock=RPC_ANYSOCK;
	CLIENT *client;

	pertry_timeout.tv_sec = 3;
	pertry_timeout.tv_usec = 0;
	total_timeout.tv_sec = 10;
	total_timeout.tv_usec = 0;

	if((client = clntudp_create(server_addr, RSTATPROG, RSTATVERS_TIME,
	pertry_timeout, &sock)) == NULL) {
		close(sock);
		return(0);
	}
	clnt_stat = clnt_call(client, RSTATPROC_STATS, (xdrproc_t)xdr_void, 0,
		(xdrproc_t)xdr_statstime, (void *)&host_stat, total_timeout);

	if(clnt_stat == RPC_SUCCESS) {
		rup_answer_v3(&host_stat, server_addr);
		clnt_destroy(client);
		close(sock);
		return(1);
	}
	else {
		clnt_destroy(client);
		close(sock);
		return(0);
	}
	return(0);
}
void usage()
{
	printf("usage: %s {-u | -v | hosts ...}\n"\
			"\t-u\tbroadcast request for info including users.\n"\
			"\t-v\tprint version and exit\n", program_name);
}

int main(int argc, char **argv)
{
	struct hostent *hp;
	struct sockaddr_in server_addr;
	char c;

	if(!(program_name = rindex(argv[0],'/'))) 
		program_name = argv[0];
	else 
		program_name++;
	
	if(argc == 1) {
		broadcast_v3();
	}

	while((c = getopt(argc,argv,"h?uv")) != EOF)
		switch(c) {
			case 'v':
						printf("%s",version);
						exit(0);
			case 'u':
						broadcast_v5();
						exit(0);
			case '?':
			case 'h':
			default:
						usage();
						exit(0);
		}

	for (; optind < argc; optind++) {
		if((hp = gethostbyname(argv[optind])) == NULL) {
			fprintf(stderr,"%s: cannot get hostname %s\n",program_name,argv[optind]);
			continue;
		}
 
		bcopy(hp->h_addr,(caddr_t)&server_addr.sin_addr, hp->h_length);
		server_addr.sin_family = AF_INET;
		server_addr.sin_port = 0;
 
		if(pointopoint_v5(&server_addr) == 1)
			continue;
		else if(pointopoint_v3(&server_addr) == 1)
			continue;
		else
			clnt_pcreateerror(program_name);
	}

	return 0;
}