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"
40 #define MY_ID "asl_in"
55 n
= read(fd
, tmp
, 11);
60 asl_client_count_decrement();
63 aslevent_removefd(fd
);
69 asldebug("%s: read error (len=%d): %s\n", MY_ID
, n
, strerror(errno
));
72 asl_client_count_decrement();
75 aslevent_removefd(fd
);
84 if (len
== 0) return NULL
;
87 if (out
== NULL
) return NULL
;
89 n
= read(fd
, out
, len
);
94 asldebug("%s: read error (body): %s\n", MY_ID
, strerror(errno
));
97 asl_client_count_decrement();
100 aslevent_removefd(fd
);
107 asldebug("asl_in_getmsg: %s\n", (out
== NULL
) ? "NULL" : out
);
112 status
= getpeereid(fd
, &uid
, &gid
);
113 m
= asl_msg_from_string(out
);
120 snprintf(tmp
, sizeof(tmp
), "%d", uid
);
121 asl_set(m
, ASL_KEY_UID
, tmp
);
123 snprintf(tmp
, sizeof(tmp
), "%d", gid
);
124 asl_set(m
, ASL_KEY_GID
, tmp
);
131 asl_in_new_connection(int fd
)
135 asldebug("%s: accepting connection\n", MY_ID
);
136 clientfd
= accept(fd
, NULL
, 0);
139 asldebug("%s: error connecting socket fd %d: %s\n", MY_ID
, fd
, strerror(errno
));
143 if (fcntl(clientfd
, F_SETFL
, O_NONBLOCK
) < 0)
147 asldebug("%s: couldn't set O_NONBLOCK for fd %d: %s\n", MY_ID
, clientfd
, strerror(errno
));
151 asl_client_count_increment();
153 aslevent_addfd(SOURCE_ASL_SOCKET
, clientfd
, ADDFD_FLAGS_LOCAL
, asl_in_getmsg
, NULL
, NULL
);
162 launch_data_t sockets_dict
, fd_array
, fd_dict
;
164 asldebug("%s: init\n", MY_ID
);
165 if (sock
>= 0) return sock
;
166 if (global
.launch_dict
== NULL
)
168 asldebug("%s: launchd dict is NULL\n", MY_ID
);
172 sockets_dict
= launch_data_dict_lookup(global
.launch_dict
, LAUNCH_JOBKEY_SOCKETS
);
173 if (sockets_dict
== NULL
)
175 asldebug("%s: launchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID
);
179 fd_array
= launch_data_dict_lookup(sockets_dict
, ASL_SOCKET_NAME
);
180 if (fd_array
== NULL
)
182 asldebug("%s: launchd lookup of ASL_SOCKET_NAME failed\n", MY_ID
);
186 len
= launch_data_array_get_count(fd_array
);
189 asldebug("%s: launchd fd array is empty\n", MY_ID
);
195 asldebug("%s: warning! launchd fd array has %d sockets\n", MY_ID
, len
);
198 fd_dict
= launch_data_array_get_index(fd_array
, 0);
201 asldebug("%s: launchd file discriptor array element 0 is NULL\n", MY_ID
);
205 sock
= launch_data_get_fd(fd_dict
);
207 rbufsize
= 128 * 1024;
208 len
= sizeof(rbufsize
);
210 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &rbufsize
, len
) < 0)
212 asldebug("%s: couldn't set receive buffer size for %d (%s): %s\n", MY_ID
, sock
, _PATH_ASL_IN
, strerror(errno
));
218 if (fcntl(sock
, F_SETFL
, O_NONBLOCK
) < 0)
220 asldebug("%s: couldn't set O_NONBLOCK for socket %d (%s): %s\n", MY_ID
, sock
, _PATH_ASL_IN
, strerror(errno
));
226 return aslevent_addfd(SOURCE_ASL_SOCKET
, sock
, ADDFD_FLAGS_LOCAL
, asl_in_new_connection
, NULL
, NULL
);
238 if (sock
< 0) return 1;
241 unlink(_PATH_ASL_IN
);