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@
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
;
45 extern int asl_log_filter
;
54 extern void db_enqueue(asl_msg_t
*m
);
56 static int filter_token
= -1;
69 n
= read(fd
, tmp
, 11);
75 aslevent_removefd(fd
);
81 asldebug("%s: read error (len=%d): %s\n", MY_ID
, n
, strerror(errno
));
85 aslevent_removefd(fd
);
94 if (len
== 0) return NULL
;
97 if (out
== NULL
) return NULL
;
99 n
= read(fd
, out
, len
);
104 asldebug("%s: read error (body): %s\n", MY_ID
, strerror(errno
));
108 aslevent_removefd(fd
);
115 asldebug("asl_in_getmsg: %s\n", (out
== NULL
) ? "NULL" : out
);
120 status
= getpeereid(fd
, &uid
, &gid
);
121 m
= asl_msg_from_string(out
);
128 snprintf(tmp
, sizeof(tmp
), "%d", uid
);
129 asl_set(m
, ASL_KEY_UID
, tmp
);
131 snprintf(tmp
, sizeof(tmp
), "%d", gid
);
132 asl_set(m
, ASL_KEY_GID
, tmp
);
139 asl_in_acceptmsg(int fd
)
143 asldebug("%s: accepting message\n", MY_ID
);
144 clientfd
= accept(fd
, NULL
, 0);
147 asldebug("%s: error accepting socket fd %d: %s\n", MY_ID
, fd
, strerror(errno
));
151 if (fcntl(clientfd
, F_SETFL
, O_NONBLOCK
) < 0)
155 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, clientfd
, strerror(errno
));
159 aslevent_addfd(clientfd
, ADDFD_FLAGS_LOCAL
, asl_in_getmsg
, NULL
, NULL
);
164 aslmod_sendmsg(asl_msg_t
*msg
, const char *outid
)
166 const char *vlevel
, *facility
, *ignore
;
169 int status
, x
, level
, log_me
;
171 /* set up com.apple.syslog.asl_filter */
172 if (filter_token
== -1)
174 status
= notify_register_check(NOTIFY_SYSTEM_ASL_FILTER
, &filter_token
);
175 if (status
!= NOTIFY_STATUS_OK
)
181 status
= notify_check(filter_token
, &x
);
182 if (status
== NOTIFY_STATUS_OK
)
184 v64
= asl_log_filter
;
185 status
= notify_set_state(filter_token
, v64
);
187 if (status
!= NOTIFY_STATUS_OK
)
189 notify_cancel(filter_token
);
195 if (filter_token
>= 0)
198 status
= notify_check(filter_token
, &x
);
199 if ((status
== NOTIFY_STATUS_OK
) && (x
== 1))
202 status
= notify_get_state(filter_token
, &v64
);
203 if ((status
== NOTIFY_STATUS_OK
) && (v64
!= 0)) asl_log_filter
= v64
;
208 facility
= asl_get(msg
, ASL_KEY_FACILITY
);
209 if ((facility
!= NULL
) && (!strcmp(facility
, "kern"))) log_me
= 1;
212 vlevel
= asl_get(msg
, ASL_KEY_LEVEL
);
214 if (vlevel
!= NULL
) level
= atoi(vlevel
);
215 lmask
= ASL_FILTER_MASK(level
);
216 if ((lmask
& asl_log_filter
) != 0) log_me
= 1;
221 ignore
= asl_get(msg
, ASL_KEY_IGNORE
);
222 if ((ignore
!= NULL
) && (!strcasecmp(ignore
, "yes"))) log_me
= 0;
225 if (log_me
== 1) db_enqueue(msg
);
235 launch_data_t sockets_dict
, fd_array
, fd_dict
;
237 asldebug("%s: init\n", MY_ID
);
238 if (sock
>= 0) return sock
;
239 if (launch_dict
== NULL
)
241 asldebug("%s: laucnchd dict is NULL\n", MY_ID
);
245 sockets_dict
= launch_data_dict_lookup(launch_dict
, LAUNCH_JOBKEY_SOCKETS
);
246 if (sockets_dict
== NULL
)
248 asldebug("%s: laucnchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID
);
252 fd_array
= launch_data_dict_lookup(sockets_dict
, ASL_SOCKET_NAME
);
253 if (fd_array
== NULL
)
255 asldebug("%s: laucnchd lookup of ASL_SOCKET_NAME failed\n", MY_ID
);
259 len
= launch_data_array_get_count(fd_array
);
262 asldebug("%s: laucnchd fd array is empty\n", MY_ID
);
268 asldebug("%s: warning! laucnchd fd array has %d sockets\n", MY_ID
, len
);
271 fd_dict
= launch_data_array_get_index(fd_array
, 0);
274 asldebug("%s: laucnchd file discriptor array element 0 is NULL\n", MY_ID
);
278 sock
= launch_data_get_fd(fd_dict
);
280 rbufsize
= 128 * 1024;
281 len
= sizeof(rbufsize
);
283 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &rbufsize
, len
) < 0)
285 asldebug("%s: couldn't set receive buffer size for %s: %s\n", MY_ID
, sock
, _PATH_ASL_IN
, strerror(errno
));
291 if (fcntl(sock
, F_SETFL
, O_NONBLOCK
) < 0)
293 asldebug("%s: couldn't set O_NONBLOCK for socket %d (%s): %s\n", MY_ID
, sock
, _PATH_ASL_IN
, strerror(errno
));
299 query
= asl_new(ASL_TYPE_QUERY
);
300 aslevent_addmatch(query
, MY_ID
);
301 aslevent_addoutput(aslmod_sendmsg
, MY_ID
);
303 return aslevent_addfd(sock
, ADDFD_FLAGS_LOCAL
, asl_in_acceptmsg
, NULL
, NULL
);
315 if (sock
< 0) return 1;
317 if (filter_token
>= 0) notify_cancel(filter_token
);
323 unlink(_PATH_ASL_IN
);