File: jmond.c

package info (click to toggle)
jmon 0.3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 108 kB
  • ctags: 76
  • sloc: ansic: 569; sh: 77; makefile: 76
file content (353 lines) | stat: -rw-r--r-- 9,132 bytes parent folder | download
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
/****************************************************************************

                   jMon - Distributed Resource Monitor

                   Copyright (C) 1999 Jaco Breitenbach

    This code is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This package 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this package; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Jaco Breitenbach can be contacted at <jjb@dsp.sun.ac.za> 

****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <time.h>
#include <signal.h>
#include <stdarg.h>
#include <syslog.h>
#include <errno.h>
#include <pwd.h>


#define MSGLEN 32
#define BAD_OPEN_MESSAGE "Error: /proc filesystem not mounted.\n"
#define STAT_FILE "/proc/stat"
#define MEMINFO_FILE "/proc/meminfo"
#define VERSION "0.3"
#define SLEEPTIME 250000
#define NOBODY "nobody"
#define PIDFILE "/var/run/jmond.pid"


#define FILE_TO_BUF(filename, fd) do	{						\
    static int n;												\
    if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) {	\
		log_err("%s", BAD_OPEN_MESSAGE);						\
		close(fd);												\
		exit(1);												\
    }															\
    lseek(fd, 0L, SEEK_SET);									\
    if ((n = read(fd, buf, sizeof buf - 1)) < 0) {				\
		close(fd);												\
		fd = -1;												\
    }															\
    buf[n] = '\0';												\
}while(0)


static char				message[MSGLEN];
pthread_mutex_t			mut = PTHREAD_MUTEX_INITIALIZER;
static char				buf[1024];
static char				*appname;
int						sock;


void log_init (void)	{
	closelog();
	openlog(appname, LOG_PID, LOG_DAEMON);
}


void log_msg (const char *fmt, ...)	{
	char msg[1024];
	va_list args;

	va_start(args, fmt);
	vsnprintf(msg, sizeof(msg), fmt, args);
	va_end(args);
	syslog(LOG_INFO, "%.500s", msg);
}


void log_err (const char *fmt, ...)	{
	char msg[1024];
	va_list args;

	va_start(args, fmt);
	vsnprintf(msg, sizeof(msg), fmt, args);
	va_end(args);
	syslog(LOG_ERR, "%.500s", msg);
}


static void sigterm_handler (int sig)	{
	log_msg("SIGTERM signal received");
	close(sock);
	exit(0);
}


int send_to_client (int sock_fd)	{
	int		result; 

	if (sock_fd >= 0)	{
	/* Lock and unlock the shared message, just to be safe. */
		pthread_mutex_lock(&mut);
		result = send(sock_fd, message, MSGLEN, 0);
		pthread_mutex_unlock(&mut);
		if (result < MSGLEN) return 1;
	}
	return 0;
}


void spawn (int *client)	{
	int						sock_fd;
	struct sockaddr_in		addr;
	socklen_t				addrlen = sizeof(addr);

	/* Set signal SIGPIPE to be ignored. */
	signal(SIGPIPE, SIG_IGN);
	sock_fd = (int) *client;
	if (getsockname(sock_fd, &addr, &addrlen))	{
		close(sock_fd);
		pthread_exit(NULL);
	}
	log_msg("Connection from %s on port %hd", 
		inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
	/* Upon error, this thread will self-destruct. */
	while (1)	{
		if (send_to_client(sock_fd))	{
			log_msg("Closing connection to %s", inet_ntoa(addr.sin_addr));
			close(sock_fd);
			pthread_exit(NULL);
		}
		usleep(SLEEPTIME);
	}
}


int make_socket (unsigned short int port)	{
	int					sock;
	struct sockaddr_in	addr;

	/* The initial server side socket is created and initialized. */
	if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)	{
		log_err("Socket: %s", sys_errlist[errno]);
		exit(1);
	}
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	addr.sin_port = htons(port);
	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)))	{
		log_err("Bind: %s", sys_errlist[errno]);
		exit(1);
	}
	listen(sock, SOMAXCONN);
	return sock;
}


void get_statistics (void *ptr) {
	static int		fd_stat=-1, fd_meminfo=-1;
	long			data[6], cpu_used, idle;
	char			tmp[1024];
	static long		cpu_old, idle_old;
	long			rt_cpu, rt_idle, cpu_total;
	float			cpu, mem, swp;
	int				i;

	/* A dedicated thread is spawned to update the statistics. It is noted
	 * that it is necessary to read the whole /proc file in order to force
	 * an update. To restrict CPU usage, memory and swap info is updated
	 * only once for every 8 updates in CPU info.
	 */
	while (1)	{
		FILE_TO_BUF(MEMINFO_FILE, fd_meminfo);
		sscanf(buf, "%[^M]Mem: %ld %ld %ld %ld %ld %ld\n",
			tmp,&data[0],&data[1],&data[2],&data[3],&data[4],&data[5]);
		mem = 100.*(data[1]-data[4]-data[5])/data[0];
		sscanf(buf, "%[^S]Swap: %ld %ld", tmp,&data[0],&data[1]);
		swp = 100.*data[1]/data[0];

		for (i=0; i<8; i++)	{
			FILE_TO_BUF(STAT_FILE, fd_stat);
			sscanf(buf,"cpu %ld %ld %ld %ld",&data[0],&data[1],&data[2],&idle);
			cpu_used = data[0] + data[1] + data[2];

			rt_cpu = cpu_used - cpu_old;
			rt_idle = idle - idle_old;
			cpu_total = rt_cpu+rt_idle;
			if (cpu_total > 0) { cpu = 100.*rt_cpu/cpu_total; }
			else { cpu = 0.; }
			cpu_old = cpu_used;
			idle_old = idle;

	/* Lock and unlock the shared message space. */
			pthread_mutex_lock(&mut);
			snprintf(message, MSGLEN, \
				"cpu:%6.2f mem:%6.2f swp:%6.2f", cpu, mem, swp);
			pthread_mutex_unlock(&mut);
			usleep(SLEEPTIME);
		}
	}
}


void set_nobody (void)	{
	struct passwd *pw;

	/* If the daemon was started as root, reset the effective uid and gid
	 * to belong to user nobody. This is to prevent ANY security leaks.
	 */
	if (!geteuid())	{
	/* We are running as root, so continue to set uid and gid to nobody.
	 * First we search the password file for nobody's uid and gid.
	 */
		pw = getpwent();
		while ((pw != NULL) && strncmp(pw->pw_name, NOBODY, sizeof(NOBODY)))
			pw = getpwent();
		endpwent();
		if (pw)	{
	/* Located nobody's password file entry. Proceed to set uid and gid. */
			if (setegid(pw->pw_gid))
				log_err("WARNING - Unable to set effective gid to nobody.");
			if (seteuid(pw->pw_uid))
				log_err("WARNING - Unable to set effective uid to nobody.");
		}
		else	{
			log_err("WARNING - Unable to set effective uid to nobody.");
		}
	}
}


void Usage (void)	{
	printf("\nUsage:\tjmond [-p port]\nThe default port number is 7777.\n\n");
	exit(1);
}


int main (int argc, char *argv[])	{
	int					new_sock, pidfile;
	FILE				*fhandle;
	int 				result;
	struct sockaddr_in 	clientname;
	int 				size;
	unsigned short int	port=7777;
	pthread_t			thread;
	int				opt;

	/* Check commandline parameters. */
	while ((opt=getopt(argc,argv,"p:h")) != EOF)	{
		switch(opt)	{
			case 'p':	{
				port = (unsigned short) atoi(optarg);
				break;
			}
			default:	Usage();
		}	
	}

	/* Extract and save calling application name for syslog. */
	if (strchr(argv[0], '/'))
		appname = strrchr(argv[0], '/') + 1;
	else
		appname = argv[0];

	/* Fork, and have the parent exit. The child becomes the server. */
	if (fork())
		exit(0);

	/* Installing SIGTERM signal handler. */
	signal(SIGTERM, (void *) &sigterm_handler);

	/* Initialize syslog. */
	log_init();

	/* Redirect stdin, stdout, and stderr to /dev/null. */
	freopen("/dev/null", "r", stdin);
	freopen("/dev/null", "w", stdout);
	freopen("/dev/null", "w", stderr);

	/* Chdir to the root directory so that the current disk can be unmounted
	 * if desired. 
	 */
	chdir("/");

	/* Check whether PID file already exists. If not, and we have the
	 * necessary access permissions, write the current PID to the file.
	 */
	if ((pidfile = open(PIDFILE, O_WRONLY | O_CREAT | O_EXCL, \
		S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)	{
		if (errno == EEXIST)
			log_err("%s already exists.", PIDFILE);
		else
			log_err("Error opening %s.", PIDFILE);
	}
	else if ((fhandle = fdopen(pidfile, "w")) != NULL)	{
		fprintf(fhandle, "%u\n", (unsigned int)getpid());
		fclose(fhandle);
	}

	/* Set uid and gid to nobody (if possible and required). */
	set_nobody();

	/* Spawn a dedicated thread to update resource statistics. */
	result = pthread_create(&thread, NULL, (void *) &get_statistics, NULL);
	if (result)	{
		log_err("pthread_create: %s", sys_errlist[errno]);
		exit(1);
	}

	/* Create and bind socket. */
	sock = make_socket(port);

	log_msg("Daemon started on port %d", port);

	while (1)	{
	/* Block until new connection received. */
		new_sock = accept(sock, (struct sockaddr *) &clientname, &size);
		if (new_sock < 0)	{
			log_err("accept: %s", sys_errlist[errno]);
			exit(1);
		}
		else	{
	/* Spawn a new thread to handle the new connection. */
			result = pthread_create(&thread, NULL, (void *) &spawn, \
				(void *) &new_sock);
			if (result)	{
				log_err("pthread_create: %s", sys_errlist[errno]);
				exit(1);
			}
		}
	}
	return 0;
}