xref: /NextBSD/lib/libxpc/xpc_internal.h (revision 33da5adc555b3bc29986eeadca03829e4ad06b1e)
1 /*
2  * Copyright 2014-2015 iXsystems, Inc.
3  * All rights reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted providing that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef	_LIBXPC_XPC_INTERNAL_H
29 #define	_LIBXPC_XPC_INTERNAL_H
30 
31 #include <nv.h>
32 
33 #define debugf(...) 				\
34     do { 					\
35     	fprintf(stderr, "%s: ", __func__);	\
36     	fprintf(stderr, __VA_ARGS__);		\
37     	fprintf(stderr, "\n");			\
38     } while(0);
39 
40 #define _XPC_TYPE_INVALID		0
41 #define _XPC_TYPE_DICTIONARY		1
42 #define _XPC_TYPE_ARRAY			2
43 #define _XPC_TYPE_BOOL			3
44 #define _XPC_TYPE_CONNECTION		4
45 #define _XPC_TYPE_ENDPOINT		5
46 #define	_XPC_TYPE_NULL			6
47 #define _XPC_TYPE_INT64			8
48 #define _XPC_TYPE_UINT64		9
49 #define _XPC_TYPE_DATE			10
50 #define _XPC_TYPE_DATA			11
51 #define _XPC_TYPE_STRING		12
52 #define _XPC_TYPE_UUID			13
53 #define _XPC_TYPE_FD			14
54 #define _XPC_TYPE_SHMEM			15
55 #define _XPC_TYPE_ERROR			16
56 #define _XPC_TYPE_DOUBLE		17
57 #define _XPC_TYPE_MAX			_XPC_TYPE_DOUBLE
58 
59 #define	XPC_SEQID	"XPC sequence number"
60 
61 struct xpc_object;
62 struct xpc_dict_pair;
63 
64 TAILQ_HEAD(xpc_dict_head, xpc_dict_pair);
65 TAILQ_HEAD(xpc_array_head, xpc_object);
66 
67 typedef union {
68 	struct xpc_dict_head dict;
69 	struct xpc_array_head array;
70 	uint64_t ui;
71 	int64_t i;
72 	const char *str;
73 	bool b;
74 	double d;
75 	uintptr_t ptr;
76 	int fd;
77 	uuid_t uuid;
78 	mach_port_t port;
79 } xpc_u;
80 
81 
82 #define _XPC_FROM_WIRE 0x1
83 struct xpc_object {
84 	uint8_t			xo_xpc_type;
85 	uint16_t		xo_flags;
86 	volatile uint32_t	xo_refcnt;
87 	size_t			xo_size;
88 	xpc_u			xo_u;
89 	audit_token_t *		xo_audit_token;
90 	TAILQ_ENTRY(xpc_object) xo_link;
91 };
92 
93 struct xpc_dict_pair {
94 	const char *		key;
95 	struct xpc_object *	value;
96 	TAILQ_ENTRY(xpc_dict_pair) xo_link;
97 };
98 
99 struct xpc_pending_call {
100 	uint64_t		xp_id;
101 	xpc_object_t		xp_response;
102 	dispatch_queue_t	xp_queue;
103 	xpc_handler_t		xp_handler;
104 	TAILQ_ENTRY(xpc_pending_call) xp_link;
105 };
106 
107 struct xpc_connection {
108 	const char *		xc_name;
109 	mach_port_t		xc_remote_port;
110 	mach_port_t		xc_local_port;
111 	xpc_handler_t		xc_handler;
112 	dispatch_source_t	xc_recv_source;
113 	dispatch_queue_t	xc_send_queue;
114 	dispatch_queue_t	xc_recv_queue;
115 	dispatch_queue_t	xc_target_queue;
116 	int			xc_suspend_count;
117 	int			xc_transaction_count;
118 	int 			xc_flags;
119 	volatile uint64_t	xc_last_id;
120 	void *			xc_context;
121 	struct xpc_connection * xc_parent;
122 	uid_t			xc_remote_euid;
123 	gid_t			xc_remote_guid;
124 	pid_t			xc_remote_pid;
125 	au_asid_t		xc_remote_asid;
126 	TAILQ_HEAD(, xpc_pending_call) xc_pending;
127 	TAILQ_HEAD(, xpc_connection) xc_peers;
128 	TAILQ_ENTRY(xpc_connection) xc_link;
129 };
130 
131 struct xpc_service {
132 	mach_port_t		xs_remote_port;
133 	TAILQ_HEAD(, xpc_connection) xs_connections;
134 };
135 
136 #define xo_nv xo_u.nv
137 #define xo_str xo_u.str
138 #define xo_bool xo_u.b
139 #define xo_uint xo_u.ui
140 #define xo_int xo_u.i
141 #define xo_ptr xo_u.ptr
142 #define xo_d xo_u.d
143 #define xo_fd xo_u.fd
144 #define xo_uuid xo_u.uuid
145 #define xo_port xo_u.port
146 #define xo_array xo_u.array
147 #define xo_dict xo_u.dict
148 
149 __private_extern__ struct xpc_object *_xpc_prim_create(int type, xpc_u value,
150     size_t size);
151 __private_extern__ struct xpc_object *_xpc_prim_create_flags(int type,
152     xpc_u value, size_t size, uint16_t flags);
153 __private_extern__ const char *_xpc_get_type_name(xpc_object_t obj);
154 __private_extern__ struct xpc_object *nv2xpc(const nvlist_t *nv);
155 __private_extern__ nvlist_t *xpc2nv(struct xpc_object *xo);
156 __private_extern__ void xpc_object_destroy(struct xpc_object *xo);
157 __private_extern__ int xpc_pipe_send(xpc_object_t obj, mach_port_t dst,
158     mach_port_t local, uint64_t id);
159 __private_extern__ int xpc_pipe_receive(mach_port_t local, mach_port_t *remote,
160     xpc_object_t *result, uint64_t *id);
161 
162 void fail_log(const char *) __dead2;
163 
164 #endif	/* _LIBXPC_XPC_INTERNAL_H */