1 /*	$CoreSDI: im_bsd.c,v 1.83 2001/11/21 05:15:25 alejo Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, Core SDI S.A., Argentina
5  * All rights reserved
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither name of the Core SDI S.A. nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * im_bsd -- classic behaviour module for BSD like systems
34  *
35  * Author: Alejo Sanchez for Core-SDI SA
36  *         from syslogd.c by Eric Allman and Ralph Campbell
37  *
38  */
39 
40 #include "config.h"
41 
42 #include <sys/types.h>
43 #include <sys/uio.h>
44 #include <sys/param.h>
45 
46 #include <fcntl.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <unistd.h>
54 #include <time.h>
55 
56 #include "../modules.h"
57 #include "../syslogd.h"
58 
59 /*
60  * initialize BSD input
61  *
62  */
63 
64 
65 int
im_bsd_init(struct i_module * I,char ** argv,int argc)66 im_bsd_init(struct i_module *I, char **argv, int argc)
67 {
68 
69 	dprintf(MSYSLOG_INFORMATIVE, "im_bsd_init: Entering\n");
70 
71 	if ((I->im_fd = open(_PATH_KLOG, O_RDONLY, 0)) < 0) {
72 		dprintf(MSYSLOG_SERIOUS, "can't open %s (%d)\n", _PATH_KLOG,
73 		    errno);
74 		return (-1);
75 	}
76 
77 	I->im_path = _PATH_KLOG;
78 	I->im_flags |= IMODULE_FLAG_KERN;
79 	add_fd_input(I->im_fd , I);
80 	return (I->im_fd);
81 }
82 
83 
84 /*
85  * get messge
86  *
87  * Take a raw input line from /dev/klog, split and format similar to syslog().
88  */
89 
90 int
im_bsd_read(struct i_module * im,int infd,struct im_msg * ret)91 im_bsd_read(struct i_module *im, int infd, struct im_msg *ret)
92 {
93 	char *p, *q, *lp;
94 	int i, c;
95 
96 	strncpy(ret->im_msg, _PATH_UNIX, sizeof(ret->im_msg) - 1);
97 	ret->im_msg[sizeof (ret->im_msg) - 1] = '\0';
98 
99 	strncat(ret->im_msg, ": ", sizeof (ret->im_msg) - 1
100 	    - strlen(ret->im_msg));
101 
102 	lp = ret->im_msg + strlen(ret->im_msg);
103 
104 	i = read(im->im_fd, im->im_buf, sizeof(im->im_buf) - 1);
105 	if (i > 0) {
106 		(im->im_buf)[i] = '\0';
107 		for (p = im->im_buf; *p != '\0'; ) {
108 			/* fsync file after write */
109 			ret->im_flags = SYNC_FILE | ADDDATE;
110 			ret->im_pri = DEFSPRI;
111 			if (*p == '<') {
112 				ret->im_pri = 0;
113 				while (isdigit((int)*++p))
114 					ret->im_pri = 10 * ret->im_pri +
115 					    (*p - '0');
116 				if (*p == '>')
117 					++p;
118 			} else {
119 				/* kernel printf's come out on console */
120 				ret->im_flags |= IGN_CONS;
121 			}
122 			if (ret->im_pri &~ (LOG_FACMASK|LOG_PRIMASK))
123 				ret->im_pri = DEFSPRI;
124 			q = lp;
125 			while (*p != '\0' && (c = *p++) != '\n' &&
126 			    q < &ret->im_msg[sizeof(ret->im_msg) - 1])
127 				*q++ = c;
128 			*q = '\0';
129 			ret->im_host[0] = '\0';
130 			ret->im_len = strlen(ret->im_msg);
131 			logmsg(ret->im_pri, ret->im_msg, ret->im_host,
132 			    ret->im_flags);
133 		}
134 	} else if (i < 0 && errno != EINTR) {
135 		logerror("im_bsd_read");
136 		im->im_fd = -1;
137 	}
138 
139 	/* if ok return (2) wich means already logged */
140 	return (im->im_fd == -1 ? -1: 2);
141 }
142 
143 int
im_bsd_close(struct i_module * im)144 im_bsd_close (struct i_module *im)
145 {
146 	if (im->im_fd >= 0)
147 		close(im->im_fd);
148 
149 	return (0);
150 }
151