xref: /freebsd/crypto/krb5/src/ccapi/server/ccs_callback.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* ccapi/server/ccs_callback.c */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright 2006 Massachusetts Institute of Technology.
4*7f2fe78bSCy Schubert  * All Rights Reserved.
5*7f2fe78bSCy Schubert  *
6*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
7*7f2fe78bSCy Schubert  * require a specific license from the United States Government.
8*7f2fe78bSCy Schubert  * It is the responsibility of any person or organization contemplating
9*7f2fe78bSCy Schubert  * export to obtain such a license before exporting.
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
13*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
14*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
15*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
16*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
17*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
18*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
19*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
20*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
21*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
22*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
23*7f2fe78bSCy Schubert  * or implied warranty.
24*7f2fe78bSCy Schubert  */
25*7f2fe78bSCy Schubert 
26*7f2fe78bSCy Schubert #include "ccs_common.h"
27*7f2fe78bSCy Schubert 
28*7f2fe78bSCy Schubert struct ccs_callback_d {
29*7f2fe78bSCy Schubert     cc_int32 pending;
30*7f2fe78bSCy Schubert     cc_int32 invalid_object_err;
31*7f2fe78bSCy Schubert     ccs_pipe_t client_pipe;
32*7f2fe78bSCy Schubert     ccs_pipe_t reply_pipe;
33*7f2fe78bSCy Schubert     ccs_callback_owner_t owner; /* pointer to owner */
34*7f2fe78bSCy Schubert     ccs_callback_owner_invalidate_t owner_invalidate;
35*7f2fe78bSCy Schubert };
36*7f2fe78bSCy Schubert 
37*7f2fe78bSCy Schubert struct ccs_callback_d ccs_callback_initializer = { 1, 1, CCS_PIPE_NULL, CCS_PIPE_NULL, NULL, NULL };
38*7f2fe78bSCy Schubert 
39*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
40*7f2fe78bSCy Schubert 
ccs_callback_new(ccs_callback_t * out_callback,cc_int32 in_invalid_object_err,ccs_pipe_t in_client_pipe,ccs_pipe_t in_reply_pipe,ccs_callback_owner_t in_owner,ccs_callback_owner_invalidate_t in_owner_invalidate_function)41*7f2fe78bSCy Schubert cc_int32 ccs_callback_new (ccs_callback_t                  *out_callback,
42*7f2fe78bSCy Schubert 			   cc_int32                         in_invalid_object_err,
43*7f2fe78bSCy Schubert 			   ccs_pipe_t                       in_client_pipe,
44*7f2fe78bSCy Schubert 			   ccs_pipe_t                       in_reply_pipe,
45*7f2fe78bSCy Schubert 			   ccs_callback_owner_t             in_owner,
46*7f2fe78bSCy Schubert 			   ccs_callback_owner_invalidate_t  in_owner_invalidate_function)
47*7f2fe78bSCy Schubert {
48*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
49*7f2fe78bSCy Schubert     ccs_callback_t callback = NULL;
50*7f2fe78bSCy Schubert     ccs_client_t client = NULL;
51*7f2fe78bSCy Schubert 
52*7f2fe78bSCy Schubert     if (!out_callback                   ) { err = cci_check_error (ccErrBadParam); }
53*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_client_pipe)) { err = cci_check_error (ccErrBadParam); }
54*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_reply_pipe) ) { err = cci_check_error (ccErrBadParam); }
55*7f2fe78bSCy Schubert     if (!in_owner                       ) { err = cci_check_error (ccErrBadParam); }
56*7f2fe78bSCy Schubert     if (!in_owner_invalidate_function   ) { err = cci_check_error (ccErrBadParam); }
57*7f2fe78bSCy Schubert 
58*7f2fe78bSCy Schubert     if (!err) {
59*7f2fe78bSCy Schubert         callback = malloc (sizeof (*callback));
60*7f2fe78bSCy Schubert         if (callback) {
61*7f2fe78bSCy Schubert             *callback = ccs_callback_initializer;
62*7f2fe78bSCy Schubert         } else {
63*7f2fe78bSCy Schubert             err = cci_check_error (ccErrNoMem);
64*7f2fe78bSCy Schubert         }
65*7f2fe78bSCy Schubert     }
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert     if (!err) {
68*7f2fe78bSCy Schubert         err = ccs_server_client_for_pipe (in_client_pipe, &client);
69*7f2fe78bSCy Schubert     }
70*7f2fe78bSCy Schubert 
71*7f2fe78bSCy Schubert     if (!err) {
72*7f2fe78bSCy Schubert 	err = ccs_pipe_copy (&callback->client_pipe, in_client_pipe);
73*7f2fe78bSCy Schubert     }
74*7f2fe78bSCy Schubert 
75*7f2fe78bSCy Schubert     if (!err) {
76*7f2fe78bSCy Schubert 	err = ccs_pipe_copy (&callback->reply_pipe, in_reply_pipe);
77*7f2fe78bSCy Schubert     }
78*7f2fe78bSCy Schubert 
79*7f2fe78bSCy Schubert     if (!err) {
80*7f2fe78bSCy Schubert         callback->client_pipe = in_client_pipe;
81*7f2fe78bSCy Schubert         callback->reply_pipe = in_reply_pipe;
82*7f2fe78bSCy Schubert         callback->invalid_object_err = in_invalid_object_err;
83*7f2fe78bSCy Schubert         callback->owner = in_owner;
84*7f2fe78bSCy Schubert         callback->owner_invalidate = in_owner_invalidate_function;
85*7f2fe78bSCy Schubert 
86*7f2fe78bSCy Schubert         err = ccs_client_add_callback (client, callback);
87*7f2fe78bSCy Schubert     }
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert     if (!err) {
90*7f2fe78bSCy Schubert         *out_callback = callback;
91*7f2fe78bSCy Schubert         callback = NULL;
92*7f2fe78bSCy Schubert     }
93*7f2fe78bSCy Schubert 
94*7f2fe78bSCy Schubert     ccs_callback_release (callback);
95*7f2fe78bSCy Schubert 
96*7f2fe78bSCy Schubert     return cci_check_error (err);
97*7f2fe78bSCy Schubert }
98*7f2fe78bSCy Schubert 
99*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
100*7f2fe78bSCy Schubert 
ccs_callback_release(ccs_callback_t io_callback)101*7f2fe78bSCy Schubert cc_int32 ccs_callback_release (ccs_callback_t io_callback)
102*7f2fe78bSCy Schubert {
103*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert     if (!err && io_callback) {
106*7f2fe78bSCy Schubert 	ccs_client_t client = NULL;
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert 	if (io_callback->pending) {
109*7f2fe78bSCy Schubert 	    err = ccs_server_send_reply (io_callback->reply_pipe,
110*7f2fe78bSCy Schubert 					 io_callback->invalid_object_err, NULL);
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert 	    io_callback->pending = 0;
113*7f2fe78bSCy Schubert 	}
114*7f2fe78bSCy Schubert 
115*7f2fe78bSCy Schubert 	if (!err) {
116*7f2fe78bSCy Schubert 	    err = ccs_server_client_for_pipe (io_callback->client_pipe, &client);
117*7f2fe78bSCy Schubert 	}
118*7f2fe78bSCy Schubert 
119*7f2fe78bSCy Schubert 	if (!err && client) {
120*7f2fe78bSCy Schubert 	    /* if client object still has a reference to us, remove it */
121*7f2fe78bSCy Schubert 	    err = ccs_client_remove_callback (client, io_callback);
122*7f2fe78bSCy Schubert 	}
123*7f2fe78bSCy Schubert 
124*7f2fe78bSCy Schubert 	if (!err) {
125*7f2fe78bSCy Schubert 	    ccs_pipe_release (io_callback->client_pipe);
126*7f2fe78bSCy Schubert 	    ccs_pipe_release (io_callback->reply_pipe);
127*7f2fe78bSCy Schubert 	    free (io_callback);
128*7f2fe78bSCy Schubert 	}
129*7f2fe78bSCy Schubert     }
130*7f2fe78bSCy Schubert 
131*7f2fe78bSCy Schubert     return cci_check_error (err);
132*7f2fe78bSCy Schubert }
133*7f2fe78bSCy Schubert 
134*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
135*7f2fe78bSCy Schubert 
ccs_callback_invalidate(ccs_callback_t io_callback)136*7f2fe78bSCy Schubert cc_int32 ccs_callback_invalidate (ccs_callback_t io_callback)
137*7f2fe78bSCy Schubert {
138*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
139*7f2fe78bSCy Schubert 
140*7f2fe78bSCy Schubert     if (!io_callback) { err = cci_check_error (ccErrBadParam); }
141*7f2fe78bSCy Schubert 
142*7f2fe78bSCy Schubert     if (!err) {
143*7f2fe78bSCy Schubert         io_callback->pending = 0; /* client is dead, don't try to talk to it */
144*7f2fe78bSCy Schubert 	if (io_callback->owner_invalidate) {
145*7f2fe78bSCy Schubert 	    err = io_callback->owner_invalidate (io_callback->owner, io_callback);
146*7f2fe78bSCy Schubert 	} else {
147*7f2fe78bSCy Schubert 	    cci_debug_printf ("WARNING %s() unable to notify callback owner!",
148*7f2fe78bSCy Schubert 			      __FUNCTION__);
149*7f2fe78bSCy Schubert 	}
150*7f2fe78bSCy Schubert     }
151*7f2fe78bSCy Schubert 
152*7f2fe78bSCy Schubert     return cci_check_error (err);
153*7f2fe78bSCy Schubert }
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
156*7f2fe78bSCy Schubert 
ccs_callback_reply_to_client(ccs_callback_t io_callback,k5_ipc_stream in_stream)157*7f2fe78bSCy Schubert cc_int32 ccs_callback_reply_to_client (ccs_callback_t io_callback,
158*7f2fe78bSCy Schubert 				       k5_ipc_stream   in_stream)
159*7f2fe78bSCy Schubert {
160*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
161*7f2fe78bSCy Schubert 
162*7f2fe78bSCy Schubert     if (!io_callback) { err = cci_check_error (ccErrBadParam); }
163*7f2fe78bSCy Schubert 
164*7f2fe78bSCy Schubert     if (!err) {
165*7f2fe78bSCy Schubert         if (io_callback->pending) {
166*7f2fe78bSCy Schubert 	    cci_debug_printf ("%s: callback %p replying to client.", __FUNCTION__, io_callback);
167*7f2fe78bSCy Schubert 
168*7f2fe78bSCy Schubert             err = ccs_server_send_reply (io_callback->reply_pipe, err, in_stream);
169*7f2fe78bSCy Schubert 
170*7f2fe78bSCy Schubert             if (err) {
171*7f2fe78bSCy Schubert                 cci_debug_printf ("WARNING %s() called on a lock belonging to a dead client!",
172*7f2fe78bSCy Schubert                                   __FUNCTION__);
173*7f2fe78bSCy Schubert             }
174*7f2fe78bSCy Schubert 
175*7f2fe78bSCy Schubert             io_callback->pending = 0;
176*7f2fe78bSCy Schubert         } else {
177*7f2fe78bSCy Schubert             cci_debug_printf ("WARNING %s() called on non-pending callback!",
178*7f2fe78bSCy Schubert                               __FUNCTION__);
179*7f2fe78bSCy Schubert         }
180*7f2fe78bSCy Schubert     }
181*7f2fe78bSCy Schubert 
182*7f2fe78bSCy Schubert     return cci_check_error (err);
183*7f2fe78bSCy Schubert }
184*7f2fe78bSCy Schubert 
185*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
186*7f2fe78bSCy Schubert 
ccs_callback_is_pending(ccs_callback_t in_callback,cc_uint32 * out_pending)187*7f2fe78bSCy Schubert cc_uint32 ccs_callback_is_pending (ccs_callback_t  in_callback,
188*7f2fe78bSCy Schubert 				   cc_uint32      *out_pending)
189*7f2fe78bSCy Schubert {
190*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
191*7f2fe78bSCy Schubert 
192*7f2fe78bSCy Schubert     if (!in_callback) { err = cci_check_error (ccErrBadParam); }
193*7f2fe78bSCy Schubert     if (!out_pending) { err = cci_check_error (ccErrBadParam); }
194*7f2fe78bSCy Schubert 
195*7f2fe78bSCy Schubert     if (!err) {
196*7f2fe78bSCy Schubert         *out_pending = in_callback->pending;
197*7f2fe78bSCy Schubert     }
198*7f2fe78bSCy Schubert 
199*7f2fe78bSCy Schubert     return cci_check_error (err);
200*7f2fe78bSCy Schubert }
201*7f2fe78bSCy Schubert 
202*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
203*7f2fe78bSCy Schubert 
ccs_callback_is_for_client_pipe(ccs_callback_t in_callback,ccs_pipe_t in_client_pipe,cc_uint32 * out_is_for_client_pipe)204*7f2fe78bSCy Schubert cc_int32 ccs_callback_is_for_client_pipe (ccs_callback_t  in_callback,
205*7f2fe78bSCy Schubert 					  ccs_pipe_t      in_client_pipe,
206*7f2fe78bSCy Schubert 					  cc_uint32      *out_is_for_client_pipe)
207*7f2fe78bSCy Schubert {
208*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
209*7f2fe78bSCy Schubert 
210*7f2fe78bSCy Schubert     if (!in_callback                    ) { err = cci_check_error (ccErrBadParam); }
211*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_client_pipe)) { err = cci_check_error (ccErrBadParam); }
212*7f2fe78bSCy Schubert     if (!out_is_for_client_pipe         ) { err = cci_check_error (ccErrBadParam); }
213*7f2fe78bSCy Schubert 
214*7f2fe78bSCy Schubert     if (!err) {
215*7f2fe78bSCy Schubert         err = ccs_pipe_compare (in_callback->client_pipe, in_client_pipe,
216*7f2fe78bSCy Schubert                                 out_is_for_client_pipe);
217*7f2fe78bSCy Schubert     }
218*7f2fe78bSCy Schubert 
219*7f2fe78bSCy Schubert     return cci_check_error (err);
220*7f2fe78bSCy Schubert }
221*7f2fe78bSCy Schubert 
222*7f2fe78bSCy Schubert 
223*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
224*7f2fe78bSCy Schubert 
ccs_callback_client_pipe(ccs_callback_t in_callback,ccs_pipe_t * out_client_pipe)225*7f2fe78bSCy Schubert cc_int32 ccs_callback_client_pipe (ccs_callback_t  in_callback,
226*7f2fe78bSCy Schubert 				   ccs_pipe_t     *out_client_pipe)
227*7f2fe78bSCy Schubert {
228*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
229*7f2fe78bSCy Schubert 
230*7f2fe78bSCy Schubert     if (!in_callback    ) { err = cci_check_error (ccErrBadParam); }
231*7f2fe78bSCy Schubert     if (!out_client_pipe) { err = cci_check_error (ccErrBadParam); }
232*7f2fe78bSCy Schubert 
233*7f2fe78bSCy Schubert     if (!err) {
234*7f2fe78bSCy Schubert         *out_client_pipe = in_callback->client_pipe;
235*7f2fe78bSCy Schubert     }
236*7f2fe78bSCy Schubert 
237*7f2fe78bSCy Schubert     return cci_check_error (err);
238*7f2fe78bSCy Schubert }
239