@@ -81,8 +81,8 @@ static int send_simple_control_msg(struct lunatik_session *session, int command,
81
81
return err ;
82
82
}
83
83
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 )
86
86
{
87
87
struct nl_msg * msg ;
88
88
char * fragment ;
@@ -98,7 +98,7 @@ static int send_fragment(struct lunatik_session *session, const char *original_s
98
98
}
99
99
strncpy (fragment , original_script + (offset * LUNATIK_FRAGMENT_SIZE ), LUNATIK_FRAGMENT_SIZE );
100
100
101
- NLA_PUT_STRING (msg , STATE_NAME , state_name );
101
+ NLA_PUT_STRING (msg , STATE_NAME , state -> name );
102
102
NLA_PUT_STRING (msg , CODE , fragment );
103
103
104
104
if (offset == 0 )
@@ -109,7 +109,7 @@ static int send_fragment(struct lunatik_session *session, const char *original_s
109
109
110
110
NLA_PUT_U8 (msg , FLAGS , flags );
111
111
112
- if ((err = nl_send_auto (session -> control_sock , msg )) < 0 ) {
112
+ if ((err = nl_send_auto (state -> control_sock , msg )) < 0 ) {
113
113
printf ("Failed to send fragment\n %s\n" , nl_geterror (err ));
114
114
nlmsg_free (msg );
115
115
return err ;
@@ -125,7 +125,7 @@ static int send_fragment(struct lunatik_session *session, const char *original_s
125
125
return err ;
126
126
}
127
127
128
- static int receive_op_result (struct lunatik_session * session ){
128
+ static int receive_session_op_result (struct lunatik_session * session ){
129
129
int ret ;
130
130
131
131
if ((ret = nl_recvmsgs_default (session -> control_sock ))) {
@@ -143,6 +143,24 @@ static int receive_op_result(struct lunatik_session *session){
143
143
return 0 ;
144
144
}
145
145
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
+
146
164
int init_recv_datasocket_on_kernel (struct lunatik_nl_state * state )
147
165
{
148
166
struct nl_msg * msg ;
@@ -197,49 +215,49 @@ int lunatikS_newstate(struct lunatik_session *session, struct lunatik_nl_state *
197
215
return ret ;
198
216
}
199
217
200
- return receive_op_result (session );
218
+ return receive_session_op_result (session );
201
219
202
220
nla_put_failure :
203
221
printf ("Failed to put attributes on message\n" );
204
222
return ret ;
205
223
}
206
224
207
- int lunatikS_closestate (struct lunatik_nl_state * state )
225
+ int lunatik_closestate (struct lunatik_nl_state * state )
208
226
{
209
- struct lunatik_session * session ;
210
227
struct nl_msg * msg ;
211
228
int ret = -1 ;
212
229
213
- session = state -> session ;
214
-
215
230
if ((msg = prepare_message (DESTROY_STATE , 0 )) == NULL )
216
231
return ret ;
217
232
218
233
NLA_PUT_STRING (msg , STATE_NAME , state -> name );
219
234
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 ));
222
237
return ret ;
223
238
}
224
239
240
+ ret = receive_state_op_result (state );
241
+
225
242
nl_socket_free (state -> send_datasock );
226
243
nl_socket_free (state -> recv_datasock );
244
+ nl_socket_free (state -> control_sock );
227
245
228
- return receive_op_result ( session ) ;
246
+ return ret ;
229
247
230
248
nla_put_failure :
231
249
printf ("Failed to put attributes on netlink message\n" );
232
250
return ret ;
233
251
}
234
252
235
- int lunatikS_dostring (struct lunatik_session * session , const char * state_name ,
253
+ int lunatik_dostring (struct lunatik_nl_state * state ,
236
254
const char * script , const char * script_name , size_t total_code_size )
237
255
{
238
256
int err = -1 ;
239
257
int parts = 0 ;
240
258
241
259
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 );
243
261
if (err )
244
262
return err ;
245
263
} else {
@@ -249,22 +267,22 @@ int lunatikS_dostring(struct lunatik_session *session, const char *state_name,
249
267
250
268
for (int i = 0 ; i < parts - 1 ; i ++ ) {
251
269
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 );
253
271
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 );
255
273
256
- nl_wait_for_ack (session -> control_sock );
274
+ nl_wait_for_ack (state -> control_sock );
257
275
258
276
if (err )
259
277
return err ;
260
278
}
261
279
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 );
263
281
if (err )
264
282
return err ;
265
283
}
266
284
267
- return receive_op_result ( session );
285
+ return receive_state_op_result ( state );
268
286
}
269
287
270
288
int lunatikS_list (struct lunatik_session * session )
@@ -446,7 +464,6 @@ static int response_handler(struct nl_msg *msg, void *arg)
446
464
{
447
465
case CREATE_STATE :
448
466
case DESTROY_STATE :
449
- case EXECUTE_CODE :
450
467
if (attrs_tb [OP_SUCESS ] && nla_get_u8 (attrs_tb [OP_SUCESS ])) {
451
468
session -> cb_result = CB_SUCCESS ;
452
469
} 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)
510
527
return NL_OK ;
511
528
}
512
529
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
+
513
554
static int init_data_buffer (struct data_buffer * data_buffer , size_t size );
514
555
515
556
static int data_handler (struct nl_msg * msg , void * arg )
@@ -657,7 +698,7 @@ void release_data_buffer(struct data_buffer *data_buffer)
657
698
data_buffer -> size = 0 ;
658
699
}
659
700
660
- int lunatikS_receive (struct lunatik_nl_state * state )
701
+ int lunatik_receive (struct lunatik_nl_state * state )
661
702
{
662
703
int err = 0 ;
663
704
@@ -671,7 +712,7 @@ int lunatikS_receive(struct lunatik_nl_state *state)
671
712
return err ;
672
713
}
673
714
674
- int lunatikS_initdata (struct lunatik_nl_state * state )
715
+ static int lunatik_initdata (struct lunatik_nl_state * state )
675
716
{
676
717
int ret = 0 ;
677
718
@@ -712,7 +753,7 @@ struct lunatik_nl_state *lunatikS_getstate(struct lunatik_session *session, cons
712
753
return NULL ;
713
754
}
714
755
715
- if (receive_op_result (session ))
756
+ if (receive_session_op_result (session ))
716
757
return NULL ;
717
758
718
759
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
726
767
printf ("Failed to put attributes on netlink message\n" );
727
768
return NULL ;
728
769
}
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