xref: /illumos-gate/usr/src/cmd/syslogd/syslogd.h (revision 80ab886d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright 1983,1984,1985,1986,1987,1988,1989  AT&T.
27  * All rights reserved.
28  *
29  * University Copyright- Copyright (c) 1982, 1986, 1988
30  * The Regents of the University of California
31  * All Rights Reserved
32  *
33  * University Acknowledgment- Portions of this document are derived from
34  * software developed by the University of California, Berkeley, and its
35  * contributors.
36  */
37 
38 #ifndef	_SYSLOGD_H
39 #define	_SYSLOGD_H
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 #ifdef	__cplusplus
44 extern "C" {
45 #endif
46 
47 	struct utmpx dummy;	/* for sizeof ut_user, ut_line */
48 
49 /*
50  * Various constants & tunable values for syslogd
51  */
52 #define	DEBUGFILE 	"/var/adm/syslog.debug"
53 #define	MAXLINE		1024		/* maximum line length */
54 #define	DEFUPRI		(LOG_USER|LOG_INFO)
55 #define	DEFSPRI		(LOG_KERN|LOG_CRIT)
56 #define	MARKCOUNT	3		/* ratio of minor to major marks */
57 #define	UNAMESZ		(sizeof (dummy.ut_user)) /* length of a login name */
58 #define	UDEVSZ		(sizeof (dummy.ut_line)) /* length of login dev name */
59 #define	MAXUNAMES	20		/* maximum number of user names */
60 #define	Q_HIGHWATER_MARK 10000		/* max outstanding msgs per file */
61 #define	NOPRI		0x10		/* the "no priority" priority */
62 #define	LOG_MARK	(LOG_NFACILITIES << 3)	/* mark "facility" */
63 
64 /*
65  * host_list_t structure contains a list of hostnames for a given address
66  */
67 typedef	struct	host_list {
68 	int	hl_cnt;			/* number of hl_hosts entries */
69 	char	**hl_hosts;		/* hostnames */
70 	pthread_mutex_t hl_mutex;	/* protects this structs members */
71 	int	hl_refcnt;		/* reference count */
72 } host_list_t;
73 
74 /*
75  * host_info_t structure contains address information for a host
76  * from which we received a message.
77  */
78 typedef struct host_info {
79 	struct netconfig *ncp;
80 	struct netbuf addr;
81 } host_info_t;
82 
83 /*
84  * statistics structure attached to each filed for debugging
85  */
86 typedef struct filed_stats {
87 	int	flag;			/* flag word */
88 	int	total;			/* total messages logged */
89 	int	dups;			/* duplicate messages */
90 	int 	cantfwd;		/* can't forward */
91 	int	errs;			/* write errors */
92 } filed_stats_t;
93 
94 
95 /*
96  * internal representation of a log message. Includes all routing & bookkeeping
97  * information for the message. created in the system & network poll routines,
98  * and passed among the various processing threads as necessary
99  */
100 
101 typedef struct log_message {
102 	pthread_mutex_t msg_mutex;	/* protects this structs members */
103 	int refcnt;			/* message reference count */
104 	int pri;			/* message priority */
105 	int flags;			/* misc flags */
106 	time_t ts;			/* timestamp */
107 	host_list_t *hlp;		/* ptr to host list struct */
108 	void *ptr;			/* for anonymous use */
109 	char msg[MAXLINE+1];		/* the message itself */
110 } log_message_t;
111 
112 _NOTE(MUTEX_PROTECTS_DATA(log_message_t::msg_mutex, log_message_t))
113 _NOTE(DATA_READABLE_WITHOUT_LOCK(log_message_t))
114 
115 
116 /*
117  * format of a saved message. For each active file we are logging
118  * we save the last message and the current message, to make it
119  * possible to suppress duplicates on a per file basis. Earlier
120  * syslogd's used a global buffer for duplicate checking, so
121  * strict per file duplicate suppression was not always possible.
122  */
123 typedef struct saved_msg {
124 	int pri;
125 	int flags;
126 	time_t time;
127 	char host[SYS_NMLN+1];
128 	char msg[MAXLINE+1];
129 } saved_message_t;
130 
131 
132 /*
133  * Flags to logmsg().
134  */
135 
136 #define	IGN_CONS	0x001		/* don't print on console */
137 #define	IGN_FILE	0x002		/* don't write to log file */
138 #define	SYNC_FILE	0x004		/* do fsync on file after printing */
139 #define	NOCOPY		0x008		/* don't suppress duplicate messages */
140 #define	ADDDATE		0x010		/* add a date to the message */
141 #define	MARK		0x020		/* this message is a mark */
142 #define	LOGSYNC		0x040		/* nightly log update message */
143 #define	NETWORK		0x100		/* message came from the net */
144 #define	SHUTDOWN	0x200		/* internal shutdown message */
145 #define	FLUSHMSG	0x400		/* internal flush message */
146 
147 /*
148  * This structure represents the files that will have log
149  * copies printed.  There is one instance of this for each
150  * file that is being logged to.
151  */
152 struct filed {
153 	pthread_mutex_t filed_mutex;	/* protects this filed */
154 	pthread_t f_thread;		/* thread that handles this file */
155 	dataq_t f_queue;		/* queue of messages for this file */
156 	int f_queue_count;		/* count of messages on the queue */
157 	int f_prev_queue_count;		/* prev count of msgs on the queue */
158 	short	f_type;			/* entry type, see below */
159 	short	f_orig_type;		/* save entry type */
160 	int	f_file;			/* file descriptor */
161 	int	f_msgflag;		/* message disposition */
162 	filed_stats_t f_stat;		/* statistics */
163 	saved_message_t f_prevmsg;	/* previous message */
164 	saved_message_t f_current;	/* current message */
165 	int	f_prevcount;		/* message repeat count */
166 	uchar_t	f_pmask[LOG_NFACILITIES+1];	/* priority mask */
167 	union {
168 		char	f_uname[MAXUNAMES][SYS_NMLN + 1];
169 		struct {
170 			char	f_hname[SYS_NMLN + 1];
171 			struct netbuf	f_addr;
172 		} f_forw;		/* forwarding address */
173 		char	f_fname[MAXPATHLEN + 1];
174 	} f_un;
175 };
176 
177 _NOTE(MUTEX_PROTECTS_DATA(filed::filed_mutex, filed))
178 _NOTE(DATA_READABLE_WITHOUT_LOCK(filed))
179 
180 /* values for f_type */
181 #define	F_UNUSED	0		/* unused entry */
182 #define	F_FILE		1		/* regular file */
183 #define	F_TTY		2		/* terminal */
184 #define	F_CONSOLE	3		/* console terminal */
185 #define	F_FORW		4		/* remote machine */
186 #define	F_USERS		5		/* list of users */
187 #define	F_WALL		6		/* everyone logged on */
188 
189 /*
190  * values for logit routine
191  */
192 #define	CURRENT		0		/* print current message */
193 #define	SAVED		1		/* print saved message */
194 /*
195  * values for f_msgflag
196  */
197 #define	CURRENT_VALID	0x01		/* new message is good */
198 #define	OLD_VALID	0x02		/* old message is valid */
199 
200 /*
201  * code translation struct for use in processing config file
202  */
203 struct code {
204 	char	*c_name;
205 	int	c_val;
206 };
207 
208 /*
209  * structure describing a message to be sent to the wall thread.
210  * the thread id and attributes are stored in the structure
211  * passed to the thread, and the thread is created detached.
212  */
213 typedef struct wall_device {
214 	pthread_t thread;
215 	pthread_attr_t thread_attr;
216 	char dev[PATH_MAX + 1];
217 	char msg[MAXLINE+1];
218 	char ut_name[sizeof (dummy.ut_name)];
219 } walldev_t;
220 
221 /*
222  * hostname caching struct to reduce hostname name lookup.
223  */
224 struct hostname_cache {
225 	struct hostname_cache *next;
226 	struct netbuf addr;
227 	struct netconfig *ncp;
228 	host_list_t *h;
229 	time_t expire;
230 };
231 
232 #define	DEF_HNC_SIZE	2037
233 #define	DEF_HNC_TTL	1200	/* 20 minutes */
234 #define	MAX_BUCKETS	30
235 
236 /*
237  * function prototypes
238  */
239 int main(int argc, char **argv);
240 static void usage(void);
241 static void untty(void);
242 static void formatnet(struct netbuf *nbp, log_message_t *mp);
243 static void formatsys(struct log_ctl *lp, char *msg, int sync);
244 static void *logmsg(void *ap);
245 static void wallmsg(struct filed *f, char *from, char *msg);
246 static host_list_t *cvthname(struct netbuf *nbp, struct netconfig *ncp, char *);
247 static void set_flush_msg(struct filed *f);
248 static void flushmsg(int flags);
249 void logerror(const char *type, ...);
250 static void init(void);
251 static void conf_init(void);
252 static void cfline(char *line, int lineno, struct filed *f);
253 static int decode(char *name, struct code *codetab);
254 static int ismyaddr(struct netbuf *nbp);
255 static void getnets(void);
256 static int addnet(struct netconfig *ncp, struct netbuf *nbp);
257 static void bindnet(void);
258 static int logforward(struct filed *f, char *ebuf);
259 static int amiloghost(void);
260 static int same_addr(struct netbuf *, struct netbuf *);
261 static void prepare_sys_poll(void);
262 static void *sys_poll(void *ap);
263 static void getkmsg(int);
264 static void *net_poll(void *ap);
265 static log_message_t *new_msg(void);
266 static void free_msg(log_message_t *lm);
267 static int logmymsg(int pri, char *msg, int flags, int);
268 static void *logit(void *ap);
269 static void freehl(host_list_t *h);
270 static int filed_init(struct filed *h);
271 static void copy_msg(struct filed *f);
272 static void dumpstats(int fd);
273 static void filter_string(char *orig, char *new, size_t max);
274 static int openklog(char *name, int mode);
275 static void writemsg(int selection, struct filed *f);
276 static void *writetodev(void *ap);
277 static int shutdown_msg(void);
278 static void server(void *, char *, size_t, door_desc_t *, uint_t);
279 static char *alloc_stacks(int);
280 static void dealloc_stacks(int);
281 static int checkm4(void);
282 static void filed_destroy(struct filed *f);
283 static void open_door(void);
284 static void close_door(void);
285 static void delete_doorfiles(void);
286 static void signull(int, siginfo_t *, void *);
287 static int putctrlc(int, char **, size_t *, size_t);
288 static size_t findnl_bkwd(const char *, const size_t);
289 static size_t copynl_frwd(char *, const size_t, const char *, const size_t);
290 static size_t copy_frwd(char *, const size_t, const char *, const size_t);
291 static void logerror_format(const char *, char *, va_list);
292 static int logerror_to_console(int, const char *);
293 static void defaults(void);
294 static void shutdown_input(void);
295 static void *hostname_lookup(void *);
296 static void reconfigure(void);
297 static void disable_errorlog(void);
298 static void enable_errorlog(void);
299 
300 static void hnc_init(int);
301 static host_list_t *hnc_lookup(struct netbuf *,
302 		    struct netconfig *, int *);
303 static void hnc_register(struct netbuf *,
304 		    struct netconfig *, host_list_t *, int);
305 static void hnc_unreg(struct hostname_cache **);
306 static int addr_hash(struct netbuf *nbp);
307 
308 
309 #ifdef	__cplusplus
310 }
311 #endif
312 
313 #endif /* _SYSLOGD_H */
314