2 * Copyright (c) 2004-2008 Apple 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@
24 #include <sys/types.h>
26 #include <sys/fcntl.h>
27 #include <sys/socket.h>
37 #define forever for(;;)
39 #define ASL_SOCKET_NAME "AppleSystemLogger"
43 static asl_msg_t
*query
= NULL
;
50 extern void db_enqueue(asl_msg_t
*m
);
52 static int filter_token
= -1;
65 n
= read(fd
, tmp
, 11);
71 aslevent_removefd(fd
);
77 asldebug("%s: read error (len=%d): %s\n", MY_ID
, n
, strerror(errno
));
81 aslevent_removefd(fd
);
90 if (len
== 0) return NULL
;
93 if (out
== NULL
) return NULL
;
95 n
= read(fd
, out
, len
);
100 asldebug("%s: read error (body): %s\n", MY_ID
, strerror(errno
));
104 aslevent_removefd(fd
);
111 asldebug("asl_in_getmsg: %s\n", (out
== NULL
) ? "NULL" : out
);
116 status
= getpeereid(fd
, &uid
, &gid
);
117 m
= asl_msg_from_string(out
);
124 snprintf(tmp
, sizeof(tmp
), "%d", uid
);
125 asl_set(m
, ASL_KEY_UID
, tmp
);
127 snprintf(tmp
, sizeof(tmp
), "%d", gid
);
128 asl_set(m
, ASL_KEY_GID
, tmp
);
135 asl_in_acceptmsg(int fd
)
139 asldebug("%s: accepting message\n", MY_ID
);
140 clientfd
= accept(fd
, NULL
, 0);
143 asldebug("%s: error accepting socket fd %d: %s\n", MY_ID
, fd
, strerror(errno
));
147 if (fcntl(clientfd
, F_SETFL
, O_NONBLOCK
) < 0)
151 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, clientfd
, strerror(errno
));
155 aslevent_addfd(clientfd
, ADDFD_FLAGS_LOCAL
, asl_in_getmsg
, NULL
, NULL
);
160 aslmod_sendmsg(asl_msg_t
*msg
, const char *outid
)
162 const char *vlevel
, *facility
, *sender
, *ignore
;
165 int status
, x
, level
, log_me
;
167 /* set up com.apple.syslog.asl_filter */
168 if (filter_token
== -1)
170 status
= notify_register_check(NOTIFY_SYSTEM_ASL_FILTER
, &filter_token
);
171 if (status
!= NOTIFY_STATUS_OK
)
177 status
= notify_check(filter_token
, &x
);
178 if (status
== NOTIFY_STATUS_OK
)
180 v64
= global
.asl_log_filter
;
181 status
= notify_set_state(filter_token
, v64
);
183 if (status
!= NOTIFY_STATUS_OK
)
185 notify_cancel(filter_token
);
191 if (filter_token
>= 0)
194 status
= notify_check(filter_token
, &x
);
195 if ((status
== NOTIFY_STATUS_OK
) && (x
== 1))
198 status
= notify_get_state(filter_token
, &v64
);
199 if ((status
== NOTIFY_STATUS_OK
) && (v64
!= 0)) global
.asl_log_filter
= v64
;
203 facility
= asl_get(msg
, ASL_KEY_FACILITY
);
204 sender
= asl_get(msg
, ASL_KEY_SENDER
);
207 if ((facility
!= NULL
) && (!strcmp(facility
, "kern"))) log_me
= 1;
208 else if ((sender
!= NULL
) && (!strcmp(sender
, "launchd"))) log_me
= 1;
211 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
213 if (vlevel
!= NULL
) level
= atoi(vlevel
);
214 lmask
= ASL_FILTER_MASK(level
);
215 if ((lmask
& global
.asl_log_filter
) != 0) log_me
= 1;
220 ignore
= asl_get(msg
, ASL_KEY_IGNORE
);
221 if ((ignore
!= NULL
) && (!strcasecmp(ignore
, "yes"))) log_me
= 0;
224 if (log_me
== 1) db_enqueue(msg
);
234 launch_data_t sockets_dict
, fd_array
, fd_dict
;
236 asldebug("%s: init\n", MY_ID
);
237 if (sock
>= 0) return sock
;
238 if (global
.launch_dict
== NULL
)
240 asldebug("%s: laucnchd dict is NULL\n", MY_ID
);
244 sockets_dict
= launch_data_dict_lookup(global
.launch_dict
, LAUNCH_JOBKEY_SOCKETS
);
245 if (sockets_dict
== NULL
)
247 asldebug("%s: laucnchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID
);
251 fd_array
= launch_data_dict_lookup(sockets_dict
, ASL_SOCKET_NAME
);
252 if (fd_array
== NULL
)
254 asldebug("%s: laucnchd lookup of ASL_SOCKET_NAME failed\n", MY_ID
);
258 len
= launch_data_array_get_count(fd_array
);
261 asldebug("%s: laucnchd fd array is empty\n", MY_ID
);
267 asldebug("%s: warning! laucnchd fd array has %d sockets\n", MY_ID
, len
);
270 fd_dict
= launch_data_array_get_index(fd_array
, 0);
273 asldebug("%s: laucnchd file discriptor array element 0 is NULL\n", MY_ID
);
277 sock
= launch_data_get_fd(fd_dict
);
279 rbufsize
= 128 * 1024;
280 len
= sizeof(rbufsize
);
282 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &rbufsize
, len
) < 0)
284 asldebug("%s: couldn't set receive buffer size for %d (%s): %s\n", MY_ID
, sock
, _PATH_ASL_IN
, strerror(errno
));
290 if (fcntl(sock
, F_SETFL
, O_NONBLOCK
) < 0)
292 asldebug("%s: couldn't set O_NONBLOCK for socket %d (%s): %s\n", MY_ID
, sock
, _PATH_ASL_IN
, strerror(errno
));
298 query
= asl_new(ASL_TYPE_QUERY
);
299 aslevent_addmatch(query
, MY_ID
);
300 aslevent_addoutput(aslmod_sendmsg
, MY_ID
);
302 return aslevent_addfd(sock
, ADDFD_FLAGS_LOCAL
, asl_in_acceptmsg
, NULL
, NULL
);
314 if (sock
< 0) return 1;
316 if (filter_token
>= 0) notify_cancel(filter_token
);
318 global
.asl_log_filter
= 0;
322 unlink(_PATH_ASL_IN
);