Skip to content

Commit ca791dc

Browse files
committed
Change socket of states related operation to the state instead of session
1 parent 66fce3c commit ca791dc

File tree

7 files changed

+136
-47
lines changed

7 files changed

+136
-47
lines changed

lib/lunatik.c

+84-24
Original file line numberDiff line numberDiff line change
@@ -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+
146164
int 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

202220
nla_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

230248
nla_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

270288
int 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+
513554
static int init_data_buffer(struct data_buffer *data_buffer, size_t size);
514555

515556
static 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+
}

lib/lunatik.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct lunatik_nl_state {
4848
struct lunatik_session *session;
4949
struct nl_sock *send_datasock;
5050
struct nl_sock *recv_datasock;
51+
struct nl_sock *control_sock;
5152
struct data_buffer data_buffer;
5253
enum callback_result cb_result;
5354
uint32_t maxalloc;
@@ -94,16 +95,16 @@ void lunatikS_close(struct lunatik_session *session);
9495

9596
int lunatikS_newstate(struct lunatik_session *session, struct lunatik_nl_state *s);
9697

97-
int lunatikS_closestate(struct lunatik_nl_state *state);
98+
int lunatik_closestate(struct lunatik_nl_state *state);
9899

99-
int lunatikS_dostring(struct lunatik_session *session, const char *state_name,
100+
int lunatik_dostring(struct lunatik_nl_state *state,
100101
const char *script, const char *script_name, size_t total_code_size);
101102

102103
int lunatikS_list(struct lunatik_session *session);
103104

104-
int lunatikS_receive(struct lunatik_nl_state *state);
105+
int lunatik_receive(struct lunatik_nl_state *state);
105106

106-
int lunatikS_initdata(struct lunatik_nl_state *state);
107+
int lunatik_initstate(struct lunatik_nl_state *state);
107108

108109
struct lunatik_nl_state *lunatikS_getstate(struct lunatik_session *session, const char *name);
109110

lib/lunatik_module.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ static int lsession_newstate(lua_State *L)
141141
return 2;
142142
}
143143

144-
if (lunatikS_initdata(state)) {
145-
lua_pushnil(L);
144+
if (lunatik_initstate(state)) {
145+
pusherrmsg(L, "Failed to initialize state\n");
146146
return 1;
147147
}
148148

@@ -154,7 +154,7 @@ static int lsession_newstate(lua_State *L)
154154
static int lstate_close(lua_State *L)
155155
{
156156
struct lunatik_nl_state *state = getnlstate(L);
157-
if (lunatikS_closestate(state)){
157+
if (lunatik_closestate(state)){
158158
lua_pushnil(L);
159159
return 1;
160160
}
@@ -166,8 +166,6 @@ static int lstate_close(lua_State *L)
166166
static int lstate_dostring(lua_State *L)
167167
{
168168
struct lunatik_nl_state *s = getnlstate(L);
169-
struct lunatik_session *session = s->session;
170-
const char *name = s->name;
171169
size_t len;
172170
const char *payload = luaL_checklstring(L, 2, &len);
173171
const char *script_name = luaL_optstring(L, 3, "Lunatik");
@@ -176,7 +174,7 @@ static int lstate_dostring(lua_State *L)
176174
printf("script name too long\n");
177175
goto error;
178176
}
179-
int status = lunatikS_dostring(session, name, payload, script_name, len);
177+
int status = lunatik_dostring(s, payload, script_name, len);
180178

181179
if (status)
182180
goto error;
@@ -243,8 +241,8 @@ static int lsession_getstate(lua_State *L)
243241

244242
*state = *received_state;
245243

246-
if (lunatikS_initdata(state)) {
247-
lua_pushnil(L);
244+
if (lunatik_initstate(state)) {
245+
pusherrmsg(L, "Failed to initialize the state\n");
248246
return 1;
249247
}
250248

@@ -292,7 +290,7 @@ static int lstate_datareceive(lua_State *L)
292290
struct lunatik_nl_state *state = getnlstate(L);
293291
char *memory;
294292

295-
if (lunatikS_receive(state))
293+
if (lunatik_receive(state))
296294
goto error;
297295

298296
memory = luamem_newalloc(L, state->data_buffer.size);

lib/tests/close.lua

+14
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ assert(s1 ~= nil)
77

88
local err = s1:close()
99
assert(err ~= nil)
10+
11+
-- Using state after close session
12+
s1 = session:new's1'
13+
assert(s1 ~= nil)
14+
15+
session:close()
16+
assert(s1:getname() == 's1')
17+
18+
err = s1:close()
19+
assert(err)
20+
21+
-- Trying to close nonexistent state
22+
err = s1:close()
23+
assert(err == nil)

lib/tests/getstate.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ local kscript = [[
77
print'olá mundo!'
88
]]
99

10+
-- Testing state normal state creation
1011
local s1 = session:new's1'
1112
assert(s1 ~= nil)
1213
local err = s1:dostring(kscript)
1314
assert(err ~= nil)
1415

16+
-- Trying to get a state created on user space
1517
local state = session:getstate's1'
16-
assert(state ~= nil)
17-
err = state:dostring(kscript)
18-
assert(err ~= nil)
18+
assert(state == nil)
1919

20+
-- Try to get a state nonexistent state
2021
local state2 = session:getstate's4'
2122
assert(state2 == nil)
2223

2324
s1:close()
24-
state:close()
2525

2626
session:close()

lunatik.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct lunatik_state {
4747
size_t maxalloc;
4848
size_t curralloc;
4949
size_t scriptsize;
50+
bool inuse;
5051
unsigned char name[LUNATIK_NAME_MAXSIZE];
5152
} lunatik_State;
5253

0 commit comments

Comments
 (0)