]>
Commit | Line | Data |
---|---|---|
b16a592a | 1 | /* |
57b0aad2 | 2 | * Copyright (c) 2004-2008 Apple Inc. All rights reserved. |
b16a592a A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
5dd30d76 A |
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 | |
11 | * file. | |
b16a592a A |
12 | * |
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, | |
5dd30d76 A |
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. | |
b16a592a A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/socket.h> | |
27 | #include <sys/fcntl.h> | |
28 | #include <sys/un.h> | |
29 | #include <sys/uio.h> | |
30 | #include <stdio.h> | |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
33 | #include <unistd.h> | |
34 | #include <errno.h> | |
35 | #include "daemon.h" | |
36 | ||
5dd30d76 | 37 | #define BSD_SOCKET_NAME "BSDSystemLogger" |
b16a592a | 38 | #define MY_ID "bsd_in" |
5dd30d76 | 39 | #define MAXLINE 4096 |
b16a592a A |
40 | |
41 | static int sock = -1; | |
42 | ||
43 | asl_msg_t * | |
44 | bsd_in_acceptmsg(int fd) | |
45 | { | |
46 | uint32_t len; | |
47 | int n; | |
48 | char line[MAXLINE + 1]; | |
49 | struct sockaddr_un sun; | |
50 | ||
51 | len = sizeof(struct sockaddr_un); | |
52 | n = recvfrom(fd, line, MAXLINE, 0, (struct sockaddr *)&sun, &len); | |
53 | ||
54 | if (n <= 0) return NULL; | |
55 | ||
56 | line[n] = '\0'; | |
5dd30d76 A |
57 | |
58 | return asl_input_parse(line, n, NULL, 0); | |
b16a592a A |
59 | } |
60 | ||
61 | int | |
62 | bsd_in_init(void) | |
63 | { | |
b16a592a A |
64 | int rbufsize; |
65 | int len; | |
5dd30d76 | 66 | launch_data_t sockets_dict, fd_array, fd_dict; |
b16a592a A |
67 | |
68 | asldebug("%s: init\n", MY_ID); | |
69 | if (sock >= 0) return sock; | |
70 | ||
57b0aad2 | 71 | if (global.launch_dict == NULL) |
b16a592a | 72 | { |
5dd30d76 | 73 | asldebug("%s: laucnchd dict is NULL\n", MY_ID); |
b16a592a A |
74 | return -1; |
75 | } | |
76 | ||
57b0aad2 | 77 | sockets_dict = launch_data_dict_lookup(global.launch_dict, LAUNCH_JOBKEY_SOCKETS); |
5dd30d76 A |
78 | if (sockets_dict == NULL) |
79 | { | |
80 | asldebug("%s: laucnchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID); | |
81 | return -1; | |
82 | } | |
b16a592a | 83 | |
5dd30d76 A |
84 | fd_array = launch_data_dict_lookup(sockets_dict, BSD_SOCKET_NAME); |
85 | if (fd_array == NULL) | |
86 | { | |
87 | asldebug("%s: laucnchd lookup of BSD_SOCKET_NAME failed\n", MY_ID); | |
88 | return -1; | |
89 | } | |
b16a592a | 90 | |
5dd30d76 A |
91 | len = launch_data_array_get_count(fd_array); |
92 | if (len <= 0) | |
b16a592a | 93 | { |
5dd30d76 | 94 | asldebug("%s: laucnchd fd array is empty\n", MY_ID); |
b16a592a A |
95 | return -1; |
96 | } | |
97 | ||
5dd30d76 A |
98 | if (len > 1) |
99 | { | |
100 | asldebug("%s: warning! laucnchd fd array has %d sockets\n", MY_ID, len); | |
101 | } | |
102 | ||
103 | fd_dict = launch_data_array_get_index(fd_array, 0); | |
104 | if (fd_dict == NULL) | |
105 | { | |
106 | asldebug("%s: laucnchd file discriptor array element 0 is NULL\n", MY_ID); | |
107 | return -1; | |
108 | } | |
109 | ||
110 | sock = launch_data_get_fd(fd_dict); | |
111 | ||
b16a592a A |
112 | rbufsize = 128 * 1024; |
113 | len = sizeof(rbufsize); | |
5dd30d76 | 114 | |
b16a592a A |
115 | if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rbufsize, len) < 0) |
116 | { | |
117 | asldebug("%s: couldn't set receive buffer size for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno)); | |
118 | close(sock); | |
119 | sock = -1; | |
120 | return -1; | |
121 | } | |
122 | ||
123 | if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) | |
124 | { | |
125 | asldebug("%s: couldn't set O_NONBLOCK for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno)); | |
126 | close(sock); | |
127 | sock = -1; | |
128 | return -1; | |
129 | } | |
130 | ||
5dd30d76 | 131 | return aslevent_addfd(sock, ADDFD_FLAGS_LOCAL, bsd_in_acceptmsg, NULL, NULL); |
b16a592a A |
132 | } |
133 | ||
134 | int | |
135 | bsd_in_reset(void) | |
136 | { | |
137 | return 0; | |
138 | } | |
139 | ||
140 | int | |
141 | bsd_in_close(void) | |
142 | { | |
143 | if (sock < 0) return 1; | |
144 | ||
145 | close(sock); | |
146 | unlink(_PATH_SYSLOG_IN); | |
147 | return 0; | |
148 | } |