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