xref: /OK3568_Linux_fs/external/xserver/hw/xquartz/mach-startup/launchd_fd.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* Copyright (c) 2008-2012 Apple Inc.
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person
4*4882a593Smuzhiyun  * obtaining a copy of this software and associated documentation files
5*4882a593Smuzhiyun  * (the "Software"), to deal in the Software without restriction,
6*4882a593Smuzhiyun  * including without limitation the rights to use, copy, modify, merge,
7*4882a593Smuzhiyun  * publish, distribute, sublicense, and/or sell copies of the Software,
8*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so,
9*4882a593Smuzhiyun  * subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be
12*4882a593Smuzhiyun  * included in all copies or substantial portions of the Software.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17*4882a593Smuzhiyun  * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
18*4882a593Smuzhiyun  * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19*4882a593Smuzhiyun  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*4882a593Smuzhiyun  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Except as contained in this notice, the name(s) of the above
24*4882a593Smuzhiyun  * copyright holders shall not be used in advertising or otherwise to
25*4882a593Smuzhiyun  * promote the sale, use or other dealings in this Software without
26*4882a593Smuzhiyun  * prior written authorization.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
30*4882a593Smuzhiyun #include <dix-config.h>
31*4882a593Smuzhiyun #endif
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <launch.h>
34*4882a593Smuzhiyun #include <asl.h>
35*4882a593Smuzhiyun #include <errno.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include "launchd_fd.h"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun extern aslclient aslc;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun int
launchd_display_fd(void)42*4882a593Smuzhiyun launchd_display_fd(void)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun     launch_data_t sockets_dict, checkin_request, checkin_response;
45*4882a593Smuzhiyun     launch_data_t listening_fd_array, listening_fd;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun     /* Get launchd fd */
48*4882a593Smuzhiyun     if ((checkin_request = launch_data_new_string(LAUNCH_KEY_CHECKIN)) ==
49*4882a593Smuzhiyun         NULL) {
50*4882a593Smuzhiyun         asl_log(
51*4882a593Smuzhiyun             aslc, NULL, ASL_LEVEL_ERR,
52*4882a593Smuzhiyun             "launch_data_new_string(\"" LAUNCH_KEY_CHECKIN
53*4882a593Smuzhiyun             "\") Unable to create string.\n");
54*4882a593Smuzhiyun         return ERROR_FD;
55*4882a593Smuzhiyun     }
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun     if ((checkin_response = launch_msg(checkin_request)) == NULL) {
58*4882a593Smuzhiyun         asl_log(aslc, NULL, ASL_LEVEL_WARNING,
59*4882a593Smuzhiyun                 "launch_msg(\"" LAUNCH_KEY_CHECKIN "\") IPC failure: %s\n",
60*4882a593Smuzhiyun                 strerror(
61*4882a593Smuzhiyun                     errno));
62*4882a593Smuzhiyun         return ERROR_FD;
63*4882a593Smuzhiyun     }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun     if (LAUNCH_DATA_ERRNO == launch_data_get_type(checkin_response)) {
66*4882a593Smuzhiyun         // ignore EACCES, which is common if we weren't started by launchd
67*4882a593Smuzhiyun         if (launch_data_get_errno(checkin_response) != EACCES)
68*4882a593Smuzhiyun             asl_log(aslc, NULL, ASL_LEVEL_ERR,
69*4882a593Smuzhiyun                     "launchd check-in failed: %s\n",
70*4882a593Smuzhiyun                     strerror(launch_data_get_errno(
71*4882a593Smuzhiyun                                  checkin_response)));
72*4882a593Smuzhiyun         return ERROR_FD;
73*4882a593Smuzhiyun     }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun     sockets_dict = launch_data_dict_lookup(checkin_response,
76*4882a593Smuzhiyun                                            LAUNCH_JOBKEY_SOCKETS);
77*4882a593Smuzhiyun     if (NULL == sockets_dict) {
78*4882a593Smuzhiyun         asl_log(aslc, NULL, ASL_LEVEL_ERR,
79*4882a593Smuzhiyun                 "launchd check-in: no sockets found to answer requests on!\n");
80*4882a593Smuzhiyun         return ERROR_FD;
81*4882a593Smuzhiyun     }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun     if (launch_data_dict_get_count(sockets_dict) > 1) {
84*4882a593Smuzhiyun         asl_log(aslc, NULL, ASL_LEVEL_ERR,
85*4882a593Smuzhiyun                 "launchd check-in: some sockets will be ignored!\n");
86*4882a593Smuzhiyun         return ERROR_FD;
87*4882a593Smuzhiyun     }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun     listening_fd_array = launch_data_dict_lookup(sockets_dict,
90*4882a593Smuzhiyun                                                  BUNDLE_ID_PREFIX ":0");
91*4882a593Smuzhiyun     if (NULL == listening_fd_array) {
92*4882a593Smuzhiyun         listening_fd_array = launch_data_dict_lookup(sockets_dict, ":0");
93*4882a593Smuzhiyun         if (NULL == listening_fd_array) {
94*4882a593Smuzhiyun             asl_log(
95*4882a593Smuzhiyun                 aslc, NULL, ASL_LEVEL_ERR,
96*4882a593Smuzhiyun                 "launchd check-in: No known sockets found to answer requests on! \"%s:0\" and \":0\" failed.\n",
97*4882a593Smuzhiyun                 BUNDLE_ID_PREFIX);
98*4882a593Smuzhiyun             return ERROR_FD;
99*4882a593Smuzhiyun         }
100*4882a593Smuzhiyun     }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun     if (launch_data_array_get_count(listening_fd_array) != 1) {
103*4882a593Smuzhiyun         asl_log(aslc, NULL, ASL_LEVEL_ERR,
104*4882a593Smuzhiyun                 "launchd check-in: Expected 1 socket from launchd, got %u)\n",
105*4882a593Smuzhiyun                 (unsigned)launch_data_array_get_count(
106*4882a593Smuzhiyun                     listening_fd_array));
107*4882a593Smuzhiyun         return ERROR_FD;
108*4882a593Smuzhiyun     }
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun     listening_fd = launch_data_array_get_index(listening_fd_array, 0);
111*4882a593Smuzhiyun     return launch_data_get_fd(listening_fd);
112*4882a593Smuzhiyun }
113