xref: /netbsd-src/external/bsd/tmux/dist/compat/daemon-darwin.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*
2  * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /*
18  * Copyright (c) 2011-2013, Chris Johnsen <chris_johnsen@pobox.com>
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  *
25  *     1. Redistributions of source code must retain the above
26  *        copyright notice, this list of conditions and the following
27  *        disclaimer.
28  *
29  *     2. Redistributions in binary form must reproduce the above
30  *        copyright notice, this list of conditions and the following
31  *        disclaimer in the documentation and/or other materials
32  *        provided with the distribution.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
38  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
40  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
44  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  * POSSIBILITY OF SUCH DAMAGE.
46 */
47 
48 #include <sys/types.h>
49 
50 #include <mach/mach.h>
51 
52 #include <Availability.h>
53 #include <unistd.h>
54 
55 void daemon_darwin(void);
56 
57 #ifdef __MAC_10_10
58 
59 extern kern_return_t	bootstrap_look_up_per_user(mach_port_t, const char *,
60 			    uid_t, mach_port_t *);
61 extern kern_return_t	bootstrap_get_root(mach_port_t, mach_port_t *);
62 
63 void
64 daemon_darwin(void)
65 {
66 	mach_port_t	root = MACH_PORT_NULL;
67 	mach_port_t	s = MACH_PORT_NULL;
68 	uid_t		uid;
69 
70 	uid = getuid();
71 	if (bootstrap_get_root(bootstrap_port, &root) == KERN_SUCCESS &&
72 	    bootstrap_look_up_per_user(root, NULL, uid, &s) == KERN_SUCCESS &&
73 	    task_set_bootstrap_port(mach_task_self(), s) == KERN_SUCCESS &&
74 	    mach_port_deallocate(mach_task_self(), bootstrap_port) == KERN_SUCCESS)
75 		bootstrap_port = s;
76 }
77 
78 #else
79 
80 void
81 daemon_darwin(void)
82 {
83 }
84 
85 #endif
86