log.c 4.84 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008
	Copyright Stichting C.A. Muller Radioastronomiestation, 2008

	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 3 of the License, 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, see <http://www.gnu.org/licenses/>.

 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h>
#include <semaphore.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
35
#include <limits.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

#include "log.h"

static int listen_port = 0;

struct log_client {
	int used;
	int fd;
};

#define LOG_MAX_CLIENTS	10
#define LOG_MAX_QUEUE	20
#define LOG_MAX_LEN	200

struct log_msg {
	int used;
	time_t t;
	int type;
	char msg[LOG_MAX_LEN];
};

static struct log_client clients[LOG_MAX_CLIENTS];

static struct log_msg msgs[LOG_MAX_QUEUE];
static int msgs_wr = 0;
static int msgs_rd = 0;

static sem_t queue_wait;

void log_send(int type, char *msg)
{
	int val;
	
	if (msgs[msgs_wr].used == 0) {
		msgs[msgs_wr].used = 1;
		msgs[msgs_wr].t = time(NULL);
		msgs[msgs_wr].type = type;
		strncpy(msgs[msgs_wr].msg, msg, LOG_MAX_LEN);
		msgs_wr++;
		msgs_wr %= LOG_MAX_QUEUE;
	}
	
	sem_getvalue(&queue_wait, &val);
	if (!val)
		sem_post(&queue_wait);
}

static void *log_server(void *arg)
{
	int fd_accept;
	struct sockaddr_in sockaddr;
	
	signal(SIGPIPE, SIG_IGN);
	
	fd_accept = socket(AF_INET, SOCK_STREAM, 0);
	if (fd_accept < 0) {
		perror("socket() failed");
		return NULL;
	}
	
	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	sockaddr.sin_port = htons(listen_port);
	
	setsockopt(fd_accept, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));

	if (bind(fd_accept,
	    (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
		perror("bind() failed");
		close(fd_accept);
		return NULL;
	}
	
	if (listen(fd_accept, 4) < 0) {
		perror("listen() failed");
		close(fd_accept);
		return NULL;
	}
	
	while (1) {
		int client;
		struct sockaddr_in sockaddr_client;
		socklen_t len;
		struct timeval tv;
		fd_set fdset_r;
		
		if (!msgs[msgs_rd].used) {
			sem_wait(&queue_wait);
		}
				
		FD_ZERO(&fdset_r);
		FD_SET(fd_accept, &fdset_r);
		tv.tv_sec = 0;
		tv.tv_usec = 0;
		
		select(fd_accept + 1, &fdset_r, NULL, NULL, &tv);
		
		if (FD_ISSET(fd_accept, &fdset_r)) {
			len = sizeof(sockaddr_client);
			client = accept(fd_accept,
			    (struct sockaddr *)&sockaddr_client, &len);
		
			if (client >= 0) {
				int i;
				
				ioctl(client, FIONBIO, &(int){1}, sizeof(int));
				
				for (i = 0; i < LOG_MAX_CLIENTS; i++) {
					if (!clients[i].used) {
						clients[i].used = 1;
						clients[i].fd = client;
						break;
					}
				}
				if (i == LOG_MAX_CLIENTS) {
					close(client);
				}
			}
		}
		
		if (msgs[msgs_rd].used) {
			int i;
			char time[100];
			char *header;
			struct tm gmt;
			
			gmtime_r(&msgs[msgs_rd].t, &gmt);
			asctime_r(&gmt, time);

			switch(msgs[msgs_rd].type) {
				case LOG_T_ERROR:
					header = "ERROR: ";
					break;
				case LOG_T_WARNING:
					header = "WARNING: ";
					break;
				case LOG_T_INFO:
					header = "INFO: ";
					break;
				default:
					header = "UNKNOWN: ";
					break;
			}
			
			for (i = 0; i < LOG_MAX_CLIENTS; i++) {
				int r;
				
				if (clients[i].used == 0)
					continue;
				
				write(clients[i].fd, header, strlen(header));
				write(clients[i].fd, time, strlen(time));
				write(clients[i].fd, header, strlen(header));
				
				write(clients[i].fd, msgs[msgs_rd].msg,
				    strlen(msgs[msgs_rd].msg));
				
				r = write(clients[i].fd, "\n", 1);
				fsync(clients[i].fd);
				
				if (r <= 0) {
					close(clients[i].fd);
					clients[i].fd = -1;
					clients[i].used = 0;
				}
			}
			
			printf("%s%s%s%s\n", 
			    header, time, header, msgs[msgs_rd].msg);
			
			msgs[msgs_rd].used = 0;
			msgs_rd++;
			msgs_rd %= LOG_MAX_QUEUE;
		}
	}

	return NULL;
}

int log_server_start(int port)
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int i;

	listen_port = port;

	for (i = 0; i < LOG_MAX_QUEUE; i++) {
		msgs[i].used = 0;
	}
	
	for (i = 0; i < LOG_MAX_CLIENTS; i++) {
		clients[i].fd = -1;
		clients[i].used = 0;
	}

	sem_init(&queue_wait, 0, 1);

	printf("Starting log server on port %d\n", port);

	pthread_attr_init(&attr);
	pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN * 2);

	pthread_create(&thread_id, &attr, log_server, NULL);

	return 0;
}