]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/syslogd.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 2004 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/fcntl.h>
33 #include <sys/queue.h>
41 #define DEFAULT_MARK_SEC 0
42 #define DEFAULT_PRUNE_DAYS 7
43 #define PRUNE_AFTER_START_DELAY 300
45 #define NOTIFY_DELAY 1
47 #define streq(A,B) (strcmp(A,B)==0)
48 #define forever for(;;)
53 static TAILQ_HEAD(ml
, module_list
) Moduleq
;
56 int asl_log_filter
= ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE
);
59 extern int __notify_78945668_info__
;
68 static int activate_asl_in
= 1;
70 int asl_action_init();
71 int asl_action_reset();
72 int asl_action_close();
73 static int activate_asl_action
= 1;
78 static int activate_klog_in
= 1;
83 static int activate_bsd_in
= 1;
88 static int activate_bsd_out
= 1;
93 static int activate_udp_in
= 0;
96 * Module approach: only one type of module. This module may implement
97 * the set of functions necessary for any of the functions (input, output,
98 * etc.) Prior to using the modules, dlsym() is consulted to see what it
105 struct module_list
*tmp
;
107 if (activate_asl_in
!= 0)
109 tmp
= calloc(1, sizeof(struct module_list
));
110 if (tmp
== NULL
) return 1;
111 tmp
->name
= strdup("asl_in");
113 tmp
->init
= asl_in_init
;
114 tmp
->reset
= asl_in_reset
;
115 tmp
->close
= asl_in_close
;
116 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
119 if (activate_asl_action
!= 0)
121 tmp
= calloc(1, sizeof(struct module_list
));
122 if (tmp
== NULL
) return 1;
123 tmp
->name
= strdup("asl_action");
125 tmp
->init
= asl_action_init
;
126 tmp
->reset
= asl_action_reset
;
127 tmp
->close
= asl_action_close
;
128 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
131 if (activate_klog_in
!= 0)
133 tmp
= calloc(1, sizeof(struct module_list
));
134 if (tmp
== NULL
) return 1;
135 tmp
->name
= strdup("klog_in");
137 tmp
->init
= klog_in_init
;
138 tmp
->reset
= klog_in_reset
;
139 tmp
->close
= klog_in_close
;
140 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
143 if (activate_bsd_in
!= 0)
145 tmp
= calloc(1, sizeof(struct module_list
));
146 if (tmp
== NULL
) return 1;
147 tmp
->name
= strdup("bsd_in");
149 tmp
->init
= bsd_in_init
;
150 tmp
->reset
= bsd_in_reset
;
151 tmp
->close
= bsd_in_close
;
152 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
155 if (activate_bsd_out
!= 0)
157 tmp
= calloc(1, sizeof(struct module_list
));
158 if (tmp
== NULL
) return 1;
159 tmp
->name
= strdup("bsd_out");
161 tmp
->init
= bsd_out_init
;
162 tmp
->reset
= bsd_out_reset
;
163 tmp
->close
= bsd_out_close
;
164 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
167 if (activate_udp_in
!= 0)
169 tmp
= calloc(1, sizeof(struct module_list
));
170 if (tmp
== NULL
) return 1;
171 tmp
->name
= strdup("udp_in");
173 tmp
->init
= udp_in_init
;
174 tmp
->reset
= udp_in_reset
;
175 tmp
->close
= udp_in_close
;
176 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
183 * Loads all the modules. This DOES NOT call the modules initializer
184 * functions. It simply scans the modules directory looking for modules
185 * and loads them. This does not mean the module will be used.
188 load_modules(char *mp
)
192 struct module_list
*tmp
;
194 char *modulepath
= NULL
;
197 if (d
== NULL
) return -1;
199 while (NULL
!= (de
= readdir(d
)))
201 if (de
->d_name
[0] == '.') continue;
203 /* Must have ".so" in the name" */
204 if (!strstr(de
->d_name
, ".so")) continue;
206 asprintf(&modulepath
, "%s/%s", mp
, de
->d_name
);
207 if (!modulepath
) continue;
209 c
= dlopen(modulepath
, RTLD_LOCAL
);
216 tmp
= calloc(1, sizeof(struct module_list
));
224 bn
= basename(modulepath
);
225 tmp
->name
= strdup(bn
);
227 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
229 tmp
->init
= dlsym(tmp
->module, "aslmod_init");
230 tmp
->reset
= dlsym(tmp
->module, "aslmod_reset");
231 tmp
->close
= dlsym(tmp
->module, "aslmod_close");
246 fp
= fopen(_PATH_PIDFILE
, "w");
249 fprintf(fp
, "%d\n", getpid());
259 for (i
= getdtablesize() - 1; i
>= 0; i
--) close(i
);
261 open("/dev/null", O_RDWR
, 0);
269 signal(SIGINT
, SIG_IGN
);
270 signal(SIGPIPE
, SIG_IGN
);
281 catch_sigwinch(int x
)
289 struct module_list
*mod
;
291 for (mod
= Moduleq
.tqh_first
; mod
!= NULL
; mod
= mod
->entries
.tqe_next
)
293 if (mod
->reset
!= NULL
) mod
->reset();
298 main(int argc
, char *argv
[])
300 struct module_list
*mod
;
301 fd_set rd
, wr
, ex
, kern
;
302 int fd
, i
, max
, status
, pdays
, daemonize
;
303 time_t lastmark
, msec
, ssec
, tick
, delta
, ptime
, notify_time
;
305 struct timeval timeout
, *pto
, zto
;
308 mp
= _PATH_MODULE_LIB
;
309 msec
= DEFAULT_MARK_SEC
;
310 pdays
= DEFAULT_PRUNE_DAYS
;
312 __notify_78945668_info__
= -1;
318 for (i
= 1; i
< argc
; i
++)
320 if (streq(argv
[i
], "-d"))
324 if (streq(argv
[i
], "-D"))
328 if (streq(argv
[i
], "-u"))
332 else if (streq(argv
[i
], "-m"))
334 if ((i
+ 1) < argc
) msec
= 60 * atoi(argv
[++i
]);
336 else if (streq(argv
[i
], "-p"))
338 if ((i
+ 1) < argc
) pdays
= atoi(argv
[++i
]);
340 else if (streq(argv
[i
], "-l"))
342 if ((i
+ 1) < argc
) mp
= argv
[++i
];
344 else if (streq(argv
[i
], "-c"))
349 if ((argv
[i
][0] >= '0') && (argv
[i
][0] <= '7') && (argv
[i
][1] == '\0')) asl_log_filter
= ASL_FILTER_MASK_UPTO(atoi(argv
[i
]));
352 else if (streq(argv
[i
], "-asl_in"))
354 if ((i
+ 1) < argc
) activate_asl_in
= atoi(argv
[++i
]);
356 else if (streq(argv
[i
], "-asl_action"))
358 if ((i
+ 1) < argc
) activate_asl_action
= atoi(argv
[++i
]);
360 else if (streq(argv
[i
], "-klog_in"))
362 if ((i
+ 1) < argc
) activate_klog_in
= atoi(argv
[++i
]);
364 else if (streq(argv
[i
], "-bsd_in"))
366 if ((i
+ 1) < argc
) activate_bsd_in
= atoi(argv
[++i
]);
368 else if (streq(argv
[i
], "-bsd_out"))
370 if ((i
+ 1) < argc
) activate_bsd_out
= atoi(argv
[++i
]);
372 else if (streq(argv
[i
], "-udp_in"))
374 if ((i
+ 1) < argc
) activate_udp_in
= atoi(argv
[++i
]);
378 TAILQ_INIT(&Moduleq
);
387 if (fork() != 0) exit(0);
396 signal(SIGHUP
, catch_sighup
);
397 signal(SIGWINCH
, catch_sigwinch
);
403 for (mod
= Moduleq
.tqh_first
; mod
!= NULL
; mod
= mod
->entries
.tqe_next
)
406 if (fd
< 0) continue;
409 lastmark
= time(NULL
);
410 notify_time
= lastmark
;
411 memset(&timeout
, 0, sizeof(struct timeval
));
417 timeout
.tv_sec
= ssec
;
422 if (pdays
> 0) ptime
= lastmark
+ PRUNE_AFTER_START_DELAY
;
424 if (kfd
>= 0) FD_SET(kfd
, &kern
);
427 * drain /dev/klog first
432 while (select(max
, &kern
, NULL
, NULL
, &zto
) > 0)
434 aslevent_handleevent(kern
, wr
, ex
, NULL
);
440 if (pto
!= NULL
) pto
->tv_sec
= ssec
;
441 max
= aslevent_fdsets(&rd
, &wr
, &ex
) + 1;
443 status
= select(max
, &rd
, &wr
, &ex
, pto
);
444 if ((kfd
>= 0) && FD_ISSET(kfd
, &rd
))
451 while (select(max
, &kern
, NULL
, NULL
, &zto
) > 0)
453 aslevent_handleevent(kern
, wr
, ex
, NULL
);
466 delta
= tick
- lastmark
;
485 pq
= asl_new(ASL_TYPE_QUERY
);
487 asprintf(&str
, "-%dd", pdays
);
488 asl_set_query(pq
, ASL_KEY_TIME
, str
, ASL_QUERY_OP_LESS
);
489 if (str
!= NULL
) free(str
);
491 /* asl_prune frees the query */
497 if (__notify_78945668_info__
< 0)
500 if (tick
>= notify_time
)
502 if (notify_post("com.apple.system.syslogd") == NOTIFY_STATUS_OK
) __notify_78945668_info__
= 0;
503 else notify_time
= tick
+ NOTIFY_DELAY
;
507 if (status
!= 0) aslevent_handleevent(rd
, wr
, ex
, NULL
);
512 asldebug(const char *str
, ...)
516 if (debug
== 0) return 0;
519 return vprintf(str
, v
);