]>
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 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/fcntl.h>
32 #include <sys/queue.h>
40 #define DEFAULT_MARK_SEC 0
41 #define DEFAULT_PRUNE_DAYS 7
42 #define PRUNE_AFTER_START_DELAY 300
44 #define NOTIFY_DELAY 1
46 #define streq(A,B) (strcmp(A,B)==0)
47 #define forever for(;;)
52 static TAILQ_HEAD(ml
, module_list
) Moduleq
;
55 int asl_log_filter
= ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE
);
58 extern int __notify_78945668_info__
;
64 static int activate_asl_in
= 1;
66 int asl_action_init();
67 int asl_action_reset();
68 int asl_action_close();
69 static int activate_asl_action
= 1;
74 static int activate_klog_in
= 1;
79 static int activate_bsd_in
= 1;
84 static int activate_bsd_out
= 1;
89 static int activate_udp_in
= 0;
92 * Module approach: only one type of module. This module may implement
93 * the set of functions necessary for any of the functions (input, output,
94 * etc.) Prior to using the modules, dlsym() is consulted to see what it
101 struct module_list
*tmp
;
103 if (activate_asl_in
!= 0)
105 tmp
= calloc(1, sizeof(struct module_list
));
106 if (tmp
== NULL
) return 1;
107 tmp
->name
= strdup("asl_in");
109 tmp
->init
= asl_in_init
;
110 tmp
->reset
= asl_in_reset
;
111 tmp
->close
= asl_in_close
;
112 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
115 if (activate_asl_action
!= 0)
117 tmp
= calloc(1, sizeof(struct module_list
));
118 if (tmp
== NULL
) return 1;
119 tmp
->name
= strdup("asl_action");
121 tmp
->init
= asl_action_init
;
122 tmp
->reset
= asl_action_reset
;
123 tmp
->close
= asl_action_close
;
124 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
127 if (activate_klog_in
!= 0)
129 tmp
= calloc(1, sizeof(struct module_list
));
130 if (tmp
== NULL
) return 1;
131 tmp
->name
= strdup("klog_in");
133 tmp
->init
= klog_in_init
;
134 tmp
->reset
= klog_in_reset
;
135 tmp
->close
= klog_in_close
;
136 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
139 if (activate_bsd_in
!= 0)
141 tmp
= calloc(1, sizeof(struct module_list
));
142 if (tmp
== NULL
) return 1;
143 tmp
->name
= strdup("bsd_in");
145 tmp
->init
= bsd_in_init
;
146 tmp
->reset
= bsd_in_reset
;
147 tmp
->close
= bsd_in_close
;
148 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
151 if (activate_bsd_out
!= 0)
153 tmp
= calloc(1, sizeof(struct module_list
));
154 if (tmp
== NULL
) return 1;
155 tmp
->name
= strdup("bsd_out");
157 tmp
->init
= bsd_out_init
;
158 tmp
->reset
= bsd_out_reset
;
159 tmp
->close
= bsd_out_close
;
160 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
163 if (activate_udp_in
!= 0)
165 tmp
= calloc(1, sizeof(struct module_list
));
166 if (tmp
== NULL
) return 1;
167 tmp
->name
= strdup("udp_in");
169 tmp
->init
= udp_in_init
;
170 tmp
->reset
= udp_in_reset
;
171 tmp
->close
= udp_in_close
;
172 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
179 * Loads all the modules. This DOES NOT call the modules initializer
180 * functions. It simply scans the modules directory looking for modules
181 * and loads them. This does not mean the module will be used.
184 load_modules(char *mp
)
188 struct module_list
*tmp
;
190 char *modulepath
= NULL
;
193 if (d
== NULL
) return -1;
195 while (NULL
!= (de
= readdir(d
)))
197 if (de
->d_name
[0] == '.') continue;
199 /* Must have ".so" in the name" */
200 if (!strstr(de
->d_name
, ".so")) continue;
202 asprintf(&modulepath
, "%s/%s", mp
, de
->d_name
);
203 if (!modulepath
) continue;
205 c
= dlopen(modulepath
, RTLD_LOCAL
);
212 tmp
= calloc(1, sizeof(struct module_list
));
220 bn
= basename(modulepath
);
221 tmp
->name
= strdup(bn
);
223 TAILQ_INSERT_TAIL(&Moduleq
, tmp
, entries
);
225 tmp
->init
= dlsym(tmp
->module, "aslmod_init");
226 tmp
->reset
= dlsym(tmp
->module, "aslmod_reset");
227 tmp
->close
= dlsym(tmp
->module, "aslmod_close");
242 fp
= fopen(_PATH_PIDFILE
, "w");
245 fprintf(fp
, "%d\n", getpid());
255 for (i
= getdtablesize() - 1; i
>= 0; i
--) close(i
);
257 open("/dev/null", O_RDWR
, 0);
265 signal(SIGINT
, SIG_IGN
);
266 signal(SIGPIPE
, SIG_IGN
);
277 catch_sigwinch(int x
)
285 struct module_list
*mod
;
287 for (mod
= Moduleq
.tqh_first
; mod
!= NULL
; mod
= mod
->entries
.tqe_next
)
289 if (mod
->reset
!= NULL
) mod
->reset();
294 main(int argc
, char *argv
[])
296 struct module_list
*mod
;
298 int fd
, i
, max
, status
, pdays
, daemonize
;
299 time_t lastmark
, msec
, ssec
, tick
, delta
, ptime
, notify_time
;
301 struct timeval timeout
, *pto
;
304 mp
= _PATH_MODULE_LIB
;
305 msec
= DEFAULT_MARK_SEC
;
306 pdays
= DEFAULT_PRUNE_DAYS
;
308 __notify_78945668_info__
= -1;
310 for (i
= 1; i
< argc
; i
++)
312 if (streq(argv
[i
], "-d"))
316 if (streq(argv
[i
], "-D"))
320 if (streq(argv
[i
], "-u"))
324 else if (streq(argv
[i
], "-m"))
326 if ((i
+ 1) < argc
) msec
= 60 * atoi(argv
[++i
]);
328 else if (streq(argv
[i
], "-p"))
330 if ((i
+ 1) < argc
) pdays
= atoi(argv
[++i
]);
332 else if (streq(argv
[i
], "-l"))
334 if ((i
+ 1) < argc
) mp
= argv
[++i
];
336 else if (streq(argv
[i
], "-c"))
341 if ((argv
[i
][0] >= '0') && (argv
[i
][0] <= '7') && (argv
[i
][1] == '\0')) asl_log_filter
= ASL_FILTER_MASK_UPTO(atoi(argv
[i
]));
344 else if (streq(argv
[i
], "-asl_in"))
346 if ((i
+ 1) < argc
) activate_asl_in
= atoi(argv
[++i
]);
348 else if (streq(argv
[i
], "-asl_action"))
350 if ((i
+ 1) < argc
) activate_asl_action
= atoi(argv
[++i
]);
352 else if (streq(argv
[i
], "-klog_in"))
354 if ((i
+ 1) < argc
) activate_klog_in
= atoi(argv
[++i
]);
356 else if (streq(argv
[i
], "-bsd_in"))
358 if ((i
+ 1) < argc
) activate_bsd_in
= atoi(argv
[++i
]);
360 else if (streq(argv
[i
], "-bsd_out"))
362 if ((i
+ 1) < argc
) activate_bsd_out
= atoi(argv
[++i
]);
364 else if (streq(argv
[i
], "-udp_in"))
366 if ((i
+ 1) < argc
) activate_udp_in
= atoi(argv
[++i
]);
370 TAILQ_INIT(&Moduleq
);
379 if (fork() != 0) exit(0);
388 signal(SIGHUP
, catch_sighup
);
389 signal(SIGWINCH
, catch_sigwinch
);
395 for (mod
= Moduleq
.tqh_first
; mod
!= NULL
; mod
= mod
->entries
.tqe_next
)
398 if (fd
< 0) continue;
401 lastmark
= time(NULL
);
402 notify_time
= lastmark
;
403 memset(&timeout
, 0, sizeof(struct timeval
));
409 timeout
.tv_sec
= ssec
;
414 if (pdays
> 0) ptime
= lastmark
+ PRUNE_AFTER_START_DELAY
;
418 if (pto
!= NULL
) pto
->tv_sec
= ssec
;
419 max
= aslevent_fdsets(&rd
, &wr
, &ex
);
421 status
= select(max
+1, &rd
, &wr
, &ex
, pto
);
432 delta
= tick
- lastmark
;
451 pq
= asl_new(ASL_TYPE_QUERY
);
453 asprintf(&str
, "-%dd", pdays
);
454 asl_set_query(pq
, ASL_KEY_TIME
, str
, ASL_QUERY_OP_LESS
);
455 if (str
!= NULL
) free(str
);
457 /* asl_prune frees the query */
463 if (__notify_78945668_info__
< 0)
466 if (tick
>= notify_time
)
468 if (notify_post("com.apple.system.syslogd") == NOTIFY_STATUS_OK
) __notify_78945668_info__
= 0;
469 else notify_time
= tick
+ NOTIFY_DELAY
;
473 if (status
!= 0) aslevent_handleevent(rd
, wr
, ex
, NULL
);
478 asldebug(const char *str
, ...)
482 if (debug
== 0) return 0;
485 return vprintf(str
, v
);