xref: /illumos-gate/usr/src/cmd/syslogd/syslogd.h (revision e2f4f3da)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50ea5e3a5Sjjj  * Common Development and Distribution License (the "License").
60ea5e3a5Sjjj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b13be141Snakanon  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
240ea5e3a5Sjjj  */
250ea5e3a5Sjjj 
260ea5e3a5Sjjj /*
277c478bd9Sstevel@tonic-gate  * Copyright 1983,1984,1985,1986,1987,1988,1989  AT&T.
287c478bd9Sstevel@tonic-gate  * All rights reserved.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifndef	_SYSLOGD_H
407c478bd9Sstevel@tonic-gate #define	_SYSLOGD_H
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
437c478bd9Sstevel@tonic-gate extern "C" {
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	struct utmpx dummy;	/* for sizeof ut_user, ut_line */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Various constants & tunable values for syslogd
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate #define	DEBUGFILE 	"/var/adm/syslog.debug"
527c478bd9Sstevel@tonic-gate #define	MAXLINE		1024		/* maximum line length */
537c478bd9Sstevel@tonic-gate #define	DEFUPRI		(LOG_USER|LOG_INFO)
547c478bd9Sstevel@tonic-gate #define	DEFSPRI		(LOG_KERN|LOG_CRIT)
557c478bd9Sstevel@tonic-gate #define	MARKCOUNT	3		/* ratio of minor to major marks */
567c478bd9Sstevel@tonic-gate #define	UNAMESZ		(sizeof (dummy.ut_user)) /* length of a login name */
577c478bd9Sstevel@tonic-gate #define	UDEVSZ		(sizeof (dummy.ut_line)) /* length of login dev name */
587c478bd9Sstevel@tonic-gate #define	MAXUNAMES	20		/* maximum number of user names */
597c478bd9Sstevel@tonic-gate #define	Q_HIGHWATER_MARK 10000		/* max outstanding msgs per file */
607c478bd9Sstevel@tonic-gate #define	NOPRI		0x10		/* the "no priority" priority */
617c478bd9Sstevel@tonic-gate #define	LOG_MARK	(LOG_NFACILITIES << 3)	/* mark "facility" */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * host_list_t structure contains a list of hostnames for a given address
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate typedef	struct	host_list {
677c478bd9Sstevel@tonic-gate 	int	hl_cnt;			/* number of hl_hosts entries */
687c478bd9Sstevel@tonic-gate 	char	**hl_hosts;		/* hostnames */
697c478bd9Sstevel@tonic-gate 	pthread_mutex_t hl_mutex;	/* protects this structs members */
707c478bd9Sstevel@tonic-gate 	int	hl_refcnt;		/* reference count */
717c478bd9Sstevel@tonic-gate } host_list_t;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * host_info_t structure contains address information for a host
757c478bd9Sstevel@tonic-gate  * from which we received a message.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate typedef struct host_info {
787c478bd9Sstevel@tonic-gate 	struct netconfig *ncp;
797c478bd9Sstevel@tonic-gate 	struct netbuf addr;
807c478bd9Sstevel@tonic-gate } host_info_t;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * statistics structure attached to each filed for debugging
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate typedef struct filed_stats {
867c478bd9Sstevel@tonic-gate 	int	flag;			/* flag word */
877c478bd9Sstevel@tonic-gate 	int	total;			/* total messages logged */
887c478bd9Sstevel@tonic-gate 	int	dups;			/* duplicate messages */
897c478bd9Sstevel@tonic-gate 	int 	cantfwd;		/* can't forward */
907c478bd9Sstevel@tonic-gate 	int	errs;			/* write errors */
917c478bd9Sstevel@tonic-gate } filed_stats_t;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * internal representation of a log message. Includes all routing & bookkeeping
967c478bd9Sstevel@tonic-gate  * information for the message. created in the system & network poll routines,
977c478bd9Sstevel@tonic-gate  * and passed among the various processing threads as necessary
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate typedef struct log_message {
1017c478bd9Sstevel@tonic-gate 	pthread_mutex_t msg_mutex;	/* protects this structs members */
1027c478bd9Sstevel@tonic-gate 	int refcnt;			/* message reference count */
1037c478bd9Sstevel@tonic-gate 	int pri;			/* message priority */
1047c478bd9Sstevel@tonic-gate 	int flags;			/* misc flags */
1057c478bd9Sstevel@tonic-gate 	time_t ts;			/* timestamp */
1067c478bd9Sstevel@tonic-gate 	host_list_t *hlp;		/* ptr to host list struct */
1077c478bd9Sstevel@tonic-gate 	void *ptr;			/* for anonymous use */
1087c478bd9Sstevel@tonic-gate 	char msg[MAXLINE+1];		/* the message itself */
1097c478bd9Sstevel@tonic-gate } log_message_t;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * format of a saved message. For each active file we are logging
1137c478bd9Sstevel@tonic-gate  * we save the last message and the current message, to make it
1147c478bd9Sstevel@tonic-gate  * possible to suppress duplicates on a per file basis. Earlier
1157c478bd9Sstevel@tonic-gate  * syslogd's used a global buffer for duplicate checking, so
1167c478bd9Sstevel@tonic-gate  * strict per file duplicate suppression was not always possible.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate typedef struct saved_msg {
1197c478bd9Sstevel@tonic-gate 	int pri;
1207c478bd9Sstevel@tonic-gate 	int flags;
1217c478bd9Sstevel@tonic-gate 	time_t time;
1227c478bd9Sstevel@tonic-gate 	char host[SYS_NMLN+1];
1237c478bd9Sstevel@tonic-gate 	char msg[MAXLINE+1];
1247c478bd9Sstevel@tonic-gate } saved_message_t;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Flags to logmsg().
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #define	IGN_CONS	0x001		/* don't print on console */
1327c478bd9Sstevel@tonic-gate #define	IGN_FILE	0x002		/* don't write to log file */
1337c478bd9Sstevel@tonic-gate #define	SYNC_FILE	0x004		/* do fsync on file after printing */
1347c478bd9Sstevel@tonic-gate #define	NOCOPY		0x008		/* don't suppress duplicate messages */
1357c478bd9Sstevel@tonic-gate #define	ADDDATE		0x010		/* add a date to the message */
1367c478bd9Sstevel@tonic-gate #define	MARK		0x020		/* this message is a mark */
1377c478bd9Sstevel@tonic-gate #define	LOGSYNC		0x040		/* nightly log update message */
1387c478bd9Sstevel@tonic-gate #define	NETWORK		0x100		/* message came from the net */
1397c478bd9Sstevel@tonic-gate #define	SHUTDOWN	0x200		/* internal shutdown message */
1407c478bd9Sstevel@tonic-gate #define	FLUSHMSG	0x400		/* internal flush message */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * This structure represents the files that will have log
1447c478bd9Sstevel@tonic-gate  * copies printed.  There is one instance of this for each
1457c478bd9Sstevel@tonic-gate  * file that is being logged to.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate struct filed {
1487c478bd9Sstevel@tonic-gate 	pthread_mutex_t filed_mutex;	/* protects this filed */
1497c478bd9Sstevel@tonic-gate 	pthread_t f_thread;		/* thread that handles this file */
1507c478bd9Sstevel@tonic-gate 	dataq_t f_queue;		/* queue of messages for this file */
1517c478bd9Sstevel@tonic-gate 	int f_queue_count;		/* count of messages on the queue */
1527c478bd9Sstevel@tonic-gate 	int f_prev_queue_count;		/* prev count of msgs on the queue */
1537c478bd9Sstevel@tonic-gate 	short	f_type;			/* entry type, see below */
1547c478bd9Sstevel@tonic-gate 	short	f_orig_type;		/* save entry type */
1557c478bd9Sstevel@tonic-gate 	int	f_file;			/* file descriptor */
1567c478bd9Sstevel@tonic-gate 	int	f_msgflag;		/* message disposition */
1577c478bd9Sstevel@tonic-gate 	filed_stats_t f_stat;		/* statistics */
1587c478bd9Sstevel@tonic-gate 	saved_message_t f_prevmsg;	/* previous message */
1597c478bd9Sstevel@tonic-gate 	saved_message_t f_current;	/* current message */
1607c478bd9Sstevel@tonic-gate 	int	f_prevcount;		/* message repeat count */
1617c478bd9Sstevel@tonic-gate 	uchar_t	f_pmask[LOG_NFACILITIES+1];	/* priority mask */
1627c478bd9Sstevel@tonic-gate 	union {
1637c478bd9Sstevel@tonic-gate 		char	f_uname[MAXUNAMES][SYS_NMLN + 1];
1647c478bd9Sstevel@tonic-gate 		struct {
1657c478bd9Sstevel@tonic-gate 			char	f_hname[SYS_NMLN + 1];
1667c478bd9Sstevel@tonic-gate 			struct netbuf	f_addr;
1677c478bd9Sstevel@tonic-gate 		} f_forw;		/* forwarding address */
1687c478bd9Sstevel@tonic-gate 		char	f_fname[MAXPATHLEN + 1];
1697c478bd9Sstevel@tonic-gate 	} f_un;
1707c478bd9Sstevel@tonic-gate };
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /* values for f_type */
1737c478bd9Sstevel@tonic-gate #define	F_UNUSED	0		/* unused entry */
1747c478bd9Sstevel@tonic-gate #define	F_FILE		1		/* regular file */
1757c478bd9Sstevel@tonic-gate #define	F_TTY		2		/* terminal */
1767c478bd9Sstevel@tonic-gate #define	F_CONSOLE	3		/* console terminal */
1777c478bd9Sstevel@tonic-gate #define	F_FORW		4		/* remote machine */
1787c478bd9Sstevel@tonic-gate #define	F_USERS		5		/* list of users */
1797c478bd9Sstevel@tonic-gate #define	F_WALL		6		/* everyone logged on */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * values for logit routine
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate #define	CURRENT		0		/* print current message */
1857c478bd9Sstevel@tonic-gate #define	SAVED		1		/* print saved message */
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * values for f_msgflag
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate #define	CURRENT_VALID	0x01		/* new message is good */
1907c478bd9Sstevel@tonic-gate #define	OLD_VALID	0x02		/* old message is valid */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * code translation struct for use in processing config file
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate struct code {
1967c478bd9Sstevel@tonic-gate 	char	*c_name;
1977c478bd9Sstevel@tonic-gate 	int	c_val;
1987c478bd9Sstevel@tonic-gate };
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * structure describing a message to be sent to the wall thread.
2027c478bd9Sstevel@tonic-gate  * the thread id and attributes are stored in the structure
2037c478bd9Sstevel@tonic-gate  * passed to the thread, and the thread is created detached.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate typedef struct wall_device {
2067c478bd9Sstevel@tonic-gate 	pthread_t thread;
2077c478bd9Sstevel@tonic-gate 	pthread_attr_t thread_attr;
2087c478bd9Sstevel@tonic-gate 	char dev[PATH_MAX + 1];
2097c478bd9Sstevel@tonic-gate 	char msg[MAXLINE+1];
2107c478bd9Sstevel@tonic-gate 	char ut_name[sizeof (dummy.ut_name)];
2117c478bd9Sstevel@tonic-gate } walldev_t;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * hostname caching struct to reduce hostname name lookup.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate struct hostname_cache {
2177c478bd9Sstevel@tonic-gate 	struct hostname_cache *next;
2187c478bd9Sstevel@tonic-gate 	struct netbuf addr;
2197c478bd9Sstevel@tonic-gate 	struct netconfig *ncp;
2207c478bd9Sstevel@tonic-gate 	host_list_t *h;
2217c478bd9Sstevel@tonic-gate 	time_t expire;
2227c478bd9Sstevel@tonic-gate };
2237c478bd9Sstevel@tonic-gate 
224293d1fc4Spd #define	DEF_HNC_SIZE	2037
225293d1fc4Spd #define	DEF_HNC_TTL	1200	/* 20 minutes */
226293d1fc4Spd #define	MAX_BUCKETS	30
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * function prototypes
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate int main(int argc, char **argv);
2327c478bd9Sstevel@tonic-gate static void usage(void);
2337c478bd9Sstevel@tonic-gate static void untty(void);
2347c478bd9Sstevel@tonic-gate static void formatnet(struct netbuf *nbp, log_message_t *mp);
2357c478bd9Sstevel@tonic-gate static void formatsys(struct log_ctl *lp, char *msg, int sync);
2367c478bd9Sstevel@tonic-gate static void *logmsg(void *ap);
2377c478bd9Sstevel@tonic-gate static void wallmsg(struct filed *f, char *from, char *msg);
2387c478bd9Sstevel@tonic-gate static host_list_t *cvthname(struct netbuf *nbp, struct netconfig *ncp, char *);
2397c478bd9Sstevel@tonic-gate static void set_flush_msg(struct filed *f);
2407c478bd9Sstevel@tonic-gate static void flushmsg(int flags);
2417c478bd9Sstevel@tonic-gate void logerror(const char *type, ...);
2427c478bd9Sstevel@tonic-gate static void init(void);
2437c478bd9Sstevel@tonic-gate static void conf_init(void);
2447c478bd9Sstevel@tonic-gate static void cfline(char *line, int lineno, struct filed *f);
2457c478bd9Sstevel@tonic-gate static int decode(char *name, struct code *codetab);
2467c478bd9Sstevel@tonic-gate static int ismyaddr(struct netbuf *nbp);
2477c478bd9Sstevel@tonic-gate static void getnets(void);
2487c478bd9Sstevel@tonic-gate static int addnet(struct netconfig *ncp, struct netbuf *nbp);
2497c478bd9Sstevel@tonic-gate static void bindnet(void);
2500ea5e3a5Sjjj static int logforward(struct filed *f, char *ebuf, size_t elen);
2517c478bd9Sstevel@tonic-gate static int amiloghost(void);
2527c478bd9Sstevel@tonic-gate static int same_addr(struct netbuf *, struct netbuf *);
2537c478bd9Sstevel@tonic-gate static void prepare_sys_poll(void);
2547c478bd9Sstevel@tonic-gate static void *sys_poll(void *ap);
2557c478bd9Sstevel@tonic-gate static void getkmsg(int);
2567c478bd9Sstevel@tonic-gate static void *net_poll(void *ap);
2577c478bd9Sstevel@tonic-gate static log_message_t *new_msg(void);
2587c478bd9Sstevel@tonic-gate static void free_msg(log_message_t *lm);
2597c478bd9Sstevel@tonic-gate static int logmymsg(int pri, char *msg, int flags, int);
2607c478bd9Sstevel@tonic-gate static void *logit(void *ap);
2617c478bd9Sstevel@tonic-gate static void freehl(host_list_t *h);
2627c478bd9Sstevel@tonic-gate static int filed_init(struct filed *h);
2637c478bd9Sstevel@tonic-gate static void copy_msg(struct filed *f);
2647c478bd9Sstevel@tonic-gate static void dumpstats(int fd);
2657c478bd9Sstevel@tonic-gate static void filter_string(char *orig, char *new, size_t max);
2667c478bd9Sstevel@tonic-gate static int openklog(char *name, int mode);
2677c478bd9Sstevel@tonic-gate static void writemsg(int selection, struct filed *f);
2687c478bd9Sstevel@tonic-gate static void *writetodev(void *ap);
2697c478bd9Sstevel@tonic-gate static int shutdown_msg(void);
2707c478bd9Sstevel@tonic-gate static void server(void *, char *, size_t, door_desc_t *, uint_t);
271*b13be141Snakanon static void *create_door_thr(void *);
272*b13be141Snakanon static void door_server_pool(door_info_t *);
2737c478bd9Sstevel@tonic-gate static char *alloc_stacks(int);
2747c478bd9Sstevel@tonic-gate static void dealloc_stacks(int);
2757c478bd9Sstevel@tonic-gate static int checkm4(void);
2767c478bd9Sstevel@tonic-gate static void filed_destroy(struct filed *f);
2777c478bd9Sstevel@tonic-gate static void open_door(void);
2787c478bd9Sstevel@tonic-gate static void close_door(void);
2797c478bd9Sstevel@tonic-gate static void delete_doorfiles(void);
2807c478bd9Sstevel@tonic-gate static void signull(int, siginfo_t *, void *);
2817c478bd9Sstevel@tonic-gate static int putctrlc(int, char **, size_t *, size_t);
2827c478bd9Sstevel@tonic-gate static size_t findnl_bkwd(const char *, const size_t);
2837c478bd9Sstevel@tonic-gate static size_t copynl_frwd(char *, const size_t, const char *, const size_t);
2847c478bd9Sstevel@tonic-gate static size_t copy_frwd(char *, const size_t, const char *, const size_t);
2857c478bd9Sstevel@tonic-gate static void logerror_format(const char *, char *, va_list);
2867c478bd9Sstevel@tonic-gate static int logerror_to_console(int, const char *);
2870ea5e3a5Sjjj static void properties(void);
2887c478bd9Sstevel@tonic-gate static void shutdown_input(void);
2897c478bd9Sstevel@tonic-gate static void *hostname_lookup(void *);
2907c478bd9Sstevel@tonic-gate static void reconfigure(void);
2917c478bd9Sstevel@tonic-gate static void disable_errorlog(void);
2927c478bd9Sstevel@tonic-gate static void enable_errorlog(void);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate static void hnc_init(int);
295293d1fc4Spd static host_list_t *hnc_lookup(struct netbuf *,
296293d1fc4Spd 		    struct netconfig *, int *);
297293d1fc4Spd static void hnc_register(struct netbuf *,
298293d1fc4Spd 		    struct netconfig *, host_list_t *, int);
2997c478bd9Sstevel@tonic-gate static void hnc_unreg(struct hostname_cache **);
300293d1fc4Spd static int addr_hash(struct netbuf *nbp);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate #endif /* _SYSLOGD_H */
308