Projet

Général

Profil

Télécharger (12,6 ko) Statistiques
| Branche: | Révision:

univnautes-tools / patches / stable / 10 / clog.diff @ 4ab3b90b

1
Index: Makefile
2
===================================================================
3
RCS file: Makefile
4
diff -N Makefile
5
--- /dev/null	1 Jan 1970 00:00:00 -0000
6
+++ Makefile	8 Jun 2009 00:41:07 -0000
7
@@ -0,0 +1,10 @@
8
+#	@(#)Makefile	8.1 (Berkeley) 6/6/93
9
+# $Id: Makefile,v 1.2 2001/10/02 06:03:21 jdw Exp $
10
+# $DragonFly: src/usr.sbin/clog/Makefile,v 1.3 2007/04/09 09:45:12 swildner Exp $
11
+
12
+PROG=	clog
13
+MAN8=	clog.8
14
+CFLAGS=-O
15
+
16
+.include <bsd.prog.mk>
17
+
18
Index: clog.8
19
===================================================================
20
RCS file: clog.8
21
diff -N clog.8
22
--- /dev/null	1 Jan 1970 00:00:00 -0000
23
+++ clog.8	2 Oct 2001 04:41:21 -0000
24
@@ -0,0 +1,98 @@
25
+.\" Copyright (c) 2001
26
+.\"	Jeffrey D. Wheelhouse.  All rights reserved.
27
+.\"
28
+.\" This code was originally developed by Jeff Wheelhouse (jdw@wwwi.com).
29
+.\"
30
+.\" Redistribution and use in source and binary forms, with or without
31
+.\" modification, are permitted provided that the following conditions
32
+.\" are met:
33
+.\" 1. Redistributions of source code must retain the above copyright
34
+.\"    notice, this list of conditions and the following disclaimer.
35
+.\" 2. Redistributions in binary form must reproduce the above copyright
36
+.\"    notice, this list of conditions and the following disclaimer in the
37
+.\"    documentation and/or other materials provided with the distribution.
38
+.\"
39
+.\" THIS SOFTWARE IS PROVIDED BY JEFF WHEELHOUSE ``AS IS'' AND ANY EXPRESS OR 
40
+.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
41
+.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
42
+.\" NO EVENT SHALL JEFF WHEELHOUSE BE LIABLE FOR ANY DIRECT, INDIRECT, 
43
+.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT
44
+.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
45
+.\" OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
46
+.\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
47
+.\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
48
+.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49
+.\"
50
+.\"     $Id: clog.8,v 1.2 2001/10/02 04:41:21 jdw Exp $
51
+.\"
52
+.Dd October 1, 2001
53
+.Dt CLOG 8
54
+.Os BSD 4
55
+.Sh NAME
56
+.Nm clog
57
+.Nd "display or initialize a circular system log"
58
+.Sh SYNOPSIS
59
+.Nm
60
+.Op Fl f
61
+.Op Fl i Fl s Ar size
62
+.Ar logfile
63
+.Sh DESCRIPTION
64
+.Nm Clog
65
+displays or initializes a circular log file.
66
+.Pp
67
+The options are as follows:
68
+.Bl -tag -width indent
69
+.It Fl f
70
+Display the contents of the circular logfile
71
+.Ar logfile ,
72
+then go into a loop waiting for
73
+new material to arrive.  This is essentially the same as using the
74
+.Fl f
75
+option of the 
76
+.Xr tail 1
77
+command on a standard syslog file.
78
+.It Fl i
79
+Initialize 
80
+.Ar logfile
81
+rather than reading it.  This option requires the 
82
+.Fl s
83
+option.  If 
84
+.Ar logfile
85
+already exists, it will be truncated and recreated by this command.
86
+.It Fl s
87
+This option specifies the size in bytes of the circular logfile that should 
88
+be created.  This option requires the
89
+.Fl i
90
+option.
91
+.El
92
+.Sh ABOUT CIRCULAR LOGFILES
93
+The
94
+.Nm
95
+command supports circular logfiles for 
96
+.Xr syslogd 8 .
97
+A circular logfile differs from a standard syslog file in that is has a fixed
98
+size.  It does not grow, and does not need to be rotated.  When 
99
+.Xr syslogd 8 
100
+reaches the end of a circular logfile, it simply begins again at the beginning,
101
+overwriting the oldest data.  The circular logfile also contains information 
102
+allowing 
103
+.Nm
104
+to establish what parts of the file are valid, and in what order they should
105
+be displayed.
106
+.Pp
107
+Circular logfiles are primarily useful for their ability to control the amount
108
+of storage devoted to logfiles.  This may be valuable when storage space is 
109
+at a premium or when the consequences of running out of storage space are 
110
+unacceptable.  Circular logfiles can safely be used on a memory disk (see
111
+.Xr md 4 ).
112
+.Pp
113
+Circular logfiles are also useful to catch messages that are generated rapidly
114
+but soon lose relevance, such as messages logged at debug priority.
115
+
116
+.Sh SEE ALSO
117
+.Xr syslogd 8 ,
118
+.Xr syslog.conf 5
119
+.Sh HISTORY
120
+The
121
+.Nm
122
+command was written for FreeBSD 4.3 but is not yet part of a BSD distribution.
123
Index: clog.c
124
===================================================================
125
RCS file: clog.c
126
diff -N clog.c
127
--- /dev/null	1 Jan 1970 00:00:00 -0000
128
+++ clog.c	8 Jun 2009 00:18:19 -0000
129
@@ -0,0 +1,235 @@
130
+/*-
131
+ * Copyright (c) 2001
132
+ *     Jeff Wheelhouse (jdw@wwwi.com)
133
+ *
134
+ * This code was originally developed by Jeff Wheelhouse (jdw@wwwi.com).
135
+ * 
136
+ * Redistribution and use in source and binary forms, with or without
137
+ * modification, are permitted provided that the following conditions
138
+ * are met:
139
+ * 1. Redistribution of source code must retail the above copyright 
140
+ *    notice, this list of conditions and the following disclaimer.
141
+ * 2. Redistributions in binary form must reproduce the above copyright
142
+ *    notice, this list of conditions and the following disclaimer in the
143
+ *    documentation and/or other materials provided with the distribution.
144
+ *
145
+ * THIS SOFTWARE IS PROVIDED BY JEFF WHEELHOUSE ``AS IS'' AND ANY EXPRESS OR 
146
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
147
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN 
148
+ * NO EVENT SHALL JEFF WHEELHOUSE BE LIABLE FOR ANY DIRECT, INDIRECT, 
149
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT 
150
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
151
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
152
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
153
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
154
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
155
+ *
156
+ * $Id: clog.c,v 1.3 2001/10/02 18:51:26 jdw Exp $
157
+ * $DragonFly: src/usr.sbin/clog/clog.c,v 1.4 2007/06/26 23:30:05 josepht Exp $
158
+ */
159
+
160
+
161
+#include <assert.h>
162
+#include <errno.h>
163
+#include <fcntl.h>
164
+#include <poll.h>
165
+#include <signal.h>
166
+#include <stdio.h>
167
+#include <stdlib.h>
168
+#include <string.h>
169
+#include <unistd.h>
170
+
171
+#include <sys/mman.h>
172
+#include <sys/sched.h>
173
+#include <sys/stat.h>
174
+#include <sys/uio.h>
175
+
176
+
177
+#include "clog.h"
178
+
179
+
180
+/*
181
+ *  The BUFFER_SIZE value is just used to allocate a buffer full of NULLs 
182
+ *  so that a new logfile can be extended to its full size.
183
+ *
184
+ *  Compiling with -pedantic complains when the buffer array is declared
185
+ *  if I declare this as a const instead of a #define.
186
+ */
187
+#define BUFFER_SIZE 16384
188
+
189
+void init_log(const char *lname, size_t size);
190
+void read_log(const char *lname, int optf);
191
+void usage(void);
192
+
193
+const char *pname;
194
+
195
+int
196
+main(int argc, char **argv)
197
+{
198
+	int ch;
199
+	int init = 0;
200
+	int size = 0;
201
+	int optf = 0;
202
+
203
+	pname = argv[0];
204
+
205
+	while ((ch = getopt(argc, argv, "fis:")) != -1)
206
+		switch(ch) {
207
+		case 'i':
208
+			init = 1;
209
+			break;
210
+		case 's':
211
+			size = atol(optarg);
212
+			if (size==0) usage();
213
+			break;
214
+		case 'f':
215
+			optf = 1;
216
+		}
217
+
218
+	if ((size>0)&&(init==0)) {
219
+		fprintf(stderr,"%s: WARNING: -s argument ignored without -i.\n",pname);
220
+		size = 0;
221
+	}
222
+	if (argv[optind]==NULL) {
223
+		fprintf(stderr,"%s: ERROR: log_file argument must be specified.\n",pname);
224
+		usage();
225
+	}
226
+	if ((init==1)&&(size==0)) {
227
+		fprintf(stderr,"%s: ERROR: -i argument requires -s.\n",pname);
228
+		usage();
229
+	}
230
+	if ((init==1)&&(optf==1)) {
231
+		fprintf(stderr,"%s: ERROR: flags -f and -i are incompatible.\n",pname);
232
+		usage();
233
+	}
234
+
235
+	if (init==1) init_log(argv[optind],size);
236
+	/* if (optf==1) follow_log(artv[optind]); */
237
+	read_log(argv[optind],optf);
238
+		
239
+	return 0;
240
+}
241
+
242
+
243
+void
244
+usage(void)
245
+{
246
+  fprintf(stderr,"usage: %s [-i -s log_size] [ -f ] log_file\n",pname);
247
+  exit(1);
248
+}
249
+
250
+
251
+void
252
+read_log(const char *lname, int optf)
253
+{
254
+	int fd;
255
+	struct stat sb;
256
+	struct clog_footer *pcf;
257
+	char *pbuffer;
258
+	struct iovec iov[2];
259
+	int iovcnt = 0;
260
+	uint32_t start = 0;
261
+	uint32_t next;
262
+	struct pollfd pfd;
263
+
264
+	pfd.fd = -1;
265
+
266
+	fd = open(lname,O_RDONLY);
267
+	if (fd==-1) {
268
+		fprintf(stderr,"%s: ERROR: could not open %s (%s)\n",pname,lname,strerror(errno));
269
+		exit(11);
270
+	}
271
+
272
+	if (fstat(fd,&sb)==-1) {
273
+		fprintf(stderr,"%s: ERROR: could not stat %s (%s)\n",pname,lname,strerror(errno));
274
+		exit(13);
275
+	}
276
+	pbuffer = mmap(NULL,sb.st_size,PROT_READ,MAP_SHARED,fd,0);
277
+	if (pbuffer==NULL) {
278
+		fprintf(stderr,"%s: ERROR: could not mmap %s body (%s)\n",pname,lname,strerror(errno));
279
+		exit(14);
280
+	}
281
+	pcf = (struct clog_footer*)(pbuffer + sb.st_size - sizeof(struct clog_footer));
282
+
283
+	if (pcf->cf_wrap==1) start = pcf->cf_next + 1;
284
+	while(1) {
285
+		while(pcf->cf_lock==1) sched_yield();
286
+		next = pcf->cf_next;
287
+		iovcnt = 0;
288
+		if (start>next) {
289
+			iov[iovcnt].iov_base = pbuffer + start;
290
+			iov[iovcnt++].iov_len = pcf->cf_max - start;
291
+			start = 0;
292
+		}
293
+		iov[iovcnt].iov_base = pbuffer + start;
294
+		iov[iovcnt++].iov_len = next - start;
295
+		if (writev(1,iov,iovcnt)==-1) {
296
+			fprintf(stderr,"%s: ERROR: could not write output (%s)\n",pname,strerror(errno));
297
+			exit(15);
298
+		}
299
+		start = next;
300
+		if (optf==0) break;
301
+		if (poll(&pfd,1,50)==-1) {
302
+			fprintf(stderr,"%s: ERROR: could not poll (%s)\n",pname,strerror(errno));
303
+			exit(16);
304
+		}
305
+	}
306
+	
307
+	munmap(pbuffer,sb.st_size);
308
+	close(fd);
309
+
310
+	exit(0);
311
+}
312
+
313
+
314
+void
315
+init_log(const char *lname, size_t size)
316
+{
317
+	int fd;
318
+	size_t fill = size;
319
+	char buffer[BUFFER_SIZE];
320
+	struct clog_footer cf;
321
+
322
+	memcpy(&cf.cf_magic,MAGIC_CONST,4);
323
+	cf.cf_max = size - sizeof(struct clog_footer);
324
+
325
+	memset(buffer,0,BUFFER_SIZE);
326
+
327
+	fd = open(lname,O_RDWR|O_CREAT,0666); 
328
+	if (fd==-1) {
329
+		fprintf(stderr,"%s: ERROR: could not open %s (%s)\n",pname,lname,strerror(errno));
330
+		exit(2);
331
+	}
332
+	if (ftruncate(fd,(off_t)0)==-1) {
333
+		fprintf(stderr,"%s: ERROR: could not truncate %s (%s)\n",pname,lname,strerror(errno));
334
+		exit(3);
335
+	}
336
+	
337
+	while(fill>BUFFER_SIZE) {
338
+		if (write(fd,buffer,BUFFER_SIZE)==-1){
339
+			fprintf(stderr,"%s: ERROR: could not write %s (%s)\n",pname,lname,strerror(errno));
340
+			exit(4);
341
+		}
342
+		fill -= BUFFER_SIZE;
343
+	}
344
+	assert(fill<=BUFFER_SIZE);
345
+	if (fill>0) {
346
+		if (write(fd,buffer,fill)==-1) {
347
+			fprintf(stderr,"%s: ERROR: could not write %s (%s)\n",pname,lname,strerror(errno));
348
+			exit(5);
349
+		}
350
+	}
351
+	if (lseek(fd,-(off_t)(sizeof(struct clog_footer)),SEEK_END)==-1) {
352
+		fprintf(stderr,"%s: ERROR: could not seek in %s (%s)\n",pname,lname,strerror(errno));
353
+		exit(6);
354
+	}
355
+	if (write(fd,&cf,sizeof(cf))==-1) {
356
+		fprintf(stderr,"%s: ERROR: could not write magic in %s (%s)\n",pname,lname,strerror(errno));
357
+		exit(7);
358
+	}
359
+	close(fd);
360
+	exit(0);
361
+}
362
+
363
+
364
+
365
Index: clog.h
366
===================================================================
367
RCS file: clog.h
368
diff -N clog.h
369
--- /dev/null	1 Jan 1970 00:00:00 -0000
370
+++ clog.h	8 Jun 2009 00:27:27 -0000
371
@@ -0,0 +1,55 @@
372
+/*-
373
+ * Copyright (c) 2001
374
+ *     Jeff Wheelhouse (jdw@wwwi.com)
375
+ *
376
+ * This code was originally developed by Jeff Wheelhouse (jdw@wwwi.com).
377
+ * 
378
+ * Redistribution and use in source and binary forms, with or without
379
+ * modification, are permitted provided that the following conditions
380
+ * are met:
381
+ * 1. Redistribution of source code must retail the above copyright 
382
+ *    notice, this list of conditions and the following disclaimer.
383
+ * 2. Redistributions in binary form must reproduce the above copyright
384
+ *    notice, this list of conditions and the following disclaimer in the
385
+ *    documentation and/or other materials provided with the distribution.
386
+ *
387
+ * THIS SOFTWARE IS PROVIDED BY JEFF WHEELHOUSE ``AS IS'' AND ANY EXPRESS OR 
388
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
389
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN 
390
+ * NO EVENT SHALL JEFF WHEELHOUSE BE LIABLE FOR ANY DIRECT, INDIRECT, 
391
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT 
392
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
393
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
394
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
395
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
396
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
397
+ *
398
+ * $Id: clog.h,v 1.2 2001/10/02 04:43:52 jdw Exp $
399
+ * $DragonFly: src/usr.sbin/clog/clog.h,v 1.1 2004/10/30 20:26:46 dillon Exp $
400
+ */
401
+
402
+
403
+#ifndef _CLOG_H_
404
+#define _CLOG_H_
405
+
406
+/*
407
+ *  This magic constant is used to identify a valid circular log file.
408
+ *  syslogd will ignore any circular log file that doesn't have this constant.
409
+ */
410
+
411
+const char MAGIC_CONST[4] = "CLOG";
412
+
413
+
414
+struct clog_footer {
415
+	uint32_t cf_magic;
416
+	uint32_t cf_wrap;
417
+	uint32_t cf_next;
418
+	uint32_t cf_max;
419
+	uint32_t cf_lock;
420
+};
421
+
422
+
423
+#endif  /* _CLOG_H_ */
424
+	
425
+
426
+
(13-13/67)