]>
Commit | Line | Data |
---|---|---|
b16a592a | 1 | /* |
db78b1bd | 2 | * Copyright (c) 2004-2011 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; | |
db78b1bd A |
42 | static dispatch_source_t in_src; |
43 | static dispatch_queue_t in_queue; | |
b16a592a | 44 | |
db78b1bd | 45 | void |
b16a592a A |
46 | bsd_in_acceptmsg(int fd) |
47 | { | |
48 | uint32_t len; | |
49 | int n; | |
db78b1bd | 50 | char line[MAXLINE]; |
b16a592a | 51 | struct sockaddr_un sun; |
db78b1bd | 52 | aslmsg m; |
b16a592a A |
53 | |
54 | len = sizeof(struct sockaddr_un); | |
55 | n = recvfrom(fd, line, MAXLINE, 0, (struct sockaddr *)&sun, &len); | |
56 | ||
db78b1bd | 57 | if (n <= 0) return; |
b16a592a A |
58 | |
59 | line[n] = '\0'; | |
5dd30d76 | 60 | |
db78b1bd A |
61 | m = asl_input_parse(line, n, NULL, SOURCE_BSD_SOCKET); |
62 | dispatch_async(global.work_queue, ^{ process_message(m, SOURCE_BSD_SOCKET); }); | |
b16a592a A |
63 | } |
64 | ||
65 | int | |
db78b1bd | 66 | bsd_in_init() |
b16a592a | 67 | { |
b16a592a A |
68 | int rbufsize; |
69 | int len; | |
5dd30d76 | 70 | launch_data_t sockets_dict, fd_array, fd_dict; |
db78b1bd A |
71 | static dispatch_once_t once; |
72 | ||
73 | dispatch_once(&once, ^{ | |
74 | in_queue = dispatch_queue_create(MY_ID, NULL); | |
75 | }); | |
b16a592a A |
76 | |
77 | asldebug("%s: init\n", MY_ID); | |
db78b1bd | 78 | if (sock >= 0) return -1; |
b16a592a | 79 | |
57b0aad2 | 80 | if (global.launch_dict == NULL) |
b16a592a | 81 | { |
c4fdb7d1 | 82 | asldebug("%s: launchd dict is NULL\n", MY_ID); |
b16a592a A |
83 | return -1; |
84 | } | |
85 | ||
57b0aad2 | 86 | sockets_dict = launch_data_dict_lookup(global.launch_dict, LAUNCH_JOBKEY_SOCKETS); |
5dd30d76 A |
87 | if (sockets_dict == NULL) |
88 | { | |
c4fdb7d1 | 89 | asldebug("%s: launchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID); |
5dd30d76 A |
90 | return -1; |
91 | } | |
b16a592a | 92 | |
5dd30d76 A |
93 | fd_array = launch_data_dict_lookup(sockets_dict, BSD_SOCKET_NAME); |
94 | if (fd_array == NULL) | |
95 | { | |
c4fdb7d1 | 96 | asldebug("%s: launchd lookup of BSD_SOCKET_NAME failed\n", MY_ID); |
5dd30d76 A |
97 | return -1; |
98 | } | |
b16a592a | 99 | |
5dd30d76 A |
100 | len = launch_data_array_get_count(fd_array); |
101 | if (len <= 0) | |
b16a592a | 102 | { |
c4fdb7d1 | 103 | asldebug("%s: launchd fd array is empty\n", MY_ID); |
b16a592a A |
104 | return -1; |
105 | } | |
106 | ||
5dd30d76 A |
107 | if (len > 1) |
108 | { | |
c4fdb7d1 | 109 | asldebug("%s: warning! launchd fd array has %d sockets\n", MY_ID, len); |
5dd30d76 A |
110 | } |
111 | ||
112 | fd_dict = launch_data_array_get_index(fd_array, 0); | |
113 | if (fd_dict == NULL) | |
114 | { | |
c4fdb7d1 | 115 | asldebug("%s: launchd file discriptor array element 0 is NULL\n", MY_ID); |
5dd30d76 A |
116 | return -1; |
117 | } | |
118 | ||
119 | sock = launch_data_get_fd(fd_dict); | |
120 | ||
b16a592a A |
121 | rbufsize = 128 * 1024; |
122 | len = sizeof(rbufsize); | |
5dd30d76 | 123 | |
b16a592a A |
124 | if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rbufsize, len) < 0) |
125 | { | |
126 | asldebug("%s: couldn't set receive buffer size for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno)); | |
127 | close(sock); | |
128 | sock = -1; | |
129 | return -1; | |
130 | } | |
131 | ||
132 | if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) | |
133 | { | |
134 | asldebug("%s: couldn't set O_NONBLOCK for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno)); | |
135 | close(sock); | |
136 | sock = -1; | |
137 | return -1; | |
138 | } | |
139 | ||
db78b1bd A |
140 | in_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)sock, 0, in_queue); |
141 | dispatch_source_set_event_handler(in_src, ^{ bsd_in_acceptmsg(sock); }); | |
142 | ||
143 | dispatch_resume(in_src); | |
144 | return 0; | |
b16a592a A |
145 | } |
146 | ||
147 | int | |
a83ff38a | 148 | bsd_in_close(void) |
b16a592a | 149 | { |
a83ff38a A |
150 | if (sock < 0) return 1; |
151 | ||
db78b1bd A |
152 | dispatch_source_cancel(in_src); |
153 | dispatch_release(in_src); | |
154 | in_src = NULL; | |
155 | ||
a83ff38a A |
156 | close(sock); |
157 | sock = -1; | |
158 | ||
b16a592a A |
159 | return 0; |
160 | } | |
161 | ||
162 | int | |
a83ff38a | 163 | bsd_in_reset(void) |
b16a592a | 164 | { |
b16a592a A |
165 | return 0; |
166 | } |