@@ -81,8 +81,8 @@ static int send_simple_control_msg(struct lunatik_session *session, int command,
8181 return err ;
8282}
8383
84- static int send_fragment (struct lunatik_session * session , const char * original_script , int offset ,
85- const char * state_name , const char * script_name , int flags )
84+ static int send_fragment (struct lunatik_nl_state * state , const char * original_script , int offset ,
85+ const char * script_name , int flags )
8686{
8787 struct nl_msg * msg ;
8888 char * fragment ;
@@ -98,7 +98,7 @@ static int send_fragment(struct lunatik_session *session, const char *original_s
9898 }
9999 strncpy (fragment , original_script + (offset * LUNATIK_FRAGMENT_SIZE ), LUNATIK_FRAGMENT_SIZE );
100100
101- NLA_PUT_STRING (msg , STATE_NAME , state_name );
101+ NLA_PUT_STRING (msg , STATE_NAME , state -> name );
102102 NLA_PUT_STRING (msg , CODE , fragment );
103103
104104 if (offset == 0 )
@@ -109,7 +109,7 @@ static int send_fragment(struct lunatik_session *session, const char *original_s
109109
110110 NLA_PUT_U8 (msg , FLAGS , flags );
111111
112- if ((err = nl_send_auto (session -> control_sock , msg )) < 0 ) {
112+ if ((err = nl_send_auto (state -> control_sock , msg )) < 0 ) {
113113 printf ("Failed to send fragment\n %s\n" , nl_geterror (err ));
114114 nlmsg_free (msg );
115115 return err ;
@@ -125,7 +125,7 @@ static int send_fragment(struct lunatik_session *session, const char *original_s
125125 return err ;
126126}
127127
128- static int receive_op_result (struct lunatik_session * session ){
128+ static int receive_session_op_result (struct lunatik_session * session ){
129129 int ret ;
130130
131131 if ((ret = nl_recvmsgs_default (session -> control_sock ))) {
@@ -143,6 +143,24 @@ static int receive_op_result(struct lunatik_session *session){
143143 return 0 ;
144144}
145145
146+ static int receive_state_op_result (struct lunatik_nl_state * state ){
147+ int ret ;
148+
149+ if ((ret = nl_recvmsgs_default (state -> control_sock ))) {
150+ printf ("Failed to receive message from kernel: %s\n" , nl_geterror (ret ));
151+ return ret ;
152+ }
153+
154+ nl_wait_for_ack (state -> control_sock );
155+
156+ if (state -> cb_result == CB_ERROR ){
157+ state -> cb_result = CB_EMPTY_RESULT ;
158+ return -1 ;
159+ }
160+
161+ return 0 ;
162+ }
163+
146164int init_recv_datasocket_on_kernel (struct lunatik_nl_state * state )
147165{
148166 struct nl_msg * msg ;
@@ -197,49 +215,49 @@ int lunatikS_newstate(struct lunatik_session *session, struct lunatik_nl_state *
197215 return ret ;
198216 }
199217
200- return receive_op_result (session );
218+ return receive_session_op_result (session );
201219
202220nla_put_failure :
203221 printf ("Failed to put attributes on message\n" );
204222 return ret ;
205223}
206224
207- int lunatikS_closestate (struct lunatik_nl_state * state )
225+ int lunatik_closestate (struct lunatik_nl_state * state )
208226{
209- struct lunatik_session * session ;
210227 struct nl_msg * msg ;
211228 int ret = -1 ;
212229
213- session = state -> session ;
214-
215230 if ((msg = prepare_message (DESTROY_STATE , 0 )) == NULL )
216231 return ret ;
217232
218233 NLA_PUT_STRING (msg , STATE_NAME , state -> name );
219234
220- if ((ret = nl_send_auto (session -> control_sock , msg )) < 0 ) {
221- printf ("Failed to send destroy message:\n %s\n" , nl_geterror (ret ));
235+ if ((ret = nl_send_auto (state -> control_sock , msg )) < 0 ) {
236+ printf ("Failed to send destroy message:\n\t %s\n" , nl_geterror (ret ));
222237 return ret ;
223238 }
224239
240+ ret = receive_state_op_result (state );
241+
225242 nl_socket_free (state -> send_datasock );
226243 nl_socket_free (state -> recv_datasock );
244+ nl_socket_free (state -> control_sock );
227245
228- return receive_op_result ( session ) ;
246+ return ret ;
229247
230248nla_put_failure :
231249 printf ("Failed to put attributes on netlink message\n" );
232250 return ret ;
233251}
234252
235- int lunatikS_dostring (struct lunatik_session * session , const char * state_name ,
253+ int lunatik_dostring (struct lunatik_nl_state * state ,
236254 const char * script , const char * script_name , size_t total_code_size )
237255{
238256 int err = -1 ;
239257 int parts = 0 ;
240258
241259 if (total_code_size <= LUNATIK_FRAGMENT_SIZE ) {
242- err = send_fragment (session , script , 0 , state_name , script_name , LUNATIK_INIT | LUNATIK_DONE );
260+ err = send_fragment (state , script , 0 , script_name , LUNATIK_INIT | LUNATIK_DONE );
243261 if (err )
244262 return err ;
245263 } else {
@@ -249,22 +267,22 @@ int lunatikS_dostring(struct lunatik_session *session, const char *state_name,
249267
250268 for (int i = 0 ; i < parts - 1 ; i ++ ) {
251269 if (i == 0 )
252- err = send_fragment (session , script , i , state_name , script_name , LUNATIK_INIT | LUNATIK_MULTI );
270+ err = send_fragment (state , script , i , script_name , LUNATIK_INIT | LUNATIK_MULTI );
253271 else
254- err = send_fragment (session , script , i , state_name , script_name , LUNATIK_MULTI );
272+ err = send_fragment (state , script , i , script_name , LUNATIK_MULTI );
255273
256- nl_wait_for_ack (session -> control_sock );
274+ nl_wait_for_ack (state -> control_sock );
257275
258276 if (err )
259277 return err ;
260278 }
261279
262- err = send_fragment (session , script , parts - 1 , state_name , script_name , LUNATIK_DONE );
280+ err = send_fragment (state , script , parts - 1 , script_name , LUNATIK_DONE );
263281 if (err )
264282 return err ;
265283 }
266284
267- return receive_op_result ( session );
285+ return receive_state_op_result ( state );
268286}
269287
270288int lunatikS_list (struct lunatik_session * session )
@@ -446,7 +464,6 @@ static int response_handler(struct nl_msg *msg, void *arg)
446464 {
447465 case CREATE_STATE :
448466 case DESTROY_STATE :
449- case EXECUTE_CODE :
450467 if (attrs_tb [OP_SUCESS ] && nla_get_u8 (attrs_tb [OP_SUCESS ])) {
451468 session -> cb_result = CB_SUCCESS ;
452469 } else if (attrs_tb [OP_ERROR ] && nla_get_u8 (attrs_tb [OP_ERROR ])) {
@@ -510,6 +527,30 @@ static int response_handler(struct nl_msg *msg, void *arg)
510527 return NL_OK ;
511528}
512529
530+ static int response_state_handler (struct nl_msg * msg , void * arg )
531+ {
532+ struct nlmsghdr * nh = nlmsg_hdr (msg );
533+ struct genlmsghdr * gnlh = genlmsg_hdr (nh );
534+ struct nlattr * attrs_tb [ATTRS_COUNT + 1 ];
535+ struct lunatik_nl_state * state = (struct lunatik_nl_state * )arg ;
536+
537+ if (nla_parse (attrs_tb , ATTRS_COUNT , genlmsg_attrdata (gnlh , 0 ),
538+ genlmsg_attrlen (gnlh , 0 ), NULL ))
539+ {
540+ printf ("Error parsing attributes\n" );
541+ state -> cb_result = CB_ERROR ;
542+ return NL_OK ;
543+ }
544+
545+ if (attrs_tb [OP_SUCESS ] && nla_get_u8 (attrs_tb [OP_SUCESS ])) {
546+ state -> cb_result = CB_SUCCESS ;
547+ } else if (attrs_tb [OP_ERROR ] && nla_get_u8 (attrs_tb [OP_ERROR ])) {
548+ state -> cb_result = CB_ERROR ;
549+ }
550+
551+ return NL_OK ;
552+ }
553+
513554static int init_data_buffer (struct data_buffer * data_buffer , size_t size );
514555
515556static int data_handler (struct nl_msg * msg , void * arg )
@@ -657,7 +698,7 @@ void release_data_buffer(struct data_buffer *data_buffer)
657698 data_buffer -> size = 0 ;
658699}
659700
660- int lunatikS_receive (struct lunatik_nl_state * state )
701+ int lunatik_receive (struct lunatik_nl_state * state )
661702{
662703 int err = 0 ;
663704
@@ -671,7 +712,7 @@ int lunatikS_receive(struct lunatik_nl_state *state)
671712 return err ;
672713}
673714
674- int lunatikS_initdata (struct lunatik_nl_state * state )
715+ static int lunatik_initdata (struct lunatik_nl_state * state )
675716{
676717 int ret = 0 ;
677718
@@ -712,7 +753,7 @@ struct lunatik_nl_state *lunatikS_getstate(struct lunatik_session *session, cons
712753 return NULL ;
713754 }
714755
715- if (receive_op_result (session ))
756+ if (receive_session_op_result (session ))
716757 return NULL ;
717758
718759 if ((session -> cb_result == CB_STATE_NOT_FOUND ) || (session -> cb_result == CB_ERROR )) {
@@ -726,3 +767,22 @@ struct lunatik_nl_state *lunatikS_getstate(struct lunatik_session *session, cons
726767 printf ("Failed to put attributes on netlink message\n" );
727768 return NULL ;
728769}
770+
771+ int lunatik_initstate (struct lunatik_nl_state * state )
772+ {
773+ int err ;
774+
775+ if ((err = lunatik_initdata (state ))) {
776+ return err ;
777+ }
778+
779+ if ((err = init_socket (& state -> control_sock ))) {
780+ printf ("Failed to initialize the control socket for state %s\n" , state -> name );
781+ nl_socket_free (state -> control_sock );
782+ return err ;
783+ }
784+
785+ nl_socket_modify_cb (state -> control_sock , NL_CB_MSG_IN , NL_CB_CUSTOM , response_state_handler , state );
786+
787+ return 0 ;
788+ }
0 commit comments